obs-video-gpu-encode.c 8.2 KB

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