obs-video.c 11.8 KB
Newer Older
J
jp9000 已提交
1
/******************************************************************************
2
    Copyright (C) 2013-2014 by Hugh Bailey <obs.jim@gmail.com>
J
jp9000 已提交
3 4 5

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
6
    the Free Software Foundation, either version 2 of the License, or
J
jp9000 已提交
7 8 9 10 11 12 13 14 15 16 17 18
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
******************************************************************************/

#include "obs.h"
J
jp9000 已提交
19
#include "obs-internal.h"
J
jp9000 已提交
20
#include "graphics/vec4.h"
21
#include "media-io/format-conversion.h"
J
jp9000 已提交
22

23
static void tick_sources(uint64_t cur_time, uint64_t *last_time)
J
jp9000 已提交
24 25 26 27 28 29
{
	size_t i;
	uint64_t delta_time;
	float seconds;

	if (!last_time)
30
		*last_time = cur_time - video_getframetime(obs->video.video);
J
jp9000 已提交
31 32 33
	delta_time = cur_time - *last_time;
	seconds = (float)((double)delta_time / 1000000000.0);

34 35
	for (i = 0; i < obs->data.sources.num; i++)
		obs_source_video_tick(obs->data.sources.array[i], seconds);
J
jp9000 已提交
36 37 38 39

	*last_time = cur_time;
}

40 41
/* in obs-display.c */
extern void render_display(struct obs_display *display);
J
jp9000 已提交
42

43 44
static inline void render_displays(void)
{
45 46 47
	if (!obs->data.valid)
		return;

48 49
	gs_entercontext(obs_graphics());

50 51
	/* render extra displays/swaps */
	pthread_mutex_lock(&obs->data.displays_mutex);
52

53
	for (size_t i = 0; i < obs->data.displays.num; i++)
54
		render_display(obs->data.displays.array[i]);
55

56
	pthread_mutex_unlock(&obs->data.displays_mutex);
57

58
	/* render main display */
59
	render_display(&obs->video.main_display);
60 61

	gs_leavecontext();
62 63
}

64
static inline void set_render_size(uint32_t width, uint32_t height)
J
jp9000 已提交
65
{
66
	gs_enable_depthtest(false);
J
jp9000 已提交
67
	gs_enable_blending(false);
68 69
	gs_setcullmode(GS_NEITHER);

70 71 72 73
	gs_ortho(0.0f, (float)width, 0.0f, (float)height, -100.0f, 100.0f);
	gs_setviewport(0, 0, width, height);
}

74
static inline void unmap_last_surface(struct obs_core_video *video)
75 76 77 78
{
	if (video->mapped_surface) {
		stagesurface_unmap(video->mapped_surface);
		video->mapped_surface = NULL;
J
jp9000 已提交
79
	}
80
}
J
jp9000 已提交
81

82
static inline void render_main_texture(struct obs_core_video *video,
J
jp9000 已提交
83
		int cur_texture)
84 85 86
{
	struct vec4 clear_color;
	vec4_set(&clear_color, 0.3f, 0.0f, 0.0f, 1.0f);
J
jp9000 已提交
87

88 89
	gs_setrendertarget(video->render_textures[cur_texture], NULL);
	gs_clear(GS_CLEAR_COLOR, &clear_color, 1.0f, 0);
J
jp9000 已提交
90

91
	set_render_size(video->base_width, video->base_height);
J
jp9000 已提交
92
	obs_view_render(&obs->data.main_view);
J
jp9000 已提交
93

94 95 96
	video->textures_rendered[cur_texture] = true;
}

