obs-video.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071
  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. struct vec4 vec0, vec1, vec2;
  250. vec4_set(&vec0, video->color_matrix[4], video->color_matrix[5],
  251. video->color_matrix[6], video->color_matrix[7]);
  252. vec4_set(&vec1, video->color_matrix[0], video->color_matrix[1],
  253. video->color_matrix[2], video->color_matrix[3]);
  254. vec4_set(&vec2, video->color_matrix[8], video->color_matrix[9],
  255. video->color_matrix[10], video->color_matrix[11]);
  256. gs_enable_blending(false);
  257. if (convert_textures[0]) {
  258. gs_effect_set_texture(image, texture);
  259. gs_effect_set_vec4(color_vec0, &vec0);
  260. render_convert_plane(effect, convert_textures[0],
  261. video->conversion_techs[0]);
  262. if (convert_textures[1]) {
  263. gs_effect_set_texture(image, texture);
  264. gs_effect_set_vec4(color_vec1, &vec1);
  265. if (!convert_textures[2])
  266. gs_effect_set_vec4(color_vec2, &vec2);
  267. gs_effect_set_float(width_i, video->conversion_width_i);
  268. render_convert_plane(effect, convert_textures[1],
  269. video->conversion_techs[1]);
  270. if (convert_textures[2]) {
  271. gs_effect_set_texture(image, texture);
  272. gs_effect_set_vec4(color_vec2, &vec2);
  273. gs_effect_set_float(width_i,
  274. video->conversion_width_i);
  275. render_convert_plane(
  276. effect, convert_textures[2],
  277. video->conversion_techs[2]);
  278. }
  279. }
  280. }
  281. gs_enable_blending(true);
  282. video->texture_converted = true;
  283. profile_end(render_convert_texture_name);
  284. }
  285. static const char *stage_output_texture_name = "stage_output_texture";
  286. static inline void
  287. stage_output_texture(struct obs_core_video *video, int cur_texture,
  288. gs_texture_t *const *const convert_textures,
  289. gs_stagesurf_t *const *const copy_surfaces,
  290. size_t channel_count)
  291. {
  292. profile_start(stage_output_texture_name);
  293. unmap_last_surface(video);
  294. if (!video->gpu_conversion) {
  295. gs_stagesurf_t *copy = copy_surfaces[0];
  296. if (copy) {
  297. gs_stage_texture(copy, video->output_texture);
  298. video->active_copy_surfaces[cur_texture][0] = copy;
  299. }
  300. video->textures_copied[cur_texture] = true;
  301. } else if (video->texture_converted) {
  302. for (int i = 0; i < channel_count; i++) {
  303. gs_stagesurf_t *copy = copy_surfaces[i];
  304. if (copy) {
  305. gs_stage_texture(copy, convert_textures[i]);
  306. video->active_copy_surfaces[cur_texture][i] =
  307. copy;
  308. }
  309. }
  310. video->textures_copied[cur_texture] = true;
  311. }
  312. profile_end(stage_output_texture_name);
  313. }
  314. #ifdef _WIN32
  315. static inline bool queue_frame(struct obs_core_video *video, bool raw_active,
  316. struct obs_vframe_info *vframe_info)
  317. {
  318. bool duplicate =
  319. !video->gpu_encoder_avail_queue.size ||
  320. (video->gpu_encoder_queue.size && vframe_info->count > 1);
  321. if (duplicate) {
  322. struct obs_tex_frame *tf = circlebuf_data(
  323. &video->gpu_encoder_queue,
  324. video->gpu_encoder_queue.size - sizeof(*tf));
  325. /* texture-based encoding is stopping */
  326. if (!tf) {
  327. return false;
  328. }
  329. tf->count++;
  330. os_sem_post(video->gpu_encode_semaphore);
  331. goto finish;
  332. }
  333. struct obs_tex_frame tf;
  334. circlebuf_pop_front(&video->gpu_encoder_avail_queue, &tf, sizeof(tf));
  335. if (tf.released) {
  336. gs_texture_acquire_sync(tf.tex, tf.lock_key, GS_WAIT_INFINITE);
  337. tf.released = false;
  338. }
  339. /* the vframe_info->count > 1 case causing a copy can only happen if by
  340. * some chance the very first frame has to be duplicated for whatever
  341. * reason. otherwise, it goes to the 'duplicate' case above, which
  342. * will ensure better performance. */
  343. if (raw_active || vframe_info->count > 1) {
  344. gs_copy_texture(tf.tex, video->convert_textures_encode[0]);
  345. } else {
  346. gs_texture_t *tex = video->convert_textures_encode[0];
  347. gs_texture_t *tex_uv = video->convert_textures_encode[1];
  348. video->convert_textures_encode[0] = tf.tex;
  349. video->convert_textures_encode[1] = tf.tex_uv;
  350. tf.tex = tex;
  351. tf.tex_uv = tex_uv;
  352. }
  353. tf.count = 1;
  354. tf.timestamp = vframe_info->timestamp;
  355. tf.released = true;
  356. tf.handle = gs_texture_get_shared_handle(tf.tex);
  357. gs_texture_release_sync(tf.tex, ++tf.lock_key);
  358. circlebuf_push_back(&video->gpu_encoder_queue, &tf, sizeof(tf));
  359. os_sem_post(video->gpu_encode_semaphore);
  360. finish:
  361. return --vframe_info->count;
  362. }
  363. extern void full_stop(struct obs_encoder *encoder);
  364. static inline void encode_gpu(struct obs_core_video *video, bool raw_active,
  365. struct obs_vframe_info *vframe_info)
  366. {
  367. while (queue_frame(video, raw_active, vframe_info))
  368. ;
  369. }
  370. static const char *output_gpu_encoders_name = "output_gpu_encoders";
  371. static void output_gpu_encoders(struct obs_core_video *video, bool raw_active)
  372. {
  373. profile_start(output_gpu_encoders_name);
  374. if (!video->texture_converted)
  375. goto end;
  376. if (!video->vframe_info_buffer_gpu.size)
  377. goto end;
  378. struct obs_vframe_info vframe_info;
  379. circlebuf_pop_front(&video->vframe_info_buffer_gpu, &vframe_info,
  380. sizeof(vframe_info));
  381. pthread_mutex_lock(&video->gpu_encoder_mutex);
  382. encode_gpu(video, raw_active, &vframe_info);
  383. pthread_mutex_unlock(&video->gpu_encoder_mutex);
  384. end:
  385. profile_end(output_gpu_encoders_name);
  386. }
  387. #endif
  388. static inline void render_video(struct obs_core_video *video, bool raw_active,
  389. const bool gpu_active, int cur_texture)
  390. {
  391. gs_begin_scene();
  392. gs_enable_depth_test(false);
  393. gs_set_cull_mode(GS_NEITHER);
  394. render_main_texture(video);
  395. if (raw_active || gpu_active) {
  396. gs_texture_t *const *convert_textures = video->convert_textures;
  397. gs_stagesurf_t *const *copy_surfaces =
  398. video->copy_surfaces[cur_texture];
  399. size_t channel_count = NUM_CHANNELS;
  400. gs_texture_t *texture = render_output_texture(video);
  401. #ifdef _WIN32
  402. if (gpu_active) {
  403. convert_textures = video->convert_textures_encode;
  404. copy_surfaces = video->copy_surfaces_encode;
  405. channel_count = 1;
  406. gs_flush();
  407. }
  408. #endif
  409. if (video->gpu_conversion)
  410. render_convert_texture(video, convert_textures,
  411. texture);
  412. #ifdef _WIN32
  413. if (gpu_active) {
  414. gs_flush();
  415. output_gpu_encoders(video, raw_active);
  416. }
  417. #endif
  418. if (raw_active)
  419. stage_output_texture(video, cur_texture,
  420. convert_textures, copy_surfaces,
  421. channel_count);
  422. }
  423. gs_set_render_target(NULL, NULL);
  424. gs_enable_blending(true);
  425. gs_end_scene();
  426. }
  427. static inline bool download_frame(struct obs_core_video *video,
  428. int prev_texture, struct video_data *frame)
  429. {
  430. if (!video->textures_copied[prev_texture])
  431. return false;
  432. for (int channel = 0; channel < NUM_CHANNELS; ++channel) {
  433. gs_stagesurf_t *surface =
  434. video->active_copy_surfaces[prev_texture][channel];
  435. if (surface) {
  436. if (!gs_stagesurface_map(surface, &frame->data[channel],
  437. &frame->linesize[channel]))
  438. return false;
  439. video->mapped_surfaces[channel] = surface;
  440. }
  441. }
  442. return true;
  443. }
  444. static const uint8_t *set_gpu_converted_plane(uint32_t width, uint32_t height,
  445. uint32_t linesize_input,
  446. uint32_t linesize_output,
  447. const uint8_t *in, uint8_t *out)
  448. {
  449. if ((width == linesize_input) && (width == linesize_output)) {
  450. size_t total = (size_t)width * (size_t)height;
  451. memcpy(out, in, total);
  452. in += total;
  453. } else {
  454. for (size_t y = 0; y < height; y++) {
  455. memcpy(out, in, width);
  456. out += linesize_output;
  457. in += linesize_input;
  458. }
  459. }
  460. return in;
  461. }
  462. static void set_gpu_converted_data(struct obs_core_video *video,
  463. struct video_frame *output,
  464. const struct video_data *input,
  465. const struct video_output_info *info)
  466. {
  467. switch (info->format) {
  468. case VIDEO_FORMAT_I420: {
  469. const uint32_t width = info->width;
  470. const uint32_t height = info->height;
  471. set_gpu_converted_plane(width, height, input->linesize[0],
  472. output->linesize[0], input->data[0],
  473. output->data[0]);
  474. const uint32_t width_d2 = width / 2;
  475. const uint32_t height_d2 = height / 2;
  476. set_gpu_converted_plane(width_d2, height_d2, input->linesize[1],
  477. output->linesize[1], input->data[1],
  478. output->data[1]);
  479. set_gpu_converted_plane(width_d2, height_d2, input->linesize[2],
  480. output->linesize[2], input->data[2],
  481. output->data[2]);
  482. break;
  483. }
  484. case VIDEO_FORMAT_NV12: {
  485. const uint32_t width = info->width;
  486. const uint32_t height = info->height;
  487. const uint32_t height_d2 = height / 2;
  488. if (input->linesize[1]) {
  489. set_gpu_converted_plane(width, height,
  490. input->linesize[0],
  491. output->linesize[0],
  492. input->data[0],
  493. output->data[0]);
  494. set_gpu_converted_plane(width, height_d2,
  495. input->linesize[1],
  496. output->linesize[1],
  497. input->data[1],
  498. output->data[1]);
  499. } else {
  500. const uint8_t *const in_uv = set_gpu_converted_plane(
  501. width, height, input->linesize[0],
  502. output->linesize[0], input->data[0],
  503. output->data[0]);
  504. set_gpu_converted_plane(width, height_d2,
  505. input->linesize[0],
  506. output->linesize[1], in_uv,
  507. output->data[1]);
  508. }
  509. break;
  510. }
  511. case VIDEO_FORMAT_I444: {
  512. const uint32_t width = info->width;
  513. const uint32_t height = info->height;
  514. set_gpu_converted_plane(width, height, input->linesize[0],
  515. output->linesize[0], input->data[0],
  516. output->data[0]);
  517. set_gpu_converted_plane(width, height, input->linesize[1],
  518. output->linesize[1], input->data[1],
  519. output->data[1]);
  520. set_gpu_converted_plane(width, height, input->linesize[2],
  521. output->linesize[2], input->data[2],
  522. output->data[2]);
  523. break;
  524. }
  525. case VIDEO_FORMAT_NONE:
  526. case VIDEO_FORMAT_YVYU:
  527. case VIDEO_FORMAT_YUY2:
  528. case VIDEO_FORMAT_UYVY:
  529. case VIDEO_FORMAT_RGBA:
  530. case VIDEO_FORMAT_BGRA:
  531. case VIDEO_FORMAT_BGRX:
  532. case VIDEO_FORMAT_Y800:
  533. case VIDEO_FORMAT_BGR3:
  534. case VIDEO_FORMAT_I422:
  535. case VIDEO_FORMAT_I40A:
  536. case VIDEO_FORMAT_I42A:
  537. case VIDEO_FORMAT_YUVA:
  538. case VIDEO_FORMAT_AYUV:
  539. /* unimplemented */
  540. ;
  541. }
  542. }
  543. static inline void copy_rgbx_frame(struct video_frame *output,
  544. const struct video_data *input,
  545. const struct video_output_info *info)
  546. {
  547. uint8_t *in_ptr = input->data[0];
  548. uint8_t *out_ptr = output->data[0];
  549. /* if the line sizes match, do a single copy */
  550. if (input->linesize[0] == output->linesize[0]) {
  551. memcpy(out_ptr, in_ptr,
  552. (size_t)input->linesize[0] * (size_t)info->height);
  553. } else {
  554. const size_t copy_size = (size_t)info->width * 4;
  555. for (size_t y = 0; y < info->height; y++) {
  556. memcpy(out_ptr, in_ptr, copy_size);
  557. in_ptr += input->linesize[0];
  558. out_ptr += output->linesize[0];
  559. }
  560. }
  561. }
  562. static inline void output_video_data(struct obs_core_video *video,
  563. struct video_data *input_frame, int count)
  564. {
  565. const struct video_output_info *info;
  566. struct video_frame output_frame;
  567. bool locked;
  568. info = video_output_get_info(video->video);
  569. locked = video_output_lock_frame(video->video, &output_frame, count,
  570. input_frame->timestamp);
  571. if (locked) {
  572. if (video->gpu_conversion) {
  573. set_gpu_converted_data(video, &output_frame,
  574. input_frame, info);
  575. } else {
  576. copy_rgbx_frame(&output_frame, input_frame, info);
  577. }
  578. video_output_unlock_frame(video->video);
  579. }
  580. }
  581. static inline void video_sleep(struct obs_core_video *video, bool raw_active,
  582. const bool gpu_active, uint64_t *p_time,
  583. uint64_t interval_ns)
  584. {
  585. struct obs_vframe_info vframe_info;
  586. uint64_t cur_time = *p_time;
  587. uint64_t t = cur_time + interval_ns;
  588. int count;
  589. if (os_sleepto_ns(t)) {
  590. *p_time = t;
  591. count = 1;
  592. } else {
  593. const uint64_t udiff = os_gettime_ns() - cur_time;
  594. int64_t diff;
  595. memcpy(&diff, &udiff, sizeof(diff));
  596. const uint64_t clamped_diff =
  597. (diff > (int64_t)interval_ns) ? diff : interval_ns;
  598. count = (int)(clamped_diff / interval_ns);
  599. *p_time = cur_time + interval_ns * count;
  600. }
  601. video->total_frames += count;
  602. video->lagged_frames += count - 1;
  603. vframe_info.timestamp = cur_time;
  604. vframe_info.count = count;
  605. if (raw_active)
  606. circlebuf_push_back(&video->vframe_info_buffer, &vframe_info,
  607. sizeof(vframe_info));
  608. if (gpu_active)
  609. circlebuf_push_back(&video->vframe_info_buffer_gpu,
  610. &vframe_info, sizeof(vframe_info));
  611. }
  612. static const char *output_frame_gs_context_name = "gs_context(video->graphics)";
  613. static const char *output_frame_render_video_name = "render_video";
  614. static const char *output_frame_download_frame_name = "download_frame";
  615. static const char *output_frame_gs_flush_name = "gs_flush";
  616. static const char *output_frame_output_video_data_name = "output_video_data";
  617. static inline void output_frame(bool raw_active, const bool gpu_active)
  618. {
  619. struct obs_core_video *video = &obs->video;
  620. int cur_texture = video->cur_texture;
  621. int prev_texture = cur_texture == 0 ? NUM_TEXTURES - 1
  622. : cur_texture - 1;
  623. struct video_data frame;
  624. bool frame_ready = 0;
  625. memset(&frame, 0, sizeof(struct video_data));
  626. profile_start(output_frame_gs_context_name);
  627. gs_enter_context(video->graphics);
  628. profile_start(output_frame_render_video_name);
  629. GS_DEBUG_MARKER_BEGIN(GS_DEBUG_COLOR_RENDER_VIDEO,
  630. output_frame_render_video_name);
  631. render_video(video, raw_active, gpu_active, cur_texture);
  632. GS_DEBUG_MARKER_END();
  633. profile_end(output_frame_render_video_name);
  634. if (raw_active) {
  635. profile_start(output_frame_download_frame_name);
  636. frame_ready = download_frame(video, prev_texture, &frame);
  637. profile_end(output_frame_download_frame_name);
  638. }
  639. profile_start(output_frame_gs_flush_name);
  640. gs_flush();
  641. profile_end(output_frame_gs_flush_name);
  642. gs_leave_context();
  643. profile_end(output_frame_gs_context_name);
  644. if (raw_active && frame_ready) {
  645. struct obs_vframe_info vframe_info;
  646. circlebuf_pop_front(&video->vframe_info_buffer, &vframe_info,
  647. sizeof(vframe_info));
  648. frame.timestamp = vframe_info.timestamp;
  649. profile_start(output_frame_output_video_data_name);
  650. output_video_data(video, &frame, vframe_info.count);
  651. profile_end(output_frame_output_video_data_name);
  652. }
  653. if (++video->cur_texture == NUM_TEXTURES)
  654. video->cur_texture = 0;
  655. }
  656. #define NBSP "\xC2\xA0"
  657. static void clear_base_frame_data(void)
  658. {
  659. struct obs_core_video *video = &obs->video;
  660. video->texture_rendered = false;
  661. video->texture_converted = false;
  662. circlebuf_free(&video->vframe_info_buffer);
  663. video->cur_texture = 0;
  664. }
  665. static void clear_raw_frame_data(void)
  666. {
  667. struct obs_core_video *video = &obs->video;
  668. memset(video->textures_copied, 0, sizeof(video->textures_copied));
  669. circlebuf_free(&video->vframe_info_buffer);
  670. }
  671. #ifdef _WIN32
  672. static void clear_gpu_frame_data(void)
  673. {
  674. struct obs_core_video *video = &obs->video;
  675. circlebuf_free(&video->vframe_info_buffer_gpu);
  676. }
  677. #endif
  678. extern THREAD_LOCAL bool is_graphics_thread;
  679. static void execute_graphics_tasks(void)
  680. {
  681. struct obs_core_video *video = &obs->video;
  682. bool tasks_remaining = true;
  683. while (tasks_remaining) {
  684. pthread_mutex_lock(&video->task_mutex);
  685. if (video->tasks.size) {
  686. struct obs_task_info info;
  687. circlebuf_pop_front(&video->tasks, &info, sizeof(info));
  688. info.task(info.param);
  689. }
  690. tasks_remaining = !!video->tasks.size;
  691. pthread_mutex_unlock(&video->task_mutex);
  692. }
  693. }
  694. #ifdef _WIN32
  695. struct winrt_exports {
  696. void (*winrt_initialize)();
  697. void (*winrt_uninitialize)();
  698. struct winrt_disaptcher *(*winrt_dispatcher_init)();
  699. void (*winrt_dispatcher_free)(struct winrt_disaptcher *dispatcher);
  700. void (*winrt_capture_thread_start)();
  701. void (*winrt_capture_thread_stop)();
  702. };
  703. #define WINRT_IMPORT(func) \
  704. do { \
  705. exports->func = os_dlsym(module, #func); \
  706. if (!exports->func) { \
  707. success = false; \
  708. blog(LOG_ERROR, \
  709. "Could not load function '%s' from " \
  710. "module '%s'", \
  711. #func, module_name); \
  712. } \
  713. } while (false)
  714. static bool load_winrt_imports(struct winrt_exports *exports, void *module,
  715. const char *module_name)
  716. {
  717. bool success = true;
  718. WINRT_IMPORT(winrt_initialize);
  719. WINRT_IMPORT(winrt_uninitialize);
  720. WINRT_IMPORT(winrt_dispatcher_init);
  721. WINRT_IMPORT(winrt_dispatcher_free);
  722. WINRT_IMPORT(winrt_capture_thread_start);
  723. WINRT_IMPORT(winrt_capture_thread_stop);
  724. return success;
  725. }
  726. struct winrt_state {
  727. bool loaded;
  728. void *winrt_module;
  729. struct winrt_exports exports;
  730. struct winrt_disaptcher *dispatcher;
  731. };
  732. static void init_winrt_state(struct winrt_state *winrt)
  733. {
  734. static const char *const module_name = "libobs-winrt";
  735. winrt->winrt_module = os_dlopen(module_name);
  736. winrt->loaded = winrt->winrt_module &&
  737. load_winrt_imports(&winrt->exports, winrt->winrt_module,
  738. module_name);
  739. winrt->dispatcher = NULL;
  740. if (winrt->loaded) {
  741. winrt->exports.winrt_initialize();
  742. winrt->dispatcher = winrt->exports.winrt_dispatcher_init();
  743. gs_enter_context(obs->video.graphics);
  744. winrt->exports.winrt_capture_thread_start();
  745. gs_leave_context();
  746. }
  747. }
  748. static void uninit_winrt_state(struct winrt_state *winrt)
  749. {
  750. if (winrt->winrt_module) {
  751. if (winrt->loaded) {
  752. winrt->exports.winrt_capture_thread_stop();
  753. if (winrt->dispatcher)
  754. winrt->exports.winrt_dispatcher_free(
  755. winrt->dispatcher);
  756. winrt->exports.winrt_uninitialize();
  757. }
  758. os_dlclose(winrt->winrt_module);
  759. }
  760. }
  761. #endif // #ifdef _WIN32
  762. static const char *tick_sources_name = "tick_sources";
  763. static const char *render_displays_name = "render_displays";
  764. static const char *output_frame_name = "output_frame";
  765. bool obs_graphics_thread_loop(struct obs_graphics_context *context)
  766. {
  767. /* defer loop break to clean up sources */
  768. const bool stop_requested = video_output_stopped(obs->video.video);
  769. uint64_t frame_start = os_gettime_ns();
  770. uint64_t frame_time_ns;
  771. bool raw_active = os_atomic_load_long(&obs->video.raw_active) > 0;
  772. #ifdef _WIN32
  773. const bool gpu_active =
  774. os_atomic_load_long(&obs->video.gpu_encoder_active) > 0;
  775. const bool active = raw_active || gpu_active;
  776. #else
  777. const bool gpu_active = 0;
  778. const bool active = raw_active;
  779. #endif
  780. if (!context->was_active && active)
  781. clear_base_frame_data();
  782. if (!context->raw_was_active && raw_active)
  783. clear_raw_frame_data();
  784. #ifdef _WIN32
  785. if (!context->gpu_was_active && gpu_active)
  786. clear_gpu_frame_data();
  787. context->gpu_was_active = gpu_active;
  788. #endif
  789. context->raw_was_active = raw_active;
  790. context->was_active = active;
  791. profile_start(context->video_thread_name);
  792. gs_enter_context(obs->video.graphics);
  793. gs_begin_frame();
  794. gs_leave_context();
  795. profile_start(tick_sources_name);
  796. context->last_time =
  797. tick_sources(obs->video.video_time, context->last_time);
  798. profile_end(tick_sources_name);
  799. #ifdef _WIN32
  800. MSG msg;
  801. while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) {
  802. TranslateMessage(&msg);
  803. DispatchMessage(&msg);
  804. }
  805. #endif
  806. profile_start(output_frame_name);
  807. output_frame(raw_active, gpu_active);
  808. profile_end(output_frame_name);
  809. profile_start(render_displays_name);
  810. render_displays();
  811. profile_end(render_displays_name);
  812. execute_graphics_tasks();
  813. frame_time_ns = os_gettime_ns() - frame_start;
  814. profile_end(context->video_thread_name);
  815. profile_reenable_thread();
  816. video_sleep(&obs->video, raw_active, gpu_active, &obs->video.video_time,
  817. context->interval);
  818. context->frame_time_total_ns += frame_time_ns;
  819. context->fps_total_ns += (obs->video.video_time - context->last_time);
  820. context->fps_total_frames++;
  821. if (context->fps_total_ns >= 1000000000ULL) {
  822. obs->video.video_fps =
  823. (double)context->fps_total_frames /
  824. ((double)context->fps_total_ns / 1000000000.0);
  825. obs->video.video_avg_frame_time_ns =
  826. context->frame_time_total_ns /
  827. (uint64_t)context->fps_total_frames;
  828. context->frame_time_total_ns = 0;
  829. context->fps_total_ns = 0;
  830. context->fps_total_frames = 0;
  831. }
  832. return !stop_requested;
  833. }
  834. void *obs_graphics_thread(void *param)
  835. {
  836. #ifdef _WIN32
  837. struct winrt_state winrt;
  838. init_winrt_state(&winrt);
  839. #endif // #ifdef _WIN32
  840. is_graphics_thread = true;
  841. const uint64_t interval = video_output_get_frame_time(obs->video.video);
  842. obs->video.video_time = os_gettime_ns();
  843. obs->video.video_frame_interval_ns = interval;
  844. os_set_thread_name("libobs: graphics thread");
  845. const char *video_thread_name = profile_store_name(
  846. obs_get_profiler_name_store(),
  847. "obs_graphics_thread(%g" NBSP "ms)", interval / 1000000.);
  848. profile_register_root(video_thread_name, interval);
  849. srand((unsigned int)time(NULL));
  850. struct obs_graphics_context context;
  851. context.interval = video_output_get_frame_time(obs->video.video);
  852. context.frame_time_total_ns = 0;
  853. context.fps_total_ns = 0;
  854. context.fps_total_frames = 0;
  855. context.last_time = 0;
  856. #ifdef _WIN32
  857. context.gpu_was_active = false;
  858. #endif
  859. context.raw_was_active = false;
  860. context.was_active = false;
  861. context.video_thread_name = video_thread_name;
  862. #ifdef __APPLE__
  863. while (obs_graphics_thread_loop_autorelease(&context))
  864. #else
  865. while (obs_graphics_thread_loop(&context))
  866. #endif
  867. ;
  868. #ifdef _WIN32
  869. uninit_winrt_state(&winrt);
  870. #endif
  871. UNUSED_PARAMETER(param);
  872. return NULL;
  873. }