obs-video.c 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243
  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 <time.h>
  15. #include <stdlib.h>
  16. #include "obs.h"
  17. #include "obs-internal.h"
  18. #include "graphics/vec4.h"
  19. #include "media-io/format-conversion.h"
  20. #include "media-io/video-frame.h"
  21. #ifdef _WIN32
  22. #define WIN32_MEAN_AND_LEAN
  23. #include <windows.h>
  24. #endif
  25. static uint64_t tick_sources(uint64_t cur_time, uint64_t last_time)
  26. {
  27. struct obs_core_data *data = &obs->data;
  28. struct obs_source *source;
  29. uint64_t delta_time;
  30. float seconds;
  31. if (!last_time)
  32. last_time = cur_time - obs->video.video_frame_interval_ns;
  33. delta_time = cur_time - last_time;
  34. seconds = (float)((double)delta_time / 1000000000.0);
  35. /* ------------------------------------- */
  36. /* call tick callbacks */
  37. pthread_mutex_lock(&data->draw_callbacks_mutex);
  38. for (size_t i = data->tick_callbacks.num; i > 0; i--) {
  39. struct tick_callback *callback;
  40. callback = data->tick_callbacks.array + (i - 1);
  41. callback->tick(callback->param, seconds);
  42. }
  43. pthread_mutex_unlock(&data->draw_callbacks_mutex);
  44. /* ------------------------------------- */
  45. /* get an array of all sources to tick */
  46. da_clear(data->sources_to_tick);
  47. pthread_mutex_lock(&data->sources_mutex);
  48. source = data->sources;
  49. while (source) {
  50. obs_source_t *s = obs_source_get_ref(source);
  51. if (s)
  52. da_push_back(data->sources_to_tick, &s);
  53. source = (struct obs_source *)source->context.hh_uuid.next;
  54. }
  55. pthread_mutex_unlock(&data->sources_mutex);
  56. /* ------------------------------------- */
  57. /* call the tick function of each source */
  58. for (size_t i = 0; i < data->sources_to_tick.num; i++) {
  59. obs_source_t *s = data->sources_to_tick.array[i];
  60. obs_source_video_tick(s, seconds);
  61. obs_source_release(s);
  62. }
  63. return cur_time;
  64. }
  65. /* in obs-display.c */
  66. extern void render_display(struct obs_display *display);
  67. static inline void render_displays(void)
  68. {
  69. struct obs_display *display;
  70. if (!obs->data.valid)
  71. return;
  72. gs_enter_context(obs->video.graphics);
  73. /* render extra displays/swaps */
  74. pthread_mutex_lock(&obs->data.displays_mutex);
  75. display = obs->data.first_display;
  76. while (display) {
  77. render_display(display);
  78. display = display->next;
  79. }
  80. pthread_mutex_unlock(&obs->data.displays_mutex);
  81. gs_leave_context();
  82. }
  83. static inline void set_render_size(uint32_t width, uint32_t height)
  84. {
  85. gs_enable_depth_test(false);
  86. gs_set_cull_mode(GS_NEITHER);
  87. gs_ortho(0.0f, (float)width, 0.0f, (float)height, -100.0f, 100.0f);
  88. gs_set_viewport(0, 0, width, height);
  89. }
  90. static inline void unmap_last_surface(struct obs_core_video_mix *video)
  91. {
  92. for (int c = 0; c < NUM_CHANNELS; ++c) {
  93. if (video->mapped_surfaces[c]) {
  94. gs_stagesurface_unmap(video->mapped_surfaces[c]);
  95. video->mapped_surfaces[c] = NULL;
  96. }
  97. }
  98. }
  99. static const char *render_main_texture_name = "render_main_texture";
  100. static inline void render_main_texture(struct obs_core_video_mix *video)
  101. {
  102. uint32_t base_width = video->ovi.base_width;
  103. uint32_t base_height = video->ovi.base_height;
  104. profile_start(render_main_texture_name);
  105. GS_DEBUG_MARKER_BEGIN(GS_DEBUG_COLOR_MAIN_TEXTURE,
  106. render_main_texture_name);
  107. struct vec4 clear_color;
  108. vec4_set(&clear_color, 0.0f, 0.0f, 0.0f, 0.0f);
  109. gs_set_render_target_with_color_space(video->render_texture, NULL,
  110. video->render_space);
  111. gs_clear(GS_CLEAR_COLOR, &clear_color, 1.0f, 0);
  112. set_render_size(base_width, base_height);
  113. pthread_mutex_lock(&obs->data.draw_callbacks_mutex);
  114. for (size_t i = obs->data.draw_callbacks.num; i > 0; i--) {
  115. struct draw_callback *const callback =
  116. obs->data.draw_callbacks.array + (i - 1);
  117. callback->draw(callback->param, base_width, base_height);
  118. }
  119. pthread_mutex_unlock(&obs->data.draw_callbacks_mutex);
  120. obs_view_render(video->view);
  121. video->texture_rendered = true;
  122. pthread_mutex_lock(&obs->data.draw_callbacks_mutex);
  123. for (size_t i = 0; i < obs->data.rendered_callbacks.num; ++i) {
  124. struct rendered_callback *const callback =
  125. &obs->data.rendered_callbacks.array[i];
  126. callback->rendered(callback->param);
  127. }
  128. pthread_mutex_unlock(&obs->data.draw_callbacks_mutex);
  129. GS_DEBUG_MARKER_END();
  130. profile_end(render_main_texture_name);
  131. }
  132. static inline gs_effect_t *
  133. get_scale_effect_internal(struct obs_core_video_mix *mix)
  134. {
  135. struct obs_core_video *video = &obs->video;
  136. const struct video_output_info *info =
  137. video_output_get_info(mix->video);
  138. /* if the dimension is under half the size of the original image,
  139. * bicubic/lanczos can't sample enough pixels to create an accurate
  140. * image, so use the bilinear low resolution effect instead */
  141. if (info->width < (mix->ovi.base_width / 2) &&
  142. info->height < (mix->ovi.base_height / 2)) {
  143. return video->bilinear_lowres_effect;
  144. }
  145. switch (mix->ovi.scale_type) {
  146. case OBS_SCALE_BILINEAR:
  147. return video->default_effect;
  148. case OBS_SCALE_LANCZOS:
  149. return video->lanczos_effect;
  150. case OBS_SCALE_AREA:
  151. return video->area_effect;
  152. case OBS_SCALE_BICUBIC:
  153. default:;
  154. }
  155. return video->bicubic_effect;
  156. }
  157. static inline bool resolution_close(struct obs_core_video_mix *mix,
  158. uint32_t width, uint32_t height)
  159. {
  160. long width_cmp = (long)mix->ovi.base_width - (long)width;
  161. long height_cmp = (long)mix->ovi.base_height - (long)height;
  162. return labs(width_cmp) <= 16 && labs(height_cmp) <= 16;
  163. }
  164. static inline gs_effect_t *get_scale_effect(struct obs_core_video_mix *mix,
  165. uint32_t width, uint32_t height)
  166. {
  167. struct obs_core_video *video = &obs->video;
  168. if (resolution_close(mix, width, height)) {
  169. return video->default_effect;
  170. } else {
  171. /* if the scale method couldn't be loaded, use either bicubic
  172. * or bilinear by default */
  173. gs_effect_t *effect = get_scale_effect_internal(mix);
  174. if (!effect)
  175. effect = !!video->bicubic_effect
  176. ? video->bicubic_effect
  177. : video->default_effect;
  178. return effect;
  179. }
  180. }
  181. static const char *render_output_texture_name = "render_output_texture";
  182. static inline gs_texture_t *
  183. render_output_texture(struct obs_core_video_mix *mix)
  184. {
  185. struct obs_video_info *const ovi = &mix->ovi;
  186. gs_texture_t *texture = mix->render_texture;
  187. gs_texture_t *target = mix->output_texture;
  188. const uint32_t width = gs_texture_get_width(target);
  189. const uint32_t height = gs_texture_get_height(target);
  190. if ((width == ovi->base_width) && (height == ovi->base_height))
  191. return texture;
  192. profile_start(render_output_texture_name);
  193. gs_effect_t *effect = get_scale_effect(mix, width, height);
  194. gs_technique_t *tech = gs_effect_get_technique(effect, "Draw");
  195. gs_eparam_t *image = gs_effect_get_param_by_name(effect, "image");
  196. gs_eparam_t *bres =
  197. gs_effect_get_param_by_name(effect, "base_dimension");
  198. gs_eparam_t *bres_i =
  199. gs_effect_get_param_by_name(effect, "base_dimension_i");
  200. size_t passes, i;
  201. gs_set_render_target(target, NULL);
  202. set_render_size(width, height);
  203. if (bres) {
  204. struct vec2 base;
  205. vec2_set(&base, (float)mix->ovi.base_width,
  206. (float)mix->ovi.base_height);
  207. gs_effect_set_vec2(bres, &base);
  208. }
  209. if (bres_i) {
  210. struct vec2 base_i;
  211. vec2_set(&base_i, 1.0f / (float)mix->ovi.base_width,
  212. 1.0f / (float)mix->ovi.base_height);
  213. gs_effect_set_vec2(bres_i, &base_i);
  214. }
  215. gs_effect_set_texture_srgb(image, texture);
  216. gs_enable_framebuffer_srgb(true);
  217. gs_enable_blending(false);
  218. passes = gs_technique_begin(tech);
  219. for (i = 0; i < passes; i++) {
  220. gs_technique_begin_pass(tech, i);
  221. gs_draw_sprite(texture, 0, width, height);
  222. gs_technique_end_pass(tech);
  223. }
  224. gs_technique_end(tech);
  225. gs_enable_blending(true);
  226. gs_enable_framebuffer_srgb(false);
  227. profile_end(render_output_texture_name);
  228. return target;
  229. }
  230. static void render_convert_plane(gs_effect_t *effect, gs_texture_t *target,
  231. const char *tech_name)
  232. {
  233. gs_technique_t *tech = gs_effect_get_technique(effect, tech_name);
  234. const uint32_t width = gs_texture_get_width(target);
  235. const uint32_t height = gs_texture_get_height(target);
  236. gs_set_render_target(target, NULL);
  237. set_render_size(width, height);
  238. size_t passes = gs_technique_begin(tech);
  239. for (size_t i = 0; i < passes; i++) {
  240. gs_technique_begin_pass(tech, i);
  241. gs_draw(GS_TRIS, 0, 3);
  242. gs_technique_end_pass(tech);
  243. }
  244. gs_technique_end(tech);
  245. }
  246. static const char *render_convert_texture_name = "render_convert_texture";
  247. static void render_convert_texture(struct obs_core_video_mix *video,
  248. gs_texture_t *const *const convert_textures,
  249. gs_texture_t *texture)
  250. {
  251. profile_start(render_convert_texture_name);
  252. gs_effect_t *effect = obs->video.conversion_effect;
  253. gs_eparam_t *color_vec0 =
  254. gs_effect_get_param_by_name(effect, "color_vec0");
  255. gs_eparam_t *color_vec1 =
  256. gs_effect_get_param_by_name(effect, "color_vec1");
  257. gs_eparam_t *color_vec2 =
  258. gs_effect_get_param_by_name(effect, "color_vec2");
  259. gs_eparam_t *image = gs_effect_get_param_by_name(effect, "image");
  260. gs_eparam_t *width_i = gs_effect_get_param_by_name(effect, "width_i");
  261. gs_eparam_t *height_i = gs_effect_get_param_by_name(effect, "height_i");
  262. gs_eparam_t *sdr_white_nits_over_maximum = gs_effect_get_param_by_name(
  263. effect, "sdr_white_nits_over_maximum");
  264. gs_eparam_t *hdr_lw = gs_effect_get_param_by_name(effect, "hdr_lw");
  265. struct vec4 vec0, vec1, vec2;
  266. vec4_set(&vec0, video->color_matrix[4], video->color_matrix[5],
  267. video->color_matrix[6], video->color_matrix[7]);
  268. vec4_set(&vec1, video->color_matrix[0], video->color_matrix[1],
  269. video->color_matrix[2], video->color_matrix[3]);
  270. vec4_set(&vec2, video->color_matrix[8], video->color_matrix[9],
  271. video->color_matrix[10], video->color_matrix[11]);
  272. gs_enable_blending(false);
  273. if (convert_textures[0]) {
  274. const float hdr_nominal_peak_level =
  275. obs->video.hdr_nominal_peak_level;
  276. const float multiplier =
  277. obs_get_video_sdr_white_level() / 10000.f;
  278. gs_effect_set_texture(image, texture);
  279. gs_effect_set_vec4(color_vec0, &vec0);
  280. gs_effect_set_float(sdr_white_nits_over_maximum, multiplier);
  281. gs_effect_set_float(hdr_lw, hdr_nominal_peak_level);
  282. render_convert_plane(effect, convert_textures[0],
  283. video->conversion_techs[0]);
  284. if (convert_textures[1]) {
  285. gs_effect_set_texture(image, texture);
  286. gs_effect_set_vec4(color_vec1, &vec1);
  287. if (!convert_textures[2])
  288. gs_effect_set_vec4(color_vec2, &vec2);
  289. gs_effect_set_float(width_i, video->conversion_width_i);
  290. gs_effect_set_float(height_i,
  291. video->conversion_height_i);
  292. gs_effect_set_float(sdr_white_nits_over_maximum,
  293. multiplier);
  294. gs_effect_set_float(hdr_lw, hdr_nominal_peak_level);
  295. render_convert_plane(effect, convert_textures[1],
  296. video->conversion_techs[1]);
  297. if (convert_textures[2]) {
  298. gs_effect_set_texture(image, texture);
  299. gs_effect_set_vec4(color_vec2, &vec2);
  300. gs_effect_set_float(width_i,
  301. video->conversion_width_i);
  302. gs_effect_set_float(height_i,
  303. video->conversion_height_i);
  304. gs_effect_set_float(sdr_white_nits_over_maximum,
  305. multiplier);
  306. gs_effect_set_float(hdr_lw,
  307. hdr_nominal_peak_level);
  308. render_convert_plane(
  309. effect, convert_textures[2],
  310. video->conversion_techs[2]);
  311. }
  312. }
  313. }
  314. gs_enable_blending(true);
  315. video->texture_converted = true;
  316. profile_end(render_convert_texture_name);
  317. }
  318. static const char *stage_output_texture_name = "stage_output_texture";
  319. static inline void
  320. stage_output_texture(struct obs_core_video_mix *video, int cur_texture,
  321. gs_texture_t *const *const convert_textures,
  322. gs_texture_t *output_texture,
  323. gs_stagesurf_t *const *const copy_surfaces,
  324. size_t channel_count)
  325. {
  326. profile_start(stage_output_texture_name);
  327. unmap_last_surface(video);
  328. if (!video->gpu_conversion) {
  329. gs_stagesurf_t *copy = copy_surfaces[0];
  330. if (copy)
  331. gs_stage_texture(copy, output_texture);
  332. video->active_copy_surfaces[cur_texture][0] = copy;
  333. for (size_t i = 1; i < NUM_CHANNELS; ++i)
  334. video->active_copy_surfaces[cur_texture][i] = NULL;
  335. video->textures_copied[cur_texture] = true;
  336. } else if (video->texture_converted) {
  337. for (size_t i = 0; i < channel_count; i++) {
  338. gs_stagesurf_t *copy = copy_surfaces[i];
  339. if (copy)
  340. gs_stage_texture(copy, convert_textures[i]);
  341. video->active_copy_surfaces[cur_texture][i] = copy;
  342. }
  343. for (size_t i = channel_count; i < NUM_CHANNELS; ++i)
  344. video->active_copy_surfaces[cur_texture][i] = NULL;
  345. video->textures_copied[cur_texture] = true;
  346. }
  347. profile_end(stage_output_texture_name);
  348. }
  349. #ifdef _WIN32
  350. static inline bool queue_frame(struct obs_core_video_mix *video,
  351. bool raw_active,
  352. struct obs_vframe_info *vframe_info)
  353. {
  354. bool duplicate =
  355. !video->gpu_encoder_avail_queue.size ||
  356. (video->gpu_encoder_queue.size && vframe_info->count > 1);
  357. if (duplicate) {
  358. struct obs_tex_frame *tf = circlebuf_data(
  359. &video->gpu_encoder_queue,
  360. video->gpu_encoder_queue.size - sizeof(*tf));
  361. /* texture-based encoding is stopping */
  362. if (!tf) {
  363. return false;
  364. }
  365. tf->count++;
  366. os_sem_post(video->gpu_encode_semaphore);
  367. goto finish;
  368. }
  369. struct obs_tex_frame tf;
  370. circlebuf_pop_front(&video->gpu_encoder_avail_queue, &tf, sizeof(tf));
  371. if (tf.released) {
  372. gs_texture_acquire_sync(tf.tex, tf.lock_key, GS_WAIT_INFINITE);
  373. tf.released = false;
  374. }
  375. /* the vframe_info->count > 1 case causing a copy can only happen if by
  376. * some chance the very first frame has to be duplicated for whatever
  377. * reason. otherwise, it goes to the 'duplicate' case above, which
  378. * will ensure better performance. */
  379. if (raw_active || vframe_info->count > 1) {
  380. gs_copy_texture(tf.tex, video->convert_textures_encode[0]);
  381. } else {
  382. gs_texture_t *tex = video->convert_textures_encode[0];
  383. gs_texture_t *tex_uv = video->convert_textures_encode[1];
  384. video->convert_textures_encode[0] = tf.tex;
  385. video->convert_textures_encode[1] = tf.tex_uv;
  386. tf.tex = tex;
  387. tf.tex_uv = tex_uv;
  388. }
  389. tf.count = 1;
  390. tf.timestamp = vframe_info->timestamp;
  391. tf.released = true;
  392. tf.handle = gs_texture_get_shared_handle(tf.tex);
  393. gs_texture_release_sync(tf.tex, ++tf.lock_key);
  394. circlebuf_push_back(&video->gpu_encoder_queue, &tf, sizeof(tf));
  395. os_sem_post(video->gpu_encode_semaphore);
  396. finish:
  397. return --vframe_info->count;
  398. }
  399. extern void full_stop(struct obs_encoder *encoder);
  400. static inline void encode_gpu(struct obs_core_video_mix *video, bool raw_active,
  401. struct obs_vframe_info *vframe_info)
  402. {
  403. while (queue_frame(video, raw_active, vframe_info))
  404. ;
  405. }
  406. static const char *output_gpu_encoders_name = "output_gpu_encoders";
  407. static void output_gpu_encoders(struct obs_core_video_mix *video,
  408. bool raw_active)
  409. {
  410. profile_start(output_gpu_encoders_name);
  411. if (!video->texture_converted)
  412. goto end;
  413. if (!video->vframe_info_buffer_gpu.size)
  414. goto end;
  415. struct obs_vframe_info vframe_info;
  416. circlebuf_pop_front(&video->vframe_info_buffer_gpu, &vframe_info,
  417. sizeof(vframe_info));
  418. pthread_mutex_lock(&video->gpu_encoder_mutex);
  419. encode_gpu(video, raw_active, &vframe_info);
  420. pthread_mutex_unlock(&video->gpu_encoder_mutex);
  421. end:
  422. profile_end(output_gpu_encoders_name);
  423. }
  424. #endif
  425. static inline void render_video(struct obs_core_video_mix *video,
  426. bool raw_active, const bool gpu_active,
  427. int cur_texture)
  428. {
  429. gs_begin_scene();
  430. gs_enable_depth_test(false);
  431. gs_set_cull_mode(GS_NEITHER);
  432. render_main_texture(video);
  433. if (raw_active || gpu_active) {
  434. gs_texture_t *const *convert_textures = video->convert_textures;
  435. gs_stagesurf_t *const *copy_surfaces =
  436. video->copy_surfaces[cur_texture];
  437. size_t channel_count = NUM_CHANNELS;
  438. gs_texture_t *output_texture = render_output_texture(video);
  439. #ifdef _WIN32
  440. if (gpu_active) {
  441. convert_textures = video->convert_textures_encode;
  442. copy_surfaces = video->copy_surfaces_encode;
  443. channel_count = 1;
  444. gs_flush();
  445. }
  446. #endif
  447. if (video->gpu_conversion) {
  448. render_convert_texture(video, convert_textures,
  449. output_texture);
  450. }
  451. #ifdef _WIN32
  452. if (gpu_active) {
  453. gs_flush();
  454. output_gpu_encoders(video, raw_active);
  455. }
  456. #endif
  457. if (raw_active) {
  458. stage_output_texture(video, cur_texture,
  459. convert_textures, output_texture,
  460. copy_surfaces, channel_count);
  461. }
  462. }
  463. gs_set_render_target(NULL, NULL);
  464. gs_enable_blending(true);
  465. gs_end_scene();
  466. }
  467. static inline bool download_frame(struct obs_core_video_mix *video,
  468. int prev_texture, struct video_data *frame)
  469. {
  470. if (!video->textures_copied[prev_texture])
  471. return false;
  472. for (int channel = 0; channel < NUM_CHANNELS; ++channel) {
  473. gs_stagesurf_t *surface =
  474. video->active_copy_surfaces[prev_texture][channel];
  475. if (surface) {
  476. if (!gs_stagesurface_map(surface, &frame->data[channel],
  477. &frame->linesize[channel]))
  478. return false;
  479. video->mapped_surfaces[channel] = surface;
  480. }
  481. }
  482. return true;
  483. }
  484. static const uint8_t *set_gpu_converted_plane(uint32_t width, uint32_t height,
  485. uint32_t linesize_input,
  486. uint32_t linesize_output,
  487. const uint8_t *in, uint8_t *out)
  488. {
  489. if ((width == linesize_input) && (width == linesize_output)) {
  490. size_t total = (size_t)width * (size_t)height;
  491. memcpy(out, in, total);
  492. in += total;
  493. } else {
  494. for (size_t y = 0; y < height; y++) {
  495. memcpy(out, in, width);
  496. out += linesize_output;
  497. in += linesize_input;
  498. }
  499. }
  500. return in;
  501. }
  502. static void set_gpu_converted_data(struct video_frame *output,
  503. const struct video_data *input,
  504. const struct video_output_info *info)
  505. {
  506. switch (info->format) {
  507. case VIDEO_FORMAT_I420: {
  508. const uint32_t width = info->width;
  509. const uint32_t height = info->height;
  510. set_gpu_converted_plane(width, height, input->linesize[0],
  511. output->linesize[0], input->data[0],
  512. output->data[0]);
  513. const uint32_t width_d2 = width / 2;
  514. const uint32_t height_d2 = height / 2;
  515. set_gpu_converted_plane(width_d2, height_d2, input->linesize[1],
  516. output->linesize[1], input->data[1],
  517. output->data[1]);
  518. set_gpu_converted_plane(width_d2, height_d2, input->linesize[2],
  519. output->linesize[2], input->data[2],
  520. output->data[2]);
  521. break;
  522. }
  523. case VIDEO_FORMAT_NV12: {
  524. const uint32_t width = info->width;
  525. const uint32_t height = info->height;
  526. const uint32_t height_d2 = height / 2;
  527. if (input->linesize[1]) {
  528. set_gpu_converted_plane(width, height,
  529. input->linesize[0],
  530. output->linesize[0],
  531. input->data[0],
  532. output->data[0]);
  533. set_gpu_converted_plane(width, height_d2,
  534. input->linesize[1],
  535. output->linesize[1],
  536. input->data[1],
  537. output->data[1]);
  538. } else {
  539. const uint8_t *const in_uv = set_gpu_converted_plane(
  540. width, height, input->linesize[0],
  541. output->linesize[0], input->data[0],
  542. output->data[0]);
  543. set_gpu_converted_plane(width, height_d2,
  544. input->linesize[0],
  545. output->linesize[1], in_uv,
  546. output->data[1]);
  547. }
  548. break;
  549. }
  550. case VIDEO_FORMAT_I444: {
  551. const uint32_t width = info->width;
  552. const uint32_t height = info->height;
  553. set_gpu_converted_plane(width, height, input->linesize[0],
  554. output->linesize[0], input->data[0],
  555. output->data[0]);
  556. set_gpu_converted_plane(width, height, input->linesize[1],
  557. output->linesize[1], input->data[1],
  558. output->data[1]);
  559. set_gpu_converted_plane(width, height, input->linesize[2],
  560. output->linesize[2], input->data[2],
  561. output->data[2]);
  562. break;
  563. }
  564. case VIDEO_FORMAT_I010: {
  565. const uint32_t width = info->width;
  566. const uint32_t height = info->height;
  567. set_gpu_converted_plane(width * 2, height, input->linesize[0],
  568. output->linesize[0], input->data[0],
  569. output->data[0]);
  570. const uint32_t height_d2 = height / 2;
  571. set_gpu_converted_plane(width, height_d2, input->linesize[1],
  572. output->linesize[1], input->data[1],
  573. output->data[1]);
  574. set_gpu_converted_plane(width, height_d2, input->linesize[2],
  575. output->linesize[2], input->data[2],
  576. output->data[2]);
  577. break;
  578. }
  579. case VIDEO_FORMAT_P010: {
  580. const uint32_t width_x2 = info->width * 2;
  581. const uint32_t height = info->height;
  582. const uint32_t height_d2 = height / 2;
  583. if (input->linesize[1]) {
  584. set_gpu_converted_plane(width_x2, height,
  585. input->linesize[0],
  586. output->linesize[0],
  587. input->data[0],
  588. output->data[0]);
  589. set_gpu_converted_plane(width_x2, height_d2,
  590. input->linesize[1],
  591. output->linesize[1],
  592. input->data[1],
  593. output->data[1]);
  594. } else {
  595. const uint8_t *const in_uv = set_gpu_converted_plane(
  596. width_x2, height, input->linesize[0],
  597. output->linesize[0], input->data[0],
  598. output->data[0]);
  599. set_gpu_converted_plane(width_x2, height_d2,
  600. input->linesize[0],
  601. output->linesize[1], in_uv,
  602. output->data[1]);
  603. }
  604. break;
  605. }
  606. case VIDEO_FORMAT_P216: {
  607. const uint32_t width_x2 = info->width * 2;
  608. const uint32_t height = info->height;
  609. set_gpu_converted_plane(width_x2, height, input->linesize[0],
  610. output->linesize[0], input->data[0],
  611. output->data[0]);
  612. set_gpu_converted_plane(width_x2, height, input->linesize[1],
  613. output->linesize[1], input->data[1],
  614. output->data[1]);
  615. break;
  616. }
  617. case VIDEO_FORMAT_P416: {
  618. const uint32_t height = info->height;
  619. set_gpu_converted_plane(info->width * 2, height,
  620. input->linesize[0], output->linesize[0],
  621. input->data[0], output->data[0]);
  622. set_gpu_converted_plane(info->width * 4, height,
  623. input->linesize[1], output->linesize[1],
  624. input->data[1], output->data[1]);
  625. break;
  626. }
  627. case VIDEO_FORMAT_NONE:
  628. case VIDEO_FORMAT_YVYU:
  629. case VIDEO_FORMAT_YUY2:
  630. case VIDEO_FORMAT_UYVY:
  631. case VIDEO_FORMAT_RGBA:
  632. case VIDEO_FORMAT_BGRA:
  633. case VIDEO_FORMAT_BGRX:
  634. case VIDEO_FORMAT_Y800:
  635. case VIDEO_FORMAT_BGR3:
  636. case VIDEO_FORMAT_I412:
  637. case VIDEO_FORMAT_I422:
  638. case VIDEO_FORMAT_I210:
  639. case VIDEO_FORMAT_I40A:
  640. case VIDEO_FORMAT_I42A:
  641. case VIDEO_FORMAT_YUVA:
  642. case VIDEO_FORMAT_YA2L:
  643. case VIDEO_FORMAT_AYUV:
  644. case VIDEO_FORMAT_V210:
  645. case VIDEO_FORMAT_R10L:
  646. /* unimplemented */
  647. ;
  648. }
  649. }
  650. static inline void copy_rgbx_frame(struct video_frame *output,
  651. const struct video_data *input,
  652. const struct video_output_info *info)
  653. {
  654. uint8_t *in_ptr = input->data[0];
  655. uint8_t *out_ptr = output->data[0];
  656. /* if the line sizes match, do a single copy */
  657. if (input->linesize[0] == output->linesize[0]) {
  658. memcpy(out_ptr, in_ptr,
  659. (size_t)input->linesize[0] * (size_t)info->height);
  660. } else {
  661. const size_t copy_size = (size_t)info->width * 4;
  662. for (size_t y = 0; y < info->height; y++) {
  663. memcpy(out_ptr, in_ptr, copy_size);
  664. in_ptr += input->linesize[0];
  665. out_ptr += output->linesize[0];
  666. }
  667. }
  668. }
  669. static inline void output_video_data(struct obs_core_video_mix *video,
  670. struct video_data *input_frame, int count)
  671. {
  672. const struct video_output_info *info;
  673. struct video_frame output_frame;
  674. bool locked;
  675. info = video_output_get_info(video->video);
  676. locked = video_output_lock_frame(video->video, &output_frame, count,
  677. input_frame->timestamp);
  678. if (locked) {
  679. if (video->gpu_conversion) {
  680. set_gpu_converted_data(&output_frame, input_frame,
  681. info);
  682. } else {
  683. copy_rgbx_frame(&output_frame, input_frame, info);
  684. }
  685. video_output_unlock_frame(video->video);
  686. }
  687. }
  688. static inline void video_sleep(struct obs_core_video *video, uint64_t *p_time,
  689. uint64_t interval_ns)
  690. {
  691. struct obs_vframe_info vframe_info;
  692. uint64_t cur_time = *p_time;
  693. uint64_t t = cur_time + interval_ns;
  694. int count;
  695. if (os_sleepto_ns(t)) {
  696. *p_time = t;
  697. count = 1;
  698. } else {
  699. const uint64_t udiff = os_gettime_ns() - cur_time;
  700. int64_t diff;
  701. memcpy(&diff, &udiff, sizeof(diff));
  702. const uint64_t clamped_diff = (diff > (int64_t)interval_ns)
  703. ? (uint64_t)diff
  704. : interval_ns;
  705. count = (int)(clamped_diff / interval_ns);
  706. *p_time = cur_time + interval_ns * count;
  707. }
  708. video->total_frames += count;
  709. video->lagged_frames += count - 1;
  710. vframe_info.timestamp = cur_time;
  711. vframe_info.count = count;
  712. pthread_mutex_lock(&obs->video.mixes_mutex);
  713. for (size_t i = 0, num = obs->video.mixes.num; i < num; i++) {
  714. struct obs_core_video_mix *video = obs->video.mixes.array[i];
  715. bool raw_active = video->raw_was_active;
  716. bool gpu_active = video->gpu_was_active;
  717. if (raw_active)
  718. circlebuf_push_back(&video->vframe_info_buffer,
  719. &vframe_info, sizeof(vframe_info));
  720. if (gpu_active)
  721. circlebuf_push_back(&video->vframe_info_buffer_gpu,
  722. &vframe_info, sizeof(vframe_info));
  723. }
  724. pthread_mutex_unlock(&obs->video.mixes_mutex);
  725. }
  726. static const char *output_frame_gs_context_name = "gs_context(video->graphics)";
  727. static const char *output_frame_render_video_name = "render_video";
  728. static const char *output_frame_download_frame_name = "download_frame";
  729. static const char *output_frame_gs_flush_name = "gs_flush";
  730. static const char *output_frame_output_video_data_name = "output_video_data";
  731. static inline void output_frame(struct obs_core_video_mix *video)
  732. {
  733. const bool raw_active = video->raw_was_active;
  734. const bool gpu_active = video->gpu_was_active;
  735. int cur_texture = video->cur_texture;
  736. int prev_texture = cur_texture == 0 ? NUM_TEXTURES - 1
  737. : cur_texture - 1;
  738. struct video_data frame;
  739. bool frame_ready = 0;
  740. memset(&frame, 0, sizeof(struct video_data));
  741. profile_start(output_frame_gs_context_name);
  742. gs_enter_context(obs->video.graphics);
  743. profile_start(output_frame_render_video_name);
  744. GS_DEBUG_MARKER_BEGIN(GS_DEBUG_COLOR_RENDER_VIDEO,
  745. output_frame_render_video_name);
  746. render_video(video, raw_active, gpu_active, cur_texture);
  747. GS_DEBUG_MARKER_END();
  748. profile_end(output_frame_render_video_name);
  749. if (raw_active) {
  750. profile_start(output_frame_download_frame_name);
  751. frame_ready = download_frame(video, prev_texture, &frame);
  752. profile_end(output_frame_download_frame_name);
  753. }
  754. profile_start(output_frame_gs_flush_name);
  755. gs_flush();
  756. profile_end(output_frame_gs_flush_name);
  757. gs_leave_context();
  758. profile_end(output_frame_gs_context_name);
  759. if (raw_active && frame_ready) {
  760. struct obs_vframe_info vframe_info;
  761. circlebuf_pop_front(&video->vframe_info_buffer, &vframe_info,
  762. sizeof(vframe_info));
  763. frame.timestamp = vframe_info.timestamp;
  764. profile_start(output_frame_output_video_data_name);
  765. output_video_data(video, &frame, vframe_info.count);
  766. profile_end(output_frame_output_video_data_name);
  767. }
  768. if (++video->cur_texture == NUM_TEXTURES)
  769. video->cur_texture = 0;
  770. }
  771. static inline void output_frames(void)
  772. {
  773. pthread_mutex_lock(&obs->video.mixes_mutex);
  774. for (size_t i = 0, num = obs->video.mixes.num; i < num; i++) {
  775. struct obs_core_video_mix *mix = obs->video.mixes.array[i];
  776. if (mix->view) {
  777. output_frame(mix);
  778. } else {
  779. obs->video.mixes.array[i] = NULL;
  780. obs_free_video_mix(mix);
  781. da_erase(obs->video.mixes, i);
  782. i--;
  783. num--;
  784. }
  785. }
  786. pthread_mutex_unlock(&obs->video.mixes_mutex);
  787. }
  788. #define NBSP "\xC2\xA0"
  789. static void clear_base_frame_data(struct obs_core_video_mix *video)
  790. {
  791. video->texture_rendered = false;
  792. video->texture_converted = false;
  793. circlebuf_free(&video->vframe_info_buffer);
  794. video->cur_texture = 0;
  795. }
  796. static void clear_raw_frame_data(struct obs_core_video_mix *video)
  797. {
  798. memset(video->textures_copied, 0, sizeof(video->textures_copied));
  799. circlebuf_free(&video->vframe_info_buffer);
  800. }
  801. #ifdef _WIN32
  802. static void clear_gpu_frame_data(struct obs_core_video_mix *video)
  803. {
  804. circlebuf_free(&video->vframe_info_buffer_gpu);
  805. }
  806. #endif
  807. extern THREAD_LOCAL bool is_graphics_thread;
  808. static void execute_graphics_tasks(void)
  809. {
  810. struct obs_core_video *video = &obs->video;
  811. bool tasks_remaining = true;
  812. while (tasks_remaining) {
  813. pthread_mutex_lock(&video->task_mutex);
  814. if (video->tasks.size) {
  815. struct obs_task_info info;
  816. circlebuf_pop_front(&video->tasks, &info, sizeof(info));
  817. info.task(info.param);
  818. }
  819. tasks_remaining = !!video->tasks.size;
  820. pthread_mutex_unlock(&video->task_mutex);
  821. }
  822. }
  823. #ifdef _WIN32
  824. struct winrt_exports {
  825. void (*winrt_initialize)();
  826. void (*winrt_uninitialize)();
  827. struct winrt_disaptcher *(*winrt_dispatcher_init)();
  828. void (*winrt_dispatcher_free)(struct winrt_disaptcher *dispatcher);
  829. void (*winrt_capture_thread_start)();
  830. void (*winrt_capture_thread_stop)();
  831. };
  832. #define WINRT_IMPORT(func) \
  833. do { \
  834. exports->func = os_dlsym(module, #func); \
  835. if (!exports->func) { \
  836. success = false; \
  837. blog(LOG_ERROR, \
  838. "Could not load function '%s' from " \
  839. "module '%s'", \
  840. #func, module_name); \
  841. } \
  842. } while (false)
  843. static bool load_winrt_imports(struct winrt_exports *exports, void *module,
  844. const char *module_name)
  845. {
  846. bool success = true;
  847. WINRT_IMPORT(winrt_initialize);
  848. WINRT_IMPORT(winrt_uninitialize);
  849. WINRT_IMPORT(winrt_dispatcher_init);
  850. WINRT_IMPORT(winrt_dispatcher_free);
  851. WINRT_IMPORT(winrt_capture_thread_start);
  852. WINRT_IMPORT(winrt_capture_thread_stop);
  853. return success;
  854. }
  855. struct winrt_state {
  856. bool loaded;
  857. void *winrt_module;
  858. struct winrt_exports exports;
  859. struct winrt_disaptcher *dispatcher;
  860. };
  861. static void init_winrt_state(struct winrt_state *winrt)
  862. {
  863. static const char *const module_name = "libobs-winrt";
  864. winrt->winrt_module = os_dlopen(module_name);
  865. winrt->loaded = winrt->winrt_module &&
  866. load_winrt_imports(&winrt->exports, winrt->winrt_module,
  867. module_name);
  868. winrt->dispatcher = NULL;
  869. if (winrt->loaded) {
  870. winrt->exports.winrt_initialize();
  871. winrt->dispatcher = winrt->exports.winrt_dispatcher_init();
  872. gs_enter_context(obs->video.graphics);
  873. winrt->exports.winrt_capture_thread_start();
  874. gs_leave_context();
  875. }
  876. }
  877. static void uninit_winrt_state(struct winrt_state *winrt)
  878. {
  879. if (winrt->winrt_module) {
  880. if (winrt->loaded) {
  881. winrt->exports.winrt_capture_thread_stop();
  882. if (winrt->dispatcher)
  883. winrt->exports.winrt_dispatcher_free(
  884. winrt->dispatcher);
  885. winrt->exports.winrt_uninitialize();
  886. }
  887. os_dlclose(winrt->winrt_module);
  888. }
  889. }
  890. #endif // #ifdef _WIN32
  891. static const char *tick_sources_name = "tick_sources";
  892. static const char *render_displays_name = "render_displays";
  893. static const char *output_frame_name = "output_frame";
  894. static inline void update_active_state(struct obs_core_video_mix *video)
  895. {
  896. const bool raw_was_active = video->raw_was_active;
  897. #ifdef _WIN32
  898. const bool gpu_was_active = video->gpu_was_active;
  899. #endif
  900. const bool was_active = video->was_active;
  901. bool raw_active = os_atomic_load_long(&video->raw_active) > 0;
  902. #ifdef _WIN32
  903. const bool gpu_active =
  904. os_atomic_load_long(&video->gpu_encoder_active) > 0;
  905. const bool active = raw_active || gpu_active;
  906. #else
  907. const bool active = raw_active;
  908. #endif
  909. if (!was_active && active)
  910. clear_base_frame_data(video);
  911. if (!raw_was_active && raw_active)
  912. clear_raw_frame_data(video);
  913. #ifdef _WIN32
  914. if (!gpu_was_active && gpu_active)
  915. clear_gpu_frame_data(video);
  916. video->gpu_was_active = gpu_active;
  917. #endif
  918. video->raw_was_active = raw_active;
  919. video->was_active = active;
  920. }
  921. static inline void update_active_states(void)
  922. {
  923. pthread_mutex_lock(&obs->video.mixes_mutex);
  924. for (size_t i = 0, num = obs->video.mixes.num; i < num; i++)
  925. update_active_state(obs->video.mixes.array[i]);
  926. pthread_mutex_unlock(&obs->video.mixes_mutex);
  927. }
  928. static inline bool stop_requested(void)
  929. {
  930. bool success = true;
  931. pthread_mutex_lock(&obs->video.mixes_mutex);
  932. for (size_t i = 0, num = obs->video.mixes.num; i < num; i++)
  933. if (!video_output_stopped(obs->video.mixes.array[i]->video))
  934. success = false;
  935. pthread_mutex_unlock(&obs->video.mixes_mutex);
  936. return success;
  937. }
  938. bool obs_graphics_thread_loop(struct obs_graphics_context *context)
  939. {
  940. uint64_t frame_start = os_gettime_ns();
  941. uint64_t frame_time_ns;
  942. update_active_states();
  943. profile_start(context->video_thread_name);
  944. gs_enter_context(obs->video.graphics);
  945. gs_begin_frame();
  946. gs_leave_context();
  947. profile_start(tick_sources_name);
  948. context->last_time =
  949. tick_sources(obs->video.video_time, context->last_time);
  950. profile_end(tick_sources_name);
  951. #ifdef _WIN32
  952. MSG msg;
  953. while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) {
  954. TranslateMessage(&msg);
  955. DispatchMessage(&msg);
  956. }
  957. #endif
  958. profile_start(output_frame_name);
  959. output_frames();
  960. profile_end(output_frame_name);
  961. profile_start(render_displays_name);
  962. render_displays();
  963. profile_end(render_displays_name);
  964. execute_graphics_tasks();
  965. frame_time_ns = os_gettime_ns() - frame_start;
  966. profile_end(context->video_thread_name);
  967. profile_reenable_thread();
  968. video_sleep(&obs->video, &obs->video.video_time, context->interval);
  969. context->frame_time_total_ns += frame_time_ns;
  970. context->fps_total_ns += (obs->video.video_time - context->last_time);
  971. context->fps_total_frames++;
  972. if (context->fps_total_ns >= 1000000000ULL) {
  973. obs->video.video_fps =
  974. (double)context->fps_total_frames /
  975. ((double)context->fps_total_ns / 1000000000.0);
  976. obs->video.video_avg_frame_time_ns =
  977. context->frame_time_total_ns /
  978. (uint64_t)context->fps_total_frames;
  979. context->frame_time_total_ns = 0;
  980. context->fps_total_ns = 0;
  981. context->fps_total_frames = 0;
  982. }
  983. return !stop_requested();
  984. }
  985. void *obs_graphics_thread(void *param)
  986. {
  987. #ifdef _WIN32
  988. struct winrt_state winrt;
  989. init_winrt_state(&winrt);
  990. #endif // #ifdef _WIN32
  991. is_graphics_thread = true;
  992. const uint64_t interval = obs->video.video_frame_interval_ns;
  993. obs->video.video_time = os_gettime_ns();
  994. os_set_thread_name("libobs: graphics thread");
  995. const char *video_thread_name = profile_store_name(
  996. obs_get_profiler_name_store(),
  997. "obs_graphics_thread(%g" NBSP "ms)", interval / 1000000.);
  998. profile_register_root(video_thread_name, interval);
  999. srand((unsigned int)time(NULL));
  1000. struct obs_graphics_context context;
  1001. context.interval = interval;
  1002. context.frame_time_total_ns = 0;
  1003. context.fps_total_ns = 0;
  1004. context.fps_total_frames = 0;
  1005. context.last_time = 0;
  1006. context.video_thread_name = video_thread_name;
  1007. #ifdef __APPLE__
  1008. while (obs_graphics_thread_loop_autorelease(&context))
  1009. #else
  1010. while (obs_graphics_thread_loop(&context))
  1011. #endif
  1012. ;
  1013. #ifdef _WIN32
  1014. uninit_winrt_state(&winrt);
  1015. #endif
  1016. UNUSED_PARAMETER(param);
  1017. return NULL;
  1018. }