obs-video.c 34 KB

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