FFmpeg coverage


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