97
static inline void render_output_texture(struct obs_core_video *video,
98 99 100 101 102 103 104 105 106 107
		int cur_texture, int prev_texture)
{
	texture_t   texture = video->render_textures[prev_texture];
	texture_t   target  = video->output_textures[cur_texture];
	uint32_t    width   = texture_getwidth(target);
	uint32_t    height  = texture_getheight(target);

	/* TODO: replace with actual downscalers or unpackers */
	effect_t    effect  = video->default_effect;
	technique_t tech    = effect_gettechnique(effect, "DrawMatrix");
J
jp9000 已提交
108
	eparam_t    image   = effect_getparambyname(effect, "image");
109 110 111 112 113 114 115 116 117 118 119 120
	eparam_t    matrix  = effect_getparambyname(effect, "color_matrix");
	size_t      passes, i;

	if (!video->textures_rendered[prev_texture])
		return;

	gs_setrendertarget(target, NULL);
	set_render_size(width, height);

	/* TODO: replace with programmable code */
	const float mat_val[16] =
	{
121 122 123
		-0.100644f, -0.338572f,  0.439216f,  0.501961f,
		 0.182586f,  0.614231f,  0.062007f,  0.062745f,
		 0.439216f, -0.398942f, -0.040274f,  0.501961f,
124 125 126 127
		 0.000000f,  0.000000f,  0.000000f,  1.000000f
	};

	effect_setval(effect, matrix, mat_val, sizeof(mat_val));
J
jp9000 已提交
128
	effect_settexture(effect, image, texture);
129 130 131 132 133 134 135 136 137 138 139 140

	passes = technique_begin(tech);
	for (i = 0; i < passes; i++) {
		technique_beginpass(tech, i);
		gs_draw_sprite(texture, 0, width, height);
		technique_endpass(tech);
	}
	technique_end(tech);

	video->textures_output[cur_texture] = true;
}

J
jp9000 已提交
141 142 143 144 145 146 147
static inline void set_eparam(effect_t effect, const char *name, float val)
{
	eparam_t param = effect_getparambyname(effect, name);
	effect_setfloat(effect, param, val);
}

static void render_convert_texture(struct obs_core_video *video,
148 149 150
		int cur_texture, int prev_texture)
{
	texture_t   texture = video->output_textures[prev_texture];
J
jp9000 已提交
151 152 153 154
	texture_t   target  = video->convert_textures[cur_texture];
	float       fwidth  = (float)video->output_width;
	float       fheight = (float)video->output_height;
	size_t      passes, i;
155

J
jp9000 已提交
156 157 158 159
	effect_t    effect  = video->conversion_effect;
	eparam_t    image   = effect_getparambyname(effect, "image");
	technique_t tech    = effect_gettechnique(effect,
			video->conversion_tech);
160 161 162 163

	if (!video->textures_output[prev_texture])
		return;

J
jp9000 已提交
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
	set_eparam(effect, "u_plane_offset", (float)video->plane_offsets[1]);
	set_eparam(effect, "v_plane_offset", (float)video->plane_offsets[2]);
	set_eparam(effect, "width",  fwidth);
	set_eparam(effect, "height", fheight);
	set_eparam(effect, "width_i",  1.0f / fwidth);
	set_eparam(effect, "height_i", 1.0f / fheight);
	set_eparam(effect, "width_d2",  fwidth  * 0.5f);
	set_eparam(effect, "height_d2", fheight * 0.5f);
	set_eparam(effect, "width_d2_i",  1.0f / (fwidth  * 0.5f));
	set_eparam(effect, "height_d2_i", 1.0f / (fheight * 0.5f));
	set_eparam(effect, "input_height", (float)video->conversion_height);

	effect_settexture(effect, image, texture);

	gs_setrendertarget(target, NULL);
	set_render_size(video->output_width, video->conversion_height);

	passes = technique_begin(tech);
	for (i = 0; i < passes; i++) {
		technique_beginpass(tech, i);
		gs_draw_sprite(texture, 0, video->output_width,
				video->conversion_height);
		technique_endpass(tech);
	}
	technique_end(tech);

	video->textures_converted[cur_texture] = true;
}

