1
0

obs-video-gpu-encode.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. /******************************************************************************
  2. Copyright (C) 2018 by Hugh 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. obs_encoder_t *encoder = encoders.array[i];
  55. struct obs_encoder *pair = encoder->paired_encoder;
  56. pkt.timebase_num = encoder->timebase_num;
  57. pkt.timebase_den = encoder->timebase_den;
  58. pkt.encoder = encoder;
  59. if (!encoder->first_received && pair) {
  60. if (!pair->first_received ||
  61. pair->first_raw_ts > timestamp) {
  62. continue;
  63. }
  64. }
  65. if (video_pause_check(&encoder->pause, timestamp))
  66. continue;
  67. if (encoder->reconfigure_requested) {
  68. encoder->reconfigure_requested = false;
  69. encoder->info.update(encoder->context.data,
  70. encoder->context.settings);
  71. }
  72. if (!encoder->start_ts)
  73. encoder->start_ts = timestamp;
  74. if (++lock_count == encoders.num)
  75. next_key = 0;
  76. else
  77. next_key++;
  78. success = encoder->info.encode_texture(
  79. encoder->context.data, tf.handle,
  80. encoder->cur_pts, lock_key, &next_key, &pkt,
  81. &received);
  82. send_off_encoder_packet(encoder, success, received,
  83. &pkt);
  84. lock_key = next_key;
  85. encoder->cur_pts += encoder->timebase_num;
  86. }
  87. /* -------------- */
  88. pthread_mutex_lock(&video->gpu_encoder_mutex);
  89. tf.lock_key = next_key;
  90. if (--tf.count) {
  91. tf.timestamp += interval;
  92. circlebuf_push_front(&video->gpu_encoder_queue, &tf,
  93. sizeof(tf));
  94. video_output_inc_texture_skipped_frames(video->video);
  95. } else {
  96. circlebuf_push_back(&video->gpu_encoder_avail_queue,
  97. &tf, sizeof(tf));
  98. }
  99. pthread_mutex_unlock(&video->gpu_encoder_mutex);
  100. /* -------------- */
  101. os_event_signal(video->gpu_encode_inactive);
  102. for (size_t i = 0; i < encoders.num; i++)
  103. obs_encoder_release(encoders.array[i]);
  104. da_resize(encoders, 0);
  105. }
  106. da_free(encoders);
  107. return NULL;
  108. }
  109. bool init_gpu_encoding(struct obs_core_video_mix *video)
  110. {
  111. #ifdef _WIN32
  112. const struct video_output_info *info =
  113. video_output_get_info(video->video);
  114. video->gpu_encode_stop = false;
  115. circlebuf_reserve(&video->gpu_encoder_avail_queue, NUM_ENCODE_TEXTURES);
  116. for (size_t i = 0; i < NUM_ENCODE_TEXTURES; i++) {
  117. gs_texture_t *tex;
  118. gs_texture_t *tex_uv;
  119. if (info->format == VIDEO_FORMAT_P010) {
  120. gs_texture_create_p010(
  121. &tex, &tex_uv, info->width, info->height,
  122. GS_RENDER_TARGET | GS_SHARED_KM_TEX);
  123. } else {
  124. gs_texture_create_nv12(
  125. &tex, &tex_uv, info->width, info->height,
  126. GS_RENDER_TARGET | GS_SHARED_KM_TEX);
  127. }
  128. if (!tex) {
  129. return false;
  130. }
  131. uint32_t handle = gs_texture_get_shared_handle(tex);
  132. struct obs_tex_frame frame = {
  133. .tex = tex, .tex_uv = tex_uv, .handle = handle};
  134. circlebuf_push_back(&video->gpu_encoder_avail_queue, &frame,
  135. sizeof(frame));
  136. }
  137. if (os_sem_init(&video->gpu_encode_semaphore, 0) != 0)
  138. return false;
  139. if (os_event_init(&video->gpu_encode_inactive, OS_EVENT_TYPE_MANUAL) !=
  140. 0)
  141. return false;
  142. if (pthread_create(&video->gpu_encode_thread, NULL, gpu_encode_thread,
  143. video) != 0)
  144. return false;
  145. os_event_signal(video->gpu_encode_inactive);
  146. video->gpu_encode_thread_initialized = true;
  147. return true;
  148. #else
  149. UNUSED_PARAMETER(video);
  150. return false;
  151. #endif
  152. }
  153. void stop_gpu_encoding_thread(struct obs_core_video_mix *video)
  154. {
  155. if (video->gpu_encode_thread_initialized) {
  156. os_atomic_set_bool(&video->gpu_encode_stop, true);
  157. os_sem_post(video->gpu_encode_semaphore);
  158. pthread_join(video->gpu_encode_thread, NULL);
  159. video->gpu_encode_thread_initialized = false;
  160. }
  161. }
  162. void free_gpu_encoding(struct obs_core_video_mix *video)
  163. {
  164. if (video->gpu_encode_semaphore) {
  165. os_sem_destroy(video->gpu_encode_semaphore);
  166. video->gpu_encode_semaphore = NULL;
  167. }
  168. if (video->gpu_encode_inactive) {
  169. os_event_destroy(video->gpu_encode_inactive);
  170. video->gpu_encode_inactive = NULL;
  171. }
  172. #define free_circlebuf(x) \
  173. do { \
  174. while (x.size) { \
  175. struct obs_tex_frame frame; \
  176. circlebuf_pop_front(&x, &frame, sizeof(frame)); \
  177. gs_texture_destroy(frame.tex); \
  178. gs_texture_destroy(frame.tex_uv); \
  179. } \
  180. circlebuf_free(&x); \
  181. } while (false)
  182. free_circlebuf(video->gpu_encoder_queue);
  183. free_circlebuf(video->gpu_encoder_avail_queue);
  184. #undef free_circlebuf
  185. }