obs-video.c 18 KB

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