[media] vivid-tpg: add hor/vert downsampling fields
[deliverable/linux.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/types.h>
24 #include <linux/errno.h>
25 #include <linux/random.h>
26 #include <linux/slab.h>
27 #include <linux/vmalloc.h>
28 #include <linux/videodev2.h>
29
30 #include "vivid-tpg-colors.h"
31
32 enum 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_2X2,
45 TPG_PAT_CHECKERS_1X1,
46 TPG_PAT_COLOR_CHECKERS_2X2,
47 TPG_PAT_COLOR_CHECKERS_1X1,
48 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
59 extern const char * const tpg_pattern_strings[];
60
61 enum tpg_quality {
62 TPG_QUAL_COLOR,
63 TPG_QUAL_GRAY,
64 TPG_QUAL_NOISE
65 };
66
67 enum 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
75 enum tpg_pixel_aspect {
76 TPG_PIXEL_ASPECT_SQUARE,
77 TPG_PIXEL_ASPECT_NTSC,
78 TPG_PIXEL_ASPECT_PAL,
79 };
80
81 enum 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
91 extern const char * const tpg_aspect_strings[];
92
93 #define TPG_MAX_PLANES 3
94 #define TPG_MAX_PAT_LINES 8
95
96 struct 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;
104 bool field_alternate;
105 /* 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;
125 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;
137 enum tpg_video_aspect vid_aspect;
138 enum tpg_pixel_aspect pix_aspect;
139 unsigned rgb_range;
140 unsigned real_rgb_range;
141 unsigned buffers;
142 unsigned planes;
143 u8 vdownsampling[TPG_MAX_PLANES];
144 u8 hdownsampling[TPG_MAX_PLANES];
145 /* 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];
178 u8 *random_line[TPG_MAX_PLANES];
179 u8 *contrast_line[TPG_MAX_PLANES];
180 u8 *black_line[TPG_MAX_PLANES];
181 };
182
183 void tpg_init(struct tpg_data *tpg, unsigned w, unsigned h);
184 int tpg_alloc(struct tpg_data *tpg, unsigned max_w);
185 void tpg_free(struct tpg_data *tpg);
186 void tpg_reset_source(struct tpg_data *tpg, unsigned width, unsigned height,
187 u32 field);
188
189 void tpg_set_font(const u8 *f);
190 void tpg_gen_text(struct tpg_data *tpg,
191 u8 *basep[TPG_MAX_PLANES][2], int y, int x, char *text);
192 void tpg_calc_text_basep(struct tpg_data *tpg,
193 u8 *basep[TPG_MAX_PLANES][2], unsigned p, u8 *vbuf);
194 void tpg_fill_plane_buffer(struct tpg_data *tpg, v4l2_std_id std, unsigned p, u8 *vbuf);
195 void tpg_fillbuffer(struct tpg_data *tpg, v4l2_std_id std, unsigned p, u8 *vbuf);
196 bool tpg_s_fourcc(struct tpg_data *tpg, u32 fourcc);
197 void tpg_s_crop_compose(struct tpg_data *tpg, const struct v4l2_rect *crop,
198 const struct v4l2_rect *compose);
199
200 static inline void tpg_s_pattern(struct tpg_data *tpg, enum tpg_pattern pattern)
201 {
202 if (tpg->pattern == pattern)
203 return;
204 tpg->pattern = pattern;
205 tpg->recalc_colors = true;
206 }
207
208 static inline void tpg_s_quality(struct tpg_data *tpg,
209 enum tpg_quality qual, unsigned qual_offset)
210 {
211 if (tpg->qual == qual && tpg->qual_offset == qual_offset)
212 return;
213 tpg->qual = qual;
214 tpg->qual_offset = qual_offset;
215 tpg->recalc_colors = true;
216 }
217
218 static inline enum tpg_quality tpg_g_quality(const struct tpg_data *tpg)
219 {
220 return tpg->qual;
221 }
222
223 static inline void tpg_s_alpha_component(struct tpg_data *tpg,
224 u8 alpha_component)
225 {
226 if (tpg->alpha_component == alpha_component)
227 return;
228 tpg->alpha_component = alpha_component;
229 tpg->recalc_colors = true;
230 }
231
232 static inline void tpg_s_alpha_mode(struct tpg_data *tpg,
233 bool red_only)
234 {
235 if (tpg->alpha_red_only == red_only)
236 return;
237 tpg->alpha_red_only = red_only;
238 tpg->recalc_colors = true;
239 }
240
241 static inline void tpg_s_brightness(struct tpg_data *tpg,
242 u8 brightness)
243 {
244 if (tpg->brightness == brightness)
245 return;
246 tpg->brightness = brightness;
247 tpg->recalc_colors = true;
248 }
249
250 static inline void tpg_s_contrast(struct tpg_data *tpg,
251 u8 contrast)
252 {
253 if (tpg->contrast == contrast)
254 return;
255 tpg->contrast = contrast;
256 tpg->recalc_colors = true;
257 }
258
259 static inline void tpg_s_saturation(struct tpg_data *tpg,
260 u8 saturation)
261 {
262 if (tpg->saturation == saturation)
263 return;
264 tpg->saturation = saturation;
265 tpg->recalc_colors = true;
266 }
267
268 static inline void tpg_s_hue(struct tpg_data *tpg,
269 s16 hue)
270 {
271 if (tpg->hue == hue)
272 return;
273 tpg->hue = hue;
274 tpg->recalc_colors = true;
275 }
276
277 static inline void tpg_s_rgb_range(struct tpg_data *tpg,
278 unsigned rgb_range)
279 {
280 if (tpg->rgb_range == rgb_range)
281 return;
282 tpg->rgb_range = rgb_range;
283 tpg->recalc_colors = true;
284 }
285
286 static inline void tpg_s_real_rgb_range(struct tpg_data *tpg,
287 unsigned rgb_range)
288 {
289 if (tpg->real_rgb_range == rgb_range)
290 return;
291 tpg->real_rgb_range = rgb_range;
292 tpg->recalc_colors = true;
293 }
294
295 static inline void tpg_s_colorspace(struct tpg_data *tpg, u32 colorspace)
296 {
297 if (tpg->colorspace == colorspace)
298 return;
299 tpg->colorspace = colorspace;
300 tpg->recalc_colors = true;
301 }
302
303 static inline u32 tpg_g_colorspace(const struct tpg_data *tpg)
304 {
305 return tpg->colorspace;
306 }
307
308 static inline void tpg_s_ycbcr_enc(struct tpg_data *tpg, u32 ycbcr_enc)
309 {
310 if (tpg->ycbcr_enc == ycbcr_enc)
311 return;
312 tpg->ycbcr_enc = ycbcr_enc;
313 tpg->recalc_colors = true;
314 }
315
316 static inline u32 tpg_g_ycbcr_enc(const struct tpg_data *tpg)
317 {
318 return tpg->ycbcr_enc;
319 }
320
321 static inline void tpg_s_quantization(struct tpg_data *tpg, u32 quantization)
322 {
323 if (tpg->quantization == quantization)
324 return;
325 tpg->quantization = quantization;
326 tpg->recalc_colors = true;
327 }
328
329 static inline u32 tpg_g_quantization(const struct tpg_data *tpg)
330 {
331 return tpg->quantization;
332 }
333
334 static inline unsigned tpg_g_buffers(const struct tpg_data *tpg)
335 {
336 return tpg->buffers;
337 }
338
339 static inline unsigned tpg_g_planes(const struct tpg_data *tpg)
340 {
341 return tpg->planes;
342 }
343
344 static inline unsigned tpg_g_twopixelsize(const struct tpg_data *tpg, unsigned plane)
345 {
346 return tpg->twopixelsize[plane];
347 }
348
349 static inline unsigned tpg_g_bytesperline(const struct tpg_data *tpg, unsigned plane)
350 {
351 return tpg->bytesperline[plane];
352 }
353
354 static inline void tpg_s_bytesperline(struct tpg_data *tpg, unsigned plane, unsigned bpl)
355 {
356 unsigned p;
357
358 if (tpg->buffers > 1) {
359 tpg->bytesperline[plane] = bpl;
360 return;
361 }
362
363 for (p = 0; p < tpg->planes; p++) {
364 unsigned plane_w = bpl * tpg->twopixelsize[p] / tpg->twopixelsize[0];
365
366 tpg->bytesperline[p] = plane_w / tpg->hdownsampling[p];
367 }
368 }
369
370
371 static inline unsigned tpg_g_line_width(const struct tpg_data *tpg, unsigned plane)
372 {
373 unsigned w = 0;
374 unsigned p;
375
376 if (tpg->buffers > 1)
377 return tpg_g_bytesperline(tpg, plane);
378 for (p = 0; p < tpg->planes; p++) {
379 unsigned plane_w = tpg_g_bytesperline(tpg, p);
380
381 w += plane_w / tpg->vdownsampling[p];
382 }
383 return w;
384 }
385
386 static inline unsigned tpg_calc_line_width(const struct tpg_data *tpg,
387 unsigned plane, unsigned bpl)
388 {
389 unsigned w = 0;
390 unsigned p;
391
392 if (tpg->buffers > 1)
393 return bpl;
394 for (p = 0; p < tpg->planes; p++) {
395 unsigned plane_w = bpl * tpg->twopixelsize[p] / tpg->twopixelsize[0];
396
397 plane_w /= tpg->hdownsampling[p];
398 w += plane_w / tpg->vdownsampling[p];
399 }
400 return w;
401 }
402
403 static inline unsigned tpg_calc_plane_size(const struct tpg_data *tpg, unsigned plane)
404 {
405 if (plane >= tpg->planes)
406 return 0;
407
408 return tpg_g_bytesperline(tpg, plane) * tpg->buf_height /
409 tpg->vdownsampling[plane];
410 }
411
412 static inline void tpg_s_buf_height(struct tpg_data *tpg, unsigned h)
413 {
414 tpg->buf_height = h;
415 }
416
417 static inline void tpg_s_field(struct tpg_data *tpg, unsigned field, bool alternate)
418 {
419 tpg->field = field;
420 tpg->field_alternate = alternate;
421 }
422
423 static inline void tpg_s_perc_fill(struct tpg_data *tpg,
424 unsigned perc_fill)
425 {
426 tpg->perc_fill = perc_fill;
427 }
428
429 static inline unsigned tpg_g_perc_fill(const struct tpg_data *tpg)
430 {
431 return tpg->perc_fill;
432 }
433
434 static inline void tpg_s_perc_fill_blank(struct tpg_data *tpg,
435 bool perc_fill_blank)
436 {
437 tpg->perc_fill_blank = perc_fill_blank;
438 }
439
440 static inline void tpg_s_video_aspect(struct tpg_data *tpg,
441 enum tpg_video_aspect vid_aspect)
442 {
443 if (tpg->vid_aspect == vid_aspect)
444 return;
445 tpg->vid_aspect = vid_aspect;
446 tpg->recalc_square_border = true;
447 }
448
449 static inline enum tpg_video_aspect tpg_g_video_aspect(const struct tpg_data *tpg)
450 {
451 return tpg->vid_aspect;
452 }
453
454 static inline void tpg_s_pixel_aspect(struct tpg_data *tpg,
455 enum tpg_pixel_aspect pix_aspect)
456 {
457 if (tpg->pix_aspect == pix_aspect)
458 return;
459 tpg->pix_aspect = pix_aspect;
460 tpg->recalc_square_border = true;
461 }
462
463 static inline void tpg_s_show_border(struct tpg_data *tpg,
464 bool show_border)
465 {
466 tpg->show_border = show_border;
467 }
468
469 static inline void tpg_s_show_square(struct tpg_data *tpg,
470 bool show_square)
471 {
472 tpg->show_square = show_square;
473 }
474
475 static inline void tpg_s_insert_sav(struct tpg_data *tpg, bool insert_sav)
476 {
477 tpg->insert_sav = insert_sav;
478 }
479
480 static inline void tpg_s_insert_eav(struct tpg_data *tpg, bool insert_eav)
481 {
482 tpg->insert_eav = insert_eav;
483 }
484
485 void tpg_update_mv_step(struct tpg_data *tpg);
486
487 static inline void tpg_s_mv_hor_mode(struct tpg_data *tpg,
488 enum tpg_move_mode mv_hor_mode)
489 {
490 tpg->mv_hor_mode = mv_hor_mode;
491 tpg_update_mv_step(tpg);
492 }
493
494 static inline void tpg_s_mv_vert_mode(struct tpg_data *tpg,
495 enum tpg_move_mode mv_vert_mode)
496 {
497 tpg->mv_vert_mode = mv_vert_mode;
498 tpg_update_mv_step(tpg);
499 }
500
501 static inline void tpg_init_mv_count(struct tpg_data *tpg)
502 {
503 tpg->mv_hor_count = tpg->mv_vert_count = 0;
504 }
505
506 static inline void tpg_update_mv_count(struct tpg_data *tpg, bool frame_is_field)
507 {
508 tpg->mv_hor_count += tpg->mv_hor_step * (frame_is_field ? 1 : 2);
509 tpg->mv_vert_count += tpg->mv_vert_step * (frame_is_field ? 1 : 2);
510 }
511
512 static inline void tpg_s_hflip(struct tpg_data *tpg, bool hflip)
513 {
514 if (tpg->hflip == hflip)
515 return;
516 tpg->hflip = hflip;
517 tpg_update_mv_step(tpg);
518 tpg->recalc_lines = true;
519 }
520
521 static inline bool tpg_g_hflip(const struct tpg_data *tpg)
522 {
523 return tpg->hflip;
524 }
525
526 static inline void tpg_s_vflip(struct tpg_data *tpg, bool vflip)
527 {
528 tpg->vflip = vflip;
529 }
530
531 static inline bool tpg_g_vflip(const struct tpg_data *tpg)
532 {
533 return tpg->vflip;
534 }
535
536 static inline bool tpg_pattern_is_static(const struct tpg_data *tpg)
537 {
538 return tpg->pattern != TPG_PAT_NOISE &&
539 tpg->mv_hor_mode == TPG_MOVE_NONE &&
540 tpg->mv_vert_mode == TPG_MOVE_NONE;
541 }
542
543 #endif
This page took 0.053674 seconds and 6 git commands to generate.