2 * Copyright (c) 2013 Paul B Mahol
4 * This file is part of FFmpeg.
6 * FFmpeg is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * FFmpeg is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with FFmpeg; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 #include "libavutil/opt.h"
24 #include "libavutil/pixdesc.h"
26 #include "drawutils.h"
36 typedef struct ThreadData {
40 typedef struct ColorChannelMixerContext {
42 double rr, rg, rb, ra;
43 double gr, gg, gb, ga;
44 double br, bg, bb, ba;
45 double ar, ag, ab, aa;
47 int preserve_lightness;
55 int (*filter_slice[2])(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs);
56 } ColorChannelMixerContext;
58 #define OFFSET(x) offsetof(ColorChannelMixerContext, x)
59 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_RUNTIME_PARAM
61 static const AVOption colorchannelmixer_options[] = {
62 { "rr", "set the red gain for the red channel", OFFSET(rr), AV_OPT_TYPE_DOUBLE, {.dbl=1}, -2, 2, FLAGS },
63 { "rg", "set the green gain for the red channel", OFFSET(rg), AV_OPT_TYPE_DOUBLE, {.dbl=0}, -2, 2, FLAGS },
64 { "rb", "set the blue gain for the red channel", OFFSET(rb), AV_OPT_TYPE_DOUBLE, {.dbl=0}, -2, 2, FLAGS },
65 { "ra", "set the alpha gain for the red channel", OFFSET(ra), AV_OPT_TYPE_DOUBLE, {.dbl=0}, -2, 2, FLAGS },
66 { "gr", "set the red gain for the green channel", OFFSET(gr), AV_OPT_TYPE_DOUBLE, {.dbl=0}, -2, 2, FLAGS },
67 { "gg", "set the green gain for the green channel", OFFSET(gg), AV_OPT_TYPE_DOUBLE, {.dbl=1}, -2, 2, FLAGS },
68 { "gb", "set the blue gain for the green channel", OFFSET(gb), AV_OPT_TYPE_DOUBLE, {.dbl=0}, -2, 2, FLAGS },
69 { "ga", "set the alpha gain for the green channel", OFFSET(ga), AV_OPT_TYPE_DOUBLE, {.dbl=0}, -2, 2, FLAGS },
70 { "br", "set the red gain for the blue channel", OFFSET(br), AV_OPT_TYPE_DOUBLE, {.dbl=0}, -2, 2, FLAGS },
71 { "bg", "set the green gain for the blue channel", OFFSET(bg), AV_OPT_TYPE_DOUBLE, {.dbl=0}, -2, 2, FLAGS },
72 { "bb", "set the blue gain for the blue channel", OFFSET(bb), AV_OPT_TYPE_DOUBLE, {.dbl=1}, -2, 2, FLAGS },
73 { "ba", "set the alpha gain for the blue channel", OFFSET(ba), AV_OPT_TYPE_DOUBLE, {.dbl=0}, -2, 2, FLAGS },
74 { "ar", "set the red gain for the alpha channel", OFFSET(ar), AV_OPT_TYPE_DOUBLE, {.dbl=0}, -2, 2, FLAGS },
75 { "ag", "set the green gain for the alpha channel", OFFSET(ag), AV_OPT_TYPE_DOUBLE, {.dbl=0}, -2, 2, FLAGS },
76 { "ab", "set the blue gain for the alpha channel", OFFSET(ab), AV_OPT_TYPE_DOUBLE, {.dbl=0}, -2, 2, FLAGS },
77 { "aa", "set the alpha gain for the alpha channel", OFFSET(aa), AV_OPT_TYPE_DOUBLE, {.dbl=1}, -2, 2, FLAGS },
78 { "pl", "preserve lightness", OFFSET(preserve_lightness), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS },
82 AVFILTER_DEFINE_CLASS(colorchannelmixer);
84 static int query_formats(AVFilterContext *ctx)
86 static const enum AVPixelFormat pix_fmts[] = {
87 AV_PIX_FMT_RGB24, AV_PIX_FMT_BGR24,
88 AV_PIX_FMT_RGBA, AV_PIX_FMT_BGRA,
89 AV_PIX_FMT_ARGB, AV_PIX_FMT_ABGR,
90 AV_PIX_FMT_0RGB, AV_PIX_FMT_0BGR,
91 AV_PIX_FMT_RGB0, AV_PIX_FMT_BGR0,
92 AV_PIX_FMT_RGB48, AV_PIX_FMT_BGR48,
93 AV_PIX_FMT_RGBA64, AV_PIX_FMT_BGRA64,
94 AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRAP,
96 AV_PIX_FMT_GBRP10, AV_PIX_FMT_GBRAP10,
97 AV_PIX_FMT_GBRP12, AV_PIX_FMT_GBRAP12,
99 AV_PIX_FMT_GBRP16, AV_PIX_FMT_GBRAP16,
103 AVFilterFormats *fmts_list = ff_make_format_list(pix_fmts);
105 return AVERROR(ENOMEM);
106 return ff_set_common_formats(ctx, fmts_list);
109 static void preservel(float *r, float *g, float *b, float lin, float lout)
116 static av_always_inline int filter_slice_rgba_planar(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs,
117 int have_alpha, int pl)
119 ColorChannelMixerContext *s = ctx->priv;
120 ThreadData *td = arg;
121 AVFrame *in = td->in;
122 AVFrame *out = td->out;
123 const int slice_start = (out->height * jobnr) / nb_jobs;
124 const int slice_end = (out->height * (jobnr+1)) / nb_jobs;
125 const uint8_t *srcg = in->data[0] + slice_start * in->linesize[0];
126 const uint8_t *srcb = in->data[1] + slice_start * in->linesize[1];
127 const uint8_t *srcr = in->data[2] + slice_start * in->linesize[2];
128 const uint8_t *srca = in->data[3] + slice_start * in->linesize[3];
129 uint8_t *dstg = out->data[0] + slice_start * out->linesize[0];
130 uint8_t *dstb = out->data[1] + slice_start * out->linesize[1];
131 uint8_t *dstr = out->data[2] + slice_start * out->linesize[2];
132 uint8_t *dsta = out->data[3] + slice_start * out->linesize[3];
135 for (i = slice_start; i < slice_end; i++) {
136 for (j = 0; j < out->width; j++) {
137 const uint8_t rin = srcr[j];
138 const uint8_t gin = srcg[j];
139 const uint8_t bin = srcb[j];
140 const uint8_t ain = have_alpha ? srca[j] : 0;
141 int rout, gout, bout;
145 lin = FFMAX3(rin, gin, bin) / 255.f + FFMIN3(rin, gin, bin) / 255.f;
147 rout = s->lut[R][R][rin] +
150 (have_alpha == 1 ? s->lut[R][A][ain] : 0);
151 gout = s->lut[G][R][rin] +
154 (have_alpha == 1 ? s->lut[G][A][ain] : 0);
155 bout = s->lut[B][R][rin] +
158 (have_alpha == 1 ? s->lut[B][A][ain] : 0);
161 float frout = rout / (255.f * s->sr);
162 float fgout = gout / (255.f * s->sg);
163 float fbout = bout / (255.f * s->sb);
164 float lout = FFMAX3(frout, fgout, fbout) + FFMIN3(frout, fgout, fbout);
166 preservel(&frout, &fgout, &fbout, lin, lout);
168 rout = lrintf(frout * 255.f);
169 gout = lrintf(fgout * 255.f);
170 bout = lrintf(fbout * 255.f);
173 dstr[j] = av_clip_uint8(rout);
174 dstg[j] = av_clip_uint8(gout);
175 dstb[j] = av_clip_uint8(bout);
177 if (have_alpha == 1) {
178 dsta[j] = av_clip_uint8(s->lut[A][R][rin] +
185 srcg += in->linesize[0];
186 srcb += in->linesize[1];
187 srcr += in->linesize[2];
188 srca += in->linesize[3];
189 dstg += out->linesize[0];
190 dstb += out->linesize[1];
191 dstr += out->linesize[2];
192 dsta += out->linesize[3];
198 static av_always_inline int filter_slice_rgba16_planar(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs,
199 int have_alpha, int depth, int pl)
201 ColorChannelMixerContext *s = ctx->priv;
202 ThreadData *td = arg;
203 AVFrame *in = td->in;
204 AVFrame *out = td->out;
205 const int slice_start = (out->height * jobnr) / nb_jobs;
206 const int slice_end = (out->height * (jobnr+1)) / nb_jobs;
207 const uint16_t *srcg = (const uint16_t *)(in->data[0] + slice_start * in->linesize[0]);
208 const uint16_t *srcb = (const uint16_t *)(in->data[1] + slice_start * in->linesize[1]);
209 const uint16_t *srcr = (const uint16_t *)(in->data[2] + slice_start * in->linesize[2]);
210 const uint16_t *srca = (const uint16_t *)(in->data[3] + slice_start * in->linesize[3]);
211 uint16_t *dstg = (uint16_t *)(out->data[0] + slice_start * out->linesize[0]);
212 uint16_t *dstb = (uint16_t *)(out->data[1] + slice_start * out->linesize[1]);
213 uint16_t *dstr = (uint16_t *)(out->data[2] + slice_start * out->linesize[2]);
214 uint16_t *dsta = (uint16_t *)(out->data[3] + slice_start * out->linesize[3]);
215 const float scale = 1.f / ((1 << depth) - 1);
216 const float factor = (1 << depth) - 1;
219 for (i = slice_start; i < slice_end; i++) {
220 for (j = 0; j < out->width; j++) {
221 const uint16_t rin = srcr[j];
222 const uint16_t gin = srcg[j];
223 const uint16_t bin = srcb[j];
224 const uint16_t ain = have_alpha ? srca[j] : 0;
225 int rout, gout, bout;
229 lin = FFMAX3(rin, gin, bin) * scale + FFMIN3(rin, gin, bin) * scale;
231 rout = s->lut[R][R][rin] +
234 (have_alpha == 1 ? s->lut[R][A][ain] : 0);
235 gout = s->lut[G][R][rin] +
238 (have_alpha == 1 ? s->lut[G][A][ain] : 0);
239 bout = s->lut[B][R][rin] +
242 (have_alpha == 1 ? s->lut[B][A][ain] : 0);
245 float frout = rout / (factor * s->sr);
246 float fgout = gout / (factor * s->sg);
247 float fbout = bout / (factor * s->sb);
248 float lout = FFMAX3(frout, fgout, fbout) + FFMIN3(frout, fgout, fbout);
250 preservel(&frout, &fgout, &fbout, lin, lout);
252 rout = lrintf(frout * factor);
253 gout = lrintf(fgout * factor);
254 bout = lrintf(fbout * factor);
257 dstr[j] = av_clip_uintp2(rout, depth);
258 dstg[j] = av_clip_uintp2(gout, depth);
259 dstb[j] = av_clip_uintp2(bout, depth);
261 if (have_alpha == 1) {
262 dsta[j] = av_clip_uintp2(s->lut[A][R][rin] +
265 s->lut[A][A][ain], depth);
269 srcg += in->linesize[0] / 2;
270 srcb += in->linesize[1] / 2;
271 srcr += in->linesize[2] / 2;
272 srca += in->linesize[3] / 2;
273 dstg += out->linesize[0] / 2;
274 dstb += out->linesize[1] / 2;
275 dstr += out->linesize[2] / 2;
276 dsta += out->linesize[3] / 2;
282 static int filter_slice_gbrp(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
284 return filter_slice_rgba_planar(ctx, arg, jobnr, nb_jobs, 0, 0);
287 static int filter_slice_gbrap(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
289 return filter_slice_rgba_planar(ctx, arg, jobnr, nb_jobs, 1, 0);
292 static int filter_slice_gbrp_pl(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
294 return filter_slice_rgba_planar(ctx, arg, jobnr, nb_jobs, 0, 1);
297 static int filter_slice_gbrap_pl(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
299 return filter_slice_rgba_planar(ctx, arg, jobnr, nb_jobs, 1, 1);
302 static int filter_slice_gbrp9(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
304 return filter_slice_rgba16_planar(ctx, arg, jobnr, nb_jobs, 0, 9, 0);
307 static int filter_slice_gbrp10(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
309 return filter_slice_rgba16_planar(ctx, arg, jobnr, nb_jobs, 0, 10, 0);
312 static int filter_slice_gbrap10(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
314 return filter_slice_rgba16_planar(ctx, arg, jobnr, nb_jobs, 1, 10, 0);
317 static int filter_slice_gbrp12(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
319 return filter_slice_rgba16_planar(ctx, arg, jobnr, nb_jobs, 0, 12, 0);
322 static int filter_slice_gbrap12(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
324 return filter_slice_rgba16_planar(ctx, arg, jobnr, nb_jobs, 1, 12, 0);
327 static int filter_slice_gbrp14(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
329 return filter_slice_rgba16_planar(ctx, arg, jobnr, nb_jobs, 0, 14, 0);
332 static int filter_slice_gbrp16(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
334 return filter_slice_rgba16_planar(ctx, arg, jobnr, nb_jobs, 0, 16, 0);
337 static int filter_slice_gbrap16(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
339 return filter_slice_rgba16_planar(ctx, arg, jobnr, nb_jobs, 1, 16, 0);
342 static int filter_slice_gbrp9_pl(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
344 return filter_slice_rgba16_planar(ctx, arg, jobnr, nb_jobs, 0, 9, 1);
347 static int filter_slice_gbrp10_pl(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
349 return filter_slice_rgba16_planar(ctx, arg, jobnr, nb_jobs, 0, 10, 1);
352 static int filter_slice_gbrap10_pl(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
354 return filter_slice_rgba16_planar(ctx, arg, jobnr, nb_jobs, 1, 10, 1);
357 static int filter_slice_gbrp12_pl(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
359 return filter_slice_rgba16_planar(ctx, arg, jobnr, nb_jobs, 0, 12, 1);
362 static int filter_slice_gbrap12_pl(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
364 return filter_slice_rgba16_planar(ctx, arg, jobnr, nb_jobs, 1, 12, 1);
367 static int filter_slice_gbrp14_pl(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
369 return filter_slice_rgba16_planar(ctx, arg, jobnr, nb_jobs, 0, 14, 1);
372 static int filter_slice_gbrp16_pl(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
374 return filter_slice_rgba16_planar(ctx, arg, jobnr, nb_jobs, 0, 16, 1);
377 static int filter_slice_gbrap16_pl(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
379 return filter_slice_rgba16_planar(ctx, arg, jobnr, nb_jobs, 1, 16, 1);
382 static av_always_inline int filter_slice_rgba_packed(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs,
383 int have_alpha, int step, int pl)
385 ColorChannelMixerContext *s = ctx->priv;
386 ThreadData *td = arg;
387 AVFrame *in = td->in;
388 AVFrame *out = td->out;
389 const int slice_start = (out->height * jobnr) / nb_jobs;
390 const int slice_end = (out->height * (jobnr+1)) / nb_jobs;
391 const uint8_t roffset = s->rgba_map[R];
392 const uint8_t goffset = s->rgba_map[G];
393 const uint8_t boffset = s->rgba_map[B];
394 const uint8_t aoffset = s->rgba_map[A];
395 const uint8_t *srcrow = in->data[0] + slice_start * in->linesize[0];
396 uint8_t *dstrow = out->data[0] + slice_start * out->linesize[0];
399 for (i = slice_start; i < slice_end; i++) {
400 const uint8_t *src = srcrow;
401 uint8_t *dst = dstrow;
403 for (j = 0; j < out->width * step; j += step) {
404 const uint8_t rin = src[j + roffset];
405 const uint8_t gin = src[j + goffset];
406 const uint8_t bin = src[j + boffset];
407 const uint8_t ain = src[j + aoffset];
408 int rout, gout, bout;
412 lin = FFMAX3(rin, gin, bin) / 255.f + FFMIN3(rin, gin, bin) / 255.f;
414 rout = s->lut[R][R][rin] +
417 (have_alpha == 1 ? s->lut[R][A][ain] : 0);
418 gout = s->lut[G][R][rin] +
421 (have_alpha == 1 ? s->lut[G][A][ain] : 0);
422 bout = s->lut[B][R][rin] +
425 (have_alpha == 1 ? s->lut[B][A][ain] : 0);
428 float frout = rout / (255.f * s->sr);
429 float fgout = gout / (255.f * s->sg);
430 float fbout = bout / (255.f * s->sb);
431 float lout = FFMAX3(frout, fgout, fbout) + FFMIN3(frout, fgout, fbout);
433 preservel(&frout, &fgout, &fbout, lin, lout);
435 rout = lrintf(frout * 255.f);
436 gout = lrintf(fgout * 255.f);
437 bout = lrintf(fbout * 255.f);
440 dst[j + roffset] = av_clip_uint8(rout);
441 dst[j + goffset] = av_clip_uint8(gout);
442 dst[j + boffset] = av_clip_uint8(bout);
444 if (have_alpha == 1) {
445 dst[j + aoffset] = av_clip_uint8(s->lut[A][R][rin] +
449 } else if (have_alpha == -1 && in != out)
450 dst[j + aoffset] = 0;
453 srcrow += in->linesize[0];
454 dstrow += out->linesize[0];
460 static av_always_inline int filter_slice_rgba16_packed(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs,
461 int have_alpha, int step, int pl)
463 ColorChannelMixerContext *s = ctx->priv;
464 ThreadData *td = arg;
465 AVFrame *in = td->in;
466 AVFrame *out = td->out;
467 const int slice_start = (out->height * jobnr) / nb_jobs;
468 const int slice_end = (out->height * (jobnr+1)) / nb_jobs;
469 const uint8_t roffset = s->rgba_map[R];
470 const uint8_t goffset = s->rgba_map[G];
471 const uint8_t boffset = s->rgba_map[B];
472 const uint8_t aoffset = s->rgba_map[A];
473 const uint8_t *srcrow = in->data[0] + slice_start * in->linesize[0];
474 uint8_t *dstrow = out->data[0] + slice_start * out->linesize[0];
477 for (i = slice_start; i < slice_end; i++) {
478 const uint16_t *src = (const uint16_t *)srcrow;
479 uint16_t *dst = (uint16_t *)dstrow;
481 for (j = 0; j < out->width * step; j += step) {
482 const uint16_t rin = src[j + roffset];
483 const uint16_t gin = src[j + goffset];
484 const uint16_t bin = src[j + boffset];
485 const uint16_t ain = src[j + aoffset];
486 int rout, gout, bout;
490 lin = FFMAX3(rin, gin, bin) / 65535.f + FFMIN3(rin, gin, bin) / 65535.f;
492 rout = s->lut[R][R][rin] +
495 (have_alpha == 1 ? s->lut[R][A][ain] : 0);
496 gout = s->lut[G][R][rin] +
499 (have_alpha == 1 ? s->lut[G][A][ain] : 0);
500 bout = s->lut[B][R][rin] +
503 (have_alpha == 1 ? s->lut[B][A][ain] : 0);
506 float frout = rout / (65535.f * s->sr);
507 float fgout = gout / (65535.f * s->sg);
508 float fbout = bout / (65535.f * s->sb);
509 float lout = FFMAX3(frout, fgout, fbout) + FFMIN3(frout, fgout, fbout);
511 preservel(&frout, &fgout, &fbout, lin, lout);
513 rout = lrintf(frout * 65535.f);
514 gout = lrintf(fgout * 65535.f);
515 bout = lrintf(fbout * 65535.f);
518 dst[j + roffset] = av_clip_uint16(rout);
519 dst[j + goffset] = av_clip_uint16(gout);
520 dst[j + boffset] = av_clip_uint16(bout);
522 if (have_alpha == 1) {
523 dst[j + aoffset] = av_clip_uint16(s->lut[A][R][rin] +
530 srcrow += in->linesize[0];
531 dstrow += out->linesize[0];
537 static int filter_slice_rgba64(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
539 return filter_slice_rgba16_packed(ctx, arg, jobnr, nb_jobs, 1, 4, 0);
542 static int filter_slice_rgb48(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
544 return filter_slice_rgba16_packed(ctx, arg, jobnr, nb_jobs, 0, 3, 0);
547 static int filter_slice_rgba64_pl(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
549 return filter_slice_rgba16_packed(ctx, arg, jobnr, nb_jobs, 1, 4, 1);
552 static int filter_slice_rgb48_pl(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
554 return filter_slice_rgba16_packed(ctx, arg, jobnr, nb_jobs, 0, 3, 1);
557 static int filter_slice_rgba(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
559 return filter_slice_rgba_packed(ctx, arg, jobnr, nb_jobs, 1, 4, 0);
562 static int filter_slice_rgb24(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
564 return filter_slice_rgba_packed(ctx, arg, jobnr, nb_jobs, 0, 3, 0);
567 static int filter_slice_rgb0(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
569 return filter_slice_rgba_packed(ctx, arg, jobnr, nb_jobs, -1, 4, 0);
572 static int filter_slice_rgba_pl(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
574 return filter_slice_rgba_packed(ctx, arg, jobnr, nb_jobs, 1, 4, 1);
577 static int filter_slice_rgb24_pl(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
579 return filter_slice_rgba_packed(ctx, arg, jobnr, nb_jobs, 0, 3, 1);
582 static int filter_slice_rgb0_pl(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
584 return filter_slice_rgba_packed(ctx, arg, jobnr, nb_jobs, -1, 4, 1);
587 static int config_output(AVFilterLink *outlink)
589 AVFilterContext *ctx = outlink->src;
590 ColorChannelMixerContext *s = ctx->priv;
591 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(outlink->format);
592 const int depth = desc->comp[0].depth;
593 int i, j, size, *buffer = s->buffer;
595 ff_fill_rgba_map(s->rgba_map, outlink->format);
599 s->buffer = buffer = av_malloc(16 * size * sizeof(*s->buffer));
601 return AVERROR(ENOMEM);
603 for (i = 0; i < 4; i++)
604 for (j = 0; j < 4; j++, buffer += size)
605 s->lut[i][j] = buffer;
608 s->sr = s->rr + s->rg + s->rb + s->ra;
609 s->sg = s->gr + s->gg + s->gb + s->ga;
610 s->sb = s->br + s->bg + s->bb + s->ba;
612 if (fabs(s->sr) <= DBL_EPSILON)
615 if (fabs(s->sg) <= DBL_EPSILON)
618 if (fabs(s->sb) <= DBL_EPSILON)
621 for (i = 0; i < size; i++) {
622 s->lut[R][R][i] = lrint(i * s->rr);
623 s->lut[R][G][i] = lrint(i * s->rg);
624 s->lut[R][B][i] = lrint(i * s->rb);
625 s->lut[R][A][i] = lrint(i * s->ra);
627 s->lut[G][R][i] = lrint(i * s->gr);
628 s->lut[G][G][i] = lrint(i * s->gg);
629 s->lut[G][B][i] = lrint(i * s->gb);
630 s->lut[G][A][i] = lrint(i * s->ga);
632 s->lut[B][R][i] = lrint(i * s->br);
633 s->lut[B][G][i] = lrint(i * s->bg);
634 s->lut[B][B][i] = lrint(i * s->bb);
635 s->lut[B][A][i] = lrint(i * s->ba);
637 s->lut[A][R][i] = lrint(i * s->ar);
638 s->lut[A][G][i] = lrint(i * s->ag);
639 s->lut[A][B][i] = lrint(i * s->ab);
640 s->lut[A][A][i] = lrint(i * s->aa);
643 switch (outlink->format) {
644 case AV_PIX_FMT_BGR24:
645 case AV_PIX_FMT_RGB24:
646 s->filter_slice[0] = filter_slice_rgb24;
647 s->filter_slice[1] = filter_slice_rgb24_pl;
649 case AV_PIX_FMT_0BGR:
650 case AV_PIX_FMT_0RGB:
651 case AV_PIX_FMT_BGR0:
652 case AV_PIX_FMT_RGB0:
653 s->filter_slice[0] = filter_slice_rgb0;
654 s->filter_slice[1] = filter_slice_rgb0_pl;
656 case AV_PIX_FMT_ABGR:
657 case AV_PIX_FMT_ARGB:
658 case AV_PIX_FMT_BGRA:
659 case AV_PIX_FMT_RGBA:
660 s->filter_slice[0] = filter_slice_rgba;
661 s->filter_slice[1] = filter_slice_rgba_pl;
663 case AV_PIX_FMT_BGR48:
664 case AV_PIX_FMT_RGB48:
665 s->filter_slice[0] = filter_slice_rgb48;
666 s->filter_slice[1] = filter_slice_rgb48_pl;
668 case AV_PIX_FMT_BGRA64:
669 case AV_PIX_FMT_RGBA64:
670 s->filter_slice[0] = filter_slice_rgba64;
671 s->filter_slice[1] = filter_slice_rgba64_pl;
673 case AV_PIX_FMT_GBRP:
674 s->filter_slice[0] = filter_slice_gbrp;
675 s->filter_slice[1] = filter_slice_gbrp_pl;
677 case AV_PIX_FMT_GBRAP:
678 s->filter_slice[0] = filter_slice_gbrap;
679 s->filter_slice[1] = filter_slice_gbrap_pl;
681 case AV_PIX_FMT_GBRP9:
682 s->filter_slice[0] = filter_slice_gbrp9;
683 s->filter_slice[1] = filter_slice_gbrp9_pl;
685 case AV_PIX_FMT_GBRP10:
686 s->filter_slice[0] = filter_slice_gbrp10;
687 s->filter_slice[1] = filter_slice_gbrp10_pl;
689 case AV_PIX_FMT_GBRAP10:
690 s->filter_slice[0] = filter_slice_gbrap10;
691 s->filter_slice[1] = filter_slice_gbrap10_pl;
693 case AV_PIX_FMT_GBRP12:
694 s->filter_slice[0] = filter_slice_gbrp12;
695 s->filter_slice[1] = filter_slice_gbrp12_pl;
697 case AV_PIX_FMT_GBRAP12:
698 s->filter_slice[0] = filter_slice_gbrap12;
699 s->filter_slice[1] = filter_slice_gbrap12_pl;
701 case AV_PIX_FMT_GBRP14:
702 s->filter_slice[0] = filter_slice_gbrp14;
703 s->filter_slice[1] = filter_slice_gbrp14_pl;
705 case AV_PIX_FMT_GBRP16:
706 s->filter_slice[0] = filter_slice_gbrp16;
707 s->filter_slice[1] = filter_slice_gbrp16_pl;
709 case AV_PIX_FMT_GBRAP16:
710 s->filter_slice[0] = filter_slice_gbrap16;
711 s->filter_slice[1] = filter_slice_gbrap16_pl;
718 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
720 AVFilterContext *ctx = inlink->dst;
721 ColorChannelMixerContext *s = ctx->priv;
722 AVFilterLink *outlink = ctx->outputs[0];
723 const int pl = s->preserve_lightness;
727 if (av_frame_is_writable(in)) {
730 out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
733 return AVERROR(ENOMEM);
735 av_frame_copy_props(out, in);
740 ctx->internal->execute(ctx, s->filter_slice[pl], &td, NULL, FFMIN(outlink->h, ff_filter_get_nb_threads(ctx)));
744 return ff_filter_frame(outlink, out);
747 static int process_command(AVFilterContext *ctx, const char *cmd, const char *args,
748 char *res, int res_len, int flags)
750 int ret = ff_filter_process_command(ctx, cmd, args, res, res_len, flags);
755 return config_output(ctx->outputs[0]);
758 static av_cold void uninit(AVFilterContext *ctx)
760 ColorChannelMixerContext *s = ctx->priv;
762 av_freep(&s->buffer);
765 static const AVFilterPad colorchannelmixer_inputs[] = {
768 .type = AVMEDIA_TYPE_VIDEO,
769 .filter_frame = filter_frame,
774 static const AVFilterPad colorchannelmixer_outputs[] = {
777 .type = AVMEDIA_TYPE_VIDEO,
778 .config_props = config_output,
783 AVFilter ff_vf_colorchannelmixer = {
784 .name = "colorchannelmixer",
785 .description = NULL_IF_CONFIG_SMALL("Adjust colors by mixing color channels."),
786 .priv_size = sizeof(ColorChannelMixerContext),
787 .priv_class = &colorchannelmixer_class,
789 .query_formats = query_formats,
790 .inputs = colorchannelmixer_inputs,
791 .outputs = colorchannelmixer_outputs,
792 .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC | AVFILTER_FLAG_SLICE_THREADS,
793 .process_command = process_command,