Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 63881df9 authored by Hans Verkuil's avatar Hans Verkuil Committed by Mauro Carvalho Chehab
Browse files

[media] vivid: add the Test Pattern Generator



The test patterns for video capture are generated by this code. All patterns
are precalculated taking into account colorspace information, pixel and video
aspect ratios and scaling information.

Signed-off-by: default avatarHans Verkuil <hans.verkuil@cisco.com>
Signed-off-by: default avatarMauro Carvalho Chehab <m.chehab@samsung.com>
parent ad4e02d5
Loading
Loading
Loading
Loading
+310 −0
Original line number Diff line number Diff line
/*
 * vivid-color.c - A table that converts colors to various colorspaces
 *
 * The test pattern generator uses the tpg_colors for its test patterns.
 * For testing colorspaces the first 8 colors of that table need to be
 * converted to their equivalent in the target colorspace.
 *
 * The tpg_csc_colors[] table is the result of that conversion and since
 * it is precalculated the colorspace conversion is just a simple table
 * lookup.
 *
 * This source also contains the code used to generate the tpg_csc_colors
 * table. Run the following command to compile it:
 *
 *	gcc vivid-colors.c -DCOMPILE_APP -o gen-colors -lm
 *
 * and run the utility.
 *
 * Note that the converted colors are in the range 0x000-0xff0 (so times 16)
 * in order to preserve precision.
 *
 * Copyright 2014 Cisco Systems, Inc. and/or its affiliates. All rights reserved.
 *
 * This program is free software; you may redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; version 2 of the License.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

#include <linux/videodev2.h>

#include "vivid-tpg-colors.h"

/* sRGB colors with range [0-255] */
const struct color tpg_colors[TPG_COLOR_MAX] = {
	/*
	 * Colors to test colorspace conversion: converting these colors
	 * to other colorspaces will never lead to out-of-gamut colors.
	 */
	{ 191, 191, 191 }, /* TPG_COLOR_CSC_WHITE */
	{ 191, 191,  50 }, /* TPG_COLOR_CSC_YELLOW */
	{  50, 191, 191 }, /* TPG_COLOR_CSC_CYAN */
	{  50, 191,  50 }, /* TPG_COLOR_CSC_GREEN */
	{ 191,  50, 191 }, /* TPG_COLOR_CSC_MAGENTA */
	{ 191,  50,  50 }, /* TPG_COLOR_CSC_RED */
	{  50,  50, 191 }, /* TPG_COLOR_CSC_BLUE */
	{  50,  50,  50 }, /* TPG_COLOR_CSC_BLACK */

	/* 75% colors */
	{ 191, 191,   0 }, /* TPG_COLOR_75_YELLOW */
	{   0, 191, 191 }, /* TPG_COLOR_75_CYAN */
	{   0, 191,   0 }, /* TPG_COLOR_75_GREEN */
	{ 191,   0, 191 }, /* TPG_COLOR_75_MAGENTA */
	{ 191,   0,   0 }, /* TPG_COLOR_75_RED */
	{   0,   0, 191 }, /* TPG_COLOR_75_BLUE */

	/* 100% colors */
	{ 255, 255, 255 }, /* TPG_COLOR_100_WHITE */
	{ 255, 255,   0 }, /* TPG_COLOR_100_YELLOW */
	{   0, 255, 255 }, /* TPG_COLOR_100_CYAN */
	{   0, 255,   0 }, /* TPG_COLOR_100_GREEN */
	{ 255,   0, 255 }, /* TPG_COLOR_100_MAGENTA */
	{ 255,   0,   0 }, /* TPG_COLOR_100_RED */
	{   0,   0, 255 }, /* TPG_COLOR_100_BLUE */
	{   0,   0,   0 }, /* TPG_COLOR_100_BLACK */

	{   0,   0,   0 }, /* TPG_COLOR_RANDOM placeholder */
};

#ifndef COMPILE_APP

