obs-video-gpu-encode.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. /******************************************************************************
  2. Copyright (C) 2023 by Lain Bailey <[email protected]>
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 2 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. ******************************************************************************/
  14. #include "obs-internal.h"
  15. static void *gpu_encode_thread(struct obs_core_video_mix *video)
  16. {
  17. uint64_t interval = video_output_get_frame_time(video->video);
  18. DARRAY(obs_encoder_t *) encoders;
  19. int wait_frames = NUM_ENCODE_TEXTURE_FRAMES_TO_WAIT;
  20. da_init(encoders);
  21. os_set_thread_name("obs gpu encode thread");
  22. while (os_sem_wait(video->gpu_encode_semaphore) == 0) {
  23. struct obs_tex_frame tf;
  24. uint64_t timestamp;
  25. uint64_t lock_key;
  26. uint64_t next_key;
  27. size_t lock_count = 0;
  28. if (os_atomic_load_bool(&video->gpu_encode_stop))
  29. break;
  30. if (wait_frames) {
  31. wait_frames--;
  32. continue;
  33. }
  34. os_event_reset(video->gpu_encode_inactive);
  35. /* -------------- */
  36. pthread_mutex_lock(&video->gpu_encoder_mutex);
  37. circlebuf_pop_front(&video->gpu_encoder_queue, &tf, sizeof(tf));
  38. timestamp = tf.timestamp;
  39. lock_key = tf.lock_key;
  40. next_key = tf.lock_key;
  41. video_output_inc_texture_frames(video->video);
  42. for (size_t i = 0; i < video->gpu_encoders.num; i++) {
  43. obs_encoder_t *encoder = obs_encoder_get_ref(
  44. video->gpu_encoders.array[i]);
  45. if (encoder)
  46. da_push_back(encoders, &encoder);
  47. }
  48. pthread_mutex_unlock(&video->gpu_encoder_mutex);
  49. /* -------------- */
  50. for (size_t i = 0; i < encoders.num; i++) {
  51. struct encoder_packet pkt = {0};
  52. bool received = false;
  53. bool success;
  54. uint32_t skip = 0;
  55. obs_encoder_t *encoder = encoders.array[i];
  56. struct obs_encoder *pair = encoder->paired_encoder;
  57. pkt.timebase_num = encoder->timebase_num *
  58. encoder->frame_rate_divisor;
  59. pkt.timebase_den = encoder->timebase_den;
  60. pkt.encoder = encoder;
  61. if (!encoder->first_received && pair) {
  62. if (!pair->first_received ||
  63. pair->first_raw_ts > timestamp) {
  64. continue;
  65. }
  66. }
  67. if (video_pause_check(&encoder->pause, timestamp))
  68. continue;
  69. if (encoder->reconfigure_requested) {
  70. encoder->reconfigure_requested = false;
  71. encoder->info.update(encoder->context.data,
  72. encoder->context.settings);
  73. }
  74. // an explicit counter is used instead of remainder calculation
  75. // to allow multiple encoders started at the same time to start on
  76. // the same frame
  77. skip = encoder->frame_rate_divisor_counter++;
  78. if (encoder->frame_rate_divisor_counter ==
  79. encoder->frame_rate_divisor)
  80. encoder->frame_rate_divisor_counter = 0;
  81. if (skip)
  82. continue;
  83. if (!encoder->start_ts)
  84. encoder->start_ts = timestamp;
  85. if (++lock_count == encoders.num)
  86. next_key = 0;
  87. else
  88. next_key++;
  89. success = encoder->info.encode_texture(
  90. encoder->context.data, tf.handle,
  91. encoder->cur_pts, lock_key, &next_key, &pkt,
  92. &received);
  93. send_off_encoder_packet(encoder, success, received,
  94. &pkt);
  95. lock_key = next_key;
  96. encoder->cur_pts += encoder->timebase_num *
  97. encoder->frame_rate_divisor;
  98. }
  99. /* -------------- */
  100. pthread_mutex_lock(&video->gpu_encoder_mutex);
  101. tf.lock_key = next_key;
  102. if (--tf.count) {
  103. tf.timestamp += interval;
  104. circlebuf_push_front(&video->gpu_encoder_queue, &tf,
  105. sizeof(tf));
  106. video_output_inc_texture_skipped_frames(video->video);
  107. } else {
  108. circlebuf_push_back(&video->gpu_encoder_avail_queue,
  109. &tf, sizeof(tf));
  110. }
  111. pthread_mutex_unlock(&video->gpu_encoder_mutex);
  112. /* -------------- */
  113. os_event_signal(video->gpu_encode_inactive);
  114. for (size_t i = 0; i < encoders.num; i++)
  115. obs_encoder_release(encoders.array[i]);
  116. da_resize(encoders, 0);
  117. }
  118. da_free(encoders);
  119. return NULL;
  120. }
  121. bool init_gpu_encoding(struct obs_core_video_mix *video)
  122. {
  123. #ifdef _WIN32
  124. const struct video_output_info *info =
  125. video_output_get_info(video->video);
  126. video->gpu_encode_stop = false;
  127. circlebuf_reserve(&video->gpu_encoder_avail_queue, NUM_ENCODE_TEXTURES);
  128. for (size_t i = 0; i < NUM_ENCODE_TEXTURES; i++) {
  129. gs_texture_t *tex;
  130. gs_texture_t *tex_uv;
  131. if (info->format == VIDEO_FORMAT_P010) {
  132. gs_texture_create_p010(
  133. &tex, &tex_uv, info->width, info->height,
  134. GS_RENDER_TARGET | GS_SHARED_KM_TEX);
  135. } else {
  136. gs_texture_create_nv12(
  137. &tex, &tex_uv, info->width, info->height,
  138. GS_RENDER_TARGET | GS_SHARED_KM_TEX);
  139. }
  140. if (!tex) {
  141. return false;
  142. }
  143. uint32_t handle = gs_texture_get_shared_handle(tex);
  144. struct obs_tex_frame frame = {.tex = tex,
  145. .tex_uv = tex_uv,
  146. .handle = handle};
  147. circlebuf_push_back(&video->gpu_encoder_avail_queue, &frame,
  148. sizeof(frame));
  149. }
  150. if (os_sem_init(&video->gpu_encode_semaphore, 0) != 0)
  151. return false;
  152. if (os_event_init(&video->gpu_encode_inactive, OS_EVENT_TYPE_MANUAL) !=
  153. 0)
  154. return false;
  155. if (pthread_create(&video->gpu_encode_thread, NULL, gpu_encode_thread,
  156. video) != 0)
  157. return false;
  158. os_event_signal(video->gpu_encode_inactive);
  159. video->gpu_encode_thread_initialized = true;
  160. return true;
  161. #else
  162. UNUSED_PARAMETER(video);
  163. return false;
  164. #endif
  165. }
  166. void stop_gpu_encoding_thread(struct obs_core_video_mix *video)
  167. {
  168. if (video->gpu_encode_thread_initialized) {
  169. os_atomic_set_bool(&video->gpu_encode_stop, true);
  170. os_sem_post(video->gpu_encode_semaphore);
  171. pthread_join(video->gpu_encode_thread, NULL);
  172. video->gpu_encode_thread_initialized = false;
  173. }
  174. }
  175. void free_gpu_encoding(struct obs_core_video_mix *video)
  176. {
  177. if (video->gpu_encode_semaphore) {
  178. os_sem_destroy(video->gpu_encode_semaphore);
  179. video->gpu_encode_semaphore = NULL;
  180. }
  181. if (video->gpu_encode_inactive) {
  182. os_event_destroy(video->gpu_encode_inactive);
  183. video->gpu_encode_inactive = NULL;
  184. }
  185. #define free_circlebuf(x) \
  186. do { \
  187. while (x.size) { \
  188. struct obs_tex_frame frame; \
  189. circlebuf_pop_front(&x, &frame, sizeof(frame)); \
  190. gs_texture_destroy(frame.tex); \
  191. gs_texture_destroy(frame.tex_uv); \
  192. } \
  193. circlebuf_free(&x); \
  194. } while (false)
  195. free_circlebuf(video->gpu_encoder_queue);
  196. free_circlebuf(video->gpu_encoder_avail_queue);
  197. #undef free_circlebuf
  198. }