obs-video.c 36 KB

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