obs-video.c 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133
  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. obs_source_t *s = obs_source_get_ref(source);
  51. if (s) {
  52. obs_source_video_tick(s, seconds);
  53. obs_source_release(s);
  54. }
  55. source = (struct obs_source *)source->context.next;
  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_with_color_space(video->render_texture, NULL,
  103. video->render_space);
  104. gs_clear(GS_CLEAR_COLOR, &clear_color, 1.0f, 0);
  105. set_render_size(video->base_width, video->base_height);
  106. pthread_mutex_lock(&obs->data.draw_callbacks_mutex);
  107. for (size_t i = obs->data.draw_callbacks.num; i > 0; i--) {
  108. struct draw_callback *callback;
  109. callback = obs->data.draw_callbacks.array + (i - 1);
  110. callback->draw(callback->param, video->base_width,
  111. video->base_height);
  112. }
  113. pthread_mutex_unlock(&obs->data.draw_callbacks_mutex);
  114. obs_view_render(&obs->data.main_view);
  115. video->texture_rendered = true;
  116. GS_DEBUG_MARKER_END();
  117. profile_end(render_main_texture_name);
  118. }
  119. static inline gs_effect_t *
  120. get_scale_effect_internal(struct obs_core_video *video)
  121. {
  122. /* if the dimension is under half the size of the original image,
  123. * bicubic/lanczos can't sample enough pixels to create an accurate
  124. * image, so use the bilinear low resolution effect instead */
  125. if (video->output_width < (video->base_width / 2) &&
  126. video->output_height < (video->base_height / 2)) {
  127. return video->bilinear_lowres_effect;
  128. }
  129. switch (video->scale_type) {
  130. case OBS_SCALE_BILINEAR:
  131. return video->default_effect;
  132. case OBS_SCALE_LANCZOS:
  133. return video->lanczos_effect;
  134. case OBS_SCALE_AREA:
  135. return video->area_effect;
  136. case OBS_SCALE_BICUBIC:
  137. default:;
  138. }
  139. return video->bicubic_effect;
  140. }
  141. static inline bool resolution_close(struct obs_core_video *video,
  142. uint32_t width, uint32_t height)
  143. {
  144. long width_cmp = (long)video->base_width - (long)width;
  145. long height_cmp = (long)video->base_height - (long)height;
  146. return labs(width_cmp) <= 16 && labs(height_cmp) <= 16;
  147. }
  148. static inline gs_effect_t *get_scale_effect(struct obs_core_video *video,
  149. uint32_t width, uint32_t height)
  150. {
  151. if (resolution_close(video, width, height)) {
  152. return video->default_effect;
  153. } else {
  154. /* if the scale method couldn't be loaded, use either bicubic
  155. * or bilinear by default */
  156. gs_effect_t *effect = get_scale_effect_internal(video);
  157. if (!effect)
  158. effect = !!video->bicubic_effect
  159. ? video->bicubic_effect
  160. : video->default_effect;
  161. return effect;
  162. }
  163. }
  164. static const char *render_output_texture_name = "render_output_texture";
  165. static inline gs_texture_t *render_output_texture(struct obs_core_video *video)
  166. {
  167. gs_texture_t *texture = video->render_texture;
  168. gs_texture_t *target = video->output_texture;
  169. uint32_t width = gs_texture_get_width(target);
  170. uint32_t height = gs_texture_get_height(target);
  171. gs_effect_t *effect = get_scale_effect(video, width, height);
  172. gs_technique_t *tech;
  173. if (video->ovi.output_format == VIDEO_FORMAT_RGBA) {
  174. tech = gs_effect_get_technique(effect, "DrawAlphaDivide");
  175. } else {
  176. if ((effect == video->default_effect) &&
  177. (width == video->base_width) &&
  178. (height == video->base_height))
  179. return texture;
  180. tech = gs_effect_get_technique(effect, "Draw");
  181. }
  182. profile_start(render_output_texture_name);
  183. gs_eparam_t *image = gs_effect_get_param_by_name(effect, "image");
  184. gs_eparam_t *bres =
  185. gs_effect_get_param_by_name(effect, "base_dimension");
  186. gs_eparam_t *bres_i =
  187. gs_effect_get_param_by_name(effect, "base_dimension_i");
  188. size_t passes, i;
  189. gs_set_render_target(target, NULL);
  190. set_render_size(width, height);
  191. if (bres) {
  192. struct vec2 base;
  193. vec2_set(&base, (float)video->base_width,
  194. (float)video->base_height);
  195. gs_effect_set_vec2(bres, &base);
  196. }
  197. if (bres_i) {
  198. struct vec2 base_i;
  199. vec2_set(&base_i, 1.0f / (float)video->base_width,
  200. 1.0f / (float)video->base_height);
  201. gs_effect_set_vec2(bres_i, &base_i);
  202. }
  203. gs_effect_set_texture_srgb(image, texture);
  204. gs_enable_framebuffer_srgb(true);
  205. gs_enable_blending(false);
  206. passes = gs_technique_begin(tech);
  207. for (i = 0; i < passes; i++) {
  208. gs_technique_begin_pass(tech, i);
  209. gs_draw_sprite(texture, 0, width, height);
  210. gs_technique_end_pass(tech);
  211. }
  212. gs_technique_end(tech);
  213. gs_enable_blending(true);
  214. gs_enable_framebuffer_srgb(false);
  215. profile_end(render_output_texture_name);
  216. return target;
  217. }
  218. static void render_convert_plane(gs_effect_t *effect, gs_texture_t *target,
  219. const char *tech_name)
  220. {
  221. gs_technique_t *tech = gs_effect_get_technique(effect, tech_name);
  222. const uint32_t width = gs_texture_get_width(target);
  223. const uint32_t height = gs_texture_get_height(target);
  224. gs_set_render_target(target, NULL);
  225. set_render_size(width, height);
  226. size_t passes = gs_technique_begin(tech);
  227. for (size_t i = 0; i < passes; i++) {
  228. gs_technique_begin_pass(tech, i);
  229. gs_draw(GS_TRIS, 0, 3);
  230. gs_technique_end_pass(tech);
  231. }
  232. gs_technique_end(tech);
  233. }
  234. static const char *render_convert_texture_name = "render_convert_texture";
  235. static void render_convert_texture(struct obs_core_video *video,
  236. gs_texture_t *const *const convert_textures,
  237. gs_texture_t *texture)
  238. {
  239. profile_start(render_convert_texture_name);
  240. gs_effect_t *effect = video->conversion_effect;
  241. gs_eparam_t *color_vec0 =
  242. gs_effect_get_param_by_name(effect, "color_vec0");
  243. gs_eparam_t *color_vec1 =
  244. gs_effect_get_param_by_name(effect, "color_vec1");
  245. gs_eparam_t *color_vec2 =
  246. gs_effect_get_param_by_name(effect, "color_vec2");
  247. gs_eparam_t *image = gs_effect_get_param_by_name(effect, "image");
  248. gs_eparam_t *width_i = gs_effect_get_param_by_name(effect, "width_i");
  249. gs_eparam_t *height_i = gs_effect_get_param_by_name(effect, "height_i");
  250. gs_eparam_t *sdr_white_nits_over_maximum = gs_effect_get_param_by_name(
  251. effect, "sdr_white_nits_over_maximum");
  252. struct vec4 vec0, vec1, vec2;
  253. vec4_set(&vec0, video->color_matrix[4], video->color_matrix[5],
  254. video->color_matrix[6], video->color_matrix[7]);
  255. vec4_set(&vec1, video->color_matrix[0], video->color_matrix[1],
  256. video->color_matrix[2], video->color_matrix[3]);
  257. vec4_set(&vec2, video->color_matrix[8], video->color_matrix[9],
  258. video->color_matrix[10], video->color_matrix[11]);
  259. gs_enable_blending(false);
  260. if (convert_textures[0]) {
  261. const float multiplier =
  262. obs_get_video_sdr_white_level() / video->maximum_nits;
  263. gs_effect_set_texture(image, texture);
  264. gs_effect_set_vec4(color_vec0, &vec0);
  265. gs_effect_set_float(sdr_white_nits_over_maximum, multiplier);
  266. render_convert_plane(effect, convert_textures[0],
  267. video->conversion_techs[0]);
  268. if (convert_textures[1]) {
  269. gs_effect_set_texture(image, texture);
  270. gs_effect_set_vec4(color_vec1, &vec1);
  271. if (!convert_textures[2])
  272. gs_effect_set_vec4(color_vec2, &vec2);
  273. gs_effect_set_float(width_i, video->conversion_width_i);
  274. gs_effect_set_float(height_i,
  275. video->conversion_height_i);
  276. gs_effect_set_float(sdr_white_nits_over_maximum,
  277. multiplier);
  278. render_convert_plane(effect, convert_textures[1],
  279. video->conversion_techs[1]);
  280. if (convert_textures[2]) {
  281. gs_effect_set_texture(image, texture);
  282. gs_effect_set_vec4(color_vec2, &vec2);
  283. gs_effect_set_float(width_i,
  284. video->conversion_width_i);
  285. gs_effect_set_float(height_i,
  286. video->conversion_height_i);
  287. gs_effect_set_float(sdr_white_nits_over_maximum,
  288. multiplier);
  289. render_convert_plane(
  290. effect, convert_textures[2],
  291. video->conversion_techs[2]);
  292. }
  293. }
  294. }
  295. gs_enable_blending(true);
  296. video->texture_converted = true;
  297. profile_end(render_convert_texture_name);
  298. }
  299. static const char *stage_output_texture_name = "stage_output_texture";
  300. static inline void
  301. stage_output_texture(struct obs_core_video *video, int cur_texture,
  302. gs_texture_t *const *const convert_textures,
  303. gs_stagesurf_t *const *const copy_surfaces,
  304. size_t channel_count)
  305. {
  306. profile_start(stage_output_texture_name);
  307. unmap_last_surface(video);
  308. if (!video->gpu_conversion) {
  309. gs_stagesurf_t *copy = copy_surfaces[0];
  310. if (copy) {
  311. gs_stage_texture(copy, video->output_texture);
  312. video->active_copy_surfaces[cur_texture][0] = copy;
  313. }
  314. video->textures_copied[cur_texture] = true;
  315. } else if (video->texture_converted) {
  316. for (size_t i = 0; i < channel_count; i++) {
  317. gs_stagesurf_t *copy = copy_surfaces[i];
  318. if (copy) {
  319. gs_stage_texture(copy, convert_textures[i]);
  320. video->active_copy_surfaces[cur_texture][i] =
  321. copy;
  322. }
  323. }
  324. video->textures_copied[cur_texture] = true;
  325. }
  326. profile_end(stage_output_texture_name);
  327. }
  328. #ifdef _WIN32
  329. static inline bool queue_frame(struct obs_core_video *video, bool raw_active,
  330. struct obs_vframe_info *vframe_info)
  331. {
  332. bool duplicate =
  333. !video->gpu_encoder_avail_queue.size ||
  334. (video->gpu_encoder_queue.size && vframe_info->count > 1);
  335. if (duplicate) {
  336. struct obs_tex_frame *tf = circlebuf_data(
  337. &video->gpu_encoder_queue,
  338. video->gpu_encoder_queue.size - sizeof(*tf));
  339. /* texture-based encoding is stopping */
  340. if (!tf) {
  341. return false;
  342. }
  343. tf->count++;
  344. os_sem_post(video->gpu_encode_semaphore);
  345. goto finish;
  346. }
  347. struct obs_tex_frame tf;
  348. circlebuf_pop_front(&video->gpu_encoder_avail_queue, &tf, sizeof(tf));
  349. if (tf.released) {
  350. gs_texture_acquire_sync(tf.tex, tf.lock_key, GS_WAIT_INFINITE);
  351. tf.released = false;
  352. }
  353. /* the vframe_info->count > 1 case causing a copy can only happen if by
  354. * some chance the very first frame has to be duplicated for whatever
  355. * reason. otherwise, it goes to the 'duplicate' case above, which
  356. * will ensure better performance. */
  357. if (raw_active || vframe_info->count > 1) {
  358. gs_copy_texture(tf.tex, video->convert_textures_encode[0]);
  359. } else {
  360. gs_texture_t *tex = video->convert_textures_encode[0];
  361. gs_texture_t *tex_uv = video->convert_textures_encode[1];
  362. video->convert_textures_encode[0] = tf.tex;
  363. video->convert_textures_encode[1] = tf.tex_uv;
  364. tf.tex = tex;
  365. tf.tex_uv = tex_uv;
  366. }
  367. tf.count = 1;
  368. tf.timestamp = vframe_info->timestamp;
  369. tf.released = true;
  370. tf.handle = gs_texture_get_shared_handle(tf.tex);
  371. gs_texture_release_sync(tf.tex, ++tf.lock_key);
  372. circlebuf_push_back(&video->gpu_encoder_queue, &tf, sizeof(tf));
  373. os_sem_post(video->gpu_encode_semaphore);
  374. finish:
  375. return --vframe_info->count;
  376. }
  377. extern void full_stop(struct obs_encoder *encoder);
  378. static inline void encode_gpu(struct obs_core_video *video, bool raw_active,
  379. struct obs_vframe_info *vframe_info)
  380. {
  381. while (queue_frame(video, raw_active, vframe_info))
  382. ;
  383. }
  384. static const char *output_gpu_encoders_name = "output_gpu_encoders";
  385. static void output_gpu_encoders(struct obs_core_video *video, bool raw_active)
  386. {
  387. profile_start(output_gpu_encoders_name);
  388. if (!video->texture_converted)
  389. goto end;
  390. if (!video->vframe_info_buffer_gpu.size)
  391. goto end;
  392. struct obs_vframe_info vframe_info;
  393. circlebuf_pop_front(&video->vframe_info_buffer_gpu, &vframe_info,
  394. sizeof(vframe_info));
  395. pthread_mutex_lock(&video->gpu_encoder_mutex);
  396. encode_gpu(video, raw_active, &vframe_info);
  397. pthread_mutex_unlock(&video->gpu_encoder_mutex);
  398. end:
  399. profile_end(output_gpu_encoders_name);
  400. }
  401. #endif
  402. static inline void render_video(struct obs_core_video *video, bool raw_active,
  403. const bool gpu_active, int cur_texture)
  404. {
  405. gs_begin_scene();
  406. gs_enable_depth_test(false);
  407. gs_set_cull_mode(GS_NEITHER);
  408. render_main_texture(video);
  409. if (raw_active || gpu_active) {
  410. gs_texture_t *const *convert_textures = video->convert_textures;
  411. gs_stagesurf_t *const *copy_surfaces =
  412. video->copy_surfaces[cur_texture];
  413. size_t channel_count = NUM_CHANNELS;
  414. gs_texture_t *texture = render_output_texture(video);
  415. #ifdef _WIN32
  416. if (gpu_active) {
  417. convert_textures = video->convert_textures_encode;
  418. copy_surfaces = video->copy_surfaces_encode;
  419. channel_count = 1;
  420. gs_flush();
  421. }
  422. #endif
  423. if (video->gpu_conversion)
  424. render_convert_texture(video, convert_textures,
  425. texture);
  426. #ifdef _WIN32
  427. if (gpu_active) {
  428. gs_flush();
  429. output_gpu_encoders(video, raw_active);
  430. }
  431. #endif
  432. if (raw_active)
  433. stage_output_texture(video, cur_texture,
  434. convert_textures, copy_surfaces,
  435. channel_count);
  436. }
  437. gs_set_render_target(NULL, NULL);
  438. gs_enable_blending(true);
  439. gs_end_scene();
  440. }
  441. static inline bool download_frame(struct obs_core_video *video,
  442. int prev_texture, struct video_data *frame)
  443. {
  444. if (!video->textures_copied[prev_texture])
  445. return false;
  446. for (int channel = 0; channel < NUM_CHANNELS; ++channel) {
  447. gs_stagesurf_t *surface =
  448. video->active_copy_surfaces[prev_texture][channel];
  449. if (surface) {
  450. if (!gs_stagesurface_map(surface, &frame->data[channel],
  451. &frame->linesize[channel]))
  452. return false;
  453. video->mapped_surfaces[channel] = surface;
  454. }
  455. }
  456. return true;
  457. }
  458. static const uint8_t *set_gpu_converted_plane(uint32_t width, uint32_t height,
  459. uint32_t linesize_input,
  460. uint32_t linesize_output,
  461. const uint8_t *in, uint8_t *out)
  462. {
  463. if ((width == linesize_input) && (width == linesize_output)) {
  464. size_t total = (size_t)width * (size_t)height;
  465. memcpy(out, in, total);
  466. in += total;
  467. } else {
  468. for (size_t y = 0; y < height; y++) {
  469. memcpy(out, in, width);
  470. out += linesize_output;
  471. in += linesize_input;
  472. }
  473. }
  474. return in;
  475. }
  476. static void set_gpu_converted_data(struct obs_core_video *video,
  477. struct video_frame *output,
  478. const struct video_data *input,
  479. const struct video_output_info *info)
  480. {
  481. switch (info->format) {
  482. case VIDEO_FORMAT_I420: {
  483. const uint32_t width = info->width;
  484. const uint32_t height = info->height;
  485. set_gpu_converted_plane(width, height, input->linesize[0],
  486. output->linesize[0], input->data[0],
  487. output->data[0]);
  488. const uint32_t width_d2 = width / 2;
  489. const uint32_t height_d2 = height / 2;
  490. set_gpu_converted_plane(width_d2, height_d2, input->linesize[1],
  491. output->linesize[1], input->data[1],
  492. output->data[1]);
  493. set_gpu_converted_plane(width_d2, height_d2, input->linesize[2],
  494. output->linesize[2], input->data[2],
  495. output->data[2]);
  496. break;
  497. }
  498. case VIDEO_FORMAT_NV12: {
  499. const uint32_t width = info->width;
  500. const uint32_t height = info->height;
  501. const uint32_t height_d2 = height / 2;
  502. if (input->linesize[1]) {
  503. set_gpu_converted_plane(width, height,
  504. input->linesize[0],
  505. output->linesize[0],
  506. input->data[0],
  507. output->data[0]);
  508. set_gpu_converted_plane(width, height_d2,
  509. input->linesize[1],
  510. output->linesize[1],
  511. input->data[1],
  512. output->data[1]);
  513. } else {
  514. const uint8_t *const in_uv = set_gpu_converted_plane(
  515. width, height, input->linesize[0],
  516. output->linesize[0], input->data[0],
  517. output->data[0]);
  518. set_gpu_converted_plane(width, height_d2,
  519. input->linesize[0],
  520. output->linesize[1], in_uv,
  521. output->data[1]);
  522. }
  523. break;
  524. }
  525. case VIDEO_FORMAT_I444: {
  526. const uint32_t width = info->width;
  527. const uint32_t height = info->height;
  528. set_gpu_converted_plane(width, height, input->linesize[0],
  529. output->linesize[0], input->data[0],
  530. output->data[0]);
  531. set_gpu_converted_plane(width, height, input->linesize[1],
  532. output->linesize[1], input->data[1],
  533. output->data[1]);
  534. set_gpu_converted_plane(width, height, input->linesize[2],
  535. output->linesize[2], input->data[2],
  536. output->data[2]);
  537. break;
  538. }
  539. case VIDEO_FORMAT_I010: {
  540. const uint32_t width = info->width;
  541. const uint32_t height = info->height;
  542. set_gpu_converted_plane(width * 2, height, input->linesize[0],
  543. output->linesize[0], input->data[0],
  544. output->data[0]);
  545. const uint32_t height_d2 = height / 2;
  546. set_gpu_converted_plane(width, height_d2, input->linesize[1],
  547. output->linesize[1], input->data[1],
  548. output->data[1]);
  549. set_gpu_converted_plane(width, height_d2, input->linesize[2],
  550. output->linesize[2], input->data[2],
  551. output->data[2]);
  552. break;
  553. }
  554. case VIDEO_FORMAT_P010: {
  555. const uint32_t width_x2 = info->width * 2;
  556. const uint32_t height = info->height;
  557. const uint32_t height_d2 = height / 2;
  558. if (input->linesize[1]) {
  559. set_gpu_converted_plane(width_x2, height,
  560. input->linesize[0],
  561. output->linesize[0],
  562. input->data[0],
  563. output->data[0]);
  564. set_gpu_converted_plane(width_x2, height_d2,
  565. input->linesize[1],
  566. output->linesize[1],
  567. input->data[1],
  568. output->data[1]);
  569. } else {
  570. const uint8_t *const in_uv = set_gpu_converted_plane(
  571. width_x2, height, input->linesize[0],
  572. output->linesize[0], input->data[0],
  573. output->data[0]);
  574. set_gpu_converted_plane(width_x2, height_d2,
  575. input->linesize[0],
  576. output->linesize[1], in_uv,
  577. output->data[1]);
  578. }
  579. break;
  580. }
  581. case VIDEO_FORMAT_NONE:
  582. case VIDEO_FORMAT_YVYU:
  583. case VIDEO_FORMAT_YUY2:
  584. case VIDEO_FORMAT_UYVY:
  585. case VIDEO_FORMAT_RGBA:
  586. case VIDEO_FORMAT_BGRA:
  587. case VIDEO_FORMAT_BGRX:
  588. case VIDEO_FORMAT_Y800:
  589. case VIDEO_FORMAT_BGR3:
  590. case VIDEO_FORMAT_I422:
  591. case VIDEO_FORMAT_I40A:
  592. case VIDEO_FORMAT_I42A:
  593. case VIDEO_FORMAT_YUVA:
  594. case VIDEO_FORMAT_AYUV:
  595. /* unimplemented */
  596. ;
  597. }
  598. }
  599. static inline void copy_rgbx_frame(struct video_frame *output,
  600. const struct video_data *input,
  601. const struct video_output_info *info)
  602. {
  603. uint8_t *in_ptr = input->data[0];
  604. uint8_t *out_ptr = output->data[0];
  605. /* if the line sizes match, do a single copy */
  606. if (input->linesize[0] == output->linesize[0]) {
  607. memcpy(out_ptr, in_ptr,
  608. (size_t)input->linesize[0] * (size_t)info->height);
  609. } else {
  610. const size_t copy_size = (size_t)info->width * 4;
  611. for (size_t y = 0; y < info->height; y++) {
  612. memcpy(out_ptr, in_ptr, copy_size);
  613. in_ptr += input->linesize[0];
  614. out_ptr += output->linesize[0];
  615. }
  616. }
  617. }
  618. static inline void output_video_data(struct obs_core_video *video,
  619. struct video_data *input_frame, int count)
  620. {
  621. const struct video_output_info *info;
  622. struct video_frame output_frame;
  623. bool locked;
  624. info = video_output_get_info(video->video);
  625. locked = video_output_lock_frame(video->video, &output_frame, count,
  626. input_frame->timestamp);
  627. if (locked) {
  628. if (video->gpu_conversion) {
  629. set_gpu_converted_data(video, &output_frame,
  630. input_frame, info);
  631. } else {
  632. copy_rgbx_frame(&output_frame, input_frame, info);
  633. }
  634. video_output_unlock_frame(video->video);
  635. }
  636. }
  637. static inline void video_sleep(struct obs_core_video *video, bool raw_active,
  638. const bool gpu_active, uint64_t *p_time,
  639. uint64_t interval_ns)
  640. {
  641. struct obs_vframe_info vframe_info;
  642. uint64_t cur_time = *p_time;
  643. uint64_t t = cur_time + interval_ns;
  644. int count;
  645. if (os_sleepto_ns(t)) {
  646. *p_time = t;
  647. count = 1;
  648. } else {
  649. const uint64_t udiff = os_gettime_ns() - cur_time;
  650. int64_t diff;
  651. memcpy(&diff, &udiff, sizeof(diff));
  652. const uint64_t clamped_diff =
  653. (diff > (int64_t)interval_ns) ? diff : interval_ns;
  654. count = (int)(clamped_diff / interval_ns);
  655. *p_time = cur_time + interval_ns * count;
  656. }
  657. video->total_frames += count;
  658. video->lagged_frames += count - 1;
  659. vframe_info.timestamp = cur_time;
  660. vframe_info.count = count;
  661. if (raw_active)
  662. circlebuf_push_back(&video->vframe_info_buffer, &vframe_info,
  663. sizeof(vframe_info));
  664. if (gpu_active)
  665. circlebuf_push_back(&video->vframe_info_buffer_gpu,
  666. &vframe_info, sizeof(vframe_info));
  667. }
  668. static const char *output_frame_gs_context_name = "gs_context(video->graphics)";
  669. static const char *output_frame_render_video_name = "render_video";
  670. static const char *output_frame_download_frame_name = "download_frame";
  671. static const char *output_frame_gs_flush_name = "gs_flush";
  672. static const char *output_frame_output_video_data_name = "output_video_data";
  673. static inline void output_frame(bool raw_active, const bool gpu_active)
  674. {
  675. struct obs_core_video *video = &obs->video;
  676. int cur_texture = video->cur_texture;
  677. int prev_texture = cur_texture == 0 ? NUM_TEXTURES - 1
  678. : cur_texture - 1;
  679. struct video_data frame;
  680. bool frame_ready = 0;
  681. memset(&frame, 0, sizeof(struct video_data));
  682. profile_start(output_frame_gs_context_name);
  683. gs_enter_context(video->graphics);
  684. profile_start(output_frame_render_video_name);
  685. GS_DEBUG_MARKER_BEGIN(GS_DEBUG_COLOR_RENDER_VIDEO,
  686. output_frame_render_video_name);
  687. render_video(video, raw_active, gpu_active, cur_texture);
  688. GS_DEBUG_MARKER_END();
  689. profile_end(output_frame_render_video_name);
  690. if (raw_active) {
  691. profile_start(output_frame_download_frame_name);
  692. frame_ready = download_frame(video, prev_texture, &frame);
  693. profile_end(output_frame_download_frame_name);
  694. }
  695. profile_start(output_frame_gs_flush_name);
  696. gs_flush();
  697. profile_end(output_frame_gs_flush_name);
  698. gs_leave_context();
  699. profile_end(output_frame_gs_context_name);
  700. if (raw_active && frame_ready) {
  701. struct obs_vframe_info vframe_info;
  702. circlebuf_pop_front(&video->vframe_info_buffer, &vframe_info,
  703. sizeof(vframe_info));
  704. frame.timestamp = vframe_info.timestamp;
  705. profile_start(output_frame_output_video_data_name);
  706. output_video_data(video, &frame, vframe_info.count);
  707. profile_end(output_frame_output_video_data_name);
  708. }
  709. if (++video->cur_texture == NUM_TEXTURES)
  710. video->cur_texture = 0;
  711. }
  712. #define NBSP "\xC2\xA0"
  713. static void clear_base_frame_data(void)
  714. {
  715. struct obs_core_video *video = &obs->video;
  716. video->texture_rendered = false;
  717. video->texture_converted = false;
  718. circlebuf_free(&video->vframe_info_buffer);
  719. video->cur_texture = 0;
  720. }
  721. static void clear_raw_frame_data(void)
  722. {
  723. struct obs_core_video *video = &obs->video;
  724. memset(video->textures_copied, 0, sizeof(video->textures_copied));
  725. circlebuf_free(&video->vframe_info_buffer);
  726. }
  727. #ifdef _WIN32
  728. static void clear_gpu_frame_data(void)
  729. {
  730. struct obs_core_video *video = &obs->video;
  731. circlebuf_free(&video->vframe_info_buffer_gpu);
  732. }
  733. #endif
  734. extern THREAD_LOCAL bool is_graphics_thread;
  735. static void execute_graphics_tasks(void)
  736. {
  737. struct obs_core_video *video = &obs->video;
  738. bool tasks_remaining = true;
  739. while (tasks_remaining) {
  740. pthread_mutex_lock(&video->task_mutex);
  741. if (video->tasks.size) {
  742. struct obs_task_info info;
  743. circlebuf_pop_front(&video->tasks, &info, sizeof(info));
  744. info.task(info.param);
  745. }
  746. tasks_remaining = !!video->tasks.size;
  747. pthread_mutex_unlock(&video->task_mutex);
  748. }
  749. }
  750. #ifdef _WIN32
  751. struct winrt_exports {
  752. void (*winrt_initialize)();
  753. void (*winrt_uninitialize)();
  754. struct winrt_disaptcher *(*winrt_dispatcher_init)();
  755. void (*winrt_dispatcher_free)(struct winrt_disaptcher *dispatcher);
  756. void (*winrt_capture_thread_start)();
  757. void (*winrt_capture_thread_stop)();
  758. };
  759. #define WINRT_IMPORT(func) \
  760. do { \
  761. exports->func = os_dlsym(module, #func); \
  762. if (!exports->func) { \
  763. success = false; \
  764. blog(LOG_ERROR, \
  765. "Could not load function '%s' from " \
  766. "module '%s'", \
  767. #func, module_name); \
  768. } \
  769. } while (false)
  770. static bool load_winrt_imports(struct winrt_exports *exports, void *module,
  771. const char *module_name)
  772. {
  773. bool success = true;
  774. WINRT_IMPORT(winrt_initialize);
  775. WINRT_IMPORT(winrt_uninitialize);
  776. WINRT_IMPORT(winrt_dispatcher_init);
  777. WINRT_IMPORT(winrt_dispatcher_free);
  778. WINRT_IMPORT(winrt_capture_thread_start);
  779. WINRT_IMPORT(winrt_capture_thread_stop);
  780. return success;
  781. }
  782. struct winrt_state {
  783. bool loaded;
  784. void *winrt_module;
  785. struct winrt_exports exports;
  786. struct winrt_disaptcher *dispatcher;
  787. };
  788. static void init_winrt_state(struct winrt_state *winrt)
  789. {
  790. static const char *const module_name = "libobs-winrt";
  791. winrt->winrt_module = os_dlopen(module_name);
  792. winrt->loaded = winrt->winrt_module &&
  793. load_winrt_imports(&winrt->exports, winrt->winrt_module,
  794. module_name);
  795. winrt->dispatcher = NULL;
  796. if (winrt->loaded) {
  797. winrt->exports.winrt_initialize();
  798. winrt->dispatcher = winrt->exports.winrt_dispatcher_init();
  799. gs_enter_context(obs->video.graphics);
  800. winrt->exports.winrt_capture_thread_start();
  801. gs_leave_context();
  802. }
  803. }
  804. static void uninit_winrt_state(struct winrt_state *winrt)
  805. {
  806. if (winrt->winrt_module) {
  807. if (winrt->loaded) {
  808. winrt->exports.winrt_capture_thread_stop();
  809. if (winrt->dispatcher)
  810. winrt->exports.winrt_dispatcher_free(
  811. winrt->dispatcher);
  812. winrt->exports.winrt_uninitialize();
  813. }
  814. os_dlclose(winrt->winrt_module);
  815. }
  816. }
  817. #endif // #ifdef _WIN32
  818. static const char *tick_sources_name = "tick_sources";
  819. static const char *render_displays_name = "render_displays";
  820. static const char *output_frame_name = "output_frame";
  821. bool obs_graphics_thread_loop(struct obs_graphics_context *context)
  822. {
  823. /* defer loop break to clean up sources */
  824. const bool stop_requested = video_output_stopped(obs->video.video);
  825. uint64_t frame_start = os_gettime_ns();
  826. uint64_t frame_time_ns;
  827. bool raw_active = os_atomic_load_long(&obs->video.raw_active) > 0;
  828. #ifdef _WIN32
  829. const bool gpu_active =
  830. os_atomic_load_long(&obs->video.gpu_encoder_active) > 0;
  831. const bool active = raw_active || gpu_active;
  832. #else
  833. const bool gpu_active = 0;
  834. const bool active = raw_active;
  835. #endif
  836. if (!context->was_active && active)
  837. clear_base_frame_data();
  838. if (!context->raw_was_active && raw_active)
  839. clear_raw_frame_data();
  840. #ifdef _WIN32
  841. if (!context->gpu_was_active && gpu_active)
  842. clear_gpu_frame_data();
  843. context->gpu_was_active = gpu_active;
  844. #endif
  845. context->raw_was_active = raw_active;
  846. context->was_active = active;
  847. profile_start(context->video_thread_name);
  848. gs_enter_context(obs->video.graphics);
  849. gs_begin_frame();
  850. gs_leave_context();
  851. profile_start(tick_sources_name);
  852. context->last_time =
  853. tick_sources(obs->video.video_time, context->last_time);
  854. profile_end(tick_sources_name);
  855. #ifdef _WIN32
  856. MSG msg;
  857. while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) {
  858. TranslateMessage(&msg);
  859. DispatchMessage(&msg);
  860. }
  861. #endif
  862. profile_start(output_frame_name);
  863. output_frame(raw_active, gpu_active);
  864. profile_end(output_frame_name);
  865. profile_start(render_displays_name);
  866. render_displays();
  867. profile_end(render_displays_name);
  868. execute_graphics_tasks();
  869. frame_time_ns = os_gettime_ns() - frame_start;
  870. profile_end(context->video_thread_name);
  871. profile_reenable_thread();
  872. video_sleep(&obs->video, raw_active, gpu_active, &obs->video.video_time,
  873. context->interval);
  874. context->frame_time_total_ns += frame_time_ns;
  875. context->fps_total_ns += (obs->video.video_time - context->last_time);
  876. context->fps_total_frames++;
  877. if (context->fps_total_ns >= 1000000000ULL) {
  878. obs->video.video_fps =
  879. (double)context->fps_total_frames /
  880. ((double)context->fps_total_ns / 1000000000.0);
  881. obs->video.video_avg_frame_time_ns =
  882. context->frame_time_total_ns /
  883. (uint64_t)context->fps_total_frames;
  884. context->frame_time_total_ns = 0;
  885. context->fps_total_ns = 0;
  886. context->fps_total_frames = 0;
  887. }
  888. return !stop_requested;
  889. }
  890. void *obs_graphics_thread(void *param)
  891. {
  892. #ifdef _WIN32
  893. struct winrt_state winrt;
  894. init_winrt_state(&winrt);
  895. #endif // #ifdef _WIN32
  896. is_graphics_thread = true;
  897. const uint64_t interval = video_output_get_frame_time(obs->video.video);
  898. obs->video.video_time = os_gettime_ns();
  899. obs->video.video_frame_interval_ns = interval;
  900. os_set_thread_name("libobs: graphics thread");
  901. const char *video_thread_name = profile_store_name(
  902. obs_get_profiler_name_store(),
  903. "obs_graphics_thread(%g" NBSP "ms)", interval / 1000000.);
  904. profile_register_root(video_thread_name, interval);
  905. srand((unsigned int)time(NULL));
  906. struct obs_graphics_context context;
  907. context.interval = video_output_get_frame_time(obs->video.video);
  908. context.frame_time_total_ns = 0;
  909. context.fps_total_ns = 0;
  910. context.fps_total_frames = 0;
  911. context.last_time = 0;
  912. #ifdef _WIN32
  913. context.gpu_was_active = false;
  914. #endif
  915. context.raw_was_active = false;
  916. context.was_active = false;
  917. context.video_thread_name = video_thread_name;
  918. #ifdef __APPLE__
  919. while (obs_graphics_thread_loop_autorelease(&context))
  920. #else
  921. while (obs_graphics_thread_loop(&context))
  922. #endif
  923. ;
  924. #ifdef _WIN32
  925. uninit_winrt_state(&winrt);
  926. #endif
  927. UNUSED_PARAMETER(param);
  928. return NULL;
  929. }