obs-video.c 35 KB

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