/* Generated table */
const struct color16 tpg_csc_colors[V4L2_COLORSPACE_SRGB + 1][TPG_COLOR_CSC_BLACK + 1] = {
	[V4L2_COLORSPACE_SMPTE170M][0] = { 2953, 2939, 2939 },
	[V4L2_COLORSPACE_SMPTE170M][1] = { 2954, 2963, 585 },
	[V4L2_COLORSPACE_SMPTE170M][2] = { 84, 2967, 2937 },
	[V4L2_COLORSPACE_SMPTE170M][3] = { 93, 2990, 575 },
	[V4L2_COLORSPACE_SMPTE170M][4] = { 3030, 259, 2933 },
	[V4L2_COLORSPACE_SMPTE170M][5] = { 3031, 406, 557 },
	[V4L2_COLORSPACE_SMPTE170M][6] = { 544, 428, 2931 },
	[V4L2_COLORSPACE_SMPTE170M][7] = { 551, 547, 547 },
	[V4L2_COLORSPACE_SMPTE240M][0] = { 2926, 2926, 2926 },
	[V4L2_COLORSPACE_SMPTE240M][1] = { 2926, 2926, 857 },
	[V4L2_COLORSPACE_SMPTE240M][2] = { 1594, 2901, 2901 },
	[V4L2_COLORSPACE_SMPTE240M][3] = { 1594, 2901, 774 },
	[V4L2_COLORSPACE_SMPTE240M][4] = { 2484, 618, 2858 },
	[V4L2_COLORSPACE_SMPTE240M][5] = { 2484, 618, 617 },
	[V4L2_COLORSPACE_SMPTE240M][6] = { 507, 507, 2832 },
	[V4L2_COLORSPACE_SMPTE240M][7] = { 507, 507, 507 },
	[V4L2_COLORSPACE_REC709][0] = { 2939, 2939, 2939 },
	[V4L2_COLORSPACE_REC709][1] = { 2939, 2939, 547 },
	[V4L2_COLORSPACE_REC709][2] = { 547, 2939, 2939 },
	[V4L2_COLORSPACE_REC709][3] = { 547, 2939, 547 },
	[V4L2_COLORSPACE_REC709][4] = { 2939, 547, 2939 },
	[V4L2_COLORSPACE_REC709][5] = { 2939, 547, 547 },
	[V4L2_COLORSPACE_REC709][6] = { 547, 547, 2939 },
	[V4L2_COLORSPACE_REC709][7] = { 547, 547, 547 },
	[V4L2_COLORSPACE_470_SYSTEM_M][0] = { 2894, 2988, 2808 },
	[V4L2_COLORSPACE_470_SYSTEM_M][1] = { 2847, 3070, 843 },
	[V4L2_COLORSPACE_470_SYSTEM_M][2] = { 1656, 2962, 2783 },
	[V4L2_COLORSPACE_470_SYSTEM_M][3] = { 1572, 3045, 763 },
	[V4L2_COLORSPACE_470_SYSTEM_M][4] = { 2477, 229, 2743 },
	[V4L2_COLORSPACE_470_SYSTEM_M][5] = { 2422, 672, 614 },
	[V4L2_COLORSPACE_470_SYSTEM_M][6] = { 725, 63, 2718 },
	[V4L2_COLORSPACE_470_SYSTEM_M][7] = { 534, 561, 509 },
	[V4L2_COLORSPACE_470_SYSTEM_BG][0] = { 2939, 2939, 2939 },
	[V4L2_COLORSPACE_470_SYSTEM_BG][1] = { 2939, 2939, 621 },
	[V4L2_COLORSPACE_470_SYSTEM_BG][2] = { 786, 2939, 2939 },
	[V4L2_COLORSPACE_470_SYSTEM_BG][3] = { 786, 2939, 621 },
	[V4L2_COLORSPACE_470_SYSTEM_BG][4] = { 2879, 547, 2923 },
	[V4L2_COLORSPACE_470_SYSTEM_BG][5] = { 2879, 547, 547 },
	[V4L2_COLORSPACE_470_SYSTEM_BG][6] = { 547, 547, 2923 },
	[V4L2_COLORSPACE_470_SYSTEM_BG][7] = { 547, 547, 547 },
	[V4L2_COLORSPACE_SRGB][0] = { 3056, 3056, 3056 },
	[V4L2_COLORSPACE_SRGB][1] = { 3056, 3056, 800 },
	[V4L2_COLORSPACE_SRGB][2] = { 800, 3056, 3056 },
	[V4L2_COLORSPACE_SRGB][3] = { 800, 3056, 800 },
	[V4L2_COLORSPACE_SRGB][4] = { 3056, 800, 3056 },
	[V4L2_COLORSPACE_SRGB][5] = { 3056, 800, 800 },
	[V4L2_COLORSPACE_SRGB][6] = { 800, 800, 3056 },
	[V4L2_COLORSPACE_SRGB][7] = { 800, 800, 800 },
};

