obs-video-gpu-encode.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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. #define NBSP "\xC2\xA0"
  16. static const char *gpu_encode_frame_name = "gpu_encode_frame";
  17. static void *gpu_encode_thread(void *data)
  18. {
  19. struct obs_core_video_mix *video = data;
  20. uint64_t interval = video_output_get_frame_time(video->video);
  21. DARRAY(obs_encoder_t *) encoders;
  22. int wait_frames = NUM_ENCODE_TEXTURE_FRAMES_TO_WAIT;
  23. da_init(encoders);
  24. os_set_thread_name("obs gpu encode thread");
  25. const char *gpu_encode_thread_name = profile_store_name(
  26. obs_get_profiler_name_store(),
  27. "obs_gpu_encode_thread(%g" NBSP "ms)", interval / 1000000.);
  28. profile_register_root(gpu_encode_thread_name, interval);
  29. while (os_sem_wait(video->gpu_encode_semaphore) == 0) {
  30. struct obs_tex_frame tf;
  31. uint64_t timestamp;
  32. uint64_t lock_key;
  33. uint64_t next_key;
  34. size_t lock_count = 0;
  35. if (os_atomic_load_bool(&video->gpu_encode_stop))
  36. break;
  37. if (wait_frames) {
  38. wait_frames--;
  39. continue;
  40. }
  41. profile_start(gpu_encode_thread_name);
  42. os_event_reset(video->gpu_encode_inactive);
  43. /* -------------- */
  44. pthread_mutex_lock(&video->gpu_encoder_mutex);
  45. deque_pop_front(&video->gpu_encoder_queue, &tf, sizeof(tf));
  46. timestamp = tf.timestamp;
  47. lock_key = tf.lock_key;
  48. next_key = tf.lock_key;
  49. video_output_inc_texture_frames(video->video);
  50. for (size_t i = 0; i < video->gpu_encoders.num; i++) {
  51. obs_encoder_t *encoder = obs_encoder_get_ref(
  52. video->gpu_encoders.array[i]);
  53. if (encoder)
  54. da_push_back(encoders, &encoder);
  55. }
  56. pthread_mutex_unlock(&video->gpu_encoder_mutex);
  57. /* -------------- */
  58. for (size_t i = 0; i < encoders.num; i++) {
  59. struct encoder_packet pkt = {0};
  60. bool received = false;
  61. bool success = false;
  62. uint32_t skip = 0;
  63. obs_encoder_t *encoder = encoders.array[i];
  64. struct obs_encoder **paired =
  65. encoder->paired_encoders.array;
  66. size_t num_paired = encoder->paired_encoders.num;
  67. pkt.timebase_num = encoder->timebase_num *
  68. encoder->frame_rate_divisor;
  69. pkt.timebase_den = encoder->timebase_den;
  70. pkt.encoder = encoder;
  71. if (!encoder->first_received && num_paired) {
  72. bool wait_for_audio = false;
  73. for (size_t idx = 0; idx < num_paired; idx++) {
  74. if (!paired[idx]->first_received ||
  75. paired[idx]->first_raw_ts >
  76. timestamp) {
  77. wait_for_audio = true;
  78. break;
  79. }
  80. }
  81. if (wait_for_audio)
  82. continue;
  83. }
  84. if (video_pause_check(&encoder->pause, timestamp))
  85. continue;
  86. if (encoder->reconfigure_requested) {
  87. encoder->reconfigure_requested = false;
  88. encoder->info.update(encoder->context.data,
  89. encoder->context.settings);
  90. }
  91. // an explicit counter is used instead of remainder calculation
  92. // to allow multiple encoders started at the same time to start on
  93. // the same frame
  94. skip = encoder->frame_rate_divisor_counter++;
  95. if (encoder->frame_rate_divisor_counter ==
  96. encoder->frame_rate_divisor)
  97. encoder->frame_rate_divisor_counter = 0;
  98. if (skip)
  99. continue;
  100. if (!encoder->start_ts)
  101. encoder->start_ts = timestamp;
  102. if (++lock_count == encoders.num)
  103. next_key = 0;
  104. else
  105. next_key++;
  106. profile_start(gpu_encode_frame_name);
  107. if (encoder->info.encode_texture2) {
  108. struct encoder_texture tex = {0};
  109. tex.handle = tf.handle;
  110. tex.tex[0] = tf.tex;
  111. tex.tex[1] = tf.tex_uv;
  112. tex.tex[2] = NULL;
  113. success = encoder->info.encode_texture2(
  114. encoder->context.data, &tex,
  115. encoder->cur_pts, lock_key, &next_key,
  116. &pkt, &received);
  117. } else {
  118. success = encoder->info.encode_texture(
  119. encoder->context.data, tf.handle,
  120. encoder->cur_pts, lock_key, &next_key,
  121. &pkt, &received);
  122. }
  123. profile_end(gpu_encode_frame_name);
  124. send_off_encoder_packet(encoder, success, received,
  125. &pkt);
  126. lock_key = next_key;
  127. encoder->cur_pts += encoder->timebase_num *
  128. encoder->frame_rate_divisor;
  129. }
  130. /* -------------- */
  131. pthread_mutex_lock(&video->gpu_encoder_mutex);
  132. tf.lock_key = next_key;
  133. if (--tf.count) {
  134. tf.timestamp += interval;
  135. deque_push_front(&video->gpu_encoder_queue, &tf,
  136. sizeof(tf));
  137. video_output_inc_texture_skipped_frames(video->video);
  138. } else {
  139. deque_push_back(&video->gpu_encoder_avail_queue, &tf,
  140. sizeof(tf));
  141. }
  142. pthread_mutex_unlock(&video->gpu_encoder_mutex);
  143. /* -------------- */
  144. os_event_signal(video->gpu_encode_inactive);
  145. for (size_t i = 0; i < encoders.num; i++)
  146. obs_encoder_release(encoders.array[i]);
  147. da_resize(encoders, 0);
  148. profile_end(gpu_encode_thread_name);
  149. profile_reenable_thread();
  150. }
  151. da_free(encoders);
  152. return NULL;
  153. }
  154. bool init_gpu_encoding(struct obs_core_video_mix *video)
  155. {
  156. const struct video_output_info *info =
  157. video_output_get_info(video->video);
  158. video->gpu_encode_stop = false;
  159. deque_reserve(&video->gpu_encoder_avail_queue, NUM_ENCODE_TEXTURES);
  160. for (size_t i = 0; i < NUM_ENCODE_TEXTURES; i++) {
  161. gs_texture_t *tex;
  162. gs_texture_t *tex_uv;
  163. if (info->format == VIDEO_FORMAT_P010) {
  164. gs_texture_create_p010(
  165. &tex, &tex_uv, info->width, info->height,
  166. GS_RENDER_TARGET | GS_SHARED_KM_TEX);
  167. } else {
  168. gs_texture_create_nv12(
  169. &tex, &tex_uv, info->width, info->height,
  170. GS_RENDER_TARGET | GS_SHARED_KM_TEX);
  171. }
  172. if (!tex) {
  173. return false;
  174. }
  175. #ifdef _WIN32
  176. uint32_t handle = gs_texture_get_shared_handle(tex);
  177. #else
  178. uint32_t handle = (uint32_t)-1;
  179. #endif
  180. struct obs_tex_frame frame = {.tex = tex,
  181. .tex_uv = tex_uv,
  182. .handle = handle};
  183. deque_push_back(&video->gpu_encoder_avail_queue, &frame,
  184. sizeof(frame));
  185. }
  186. if (os_sem_init(&video->gpu_encode_semaphore, 0) != 0)
  187. return false;
  188. if (os_event_init(&video->gpu_encode_inactive, OS_EVENT_TYPE_MANUAL) !=
  189. 0)
  190. return false;
  191. if (pthread_create(&video->gpu_encode_thread, NULL, gpu_encode_thread,
  192. video) != 0)
  193. return false;
  194. os_event_signal(video->gpu_encode_inactive);
  195. video->gpu_encode_thread_initialized = true;
  196. return true;
  197. }
  198. void stop_gpu_encoding_thread(struct obs_core_video_mix *video)
  199. {
  200. if (video->gpu_encode_thread_initialized) {
  201. os_atomic_set_bool(&video->gpu_encode_stop, true);
  202. os_sem_post(video->gpu_encode_semaphore);
  203. pthread_join(video->gpu_encode_thread, NULL);
  204. video->gpu_encode_thread_initialized = false;
  205. }
  206. }
  207. void free_gpu_encoding(struct obs_core_video_mix *video)
  208. {
  209. if (video->gpu_encode_semaphore) {
  210. os_sem_destroy(video->gpu_encode_semaphore);
  211. video->gpu_encode_semaphore = NULL;
  212. }
  213. if (video->gpu_encode_inactive) {
  214. os_event_destroy(video->gpu_encode_inactive);
  215. video->gpu_encode_inactive = NULL;
  216. }
  217. #define free_deque(x) \
  218. do { \
  219. while (x.size) { \
  220. struct obs_tex_frame frame; \
  221. deque_pop_front(&x, &frame, sizeof(frame)); \
  222. gs_texture_destroy(frame.tex); \
  223. gs_texture_destroy(frame.tex_uv); \
  224. } \
  225. deque_free(&x); \
  226. } while (false)
  227. free_deque(video->gpu_encoder_queue);
  228. free_deque(video->gpu_encoder_avail_queue);
  229. #undef free_deque
  230. }