FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavfilter/vf_colorspace.c
Date: 2025-01-20 09:27:23
Exec Total Coverage
Lines: 0 443 0.0%
Functions: 0 12 0.0%
Branches: 0 298 0.0%

Line Branch Exec Source
1 /*
2 * Copyright (c) 2016 Ronald S. Bultje <rsbultje@gmail.com>
3 *
4 * This file is part of FFmpeg.
5 *
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.
10 *
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.
15 *
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
19 */
20
21 /*
22 * @file
23 * Convert between colorspaces.
24 */
25
26 #include "libavutil/avassert.h"
27 #include "libavutil/csp.h"
28 #include "libavutil/frame.h"
29 #include "libavutil/mem.h"
30 #include "libavutil/mem_internal.h"
31 #include "libavutil/opt.h"
32 #include "libavutil/pixdesc.h"
33 #include "libavutil/pixfmt.h"
34
35 #include "avfilter.h"
36 #include "colorspacedsp.h"
37 #include "filters.h"
38 #include "formats.h"
39 #include "video.h"
40 #include "colorspace.h"
41
42 enum DitherMode {
43 DITHER_NONE,
44 DITHER_FSB,
45 DITHER_NB,
46 };
47
48 enum Colorspace {
49 CS_UNSPECIFIED,
50 CS_BT470M,
51 CS_BT470BG,
52 CS_BT601_6_525,
53 CS_BT601_6_625,
54 CS_BT709,
55 CS_SMPTE170M,
56 CS_SMPTE240M,
57 CS_BT2020,
58 CS_NB,
59 };
60
61 enum WhitepointAdaptation {
62 WP_ADAPT_BRADFORD,
63 WP_ADAPT_VON_KRIES,
64 NB_WP_ADAPT_NON_IDENTITY,
65 WP_ADAPT_IDENTITY = NB_WP_ADAPT_NON_IDENTITY,
66 NB_WP_ADAPT,
67 };
68
69 static const enum AVColorTransferCharacteristic default_trc[CS_NB + 1] = {
70 [CS_UNSPECIFIED] = AVCOL_TRC_UNSPECIFIED,
71 [CS_BT470M] = AVCOL_TRC_GAMMA22,
72 [CS_BT470BG] = AVCOL_TRC_GAMMA28,
73 [CS_BT601_6_525] = AVCOL_TRC_SMPTE170M,
74 [CS_BT601_6_625] = AVCOL_TRC_SMPTE170M,
75 [CS_BT709] = AVCOL_TRC_BT709,
76 [CS_SMPTE170M] = AVCOL_TRC_SMPTE170M,
77 [CS_SMPTE240M] = AVCOL_TRC_SMPTE240M,
78 [CS_BT2020] = AVCOL_TRC_BT2020_10,
79 [CS_NB] = AVCOL_TRC_UNSPECIFIED,
80 };
81
82 static const enum AVColorPrimaries default_prm[CS_NB + 1] = {
83 [CS_UNSPECIFIED] = AVCOL_PRI_UNSPECIFIED,
84 [CS_BT470M] = AVCOL_PRI_BT470M,
85 [CS_BT470BG] = AVCOL_PRI_BT470BG,
86 [CS_BT601_6_525] = AVCOL_PRI_SMPTE170M,
87 [CS_BT601_6_625] = AVCOL_PRI_BT470BG,
88 [CS_BT709] = AVCOL_PRI_BT709,
89 [CS_SMPTE170M] = AVCOL_PRI_SMPTE170M,
90 [CS_SMPTE240M] = AVCOL_PRI_SMPTE240M,
91 [CS_BT2020] = AVCOL_PRI_BT2020,
92 [CS_NB] = AVCOL_PRI_UNSPECIFIED,
93 };
94
95 static const enum AVColorSpace default_csp[CS_NB + 1] = {
96 [CS_UNSPECIFIED] = AVCOL_SPC_UNSPECIFIED,
97 [CS_BT470M] = AVCOL_SPC_SMPTE170M,
98 [CS_BT470BG] = AVCOL_SPC_BT470BG,
99 [CS_BT601_6_525] = AVCOL_SPC_SMPTE170M,
100 [CS_BT601_6_625] = AVCOL_SPC_BT470BG,
101 [CS_BT709] = AVCOL_SPC_BT709,
102 [CS_SMPTE170M] = AVCOL_SPC_SMPTE170M,
103 [CS_SMPTE240M] = AVCOL_SPC_SMPTE240M,
104 [CS_BT2020] = AVCOL_SPC_BT2020_NCL,
105 [CS_NB] = AVCOL_SPC_UNSPECIFIED,
106 };
107
108 struct TransferCharacteristics {
109 double alpha, beta, gamma, delta;
110 };
111
112 typedef struct ColorSpaceContext {
113 const AVClass *class;
114
115 ColorSpaceDSPContext dsp;
116
117 enum Colorspace user_all, user_iall;
118 enum AVColorSpace in_csp, out_csp, user_csp, user_icsp;
119 enum AVColorRange in_rng, out_rng, user_rng, user_irng;
120 enum AVColorTransferCharacteristic in_trc, out_trc, user_trc, user_itrc;
121 enum AVColorPrimaries in_prm, out_prm, user_prm, user_iprm;
122 enum AVPixelFormat in_format, user_format;
123 int fast_mode;
124 enum DitherMode dither;
125 enum WhitepointAdaptation wp_adapt;
126
127 int16_t *rgb[3];
128 ptrdiff_t rgb_stride;
129 unsigned rgb_sz;
130 int *dither_scratch[3][2], *dither_scratch_base[3][2];
131
132 const AVColorPrimariesDesc *in_primaries, *out_primaries;
133 int lrgb2lrgb_passthrough;
134 DECLARE_ALIGNED(16, int16_t, lrgb2lrgb_coeffs)[3][3][8];
135
136 const struct TransferCharacteristics *in_txchr, *out_txchr;
137 int rgb2rgb_passthrough;
138 int16_t *lin_lut, *delin_lut;
139
140 const AVLumaCoefficients *in_lumacoef, *out_lumacoef;
141 int yuv2yuv_passthrough, yuv2yuv_fastmode;
142 DECLARE_ALIGNED(16, int16_t, yuv2rgb_coeffs)[3][3][8];
143 DECLARE_ALIGNED(16, int16_t, rgb2yuv_coeffs)[3][3][8];
144 DECLARE_ALIGNED(16, int16_t, yuv2yuv_coeffs)[3][3][8];
145 DECLARE_ALIGNED(16, int16_t, yuv_offset)[2 /* in, out */][8];
146 yuv2rgb_fn yuv2rgb;
147 rgb2yuv_fn rgb2yuv;
148 rgb2yuv_fsb_fn rgb2yuv_fsb;
149 yuv2yuv_fn yuv2yuv;
150 double yuv2rgb_dbl_coeffs[3][3], rgb2yuv_dbl_coeffs[3][3];
151 int in_y_rng, in_uv_rng, out_y_rng, out_uv_rng;
152
153 int did_warn_range;
154 } ColorSpaceContext;
155
156 // FIXME deal with odd width/heights
157 // FIXME faster linearize/delinearize implementation (integer pow)
158 // FIXME bt2020cl support (linearization between yuv/rgb step instead of between rgb/xyz)
159 // FIXME test that the values in (de)lin_lut don't exceed their container storage
160 // type size (only useful if we keep the LUT and don't move to fast integer pow)
161 // FIXME dithering if bitdepth goes down?
162 // FIXME bitexact for fate integration?
163
164 // FIXME I'm pretty sure gamma22/28 also have a linear toe slope, but I can't
165 // find any actual tables that document their real values...
166 // See http://www.13thmonkey.org/~boris/gammacorrection/ first graph why it matters
167 static const struct TransferCharacteristics transfer_characteristics[AVCOL_TRC_NB] = {
168 [AVCOL_TRC_BT709] = { 1.099, 0.018, 0.45, 4.5 },
169 [AVCOL_TRC_GAMMA22] = { 1.0, 0.0, 1.0 / 2.2, 0.0 },
170 [AVCOL_TRC_GAMMA28] = { 1.0, 0.0, 1.0 / 2.8, 0.0 },
171 [AVCOL_TRC_SMPTE170M] = { 1.099, 0.018, 0.45, 4.5 },
172 [AVCOL_TRC_SMPTE240M] = { 1.1115, 0.0228, 0.45, 4.0 },
173 [AVCOL_TRC_LINEAR] = { 1.0, 0.0, 1.0, 0.0 },
174 [AVCOL_TRC_IEC61966_2_1] = { 1.055, 0.0031308, 1.0 / 2.4, 12.92 },
175 [AVCOL_TRC_IEC61966_2_4] = { 1.099, 0.018, 0.45, 4.5 },
176 [AVCOL_TRC_BT2020_10] = { 1.099, 0.018, 0.45, 4.5 },
177 [AVCOL_TRC_BT2020_12] = { 1.0993, 0.0181, 0.45, 4.5 },
178 };
179
180 static const struct TransferCharacteristics *
181 get_transfer_characteristics(enum AVColorTransferCharacteristic trc)
182 {
183 const struct TransferCharacteristics *coeffs;
184
185 if (trc >= AVCOL_TRC_NB)
186 return NULL;
187 coeffs = &transfer_characteristics[trc];
188 if (!coeffs->alpha)
189 return NULL;
190
191 return coeffs;
192 }
193
194 static int fill_gamma_table(ColorSpaceContext *s)
195 {
196 int n;
197 double in_alpha = s->in_txchr->alpha, in_beta = s->in_txchr->beta;
198 double in_gamma = s->in_txchr->gamma, in_delta = s->in_txchr->delta;
199 double in_ialpha = 1.0 / in_alpha, in_igamma = 1.0 / in_gamma, in_idelta = 1.0 / in_delta;
200 double out_alpha = s->out_txchr->alpha, out_beta = s->out_txchr->beta;
201 double out_gamma = s->out_txchr->gamma, out_delta = s->out_txchr->delta;
202
203 s->lin_lut = av_malloc(sizeof(*s->lin_lut) * 32768 * 2);
204 if (!s->lin_lut)
205 return AVERROR(ENOMEM);
206 s->delin_lut = &s->lin_lut[32768];
207 for (n = 0; n < 32768; n++) {
208 double v = (n - 2048.0) / 28672.0, d, l;
209
210 // delinearize
211 if (v <= -out_beta) {
212 d = -out_alpha * pow(-v, out_gamma) + (out_alpha - 1.0);
213 } else if (v < out_beta) {
214 d = out_delta * v;
215 } else {
216 d = out_alpha * pow(v, out_gamma) - (out_alpha - 1.0);
217 }
218 s->delin_lut[n] = av_clip_int16(lrint(d * 28672.0));
219
220 // linearize
221 if (v <= -in_beta * in_delta) {
222 l = -pow((1.0 - in_alpha - v) * in_ialpha, in_igamma);
223 } else if (v < in_beta * in_delta) {
224 l = v * in_idelta;
225 } else {
226 l = pow((v + in_alpha - 1.0) * in_ialpha, in_igamma);
227 }
228 s->lin_lut[n] = av_clip_int16(lrint(l * 28672.0));
229 }
230
231 return 0;
232 }
233
234 /*
235 * See http://www.brucelindbloom.com/index.html?Eqn_ChromAdapt.html
236 * This function uses the Bradford mechanism.
237 */
238 static void fill_whitepoint_conv_table(double out[3][3], enum WhitepointAdaptation wp_adapt,
239 const AVWhitepointCoefficients *wp_src,
240 const AVWhitepointCoefficients *wp_dst)
241 {
242 static const double ma_tbl[NB_WP_ADAPT_NON_IDENTITY][3][3] = {
243 [WP_ADAPT_BRADFORD] = {
244 { 0.8951, 0.2664, -0.1614 },
245 { -0.7502, 1.7135, 0.0367 },
246 { 0.0389, -0.0685, 1.0296 },
247 }, [WP_ADAPT_VON_KRIES] = {
248 { 0.40024, 0.70760, -0.08081 },
249 { -0.22630, 1.16532, 0.04570 },
250 { 0.00000, 0.00000, 0.91822 },
251 },
252 };
253 const double (*ma)[3] = ma_tbl[wp_adapt];
254 double xw_src = av_q2d(wp_src->x), yw_src = av_q2d(wp_src->y);
255 double xw_dst = av_q2d(wp_dst->x), yw_dst = av_q2d(wp_dst->y);
256 double zw_src = 1.0 - xw_src - yw_src;
257 double zw_dst = 1.0 - xw_dst - yw_dst;
258 double mai[3][3], fac[3][3], tmp[3][3];
259 double rs, gs, bs, rd, gd, bd;
260
261 ff_matrix_invert_3x3(ma, mai);
262 rs = ma[0][0] * xw_src + ma[0][1] * yw_src + ma[0][2] * zw_src;
263 gs = ma[1][0] * xw_src + ma[1][1] * yw_src + ma[1][2] * zw_src;
264 bs = ma[2][0] * xw_src + ma[2][1] * yw_src + ma[2][2] * zw_src;
265 rd = ma[0][0] * xw_dst + ma[0][1] * yw_dst + ma[0][2] * zw_dst;
266 gd = ma[1][0] * xw_dst + ma[1][1] * yw_dst + ma[1][2] * zw_dst;
267 bd = ma[2][0] * xw_dst + ma[2][1] * yw_dst + ma[2][2] * zw_dst;
268 fac[0][0] = rd / rs;
269 fac[1][1] = gd / gs;
270 fac[2][2] = bd / bs;
271 fac[0][1] = fac[0][2] = fac[1][0] = fac[1][2] = fac[2][0] = fac[2][1] = 0.0;
272 ff_matrix_mul_3x3(tmp, ma, fac);
273 ff_matrix_mul_3x3(out, tmp, mai);
274 }
275
276 static void apply_lut(int16_t *buf[3], ptrdiff_t stride,
277 int w, int h, const int16_t *lut)
278 {
279 int y, x, n;
280
281 for (n = 0; n < 3; n++) {
282 int16_t *data = buf[n];
283
284 for (y = 0; y < h; y++) {
285 for (x = 0; x < w; x++)
286 data[x] = lut[av_clip_uintp2(2048 + data[x], 15)];
287
288 data += stride;
289 }
290 }
291 }
292
293 typedef struct ThreadData {
294 AVFrame *in, *out;
295 ptrdiff_t in_linesize[3], out_linesize[3];
296 int in_ss_h, out_ss_h;
297 } ThreadData;
298
299 static int convert(AVFilterContext *ctx, void *data, int job_nr, int n_jobs)
300 {
301 const ThreadData *td = data;
302 ColorSpaceContext *s = ctx->priv;
303 uint8_t *in_data[3], *out_data[3];
304 int16_t *rgb[3];
305 int h_in = (td->in->height + 1) >> 1;
306 int h1 = 2 * (job_nr * h_in / n_jobs), h2 = 2 * ((job_nr + 1) * h_in / n_jobs);
307 int w = td->in->width, h = h2 - h1;
308
309 in_data[0] = td->in->data[0] + td->in_linesize[0] * h1;
310 in_data[1] = td->in->data[1] + td->in_linesize[1] * (h1 >> td->in_ss_h);
311 in_data[2] = td->in->data[2] + td->in_linesize[2] * (h1 >> td->in_ss_h);
312 out_data[0] = td->out->data[0] + td->out_linesize[0] * h1;
313 out_data[1] = td->out->data[1] + td->out_linesize[1] * (h1 >> td->out_ss_h);
314 out_data[2] = td->out->data[2] + td->out_linesize[2] * (h1 >> td->out_ss_h);
315 rgb[0] = s->rgb[0] + s->rgb_stride * h1;
316 rgb[1] = s->rgb[1] + s->rgb_stride * h1;
317 rgb[2] = s->rgb[2] + s->rgb_stride * h1;
318
319 // FIXME for simd, also make sure we do pictures with negative stride
320 // top-down so we don't overwrite lines with padding of data before it
321 // in the same buffer (same as swscale)
322
323 if (s->yuv2yuv_fastmode) {
324 // FIXME possibly use a fast mode in case only the y range changes?
325 // since in that case, only the diagonal entries in yuv2yuv_coeffs[]
326 // are non-zero
327 s->yuv2yuv(out_data, td->out_linesize, in_data, td->in_linesize, w, h,
328 s->yuv2yuv_coeffs, s->yuv_offset);
329 } else {
330 // FIXME maybe (for caching efficiency) do pipeline per-line instead of
331 // full buffer per function? (Or, since yuv2rgb requires 2 lines: per
332 // 2 lines, for yuv420.)
333 /*
334 * General design:
335 * - yuv2rgb converts from whatever range the input was ([16-235/240] or
336 * [0,255] or the 10/12bpp equivalents thereof) to an integer version
337 * of RGB in psuedo-restricted 15+sign bits. That means that the float
338 * range [0.0,1.0] is in [0,28762], and the remainder of the int16_t
339 * range is used for overflow/underflow outside the representable
340 * range of this RGB type. rgb2yuv is the exact opposite.
341 * - gamma correction is done using a LUT since that appears to work
342 * fairly fast.
343 * - If the input is chroma-subsampled (420/422), the yuv2rgb conversion
344 * (or rgb2yuv conversion) uses nearest-neighbour sampling to read
345 * read chroma pixels at luma resolution. If you want some more fancy
346 * filter, you can use swscale to convert to yuv444p.
347 * - all coefficients are 14bit (so in the [-2.0,2.0] range).
348 */
349 s->yuv2rgb(rgb, s->rgb_stride, in_data, td->in_linesize, w, h,
350 s->yuv2rgb_coeffs, s->yuv_offset[0]);
351 if (!s->rgb2rgb_passthrough) {
352 apply_lut(rgb, s->rgb_stride, w, h, s->lin_lut);
353 if (!s->lrgb2lrgb_passthrough)
354 s->dsp.multiply3x3(rgb, s->rgb_stride, w, h, s->lrgb2lrgb_coeffs);
355 apply_lut(rgb, s->rgb_stride, w, h, s->delin_lut);
356 }
357 if (s->dither == DITHER_FSB) {
358 s->rgb2yuv_fsb(out_data, td->out_linesize, rgb, s->rgb_stride, w, h,
359 s->rgb2yuv_coeffs, s->yuv_offset[1], s->dither_scratch);
360 } else {
361 s->rgb2yuv(out_data, td->out_linesize, rgb, s->rgb_stride, w, h,
362 s->rgb2yuv_coeffs, s->yuv_offset[1]);
363 }
364 }
365
366 return 0;
367 }
368
369 static int get_range_off(AVFilterContext *ctx, int *off,
370 int *y_rng, int *uv_rng,
371 enum AVColorRange rng, int depth)
372 {
373 switch (rng) {
374 case AVCOL_RANGE_UNSPECIFIED: {
375 ColorSpaceContext *s = ctx->priv;
376
377 if (!s->did_warn_range) {
378 av_log(ctx, AV_LOG_WARNING, "Input range not set, assuming tv/mpeg\n");
379 s->did_warn_range = 1;
380 }
381 }
382 // fall-through
383 case AVCOL_RANGE_MPEG:
384 *off = 16 << (depth - 8);
385 *y_rng = 219 << (depth - 8);
386 *uv_rng = 224 << (depth - 8);
387 break;
388 case AVCOL_RANGE_JPEG:
389 *off = 0;
390 *y_rng = *uv_rng = (256 << (depth - 8)) - 1;
391 break;
392 default:
393 return AVERROR(EINVAL);
394 }
395
396 return 0;
397 }
398
399 static int create_filtergraph(AVFilterContext *ctx,
400 const AVFrame *in, const AVFrame *out)
401 {
402 ColorSpaceContext *s = ctx->priv;
403 const AVPixFmtDescriptor *in_desc = av_pix_fmt_desc_get(in->format);
404 const AVPixFmtDescriptor *out_desc = av_pix_fmt_desc_get(out->format);
405 int m, n, o, res, fmt_identical, redo_yuv2rgb = 0, redo_rgb2yuv = 0;
406
407 #define supported_depth(d) ((d) == 8 || (d) == 10 || (d) == 12)
408 #define supported_subsampling(lcw, lch) \
409 (((lcw) == 0 && (lch) == 0) || ((lcw) == 1 && (lch) == 0) || ((lcw) == 1 && (lch) == 1))
410 #define supported_format(d) \
411 ((d) != NULL && (d)->nb_components == 3 && \
412 !((d)->flags & AV_PIX_FMT_FLAG_RGB) && \
413 supported_depth((d)->comp[0].depth) && \
414 supported_subsampling((d)->log2_chroma_w, (d)->log2_chroma_h))
415
416 if (!supported_format(in_desc)) {
417 av_log(ctx, AV_LOG_ERROR,
418 "Unsupported input format %d (%s) or bitdepth (%d)\n",
419 in->format, av_get_pix_fmt_name(in->format),
420 in_desc ? in_desc->comp[0].depth : -1);
421 return AVERROR(EINVAL);
422 }
423 if (!supported_format(out_desc)) {
424 av_log(ctx, AV_LOG_ERROR,
425 "Unsupported output format %d (%s) or bitdepth (%d)\n",
426 out->format, av_get_pix_fmt_name(out->format),
427 out_desc ? out_desc->comp[0].depth : -1);
428 return AVERROR(EINVAL);
429 }
430
431 if (in->color_primaries != s->in_prm) s->in_primaries = NULL;
432 if (out->color_primaries != s->out_prm) s->out_primaries = NULL;
433 if (in->color_trc != s->in_trc) s->in_txchr = NULL;
434 if (out->color_trc != s->out_trc) s->out_txchr = NULL;
435 if (in->colorspace != s->in_csp ||
436 in->color_range != s->in_rng) s->in_lumacoef = NULL;
437 if (out->color_range != s->out_rng) s->rgb2yuv = NULL;
438
439 if (!s->out_primaries || !s->in_primaries) {
440 s->in_prm = in->color_primaries;
441 if (s->user_iall != CS_UNSPECIFIED)
442 s->in_prm = default_prm[FFMIN(s->user_iall, CS_NB)];
443 if (s->user_iprm != AVCOL_PRI_UNSPECIFIED)
444 s->in_prm = s->user_iprm;
445 s->in_primaries = av_csp_primaries_desc_from_id(s->in_prm);
446 if (!s->in_primaries) {
447 av_log(ctx, AV_LOG_ERROR,
448 "Unsupported input primaries %d (%s)\n",
449 s->in_prm, av_color_primaries_name(s->in_prm));
450 return AVERROR(EINVAL);
451 }
452 s->out_prm = out->color_primaries;
453 s->out_primaries = av_csp_primaries_desc_from_id(s->out_prm);
454 if (!s->out_primaries) {
455 if (s->out_prm == AVCOL_PRI_UNSPECIFIED) {
456 if (s->user_all == CS_UNSPECIFIED) {
457 av_log(ctx, AV_LOG_ERROR, "Please specify output primaries\n");
458 } else {
459 av_log(ctx, AV_LOG_ERROR,
460 "Unsupported output color property %d\n", s->user_all);
461 }
462 } else {
463 av_log(ctx, AV_LOG_ERROR,
464 "Unsupported output primaries %d (%s)\n",
465 s->out_prm, av_color_primaries_name(s->out_prm));
466 }
467 return AVERROR(EINVAL);
468 }
469 s->lrgb2lrgb_passthrough = !memcmp(s->in_primaries, s->out_primaries,
470 sizeof(*s->in_primaries));
471 if (!s->lrgb2lrgb_passthrough) {
472 double rgb2xyz[3][3], xyz2rgb[3][3], rgb2rgb[3][3];
473 const AVWhitepointCoefficients *wp_out, *wp_in;
474
475 wp_out = &s->out_primaries->wp;
476 wp_in = &s->in_primaries->wp;
477 ff_fill_rgb2xyz_table(&s->out_primaries->prim, wp_out, rgb2xyz);
478 ff_matrix_invert_3x3(rgb2xyz, xyz2rgb);
479 ff_fill_rgb2xyz_table(&s->in_primaries->prim, wp_in, rgb2xyz);
480 if (memcmp(wp_in, wp_out, sizeof(*wp_in)) != 0 &&
481 s->wp_adapt != WP_ADAPT_IDENTITY) {
482 double wpconv[3][3], tmp[3][3];
483
484 fill_whitepoint_conv_table(wpconv, s->wp_adapt, &s->in_primaries->wp,
485 &s->out_primaries->wp);
486 ff_matrix_mul_3x3(tmp, rgb2xyz, wpconv);
487 ff_matrix_mul_3x3(rgb2rgb, tmp, xyz2rgb);
488 } else {
489 ff_matrix_mul_3x3(rgb2rgb, rgb2xyz, xyz2rgb);
490 }
491 for (m = 0; m < 3; m++)
492 for (n = 0; n < 3; n++) {
493 s->lrgb2lrgb_coeffs[m][n][0] = lrint(16384.0 * rgb2rgb[m][n]);
494 for (o = 1; o < 8; o++)
495 s->lrgb2lrgb_coeffs[m][n][o] = s->lrgb2lrgb_coeffs[m][n][0];
496 }
497
498 }
499 }
500
501 if (!s->in_txchr) {
502 av_freep(&s->lin_lut);
503 s->in_trc = in->color_trc;
504 if (s->user_iall != CS_UNSPECIFIED)
505 s->in_trc = default_trc[FFMIN(s->user_iall, CS_NB)];
506 if (s->user_itrc != AVCOL_TRC_UNSPECIFIED)
507 s->in_trc = s->user_itrc;
508 s->in_txchr = get_transfer_characteristics(s->in_trc);
509 if (!s->in_txchr) {
510 av_log(ctx, AV_LOG_ERROR,
511 "Unsupported input transfer characteristics %d (%s)\n",
512 s->in_trc, av_color_transfer_name(s->in_trc));
513 return AVERROR(EINVAL);
514 }
515 }
516
517 if (!s->out_txchr) {
518 av_freep(&s->lin_lut);
519 s->out_trc = out->color_trc;
520 s->out_txchr = get_transfer_characteristics(s->out_trc);
521 if (!s->out_txchr) {
522 if (s->out_trc == AVCOL_TRC_UNSPECIFIED) {
523 if (s->user_all == CS_UNSPECIFIED) {
524 av_log(ctx, AV_LOG_ERROR,
525 "Please specify output transfer characteristics\n");
526 } else {
527 av_log(ctx, AV_LOG_ERROR,
528 "Unsupported output color property %d\n", s->user_all);
529 }
530 } else {
531 av_log(ctx, AV_LOG_ERROR,
532 "Unsupported output transfer characteristics %d (%s)\n",
533 s->out_trc, av_color_transfer_name(s->out_trc));
534 }
535 return AVERROR(EINVAL);
536 }
537 }
538
539 s->rgb2rgb_passthrough = s->fast_mode || (s->lrgb2lrgb_passthrough &&
540 !memcmp(s->in_txchr, s->out_txchr, sizeof(*s->in_txchr)));
541 if (!s->rgb2rgb_passthrough && !s->lin_lut) {
542 res = fill_gamma_table(s);
543 if (res < 0)
544 return res;
545 }
546
547 if (!s->in_lumacoef) {
548 s->in_csp = in->colorspace;
549 if (s->user_iall != CS_UNSPECIFIED)
550 s->in_csp = default_csp[FFMIN(s->user_iall, CS_NB)];
551 if (s->user_icsp != AVCOL_SPC_UNSPECIFIED)
552 s->in_csp = s->user_icsp;
553 s->in_rng = in->color_range;
554 if (s->user_irng != AVCOL_RANGE_UNSPECIFIED)
555 s->in_rng = s->user_irng;
556 s->in_lumacoef = av_csp_luma_coeffs_from_avcsp(s->in_csp);
557 if (!s->in_lumacoef) {
558 av_log(ctx, AV_LOG_ERROR,
559 "Unsupported input colorspace %d (%s)\n",
560 s->in_csp, av_color_space_name(s->in_csp));
561 return AVERROR(EINVAL);
562 }
563 redo_yuv2rgb = 1;
564 }
565
566 if (!s->rgb2yuv) {
567 s->out_rng = out->color_range;
568 redo_rgb2yuv = 1;
569 }
570
571 fmt_identical = in_desc->log2_chroma_h == out_desc->log2_chroma_h &&
572 in_desc->log2_chroma_w == out_desc->log2_chroma_w;
573 s->yuv2yuv_fastmode = s->rgb2rgb_passthrough && fmt_identical;
574 s->yuv2yuv_passthrough = s->yuv2yuv_fastmode && s->in_rng == s->out_rng &&
575 !memcmp(s->in_lumacoef, s->out_lumacoef,
576 sizeof(*s->in_lumacoef)) &&
577 in_desc->comp[0].depth == out_desc->comp[0].depth;
578 if (!s->yuv2yuv_passthrough) {
579 if (redo_yuv2rgb) {
580 double rgb2yuv[3][3], (*yuv2rgb)[3] = s->yuv2rgb_dbl_coeffs;
581 int off, bits, in_rng;
582
583 res = get_range_off(ctx, &off, &s->in_y_rng, &s->in_uv_rng,
584 s->in_rng, in_desc->comp[0].depth);
585 if (res < 0) {
586 av_log(ctx, AV_LOG_ERROR,
587 "Unsupported input color range %d (%s)\n",
588 s->in_rng, av_color_range_name(s->in_rng));
589 return res;
590 }
591 for (n = 0; n < 8; n++)
592 s->yuv_offset[0][n] = off;
593 ff_fill_rgb2yuv_table(s->in_lumacoef, rgb2yuv);
594 ff_matrix_invert_3x3(rgb2yuv, yuv2rgb);
595 bits = 1 << (in_desc->comp[0].depth - 1);
596 for (n = 0; n < 3; n++) {
597 for (in_rng = s->in_y_rng, m = 0; m < 3; m++, in_rng = s->in_uv_rng) {
598 s->yuv2rgb_coeffs[n][m][0] = lrint(28672 * bits * yuv2rgb[n][m] / in_rng);
599 for (o = 1; o < 8; o++)
600 s->yuv2rgb_coeffs[n][m][o] = s->yuv2rgb_coeffs[n][m][0];
601 }
602 }
603 av_assert2(s->yuv2rgb_coeffs[0][1][0] == 0);
604 av_assert2(s->yuv2rgb_coeffs[2][2][0] == 0);
605 av_assert2(s->yuv2rgb_coeffs[0][0][0] == s->yuv2rgb_coeffs[1][0][0]);
606 av_assert2(s->yuv2rgb_coeffs[0][0][0] == s->yuv2rgb_coeffs[2][0][0]);
607 s->yuv2rgb = s->dsp.yuv2rgb[(in_desc->comp[0].depth - 8) >> 1]
608 [in_desc->log2_chroma_h + in_desc->log2_chroma_w];
609 }
610
611 if (redo_rgb2yuv) {
612 double (*rgb2yuv)[3] = s->rgb2yuv_dbl_coeffs;
613 int off, out_rng, bits;
614
615 res = get_range_off(ctx, &off, &s->out_y_rng, &s->out_uv_rng,
616 s->out_rng, out_desc->comp[0].depth);
617 if (res < 0) {
618 av_log(ctx, AV_LOG_ERROR,
619 "Unsupported output color range %d (%s)\n",
620 s->out_rng, av_color_range_name(s->out_rng));
621 return res;
622 }
623 for (n = 0; n < 8; n++)
624 s->yuv_offset[1][n] = off;
625 ff_fill_rgb2yuv_table(s->out_lumacoef, rgb2yuv);
626 bits = 1 << (29 - out_desc->comp[0].depth);
627 for (out_rng = s->out_y_rng, n = 0; n < 3; n++, out_rng = s->out_uv_rng) {
628 for (m = 0; m < 3; m++) {
629 s->rgb2yuv_coeffs[n][m][0] = lrint(bits * out_rng * rgb2yuv[n][m] / 28672);
630 for (o = 1; o < 8; o++)
631 s->rgb2yuv_coeffs[n][m][o] = s->rgb2yuv_coeffs[n][m][0];
632 }
633 }
634 av_assert2(s->rgb2yuv_coeffs[1][2][0] == s->rgb2yuv_coeffs[2][0][0]);
635 s->rgb2yuv = s->dsp.rgb2yuv[(out_desc->comp[0].depth - 8) >> 1]
636 [out_desc->log2_chroma_h + out_desc->log2_chroma_w];
637 s->rgb2yuv_fsb = s->dsp.rgb2yuv_fsb[(out_desc->comp[0].depth - 8) >> 1]
638 [out_desc->log2_chroma_h + out_desc->log2_chroma_w];
639 }
640
641 if (s->yuv2yuv_fastmode && (redo_yuv2rgb || redo_rgb2yuv)) {
642 int idepth = in_desc->comp[0].depth, odepth = out_desc->comp[0].depth;
643 double (*rgb2yuv)[3] = s->rgb2yuv_dbl_coeffs;
644 double (*yuv2rgb)[3] = s->yuv2rgb_dbl_coeffs;
645 double yuv2yuv[3][3];
646 int in_rng, out_rng;
647
648 ff_matrix_mul_3x3(yuv2yuv, yuv2rgb, rgb2yuv);
649 for (out_rng = s->out_y_rng, m = 0; m < 3; m++, out_rng = s->out_uv_rng) {
650 for (in_rng = s->in_y_rng, n = 0; n < 3; n++, in_rng = s->in_uv_rng) {
651 s->yuv2yuv_coeffs[m][n][0] =
652 lrint(16384 * yuv2yuv[m][n] * out_rng * (1 << idepth) /
653 (in_rng * (1 << odepth)));
654 for (o = 1; o < 8; o++)
655 s->yuv2yuv_coeffs[m][n][o] = s->yuv2yuv_coeffs[m][n][0];
656 }
657 }
658 av_assert2(s->yuv2yuv_coeffs[1][0][0] == 0);
659 av_assert2(s->yuv2yuv_coeffs[2][0][0] == 0);
660 s->yuv2yuv = s->dsp.yuv2yuv[(idepth - 8) >> 1][(odepth - 8) >> 1]
661 [in_desc->log2_chroma_h + in_desc->log2_chroma_w];
662 }
663 }
664
665 return 0;
666 }
667
668 static av_cold int init(AVFilterContext *ctx)
669 {
670 ColorSpaceContext *s = ctx->priv;
671
672 s->out_csp = s->user_csp == AVCOL_SPC_UNSPECIFIED ?
673 default_csp[FFMIN(s->user_all, CS_NB)] : s->user_csp;
674 s->out_lumacoef = av_csp_luma_coeffs_from_avcsp(s->out_csp);
675 if (!s->out_lumacoef) {
676 if (s->out_csp == AVCOL_SPC_UNSPECIFIED) {
677 if (s->user_all == CS_UNSPECIFIED) {
678 av_log(ctx, AV_LOG_ERROR,
679 "Please specify output colorspace\n");
680 } else {
681 av_log(ctx, AV_LOG_ERROR,
682 "Unsupported output color property %d\n", s->user_all);
683 }
684 } else {
685 av_log(ctx, AV_LOG_ERROR,
686 "Unsupported output colorspace %d (%s)\n", s->out_csp,
687 av_color_space_name(s->out_csp));
688 }
689 return AVERROR(EINVAL);
690 }
691
692 ff_colorspacedsp_init(&s->dsp);
693
694 return 0;
695 }
696
697 static void uninit(AVFilterContext *ctx)
698 {
699 ColorSpaceContext *s = ctx->priv;
700
701 av_freep(&s->rgb[0]);
702 av_freep(&s->rgb[1]);
703 av_freep(&s->rgb[2]);
704 s->rgb_sz = 0;
705 av_freep(&s->dither_scratch_base[0][0]);
706 av_freep(&s->dither_scratch_base[0][1]);
707 av_freep(&s->dither_scratch_base[1][0]);
708 av_freep(&s->dither_scratch_base[1][1]);
709 av_freep(&s->dither_scratch_base[2][0]);
710 av_freep(&s->dither_scratch_base[2][1]);
711
712 av_freep(&s->lin_lut);
713 }
714
715 static int filter_frame(AVFilterLink *link, AVFrame *in)
716 {
717 AVFilterContext *ctx = link->dst;
718 AVFilterLink *outlink = ctx->outputs[0];
719 ColorSpaceContext *s = ctx->priv;
720 // FIXME if yuv2yuv_passthrough, don't get a new buffer but use the
721 // input one if it is writable *OR* the actual literal values of in_*
722 // and out_* are identical (not just their respective properties)
723 AVFrame *out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
724 int res;
725 ptrdiff_t rgb_stride = FFALIGN(in->width * sizeof(int16_t), 32);
726 unsigned rgb_sz = rgb_stride * in->height;
727 ThreadData td;
728
729 if (!out) {
730 av_frame_free(&in);
731 return AVERROR(ENOMEM);
732 }
733 res = av_frame_copy_props(out, in);
734 if (res < 0) {
735 av_frame_free(&in);
736 av_frame_free(&out);
737 return res;
738 }
739
740 out->colorspace = s->out_csp;
741 out->color_range = s->user_rng == AVCOL_RANGE_UNSPECIFIED ?
742 in->color_range : s->user_rng;
743 out->color_primaries = s->user_prm == AVCOL_PRI_UNSPECIFIED ?
744 default_prm[FFMIN(s->user_all, CS_NB)] : s->user_prm;
745 if (s->user_trc == AVCOL_TRC_UNSPECIFIED) {
746 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(out->format);
747
748 out->color_trc = default_trc[FFMIN(s->user_all, CS_NB)];
749 if (out->color_trc == AVCOL_TRC_BT2020_10 && desc && desc->comp[0].depth >= 12)
750 out->color_trc = AVCOL_TRC_BT2020_12;
751 } else {
752 out->color_trc = s->user_trc;
753 }
754
755 if (out->color_primaries != in->color_primaries || out->color_trc != in->color_trc) {
756 av_frame_side_data_remove_by_props(&out->side_data, &out->nb_side_data,
757 AV_SIDE_DATA_PROP_COLOR_DEPENDENT);
758 }
759
760 if (rgb_sz != s->rgb_sz) {
761 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(out->format);
762 int uvw = in->width >> desc->log2_chroma_w;
763
764 av_freep(&s->rgb[0]);
765 av_freep(&s->rgb[1]);
766 av_freep(&s->rgb[2]);
767 s->rgb_sz = 0;
768 av_freep(&s->dither_scratch_base[0][0]);
769 av_freep(&s->dither_scratch_base[0][1]);
770 av_freep(&s->dither_scratch_base[1][0]);
771 av_freep(&s->dither_scratch_base[1][1]);
772 av_freep(&s->dither_scratch_base[2][0]);
773 av_freep(&s->dither_scratch_base[2][1]);
774
775 s->rgb[0] = av_malloc(rgb_sz);
776 s->rgb[1] = av_malloc(rgb_sz);
777 s->rgb[2] = av_malloc(rgb_sz);
778 s->dither_scratch_base[0][0] =
779 av_malloc(sizeof(*s->dither_scratch_base[0][0]) * (in->width + 4));
780 s->dither_scratch_base[0][1] =
781 av_malloc(sizeof(*s->dither_scratch_base[0][1]) * (in->width + 4));
782 s->dither_scratch_base[1][0] =
783 av_malloc(sizeof(*s->dither_scratch_base[1][0]) * (uvw + 4));
784 s->dither_scratch_base[1][1] =
785 av_malloc(sizeof(*s->dither_scratch_base[1][1]) * (uvw + 4));
786 s->dither_scratch_base[2][0] =
787 av_malloc(sizeof(*s->dither_scratch_base[2][0]) * (uvw + 4));
788 s->dither_scratch_base[2][1] =
789 av_malloc(sizeof(*s->dither_scratch_base[2][1]) * (uvw + 4));
790 s->dither_scratch[0][0] = &s->dither_scratch_base[0][0][1];
791 s->dither_scratch[0][1] = &s->dither_scratch_base[0][1][1];
792 s->dither_scratch[1][0] = &s->dither_scratch_base[1][0][1];
793 s->dither_scratch[1][1] = &s->dither_scratch_base[1][1][1];
794 s->dither_scratch[2][0] = &s->dither_scratch_base[2][0][1];
795 s->dither_scratch[2][1] = &s->dither_scratch_base[2][1][1];
796 if (!s->rgb[0] || !s->rgb[1] || !s->rgb[2] ||
797 !s->dither_scratch_base[0][0] || !s->dither_scratch_base[0][1] ||
798 !s->dither_scratch_base[1][0] || !s->dither_scratch_base[1][1] ||
799 !s->dither_scratch_base[2][0] || !s->dither_scratch_base[2][1]) {
800 uninit(ctx);
801 av_frame_free(&in);
802 av_frame_free(&out);
803 return AVERROR(ENOMEM);
804 }
805 s->rgb_sz = rgb_sz;
806 }
807 res = create_filtergraph(ctx, in, out);
808 if (res < 0) {
809 av_frame_free(&in);
810 av_frame_free(&out);
811 return res;
812 }
813 s->rgb_stride = rgb_stride / sizeof(int16_t);
814 td.in = in;
815 td.out = out;
816 td.in_linesize[0] = in->linesize[0];
817 td.in_linesize[1] = in->linesize[1];
818 td.in_linesize[2] = in->linesize[2];
819 td.out_linesize[0] = out->linesize[0];
820 td.out_linesize[1] = out->linesize[1];
821 td.out_linesize[2] = out->linesize[2];
822 td.in_ss_h = av_pix_fmt_desc_get(in->format)->log2_chroma_h;
823 td.out_ss_h = av_pix_fmt_desc_get(out->format)->log2_chroma_h;
824 if (s->yuv2yuv_passthrough) {
825 res = av_frame_copy(out, in);
826 if (res < 0) {
827 av_frame_free(&in);
828 av_frame_free(&out);
829 return res;
830 }
831 } else {
832 ff_filter_execute(ctx, convert, &td, NULL,
833 FFMIN((in->height + 1) >> 1, ff_filter_get_nb_threads(ctx)));
834 }
835 av_frame_free(&in);
836
837 return ff_filter_frame(outlink, out);
838 }
839
840 static int query_formats(const AVFilterContext *ctx,
841 AVFilterFormatsConfig **cfg_in,
842 AVFilterFormatsConfig **cfg_out)
843 {
844 static const enum AVPixelFormat pix_fmts[] = {
845 AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV444P,
846 AV_PIX_FMT_YUV420P10, AV_PIX_FMT_YUV422P10, AV_PIX_FMT_YUV444P10,
847 AV_PIX_FMT_YUV420P12, AV_PIX_FMT_YUV422P12, AV_PIX_FMT_YUV444P12,
848 AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ444P,
849 AV_PIX_FMT_NONE
850 };
851 int res;
852 const ColorSpaceContext *s = ctx->priv;
853 AVFilterFormats *formats;
854
855 res = ff_formats_ref(ff_make_formats_list_singleton(s->out_csp), &cfg_out[0]->color_spaces);
856 if (res < 0)
857 return res;
858 if (s->user_rng != AVCOL_RANGE_UNSPECIFIED) {
859 res = ff_formats_ref(ff_make_formats_list_singleton(s->user_rng), &cfg_out[0]->color_ranges);
860 if (res < 0)
861 return res;
862 }
863
864 formats = ff_make_format_list(pix_fmts);
865 if (!formats)
866 return AVERROR(ENOMEM);
867 if (s->user_format == AV_PIX_FMT_NONE)
868 return ff_set_common_formats2(ctx, cfg_in, cfg_out, formats);
869
870 res = ff_formats_ref(formats, &cfg_in[0]->formats);
871 if (res < 0)
872 return res;
873
874 formats = NULL;
875 res = ff_add_format(&formats, s->user_format);
876 if (res < 0)
877 return res;
878
879 return ff_formats_ref(formats, &cfg_out[0]->formats);
880 }
881
882 static int config_props(AVFilterLink *outlink)
883 {
884 AVFilterContext *ctx = outlink->dst;
885 AVFilterLink *inlink = outlink->src->inputs[0];
886
887 if (inlink->w % 2 || inlink->h % 2) {
888 av_log(ctx, AV_LOG_ERROR, "Invalid odd size (%dx%d)\n",
889 inlink->w, inlink->h);
890 return AVERROR_PATCHWELCOME;
891 }
892
893 outlink->w = inlink->w;
894 outlink->h = inlink->h;
895 outlink->sample_aspect_ratio = inlink->sample_aspect_ratio;
896 outlink->time_base = inlink->time_base;
897
898 return 0;
899 }
900
901 #define OFFSET(x) offsetof(ColorSpaceContext, x)
902 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM | AV_OPT_FLAG_VIDEO_PARAM
903 #define ENUM(x, y, z) { x, "", 0, AV_OPT_TYPE_CONST, { .i64 = y }, INT_MIN, INT_MAX, FLAGS, .unit = z }
904
905 static const AVOption colorspace_options[] = {
906 { "all", "Set all color properties together",
907 OFFSET(user_all), AV_OPT_TYPE_INT, { .i64 = CS_UNSPECIFIED },
908 CS_UNSPECIFIED, CS_NB - 1, FLAGS, .unit = "all" },
909 ENUM("bt470m", CS_BT470M, "all"),
910 ENUM("bt470bg", CS_BT470BG, "all"),
911 ENUM("bt601-6-525", CS_BT601_6_525, "all"),
912 ENUM("bt601-6-625", CS_BT601_6_625, "all"),
913 ENUM("bt709", CS_BT709, "all"),
914 ENUM("smpte170m", CS_SMPTE170M, "all"),
915 ENUM("smpte240m", CS_SMPTE240M, "all"),
916 ENUM("bt2020", CS_BT2020, "all"),
917
918 { "space", "Output colorspace",
919 OFFSET(user_csp), AV_OPT_TYPE_INT, { .i64 = AVCOL_SPC_UNSPECIFIED },
920 AVCOL_SPC_RGB, AVCOL_SPC_NB - 1, FLAGS, .unit = "csp"},
921 ENUM("bt709", AVCOL_SPC_BT709, "csp"),
922 ENUM("fcc", AVCOL_SPC_FCC, "csp"),
923 ENUM("bt470bg", AVCOL_SPC_BT470BG, "csp"),
924 ENUM("smpte170m", AVCOL_SPC_SMPTE170M, "csp"),
925 ENUM("smpte240m", AVCOL_SPC_SMPTE240M, "csp"),
926 ENUM("ycgco", AVCOL_SPC_YCGCO, "csp"),
927 ENUM("gbr", AVCOL_SPC_RGB, "csp"),
928 ENUM("bt2020nc", AVCOL_SPC_BT2020_NCL, "csp"),
929 ENUM("bt2020ncl", AVCOL_SPC_BT2020_NCL, "csp"),
930
931 { "range", "Output color range",
932 OFFSET(user_rng), AV_OPT_TYPE_INT, { .i64 = AVCOL_RANGE_UNSPECIFIED },
933 AVCOL_RANGE_UNSPECIFIED, AVCOL_RANGE_NB - 1, FLAGS, .unit = "rng" },
934 ENUM("tv", AVCOL_RANGE_MPEG, "rng"),
935 ENUM("mpeg", AVCOL_RANGE_MPEG, "rng"),
936 ENUM("pc", AVCOL_RANGE_JPEG, "rng"),
937 ENUM("jpeg", AVCOL_RANGE_JPEG, "rng"),
938
939 { "primaries", "Output color primaries",
940 OFFSET(user_prm), AV_OPT_TYPE_INT, { .i64 = AVCOL_PRI_UNSPECIFIED },
941 AVCOL_PRI_RESERVED0, AVCOL_PRI_NB - 1, FLAGS, .unit = "prm" },
942 ENUM("bt709", AVCOL_PRI_BT709, "prm"),
943 ENUM("bt470m", AVCOL_PRI_BT470M, "prm"),
944 ENUM("bt470bg", AVCOL_PRI_BT470BG, "prm"),
945 ENUM("smpte170m", AVCOL_PRI_SMPTE170M, "prm"),
946 ENUM("smpte240m", AVCOL_PRI_SMPTE240M, "prm"),
947 ENUM("smpte428", AVCOL_PRI_SMPTE428, "prm"),
948 ENUM("film", AVCOL_PRI_FILM, "prm"),
949 ENUM("smpte431", AVCOL_PRI_SMPTE431, "prm"),
950 ENUM("smpte432", AVCOL_PRI_SMPTE432, "prm"),
951 ENUM("bt2020", AVCOL_PRI_BT2020, "prm"),
952 ENUM("jedec-p22", AVCOL_PRI_JEDEC_P22, "prm"),
953 ENUM("ebu3213", AVCOL_PRI_EBU3213, "prm"),
954
955 { "trc", "Output transfer characteristics",
956 OFFSET(user_trc), AV_OPT_TYPE_INT, { .i64 = AVCOL_TRC_UNSPECIFIED },
957 AVCOL_TRC_RESERVED0, AVCOL_TRC_NB - 1, FLAGS, .unit = "trc" },
958 ENUM("bt709", AVCOL_TRC_BT709, "trc"),
959 ENUM("bt470m", AVCOL_TRC_GAMMA22, "trc"),
960 ENUM("gamma22", AVCOL_TRC_GAMMA22, "trc"),
961 ENUM("bt470bg", AVCOL_TRC_GAMMA28, "trc"),
962 ENUM("gamma28", AVCOL_TRC_GAMMA28, "trc"),
963 ENUM("smpte170m", AVCOL_TRC_SMPTE170M, "trc"),
964 ENUM("smpte240m", AVCOL_TRC_SMPTE240M, "trc"),
965 ENUM("linear", AVCOL_TRC_LINEAR, "trc"),
966 ENUM("srgb", AVCOL_TRC_IEC61966_2_1, "trc"),
967 ENUM("iec61966-2-1", AVCOL_TRC_IEC61966_2_1, "trc"),
968 ENUM("xvycc", AVCOL_TRC_IEC61966_2_4, "trc"),
969 ENUM("iec61966-2-4", AVCOL_TRC_IEC61966_2_4, "trc"),
970 ENUM("bt2020-10", AVCOL_TRC_BT2020_10, "trc"),
971 ENUM("bt2020-12", AVCOL_TRC_BT2020_12, "trc"),
972
973 { "format", "Output pixel format",
974 OFFSET(user_format), AV_OPT_TYPE_INT, { .i64 = AV_PIX_FMT_NONE },
975 AV_PIX_FMT_NONE, AV_PIX_FMT_GBRAP12LE, FLAGS, .unit = "fmt" },
976 ENUM("yuv420p", AV_PIX_FMT_YUV420P, "fmt"),
977 ENUM("yuv420p10", AV_PIX_FMT_YUV420P10, "fmt"),
978 ENUM("yuv420p12", AV_PIX_FMT_YUV420P12, "fmt"),
979 ENUM("yuv422p", AV_PIX_FMT_YUV422P, "fmt"),
980 ENUM("yuv422p10", AV_PIX_FMT_YUV422P10, "fmt"),
981 ENUM("yuv422p12", AV_PIX_FMT_YUV422P12, "fmt"),
982 ENUM("yuv444p", AV_PIX_FMT_YUV444P, "fmt"),
983 ENUM("yuv444p10", AV_PIX_FMT_YUV444P10, "fmt"),
984 ENUM("yuv444p12", AV_PIX_FMT_YUV444P12, "fmt"),
985
986 { "fast", "Ignore primary chromaticity and gamma correction",
987 OFFSET(fast_mode), AV_OPT_TYPE_BOOL, { .i64 = 0 },
988 0, 1, FLAGS },
989
990 { "dither", "Dithering mode",
991 OFFSET(dither), AV_OPT_TYPE_INT, { .i64 = DITHER_NONE },
992 DITHER_NONE, DITHER_NB - 1, FLAGS, .unit = "dither" },
993 ENUM("none", DITHER_NONE, "dither"),
994 ENUM("fsb", DITHER_FSB, "dither"),
995
996 { "wpadapt", "Whitepoint adaptation method",
997 OFFSET(wp_adapt), AV_OPT_TYPE_INT, { .i64 = WP_ADAPT_BRADFORD },
998 WP_ADAPT_BRADFORD, NB_WP_ADAPT - 1, FLAGS, .unit = "wpadapt" },
999 ENUM("bradford", WP_ADAPT_BRADFORD, "wpadapt"),
1000 ENUM("vonkries", WP_ADAPT_VON_KRIES, "wpadapt"),
1001 ENUM("identity", WP_ADAPT_IDENTITY, "wpadapt"),
1002
1003 { "iall", "Set all input color properties together",
1004 OFFSET(user_iall), AV_OPT_TYPE_INT, { .i64 = CS_UNSPECIFIED },
1005 CS_UNSPECIFIED, CS_NB - 1, FLAGS, .unit = "all" },
1006 { "ispace", "Input colorspace",
1007 OFFSET(user_icsp), AV_OPT_TYPE_INT, { .i64 = AVCOL_SPC_UNSPECIFIED },
1008 AVCOL_PRI_RESERVED0, AVCOL_PRI_NB - 1, FLAGS, .unit = "csp" },
1009 { "irange", "Input color range",
1010 OFFSET(user_irng), AV_OPT_TYPE_INT, { .i64 = AVCOL_RANGE_UNSPECIFIED },
1011 AVCOL_RANGE_UNSPECIFIED, AVCOL_RANGE_NB - 1, FLAGS, .unit = "rng" },
1012 { "iprimaries", "Input color primaries",
1013 OFFSET(user_iprm), AV_OPT_TYPE_INT, { .i64 = AVCOL_PRI_UNSPECIFIED },
1014 AVCOL_PRI_RESERVED0, AVCOL_PRI_NB - 1, FLAGS, .unit = "prm" },
1015 { "itrc", "Input transfer characteristics",
1016 OFFSET(user_itrc), AV_OPT_TYPE_INT, { .i64 = AVCOL_TRC_UNSPECIFIED },
1017 AVCOL_TRC_RESERVED0, AVCOL_TRC_NB - 1, FLAGS, .unit = "trc" },
1018
1019 { NULL }
1020 };
1021
1022 AVFILTER_DEFINE_CLASS(colorspace);
1023
1024 static const AVFilterPad inputs[] = {
1025 {
1026 .name = "default",
1027 .type = AVMEDIA_TYPE_VIDEO,
1028 .filter_frame = filter_frame,
1029 },
1030 };
1031
1032 static const AVFilterPad outputs[] = {
1033 {
1034 .name = "default",
1035 .type = AVMEDIA_TYPE_VIDEO,
1036 .config_props = config_props,
1037 },
1038 };
1039
1040 const FFFilter ff_vf_colorspace = {
1041 .p.name = "colorspace",
1042 .p.description = NULL_IF_CONFIG_SMALL("Convert between colorspaces."),
1043 .p.priv_class = &colorspace_class,
1044 .p.flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC | AVFILTER_FLAG_SLICE_THREADS,
1045 .init = init,
1046 .uninit = uninit,
1047 .priv_size = sizeof(ColorSpaceContext),
1048 FILTER_INPUTS(inputs),
1049 FILTER_OUTPUTS(outputs),
1050 FILTER_QUERY_FUNC2(query_formats),
1051 };
1052