obs-video-gpu-encode.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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(void *unused)
  16. {
  17. struct obs_core_video *video = &obs->video;
  18. uint64_t interval = video_output_get_frame_time(obs->video.video);
  19. DARRAY(obs_encoder_t *) encoders;
  20. int wait_frames = NUM_ENCODE_TEXTURE_FRAMES_TO_WAIT;
  21. UNUSED_PARAMETER(unused);
  22. da_init(encoders);
  23. os_set_thread_name("obs gpu encode thread");
  24. while (os_sem_wait(video->gpu_encode_semaphore) == 0) {
  25. struct obs_tex_frame tf;
  26. uint64_t timestamp;
  27. uint64_t lock_key;
  28. uint64_t next_key;
  29. size_t lock_count = 0;
  30. if (os_atomic_load_bool(&video->gpu_encode_stop))
  31. break;
  32. if (wait_frames) {
  33. wait_frames--;
  34. continue;
  35. }
  36. os_event_reset(video->gpu_encode_inactive);
  37. /* -------------- */
  38. pthread_mutex_lock(&video->gpu_encoder_mutex);
  39. circlebuf_pop_front(&video->gpu_encoder_queue, &tf, sizeof(tf));
  40. timestamp = tf.timestamp;
  41. lock_key = tf.lock_key;
  42. next_key = tf.lock_key;
  43. video_output_inc_texture_frames(video->video);
  44. for (size_t i = 0; i < video->gpu_encoders.num; i++) {
  45. obs_encoder_t *encoder = obs_encoder_get_ref(
  46. video->gpu_encoders.array[i]);
  47. if (encoder)
  48. da_push_back(encoders, &encoder);
  49. }
  50. pthread_mutex_unlock(&video->gpu_encoder_mutex);
  51. /* -------------- */
  52. for (size_t i = 0; i < encoders.num; i++) {
  53. struct encoder_packet pkt = {0};
  54. bool received = false;
  55. bool success;
  56. obs_encoder_t *encoder = encoders.array[i];
  57. struct obs_encoder *pair = encoder->paired_encoder;
  58. pkt.timebase_num = encoder->timebase_num;
  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. if (!encoder->start_ts)
  75. encoder->start_ts = timestamp;
  76. if (++lock_count == encoders.num)
  77. next_key = 0;
  78. else
  79. next_key++;
  80. success = encoder->info.encode_texture(
  81. encoder->context.data, tf.handle,
  82. encoder->cur_pts, lock_key, &next_key, &pkt,
  83. &received);
  84. send_off_encoder_packet(encoder, success, received,
  85. &pkt);
  86. lock_key = next_key;
  87. encoder->cur_pts += encoder->timebase_num;
  88. }
  89. /* -------------- */
  90. pthread_mutex_lock(&video->gpu_encoder_mutex);
  91. tf.lock_key = next_key;
  92. if (--tf.count) {
  93. tf.timestamp += interval;
  94. circlebuf_push_front(&video->gpu_encoder_queue, &tf,
  95. sizeof(tf));
  96. video_output_inc_texture_skipped_frames(video->video);
  97. } else {
  98. circlebuf_push_back(&video->gpu_encoder_avail_queue,
  99. &tf, sizeof(tf));
  100. }
  101. pthread_mutex_unlock(&video->gpu_encoder_mutex);
  102. /* -------------- */
  103. os_event_signal(video->gpu_encode_inactive);
  104. for (size_t i = 0; i < encoders.num; i++)
  105. obs_encoder_release(encoders.array[i]);
  106. da_resize(encoders, 0);
  107. }
  108. da_free(encoders);
  109. return NULL;
  110. }
  111. bool init_gpu_encoding(struct obs_core_video *video)
  112. {
  113. #ifdef _WIN32
  114. struct obs_video_info *ovi = &video->ovi;
  115. video->gpu_encode_stop = false;
  116. circlebuf_reserve(&video->gpu_encoder_avail_queue, NUM_ENCODE_TEXTURES);
  117. for (size_t i = 0; i < NUM_ENCODE_TEXTURES; i++) {
  118. gs_texture_t *tex;
  119. gs_texture_t *tex_uv;
  120. if (ovi->output_format == VIDEO_FORMAT_P010) {
  121. gs_texture_create_p010(&tex, &tex_uv, ovi->output_width,
  122. ovi->output_height,
  123. GS_RENDER_TARGET |
  124. GS_SHARED_KM_TEX);
  125. } else {
  126. gs_texture_create_nv12(&tex, &tex_uv, ovi->output_width,
  127. ovi->output_height,
  128. GS_RENDER_TARGET |
  129. GS_SHARED_KM_TEX);
  130. }
  131. if (!tex) {
  132. return false;
  133. }
  134. uint32_t handle = gs_texture_get_shared_handle(tex);
  135. struct obs_tex_frame frame = {
  136. .tex = tex, .tex_uv = tex_uv, .handle = handle};
  137. circlebuf_push_back(&video->gpu_encoder_avail_queue, &frame,
  138. sizeof(frame));
  139. }
  140. if (os_sem_init(&video->gpu_encode_semaphore, 0) != 0)
  141. return false;
  142. if (os_event_init(&video->gpu_encode_inactive, OS_EVENT_TYPE_MANUAL) !=
  143. 0)
  144. return false;
  145. if (pthread_create(&video->gpu_encode_thread, NULL, gpu_encode_thread,
  146. NULL) != 0)
  147. return false;
  148. os_event_signal(video->gpu_encode_inactive);
  149. video->gpu_encode_thread_initialized = true;
  150. return true;
  151. #else
  152. UNUSED_PARAMETER(video);
  153. return false;
  154. #endif
  155. }
  156. void stop_gpu_encoding_thread(struct obs_core_video *video)
  157. {
  158. if (video->gpu_encode_thread_initialized) {
  159. os_atomic_set_bool(&video->gpu_encode_stop, true);
  160. os_sem_post(video->gpu_encode_semaphore);
  161. pthread_join(video->gpu_encode_thread, NULL);
  162. video->gpu_encode_thread_initialized = false;
  163. }
  164. }
  165. void free_gpu_encoding(struct obs_core_video *video)
  166. {
  167. if (video->gpu_encode_semaphore) {
  168. os_sem_destroy(video->gpu_encode_semaphore);
  169. video->gpu_encode_semaphore = NULL;
  170. }
  171. if (video->gpu_encode_inactive) {
  172. os_event_destroy(video->gpu_encode_inactive);
  173. video->gpu_encode_inactive = NULL;
  174. }
  175. #define free_circlebuf(x) \
  176. do { \
  177. while (x.size) { \
  178. struct obs_tex_frame frame; \
  179. circlebuf_pop_front(&x, &frame, sizeof(frame)); \
  180. gs_texture_destroy(frame.tex); \
  181. gs_texture_destroy(frame.tex_uv); \
  182. } \
  183. circlebuf_free(&x); \
  184. } while (false)
  185. free_circlebuf(video->gpu_encoder_queue);
  186. free_circlebuf(video->gpu_encoder_avail_queue);
  187. #undef free_circlebuf
  188. }