1
0

obs-video.c 36 KB

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