obs-video.c 26 KB

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