1
0

obs-video.c 34 KB

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