obs-video.c 33 KB

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