FFmpeg coverage


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