obs-video.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645
  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 "obs.h"
  15. #include "obs-internal.h"
  16. #include "graphics/vec4.h"
  17. #include "media-io/format-conversion.h"
  18. #include "media-io/video-frame.h"
  19. static inline void calculate_base_volume(struct obs_core_data *data,
  20. struct obs_view *view, obs_source_t *target)
  21. {
  22. if (!target->activate_refs) {
  23. target->base_volume = 0.0f;
  24. /* only walk the tree if there are transitions active */
  25. } else if (data->active_transitions) {
  26. float best_vol = 0.0f;
  27. for (size_t i = 0; i < MAX_CHANNELS; i++) {
  28. struct obs_source *source = view->channels[i];
  29. float vol = 0.0f;
  30. if (!source)
  31. continue;
  32. vol = obs_source_get_target_volume(source, target);
  33. if (best_vol < vol)
  34. best_vol = vol;
  35. }
  36. target->base_volume = best_vol;
  37. } else {
  38. target->base_volume = 1.0f;
  39. }
  40. }
  41. static uint64_t tick_sources(uint64_t cur_time, uint64_t last_time)
  42. {
  43. struct obs_core_data *data = &obs->data;
  44. struct obs_view *view = &data->main_view;
  45. struct obs_source *source;
  46. uint64_t delta_time;
  47. float seconds;
  48. if (!last_time)
  49. last_time = cur_time -
  50. video_output_get_frame_time(obs->video.video);
  51. delta_time = cur_time - last_time;
  52. seconds = (float)((double)delta_time / 1000000000.0);
  53. pthread_mutex_lock(&data->sources_mutex);
  54. /* call the tick function of each source */
  55. source = data->first_source;
  56. while (source) {
  57. obs_source_video_tick(source, seconds);
  58. source = (struct obs_source*)source->context.next;
  59. }
  60. /* calculate source volumes */
  61. pthread_mutex_lock(&view->channels_mutex);
  62. source = data->first_source;
  63. while (source) {
  64. calculate_base_volume(data, view, source);
  65. source = (struct obs_source*)source->context.next;
  66. }
  67. pthread_mutex_unlock(&view->channels_mutex);
  68. pthread_mutex_unlock(&data->sources_mutex);
  69. return cur_time;
  70. }
  71. /* in obs-display.c */
  72. extern void render_display(struct obs_display *display);
  73. static inline void render_displays(void)
  74. {
  75. struct obs_display *display;
  76. if (!obs->data.valid)
  77. return;
  78. gs_enter_context(obs->video.graphics);
  79. /* render extra displays/swaps */
  80. pthread_mutex_lock(&obs->data.displays_mutex);
  81. display = obs->data.first_display;
  82. while (display) {
  83. render_display(display);
  84. display = display->next;
  85. }
  86. pthread_mutex_unlock(&obs->data.displays_mutex);
  87. gs_leave_context();
  88. }
  89. static inline void set_render_size(uint32_t width, uint32_t height)
  90. {
  91. gs_enable_depth_test(false);
  92. gs_set_cull_mode(GS_NEITHER);
  93. gs_ortho(0.0f, (float)width, 0.0f, (float)height, -100.0f, 100.0f);
  94. gs_set_viewport(0, 0, width, height);
  95. }
  96. static inline void unmap_last_surface(struct obs_core_video *video)
  97. {
  98. if (video->mapped_surface) {
  99. gs_stagesurface_unmap(video->mapped_surface);
  100. video->mapped_surface = NULL;
  101. }
  102. }
  103. static const char *render_main_texture_name = "render_main_texture";
  104. static inline void render_main_texture(struct obs_core_video *video,
  105. int cur_texture)
  106. {
  107. profile_start(render_main_texture_name);
  108. struct vec4 clear_color;
  109. vec4_set(&clear_color, 0.0f, 0.0f, 0.0f, 1.0f);
  110. gs_set_render_target(video->render_textures[cur_texture], NULL);
  111. gs_clear(GS_CLEAR_COLOR, &clear_color, 1.0f, 0);
  112. set_render_size(video->base_width, video->base_height);
  113. obs_view_render(&obs->data.main_view);
  114. video->textures_rendered[cur_texture] = true;
  115. profile_end(render_main_texture_name);
  116. }
  117. static inline gs_effect_t *get_scale_effect_internal(
  118. struct obs_core_video *video)
  119. {
  120. /* if the dimension is under half the size of the original image,
  121. * bicubic/lanczos can't sample enough pixels to create an accurate
  122. * image, so use the bilinear low resolution effect instead */
  123. if (video->output_width < (video->base_width / 2) &&
  124. video->output_height < (video->base_height / 2)) {
  125. return video->bilinear_lowres_effect;
  126. }
  127. switch (video->scale_type) {
  128. case OBS_SCALE_BILINEAR: return video->default_effect;
  129. case OBS_SCALE_LANCZOS: return video->lanczos_effect;
  130. case OBS_SCALE_BICUBIC:;
  131. }
  132. return video->bicubic_effect;
  133. }
  134. static inline bool resolution_close(struct obs_core_video *video,
  135. uint32_t width, uint32_t height)
  136. {
  137. long width_cmp = (long)video->base_width - (long)width;
  138. long height_cmp = (long)video->base_height - (long)height;
  139. return labs(width_cmp) <= 16 && labs(height_cmp) <= 16;
  140. }
  141. static inline gs_effect_t *get_scale_effect(struct obs_core_video *video,
  142. uint32_t width, uint32_t height)
  143. {
  144. if (resolution_close(video, width, height)) {
  145. return video->default_effect;
  146. } else {
  147. /* if the scale method couldn't be loaded, use either bicubic
  148. * or bilinear by default */
  149. gs_effect_t *effect = get_scale_effect_internal(video);
  150. if (!effect)
  151. effect = !!video->bicubic_effect ?
  152. video->bicubic_effect :
  153. video->default_effect;
  154. return effect;
  155. }
  156. }
  157. static const char *render_output_texture_name = "render_output_texture";
  158. static inline void render_output_texture(struct obs_core_video *video,
  159. int cur_texture, int prev_texture)
  160. {
  161. profile_start(render_output_texture_name);
  162. gs_texture_t *texture = video->render_textures[prev_texture];
  163. gs_texture_t *target = video->output_textures[cur_texture];
  164. uint32_t width = gs_texture_get_width(target);
  165. uint32_t height = gs_texture_get_height(target);
  166. struct vec2 base_i;
  167. vec2_set(&base_i,
  168. 1.0f / (float)video->base_width,
  169. 1.0f / (float)video->base_height);
  170. gs_effect_t *effect = get_scale_effect(video, width, height);
  171. gs_technique_t *tech = gs_effect_get_technique(effect, "DrawMatrix");
  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. if (!video->textures_rendered[prev_texture])
  179. goto end;
  180. gs_set_render_target(target, NULL);
  181. set_render_size(width, height);
  182. if (bres_i)
  183. gs_effect_set_vec2(bres_i, &base_i);
  184. gs_effect_set_val(matrix, video->color_matrix, sizeof(float) * 16);
  185. gs_effect_set_texture(image, texture);
  186. gs_enable_blending(false);
  187. passes = gs_technique_begin(tech);
  188. for (i = 0; i < passes; i++) {
  189. gs_technique_begin_pass(tech, i);
  190. gs_draw_sprite(texture, 0, width, height);
  191. gs_technique_end_pass(tech);
  192. }
  193. gs_technique_end(tech);
  194. gs_enable_blending(true);
  195. video->textures_output[cur_texture] = true;
  196. end:
  197. profile_end(render_output_texture_name);
  198. }
  199. static inline void set_eparam(gs_effect_t *effect, const char *name, float val)
  200. {
  201. gs_eparam_t *param = gs_effect_get_param_by_name(effect, name);
  202. gs_effect_set_float(param, val);
  203. }
  204. static const char *render_convert_texture_name = "render_convert_texture";
  205. static void render_convert_texture(struct obs_core_video *video,
  206. int cur_texture, int prev_texture)
  207. {
  208. profile_start(render_convert_texture_name);
  209. gs_texture_t *texture = video->output_textures[prev_texture];
  210. gs_texture_t *target = video->convert_textures[cur_texture];
  211. float fwidth = (float)video->output_width;
  212. float fheight = (float)video->output_height;
  213. size_t passes, i;
  214. gs_effect_t *effect = video->conversion_effect;
  215. gs_eparam_t *image = gs_effect_get_param_by_name(effect, "image");
  216. gs_technique_t *tech = gs_effect_get_technique(effect,
  217. video->conversion_tech);
  218. if (!video->textures_output[prev_texture])
  219. goto end;
  220. set_eparam(effect, "u_plane_offset", (float)video->plane_offsets[1]);
  221. set_eparam(effect, "v_plane_offset", (float)video->plane_offsets[2]);
  222. set_eparam(effect, "width", fwidth);
  223. set_eparam(effect, "height", fheight);
  224. set_eparam(effect, "width_i", 1.0f / fwidth);
  225. set_eparam(effect, "height_i", 1.0f / fheight);
  226. set_eparam(effect, "width_d2", fwidth * 0.5f);
  227. set_eparam(effect, "height_d2", fheight * 0.5f);
  228. set_eparam(effect, "width_d2_i", 1.0f / (fwidth * 0.5f));
  229. set_eparam(effect, "height_d2_i", 1.0f / (fheight * 0.5f));
  230. set_eparam(effect, "input_height", (float)video->conversion_height);
  231. gs_effect_set_texture(image, texture);
  232. gs_set_render_target(target, NULL);
  233. set_render_size(video->output_width, video->conversion_height);
  234. gs_enable_blending(false);
  235. passes = gs_technique_begin(tech);
  236. for (i = 0; i < passes; i++) {
  237. gs_technique_begin_pass(tech, i);
  238. gs_draw_sprite(texture, 0, video->output_width,
  239. video->conversion_height);
  240. gs_technique_end_pass(tech);
  241. }
  242. gs_technique_end(tech);
  243. gs_enable_blending(true);
  244. video->textures_converted[cur_texture] = true;
  245. end:
  246. profile_end(render_convert_texture_name);
  247. }
  248. static const char *stage_output_texture_name = "stage_output_texture";
  249. static inline void stage_output_texture(struct obs_core_video *video,
  250. int cur_texture, int prev_texture)
  251. {
  252. profile_start(stage_output_texture_name);
  253. gs_texture_t *texture;
  254. bool texture_ready;
  255. gs_stagesurf_t *copy = video->copy_surfaces[cur_texture];
  256. if (video->gpu_conversion) {
  257. texture = video->convert_textures[prev_texture];
  258. texture_ready = video->textures_converted[prev_texture];
  259. } else {
  260. texture = video->output_textures[prev_texture];
  261. texture_ready = video->output_textures[prev_texture];
  262. }
  263. unmap_last_surface(video);
  264. if (!texture_ready)
  265. goto end;
  266. gs_stage_texture(copy, texture);
  267. video->textures_copied[cur_texture] = true;
  268. end:
  269. profile_end(stage_output_texture_name);
  270. }
  271. static inline void render_video(struct obs_core_video *video, int cur_texture,
  272. int prev_texture)
  273. {
  274. gs_begin_scene();
  275. gs_enable_depth_test(false);
  276. gs_set_cull_mode(GS_NEITHER);
  277. render_main_texture(video, cur_texture);
  278. render_output_texture(video, cur_texture, prev_texture);
  279. if (video->gpu_conversion)
  280. render_convert_texture(video, cur_texture, prev_texture);
  281. stage_output_texture(video, cur_texture, prev_texture);
  282. gs_set_render_target(NULL, NULL);
  283. gs_enable_blending(true);
  284. gs_end_scene();
  285. }
  286. static inline bool download_frame(struct obs_core_video *video,
  287. int prev_texture, struct video_data *frame)
  288. {
  289. gs_stagesurf_t *surface = video->copy_surfaces[prev_texture];
  290. if (!video->textures_copied[prev_texture])
  291. return false;
  292. if (!gs_stagesurface_map(surface, &frame->data[0], &frame->linesize[0]))
  293. return false;
  294. video->mapped_surface = surface;
  295. return true;
  296. }
  297. static inline uint32_t calc_linesize(uint32_t pos, uint32_t linesize)
  298. {
  299. uint32_t size = pos % linesize;
  300. return size ? size : linesize;
  301. }
  302. static void copy_dealign(
  303. uint8_t *dst, uint32_t dst_pos, uint32_t dst_linesize,
  304. const uint8_t *src, uint32_t src_pos, uint32_t src_linesize,
  305. uint32_t remaining)
  306. {
  307. while (remaining) {
  308. uint32_t src_remainder = src_pos % src_linesize;
  309. uint32_t dst_offset = dst_linesize - src_remainder;
  310. uint32_t src_offset = src_linesize - src_remainder;
  311. if (remaining < dst_offset) {
  312. memcpy(dst + dst_pos, src + src_pos, remaining);
  313. src_pos += remaining;
  314. dst_pos += remaining;
  315. remaining = 0;
  316. } else {
  317. memcpy(dst + dst_pos, src + src_pos, dst_offset);
  318. src_pos += src_offset;
  319. dst_pos += dst_offset;
  320. remaining -= dst_offset;
  321. }
  322. }
  323. }
  324. static inline uint32_t make_aligned_linesize_offset(uint32_t offset,
  325. uint32_t dst_linesize, uint32_t src_linesize)
  326. {
  327. uint32_t remainder = offset % dst_linesize;
  328. return (offset / dst_linesize) * src_linesize + remainder;
  329. }
  330. static void fix_gpu_converted_alignment(struct obs_core_video *video,
  331. struct video_frame *output, const struct video_data *input)
  332. {
  333. uint32_t src_linesize = input->linesize[0];
  334. uint32_t dst_linesize = output->linesize[0] * 4;
  335. uint32_t src_pos = 0;
  336. for (size_t i = 0; i < 3; i++) {
  337. if (video->plane_linewidth[i] == 0)
  338. break;
  339. src_pos = make_aligned_linesize_offset(video->plane_offsets[i],
  340. dst_linesize, src_linesize);
  341. copy_dealign(output->data[i], 0, dst_linesize,
  342. input->data[0], src_pos, src_linesize,
  343. video->plane_sizes[i]);
  344. }
  345. }
  346. static void set_gpu_converted_data(struct obs_core_video *video,
  347. struct video_frame *output, const struct video_data *input,
  348. const struct video_output_info *info)
  349. {
  350. if (input->linesize[0] == video->output_width*4) {
  351. struct video_frame frame;
  352. for (size_t i = 0; i < 3; i++) {
  353. if (video->plane_linewidth[i] == 0)
  354. break;
  355. frame.linesize[i] = video->plane_linewidth[i];
  356. frame.data[i] =
  357. input->data[0] + video->plane_offsets[i];
  358. }
  359. video_frame_copy(output, &frame, info->format, info->height);
  360. } else {
  361. fix_gpu_converted_alignment(video, output, input);
  362. }
  363. }
  364. static void convert_frame(
  365. struct video_frame *output, const struct video_data *input,
  366. const struct video_output_info *info)
  367. {
  368. if (info->format == VIDEO_FORMAT_I420) {
  369. compress_uyvx_to_i420(
  370. input->data[0], input->linesize[0],
  371. 0, info->height,
  372. output->data, output->linesize);
  373. } else if (info->format == VIDEO_FORMAT_NV12) {
  374. compress_uyvx_to_nv12(
  375. input->data[0], input->linesize[0],
  376. 0, info->height,
  377. output->data, output->linesize);
  378. } else if (info->format == VIDEO_FORMAT_I444) {
  379. convert_uyvx_to_i444(
  380. input->data[0], input->linesize[0],
  381. 0, info->height,
  382. output->data, output->linesize);
  383. } else {
  384. blog(LOG_ERROR, "convert_frame: unsupported texture format");
  385. }
  386. }
  387. static inline void copy_rgbx_frame(
  388. struct video_frame *output, const struct video_data *input,
  389. const struct video_output_info *info)
  390. {
  391. uint8_t *in_ptr = input->data[0];
  392. uint8_t *out_ptr = output->data[0];
  393. /* if the line sizes match, do a single copy */
  394. if (input->linesize[0] == output->linesize[0]) {
  395. memcpy(out_ptr, in_ptr, input->linesize[0] * info->height);
  396. } else {
  397. for (size_t y = 0; y < info->height; y++) {
  398. memcpy(out_ptr, in_ptr, info->width * 4);
  399. in_ptr += input->linesize[0];
  400. out_ptr += output->linesize[0];
  401. }
  402. }
  403. }
  404. static inline void output_video_data(struct obs_core_video *video,
  405. struct video_data *input_frame, int count)
  406. {
  407. const struct video_output_info *info;
  408. struct video_frame output_frame;
  409. bool locked;
  410. info = video_output_get_info(video->video);
  411. locked = video_output_lock_frame(video->video, &output_frame, count,
  412. input_frame->timestamp);
  413. if (locked) {
  414. if (video->gpu_conversion) {
  415. set_gpu_converted_data(video, &output_frame,
  416. input_frame, info);
  417. } else if (format_is_yuv(info->format)) {
  418. convert_frame(&output_frame, input_frame, info);
  419. } else {
  420. copy_rgbx_frame(&output_frame, input_frame, info);
  421. }
  422. video_output_unlock_frame(video->video);
  423. }
  424. }
  425. static inline void video_sleep(struct obs_core_video *video,
  426. uint64_t *p_time, uint64_t interval_ns)
  427. {
  428. struct obs_vframe_info vframe_info;
  429. uint64_t cur_time = *p_time;
  430. uint64_t t = cur_time + interval_ns;
  431. int count;
  432. if (os_sleepto_ns(t)) {
  433. *p_time = t;
  434. count = 1;
  435. } else {
  436. count = (int)((os_gettime_ns() - cur_time) / interval_ns);
  437. *p_time = cur_time + interval_ns * count;
  438. }
  439. vframe_info.timestamp = cur_time;
  440. vframe_info.count = count;
  441. circlebuf_push_back(&video->vframe_info_buffer, &vframe_info,
  442. sizeof(vframe_info));
  443. }
  444. static const char *output_frame_gs_context_name = "gs_context(video->graphics)";
  445. static const char *output_frame_render_video_name = "render_video";
  446. static const char *output_frame_download_frame_name = "download_frame";
  447. static const char *output_frame_gs_flush_name = "gs_flush";
  448. static const char *output_frame_output_video_data_name = "output_video_data";
  449. static inline void output_frame(void)
  450. {
  451. struct obs_core_video *video = &obs->video;
  452. int cur_texture = video->cur_texture;
  453. int prev_texture = cur_texture == 0 ? NUM_TEXTURES-1 : cur_texture-1;
  454. struct video_data frame;
  455. bool frame_ready;
  456. memset(&frame, 0, sizeof(struct video_data));
  457. profile_start(output_frame_gs_context_name);
  458. gs_enter_context(video->graphics);
  459. profile_start(output_frame_render_video_name);
  460. render_video(video, cur_texture, prev_texture);
  461. profile_end(output_frame_render_video_name);
  462. profile_start(output_frame_download_frame_name);
  463. frame_ready = download_frame(video, prev_texture, &frame);
  464. profile_end(output_frame_download_frame_name);
  465. profile_start(output_frame_gs_flush_name);
  466. gs_flush();
  467. profile_end(output_frame_gs_flush_name);
  468. gs_leave_context();
  469. profile_end(output_frame_gs_context_name);
  470. if (frame_ready) {
  471. struct obs_vframe_info vframe_info;
  472. circlebuf_pop_front(&video->vframe_info_buffer, &vframe_info,
  473. sizeof(vframe_info));
  474. frame.timestamp = vframe_info.timestamp;
  475. profile_start(output_frame_output_video_data_name);
  476. output_video_data(video, &frame, vframe_info.count);
  477. profile_end(output_frame_output_video_data_name);
  478. }
  479. if (++video->cur_texture == NUM_TEXTURES)
  480. video->cur_texture = 0;
  481. }
  482. static const char *tick_sources_name = "tick_sources";
  483. static const char *render_displays_name = "render_displays";
  484. static const char *output_frame_name = "output_frame";
  485. void *obs_video_thread(void *param)
  486. {
  487. uint64_t last_time = 0;
  488. uint64_t interval = video_output_get_frame_time(obs->video.video);
  489. obs->video.video_time = os_gettime_ns();
  490. os_set_thread_name("libobs: graphics thread");
  491. const char *video_thread_name =
  492. profile_store_name(obs_get_profiler_name_store(),
  493. "obs_video_thread(%g ms)", interval / 1000000.);
  494. profile_register_root(video_thread_name, interval);
  495. while (!video_output_stopped(obs->video.video)) {
  496. profile_start(video_thread_name);
  497. profile_start(tick_sources_name);
  498. last_time = tick_sources(obs->video.video_time, last_time);
  499. profile_end(tick_sources_name);
  500. profile_start(render_displays_name);
  501. render_displays();
  502. profile_end(render_displays_name);
  503. profile_start(output_frame_name);
  504. output_frame();
  505. profile_end(output_frame_name);
  506. profile_end(video_thread_name);
  507. profile_reenable_thread();
  508. video_sleep(&obs->video, &obs->video.video_time, interval);
  509. }
  510. UNUSED_PARAMETER(param);
  511. return NULL;
  512. }