1
0

obs-video.c 31 KB

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