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