#else

/* This code generates the table above */

#include <math.h>
#include <stdio.h>
#include <stdlib.h>

static const double rec709_to_ntsc1953[3][3] = {
	{ 0.6698, 0.2678,  0.0323 },
	{ 0.0185, 1.0742, -0.0603 },
	{ 0.0162, 0.0432,  0.8551 }
};

static const double rec709_to_ebu[3][3] = {
	{ 0.9578, 0.0422, 0      },
	{ 0     , 1     , 0      },
	{ 0     , 0.0118, 0.9882 }
};

static const double rec709_to_170m[3][3] = {
	{  1.0654, -0.0554, -0.0010 },
	{ -0.0196,  1.0364, -0.0167 },
	{  0.0016,  0.0044,  0.9940 }
};

static const double rec709_to_240m[3][3] = {
	{ 0.7151, 0.2849, 0      },
	{ 0.0179, 0.9821, 0      },
	{ 0.0177, 0.0472, 0.9350 }
};


static void mult_matrix(double *r, double *g, double *b, const double m[3][3])
{
	double ir, ig, ib;

	ir = m[0][0] * (*r) + m[0][1] * (*g) + m[0][2] * (*b);
	ig = m[1][0] * (*r) + m[1][1] * (*g) + m[1][2] * (*b);
	ib = m[2][0] * (*r) + m[2][1] * (*g) + m[2][2] * (*b);
	*r = ir;
	*g = ig;
	*b = ib;
}

static double transfer_srgb_to_rgb(double v)
{
	return (v <= 0.03928) ? v / 12.92 : pow((v + 0.055) / 1.055, 2.4);
}

static double transfer_rgb_to_smpte240m(double v)
{
	return (v <= 0.0228) ? v * 4.0 : 1.1115 * pow(v, 0.45) - 0.1115;
}

static double transfer_rgb_to_rec709(double v)
{
	return (v < 0.018) ? v * 4.5 : 1.099 * pow(v, 0.45) - 0.099;
}

static double transfer_srgb_to_rec709(double v)
{
	return transfer_rgb_to_rec709(transfer_srgb_to_rgb(v));
}

