obs-video.c 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147
  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. for (size_t i = 1; i < NUM_CHANNELS; ++i)
  321. video->active_copy_surfaces[cur_texture][i] = NULL;
  322. video->textures_copied[cur_texture] = true;
  323. } else if (video->texture_converted) {
  324. for (size_t i = 0; i < channel_count; i++) {
  325. gs_stagesurf_t *copy = copy_surfaces[i];
  326. if (copy)
  327. gs_stage_texture(copy, convert_textures[i]);
  328. video->active_copy_surfaces[cur_texture][i] = copy;
  329. }
  330. for (size_t i = channel_count; i < NUM_CHANNELS; ++i)
  331. video->active_copy_surfaces[cur_texture][i] = NULL;
  332. video->textures_copied[cur_texture] = true;
  333. }
  334. profile_end(stage_output_texture_name);
  335. }
  336. #ifdef _WIN32
  337. static inline bool queue_frame(struct obs_core_video *video, bool raw_active,
  338. struct obs_vframe_info *vframe_info)
  339. {
  340. bool duplicate =
  341. !video->gpu_encoder_avail_queue.size ||
  342. (video->gpu_encoder_queue.size && vframe_info->count > 1);
  343. if (duplicate) {
  344. struct obs_tex_frame *tf = circlebuf_data(
  345. &video->gpu_encoder_queue,
  346. video->gpu_encoder_queue.size - sizeof(*tf));
  347. /* texture-based encoding is stopping */
  348. if (!tf) {
  349. return false;
  350. }
  351. tf->count++;
  352. os_sem_post(video->gpu_encode_semaphore);
  353. goto finish;
  354. }
  355. struct obs_tex_frame tf;
  356. circlebuf_pop_front(&video->gpu_encoder_avail_queue, &tf, sizeof(tf));
  357. if (tf.released) {
  358. gs_texture_acquire_sync(tf.tex, tf.lock_key, GS_WAIT_INFINITE);
  359. tf.released = false;
  360. }
  361. /* the vframe_info->count > 1 case causing a copy can only happen if by
  362. * some chance the very first frame has to be duplicated for whatever
  363. * reason. otherwise, it goes to the 'duplicate' case above, which
  364. * will ensure better performance. */
  365. if (raw_active || vframe_info->count > 1) {
  366. gs_copy_texture(tf.tex, video->convert_textures_encode[0]);
  367. } else {
  368. gs_texture_t *tex = video->convert_textures_encode[0];
  369. gs_texture_t *tex_uv = video->convert_textures_encode[1];
  370. video->convert_textures_encode[0] = tf.tex;
  371. video->convert_textures_encode[1] = tf.tex_uv;
  372. tf.tex = tex;
  373. tf.tex_uv = tex_uv;
  374. }
  375. tf.count = 1;
  376. tf.timestamp = vframe_info->timestamp;
  377. tf.released = true;
  378. tf.handle = gs_texture_get_shared_handle(tf.tex);
  379. gs_texture_release_sync(tf.tex, ++tf.lock_key);
  380. circlebuf_push_back(&video->gpu_encoder_queue, &tf, sizeof(tf));
  381. os_sem_post(video->gpu_encode_semaphore);
  382. finish:
  383. return --vframe_info->count;
  384. }
  385. extern void full_stop(struct obs_encoder *encoder);
  386. static inline void encode_gpu(struct obs_core_video *video, bool raw_active,
  387. struct obs_vframe_info *vframe_info)
  388. {
  389. while (queue_frame(video, raw_active, vframe_info))
  390. ;
  391. }
  392. static const char *output_gpu_encoders_name = "output_gpu_encoders";
  393. static void output_gpu_encoders(struct obs_core_video *video, bool raw_active)
  394. {
  395. profile_start(output_gpu_encoders_name);
  396. if (!video->texture_converted)
  397. goto end;
  398. if (!video->vframe_info_buffer_gpu.size)
  399. goto end;
  400. struct obs_vframe_info vframe_info;
  401. circlebuf_pop_front(&video->vframe_info_buffer_gpu, &vframe_info,
  402. sizeof(vframe_info));
  403. pthread_mutex_lock(&video->gpu_encoder_mutex);
  404. encode_gpu(video, raw_active, &vframe_info);
  405. pthread_mutex_unlock(&video->gpu_encoder_mutex);
  406. end:
  407. profile_end(output_gpu_encoders_name);
  408. }
  409. #endif
  410. static inline void render_video(struct obs_core_video *video, bool raw_active,
  411. const bool gpu_active, int cur_texture)
  412. {
  413. gs_begin_scene();
  414. gs_enable_depth_test(false);
  415. gs_set_cull_mode(GS_NEITHER);
  416. render_main_texture(video);
  417. if (raw_active || gpu_active) {
  418. gs_texture_t *const *convert_textures = video->convert_textures;
  419. gs_stagesurf_t *const *copy_surfaces =
  420. video->copy_surfaces[cur_texture];
  421. size_t channel_count = NUM_CHANNELS;
  422. gs_texture_t *texture = render_output_texture(video);
  423. #ifdef _WIN32
  424. if (gpu_active) {
  425. convert_textures = video->convert_textures_encode;
  426. copy_surfaces = video->copy_surfaces_encode;
  427. channel_count = 1;
  428. gs_flush();
  429. }
  430. #endif
  431. if (video->gpu_conversion)
  432. render_convert_texture(video, convert_textures,
  433. texture);
  434. #ifdef _WIN32
  435. if (gpu_active) {
  436. gs_flush();
  437. output_gpu_encoders(video, raw_active);
  438. }
  439. #endif
  440. if (raw_active)
  441. stage_output_texture(video, cur_texture,
  442. convert_textures, copy_surfaces,
  443. channel_count);
  444. }
  445. gs_set_render_target(NULL, NULL);
  446. gs_enable_blending(true);
  447. gs_end_scene();
  448. }
  449. static inline bool download_frame(struct obs_core_video *video,
  450. int prev_texture, struct video_data *frame)
  451. {
  452. if (!video->textures_copied[prev_texture])
  453. return false;
  454. for (int channel = 0; channel < NUM_CHANNELS; ++channel) {
  455. gs_stagesurf_t *surface =
  456. video->active_copy_surfaces[prev_texture][channel];
  457. if (surface) {
  458. if (!gs_stagesurface_map(surface, &frame->data[channel],
  459. &frame->linesize[channel]))
  460. return false;
  461. video->mapped_surfaces[channel] = surface;
  462. }
  463. }
  464. return true;
  465. }
  466. static const uint8_t *set_gpu_converted_plane(uint32_t width, uint32_t height,
  467. uint32_t linesize_input,
  468. uint32_t linesize_output,
  469. const uint8_t *in, uint8_t *out)
  470. {
  471. if ((width == linesize_input) && (width == linesize_output)) {
  472. size_t total = (size_t)width * (size_t)height;
  473. memcpy(out, in, total);
  474. in += total;
  475. } else {
  476. for (size_t y = 0; y < height; y++) {
  477. memcpy(out, in, width);
  478. out += linesize_output;
  479. in += linesize_input;
  480. }
  481. }
  482. return in;
  483. }
  484. static void set_gpu_converted_data(struct obs_core_video *video,
  485. struct video_frame *output,
  486. const struct video_data *input,
  487. const struct video_output_info *info)
  488. {
  489. switch (info->format) {
  490. case VIDEO_FORMAT_I420: {
  491. const uint32_t width = info->width;
  492. const uint32_t height = info->height;
  493. set_gpu_converted_plane(width, height, input->linesize[0],
  494. output->linesize[0], input->data[0],
  495. output->data[0]);
  496. const uint32_t width_d2 = width / 2;
  497. const uint32_t height_d2 = height / 2;
  498. set_gpu_converted_plane(width_d2, height_d2, input->linesize[1],
  499. output->linesize[1], input->data[1],
  500. output->data[1]);
  501. set_gpu_converted_plane(width_d2, height_d2, input->linesize[2],
  502. output->linesize[2], input->data[2],
  503. output->data[2]);
  504. break;
  505. }
  506. case VIDEO_FORMAT_NV12: {
  507. const uint32_t width = info->width;
  508. const uint32_t height = info->height;
  509. const uint32_t height_d2 = height / 2;
  510. if (input->linesize[1]) {
  511. set_gpu_converted_plane(width, height,
  512. input->linesize[0],
  513. output->linesize[0],
  514. input->data[0],
  515. output->data[0]);
  516. set_gpu_converted_plane(width, height_d2,
  517. input->linesize[1],
  518. output->linesize[1],
  519. input->data[1],
  520. output->data[1]);
  521. } else {
  522. const uint8_t *const in_uv = set_gpu_converted_plane(
  523. width, height, input->linesize[0],
  524. output->linesize[0], input->data[0],
  525. output->data[0]);
  526. set_gpu_converted_plane(width, height_d2,
  527. input->linesize[0],
  528. output->linesize[1], in_uv,
  529. output->data[1]);
  530. }
  531. break;
  532. }
  533. case VIDEO_FORMAT_I444: {
  534. const uint32_t width = info->width;
  535. const uint32_t height = info->height;
  536. set_gpu_converted_plane(width, height, input->linesize[0],
  537. output->linesize[0], input->data[0],
  538. output->data[0]);
  539. set_gpu_converted_plane(width, height, input->linesize[1],
  540. output->linesize[1], input->data[1],
  541. output->data[1]);
  542. set_gpu_converted_plane(width, height, input->linesize[2],
  543. output->linesize[2], input->data[2],
  544. output->data[2]);
  545. break;
  546. }
  547. case VIDEO_FORMAT_I010: {
  548. const uint32_t width = info->width;
  549. const uint32_t height = info->height;
  550. set_gpu_converted_plane(width * 2, height, input->linesize[0],
  551. output->linesize[0], input->data[0],
  552. output->data[0]);
  553. const uint32_t height_d2 = height / 2;
  554. set_gpu_converted_plane(width, height_d2, input->linesize[1],
  555. output->linesize[1], input->data[1],
  556. output->data[1]);
  557. set_gpu_converted_plane(width, height_d2, input->linesize[2],
  558. output->linesize[2], input->data[2],
  559. output->data[2]);
  560. break;
  561. }
  562. case VIDEO_FORMAT_P010: {
  563. const uint32_t width_x2 = info->width * 2;
  564. const uint32_t height = info->height;
  565. const uint32_t height_d2 = height / 2;
  566. if (input->linesize[1]) {
  567. set_gpu_converted_plane(width_x2, height,
  568. input->linesize[0],
  569. output->linesize[0],
  570. input->data[0],
  571. output->data[0]);
  572. set_gpu_converted_plane(width_x2, height_d2,
  573. input->linesize[1],
  574. output->linesize[1],
  575. input->data[1],
  576. output->data[1]);
  577. } else {
  578. const uint8_t *const in_uv = set_gpu_converted_plane(
  579. width_x2, height, input->linesize[0],
  580. output->linesize[0], input->data[0],
  581. output->data[0]);
  582. set_gpu_converted_plane(width_x2, height_d2,
  583. input->linesize[0],
  584. output->linesize[1], in_uv,
  585. output->data[1]);
  586. }
  587. break;
  588. }
  589. case VIDEO_FORMAT_NONE:
  590. case VIDEO_FORMAT_YVYU:
  591. case VIDEO_FORMAT_YUY2:
  592. case VIDEO_FORMAT_UYVY:
  593. case VIDEO_FORMAT_RGBA:
  594. case VIDEO_FORMAT_BGRA:
  595. case VIDEO_FORMAT_BGRX:
  596. case VIDEO_FORMAT_Y800:
  597. case VIDEO_FORMAT_BGR3:
  598. case VIDEO_FORMAT_I412:
  599. case VIDEO_FORMAT_I422:
  600. case VIDEO_FORMAT_I210:
  601. case VIDEO_FORMAT_I40A:
  602. case VIDEO_FORMAT_I42A:
  603. case VIDEO_FORMAT_YUVA:
  604. case VIDEO_FORMAT_YA2L:
  605. case VIDEO_FORMAT_AYUV:
  606. /* unimplemented */
  607. ;
  608. }
  609. }
  610. static inline void copy_rgbx_frame(struct video_frame *output,
  611. const struct video_data *input,
  612. const struct video_output_info *info)
  613. {
  614. uint8_t *in_ptr = input->data[0];
  615. uint8_t *out_ptr = output->data[0];
  616. /* if the line sizes match, do a single copy */
  617. if (input->linesize[0] == output->linesize[0]) {
  618. memcpy(out_ptr, in_ptr,
  619. (size_t)input->linesize[0] * (size_t)info->height);
  620. } else {
  621. const size_t copy_size = (size_t)info->width * 4;
  622. for (size_t y = 0; y < info->height; y++) {
  623. memcpy(out_ptr, in_ptr, copy_size);
  624. in_ptr += input->linesize[0];
  625. out_ptr += output->linesize[0];
  626. }
  627. }
  628. }
  629. static inline void output_video_data(struct obs_core_video *video,
  630. struct video_data *input_frame, int count)
  631. {
  632. const struct video_output_info *info;
  633. struct video_frame output_frame;
  634. bool locked;
  635. info = video_output_get_info(video->video);
  636. locked = video_output_lock_frame(video->video, &output_frame, count,
  637. input_frame->timestamp);
  638. if (locked) {
  639. if (video->gpu_conversion) {
  640. set_gpu_converted_data(video, &output_frame,
  641. input_frame, info);
  642. } else {
  643. copy_rgbx_frame(&output_frame, input_frame, info);
  644. }
  645. video_output_unlock_frame(video->video);
  646. }
  647. }
  648. static inline void video_sleep(struct obs_core_video *video, bool raw_active,
  649. const bool gpu_active, uint64_t *p_time,
  650. uint64_t interval_ns)
  651. {
  652. struct obs_vframe_info vframe_info;
  653. uint64_t cur_time = *p_time;
  654. uint64_t t = cur_time + interval_ns;
  655. int count;
  656. if (os_sleepto_ns(t)) {
  657. *p_time = t;
  658. count = 1;
  659. } else {
  660. const uint64_t udiff = os_gettime_ns() - cur_time;
  661. int64_t diff;
  662. memcpy(&diff, &udiff, sizeof(diff));
  663. const uint64_t clamped_diff = (diff > (int64_t)interval_ns)
  664. ? (uint64_t)diff
  665. : interval_ns;
  666. count = (int)(clamped_diff / interval_ns);
  667. *p_time = cur_time + interval_ns * count;
  668. }
  669. video->total_frames += count;
  670. video->lagged_frames += count - 1;
  671. vframe_info.timestamp = cur_time;
  672. vframe_info.count = count;
  673. if (raw_active)
  674. circlebuf_push_back(&video->vframe_info_buffer, &vframe_info,
  675. sizeof(vframe_info));
  676. if (gpu_active)
  677. circlebuf_push_back(&video->vframe_info_buffer_gpu,
  678. &vframe_info, sizeof(vframe_info));
  679. }
  680. static const char *output_frame_gs_context_name = "gs_context(video->graphics)";
  681. static const char *output_frame_render_video_name = "render_video";
  682. static const char *output_frame_download_frame_name = "download_frame";
  683. static const char *output_frame_gs_flush_name = "gs_flush";
  684. static const char *output_frame_output_video_data_name = "output_video_data";
  685. static inline void output_frame(bool raw_active, const bool gpu_active)
  686. {
  687. struct obs_core_video *video = &obs->video;
  688. int cur_texture = video->cur_texture;
  689. int prev_texture = cur_texture == 0 ? NUM_TEXTURES - 1
  690. : cur_texture - 1;
  691. struct video_data frame;
  692. bool frame_ready = 0;
  693. memset(&frame, 0, sizeof(struct video_data));
  694. profile_start(output_frame_gs_context_name);
  695. gs_enter_context(video->graphics);
  696. profile_start(output_frame_render_video_name);
  697. GS_DEBUG_MARKER_BEGIN(GS_DEBUG_COLOR_RENDER_VIDEO,
  698. output_frame_render_video_name);
  699. render_video(video, raw_active, gpu_active, cur_texture);
  700. GS_DEBUG_MARKER_END();
  701. profile_end(output_frame_render_video_name);
  702. if (raw_active) {
  703. profile_start(output_frame_download_frame_name);
  704. frame_ready = download_frame(video, prev_texture, &frame);
  705. profile_end(output_frame_download_frame_name);
  706. }
  707. profile_start(output_frame_gs_flush_name);
  708. gs_flush();
  709. profile_end(output_frame_gs_flush_name);
  710. gs_leave_context();
  711. profile_end(output_frame_gs_context_name);
  712. if (raw_active && frame_ready) {
  713. struct obs_vframe_info vframe_info;
  714. circlebuf_pop_front(&video->vframe_info_buffer, &vframe_info,
  715. sizeof(vframe_info));
  716. frame.timestamp = vframe_info.timestamp;
  717. profile_start(output_frame_output_video_data_name);
  718. output_video_data(video, &frame, vframe_info.count);
  719. profile_end(output_frame_output_video_data_name);
  720. }
  721. if (++video->cur_texture == NUM_TEXTURES)
  722. video->cur_texture = 0;
  723. }
  724. #define NBSP "\xC2\xA0"
  725. static void clear_base_frame_data(void)
  726. {
  727. struct obs_core_video *video = &obs->video;
  728. video->texture_rendered = false;
  729. video->texture_converted = false;
  730. circlebuf_free(&video->vframe_info_buffer);
  731. video->cur_texture = 0;
  732. }
  733. static void clear_raw_frame_data(void)
  734. {
  735. struct obs_core_video *video = &obs->video;
  736. memset(video->textures_copied, 0, sizeof(video->textures_copied));
  737. circlebuf_free(&video->vframe_info_buffer);
  738. }
  739. #ifdef _WIN32
  740. static void clear_gpu_frame_data(void)
  741. {
  742. struct obs_core_video *video = &obs->video;
  743. circlebuf_free(&video->vframe_info_buffer_gpu);
  744. }
  745. #endif
  746. extern THREAD_LOCAL bool is_graphics_thread;
  747. static void execute_graphics_tasks(void)
  748. {
  749. struct obs_core_video *video = &obs->video;
  750. bool tasks_remaining = true;
  751. while (tasks_remaining) {
  752. pthread_mutex_lock(&video->task_mutex);
  753. if (video->tasks.size) {
  754. struct obs_task_info info;
  755. circlebuf_pop_front(&video->tasks, &info, sizeof(info));
  756. info.task(info.param);
  757. }
  758. tasks_remaining = !!video->tasks.size;
  759. pthread_mutex_unlock(&video->task_mutex);
  760. }
  761. }
  762. #ifdef _WIN32
  763. struct winrt_exports {
  764. void (*winrt_initialize)();
  765. void (*winrt_uninitialize)();
  766. struct winrt_disaptcher *(*winrt_dispatcher_init)();
  767. void (*winrt_dispatcher_free)(struct winrt_disaptcher *dispatcher);
  768. void (*winrt_capture_thread_start)();
  769. void (*winrt_capture_thread_stop)();
  770. };
  771. #define WINRT_IMPORT(func) \
  772. do { \
  773. exports->func = os_dlsym(module, #func); \
  774. if (!exports->func) { \
  775. success = false; \
  776. blog(LOG_ERROR, \
  777. "Could not load function '%s' from " \
  778. "module '%s'", \
  779. #func, module_name); \
  780. } \
  781. } while (false)
  782. static bool load_winrt_imports(struct winrt_exports *exports, void *module,
  783. const char *module_name)
  784. {
  785. bool success = true;
  786. WINRT_IMPORT(winrt_initialize);
  787. WINRT_IMPORT(winrt_uninitialize);
  788. WINRT_IMPORT(winrt_dispatcher_init);
  789. WINRT_IMPORT(winrt_dispatcher_free);
  790. WINRT_IMPORT(winrt_capture_thread_start);
  791. WINRT_IMPORT(winrt_capture_thread_stop);
  792. return success;
  793. }
  794. struct winrt_state {
  795. bool loaded;
  796. void *winrt_module;
  797. struct winrt_exports exports;
  798. struct winrt_disaptcher *dispatcher;
  799. };
  800. static void init_winrt_state(struct winrt_state *winrt)
  801. {
  802. static const char *const module_name = "libobs-winrt";
  803. winrt->winrt_module = os_dlopen(module_name);
  804. winrt->loaded = winrt->winrt_module &&
  805. load_winrt_imports(&winrt->exports, winrt->winrt_module,
  806. module_name);
  807. winrt->dispatcher = NULL;
  808. if (winrt->loaded) {
  809. winrt->exports.winrt_initialize();
  810. winrt->dispatcher = winrt->exports.winrt_dispatcher_init();
  811. gs_enter_context(obs->video.graphics);
  812. winrt->exports.winrt_capture_thread_start();
  813. gs_leave_context();
  814. }
  815. }
  816. static void uninit_winrt_state(struct winrt_state *winrt)
  817. {
  818. if (winrt->winrt_module) {
  819. if (winrt->loaded) {
  820. winrt->exports.winrt_capture_thread_stop();
  821. if (winrt->dispatcher)
  822. winrt->exports.winrt_dispatcher_free(
  823. winrt->dispatcher);
  824. winrt->exports.winrt_uninitialize();
  825. }
  826. os_dlclose(winrt->winrt_module);
  827. }
  828. }
  829. #endif // #ifdef _WIN32
  830. static const char *tick_sources_name = "tick_sources";
  831. static const char *render_displays_name = "render_displays";
  832. static const char *output_frame_name = "output_frame";
  833. bool obs_graphics_thread_loop(struct obs_graphics_context *context)
  834. {
  835. /* defer loop break to clean up sources */
  836. const bool stop_requested = video_output_stopped(obs->video.video);
  837. uint64_t frame_start = os_gettime_ns();
  838. uint64_t frame_time_ns;
  839. bool raw_active = os_atomic_load_long(&obs->video.raw_active) > 0;
  840. #ifdef _WIN32
  841. const bool gpu_active =
  842. os_atomic_load_long(&obs->video.gpu_encoder_active) > 0;
  843. const bool active = raw_active || gpu_active;
  844. #else
  845. const bool gpu_active = 0;
  846. const bool active = raw_active;
  847. #endif
  848. if (!context->was_active && active)
  849. clear_base_frame_data();
  850. if (!context->raw_was_active && raw_active)
  851. clear_raw_frame_data();
  852. #ifdef _WIN32
  853. if (!context->gpu_was_active && gpu_active)
  854. clear_gpu_frame_data();
  855. context->gpu_was_active = gpu_active;
  856. #endif
  857. context->raw_was_active = raw_active;
  858. context->was_active = active;
  859. profile_start(context->video_thread_name);
  860. gs_enter_context(obs->video.graphics);
  861. gs_begin_frame();
  862. gs_leave_context();
  863. profile_start(tick_sources_name);
  864. context->last_time =
  865. tick_sources(obs->video.video_time, context->last_time);
  866. profile_end(tick_sources_name);
  867. #ifdef _WIN32
  868. MSG msg;
  869. while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) {
  870. TranslateMessage(&msg);
  871. DispatchMessage(&msg);
  872. }
  873. #endif
  874. profile_start(output_frame_name);
  875. output_frame(raw_active, gpu_active);
  876. profile_end(output_frame_name);
  877. profile_start(render_displays_name);
  878. render_displays();
  879. profile_end(render_displays_name);
  880. execute_graphics_tasks();
  881. frame_time_ns = os_gettime_ns() - frame_start;
  882. profile_end(context->video_thread_name);
  883. profile_reenable_thread();
  884. video_sleep(&obs->video, raw_active, gpu_active, &obs->video.video_time,
  885. context->interval);
  886. context->frame_time_total_ns += frame_time_ns;
  887. context->fps_total_ns += (obs->video.video_time - context->last_time);
  888. context->fps_total_frames++;
  889. if (context->fps_total_ns >= 1000000000ULL) {
  890. obs->video.video_fps =
  891. (double)context->fps_total_frames /
  892. ((double)context->fps_total_ns / 1000000000.0);
  893. obs->video.video_avg_frame_time_ns =
  894. context->frame_time_total_ns /
  895. (uint64_t)context->fps_total_frames;
  896. context->frame_time_total_ns = 0;
  897. context->fps_total_ns = 0;
  898. context->fps_total_frames = 0;
  899. }
  900. return !stop_requested;
  901. }
  902. void *obs_graphics_thread(void *param)
  903. {
  904. #ifdef _WIN32
  905. struct winrt_state winrt;
  906. init_winrt_state(&winrt);
  907. #endif // #ifdef _WIN32
  908. is_graphics_thread = true;
  909. const uint64_t interval = video_output_get_frame_time(obs->video.video);
  910. obs->video.video_time = os_gettime_ns();
  911. obs->video.video_frame_interval_ns = interval;
  912. os_set_thread_name("libobs: graphics thread");
  913. const char *video_thread_name = profile_store_name(
  914. obs_get_profiler_name_store(),
  915. "obs_graphics_thread(%g" NBSP "ms)", interval / 1000000.);
  916. profile_register_root(video_thread_name, interval);
  917. srand((unsigned int)time(NULL));
  918. struct obs_graphics_context context;
  919. context.interval = video_output_get_frame_time(obs->video.video);
  920. context.frame_time_total_ns = 0;
  921. context.fps_total_ns = 0;
  922. context.fps_total_frames = 0;
  923. context.last_time = 0;
  924. #ifdef _WIN32
  925. context.gpu_was_active = false;
  926. #endif
  927. context.raw_was_active = false;
  928. context.was_active = false;
  929. context.video_thread_name = video_thread_name;
  930. #ifdef __APPLE__
  931. while (obs_graphics_thread_loop_autorelease(&context))
  932. #else
  933. while (obs_graphics_thread_loop(&context))
  934. #endif
  935. ;
  936. #ifdef _WIN32
  937. uninit_winrt_state(&winrt);
  938. #endif
  939. UNUSED_PARAMETER(param);
  940. return NULL;
  941. }