Skip to content

Commit f208b26

Browse files
slongerbeampH5
authored andcommitted
gpu: ipu-v3: ipu-ic: Fully describe colorspace conversions
Only providing the input and output RGB/YUV space to the IC task init functions is not sufficient. To fully characterize a colorspace conversion, the Y'CbCr encoding standard, and quantization also need to be specified. Define a 'struct ipu_ic_colorspace' that includes all the above. This allows to actually enforce the fact that the IC: - can only encode to/from YUV and RGB full range. A follow-up patch will remove this restriction. - can only encode using BT.601 standard. A follow-up patch will add Rec.709 encoding support. The determination of the CSC coefficients based on the input/output 'struct ipu_ic_colorspace' are moved to a new exported function ipu_ic_calc_csc(), and 'struct ic_csc_params' is exported as 'struct ipu_ic_csc_params'. ipu_ic_calc_csc() fills a 'struct ipu_ic_csc' with the input/output 'struct ipu_ic_colorspace' and the calculated 'struct ic_csc_params' from those input/output colorspaces. The functions ipu_ic_task_init(_rsc)() now take a filled 'struct ipu_ic_csc'. The existing CSC coefficient tables and ipu_ic_calc_csc() are moved to a new module ipu-ic-csc.c. This is in preparation for adding more coefficient tables for limited range quantization and more encoding standards. The existing ycbcr2rgb and inverse rgb2ycbcr tables defined the BT.601 Y'CbCr encoding coefficients. The rgb2ycbcr table specifically described the BT.601 encoding from full range RGB to full range YUV. Table comments have been added in ipu-ic-csc.c to make this more clear. The ycbcr2rgb inverse table described encoding YUV limited range to RGB full range. To be consistent with the rgb2ycbcr table, this table is converted to YUV full range to RGB full range, and the comments are expanded in ipu-ic-csc.c. The ic_csc_rgb2rgb table was just an identity matrix, so it is renamed 'identity' in ipu-ic-csc.c. Signed-off-by: Steve Longerbeam <slongerbeam@gmail.com> [p.zabel@pengutronix.de: removed a superfluous blank line] Signed-off-by: Philipp Zabel <p.zabel@pengutronix.de>
1 parent 3d1f62c commit f208b26

6 files changed

Lines changed: 270 additions & 118 deletions

File tree

‎drivers/gpu/ipu-v3/Makefile‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
obj-$(CONFIG_IMX_IPUV3_CORE) += imx-ipu-v3.o
33

44
imx-ipu-v3-objs := ipu-common.o ipu-cpmem.o ipu-csi.o ipu-dc.o ipu-di.o \
5-
ipu-dp.o ipu-dmfc.o ipu-ic.o ipu-image-convert.o \
6-
ipu-smfc.o ipu-vdi.o
5+
ipu-dp.o ipu-dmfc.o ipu-ic.o ipu-ic-csc.o \
6+
ipu-image-convert.o ipu-smfc.o ipu-vdi.o
77

88
ifdef CONFIG_DRM
99
imx-ipu-v3-objs += ipu-pre.o ipu-prg.o