static void csc(enum v4l2_colorspace colorspace, double *r, double *g, double *b)
{
	/* Convert the primaries of Rec. 709 Linear RGB */
	switch (colorspace) {
	case V4L2_COLORSPACE_SMPTE240M:
		*r = transfer_srgb_to_rgb(*r);
		*g = transfer_srgb_to_rgb(*g);
		*b = transfer_srgb_to_rgb(*b);
		mult_matrix(r, g, b, rec709_to_240m);
		break;
	case V4L2_COLORSPACE_SMPTE170M:
		*r = transfer_srgb_to_rgb(*r);
		*g = transfer_srgb_to_rgb(*g);
		*b = transfer_srgb_to_rgb(*b);
		mult_matrix(r, g, b, rec709_to_170m);
		break;
	case V4L2_COLORSPACE_470_SYSTEM_BG:
		*r = transfer_srgb_to_rgb(*r);
		*g = transfer_srgb_to_rgb(*g);
		*b = transfer_srgb_to_rgb(*b);
		mult_matrix(r, g, b, rec709_to_ebu);
		break;
	case V4L2_COLORSPACE_470_SYSTEM_M:
		*r = transfer_srgb_to_rgb(*r);
		*g = transfer_srgb_to_rgb(*g);
		*b = transfer_srgb_to_rgb(*b);
		mult_matrix(r, g, b, rec709_to_ntsc1953);
		break;
	case V4L2_COLORSPACE_SRGB:
	case V4L2_COLORSPACE_REC709:
	default:
		break;
	}

	*r = ((*r) < 0) ? 0 : (((*r) > 1) ? 1 : (*r));
	*g = ((*g) < 0) ? 0 : (((*g) > 1) ? 1 : (*g));
	*b = ((*b) < 0) ? 0 : (((*b) > 1) ? 1 : (*b));

	/* Encode to gamma corrected colorspace */
	switch (colorspace) {
	case V4L2_COLORSPACE_SMPTE240M:
		*r = transfer_rgb_to_smpte240m(*r);
		*g = transfer_rgb_to_smpte240m(*g);
		*b = transfer_rgb_to_smpte240m(*b);
		break;
	case V4L2_COLORSPACE_SMPTE170M:
	case V4L2_COLORSPACE_470_SYSTEM_M:
	case V4L2_COLORSPACE_470_SYSTEM_BG:
		*r = transfer_rgb_to_rec709(*r);
		*g = transfer_rgb_to_rec709(*g);
		*b = transfer_rgb_to_rec709(*b);
		break;
	case V4L2_COLORSPACE_SRGB:
		break;
	case V4L2_COLORSPACE_REC709:
	default:
		*r = transfer_srgb_to_rec709(*r);
		*g = transfer_srgb_to_rec709(*g);
		*b = transfer_srgb_to_rec709(*b);
		break;
	}
}

int main(int argc, char **argv)
{
	static const unsigned colorspaces[] = {
		0,
		V4L2_COLORSPACE_SMPTE170M,
		V4L2_COLORSPACE_SMPTE240M,
		V4L2_COLORSPACE_REC709,
		0,
		V4L2_COLORSPACE_470_SYSTEM_M,
		V4L2_COLORSPACE_470_SYSTEM_BG,
		0,
		V4L2_COLORSPACE_SRGB,
	};
	static const char * const colorspace_names[] = {
		"",
		"V4L2_COLORSPACE_SMPTE170M",
		"V4L2_COLORSPACE_SMPTE240M",
		"V4L2_COLORSPACE_REC709",
		"",
		"V4L2_COLORSPACE_470_SYSTEM_M",
		"V4L2_COLORSPACE_470_SYSTEM_BG",
		"",
		"V4L2_COLORSPACE_SRGB",
	};
	int i;
	int c;

	printf("/* Generated table */\n");
	printf("const struct color16 tpg_csc_colors[V4L2_COLORSPACE_SRGB + 1][TPG_COLOR_CSC_BLACK + 1] = {\n");
	for (c = 0; c <= V4L2_COLORSPACE_SRGB; c++) {
		for (i = 0; i <= TPG_COLOR_CSC_BLACK; i++) {
			double r, g, b;

			if (colorspaces[c] == 0)
				continue;

			r = tpg_colors[i].r / 255.0;
			g = tpg_colors[i].g / 255.0;
			b = tpg_colors[i].b / 255.0;

			csc(c, &r, &g, &b);

			printf("\t[%s][%d] = { %d, %d, %d },\n", colorspace_names[c], i,
				(int)(r * 4080), (int)(g * 4080), (int)(b * 4080));
		}
	}
	printf("};\n\n");
	return 0;
}

