obs-video.c 34 KB

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