‎drivers/gpu/ipu-v3/ipu-ic-csc.c‎

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
// SPDX-License-Identifier: GPL-2.0+
2+
/*
3+
* Copyright (C) 2019 Mentor Graphics Inc.
4+
*/
5+
6+
#include <linux/types.h>
7+
#include <linux/init.h>
8+
#include <linux/errno.h>
9+
#include <linux/err.h>
10+
#include <linux/sizes.h>
11+
#include "ipu-prv.h"
12+
13+
/* identity matrix */
14+
static const struct ipu_ic_csc_params identity = {
15+
.coeff = {
16+
{ 128, 0, 0, },
17+
{ 0, 128, 0, },
18+
{ 0, 0, 128, },
19+
},
20+
.offset = { 0, 0, 0, },
21+
.scale = 2,
22+
};
23+
24+
static const struct ipu_ic_csc_params *rgb2rgb[] = {
25+
&identity,
26+
};
27+
28+
static const struct ipu_ic_csc_params *yuv2yuv[] = {
29+
&identity,
30+
};
31+
32+
/*
33+
* BT.601 RGB full-range to YUV full-range
34+
*
35+
* Y = .2990 * R + .5870 * G + .1140 * B
36+
* U = -.1687 * R - .3313 * G + .5000 * B + 128
37+
* V = .5000 * R - .4187 * G - .0813 * B + 128
38+
*/
39+
static const struct ipu_ic_csc_params rgbf2yuvf_601 = {
40+
.coeff = {
41+
{ 77, 150, 29, },
42+
{ -43, -85, 128, },
43+
{ 128, -107, -21, },
44+
},
45+
.offset = { 0, 512, 512, },
46+
.scale = 1,
47+
};
48+
49+
/*
50+
* BT.601 YUV full-range to RGB full-range
51+
*
52+
* R = 1. * Y + 0 * (Cb - 128) + 1.4020 * (Cr - 128)
53+
* G = 1. * Y - .3441 * (Cb - 128) - .7141 * (Cr - 128)
54+
* B = 1. * Y + 1.7720 * (Cb - 128) + 0 * (Cr - 128)
55+
*
56+
* equivalently (factoring out the offsets):
57+
*
58+
* R = 1. * Y + 0 * Cb + 1.4020 * Cr - 179.456
59+
* G = 1. * Y - .3441 * Cb - .7141 * Cr + 135.450
60+
* B = 1. * Y + 1.7720 * Cb + 0 * Cr - 226.816
61+
*/
62+
static const struct ipu_ic_csc_params yuvf2rgbf_601 = {
63+
.coeff = {
64+
{ 128, 0, 179, },
65+
{ 128, -44, -91, },
66+
{ 128, 227, 0, },
67+
},
68+
.offset = { -359, 271, -454, },
69+
.scale = 2,
70+
};
71+
72+
static const struct ipu_ic_csc_params *rgb2yuv_601[] = {
73+
&rgbf2yuvf_601,
74+
};
75+
76+
static const struct ipu_ic_csc_params *yuv2rgb_601[] = {
77+
&yuvf2rgbf_601,
78+
};
79+
80+
static int calc_csc_coeffs(struct ipu_ic_csc *csc)
81+
{
82+
if (csc->out_cs.enc != V4L2_YCBCR_ENC_601)
83+
return -ENOTSUPP;
84+
85+
if ((csc->in_cs.cs == IPUV3_COLORSPACE_YUV &&
86+
csc->in_cs.quant != V4L2_QUANTIZATION_FULL_RANGE) ||
87+
(csc->out_cs.cs == IPUV3_COLORSPACE_YUV &&
88+
csc->out_cs.quant != V4L2_QUANTIZATION_FULL_RANGE))
89+
return -ENOTSUPP;
90+
91+
if ((csc->in_cs.cs == IPUV3_COLORSPACE_RGB &&
92+
csc->in_cs.quant != V4L2_QUANTIZATION_FULL_RANGE) ||
93+
(csc->out_cs.cs == IPUV3_COLORSPACE_RGB &&
94+
csc->out_cs.quant != V4L2_QUANTIZATION_FULL_RANGE))
95+
return -ENOTSUPP;
96+
97+
if (csc->in_cs.cs == csc->out_cs.cs) {
98+
csc->params = (csc->in_cs.cs == IPUV3_COLORSPACE_YUV) ?
99+
*yuv2yuv[0] : *rgb2rgb[0];
100+
return 0;
101+
}
102+
103+
csc->params = (csc->in_cs.cs == IPUV3_COLORSPACE_YUV) ?
104+
*yuv2rgb_601[0] : *rgb2yuv_601[0];
105+
106+
return 0;
107+
}
108+
109+
int __ipu_ic_calc_csc(struct ipu_ic_csc *csc)
110+
{
111+
return calc_csc_coeffs(csc);
112+
}
113+
EXPORT_SYMBOL_GPL(__ipu_ic_calc_csc);
114+
115+
int ipu_ic_calc_csc(struct ipu_ic_csc *csc,
116+
enum v4l2_ycbcr_encoding in_enc,
117+
enum v4l2_quantization in_quant,
118+
enum ipu_color_space in_cs,
119+
enum v4l2_ycbcr_encoding out_enc,
120+
enum v4l2_quantization out_quant,
121+
enum ipu_color_space out_cs)
122+
{
123+
ipu_ic_fill_colorspace(&csc->in_cs, in_enc, in_quant, in_cs);
124+
ipu_ic_fill_colorspace(&csc->out_cs, out_enc, out_quant, out_cs);
125+
126+
return __ipu_ic_calc_csc(csc);
127+
}
128+
EXPORT_SYMBOL_GPL(ipu_ic_calc_csc);