#endif
+64 −0
Original line number Diff line number Diff line
/*
 * vivid-color.h - Color definitions for the test pattern generator
 *
 * Copyright 2014 Cisco Systems, Inc. and/or its affiliates. All rights reserved.
 *
 * This program is free software; you may redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; version 2 of the License.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

#ifndef _VIVID_COLORS_H_
#define _VIVID_COLORS_H_

struct color {
	unsigned char r, g, b;
};

struct color16 {
	int r, g, b;
};

enum tpg_color {
	TPG_COLOR_CSC_WHITE,
	TPG_COLOR_CSC_YELLOW,
	TPG_COLOR_CSC_CYAN,
	TPG_COLOR_CSC_GREEN,
	TPG_COLOR_CSC_MAGENTA,
	TPG_COLOR_CSC_RED,
	TPG_COLOR_CSC_BLUE,
	TPG_COLOR_CSC_BLACK,
	TPG_COLOR_75_YELLOW,
	TPG_COLOR_75_CYAN,
	TPG_COLOR_75_GREEN,
	TPG_COLOR_75_MAGENTA,
	TPG_COLOR_75_RED,
	TPG_COLOR_75_BLUE,
	TPG_COLOR_100_WHITE,
	TPG_COLOR_100_YELLOW,
	TPG_COLOR_100_CYAN,
	TPG_COLOR_100_GREEN,
	TPG_COLOR_100_MAGENTA,
	TPG_COLOR_100_RED,
	TPG_COLOR_100_BLUE,
	TPG_COLOR_100_BLACK,
	TPG_COLOR_TEXTFG,
	TPG_COLOR_TEXTBG,
	TPG_COLOR_RANDOM,
	TPG_COLOR_RAMP,
	TPG_COLOR_MAX = TPG_COLOR_RAMP + 256
};

extern const struct color tpg_colors[TPG_COLOR_MAX];
extern const struct color16 tpg_csc_colors[V4L2_COLORSPACE_SRGB + 1][TPG_COLOR_CSC_BLACK + 1];

#endif
+1439 −0

File added.

Preview size limit exceeded, changes collapsed.

+438 −0
Original line number Diff line number Diff line
/*
 * vivid-tpg.h - Test Pattern Generator
 *
 * Copyright 2014 Cisco Systems, Inc. and/or its affiliates. All rights reserved.
 *
 * This program is free software; you may redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; version 2 of the License.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

#ifndef _VIVID_TPG_H_
#define _VIVID_TPG_H_

#include <linux/version.h>
#include <linux/types.h>
#include <linux/errno.h>
#include <linux/random.h>
#include <linux/slab.h>
#include <linux/videodev2.h>

#include "vivid-tpg-colors.h"

enum tpg_pattern {
	TPG_PAT_75_COLORBAR,
	TPG_PAT_100_COLORBAR,
	TPG_PAT_CSC_COLORBAR,
	TPG_PAT_100_HCOLORBAR,
	TPG_PAT_100_COLORSQUARES,
	TPG_PAT_BLACK,
	TPG_PAT_WHITE,
	TPG_PAT_RED,
	TPG_PAT_GREEN,
	TPG_PAT_BLUE,
	TPG_PAT_CHECKERS_16X16,
	TPG_PAT_CHECKERS_1X1,
	TPG_PAT_ALTERNATING_HLINES,
	TPG_PAT_ALTERNATING_VLINES,
	TPG_PAT_CROSS_1_PIXEL,
	TPG_PAT_CROSS_2_PIXELS,
	TPG_PAT_CROSS_10_PIXELS,
	TPG_PAT_GRAY_RAMP,

	/* Must be the last pattern */
	TPG_PAT_NOISE,
};

extern const char * const tpg_pattern_strings[];

enum tpg_quality {
	TPG_QUAL_COLOR,
	TPG_QUAL_GRAY,
	TPG_QUAL_NOISE
};

enum tpg_video_aspect {
	TPG_VIDEO_ASPECT_IMAGE,
	TPG_VIDEO_ASPECT_4X3,
	TPG_VIDEO_ASPECT_14X9_CENTRE,
	TPG_VIDEO_ASPECT_16X9_CENTRE,
	TPG_VIDEO_ASPECT_16X9_ANAMORPHIC,
};

