obs-video.c 26 KB

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