‎drivers/gpu/ipu-v3/ipu-ic.c‎

Lines changed: 45 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -140,8 +140,10 @@ struct ipu_ic {
140140
const struct ic_task_regoffs *reg;
141141
const struct ic_task_bitfields *bit;
142142

143-
enum ipu_color_space in_cs, g_in_cs;
144-
enum ipu_color_space out_cs;
143+
struct ipu_ic_colorspace in_cs;
144+
struct ipu_ic_colorspace g_in_cs;
145+
struct ipu_ic_colorspace out_cs;
146+
145147
bool graphics;
146148
bool rotation;
147149
bool in_use;
@@ -169,60 +171,11 @@ static inline void ipu_ic_write(struct ipu_ic *ic, u32 value, unsigned offset)
169171
writel(value, ic->priv->base + offset);
170172
}
171173

172-
struct ic_csc_params {
173-
s16 coeff[3][3]; /* signed 9-bit integer coefficients */
174-
s16 offset[3]; /* signed 11+2-bit fixed point offset */
175-
u8 scale:2; /* scale coefficients * 2^(scale-1) */
176-
bool sat:1; /* saturate to (16, 235(Y) / 240(U, V)) */
177-
};
178-
179-
/*
180-
* Y = R * .299 + G * .587 + B * .114;
181-
* U = R * -.169 + G * -.332 + B * .500 + 128.;
182-
* V = R * .500 + G * -.419 + B * -.0813 + 128.;
183-
*/
184-
static const struct ic_csc_params ic_csc_rgb2ycbcr = {
185-
.coeff = {
186-
{ 77, 150, 29 },
187-
{ 469, 427, 128 },
188-
{ 128, 405, 491 },
189-
},
190-
.offset = { 0, 512, 512 },
191-
.scale = 1,
192-
};
193-
194-
/* transparent RGB->RGB matrix for graphics combining */
195-
static const struct ic_csc_params ic_csc_rgb2rgb = {
196-
.coeff = {
197-
{ 128, 0, 0 },
198-
{ 0, 128, 0 },
199-
{ 0, 0, 128 },
200-
},
201-
.scale = 2,
202-
};
203-
204-
/*
205-
* R = (1.164 * (Y - 16)) + (1.596 * (Cr - 128));
206-
* G = (1.164 * (Y - 16)) - (0.392 * (Cb - 128)) - (0.813 * (Cr - 128));
207-
* B = (1.164 * (Y - 16)) + (2.017 * (Cb - 128);
208-
*/
209-
static const struct ic_csc_params ic_csc_ycbcr2rgb = {
210-
.coeff = {
211-
{ 149, 0, 204 },
212-
{ 149, 462, 408 },
213-
{ 149, 255, 0 },
214-
},
215-
.offset = { -446, 266, -554 },
216-
.scale = 2,
217-
};
218-
219174
static int init_csc(struct ipu_ic *ic,
220-
enum ipu_color_space inf,
221-
enum ipu_color_space outf,
175+
const struct ipu_ic_csc *csc,
222176
int csc_index)
223177
{
224178
struct ipu_ic_priv *priv = ic->priv;
225-
const struct ic_csc_params *params;
226179
u32 __iomem *base;
227180
const u16 (*c)[3];
228181
const u16 *a;
@@ -231,27 +184,16 @@ static int init_csc(struct ipu_ic *ic,
231184
base = (u32 __iomem *)
232185
(priv->tpmem_base + ic->reg->tpmem_csc[csc_index]);
233186

234-
if (inf == IPUV3_COLORSPACE_YUV && outf == IPUV3_COLORSPACE_RGB)
235-
params = &ic_csc_ycbcr2rgb;
236-
else if (inf == IPUV3_COLORSPACE_RGB && outf == IPUV3_COLORSPACE_YUV)
237-
params = &ic_csc_rgb2ycbcr;
238-
else if (inf == IPUV3_COLORSPACE_RGB && outf == IPUV3_COLORSPACE_RGB)
239-
params = &ic_csc_rgb2rgb;
240-
else {
241-
dev_err(priv->ipu->dev, "Unsupported color space conversion\n");
242-
return -EINVAL;
243-
}
244-
245187
/* Cast to unsigned */
246-
c = (const u16 (*)[3])params->coeff;
247-
a = (const u16 *)params->offset;
188+
c = (const u16 (*)[3])csc->params.coeff;
189+
a = (const u16 *)csc->params.offset;
248190

249191
param = ((a[0] & 0x1f) << 27) | ((c[0][0] & 0x1ff) << 18) |
250192
((c[1][1] & 0x1ff) << 9) | (c[2][2] & 0x1ff);
251193
writel(param, base++);
252194

253-
param = ((a[0] & 0x1fe0) >> 5) | (params->scale << 8) |
254-
(params->sat << 10);
195+
param = ((a[0] & 0x1fe0) >> 5) | (csc->params.scale << 8) |
196+
(csc->params.sat << 10);
255197
writel(param, base++);
256198

257199
param = ((a[1] & 0x1f) << 27) | ((c[0][1] & 0x1ff) << 18) |
@@ -338,14 +280,14 @@ void ipu_ic_task_enable(struct ipu_ic *ic)
338280
if (ic->rotation)
339281
ic_conf |= ic->bit->ic_conf_rot_en;
340282

341-
if (ic->in_cs != ic->out_cs)
283+
if (ic->in_cs.cs != ic->out_cs.cs)
342284
ic_conf |= ic->bit->ic_conf_csc1_en;
343285

344286
if (ic->graphics) {
345287
ic_conf |= ic->bit->ic_conf_cmb_en;
346288
ic_conf |= ic->bit->ic_conf_csc1_en;
347289

348-
if (ic->g_in_cs != ic->out_cs)
290+
if (ic->g_in_cs.cs != ic->out_cs.cs)
349291
ic_conf |= ic->bit->ic_conf_csc2_en;
350292
}
351293

@@ -380,11 +322,12 @@ void ipu_ic_task_disable(struct ipu_ic *ic)
380322
EXPORT_SYMBOL_GPL(ipu_ic_task_disable);
381323

382324
int ipu_ic_task_graphics_init(struct ipu_ic *ic,
383-
enum ipu_color_space in_g_cs,
325+
const struct ipu_ic_colorspace *g_in_cs,
384326
bool galpha_en, u32 galpha,
385327
bool colorkey_en, u32 colorkey)
386328
{
387329
struct ipu_ic_priv *priv = ic->priv;
330+
struct ipu_ic_csc csc2;
388331
unsigned long flags;
389332
u32 reg, ic_conf;
390333
int ret = 0;
@@ -397,21 +340,36 @@ int ipu_ic_task_graphics_init(struct ipu_ic *ic,
397340
ic_conf = ipu_ic_read(ic, IC_CONF);
398341

399342
if (!(ic_conf & ic->bit->ic_conf_csc1_en)) {
400-
/* need transparent CSC1 conversion */
401-
ret = init_csc(ic, IPUV3_COLORSPACE_RGB,
402-
IPUV3_COLORSPACE_RGB, 0);
343+
struct ipu_ic_csc csc1;
344+
345+
ret = ipu_ic_calc_csc(&csc1,
346+
V4L2_YCBCR_ENC_601,
347+
V4L2_QUANTIZATION_FULL_RANGE,
348+
IPUV3_COLORSPACE_RGB,
349+
V4L2_YCBCR_ENC_601,
350+
V4L2_QUANTIZATION_FULL_RANGE,
351+
IPUV3_COLORSPACE_RGB);
403352
if (ret)
404353
goto unlock;
405-
}
406-
407-
ic->g_in_cs = in_g_cs;
408354

409-
if (ic->g_in_cs != ic->out_cs) {
410-
ret = init_csc(ic, ic->g_in_cs, ic->out_cs, 1);
355+
/* need transparent CSC1 conversion */
356+
ret = init_csc(ic, &csc1, 0);
411357
if (ret)
412358
goto unlock;
413359
}
414360

361+
ic->g_in_cs = *g_in_cs;
362+
csc2.in_cs = ic->g_in_cs;
363+
csc2.out_cs = ic->out_cs;
364+
365+
ret = __ipu_ic_calc_csc(&csc2);
366+
if (ret)
367+
goto unlock;
368+
369+
ret = init_csc(ic, &csc2, 1);
370+
if (ret)
371+
goto unlock;
372+
415373
if (galpha_en) {
416374
ic_conf |= IC_CONF_IC_GLB_LOC_A;
417375
reg = ipu_ic_read(ic, IC_CMBP_1);
@@ -437,10 +395,9 @@ int ipu_ic_task_graphics_init(struct ipu_ic *ic,
437395
EXPORT_SYMBOL_GPL(ipu_ic_task_graphics_init);
438396

439397
int ipu_ic_task_init_rsc(struct ipu_ic *ic,
398+
const struct ipu_ic_csc *csc,
440399
int in_width, int in_height,
441400
int out_width, int out_height,
442-
enum ipu_color_space in_cs,
443-
enum ipu_color_space out_cs,
444401
u32 rsc)
445402
{
446403
struct ipu_ic_priv *priv = ic->priv;
@@ -472,28 +429,23 @@ int ipu_ic_task_init_rsc(struct ipu_ic *ic,
472429
ipu_ic_write(ic, rsc, ic->reg->rsc);
473430

474431
/* Setup color space conversion */
475-
ic->in_cs = in_cs;
476-
ic->out_cs = out_cs;
432+
ic->in_cs = csc->in_cs;
433+
ic->out_cs = csc->out_cs;
477434

478-
if (ic->in_cs != ic->out_cs) {
479-
ret = init_csc(ic, ic->in_cs, ic->out_cs, 0);
480-
if (ret)
481-
goto unlock;
482-
}
435+
ret = init_csc(ic, csc, 0);
483436

484-
unlock:
485437
spin_unlock_irqrestore(&priv->lock, flags);
486438
return ret;
487439
}
488440

489441
int ipu_ic_task_init(struct ipu_ic *ic,
442+
const struct ipu_ic_csc *csc,
490443
int in_width, int in_height,
491-
int out_width, int out_height,
492-
enum ipu_color_space in_cs,
493-
enum ipu_color_space out_cs)
444+
int out_width, int out_height)
494445
{
495-
return ipu_ic_task_init_rsc(ic, in_width, in_height, out_width,
496-
out_height, in_cs, out_cs, 0);
446+
return ipu_ic_task_init_rsc(ic, csc,
447+
in_width, in_height,
448+
out_width, out_height, 0);
497449
}
498450
EXPORT_SYMBOL_GPL(ipu_ic_task_init);
499451

0 commit comments

Comments
 (0)