2 * v4l2-tpg.h - Test Pattern Generator
4 * Copyright 2014 Cisco Systems, Inc. and/or its affiliates. All rights reserved.
6 * This program is free software; you may redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; version 2 of the License.
10 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
11 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
12 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
13 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
14 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
15 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
16 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 #include <linux/types.h>
24 #include <linux/errno.h>
25 #include <linux/random.h>
26 #include <linux/slab.h>
27 #include <linux/vmalloc.h>
28 #include <linux/videodev2.h>
29 #include <media/v4l2-tpg-colors.h>
35 TPG_PAT_100_HCOLORBAR
,
36 TPG_PAT_100_COLORSQUARES
,
42 TPG_PAT_CHECKERS_16X16
,
45 TPG_PAT_COLOR_CHECKERS_2X2
,
46 TPG_PAT_COLOR_CHECKERS_1X1
,
47 TPG_PAT_ALTERNATING_HLINES
,
48 TPG_PAT_ALTERNATING_VLINES
,
49 TPG_PAT_CROSS_1_PIXEL
,
50 TPG_PAT_CROSS_2_PIXELS
,
51 TPG_PAT_CROSS_10_PIXELS
,
54 /* Must be the last pattern */
58 extern const char * const tpg_pattern_strings
[];
66 enum tpg_video_aspect
{
67 TPG_VIDEO_ASPECT_IMAGE
,
69 TPG_VIDEO_ASPECT_14X9_CENTRE
,
70 TPG_VIDEO_ASPECT_16X9_CENTRE
,
71 TPG_VIDEO_ASPECT_16X9_ANAMORPHIC
,
74 enum tpg_pixel_aspect
{
75 TPG_PIXEL_ASPECT_SQUARE
,
76 TPG_PIXEL_ASPECT_NTSC
,
90 extern const char * const tpg_aspect_strings
[];
92 #define TPG_MAX_PLANES 3
93 #define TPG_MAX_PAT_LINES 8
96 /* Source frame size */
97 unsigned src_width
, src_height
;
100 /* Scaled output frame size */
101 unsigned scaled_width
;
103 bool field_alternate
;
104 /* crop coordinates are frame-based */
105 struct v4l2_rect crop
;
106 /* compose coordinates are format-based */
107 struct v4l2_rect compose
;
108 /* border and square coordinates are frame-based */
109 struct v4l2_rect border
;
110 struct v4l2_rect square
;
112 /* Color-related fields */
113 enum tpg_quality qual
;
114 unsigned qual_offset
;
127 * Stores the actual transfer function, i.e. will never be
128 * V4L2_XFER_FUNC_DEFAULT.
132 * Stores the actual Y'CbCr encoding, i.e. will never be
133 * V4L2_YCBCR_ENC_DEFAULT.
138 * Stores the actual quantization, i.e. will never be
139 * V4L2_QUANTIZATION_DEFAULT.
141 u32 real_quantization
;
142 enum tpg_video_aspect vid_aspect
;
143 enum tpg_pixel_aspect pix_aspect
;
145 unsigned real_rgb_range
;
149 u8 vdownsampling
[TPG_MAX_PLANES
];
150 u8 hdownsampling
[TPG_MAX_PLANES
];
152 * horizontal positions must be ANDed with this value to enforce
153 * correct boundaries for packed YUYV values.
155 unsigned hmask
[TPG_MAX_PLANES
];
156 /* Used to store the colors in native format, either RGB or YUV */
157 u8 colors
[TPG_COLOR_MAX
][3];
158 u8 textfg
[TPG_MAX_PLANES
][8], textbg
[TPG_MAX_PLANES
][8];
159 /* size in bytes for two pixels in each plane */
160 unsigned twopixelsize
[TPG_MAX_PLANES
];
161 unsigned bytesperline
[TPG_MAX_PLANES
];
164 enum tpg_pattern pattern
;
168 bool perc_fill_blank
;
174 /* Test pattern movement */
175 enum tpg_move_mode mv_hor_mode
;
178 enum tpg_move_mode mv_vert_mode
;
184 bool recalc_square_border
;
186 /* Used to store TPG_MAX_PAT_LINES lines, each with up to two planes */
187 unsigned max_line_width
;
188 u8
*lines
[TPG_MAX_PAT_LINES
][TPG_MAX_PLANES
];
189 u8
*downsampled_lines
[TPG_MAX_PAT_LINES
][TPG_MAX_PLANES
];
190 u8
*random_line
[TPG_MAX_PLANES
];
191 u8
*contrast_line
[TPG_MAX_PLANES
];
192 u8
*black_line
[TPG_MAX_PLANES
];
195 void tpg_init(struct tpg_data
*tpg
, unsigned w
, unsigned h
);
196 int tpg_alloc(struct tpg_data
*tpg
, unsigned max_w
);
197 void tpg_free(struct tpg_data
*tpg
);
198 void tpg_reset_source(struct tpg_data
*tpg
, unsigned width
, unsigned height
,
200 void tpg_log_status(struct tpg_data
*tpg
);
202 void tpg_set_font(const u8
*f
);
203 void tpg_gen_text(const struct tpg_data
*tpg
,
204 u8
*basep
[TPG_MAX_PLANES
][2], int y
, int x
, char *text
);
205 void tpg_calc_text_basep(struct tpg_data
*tpg
,
206 u8
*basep
[TPG_MAX_PLANES
][2], unsigned p
, u8
*vbuf
);
207 unsigned tpg_g_interleaved_plane(const struct tpg_data
*tpg
, unsigned buf_line
);
208 void tpg_fill_plane_buffer(struct tpg_data
*tpg
, v4l2_std_id std
,
209 unsigned p
, u8
*vbuf
);
210 void tpg_fillbuffer(struct tpg_data
*tpg
, v4l2_std_id std
,
211 unsigned p
, u8
*vbuf
);
212 bool tpg_s_fourcc(struct tpg_data
*tpg
, u32 fourcc
);
213 void tpg_s_crop_compose(struct tpg_data
*tpg
, const struct v4l2_rect
*crop
,
214 const struct v4l2_rect
*compose
);
216 static inline void tpg_s_pattern(struct tpg_data
*tpg
, enum tpg_pattern pattern
)
218 if (tpg
->pattern
== pattern
)
220 tpg
->pattern
= pattern
;
221 tpg
->recalc_colors
= true;
224 static inline void tpg_s_quality(struct tpg_data
*tpg
,
225 enum tpg_quality qual
, unsigned qual_offset
)
227 if (tpg
->qual
== qual
&& tpg
->qual_offset
== qual_offset
)
230 tpg
->qual_offset
= qual_offset
;
231 tpg
->recalc_colors
= true;
234 static inline enum tpg_quality
tpg_g_quality(const struct tpg_data
*tpg
)
239 static inline void tpg_s_alpha_component(struct tpg_data
*tpg
,
242 if (tpg
->alpha_component
== alpha_component
)
244 tpg
->alpha_component
= alpha_component
;
245 tpg
->recalc_colors
= true;
248 static inline void tpg_s_alpha_mode(struct tpg_data
*tpg
,
251 if (tpg
->alpha_red_only
== red_only
)
253 tpg
->alpha_red_only
= red_only
;
254 tpg
->recalc_colors
= true;
257 static inline void tpg_s_brightness(struct tpg_data
*tpg
,
260 if (tpg
->brightness
== brightness
)
262 tpg
->brightness
= brightness
;
263 tpg
->recalc_colors
= true;
266 static inline void tpg_s_contrast(struct tpg_data
*tpg
,
269 if (tpg
->contrast
== contrast
)
271 tpg
->contrast
= contrast
;
272 tpg
->recalc_colors
= true;
275 static inline void tpg_s_saturation(struct tpg_data
*tpg
,
278 if (tpg
->saturation
== saturation
)
280 tpg
->saturation
= saturation
;
281 tpg
->recalc_colors
= true;
284 static inline void tpg_s_hue(struct tpg_data
*tpg
,
290 tpg
->recalc_colors
= true;
293 static inline void tpg_s_rgb_range(struct tpg_data
*tpg
,
296 if (tpg
->rgb_range
== rgb_range
)
298 tpg
->rgb_range
= rgb_range
;
299 tpg
->recalc_colors
= true;
302 static inline void tpg_s_real_rgb_range(struct tpg_data
*tpg
,
305 if (tpg
->real_rgb_range
== rgb_range
)
307 tpg
->real_rgb_range
= rgb_range
;
308 tpg
->recalc_colors
= true;
311 static inline void tpg_s_colorspace(struct tpg_data
*tpg
, u32 colorspace
)
313 if (tpg
->colorspace
== colorspace
)
315 tpg
->colorspace
= colorspace
;
316 tpg
->recalc_colors
= true;
319 static inline u32
tpg_g_colorspace(const struct tpg_data
*tpg
)
321 return tpg
->colorspace
;
324 static inline void tpg_s_ycbcr_enc(struct tpg_data
*tpg
, u32 ycbcr_enc
)
326 if (tpg
->ycbcr_enc
== ycbcr_enc
)
328 tpg
->ycbcr_enc
= ycbcr_enc
;
329 tpg
->recalc_colors
= true;
332 static inline u32
tpg_g_ycbcr_enc(const struct tpg_data
*tpg
)
334 return tpg
->ycbcr_enc
;
337 static inline void tpg_s_xfer_func(struct tpg_data
*tpg
, u32 xfer_func
)
339 if (tpg
->xfer_func
== xfer_func
)
341 tpg
->xfer_func
= xfer_func
;
342 tpg
->recalc_colors
= true;
345 static inline u32
tpg_g_xfer_func(const struct tpg_data
*tpg
)
347 return tpg
->xfer_func
;
350 static inline void tpg_s_quantization(struct tpg_data
*tpg
, u32 quantization
)
352 if (tpg
->quantization
== quantization
)
354 tpg
->quantization
= quantization
;
355 tpg
->recalc_colors
= true;
358 static inline u32
tpg_g_quantization(const struct tpg_data
*tpg
)
360 return tpg
->quantization
;
363 static inline unsigned tpg_g_buffers(const struct tpg_data
*tpg
)
368 static inline unsigned tpg_g_planes(const struct tpg_data
*tpg
)
370 return tpg
->interleaved
? 1 : tpg
->planes
;
373 static inline bool tpg_g_interleaved(const struct tpg_data
*tpg
)
375 return tpg
->interleaved
;
378 static inline unsigned tpg_g_twopixelsize(const struct tpg_data
*tpg
, unsigned plane
)
380 return tpg
->twopixelsize
[plane
];
383 static inline unsigned tpg_hdiv(const struct tpg_data
*tpg
,
384 unsigned plane
, unsigned x
)
386 return ((x
/ tpg
->hdownsampling
[plane
]) & tpg
->hmask
[plane
]) *
387 tpg
->twopixelsize
[plane
] / 2;
390 static inline unsigned tpg_hscale(const struct tpg_data
*tpg
, unsigned x
)
392 return (x
* tpg
->scaled_width
) / tpg
->src_width
;
395 static inline unsigned tpg_hscale_div(const struct tpg_data
*tpg
,
396 unsigned plane
, unsigned x
)
398 return tpg_hdiv(tpg
, plane
, tpg_hscale(tpg
, x
));
401 static inline unsigned tpg_g_bytesperline(const struct tpg_data
*tpg
, unsigned plane
)
403 return tpg
->bytesperline
[plane
];
406 static inline void tpg_s_bytesperline(struct tpg_data
*tpg
, unsigned plane
, unsigned bpl
)
410 if (tpg
->buffers
> 1) {
411 tpg
->bytesperline
[plane
] = bpl
;
415 for (p
= 0; p
< tpg_g_planes(tpg
); p
++) {
416 unsigned plane_w
= bpl
* tpg
->twopixelsize
[p
] / tpg
->twopixelsize
[0];
418 tpg
->bytesperline
[p
] = plane_w
/ tpg
->hdownsampling
[p
];
420 if (tpg_g_interleaved(tpg
))
421 tpg
->bytesperline
[1] = tpg
->bytesperline
[0];
425 static inline unsigned tpg_g_line_width(const struct tpg_data
*tpg
, unsigned plane
)
430 if (tpg
->buffers
> 1)
431 return tpg_g_bytesperline(tpg
, plane
);
432 for (p
= 0; p
< tpg_g_planes(tpg
); p
++) {
433 unsigned plane_w
= tpg_g_bytesperline(tpg
, p
);
435 w
+= plane_w
/ tpg
->vdownsampling
[p
];
440 static inline unsigned tpg_calc_line_width(const struct tpg_data
*tpg
,
441 unsigned plane
, unsigned bpl
)
446 if (tpg
->buffers
> 1)
448 for (p
= 0; p
< tpg_g_planes(tpg
); p
++) {
449 unsigned plane_w
= bpl
* tpg
->twopixelsize
[p
] / tpg
->twopixelsize
[0];
451 plane_w
/= tpg
->hdownsampling
[p
];
452 w
+= plane_w
/ tpg
->vdownsampling
[p
];
457 static inline unsigned tpg_calc_plane_size(const struct tpg_data
*tpg
, unsigned plane
)
459 if (plane
>= tpg_g_planes(tpg
))
462 return tpg_g_bytesperline(tpg
, plane
) * tpg
->buf_height
/
463 tpg
->vdownsampling
[plane
];
466 static inline void tpg_s_buf_height(struct tpg_data
*tpg
, unsigned h
)
471 static inline void tpg_s_field(struct tpg_data
*tpg
, unsigned field
, bool alternate
)
474 tpg
->field_alternate
= alternate
;
477 static inline void tpg_s_perc_fill(struct tpg_data
*tpg
,
480 tpg
->perc_fill
= perc_fill
;
483 static inline unsigned tpg_g_perc_fill(const struct tpg_data
*tpg
)
485 return tpg
->perc_fill
;
488 static inline void tpg_s_perc_fill_blank(struct tpg_data
*tpg
,
489 bool perc_fill_blank
)
491 tpg
->perc_fill_blank
= perc_fill_blank
;
494 static inline void tpg_s_video_aspect(struct tpg_data
*tpg
,
495 enum tpg_video_aspect vid_aspect
)
497 if (tpg
->vid_aspect
== vid_aspect
)
499 tpg
->vid_aspect
= vid_aspect
;
500 tpg
->recalc_square_border
= true;
503 static inline enum tpg_video_aspect
tpg_g_video_aspect(const struct tpg_data
*tpg
)
505 return tpg
->vid_aspect
;
508 static inline void tpg_s_pixel_aspect(struct tpg_data
*tpg
,
509 enum tpg_pixel_aspect pix_aspect
)
511 if (tpg
->pix_aspect
== pix_aspect
)
513 tpg
->pix_aspect
= pix_aspect
;
514 tpg
->recalc_square_border
= true;
517 static inline void tpg_s_show_border(struct tpg_data
*tpg
,
520 tpg
->show_border
= show_border
;
523 static inline void tpg_s_show_square(struct tpg_data
*tpg
,
526 tpg
->show_square
= show_square
;
529 static inline void tpg_s_insert_sav(struct tpg_data
*tpg
, bool insert_sav
)
531 tpg
->insert_sav
= insert_sav
;
534 static inline void tpg_s_insert_eav(struct tpg_data
*tpg
, bool insert_eav
)
536 tpg
->insert_eav
= insert_eav
;
539 void tpg_update_mv_step(struct tpg_data
*tpg
);
541 static inline void tpg_s_mv_hor_mode(struct tpg_data
*tpg
,
542 enum tpg_move_mode mv_hor_mode
)
544 tpg
->mv_hor_mode
= mv_hor_mode
;
545 tpg_update_mv_step(tpg
);
548 static inline void tpg_s_mv_vert_mode(struct tpg_data
*tpg
,
549 enum tpg_move_mode mv_vert_mode
)
551 tpg
->mv_vert_mode
= mv_vert_mode
;
552 tpg_update_mv_step(tpg
);
555 static inline void tpg_init_mv_count(struct tpg_data
*tpg
)
557 tpg
->mv_hor_count
= tpg
->mv_vert_count
= 0;
560 static inline void tpg_update_mv_count(struct tpg_data
*tpg
, bool frame_is_field
)
562 tpg
->mv_hor_count
+= tpg
->mv_hor_step
* (frame_is_field
? 1 : 2);
563 tpg
->mv_vert_count
+= tpg
->mv_vert_step
* (frame_is_field
? 1 : 2);
566 static inline void tpg_s_hflip(struct tpg_data
*tpg
, bool hflip
)
568 if (tpg
->hflip
== hflip
)
571 tpg_update_mv_step(tpg
);
572 tpg
->recalc_lines
= true;
575 static inline bool tpg_g_hflip(const struct tpg_data
*tpg
)
580 static inline void tpg_s_vflip(struct tpg_data
*tpg
, bool vflip
)
585 static inline bool tpg_g_vflip(const struct tpg_data
*tpg
)
590 static inline bool tpg_pattern_is_static(const struct tpg_data
*tpg
)
592 return tpg
->pattern
!= TPG_PAT_NOISE
&&
593 tpg
->mv_hor_mode
== TPG_MOVE_NONE
&&
594 tpg
->mv_vert_mode
== TPG_MOVE_NONE
;