2 * vivid-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>
30 #include "vivid-tpg-colors.h"
36 TPG_PAT_100_HCOLORBAR,
37 TPG_PAT_100_COLORSQUARES,
43 TPG_PAT_CHECKERS_16X16,
46 TPG_PAT_COLOR_CHECKERS_2X2,
47 TPG_PAT_COLOR_CHECKERS_1X1,
48 TPG_PAT_ALTERNATING_HLINES,
49 TPG_PAT_ALTERNATING_VLINES,
50 TPG_PAT_CROSS_1_PIXEL,
51 TPG_PAT_CROSS_2_PIXELS,
52 TPG_PAT_CROSS_10_PIXELS,
55 /* Must be the last pattern */
59 extern const char * const tpg_pattern_strings[];
67 enum tpg_video_aspect {
68 TPG_VIDEO_ASPECT_IMAGE,
70 TPG_VIDEO_ASPECT_14X9_CENTRE,
71 TPG_VIDEO_ASPECT_16X9_CENTRE,
72 TPG_VIDEO_ASPECT_16X9_ANAMORPHIC,
75 enum tpg_pixel_aspect {
76 TPG_PIXEL_ASPECT_SQUARE,
77 TPG_PIXEL_ASPECT_NTSC,
91 extern const char * const tpg_aspect_strings[];
93 #define TPG_MAX_PLANES 3
94 #define TPG_MAX_PAT_LINES 8
97 /* Source frame size */
98 unsigned src_width, src_height;
101 /* Scaled output frame size */
102 unsigned scaled_width;
104 bool field_alternate;
105 /* crop coordinates are frame-based */
106 struct v4l2_rect crop;
107 /* compose coordinates are format-based */
108 struct v4l2_rect compose;
109 /* border and square coordinates are frame-based */
110 struct v4l2_rect border;
111 struct v4l2_rect square;
113 /* Color-related fields */
114 enum tpg_quality qual;
115 unsigned qual_offset;
127 * Stores the actual Y'CbCr encoding, i.e. will never be
128 * V4L2_YCBCR_ENC_DEFAULT.
133 * Stores the actual quantization, i.e. will never be
134 * V4L2_QUANTIZATION_DEFAULT.
136 u32 real_quantization;
137 enum tpg_video_aspect vid_aspect;
138 enum tpg_pixel_aspect pix_aspect;
140 unsigned real_rgb_range;
144 u8 vdownsampling[TPG_MAX_PLANES];
145 u8 hdownsampling[TPG_MAX_PLANES];
147 * horizontal positions must be ANDed with this value to enforce
148 * correct boundaries for packed YUYV values.
150 unsigned hmask[TPG_MAX_PLANES];
151 /* Used to store the colors in native format, either RGB or YUV */
152 u8 colors[TPG_COLOR_MAX][3];
153 u8 textfg[TPG_MAX_PLANES][8], textbg[TPG_MAX_PLANES][8];
154 /* size in bytes for two pixels in each plane */
155 unsigned twopixelsize[TPG_MAX_PLANES];
156 unsigned bytesperline[TPG_MAX_PLANES];
159 enum tpg_pattern pattern;
163 bool perc_fill_blank;
169 /* Test pattern movement */
170 enum tpg_move_mode mv_hor_mode;
173 enum tpg_move_mode mv_vert_mode;
179 bool recalc_square_border;
181 /* Used to store TPG_MAX_PAT_LINES lines, each with up to two planes */
182 unsigned max_line_width;
183 u8 *lines[TPG_MAX_PAT_LINES][TPG_MAX_PLANES];
184 u8 *downsampled_lines[TPG_MAX_PAT_LINES][TPG_MAX_PLANES];
185 u8 *random_line[TPG_MAX_PLANES];
186 u8 *contrast_line[TPG_MAX_PLANES];
187 u8 *black_line[TPG_MAX_PLANES];
190 void tpg_init(struct tpg_data *tpg, unsigned w, unsigned h);
191 int tpg_alloc(struct tpg_data *tpg, unsigned max_w);
192 void tpg_free(struct tpg_data *tpg);
193 void tpg_reset_source(struct tpg_data *tpg, unsigned width, unsigned height,
196 void tpg_set_font(const u8 *f);
197 void tpg_gen_text(const struct tpg_data *tpg,
198 u8 *basep[TPG_MAX_PLANES][2], int y, int x, char *text);
199 void tpg_calc_text_basep(struct tpg_data *tpg,
200 u8 *basep[TPG_MAX_PLANES][2], unsigned p, u8 *vbuf);
201 unsigned tpg_g_interleaved_plane(const struct tpg_data *tpg, unsigned buf_line);
202 void tpg_fill_plane_buffer(struct tpg_data *tpg, v4l2_std_id std,
203 unsigned p, u8 *vbuf);
204 void tpg_fillbuffer(struct tpg_data *tpg, v4l2_std_id std,
205 unsigned p, u8 *vbuf);
206 bool tpg_s_fourcc(struct tpg_data *tpg, u32 fourcc);
207 void tpg_s_crop_compose(struct tpg_data *tpg, const struct v4l2_rect *crop,
208 const struct v4l2_rect *compose);
210 static inline void tpg_s_pattern(struct tpg_data *tpg, enum tpg_pattern pattern)
212 if (tpg->pattern == pattern)
214 tpg->pattern = pattern;
215 tpg->recalc_colors = true;
218 static inline void tpg_s_quality(struct tpg_data *tpg,
219 enum tpg_quality qual, unsigned qual_offset)
221 if (tpg->qual == qual && tpg->qual_offset == qual_offset)
224 tpg->qual_offset = qual_offset;
225 tpg->recalc_colors = true;
228 static inline enum tpg_quality tpg_g_quality(const struct tpg_data *tpg)
233 static inline void tpg_s_alpha_component(struct tpg_data *tpg,
236 if (tpg->alpha_component == alpha_component)
238 tpg->alpha_component = alpha_component;
239 tpg->recalc_colors = true;
242 static inline void tpg_s_alpha_mode(struct tpg_data *tpg,
245 if (tpg->alpha_red_only == red_only)
247 tpg->alpha_red_only = red_only;
248 tpg->recalc_colors = true;
251 static inline void tpg_s_brightness(struct tpg_data *tpg,
254 if (tpg->brightness == brightness)
256 tpg->brightness = brightness;
257 tpg->recalc_colors = true;
260 static inline void tpg_s_contrast(struct tpg_data *tpg,
263 if (tpg->contrast == contrast)
265 tpg->contrast = contrast;
266 tpg->recalc_colors = true;
269 static inline void tpg_s_saturation(struct tpg_data *tpg,
272 if (tpg->saturation == saturation)
274 tpg->saturation = saturation;
275 tpg->recalc_colors = true;
278 static inline void tpg_s_hue(struct tpg_data *tpg,
284 tpg->recalc_colors = true;
287 static inline void tpg_s_rgb_range(struct tpg_data *tpg,
290 if (tpg->rgb_range == rgb_range)
292 tpg->rgb_range = rgb_range;
293 tpg->recalc_colors = true;
296 static inline void tpg_s_real_rgb_range(struct tpg_data *tpg,
299 if (tpg->real_rgb_range == rgb_range)
301 tpg->real_rgb_range = rgb_range;
302 tpg->recalc_colors = true;
305 static inline void tpg_s_colorspace(struct tpg_data *tpg, u32 colorspace)
307 if (tpg->colorspace == colorspace)
309 tpg->colorspace = colorspace;
310 tpg->recalc_colors = true;
313 static inline u32 tpg_g_colorspace(const struct tpg_data *tpg)
315 return tpg->colorspace;
318 static inline void tpg_s_ycbcr_enc(struct tpg_data *tpg, u32 ycbcr_enc)
320 if (tpg->ycbcr_enc == ycbcr_enc)
322 tpg->ycbcr_enc = ycbcr_enc;
323 tpg->recalc_colors = true;
326 static inline u32 tpg_g_ycbcr_enc(const struct tpg_data *tpg)
328 return tpg->ycbcr_enc;
331 static inline void tpg_s_quantization(struct tpg_data *tpg, u32 quantization)
333 if (tpg->quantization == quantization)
335 tpg->quantization = quantization;
336 tpg->recalc_colors = true;
339 static inline u32 tpg_g_quantization(const struct tpg_data *tpg)
341 return tpg->quantization;
344 static inline unsigned tpg_g_buffers(const struct tpg_data *tpg)
349 static inline unsigned tpg_g_planes(const struct tpg_data *tpg)
351 return tpg->interleaved ? 1 : tpg->planes;
354 static inline bool tpg_g_interleaved(const struct tpg_data *tpg)
356 return tpg->interleaved;
359 static inline unsigned tpg_g_twopixelsize(const struct tpg_data *tpg, unsigned plane)
361 return tpg->twopixelsize[plane];
364 static inline unsigned tpg_hdiv(const struct tpg_data *tpg,
365 unsigned plane, unsigned x)
367 return ((x / tpg->hdownsampling[plane]) & tpg->hmask[plane]) *
368 tpg->twopixelsize[plane] / 2;
371 static inline unsigned tpg_hscale(const struct tpg_data *tpg, unsigned x)
373 return (x * tpg->scaled_width) / tpg->src_width;
376 static inline unsigned tpg_hscale_div(const struct tpg_data *tpg,
377 unsigned plane, unsigned x)
379 return tpg_hdiv(tpg, plane, tpg_hscale(tpg, x));
382 static inline unsigned tpg_g_bytesperline(const struct tpg_data *tpg, unsigned plane)
384 return tpg->bytesperline[plane];
387 static inline void tpg_s_bytesperline(struct tpg_data *tpg, unsigned plane, unsigned bpl)
391 if (tpg->buffers > 1) {
392 tpg->bytesperline[plane] = bpl;
396 for (p = 0; p < tpg_g_planes(tpg); p++) {
397 unsigned plane_w = bpl * tpg->twopixelsize[p] / tpg->twopixelsize[0];
399 tpg->bytesperline[p] = plane_w / tpg->hdownsampling[p];
404 static inline unsigned tpg_g_line_width(const struct tpg_data *tpg, unsigned plane)
409 if (tpg->buffers > 1)
410 return tpg_g_bytesperline(tpg, plane);
411 for (p = 0; p < tpg_g_planes(tpg); p++) {
412 unsigned plane_w = tpg_g_bytesperline(tpg, p);
414 w += plane_w / tpg->vdownsampling[p];
419 static inline unsigned tpg_calc_line_width(const struct tpg_data *tpg,
420 unsigned plane, unsigned bpl)
425 if (tpg->buffers > 1)
427 for (p = 0; p < tpg_g_planes(tpg); p++) {
428 unsigned plane_w = bpl * tpg->twopixelsize[p] / tpg->twopixelsize[0];
430 plane_w /= tpg->hdownsampling[p];
431 w += plane_w / tpg->vdownsampling[p];
436 static inline unsigned tpg_calc_plane_size(const struct tpg_data *tpg, unsigned plane)
438 if (plane >= tpg_g_planes(tpg))
441 return tpg_g_bytesperline(tpg, plane) * tpg->buf_height /
442 tpg->vdownsampling[plane];
445 static inline void tpg_s_buf_height(struct tpg_data *tpg, unsigned h)
450 static inline void tpg_s_field(struct tpg_data *tpg, unsigned field, bool alternate)
453 tpg->field_alternate = alternate;
456 static inline void tpg_s_perc_fill(struct tpg_data *tpg,
459 tpg->perc_fill = perc_fill;
462 static inline unsigned tpg_g_perc_fill(const struct tpg_data *tpg)
464 return tpg->perc_fill;
467 static inline void tpg_s_perc_fill_blank(struct tpg_data *tpg,
468 bool perc_fill_blank)
470 tpg->perc_fill_blank = perc_fill_blank;
473 static inline void tpg_s_video_aspect(struct tpg_data *tpg,
474 enum tpg_video_aspect vid_aspect)
476 if (tpg->vid_aspect == vid_aspect)
478 tpg->vid_aspect = vid_aspect;
479 tpg->recalc_square_border = true;
482 static inline enum tpg_video_aspect tpg_g_video_aspect(const struct tpg_data *tpg)
484 return tpg->vid_aspect;
487 static inline void tpg_s_pixel_aspect(struct tpg_data *tpg,
488 enum tpg_pixel_aspect pix_aspect)
490 if (tpg->pix_aspect == pix_aspect)
492 tpg->pix_aspect = pix_aspect;
493 tpg->recalc_square_border = true;
496 static inline void tpg_s_show_border(struct tpg_data *tpg,
499 tpg->show_border = show_border;
502 static inline void tpg_s_show_square(struct tpg_data *tpg,
505 tpg->show_square = show_square;
508 static inline void tpg_s_insert_sav(struct tpg_data *tpg, bool insert_sav)
510 tpg->insert_sav = insert_sav;
513 static inline void tpg_s_insert_eav(struct tpg_data *tpg, bool insert_eav)
515 tpg->insert_eav = insert_eav;
518 void tpg_update_mv_step(struct tpg_data *tpg);
520 static inline void tpg_s_mv_hor_mode(struct tpg_data *tpg,
521 enum tpg_move_mode mv_hor_mode)
523 tpg->mv_hor_mode = mv_hor_mode;
524 tpg_update_mv_step(tpg);
527 static inline void tpg_s_mv_vert_mode(struct tpg_data *tpg,
528 enum tpg_move_mode mv_vert_mode)
530 tpg->mv_vert_mode = mv_vert_mode;
531 tpg_update_mv_step(tpg);
534 static inline void tpg_init_mv_count(struct tpg_data *tpg)
536 tpg->mv_hor_count = tpg->mv_vert_count = 0;
539 static inline void tpg_update_mv_count(struct tpg_data *tpg, bool frame_is_field)
541 tpg->mv_hor_count += tpg->mv_hor_step * (frame_is_field ? 1 : 2);
542 tpg->mv_vert_count += tpg->mv_vert_step * (frame_is_field ? 1 : 2);
545 static inline void tpg_s_hflip(struct tpg_data *tpg, bool hflip)
547 if (tpg->hflip == hflip)
550 tpg_update_mv_step(tpg);
551 tpg->recalc_lines = true;
554 static inline bool tpg_g_hflip(const struct tpg_data *tpg)
559 static inline void tpg_s_vflip(struct tpg_data *tpg, bool vflip)
564 static inline bool tpg_g_vflip(const struct tpg_data *tpg)
569 static inline bool tpg_pattern_is_static(const struct tpg_data *tpg)
571 return tpg->pattern != TPG_PAT_NOISE &&
572 tpg->mv_hor_mode == TPG_MOVE_NONE &&
573 tpg->mv_vert_mode == TPG_MOVE_NONE;