static inline void stage_output_texture(struct obs_core_video *video,
		int cur_texture, int prev_texture)
{
	texture_t   texture;
	bool        texture_ready;
	stagesurf_t copy = video->copy_surfaces[cur_texture];

	if (video->gpu_conversion) {
		texture = video->convert_textures[prev_texture];
		texture_ready = video->textures_converted[prev_texture];
	} else {
		texture = video->output_textures[prev_texture];
		texture_ready = video->output_textures[prev_texture];
	}

	unmap_last_surface(video);

	if (!texture_ready)
		return;

213 214 215 216 217
	gs_stage_texture(copy, texture);

	video->textures_copied[cur_texture] = true;
}

218
static inline void render_video(struct obs_core_video *video, int cur_texture,
219 220 221 222 223 224 225
		int prev_texture)
{
	gs_beginscene();

	gs_enable_depthtest(false);
	gs_setcullmode(GS_NEITHER);

J
jp9000 已提交
226
	render_main_texture(video, cur_texture);
227
	render_output_texture(video, cur_texture, prev_texture);
J
jp9000 已提交
228 229 230
	if (video->gpu_conversion)
		render_convert_texture(video, cur_texture, prev_texture);

231 232 233
	stage_output_texture(video, cur_texture, prev_texture);

	gs_setrendertarget(NULL, NULL);
J
jp9000 已提交
234
	gs_enable_blending(true);
235 236 237 238

	gs_endscene();
}

J
jp9000 已提交
239
static inline bool download_frame(struct obs_core_video *video,
240
		int prev_texture, struct video_data *frame)
241 242 243 244
{
	stagesurf_t surface = video->copy_surfaces[prev_texture];

	if (!video->textures_copied[prev_texture])
245
		return false;
246

247 248 249 250 251 252
	if (!stagesurface_map(surface, &frame->data[0], &frame->linesize[0]))
		return false;

	video->mapped_surface = surface;
	return true;
}
253

J
jp9000 已提交
254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291
static inline uint32_t calc_linesize(uint32_t pos, uint32_t linesize)
{
	uint32_t size = pos % linesize;
	return size ? size : linesize;
}

static void copy_dealign(
		uint8_t *dst, uint32_t dst_pos, uint32_t dst_linesize,
		const uint8_t *src, uint32_t src_pos, uint32_t src_linesize,
		uint32_t remaining)
{
	while (remaining) {
		uint32_t src_remainder = src_pos % src_linesize;
		uint32_t dst_offset = dst_linesize - src_remainder;
		uint32_t src_offset = src_linesize - src_remainder;

		if (remaining < dst_offset) {
			memcpy(dst + dst_pos, src + src_pos, remaining);
			src_pos += remaining;
			dst_pos += remaining;
			remaining = 0;
		} else {
			memcpy(dst + dst_pos, src + src_pos, dst_offset);
			src_pos += src_offset;
			dst_pos += dst_offset;
			remaining -= dst_offset;
		}
	}
}

static inline uint32_t make_aligned_linesize_offset(uint32_t offset,
		uint32_t dst_linesize, uint32_t src_linesize)
{
	uint32_t remainder = offset % dst_linesize;
	return (offset / dst_linesize) * src_linesize + remainder;
}

static void fix_gpu_converted_alignment(struct obs_core_video *video,
292
		struct video_data *frame, int cur_texture)
J
jp9000 已提交
293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318
{
	struct source_frame *new_frame = &video->convert_frames[cur_texture];
	uint32_t src_linesize = frame->linesize[0];
	uint32_t dst_linesize = video->output_width * 4;
	uint32_t src_pos      = 0;

	for (size_t i = 0; i < 3; i++) {
		if (video->plane_linewidth[i] == 0)
			break;

		src_pos = make_aligned_linesize_offset(video->plane_offsets[i],
				dst_linesize, src_linesize);

		copy_dealign(new_frame->data[i], 0, dst_linesize,
				frame->data[0], src_pos, src_linesize,
				video->plane_sizes[i]);
	}

	/* replace with cached frames */
	for (size_t i = 0; i < MAX_AV_PLANES; i++) {
		frame->data[i]     = new_frame->data[i];
		frame->linesize[i] = new_frame->linesize[i];
	}
}

