obs-video-gpu-encode.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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. obs_weak_encoder_t **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->encoder_group && !encoder->start_ts) {
  72. struct obs_encoder_group *group =
  73. encoder->encoder_group;
  74. bool ready = false;
  75. pthread_mutex_lock(&group->mutex);
  76. ready = group->start_timestamp == timestamp;
  77. pthread_mutex_unlock(&group->mutex);
  78. if (!ready)
  79. continue;
  80. }
  81. if (!encoder->first_received && num_paired) {
  82. bool wait_for_audio = false;
  83. for (size_t idx = 0;
  84. !wait_for_audio && idx < num_paired;
  85. idx++) {
  86. obs_encoder_t *enc =
  87. obs_weak_encoder_get_encoder(
  88. paired[idx]);
  89. if (!enc)
  90. continue;
  91. if (!enc->first_received ||
  92. enc->first_raw_ts > timestamp) {
  93. wait_for_audio = true;
  94. }
  95. obs_encoder_release(enc);
  96. }
  97. if (wait_for_audio)
  98. continue;
  99. }
  100. if (video_pause_check(&encoder->pause, timestamp))
  101. continue;
  102. if (encoder->reconfigure_requested) {
  103. encoder->reconfigure_requested = false;
  104. encoder->info.update(encoder->context.data,
  105. encoder->context.settings);
  106. }
  107. // an explicit counter is used instead of remainder calculation
  108. // to allow multiple encoders started at the same time to start on
  109. // the same frame
  110. skip = encoder->frame_rate_divisor_counter++;
  111. if (encoder->frame_rate_divisor_counter ==
  112. encoder->frame_rate_divisor)
  113. encoder->frame_rate_divisor_counter = 0;
  114. if (skip)
  115. continue;
  116. if (!encoder->start_ts)
  117. encoder->start_ts = timestamp;
  118. if (++lock_count == encoders.num)
  119. next_key = 0;
  120. else
  121. next_key++;
  122. profile_start(gpu_encode_frame_name);
  123. if (encoder->info.encode_texture2) {
  124. struct encoder_texture tex = {0};
  125. tex.handle = tf.handle;
  126. tex.tex[0] = tf.tex;
  127. tex.tex[1] = tf.tex_uv;
  128. tex.tex[2] = NULL;
  129. success = encoder->info.encode_texture2(
  130. encoder->context.data, &tex,
  131. encoder->cur_pts, lock_key, &next_key,
  132. &pkt, &received);
  133. } else {
  134. success = encoder->info.encode_texture(
  135. encoder->context.data, tf.handle,
  136. encoder->cur_pts, lock_key, &next_key,
  137. &pkt, &received);
  138. }
  139. profile_end(gpu_encode_frame_name);
  140. send_off_encoder_packet(encoder, success, received,
  141. &pkt);
  142. lock_key = next_key;
  143. encoder->cur_pts += encoder->timebase_num *
  144. encoder->frame_rate_divisor;
  145. }
  146. /* -------------- */
  147. pthread_mutex_lock(&video->gpu_encoder_mutex);
  148. tf.lock_key = next_key;
  149. if (--tf.count) {
  150. tf.timestamp += interval;
  151. deque_push_front(&video->gpu_encoder_queue, &tf,
  152. sizeof(tf));
  153. video_output_inc_texture_skipped_frames(video->video);
  154. } else {
  155. deque_push_back(&video->gpu_encoder_avail_queue, &tf,
  156. sizeof(tf));
  157. }
  158. pthread_mutex_unlock(&video->gpu_encoder_mutex);
  159. /* -------------- */
  160. os_event_signal(video->gpu_encode_inactive);
  161. for (size_t i = 0; i < encoders.num; i++)
  162. obs_encoder_release(encoders.array[i]);
  163. da_resize(encoders, 0);
  164. profile_end(gpu_encode_thread_name);
  165. profile_reenable_thread();
  166. }
  167. da_free(encoders);
  168. return NULL;
  169. }
  170. bool init_gpu_encoding(struct obs_core_video_mix *video)
  171. {
  172. const struct video_output_info *info =
  173. video_output_get_info(video->video);
  174. video->gpu_encode_stop = false;
  175. deque_reserve(&video->gpu_encoder_avail_queue, NUM_ENCODE_TEXTURES);
  176. for (size_t i = 0; i < NUM_ENCODE_TEXTURES; i++) {
  177. gs_texture_t *tex;
  178. gs_texture_t *tex_uv;
  179. if (info->format == VIDEO_FORMAT_P010) {
  180. gs_texture_create_p010(
  181. &tex, &tex_uv, info->width, info->height,
  182. GS_RENDER_TARGET | GS_SHARED_KM_TEX);
  183. } else {
  184. gs_texture_create_nv12(
  185. &tex, &tex_uv, info->width, info->height,
  186. GS_RENDER_TARGET | GS_SHARED_KM_TEX);
  187. }
  188. if (!tex) {
  189. return false;
  190. }
  191. #ifdef _WIN32
  192. uint32_t handle = gs_texture_get_shared_handle(tex);
  193. #else
  194. uint32_t handle = (uint32_t)-1;
  195. #endif
  196. struct obs_tex_frame frame = {.tex = tex,
  197. .tex_uv = tex_uv,
  198. .handle = handle};
  199. deque_push_back(&video->gpu_encoder_avail_queue, &frame,
  200. sizeof(frame));
  201. }
  202. if (os_sem_init(&video->gpu_encode_semaphore, 0) != 0)
  203. return false;
  204. if (os_event_init(&video->gpu_encode_inactive, OS_EVENT_TYPE_MANUAL) !=
  205. 0)
  206. return false;
  207. if (pthread_create(&video->gpu_encode_thread, NULL, gpu_encode_thread,
  208. video) != 0)
  209. return false;
  210. os_event_signal(video->gpu_encode_inactive);
  211. video->gpu_encode_thread_initialized = true;
  212. return true;
  213. }
  214. void stop_gpu_encoding_thread(struct obs_core_video_mix *video)
  215. {
  216. if (video->gpu_encode_thread_initialized) {
  217. os_atomic_set_bool(&video->gpu_encode_stop, true);
  218. os_sem_post(video->gpu_encode_semaphore);
  219. pthread_join(video->gpu_encode_thread, NULL);
  220. video->gpu_encode_thread_initialized = false;
  221. }
  222. }
  223. void free_gpu_encoding(struct obs_core_video_mix *video)
  224. {
  225. if (video->gpu_encode_semaphore) {
  226. os_sem_destroy(video->gpu_encode_semaphore);
  227. video->gpu_encode_semaphore = NULL;
  228. }
  229. if (video->gpu_encode_inactive) {
  230. os_event_destroy(video->gpu_encode_inactive);
  231. video->gpu_encode_inactive = NULL;
  232. }
  233. #define free_deque(x) \
  234. do { \
  235. while (x.size) { \
  236. struct obs_tex_frame frame; \
  237. deque_pop_front(&x, &frame, sizeof(frame)); \
  238. gs_texture_destroy(frame.tex); \
  239. gs_texture_destroy(frame.tex_uv); \
  240. } \
  241. deque_free(&x); \
  242. } while (false)
  243. free_deque(video->gpu_encoder_queue);
  244. free_deque(video->gpu_encoder_avail_queue);
  245. #undef free_deque
  246. }