9dc463a40ed3572752a7fc4cad1b2d735cad998a
[linux-2.6-block.git] / drivers / media / platform / vivid / vivid-tpg.h
1 /*
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>
28 #include <linux/vmalloc.h>
29 #include <linux/videodev2.h>
30
31 #include "vivid-tpg-colors.h"
32
33 enum 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
57 extern const char * const tpg_pattern_strings[];
58
59 enum tpg_quality {
60         TPG_QUAL_COLOR,
61         TPG_QUAL_GRAY,
62         TPG_QUAL_NOISE
63 };
64
65 enum 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
73 enum tpg_pixel_aspect {
74         TPG_PIXEL_ASPECT_SQUARE,
75         TPG_PIXEL_ASPECT_NTSC,
76         TPG_PIXEL_ASPECT_PAL,
77 };
78
79 enum 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
89 extern const char * const tpg_aspect_strings[];
90
91 #define TPG_MAX_PLANES 2
92 #define TPG_MAX_PAT_LINES 8
93
94 struct 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         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;
134         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
177 void tpg_init(struct tpg_data *tpg, unsigned w, unsigned h);
178 int tpg_alloc(struct tpg_data *tpg, unsigned max_w);
179 void tpg_free(struct tpg_data *tpg);
180 void tpg_reset_source(struct tpg_data *tpg, unsigned width, unsigned height,
181                        u32 field);
182
183 void tpg_set_font(const u8 *f);
184 void tpg_gen_text(struct tpg_data *tpg,
185                 u8 *basep[TPG_MAX_PLANES][2], int y, int x, char *text);
186 void tpg_calc_text_basep(struct tpg_data *tpg,
187                 u8 *basep[TPG_MAX_PLANES][2], unsigned p, u8 *vbuf);
188 void tpg_fillbuffer(struct tpg_data *tpg, v4l2_std_id std, unsigned p, u8 *vbuf);
189 bool tpg_s_fourcc(struct tpg_data *tpg, u32 fourcc);
190 void tpg_s_crop_compose(struct tpg_data *tpg, const struct v4l2_rect *crop,
191                 const struct v4l2_rect *compose);
192
193 static 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
201 static 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
211 static inline enum tpg_quality tpg_g_quality(const struct tpg_data *tpg)
212 {
213         return tpg->qual;
214 }
215
216 static 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
225 static 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
234 static 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
243 static 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
252 static 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
261 static 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
270 static 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
279 static 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
288 static 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
296 static inline u32 tpg_g_colorspace(const struct tpg_data *tpg)
297 {
298         return tpg->colorspace;
299 }
300
301 static 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
309 static inline u32 tpg_g_ycbcr_enc(const struct tpg_data *tpg)
310 {
311         return tpg->ycbcr_enc;
312 }
313
314 static 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
322 static inline u32 tpg_g_quantization(const struct tpg_data *tpg)
323 {
324         return tpg->quantization;
325 }
326
327 static inline unsigned tpg_g_planes(const struct tpg_data *tpg)
328 {
329         return tpg->planes;
330 }
331
332 static inline unsigned tpg_g_twopixelsize(const struct tpg_data *tpg, unsigned plane)
333 {
334         return tpg->twopixelsize[plane];
335 }
336
337 static inline unsigned tpg_g_bytesperline(const struct tpg_data *tpg, unsigned plane)
338 {
339         return tpg->bytesperline[plane];
340 }
341
342 static inline void tpg_s_bytesperline(struct tpg_data *tpg, unsigned plane, unsigned bpl)
343 {
344         tpg->bytesperline[plane] = bpl;
345 }
346
347 static inline void tpg_s_buf_height(struct tpg_data *tpg, unsigned h)
348 {
349         tpg->buf_height = h;
350 }
351
352 static inline void tpg_s_field(struct tpg_data *tpg, unsigned field)
353 {
354         tpg->field = field;
355 }
356
357 static inline void tpg_s_perc_fill(struct tpg_data *tpg,
358                                       unsigned perc_fill)
359 {
360         tpg->perc_fill = perc_fill;
361 }
362
363 static inline unsigned tpg_g_perc_fill(const struct tpg_data *tpg)
364 {
365         return tpg->perc_fill;
366 }
367
368 static inline void tpg_s_perc_fill_blank(struct tpg_data *tpg,
369                                          bool perc_fill_blank)
370 {
371         tpg->perc_fill_blank = perc_fill_blank;
372 }
373
374 static inline void tpg_s_video_aspect(struct tpg_data *tpg,
375                                         enum tpg_video_aspect vid_aspect)
376 {
377         if (tpg->vid_aspect == vid_aspect)
378                 return;
379         tpg->vid_aspect = vid_aspect;
380         tpg->recalc_square_border = true;
381 }
382
383 static inline enum tpg_video_aspect tpg_g_video_aspect(const struct tpg_data *tpg)
384 {
385         return tpg->vid_aspect;
386 }
387
388 static inline void tpg_s_pixel_aspect(struct tpg_data *tpg,
389                                         enum tpg_pixel_aspect pix_aspect)
390 {
391         if (tpg->pix_aspect == pix_aspect)
392                 return;
393         tpg->pix_aspect = pix_aspect;
394         tpg->recalc_square_border = true;
395 }
396
397 static inline void tpg_s_show_border(struct tpg_data *tpg,
398                                         bool show_border)
399 {
400         tpg->show_border = show_border;
401 }
402
403 static inline void tpg_s_show_square(struct tpg_data *tpg,
404                                         bool show_square)
405 {
406         tpg->show_square = show_square;
407 }
408
409 static inline void tpg_s_insert_sav(struct tpg_data *tpg, bool insert_sav)
410 {
411         tpg->insert_sav = insert_sav;
412 }
413
414 static inline void tpg_s_insert_eav(struct tpg_data *tpg, bool insert_eav)
415 {
416         tpg->insert_eav = insert_eav;
417 }
418
419 void tpg_update_mv_step(struct tpg_data *tpg);
420
421 static inline void tpg_s_mv_hor_mode(struct tpg_data *tpg,
422                                 enum tpg_move_mode mv_hor_mode)
423 {
424         tpg->mv_hor_mode = mv_hor_mode;
425         tpg_update_mv_step(tpg);
426 }
427
428 static inline void tpg_s_mv_vert_mode(struct tpg_data *tpg,
429                                 enum tpg_move_mode mv_vert_mode)
430 {
431         tpg->mv_vert_mode = mv_vert_mode;
432         tpg_update_mv_step(tpg);
433 }
434
435 static inline void tpg_init_mv_count(struct tpg_data *tpg)
436 {
437         tpg->mv_hor_count = tpg->mv_vert_count = 0;
438 }
439
440 static inline void tpg_update_mv_count(struct tpg_data *tpg, bool frame_is_field)
441 {
442         tpg->mv_hor_count += tpg->mv_hor_step * (frame_is_field ? 1 : 2);
443         tpg->mv_vert_count += tpg->mv_vert_step * (frame_is_field ? 1 : 2);
444 }
445
446 static inline void tpg_s_hflip(struct tpg_data *tpg, bool hflip)
447 {
448         if (tpg->hflip == hflip)
449                 return;
450         tpg->hflip = hflip;
451         tpg_update_mv_step(tpg);
452         tpg->recalc_lines = true;
453 }
454
455 static inline bool tpg_g_hflip(const struct tpg_data *tpg)
456 {
457         return tpg->hflip;
458 }
459
460 static inline void tpg_s_vflip(struct tpg_data *tpg, bool vflip)
461 {
462         tpg->vflip = vflip;
463 }
464
465 static inline bool tpg_g_vflip(const struct tpg_data *tpg)
466 {
467         return tpg->vflip;
468 }
469
470 static inline bool tpg_pattern_is_static(const struct tpg_data *tpg)
471 {
472         return tpg->pattern != TPG_PAT_NOISE &&
473                tpg->mv_hor_mode == TPG_MOVE_NONE &&
474                tpg->mv_vert_mode == TPG_MOVE_NONE;
475 }
476
477 #endif