blob: b90ce7d4a384f09b255a8383d30bdae56768e2d6 [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;
Hans Verkuil06d1f0c2015-03-07 13:01:53 -0300141 unsigned buffers;
Hans Verkuil63881df2014-08-25 08:02:14 -0300142 unsigned planes;
143 /* Used to store the colors in native format, either RGB or YUV */
144 u8 colors[TPG_COLOR_MAX][3];
145 u8 textfg[TPG_MAX_PLANES][8], textbg[TPG_MAX_PLANES][8];
146 /* size in bytes for two pixels in each plane */
147 unsigned twopixelsize[TPG_MAX_PLANES];
148 unsigned bytesperline[TPG_MAX_PLANES];
149
150 /* Configuration */
151 enum tpg_pattern pattern;
152 bool hflip;
153 bool vflip;
154 unsigned perc_fill;
155 bool perc_fill_blank;
156 bool show_border;
157 bool show_square;
158 bool insert_sav;
159 bool insert_eav;
160
161 /* Test pattern movement */
162 enum tpg_move_mode mv_hor_mode;
163 int mv_hor_count;
164 int mv_hor_step;
165 enum tpg_move_mode mv_vert_mode;
166 int mv_vert_count;
167 int mv_vert_step;
168
169 bool recalc_colors;
170 bool recalc_lines;
171 bool recalc_square_border;
172
173 /* Used to store TPG_MAX_PAT_LINES lines, each with up to two planes */
174 unsigned max_line_width;
175 u8 *lines[TPG_MAX_PAT_LINES][TPG_MAX_PLANES];
176 u8 *random_line[TPG_MAX_PLANES];
177 u8 *contrast_line[TPG_MAX_PLANES];
178 u8 *black_line[TPG_MAX_PLANES];
179};
180
181void tpg_init(struct tpg_data *tpg, unsigned w, unsigned h);
182int tpg_alloc(struct tpg_data *tpg, unsigned max_w);
183void tpg_free(struct tpg_data *tpg);
184void tpg_reset_source(struct tpg_data *tpg, unsigned width, unsigned height,
185 u32 field);
186
187void tpg_set_font(const u8 *f);
188void tpg_gen_text(struct tpg_data *tpg,
189 u8 *basep[TPG_MAX_PLANES][2], int y, int x, char *text);
190void tpg_calc_text_basep(struct tpg_data *tpg,
191 u8 *basep[TPG_MAX_PLANES][2], unsigned p, u8 *vbuf);
Hans Verkuil4db22042015-03-07 13:39:01 -0300192void tpg_fill_plane_buffer(struct tpg_data *tpg, v4l2_std_id std, unsigned p, u8 *vbuf);
Hans Verkuil63881df2014-08-25 08:02:14 -0300193void tpg_fillbuffer(struct tpg_data *tpg, v4l2_std_id std, unsigned p, u8 *vbuf);
194bool tpg_s_fourcc(struct tpg_data *tpg, u32 fourcc);
195void tpg_s_crop_compose(struct tpg_data *tpg, const struct v4l2_rect *crop,
196 const struct v4l2_rect *compose);
197
198static inline void tpg_s_pattern(struct tpg_data *tpg, enum tpg_pattern pattern)
199{
200 if (tpg->pattern == pattern)
201 return;
202 tpg->pattern = pattern;
203 tpg->recalc_colors = true;
204}
205
206static inline void tpg_s_quality(struct tpg_data *tpg,
207 enum tpg_quality qual, unsigned qual_offset)
208{
209 if (tpg->qual == qual && tpg->qual_offset == qual_offset)
210 return;
211 tpg->qual = qual;
212 tpg->qual_offset = qual_offset;
213 tpg->recalc_colors = true;
214}
215
216static inline enum tpg_quality tpg_g_quality(const struct tpg_data *tpg)
217{
218 return tpg->qual;
219}
220
221static inline void tpg_s_alpha_component(struct tpg_data *tpg,
222 u8 alpha_component)
223{
224 if (tpg->alpha_component == alpha_component)
225 return;
226 tpg->alpha_component = alpha_component;
227 tpg->recalc_colors = true;
228}
229
230static inline void tpg_s_alpha_mode(struct tpg_data *tpg,
231 bool red_only)
232{
233 if (tpg->alpha_red_only == red_only)
234 return;
235 tpg->alpha_red_only = red_only;
236 tpg->recalc_colors = true;
237}
238
239static inline void tpg_s_brightness(struct tpg_data *tpg,
240 u8 brightness)
241{
242 if (tpg->brightness == brightness)
243 return;
244 tpg->brightness = brightness;
245 tpg->recalc_colors = true;
246}
247
248static inline void tpg_s_contrast(struct tpg_data *tpg,
249 u8 contrast)
250{
251 if (tpg->contrast == contrast)
252 return;
253 tpg->contrast = contrast;
254 tpg->recalc_colors = true;
255}
256
257static inline void tpg_s_saturation(struct tpg_data *tpg,
258 u8 saturation)
259{
260 if (tpg->saturation == saturation)
261 return;
262 tpg->saturation = saturation;
263 tpg->recalc_colors = true;
264}
265
266static inline void tpg_s_hue(struct tpg_data *tpg,
267 s16 hue)
268{
269 if (tpg->hue == hue)
270 return;
271 tpg->hue = hue;
272 tpg->recalc_colors = true;
273}
274
275static inline void tpg_s_rgb_range(struct tpg_data *tpg,
276 unsigned rgb_range)
277{
278 if (tpg->rgb_range == rgb_range)
279 return;
280 tpg->rgb_range = rgb_range;
281 tpg->recalc_colors = true;
282}
283
284static inline void tpg_s_real_rgb_range(struct tpg_data *tpg,
285 unsigned rgb_range)
286{
287 if (tpg->real_rgb_range == rgb_range)
288 return;
289 tpg->real_rgb_range = rgb_range;
290 tpg->recalc_colors = true;
291}
292
293static inline void tpg_s_colorspace(struct tpg_data *tpg, u32 colorspace)
294{
295 if (tpg->colorspace == colorspace)
296 return;
297 tpg->colorspace = colorspace;
298 tpg->recalc_colors = true;
299}
300
301static inline u32 tpg_g_colorspace(const struct tpg_data *tpg)
302{
303 return tpg->colorspace;
304}
305
Hans Verkuil481b97a2014-11-17 10:14:32 -0300306static inline void tpg_s_ycbcr_enc(struct tpg_data *tpg, u32 ycbcr_enc)
307{
308 if (tpg->ycbcr_enc == ycbcr_enc)
309 return;
310 tpg->ycbcr_enc = ycbcr_enc;
311 tpg->recalc_colors = true;
312}
313
314static inline u32 tpg_g_ycbcr_enc(const struct tpg_data *tpg)
315{
316 return tpg->ycbcr_enc;
317}
318
319static inline void tpg_s_quantization(struct tpg_data *tpg, u32 quantization)
320{
321 if (tpg->quantization == quantization)
322 return;
323 tpg->quantization = quantization;
324 tpg->recalc_colors = true;
325}
326
327static inline u32 tpg_g_quantization(const struct tpg_data *tpg)
328{
329 return tpg->quantization;
330}
331
Hans Verkuil06d1f0c2015-03-07 13:01:53 -0300332static inline unsigned tpg_g_buffers(const struct tpg_data *tpg)
333{
334 return tpg->buffers;
335}
336
Hans Verkuil63881df2014-08-25 08:02:14 -0300337static inline unsigned tpg_g_planes(const struct tpg_data *tpg)
338{
339 return tpg->planes;
340}
341
342static inline unsigned tpg_g_twopixelsize(const struct tpg_data *tpg, unsigned plane)
343{
344 return tpg->twopixelsize[plane];
345}
346
347static inline unsigned tpg_g_bytesperline(const struct tpg_data *tpg, unsigned plane)
348{
349 return tpg->bytesperline[plane];
350}
351
352static inline void tpg_s_bytesperline(struct tpg_data *tpg, unsigned plane, unsigned bpl)
353{
Hans Verkuil4db22042015-03-07 13:39:01 -0300354 unsigned p;
355
356 if (tpg->buffers > 1) {
357 tpg->bytesperline[plane] = bpl;
358 return;
359 }
360
361 for (p = 0; p < tpg->planes; p++) {
362 unsigned plane_w = bpl * tpg->twopixelsize[p] / tpg->twopixelsize[0];
363
364 tpg->bytesperline[p] = plane_w;
365 }
366}
367
368static inline unsigned tpg_g_line_width(const struct tpg_data *tpg, unsigned plane)
369{
370 unsigned w = 0;
371 unsigned p;
372
373 if (tpg->buffers > 1)
374 return tpg_g_bytesperline(tpg, plane);
375 for (p = 0; p < tpg->planes; p++) {
376 unsigned plane_w = tpg_g_bytesperline(tpg, p);
377
378 w += plane_w;
379 }
380 return w;
381}
382
383static inline unsigned tpg_calc_line_width(const struct tpg_data *tpg,
384 unsigned plane, unsigned bpl)
385{
386 unsigned w = 0;
387 unsigned p;
388
389 if (tpg->buffers > 1)
390 return bpl;
391 for (p = 0; p < tpg->planes; p++) {
392 unsigned plane_w = bpl * tpg->twopixelsize[p] / tpg->twopixelsize[0];
393
394 w += plane_w;
395 }
396 return w;
397}
398
399static inline unsigned tpg_calc_plane_size(const struct tpg_data *tpg, unsigned plane)
400{
401 if (plane >= tpg->planes)
402 return 0;
403
404 return tpg_g_bytesperline(tpg, plane) * tpg->buf_height;
Hans Verkuil63881df2014-08-25 08:02:14 -0300405}
406
407static inline void tpg_s_buf_height(struct tpg_data *tpg, unsigned h)
408{
409 tpg->buf_height = h;
410}
411
Hans Verkuil43047f62015-03-07 12:38:42 -0300412static inline void tpg_s_field(struct tpg_data *tpg, unsigned field, bool alternate)
Hans Verkuil63881df2014-08-25 08:02:14 -0300413{
414 tpg->field = field;
Hans Verkuil43047f62015-03-07 12:38:42 -0300415 tpg->field_alternate = alternate;
Hans Verkuil63881df2014-08-25 08:02:14 -0300416}
417
418static inline void tpg_s_perc_fill(struct tpg_data *tpg,
419 unsigned perc_fill)
420{
421 tpg->perc_fill = perc_fill;
422}
423
424static inline unsigned tpg_g_perc_fill(const struct tpg_data *tpg)
425{
426 return tpg->perc_fill;
427}
428
429static inline void tpg_s_perc_fill_blank(struct tpg_data *tpg,
430 bool perc_fill_blank)
431{
432 tpg->perc_fill_blank = perc_fill_blank;
433}
434
435static inline void tpg_s_video_aspect(struct tpg_data *tpg,
436 enum tpg_video_aspect vid_aspect)
437{
438 if (tpg->vid_aspect == vid_aspect)
439 return;
440 tpg->vid_aspect = vid_aspect;
441 tpg->recalc_square_border = true;
442}
443
444static inline enum tpg_video_aspect tpg_g_video_aspect(const struct tpg_data *tpg)
445{
446 return tpg->vid_aspect;
447}
448
449static inline void tpg_s_pixel_aspect(struct tpg_data *tpg,
450 enum tpg_pixel_aspect pix_aspect)
451{
452 if (tpg->pix_aspect == pix_aspect)
453 return;
454 tpg->pix_aspect = pix_aspect;
455 tpg->recalc_square_border = true;
456}
457
458static inline void tpg_s_show_border(struct tpg_data *tpg,
459 bool show_border)
460{
461 tpg->show_border = show_border;
462}
463
464static inline void tpg_s_show_square(struct tpg_data *tpg,
465 bool show_square)
466{
467 tpg->show_square = show_square;
468}
469
470static inline void tpg_s_insert_sav(struct tpg_data *tpg, bool insert_sav)
471{
472 tpg->insert_sav = insert_sav;
473}
474
475static inline void tpg_s_insert_eav(struct tpg_data *tpg, bool insert_eav)
476{
477 tpg->insert_eav = insert_eav;
478}
479
480void tpg_update_mv_step(struct tpg_data *tpg);
481
482static inline void tpg_s_mv_hor_mode(struct tpg_data *tpg,
483 enum tpg_move_mode mv_hor_mode)
484{
485 tpg->mv_hor_mode = mv_hor_mode;
486 tpg_update_mv_step(tpg);
487}
488
489static inline void tpg_s_mv_vert_mode(struct tpg_data *tpg,
490 enum tpg_move_mode mv_vert_mode)
491{
492 tpg->mv_vert_mode = mv_vert_mode;
493 tpg_update_mv_step(tpg);
494}
495
496static inline void tpg_init_mv_count(struct tpg_data *tpg)
497{
498 tpg->mv_hor_count = tpg->mv_vert_count = 0;
499}
500
501static inline void tpg_update_mv_count(struct tpg_data *tpg, bool frame_is_field)
502{
503 tpg->mv_hor_count += tpg->mv_hor_step * (frame_is_field ? 1 : 2);
504 tpg->mv_vert_count += tpg->mv_vert_step * (frame_is_field ? 1 : 2);
505}
506
507static inline void tpg_s_hflip(struct tpg_data *tpg, bool hflip)
508{
509 if (tpg->hflip == hflip)
510 return;
511 tpg->hflip = hflip;
512 tpg_update_mv_step(tpg);
513 tpg->recalc_lines = true;
514}
515
516static inline bool tpg_g_hflip(const struct tpg_data *tpg)
517{
518 return tpg->hflip;
519}
520
521static inline void tpg_s_vflip(struct tpg_data *tpg, bool vflip)
522{
523 tpg->vflip = vflip;
524}
525
526static inline bool tpg_g_vflip(const struct tpg_data *tpg)
527{
528 return tpg->vflip;
529}
530
531static inline bool tpg_pattern_is_static(const struct tpg_data *tpg)
532{
533 return tpg->pattern != TPG_PAT_NOISE &&
534 tpg->mv_hor_mode == TPG_MOVE_NONE &&
535 tpg->mv_vert_mode == TPG_MOVE_NONE;
536}
537
538#endif