enum tpg_pixel_aspect {
	TPG_PIXEL_ASPECT_SQUARE,
	TPG_PIXEL_ASPECT_NTSC,
	TPG_PIXEL_ASPECT_PAL,
};

enum tpg_move_mode {
	TPG_MOVE_NEG_FAST,
	TPG_MOVE_NEG,
	TPG_MOVE_NEG_SLOW,
	TPG_MOVE_NONE,
	TPG_MOVE_POS_SLOW,
	TPG_MOVE_POS,
	TPG_MOVE_POS_FAST,
};

extern const char * const tpg_aspect_strings[];

#define TPG_MAX_PLANES 2
#define TPG_MAX_PAT_LINES 8

struct tpg_data {
	/* Source frame size */
	unsigned			src_width, src_height;
	/* Buffer height */
	unsigned			buf_height;
	/* Scaled output frame size */
	unsigned			scaled_width;
	u32				field;
	/* crop coordinates are frame-based */
	struct v4l2_rect		crop;
	/* compose coordinates are format-based */
	struct v4l2_rect		compose;
	/* border and square coordinates are frame-based */
	struct v4l2_rect		border;
	struct v4l2_rect		square;

	/* Color-related fields */
	enum tpg_quality		qual;
	unsigned			qual_offset;
	u8				alpha_component;
	bool				alpha_red_only;
	u8				brightness;
	u8				contrast;
	u8				saturation;
	s16				hue;
	u32				fourcc;
	bool				is_yuv;
	u32				colorspace;
	enum tpg_video_aspect		vid_aspect;
	enum tpg_pixel_aspect		pix_aspect;
	unsigned			rgb_range;
	unsigned			real_rgb_range;
	unsigned			planes;
	/* Used to store the colors in native format, either RGB or YUV */
	u8				colors[TPG_COLOR_MAX][3];
	u8				textfg[TPG_MAX_PLANES][8], textbg[TPG_MAX_PLANES][8];
	/* size in bytes for two pixels in each plane */
	unsigned			twopixelsize[TPG_MAX_PLANES];
	unsigned			bytesperline[TPG_MAX_PLANES];

	/* Configuration */
	enum tpg_pattern		pattern;
	bool				hflip;
	bool				vflip;
	unsigned			perc_fill;
	bool				perc_fill_blank;
	bool				show_border;
	bool				show_square;
	bool				insert_sav;
	bool				insert_eav;

	/* Test pattern movement */
	enum tpg_move_mode		mv_hor_mode;
	int				mv_hor_count;
	int				mv_hor_step;
	enum tpg_move_mode		mv_vert_mode;
	int				mv_vert_count;
	int				mv_vert_step;

	bool				recalc_colors;
	bool				recalc_lines;
	bool				recalc_square_border;

	/* Used to store TPG_MAX_PAT_LINES lines, each with up to two planes */
	unsigned			max_line_width;
	u8				*lines[TPG_MAX_PAT_LINES][TPG_MAX_PLANES];
	u8				*random_line[TPG_MAX_PLANES];
	u8				*contrast_line[TPG_MAX_PLANES];
	u8				*black_line[TPG_MAX_PLANES];
};

void tpg_init(struct tpg_data *tpg, unsigned w, unsigned h);
int tpg_alloc(struct tpg_data *tpg, unsigned max_w);
void tpg_free(struct tpg_data *tpg);
void tpg_reset_source(struct tpg_data *tpg, unsigned width, unsigned height,
		       u32 field);

void tpg_set_font(const u8 *f);
void tpg_gen_text(struct tpg_data *tpg,
		u8 *basep[TPG_MAX_PLANES][2], int y, int x, char *text);
void tpg_calc_text_basep(struct tpg_data *tpg,
		u8 *basep[TPG_MAX_PLANES][2], unsigned p, u8 *vbuf);
