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