1
0

obs-video.c 24 KB

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