void tpg_fillbuffer(struct tpg_data *tpg, v4l2_std_id std, unsigned p, u8 *vbuf);
bool tpg_s_fourcc(struct tpg_data *tpg, u32 fourcc);
void tpg_s_crop_compose(struct tpg_data *tpg, const struct v4l2_rect *crop,
		const struct v4l2_rect *compose);

static inline void tpg_s_pattern(struct tpg_data *tpg, enum tpg_pattern pattern)
{
	if (tpg->pattern == pattern)
		return;
	tpg->pattern = pattern;
	tpg->recalc_colors = true;
}

static inline void tpg_s_quality(struct tpg_data *tpg,
				    enum tpg_quality qual, unsigned qual_offset)
{
	if (tpg->qual == qual && tpg->qual_offset == qual_offset)
		return;
	tpg->qual = qual;
	tpg->qual_offset = qual_offset;
	tpg->recalc_colors = true;
}

static inline enum tpg_quality tpg_g_quality(const struct tpg_data *tpg)
{
	return tpg->qual;
}

static inline void tpg_s_alpha_component(struct tpg_data *tpg,
					    u8 alpha_component)
{
	if (tpg->alpha_component == alpha_component)
		return;
	tpg->alpha_component = alpha_component;
	tpg->recalc_colors = true;
}

static inline void tpg_s_alpha_mode(struct tpg_data *tpg,
					    bool red_only)
{
	if (tpg->alpha_red_only == red_only)
		return;
	tpg->alpha_red_only = red_only;
	tpg->recalc_colors = true;
}

static inline void tpg_s_brightness(struct tpg_data *tpg,
					u8 brightness)
{
	if (tpg->brightness == brightness)
		return;
	tpg->brightness = brightness;
	tpg->recalc_colors = true;
}

static inline void tpg_s_contrast(struct tpg_data *tpg,
					u8 contrast)
{
	if (tpg->contrast == contrast)
		return;
	tpg->contrast = contrast;
	tpg->recalc_colors = true;
}

static inline void tpg_s_saturation(struct tpg_data *tpg,
					u8 saturation)
{
	if (tpg->saturation == saturation)
		return;
	tpg->saturation = saturation;
	tpg->recalc_colors = true;
}

static inline void tpg_s_hue(struct tpg_data *tpg,
					s16 hue)
{
	if (tpg->hue == hue)
		return;
	tpg->hue = hue;
	tpg->recalc_colors = true;
}

static inline void tpg_s_rgb_range(struct tpg_data *tpg,
					unsigned rgb_range)
{
	if (tpg->rgb_range == rgb_range)
		return;
	tpg->rgb_range = rgb_range;
	tpg->recalc_colors = true;
}

static inline void tpg_s_real_rgb_range(struct tpg_data *tpg,
					unsigned rgb_range)
{
	if (tpg->real_rgb_range == rgb_range)
		return;
	tpg->real_rgb_range = rgb_range;
	tpg->recalc_colors = true;
}

static inline void tpg_s_colorspace(struct tpg_data *tpg, u32 colorspace)
{
	if (tpg->colorspace == colorspace)
		return;
	tpg->colorspace = colorspace;
	tpg->recalc_colors = true;
}

static inline u32 tpg_g_colorspace(const struct tpg_data *tpg)
{
	return tpg->colorspace;
}

static inline unsigned tpg_g_planes(const struct tpg_data *tpg)
{
	return tpg->planes;
}

static inline unsigned tpg_g_twopixelsize(const struct tpg_data *tpg, unsigned plane)
{
	return tpg->twopixelsize[plane];
}

static inline unsigned tpg_g_bytesperline(const struct tpg_data *tpg, unsigned plane)
{
	return tpg->bytesperline[plane];
}

static inline void tpg_s_bytesperline(struct tpg_data *tpg, unsigned plane, unsigned bpl)
{
	tpg->bytesperline[plane] = bpl;
}

