obs-video.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884
  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. static uint64_t tick_sources(uint64_t cur_time, uint64_t last_time)
  22. {
  23. struct obs_core_data *data = &obs->data;
  24. struct obs_source *source;
  25. uint64_t delta_time;
  26. float seconds;
  27. if (!last_time)
  28. last_time = cur_time -
  29. video_output_get_frame_time(obs->video.video);
  30. delta_time = cur_time - last_time;
  31. seconds = (float)((double)delta_time / 1000000000.0);
  32. /* ------------------------------------- */
  33. /* call tick callbacks */
  34. pthread_mutex_lock(&obs->data.draw_callbacks_mutex);
  35. for (size_t i = obs->data.tick_callbacks.num; i > 0; i--) {
  36. struct tick_callback *callback;
  37. callback = obs->data.tick_callbacks.array + (i - 1);
  38. callback->tick(callback->param, seconds);
  39. }
  40. pthread_mutex_unlock(&obs->data.draw_callbacks_mutex);
  41. /* ------------------------------------- */
  42. /* call the tick function of each source */
  43. pthread_mutex_lock(&data->sources_mutex);
  44. source = data->first_source;
  45. while (source) {
  46. struct obs_source *cur_source = obs_source_get_ref(source);
  47. source = (struct obs_source *)source->context.next;
  48. if (cur_source) {
  49. obs_source_video_tick(cur_source, seconds);
  50. obs_source_release(cur_source);
  51. }
  52. }
  53. pthread_mutex_unlock(&data->sources_mutex);
  54. return cur_time;
  55. }
  56. /* in obs-display.c */
  57. extern void render_display(struct obs_display *display);
  58. static inline void render_displays(void)
  59. {
  60. struct obs_display *display;
  61. if (!obs->data.valid)
  62. return;
  63. gs_enter_context(obs->video.graphics);
  64. /* render extra displays/swaps */
  65. pthread_mutex_lock(&obs->data.displays_mutex);
  66. display = obs->data.first_display;
  67. while (display) {
  68. render_display(display);
  69. display = display->next;
  70. }
  71. pthread_mutex_unlock(&obs->data.displays_mutex);
  72. gs_leave_context();
  73. }
  74. static inline void set_render_size(uint32_t width, uint32_t height)
  75. {
  76. gs_enable_depth_test(false);
  77. gs_set_cull_mode(GS_NEITHER);
  78. gs_ortho(0.0f, (float)width, 0.0f, (float)height, -100.0f, 100.0f);
  79. gs_set_viewport(0, 0, width, height);
  80. }
  81. static inline void unmap_last_surface(struct obs_core_video *video)
  82. {
  83. if (video->mapped_surface) {
  84. gs_stagesurface_unmap(video->mapped_surface);
  85. video->mapped_surface = NULL;
  86. }
  87. }
  88. static const char *render_main_texture_name = "render_main_texture";
  89. static inline void render_main_texture(struct obs_core_video *video)
  90. {
  91. profile_start(render_main_texture_name);
  92. GS_DEBUG_MARKER_BEGIN(GS_DEBUG_COLOR_MAIN_TEXTURE,
  93. render_main_texture_name);
  94. struct vec4 clear_color;
  95. vec4_set(&clear_color, 0.0f, 0.0f, 0.0f, 0.0f);
  96. gs_set_render_target(video->render_texture, NULL);
  97. gs_clear(GS_CLEAR_COLOR, &clear_color, 1.0f, 0);
  98. set_render_size(video->base_width, video->base_height);
  99. pthread_mutex_lock(&obs->data.draw_callbacks_mutex);
  100. for (size_t i = obs->data.draw_callbacks.num; i > 0; i--) {
  101. struct draw_callback *callback;
  102. callback = obs->data.draw_callbacks.array + (i - 1);
  103. callback->draw(callback->param, video->base_width,
  104. video->base_height);
  105. }
  106. pthread_mutex_unlock(&obs->data.draw_callbacks_mutex);
  107. obs_view_render(&obs->data.main_view);
  108. video->texture_rendered = true;
  109. GS_DEBUG_MARKER_END();
  110. profile_end(render_main_texture_name);
  111. }
  112. static inline gs_effect_t *
  113. get_scale_effect_internal(struct obs_core_video *video)
  114. {
  115. /* if the dimension is under half the size of the original image,
  116. * bicubic/lanczos can't sample enough pixels to create an accurate
  117. * image, so use the bilinear low resolution effect instead */
  118. if (video->output_width < (video->base_width / 2) &&
  119. video->output_height < (video->base_height / 2)) {
  120. return video->bilinear_lowres_effect;
  121. }
  122. switch (video->scale_type) {
  123. case OBS_SCALE_BILINEAR:
  124. return video->default_effect;
  125. case OBS_SCALE_LANCZOS:
  126. return video->lanczos_effect;
  127. case OBS_SCALE_BICUBIC:
  128. default:;
  129. }
  130. return video->bicubic_effect;
  131. }
  132. static inline bool resolution_close(struct obs_core_video *video,
  133. uint32_t width, uint32_t height)
  134. {
  135. long width_cmp = (long)video->base_width - (long)width;
  136. long height_cmp = (long)video->base_height - (long)height;
  137. return labs(width_cmp) <= 16 && labs(height_cmp) <= 16;
  138. }
  139. static inline gs_effect_t *get_scale_effect(struct obs_core_video *video,
  140. uint32_t width, uint32_t height)
  141. {
  142. if (resolution_close(video, width, height)) {
  143. return video->default_effect;
  144. } else {
  145. /* if the scale method couldn't be loaded, use either bicubic
  146. * or bilinear by default */
  147. gs_effect_t *effect = get_scale_effect_internal(video);
  148. if (!effect)
  149. effect = !!video->bicubic_effect
  150. ? video->bicubic_effect
  151. : video->default_effect;
  152. return effect;
  153. }
  154. }
  155. static const char *render_output_texture_name = "render_output_texture";
  156. static inline void render_output_texture(struct obs_core_video *video)
  157. {
  158. profile_start(render_output_texture_name);
  159. gs_texture_t *texture = video->render_texture;
  160. gs_texture_t *target = video->output_texture;
  161. uint32_t width = gs_texture_get_width(target);
  162. uint32_t height = gs_texture_get_height(target);
  163. struct vec2 base, base_i;
  164. vec2_set(&base, (float)video->base_width, (float)video->base_height);
  165. vec2_set(&base_i, 1.0f / (float)video->base_width,
  166. 1.0f / (float)video->base_height);
  167. gs_effect_t *effect = get_scale_effect(video, width, height);
  168. gs_technique_t *tech;
  169. if (video->ovi.output_format == VIDEO_FORMAT_RGBA) {
  170. tech = gs_effect_get_technique(effect, "DrawAlphaDivide");
  171. } else {
  172. tech = gs_effect_get_technique(effect, "DrawMatrix");
  173. }
  174. gs_eparam_t *image = gs_effect_get_param_by_name(effect, "image");
  175. gs_eparam_t *matrix =
  176. gs_effect_get_param_by_name(effect, "color_matrix");
  177. gs_eparam_t *bres =
  178. gs_effect_get_param_by_name(effect, "base_dimension");
  179. gs_eparam_t *bres_i =
  180. gs_effect_get_param_by_name(effect, "base_dimension_i");
  181. size_t passes, i;
  182. gs_set_render_target(target, NULL);
  183. set_render_size(width, height);
  184. if (bres)
  185. gs_effect_set_vec2(bres, &base);
  186. if (bres_i)
  187. gs_effect_set_vec2(bres_i, &base_i);
  188. gs_effect_set_val(matrix, video->color_matrix, sizeof(float) * 16);
  189. gs_effect_set_texture(image, texture);
  190. gs_enable_blending(false);
  191. passes = gs_technique_begin(tech);
  192. for (i = 0; i < passes; i++) {
  193. gs_technique_begin_pass(tech, i);
  194. gs_draw_sprite(texture, 0, width, height);
  195. gs_technique_end_pass(tech);
  196. }
  197. gs_technique_end(tech);
  198. gs_enable_blending(true);
  199. profile_end(render_output_texture_name);
  200. }
  201. static inline void set_eparam(gs_effect_t *effect, const char *name, float val)
  202. {
  203. gs_eparam_t *param = gs_effect_get_param_by_name(effect, name);
  204. gs_effect_set_float(param, val);
  205. }
  206. static const char *render_convert_texture_name = "render_convert_texture";
  207. static void render_convert_texture(struct obs_core_video *video)
  208. {
  209. profile_start(render_convert_texture_name);
  210. gs_texture_t *texture = video->output_texture;
  211. gs_texture_t *target = video->convert_texture;
  212. float fwidth = (float)video->output_width;
  213. float fheight = (float)video->output_height;
  214. size_t passes, i;
  215. gs_effect_t *effect = video->conversion_effect;
  216. gs_eparam_t *image = gs_effect_get_param_by_name(effect, "image");
  217. gs_technique_t *tech =
  218. gs_effect_get_technique(effect, video->conversion_tech);
  219. set_eparam(effect, "u_plane_offset", (float)video->plane_offsets[1]);
  220. set_eparam(effect, "v_plane_offset", (float)video->plane_offsets[2]);
  221. set_eparam(effect, "width", fwidth);
  222. set_eparam(effect, "height", fheight);
  223. set_eparam(effect, "width_i", 1.0f / fwidth);
  224. set_eparam(effect, "height_i", 1.0f / fheight);
  225. set_eparam(effect, "width_d2", fwidth * 0.5f);
  226. set_eparam(effect, "height_d2", fheight * 0.5f);
  227. set_eparam(effect, "width_d2_i", 1.0f / (fwidth * 0.5f));
  228. set_eparam(effect, "height_d2_i", 1.0f / (fheight * 0.5f));
  229. set_eparam(effect, "input_height", (float)video->conversion_height);
  230. gs_effect_set_texture(image, texture);
  231. gs_set_render_target(target, NULL);
  232. set_render_size(video->output_width, video->conversion_height);
  233. gs_enable_blending(false);
  234. passes = gs_technique_begin(tech);
  235. for (i = 0; i < passes; i++) {
  236. gs_technique_begin_pass(tech, i);
  237. gs_draw(GS_TRIS, 0, 3);
  238. gs_technique_end_pass(tech);
  239. }
  240. gs_technique_end(tech);
  241. gs_enable_blending(true);
  242. video->texture_converted = true;
  243. profile_end(render_convert_texture_name);
  244. }
  245. static void render_nv12(struct obs_core_video *video, gs_texture_t *target,
  246. const char *tech_name, uint32_t width, uint32_t height)
  247. {
  248. gs_texture_t *texture = video->output_texture;
  249. gs_effect_t *effect = video->conversion_effect;
  250. gs_eparam_t *image = gs_effect_get_param_by_name(effect, "image");
  251. gs_technique_t *tech = gs_effect_get_technique(effect, tech_name);
  252. size_t passes, i;
  253. gs_effect_set_texture(image, texture);
  254. gs_set_render_target(target, NULL);
  255. set_render_size(width, height);
  256. gs_enable_blending(false);
  257. passes = gs_technique_begin(tech);
  258. for (i = 0; i < passes; i++) {
  259. gs_technique_begin_pass(tech, i);
  260. gs_draw(GS_TRIS, 0, 3);
  261. gs_technique_end_pass(tech);
  262. }
  263. gs_technique_end(tech);
  264. gs_enable_blending(true);
  265. }
  266. static const char *render_convert_nv12_name = "render_convert_texture_nv12";
  267. static void render_convert_texture_nv12(struct obs_core_video *video)
  268. {
  269. profile_start(render_convert_nv12_name);
  270. render_nv12(video, video->convert_texture, "NV12_Y",
  271. video->output_width, video->output_height);
  272. render_nv12(video, video->convert_uv_texture, "NV12_UV",
  273. video->output_width / 2, video->output_height / 2);
  274. video->texture_converted = true;
  275. profile_end(render_convert_nv12_name);
  276. }
  277. static const char *stage_output_texture_name = "stage_output_texture";
  278. static inline void stage_output_texture(struct obs_core_video *video,
  279. int cur_texture)
  280. {
  281. profile_start(stage_output_texture_name);
  282. gs_texture_t *texture;
  283. bool texture_ready;
  284. gs_stagesurf_t *copy = video->copy_surfaces[cur_texture];
  285. if (video->gpu_conversion) {
  286. texture = video->convert_texture;
  287. texture_ready = video->texture_converted;
  288. } else {
  289. texture = video->output_texture;
  290. texture_ready = true;
  291. }
  292. unmap_last_surface(video);
  293. if (!texture_ready)
  294. goto end;
  295. gs_stage_texture(copy, texture);
  296. video->textures_copied[cur_texture] = true;
  297. end:
  298. profile_end(stage_output_texture_name);
  299. }
  300. #ifdef _WIN32
  301. static inline bool queue_frame(struct obs_core_video *video, bool raw_active,
  302. struct obs_vframe_info *vframe_info)
  303. {
  304. bool duplicate =
  305. !video->gpu_encoder_avail_queue.size ||
  306. (video->gpu_encoder_queue.size && vframe_info->count > 1);
  307. if (duplicate) {
  308. struct obs_tex_frame *tf = circlebuf_data(
  309. &video->gpu_encoder_queue,
  310. video->gpu_encoder_queue.size - sizeof(*tf));
  311. /* texture-based encoding is stopping */
  312. if (!tf) {
  313. return false;
  314. }
  315. tf->count++;
  316. os_sem_post(video->gpu_encode_semaphore);
  317. goto finish;
  318. }
  319. struct obs_tex_frame tf;
  320. circlebuf_pop_front(&video->gpu_encoder_avail_queue, &tf, sizeof(tf));
  321. if (tf.released) {
  322. gs_texture_acquire_sync(tf.tex, tf.lock_key, GS_WAIT_INFINITE);
  323. tf.released = false;
  324. }
  325. /* the vframe_info->count > 1 case causing a copy can only happen if by
  326. * some chance the very first frame has to be duplicated for whatever
  327. * reason. otherwise, it goes to the 'duplicate' case above, which
  328. * will ensure better performance. */
  329. if (raw_active || vframe_info->count > 1) {
  330. gs_copy_texture(tf.tex, video->convert_texture);
  331. } else {
  332. gs_texture_t *tex = video->convert_texture;
  333. gs_texture_t *tex_uv = video->convert_uv_texture;
  334. video->convert_texture = tf.tex;
  335. video->convert_uv_texture = tf.tex_uv;
  336. tf.tex = tex;
  337. tf.tex_uv = tex_uv;
  338. }
  339. tf.count = 1;
  340. tf.timestamp = vframe_info->timestamp;
  341. tf.released = true;
  342. tf.handle = gs_texture_get_shared_handle(tf.tex);
  343. gs_texture_release_sync(tf.tex, ++tf.lock_key);
  344. circlebuf_push_back(&video->gpu_encoder_queue, &tf, sizeof(tf));
  345. os_sem_post(video->gpu_encode_semaphore);
  346. finish:
  347. return --vframe_info->count;
  348. }
  349. extern void full_stop(struct obs_encoder *encoder);
  350. static inline void encode_gpu(struct obs_core_video *video, bool raw_active,
  351. struct obs_vframe_info *vframe_info)
  352. {
  353. while (queue_frame(video, raw_active, vframe_info))
  354. ;
  355. }
  356. static const char *output_gpu_encoders_name = "output_gpu_encoders";
  357. static void output_gpu_encoders(struct obs_core_video *video, bool raw_active)
  358. {
  359. profile_start(output_gpu_encoders_name);
  360. if (!video->texture_converted)
  361. goto end;
  362. if (!video->vframe_info_buffer_gpu.size)
  363. goto end;
  364. struct obs_vframe_info vframe_info;
  365. circlebuf_pop_front(&video->vframe_info_buffer_gpu, &vframe_info,
  366. sizeof(vframe_info));
  367. pthread_mutex_lock(&video->gpu_encoder_mutex);
  368. encode_gpu(video, raw_active, &vframe_info);
  369. pthread_mutex_unlock(&video->gpu_encoder_mutex);
  370. end:
  371. profile_end(output_gpu_encoders_name);
  372. }
  373. #endif
  374. static inline void render_video(struct obs_core_video *video, bool raw_active,
  375. const bool gpu_active, int cur_texture)
  376. {
  377. gs_begin_scene();
  378. gs_enable_depth_test(false);
  379. gs_set_cull_mode(GS_NEITHER);
  380. render_main_texture(video);
  381. if (raw_active || gpu_active) {
  382. render_output_texture(video);
  383. #ifdef _WIN32
  384. if (gpu_active) {
  385. gs_flush();
  386. }
  387. #endif
  388. if (video->gpu_conversion) {
  389. if (video->using_nv12_tex)
  390. render_convert_texture_nv12(video);
  391. else
  392. render_convert_texture(video);
  393. }
  394. #ifdef _WIN32
  395. if (gpu_active) {
  396. gs_flush();
  397. output_gpu_encoders(video, raw_active);
  398. }
  399. #endif
  400. if (raw_active)
  401. stage_output_texture(video, cur_texture);
  402. }
  403. gs_set_render_target(NULL, NULL);
  404. gs_enable_blending(true);
  405. gs_end_scene();
  406. }
  407. static inline bool download_frame(struct obs_core_video *video,
  408. int prev_texture, struct video_data *frame)
  409. {
  410. gs_stagesurf_t *surface = video->copy_surfaces[prev_texture];
  411. if (!video->textures_copied[prev_texture])
  412. return false;
  413. if (!gs_stagesurface_map(surface, &frame->data[0], &frame->linesize[0]))
  414. return false;
  415. video->mapped_surface = surface;
  416. return true;
  417. }
  418. static inline uint32_t calc_linesize(uint32_t pos, uint32_t linesize)
  419. {
  420. uint32_t size = pos % linesize;
  421. return size ? size : linesize;
  422. }
  423. static void copy_dealign(uint8_t *dst, uint32_t dst_pos, uint32_t dst_linesize,
  424. const uint8_t *src, uint32_t src_pos,
  425. uint32_t src_linesize, uint32_t remaining)
  426. {
  427. while (remaining) {
  428. uint32_t src_remainder = src_pos % src_linesize;
  429. uint32_t dst_offset = dst_linesize - src_remainder;
  430. uint32_t src_offset = src_linesize - src_remainder;
  431. if (remaining < dst_offset) {
  432. memcpy(dst + dst_pos, src + src_pos, remaining);
  433. src_pos += remaining;
  434. dst_pos += remaining;
  435. remaining = 0;
  436. } else {
  437. memcpy(dst + dst_pos, src + src_pos, dst_offset);
  438. src_pos += src_offset;
  439. dst_pos += dst_offset;
  440. remaining -= dst_offset;
  441. }
  442. }
  443. }
  444. static inline uint32_t make_aligned_linesize_offset(uint32_t offset,
  445. uint32_t dst_linesize,
  446. uint32_t src_linesize)
  447. {
  448. uint32_t remainder = offset % dst_linesize;
  449. return (offset / dst_linesize) * src_linesize + remainder;
  450. }
  451. static void fix_gpu_converted_alignment(struct obs_core_video *video,
  452. struct video_frame *output,
  453. const struct video_data *input)
  454. {
  455. uint32_t src_linesize = input->linesize[0];
  456. uint32_t dst_linesize = output->linesize[0] * 4;
  457. uint32_t src_pos = 0;
  458. for (size_t i = 0; i < 3; i++) {
  459. if (video->plane_linewidth[i] == 0)
  460. break;
  461. src_pos = make_aligned_linesize_offset(
  462. video->plane_offsets[i], dst_linesize, src_linesize);
  463. copy_dealign(output->data[i], 0, dst_linesize, input->data[0],
  464. src_pos, src_linesize, video->plane_sizes[i]);
  465. }
  466. }
  467. static void set_gpu_converted_data(struct obs_core_video *video,
  468. struct video_frame *output,
  469. const struct video_data *input,
  470. const struct video_output_info *info)
  471. {
  472. if (input->linesize[0] == video->output_width * 4) {
  473. struct video_frame frame;
  474. for (size_t i = 0; i < 3; i++) {
  475. if (video->plane_linewidth[i] == 0)
  476. break;
  477. frame.linesize[i] = video->plane_linewidth[i];
  478. frame.data[i] =
  479. input->data[0] + video->plane_offsets[i];
  480. }
  481. video_frame_copy(output, &frame, info->format, info->height);
  482. } else if (video->using_nv12_tex) {
  483. size_t width = info->width;
  484. size_t height = info->height;
  485. size_t height_d2 = height / 2;
  486. uint8_t *out_y = output->data[0];
  487. uint8_t *out_uv = output->data[1];
  488. uint8_t *in = input->data[0];
  489. for (size_t y = 0; y < height; y++) {
  490. memcpy(out_y, in, width);
  491. out_y += output->linesize[0];
  492. in += input->linesize[0];
  493. }
  494. for (size_t y = 0; y < height_d2; y++) {
  495. memcpy(out_uv, in, width);
  496. out_uv += output->linesize[0];
  497. in += input->linesize[0];
  498. }
  499. } else {
  500. fix_gpu_converted_alignment(video, output, input);
  501. }
  502. }
  503. static inline void copy_rgbx_frame(struct video_frame *output,
  504. const struct video_data *input,
  505. const struct video_output_info *info)
  506. {
  507. uint8_t *in_ptr = input->data[0];
  508. uint8_t *out_ptr = output->data[0];
  509. /* if the line sizes match, do a single copy */
  510. if (input->linesize[0] == output->linesize[0]) {
  511. memcpy(out_ptr, in_ptr, input->linesize[0] * info->height);
  512. } else {
  513. for (size_t y = 0; y < info->height; y++) {
  514. memcpy(out_ptr, in_ptr, info->width * 4);
  515. in_ptr += input->linesize[0];
  516. out_ptr += output->linesize[0];
  517. }
  518. }
  519. }
  520. static inline void output_video_data(struct obs_core_video *video,
  521. struct video_data *input_frame, int count)
  522. {
  523. const struct video_output_info *info;
  524. struct video_frame output_frame;
  525. bool locked;
  526. info = video_output_get_info(video->video);
  527. locked = video_output_lock_frame(video->video, &output_frame, count,
  528. input_frame->timestamp);
  529. if (locked) {
  530. if (video->gpu_conversion) {
  531. set_gpu_converted_data(video, &output_frame,
  532. input_frame, info);
  533. } else {
  534. copy_rgbx_frame(&output_frame, input_frame, info);
  535. }
  536. video_output_unlock_frame(video->video);
  537. }
  538. }
  539. static inline void video_sleep(struct obs_core_video *video, bool raw_active,
  540. const bool gpu_active, uint64_t *p_time,
  541. uint64_t interval_ns)
  542. {
  543. struct obs_vframe_info vframe_info;
  544. uint64_t cur_time = *p_time;
  545. uint64_t t = cur_time + interval_ns;
  546. int count;
  547. if (os_sleepto_ns(t)) {
  548. *p_time = t;
  549. count = 1;
  550. } else {
  551. count = (int)((os_gettime_ns() - cur_time) / interval_ns);
  552. *p_time = cur_time + interval_ns * count;
  553. }
  554. video->total_frames += count;
  555. video->lagged_frames += count - 1;
  556. vframe_info.timestamp = cur_time;
  557. vframe_info.count = count;
  558. if (raw_active)
  559. circlebuf_push_back(&video->vframe_info_buffer, &vframe_info,
  560. sizeof(vframe_info));
  561. if (gpu_active)
  562. circlebuf_push_back(&video->vframe_info_buffer_gpu,
  563. &vframe_info, sizeof(vframe_info));
  564. }
  565. static const char *output_frame_gs_context_name = "gs_context(video->graphics)";
  566. static const char *output_frame_render_video_name = "render_video";
  567. static const char *output_frame_download_frame_name = "download_frame";
  568. static const char *output_frame_gs_flush_name = "gs_flush";
  569. static const char *output_frame_output_video_data_name = "output_video_data";
  570. static inline void output_frame(bool raw_active, const bool gpu_active)
  571. {
  572. struct obs_core_video *video = &obs->video;
  573. int cur_texture = video->cur_texture;
  574. int prev_texture = cur_texture == 0 ? NUM_TEXTURES - 1
  575. : cur_texture - 1;
  576. struct video_data frame;
  577. bool frame_ready = 0;
  578. memset(&frame, 0, sizeof(struct video_data));
  579. profile_start(output_frame_gs_context_name);
  580. gs_enter_context(video->graphics);
  581. profile_start(output_frame_render_video_name);
  582. GS_DEBUG_MARKER_BEGIN(GS_DEBUG_COLOR_RENDER_VIDEO,
  583. output_frame_render_video_name);
  584. render_video(video, raw_active, gpu_active, cur_texture);
  585. GS_DEBUG_MARKER_END();
  586. profile_end(output_frame_render_video_name);
  587. if (raw_active) {
  588. profile_start(output_frame_download_frame_name);
  589. frame_ready = download_frame(video, prev_texture, &frame);
  590. profile_end(output_frame_download_frame_name);
  591. }
  592. profile_start(output_frame_gs_flush_name);
  593. gs_flush();
  594. profile_end(output_frame_gs_flush_name);
  595. gs_leave_context();
  596. profile_end(output_frame_gs_context_name);
  597. if (raw_active && frame_ready) {
  598. struct obs_vframe_info vframe_info;
  599. circlebuf_pop_front(&video->vframe_info_buffer, &vframe_info,
  600. sizeof(vframe_info));
  601. frame.timestamp = vframe_info.timestamp;
  602. profile_start(output_frame_output_video_data_name);
  603. output_video_data(video, &frame, vframe_info.count);
  604. profile_end(output_frame_output_video_data_name);
  605. }
  606. if (++video->cur_texture == NUM_TEXTURES)
  607. video->cur_texture = 0;
  608. }
  609. #define NBSP "\xC2\xA0"
  610. static void clear_base_frame_data(void)
  611. {
  612. struct obs_core_video *video = &obs->video;
  613. video->texture_rendered = false;
  614. video->texture_converted = false;
  615. circlebuf_free(&video->vframe_info_buffer);
  616. video->cur_texture = 0;
  617. }
  618. static void clear_raw_frame_data(void)
  619. {
  620. struct obs_core_video *video = &obs->video;
  621. memset(video->textures_copied, 0, sizeof(video->textures_copied));
  622. circlebuf_free(&video->vframe_info_buffer);
  623. }
  624. #ifdef _WIN32
  625. static void clear_gpu_frame_data(void)
  626. {
  627. struct obs_core_video *video = &obs->video;
  628. circlebuf_free(&video->vframe_info_buffer_gpu);
  629. }
  630. #endif
  631. static const char *tick_sources_name = "tick_sources";
  632. static const char *render_displays_name = "render_displays";
  633. static const char *output_frame_name = "output_frame";
  634. void *obs_graphics_thread(void *param)
  635. {
  636. uint64_t last_time = 0;
  637. uint64_t interval = video_output_get_frame_time(obs->video.video);
  638. uint64_t frame_time_total_ns = 0;
  639. uint64_t fps_total_ns = 0;
  640. uint32_t fps_total_frames = 0;
  641. #ifdef _WIN32
  642. bool gpu_was_active = false;
  643. #endif
  644. bool raw_was_active = false;
  645. bool was_active = false;
  646. obs->video.video_time = os_gettime_ns();
  647. obs->video.video_frame_interval_ns = interval;
  648. os_set_thread_name("libobs: graphics thread");
  649. const char *video_thread_name = profile_store_name(
  650. obs_get_profiler_name_store(),
  651. "obs_graphics_thread(%g" NBSP "ms)", interval / 1000000.);
  652. profile_register_root(video_thread_name, interval);
  653. srand((unsigned int)time(NULL));
  654. while (!video_output_stopped(obs->video.video)) {
  655. uint64_t frame_start = os_gettime_ns();
  656. uint64_t frame_time_ns;
  657. bool raw_active = obs->video.raw_active > 0;
  658. #ifdef _WIN32
  659. const bool gpu_active = obs->video.gpu_encoder_active > 0;
  660. const bool active = raw_active || gpu_active;
  661. #else
  662. const bool gpu_active = 0;
  663. const bool active = raw_active;
  664. #endif
  665. if (!was_active && active)
  666. clear_base_frame_data();
  667. if (!raw_was_active && raw_active)
  668. clear_raw_frame_data();
  669. #ifdef _WIN32
  670. if (!gpu_was_active && gpu_active)
  671. clear_gpu_frame_data();
  672. gpu_was_active = gpu_active;
  673. #endif
  674. raw_was_active = raw_active;
  675. was_active = active;
  676. profile_start(video_thread_name);
  677. profile_start(tick_sources_name);
  678. last_time = tick_sources(obs->video.video_time, last_time);
  679. profile_end(tick_sources_name);
  680. profile_start(output_frame_name);
  681. output_frame(raw_active, gpu_active);
  682. profile_end(output_frame_name);
  683. profile_start(render_displays_name);
  684. render_displays();
  685. profile_end(render_displays_name);
  686. frame_time_ns = os_gettime_ns() - frame_start;
  687. profile_end(video_thread_name);
  688. profile_reenable_thread();
  689. video_sleep(&obs->video, raw_active, gpu_active,
  690. &obs->video.video_time, interval);
  691. frame_time_total_ns += frame_time_ns;
  692. fps_total_ns += (obs->video.video_time - last_time);
  693. fps_total_frames++;
  694. if (fps_total_ns >= 1000000000ULL) {
  695. obs->video.video_fps =
  696. (double)fps_total_frames /
  697. ((double)fps_total_ns / 1000000000.0);
  698. obs->video.video_avg_frame_time_ns =
  699. frame_time_total_ns /
  700. (uint64_t)fps_total_frames;
  701. frame_time_total_ns = 0;
  702. fps_total_ns = 0;
  703. fps_total_frames = 0;
  704. }
  705. }
  706. UNUSED_PARAMETER(param);
  707. return NULL;
  708. }