obs-video.c 31 KB

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