obs-video.c 25 KB

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