1 // SPDX-License-Identifier: GPL-2.0-only
3 * v4l2-tpg-core.c - Test Pattern Generator
5 * Note: gen_twopix and tpg_gen_text are based on code from vivi.c. See the
6 * vivi.c source for the copyright information of those functions.
8 * Copyright 2014 Cisco Systems, Inc. and/or its affiliates. All rights reserved.
11 #include <linux/module.h>
12 #include <media/tpg/v4l2-tpg.h>
14 /* Must remain in sync with enum tpg_pattern */
15 const char * const tpg_pattern_strings[] = {
19 "Horizontal 100% Colorbar",
29 "2x2 Red/Green Checkers",
30 "1x1 Red/Green Checkers",
31 "Alternating Hor Lines",
32 "Alternating Vert Lines",
33 "One Pixel Wide Cross",
34 "Two Pixels Wide Cross",
35 "Ten Pixels Wide Cross",
40 EXPORT_SYMBOL_GPL(tpg_pattern_strings);
42 /* Must remain in sync with enum tpg_aspect */
43 const char * const tpg_aspect_strings[] = {
44 "Source Width x Height",
51 EXPORT_SYMBOL_GPL(tpg_aspect_strings);
54 * Sine table: sin[0] = 127 * sin(-180 degrees)
55 * sin[128] = 127 * sin(0 degrees)
56 * sin[256] = 127 * sin(180 degrees)
58 static const s8 sin[257] = {
59 0, -4, -7, -11, -13, -18, -20, -22, -26, -29, -33, -35, -37, -41, -43, -48,
60 -50, -52, -56, -58, -62, -63, -65, -69, -71, -75, -76, -78, -82, -83, -87, -88,
61 -90, -93, -94, -97, -99, -101, -103, -104, -107, -108, -110, -111, -112, -114, -115, -117,
62 -118, -119, -120, -121, -122, -123, -123, -124, -125, -125, -126, -126, -127, -127, -127, -127,
63 -127, -127, -127, -127, -126, -126, -125, -125, -124, -124, -123, -122, -121, -120, -119, -118,
64 -117, -116, -114, -113, -111, -110, -109, -107, -105, -103, -101, -100, -97, -96, -93, -91,
65 -90, -87, -85, -82, -80, -76, -75, -73, -69, -67, -63, -62, -60, -56, -54, -50,
66 -48, -46, -41, -39, -35, -33, -31, -26, -24, -20, -18, -15, -11, -9, -4, -2,
67 0, 2, 4, 9, 11, 15, 18, 20, 24, 26, 31, 33, 35, 39, 41, 46,
68 48, 50, 54, 56, 60, 62, 64, 67, 69, 73, 75, 76, 80, 82, 85, 87,
69 90, 91, 93, 96, 97, 100, 101, 103, 105, 107, 109, 110, 111, 113, 114, 116,
70 117, 118, 119, 120, 121, 122, 123, 124, 124, 125, 125, 126, 126, 127, 127, 127,
71 127, 127, 127, 127, 127, 126, 126, 125, 125, 124, 123, 123, 122, 121, 120, 119,
72 118, 117, 115, 114, 112, 111, 110, 108, 107, 104, 103, 101, 99, 97, 94, 93,
73 90, 88, 87, 83, 82, 78, 76, 75, 71, 69, 65, 64, 62, 58, 56, 52,
74 50, 48, 43, 41, 37, 35, 33, 29, 26, 22, 20, 18, 13, 11, 7, 4,
78 #define cos(idx) sin[((idx) + 64) % sizeof(sin)]
80 /* Global font descriptor */
81 static const u8 *font8x16;
83 void tpg_set_font(const u8 *f)
87 EXPORT_SYMBOL_GPL(tpg_set_font);
89 void tpg_init(struct tpg_data *tpg, unsigned w, unsigned h)
91 memset(tpg, 0, sizeof(*tpg));
92 tpg->scaled_width = tpg->src_width = w;
93 tpg->src_height = tpg->buf_height = h;
94 tpg->crop.width = tpg->compose.width = w;
95 tpg->crop.height = tpg->compose.height = h;
96 tpg->recalc_colors = true;
97 tpg->recalc_square_border = true;
98 tpg->brightness = 128;
100 tpg->saturation = 128;
102 tpg->mv_hor_mode = TPG_MOVE_NONE;
103 tpg->mv_vert_mode = TPG_MOVE_NONE;
104 tpg->field = V4L2_FIELD_NONE;
105 tpg_s_fourcc(tpg, V4L2_PIX_FMT_RGB24);
106 tpg->colorspace = V4L2_COLORSPACE_SRGB;
107 tpg->perc_fill = 100;
108 tpg->hsv_enc = V4L2_HSV_ENC_180;
110 EXPORT_SYMBOL_GPL(tpg_init);
112 int tpg_alloc(struct tpg_data *tpg, unsigned max_w)
117 tpg->max_line_width = max_w;
118 for (pat = 0; pat < TPG_MAX_PAT_LINES; pat++) {
119 for (plane = 0; plane < TPG_MAX_PLANES; plane++) {
120 unsigned pixelsz = plane ? 2 : 4;
122 tpg->lines[pat][plane] =
123 vzalloc(array3_size(max_w, 2, pixelsz));
124 if (!tpg->lines[pat][plane])
128 tpg->downsampled_lines[pat][plane] =
129 vzalloc(array3_size(max_w, 2, pixelsz));
130 if (!tpg->downsampled_lines[pat][plane])
134 for (plane = 0; plane < TPG_MAX_PLANES; plane++) {
135 unsigned pixelsz = plane ? 2 : 4;
137 tpg->contrast_line[plane] =
138 vzalloc(array_size(pixelsz, max_w));
139 if (!tpg->contrast_line[plane])
141 tpg->black_line[plane] =
142 vzalloc(array_size(pixelsz, max_w));
143 if (!tpg->black_line[plane])
145 tpg->random_line[plane] =
146 vzalloc(array3_size(max_w, 2, pixelsz));
147 if (!tpg->random_line[plane])
152 EXPORT_SYMBOL_GPL(tpg_alloc);
154 void tpg_free(struct tpg_data *tpg)
159 for (pat = 0; pat < TPG_MAX_PAT_LINES; pat++)
160 for (plane = 0; plane < TPG_MAX_PLANES; plane++) {
161 vfree(tpg->lines[pat][plane]);
162 tpg->lines[pat][plane] = NULL;
165 vfree(tpg->downsampled_lines[pat][plane]);
166 tpg->downsampled_lines[pat][plane] = NULL;
168 for (plane = 0; plane < TPG_MAX_PLANES; plane++) {
169 vfree(tpg->contrast_line[plane]);
170 vfree(tpg->black_line[plane]);
171 vfree(tpg->random_line[plane]);
172 tpg->contrast_line[plane] = NULL;
173 tpg->black_line[plane] = NULL;
174 tpg->random_line[plane] = NULL;
177 EXPORT_SYMBOL_GPL(tpg_free);
179 bool tpg_s_fourcc(struct tpg_data *tpg, u32 fourcc)
181 tpg->fourcc = fourcc;
184 tpg->recalc_colors = true;
185 tpg->interleaved = false;
186 tpg->vdownsampling[0] = 1;
187 tpg->hdownsampling[0] = 1;
193 case V4L2_PIX_FMT_SBGGR8:
194 case V4L2_PIX_FMT_SGBRG8:
195 case V4L2_PIX_FMT_SGRBG8:
196 case V4L2_PIX_FMT_SRGGB8:
197 case V4L2_PIX_FMT_SBGGR10:
198 case V4L2_PIX_FMT_SGBRG10:
199 case V4L2_PIX_FMT_SGRBG10:
200 case V4L2_PIX_FMT_SRGGB10:
201 case V4L2_PIX_FMT_SBGGR12:
202 case V4L2_PIX_FMT_SGBRG12:
203 case V4L2_PIX_FMT_SGRBG12:
204 case V4L2_PIX_FMT_SRGGB12:
205 case V4L2_PIX_FMT_SBGGR16:
206 case V4L2_PIX_FMT_SGBRG16:
207 case V4L2_PIX_FMT_SGRBG16:
208 case V4L2_PIX_FMT_SRGGB16:
209 tpg->interleaved = true;
210 tpg->vdownsampling[1] = 1;
211 tpg->hdownsampling[1] = 1;
214 case V4L2_PIX_FMT_RGB332:
215 case V4L2_PIX_FMT_RGB565:
216 case V4L2_PIX_FMT_RGB565X:
217 case V4L2_PIX_FMT_RGB444:
218 case V4L2_PIX_FMT_XRGB444:
219 case V4L2_PIX_FMT_ARGB444:
220 case V4L2_PIX_FMT_RGBX444:
221 case V4L2_PIX_FMT_RGBA444:
222 case V4L2_PIX_FMT_XBGR444:
223 case V4L2_PIX_FMT_ABGR444:
224 case V4L2_PIX_FMT_BGRX444:
225 case V4L2_PIX_FMT_BGRA444:
226 case V4L2_PIX_FMT_RGB555:
227 case V4L2_PIX_FMT_XRGB555:
228 case V4L2_PIX_FMT_ARGB555:
229 case V4L2_PIX_FMT_RGBX555:
230 case V4L2_PIX_FMT_RGBA555:
231 case V4L2_PIX_FMT_XBGR555:
232 case V4L2_PIX_FMT_ABGR555:
233 case V4L2_PIX_FMT_BGRX555:
234 case V4L2_PIX_FMT_BGRA555:
235 case V4L2_PIX_FMT_RGB555X:
236 case V4L2_PIX_FMT_XRGB555X:
237 case V4L2_PIX_FMT_ARGB555X:
238 case V4L2_PIX_FMT_BGR666:
239 case V4L2_PIX_FMT_RGB24:
240 case V4L2_PIX_FMT_BGR24:
241 case V4L2_PIX_FMT_RGB32:
242 case V4L2_PIX_FMT_BGR32:
243 case V4L2_PIX_FMT_XRGB32:
244 case V4L2_PIX_FMT_XBGR32:
245 case V4L2_PIX_FMT_ARGB32:
246 case V4L2_PIX_FMT_ABGR32:
247 case V4L2_PIX_FMT_RGBX32:
248 case V4L2_PIX_FMT_BGRX32:
249 case V4L2_PIX_FMT_RGBA32:
250 case V4L2_PIX_FMT_BGRA32:
251 tpg->color_enc = TGP_COLOR_ENC_RGB;
253 case V4L2_PIX_FMT_GREY:
254 case V4L2_PIX_FMT_Y10:
255 case V4L2_PIX_FMT_Y12:
256 case V4L2_PIX_FMT_Y16:
257 case V4L2_PIX_FMT_Y16_BE:
258 case V4L2_PIX_FMT_Z16:
259 tpg->color_enc = TGP_COLOR_ENC_LUMA;
261 case V4L2_PIX_FMT_YUV444:
262 case V4L2_PIX_FMT_YUV555:
263 case V4L2_PIX_FMT_YUV565:
264 case V4L2_PIX_FMT_YUV32:
265 case V4L2_PIX_FMT_AYUV32:
266 case V4L2_PIX_FMT_XYUV32:
267 case V4L2_PIX_FMT_VUYA32:
268 case V4L2_PIX_FMT_VUYX32:
269 case V4L2_PIX_FMT_YUVA32:
270 case V4L2_PIX_FMT_YUVX32:
271 tpg->color_enc = TGP_COLOR_ENC_YCBCR;
273 case V4L2_PIX_FMT_YUV420M:
274 case V4L2_PIX_FMT_YVU420M:
277 case V4L2_PIX_FMT_YUV420:
278 case V4L2_PIX_FMT_YVU420:
279 tpg->vdownsampling[1] = 2;
280 tpg->vdownsampling[2] = 2;
281 tpg->hdownsampling[1] = 2;
282 tpg->hdownsampling[2] = 2;
284 tpg->color_enc = TGP_COLOR_ENC_YCBCR;
286 case V4L2_PIX_FMT_YUV422M:
287 case V4L2_PIX_FMT_YVU422M:
290 case V4L2_PIX_FMT_YUV422P:
291 tpg->vdownsampling[1] = 1;
292 tpg->vdownsampling[2] = 1;
293 tpg->hdownsampling[1] = 2;
294 tpg->hdownsampling[2] = 2;
296 tpg->color_enc = TGP_COLOR_ENC_YCBCR;
298 case V4L2_PIX_FMT_NV16M:
299 case V4L2_PIX_FMT_NV61M:
302 case V4L2_PIX_FMT_NV16:
303 case V4L2_PIX_FMT_NV61:
304 tpg->vdownsampling[1] = 1;
305 tpg->hdownsampling[1] = 1;
308 tpg->color_enc = TGP_COLOR_ENC_YCBCR;
310 case V4L2_PIX_FMT_NV12M:
311 case V4L2_PIX_FMT_NV21M:
314 case V4L2_PIX_FMT_NV12:
315 case V4L2_PIX_FMT_NV21:
316 tpg->vdownsampling[1] = 2;
317 tpg->hdownsampling[1] = 1;
320 tpg->color_enc = TGP_COLOR_ENC_YCBCR;
322 case V4L2_PIX_FMT_YUV444M:
323 case V4L2_PIX_FMT_YVU444M:
326 tpg->vdownsampling[1] = 1;
327 tpg->vdownsampling[2] = 1;
328 tpg->hdownsampling[1] = 1;
329 tpg->hdownsampling[2] = 1;
330 tpg->color_enc = TGP_COLOR_ENC_YCBCR;
332 case V4L2_PIX_FMT_NV24:
333 case V4L2_PIX_FMT_NV42:
334 tpg->vdownsampling[1] = 1;
335 tpg->hdownsampling[1] = 1;
337 tpg->color_enc = TGP_COLOR_ENC_YCBCR;
339 case V4L2_PIX_FMT_YUYV:
340 case V4L2_PIX_FMT_UYVY:
341 case V4L2_PIX_FMT_YVYU:
342 case V4L2_PIX_FMT_VYUY:
344 tpg->color_enc = TGP_COLOR_ENC_YCBCR;
346 case V4L2_PIX_FMT_HSV24:
347 case V4L2_PIX_FMT_HSV32:
348 tpg->color_enc = TGP_COLOR_ENC_HSV;
355 case V4L2_PIX_FMT_GREY:
356 case V4L2_PIX_FMT_RGB332:
357 tpg->twopixelsize[0] = 2;
359 case V4L2_PIX_FMT_RGB565:
360 case V4L2_PIX_FMT_RGB565X:
361 case V4L2_PIX_FMT_RGB444:
362 case V4L2_PIX_FMT_XRGB444:
363 case V4L2_PIX_FMT_ARGB444:
364 case V4L2_PIX_FMT_RGBX444:
365 case V4L2_PIX_FMT_RGBA444:
366 case V4L2_PIX_FMT_XBGR444:
367 case V4L2_PIX_FMT_ABGR444:
368 case V4L2_PIX_FMT_BGRX444:
369 case V4L2_PIX_FMT_BGRA444:
370 case V4L2_PIX_FMT_RGB555:
371 case V4L2_PIX_FMT_XRGB555:
372 case V4L2_PIX_FMT_ARGB555:
373 case V4L2_PIX_FMT_RGBX555:
374 case V4L2_PIX_FMT_RGBA555:
375 case V4L2_PIX_FMT_XBGR555:
376 case V4L2_PIX_FMT_ABGR555:
377 case V4L2_PIX_FMT_BGRX555:
378 case V4L2_PIX_FMT_BGRA555:
379 case V4L2_PIX_FMT_RGB555X:
380 case V4L2_PIX_FMT_XRGB555X:
381 case V4L2_PIX_FMT_ARGB555X:
382 case V4L2_PIX_FMT_YUYV:
383 case V4L2_PIX_FMT_UYVY:
384 case V4L2_PIX_FMT_YVYU:
385 case V4L2_PIX_FMT_VYUY:
386 case V4L2_PIX_FMT_YUV444:
387 case V4L2_PIX_FMT_YUV555:
388 case V4L2_PIX_FMT_YUV565:
389 case V4L2_PIX_FMT_Y10:
390 case V4L2_PIX_FMT_Y12:
391 case V4L2_PIX_FMT_Y16:
392 case V4L2_PIX_FMT_Y16_BE:
393 case V4L2_PIX_FMT_Z16:
394 tpg->twopixelsize[0] = 2 * 2;
396 case V4L2_PIX_FMT_RGB24:
397 case V4L2_PIX_FMT_BGR24:
398 case V4L2_PIX_FMT_HSV24:
399 tpg->twopixelsize[0] = 2 * 3;
401 case V4L2_PIX_FMT_BGR666:
402 case V4L2_PIX_FMT_RGB32:
403 case V4L2_PIX_FMT_BGR32:
404 case V4L2_PIX_FMT_XRGB32:
405 case V4L2_PIX_FMT_XBGR32:
406 case V4L2_PIX_FMT_ARGB32:
407 case V4L2_PIX_FMT_ABGR32:
408 case V4L2_PIX_FMT_RGBX32:
409 case V4L2_PIX_FMT_BGRX32:
410 case V4L2_PIX_FMT_RGBA32:
411 case V4L2_PIX_FMT_BGRA32:
412 case V4L2_PIX_FMT_YUV32:
413 case V4L2_PIX_FMT_AYUV32:
414 case V4L2_PIX_FMT_XYUV32:
415 case V4L2_PIX_FMT_VUYA32:
416 case V4L2_PIX_FMT_VUYX32:
417 case V4L2_PIX_FMT_YUVA32:
418 case V4L2_PIX_FMT_YUVX32:
419 case V4L2_PIX_FMT_HSV32:
420 tpg->twopixelsize[0] = 2 * 4;
422 case V4L2_PIX_FMT_NV12:
423 case V4L2_PIX_FMT_NV21:
424 case V4L2_PIX_FMT_NV12M:
425 case V4L2_PIX_FMT_NV21M:
426 case V4L2_PIX_FMT_NV16:
427 case V4L2_PIX_FMT_NV61:
428 case V4L2_PIX_FMT_NV16M:
429 case V4L2_PIX_FMT_NV61M:
430 case V4L2_PIX_FMT_SBGGR8:
431 case V4L2_PIX_FMT_SGBRG8:
432 case V4L2_PIX_FMT_SGRBG8:
433 case V4L2_PIX_FMT_SRGGB8:
434 tpg->twopixelsize[0] = 2;
435 tpg->twopixelsize[1] = 2;
437 case V4L2_PIX_FMT_SRGGB10:
438 case V4L2_PIX_FMT_SGRBG10:
439 case V4L2_PIX_FMT_SGBRG10:
440 case V4L2_PIX_FMT_SBGGR10:
441 case V4L2_PIX_FMT_SRGGB12:
442 case V4L2_PIX_FMT_SGRBG12:
443 case V4L2_PIX_FMT_SGBRG12:
444 case V4L2_PIX_FMT_SBGGR12:
445 case V4L2_PIX_FMT_SRGGB16:
446 case V4L2_PIX_FMT_SGRBG16:
447 case V4L2_PIX_FMT_SGBRG16:
448 case V4L2_PIX_FMT_SBGGR16:
449 tpg->twopixelsize[0] = 4;
450 tpg->twopixelsize[1] = 4;
452 case V4L2_PIX_FMT_YUV444M:
453 case V4L2_PIX_FMT_YVU444M:
454 case V4L2_PIX_FMT_YUV422M:
455 case V4L2_PIX_FMT_YVU422M:
456 case V4L2_PIX_FMT_YUV422P:
457 case V4L2_PIX_FMT_YUV420:
458 case V4L2_PIX_FMT_YVU420:
459 case V4L2_PIX_FMT_YUV420M:
460 case V4L2_PIX_FMT_YVU420M:
461 tpg->twopixelsize[0] = 2;
462 tpg->twopixelsize[1] = 2;
463 tpg->twopixelsize[2] = 2;
465 case V4L2_PIX_FMT_NV24:
466 case V4L2_PIX_FMT_NV42:
467 tpg->twopixelsize[0] = 2;
468 tpg->twopixelsize[1] = 4;
473 EXPORT_SYMBOL_GPL(tpg_s_fourcc);
475 void tpg_s_crop_compose(struct tpg_data *tpg, const struct v4l2_rect *crop,
476 const struct v4l2_rect *compose)
479 tpg->compose = *compose;
480 tpg->scaled_width = (tpg->src_width * tpg->compose.width +
481 tpg->crop.width - 1) / tpg->crop.width;
482 tpg->scaled_width &= ~1;
483 if (tpg->scaled_width > tpg->max_line_width)
484 tpg->scaled_width = tpg->max_line_width;
485 if (tpg->scaled_width < 2)
486 tpg->scaled_width = 2;
487 tpg->recalc_lines = true;
489 EXPORT_SYMBOL_GPL(tpg_s_crop_compose);
491 void tpg_reset_source(struct tpg_data *tpg, unsigned width, unsigned height,
496 tpg->src_width = width;
497 tpg->src_height = height;
499 tpg->buf_height = height;
500 if (V4L2_FIELD_HAS_T_OR_B(field))
501 tpg->buf_height /= 2;
502 tpg->scaled_width = width;
503 tpg->crop.top = tpg->crop.left = 0;
504 tpg->crop.width = width;
505 tpg->crop.height = height;
506 tpg->compose.top = tpg->compose.left = 0;
507 tpg->compose.width = width;
508 tpg->compose.height = tpg->buf_height;
509 for (p = 0; p < tpg->planes; p++)
510 tpg->bytesperline[p] = (width * tpg->twopixelsize[p]) /
511 (2 * tpg->hdownsampling[p]);
512 tpg->recalc_square_border = true;
514 EXPORT_SYMBOL_GPL(tpg_reset_source);
516 static enum tpg_color tpg_get_textbg_color(struct tpg_data *tpg)
518 switch (tpg->pattern) {
520 return TPG_COLOR_100_WHITE;
521 case TPG_PAT_CSC_COLORBAR:
522 return TPG_COLOR_CSC_BLACK;
524 return TPG_COLOR_100_BLACK;
528 static enum tpg_color tpg_get_textfg_color(struct tpg_data *tpg)
530 switch (tpg->pattern) {
531 case TPG_PAT_75_COLORBAR:
532 case TPG_PAT_CSC_COLORBAR:
533 return TPG_COLOR_CSC_WHITE;
535 return TPG_COLOR_100_BLACK;
537 return TPG_COLOR_100_WHITE;
541 static inline int rec709_to_linear(int v)
543 v = clamp(v, 0, 0xff0);
544 return tpg_rec709_to_linear[v];
547 static inline int linear_to_rec709(int v)
549 v = clamp(v, 0, 0xff0);
550 return tpg_linear_to_rec709[v];
553 static void color_to_hsv(struct tpg_data *tpg, int r, int g, int b,
554 int *h, int *s, int *v)
556 int max_rgb, min_rgb, diff_rgb;
566 max_rgb = max3(r, g, b);
575 min_rgb = min3(r, g, b);
576 diff_rgb = max_rgb - min_rgb;
577 aux = 255 * diff_rgb;
586 third_size = (tpg->real_hsv_enc == V4L2_HSV_ENC_180) ? 60 : 85;
592 } else if (max_rgb == g) {
597 third = third_size * 2;
600 aux *= third_size / 2;
606 if (tpg->real_hsv_enc == V4L2_HSV_ENC_180) {
618 static void rgb2ycbcr(const int m[3][3], int r, int g, int b,
619 int y_offset, int *y, int *cb, int *cr)
621 *y = ((m[0][0] * r + m[0][1] * g + m[0][2] * b) >> 16) + (y_offset << 4);
622 *cb = ((m[1][0] * r + m[1][1] * g + m[1][2] * b) >> 16) + (128 << 4);
623 *cr = ((m[2][0] * r + m[2][1] * g + m[2][2] * b) >> 16) + (128 << 4);
626 static void color_to_ycbcr(struct tpg_data *tpg, int r, int g, int b,
627 int *y, int *cb, int *cr)
629 #define COEFF(v, r) ((int)(0.5 + (v) * (r) * 256.0))
631 static const int bt601[3][3] = {
632 { COEFF(0.299, 219), COEFF(0.587, 219), COEFF(0.114, 219) },
633 { COEFF(-0.1687, 224), COEFF(-0.3313, 224), COEFF(0.5, 224) },
634 { COEFF(0.5, 224), COEFF(-0.4187, 224), COEFF(-0.0813, 224) },
636 static const int bt601_full[3][3] = {
637 { COEFF(0.299, 255), COEFF(0.587, 255), COEFF(0.114, 255) },
638 { COEFF(-0.1687, 255), COEFF(-0.3313, 255), COEFF(0.5, 255) },
639 { COEFF(0.5, 255), COEFF(-0.4187, 255), COEFF(-0.0813, 255) },
641 static const int rec709[3][3] = {
642 { COEFF(0.2126, 219), COEFF(0.7152, 219), COEFF(0.0722, 219) },
643 { COEFF(-0.1146, 224), COEFF(-0.3854, 224), COEFF(0.5, 224) },
644 { COEFF(0.5, 224), COEFF(-0.4542, 224), COEFF(-0.0458, 224) },
646 static const int rec709_full[3][3] = {
647 { COEFF(0.2126, 255), COEFF(0.7152, 255), COEFF(0.0722, 255) },
648 { COEFF(-0.1146, 255), COEFF(-0.3854, 255), COEFF(0.5, 255) },
649 { COEFF(0.5, 255), COEFF(-0.4542, 255), COEFF(-0.0458, 255) },
651 static const int smpte240m[3][3] = {
652 { COEFF(0.212, 219), COEFF(0.701, 219), COEFF(0.087, 219) },
653 { COEFF(-0.116, 224), COEFF(-0.384, 224), COEFF(0.5, 224) },
654 { COEFF(0.5, 224), COEFF(-0.445, 224), COEFF(-0.055, 224) },
656 static const int smpte240m_full[3][3] = {
657 { COEFF(0.212, 255), COEFF(0.701, 255), COEFF(0.087, 255) },
658 { COEFF(-0.116, 255), COEFF(-0.384, 255), COEFF(0.5, 255) },
659 { COEFF(0.5, 255), COEFF(-0.445, 255), COEFF(-0.055, 255) },
661 static const int bt2020[3][3] = {
662 { COEFF(0.2627, 219), COEFF(0.6780, 219), COEFF(0.0593, 219) },
663 { COEFF(-0.1396, 224), COEFF(-0.3604, 224), COEFF(0.5, 224) },
664 { COEFF(0.5, 224), COEFF(-0.4598, 224), COEFF(-0.0402, 224) },
666 static const int bt2020_full[3][3] = {
667 { COEFF(0.2627, 255), COEFF(0.6780, 255), COEFF(0.0593, 255) },
668 { COEFF(-0.1396, 255), COEFF(-0.3604, 255), COEFF(0.5, 255) },
669 { COEFF(0.5, 255), COEFF(-0.4598, 255), COEFF(-0.0402, 255) },
671 static const int bt2020c[4] = {
672 COEFF(1.0 / 1.9404, 224), COEFF(1.0 / 1.5816, 224),
673 COEFF(1.0 / 1.7184, 224), COEFF(1.0 / 0.9936, 224),
675 static const int bt2020c_full[4] = {
676 COEFF(1.0 / 1.9404, 255), COEFF(1.0 / 1.5816, 255),
677 COEFF(1.0 / 1.7184, 255), COEFF(1.0 / 0.9936, 255),
680 bool full = tpg->real_quantization == V4L2_QUANTIZATION_FULL_RANGE;
681 unsigned y_offset = full ? 0 : 16;
684 switch (tpg->real_ycbcr_enc) {
685 case V4L2_YCBCR_ENC_601:
686 rgb2ycbcr(full ? bt601_full : bt601, r, g, b, y_offset, y, cb, cr);
688 case V4L2_YCBCR_ENC_XV601:
689 /* Ignore quantization range, there is only one possible
690 * Y'CbCr encoding. */
691 rgb2ycbcr(bt601, r, g, b, 16, y, cb, cr);
693 case V4L2_YCBCR_ENC_XV709:
694 /* Ignore quantization range, there is only one possible
695 * Y'CbCr encoding. */
696 rgb2ycbcr(rec709, r, g, b, 16, y, cb, cr);
698 case V4L2_YCBCR_ENC_BT2020:
699 rgb2ycbcr(full ? bt2020_full : bt2020, r, g, b, y_offset, y, cb, cr);
701 case V4L2_YCBCR_ENC_BT2020_CONST_LUM:
702 lin_y = (COEFF(0.2627, 255) * rec709_to_linear(r) +
703 COEFF(0.6780, 255) * rec709_to_linear(g) +
704 COEFF(0.0593, 255) * rec709_to_linear(b)) >> 16;
705 yc = linear_to_rec709(lin_y);
706 *y = full ? yc : (yc * 219) / 255 + (16 << 4);
708 *cb = (((b - yc) * (full ? bt2020c_full[0] : bt2020c[0])) >> 16) + (128 << 4);
710 *cb = (((b - yc) * (full ? bt2020c_full[1] : bt2020c[1])) >> 16) + (128 << 4);
712 *cr = (((r - yc) * (full ? bt2020c_full[2] : bt2020c[2])) >> 16) + (128 << 4);
714 *cr = (((r - yc) * (full ? bt2020c_full[3] : bt2020c[3])) >> 16) + (128 << 4);
716 case V4L2_YCBCR_ENC_SMPTE240M:
717 rgb2ycbcr(full ? smpte240m_full : smpte240m, r, g, b, y_offset, y, cb, cr);
719 case V4L2_YCBCR_ENC_709:
721 rgb2ycbcr(full ? rec709_full : rec709, r, g, b, y_offset, y, cb, cr);
726 static void ycbcr2rgb(const int m[3][3], int y, int cb, int cr,
727 int y_offset, int *r, int *g, int *b)
732 *r = m[0][0] * y + m[0][1] * cb + m[0][2] * cr;
733 *g = m[1][0] * y + m[1][1] * cb + m[1][2] * cr;
734 *b = m[2][0] * y + m[2][1] * cb + m[2][2] * cr;
735 *r = clamp(*r >> 12, 0, 0xff0);
736 *g = clamp(*g >> 12, 0, 0xff0);
737 *b = clamp(*b >> 12, 0, 0xff0);
740 static void ycbcr_to_color(struct tpg_data *tpg, int y, int cb, int cr,
741 int *r, int *g, int *b)
744 #define COEFF(v, r) ((int)(0.5 + (v) * ((255.0 * 255.0 * 16.0) / (r))))
745 static const int bt601[3][3] = {
746 { COEFF(1, 219), COEFF(0, 224), COEFF(1.4020, 224) },
747 { COEFF(1, 219), COEFF(-0.3441, 224), COEFF(-0.7141, 224) },
748 { COEFF(1, 219), COEFF(1.7720, 224), COEFF(0, 224) },
750 static const int bt601_full[3][3] = {
751 { COEFF(1, 255), COEFF(0, 255), COEFF(1.4020, 255) },
752 { COEFF(1, 255), COEFF(-0.3441, 255), COEFF(-0.7141, 255) },
753 { COEFF(1, 255), COEFF(1.7720, 255), COEFF(0, 255) },
755 static const int rec709[3][3] = {
756 { COEFF(1, 219), COEFF(0, 224), COEFF(1.5748, 224) },
757 { COEFF(1, 219), COEFF(-0.1873, 224), COEFF(-0.4681, 224) },
758 { COEFF(1, 219), COEFF(1.8556, 224), COEFF(0, 224) },
760 static const int rec709_full[3][3] = {
761 { COEFF(1, 255), COEFF(0, 255), COEFF(1.5748, 255) },
762 { COEFF(1, 255), COEFF(-0.1873, 255), COEFF(-0.4681, 255) },
763 { COEFF(1, 255), COEFF(1.8556, 255), COEFF(0, 255) },
765 static const int smpte240m[3][3] = {
766 { COEFF(1, 219), COEFF(0, 224), COEFF(1.5756, 224) },
767 { COEFF(1, 219), COEFF(-0.2253, 224), COEFF(-0.4767, 224) },
768 { COEFF(1, 219), COEFF(1.8270, 224), COEFF(0, 224) },
770 static const int smpte240m_full[3][3] = {
771 { COEFF(1, 255), COEFF(0, 255), COEFF(1.5756, 255) },
772 { COEFF(1, 255), COEFF(-0.2253, 255), COEFF(-0.4767, 255) },
773 { COEFF(1, 255), COEFF(1.8270, 255), COEFF(0, 255) },
775 static const int bt2020[3][3] = {
776 { COEFF(1, 219), COEFF(0, 224), COEFF(1.4746, 224) },
777 { COEFF(1, 219), COEFF(-0.1646, 224), COEFF(-0.5714, 224) },
778 { COEFF(1, 219), COEFF(1.8814, 224), COEFF(0, 224) },
780 static const int bt2020_full[3][3] = {
781 { COEFF(1, 255), COEFF(0, 255), COEFF(1.4746, 255) },
782 { COEFF(1, 255), COEFF(-0.1646, 255), COEFF(-0.5714, 255) },
783 { COEFF(1, 255), COEFF(1.8814, 255), COEFF(0, 255) },
785 static const int bt2020c[4] = {
786 COEFF(1.9404, 224), COEFF(1.5816, 224),
787 COEFF(1.7184, 224), COEFF(0.9936, 224),
789 static const int bt2020c_full[4] = {
790 COEFF(1.9404, 255), COEFF(1.5816, 255),
791 COEFF(1.7184, 255), COEFF(0.9936, 255),
794 bool full = tpg->real_quantization == V4L2_QUANTIZATION_FULL_RANGE;
795 unsigned y_offset = full ? 0 : 16;
796 int y_fac = full ? COEFF(1.0, 255) : COEFF(1.0, 219);
797 int lin_r, lin_g, lin_b, lin_y;
799 switch (tpg->real_ycbcr_enc) {
800 case V4L2_YCBCR_ENC_601:
801 ycbcr2rgb(full ? bt601_full : bt601, y, cb, cr, y_offset, r, g, b);
803 case V4L2_YCBCR_ENC_XV601:
804 /* Ignore quantization range, there is only one possible
805 * Y'CbCr encoding. */
806 ycbcr2rgb(bt601, y, cb, cr, 16, r, g, b);
808 case V4L2_YCBCR_ENC_XV709:
809 /* Ignore quantization range, there is only one possible
810 * Y'CbCr encoding. */
811 ycbcr2rgb(rec709, y, cb, cr, 16, r, g, b);
813 case V4L2_YCBCR_ENC_BT2020:
814 ycbcr2rgb(full ? bt2020_full : bt2020, y, cb, cr, y_offset, r, g, b);
816 case V4L2_YCBCR_ENC_BT2020_CONST_LUM:
817 y -= full ? 0 : 16 << 4;
822 *b = y_fac * y + (full ? bt2020c_full[0] : bt2020c[0]) * cb;
824 *b = y_fac * y + (full ? bt2020c_full[1] : bt2020c[1]) * cb;
827 *r = y_fac * y + (full ? bt2020c_full[2] : bt2020c[2]) * cr;
829 *r = y_fac * y + (full ? bt2020c_full[3] : bt2020c[3]) * cr;
831 lin_r = rec709_to_linear(*r);
832 lin_b = rec709_to_linear(*b);
833 lin_y = rec709_to_linear((y * 255) / (full ? 255 : 219));
835 lin_g = COEFF(1.0 / 0.6780, 255) * lin_y -
836 COEFF(0.2627 / 0.6780, 255) * lin_r -
837 COEFF(0.0593 / 0.6780, 255) * lin_b;
838 *g = linear_to_rec709(lin_g >> 12);
840 case V4L2_YCBCR_ENC_SMPTE240M:
841 ycbcr2rgb(full ? smpte240m_full : smpte240m, y, cb, cr, y_offset, r, g, b);
843 case V4L2_YCBCR_ENC_709:
845 ycbcr2rgb(full ? rec709_full : rec709, y, cb, cr, y_offset, r, g, b);
850 /* precalculate color bar values to speed up rendering */
851 static void precalculate_color(struct tpg_data *tpg, int k)
854 int r = tpg_colors[col].r;
855 int g = tpg_colors[col].g;
856 int b = tpg_colors[col].b;
858 bool ycbcr_valid = false;
860 if (k == TPG_COLOR_TEXTBG) {
861 col = tpg_get_textbg_color(tpg);
863 r = tpg_colors[col].r;
864 g = tpg_colors[col].g;
865 b = tpg_colors[col].b;
866 } else if (k == TPG_COLOR_TEXTFG) {
867 col = tpg_get_textfg_color(tpg);
869 r = tpg_colors[col].r;
870 g = tpg_colors[col].g;
871 b = tpg_colors[col].b;
872 } else if (tpg->pattern == TPG_PAT_NOISE) {
873 r = g = b = prandom_u32_max(256);
874 } else if (k == TPG_COLOR_RANDOM) {
875 r = g = b = tpg->qual_offset + prandom_u32_max(196);
876 } else if (k >= TPG_COLOR_RAMP) {
877 r = g = b = k - TPG_COLOR_RAMP;
880 if (tpg->pattern == TPG_PAT_CSC_COLORBAR && col <= TPG_COLOR_CSC_BLACK) {
881 r = tpg_csc_colors[tpg->colorspace][tpg->real_xfer_func][col].r;
882 g = tpg_csc_colors[tpg->colorspace][tpg->real_xfer_func][col].g;
883 b = tpg_csc_colors[tpg->colorspace][tpg->real_xfer_func][col].b;
890 if (tpg->qual == TPG_QUAL_GRAY ||
891 tpg->color_enc == TGP_COLOR_ENC_LUMA) {
892 /* Rec. 709 Luma function */
893 /* (0.2126, 0.7152, 0.0722) * (255 * 256) */
894 r = g = b = (13879 * r + 46688 * g + 4713 * b) >> 16;
898 * The assumption is that the RGB output is always full range,
899 * so only if the rgb_range overrides the 'real' rgb range do
900 * we need to convert the RGB values.
902 * Remember that r, g and b are still in the 0 - 0xff0 range.
904 if (tpg->real_rgb_range == V4L2_DV_RGB_RANGE_LIMITED &&
905 tpg->rgb_range == V4L2_DV_RGB_RANGE_FULL &&
906 tpg->color_enc == TGP_COLOR_ENC_RGB) {
908 * Convert from full range (which is what r, g and b are)
909 * to limited range (which is the 'real' RGB range), which
910 * is then interpreted as full range.
912 r = (r * 219) / 255 + (16 << 4);
913 g = (g * 219) / 255 + (16 << 4);
914 b = (b * 219) / 255 + (16 << 4);
915 } else if (tpg->real_rgb_range != V4L2_DV_RGB_RANGE_LIMITED &&
916 tpg->rgb_range == V4L2_DV_RGB_RANGE_LIMITED &&
917 tpg->color_enc == TGP_COLOR_ENC_RGB) {
920 * Clamp r, g and b to the limited range and convert to full
921 * range since that's what we deliver.
923 r = clamp(r, 16 << 4, 235 << 4);
924 g = clamp(g, 16 << 4, 235 << 4);
925 b = clamp(b, 16 << 4, 235 << 4);
926 r = (r - (16 << 4)) * 255 / 219;
927 g = (g - (16 << 4)) * 255 / 219;
928 b = (b - (16 << 4)) * 255 / 219;
931 if ((tpg->brightness != 128 || tpg->contrast != 128 ||
932 tpg->saturation != 128 || tpg->hue) &&
933 tpg->color_enc != TGP_COLOR_ENC_LUMA) {
934 /* Implement these operations */
937 /* First convert to YCbCr */
939 color_to_ycbcr(tpg, r, g, b, &y, &cb, &cr);
941 y = (16 << 4) + ((y - (16 << 4)) * tpg->contrast) / 128;
942 y += (tpg->brightness << 4) - (128 << 4);
946 tmp_cb = (cb * cos(128 + tpg->hue)) / 127 + (cr * sin[128 + tpg->hue]) / 127;
947 tmp_cr = (cr * cos(128 + tpg->hue)) / 127 - (cb * sin[128 + tpg->hue]) / 127;
949 cb = (128 << 4) + (tmp_cb * tpg->contrast * tpg->saturation) / (128 * 128);
950 cr = (128 << 4) + (tmp_cr * tpg->contrast * tpg->saturation) / (128 * 128);
951 if (tpg->color_enc == TGP_COLOR_ENC_YCBCR)
954 ycbcr_to_color(tpg, y, cb, cr, &r, &g, &b);
955 } else if ((tpg->brightness != 128 || tpg->contrast != 128) &&
956 tpg->color_enc == TGP_COLOR_ENC_LUMA) {
957 r = (16 << 4) + ((r - (16 << 4)) * tpg->contrast) / 128;
958 r += (tpg->brightness << 4) - (128 << 4);
961 switch (tpg->color_enc) {
962 case TGP_COLOR_ENC_HSV:
966 color_to_hsv(tpg, r, g, b, &h, &s, &v);
967 tpg->colors[k][0] = h;
968 tpg->colors[k][1] = s;
969 tpg->colors[k][2] = v;
972 case TGP_COLOR_ENC_YCBCR:
974 /* Convert to YCbCr */
976 color_to_ycbcr(tpg, r, g, b, &y, &cb, &cr);
982 * XV601/709 use the header/footer margins to encode R', G'
983 * and B' values outside the range [0-1]. So do not clamp
986 if (tpg->real_quantization == V4L2_QUANTIZATION_LIM_RANGE &&
987 tpg->real_ycbcr_enc != V4L2_YCBCR_ENC_XV601 &&
988 tpg->real_ycbcr_enc != V4L2_YCBCR_ENC_XV709) {
989 y = clamp(y, 16, 235);
990 cb = clamp(cb, 16, 240);
991 cr = clamp(cr, 16, 240);
993 y = clamp(y, 1, 254);
994 cb = clamp(cb, 1, 254);
995 cr = clamp(cr, 1, 254);
997 switch (tpg->fourcc) {
998 case V4L2_PIX_FMT_YUV444:
1003 case V4L2_PIX_FMT_YUV555:
1008 case V4L2_PIX_FMT_YUV565:
1014 tpg->colors[k][0] = y;
1015 tpg->colors[k][1] = cb;
1016 tpg->colors[k][2] = cr;
1019 case TGP_COLOR_ENC_LUMA:
1021 tpg->colors[k][0] = r >> 4;
1024 case TGP_COLOR_ENC_RGB:
1026 if (tpg->real_quantization == V4L2_QUANTIZATION_LIM_RANGE) {
1027 r = (r * 219) / 255 + (16 << 4);
1028 g = (g * 219) / 255 + (16 << 4);
1029 b = (b * 219) / 255 + (16 << 4);
1031 switch (tpg->fourcc) {
1032 case V4L2_PIX_FMT_RGB332:
1037 case V4L2_PIX_FMT_RGB565:
1038 case V4L2_PIX_FMT_RGB565X:
1043 case V4L2_PIX_FMT_RGB444:
1044 case V4L2_PIX_FMT_XRGB444:
1045 case V4L2_PIX_FMT_ARGB444:
1046 case V4L2_PIX_FMT_RGBX444:
1047 case V4L2_PIX_FMT_RGBA444:
1048 case V4L2_PIX_FMT_XBGR444:
1049 case V4L2_PIX_FMT_ABGR444:
1050 case V4L2_PIX_FMT_BGRX444:
1051 case V4L2_PIX_FMT_BGRA444:
1056 case V4L2_PIX_FMT_RGB555:
1057 case V4L2_PIX_FMT_XRGB555:
1058 case V4L2_PIX_FMT_ARGB555:
1059 case V4L2_PIX_FMT_RGBX555:
1060 case V4L2_PIX_FMT_RGBA555:
1061 case V4L2_PIX_FMT_XBGR555:
1062 case V4L2_PIX_FMT_ABGR555:
1063 case V4L2_PIX_FMT_BGRX555:
1064 case V4L2_PIX_FMT_BGRA555:
1065 case V4L2_PIX_FMT_RGB555X:
1066 case V4L2_PIX_FMT_XRGB555X:
1067 case V4L2_PIX_FMT_ARGB555X:
1072 case V4L2_PIX_FMT_BGR666:
1084 tpg->colors[k][0] = r;
1085 tpg->colors[k][1] = g;
1086 tpg->colors[k][2] = b;
1092 static void tpg_precalculate_colors(struct tpg_data *tpg)
1096 for (k = 0; k < TPG_COLOR_MAX; k++)
1097 precalculate_color(tpg, k);
1100 /* 'odd' is true for pixels 1, 3, 5, etc. and false for pixels 0, 2, 4, etc. */
1101 static void gen_twopix(struct tpg_data *tpg,
1102 u8 buf[TPG_MAX_PLANES][8], int color, bool odd)
1104 unsigned offset = odd * tpg->twopixelsize[0] / 2;
1105 u8 alpha = tpg->alpha_component;
1106 u8 r_y_h, g_u_s, b_v;
1108 if (tpg->alpha_red_only && color != TPG_COLOR_CSC_RED &&
1109 color != TPG_COLOR_100_RED &&
1110 color != TPG_COLOR_75_RED)
1112 if (color == TPG_COLOR_RANDOM)
1113 precalculate_color(tpg, color);
1114 r_y_h = tpg->colors[color][0]; /* R or precalculated Y, H */
1115 g_u_s = tpg->colors[color][1]; /* G or precalculated U, V */
1116 b_v = tpg->colors[color][2]; /* B or precalculated V */
1118 switch (tpg->fourcc) {
1119 case V4L2_PIX_FMT_GREY:
1120 buf[0][offset] = r_y_h;
1122 case V4L2_PIX_FMT_Y10:
1123 buf[0][offset] = (r_y_h << 2) & 0xff;
1124 buf[0][offset+1] = r_y_h >> 6;
1126 case V4L2_PIX_FMT_Y12:
1127 buf[0][offset] = (r_y_h << 4) & 0xff;
1128 buf[0][offset+1] = r_y_h >> 4;
1130 case V4L2_PIX_FMT_Y16:
1131 case V4L2_PIX_FMT_Z16:
1133 * Ideally both bytes should be set to r_y_h, but then you won't
1134 * be able to detect endian problems. So keep it 0 except for
1135 * the corner case where r_y_h is 0xff so white really will be
1138 buf[0][offset] = r_y_h == 0xff ? r_y_h : 0;
1139 buf[0][offset+1] = r_y_h;
1141 case V4L2_PIX_FMT_Y16_BE:
1142 /* See comment for V4L2_PIX_FMT_Y16 above */
1143 buf[0][offset] = r_y_h;
1144 buf[0][offset+1] = r_y_h == 0xff ? r_y_h : 0;
1146 case V4L2_PIX_FMT_YUV422M:
1147 case V4L2_PIX_FMT_YUV422P:
1148 case V4L2_PIX_FMT_YUV420:
1149 case V4L2_PIX_FMT_YUV420M:
1150 buf[0][offset] = r_y_h;
1152 buf[1][0] = (buf[1][0] + g_u_s) / 2;
1153 buf[2][0] = (buf[2][0] + b_v) / 2;
1154 buf[1][1] = buf[1][0];
1155 buf[2][1] = buf[2][0];
1161 case V4L2_PIX_FMT_YVU422M:
1162 case V4L2_PIX_FMT_YVU420:
1163 case V4L2_PIX_FMT_YVU420M:
1164 buf[0][offset] = r_y_h;
1166 buf[1][0] = (buf[1][0] + b_v) / 2;
1167 buf[2][0] = (buf[2][0] + g_u_s) / 2;
1168 buf[1][1] = buf[1][0];
1169 buf[2][1] = buf[2][0];
1176 case V4L2_PIX_FMT_NV12:
1177 case V4L2_PIX_FMT_NV12M:
1178 case V4L2_PIX_FMT_NV16:
1179 case V4L2_PIX_FMT_NV16M:
1180 buf[0][offset] = r_y_h;
1182 buf[1][0] = (buf[1][0] + g_u_s) / 2;
1183 buf[1][1] = (buf[1][1] + b_v) / 2;
1189 case V4L2_PIX_FMT_NV21:
1190 case V4L2_PIX_FMT_NV21M:
1191 case V4L2_PIX_FMT_NV61:
1192 case V4L2_PIX_FMT_NV61M:
1193 buf[0][offset] = r_y_h;
1195 buf[1][0] = (buf[1][0] + b_v) / 2;
1196 buf[1][1] = (buf[1][1] + g_u_s) / 2;
1203 case V4L2_PIX_FMT_YUV444M:
1204 buf[0][offset] = r_y_h;
1205 buf[1][offset] = g_u_s;
1206 buf[2][offset] = b_v;
1209 case V4L2_PIX_FMT_YVU444M:
1210 buf[0][offset] = r_y_h;
1211 buf[1][offset] = b_v;
1212 buf[2][offset] = g_u_s;
1215 case V4L2_PIX_FMT_NV24:
1216 buf[0][offset] = r_y_h;
1217 buf[1][2 * offset] = g_u_s;
1218 buf[1][(2 * offset + 1) % 8] = b_v;
1221 case V4L2_PIX_FMT_NV42:
1222 buf[0][offset] = r_y_h;
1223 buf[1][2 * offset] = b_v;
1224 buf[1][(2 * offset + 1) % 8] = g_u_s;
1227 case V4L2_PIX_FMT_YUYV:
1228 buf[0][offset] = r_y_h;
1230 buf[0][1] = (buf[0][1] + g_u_s) / 2;
1231 buf[0][3] = (buf[0][3] + b_v) / 2;
1237 case V4L2_PIX_FMT_UYVY:
1238 buf[0][offset + 1] = r_y_h;
1240 buf[0][0] = (buf[0][0] + g_u_s) / 2;
1241 buf[0][2] = (buf[0][2] + b_v) / 2;
1247 case V4L2_PIX_FMT_YVYU:
1248 buf[0][offset] = r_y_h;
1250 buf[0][1] = (buf[0][1] + b_v) / 2;
1251 buf[0][3] = (buf[0][3] + g_u_s) / 2;
1257 case V4L2_PIX_FMT_VYUY:
1258 buf[0][offset + 1] = r_y_h;
1260 buf[0][0] = (buf[0][0] + b_v) / 2;
1261 buf[0][2] = (buf[0][2] + g_u_s) / 2;
1267 case V4L2_PIX_FMT_RGB332:
1268 buf[0][offset] = (r_y_h << 5) | (g_u_s << 2) | b_v;
1270 case V4L2_PIX_FMT_YUV565:
1271 case V4L2_PIX_FMT_RGB565:
1272 buf[0][offset] = (g_u_s << 5) | b_v;
1273 buf[0][offset + 1] = (r_y_h << 3) | (g_u_s >> 3);
1275 case V4L2_PIX_FMT_RGB565X:
1276 buf[0][offset] = (r_y_h << 3) | (g_u_s >> 3);
1277 buf[0][offset + 1] = (g_u_s << 5) | b_v;
1279 case V4L2_PIX_FMT_RGB444:
1280 case V4L2_PIX_FMT_XRGB444:
1283 case V4L2_PIX_FMT_YUV444:
1284 case V4L2_PIX_FMT_ARGB444:
1285 buf[0][offset] = (g_u_s << 4) | b_v;
1286 buf[0][offset + 1] = (alpha & 0xf0) | r_y_h;
1288 case V4L2_PIX_FMT_RGBX444:
1291 case V4L2_PIX_FMT_RGBA444:
1292 buf[0][offset] = (b_v << 4) | (alpha >> 4);
1293 buf[0][offset + 1] = (r_y_h << 4) | g_u_s;
1295 case V4L2_PIX_FMT_XBGR444:
1298 case V4L2_PIX_FMT_ABGR444:
1299 buf[0][offset] = (g_u_s << 4) | r_y_h;
1300 buf[0][offset + 1] = (alpha & 0xf0) | b_v;
1302 case V4L2_PIX_FMT_BGRX444:
1305 case V4L2_PIX_FMT_BGRA444:
1306 buf[0][offset] = (r_y_h << 4) | (alpha >> 4);
1307 buf[0][offset + 1] = (b_v << 4) | g_u_s;
1309 case V4L2_PIX_FMT_RGB555:
1310 case V4L2_PIX_FMT_XRGB555:
1313 case V4L2_PIX_FMT_YUV555:
1314 case V4L2_PIX_FMT_ARGB555:
1315 buf[0][offset] = (g_u_s << 5) | b_v;
1316 buf[0][offset + 1] = (alpha & 0x80) | (r_y_h << 2)
1319 case V4L2_PIX_FMT_RGBX555:
1322 case V4L2_PIX_FMT_RGBA555:
1323 buf[0][offset] = (g_u_s << 6) | (b_v << 1) |
1324 ((alpha & 0x80) >> 7);
1325 buf[0][offset + 1] = (r_y_h << 3) | (g_u_s >> 2);
1327 case V4L2_PIX_FMT_XBGR555:
1330 case V4L2_PIX_FMT_ABGR555:
1331 buf[0][offset] = (g_u_s << 5) | r_y_h;
1332 buf[0][offset + 1] = (alpha & 0x80) | (b_v << 2)
1335 case V4L2_PIX_FMT_BGRX555:
1338 case V4L2_PIX_FMT_BGRA555:
1339 buf[0][offset] = (g_u_s << 6) | (r_y_h << 1) |
1340 ((alpha & 0x80) >> 7);
1341 buf[0][offset + 1] = (b_v << 3) | (g_u_s >> 2);
1343 case V4L2_PIX_FMT_RGB555X:
1344 case V4L2_PIX_FMT_XRGB555X:
1347 case V4L2_PIX_FMT_ARGB555X:
1348 buf[0][offset] = (alpha & 0x80) | (r_y_h << 2) | (g_u_s >> 3);
1349 buf[0][offset + 1] = (g_u_s << 5) | b_v;
1351 case V4L2_PIX_FMT_RGB24:
1352 case V4L2_PIX_FMT_HSV24:
1353 buf[0][offset] = r_y_h;
1354 buf[0][offset + 1] = g_u_s;
1355 buf[0][offset + 2] = b_v;
1357 case V4L2_PIX_FMT_BGR24:
1358 buf[0][offset] = b_v;
1359 buf[0][offset + 1] = g_u_s;
1360 buf[0][offset + 2] = r_y_h;
1362 case V4L2_PIX_FMT_BGR666:
1363 buf[0][offset] = (b_v << 2) | (g_u_s >> 4);
1364 buf[0][offset + 1] = (g_u_s << 4) | (r_y_h >> 2);
1365 buf[0][offset + 2] = r_y_h << 6;
1366 buf[0][offset + 3] = 0;
1368 case V4L2_PIX_FMT_RGB32:
1369 case V4L2_PIX_FMT_XRGB32:
1370 case V4L2_PIX_FMT_HSV32:
1371 case V4L2_PIX_FMT_XYUV32:
1374 case V4L2_PIX_FMT_YUV32:
1375 case V4L2_PIX_FMT_ARGB32:
1376 case V4L2_PIX_FMT_AYUV32:
1377 buf[0][offset] = alpha;
1378 buf[0][offset + 1] = r_y_h;
1379 buf[0][offset + 2] = g_u_s;
1380 buf[0][offset + 3] = b_v;
1382 case V4L2_PIX_FMT_RGBX32:
1383 case V4L2_PIX_FMT_YUVX32:
1386 case V4L2_PIX_FMT_RGBA32:
1387 case V4L2_PIX_FMT_YUVA32:
1388 buf[0][offset] = r_y_h;
1389 buf[0][offset + 1] = g_u_s;
1390 buf[0][offset + 2] = b_v;
1391 buf[0][offset + 3] = alpha;
1393 case V4L2_PIX_FMT_BGR32:
1394 case V4L2_PIX_FMT_XBGR32:
1395 case V4L2_PIX_FMT_VUYX32:
1398 case V4L2_PIX_FMT_ABGR32:
1399 case V4L2_PIX_FMT_VUYA32:
1400 buf[0][offset] = b_v;
1401 buf[0][offset + 1] = g_u_s;
1402 buf[0][offset + 2] = r_y_h;
1403 buf[0][offset + 3] = alpha;
1405 case V4L2_PIX_FMT_BGRX32:
1408 case V4L2_PIX_FMT_BGRA32:
1409 buf[0][offset] = alpha;
1410 buf[0][offset + 1] = b_v;
1411 buf[0][offset + 2] = g_u_s;
1412 buf[0][offset + 3] = r_y_h;
1414 case V4L2_PIX_FMT_SBGGR8:
1415 buf[0][offset] = odd ? g_u_s : b_v;
1416 buf[1][offset] = odd ? r_y_h : g_u_s;
1418 case V4L2_PIX_FMT_SGBRG8:
1419 buf[0][offset] = odd ? b_v : g_u_s;
1420 buf[1][offset] = odd ? g_u_s : r_y_h;
1422 case V4L2_PIX_FMT_SGRBG8:
1423 buf[0][offset] = odd ? r_y_h : g_u_s;
1424 buf[1][offset] = odd ? g_u_s : b_v;
1426 case V4L2_PIX_FMT_SRGGB8:
1427 buf[0][offset] = odd ? g_u_s : r_y_h;
1428 buf[1][offset] = odd ? b_v : g_u_s;
1430 case V4L2_PIX_FMT_SBGGR10:
1431 buf[0][offset] = odd ? g_u_s << 2 : b_v << 2;
1432 buf[0][offset + 1] = odd ? g_u_s >> 6 : b_v >> 6;
1433 buf[1][offset] = odd ? r_y_h << 2 : g_u_s << 2;
1434 buf[1][offset + 1] = odd ? r_y_h >> 6 : g_u_s >> 6;
1435 buf[0][offset] |= (buf[0][offset] >> 2) & 3;
1436 buf[1][offset] |= (buf[1][offset] >> 2) & 3;
1438 case V4L2_PIX_FMT_SGBRG10:
1439 buf[0][offset] = odd ? b_v << 2 : g_u_s << 2;
1440 buf[0][offset + 1] = odd ? b_v >> 6 : g_u_s >> 6;
1441 buf[1][offset] = odd ? g_u_s << 2 : r_y_h << 2;
1442 buf[1][offset + 1] = odd ? g_u_s >> 6 : r_y_h >> 6;
1443 buf[0][offset] |= (buf[0][offset] >> 2) & 3;
1444 buf[1][offset] |= (buf[1][offset] >> 2) & 3;
1446 case V4L2_PIX_FMT_SGRBG10:
1447 buf[0][offset] = odd ? r_y_h << 2 : g_u_s << 2;
1448 buf[0][offset + 1] = odd ? r_y_h >> 6 : g_u_s >> 6;
1449 buf[1][offset] = odd ? g_u_s << 2 : b_v << 2;
1450 buf[1][offset + 1] = odd ? g_u_s >> 6 : b_v >> 6;
1451 buf[0][offset] |= (buf[0][offset] >> 2) & 3;
1452 buf[1][offset] |= (buf[1][offset] >> 2) & 3;
1454 case V4L2_PIX_FMT_SRGGB10:
1455 buf[0][offset] = odd ? g_u_s << 2 : r_y_h << 2;
1456 buf[0][offset + 1] = odd ? g_u_s >> 6 : r_y_h >> 6;
1457 buf[1][offset] = odd ? b_v << 2 : g_u_s << 2;
1458 buf[1][offset + 1] = odd ? b_v >> 6 : g_u_s >> 6;
1459 buf[0][offset] |= (buf[0][offset] >> 2) & 3;
1460 buf[1][offset] |= (buf[1][offset] >> 2) & 3;
1462 case V4L2_PIX_FMT_SBGGR12:
1463 buf[0][offset] = odd ? g_u_s << 4 : b_v << 4;
1464 buf[0][offset + 1] = odd ? g_u_s >> 4 : b_v >> 4;
1465 buf[1][offset] = odd ? r_y_h << 4 : g_u_s << 4;
1466 buf[1][offset + 1] = odd ? r_y_h >> 4 : g_u_s >> 4;
1467 buf[0][offset] |= (buf[0][offset] >> 4) & 0xf;
1468 buf[1][offset] |= (buf[1][offset] >> 4) & 0xf;
1470 case V4L2_PIX_FMT_SGBRG12:
1471 buf[0][offset] = odd ? b_v << 4 : g_u_s << 4;
1472 buf[0][offset + 1] = odd ? b_v >> 4 : g_u_s >> 4;
1473 buf[1][offset] = odd ? g_u_s << 4 : r_y_h << 4;
1474 buf[1][offset + 1] = odd ? g_u_s >> 4 : r_y_h >> 4;
1475 buf[0][offset] |= (buf[0][offset] >> 4) & 0xf;
1476 buf[1][offset] |= (buf[1][offset] >> 4) & 0xf;
1478 case V4L2_PIX_FMT_SGRBG12:
1479 buf[0][offset] = odd ? r_y_h << 4 : g_u_s << 4;
1480 buf[0][offset + 1] = odd ? r_y_h >> 4 : g_u_s >> 4;
1481 buf[1][offset] = odd ? g_u_s << 4 : b_v << 4;
1482 buf[1][offset + 1] = odd ? g_u_s >> 4 : b_v >> 4;
1483 buf[0][offset] |= (buf[0][offset] >> 4) & 0xf;
1484 buf[1][offset] |= (buf[1][offset] >> 4) & 0xf;
1486 case V4L2_PIX_FMT_SRGGB12:
1487 buf[0][offset] = odd ? g_u_s << 4 : r_y_h << 4;
1488 buf[0][offset + 1] = odd ? g_u_s >> 4 : r_y_h >> 4;
1489 buf[1][offset] = odd ? b_v << 4 : g_u_s << 4;
1490 buf[1][offset + 1] = odd ? b_v >> 4 : g_u_s >> 4;
1491 buf[0][offset] |= (buf[0][offset] >> 4) & 0xf;
1492 buf[1][offset] |= (buf[1][offset] >> 4) & 0xf;
1494 case V4L2_PIX_FMT_SBGGR16:
1495 buf[0][offset] = buf[0][offset + 1] = odd ? g_u_s : b_v;
1496 buf[1][offset] = buf[1][offset + 1] = odd ? r_y_h : g_u_s;
1498 case V4L2_PIX_FMT_SGBRG16:
1499 buf[0][offset] = buf[0][offset + 1] = odd ? b_v : g_u_s;
1500 buf[1][offset] = buf[1][offset + 1] = odd ? g_u_s : r_y_h;
1502 case V4L2_PIX_FMT_SGRBG16:
1503 buf[0][offset] = buf[0][offset + 1] = odd ? r_y_h : g_u_s;
1504 buf[1][offset] = buf[1][offset + 1] = odd ? g_u_s : b_v;
1506 case V4L2_PIX_FMT_SRGGB16:
1507 buf[0][offset] = buf[0][offset + 1] = odd ? g_u_s : r_y_h;
1508 buf[1][offset] = buf[1][offset + 1] = odd ? b_v : g_u_s;
1513 unsigned tpg_g_interleaved_plane(const struct tpg_data *tpg, unsigned buf_line)
1515 switch (tpg->fourcc) {
1516 case V4L2_PIX_FMT_SBGGR8:
1517 case V4L2_PIX_FMT_SGBRG8:
1518 case V4L2_PIX_FMT_SGRBG8:
1519 case V4L2_PIX_FMT_SRGGB8:
1520 case V4L2_PIX_FMT_SBGGR10:
1521 case V4L2_PIX_FMT_SGBRG10:
1522 case V4L2_PIX_FMT_SGRBG10:
1523 case V4L2_PIX_FMT_SRGGB10:
1524 case V4L2_PIX_FMT_SBGGR12:
1525 case V4L2_PIX_FMT_SGBRG12:
1526 case V4L2_PIX_FMT_SGRBG12:
1527 case V4L2_PIX_FMT_SRGGB12:
1528 case V4L2_PIX_FMT_SBGGR16:
1529 case V4L2_PIX_FMT_SGBRG16:
1530 case V4L2_PIX_FMT_SGRBG16:
1531 case V4L2_PIX_FMT_SRGGB16:
1532 return buf_line & 1;
1537 EXPORT_SYMBOL_GPL(tpg_g_interleaved_plane);
1539 /* Return how many pattern lines are used by the current pattern. */
1540 static unsigned tpg_get_pat_lines(const struct tpg_data *tpg)
1542 switch (tpg->pattern) {
1543 case TPG_PAT_CHECKERS_16X16:
1544 case TPG_PAT_CHECKERS_2X2:
1545 case TPG_PAT_CHECKERS_1X1:
1546 case TPG_PAT_COLOR_CHECKERS_2X2:
1547 case TPG_PAT_COLOR_CHECKERS_1X1:
1548 case TPG_PAT_ALTERNATING_HLINES:
1549 case TPG_PAT_CROSS_1_PIXEL:
1550 case TPG_PAT_CROSS_2_PIXELS:
1551 case TPG_PAT_CROSS_10_PIXELS:
1553 case TPG_PAT_100_COLORSQUARES:
1554 case TPG_PAT_100_HCOLORBAR:
1561 /* Which pattern line should be used for the given frame line. */
1562 static unsigned tpg_get_pat_line(const struct tpg_data *tpg, unsigned line)
1564 switch (tpg->pattern) {
1565 case TPG_PAT_CHECKERS_16X16:
1566 return (line >> 4) & 1;
1567 case TPG_PAT_CHECKERS_1X1:
1568 case TPG_PAT_COLOR_CHECKERS_1X1:
1569 case TPG_PAT_ALTERNATING_HLINES:
1571 case TPG_PAT_CHECKERS_2X2:
1572 case TPG_PAT_COLOR_CHECKERS_2X2:
1573 return (line & 2) >> 1;
1574 case TPG_PAT_100_COLORSQUARES:
1575 case TPG_PAT_100_HCOLORBAR:
1576 return (line * 8) / tpg->src_height;
1577 case TPG_PAT_CROSS_1_PIXEL:
1578 return line == tpg->src_height / 2;
1579 case TPG_PAT_CROSS_2_PIXELS:
1580 return (line + 1) / 2 == tpg->src_height / 4;
1581 case TPG_PAT_CROSS_10_PIXELS:
1582 return (line + 10) / 20 == tpg->src_height / 40;
1589 * Which color should be used for the given pattern line and X coordinate.
1590 * Note: x is in the range 0 to 2 * tpg->src_width.
1592 static enum tpg_color tpg_get_color(const struct tpg_data *tpg,
1593 unsigned pat_line, unsigned x)
1595 /* Maximum number of bars are TPG_COLOR_MAX - otherwise, the input print code
1596 should be modified */
1597 static const enum tpg_color bars[3][8] = {
1598 /* Standard ITU-R 75% color bar sequence */
1599 { TPG_COLOR_CSC_WHITE, TPG_COLOR_75_YELLOW,
1600 TPG_COLOR_75_CYAN, TPG_COLOR_75_GREEN,
1601 TPG_COLOR_75_MAGENTA, TPG_COLOR_75_RED,
1602 TPG_COLOR_75_BLUE, TPG_COLOR_100_BLACK, },
1603 /* Standard ITU-R 100% color bar sequence */
1604 { TPG_COLOR_100_WHITE, TPG_COLOR_100_YELLOW,
1605 TPG_COLOR_100_CYAN, TPG_COLOR_100_GREEN,
1606 TPG_COLOR_100_MAGENTA, TPG_COLOR_100_RED,
1607 TPG_COLOR_100_BLUE, TPG_COLOR_100_BLACK, },
1608 /* Color bar sequence suitable to test CSC */
1609 { TPG_COLOR_CSC_WHITE, TPG_COLOR_CSC_YELLOW,
1610 TPG_COLOR_CSC_CYAN, TPG_COLOR_CSC_GREEN,
1611 TPG_COLOR_CSC_MAGENTA, TPG_COLOR_CSC_RED,
1612 TPG_COLOR_CSC_BLUE, TPG_COLOR_CSC_BLACK, },
1615 switch (tpg->pattern) {
1616 case TPG_PAT_75_COLORBAR:
1617 case TPG_PAT_100_COLORBAR:
1618 case TPG_PAT_CSC_COLORBAR:
1619 return bars[tpg->pattern][((x * 8) / tpg->src_width) % 8];
1620 case TPG_PAT_100_COLORSQUARES:
1621 return bars[1][(pat_line + (x * 8) / tpg->src_width) % 8];
1622 case TPG_PAT_100_HCOLORBAR:
1623 return bars[1][pat_line];
1625 return TPG_COLOR_100_BLACK;
1627 return TPG_COLOR_100_WHITE;
1629 return TPG_COLOR_100_RED;
1631 return TPG_COLOR_100_GREEN;
1633 return TPG_COLOR_100_BLUE;
1634 case TPG_PAT_CHECKERS_16X16:
1635 return (((x >> 4) & 1) ^ (pat_line & 1)) ?
1636 TPG_COLOR_100_BLACK : TPG_COLOR_100_WHITE;
1637 case TPG_PAT_CHECKERS_1X1:
1638 return ((x & 1) ^ (pat_line & 1)) ?
1639 TPG_COLOR_100_WHITE : TPG_COLOR_100_BLACK;
1640 case TPG_PAT_COLOR_CHECKERS_1X1:
1641 return ((x & 1) ^ (pat_line & 1)) ?
1642 TPG_COLOR_100_RED : TPG_COLOR_100_BLUE;
1643 case TPG_PAT_CHECKERS_2X2:
1644 return (((x >> 1) & 1) ^ (pat_line & 1)) ?
1645 TPG_COLOR_100_WHITE : TPG_COLOR_100_BLACK;
1646 case TPG_PAT_COLOR_CHECKERS_2X2:
1647 return (((x >> 1) & 1) ^ (pat_line & 1)) ?
1648 TPG_COLOR_100_RED : TPG_COLOR_100_BLUE;
1649 case TPG_PAT_ALTERNATING_HLINES:
1650 return pat_line ? TPG_COLOR_100_WHITE : TPG_COLOR_100_BLACK;
1651 case TPG_PAT_ALTERNATING_VLINES:
1652 return (x & 1) ? TPG_COLOR_100_WHITE : TPG_COLOR_100_BLACK;
1653 case TPG_PAT_CROSS_1_PIXEL:
1654 if (pat_line || (x % tpg->src_width) == tpg->src_width / 2)
1655 return TPG_COLOR_100_BLACK;
1656 return TPG_COLOR_100_WHITE;
1657 case TPG_PAT_CROSS_2_PIXELS:
1658 if (pat_line || ((x % tpg->src_width) + 1) / 2 == tpg->src_width / 4)
1659 return TPG_COLOR_100_BLACK;
1660 return TPG_COLOR_100_WHITE;
1661 case TPG_PAT_CROSS_10_PIXELS:
1662 if (pat_line || ((x % tpg->src_width) + 10) / 20 == tpg->src_width / 40)
1663 return TPG_COLOR_100_BLACK;
1664 return TPG_COLOR_100_WHITE;
1665 case TPG_PAT_GRAY_RAMP:
1666 return TPG_COLOR_RAMP + ((x % tpg->src_width) * 256) / tpg->src_width;
1668 return TPG_COLOR_100_RED;
1673 * Given the pixel aspect ratio and video aspect ratio calculate the
1674 * coordinates of a centered square and the coordinates of the border of
1675 * the active video area. The coordinates are relative to the source
1678 static void tpg_calculate_square_border(struct tpg_data *tpg)
1680 unsigned w = tpg->src_width;
1681 unsigned h = tpg->src_height;
1682 unsigned sq_w, sq_h;
1684 sq_w = (w * 2 / 5) & ~1;
1685 if (((w - sq_w) / 2) & 1)
1688 tpg->square.width = sq_w;
1689 if (tpg->vid_aspect == TPG_VIDEO_ASPECT_16X9_ANAMORPHIC) {
1690 unsigned ana_sq_w = (sq_w / 4) * 3;
1692 if (((w - ana_sq_w) / 2) & 1)
1694 tpg->square.width = ana_sq_w;
1696 tpg->square.left = (w - tpg->square.width) / 2;
1697 if (tpg->pix_aspect == TPG_PIXEL_ASPECT_NTSC)
1698 sq_h = sq_w * 10 / 11;
1699 else if (tpg->pix_aspect == TPG_PIXEL_ASPECT_PAL)
1700 sq_h = sq_w * 59 / 54;
1701 tpg->square.height = sq_h;
1702 tpg->square.top = (h - sq_h) / 2;
1703 tpg->border.left = 0;
1704 tpg->border.width = w;
1705 tpg->border.top = 0;
1706 tpg->border.height = h;
1707 switch (tpg->vid_aspect) {
1708 case TPG_VIDEO_ASPECT_4X3:
1709 if (tpg->pix_aspect)
1711 if (3 * w >= 4 * h) {
1712 tpg->border.width = ((4 * h) / 3) & ~1;
1713 if (((w - tpg->border.width) / 2) & ~1)
1714 tpg->border.width -= 2;
1715 tpg->border.left = (w - tpg->border.width) / 2;
1718 tpg->border.height = ((3 * w) / 4) & ~1;
1719 tpg->border.top = (h - tpg->border.height) / 2;
1721 case TPG_VIDEO_ASPECT_14X9_CENTRE:
1722 if (tpg->pix_aspect) {
1723 tpg->border.height = tpg->pix_aspect == TPG_PIXEL_ASPECT_NTSC ? 420 : 506;
1724 tpg->border.top = (h - tpg->border.height) / 2;
1727 if (9 * w >= 14 * h) {
1728 tpg->border.width = ((14 * h) / 9) & ~1;
1729 if (((w - tpg->border.width) / 2) & ~1)
1730 tpg->border.width -= 2;
1731 tpg->border.left = (w - tpg->border.width) / 2;
1734 tpg->border.height = ((9 * w) / 14) & ~1;
1735 tpg->border.top = (h - tpg->border.height) / 2;
1737 case TPG_VIDEO_ASPECT_16X9_CENTRE:
1738 if (tpg->pix_aspect) {
1739 tpg->border.height = tpg->pix_aspect == TPG_PIXEL_ASPECT_NTSC ? 368 : 442;
1740 tpg->border.top = (h - tpg->border.height) / 2;
1743 if (9 * w >= 16 * h) {
1744 tpg->border.width = ((16 * h) / 9) & ~1;
1745 if (((w - tpg->border.width) / 2) & ~1)
1746 tpg->border.width -= 2;
1747 tpg->border.left = (w - tpg->border.width) / 2;
1750 tpg->border.height = ((9 * w) / 16) & ~1;
1751 tpg->border.top = (h - tpg->border.height) / 2;
1758 static void tpg_precalculate_line(struct tpg_data *tpg)
1760 enum tpg_color contrast;
1761 u8 pix[TPG_MAX_PLANES][8];
1766 switch (tpg->pattern) {
1768 contrast = TPG_COLOR_100_RED;
1770 case TPG_PAT_CSC_COLORBAR:
1771 contrast = TPG_COLOR_CSC_GREEN;
1774 contrast = TPG_COLOR_100_GREEN;
1778 for (pat = 0; pat < tpg_get_pat_lines(tpg); pat++) {
1779 /* Coarse scaling with Bresenham */
1780 unsigned int_part = tpg->src_width / tpg->scaled_width;
1781 unsigned fract_part = tpg->src_width % tpg->scaled_width;
1785 for (x = 0; x < tpg->scaled_width * 2; x += 2) {
1786 unsigned real_x = src_x;
1787 enum tpg_color color1, color2;
1789 real_x = tpg->hflip ? tpg->src_width * 2 - real_x - 2 : real_x;
1790 color1 = tpg_get_color(tpg, pat, real_x);
1793 error += fract_part;
1794 if (error >= tpg->scaled_width) {
1795 error -= tpg->scaled_width;
1800 real_x = tpg->hflip ? tpg->src_width * 2 - real_x - 2 : real_x;
1801 color2 = tpg_get_color(tpg, pat, real_x);
1804 error += fract_part;
1805 if (error >= tpg->scaled_width) {
1806 error -= tpg->scaled_width;
1810 gen_twopix(tpg, pix, tpg->hflip ? color2 : color1, 0);
1811 gen_twopix(tpg, pix, tpg->hflip ? color1 : color2, 1);
1812 for (p = 0; p < tpg->planes; p++) {
1813 unsigned twopixsize = tpg->twopixelsize[p];
1814 unsigned hdiv = tpg->hdownsampling[p];
1815 u8 *pos = tpg->lines[pat][p] + tpg_hdiv(tpg, p, x);
1817 memcpy(pos, pix[p], twopixsize / hdiv);
1822 if (tpg->vdownsampling[tpg->planes - 1] > 1) {
1823 unsigned pat_lines = tpg_get_pat_lines(tpg);
1825 for (pat = 0; pat < pat_lines; pat++) {
1826 unsigned next_pat = (pat + 1) % pat_lines;
1828 for (p = 1; p < tpg->planes; p++) {
1829 unsigned w = tpg_hdiv(tpg, p, tpg->scaled_width * 2);
1830 u8 *pos1 = tpg->lines[pat][p];
1831 u8 *pos2 = tpg->lines[next_pat][p];
1832 u8 *dest = tpg->downsampled_lines[pat][p];
1834 for (x = 0; x < w; x++, pos1++, pos2++, dest++)
1835 *dest = ((u16)*pos1 + (u16)*pos2) / 2;
1840 gen_twopix(tpg, pix, contrast, 0);
1841 gen_twopix(tpg, pix, contrast, 1);
1842 for (p = 0; p < tpg->planes; p++) {
1843 unsigned twopixsize = tpg->twopixelsize[p];
1844 u8 *pos = tpg->contrast_line[p];
1846 for (x = 0; x < tpg->scaled_width; x += 2, pos += twopixsize)
1847 memcpy(pos, pix[p], twopixsize);
1850 gen_twopix(tpg, pix, TPG_COLOR_100_BLACK, 0);
1851 gen_twopix(tpg, pix, TPG_COLOR_100_BLACK, 1);
1852 for (p = 0; p < tpg->planes; p++) {
1853 unsigned twopixsize = tpg->twopixelsize[p];
1854 u8 *pos = tpg->black_line[p];
1856 for (x = 0; x < tpg->scaled_width; x += 2, pos += twopixsize)
1857 memcpy(pos, pix[p], twopixsize);
1860 for (x = 0; x < tpg->scaled_width * 2; x += 2) {
1861 gen_twopix(tpg, pix, TPG_COLOR_RANDOM, 0);
1862 gen_twopix(tpg, pix, TPG_COLOR_RANDOM, 1);
1863 for (p = 0; p < tpg->planes; p++) {
1864 unsigned twopixsize = tpg->twopixelsize[p];
1865 u8 *pos = tpg->random_line[p] + x * twopixsize / 2;
1867 memcpy(pos, pix[p], twopixsize);
1871 gen_twopix(tpg, tpg->textbg, TPG_COLOR_TEXTBG, 0);
1872 gen_twopix(tpg, tpg->textbg, TPG_COLOR_TEXTBG, 1);
1873 gen_twopix(tpg, tpg->textfg, TPG_COLOR_TEXTFG, 0);
1874 gen_twopix(tpg, tpg->textfg, TPG_COLOR_TEXTFG, 1);
1877 /* need this to do rgb24 rendering */
1878 typedef struct { u16 __; u8 _; } __packed x24;
1880 #define PRINTSTR(PIXTYPE) do { \
1881 unsigned vdiv = tpg->vdownsampling[p]; \
1882 unsigned hdiv = tpg->hdownsampling[p]; \
1886 memcpy(&fg, tpg->textfg[p], sizeof(PIXTYPE)); \
1887 memcpy(&bg, tpg->textbg[p], sizeof(PIXTYPE)); \
1889 for (line = first; line < 16; line += vdiv * step) { \
1890 int l = tpg->vflip ? 15 - line : line; \
1891 PIXTYPE *pos = (PIXTYPE *)(basep[p][(line / vdiv) & 1] + \
1892 ((y * step + l) / (vdiv * div)) * tpg->bytesperline[p] + \
1893 (x / hdiv) * sizeof(PIXTYPE)); \
1896 for (s = 0; s < len; s++) { \
1897 u8 chr = font8x16[(u8)text[s] * 16 + line]; \
1899 if (hdiv == 2 && tpg->hflip) { \
1900 pos[3] = (chr & (0x01 << 6) ? fg : bg); \
1901 pos[2] = (chr & (0x01 << 4) ? fg : bg); \
1902 pos[1] = (chr & (0x01 << 2) ? fg : bg); \
1903 pos[0] = (chr & (0x01 << 0) ? fg : bg); \
1904 } else if (hdiv == 2) { \
1905 pos[0] = (chr & (0x01 << 7) ? fg : bg); \
1906 pos[1] = (chr & (0x01 << 5) ? fg : bg); \
1907 pos[2] = (chr & (0x01 << 3) ? fg : bg); \
1908 pos[3] = (chr & (0x01 << 1) ? fg : bg); \
1909 } else if (tpg->hflip) { \
1910 pos[7] = (chr & (0x01 << 7) ? fg : bg); \
1911 pos[6] = (chr & (0x01 << 6) ? fg : bg); \
1912 pos[5] = (chr & (0x01 << 5) ? fg : bg); \
1913 pos[4] = (chr & (0x01 << 4) ? fg : bg); \
1914 pos[3] = (chr & (0x01 << 3) ? fg : bg); \
1915 pos[2] = (chr & (0x01 << 2) ? fg : bg); \
1916 pos[1] = (chr & (0x01 << 1) ? fg : bg); \
1917 pos[0] = (chr & (0x01 << 0) ? fg : bg); \
1919 pos[0] = (chr & (0x01 << 7) ? fg : bg); \
1920 pos[1] = (chr & (0x01 << 6) ? fg : bg); \
1921 pos[2] = (chr & (0x01 << 5) ? fg : bg); \
1922 pos[3] = (chr & (0x01 << 4) ? fg : bg); \
1923 pos[4] = (chr & (0x01 << 3) ? fg : bg); \
1924 pos[5] = (chr & (0x01 << 2) ? fg : bg); \
1925 pos[6] = (chr & (0x01 << 1) ? fg : bg); \
1926 pos[7] = (chr & (0x01 << 0) ? fg : bg); \
1929 pos += (tpg->hflip ? -8 : 8) / (int)hdiv; \
1934 static noinline void tpg_print_str_2(const struct tpg_data *tpg, u8 *basep[TPG_MAX_PLANES][2],
1935 unsigned p, unsigned first, unsigned div, unsigned step,
1936 int y, int x, const char *text, unsigned len)
1941 static noinline void tpg_print_str_4(const struct tpg_data *tpg, u8 *basep[TPG_MAX_PLANES][2],
1942 unsigned p, unsigned first, unsigned div, unsigned step,
1943 int y, int x, const char *text, unsigned len)
1948 static noinline void tpg_print_str_6(const struct tpg_data *tpg, u8 *basep[TPG_MAX_PLANES][2],
1949 unsigned p, unsigned first, unsigned div, unsigned step,
1950 int y, int x, const char *text, unsigned len)
1955 static noinline void tpg_print_str_8(const struct tpg_data *tpg, u8 *basep[TPG_MAX_PLANES][2],
1956 unsigned p, unsigned first, unsigned div, unsigned step,
1957 int y, int x, const char *text, unsigned len)
1962 void tpg_gen_text(const struct tpg_data *tpg, u8 *basep[TPG_MAX_PLANES][2],
1963 int y, int x, const char *text)
1965 unsigned step = V4L2_FIELD_HAS_T_OR_B(tpg->field) ? 2 : 1;
1966 unsigned div = step;
1971 if (font8x16 == NULL || basep == NULL || text == NULL)
1976 /* Checks if it is possible to show string */
1977 if (y + 16 >= tpg->compose.height || x + 8 >= tpg->compose.width)
1980 if (len > (tpg->compose.width - x) / 8)
1981 len = (tpg->compose.width - x) / 8;
1983 y = tpg->compose.height - y - 16;
1985 x = tpg->compose.width - x - 8;
1986 y += tpg->compose.top;
1987 x += tpg->compose.left;
1988 if (tpg->field == V4L2_FIELD_BOTTOM)
1990 else if (tpg->field == V4L2_FIELD_SEQ_TB || tpg->field == V4L2_FIELD_SEQ_BT)
1993 for (p = 0; p < tpg->planes; p++) {
1995 switch (tpg->twopixelsize[p]) {
1997 tpg_print_str_2(tpg, basep, p, first, div, step, y, x,
2001 tpg_print_str_4(tpg, basep, p, first, div, step, y, x,
2005 tpg_print_str_6(tpg, basep, p, first, div, step, y, x,
2009 tpg_print_str_8(tpg, basep, p, first, div, step, y, x,
2015 EXPORT_SYMBOL_GPL(tpg_gen_text);
2017 const char *tpg_g_color_order(const struct tpg_data *tpg)
2019 switch (tpg->pattern) {
2020 case TPG_PAT_75_COLORBAR:
2021 case TPG_PAT_100_COLORBAR:
2022 case TPG_PAT_CSC_COLORBAR:
2023 case TPG_PAT_100_HCOLORBAR:
2024 return "White, yellow, cyan, green, magenta, red, blue, black";
2039 EXPORT_SYMBOL_GPL(tpg_g_color_order);
2041 void tpg_update_mv_step(struct tpg_data *tpg)
2043 int factor = tpg->mv_hor_mode > TPG_MOVE_NONE ? -1 : 1;
2047 switch (tpg->mv_hor_mode) {
2048 case TPG_MOVE_NEG_FAST:
2049 case TPG_MOVE_POS_FAST:
2050 tpg->mv_hor_step = ((tpg->src_width + 319) / 320) * 4;
2054 tpg->mv_hor_step = ((tpg->src_width + 639) / 640) * 4;
2056 case TPG_MOVE_NEG_SLOW:
2057 case TPG_MOVE_POS_SLOW:
2058 tpg->mv_hor_step = 2;
2061 tpg->mv_hor_step = 0;
2065 tpg->mv_hor_step = tpg->src_width - tpg->mv_hor_step;
2067 factor = tpg->mv_vert_mode > TPG_MOVE_NONE ? -1 : 1;
2068 switch (tpg->mv_vert_mode) {
2069 case TPG_MOVE_NEG_FAST:
2070 case TPG_MOVE_POS_FAST:
2071 tpg->mv_vert_step = ((tpg->src_width + 319) / 320) * 4;
2075 tpg->mv_vert_step = ((tpg->src_width + 639) / 640) * 4;
2077 case TPG_MOVE_NEG_SLOW:
2078 case TPG_MOVE_POS_SLOW:
2079 tpg->mv_vert_step = 1;
2082 tpg->mv_vert_step = 0;
2086 tpg->mv_vert_step = tpg->src_height - tpg->mv_vert_step;
2088 EXPORT_SYMBOL_GPL(tpg_update_mv_step);
2090 /* Map the line number relative to the crop rectangle to a frame line number */
2091 static unsigned tpg_calc_frameline(const struct tpg_data *tpg, unsigned src_y,
2095 case V4L2_FIELD_TOP:
2096 return tpg->crop.top + src_y * 2;
2097 case V4L2_FIELD_BOTTOM:
2098 return tpg->crop.top + src_y * 2 + 1;
2100 return src_y + tpg->crop.top;
2105 * Map the line number relative to the compose rectangle to a destination
2106 * buffer line number.
2108 static unsigned tpg_calc_buffer_line(const struct tpg_data *tpg, unsigned y,
2111 y += tpg->compose.top;
2113 case V4L2_FIELD_SEQ_TB:
2115 return tpg->buf_height / 2 + y / 2;
2117 case V4L2_FIELD_SEQ_BT:
2120 return tpg->buf_height / 2 + y / 2;
2126 static void tpg_recalc(struct tpg_data *tpg)
2128 if (tpg->recalc_colors) {
2129 tpg->recalc_colors = false;
2130 tpg->recalc_lines = true;
2131 tpg->real_xfer_func = tpg->xfer_func;
2132 tpg->real_ycbcr_enc = tpg->ycbcr_enc;
2133 tpg->real_hsv_enc = tpg->hsv_enc;
2134 tpg->real_quantization = tpg->quantization;
2136 if (tpg->xfer_func == V4L2_XFER_FUNC_DEFAULT)
2137 tpg->real_xfer_func =
2138 V4L2_MAP_XFER_FUNC_DEFAULT(tpg->colorspace);
2140 if (tpg->ycbcr_enc == V4L2_YCBCR_ENC_DEFAULT)
2141 tpg->real_ycbcr_enc =
2142 V4L2_MAP_YCBCR_ENC_DEFAULT(tpg->colorspace);
2144 if (tpg->quantization == V4L2_QUANTIZATION_DEFAULT)
2145 tpg->real_quantization =
2146 V4L2_MAP_QUANTIZATION_DEFAULT(
2147 tpg->color_enc != TGP_COLOR_ENC_YCBCR,
2148 tpg->colorspace, tpg->real_ycbcr_enc);
2150 tpg_precalculate_colors(tpg);
2152 if (tpg->recalc_square_border) {
2153 tpg->recalc_square_border = false;
2154 tpg_calculate_square_border(tpg);
2156 if (tpg->recalc_lines) {
2157 tpg->recalc_lines = false;
2158 tpg_precalculate_line(tpg);
2162 void tpg_calc_text_basep(struct tpg_data *tpg,
2163 u8 *basep[TPG_MAX_PLANES][2], unsigned p, u8 *vbuf)
2165 unsigned stride = tpg->bytesperline[p];
2166 unsigned h = tpg->buf_height;
2172 h /= tpg->vdownsampling[p];
2173 if (tpg->field == V4L2_FIELD_SEQ_TB)
2174 basep[p][1] += h * stride / 2;
2175 else if (tpg->field == V4L2_FIELD_SEQ_BT)
2176 basep[p][0] += h * stride / 2;
2177 if (p == 0 && tpg->interleaved)
2178 tpg_calc_text_basep(tpg, basep, 1, vbuf);
2180 EXPORT_SYMBOL_GPL(tpg_calc_text_basep);
2182 static int tpg_pattern_avg(const struct tpg_data *tpg,
2183 unsigned pat1, unsigned pat2)
2185 unsigned pat_lines = tpg_get_pat_lines(tpg);
2187 if (pat1 == (pat2 + 1) % pat_lines)
2189 if (pat2 == (pat1 + 1) % pat_lines)
2194 static const char *tpg_color_enc_str(enum tgp_color_enc
2197 switch (color_enc) {
2198 case TGP_COLOR_ENC_HSV:
2200 case TGP_COLOR_ENC_YCBCR:
2202 case TGP_COLOR_ENC_LUMA:
2204 case TGP_COLOR_ENC_RGB:
2211 void tpg_log_status(struct tpg_data *tpg)
2213 pr_info("tpg source WxH: %ux%u (%s)\n",
2214 tpg->src_width, tpg->src_height,
2215 tpg_color_enc_str(tpg->color_enc));
2216 pr_info("tpg field: %u\n", tpg->field);
2217 pr_info("tpg crop: %ux%u@%dx%d\n", tpg->crop.width, tpg->crop.height,
2218 tpg->crop.left, tpg->crop.top);
2219 pr_info("tpg compose: %ux%u@%dx%d\n", tpg->compose.width, tpg->compose.height,
2220 tpg->compose.left, tpg->compose.top);
2221 pr_info("tpg colorspace: %d\n", tpg->colorspace);
2222 pr_info("tpg transfer function: %d/%d\n", tpg->xfer_func, tpg->real_xfer_func);
2223 if (tpg->color_enc == TGP_COLOR_ENC_HSV)
2224 pr_info("tpg HSV encoding: %d/%d\n",
2225 tpg->hsv_enc, tpg->real_hsv_enc);
2226 else if (tpg->color_enc == TGP_COLOR_ENC_YCBCR)
2227 pr_info("tpg Y'CbCr encoding: %d/%d\n",
2228 tpg->ycbcr_enc, tpg->real_ycbcr_enc);
2229 pr_info("tpg quantization: %d/%d\n", tpg->quantization, tpg->real_quantization);
2230 pr_info("tpg RGB range: %d/%d\n", tpg->rgb_range, tpg->real_rgb_range);
2232 EXPORT_SYMBOL_GPL(tpg_log_status);
2235 * This struct contains common parameters used by both the drawing of the
2236 * test pattern and the drawing of the extras (borders, square, etc.)
2238 struct tpg_draw_params {
2242 unsigned twopixsize;
2246 unsigned frame_line;
2247 unsigned frame_line_next;
2250 unsigned mv_hor_old;
2251 unsigned mv_hor_new;
2252 unsigned mv_vert_old;
2253 unsigned mv_vert_new;
2257 unsigned wss_random_offset;
2259 unsigned left_pillar_width;
2260 unsigned right_pillar_start;
2263 static void tpg_fill_params_pattern(const struct tpg_data *tpg, unsigned p,
2264 struct tpg_draw_params *params)
2266 params->mv_hor_old =
2267 tpg_hscale_div(tpg, p, tpg->mv_hor_count % tpg->src_width);
2268 params->mv_hor_new =
2269 tpg_hscale_div(tpg, p, (tpg->mv_hor_count + tpg->mv_hor_step) %
2271 params->mv_vert_old = tpg->mv_vert_count % tpg->src_height;
2272 params->mv_vert_new =
2273 (tpg->mv_vert_count + tpg->mv_vert_step) % tpg->src_height;
2276 static void tpg_fill_params_extras(const struct tpg_data *tpg,
2278 struct tpg_draw_params *params)
2280 unsigned left_pillar_width = 0;
2281 unsigned right_pillar_start = params->img_width;
2283 params->wss_width = tpg->crop.left < tpg->src_width / 2 ?
2284 tpg->src_width / 2 - tpg->crop.left : 0;
2285 if (params->wss_width > tpg->crop.width)
2286 params->wss_width = tpg->crop.width;
2287 params->wss_width = tpg_hscale_div(tpg, p, params->wss_width);
2288 params->wss_random_offset =
2289 params->twopixsize * prandom_u32_max(tpg->src_width / 2);
2291 if (tpg->crop.left < tpg->border.left) {
2292 left_pillar_width = tpg->border.left - tpg->crop.left;
2293 if (left_pillar_width > tpg->crop.width)
2294 left_pillar_width = tpg->crop.width;
2295 left_pillar_width = tpg_hscale_div(tpg, p, left_pillar_width);
2297 params->left_pillar_width = left_pillar_width;
2299 if (tpg->crop.left + tpg->crop.width >
2300 tpg->border.left + tpg->border.width) {
2301 right_pillar_start =
2302 tpg->border.left + tpg->border.width - tpg->crop.left;
2303 right_pillar_start =
2304 tpg_hscale_div(tpg, p, right_pillar_start);
2305 if (right_pillar_start > params->img_width)
2306 right_pillar_start = params->img_width;
2308 params->right_pillar_start = right_pillar_start;
2310 params->sav_eav_f = tpg->field ==
2311 (params->is_60hz ? V4L2_FIELD_TOP : V4L2_FIELD_BOTTOM);
2314 static void tpg_fill_plane_extras(const struct tpg_data *tpg,
2315 const struct tpg_draw_params *params,
2316 unsigned p, unsigned h, u8 *vbuf)
2318 unsigned twopixsize = params->twopixsize;
2319 unsigned img_width = params->img_width;
2320 unsigned frame_line = params->frame_line;
2321 const struct v4l2_rect *sq = &tpg->square;
2322 const struct v4l2_rect *b = &tpg->border;
2323 const struct v4l2_rect *c = &tpg->crop;
2325 if (params->is_tv && !params->is_60hz &&
2326 frame_line == 0 && params->wss_width) {
2328 * Replace the first half of the top line of a 50 Hz frame
2329 * with random data to simulate a WSS signal.
2331 u8 *wss = tpg->random_line[p] + params->wss_random_offset;
2333 memcpy(vbuf, wss, params->wss_width);
2336 if (tpg->show_border && frame_line >= b->top &&
2337 frame_line < b->top + b->height) {
2338 unsigned bottom = b->top + b->height - 1;
2339 unsigned left = params->left_pillar_width;
2340 unsigned right = params->right_pillar_start;
2342 if (frame_line == b->top || frame_line == b->top + 1 ||
2343 frame_line == bottom || frame_line == bottom - 1) {
2344 memcpy(vbuf + left, tpg->contrast_line[p],
2347 if (b->left >= c->left &&
2348 b->left < c->left + c->width)
2350 tpg->contrast_line[p], twopixsize);
2351 if (b->left + b->width > c->left &&
2352 b->left + b->width <= c->left + c->width)
2353 memcpy(vbuf + right - twopixsize,
2354 tpg->contrast_line[p], twopixsize);
2357 if (tpg->qual != TPG_QUAL_NOISE && frame_line >= b->top &&
2358 frame_line < b->top + b->height) {
2359 memcpy(vbuf, tpg->black_line[p], params->left_pillar_width);
2360 memcpy(vbuf + params->right_pillar_start, tpg->black_line[p],
2361 img_width - params->right_pillar_start);
2363 if (tpg->show_square && frame_line >= sq->top &&
2364 frame_line < sq->top + sq->height &&
2365 sq->left < c->left + c->width &&
2366 sq->left + sq->width >= c->left) {
2367 unsigned left = sq->left;
2368 unsigned width = sq->width;
2370 if (c->left > left) {
2371 width -= c->left - left;
2374 if (c->left + c->width < left + width)
2375 width -= left + width - c->left - c->width;
2377 left = tpg_hscale_div(tpg, p, left);
2378 width = tpg_hscale_div(tpg, p, width);
2379 memcpy(vbuf + left, tpg->contrast_line[p], width);
2381 if (tpg->insert_sav) {
2382 unsigned offset = tpg_hdiv(tpg, p, tpg->compose.width / 3);
2383 u8 *p = vbuf + offset;
2384 unsigned vact = 0, hact = 0;
2389 p[3] = 0x80 | (params->sav_eav_f << 6) |
2390 (vact << 5) | (hact << 4) |
2391 ((hact ^ vact) << 3) |
2392 ((hact ^ params->sav_eav_f) << 2) |
2393 ((params->sav_eav_f ^ vact) << 1) |
2394 (hact ^ vact ^ params->sav_eav_f);
2396 if (tpg->insert_eav) {
2397 unsigned offset = tpg_hdiv(tpg, p, tpg->compose.width * 2 / 3);
2398 u8 *p = vbuf + offset;
2399 unsigned vact = 0, hact = 1;
2404 p[3] = 0x80 | (params->sav_eav_f << 6) |
2405 (vact << 5) | (hact << 4) |
2406 ((hact ^ vact) << 3) |
2407 ((hact ^ params->sav_eav_f) << 2) |
2408 ((params->sav_eav_f ^ vact) << 1) |
2409 (hact ^ vact ^ params->sav_eav_f);
2411 if (tpg->insert_hdmi_video_guard_band) {
2414 switch (tpg->fourcc) {
2415 case V4L2_PIX_FMT_BGR24:
2416 case V4L2_PIX_FMT_RGB24:
2417 for (i = 0; i < 3 * 4; i += 3) {
2423 case V4L2_PIX_FMT_RGB32:
2424 case V4L2_PIX_FMT_ARGB32:
2425 case V4L2_PIX_FMT_XRGB32:
2426 case V4L2_PIX_FMT_BGRX32:
2427 case V4L2_PIX_FMT_BGRA32:
2428 for (i = 0; i < 4 * 4; i += 4) {
2435 case V4L2_PIX_FMT_BGR32:
2436 case V4L2_PIX_FMT_XBGR32:
2437 case V4L2_PIX_FMT_ABGR32:
2438 case V4L2_PIX_FMT_RGBX32:
2439 case V4L2_PIX_FMT_RGBA32:
2440 for (i = 0; i < 4 * 4; i += 4) {
2451 static void tpg_fill_plane_pattern(const struct tpg_data *tpg,
2452 const struct tpg_draw_params *params,
2453 unsigned p, unsigned h, u8 *vbuf)
2455 unsigned twopixsize = params->twopixsize;
2456 unsigned img_width = params->img_width;
2457 unsigned mv_hor_old = params->mv_hor_old;
2458 unsigned mv_hor_new = params->mv_hor_new;
2459 unsigned mv_vert_old = params->mv_vert_old;
2460 unsigned mv_vert_new = params->mv_vert_new;
2461 unsigned frame_line = params->frame_line;
2462 unsigned frame_line_next = params->frame_line_next;
2463 unsigned line_offset = tpg_hscale_div(tpg, p, tpg->crop.left);
2465 bool fill_blank = false;
2466 unsigned pat_line_old;
2467 unsigned pat_line_new;
2468 u8 *linestart_older;
2469 u8 *linestart_newer;
2471 u8 *linestart_bottom;
2473 even = !(frame_line & 1);
2475 if (h >= params->hmax) {
2476 if (params->hmax == tpg->compose.height)
2478 if (!tpg->perc_fill_blank)
2484 frame_line = tpg->src_height - frame_line - 1;
2485 frame_line_next = tpg->src_height - frame_line_next - 1;
2489 linestart_older = tpg->contrast_line[p];
2490 linestart_newer = tpg->contrast_line[p];
2491 } else if (tpg->qual != TPG_QUAL_NOISE &&
2492 (frame_line < tpg->border.top ||
2493 frame_line >= tpg->border.top + tpg->border.height)) {
2494 linestart_older = tpg->black_line[p];
2495 linestart_newer = tpg->black_line[p];
2496 } else if (tpg->pattern == TPG_PAT_NOISE || tpg->qual == TPG_QUAL_NOISE) {
2497 linestart_older = tpg->random_line[p] +
2498 twopixsize * prandom_u32_max(tpg->src_width / 2);
2499 linestart_newer = tpg->random_line[p] +
2500 twopixsize * prandom_u32_max(tpg->src_width / 2);
2502 unsigned frame_line_old =
2503 (frame_line + mv_vert_old) % tpg->src_height;
2504 unsigned frame_line_new =
2505 (frame_line + mv_vert_new) % tpg->src_height;
2506 unsigned pat_line_next_old;
2507 unsigned pat_line_next_new;
2509 pat_line_old = tpg_get_pat_line(tpg, frame_line_old);
2510 pat_line_new = tpg_get_pat_line(tpg, frame_line_new);
2511 linestart_older = tpg->lines[pat_line_old][p] + mv_hor_old;
2512 linestart_newer = tpg->lines[pat_line_new][p] + mv_hor_new;
2514 if (tpg->vdownsampling[p] > 1 && frame_line != frame_line_next) {
2518 * Now decide whether we need to use downsampled_lines[].
2519 * That's necessary if the two lines use different patterns.
2521 pat_line_next_old = tpg_get_pat_line(tpg,
2522 (frame_line_next + mv_vert_old) % tpg->src_height);
2523 pat_line_next_new = tpg_get_pat_line(tpg,
2524 (frame_line_next + mv_vert_new) % tpg->src_height);
2526 switch (tpg->field) {
2527 case V4L2_FIELD_INTERLACED:
2528 case V4L2_FIELD_INTERLACED_BT:
2529 case V4L2_FIELD_INTERLACED_TB:
2530 avg_pat = tpg_pattern_avg(tpg, pat_line_old, pat_line_new);
2533 linestart_older = tpg->downsampled_lines[avg_pat][p] + mv_hor_old;
2534 linestart_newer = linestart_older;
2536 case V4L2_FIELD_NONE:
2537 case V4L2_FIELD_TOP:
2538 case V4L2_FIELD_BOTTOM:
2539 case V4L2_FIELD_SEQ_BT:
2540 case V4L2_FIELD_SEQ_TB:
2541 avg_pat = tpg_pattern_avg(tpg, pat_line_old, pat_line_next_old);
2543 linestart_older = tpg->downsampled_lines[avg_pat][p] +
2545 avg_pat = tpg_pattern_avg(tpg, pat_line_new, pat_line_next_new);
2547 linestart_newer = tpg->downsampled_lines[avg_pat][p] +
2552 linestart_older += line_offset;
2553 linestart_newer += line_offset;
2555 if (tpg->field_alternate) {
2556 linestart_top = linestart_bottom = linestart_older;
2557 } else if (params->is_60hz) {
2558 linestart_top = linestart_newer;
2559 linestart_bottom = linestart_older;
2561 linestart_top = linestart_older;
2562 linestart_bottom = linestart_newer;
2565 switch (tpg->field) {
2566 case V4L2_FIELD_INTERLACED:
2567 case V4L2_FIELD_INTERLACED_TB:
2568 case V4L2_FIELD_SEQ_TB:
2569 case V4L2_FIELD_SEQ_BT:
2571 memcpy(vbuf, linestart_top, img_width);
2573 memcpy(vbuf, linestart_bottom, img_width);
2575 case V4L2_FIELD_INTERLACED_BT:
2577 memcpy(vbuf, linestart_bottom, img_width);
2579 memcpy(vbuf, linestart_top, img_width);
2581 case V4L2_FIELD_TOP:
2582 memcpy(vbuf, linestart_top, img_width);
2584 case V4L2_FIELD_BOTTOM:
2585 memcpy(vbuf, linestart_bottom, img_width);
2587 case V4L2_FIELD_NONE:
2589 memcpy(vbuf, linestart_older, img_width);
2594 void tpg_fill_plane_buffer(struct tpg_data *tpg, v4l2_std_id std,
2595 unsigned p, u8 *vbuf)
2597 struct tpg_draw_params params;
2598 unsigned factor = V4L2_FIELD_HAS_T_OR_B(tpg->field) ? 2 : 1;
2600 /* Coarse scaling with Bresenham */
2601 unsigned int_part = (tpg->crop.height / factor) / tpg->compose.height;
2602 unsigned fract_part = (tpg->crop.height / factor) % tpg->compose.height;
2610 params.is_60hz = std & V4L2_STD_525_60;
2611 params.twopixsize = tpg->twopixelsize[p];
2612 params.img_width = tpg_hdiv(tpg, p, tpg->compose.width);
2613 params.stride = tpg->bytesperline[p];
2614 params.hmax = (tpg->compose.height * tpg->perc_fill) / 100;
2616 tpg_fill_params_pattern(tpg, p, ¶ms);
2617 tpg_fill_params_extras(tpg, p, ¶ms);
2619 vbuf += tpg_hdiv(tpg, p, tpg->compose.left);
2621 for (h = 0; h < tpg->compose.height; h++) {
2624 params.frame_line = tpg_calc_frameline(tpg, src_y, tpg->field);
2625 params.frame_line_next = params.frame_line;
2626 buf_line = tpg_calc_buffer_line(tpg, h, tpg->field);
2628 error += fract_part;
2629 if (error >= tpg->compose.height) {
2630 error -= tpg->compose.height;
2635 * For line-interleaved formats determine the 'plane'
2636 * based on the buffer line.
2638 if (tpg_g_interleaved(tpg))
2639 p = tpg_g_interleaved_plane(tpg, buf_line);
2641 if (tpg->vdownsampling[p] > 1) {
2643 * When doing vertical downsampling the field setting
2644 * matters: for SEQ_BT/TB we downsample each field
2645 * separately (i.e. lines 0+2 are combined, as are
2646 * lines 1+3), for the other field settings we combine
2647 * odd and even lines. Doing that for SEQ_BT/TB would
2650 if (tpg->field == V4L2_FIELD_SEQ_BT ||
2651 tpg->field == V4L2_FIELD_SEQ_TB) {
2652 unsigned next_src_y = src_y;
2656 next_src_y += int_part;
2657 if (error + fract_part >= tpg->compose.height)
2659 params.frame_line_next =
2660 tpg_calc_frameline(tpg, next_src_y, tpg->field);
2664 params.frame_line_next =
2665 tpg_calc_frameline(tpg, src_y, tpg->field);
2668 buf_line /= tpg->vdownsampling[p];
2670 tpg_fill_plane_pattern(tpg, ¶ms, p, h,
2671 vbuf + buf_line * params.stride);
2672 tpg_fill_plane_extras(tpg, ¶ms, p, h,
2673 vbuf + buf_line * params.stride);
2676 EXPORT_SYMBOL_GPL(tpg_fill_plane_buffer);
2678 void tpg_fillbuffer(struct tpg_data *tpg, v4l2_std_id std, unsigned p, u8 *vbuf)
2680 unsigned offset = 0;
2683 if (tpg->buffers > 1) {
2684 tpg_fill_plane_buffer(tpg, std, p, vbuf);
2688 for (i = 0; i < tpg_g_planes(tpg); i++) {
2689 tpg_fill_plane_buffer(tpg, std, i, vbuf + offset);
2690 offset += tpg_calc_plane_size(tpg, i);
2693 EXPORT_SYMBOL_GPL(tpg_fillbuffer);
2695 MODULE_DESCRIPTION("V4L2 Test Pattern Generator");
2696 MODULE_AUTHOR("Hans Verkuil");
2697 MODULE_LICENSE("GPL");