obs-video-gpu-encode.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. /******************************************************************************
  2. Copyright (C) 2023 by Lain Bailey <lain@obsproject.com>
  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(), "obs_gpu_encode_thread(%g" NBSP "ms)", interval / 1000000.);
  27. profile_register_root(gpu_encode_thread_name, interval);
  28. while (os_sem_wait(video->gpu_encode_semaphore) == 0) {
  29. struct obs_tex_frame tf;
  30. uint64_t timestamp;
  31. uint64_t lock_key;
  32. uint64_t next_key;
  33. size_t lock_count = 0;
  34. uint64_t fer_ts = 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(video->gpu_encoders.array[i]);
  52. if (encoder)
  53. da_push_back(encoders, &encoder);
  54. }
  55. pthread_mutex_unlock(&video->gpu_encoder_mutex);
  56. /* -------------- */
  57. for (size_t i = 0; i < encoders.num; i++) {
  58. struct encoder_packet pkt = {0};
  59. bool received = false;
  60. bool success = false;
  61. uint32_t skip = 0;
  62. obs_encoder_t *encoder = encoders.array[i];
  63. obs_weak_encoder_t **paired = encoder->paired_encoders.array;
  64. size_t num_paired = encoder->paired_encoders.num;
  65. pkt.timebase_num = encoder->timebase_num * encoder->frame_rate_divisor;
  66. pkt.timebase_den = encoder->timebase_den;
  67. pkt.encoder = encoder;
  68. if (encoder->encoder_group && !encoder->start_ts) {
  69. struct obs_encoder_group *group = encoder->encoder_group;
  70. bool ready = false;
  71. pthread_mutex_lock(&group->mutex);
  72. ready = group->start_timestamp == timestamp;
  73. pthread_mutex_unlock(&group->mutex);
  74. if (!ready)
  75. continue;
  76. }
  77. if (!encoder->first_received && num_paired) {
  78. bool wait_for_audio = false;
  79. for (size_t idx = 0; !wait_for_audio && idx < num_paired; idx++) {
  80. obs_encoder_t *enc = obs_weak_encoder_get_encoder(paired[idx]);
  81. if (!enc)
  82. continue;
  83. if (!enc->first_received || enc->first_raw_ts > timestamp) {
  84. wait_for_audio = true;
  85. }
  86. obs_encoder_release(enc);
  87. }
  88. if (wait_for_audio)
  89. continue;
  90. }
  91. if (video_pause_check(&encoder->pause, timestamp))
  92. continue;
  93. if (encoder->reconfigure_requested) {
  94. encoder->reconfigure_requested = false;
  95. encoder->info.update(encoder->context.data, encoder->context.settings);
  96. }
  97. // an explicit counter is used instead of remainder calculation
  98. // to allow multiple encoders started at the same time to start on
  99. // the same frame
  100. skip = encoder->frame_rate_divisor_counter++;
  101. if (encoder->frame_rate_divisor_counter == encoder->frame_rate_divisor)
  102. encoder->frame_rate_divisor_counter = 0;
  103. if (skip)
  104. continue;
  105. if (!encoder->start_ts)
  106. encoder->start_ts = timestamp;
  107. if (++lock_count == encoders.num)
  108. next_key = 0;
  109. else
  110. next_key++;
  111. /* Get the frame encode request timestamp. This
  112. * needs to be read just before the encode request.
  113. */
  114. fer_ts = os_gettime_ns();
  115. profile_start(gpu_encode_frame_name);
  116. if (encoder->info.encode_texture2) {
  117. struct encoder_texture tex = {0};
  118. tex.handle = tf.handle;
  119. tex.tex[0] = tf.tex;
  120. tex.tex[1] = tf.tex_uv;
  121. tex.tex[2] = NULL;
  122. success = encoder->info.encode_texture2(encoder->context.data, &tex, encoder->cur_pts,
  123. lock_key, &next_key, &pkt, &received);
  124. } else {
  125. success = encoder->info.encode_texture(encoder->context.data, tf.handle,
  126. encoder->cur_pts, lock_key, &next_key, &pkt,
  127. &received);
  128. }
  129. profile_end(gpu_encode_frame_name);
  130. /* Generate and enqueue the frame timing metrics, namely
  131. * the CTS (composition time), FER (frame encode request), FERC
  132. * (frame encode request complete) and current PTS. PTS is used to
  133. * associate the frame timing data with the encode packet. */
  134. if (tf.timestamp) {
  135. struct encoder_packet_time *ept = da_push_back_new(encoder->encoder_packet_times);
  136. // Get the frame encode request complete timestamp
  137. if (success) {
  138. ept->ferc = os_gettime_ns();
  139. } else {
  140. // Encode had error, set ferc to 0
  141. ept->ferc = 0;
  142. }
  143. ept->pts = encoder->cur_pts;
  144. ept->cts = tf.timestamp;
  145. ept->fer = fer_ts;
  146. }
  147. send_off_encoder_packet(encoder, success, received, &pkt);
  148. lock_key = next_key;
  149. encoder->cur_pts += encoder->timebase_num * encoder->frame_rate_divisor;
  150. }
  151. /* -------------- */
  152. pthread_mutex_lock(&video->gpu_encoder_mutex);
  153. tf.lock_key = next_key;
  154. if (--tf.count) {
  155. tf.timestamp += interval;
  156. deque_push_front(&video->gpu_encoder_queue, &tf, sizeof(tf));
  157. video_output_inc_texture_skipped_frames(video->video);
  158. } else {
  159. deque_push_back(&video->gpu_encoder_avail_queue, &tf, sizeof(tf));
  160. }
  161. pthread_mutex_unlock(&video->gpu_encoder_mutex);
  162. /* -------------- */
  163. os_event_signal(video->gpu_encode_inactive);
  164. for (size_t i = 0; i < encoders.num; i++)
  165. obs_encoder_release(encoders.array[i]);
  166. da_resize(encoders, 0);
  167. profile_end(gpu_encode_thread_name);
  168. profile_reenable_thread();
  169. }
  170. da_free(encoders);
  171. return NULL;
  172. }
  173. bool init_gpu_encoding(struct obs_core_video_mix *video)
  174. {
  175. const struct video_output_info *info = video_output_get_info(video->video);
  176. video->gpu_encode_stop = false;
  177. deque_reserve(&video->gpu_encoder_avail_queue, NUM_ENCODE_TEXTURES);
  178. for (size_t i = 0; i < NUM_ENCODE_TEXTURES; i++) {
  179. gs_texture_t *tex;
  180. gs_texture_t *tex_uv;
  181. if (info->format == VIDEO_FORMAT_P010) {
  182. gs_texture_create_p010(&tex, &tex_uv, info->width, info->height,
  183. GS_RENDER_TARGET | GS_SHARED_KM_TEX);
  184. } else {
  185. gs_texture_create_nv12(&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, .tex_uv = tex_uv, .handle = handle};
  197. deque_push_back(&video->gpu_encoder_avail_queue, &frame, sizeof(frame));
  198. }
  199. if (os_sem_init(&video->gpu_encode_semaphore, 0) != 0)
  200. return false;
  201. if (os_event_init(&video->gpu_encode_inactive, OS_EVENT_TYPE_MANUAL) != 0)
  202. return false;
  203. if (pthread_create(&video->gpu_encode_thread, NULL, gpu_encode_thread, video) != 0)
  204. return false;
  205. os_event_signal(video->gpu_encode_inactive);
  206. video->gpu_encode_thread_initialized = true;
  207. return true;
  208. }
  209. void stop_gpu_encoding_thread(struct obs_core_video_mix *video)
  210. {
  211. if (video->gpu_encode_thread_initialized) {
  212. os_atomic_set_bool(&video->gpu_encode_stop, true);
  213. os_sem_post(video->gpu_encode_semaphore);
  214. pthread_join(video->gpu_encode_thread, NULL);
  215. video->gpu_encode_thread_initialized = false;
  216. }
  217. }
  218. void free_gpu_encoding(struct obs_core_video_mix *video)
  219. {
  220. if (video->gpu_encode_semaphore) {
  221. os_sem_destroy(video->gpu_encode_semaphore);
  222. video->gpu_encode_semaphore = NULL;
  223. }
  224. if (video->gpu_encode_inactive) {
  225. os_event_destroy(video->gpu_encode_inactive);
  226. video->gpu_encode_inactive = NULL;
  227. }
  228. #define free_deque(x) \
  229. do { \
  230. while (x.size) { \
  231. struct obs_tex_frame frame; \
  232. deque_pop_front(&x, &frame, sizeof(frame)); \
  233. gs_texture_destroy(frame.tex); \
  234. gs_texture_destroy(frame.tex_uv); \
  235. } \
  236. deque_free(&x); \
  237. } while (false)
  238. free_deque(video->gpu_encoder_queue);
  239. free_deque(video->gpu_encoder_avail_queue);
  240. #undef free_deque
  241. }