obs-video.c 26 KB

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