obs-video.c 33 KB

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