blob: e796a54e6c9f9708ec754342b7be77a0cf088080 [file] [log] [blame]
Hans Verkuil63881df2014-08-25 08:02:14 -03001/*
2 * vivid-tpg.h - Test Pattern Generator
3 *
4 * Copyright 2014 Cisco Systems, Inc. and/or its affiliates. All rights reserved.
5 *
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.
9 *
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
17 * SOFTWARE.
18 */
19
20#ifndef _VIVID_TPG_H_
21#define _VIVID_TPG_H_
22
Hans Verkuil63881df2014-08-25 08:02:14 -030023#include <linux/types.h>
24#include <linux/errno.h>
25#include <linux/random.h>
26#include <linux/slab.h>
Hans Verkuil5754d0d2014-09-03 04:29:00 -030027#include <linux/vmalloc.h>
Hans Verkuil63881df2014-08-25 08:02:14 -030028#include <linux/videodev2.h>
29
30#include "vivid-tpg-colors.h"
31
32enum tpg_pattern {
33 TPG_PAT_75_COLORBAR,
34 TPG_PAT_100_COLORBAR,
35 TPG_PAT_CSC_COLORBAR,
36 TPG_PAT_100_HCOLORBAR,
37 TPG_PAT_100_COLORSQUARES,
38 TPG_PAT_BLACK,
39 TPG_PAT_WHITE,
40 TPG_PAT_RED,
41 TPG_PAT_GREEN,
42 TPG_PAT_BLUE,
43 TPG_PAT_CHECKERS_16X16,
Hans Verkuil1a05d312015-03-07 12:49:57 -030044 TPG_PAT_CHECKERS_2X2,
Hans Verkuil63881df2014-08-25 08:02:14 -030045 TPG_PAT_CHECKERS_1X1,
Hans Verkuil1a05d312015-03-07 12:49:57 -030046 TPG_PAT_COLOR_CHECKERS_2X2,
47 TPG_PAT_COLOR_CHECKERS_1X1,
Hans Verkuil63881df2014-08-25 08:02:14 -030048 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,
53 TPG_PAT_GRAY_RAMP,
54
55 /* Must be the last pattern */
56 TPG_PAT_NOISE,
57};
58
59extern const char * const tpg_pattern_strings[];
60
61enum tpg_quality {
62 TPG_QUAL_COLOR,
63 TPG_QUAL_GRAY,
64 TPG_QUAL_NOISE
65};
66
67enum tpg_video_aspect {
68 TPG_VIDEO_ASPECT_IMAGE,
69 TPG_VIDEO_ASPECT_4X3,
70 TPG_VIDEO_ASPECT_14X9_CENTRE,
71 TPG_VIDEO_ASPECT_16X9_CENTRE,
72 TPG_VIDEO_ASPECT_16X9_ANAMORPHIC,
73};
74
75enum tpg_pixel_aspect {
76 TPG_PIXEL_ASPECT_SQUARE,
77 TPG_PIXEL_ASPECT_NTSC,
78 TPG_PIXEL_ASPECT_PAL,
79};
80
81enum tpg_move_mode {
82 TPG_MOVE_NEG_FAST,
83 TPG_MOVE_NEG,
84 TPG_MOVE_NEG_SLOW,
85 TPG_MOVE_NONE,
86 TPG_MOVE_POS_SLOW,
87 TPG_MOVE_POS,
88 TPG_MOVE_POS_FAST,
89};
90
91extern const char * const tpg_aspect_strings[];
92
93#define TPG_MAX_PLANES 2
94#define TPG_MAX_PAT_LINES 8
95
96struct tpg_data {
97 /* Source frame size */
98 unsigned src_width, src_height;
99 /* Buffer height */
100 unsigned buf_height;
101 /* Scaled output frame size */
102 unsigned scaled_width;
103 u32 field;
Hans Verkuil43047f62015-03-07 12:38:42 -0300104 bool field_alternate;
Hans Verkuil63881df2014-08-25 08:02:14 -0300105 /* 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;
112
113 /* Color-related fields */
114 enum tpg_quality qual;
115 unsigned qual_offset;
116 u8 alpha_component;
117 bool alpha_red_only;
118 u8 brightness;
119 u8 contrast;
120 u8 saturation;
121 s16 hue;
122 u32 fourcc;
123 bool is_yuv;
124 u32 colorspace;
Hans Verkuil481b97a2014-11-17 10:14:32 -0300125 u32 ycbcr_enc;
126 /*
127 * Stores the actual Y'CbCr encoding, i.e. will never be
128 * V4L2_YCBCR_ENC_DEFAULT.
129 */
130 u32 real_ycbcr_enc;
131 u32 quantization;
132 /*
133 * Stores the actual quantization, i.e. will never be
134 * V4L2_QUANTIZATION_DEFAULT.
135 */
136 u32 real_quantization;
Hans Verkuil63881df2014-08-25 08:02:14 -0300137 enum tpg_video_aspect vid_aspect;
138 enum tpg_pixel_aspect pix_aspect;
139 unsigned rgb_range;
140 unsigned real_rgb_range;
141 unsigned planes;
142 /* Used to store the colors in native format, either RGB or YUV */
143 u8 colors[TPG_COLOR_MAX][3];
144 u8 textfg[TPG_MAX_PLANES][8], textbg[TPG_MAX_PLANES][8];
145 /* size in bytes for two pixels in each plane */
146 unsigned twopixelsize[TPG_MAX_PLANES];
147 unsigned bytesperline[TPG_MAX_PLANES];
148
149 /* Configuration */
150 enum tpg_pattern pattern;
151 bool hflip;
152 bool vflip;
153 unsigned perc_fill;
154 bool perc_fill_blank;
155 bool show_border;
156 bool show_square;
157 bool insert_sav;
158 bool insert_eav;
159
160 /* Test pattern movement */
161 enum tpg_move_mode mv_hor_mode;
162 int mv_hor_count;
163 int mv_hor_step;
164 enum tpg_move_mode mv_vert_mode;
165 int mv_vert_count;
166 int mv_vert_step;
167
168 bool recalc_colors;
169 bool recalc_lines;
170 bool recalc_square_border;
171
172 /* Used to store TPG_MAX_PAT_LINES lines, each with up to two planes */
173 unsigned max_line_width;
174 u8 *lines[TPG_MAX_PAT_LINES][TPG_MAX_PLANES];
175 u8 *random_line[TPG_MAX_PLANES];
176 u8 *contrast_line[TPG_MAX_PLANES];
177 u8 *black_line[TPG_MAX_PLANES];
178};
179
180void tpg_init(struct tpg_data *tpg, unsigned w, unsigned h);
181int tpg_alloc(struct tpg_data *tpg, unsigned max_w);
182void tpg_free(struct tpg_data *tpg);
183void tpg_reset_source(struct tpg_data *tpg, unsigned width, unsigned height,
184 u32 field);
185
186void tpg_set_font(const u8 *f);
187void tpg_gen_text(struct tpg_data *tpg,
188 u8 *basep[TPG_MAX_PLANES][2], int y, int x, char *text);
189void tpg_calc_text_basep(struct tpg_data *tpg,
190 u8 *basep[TPG_MAX_PLANES][2], unsigned p, u8 *vbuf);
191void tpg_fillbuffer(struct tpg_data *tpg, v4l2_std_id std, unsigned p, u8 *vbuf);
192bool tpg_s_fourcc(struct tpg_data *tpg, u32 fourcc);
193void tpg_s_crop_compose(struct tpg_data *tpg, const struct v4l2_rect *crop,
194 const struct v4l2_rect *compose);
195
196static inline void tpg_s_pattern(struct tpg_data *tpg, enum tpg_pattern pattern)
197{
198 if (tpg->pattern == pattern)
199 return;
200 tpg->pattern = pattern;
201 tpg->recalc_colors = true;
202}
203
204static inline void tpg_s_quality(struct tpg_data *tpg,
205 enum tpg_quality qual, unsigned qual_offset)
206{
207 if (tpg->qual == qual && tpg->qual_offset == qual_offset)
208 return;
209 tpg->qual = qual;
210 tpg->qual_offset = qual_offset;
211 tpg->recalc_colors = true;
212}
213
214static inline enum tpg_quality tpg_g_quality(const struct tpg_data *tpg)
215{
216 return tpg->qual;
217}
218
219static inline void tpg_s_alpha_component(struct tpg_data *tpg,
220 u8 alpha_component)
221{
222 if (tpg->alpha_component == alpha_component)
223 return;
224 tpg->alpha_component = alpha_component;
225 tpg->recalc_colors = true;
226}
227
228static inline void tpg_s_alpha_mode(struct tpg_data *tpg,
229 bool red_only)
230{
231 if (tpg->alpha_red_only == red_only)
232 return;
233 tpg->alpha_red_only = red_only;
234 tpg->recalc_colors = true;
235}
236
237static inline void tpg_s_brightness(struct tpg_data *tpg,
238 u8 brightness)
239{
240 if (tpg->brightness == brightness)
241 return;
242 tpg->brightness = brightness;
243 tpg->recalc_colors = true;
244}
245
246static inline void tpg_s_contrast(struct tpg_data *tpg,
247 u8 contrast)
248{
249 if (tpg->contrast == contrast)
250 return;
251 tpg->contrast = contrast;
252 tpg->recalc_colors = true;
253}
254
255static inline void tpg_s_saturation(struct tpg_data *tpg,
256 u8 saturation)
257{
258 if (tpg->saturation == saturation)
259 return;
260 tpg->saturation = saturation;
261 tpg->recalc_colors = true;
262}
263
264static inline void tpg_s_hue(struct tpg_data *tpg,
265 s16 hue)
266{
267 if (tpg->hue == hue)
268 return;
269 tpg->hue = hue;
270 tpg->recalc_colors = true;
271}
272
273static inline void tpg_s_rgb_range(struct tpg_data *tpg,
274 unsigned rgb_range)
275{
276 if (tpg->rgb_range == rgb_range)
277 return;
278 tpg->rgb_range = rgb_range;
279 tpg->recalc_colors = true;
280}
281
282static inline void tpg_s_real_rgb_range(struct tpg_data *tpg,
283 unsigned rgb_range)
284{
285 if (tpg->real_rgb_range == rgb_range)
286 return;
287 tpg->real_rgb_range = rgb_range;
288 tpg->recalc_colors = true;
289}
290
291static inline void tpg_s_colorspace(struct tpg_data *tpg, u32 colorspace)
292{
293 if (tpg->colorspace == colorspace)
294 return;
295 tpg->colorspace = colorspace;
296 tpg->recalc_colors = true;
297}
298
299static inline u32 tpg_g_colorspace(const struct tpg_data *tpg)
300{
301 return tpg->colorspace;
302}
303
Hans Verkuil481b97a2014-11-17 10:14:32 -0300304static inline void tpg_s_ycbcr_enc(struct tpg_data *tpg, u32 ycbcr_enc)
305{
306 if (tpg->ycbcr_enc == ycbcr_enc)
307 return;
308 tpg->ycbcr_enc = ycbcr_enc;
309 tpg->recalc_colors = true;
310}
311
312static inline u32 tpg_g_ycbcr_enc(const struct tpg_data *tpg)
313{
314 return tpg->ycbcr_enc;
315}
316
317static inline void tpg_s_quantization(struct tpg_data *tpg, u32 quantization)
318{
319 if (tpg->quantization == quantization)
320 return;
321 tpg->quantization = quantization;
322 tpg->recalc_colors = true;
323}
324
325static inline u32 tpg_g_quantization(const struct tpg_data *tpg)
326{
327 return tpg->quantization;
328}
329
Hans Verkuil63881df2014-08-25 08:02:14 -0300330static inline unsigned tpg_g_planes(const struct tpg_data *tpg)
331{
332 return tpg->planes;
333}
334
335static inline unsigned tpg_g_twopixelsize(const struct tpg_data *tpg, unsigned plane)
336{
337 return tpg->twopixelsize[plane];
338}
339
340static inline unsigned tpg_g_bytesperline(const struct tpg_data *tpg, unsigned plane)
341{
342 return tpg->bytesperline[plane];
343}
344
345static inline void tpg_s_bytesperline(struct tpg_data *tpg, unsigned plane, unsigned bpl)
346{
347 tpg->bytesperline[plane] = bpl;
348}
349
350static inline void tpg_s_buf_height(struct tpg_data *tpg, unsigned h)
351{
352 tpg->buf_height = h;
353}
354
Hans Verkuil43047f62015-03-07 12:38:42 -0300355static inline void tpg_s_field(struct tpg_data *tpg, unsigned field, bool alternate)
Hans Verkuil63881df2014-08-25 08:02:14 -0300356{
357 tpg->field = field;
Hans Verkuil43047f62015-03-07 12:38:42 -0300358 tpg->field_alternate = alternate;
Hans Verkuil63881df2014-08-25 08:02:14 -0300359}
360
361static inline void tpg_s_perc_fill(struct tpg_data *tpg,
362 unsigned perc_fill)
363{
364 tpg->perc_fill = perc_fill;
365}
366
367static inline unsigned tpg_g_perc_fill(const struct tpg_data *tpg)
368{
369 return tpg->perc_fill;
370}
371
372static inline void tpg_s_perc_fill_blank(struct tpg_data *tpg,
373 bool perc_fill_blank)
374{
375 tpg->perc_fill_blank = perc_fill_blank;
376}
377
378static inline void tpg_s_video_aspect(struct tpg_data *tpg,
379 enum tpg_video_aspect vid_aspect)
380{
381 if (tpg->vid_aspect == vid_aspect)
382 return;
383 tpg->vid_aspect = vid_aspect;
384 tpg->recalc_square_border = true;
385}
386
387static inline enum tpg_video_aspect tpg_g_video_aspect(const struct tpg_data *tpg)
388{
389 return tpg->vid_aspect;
390}
391
392static inline void tpg_s_pixel_aspect(struct tpg_data *tpg,
393 enum tpg_pixel_aspect pix_aspect)
394{
395 if (tpg->pix_aspect == pix_aspect)
396 return;
397 tpg->pix_aspect = pix_aspect;
398 tpg->recalc_square_border = true;
399}
400
401static inline void tpg_s_show_border(struct tpg_data *tpg,
402 bool show_border)
403{
404 tpg->show_border = show_border;
405}
406
407static inline void tpg_s_show_square(struct tpg_data *tpg,
408 bool show_square)
409{
410 tpg->show_square = show_square;
411}
412
413static inline void tpg_s_insert_sav(struct tpg_data *tpg, bool insert_sav)
414{
415 tpg->insert_sav = insert_sav;
416}
417
418static inline void tpg_s_insert_eav(struct tpg_data *tpg, bool insert_eav)
419{
420 tpg->insert_eav = insert_eav;
421}
422
423void tpg_update_mv_step(struct tpg_data *tpg);
424
425static inline void tpg_s_mv_hor_mode(struct tpg_data *tpg,
426 enum tpg_move_mode mv_hor_mode)
427{
428 tpg->mv_hor_mode = mv_hor_mode;
429 tpg_update_mv_step(tpg);
430}
431
432static inline void tpg_s_mv_vert_mode(struct tpg_data *tpg,
433 enum tpg_move_mode mv_vert_mode)
434{
435 tpg->mv_vert_mode = mv_vert_mode;
436 tpg_update_mv_step(tpg);
437}
438
439static inline void tpg_init_mv_count(struct tpg_data *tpg)
440{
441 tpg->mv_hor_count = tpg->mv_vert_count = 0;
442}
443
444static inline void tpg_update_mv_count(struct tpg_data *tpg, bool frame_is_field)
445{
446 tpg->mv_hor_count += tpg->mv_hor_step * (frame_is_field ? 1 : 2);
447 tpg->mv_vert_count += tpg->mv_vert_step * (frame_is_field ? 1 : 2);
448}
449
450static inline void tpg_s_hflip(struct tpg_data *tpg, bool hflip)
451{
452 if (tpg->hflip == hflip)
453 return;
454 tpg->hflip = hflip;
455 tpg_update_mv_step(tpg);
456 tpg->recalc_lines = true;
457}
458
459static inline bool tpg_g_hflip(const struct tpg_data *tpg)
460{
461 return tpg->hflip;
462}
463
464static inline void tpg_s_vflip(struct tpg_data *tpg, bool vflip)
465{
466 tpg->vflip = vflip;
467}
468
469static inline bool tpg_g_vflip(const struct tpg_data *tpg)
470{
471 return tpg->vflip;
472}
473
474static inline bool tpg_pattern_is_static(const struct tpg_data *tpg)
475{
476 return tpg->pattern != TPG_PAT_NOISE &&
477 tpg->mv_hor_mode == TPG_MOVE_NONE &&
478 tpg->mv_vert_mode == TPG_MOVE_NONE;
479}
480
481#endif