Debian Codegraph @

utils/qvidcap/v4l2-tpg-core.c

raw
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * v4l2-tpg-core.c - Test Pattern Generator
4 *
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.
7 *
8 * Copyright 2014 Cisco Systems, Inc. and/or its affiliates. All rights reserved.
9 */
10
11 #include "compiler.h"
12 #include "v4l2-tpg-colors.h"
13
14 /* Must remain in sync with enum tpg_pattern */
15 const char * const tpg_pattern_strings[] = {
16 "75% Colorbar",
17 "100% Colorbar",
18 "CSC Colorbar",
19 "Horizontal 100% Colorbar",
20 "100% Color Squares",
21 "100% Black",
22 "100% White",
23 "100% Red",
24 "100% Green",
25 "100% Blue",
26 "16x16 Checkers",
27 "2x2 Checkers",
28 "1x1 Checkers",
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",
36 "Gray Ramp",
37 "Noise",
38 NULL
39 };
40
41 /* Must remain in sync with enum tpg_aspect */
42 const char * const tpg_aspect_strings[] = {
43 "Source Width x Height",
44 "4x3",
45 "14x9",
46 "16x9",
47 "16x9 Anamorphic",
48 NULL
49 };
50
51 /*
52 * Sine table: sin[0] = 127 * sin(-180 degrees)
53 * sin[128] = 127 * sin(0 degrees)
54 * sin[256] = 127 * sin(180 degrees)
55 */
56 static const s8 sin[257] = {
57 0, -4, -7, -11, -13, -18, -20, -22, -26, -29, -33, -35, -37, -41, -43, -48,
58 -50, -52, -56, -58, -62, -63, -65, -69, -71, -75, -76, -78, -82, -83, -87, -88,
59 -90, -93, -94, -97, -99, -101, -103, -104, -107, -108, -110, -111, -112, -114, -115, -117,
60 -118, -119, -120, -121, -122, -123, -123, -124, -125, -125, -126, -126, -127, -127, -127, -127,
61 -127, -127, -127, -127, -126, -126, -125, -125, -124, -124, -123, -122, -121, -120, -119, -118,
62 -117, -116, -114, -113, -111, -110, -109, -107, -105, -103, -101, -100, -97, -96, -93, -91,
63 -90, -87, -85, -82, -80, -76, -75, -73, -69, -67, -63, -62, -60, -56, -54, -50,
64 -48, -46, -41, -39, -35, -33, -31, -26, -24, -20, -18, -15, -11, -9, -4, -2,
65 0, 2, 4, 9, 11, 15, 18, 20, 24, 26, 31, 33, 35, 39, 41, 46,
66 48, 50, 54, 56, 60, 62, 64, 67, 69, 73, 75, 76, 80, 82, 85, 87,
67 90, 91, 93, 96, 97, 100, 101, 103, 105, 107, 109, 110, 111, 113, 114, 116,
68 117, 118, 119, 120, 121, 122, 123, 124, 124, 125, 125, 126, 126, 127, 127, 127,
69 127, 127, 127, 127, 127, 126, 126, 125, 125, 124, 123, 123, 122, 121, 120, 119,
70 118, 117, 115, 114, 112, 111, 110, 108, 107, 104, 103, 101, 99, 97, 94, 93,
71 90, 88, 87, 83, 82, 78, 76, 75, 71, 69, 65, 64, 62, 58, 56, 52,
72 50, 48, 43, 41, 37, 35, 33, 29, 26, 22, 20, 18, 13, 11, 7, 4,
73 0,
74 };
75
76 #define cos(idx) sin[((idx) + 64) % sizeof(sin)]
77
78 /* Global font descriptor */
79 static const u8 *font8x16;
80
81 void tpg_set_font(const u8 *f)
82 {
83 font8x16 = f;
84 }
85
86 void tpg_init(struct tpg_data *tpg, unsigned w, unsigned h)
87 {
88 memset(tpg, 0, sizeof(*tpg));
89 tpg->scaled_width = tpg->src_width = w;
90 tpg->src_height = tpg->buf_height = h;
91 tpg->crop.width = tpg->compose.width = w;
92 tpg->crop.height = tpg->compose.height = h;
93 tpg->recalc_colors = true;
94 tpg->recalc_square_border = true;
95 tpg->brightness = 128;
96 tpg->contrast = 128;
97 tpg->saturation = 128;
98 tpg->hue = 0;
99 tpg->mv_hor_mode = TPG_MOVE_NONE;
100 tpg->mv_vert_mode = TPG_MOVE_NONE;
101 tpg->field = V4L2_FIELD_NONE;
102 tpg_s_fourcc(tpg, V4L2_PIX_FMT_RGB24);
103 tpg->colorspace = V4L2_COLORSPACE_SRGB;
104 tpg->perc_fill = 100;
105 tpg->hsv_enc = V4L2_HSV_ENC_180;
106 }
107
108 int tpg_alloc(struct tpg_data *tpg, unsigned max_w)
109 {
110 unsigned pat;
111 unsigned plane;
112 int ret = 0;
113
114 tpg->max_line_width = max_w;
115 for (pat = 0; pat < TPG_MAX_PAT_LINES; pat++) {
116 for (plane = 0; plane < TPG_MAX_PLANES; plane++) {
117 unsigned pixelsz = plane ? 2 : 4;
118
119 tpg->lines[pat][plane] =
120 vzalloc(array3_size(max_w, 2, pixelsz));
121 if (!tpg->lines[pat][plane]) {
122 ret = -ENOMEM;
123 goto free_lines;
124 }
125 if (plane == 0)
126 continue;
127 tpg->downsampled_lines[pat][plane] =
128 vzalloc(array3_size(max_w, 2, pixelsz));
129 if (!tpg->downsampled_lines[pat][plane]) {
130 ret = -ENOMEM;
131 goto free_lines;
132 }
133 }
134 }
135 for (plane = 0; plane < TPG_MAX_PLANES; plane++) {
136 unsigned pixelsz = plane ? 2 : 4;
137
138 tpg->contrast_line[plane] =
139 vzalloc(array_size(pixelsz, max_w));
140 if (!tpg->contrast_line[plane]) {
141 ret = -ENOMEM;
142 goto free_contrast_line;
143 }
144 tpg->black_line[plane] =
145 vzalloc(array_size(pixelsz, max_w));
146 if (!tpg->black_line[plane]) {
147 ret = -ENOMEM;
148 goto free_contrast_line;
149 }
150 tpg->random_line[plane] =
151 vzalloc(array3_size(max_w, 2, pixelsz));
152 if (!tpg->random_line[plane]) {
153 ret = -ENOMEM;
154 goto free_contrast_line;
155 }
156 }
157 return 0;
158
159 free_contrast_line:
160 for (plane = 0; plane < TPG_MAX_PLANES; plane++) {
161 vfree(tpg->contrast_line[plane]);
162 vfree(tpg->black_line[plane]);
163 vfree(tpg->random_line[plane]);
164 tpg->contrast_line[plane] = NULL;
165 tpg->black_line[plane] = NULL;
166 tpg->random_line[plane] = NULL;
167 }
168 free_lines:
169 for (pat = 0; pat < TPG_MAX_PAT_LINES; pat++)
170 for (plane = 0; plane < TPG_MAX_PLANES; plane++) {
171 vfree(tpg->lines[pat][plane]);
172 tpg->lines[pat][plane] = NULL;
173 if (plane == 0)
174 continue;
175 vfree(tpg->downsampled_lines[pat][plane]);
176 tpg->downsampled_lines[pat][plane] = NULL;
177 }
178 return ret;
179 }
180
181 void tpg_free(struct tpg_data *tpg)
182 {
183 unsigned pat;
184 unsigned plane;
185
186 for (pat = 0; pat < TPG_MAX_PAT_LINES; pat++)
187 for (plane = 0; plane < TPG_MAX_PLANES; plane++) {
188 vfree(tpg->lines[pat][plane]);
189 tpg->lines[pat][plane] = NULL;
190 if (plane == 0)
191 continue;
192 vfree(tpg->downsampled_lines[pat][plane]);
193 tpg->downsampled_lines[pat][plane] = NULL;
194 }
195 for (plane = 0; plane < TPG_MAX_PLANES; plane++) {
196 vfree(tpg->contrast_line[plane]);
197 vfree(tpg->black_line[plane]);
198 vfree(tpg->random_line[plane]);
199 tpg->contrast_line[plane] = NULL;
200 tpg->black_line[plane] = NULL;
201 tpg->random_line[plane] = NULL;
202 }
203 }
204
205 bool tpg_s_fourcc(struct tpg_data *tpg, u32 fourcc)
206 {
207 tpg->fourcc = fourcc;
208 tpg->planes = 1;
209 tpg->buffers = 1;
210 tpg->recalc_colors = true;
211 tpg->interleaved = false;
212 tpg->vdownsampling[0] = 1;
213 tpg->hdownsampling[0] = 1;
214 tpg->hmask[0] = ~0;
215 tpg->hmask[1] = ~0;
216 tpg->hmask[2] = ~0;
217
218 switch (fourcc) {
219 case V4L2_PIX_FMT_SBGGR8:
220 case V4L2_PIX_FMT_SGBRG8:
221 case V4L2_PIX_FMT_SGRBG8:
222 case V4L2_PIX_FMT_SRGGB8:
223 case V4L2_PIX_FMT_SBGGR10:
224 case V4L2_PIX_FMT_SGBRG10:
225 case V4L2_PIX_FMT_SGRBG10:
226 case V4L2_PIX_FMT_SRGGB10:
227 case V4L2_PIX_FMT_SBGGR12:
228 case V4L2_PIX_FMT_SGBRG12:
229 case V4L2_PIX_FMT_SGRBG12:
230 case V4L2_PIX_FMT_SRGGB12:
231 case V4L2_PIX_FMT_SBGGR16:
232 case V4L2_PIX_FMT_SGBRG16:
233 case V4L2_PIX_FMT_SGRBG16:
234 case V4L2_PIX_FMT_SRGGB16:
235 tpg->interleaved = true;
236 tpg->vdownsampling[1] = 1;
237 tpg->hdownsampling[1] = 1;
238 tpg->planes = 2;
239 fallthrough;
240 case V4L2_PIX_FMT_RGB332:
241 case V4L2_PIX_FMT_RGB565:
242 case V4L2_PIX_FMT_RGB565X:
243 case V4L2_PIX_FMT_RGB444:
244 case V4L2_PIX_FMT_XRGB444:
245 case V4L2_PIX_FMT_ARGB444:
246 case V4L2_PIX_FMT_RGBX444:
247 case V4L2_PIX_FMT_RGBA444:
248 case V4L2_PIX_FMT_XBGR444:
249 case V4L2_PIX_FMT_ABGR444:
250 case V4L2_PIX_FMT_BGRX444:
251 case V4L2_PIX_FMT_BGRA444:
252 case V4L2_PIX_FMT_RGB555:
253 case V4L2_PIX_FMT_XRGB555:
254 case V4L2_PIX_FMT_ARGB555:
255 case V4L2_PIX_FMT_RGBX555:
256 case V4L2_PIX_FMT_RGBA555:
257 case V4L2_PIX_FMT_XBGR555:
258 case V4L2_PIX_FMT_ABGR555:
259 case V4L2_PIX_FMT_BGRX555:
260 case V4L2_PIX_FMT_BGRA555:
261 case V4L2_PIX_FMT_RGB555X:
262 case V4L2_PIX_FMT_XRGB555X:
263 case V4L2_PIX_FMT_ARGB555X:
264 case V4L2_PIX_FMT_BGR666:
265 case V4L2_PIX_FMT_RGB24:
266 case V4L2_PIX_FMT_BGR24:
267 case V4L2_PIX_FMT_RGB32:
268 case V4L2_PIX_FMT_BGR32:
269 case V4L2_PIX_FMT_XRGB32:
270 case V4L2_PIX_FMT_XBGR32:
271 case V4L2_PIX_FMT_ARGB32:
272 case V4L2_PIX_FMT_ABGR32:
273 case V4L2_PIX_FMT_RGBX32:
274 case V4L2_PIX_FMT_BGRX32:
275 case V4L2_PIX_FMT_RGBA32:
276 case V4L2_PIX_FMT_BGRA32:
277 tpg->color_enc = TGP_COLOR_ENC_RGB;
278 break;
279 case V4L2_PIX_FMT_GREY:
280 case V4L2_PIX_FMT_Y10:
281 case V4L2_PIX_FMT_Y12:
282 case V4L2_PIX_FMT_Y16:
283 case V4L2_PIX_FMT_Y16_BE:
284 case V4L2_PIX_FMT_Z16:
285 tpg->color_enc = TGP_COLOR_ENC_LUMA;
286 break;
287 case V4L2_PIX_FMT_YUV444:
288 case V4L2_PIX_FMT_YUV555:
289 case V4L2_PIX_FMT_YUV565:
290 case V4L2_PIX_FMT_YUV32:
291 case V4L2_PIX_FMT_AYUV32:
292 case V4L2_PIX_FMT_XYUV32:
293 case V4L2_PIX_FMT_VUYA32:
294 case V4L2_PIX_FMT_VUYX32:
295 case V4L2_PIX_FMT_YUVA32:
296 case V4L2_PIX_FMT_YUVX32:
297 tpg->color_enc = TGP_COLOR_ENC_YCBCR;
298 break;
299 case V4L2_PIX_FMT_YUV420M:
300 case V4L2_PIX_FMT_YVU420M:
301 tpg->buffers = 3;
302 fallthrough;
303 case V4L2_PIX_FMT_YUV420:
304 case V4L2_PIX_FMT_YVU420:
305 tpg->vdownsampling[1] = 2;
306 tpg->vdownsampling[2] = 2;
307 tpg->hdownsampling[1] = 2;
308 tpg->hdownsampling[2] = 2;
309 tpg->planes = 3;
310 tpg->color_enc = TGP_COLOR_ENC_YCBCR;
311 break;
312 case V4L2_PIX_FMT_YUV422M:
313 case V4L2_PIX_FMT_YVU422M:
314 tpg->buffers = 3;
315 fallthrough;
316 case V4L2_PIX_FMT_YUV422P:
317 tpg->vdownsampling[1] = 1;
318 tpg->vdownsampling[2] = 1;
319 tpg->hdownsampling[1] = 2;
320 tpg->hdownsampling[2] = 2;
321 tpg->planes = 3;
322 tpg->color_enc = TGP_COLOR_ENC_YCBCR;
323 break;
324 case V4L2_PIX_FMT_NV16M:
325 case V4L2_PIX_FMT_NV61M:
326 tpg->buffers = 2;
327 fallthrough;
328 case V4L2_PIX_FMT_NV16:
329 case V4L2_PIX_FMT_NV61:
330 tpg->vdownsampling[1] = 1;
331 tpg->hdownsampling[1] = 1;
332 tpg->hmask[1] = ~1;
333 tpg->planes = 2;
334 tpg->color_enc = TGP_COLOR_ENC_YCBCR;
335 break;
336 case V4L2_PIX_FMT_NV12M:
337 case V4L2_PIX_FMT_NV21M:
338 tpg->buffers = 2;
339 fallthrough;
340 case V4L2_PIX_FMT_NV12:
341 case V4L2_PIX_FMT_NV21:
342 tpg->vdownsampling[1] = 2;
343 tpg->hdownsampling[1] = 1;
344 tpg->hmask[1] = ~1;
345 tpg->planes = 2;
346 tpg->color_enc = TGP_COLOR_ENC_YCBCR;
347 break;
348 case V4L2_PIX_FMT_YUV444M:
349 case V4L2_PIX_FMT_YVU444M:
350 tpg->buffers = 3;
351 tpg->planes = 3;
352 tpg->vdownsampling[1] = 1;
353 tpg->vdownsampling[2] = 1;
354 tpg->hdownsampling[1] = 1;
355 tpg->hdownsampling[2] = 1;
356 tpg->color_enc = TGP_COLOR_ENC_YCBCR;
357 break;
358 case V4L2_PIX_FMT_NV24:
359 case V4L2_PIX_FMT_NV42:
360 tpg->vdownsampling[1] = 1;
361 tpg->hdownsampling[1] = 1;
362 tpg->planes = 2;
363 tpg->color_enc = TGP_COLOR_ENC_YCBCR;
364 break;
365 case V4L2_PIX_FMT_YUYV:
366 case V4L2_PIX_FMT_UYVY:
367 case V4L2_PIX_FMT_YVYU:
368 case V4L2_PIX_FMT_VYUY:
369 tpg->hmask[0] = ~1;
370 tpg->color_enc = TGP_COLOR_ENC_YCBCR;
371 break;
372 case V4L2_PIX_FMT_HSV24:
373 case V4L2_PIX_FMT_HSV32:
374 tpg->color_enc = TGP_COLOR_ENC_HSV;
375 break;
376 default:
377 return false;
378 }
379
380 switch (fourcc) {
381 case V4L2_PIX_FMT_GREY:
382 case V4L2_PIX_FMT_RGB332:
383 tpg->twopixelsize[0] = 2;
384 break;
385 case V4L2_PIX_FMT_RGB565:
386 case V4L2_PIX_FMT_RGB565X:
387 case V4L2_PIX_FMT_RGB444:
388 case V4L2_PIX_FMT_XRGB444:
389 case V4L2_PIX_FMT_ARGB444:
390 case V4L2_PIX_FMT_RGBX444:
391 case V4L2_PIX_FMT_RGBA444:
392 case V4L2_PIX_FMT_XBGR444:
393 case V4L2_PIX_FMT_ABGR444:
394 case V4L2_PIX_FMT_BGRX444:
395 case V4L2_PIX_FMT_BGRA444:
396 case V4L2_PIX_FMT_RGB555:
397 case V4L2_PIX_FMT_XRGB555:
398 case V4L2_PIX_FMT_ARGB555:
399 case V4L2_PIX_FMT_RGBX555:
400 case V4L2_PIX_FMT_RGBA555:
401 case V4L2_PIX_FMT_XBGR555:
402 case V4L2_PIX_FMT_ABGR555:
403 case V4L2_PIX_FMT_BGRX555:
404 case V4L2_PIX_FMT_BGRA555:
405 case V4L2_PIX_FMT_RGB555X:
406 case V4L2_PIX_FMT_XRGB555X:
407 case V4L2_PIX_FMT_ARGB555X:
408 case V4L2_PIX_FMT_YUYV:
409 case V4L2_PIX_FMT_UYVY:
410 case V4L2_PIX_FMT_YVYU:
411 case V4L2_PIX_FMT_VYUY:
412 case V4L2_PIX_FMT_YUV444:
413 case V4L2_PIX_FMT_YUV555:
414 case V4L2_PIX_FMT_YUV565:
415 case V4L2_PIX_FMT_Y10:
416 case V4L2_PIX_FMT_Y12:
417 case V4L2_PIX_FMT_Y16:
418 case V4L2_PIX_FMT_Y16_BE:
419 case V4L2_PIX_FMT_Z16:
420 tpg->twopixelsize[0] = 2 * 2;
421 break;
422 case V4L2_PIX_FMT_RGB24:
423 case V4L2_PIX_FMT_BGR24:
424 case V4L2_PIX_FMT_HSV24:
425 tpg->twopixelsize[0] = 2 * 3;
426 break;
427 case V4L2_PIX_FMT_BGR666:
428 case V4L2_PIX_FMT_RGB32:
429 case V4L2_PIX_FMT_BGR32:
430 case V4L2_PIX_FMT_XRGB32:
431 case V4L2_PIX_FMT_XBGR32:
432 case V4L2_PIX_FMT_ARGB32:
433 case V4L2_PIX_FMT_ABGR32:
434 case V4L2_PIX_FMT_RGBX32:
435 case V4L2_PIX_FMT_BGRX32:
436 case V4L2_PIX_FMT_RGBA32:
437 case V4L2_PIX_FMT_BGRA32:
438 case V4L2_PIX_FMT_YUV32:
439 case V4L2_PIX_FMT_AYUV32:
440 case V4L2_PIX_FMT_XYUV32:
441 case V4L2_PIX_FMT_VUYA32:
442 case V4L2_PIX_FMT_VUYX32:
443 case V4L2_PIX_FMT_YUVA32:
444 case V4L2_PIX_FMT_YUVX32:
445 case V4L2_PIX_FMT_HSV32:
446 tpg->twopixelsize[0] = 2 * 4;
447 break;
448 case V4L2_PIX_FMT_NV12:
449 case V4L2_PIX_FMT_NV21:
450 case V4L2_PIX_FMT_NV12M:
451 case V4L2_PIX_FMT_NV21M:
452 case V4L2_PIX_FMT_NV16:
453 case V4L2_PIX_FMT_NV61:
454 case V4L2_PIX_FMT_NV16M:
455 case V4L2_PIX_FMT_NV61M:
456 case V4L2_PIX_FMT_SBGGR8:
457 case V4L2_PIX_FMT_SGBRG8:
458 case V4L2_PIX_FMT_SGRBG8:
459 case V4L2_PIX_FMT_SRGGB8:
460 tpg->twopixelsize[0] = 2;
461 tpg->twopixelsize[1] = 2;
462 break;
463 case V4L2_PIX_FMT_SRGGB10:
464 case V4L2_PIX_FMT_SGRBG10:
465 case V4L2_PIX_FMT_SGBRG10:
466 case V4L2_PIX_FMT_SBGGR10:
467 case V4L2_PIX_FMT_SRGGB12:
468 case V4L2_PIX_FMT_SGRBG12:
469 case V4L2_PIX_FMT_SGBRG12:
470 case V4L2_PIX_FMT_SBGGR12:
471 case V4L2_PIX_FMT_SRGGB16:
472 case V4L2_PIX_FMT_SGRBG16:
473 case V4L2_PIX_FMT_SGBRG16:
474 case V4L2_PIX_FMT_SBGGR16:
475 tpg->twopixelsize[0] = 4;
476 tpg->twopixelsize[1] = 4;
477 break;
478 case V4L2_PIX_FMT_YUV444M:
479 case V4L2_PIX_FMT_YVU444M:
480 case V4L2_PIX_FMT_YUV422M:
481 case V4L2_PIX_FMT_YVU422M:
482 case V4L2_PIX_FMT_YUV422P:
483 case V4L2_PIX_FMT_YUV420:
484 case V4L2_PIX_FMT_YVU420:
485 case V4L2_PIX_FMT_YUV420M:
486 case V4L2_PIX_FMT_YVU420M:
487 tpg->twopixelsize[0] = 2;
488 tpg->twopixelsize[1] = 2;
489 tpg->twopixelsize[2] = 2;
490 break;
491 case V4L2_PIX_FMT_NV24:
492 case V4L2_PIX_FMT_NV42:
493 tpg->twopixelsize[0] = 2;
494 tpg->twopixelsize[1] = 4;
495 break;
496 }
497 return true;
498 }
499
500 void tpg_s_crop_compose(struct tpg_data *tpg, const struct v4l2_rect *crop,
501 const struct v4l2_rect *compose)
502 {
503 tpg->crop = *crop;
504 tpg->compose = *compose;
505 tpg->scaled_width = (tpg->src_width * tpg->compose.width +
506 tpg->crop.width - 1) / tpg->crop.width;
507 tpg->scaled_width &= ~1;
508 if (tpg->scaled_width > tpg->max_line_width)
509 tpg->scaled_width = tpg->max_line_width;
510 if (tpg->scaled_width < 2)
511 tpg->scaled_width = 2;
512 tpg->recalc_lines = true;
513 }
514
515 void tpg_reset_source(struct tpg_data *tpg, unsigned width, unsigned height,
516 u32 field)
517 {
518 unsigned p;
519
520 tpg->src_width = width;
521 tpg->src_height = height;
522 tpg->field = field;
523 tpg->buf_height = height;
524 if (V4L2_FIELD_HAS_T_OR_B(field))
525 tpg->buf_height /= 2;
526 tpg->scaled_width = width;
527 tpg->crop.top = tpg->crop.left = 0;
528 tpg->crop.width = width;
529 tpg->crop.height = height;
530 tpg->compose.top = tpg->compose.left = 0;
531 tpg->compose.width = width;
532 tpg->compose.height = tpg->buf_height;
533 for (p = 0; p < tpg->planes; p++)
534 tpg->bytesperline[p] = (width * tpg->twopixelsize[p]) /
535 (2 * tpg->hdownsampling[p]);
536 tpg->recalc_square_border = true;
537 }
538
539 static enum tpg_color tpg_get_textbg_color(struct tpg_data *tpg)
540 {
541 switch (tpg->pattern) {
542 case TPG_PAT_BLACK:
543 return TPG_COLOR_100_WHITE;
544 case TPG_PAT_CSC_COLORBAR:
545 return TPG_COLOR_CSC_BLACK;
546 default:
547 return TPG_COLOR_100_BLACK;
548 }
549 }
550
551 static enum tpg_color tpg_get_textfg_color(struct tpg_data *tpg)
552 {
553 switch (tpg->pattern) {
554 case TPG_PAT_75_COLORBAR:
555 case TPG_PAT_CSC_COLORBAR:
556 return TPG_COLOR_CSC_WHITE;
557 case TPG_PAT_BLACK:
558 return TPG_COLOR_100_BLACK;
559 default:
560 return TPG_COLOR_100_WHITE;
561 }
562 }
563
564 static inline int rec709_to_linear(int v)
565 {
566 v = clamp(v, 0, 0xff0);
567 return tpg_rec709_to_linear[v];
568 }
569
570 static inline int linear_to_rec709(int v)
571 {
572 v = clamp(v, 0, 0xff0);
573 return tpg_linear_to_rec709[v];
574 }
575
576 static void color_to_hsv(struct tpg_data *tpg, int r, int g, int b,
577 int *h, int *s, int *v)
578 {
579 int max_rgb, min_rgb, diff_rgb;
580 int aux;
581 int third;
582 int third_size;
583
584 r >>= 4;
585 g >>= 4;
586 b >>= 4;
587
588 /* Value */
589 max_rgb = max3(r, g, b);
590 *v = max_rgb;
591 if (!max_rgb) {
592 *h = 0;
593 *s = 0;
594 return;
595 }
596
597 /* Saturation */
598 min_rgb = min3(r, g, b);
599 diff_rgb = max_rgb - min_rgb;
600 aux = 255 * diff_rgb;
601 aux += max_rgb / 2;
602 aux /= max_rgb;
603 *s = aux;
604 if (!aux) {
605 *h = 0;
606 return;
607 }
608
609 third_size = (tpg->real_hsv_enc == V4L2_HSV_ENC_180) ? 60 : 85;
610
611 /* Hue */
612 if (max_rgb == r) {
613 aux = g - b;
614 third = 0;
615 } else if (max_rgb == g) {
616 aux = b - r;
617 third = third_size;
618 } else {
619 aux = r - g;
620 third = third_size * 2;
621 }
622
623 aux *= third_size / 2;
624 aux += diff_rgb / 2;
625 aux /= diff_rgb;
626 aux += third;
627
628 /* Clamp Hue */
629 if (tpg->real_hsv_enc == V4L2_HSV_ENC_180) {
630 if (aux < 0)
631 aux += 180;
632 else if (aux > 180)
633 aux -= 180;
634 } else {
635 aux = aux & 0xff;
636 }
637
638 *h = aux;
639 }
640
641 static void rgb2ycbcr(const int m[3][3], int r, int g, int b,
642 int y_offset, int *y, int *cb, int *cr)
643 {
644 *y = ((m[0][0] * r + m[0][1] * g + m[0][2] * b) >> 16) + (y_offset << 4);
645 *cb = ((m[1][0] * r + m[1][1] * g + m[1][2] * b) >> 16) + (128 << 4);
646 *cr = ((m[2][0] * r + m[2][1] * g + m[2][2] * b) >> 16) + (128 << 4);
647 }
648
649 static void color_to_ycbcr(struct tpg_data *tpg, int r, int g, int b,
650 int *y, int *cb, int *cr)
651 {
652 #define COEFF(v, r) ((int)(0.5 + (v) * (r) * 256.0))
653
654 static const int bt601[3][3] = {
655 { COEFF(0.299, 219), COEFF(0.587, 219), COEFF(0.114, 219) },
656 { COEFF(-0.1687, 224), COEFF(-0.3313, 224), COEFF(0.5, 224) },
657 { COEFF(0.5, 224), COEFF(-0.4187, 224), COEFF(-0.0813, 224) },
658 };
659 static const int bt601_full[3][3] = {
660 { COEFF(0.299, 255), COEFF(0.587, 255), COEFF(0.114, 255) },
661 { COEFF(-0.1687, 255), COEFF(-0.3313, 255), COEFF(0.5, 255) },
662 { COEFF(0.5, 255), COEFF(-0.4187, 255), COEFF(-0.0813, 255) },
663 };
664 static const int rec709[3][3] = {
665 { COEFF(0.2126, 219), COEFF(0.7152, 219), COEFF(0.0722, 219) },
666 { COEFF(-0.1146, 224), COEFF(-0.3854, 224), COEFF(0.5, 224) },
667 { COEFF(0.5, 224), COEFF(-0.4542, 224), COEFF(-0.0458, 224) },
668 };
669 static const int rec709_full[3][3] = {
670 { COEFF(0.2126, 255), COEFF(0.7152, 255), COEFF(0.0722, 255) },
671 { COEFF(-0.1146, 255), COEFF(-0.3854, 255), COEFF(0.5, 255) },
672 { COEFF(0.5, 255), COEFF(-0.4542, 255), COEFF(-0.0458, 255) },
673 };
674 static const int smpte240m[3][3] = {
675 { COEFF(0.212, 219), COEFF(0.701, 219), COEFF(0.087, 219) },
676 { COEFF(-0.116, 224), COEFF(-0.384, 224), COEFF(0.5, 224) },
677 { COEFF(0.5, 224), COEFF(-0.445, 224), COEFF(-0.055, 224) },
678 };
679 static const int smpte240m_full[3][3] = {
680 { COEFF(0.212, 255), COEFF(0.701, 255), COEFF(0.087, 255) },
681 { COEFF(-0.116, 255), COEFF(-0.384, 255), COEFF(0.5, 255) },
682 { COEFF(0.5, 255), COEFF(-0.445, 255), COEFF(-0.055, 255) },
683 };
684 static const int bt2020[3][3] = {
685 { COEFF(0.2627, 219), COEFF(0.6780, 219), COEFF(0.0593, 219) },
686 { COEFF(-0.1396, 224), COEFF(-0.3604, 224), COEFF(0.5, 224) },
687 { COEFF(0.5, 224), COEFF(-0.4598, 224), COEFF(-0.0402, 224) },
688 };
689 static const int bt2020_full[3][3] = {
690 { COEFF(0.2627, 255), COEFF(0.6780, 255), COEFF(0.0593, 255) },
691 { COEFF(-0.1396, 255), COEFF(-0.3604, 255), COEFF(0.5, 255) },
692 { COEFF(0.5, 255), COEFF(-0.4598, 255), COEFF(-0.0402, 255) },
693 };
694 static const int bt2020c[4] = {
695 COEFF(1.0 / 1.9404, 224), COEFF(1.0 / 1.5816, 224),
696 COEFF(1.0 / 1.7184, 224), COEFF(1.0 / 0.9936, 224),
697 };
698 static const int bt2020c_full[4] = {
699 COEFF(1.0 / 1.9404, 255), COEFF(1.0 / 1.5816, 255),
700 COEFF(1.0 / 1.7184, 255), COEFF(1.0 / 0.9936, 255),
701 };
702
703 bool full = tpg->real_quantization == V4L2_QUANTIZATION_FULL_RANGE;
704 unsigned y_offset = full ? 0 : 16;
705 int lin_y, yc;
706
707 switch (tpg->real_ycbcr_enc) {
708 case V4L2_YCBCR_ENC_601:
709 rgb2ycbcr(full ? bt601_full : bt601, r, g, b, y_offset, y, cb, cr);
710 break;
711 case V4L2_YCBCR_ENC_XV601:
712 /* Ignore quantization range, there is only one possible
713 * Y'CbCr encoding. */
714 rgb2ycbcr(bt601, r, g, b, 16, y, cb, cr);
715 break;
716 case V4L2_YCBCR_ENC_XV709:
717 /* Ignore quantization range, there is only one possible
718 * Y'CbCr encoding. */
719 rgb2ycbcr(rec709, r, g, b, 16, y, cb, cr);
720 break;
721 case V4L2_YCBCR_ENC_BT2020:
722 rgb2ycbcr(full ? bt2020_full : bt2020, r, g, b, y_offset, y, cb, cr);
723 break;
724 case V4L2_YCBCR_ENC_BT2020_CONST_LUM:
725 lin_y = (COEFF(0.2627, 255) * rec709_to_linear(r) +
726 COEFF(0.6780, 255) * rec709_to_linear(g) +
727 COEFF(0.0593, 255) * rec709_to_linear(b)) >> 16;
728 yc = linear_to_rec709(lin_y);
729 *y = full ? yc : (yc * 219) / 255 + (16 << 4);
730 if (b <= yc)
731 *cb = (((b - yc) * (full ? bt2020c_full[0] : bt2020c[0])) >> 16) + (128 << 4);
732 else
733 *cb = (((b - yc) * (full ? bt2020c_full[1] : bt2020c[1])) >> 16) + (128 << 4);
734 if (r <= yc)
735 *cr = (((r - yc) * (full ? bt2020c_full[2] : bt2020c[2])) >> 16) + (128 << 4);
736 else
737 *cr = (((r - yc) * (full ? bt2020c_full[3] : bt2020c[3])) >> 16) + (128 << 4);
738 break;
739 case V4L2_YCBCR_ENC_SMPTE240M:
740 rgb2ycbcr(full ? smpte240m_full : smpte240m, r, g, b, y_offset, y, cb, cr);
741 break;
742 case V4L2_YCBCR_ENC_709:
743 default:
744 rgb2ycbcr(full ? rec709_full : rec709, r, g, b, y_offset, y, cb, cr);
745 break;
746 }
747 }
748
749 static void ycbcr2rgb(const int m[3][3], int y, int cb, int cr,
750 int y_offset, int *r, int *g, int *b)
751 {
752 y -= y_offset << 4;
753 cb -= 128 << 4;
754 cr -= 128 << 4;
755 *r = m[0][0] * y + m[0][1] * cb + m[0][2] * cr;
756 *g = m[1][0] * y + m[1][1] * cb + m[1][2] * cr;
757 *b = m[2][0] * y + m[2][1] * cb + m[2][2] * cr;
758 *r = clamp(*r >> 12, 0, 0xff0);
759 *g = clamp(*g >> 12, 0, 0xff0);
760 *b = clamp(*b >> 12, 0, 0xff0);
761 }
762
763 static void ycbcr_to_color(struct tpg_data *tpg, int y, int cb, int cr,
764 int *r, int *g, int *b)
765 {
766 #undef COEFF
767 #define COEFF(v, r) ((int)(0.5 + (v) * ((255.0 * 255.0 * 16.0) / (r))))
768 static const int bt601[3][3] = {
769 { COEFF(1, 219), COEFF(0, 224), COEFF(1.4020, 224) },
770 { COEFF(1, 219), COEFF(-0.3441, 224), COEFF(-0.7141, 224) },
771 { COEFF(1, 219), COEFF(1.7720, 224), COEFF(0, 224) },
772 };
773 static const int bt601_full[3][3] = {
774 { COEFF(1, 255), COEFF(0, 255), COEFF(1.4020, 255) },
775 { COEFF(1, 255), COEFF(-0.3441, 255), COEFF(-0.7141, 255) },
776 { COEFF(1, 255), COEFF(1.7720, 255), COEFF(0, 255) },
777 };
778 static const int rec709[3][3] = {
779 { COEFF(1, 219), COEFF(0, 224), COEFF(1.5748, 224) },
780 { COEFF(1, 219), COEFF(-0.1873, 224), COEFF(-0.4681, 224) },
781 { COEFF(1, 219), COEFF(1.8556, 224), COEFF(0, 224) },
782 };
783 static const int rec709_full[3][3] = {
784 { COEFF(1, 255), COEFF(0, 255), COEFF(1.5748, 255) },
785 { COEFF(1, 255), COEFF(-0.1873, 255), COEFF(-0.4681, 255) },
786 { COEFF(1, 255), COEFF(1.8556, 255), COEFF(0, 255) },
787 };
788 static const int smpte240m[3][3] = {
789 { COEFF(1, 219), COEFF(0, 224), COEFF(1.5756, 224) },
790 { COEFF(1, 219), COEFF(-0.2253, 224), COEFF(-0.4767, 224) },
791 { COEFF(1, 219), COEFF(1.8270, 224), COEFF(0, 224) },
792 };
793 static const int smpte240m_full[3][3] = {
794 { COEFF(1, 255), COEFF(0, 255), COEFF(1.5756, 255) },
795 { COEFF(1, 255), COEFF(-0.2253, 255), COEFF(-0.4767, 255) },
796 { COEFF(1, 255), COEFF(1.8270, 255), COEFF(0, 255) },
797 };
798 static const int bt2020[3][3] = {
799 { COEFF(1, 219), COEFF(0, 224), COEFF(1.4746, 224) },
800 { COEFF(1, 219), COEFF(-0.1646, 224), COEFF(-0.5714, 224) },
801 { COEFF(1, 219), COEFF(1.8814, 224), COEFF(0, 224) },
802 };
803 static const int bt2020_full[3][3] = {
804 { COEFF(1, 255), COEFF(0, 255), COEFF(1.4746, 255) },
805 { COEFF(1, 255), COEFF(-0.1646, 255), COEFF(-0.5714, 255) },
806 { COEFF(1, 255), COEFF(1.8814, 255), COEFF(0, 255) },
807 };
808 static const int bt2020c[4] = {
809 COEFF(1.9404, 224), COEFF(1.5816, 224),
810 COEFF(1.7184, 224), COEFF(0.9936, 224),
811 };
812 static const int bt2020c_full[4] = {
813 COEFF(1.9404, 255), COEFF(1.5816, 255),
814 COEFF(1.7184, 255), COEFF(0.9936, 255),
815 };
816
817 bool full = tpg->real_quantization == V4L2_QUANTIZATION_FULL_RANGE;
818 unsigned y_offset = full ? 0 : 16;
819 int y_fac = full ? COEFF(1.0, 255) : COEFF(1.0, 219);
820 int lin_r, lin_g, lin_b, lin_y;
821
822 switch (tpg->real_ycbcr_enc) {
823 case V4L2_YCBCR_ENC_601:
824 ycbcr2rgb(full ? bt601_full : bt601, y, cb, cr, y_offset, r, g, b);
825 break;
826 case V4L2_YCBCR_ENC_XV601:
827 /* Ignore quantization range, there is only one possible
828 * Y'CbCr encoding. */
829 ycbcr2rgb(bt601, y, cb, cr, 16, r, g, b);
830 break;
831 case V4L2_YCBCR_ENC_XV709:
832 /* Ignore quantization range, there is only one possible
833 * Y'CbCr encoding. */
834 ycbcr2rgb(rec709, y, cb, cr, 16, r, g, b);
835 break;
836 case V4L2_YCBCR_ENC_BT2020:
837 ycbcr2rgb(full ? bt2020_full : bt2020, y, cb, cr, y_offset, r, g, b);
838 break;
839 case V4L2_YCBCR_ENC_BT2020_CONST_LUM:
840 y -= full ? 0 : 16 << 4;
841 cb -= 128 << 4;
842 cr -= 128 << 4;
843
844 if (cb <= 0)
845 *b = y_fac * y + (full ? bt2020c_full[0] : bt2020c[0]) * cb;
846 else
847 *b = y_fac * y + (full ? bt2020c_full[1] : bt2020c[1]) * cb;
848 *b = *b >> 12;
849 if (cr <= 0)
850 *r = y_fac * y + (full ? bt2020c_full[2] : bt2020c[2]) * cr;
851 else
852 *r = y_fac * y + (full ? bt2020c_full[3] : bt2020c[3]) * cr;
853 *r = *r >> 12;
854 lin_r = rec709_to_linear(*r);
855 lin_b = rec709_to_linear(*b);
856 lin_y = rec709_to_linear((y * 255) / (full ? 255 : 219));
857
858 lin_g = COEFF(1.0 / 0.6780, 255) * lin_y -
859 COEFF(0.2627 / 0.6780, 255) * lin_r -
860 COEFF(0.0593 / 0.6780, 255) * lin_b;
861 *g = linear_to_rec709(lin_g >> 12);
862 break;
863 case V4L2_YCBCR_ENC_SMPTE240M:
864 ycbcr2rgb(full ? smpte240m_full : smpte240m, y, cb, cr, y_offset, r, g, b);
865 break;
866 case V4L2_YCBCR_ENC_709:
867 default:
868 ycbcr2rgb(full ? rec709_full : rec709, y, cb, cr, y_offset, r, g, b);
869 break;
870 }
871 }
872
873 /* precalculate color bar values to speed up rendering */
874 static void precalculate_color(struct tpg_data *tpg, int k)
875 {
876 int col = k;
877 int r = tpg_colors[col].r;
878 int g = tpg_colors[col].g;
879 int b = tpg_colors[col].b;
880 int y, cb, cr;
881 bool ycbcr_valid = false;
882
883 if (k == TPG_COLOR_TEXTBG) {
884 col = tpg_get_textbg_color(tpg);
885
886 r = tpg_colors[col].r;
887 g = tpg_colors[col].g;
888 b = tpg_colors[col].b;
889 } else if (k == TPG_COLOR_TEXTFG) {
890 col = tpg_get_textfg_color(tpg);
891
892 r = tpg_colors[col].r;
893 g = tpg_colors[col].g;
894 b = tpg_colors[col].b;
895 } else if (tpg->pattern == TPG_PAT_NOISE) {
896 r = g = b = get_random_u8();
897 } else if (k == TPG_COLOR_RANDOM) {
898 r = g = b = tpg->qual_offset + get_random_u32_below(196);
899 } else if (k >= TPG_COLOR_RAMP) {
900 r = g = b = k - TPG_COLOR_RAMP;
901 }
902
903 if (tpg->pattern == TPG_PAT_CSC_COLORBAR && col <= TPG_COLOR_CSC_BLACK) {
904 r = tpg_csc_colors[tpg->colorspace][tpg->real_xfer_func][col].r;
905 g = tpg_csc_colors[tpg->colorspace][tpg->real_xfer_func][col].g;
906 b = tpg_csc_colors[tpg->colorspace][tpg->real_xfer_func][col].b;
907 } else {
908 r <<= 4;
909 g <<= 4;
910 b <<= 4;
911 }
912
913 if (tpg->qual == TPG_QUAL_GRAY ||
914 tpg->color_enc == TGP_COLOR_ENC_LUMA) {
915 /* Rec. 709 Luma function */
916 /* (0.2126, 0.7152, 0.0722) * (255 * 256) */
917 r = g = b = (13879 * r + 46688 * g + 4713 * b) >> 16;
918 }
919
920 /*
921 * The assumption is that the RGB output is always full range,
922 * so only if the rgb_range overrides the 'real' rgb range do
923 * we need to convert the RGB values.
924 *
925 * Remember that r, g and b are still in the 0 - 0xff0 range.
926 */
927 if (tpg->real_rgb_range == V4L2_DV_RGB_RANGE_LIMITED &&
928 tpg->rgb_range == V4L2_DV_RGB_RANGE_FULL &&
929 tpg->color_enc == TGP_COLOR_ENC_RGB) {
930 /*
931 * Convert from full range (which is what r, g and b are)
932 * to limited range (which is the 'real' RGB range), which
933 * is then interpreted as full range.
934 */
935 r = (r * 219) / 255 + (16 << 4);
936 g = (g * 219) / 255 + (16 << 4);
937 b = (b * 219) / 255 + (16 << 4);
938 } else if (tpg->real_rgb_range != V4L2_DV_RGB_RANGE_LIMITED &&
939 tpg->rgb_range == V4L2_DV_RGB_RANGE_LIMITED &&
940 tpg->color_enc == TGP_COLOR_ENC_RGB) {
941
942 /*
943 * Clamp r, g and b to the limited range and convert to full
944 * range since that's what we deliver.
945 */
946 r = clamp(r, 16 << 4, 235 << 4);
947 g = clamp(g, 16 << 4, 235 << 4);
948 b = clamp(b, 16 << 4, 235 << 4);
949 r = (r - (16 << 4)) * 255 / 219;
950 g = (g - (16 << 4)) * 255 / 219;
951 b = (b - (16 << 4)) * 255 / 219;
952 }
953
954 if ((tpg->brightness != 128 || tpg->contrast != 128 ||
955 tpg->saturation != 128 || tpg->hue) &&
956 tpg->color_enc != TGP_COLOR_ENC_LUMA) {
957 /* Implement these operations */
958 int tmp_cb, tmp_cr;
959
960 /* First convert to YCbCr */
961
962 color_to_ycbcr(tpg, r, g, b, &y, &cb, &cr);
963
964 y = (16 << 4) + ((y - (16 << 4)) * tpg->contrast) / 128;
965 y += (tpg->brightness << 4) - (128 << 4);
966
967 cb -= 128 << 4;
968 cr -= 128 << 4;
969 tmp_cb = (cb * cos(128 + tpg->hue)) / 127 + (cr * sin[128 + tpg->hue]) / 127;
970 tmp_cr = (cr * cos(128 + tpg->hue)) / 127 - (cb * sin[128 + tpg->hue]) / 127;
971
972 cb = (128 << 4) + (tmp_cb * tpg->contrast * tpg->saturation) / (128 * 128);
973 cr = (128 << 4) + (tmp_cr * tpg->contrast * tpg->saturation) / (128 * 128);
974 if (tpg->color_enc == TGP_COLOR_ENC_YCBCR)
975 ycbcr_valid = true;
976 else
977 ycbcr_to_color(tpg, y, cb, cr, &r, &g, &b);
978 } else if ((tpg->brightness != 128 || tpg->contrast != 128) &&
979 tpg->color_enc == TGP_COLOR_ENC_LUMA) {
980 r = (16 << 4) + ((r - (16 << 4)) * tpg->contrast) / 128;
981 r += (tpg->brightness << 4) - (128 << 4);
982 }
983
984 switch (tpg->color_enc) {
985 case TGP_COLOR_ENC_HSV:
986 {
987 int h, s, v;
988
989 color_to_hsv(tpg, r, g, b, &h, &s, &v);
990 tpg->colors[k][0] = h;
991 tpg->colors[k][1] = s;
992 tpg->colors[k][2] = v;
993 break;
994 }
995 case TGP_COLOR_ENC_YCBCR:
996 {
997 /* Convert to YCbCr */
998 if (!ycbcr_valid)
999 color_to_ycbcr(tpg, r, g, b, &y, &cb, &cr);
1000
1001 y >>= 4;
1002 cb >>= 4;
1003 cr >>= 4;
1004 /*
1005 * XV601/709 use the header/footer margins to encode R', G'
1006 * and B' values outside the range [0-1]. So do not clamp
1007 * XV601/709 values.
1008 */
1009 if (tpg->real_quantization == V4L2_QUANTIZATION_LIM_RANGE &&
1010 tpg->real_ycbcr_enc != V4L2_YCBCR_ENC_XV601 &&
1011 tpg->real_ycbcr_enc != V4L2_YCBCR_ENC_XV709) {
1012 y = clamp(y, 16, 235);
1013 cb = clamp(cb, 16, 240);
1014 cr = clamp(cr, 16, 240);
1015 } else {
1016 y = clamp(y, 1, 254);
1017 cb = clamp(cb, 1, 254);
1018 cr = clamp(cr, 1, 254);
1019 }
1020 switch (tpg->fourcc) {
1021 case V4L2_PIX_FMT_YUV444:
1022 y >>= 4;
1023 cb >>= 4;
1024 cr >>= 4;
1025 break;
1026 case V4L2_PIX_FMT_YUV555:
1027 y >>= 3;
1028 cb >>= 3;
1029 cr >>= 3;
1030 break;
1031 case V4L2_PIX_FMT_YUV565:
1032 y >>= 3;
1033 cb >>= 2;
1034 cr >>= 3;
1035 break;
1036 }
1037 tpg->colors[k][0] = y;
1038 tpg->colors[k][1] = cb;
1039 tpg->colors[k][2] = cr;
1040 break;
1041 }
1042 case TGP_COLOR_ENC_LUMA:
1043 {
1044 tpg->colors[k][0] = r >> 4;
1045 break;
1046 }
1047 case TGP_COLOR_ENC_RGB:
1048 {
1049 if (tpg->real_quantization == V4L2_QUANTIZATION_LIM_RANGE) {
1050 r = (r * 219) / 255 + (16 << 4);
1051 g = (g * 219) / 255 + (16 << 4);
1052 b = (b * 219) / 255 + (16 << 4);
1053 }
1054 switch (tpg->fourcc) {
1055 case V4L2_PIX_FMT_RGB332:
1056 r >>= 9;
1057 g >>= 9;
1058 b >>= 10;
1059 break;
1060 case V4L2_PIX_FMT_RGB565:
1061 case V4L2_PIX_FMT_RGB565X:
1062 r >>= 7;
1063 g >>= 6;
1064 b >>= 7;
1065 break;
1066 case V4L2_PIX_FMT_RGB444:
1067 case V4L2_PIX_FMT_XRGB444:
1068 case V4L2_PIX_FMT_ARGB444:
1069 case V4L2_PIX_FMT_RGBX444:
1070 case V4L2_PIX_FMT_RGBA444:
1071 case V4L2_PIX_FMT_XBGR444:
1072 case V4L2_PIX_FMT_ABGR444:
1073 case V4L2_PIX_FMT_BGRX444:
1074 case V4L2_PIX_FMT_BGRA444:
1075 r >>= 8;
1076 g >>= 8;
1077 b >>= 8;
1078 break;
1079 case V4L2_PIX_FMT_RGB555:
1080 case V4L2_PIX_FMT_XRGB555:
1081 case V4L2_PIX_FMT_ARGB555:
1082 case V4L2_PIX_FMT_RGBX555:
1083 case V4L2_PIX_FMT_RGBA555:
1084 case V4L2_PIX_FMT_XBGR555:
1085 case V4L2_PIX_FMT_ABGR555:
1086 case V4L2_PIX_FMT_BGRX555:
1087 case V4L2_PIX_FMT_BGRA555:
1088 case V4L2_PIX_FMT_RGB555X:
1089 case V4L2_PIX_FMT_XRGB555X:
1090 case V4L2_PIX_FMT_ARGB555X:
1091 r >>= 7;
1092 g >>= 7;
1093 b >>= 7;
1094 break;
1095 case V4L2_PIX_FMT_BGR666:
1096 r >>= 6;
1097 g >>= 6;
1098 b >>= 6;
1099 break;
1100 default:
1101 r >>= 4;
1102 g >>= 4;
1103 b >>= 4;
1104 break;
1105 }
1106
1107 tpg->colors[k][0] = r;
1108 tpg->colors[k][1] = g;
1109 tpg->colors[k][2] = b;
1110 break;
1111 }
1112 }
1113 }
1114
1115 static void tpg_precalculate_colors(struct tpg_data *tpg)
1116 {
1117 int k;
1118
1119 for (k = 0; k < TPG_COLOR_MAX; k++)
1120 precalculate_color(tpg, k);
1121 }
1122
1123 /* 'odd' is true for pixels 1, 3, 5, etc. and false for pixels 0, 2, 4, etc. */
1124 static void gen_twopix(struct tpg_data *tpg,
1125 u8 buf[TPG_MAX_PLANES][8], int color, bool odd)
1126 {
1127 unsigned offset = odd * tpg->twopixelsize[0] / 2;
1128 u8 alpha = tpg->alpha_component;
1129 u8 r_y_h, g_u_s, b_v;
1130
1131 if (tpg->alpha_red_only && color != TPG_COLOR_CSC_RED &&
1132 color != TPG_COLOR_100_RED &&
1133 color != TPG_COLOR_75_RED)
1134 alpha = 0;
1135 if (color == TPG_COLOR_RANDOM)
1136 precalculate_color(tpg, color);
1137 r_y_h = tpg->colors[color][0]; /* R or precalculated Y, H */
1138 g_u_s = tpg->colors[color][1]; /* G or precalculated U, V */
1139 b_v = tpg->colors[color][2]; /* B or precalculated V */
1140
1141 switch (tpg->fourcc) {
1142 case V4L2_PIX_FMT_GREY:
1143 buf[0][offset] = r_y_h;
1144 break;
1145 case V4L2_PIX_FMT_Y10:
1146 buf[0][offset] = (r_y_h << 2) & 0xff;
1147 buf[0][offset+1] = r_y_h >> 6;
1148 break;
1149 case V4L2_PIX_FMT_Y12:
1150 buf[0][offset] = (r_y_h << 4) & 0xff;
1151 buf[0][offset+1] = r_y_h >> 4;
1152 break;
1153 case V4L2_PIX_FMT_Y16:
1154 case V4L2_PIX_FMT_Z16:
1155 /*
1156 * Ideally both bytes should be set to r_y_h, but then you won't
1157 * be able to detect endian problems. So keep it 0 except for
1158 * the corner case where r_y_h is 0xff so white really will be
1159 * white (0xffff).
1160 */
1161 buf[0][offset] = r_y_h == 0xff ? r_y_h : 0;
1162 buf[0][offset+1] = r_y_h;
1163 break;
1164 case V4L2_PIX_FMT_Y16_BE:
1165 /* See comment for V4L2_PIX_FMT_Y16 above */
1166 buf[0][offset] = r_y_h;
1167 buf[0][offset+1] = r_y_h == 0xff ? r_y_h : 0;
1168 break;
1169 case V4L2_PIX_FMT_YUV422M:
1170 case V4L2_PIX_FMT_YUV422P:
1171 case V4L2_PIX_FMT_YUV420:
1172 case V4L2_PIX_FMT_YUV420M:
1173 buf[0][offset] = r_y_h;
1174 if (odd) {
1175 buf[1][0] = (buf[1][0] + g_u_s) / 2;
1176 buf[2][0] = (buf[2][0] + b_v) / 2;
1177 buf[1][1] = buf[1][0];
1178 buf[2][1] = buf[2][0];
1179 break;
1180 }
1181 buf[1][0] = g_u_s;
1182 buf[2][0] = b_v;
1183 break;
1184 case V4L2_PIX_FMT_YVU422M:
1185 case V4L2_PIX_FMT_YVU420:
1186 case V4L2_PIX_FMT_YVU420M:
1187 buf[0][offset] = r_y_h;
1188 if (odd) {
1189 buf[1][0] = (buf[1][0] + b_v) / 2;
1190 buf[2][0] = (buf[2][0] + g_u_s) / 2;
1191 buf[1][1] = buf[1][0];
1192 buf[2][1] = buf[2][0];
1193 break;
1194 }
1195 buf[1][0] = b_v;
1196 buf[2][0] = g_u_s;
1197 break;
1198
1199 case V4L2_PIX_FMT_NV12:
1200 case V4L2_PIX_FMT_NV12M:
1201 case V4L2_PIX_FMT_NV16:
1202 case V4L2_PIX_FMT_NV16M:
1203 buf[0][offset] = r_y_h;
1204 if (odd) {
1205 buf[1][0] = (buf[1][0] + g_u_s) / 2;
1206 buf[1][1] = (buf[1][1] + b_v) / 2;
1207 break;
1208 }
1209 buf[1][0] = g_u_s;
1210 buf[1][1] = b_v;
1211 break;
1212 case V4L2_PIX_FMT_NV21:
1213 case V4L2_PIX_FMT_NV21M:
1214 case V4L2_PIX_FMT_NV61:
1215 case V4L2_PIX_FMT_NV61M:
1216 buf[0][offset] = r_y_h;
1217 if (odd) {
1218 buf[1][0] = (buf[1][0] + b_v) / 2;
1219 buf[1][1] = (buf[1][1] + g_u_s) / 2;
1220 break;
1221 }
1222 buf[1][0] = b_v;
1223 buf[1][1] = g_u_s;
1224 break;
1225
1226 case V4L2_PIX_FMT_YUV444M:
1227 buf[0][offset] = r_y_h;
1228 buf[1][offset] = g_u_s;
1229 buf[2][offset] = b_v;
1230 break;
1231
1232 case V4L2_PIX_FMT_YVU444M:
1233 buf[0][offset] = r_y_h;
1234 buf[1][offset] = b_v;
1235 buf[2][offset] = g_u_s;
1236 break;
1237
1238 case V4L2_PIX_FMT_NV24:
1239 buf[0][offset] = r_y_h;
1240 buf[1][2 * offset] = g_u_s;
1241 buf[1][(2 * offset + 1) % 8] = b_v;
1242 break;
1243
1244 case V4L2_PIX_FMT_NV42:
1245 buf[0][offset] = r_y_h;
1246 buf[1][2 * offset] = b_v;
1247 buf[1][(2 * offset + 1) % 8] = g_u_s;
1248 break;
1249
1250 case V4L2_PIX_FMT_YUYV:
1251 buf[0][offset] = r_y_h;
1252 if (odd) {
1253 buf[0][1] = (buf[0][1] + g_u_s) / 2;
1254 buf[0][3] = (buf[0][3] + b_v) / 2;
1255 break;
1256 }
1257 buf[0][1] = g_u_s;
1258 buf[0][3] = b_v;
1259 break;
1260 case V4L2_PIX_FMT_UYVY:
1261 buf[0][offset + 1] = r_y_h;
1262 if (odd) {
1263 buf[0][0] = (buf[0][0] + g_u_s) / 2;
1264 buf[0][2] = (buf[0][2] + b_v) / 2;
1265 break;
1266 }
1267 buf[0][0] = g_u_s;
1268 buf[0][2] = b_v;
1269 break;
1270 case V4L2_PIX_FMT_YVYU:
1271 buf[0][offset] = r_y_h;
1272 if (odd) {
1273 buf[0][1] = (buf[0][1] + b_v) / 2;
1274 buf[0][3] = (buf[0][3] + g_u_s) / 2;
1275 break;
1276 }
1277 buf[0][1] = b_v;
1278 buf[0][3] = g_u_s;
1279 break;
1280 case V4L2_PIX_FMT_VYUY:
1281 buf[0][offset + 1] = r_y_h;
1282 if (odd) {
1283 buf[0][0] = (buf[0][0] + b_v) / 2;
1284 buf[0][2] = (buf[0][2] + g_u_s) / 2;
1285 break;
1286 }
1287 buf[0][0] = b_v;
1288 buf[0][2] = g_u_s;
1289 break;
1290 case V4L2_PIX_FMT_RGB332:
1291 buf[0][offset] = (r_y_h << 5) | (g_u_s << 2) | b_v;
1292 break;
1293 case V4L2_PIX_FMT_YUV565:
1294 case V4L2_PIX_FMT_RGB565:
1295 buf[0][offset] = (g_u_s << 5) | b_v;
1296 buf[0][offset + 1] = (r_y_h << 3) | (g_u_s >> 3);
1297 break;
1298 case V4L2_PIX_FMT_RGB565X:
1299 buf[0][offset] = (r_y_h << 3) | (g_u_s >> 3);
1300 buf[0][offset + 1] = (g_u_s << 5) | b_v;
1301 break;
1302 case V4L2_PIX_FMT_RGB444:
1303 case V4L2_PIX_FMT_XRGB444:
1304 alpha = 0;
1305 fallthrough;
1306 case V4L2_PIX_FMT_YUV444:
1307 case V4L2_PIX_FMT_ARGB444:
1308 buf[0][offset] = (g_u_s << 4) | b_v;
1309 buf[0][offset + 1] = (alpha & 0xf0) | r_y_h;
1310 break;
1311 case V4L2_PIX_FMT_RGBX444:
1312 alpha = 0;
1313 fallthrough;
1314 case V4L2_PIX_FMT_RGBA444:
1315 buf[0][offset] = (b_v << 4) | (alpha >> 4);
1316 buf[0][offset + 1] = (r_y_h << 4) | g_u_s;
1317 break;
1318 case V4L2_PIX_FMT_XBGR444:
1319 alpha = 0;
1320 fallthrough;
1321 case V4L2_PIX_FMT_ABGR444:
1322 buf[0][offset] = (g_u_s << 4) | r_y_h;
1323 buf[0][offset + 1] = (alpha & 0xf0) | b_v;
1324 break;
1325 case V4L2_PIX_FMT_BGRX444:
1326 alpha = 0;
1327 fallthrough;
1328 case V4L2_PIX_FMT_BGRA444:
1329 buf[0][offset] = (r_y_h << 4) | (alpha >> 4);
1330 buf[0][offset + 1] = (b_v << 4) | g_u_s;
1331 break;
1332 case V4L2_PIX_FMT_RGB555:
1333 case V4L2_PIX_FMT_XRGB555:
1334 alpha = 0;
1335 fallthrough;
1336 case V4L2_PIX_FMT_YUV555:
1337 case V4L2_PIX_FMT_ARGB555:
1338 buf[0][offset] = (g_u_s << 5) | b_v;
1339 buf[0][offset + 1] = (alpha & 0x80) | (r_y_h << 2)
1340 | (g_u_s >> 3);
1341 break;
1342 case V4L2_PIX_FMT_RGBX555:
1343 alpha = 0;
1344 fallthrough;
1345 case V4L2_PIX_FMT_RGBA555:
1346 buf[0][offset] = (g_u_s << 6) | (b_v << 1) |
1347 ((alpha & 0x80) >> 7);
1348 buf[0][offset + 1] = (r_y_h << 3) | (g_u_s >> 2);
1349 break;
1350 case V4L2_PIX_FMT_XBGR555:
1351 alpha = 0;
1352 fallthrough;
1353 case V4L2_PIX_FMT_ABGR555:
1354 buf[0][offset] = (g_u_s << 5) | r_y_h;
1355 buf[0][offset + 1] = (alpha & 0x80) | (b_v << 2)
1356 | (g_u_s >> 3);
1357 break;
1358 case V4L2_PIX_FMT_BGRX555:
1359 alpha = 0;
1360 fallthrough;
1361 case V4L2_PIX_FMT_BGRA555:
1362 buf[0][offset] = (g_u_s << 6) | (r_y_h << 1) |
1363 ((alpha & 0x80) >> 7);
1364 buf[0][offset + 1] = (b_v << 3) | (g_u_s >> 2);
1365 break;
1366 case V4L2_PIX_FMT_RGB555X:
1367 case V4L2_PIX_FMT_XRGB555X:
1368 alpha = 0;
1369 fallthrough;
1370 case V4L2_PIX_FMT_ARGB555X:
1371 buf[0][offset] = (alpha & 0x80) | (r_y_h << 2) | (g_u_s >> 3);
1372 buf[0][offset + 1] = (g_u_s << 5) | b_v;
1373 break;
1374 case V4L2_PIX_FMT_RGB24:
1375 case V4L2_PIX_FMT_HSV24:
1376 buf[0][offset] = r_y_h;
1377 buf[0][offset + 1] = g_u_s;
1378 buf[0][offset + 2] = b_v;
1379 break;
1380 case V4L2_PIX_FMT_BGR24:
1381 buf[0][offset] = b_v;
1382 buf[0][offset + 1] = g_u_s;
1383 buf[0][offset + 2] = r_y_h;
1384 break;
1385 case V4L2_PIX_FMT_BGR666:
1386 buf[0][offset] = (b_v << 2) | (g_u_s >> 4);
1387 buf[0][offset + 1] = (g_u_s << 4) | (r_y_h >> 2);
1388 buf[0][offset + 2] = r_y_h << 6;
1389 buf[0][offset + 3] = 0;
1390 break;
1391 case V4L2_PIX_FMT_RGB32:
1392 case V4L2_PIX_FMT_XRGB32:
1393 case V4L2_PIX_FMT_HSV32:
1394 case V4L2_PIX_FMT_XYUV32:
1395 alpha = 0;
1396 fallthrough;
1397 case V4L2_PIX_FMT_YUV32:
1398 case V4L2_PIX_FMT_ARGB32:
1399 case V4L2_PIX_FMT_AYUV32:
1400 buf[0][offset] = alpha;
1401 buf[0][offset + 1] = r_y_h;
1402 buf[0][offset + 2] = g_u_s;
1403 buf[0][offset + 3] = b_v;
1404 break;
1405 case V4L2_PIX_FMT_RGBX32:
1406 case V4L2_PIX_FMT_YUVX32:
1407 alpha = 0;
1408 fallthrough;
1409 case V4L2_PIX_FMT_RGBA32:
1410 case V4L2_PIX_FMT_YUVA32:
1411 buf[0][offset] = r_y_h;
1412 buf[0][offset + 1] = g_u_s;
1413 buf[0][offset + 2] = b_v;
1414 buf[0][offset + 3] = alpha;
1415 break;
1416 case V4L2_PIX_FMT_BGR32:
1417 case V4L2_PIX_FMT_XBGR32:
1418 case V4L2_PIX_FMT_VUYX32:
1419 alpha = 0;
1420 fallthrough;
1421 case V4L2_PIX_FMT_ABGR32:
1422 case V4L2_PIX_FMT_VUYA32:
1423 buf[0][offset] = b_v;
1424 buf[0][offset + 1] = g_u_s;
1425 buf[0][offset + 2] = r_y_h;
1426 buf[0][offset + 3] = alpha;
1427 break;
1428 case V4L2_PIX_FMT_BGRX32:
1429 alpha = 0;
1430 fallthrough;
1431 case V4L2_PIX_FMT_BGRA32:
1432 buf[0][offset] = alpha;
1433 buf[0][offset + 1] = b_v;
1434 buf[0][offset + 2] = g_u_s;
1435 buf[0][offset + 3] = r_y_h;
1436 break;
1437 case V4L2_PIX_FMT_SBGGR8:
1438 buf[0][offset] = odd ? g_u_s : b_v;
1439 buf[1][offset] = odd ? r_y_h : g_u_s;
1440 break;
1441 case V4L2_PIX_FMT_SGBRG8:
1442 buf[0][offset] = odd ? b_v : g_u_s;
1443 buf[1][offset] = odd ? g_u_s : r_y_h;
1444 break;
1445 case V4L2_PIX_FMT_SGRBG8:
1446 buf[0][offset] = odd ? r_y_h : g_u_s;
1447 buf[1][offset] = odd ? g_u_s : b_v;
1448 break;
1449 case V4L2_PIX_FMT_SRGGB8:
1450 buf[0][offset] = odd ? g_u_s : r_y_h;
1451 buf[1][offset] = odd ? b_v : g_u_s;
1452 break;
1453 case V4L2_PIX_FMT_SBGGR10:
1454 buf[0][offset] = odd ? g_u_s << 2 : b_v << 2;
1455 buf[0][offset + 1] = odd ? g_u_s >> 6 : b_v >> 6;
1456 buf[1][offset] = odd ? r_y_h << 2 : g_u_s << 2;
1457 buf[1][offset + 1] = odd ? r_y_h >> 6 : g_u_s >> 6;
1458 buf[0][offset] |= (buf[0][offset] >> 2) & 3;
1459 buf[1][offset] |= (buf[1][offset] >> 2) & 3;
1460 break;
1461 case V4L2_PIX_FMT_SGBRG10:
1462 buf[0][offset] = odd ? b_v << 2 : g_u_s << 2;
1463 buf[0][offset + 1] = odd ? b_v >> 6 : g_u_s >> 6;
1464 buf[1][offset] = odd ? g_u_s << 2 : r_y_h << 2;
1465 buf[1][offset + 1] = odd ? g_u_s >> 6 : r_y_h >> 6;
1466 buf[0][offset] |= (buf[0][offset] >> 2) & 3;
1467 buf[1][offset] |= (buf[1][offset] >> 2) & 3;
1468 break;
1469 case V4L2_PIX_FMT_SGRBG10:
1470 buf[0][offset] = odd ? r_y_h << 2 : g_u_s << 2;
1471 buf[0][offset + 1] = odd ? r_y_h >> 6 : g_u_s >> 6;
1472 buf[1][offset] = odd ? g_u_s << 2 : b_v << 2;
1473 buf[1][offset + 1] = odd ? g_u_s >> 6 : b_v >> 6;
1474 buf[0][offset] |= (buf[0][offset] >> 2) & 3;
1475 buf[1][offset] |= (buf[1][offset] >> 2) & 3;
1476 break;
1477 case V4L2_PIX_FMT_SRGGB10:
1478 buf[0][offset] = odd ? g_u_s << 2 : r_y_h << 2;
1479 buf[0][offset + 1] = odd ? g_u_s >> 6 : r_y_h >> 6;
1480 buf[1][offset] = odd ? b_v << 2 : g_u_s << 2;
1481 buf[1][offset + 1] = odd ? b_v >> 6 : g_u_s >> 6;
1482 buf[0][offset] |= (buf[0][offset] >> 2) & 3;
1483 buf[1][offset] |= (buf[1][offset] >> 2) & 3;
1484 break;
1485 case V4L2_PIX_FMT_SBGGR12:
1486 buf[0][offset] = odd ? g_u_s << 4 : b_v << 4;
1487 buf[0][offset + 1] = odd ? g_u_s >> 4 : b_v >> 4;
1488 buf[1][offset] = odd ? r_y_h << 4 : g_u_s << 4;
1489 buf[1][offset + 1] = odd ? r_y_h >> 4 : g_u_s >> 4;
1490 buf[0][offset] |= (buf[0][offset] >> 4) & 0xf;
1491 buf[1][offset] |= (buf[1][offset] >> 4) & 0xf;
1492 break;
1493 case V4L2_PIX_FMT_SGBRG12:
1494 buf[0][offset] = odd ? b_v << 4 : g_u_s << 4;
1495 buf[0][offset + 1] = odd ? b_v >> 4 : g_u_s >> 4;
1496 buf[1][offset] = odd ? g_u_s << 4 : r_y_h << 4;
1497 buf[1][offset + 1] = odd ? g_u_s >> 4 : r_y_h >> 4;
1498 buf[0][offset] |= (buf[0][offset] >> 4) & 0xf;
1499 buf[1][offset] |= (buf[1][offset] >> 4) & 0xf;
1500 break;
1501 case V4L2_PIX_FMT_SGRBG12:
1502 buf[0][offset] = odd ? r_y_h << 4 : g_u_s << 4;
1503 buf[0][offset + 1] = odd ? r_y_h >> 4 : g_u_s >> 4;
1504 buf[1][offset] = odd ? g_u_s << 4 : b_v << 4;
1505 buf[1][offset + 1] = odd ? g_u_s >> 4 : b_v >> 4;
1506 buf[0][offset] |= (buf[0][offset] >> 4) & 0xf;
1507 buf[1][offset] |= (buf[1][offset] >> 4) & 0xf;
1508 break;
1509 case V4L2_PIX_FMT_SRGGB12:
1510 buf[0][offset] = odd ? g_u_s << 4 : r_y_h << 4;
1511 buf[0][offset + 1] = odd ? g_u_s >> 4 : r_y_h >> 4;
1512 buf[1][offset] = odd ? b_v << 4 : g_u_s << 4;
1513 buf[1][offset + 1] = odd ? b_v >> 4 : g_u_s >> 4;
1514 buf[0][offset] |= (buf[0][offset] >> 4) & 0xf;
1515 buf[1][offset] |= (buf[1][offset] >> 4) & 0xf;
1516 break;
1517 case V4L2_PIX_FMT_SBGGR16:
1518 buf[0][offset] = buf[0][offset + 1] = odd ? g_u_s : b_v;
1519 buf[1][offset] = buf[1][offset + 1] = odd ? r_y_h : g_u_s;
1520 break;
1521 case V4L2_PIX_FMT_SGBRG16:
1522 buf[0][offset] = buf[0][offset + 1] = odd ? b_v : g_u_s;
1523 buf[1][offset] = buf[1][offset + 1] = odd ? g_u_s : r_y_h;
1524 break;
1525 case V4L2_PIX_FMT_SGRBG16:
1526 buf[0][offset] = buf[0][offset + 1] = odd ? r_y_h : g_u_s;
1527 buf[1][offset] = buf[1][offset + 1] = odd ? g_u_s : b_v;
1528 break;
1529 case V4L2_PIX_FMT_SRGGB16:
1530 buf[0][offset] = buf[0][offset + 1] = odd ? g_u_s : r_y_h;
1531 buf[1][offset] = buf[1][offset + 1] = odd ? b_v : g_u_s;
1532 break;
1533 }
1534 }
1535
1536 unsigned tpg_g_interleaved_plane(const struct tpg_data *tpg, unsigned buf_line)
1537 {
1538 switch (tpg->fourcc) {
1539 case V4L2_PIX_FMT_SBGGR8:
1540 case V4L2_PIX_FMT_SGBRG8:
1541 case V4L2_PIX_FMT_SGRBG8:
1542 case V4L2_PIX_FMT_SRGGB8:
1543 case V4L2_PIX_FMT_SBGGR10:
1544 case V4L2_PIX_FMT_SGBRG10:
1545 case V4L2_PIX_FMT_SGRBG10:
1546 case V4L2_PIX_FMT_SRGGB10:
1547 case V4L2_PIX_FMT_SBGGR12:
1548 case V4L2_PIX_FMT_SGBRG12:
1549 case V4L2_PIX_FMT_SGRBG12:
1550 case V4L2_PIX_FMT_SRGGB12:
1551 case V4L2_PIX_FMT_SBGGR16:
1552 case V4L2_PIX_FMT_SGBRG16:
1553 case V4L2_PIX_FMT_SGRBG16:
1554 case V4L2_PIX_FMT_SRGGB16:
1555 return buf_line & 1;
1556 default:
1557 return 0;
1558 }
1559 }
1560
1561 /* Return how many pattern lines are used by the current pattern. */
1562 static unsigned tpg_get_pat_lines(const struct tpg_data *tpg)
1563 {
1564 switch (tpg->pattern) {
1565 case TPG_PAT_CHECKERS_16X16:
1566 case TPG_PAT_CHECKERS_2X2:
1567 case TPG_PAT_CHECKERS_1X1:
1568 case TPG_PAT_COLOR_CHECKERS_2X2:
1569 case TPG_PAT_COLOR_CHECKERS_1X1:
1570 case TPG_PAT_ALTERNATING_HLINES:
1571 case TPG_PAT_CROSS_1_PIXEL:
1572 case TPG_PAT_CROSS_2_PIXELS:
1573 case TPG_PAT_CROSS_10_PIXELS:
1574 return 2;
1575 case TPG_PAT_100_COLORSQUARES:
1576 case TPG_PAT_100_HCOLORBAR:
1577 return 8;
1578 default:
1579 return 1;
1580 }
1581 }
1582
1583 /* Which pattern line should be used for the given frame line. */
1584 static unsigned tpg_get_pat_line(const struct tpg_data *tpg, unsigned line)
1585 {
1586 switch (tpg->pattern) {
1587 case TPG_PAT_CHECKERS_16X16:
1588 return (line >> 4) & 1;
1589 case TPG_PAT_CHECKERS_1X1:
1590 case TPG_PAT_COLOR_CHECKERS_1X1:
1591 case TPG_PAT_ALTERNATING_HLINES:
1592 return line & 1;
1593 case TPG_PAT_CHECKERS_2X2:
1594 case TPG_PAT_COLOR_CHECKERS_2X2:
1595 return (line & 2) >> 1;
1596 case TPG_PAT_100_COLORSQUARES:
1597 case TPG_PAT_100_HCOLORBAR:
1598 return (line * 8) / tpg->src_height;
1599 case TPG_PAT_CROSS_1_PIXEL:
1600 return line == tpg->src_height / 2;
1601 case TPG_PAT_CROSS_2_PIXELS:
1602 return (line + 1) / 2 == tpg->src_height / 4;
1603 case TPG_PAT_CROSS_10_PIXELS:
1604 return (line + 10) / 20 == tpg->src_height / 40;
1605 default:
1606 return 0;
1607 }
1608 }
1609
1610 /*
1611 * Which color should be used for the given pattern line and X coordinate.
1612 * Note: x is in the range 0 to 2 * tpg->src_width.
1613 */
1614 static enum tpg_color tpg_get_color(const struct tpg_data *tpg,
1615 unsigned pat_line, unsigned x)
1616 {
1617 /* Maximum number of bars are TPG_COLOR_MAX - otherwise, the input print code
1618 should be modified */
1619 static const enum tpg_color bars[3][8] = {
1620 /* Standard ITU-R 75% color bar sequence */
1621 { TPG_COLOR_CSC_WHITE, TPG_COLOR_75_YELLOW,
1622 TPG_COLOR_75_CYAN, TPG_COLOR_75_GREEN,
1623 TPG_COLOR_75_MAGENTA, TPG_COLOR_75_RED,
1624 TPG_COLOR_75_BLUE, TPG_COLOR_100_BLACK, },
1625 /* Standard ITU-R 100% color bar sequence */
1626 { TPG_COLOR_100_WHITE, TPG_COLOR_100_YELLOW,
1627 TPG_COLOR_100_CYAN, TPG_COLOR_100_GREEN,
1628 TPG_COLOR_100_MAGENTA, TPG_COLOR_100_RED,
1629 TPG_COLOR_100_BLUE, TPG_COLOR_100_BLACK, },
1630 /* Color bar sequence suitable to test CSC */
1631 { TPG_COLOR_CSC_WHITE, TPG_COLOR_CSC_YELLOW,
1632 TPG_COLOR_CSC_CYAN, TPG_COLOR_CSC_GREEN,
1633 TPG_COLOR_CSC_MAGENTA, TPG_COLOR_CSC_RED,
1634 TPG_COLOR_CSC_BLUE, TPG_COLOR_CSC_BLACK, },
1635 };
1636
1637 switch (tpg->pattern) {
1638 case TPG_PAT_75_COLORBAR:
1639 case TPG_PAT_100_COLORBAR:
1640 case TPG_PAT_CSC_COLORBAR:
1641 return bars[tpg->pattern][((x * 8) / tpg->src_width) % 8];
1642 case TPG_PAT_100_COLORSQUARES:
1643 return bars[1][(pat_line + (x * 8) / tpg->src_width) % 8];
1644 case TPG_PAT_100_HCOLORBAR:
1645 return bars[1][pat_line];
1646 case TPG_PAT_BLACK:
1647 return TPG_COLOR_100_BLACK;
1648 case TPG_PAT_WHITE:
1649 return TPG_COLOR_100_WHITE;
1650 case TPG_PAT_RED:
1651 return TPG_COLOR_100_RED;
1652 case TPG_PAT_GREEN:
1653 return TPG_COLOR_100_GREEN;
1654 case TPG_PAT_BLUE:
1655 return TPG_COLOR_100_BLUE;
1656 case TPG_PAT_CHECKERS_16X16:
1657 return (((x >> 4) & 1) ^ (pat_line & 1)) ?
1658 TPG_COLOR_100_BLACK : TPG_COLOR_100_WHITE;
1659 case TPG_PAT_CHECKERS_1X1:
1660 return ((x & 1) ^ (pat_line & 1)) ?
1661 TPG_COLOR_100_WHITE : TPG_COLOR_100_BLACK;
1662 case TPG_PAT_COLOR_CHECKERS_1X1:
1663 return ((x & 1) ^ (pat_line & 1)) ?
1664 TPG_COLOR_100_RED : TPG_COLOR_100_BLUE;
1665 case TPG_PAT_CHECKERS_2X2:
1666 return (((x >> 1) & 1) ^ (pat_line & 1)) ?
1667 TPG_COLOR_100_WHITE : TPG_COLOR_100_BLACK;
1668 case TPG_PAT_COLOR_CHECKERS_2X2:
1669 return (((x >> 1) & 1) ^ (pat_line & 1)) ?
1670 TPG_COLOR_100_RED : TPG_COLOR_100_BLUE;
1671 case TPG_PAT_ALTERNATING_HLINES:
1672 return pat_line ? TPG_COLOR_100_WHITE : TPG_COLOR_100_BLACK;
1673 case TPG_PAT_ALTERNATING_VLINES:
1674 return (x & 1) ? TPG_COLOR_100_WHITE : TPG_COLOR_100_BLACK;
1675 case TPG_PAT_CROSS_1_PIXEL:
1676 if (pat_line || (x % tpg->src_width) == tpg->src_width / 2)
1677 return TPG_COLOR_100_BLACK;
1678 return TPG_COLOR_100_WHITE;
1679 case TPG_PAT_CROSS_2_PIXELS:
1680 if (pat_line || ((x % tpg->src_width) + 1) / 2 == tpg->src_width / 4)
1681 return TPG_COLOR_100_BLACK;
1682 return TPG_COLOR_100_WHITE;
1683 case TPG_PAT_CROSS_10_PIXELS:
1684 if (pat_line || ((x % tpg->src_width) + 10) / 20 == tpg->src_width / 40)
1685 return TPG_COLOR_100_BLACK;
1686 return TPG_COLOR_100_WHITE;
1687 case TPG_PAT_GRAY_RAMP:
1688 return TPG_COLOR_RAMP + ((x % tpg->src_width) * 256) / tpg->src_width;
1689 default:
1690 return TPG_COLOR_100_RED;
1691 }
1692 }
1693
1694 /*
1695 * Given the pixel aspect ratio and video aspect ratio calculate the
1696 * coordinates of a centered square and the coordinates of the border of
1697 * the active video area. The coordinates are relative to the source
1698 * frame rectangle.
1699 */
1700 static void tpg_calculate_square_border(struct tpg_data *tpg)
1701 {
1702 unsigned w = tpg->src_width;
1703 unsigned h = tpg->src_height;
1704 unsigned sq_w, sq_h;
1705
1706 sq_w = (w * 2 / 5) & ~1;
1707 if (((w - sq_w) / 2) & 1)
1708 sq_w += 2;
1709 sq_h = sq_w;
1710 tpg->square.width = sq_w;
1711 if (tpg->vid_aspect == TPG_VIDEO_ASPECT_16X9_ANAMORPHIC) {
1712 unsigned ana_sq_w = (sq_w / 4) * 3;
1713
1714 if (((w - ana_sq_w) / 2) & 1)
1715 ana_sq_w += 2;
1716 tpg->square.width = ana_sq_w;
1717 }
1718 tpg->square.left = (w - tpg->square.width) / 2;
1719 if (tpg->pix_aspect == TPG_PIXEL_ASPECT_NTSC)
1720 sq_h = sq_w * 10 / 11;
1721 else if (tpg->pix_aspect == TPG_PIXEL_ASPECT_PAL)
1722 sq_h = sq_w * 59 / 54;
1723 tpg->square.height = sq_h;
1724 tpg->square.top = (h - sq_h) / 2;
1725 tpg->border.left = 0;
1726 tpg->border.width = w;
1727 tpg->border.top = 0;
1728 tpg->border.height = h;
1729 switch (tpg->vid_aspect) {
1730 case TPG_VIDEO_ASPECT_4X3:
1731 if (tpg->pix_aspect)
1732 return;
1733 if (3 * w >= 4 * h) {
1734 tpg->border.width = ((4 * h) / 3) & ~1;
1735 if (((w - tpg->border.width) / 2) & ~1)
1736 tpg->border.width -= 2;
1737 tpg->border.left = (w - tpg->border.width) / 2;
1738 break;
1739 }
1740 tpg->border.height = ((3 * w) / 4) & ~1;
1741 tpg->border.top = (h - tpg->border.height) / 2;
1742 break;
1743 case TPG_VIDEO_ASPECT_14X9_CENTRE:
1744 if (tpg->pix_aspect) {
1745 tpg->border.height = tpg->pix_aspect == TPG_PIXEL_ASPECT_NTSC ? 420 : 506;
1746 tpg->border.top = (h - tpg->border.height) / 2;
1747 break;
1748 }
1749 if (9 * w >= 14 * h) {
1750 tpg->border.width = ((14 * h) / 9) & ~1;
1751 if (((w - tpg->border.width) / 2) & ~1)
1752 tpg->border.width -= 2;
1753 tpg->border.left = (w - tpg->border.width) / 2;
1754 break;
1755 }
1756 tpg->border.height = ((9 * w) / 14) & ~1;
1757 tpg->border.top = (h - tpg->border.height) / 2;
1758 break;
1759 case TPG_VIDEO_ASPECT_16X9_CENTRE:
1760 if (tpg->pix_aspect) {
1761 tpg->border.height = tpg->pix_aspect == TPG_PIXEL_ASPECT_NTSC ? 368 : 442;
1762 tpg->border.top = (h - tpg->border.height) / 2;
1763 break;
1764 }
1765 if (9 * w >= 16 * h) {
1766 tpg->border.width = ((16 * h) / 9) & ~1;
1767 if (((w - tpg->border.width) / 2) & ~1)
1768 tpg->border.width -= 2;
1769 tpg->border.left = (w - tpg->border.width) / 2;
1770 break;
1771 }
1772 tpg->border.height = ((9 * w) / 16) & ~1;
1773 tpg->border.top = (h - tpg->border.height) / 2;
1774 break;
1775 default:
1776 break;
1777 }
1778 }
1779
1780 static void tpg_precalculate_line(struct tpg_data *tpg)
1781 {
1782 enum tpg_color contrast;
1783 u8 pix[TPG_MAX_PLANES][8];
1784 unsigned pat;
1785 unsigned p;
1786 unsigned x;
1787
1788 if (WARN_ON_ONCE(!tpg->src_width || !tpg->scaled_width))
1789 return;
1790
1791 switch (tpg->pattern) {
1792 case TPG_PAT_GREEN:
1793 contrast = TPG_COLOR_100_RED;
1794 break;
1795 case TPG_PAT_CSC_COLORBAR:
1796 contrast = TPG_COLOR_CSC_GREEN;
1797 break;
1798 default:
1799 contrast = TPG_COLOR_100_GREEN;
1800 break;
1801 }
1802
1803 for (pat = 0; pat < tpg_get_pat_lines(tpg); pat++) {
1804 /* Coarse scaling with Bresenham */
1805 unsigned int_part = tpg->src_width / tpg->scaled_width;
1806 unsigned fract_part = tpg->src_width % tpg->scaled_width;
1807 unsigned src_x = 0;
1808 unsigned error = 0;
1809
1810 for (x = 0; x < tpg->scaled_width * 2; x += 2) {
1811 unsigned real_x = src_x;
1812 enum tpg_color color1, color2;
1813
1814 real_x = tpg->hflip ? tpg->src_width * 2 - real_x - 2 : real_x;
1815 color1 = tpg_get_color(tpg, pat, real_x);
1816
1817 src_x += int_part;
1818 error += fract_part;
1819 if (error >= tpg->scaled_width) {
1820 error -= tpg->scaled_width;
1821 src_x++;
1822 }
1823
1824 real_x = src_x;
1825 real_x = tpg->hflip ? tpg->src_width * 2 - real_x - 2 : real_x;
1826 color2 = tpg_get_color(tpg, pat, real_x);
1827
1828 src_x += int_part;
1829 error += fract_part;
1830 if (error >= tpg->scaled_width) {
1831 error -= tpg->scaled_width;
1832 src_x++;
1833 }
1834
1835 gen_twopix(tpg, pix, tpg->hflip ? color2 : color1, 0);
1836 gen_twopix(tpg, pix, tpg->hflip ? color1 : color2, 1);
1837 for (p = 0; p < tpg->planes; p++) {
1838 unsigned twopixsize = tpg->twopixelsize[p];
1839 unsigned hdiv = tpg->hdownsampling[p];
1840 u8 *pos = tpg->lines[pat][p] + tpg_hdiv(tpg, p, x);
1841
1842 memcpy(pos, pix[p], twopixsize / hdiv);
1843 }
1844 }
1845 }
1846
1847 if (tpg->vdownsampling[tpg->planes - 1] > 1) {
1848 unsigned pat_lines = tpg_get_pat_lines(tpg);
1849
1850 for (pat = 0; pat < pat_lines; pat++) {
1851 unsigned next_pat = (pat + 1) % pat_lines;
1852
1853 for (p = 1; p < tpg->planes; p++) {
1854 unsigned w = tpg_hdiv(tpg, p, tpg->scaled_width * 2);
1855 u8 *pos1 = tpg->lines[pat][p];
1856 u8 *pos2 = tpg->lines[next_pat][p];
1857 u8 *dest = tpg->downsampled_lines[pat][p];
1858
1859 for (x = 0; x < w; x++, pos1++, pos2++, dest++)
1860 *dest = ((u16)*pos1 + (u16)*pos2) / 2;
1861 }
1862 }
1863 }
1864
1865 gen_twopix(tpg, pix, contrast, 0);
1866 gen_twopix(tpg, pix, contrast, 1);
1867 for (p = 0; p < tpg->planes; p++) {
1868 unsigned twopixsize = tpg->twopixelsize[p];
1869 u8 *pos = tpg->contrast_line[p];
1870
1871 for (x = 0; x < tpg->scaled_width; x += 2, pos += twopixsize)
1872 memcpy(pos, pix[p], twopixsize);
1873 }
1874
1875 gen_twopix(tpg, pix, TPG_COLOR_100_BLACK, 0);
1876 gen_twopix(tpg, pix, TPG_COLOR_100_BLACK, 1);
1877 for (p = 0; p < tpg->planes; p++) {
1878 unsigned twopixsize = tpg->twopixelsize[p];
1879 u8 *pos = tpg->black_line[p];
1880
1881 for (x = 0; x < tpg->scaled_width; x += 2, pos += twopixsize)
1882 memcpy(pos, pix[p], twopixsize);
1883 }
1884
1885 for (x = 0; x < tpg->scaled_width * 2; x += 2) {
1886 gen_twopix(tpg, pix, TPG_COLOR_RANDOM, 0);
1887 gen_twopix(tpg, pix, TPG_COLOR_RANDOM, 1);
1888 for (p = 0; p < tpg->planes; p++) {
1889 unsigned twopixsize = tpg->twopixelsize[p];
1890 u8 *pos = tpg->random_line[p] + x * twopixsize / 2;
1891
1892 memcpy(pos, pix[p], twopixsize);
1893 }
1894 }
1895
1896 gen_twopix(tpg, tpg->textbg, TPG_COLOR_TEXTBG, 0);
1897 gen_twopix(tpg, tpg->textbg, TPG_COLOR_TEXTBG, 1);
1898 gen_twopix(tpg, tpg->textfg, TPG_COLOR_TEXTFG, 0);
1899 gen_twopix(tpg, tpg->textfg, TPG_COLOR_TEXTFG, 1);
1900 }
1901
1902 /* need this to do rgb24 rendering */
1903 typedef struct { u16 __; u8 _; } __packed x24;
1904
1905 #define PRINTSTR(PIXTYPE) do { \
1906 unsigned vdiv = tpg->vdownsampling[p]; \
1907 unsigned hdiv = tpg->hdownsampling[p]; \
1908 int line; \
1909 PIXTYPE fg; \
1910 PIXTYPE bg; \
1911 memcpy(&fg, tpg->textfg[p], sizeof(PIXTYPE)); \
1912 memcpy(&bg, tpg->textbg[p], sizeof(PIXTYPE)); \
1913 \
1914 for (line = first; line < 16; line += vdiv * step) { \
1915 int l = tpg->vflip ? 15 - line : line; \
1916 PIXTYPE *pos = (PIXTYPE *)(basep[p][(line / vdiv) & 1] + \
1917 ((y * step + l) / (vdiv * div)) * tpg->bytesperline[p] + \
1918 (x / hdiv) * sizeof(PIXTYPE)); \
1919 unsigned s; \
1920 \
1921 for (s = 0; s < len; s++) { \
1922 u8 chr = font8x16[(u8)text[s] * 16 + line]; \
1923 \
1924 if (hdiv == 2 && tpg->hflip) { \
1925 pos[3] = (chr & (0x01 << 6) ? fg : bg); \
1926 pos[2] = (chr & (0x01 << 4) ? fg : bg); \
1927 pos[1] = (chr & (0x01 << 2) ? fg : bg); \
1928 pos[0] = (chr & (0x01 << 0) ? fg : bg); \
1929 } else if (hdiv == 2) { \
1930 pos[0] = (chr & (0x01 << 7) ? fg : bg); \
1931 pos[1] = (chr & (0x01 << 5) ? fg : bg); \
1932 pos[2] = (chr & (0x01 << 3) ? fg : bg); \
1933 pos[3] = (chr & (0x01 << 1) ? fg : bg); \
1934 } else if (tpg->hflip) { \
1935 pos[7] = (chr & (0x01 << 7) ? fg : bg); \
1936 pos[6] = (chr & (0x01 << 6) ? fg : bg); \
1937 pos[5] = (chr & (0x01 << 5) ? fg : bg); \
1938 pos[4] = (chr & (0x01 << 4) ? fg : bg); \
1939 pos[3] = (chr & (0x01 << 3) ? fg : bg); \
1940 pos[2] = (chr & (0x01 << 2) ? fg : bg); \
1941 pos[1] = (chr & (0x01 << 1) ? fg : bg); \
1942 pos[0] = (chr & (0x01 << 0) ? fg : bg); \
1943 } else { \
1944 pos[0] = (chr & (0x01 << 7) ? fg : bg); \
1945 pos[1] = (chr & (0x01 << 6) ? fg : bg); \
1946 pos[2] = (chr & (0x01 << 5) ? fg : bg); \
1947 pos[3] = (chr & (0x01 << 4) ? fg : bg); \
1948 pos[4] = (chr & (0x01 << 3) ? fg : bg); \
1949 pos[5] = (chr & (0x01 << 2) ? fg : bg); \
1950 pos[6] = (chr & (0x01 << 1) ? fg : bg); \
1951 pos[7] = (chr & (0x01 << 0) ? fg : bg); \
1952 } \
1953 \
1954 pos += (tpg->hflip ? -8 : 8) / (int)hdiv; \
1955 } \
1956 } \
1957 } while (0)
1958
1959 static noinline void tpg_print_str_2(const struct tpg_data *tpg, u8 *basep[TPG_MAX_PLANES][2],
1960 unsigned p, unsigned first, unsigned div, unsigned step,
1961 int y, int x, const char *text, unsigned len)
1962 {
1963 PRINTSTR(u8);
1964 }
1965
1966 static noinline void tpg_print_str_4(const struct tpg_data *tpg, u8 *basep[TPG_MAX_PLANES][2],
1967 unsigned p, unsigned first, unsigned div, unsigned step,
1968 int y, int x, const char *text, unsigned len)
1969 {
1970 PRINTSTR(u16);
1971 }
1972
1973 static noinline void tpg_print_str_6(const struct tpg_data *tpg, u8 *basep[TPG_MAX_PLANES][2],
1974 unsigned p, unsigned first, unsigned div, unsigned step,
1975 int y, int x, const char *text, unsigned len)
1976 {
1977 PRINTSTR(x24);
1978 }
1979
1980 static noinline void tpg_print_str_8(const struct tpg_data *tpg, u8 *basep[TPG_MAX_PLANES][2],
1981 unsigned p, unsigned first, unsigned div, unsigned step,
1982 int y, int x, const char *text, unsigned len)
1983 {
1984 PRINTSTR(u32);
1985 }
1986
1987 void tpg_gen_text(const struct tpg_data *tpg, u8 *basep[TPG_MAX_PLANES][2],
1988 int y, int x, const char *text)
1989 {
1990 unsigned step = V4L2_FIELD_HAS_T_OR_B(tpg->field) ? 2 : 1;
1991 unsigned div = step;
1992 unsigned first = 0;
1993 unsigned len;
1994 unsigned p;
1995
1996 if (font8x16 == NULL || basep == NULL || text == NULL)
1997 return;
1998
1999 len = strlen(text);
2000
2001 /* Checks if it is possible to show string */
2002 if (y + 16 >= tpg->compose.height || x + 8 >= tpg->compose.width)
2003 return;
2004
2005 if (len > (tpg->compose.width - x) / 8)
2006 len = (tpg->compose.width - x) / 8;
2007 if (tpg->vflip)
2008 y = tpg->compose.height - y - 16;
2009 if (tpg->hflip)
2010 x = tpg->compose.width - x - 8;
2011 y += tpg->compose.top;
2012 x += tpg->compose.left;
2013 if (tpg->field == V4L2_FIELD_BOTTOM)
2014 first = 1;
2015 else if (tpg->field == V4L2_FIELD_SEQ_TB || tpg->field == V4L2_FIELD_SEQ_BT)
2016 div = 2;
2017
2018 for (p = 0; p < tpg->planes; p++) {
2019 /* Print text */
2020 switch (tpg->twopixelsize[p]) {
2021 case 2:
2022 tpg_print_str_2(tpg, basep, p, first, div, step, y, x,
2023 text, len);
2024 break;
2025 case 4:
2026 tpg_print_str_4(tpg, basep, p, first, div, step, y, x,
2027 text, len);
2028 break;
2029 case 6:
2030 tpg_print_str_6(tpg, basep, p, first, div, step, y, x,
2031 text, len);
2032 break;
2033 case 8:
2034 tpg_print_str_8(tpg, basep, p, first, div, step, y, x,
2035 text, len);
2036 break;
2037 }
2038 }
2039 }
2040
2041 const char *tpg_g_color_order(const struct tpg_data *tpg)
2042 {
2043 switch (tpg->pattern) {
2044 case TPG_PAT_75_COLORBAR:
2045 case TPG_PAT_100_COLORBAR:
2046 case TPG_PAT_CSC_COLORBAR:
2047 case TPG_PAT_100_HCOLORBAR:
2048 return "White, yellow, cyan, green, magenta, red, blue, black";
2049 case TPG_PAT_BLACK:
2050 return "Black";
2051 case TPG_PAT_WHITE:
2052 return "White";
2053 case TPG_PAT_RED:
2054 return "Red";
2055 case TPG_PAT_GREEN:
2056 return "Green";
2057 case TPG_PAT_BLUE:
2058 return "Blue";
2059 default:
2060 return NULL;
2061 }
2062 }
2063
2064 void tpg_update_mv_step(struct tpg_data *tpg)
2065 {
2066 int factor = tpg->mv_hor_mode > TPG_MOVE_NONE ? -1 : 1;
2067
2068 if (tpg->hflip)
2069 factor = -factor;
2070 switch (tpg->mv_hor_mode) {
2071 case TPG_MOVE_NEG_FAST:
2072 case TPG_MOVE_POS_FAST:
2073 tpg->mv_hor_step = ((tpg->src_width + 319) / 320) * 4;
2074 break;
2075 case TPG_MOVE_NEG:
2076 case TPG_MOVE_POS:
2077 tpg->mv_hor_step = ((tpg->src_width + 639) / 640) * 4;
2078 break;
2079 case TPG_MOVE_NEG_SLOW:
2080 case TPG_MOVE_POS_SLOW:
2081 tpg->mv_hor_step = 2;
2082 break;
2083 case TPG_MOVE_NONE:
2084 tpg->mv_hor_step = 0;
2085 break;
2086 }
2087 if (factor < 0)
2088 tpg->mv_hor_step = tpg->src_width - tpg->mv_hor_step;
2089
2090 factor = tpg->mv_vert_mode > TPG_MOVE_NONE ? -1 : 1;
2091 switch (tpg->mv_vert_mode) {
2092 case TPG_MOVE_NEG_FAST:
2093 case TPG_MOVE_POS_FAST:
2094 tpg->mv_vert_step = ((tpg->src_width + 319) / 320) * 4;
2095 break;
2096 case TPG_MOVE_NEG:
2097 case TPG_MOVE_POS:
2098 tpg->mv_vert_step = ((tpg->src_width + 639) / 640) * 4;
2099 break;
2100 case TPG_MOVE_NEG_SLOW:
2101 case TPG_MOVE_POS_SLOW:
2102 tpg->mv_vert_step = 1;
2103 break;
2104 case TPG_MOVE_NONE:
2105 tpg->mv_vert_step = 0;
2106 break;
2107 }
2108 if (factor < 0)
2109 tpg->mv_vert_step = tpg->src_height - tpg->mv_vert_step;
2110 }
2111
2112 /* Map the line number relative to the crop rectangle to a frame line number */
2113 static unsigned tpg_calc_frameline(const struct tpg_data *tpg, unsigned src_y,
2114 unsigned field)
2115 {
2116 switch (field) {
2117 case V4L2_FIELD_TOP:
2118 return tpg->crop.top + src_y * 2;
2119 case V4L2_FIELD_BOTTOM:
2120 return tpg->crop.top + src_y * 2 + 1;
2121 default:
2122 return src_y + tpg->crop.top;
2123 }
2124 }
2125
2126 /*
2127 * Map the line number relative to the compose rectangle to a destination
2128 * buffer line number.
2129 */
2130 static unsigned tpg_calc_buffer_line(const struct tpg_data *tpg, unsigned y,
2131 unsigned field)
2132 {
2133 y += tpg->compose.top;
2134 switch (field) {
2135 case V4L2_FIELD_SEQ_TB:
2136 if (y & 1)
2137 return tpg->buf_height / 2 + y / 2;
2138 return y / 2;
2139 case V4L2_FIELD_SEQ_BT:
2140 if (y & 1)
2141 return y / 2;
2142 return tpg->buf_height / 2 + y / 2;
2143 default:
2144 return y;
2145 }
2146 }
2147
2148 static void tpg_recalc(struct tpg_data *tpg)
2149 {
2150 if (tpg->recalc_colors) {
2151 tpg->recalc_colors = false;
2152 tpg->recalc_lines = true;
2153 tpg->real_xfer_func = tpg->xfer_func;
2154 tpg->real_ycbcr_enc = tpg->ycbcr_enc;
2155 tpg->real_hsv_enc = tpg->hsv_enc;
2156 tpg->real_quantization = tpg->quantization;
2157
2158 if (tpg->xfer_func == V4L2_XFER_FUNC_DEFAULT)
2159 tpg->real_xfer_func =
2160 V4L2_MAP_XFER_FUNC_DEFAULT(tpg->colorspace);
2161
2162 if (tpg->ycbcr_enc == V4L2_YCBCR_ENC_DEFAULT)
2163 tpg->real_ycbcr_enc =
2164 V4L2_MAP_YCBCR_ENC_DEFAULT(tpg->colorspace);
2165
2166 if (tpg->quantization == V4L2_QUANTIZATION_DEFAULT)
2167 tpg->real_quantization =
2168 V4L2_MAP_QUANTIZATION_DEFAULT(
2169 tpg->color_enc != TGP_COLOR_ENC_YCBCR,
2170 tpg->colorspace, tpg->real_ycbcr_enc);
2171
2172 tpg_precalculate_colors(tpg);
2173 }
2174 if (tpg->recalc_square_border) {
2175 tpg->recalc_square_border = false;
2176 tpg_calculate_square_border(tpg);
2177 }
2178 if (tpg->recalc_lines) {
2179 tpg->recalc_lines = false;
2180 tpg_precalculate_line(tpg);
2181 }
2182 }
2183
2184 void tpg_calc_text_basep(struct tpg_data *tpg,
2185 u8 *basep[TPG_MAX_PLANES][2], unsigned p, u8 *vbuf)
2186 {
2187 unsigned stride = tpg->bytesperline[p];
2188 unsigned h = tpg->buf_height;
2189
2190 tpg_recalc(tpg);
2191
2192 basep[p][0] = vbuf;
2193 basep[p][1] = vbuf;
2194 h /= tpg->vdownsampling[p];
2195 if (tpg->field == V4L2_FIELD_SEQ_TB)
2196 basep[p][1] += h * stride / 2;
2197 else if (tpg->field == V4L2_FIELD_SEQ_BT)
2198 basep[p][0] += h * stride / 2;
2199 if (p == 0 && tpg->interleaved)
2200 tpg_calc_text_basep(tpg, basep, 1, vbuf);
2201 }
2202
2203 static int tpg_pattern_avg(const struct tpg_data *tpg,
2204 unsigned pat1, unsigned pat2)
2205 {
2206 unsigned pat_lines = tpg_get_pat_lines(tpg);
2207
2208 if (pat1 == (pat2 + 1) % pat_lines)
2209 return pat2;
2210 if (pat2 == (pat1 + 1) % pat_lines)
2211 return pat1;
2212 return -1;
2213 }
2214
2215 static const char *tpg_color_enc_str(enum tgp_color_enc
2216 color_enc)
2217 {
2218 switch (color_enc) {
2219 case TGP_COLOR_ENC_HSV:
2220 return "HSV";
2221 case TGP_COLOR_ENC_YCBCR:
2222 return "Y'CbCr";
2223 case TGP_COLOR_ENC_LUMA:
2224 return "Luma";
2225 case TGP_COLOR_ENC_RGB:
2226 default:
2227 return "R'G'B";
2228
2229 }
2230 }
2231
2232 void tpg_log_status(struct tpg_data *tpg)
2233 {
2234 pr_info("tpg source WxH: %ux%u (%s)\n",
2235 tpg->src_width, tpg->src_height,
2236 tpg_color_enc_str(tpg->color_enc));
2237 pr_info("tpg field: %u\n", tpg->field);
2238 pr_info("tpg crop: (%d,%d)/%ux%u\n", tpg->crop.left, tpg->crop.top,
2239 tpg->crop.width, tpg->crop.height);
2240 pr_info("tpg compose: (%d,%d)/%ux%u\n", tpg->compose.left, tpg->compose.top,
2241 tpg->compose.width, tpg->compose.height);
2242 pr_info("tpg colorspace: %d\n", tpg->colorspace);
2243 pr_info("tpg transfer function: %d/%d\n", tpg->xfer_func, tpg->real_xfer_func);
2244 if (tpg->color_enc == TGP_COLOR_ENC_HSV)
2245 pr_info("tpg HSV encoding: %d/%d\n",
2246 tpg->hsv_enc, tpg->real_hsv_enc);
2247 else if (tpg->color_enc == TGP_COLOR_ENC_YCBCR)
2248 pr_info("tpg Y'CbCr encoding: %d/%d\n",
2249 tpg->ycbcr_enc, tpg->real_ycbcr_enc);
2250 pr_info("tpg quantization: %d/%d\n", tpg->quantization, tpg->real_quantization);
2251 pr_info("tpg RGB range: %d/%d\n", tpg->rgb_range, tpg->real_rgb_range);
2252 }
2253
2254 /*
2255 * This struct contains common parameters used by both the drawing of the
2256 * test pattern and the drawing of the extras (borders, square, etc.)
2257 */
2258 struct tpg_draw_params {
2259 /* common data */
2260 bool is_tv;
2261 bool is_60hz;
2262 unsigned twopixsize;
2263 unsigned img_width;
2264 unsigned stride;
2265 unsigned hmax;
2266 unsigned frame_line;
2267 unsigned frame_line_next;
2268
2269 /* test pattern */
2270 unsigned mv_hor_old;
2271 unsigned mv_hor_new;
2272 unsigned mv_vert_old;
2273 unsigned mv_vert_new;
2274
2275 /* extras */
2276 unsigned wss_width;
2277 unsigned wss_random_offset;
2278 unsigned sav_eav_f;
2279 unsigned left_pillar_width;
2280 unsigned right_pillar_start;
2281 };
2282
2283 static void tpg_fill_params_pattern(const struct tpg_data *tpg, unsigned p,
2284 struct tpg_draw_params *params)
2285 {
2286 params->mv_hor_old =
2287 tpg_hscale_div(tpg, p, tpg->mv_hor_count % tpg->src_width);
2288 params->mv_hor_new =
2289 tpg_hscale_div(tpg, p, (tpg->mv_hor_count + tpg->mv_hor_step) %
2290 tpg->src_width);
2291 params->mv_vert_old = tpg->mv_vert_count % tpg->src_height;
2292 params->mv_vert_new =
2293 (tpg->mv_vert_count + tpg->mv_vert_step) % tpg->src_height;
2294 }
2295
2296 static void tpg_fill_params_extras(const struct tpg_data *tpg,
2297 unsigned p,
2298 struct tpg_draw_params *params)
2299 {
2300 unsigned left_pillar_width = 0;
2301 unsigned right_pillar_start = params->img_width;
2302
2303 params->wss_width = tpg->crop.left < tpg->src_width / 2 ?
2304 tpg->src_width / 2 - tpg->crop.left : 0;
2305 if (params->wss_width > tpg->crop.width)
2306 params->wss_width = tpg->crop.width;
2307 params->wss_width = tpg_hscale_div(tpg, p, params->wss_width);
2308 params->wss_random_offset =
2309 params->twopixsize * get_random_u32_below(tpg->src_width / 2);
2310
2311 if (tpg->crop.left < tpg->border.left) {
2312 left_pillar_width = tpg->border.left - tpg->crop.left;
2313 if (left_pillar_width > tpg->crop.width)
2314 left_pillar_width = tpg->crop.width;
2315 left_pillar_width = tpg_hscale_div(tpg, p, left_pillar_width);
2316 }
2317 params->left_pillar_width = left_pillar_width;
2318
2319 if (tpg->crop.left + tpg->crop.width >
2320 tpg->border.left + tpg->border.width) {
2321 right_pillar_start =
2322 tpg->border.left + tpg->border.width - tpg->crop.left;
2323 right_pillar_start =
2324 tpg_hscale_div(tpg, p, right_pillar_start);
2325 if (right_pillar_start > params->img_width)
2326 right_pillar_start = params->img_width;
2327 }
2328 params->right_pillar_start = right_pillar_start;
2329
2330 params->sav_eav_f = tpg->field ==
2331 (params->is_60hz ? V4L2_FIELD_TOP : V4L2_FIELD_BOTTOM);
2332 }
2333
2334 static void tpg_fill_plane_extras(const struct tpg_data *tpg,
2335 const struct tpg_draw_params *params,
2336 unsigned p, unsigned h, u8 *vbuf)
2337 {
2338 unsigned twopixsize = params->twopixsize;
2339 unsigned img_width = params->img_width;
2340 unsigned frame_line = params->frame_line;
2341 const struct v4l2_rect *sq = &tpg->square;
2342 const struct v4l2_rect *b = &tpg->border;
2343 const struct v4l2_rect *c = &tpg->crop;
2344
2345 if (params->is_tv && !params->is_60hz &&
2346 frame_line == 0 && params->wss_width) {
2347 /*
2348 * Replace the first half of the top line of a 50 Hz frame
2349 * with random data to simulate a WSS signal.
2350 */
2351 u8 *wss = tpg->random_line[p] + params->wss_random_offset;
2352
2353 memcpy(vbuf, wss, params->wss_width);
2354 }
2355
2356 if (tpg->show_border && frame_line >= b->top &&
2357 frame_line < b->top + b->height) {
2358 unsigned bottom = b->top + b->height - 1;
2359 unsigned left = params->left_pillar_width;
2360 unsigned right = params->right_pillar_start;
2361
2362 if (frame_line == b->top || frame_line == b->top + 1 ||
2363 frame_line == bottom || frame_line == bottom - 1) {
2364 memcpy(vbuf + left, tpg->contrast_line[p],
2365 right - left);
2366 } else {
2367 if (b->left >= c->left &&
2368 b->left < c->left + c->width)
2369 memcpy(vbuf + left,
2370 tpg->contrast_line[p], twopixsize);
2371 if (b->left + b->width > c->left &&
2372 b->left + b->width <= c->left + c->width)
2373 memcpy(vbuf + right - twopixsize,
2374 tpg->contrast_line[p], twopixsize);
2375 }
2376 }
2377 if (tpg->qual != TPG_QUAL_NOISE && frame_line >= b->top &&
2378 frame_line < b->top + b->height) {
2379 memcpy(vbuf, tpg->black_line[p], params->left_pillar_width);
2380 memcpy(vbuf + params->right_pillar_start, tpg->black_line[p],
2381 img_width - params->right_pillar_start);
2382 }
2383 if (tpg->show_square && frame_line >= sq->top &&
2384 frame_line < sq->top + sq->height &&
2385 sq->left < c->left + c->width &&
2386 sq->left + sq->width >= c->left) {
2387 unsigned left = sq->left;
2388 unsigned width = sq->width;
2389
2390 if (c->left > left) {
2391 width -= c->left - left;
2392 left = c->left;
2393 }
2394 if (c->left + c->width < left + width)
2395 width -= left + width - c->left - c->width;
2396 left -= c->left;
2397 left = tpg_hscale_div(tpg, p, left);
2398 width = tpg_hscale_div(tpg, p, width);
2399 memcpy(vbuf + left, tpg->contrast_line[p], width);
2400 }
2401 if (tpg->insert_sav) {
2402 unsigned offset = tpg_hdiv(tpg, p, tpg->compose.width / 3);
2403 u8 *p = vbuf + offset;
2404 unsigned vact = 0, hact = 0;
2405
2406 p[0] = 0xff;
2407 p[1] = 0;
2408 p[2] = 0;
2409 p[3] = 0x80 | (params->sav_eav_f << 6) |
2410 (vact << 5) | (hact << 4) |
2411 ((hact ^ vact) << 3) |
2412 ((hact ^ params->sav_eav_f) << 2) |
2413 ((params->sav_eav_f ^ vact) << 1) |
2414 (hact ^ vact ^ params->sav_eav_f);
2415 }
2416 if (tpg->insert_eav) {
2417 unsigned offset = tpg_hdiv(tpg, p, tpg->compose.width * 2 / 3);
2418 u8 *p = vbuf + offset;
2419 unsigned vact = 0, hact = 1;
2420
2421 p[0] = 0xff;
2422 p[1] = 0;
2423 p[2] = 0;
2424 p[3] = 0x80 | (params->sav_eav_f << 6) |
2425 (vact << 5) | (hact << 4) |
2426 ((hact ^ vact) << 3) |
2427 ((hact ^ params->sav_eav_f) << 2) |
2428 ((params->sav_eav_f ^ vact) << 1) |
2429 (hact ^ vact ^ params->sav_eav_f);
2430 }
2431 if (tpg->insert_hdmi_video_guard_band) {
2432 unsigned int i;
2433
2434 switch (tpg->fourcc) {
2435 case V4L2_PIX_FMT_BGR24:
2436 case V4L2_PIX_FMT_RGB24:
2437 for (i = 0; i < 3 * 4; i += 3) {
2438 vbuf[i] = 0xab;
2439 vbuf[i + 1] = 0x55;
2440 vbuf[i + 2] = 0xab;
2441 }
2442 break;
2443 case V4L2_PIX_FMT_RGB32:
2444 case V4L2_PIX_FMT_ARGB32:
2445 case V4L2_PIX_FMT_XRGB32:
2446 case V4L2_PIX_FMT_BGRX32:
2447 case V4L2_PIX_FMT_BGRA32:
2448 for (i = 0; i < 4 * 4; i += 4) {
2449 vbuf[i] = 0x00;
2450 vbuf[i + 1] = 0xab;
2451 vbuf[i + 2] = 0x55;
2452 vbuf[i + 3] = 0xab;
2453 }
2454 break;
2455 case V4L2_PIX_FMT_BGR32:
2456 case V4L2_PIX_FMT_XBGR32:
2457 case V4L2_PIX_FMT_ABGR32:
2458 case V4L2_PIX_FMT_RGBX32:
2459 case V4L2_PIX_FMT_RGBA32:
2460 for (i = 0; i < 4 * 4; i += 4) {
2461 vbuf[i] = 0xab;
2462 vbuf[i + 1] = 0x55;
2463 vbuf[i + 2] = 0xab;
2464 vbuf[i + 3] = 0x00;
2465 }
2466 break;
2467 }
2468 }
2469 }
2470
2471 static void tpg_fill_plane_pattern(const struct tpg_data *tpg,
2472 const struct tpg_draw_params *params,
2473 unsigned p, unsigned h, u8 *vbuf)
2474 {
2475 unsigned twopixsize = params->twopixsize;
2476 unsigned img_width = params->img_width;
2477 unsigned mv_hor_old = params->mv_hor_old;
2478 unsigned mv_hor_new = params->mv_hor_new;
2479 unsigned mv_vert_old = params->mv_vert_old;
2480 unsigned mv_vert_new = params->mv_vert_new;
2481 unsigned frame_line = params->frame_line;
2482 unsigned frame_line_next = params->frame_line_next;
2483 unsigned line_offset = tpg_hscale_div(tpg, p, tpg->crop.left);
2484 bool even;
2485 bool fill_blank = false;
2486 unsigned pat_line_old;
2487 unsigned pat_line_new;
2488 u8 *linestart_older;
2489 u8 *linestart_newer;
2490 u8 *linestart_top;
2491 u8 *linestart_bottom;
2492
2493 even = !(frame_line & 1);
2494
2495 if (h >= params->hmax) {
2496 if (params->hmax == tpg->compose.height)
2497 return;
2498 if (!tpg->perc_fill_blank)
2499 return;
2500 fill_blank = true;
2501 }
2502
2503 if (tpg->vflip) {
2504 frame_line = tpg->src_height - frame_line - 1;
2505 frame_line_next = tpg->src_height - frame_line_next - 1;
2506 }
2507
2508 if (fill_blank) {
2509 linestart_older = tpg->contrast_line[p];
2510 linestart_newer = tpg->contrast_line[p];
2511 } else if (tpg->qual != TPG_QUAL_NOISE &&
2512 (frame_line < tpg->border.top ||
2513 frame_line >= tpg->border.top + tpg->border.height)) {
2514 linestart_older = tpg->black_line[p];
2515 linestart_newer = tpg->black_line[p];
2516 } else if (tpg->pattern == TPG_PAT_NOISE || tpg->qual == TPG_QUAL_NOISE) {
2517 linestart_older = tpg->random_line[p] +
2518 twopixsize * get_random_u32_below(tpg->src_width / 2);
2519 linestart_newer = tpg->random_line[p] +
2520 twopixsize * get_random_u32_below(tpg->src_width / 2);
2521 } else {
2522 unsigned frame_line_old =
2523 (frame_line + mv_vert_old) % tpg->src_height;
2524 unsigned frame_line_new =
2525 (frame_line + mv_vert_new) % tpg->src_height;
2526 unsigned pat_line_next_old;
2527 unsigned pat_line_next_new;
2528
2529 pat_line_old = tpg_get_pat_line(tpg, frame_line_old);
2530 pat_line_new = tpg_get_pat_line(tpg, frame_line_new);
2531 linestart_older = tpg->lines[pat_line_old][p] + mv_hor_old;
2532 linestart_newer = tpg->lines[pat_line_new][p] + mv_hor_new;
2533
2534 if (tpg->vdownsampling[p] > 1 && frame_line != frame_line_next) {
2535 int avg_pat;
2536
2537 /*
2538 * Now decide whether we need to use downsampled_lines[].
2539 * That's necessary if the two lines use different patterns.
2540 */
2541 pat_line_next_old = tpg_get_pat_line(tpg,
2542 (frame_line_next + mv_vert_old) % tpg->src_height);
2543 pat_line_next_new = tpg_get_pat_line(tpg,
2544 (frame_line_next + mv_vert_new) % tpg->src_height);
2545
2546 switch (tpg->field) {
2547 case V4L2_FIELD_INTERLACED:
2548 case V4L2_FIELD_INTERLACED_BT:
2549 case V4L2_FIELD_INTERLACED_TB:
2550 avg_pat = tpg_pattern_avg(tpg, pat_line_old, pat_line_new);
2551 if (avg_pat < 0)
2552 break;
2553 linestart_older = tpg->downsampled_lines[avg_pat][p] + mv_hor_old;
2554 linestart_newer = linestart_older;
2555 break;
2556 case V4L2_FIELD_NONE:
2557 case V4L2_FIELD_TOP:
2558 case V4L2_FIELD_BOTTOM:
2559 case V4L2_FIELD_SEQ_BT:
2560 case V4L2_FIELD_SEQ_TB:
2561 avg_pat = tpg_pattern_avg(tpg, pat_line_old, pat_line_next_old);
2562 if (avg_pat >= 0)
2563 linestart_older = tpg->downsampled_lines[avg_pat][p] +
2564 mv_hor_old;
2565 avg_pat = tpg_pattern_avg(tpg, pat_line_new, pat_line_next_new);
2566 if (avg_pat >= 0)
2567 linestart_newer = tpg->downsampled_lines[avg_pat][p] +
2568 mv_hor_new;
2569 break;
2570 }
2571 }
2572 linestart_older += line_offset;
2573 linestart_newer += line_offset;
2574 }
2575 if (tpg->field_alternate) {
2576 linestart_top = linestart_bottom = linestart_older;
2577 } else if (params->is_60hz) {
2578 linestart_top = linestart_newer;
2579 linestart_bottom = linestart_older;
2580 } else {
2581 linestart_top = linestart_older;
2582 linestart_bottom = linestart_newer;
2583 }
2584
2585 switch (tpg->field) {
2586 case V4L2_FIELD_INTERLACED:
2587 case V4L2_FIELD_INTERLACED_TB:
2588 case V4L2_FIELD_SEQ_TB:
2589 case V4L2_FIELD_SEQ_BT:
2590 if (even)
2591 memcpy(vbuf, linestart_top, img_width);
2592 else
2593 memcpy(vbuf, linestart_bottom, img_width);
2594 break;
2595 case V4L2_FIELD_INTERLACED_BT:
2596 if (even)
2597 memcpy(vbuf, linestart_bottom, img_width);
2598 else
2599 memcpy(vbuf, linestart_top, img_width);
2600 break;
2601 case V4L2_FIELD_TOP:
2602 memcpy(vbuf, linestart_top, img_width);
2603 break;
2604 case V4L2_FIELD_BOTTOM:
2605 memcpy(vbuf, linestart_bottom, img_width);
2606 break;
2607 case V4L2_FIELD_NONE:
2608 default:
2609 memcpy(vbuf, linestart_older, img_width);
2610 break;
2611 }
2612 }
2613
2614 void tpg_fill_plane_buffer(struct tpg_data *tpg, v4l2_std_id std,
2615 unsigned p, u8 *vbuf)
2616 {
2617 struct tpg_draw_params params;
2618 unsigned factor = V4L2_FIELD_HAS_T_OR_B(tpg->field) ? 2 : 1;
2619
2620 /* Coarse scaling with Bresenham */
2621 unsigned int_part = (tpg->crop.height / factor) / tpg->compose.height;
2622 unsigned fract_part = (tpg->crop.height / factor) % tpg->compose.height;
2623 unsigned src_y = 0;
2624 unsigned error = 0;
2625 unsigned h;
2626
2627 tpg_recalc(tpg);
2628
2629 params.is_tv = std;
2630 params.is_60hz = std & V4L2_STD_525_60;
2631 params.twopixsize = tpg->twopixelsize[p];
2632 params.img_width = tpg_hdiv(tpg, p, tpg->compose.width);
2633 params.stride = tpg->bytesperline[p];
2634 params.hmax = (tpg->compose.height * tpg->perc_fill) / 100;
2635
2636 tpg_fill_params_pattern(tpg, p, &params);
2637 tpg_fill_params_extras(tpg, p, &params);
2638
2639 vbuf += tpg_hdiv(tpg, p, tpg->compose.left);
2640
2641 for (h = 0; h < tpg->compose.height; h++) {
2642 unsigned buf_line;
2643
2644 params.frame_line = tpg_calc_frameline(tpg, src_y, tpg->field);
2645 params.frame_line_next = params.frame_line;
2646 buf_line = tpg_calc_buffer_line(tpg, h, tpg->field);
2647 src_y += int_part;
2648 error += fract_part;
2649 if (error >= tpg->compose.height) {
2650 error -= tpg->compose.height;
2651 src_y++;
2652 }
2653
2654 /*
2655 * For line-interleaved formats determine the 'plane'
2656 * based on the buffer line.
2657 */
2658 if (tpg_g_interleaved(tpg))
2659 p = tpg_g_interleaved_plane(tpg, buf_line);
2660
2661 if (tpg->vdownsampling[p] > 1) {
2662 /*
2663 * When doing vertical downsampling the field setting
2664 * matters: for SEQ_BT/TB we downsample each field
2665 * separately (i.e. lines 0+2 are combined, as are
2666 * lines 1+3), for the other field settings we combine
2667 * odd and even lines. Doing that for SEQ_BT/TB would
2668 * be really weird.
2669 */
2670 if (tpg->field == V4L2_FIELD_SEQ_BT ||
2671 tpg->field == V4L2_FIELD_SEQ_TB) {
2672 unsigned next_src_y = src_y;
2673
2674 if ((h & 3) >= 2)
2675 continue;
2676 next_src_y += int_part;
2677 if (error + fract_part >= tpg->compose.height)
2678 next_src_y++;
2679 params.frame_line_next =
2680 tpg_calc_frameline(tpg, next_src_y, tpg->field);
2681 } else {
2682 if (h & 1)
2683 continue;
2684 params.frame_line_next =
2685 tpg_calc_frameline(tpg, src_y, tpg->field);
2686 }
2687
2688 buf_line /= tpg->vdownsampling[p];
2689 }
2690 tpg_fill_plane_pattern(tpg, &params, p, h,
2691 vbuf + buf_line * params.stride);
2692 tpg_fill_plane_extras(tpg, &params, p, h,
2693 vbuf + buf_line * params.stride);
2694 }
2695 }
2696
2697 void tpg_fillbuffer(struct tpg_data *tpg, v4l2_std_id std, unsigned p, u8 *vbuf)
2698 {
2699 unsigned offset = 0;
2700 unsigned i;
2701
2702 if (tpg->buffers > 1) {
2703 tpg_fill_plane_buffer(tpg, std, p, vbuf);
2704 return;
2705 }
2706
2707 for (i = 0; i < tpg_g_planes(tpg); i++) {
2708 tpg_fill_plane_buffer(tpg, std, i, vbuf + offset);
2709 offset += tpg_calc_plane_size(tpg, i);
2710 }
2711 }