static inline void tpg_s_buf_height(struct tpg_data *tpg, unsigned h)
{
	tpg->buf_height = h;
}

static inline void tpg_s_field(struct tpg_data *tpg, unsigned field)
{
	tpg->field = field;
}

static inline void tpg_s_perc_fill(struct tpg_data *tpg,
				      unsigned perc_fill)
{
	tpg->perc_fill = perc_fill;
}

static inline unsigned tpg_g_perc_fill(const struct tpg_data *tpg)
{
	return tpg->perc_fill;
}

static inline void tpg_s_perc_fill_blank(struct tpg_data *tpg,
					 bool perc_fill_blank)
{
	tpg->perc_fill_blank = perc_fill_blank;
}

static inline void tpg_s_video_aspect(struct tpg_data *tpg,
					enum tpg_video_aspect vid_aspect)
{
	if (tpg->vid_aspect == vid_aspect)
		return;
	tpg->vid_aspect = vid_aspect;
	tpg->recalc_square_border = true;
}

static inline enum tpg_video_aspect tpg_g_video_aspect(const struct tpg_data *tpg)
{
	return tpg->vid_aspect;
}

static inline void tpg_s_pixel_aspect(struct tpg_data *tpg,
					enum tpg_pixel_aspect pix_aspect)
{
	if (tpg->pix_aspect == pix_aspect)
		return;
	tpg->pix_aspect = pix_aspect;
	tpg->recalc_square_border = true;
}

static inline void tpg_s_show_border(struct tpg_data *tpg,
					bool show_border)
{
	tpg->show_border = show_border;
}

static inline void tpg_s_show_square(struct tpg_data *tpg,
					bool show_square)
{
	tpg->show_square = show_square;
}

static inline void tpg_s_insert_sav(struct tpg_data *tpg, bool insert_sav)
{
	tpg->insert_sav = insert_sav;
}

static inline void tpg_s_insert_eav(struct tpg_data *tpg, bool insert_eav)
{
	tpg->insert_eav = insert_eav;
}

void tpg_update_mv_step(struct tpg_data *tpg);

static inline void tpg_s_mv_hor_mode(struct tpg_data *tpg,
				enum tpg_move_mode mv_hor_mode)
{
	tpg->mv_hor_mode = mv_hor_mode;
	tpg_update_mv_step(tpg);
}

static inline void tpg_s_mv_vert_mode(struct tpg_data *tpg,
				enum tpg_move_mode mv_vert_mode)
{
	tpg->mv_vert_mode = mv_vert_mode;
	tpg_update_mv_step(tpg);
}

static inline void tpg_init_mv_count(struct tpg_data *tpg)
{
	tpg->mv_hor_count = tpg->mv_vert_count = 0;
}

static inline void tpg_update_mv_count(struct tpg_data *tpg, bool frame_is_field)
{
	tpg->mv_hor_count += tpg->mv_hor_step * (frame_is_field ? 1 : 2);
	tpg->mv_vert_count += tpg->mv_vert_step * (frame_is_field ? 1 : 2);
}

static inline void tpg_s_hflip(struct tpg_data *tpg, bool hflip)
{
	if (tpg->hflip == hflip)
		return;
	tpg->hflip = hflip;
	tpg_update_mv_step(tpg);
	tpg->recalc_lines = true;
}

static inline bool tpg_g_hflip(const struct tpg_data *tpg)
{
	return tpg->hflip;
}

static inline void tpg_s_vflip(struct tpg_data *tpg, bool vflip)
{
	tpg->vflip = vflip;
}

static inline bool tpg_g_vflip(const struct tpg_data *tpg)
{
	return tpg->vflip;
}

static inline bool tpg_pattern_is_static(const struct tpg_data *tpg)
{
	return tpg->pattern != TPG_PAT_NOISE &&
	       tpg->mv_hor_mode == TPG_MOVE_NONE &&
	       tpg->mv_vert_mode == TPG_MOVE_NONE;
}

#endif