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