obs-video-gpu-encode.c 9.1 KB

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