2 * Copyright (c) 2016 Ronald S. Bultje <rsbultje@gmail.com>
4 * This file is part of FFmpeg.
6 * FFmpeg is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * FFmpeg is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with FFmpeg; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 * Convert between colorspaces.
26 #include "libavutil/avassert.h"
27 #include "libavutil/opt.h"
28 #include "libavutil/pixdesc.h"
29 #include "libavutil/pixfmt.h"
32 #include "colorspacedsp.h"
63 enum WhitepointAdaptation {
66 NB_WP_ADAPT_NON_IDENTITY,
67 WP_ADAPT_IDENTITY = NB_WP_ADAPT_NON_IDENTITY,
71 static const enum AVColorTransferCharacteristic default_trc[CS_NB + 1] = {
72 [CS_UNSPECIFIED] = AVCOL_TRC_UNSPECIFIED,
73 [CS_BT470M] = AVCOL_TRC_GAMMA22,
74 [CS_BT470BG] = AVCOL_TRC_GAMMA28,
75 [CS_BT601_6_525] = AVCOL_TRC_SMPTE170M,
76 [CS_BT601_6_625] = AVCOL_TRC_SMPTE170M,
77 [CS_BT709] = AVCOL_TRC_BT709,
78 [CS_SMPTE170M] = AVCOL_TRC_SMPTE170M,
79 [CS_SMPTE240M] = AVCOL_TRC_SMPTE240M,
80 [CS_BT2020] = AVCOL_TRC_BT2020_10,
81 [CS_NB] = AVCOL_TRC_UNSPECIFIED,
84 static const enum AVColorPrimaries default_prm[CS_NB + 1] = {
85 [CS_UNSPECIFIED] = AVCOL_PRI_UNSPECIFIED,
86 [CS_BT470M] = AVCOL_PRI_BT470M,
87 [CS_BT470BG] = AVCOL_PRI_BT470BG,
88 [CS_BT601_6_525] = AVCOL_PRI_SMPTE170M,
89 [CS_BT601_6_625] = AVCOL_PRI_BT470BG,
90 [CS_BT709] = AVCOL_PRI_BT709,
91 [CS_SMPTE170M] = AVCOL_PRI_SMPTE170M,
92 [CS_SMPTE240M] = AVCOL_PRI_SMPTE240M,
93 [CS_BT2020] = AVCOL_PRI_BT2020,
94 [CS_NB] = AVCOL_PRI_UNSPECIFIED,
97 static const enum AVColorSpace default_csp[CS_NB + 1] = {
98 [CS_UNSPECIFIED] = AVCOL_SPC_UNSPECIFIED,
99 [CS_BT470M] = AVCOL_SPC_SMPTE170M,
100 [CS_BT470BG] = AVCOL_SPC_BT470BG,
101 [CS_BT601_6_525] = AVCOL_SPC_SMPTE170M,
102 [CS_BT601_6_625] = AVCOL_SPC_BT470BG,
103 [CS_BT709] = AVCOL_SPC_BT709,
104 [CS_SMPTE170M] = AVCOL_SPC_SMPTE170M,
105 [CS_SMPTE240M] = AVCOL_SPC_SMPTE240M,
106 [CS_BT2020] = AVCOL_SPC_BT2020_NCL,
107 [CS_NB] = AVCOL_SPC_UNSPECIFIED,
110 struct ColorPrimaries {
112 double xr, yr, xg, yg, xb, yb;
115 struct TransferCharacteristics {
116 double alpha, beta, gamma, delta;
119 struct LumaCoefficients {
123 struct WhitepointCoefficients {
127 typedef struct ColorSpaceContext {
128 const AVClass *class;
130 ColorSpaceDSPContext dsp;
132 enum Colorspace user_all, user_iall;
133 enum AVColorSpace in_csp, out_csp, user_csp, user_icsp;
134 enum AVColorRange in_rng, out_rng, user_rng, user_irng;
135 enum AVColorTransferCharacteristic in_trc, out_trc, user_trc, user_itrc;
136 enum AVColorPrimaries in_prm, out_prm, user_prm, user_iprm;
137 enum AVPixelFormat in_format, user_format;
139 enum DitherMode dither;
140 enum WhitepointAdaptation wp_adapt;
143 ptrdiff_t rgb_stride;
145 int *dither_scratch[3][2], *dither_scratch_base[3][2];
147 const struct ColorPrimaries *in_primaries, *out_primaries;
148 int lrgb2lrgb_passthrough;
149 DECLARE_ALIGNED(16, int16_t, lrgb2lrgb_coeffs)[3][3][8];
151 const struct TransferCharacteristics *in_txchr, *out_txchr;
152 int rgb2rgb_passthrough;
153 int16_t *lin_lut, *delin_lut;
155 const struct LumaCoefficients *in_lumacoef, *out_lumacoef;
156 int yuv2yuv_passthrough, yuv2yuv_fastmode;
157 DECLARE_ALIGNED(16, int16_t, yuv2rgb_coeffs)[3][3][8];
158 DECLARE_ALIGNED(16, int16_t, rgb2yuv_coeffs)[3][3][8];
159 DECLARE_ALIGNED(16, int16_t, yuv2yuv_coeffs)[3][3][8];
160 DECLARE_ALIGNED(16, int16_t, yuv_offset)[2 /* in, out */][8];
163 rgb2yuv_fsb_fn rgb2yuv_fsb;
165 double yuv2rgb_dbl_coeffs[3][3], rgb2yuv_dbl_coeffs[3][3];
166 int in_y_rng, in_uv_rng, out_y_rng, out_uv_rng;
171 // FIXME deal with odd width/heights (or just forbid it)
172 // FIXME faster linearize/delinearize implementation (integer pow)
173 // FIXME bt2020cl support (linearization between yuv/rgb step instead of between rgb/xyz)
174 // FIXME test that the values in (de)lin_lut don't exceed their container storage
175 // type size (only useful if we keep the LUT and don't move to fast integer pow)
176 // FIXME dithering if bitdepth goes down?
177 // FIXME bitexact for fate integration?
179 static const double ycgco_matrix[3][3] =
182 { -0.25, 0.5, -0.25 },
187 * All constants explained in e.g. https://linuxtv.org/downloads/v4l-dvb-apis/ch02s06.html
188 * The older ones (bt470bg/m) are also explained in their respective ITU docs
189 * (e.g. https://www.itu.int/dms_pubrec/itu-r/rec/bt/R-REC-BT.470-5-199802-S!!PDF-E.pdf)
190 * whereas the newer ones can typically be copied directly from wikipedia :)
192 static const struct LumaCoefficients luma_coefficients[AVCOL_SPC_NB] = {
193 [AVCOL_SPC_FCC] = { 0.30, 0.59, 0.11 },
194 [AVCOL_SPC_BT470BG] = { 0.299, 0.587, 0.114 },
195 [AVCOL_SPC_SMPTE170M] = { 0.299, 0.587, 0.114 },
196 [AVCOL_SPC_BT709] = { 0.2126, 0.7152, 0.0722 },
197 [AVCOL_SPC_SMPTE240M] = { 0.212, 0.701, 0.087 },
198 [AVCOL_SPC_YCOCG] = { 0.25, 0.5, 0.25 },
199 [AVCOL_SPC_BT2020_NCL] = { 0.2627, 0.6780, 0.0593 },
200 [AVCOL_SPC_BT2020_CL] = { 0.2627, 0.6780, 0.0593 },
203 static const struct LumaCoefficients *get_luma_coefficients(enum AVColorSpace csp)
205 const struct LumaCoefficients *coeffs;
207 if (csp >= AVCOL_SPC_NB)
209 coeffs = &luma_coefficients[csp];
216 static void fill_rgb2yuv_table(const struct LumaCoefficients *coeffs,
217 double rgb2yuv[3][3])
219 double bscale, rscale;
221 // special ycgco matrix
222 if (coeffs->cr == 0.25 && coeffs->cg == 0.5 && coeffs->cb == 0.25) {
223 memcpy(rgb2yuv, ycgco_matrix, sizeof(double) * 9);
227 rgb2yuv[0][0] = coeffs->cr;
228 rgb2yuv[0][1] = coeffs->cg;
229 rgb2yuv[0][2] = coeffs->cb;
230 bscale = 0.5 / (coeffs->cb - 1.0);
231 rscale = 0.5 / (coeffs->cr - 1.0);
232 rgb2yuv[1][0] = bscale * coeffs->cr;
233 rgb2yuv[1][1] = bscale * coeffs->cg;
236 rgb2yuv[2][1] = rscale * coeffs->cg;
237 rgb2yuv[2][2] = rscale * coeffs->cb;
240 // FIXME I'm pretty sure gamma22/28 also have a linear toe slope, but I can't
241 // find any actual tables that document their real values...
242 // See http://www.13thmonkey.org/~boris/gammacorrection/ first graph why it matters
243 static const struct TransferCharacteristics transfer_characteristics[AVCOL_TRC_NB] = {
244 [AVCOL_TRC_BT709] = { 1.099, 0.018, 0.45, 4.5 },
245 [AVCOL_TRC_GAMMA22] = { 1.0, 0.0, 1.0 / 2.2, 0.0 },
246 [AVCOL_TRC_GAMMA28] = { 1.0, 0.0, 1.0 / 2.8, 0.0 },
247 [AVCOL_TRC_SMPTE170M] = { 1.099, 0.018, 0.45, 4.5 },
248 [AVCOL_TRC_SMPTE240M] = { 1.1115, 0.0228, 0.45, 4.0 },
249 [AVCOL_TRC_IEC61966_2_1] = { 1.055, 0.0031308, 1.0 / 2.4, 12.92 },
250 [AVCOL_TRC_IEC61966_2_4] = { 1.099, 0.018, 0.45, 4.5 },
251 [AVCOL_TRC_BT2020_10] = { 1.099, 0.018, 0.45, 4.5 },
252 [AVCOL_TRC_BT2020_12] = { 1.0993, 0.0181, 0.45, 4.5 },
255 static const struct TransferCharacteristics *
256 get_transfer_characteristics(enum AVColorTransferCharacteristic trc)
258 const struct TransferCharacteristics *coeffs;
260 if (trc >= AVCOL_TRC_NB)
262 coeffs = &transfer_characteristics[trc];
269 static const struct WhitepointCoefficients whitepoint_coefficients[WP_NB] = {
270 [WP_D65] = { 0.3127, 0.3290 },
271 [WP_C] = { 0.3100, 0.3160 },
272 [WP_DCI] = { 0.3140, 0.3510 },
275 static const struct ColorPrimaries color_primaries[AVCOL_PRI_NB] = {
276 [AVCOL_PRI_BT709] = { WP_D65, 0.640, 0.330, 0.300, 0.600, 0.150, 0.060 },
277 [AVCOL_PRI_BT470M] = { WP_C, 0.670, 0.330, 0.210, 0.710, 0.140, 0.080 },
278 [AVCOL_PRI_BT470BG] = { WP_D65, 0.640, 0.330, 0.290, 0.600, 0.150, 0.060,},
279 [AVCOL_PRI_SMPTE170M] = { WP_D65, 0.630, 0.340, 0.310, 0.595, 0.155, 0.070 },
280 [AVCOL_PRI_SMPTE240M] = { WP_D65, 0.630, 0.340, 0.310, 0.595, 0.155, 0.070 },
281 [AVCOL_PRI_SMPTE431] = { WP_DCI, 0.680, 0.320, 0.265, 0.690, 0.150, 0.060 },
282 [AVCOL_PRI_SMPTE432] = { WP_D65, 0.680, 0.320, 0.265, 0.690, 0.150, 0.060 },
283 [AVCOL_PRI_FILM] = { WP_C, 0.681, 0.319, 0.243, 0.692, 0.145, 0.049 },
284 [AVCOL_PRI_BT2020] = { WP_D65, 0.708, 0.292, 0.170, 0.797, 0.131, 0.046 },
287 static const struct ColorPrimaries *get_color_primaries(enum AVColorPrimaries prm)
289 const struct ColorPrimaries *coeffs;
291 if (prm >= AVCOL_PRI_NB)
293 coeffs = &color_primaries[prm];
300 static void invert_matrix3x3(const double in[3][3], double out[3][3])
302 double m00 = in[0][0], m01 = in[0][1], m02 = in[0][2],
303 m10 = in[1][0], m11 = in[1][1], m12 = in[1][2],
304 m20 = in[2][0], m21 = in[2][1], m22 = in[2][2];
308 out[0][0] = (m11 * m22 - m21 * m12);
309 out[0][1] = -(m01 * m22 - m21 * m02);
310 out[0][2] = (m01 * m12 - m11 * m02);
311 out[1][0] = -(m10 * m22 - m20 * m12);
312 out[1][1] = (m00 * m22 - m20 * m02);
313 out[1][2] = -(m00 * m12 - m10 * m02);
314 out[2][0] = (m10 * m21 - m20 * m11);
315 out[2][1] = -(m00 * m21 - m20 * m01);
316 out[2][2] = (m00 * m11 - m10 * m01);
318 det = m00 * out[0][0] + m10 * out[0][1] + m20 * out[0][2];
321 for (i = 0; i < 3; i++) {
322 for (j = 0; j < 3; j++)
327 static int fill_gamma_table(ColorSpaceContext *s)
330 double in_alpha = s->in_txchr->alpha, in_beta = s->in_txchr->beta;
331 double in_gamma = s->in_txchr->gamma, in_delta = s->in_txchr->delta;
332 double in_ialpha = 1.0 / in_alpha, in_igamma = 1.0 / in_gamma, in_idelta = 1.0 / in_delta;
333 double out_alpha = s->out_txchr->alpha, out_beta = s->out_txchr->beta;
334 double out_gamma = s->out_txchr->gamma, out_delta = s->out_txchr->delta;
336 s->lin_lut = av_malloc(sizeof(*s->lin_lut) * 32768 * 2);
338 return AVERROR(ENOMEM);
339 s->delin_lut = &s->lin_lut[32768];
340 for (n = 0; n < 32768; n++) {
341 double v = (n - 2048.0) / 28672.0, d, l;
344 if (v <= -out_beta) {
345 d = -out_alpha * pow(-v, out_gamma) + (out_alpha - 1.0);
346 } else if (v < out_beta) {
349 d = out_alpha * pow(v, out_gamma) - (out_alpha - 1.0);
351 s->delin_lut[n] = av_clip_int16(lrint(d * 28672.0));
355 l = -pow((1.0 - in_alpha - v) * in_ialpha, in_igamma);
356 } else if (v < in_beta) {
359 l = pow((v + in_alpha - 1.0) * in_ialpha, in_igamma);
361 s->lin_lut[n] = av_clip_int16(lrint(l * 28672.0));
368 * see e.g. http://www.brucelindbloom.com/index.html?Eqn_RGB_XYZ_Matrix.html
370 static void fill_rgb2xyz_table(const struct ColorPrimaries *coeffs,
371 double rgb2xyz[3][3])
373 const struct WhitepointCoefficients *wp = &whitepoint_coefficients[coeffs->wp];
374 double i[3][3], sr, sg, sb, zw;
376 rgb2xyz[0][0] = coeffs->xr / coeffs->yr;
377 rgb2xyz[0][1] = coeffs->xg / coeffs->yg;
378 rgb2xyz[0][2] = coeffs->xb / coeffs->yb;
379 rgb2xyz[1][0] = rgb2xyz[1][1] = rgb2xyz[1][2] = 1.0;
380 rgb2xyz[2][0] = (1.0 - coeffs->xr - coeffs->yr) / coeffs->yr;
381 rgb2xyz[2][1] = (1.0 - coeffs->xg - coeffs->yg) / coeffs->yg;
382 rgb2xyz[2][2] = (1.0 - coeffs->xb - coeffs->yb) / coeffs->yb;
383 invert_matrix3x3(rgb2xyz, i);
384 zw = 1.0 - wp->xw - wp->yw;
385 sr = i[0][0] * wp->xw + i[0][1] * wp->yw + i[0][2] * zw;
386 sg = i[1][0] * wp->xw + i[1][1] * wp->yw + i[1][2] * zw;
387 sb = i[2][0] * wp->xw + i[2][1] * wp->yw + i[2][2] * zw;
399 static void mul3x3(double dst[3][3], const double src1[3][3], const double src2[3][3])
403 for (m = 0; m < 3; m++)
404 for (n = 0; n < 3; n++)
405 dst[m][n] = src2[m][0] * src1[0][n] +
406 src2[m][1] * src1[1][n] +
407 src2[m][2] * src1[2][n];
411 * See http://www.brucelindbloom.com/index.html?Eqn_ChromAdapt.html
412 * This function uses the Bradford mechanism.
414 static void fill_whitepoint_conv_table(double out[3][3], enum WhitepointAdaptation wp_adapt,
415 enum Whitepoint src, enum Whitepoint dst)
417 static const double ma_tbl[NB_WP_ADAPT_NON_IDENTITY][3][3] = {
418 [WP_ADAPT_BRADFORD] = {
419 { 0.8951, 0.2664, -0.1614 },
420 { -0.7502, 1.7135, 0.0367 },
421 { 0.0389, -0.0685, 1.0296 },
422 }, [WP_ADAPT_VON_KRIES] = {
423 { 0.40024, 0.70760, -0.08081 },
424 { -0.22630, 1.16532, 0.04570 },
425 { 0.00000, 0.00000, 0.91822 },
428 const double (*ma)[3] = ma_tbl[wp_adapt];
429 const struct WhitepointCoefficients *wp_src = &whitepoint_coefficients[src];
430 double zw_src = 1.0 - wp_src->xw - wp_src->yw;
431 const struct WhitepointCoefficients *wp_dst = &whitepoint_coefficients[dst];
432 double zw_dst = 1.0 - wp_dst->xw - wp_dst->yw;
433 double mai[3][3], fac[3][3], tmp[3][3];
434 double rs, gs, bs, rd, gd, bd;
436 invert_matrix3x3(ma, mai);
437 rs = ma[0][0] * wp_src->xw + ma[0][1] * wp_src->yw + ma[0][2] * zw_src;
438 gs = ma[1][0] * wp_src->xw + ma[1][1] * wp_src->yw + ma[1][2] * zw_src;
439 bs = ma[2][0] * wp_src->xw + ma[2][1] * wp_src->yw + ma[2][2] * zw_src;
440 rd = ma[0][0] * wp_dst->xw + ma[0][1] * wp_dst->yw + ma[0][2] * zw_dst;
441 gd = ma[1][0] * wp_dst->xw + ma[1][1] * wp_dst->yw + ma[1][2] * zw_dst;
442 bd = ma[2][0] * wp_dst->xw + ma[2][1] * wp_dst->yw + ma[2][2] * zw_dst;
446 fac[0][1] = fac[0][2] = fac[1][0] = fac[1][2] = fac[2][0] = fac[2][1] = 0.0;
447 mul3x3(tmp, ma, fac);
448 mul3x3(out, tmp, mai);
451 static void apply_lut(int16_t *buf[3], ptrdiff_t stride,
452 int w, int h, const int16_t *lut)
456 for (n = 0; n < 3; n++) {
457 int16_t *data = buf[n];
459 for (y = 0; y < h; y++) {
460 for (x = 0; x < w; x++)
461 data[x] = lut[av_clip_uintp2(2048 + data[x], 15)];
470 ptrdiff_t in_linesize[3], out_linesize[3];
471 int in_ss_h, out_ss_h;
474 static int convert(AVFilterContext *ctx, void *data, int job_nr, int n_jobs)
476 struct ThreadData *td = data;
477 ColorSpaceContext *s = ctx->priv;
478 uint8_t *in_data[3], *out_data[3];
480 int h_in = (td->in->height + 1) >> 1;
481 int h1 = 2 * (job_nr * h_in / n_jobs), h2 = 2 * ((job_nr + 1) * h_in / n_jobs);
482 int w = td->in->width, h = h2 - h1;
484 in_data[0] = td->in->data[0] + td->in_linesize[0] * h1;
485 in_data[1] = td->in->data[1] + td->in_linesize[1] * (h1 >> td->in_ss_h);
486 in_data[2] = td->in->data[2] + td->in_linesize[2] * (h1 >> td->in_ss_h);
487 out_data[0] = td->out->data[0] + td->out_linesize[0] * h1;
488 out_data[1] = td->out->data[1] + td->out_linesize[1] * (h1 >> td->out_ss_h);
489 out_data[2] = td->out->data[2] + td->out_linesize[2] * (h1 >> td->out_ss_h);
490 rgb[0] = s->rgb[0] + s->rgb_stride * h1;
491 rgb[1] = s->rgb[1] + s->rgb_stride * h1;
492 rgb[2] = s->rgb[2] + s->rgb_stride * h1;
494 // FIXME for simd, also make sure we do pictures with negative stride
495 // top-down so we don't overwrite lines with padding of data before it
496 // in the same buffer (same as swscale)
498 if (s->yuv2yuv_fastmode) {
499 // FIXME possibly use a fast mode in case only the y range changes?
500 // since in that case, only the diagonal entries in yuv2yuv_coeffs[]
502 s->yuv2yuv(out_data, td->out_linesize, in_data, td->in_linesize, w, h,
503 s->yuv2yuv_coeffs, s->yuv_offset);
505 // FIXME maybe (for caching effciency) do pipeline per-line instead of
506 // full buffer per function? (Or, since yuv2rgb requires 2 lines: per
507 // 2 lines, for yuv420.)
510 * - yuv2rgb converts from whatever range the input was ([16-235/240] or
511 * [0,255] or the 10/12bpp equivalents thereof) to an integer version
512 * of RGB in psuedo-restricted 15+sign bits. That means that the float
513 * range [0.0,1.0] is in [0,28762], and the remainder of the int16_t
514 * range is used for overflow/underflow outside the representable
515 * range of this RGB type. rgb2yuv is the exact opposite.
516 * - gamma correction is done using a LUT since that appears to work
518 * - If the input is chroma-subsampled (420/422), the yuv2rgb conversion
519 * (or rgb2yuv conversion) uses nearest-neighbour sampling to read
520 * read chroma pixels at luma resolution. If you want some more fancy
521 * filter, you can use swscale to convert to yuv444p.
522 * - all coefficients are 14bit (so in the [-2.0,2.0] range).
524 s->yuv2rgb(rgb, s->rgb_stride, in_data, td->in_linesize, w, h,
525 s->yuv2rgb_coeffs, s->yuv_offset[0]);
526 if (!s->rgb2rgb_passthrough) {
527 apply_lut(rgb, s->rgb_stride, w, h, s->lin_lut);
528 if (!s->lrgb2lrgb_passthrough)
529 s->dsp.multiply3x3(rgb, s->rgb_stride, w, h, s->lrgb2lrgb_coeffs);
530 apply_lut(rgb, s->rgb_stride, w, h, s->delin_lut);
532 if (s->dither == DITHER_FSB) {
533 s->rgb2yuv_fsb(out_data, td->out_linesize, rgb, s->rgb_stride, w, h,
534 s->rgb2yuv_coeffs, s->yuv_offset[1], s->dither_scratch);
536 s->rgb2yuv(out_data, td->out_linesize, rgb, s->rgb_stride, w, h,
537 s->rgb2yuv_coeffs, s->yuv_offset[1]);
544 static int get_range_off(AVFilterContext *ctx, int *off,
545 int *y_rng, int *uv_rng,
546 enum AVColorRange rng, int depth)
549 case AVCOL_RANGE_UNSPECIFIED: {
550 ColorSpaceContext *s = ctx->priv;
552 if (!s->did_warn_range) {
553 av_log(ctx, AV_LOG_WARNING, "Input range not set, assuming tv/mpeg\n");
554 s->did_warn_range = 1;
558 case AVCOL_RANGE_MPEG:
559 *off = 16 << (depth - 8);
560 *y_rng = 219 << (depth - 8);
561 *uv_rng = 224 << (depth - 8);
563 case AVCOL_RANGE_JPEG:
565 *y_rng = *uv_rng = (256 << (depth - 8)) - 1;
568 return AVERROR(EINVAL);
574 static int create_filtergraph(AVFilterContext *ctx,
575 const AVFrame *in, const AVFrame *out)
577 ColorSpaceContext *s = ctx->priv;
578 const AVPixFmtDescriptor *in_desc = av_pix_fmt_desc_get(in->format);
579 const AVPixFmtDescriptor *out_desc = av_pix_fmt_desc_get(out->format);
580 int emms = 0, m, n, o, res, fmt_identical, redo_yuv2rgb = 0, redo_rgb2yuv = 0;
582 #define supported_depth(d) ((d) == 8 || (d) == 10 || (d) == 12)
583 #define supported_subsampling(lcw, lch) \
584 (((lcw) == 0 && (lch) == 0) || ((lcw) == 1 && (lch) == 0) || ((lcw) == 1 && (lch) == 1))
585 #define supported_format(d) \
586 ((d) != NULL && (d)->nb_components == 3 && \
587 !((d)->flags & AV_PIX_FMT_FLAG_RGB) && \
588 supported_depth((d)->comp[0].depth) && \
589 supported_subsampling((d)->log2_chroma_w, (d)->log2_chroma_h))
591 if (!supported_format(in_desc)) {
592 av_log(ctx, AV_LOG_ERROR,
593 "Unsupported input format %d (%s) or bitdepth (%d)\n",
594 in->format, av_get_pix_fmt_name(in->format),
595 in_desc ? in_desc->comp[0].depth : -1);
596 return AVERROR(EINVAL);
598 if (!supported_format(out_desc)) {
599 av_log(ctx, AV_LOG_ERROR,
600 "Unsupported output format %d (%s) or bitdepth (%d)\n",
601 out->format, av_get_pix_fmt_name(out->format),
602 out_desc ? out_desc->comp[0].depth : -1);
603 return AVERROR(EINVAL);
606 if (in->color_primaries != s->in_prm) s->in_primaries = NULL;
607 if (out->color_primaries != s->out_prm) s->out_primaries = NULL;
608 if (in->color_trc != s->in_trc) s->in_txchr = NULL;
609 if (out->color_trc != s->out_trc) s->out_txchr = NULL;
610 if (in->colorspace != s->in_csp ||
611 in->color_range != s->in_rng) s->in_lumacoef = NULL;
612 if (out->colorspace != s->out_csp ||
613 out->color_range != s->out_rng) s->out_lumacoef = NULL;
615 if (!s->out_primaries || !s->in_primaries) {
616 s->in_prm = in->color_primaries;
617 if (s->user_iall != CS_UNSPECIFIED)
618 s->in_prm = default_prm[FFMIN(s->user_iall, CS_NB)];
619 if (s->user_iprm != AVCOL_PRI_UNSPECIFIED)
620 s->in_prm = s->user_iprm;
621 s->in_primaries = get_color_primaries(s->in_prm);
622 if (!s->in_primaries) {
623 av_log(ctx, AV_LOG_ERROR,
624 "Unsupported input primaries %d (%s)\n",
625 s->in_prm, av_color_primaries_name(s->in_prm));
626 return AVERROR(EINVAL);
628 s->out_prm = out->color_primaries;
629 s->out_primaries = get_color_primaries(s->out_prm);
630 if (!s->out_primaries) {
631 if (s->out_prm == AVCOL_PRI_UNSPECIFIED) {
632 if (s->user_all == CS_UNSPECIFIED) {
633 av_log(ctx, AV_LOG_ERROR, "Please specify output primaries\n");
635 av_log(ctx, AV_LOG_ERROR,
636 "Unsupported output color property %d\n", s->user_all);
639 av_log(ctx, AV_LOG_ERROR,
640 "Unsupported output primaries %d (%s)\n",
641 s->out_prm, av_color_primaries_name(s->out_prm));
643 return AVERROR(EINVAL);
645 s->lrgb2lrgb_passthrough = !memcmp(s->in_primaries, s->out_primaries,
646 sizeof(*s->in_primaries));
647 if (!s->lrgb2lrgb_passthrough) {
648 double rgb2xyz[3][3], xyz2rgb[3][3], rgb2rgb[3][3];
650 fill_rgb2xyz_table(s->out_primaries, rgb2xyz);
651 invert_matrix3x3(rgb2xyz, xyz2rgb);
652 fill_rgb2xyz_table(s->in_primaries, rgb2xyz);
653 if (s->out_primaries->wp != s->in_primaries->wp &&
654 s->wp_adapt != WP_ADAPT_IDENTITY) {
655 double wpconv[3][3], tmp[3][3];
657 fill_whitepoint_conv_table(wpconv, s->wp_adapt, s->in_primaries->wp,
658 s->out_primaries->wp);
659 mul3x3(tmp, rgb2xyz, wpconv);
660 mul3x3(rgb2rgb, tmp, xyz2rgb);
662 mul3x3(rgb2rgb, rgb2xyz, xyz2rgb);
664 for (m = 0; m < 3; m++)
665 for (n = 0; n < 3; n++) {
666 s->lrgb2lrgb_coeffs[m][n][0] = lrint(16384.0 * rgb2rgb[m][n]);
667 for (o = 1; o < 8; o++)
668 s->lrgb2lrgb_coeffs[m][n][o] = s->lrgb2lrgb_coeffs[m][n][0];
676 av_freep(&s->lin_lut);
677 s->in_trc = in->color_trc;
678 if (s->user_iall != CS_UNSPECIFIED)
679 s->in_trc = default_trc[FFMIN(s->user_iall, CS_NB)];
680 if (s->user_itrc != AVCOL_TRC_UNSPECIFIED)
681 s->in_trc = s->user_itrc;
682 s->in_txchr = get_transfer_characteristics(s->in_trc);
684 av_log(ctx, AV_LOG_ERROR,
685 "Unsupported input transfer characteristics %d (%s)\n",
686 s->in_trc, av_color_transfer_name(s->in_trc));
687 return AVERROR(EINVAL);
692 av_freep(&s->lin_lut);
693 s->out_trc = out->color_trc;
694 s->out_txchr = get_transfer_characteristics(s->out_trc);
696 if (s->out_trc == AVCOL_TRC_UNSPECIFIED) {
697 if (s->user_all == CS_UNSPECIFIED) {
698 av_log(ctx, AV_LOG_ERROR,
699 "Please specify output transfer characteristics\n");
701 av_log(ctx, AV_LOG_ERROR,
702 "Unsupported output color property %d\n", s->user_all);
705 av_log(ctx, AV_LOG_ERROR,
706 "Unsupported output transfer characteristics %d (%s)\n",
707 s->out_trc, av_color_transfer_name(s->out_trc));
709 return AVERROR(EINVAL);
713 s->rgb2rgb_passthrough = s->fast_mode || (s->lrgb2lrgb_passthrough &&
714 !memcmp(s->in_txchr, s->out_txchr, sizeof(*s->in_txchr)));
715 if (!s->rgb2rgb_passthrough && !s->lin_lut) {
716 res = fill_gamma_table(s);
722 if (!s->in_lumacoef) {
723 s->in_csp = in->colorspace;
724 if (s->user_iall != CS_UNSPECIFIED)
725 s->in_csp = default_csp[FFMIN(s->user_iall, CS_NB)];
726 if (s->user_icsp != AVCOL_SPC_UNSPECIFIED)
727 s->in_csp = s->user_icsp;
728 s->in_rng = in->color_range;
729 if (s->user_irng != AVCOL_RANGE_UNSPECIFIED)
730 s->in_rng = s->user_irng;
731 s->in_lumacoef = get_luma_coefficients(s->in_csp);
732 if (!s->in_lumacoef) {
733 av_log(ctx, AV_LOG_ERROR,
734 "Unsupported input colorspace %d (%s)\n",
735 s->in_csp, av_color_space_name(s->in_csp));
736 return AVERROR(EINVAL);
741 if (!s->out_lumacoef) {
742 s->out_csp = out->colorspace;
743 s->out_rng = out->color_range;
744 s->out_lumacoef = get_luma_coefficients(s->out_csp);
745 if (!s->out_lumacoef) {
746 if (s->out_csp == AVCOL_SPC_UNSPECIFIED) {
747 if (s->user_all == CS_UNSPECIFIED) {
748 av_log(ctx, AV_LOG_ERROR,
749 "Please specify output transfer characteristics\n");
751 av_log(ctx, AV_LOG_ERROR,
752 "Unsupported output color property %d\n", s->user_all);
755 av_log(ctx, AV_LOG_ERROR,
756 "Unsupported output transfer characteristics %d (%s)\n",
757 s->out_csp, av_color_space_name(s->out_csp));
759 return AVERROR(EINVAL);
764 fmt_identical = in_desc->log2_chroma_h == out_desc->log2_chroma_h &&
765 in_desc->log2_chroma_w == out_desc->log2_chroma_w;
766 s->yuv2yuv_fastmode = s->rgb2rgb_passthrough && fmt_identical;
767 s->yuv2yuv_passthrough = s->yuv2yuv_fastmode && s->in_rng == s->out_rng &&
768 !memcmp(s->in_lumacoef, s->out_lumacoef,
769 sizeof(*s->in_lumacoef)) &&
770 in_desc->comp[0].depth == out_desc->comp[0].depth;
771 if (!s->yuv2yuv_passthrough) {
773 double rgb2yuv[3][3], (*yuv2rgb)[3] = s->yuv2rgb_dbl_coeffs;
774 int off, bits, in_rng;
776 res = get_range_off(ctx, &off, &s->in_y_rng, &s->in_uv_rng,
777 s->in_rng, in_desc->comp[0].depth);
779 av_log(ctx, AV_LOG_ERROR,
780 "Unsupported input color range %d (%s)\n",
781 s->in_rng, av_color_range_name(s->in_rng));
784 for (n = 0; n < 8; n++)
785 s->yuv_offset[0][n] = off;
786 fill_rgb2yuv_table(s->in_lumacoef, rgb2yuv);
787 invert_matrix3x3(rgb2yuv, yuv2rgb);
788 bits = 1 << (in_desc->comp[0].depth - 1);
789 for (n = 0; n < 3; n++) {
790 for (in_rng = s->in_y_rng, m = 0; m < 3; m++, in_rng = s->in_uv_rng) {
791 s->yuv2rgb_coeffs[n][m][0] = lrint(28672 * bits * yuv2rgb[n][m] / in_rng);
792 for (o = 1; o < 8; o++)
793 s->yuv2rgb_coeffs[n][m][o] = s->yuv2rgb_coeffs[n][m][0];
796 av_assert2(s->yuv2rgb_coeffs[0][1][0] == 0);
797 av_assert2(s->yuv2rgb_coeffs[2][2][0] == 0);
798 av_assert2(s->yuv2rgb_coeffs[0][0][0] == s->yuv2rgb_coeffs[1][0][0]);
799 av_assert2(s->yuv2rgb_coeffs[0][0][0] == s->yuv2rgb_coeffs[2][0][0]);
800 s->yuv2rgb = s->dsp.yuv2rgb[(in_desc->comp[0].depth - 8) >> 1]
801 [in_desc->log2_chroma_h + in_desc->log2_chroma_w];
806 double (*rgb2yuv)[3] = s->rgb2yuv_dbl_coeffs;
807 int off, out_rng, bits;
809 res = get_range_off(ctx, &off, &s->out_y_rng, &s->out_uv_rng,
810 s->out_rng, out_desc->comp[0].depth);
812 av_log(ctx, AV_LOG_ERROR,
813 "Unsupported output color range %d (%s)\n",
814 s->out_rng, av_color_range_name(s->out_rng));
817 for (n = 0; n < 8; n++)
818 s->yuv_offset[1][n] = off;
819 fill_rgb2yuv_table(s->out_lumacoef, rgb2yuv);
820 bits = 1 << (29 - out_desc->comp[0].depth);
821 for (out_rng = s->out_y_rng, n = 0; n < 3; n++, out_rng = s->out_uv_rng) {
822 for (m = 0; m < 3; m++) {
823 s->rgb2yuv_coeffs[n][m][0] = lrint(bits * out_rng * rgb2yuv[n][m] / 28672);
824 for (o = 1; o < 8; o++)
825 s->rgb2yuv_coeffs[n][m][o] = s->rgb2yuv_coeffs[n][m][0];
828 av_assert2(s->rgb2yuv_coeffs[1][2][0] == s->rgb2yuv_coeffs[2][0][0]);
829 s->rgb2yuv = s->dsp.rgb2yuv[(out_desc->comp[0].depth - 8) >> 1]
830 [out_desc->log2_chroma_h + out_desc->log2_chroma_w];
831 s->rgb2yuv_fsb = s->dsp.rgb2yuv_fsb[(out_desc->comp[0].depth - 8) >> 1]
832 [out_desc->log2_chroma_h + out_desc->log2_chroma_w];
836 if (s->yuv2yuv_fastmode && (redo_yuv2rgb || redo_rgb2yuv)) {
837 int idepth = in_desc->comp[0].depth, odepth = out_desc->comp[0].depth;
838 double (*rgb2yuv)[3] = s->rgb2yuv_dbl_coeffs;
839 double (*yuv2rgb)[3] = s->yuv2rgb_dbl_coeffs;
840 double yuv2yuv[3][3];
843 mul3x3(yuv2yuv, yuv2rgb, rgb2yuv);
844 for (out_rng = s->out_y_rng, m = 0; m < 3; m++, out_rng = s->out_uv_rng) {
845 for (in_rng = s->in_y_rng, n = 0; n < 3; n++, in_rng = s->in_uv_rng) {
846 s->yuv2yuv_coeffs[m][n][0] =
847 lrint(16384 * yuv2yuv[m][n] * out_rng * (1 << idepth) /
848 (in_rng * (1 << odepth)));
849 for (o = 1; o < 8; o++)
850 s->yuv2yuv_coeffs[m][n][o] = s->yuv2yuv_coeffs[m][n][0];
853 av_assert2(s->yuv2yuv_coeffs[1][0][0] == 0);
854 av_assert2(s->yuv2yuv_coeffs[2][0][0] == 0);
855 s->yuv2yuv = s->dsp.yuv2yuv[(idepth - 8) >> 1][(odepth - 8) >> 1]
856 [in_desc->log2_chroma_h + in_desc->log2_chroma_w];
866 static int init(AVFilterContext *ctx)
868 ColorSpaceContext *s = ctx->priv;
870 ff_colorspacedsp_init(&s->dsp);
875 static void uninit(AVFilterContext *ctx)
877 ColorSpaceContext *s = ctx->priv;
879 av_freep(&s->rgb[0]);
880 av_freep(&s->rgb[1]);
881 av_freep(&s->rgb[2]);
883 av_freep(&s->dither_scratch_base[0][0]);
884 av_freep(&s->dither_scratch_base[0][1]);
885 av_freep(&s->dither_scratch_base[1][0]);
886 av_freep(&s->dither_scratch_base[1][1]);
887 av_freep(&s->dither_scratch_base[2][0]);
888 av_freep(&s->dither_scratch_base[2][1]);
890 av_freep(&s->lin_lut);
893 static int filter_frame(AVFilterLink *link, AVFrame *in)
895 AVFilterContext *ctx = link->dst;
896 AVFilterLink *outlink = ctx->outputs[0];
897 ColorSpaceContext *s = ctx->priv;
898 // FIXME if yuv2yuv_passthrough, don't get a new buffer but use the
899 // input one if it is writable *OR* the actual literal values of in_*
900 // and out_* are identical (not just their respective properties)
901 AVFrame *out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
903 ptrdiff_t rgb_stride = FFALIGN(in->width * sizeof(int16_t), 32);
904 unsigned rgb_sz = rgb_stride * in->height;
905 struct ThreadData td;
909 return AVERROR(ENOMEM);
911 res = av_frame_copy_props(out, in);
917 out->color_primaries = s->user_prm == AVCOL_PRI_UNSPECIFIED ?
918 default_prm[FFMIN(s->user_all, CS_NB)] : s->user_prm;
919 if (s->user_trc == AVCOL_TRC_UNSPECIFIED) {
920 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(out->format);
922 out->color_trc = default_trc[FFMIN(s->user_all, CS_NB)];
923 if (out->color_trc == AVCOL_TRC_BT2020_10 && desc && desc->comp[0].depth >= 12)
924 out->color_trc = AVCOL_TRC_BT2020_12;
926 out->color_trc = s->user_trc;
928 out->colorspace = s->user_csp == AVCOL_SPC_UNSPECIFIED ?
929 default_csp[FFMIN(s->user_all, CS_NB)] : s->user_csp;
930 out->color_range = s->user_rng == AVCOL_RANGE_UNSPECIFIED ?
931 in->color_range : s->user_rng;
932 if (rgb_sz != s->rgb_sz) {
933 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(out->format);
934 int uvw = in->width >> desc->log2_chroma_w;
936 av_freep(&s->rgb[0]);
937 av_freep(&s->rgb[1]);
938 av_freep(&s->rgb[2]);
940 av_freep(&s->dither_scratch_base[0][0]);
941 av_freep(&s->dither_scratch_base[0][1]);
942 av_freep(&s->dither_scratch_base[1][0]);
943 av_freep(&s->dither_scratch_base[1][1]);
944 av_freep(&s->dither_scratch_base[2][0]);
945 av_freep(&s->dither_scratch_base[2][1]);
947 s->rgb[0] = av_malloc(rgb_sz);
948 s->rgb[1] = av_malloc(rgb_sz);
949 s->rgb[2] = av_malloc(rgb_sz);
950 s->dither_scratch_base[0][0] =
951 av_malloc(sizeof(*s->dither_scratch_base[0][0]) * (in->width + 4));
952 s->dither_scratch_base[0][1] =
953 av_malloc(sizeof(*s->dither_scratch_base[0][1]) * (in->width + 4));
954 s->dither_scratch_base[1][0] =
955 av_malloc(sizeof(*s->dither_scratch_base[1][0]) * (uvw + 4));
956 s->dither_scratch_base[1][1] =
957 av_malloc(sizeof(*s->dither_scratch_base[1][1]) * (uvw + 4));
958 s->dither_scratch_base[2][0] =
959 av_malloc(sizeof(*s->dither_scratch_base[2][0]) * (uvw + 4));
960 s->dither_scratch_base[2][1] =
961 av_malloc(sizeof(*s->dither_scratch_base[2][1]) * (uvw + 4));
962 s->dither_scratch[0][0] = &s->dither_scratch_base[0][0][1];
963 s->dither_scratch[0][1] = &s->dither_scratch_base[0][1][1];
964 s->dither_scratch[1][0] = &s->dither_scratch_base[1][0][1];
965 s->dither_scratch[1][1] = &s->dither_scratch_base[1][1][1];
966 s->dither_scratch[2][0] = &s->dither_scratch_base[2][0][1];
967 s->dither_scratch[2][1] = &s->dither_scratch_base[2][1][1];
968 if (!s->rgb[0] || !s->rgb[1] || !s->rgb[2] ||
969 !s->dither_scratch_base[0][0] || !s->dither_scratch_base[0][1] ||
970 !s->dither_scratch_base[1][0] || !s->dither_scratch_base[1][1] ||
971 !s->dither_scratch_base[2][0] || !s->dither_scratch_base[2][1]) {
973 return AVERROR(ENOMEM);
977 res = create_filtergraph(ctx, in, out);
980 s->rgb_stride = rgb_stride / sizeof(int16_t);
983 td.in_linesize[0] = in->linesize[0];
984 td.in_linesize[1] = in->linesize[1];
985 td.in_linesize[2] = in->linesize[2];
986 td.out_linesize[0] = out->linesize[0];
987 td.out_linesize[1] = out->linesize[1];
988 td.out_linesize[2] = out->linesize[2];
989 td.in_ss_h = av_pix_fmt_desc_get(in->format)->log2_chroma_h;
990 td.out_ss_h = av_pix_fmt_desc_get(out->format)->log2_chroma_h;
991 if (s->yuv2yuv_passthrough) {
992 res = av_frame_copy(out, in);
996 ctx->internal->execute(ctx, convert, &td, NULL,
997 FFMIN((in->height + 1) >> 1, ff_filter_get_nb_threads(ctx)));
1001 return ff_filter_frame(outlink, out);
1004 static int query_formats(AVFilterContext *ctx)
1006 static const enum AVPixelFormat pix_fmts[] = {
1007 AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV444P,
1008 AV_PIX_FMT_YUV420P10, AV_PIX_FMT_YUV422P10, AV_PIX_FMT_YUV444P10,
1009 AV_PIX_FMT_YUV420P12, AV_PIX_FMT_YUV422P12, AV_PIX_FMT_YUV444P12,
1010 AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ444P,
1014 ColorSpaceContext *s = ctx->priv;
1015 AVFilterFormats *formats = ff_make_format_list(pix_fmts);
1018 return AVERROR(ENOMEM);
1019 if (s->user_format == AV_PIX_FMT_NONE)
1020 return ff_set_common_formats(ctx, formats);
1021 res = ff_formats_ref(formats, &ctx->inputs[0]->out_formats);
1025 res = ff_add_format(&formats, s->user_format);
1029 return ff_formats_ref(formats, &ctx->outputs[0]->in_formats);
1032 static int config_props(AVFilterLink *outlink)
1034 AVFilterLink *inlink = outlink->src->inputs[0];
1036 outlink->w = inlink->w;
1037 outlink->h = inlink->h;
1038 outlink->sample_aspect_ratio = inlink->sample_aspect_ratio;
1039 outlink->time_base = inlink->time_base;
1044 #define OFFSET(x) offsetof(ColorSpaceContext, x)
1045 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM | AV_OPT_FLAG_VIDEO_PARAM
1046 #define ENUM(x, y, z) { x, "", 0, AV_OPT_TYPE_CONST, { .i64 = y }, INT_MIN, INT_MAX, FLAGS, z }
1048 static const AVOption colorspace_options[] = {
1049 { "all", "Set all color properties together",
1050 OFFSET(user_all), AV_OPT_TYPE_INT, { .i64 = CS_UNSPECIFIED },
1051 CS_UNSPECIFIED, CS_NB - 1, FLAGS, "all" },
1052 ENUM("bt470m", CS_BT470M, "all"),
1053 ENUM("bt470bg", CS_BT470BG, "all"),
1054 ENUM("bt601-6-525", CS_BT601_6_525, "all"),
1055 ENUM("bt601-6-625", CS_BT601_6_625, "all"),
1056 ENUM("bt709", CS_BT709, "all"),
1057 ENUM("smpte170m", CS_SMPTE170M, "all"),
1058 ENUM("smpte240m", CS_SMPTE240M, "all"),
1059 ENUM("bt2020", CS_BT2020, "all"),
1061 { "space", "Output colorspace",
1062 OFFSET(user_csp), AV_OPT_TYPE_INT, { .i64 = AVCOL_SPC_UNSPECIFIED },
1063 AVCOL_SPC_RGB, AVCOL_SPC_NB - 1, FLAGS, "csp"},
1064 ENUM("bt709", AVCOL_SPC_BT709, "csp"),
1065 ENUM("fcc", AVCOL_SPC_FCC, "csp"),
1066 ENUM("bt470bg", AVCOL_SPC_BT470BG, "csp"),
1067 ENUM("smpte170m", AVCOL_SPC_SMPTE170M, "csp"),
1068 ENUM("smpte240m", AVCOL_SPC_SMPTE240M, "csp"),
1069 ENUM("ycgco", AVCOL_SPC_YCGCO, "csp"),
1070 ENUM("bt2020ncl", AVCOL_SPC_BT2020_NCL, "csp"),
1072 { "range", "Output color range",
1073 OFFSET(user_rng), AV_OPT_TYPE_INT, { .i64 = AVCOL_RANGE_UNSPECIFIED },
1074 AVCOL_RANGE_UNSPECIFIED, AVCOL_RANGE_NB - 1, FLAGS, "rng" },
1075 ENUM("tv", AVCOL_RANGE_MPEG, "rng"),
1076 ENUM("mpeg", AVCOL_RANGE_MPEG, "rng"),
1077 ENUM("pc", AVCOL_RANGE_JPEG, "rng"),
1078 ENUM("jpeg", AVCOL_RANGE_JPEG, "rng"),
1080 { "primaries", "Output color primaries",
1081 OFFSET(user_prm), AV_OPT_TYPE_INT, { .i64 = AVCOL_PRI_UNSPECIFIED },
1082 AVCOL_PRI_RESERVED0, AVCOL_PRI_NB - 1, FLAGS, "prm" },
1083 ENUM("bt709", AVCOL_PRI_BT709, "prm"),
1084 ENUM("bt470m", AVCOL_PRI_BT470M, "prm"),
1085 ENUM("bt470bg", AVCOL_PRI_BT470BG, "prm"),
1086 ENUM("smpte170m", AVCOL_PRI_SMPTE170M, "prm"),
1087 ENUM("smpte240m", AVCOL_PRI_SMPTE240M, "prm"),
1088 ENUM("film", AVCOL_PRI_FILM, "prm"),
1089 ENUM("smpte431", AVCOL_PRI_SMPTE431, "prm"),
1090 ENUM("smpte432", AVCOL_PRI_SMPTE432, "prm"),
1091 ENUM("bt2020", AVCOL_PRI_BT2020, "prm"),
1093 { "trc", "Output transfer characteristics",
1094 OFFSET(user_trc), AV_OPT_TYPE_INT, { .i64 = AVCOL_TRC_UNSPECIFIED },
1095 AVCOL_TRC_RESERVED0, AVCOL_TRC_NB - 1, FLAGS, "trc" },
1096 ENUM("bt709", AVCOL_TRC_BT709, "trc"),
1097 ENUM("bt470m", AVCOL_TRC_GAMMA22, "trc"),
1098 ENUM("gamma22", AVCOL_TRC_GAMMA22, "trc"),
1099 ENUM("bt470bg", AVCOL_TRC_GAMMA28, "trc"),
1100 ENUM("gamma28", AVCOL_TRC_GAMMA28, "trc"),
1101 ENUM("smpte170m", AVCOL_TRC_SMPTE170M, "trc"),
1102 ENUM("smpte240m", AVCOL_TRC_SMPTE240M, "trc"),
1103 ENUM("srgb", AVCOL_TRC_IEC61966_2_1, "trc"),
1104 ENUM("iec61966-2-1", AVCOL_TRC_IEC61966_2_1, "trc"),
1105 ENUM("xvycc", AVCOL_TRC_IEC61966_2_4, "trc"),
1106 ENUM("iec61966-2-4", AVCOL_TRC_IEC61966_2_4, "trc"),
1107 ENUM("bt2020-10", AVCOL_TRC_BT2020_10, "trc"),
1108 ENUM("bt2020-12", AVCOL_TRC_BT2020_12, "trc"),
1110 { "format", "Output pixel format",
1111 OFFSET(user_format), AV_OPT_TYPE_INT, { .i64 = AV_PIX_FMT_NONE },
1112 AV_PIX_FMT_NONE, AV_PIX_FMT_GBRAP12LE, FLAGS, "fmt" },
1113 ENUM("yuv420p", AV_PIX_FMT_YUV420P, "fmt"),
1114 ENUM("yuv420p10", AV_PIX_FMT_YUV420P10, "fmt"),
1115 ENUM("yuv420p12", AV_PIX_FMT_YUV420P12, "fmt"),
1116 ENUM("yuv422p", AV_PIX_FMT_YUV422P, "fmt"),
1117 ENUM("yuv422p10", AV_PIX_FMT_YUV422P10, "fmt"),
1118 ENUM("yuv422p12", AV_PIX_FMT_YUV422P12, "fmt"),
1119 ENUM("yuv444p", AV_PIX_FMT_YUV444P, "fmt"),
1120 ENUM("yuv444p10", AV_PIX_FMT_YUV444P10, "fmt"),
1121 ENUM("yuv444p12", AV_PIX_FMT_YUV444P12, "fmt"),
1123 { "fast", "Ignore primary chromaticity and gamma correction",
1124 OFFSET(fast_mode), AV_OPT_TYPE_BOOL, { .i64 = 0 },
1127 { "dither", "Dithering mode",
1128 OFFSET(dither), AV_OPT_TYPE_INT, { .i64 = DITHER_NONE },
1129 DITHER_NONE, DITHER_NB - 1, FLAGS, "dither" },
1130 ENUM("none", DITHER_NONE, "dither"),
1131 ENUM("fsb", DITHER_FSB, "dither"),
1133 { "wpadapt", "Whitepoint adaptation method",
1134 OFFSET(wp_adapt), AV_OPT_TYPE_INT, { .i64 = WP_ADAPT_BRADFORD },
1135 WP_ADAPT_BRADFORD, NB_WP_ADAPT - 1, FLAGS, "wpadapt" },
1136 ENUM("bradford", WP_ADAPT_BRADFORD, "wpadapt"),
1137 ENUM("vonkries", WP_ADAPT_VON_KRIES, "wpadapt"),
1138 ENUM("identity", WP_ADAPT_IDENTITY, "wpadapt"),
1140 { "iall", "Set all input color properties together",
1141 OFFSET(user_iall), AV_OPT_TYPE_INT, { .i64 = CS_UNSPECIFIED },
1142 CS_UNSPECIFIED, CS_NB - 1, FLAGS, "all" },
1143 { "ispace", "Input colorspace",
1144 OFFSET(user_icsp), AV_OPT_TYPE_INT, { .i64 = AVCOL_SPC_UNSPECIFIED },
1145 AVCOL_PRI_RESERVED0, AVCOL_PRI_NB - 1, FLAGS, "csp" },
1146 { "irange", "Input color range",
1147 OFFSET(user_irng), AV_OPT_TYPE_INT, { .i64 = AVCOL_RANGE_UNSPECIFIED },
1148 AVCOL_RANGE_UNSPECIFIED, AVCOL_RANGE_NB - 1, FLAGS, "rng" },
1149 { "iprimaries", "Input color primaries",
1150 OFFSET(user_iprm), AV_OPT_TYPE_INT, { .i64 = AVCOL_PRI_UNSPECIFIED },
1151 AVCOL_PRI_RESERVED0, AVCOL_PRI_NB - 1, FLAGS, "prm" },
1152 { "itrc", "Input transfer characteristics",
1153 OFFSET(user_itrc), AV_OPT_TYPE_INT, { .i64 = AVCOL_TRC_UNSPECIFIED },
1154 AVCOL_TRC_RESERVED0, AVCOL_TRC_NB - 1, FLAGS, "trc" },
1159 AVFILTER_DEFINE_CLASS(colorspace);
1161 static const AVFilterPad inputs[] = {
1164 .type = AVMEDIA_TYPE_VIDEO,
1165 .filter_frame = filter_frame,
1170 static const AVFilterPad outputs[] = {
1173 .type = AVMEDIA_TYPE_VIDEO,
1174 .config_props = config_props,
1179 AVFilter ff_vf_colorspace = {
1180 .name = "colorspace",
1181 .description = NULL_IF_CONFIG_SMALL("Convert between colorspaces."),
1184 .query_formats = query_formats,
1185 .priv_size = sizeof(ColorSpaceContext),
1186 .priv_class = &colorspace_class,
1189 .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC | AVFILTER_FLAG_SLICE_THREADS,