obs-video.c 28 KB

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