static bool set_gpu_converted_data(struct obs_core_video *video,
319
		struct video_data *frame, int cur_texture)
J
jp9000 已提交
320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337
{
	if (frame->linesize[0] == video->output_width*4) {
		for (size_t i = 0; i < 3; i++) {
			if (video->plane_linewidth[i] == 0)
				break;

			frame->linesize[i] = video->plane_linewidth[i];
			frame->data[i] =
				frame->data[0] + video->plane_offsets[i];
		}

	} else {
		fix_gpu_converted_alignment(video, frame, cur_texture);
	}

	return true;
}

338
static bool convert_frame(struct obs_core_video *video,
339
		struct video_data *frame,
340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358
		const struct video_output_info *info, int cur_texture)
{
	struct source_frame *new_frame = &video->convert_frames[cur_texture];

	if (info->format == VIDEO_FORMAT_I420) {
		compress_uyvx_to_i420(
				frame->data[0], frame->linesize[0],
				0, info->height,
				new_frame->data, new_frame->linesize);

	} else if (info->format == VIDEO_FORMAT_NV12) {
		compress_uyvx_to_nv12(
				frame->data[0], frame->linesize[0],
				0, info->height,
				new_frame->data, new_frame->linesize);

	} else {
		blog(LOG_WARNING, "convert_frame: unsupported texture format");
		return false;
J
jp9000 已提交
359
	}
360

J
jp9000 已提交
361
	for (size_t i = 0; i < MAX_AV_PLANES; i++) {
362 363 364 365 366 367 368 369
		frame->data[i]     = new_frame->data[i];
		frame->linesize[i] = new_frame->linesize[i];
	}

	return true;
}

static inline void output_video_data(struct obs_core_video *video,
370
		struct video_data *frame, int cur_texture)
371 372 373 374
{
	const struct video_output_info *info;
	info = video_output_getinfo(video->video);

J
jp9000 已提交
375 376 377 378 379
	if (video->gpu_conversion) {
		if (!set_gpu_converted_data(video, frame, cur_texture))
			return;

	} else if (format_is_yuv(info->format)) {
380 381
		if (!convert_frame(video, frame, info, cur_texture))
			return;
J
jp9000 已提交
382
	}
383

384
	video_output_swap_frame(video->video, frame);
385 386 387 388
}

static inline void output_frame(uint64_t timestamp)
{
389
	struct obs_core_video *video = &obs->video;
390 391
	int cur_texture  = video->cur_texture;
	int prev_texture = cur_texture == 0 ? NUM_TEXTURES-1 : cur_texture-1;
392
	struct video_data frame;
393 394
	bool frame_ready;

395
	memset(&frame, 0, sizeof(struct video_data));
396 397 398
	frame.timestamp = timestamp;

	gs_entercontext(obs_graphics());
399 400

	render_video(video, cur_texture, prev_texture);
J
jp9000 已提交
401
	frame_ready = download_frame(video, prev_texture, &frame);
402 403 404 405 406

	gs_leavecontext();

	if (frame_ready)
		output_video_data(video, &frame, cur_texture);
J
jp9000 已提交
407

408 409
	if (++video->cur_texture == NUM_TEXTURES)
		video->cur_texture = 0;
J
jp9000 已提交
410 411 412 413 414 415
}

void *obs_video_thread(void *param)
{
	uint64_t last_time = 0;

416 417
	while (video_output_wait(obs->video.video)) {
		uint64_t cur_time = video_gettime(obs->video.video);
J
jp9000 已提交
418

419
		tick_sources(cur_time, &last_time);
420

421
		render_displays();
422

423
		output_frame(cur_time);
J
jp9000 已提交
424 425
	}

J
jp9000 已提交
426
	UNUSED_PARAMETER(param);
J
jp9000 已提交
427 428
	return NULL;
}