obs-video.c 25 KB

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