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