1 /* SPDX-License-Identifier: GPL-2.0-only */
3 * v4l2-tpg.h - Test Pattern Generator
5 * Copyright 2014 Cisco Systems, Inc. and/or its affiliates. All rights reserved.
11 #include <linux/types.h>
12 #include <linux/errno.h>
13 #include <linux/random.h>
14 #include <linux/slab.h>
15 #include <linux/vmalloc.h>
16 #include <linux/videodev2.h>
18 struct tpg_rbg_color8
{
19 unsigned char r
, g
, b
;
22 struct tpg_rbg_color16
{
31 TPG_COLOR_CSC_MAGENTA
,
45 TPG_COLOR_100_MAGENTA
,
53 TPG_COLOR_MAX
= TPG_COLOR_RAMP
+ 256
56 extern const struct tpg_rbg_color8 tpg_colors
[TPG_COLOR_MAX
];
57 extern const unsigned short tpg_rec709_to_linear
[255 * 16 + 1];
58 extern const unsigned short tpg_linear_to_rec709
[255 * 16 + 1];
59 extern const struct tpg_rbg_color16 tpg_csc_colors
[V4L2_COLORSPACE_DCI_P3
+ 1]
60 [V4L2_XFER_FUNC_SMPTE2084
+ 1]
61 [TPG_COLOR_CSC_BLACK
+ 1];
66 TPG_PAT_100_HCOLORBAR
,
67 TPG_PAT_100_COLORSQUARES
,
73 TPG_PAT_CHECKERS_16X16
,
76 TPG_PAT_COLOR_CHECKERS_2X2
,
77 TPG_PAT_COLOR_CHECKERS_1X1
,
78 TPG_PAT_ALTERNATING_HLINES
,
79 TPG_PAT_ALTERNATING_VLINES
,
80 TPG_PAT_CROSS_1_PIXEL
,
81 TPG_PAT_CROSS_2_PIXELS
,
82 TPG_PAT_CROSS_10_PIXELS
,
85 /* Must be the last pattern */
89 extern const char * const tpg_pattern_strings
[];
97 enum tpg_video_aspect
{
98 TPG_VIDEO_ASPECT_IMAGE
,
100 TPG_VIDEO_ASPECT_14X9_CENTRE
,
101 TPG_VIDEO_ASPECT_16X9_CENTRE
,
102 TPG_VIDEO_ASPECT_16X9_ANAMORPHIC
,
105 enum tpg_pixel_aspect
{
106 TPG_PIXEL_ASPECT_SQUARE
,
107 TPG_PIXEL_ASPECT_NTSC
,
108 TPG_PIXEL_ASPECT_PAL
,
128 extern const char * const tpg_aspect_strings
[];
130 #define TPG_MAX_PLANES 3
131 #define TPG_MAX_PAT_LINES 8
134 /* Source frame size */
135 unsigned src_width
, src_height
;
138 /* Scaled output frame size */
139 unsigned scaled_width
;
141 bool field_alternate
;
142 /* crop coordinates are frame-based */
143 struct v4l2_rect crop
;
144 /* compose coordinates are format-based */
145 struct v4l2_rect compose
;
146 /* border and square coordinates are frame-based */
147 struct v4l2_rect border
;
148 struct v4l2_rect square
;
150 /* Color-related fields */
151 enum tpg_quality qual
;
152 unsigned qual_offset
;
160 enum tgp_color_enc color_enc
;
166 * Stores the actual transfer function, i.e. will never be
167 * V4L2_XFER_FUNC_DEFAULT.
171 * Stores the actual Y'CbCr encoding, i.e. will never be
172 * V4L2_YCBCR_ENC_DEFAULT.
178 * Stores the actual quantization, i.e. will never be
179 * V4L2_QUANTIZATION_DEFAULT.
181 u32 real_quantization
;
182 enum tpg_video_aspect vid_aspect
;
183 enum tpg_pixel_aspect pix_aspect
;
185 unsigned real_rgb_range
;
189 u8 vdownsampling
[TPG_MAX_PLANES
];
190 u8 hdownsampling
[TPG_MAX_PLANES
];
192 * horizontal positions must be ANDed with this value to enforce
193 * correct boundaries for packed YUYV values.
195 unsigned hmask
[TPG_MAX_PLANES
];
196 /* Used to store the colors in native format, either RGB or YUV */
197 u8 colors
[TPG_COLOR_MAX
][3];
198 u8 textfg
[TPG_MAX_PLANES
][8], textbg
[TPG_MAX_PLANES
][8];
199 /* size in bytes for two pixels in each plane */
200 unsigned twopixelsize
[TPG_MAX_PLANES
];
201 unsigned bytesperline
[TPG_MAX_PLANES
];
204 enum tpg_pattern pattern
;
208 bool perc_fill_blank
;
214 /* Test pattern movement */
215 enum tpg_move_mode mv_hor_mode
;
218 enum tpg_move_mode mv_vert_mode
;
224 bool recalc_square_border
;
226 /* Used to store TPG_MAX_PAT_LINES lines, each with up to two planes */
227 unsigned max_line_width
;
228 u8
*lines
[TPG_MAX_PAT_LINES
][TPG_MAX_PLANES
];
229 u8
*downsampled_lines
[TPG_MAX_PAT_LINES
][TPG_MAX_PLANES
];
230 u8
*random_line
[TPG_MAX_PLANES
];
231 u8
*contrast_line
[TPG_MAX_PLANES
];
232 u8
*black_line
[TPG_MAX_PLANES
];
235 void tpg_init(struct tpg_data
*tpg
, unsigned w
, unsigned h
);
236 int tpg_alloc(struct tpg_data
*tpg
, unsigned max_w
);
237 void tpg_free(struct tpg_data
*tpg
);
238 void tpg_reset_source(struct tpg_data
*tpg
, unsigned width
, unsigned height
,
240 void tpg_log_status(struct tpg_data
*tpg
);
242 void tpg_set_font(const u8
*f
);
243 void tpg_gen_text(const struct tpg_data
*tpg
,
244 u8
*basep
[TPG_MAX_PLANES
][2], int y
, int x
, const char *text
);
245 void tpg_calc_text_basep(struct tpg_data
*tpg
,
246 u8
*basep
[TPG_MAX_PLANES
][2], unsigned p
, u8
*vbuf
);
247 unsigned tpg_g_interleaved_plane(const struct tpg_data
*tpg
, unsigned buf_line
);
248 void tpg_fill_plane_buffer(struct tpg_data
*tpg
, v4l2_std_id std
,
249 unsigned p
, u8
*vbuf
);
250 void tpg_fillbuffer(struct tpg_data
*tpg
, v4l2_std_id std
,
251 unsigned p
, u8
*vbuf
);
252 bool tpg_s_fourcc(struct tpg_data
*tpg
, u32 fourcc
);
253 void tpg_s_crop_compose(struct tpg_data
*tpg
, const struct v4l2_rect
*crop
,
254 const struct v4l2_rect
*compose
);
255 const char *tpg_g_color_order(const struct tpg_data
*tpg
);
257 static inline void tpg_s_pattern(struct tpg_data
*tpg
, enum tpg_pattern pattern
)
259 if (tpg
->pattern
== pattern
)
261 tpg
->pattern
= pattern
;
262 tpg
->recalc_colors
= true;
265 static inline void tpg_s_quality(struct tpg_data
*tpg
,
266 enum tpg_quality qual
, unsigned qual_offset
)
268 if (tpg
->qual
== qual
&& tpg
->qual_offset
== qual_offset
)
271 tpg
->qual_offset
= qual_offset
;
272 tpg
->recalc_colors
= true;
275 static inline enum tpg_quality
tpg_g_quality(const struct tpg_data
*tpg
)
280 static inline void tpg_s_alpha_component(struct tpg_data
*tpg
,
283 if (tpg
->alpha_component
== alpha_component
)
285 tpg
->alpha_component
= alpha_component
;
286 tpg
->recalc_colors
= true;
289 static inline void tpg_s_alpha_mode(struct tpg_data
*tpg
,
292 if (tpg
->alpha_red_only
== red_only
)
294 tpg
->alpha_red_only
= red_only
;
295 tpg
->recalc_colors
= true;
298 static inline void tpg_s_brightness(struct tpg_data
*tpg
,
301 if (tpg
->brightness
== brightness
)
303 tpg
->brightness
= brightness
;
304 tpg
->recalc_colors
= true;
307 static inline void tpg_s_contrast(struct tpg_data
*tpg
,
310 if (tpg
->contrast
== contrast
)
312 tpg
->contrast
= contrast
;
313 tpg
->recalc_colors
= true;
316 static inline void tpg_s_saturation(struct tpg_data
*tpg
,
319 if (tpg
->saturation
== saturation
)
321 tpg
->saturation
= saturation
;
322 tpg
->recalc_colors
= true;
325 static inline void tpg_s_hue(struct tpg_data
*tpg
,
331 tpg
->recalc_colors
= true;
334 static inline void tpg_s_rgb_range(struct tpg_data
*tpg
,
337 if (tpg
->rgb_range
== rgb_range
)
339 tpg
->rgb_range
= rgb_range
;
340 tpg
->recalc_colors
= true;
343 static inline void tpg_s_real_rgb_range(struct tpg_data
*tpg
,
346 if (tpg
->real_rgb_range
== rgb_range
)
348 tpg
->real_rgb_range
= rgb_range
;
349 tpg
->recalc_colors
= true;
352 static inline void tpg_s_colorspace(struct tpg_data
*tpg
, u32 colorspace
)
354 if (tpg
->colorspace
== colorspace
)
356 tpg
->colorspace
= colorspace
;
357 tpg
->recalc_colors
= true;
360 static inline u32
tpg_g_colorspace(const struct tpg_data
*tpg
)
362 return tpg
->colorspace
;
365 static inline void tpg_s_ycbcr_enc(struct tpg_data
*tpg
, u32 ycbcr_enc
)
367 if (tpg
->ycbcr_enc
== ycbcr_enc
)
369 tpg
->ycbcr_enc
= ycbcr_enc
;
370 tpg
->recalc_colors
= true;
373 static inline u32
tpg_g_ycbcr_enc(const struct tpg_data
*tpg
)
375 return tpg
->ycbcr_enc
;
378 static inline void tpg_s_hsv_enc(struct tpg_data
*tpg
, u32 hsv_enc
)
380 if (tpg
->hsv_enc
== hsv_enc
)
382 tpg
->hsv_enc
= hsv_enc
;
383 tpg
->recalc_colors
= true;
386 static inline u32
tpg_g_hsv_enc(const struct tpg_data
*tpg
)
391 static inline void tpg_s_xfer_func(struct tpg_data
*tpg
, u32 xfer_func
)
393 if (tpg
->xfer_func
== xfer_func
)
395 tpg
->xfer_func
= xfer_func
;
396 tpg
->recalc_colors
= true;
399 static inline u32
tpg_g_xfer_func(const struct tpg_data
*tpg
)
401 return tpg
->xfer_func
;
404 static inline void tpg_s_quantization(struct tpg_data
*tpg
, u32 quantization
)
406 if (tpg
->quantization
== quantization
)
408 tpg
->quantization
= quantization
;
409 tpg
->recalc_colors
= true;
412 static inline u32
tpg_g_quantization(const struct tpg_data
*tpg
)
414 return tpg
->quantization
;
417 static inline unsigned tpg_g_buffers(const struct tpg_data
*tpg
)
422 static inline unsigned tpg_g_planes(const struct tpg_data
*tpg
)
424 return tpg
->interleaved
? 1 : tpg
->planes
;
427 static inline bool tpg_g_interleaved(const struct tpg_data
*tpg
)
429 return tpg
->interleaved
;
432 static inline unsigned tpg_g_twopixelsize(const struct tpg_data
*tpg
, unsigned plane
)
434 return tpg
->twopixelsize
[plane
];
437 static inline unsigned tpg_hdiv(const struct tpg_data
*tpg
,
438 unsigned plane
, unsigned x
)
440 return ((x
/ tpg
->hdownsampling
[plane
]) & tpg
->hmask
[plane
]) *
441 tpg
->twopixelsize
[plane
] / 2;
444 static inline unsigned tpg_hscale(const struct tpg_data
*tpg
, unsigned x
)
446 return (x
* tpg
->scaled_width
) / tpg
->src_width
;
449 static inline unsigned tpg_hscale_div(const struct tpg_data
*tpg
,
450 unsigned plane
, unsigned x
)
452 return tpg_hdiv(tpg
, plane
, tpg_hscale(tpg
, x
));
455 static inline unsigned tpg_g_bytesperline(const struct tpg_data
*tpg
, unsigned plane
)
457 return tpg
->bytesperline
[plane
];
460 static inline void tpg_s_bytesperline(struct tpg_data
*tpg
, unsigned plane
, unsigned bpl
)
464 if (tpg
->buffers
> 1) {
465 tpg
->bytesperline
[plane
] = bpl
;
469 for (p
= 0; p
< tpg_g_planes(tpg
); p
++) {
470 unsigned plane_w
= bpl
* tpg
->twopixelsize
[p
] / tpg
->twopixelsize
[0];
472 tpg
->bytesperline
[p
] = plane_w
/ tpg
->hdownsampling
[p
];
474 if (tpg_g_interleaved(tpg
))
475 tpg
->bytesperline
[1] = tpg
->bytesperline
[0];
479 static inline unsigned tpg_g_line_width(const struct tpg_data
*tpg
, unsigned plane
)
484 if (tpg
->buffers
> 1)
485 return tpg_g_bytesperline(tpg
, plane
);
486 for (p
= 0; p
< tpg_g_planes(tpg
); p
++) {
487 unsigned plane_w
= tpg_g_bytesperline(tpg
, p
);
489 w
+= plane_w
/ tpg
->vdownsampling
[p
];
494 static inline unsigned tpg_calc_line_width(const struct tpg_data
*tpg
,
495 unsigned plane
, unsigned bpl
)
500 if (tpg
->buffers
> 1)
502 for (p
= 0; p
< tpg_g_planes(tpg
); p
++) {
503 unsigned plane_w
= bpl
* tpg
->twopixelsize
[p
] / tpg
->twopixelsize
[0];
505 plane_w
/= tpg
->hdownsampling
[p
];
506 w
+= plane_w
/ tpg
->vdownsampling
[p
];
511 static inline unsigned tpg_calc_plane_size(const struct tpg_data
*tpg
, unsigned plane
)
513 if (plane
>= tpg_g_planes(tpg
))
516 return tpg_g_bytesperline(tpg
, plane
) * tpg
->buf_height
/
517 tpg
->vdownsampling
[plane
];
520 static inline void tpg_s_buf_height(struct tpg_data
*tpg
, unsigned h
)
525 static inline void tpg_s_field(struct tpg_data
*tpg
, unsigned field
, bool alternate
)
528 tpg
->field_alternate
= alternate
;
531 static inline void tpg_s_perc_fill(struct tpg_data
*tpg
,
534 tpg
->perc_fill
= perc_fill
;
537 static inline unsigned tpg_g_perc_fill(const struct tpg_data
*tpg
)
539 return tpg
->perc_fill
;
542 static inline void tpg_s_perc_fill_blank(struct tpg_data
*tpg
,
543 bool perc_fill_blank
)
545 tpg
->perc_fill_blank
= perc_fill_blank
;
548 static inline void tpg_s_video_aspect(struct tpg_data
*tpg
,
549 enum tpg_video_aspect vid_aspect
)
551 if (tpg
->vid_aspect
== vid_aspect
)
553 tpg
->vid_aspect
= vid_aspect
;
554 tpg
->recalc_square_border
= true;
557 static inline enum tpg_video_aspect
tpg_g_video_aspect(const struct tpg_data
*tpg
)
559 return tpg
->vid_aspect
;
562 static inline void tpg_s_pixel_aspect(struct tpg_data
*tpg
,
563 enum tpg_pixel_aspect pix_aspect
)
565 if (tpg
->pix_aspect
== pix_aspect
)
567 tpg
->pix_aspect
= pix_aspect
;
568 tpg
->recalc_square_border
= true;
571 static inline void tpg_s_show_border(struct tpg_data
*tpg
,
574 tpg
->show_border
= show_border
;
577 static inline void tpg_s_show_square(struct tpg_data
*tpg
,
580 tpg
->show_square
= show_square
;
583 static inline void tpg_s_insert_sav(struct tpg_data
*tpg
, bool insert_sav
)
585 tpg
->insert_sav
= insert_sav
;
588 static inline void tpg_s_insert_eav(struct tpg_data
*tpg
, bool insert_eav
)
590 tpg
->insert_eav
= insert_eav
;
593 void tpg_update_mv_step(struct tpg_data
*tpg
);
595 static inline void tpg_s_mv_hor_mode(struct tpg_data
*tpg
,
596 enum tpg_move_mode mv_hor_mode
)
598 tpg
->mv_hor_mode
= mv_hor_mode
;
599 tpg_update_mv_step(tpg
);
602 static inline void tpg_s_mv_vert_mode(struct tpg_data
*tpg
,
603 enum tpg_move_mode mv_vert_mode
)
605 tpg
->mv_vert_mode
= mv_vert_mode
;
606 tpg_update_mv_step(tpg
);
609 static inline void tpg_init_mv_count(struct tpg_data
*tpg
)
611 tpg
->mv_hor_count
= tpg
->mv_vert_count
= 0;
614 static inline void tpg_update_mv_count(struct tpg_data
*tpg
, bool frame_is_field
)
616 tpg
->mv_hor_count
+= tpg
->mv_hor_step
* (frame_is_field
? 1 : 2);
617 tpg
->mv_vert_count
+= tpg
->mv_vert_step
* (frame_is_field
? 1 : 2);
620 static inline void tpg_s_hflip(struct tpg_data
*tpg
, bool hflip
)
622 if (tpg
->hflip
== hflip
)
625 tpg_update_mv_step(tpg
);
626 tpg
->recalc_lines
= true;
629 static inline bool tpg_g_hflip(const struct tpg_data
*tpg
)
634 static inline void tpg_s_vflip(struct tpg_data
*tpg
, bool vflip
)
639 static inline bool tpg_g_vflip(const struct tpg_data
*tpg
)
644 static inline bool tpg_pattern_is_static(const struct tpg_data
*tpg
)
646 return tpg
->pattern
!= TPG_PAT_NOISE
&&
647 tpg
->mv_hor_mode
== TPG_MOVE_NONE
&&
648 tpg
->mv_vert_mode
== TPG_MOVE_NONE
;