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 double 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_DOUBLE, {.dbl=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 float lerpf(float v0, float v1, float f)
111 return v0 + (v1 - v0) * f;
114 static void preservel(float *r, float *g, float *b, float lin, float lout)
121 static av_always_inline int filter_slice_rgba_planar(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs,
122 int have_alpha, int pl)
124 ColorChannelMixerContext *s = ctx->priv;
125 ThreadData *td = arg;
126 AVFrame *in = td->in;
127 AVFrame *out = td->out;
128 const float l = s->preserve_lightness;
129 const float sr = s->sr;
130 const float sg = s->sg;
131 const float sb = s->sb;
132 const int slice_start = (out->height * jobnr) / nb_jobs;
133 const int slice_end = (out->height * (jobnr+1)) / nb_jobs;
134 const uint8_t *srcg = in->data[0] + slice_start * in->linesize[0];
135 const uint8_t *srcb = in->data[1] + slice_start * in->linesize[1];
136 const uint8_t *srcr = in->data[2] + slice_start * in->linesize[2];
137 const uint8_t *srca = in->data[3] + slice_start * in->linesize[3];
138 uint8_t *dstg = out->data[0] + slice_start * out->linesize[0];
139 uint8_t *dstb = out->data[1] + slice_start * out->linesize[1];
140 uint8_t *dstr = out->data[2] + slice_start * out->linesize[2];
141 uint8_t *dsta = out->data[3] + slice_start * out->linesize[3];
144 for (i = slice_start; i < slice_end; i++) {
145 for (j = 0; j < out->width; j++) {
146 const uint8_t rin = srcr[j];
147 const uint8_t gin = srcg[j];
148 const uint8_t bin = srcb[j];
149 const uint8_t ain = have_alpha ? srca[j] : 0;
150 int rout, gout, bout;
154 lin = FFMAX3(rin, gin, bin) + FFMIN3(rin, gin, bin);
156 rout = s->lut[R][R][rin] +
159 (have_alpha == 1 ? s->lut[R][A][ain] : 0);
160 gout = s->lut[G][R][rin] +
163 (have_alpha == 1 ? s->lut[G][A][ain] : 0);
164 bout = s->lut[B][R][rin] +
167 (have_alpha == 1 ? s->lut[B][A][ain] : 0);
170 float frout = rout / sr;
171 float fgout = gout / sg;
172 float fbout = bout / sb;
173 float lout = FFMAX3(frout, fgout, fbout) + FFMIN3(frout, fgout, fbout);
175 preservel(&frout, &fgout, &fbout, lin, lout);
177 rout = lrintf(lerpf(rout, frout, l));
178 gout = lrintf(lerpf(gout, fgout, l));
179 bout = lrintf(lerpf(bout, fbout, l));
182 dstr[j] = av_clip_uint8(rout);
183 dstg[j] = av_clip_uint8(gout);
184 dstb[j] = av_clip_uint8(bout);
186 if (have_alpha == 1) {
187 dsta[j] = av_clip_uint8(s->lut[A][R][rin] +
194 srcg += in->linesize[0];
195 srcb += in->linesize[1];
196 srcr += in->linesize[2];
197 srca += in->linesize[3];
198 dstg += out->linesize[0];
199 dstb += out->linesize[1];
200 dstr += out->linesize[2];
201 dsta += out->linesize[3];
207 static av_always_inline int filter_slice_rgba16_planar(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs,
208 int have_alpha, int depth, int pl)
210 ColorChannelMixerContext *s = ctx->priv;
211 ThreadData *td = arg;
212 AVFrame *in = td->in;
213 AVFrame *out = td->out;
214 const float l = s->preserve_lightness;
215 const float sr = s->sr;
216 const float sg = s->sg;
217 const float sb = s->sb;
218 const int slice_start = (out->height * jobnr) / nb_jobs;
219 const int slice_end = (out->height * (jobnr+1)) / nb_jobs;
220 const uint16_t *srcg = (const uint16_t *)(in->data[0] + slice_start * in->linesize[0]);
221 const uint16_t *srcb = (const uint16_t *)(in->data[1] + slice_start * in->linesize[1]);
222 const uint16_t *srcr = (const uint16_t *)(in->data[2] + slice_start * in->linesize[2]);
223 const uint16_t *srca = (const uint16_t *)(in->data[3] + slice_start * in->linesize[3]);
224 uint16_t *dstg = (uint16_t *)(out->data[0] + slice_start * out->linesize[0]);
225 uint16_t *dstb = (uint16_t *)(out->data[1] + slice_start * out->linesize[1]);
226 uint16_t *dstr = (uint16_t *)(out->data[2] + slice_start * out->linesize[2]);
227 uint16_t *dsta = (uint16_t *)(out->data[3] + slice_start * out->linesize[3]);
230 for (i = slice_start; i < slice_end; i++) {
231 for (j = 0; j < out->width; j++) {
232 const uint16_t rin = srcr[j];
233 const uint16_t gin = srcg[j];
234 const uint16_t bin = srcb[j];
235 const uint16_t ain = have_alpha ? srca[j] : 0;
236 int rout, gout, bout;
240 lin = FFMAX3(rin, gin, bin) + FFMIN3(rin, gin, bin);
242 rout = s->lut[R][R][rin] +
245 (have_alpha == 1 ? s->lut[R][A][ain] : 0);
246 gout = s->lut[G][R][rin] +
249 (have_alpha == 1 ? s->lut[G][A][ain] : 0);
250 bout = s->lut[B][R][rin] +
253 (have_alpha == 1 ? s->lut[B][A][ain] : 0);
256 float frout = rout / sr;
257 float fgout = gout / sg;
258 float fbout = bout / sb;
259 float lout = FFMAX3(frout, fgout, fbout) + FFMIN3(frout, fgout, fbout);
261 preservel(&frout, &fgout, &fbout, lin, lout);
263 rout = lrintf(lerpf(rout, frout, l));
264 gout = lrintf(lerpf(gout, fgout, l));
265 bout = lrintf(lerpf(bout, fbout, l));
268 dstr[j] = av_clip_uintp2(rout, depth);
269 dstg[j] = av_clip_uintp2(gout, depth);
270 dstb[j] = av_clip_uintp2(bout, depth);
272 if (have_alpha == 1) {
273 dsta[j] = av_clip_uintp2(s->lut[A][R][rin] +
276 s->lut[A][A][ain], depth);
280 srcg += in->linesize[0] / 2;
281 srcb += in->linesize[1] / 2;
282 srcr += in->linesize[2] / 2;
283 srca += in->linesize[3] / 2;
284 dstg += out->linesize[0] / 2;
285 dstb += out->linesize[1] / 2;
286 dstr += out->linesize[2] / 2;
287 dsta += out->linesize[3] / 2;
293 static int filter_slice_gbrp(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
295 return filter_slice_rgba_planar(ctx, arg, jobnr, nb_jobs, 0, 0);
298 static int filter_slice_gbrap(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
300 return filter_slice_rgba_planar(ctx, arg, jobnr, nb_jobs, 1, 0);
303 static int filter_slice_gbrp_pl(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
305 return filter_slice_rgba_planar(ctx, arg, jobnr, nb_jobs, 0, 1);
308 static int filter_slice_gbrap_pl(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
310 return filter_slice_rgba_planar(ctx, arg, jobnr, nb_jobs, 1, 1);
313 static int filter_slice_gbrp9(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
315 return filter_slice_rgba16_planar(ctx, arg, jobnr, nb_jobs, 0, 9, 0);
318 static int filter_slice_gbrp10(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
320 return filter_slice_rgba16_planar(ctx, arg, jobnr, nb_jobs, 0, 10, 0);
323 static int filter_slice_gbrap10(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
325 return filter_slice_rgba16_planar(ctx, arg, jobnr, nb_jobs, 1, 10, 0);
328 static int filter_slice_gbrp12(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
330 return filter_slice_rgba16_planar(ctx, arg, jobnr, nb_jobs, 0, 12, 0);
333 static int filter_slice_gbrap12(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
335 return filter_slice_rgba16_planar(ctx, arg, jobnr, nb_jobs, 1, 12, 0);
338 static int filter_slice_gbrp14(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
340 return filter_slice_rgba16_planar(ctx, arg, jobnr, nb_jobs, 0, 14, 0);
343 static int filter_slice_gbrp16(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
345 return filter_slice_rgba16_planar(ctx, arg, jobnr, nb_jobs, 0, 16, 0);
348 static int filter_slice_gbrap16(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
350 return filter_slice_rgba16_planar(ctx, arg, jobnr, nb_jobs, 1, 16, 0);
353 static int filter_slice_gbrp9_pl(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
355 return filter_slice_rgba16_planar(ctx, arg, jobnr, nb_jobs, 0, 9, 1);
358 static int filter_slice_gbrp10_pl(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
360 return filter_slice_rgba16_planar(ctx, arg, jobnr, nb_jobs, 0, 10, 1);
363 static int filter_slice_gbrap10_pl(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
365 return filter_slice_rgba16_planar(ctx, arg, jobnr, nb_jobs, 1, 10, 1);
368 static int filter_slice_gbrp12_pl(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
370 return filter_slice_rgba16_planar(ctx, arg, jobnr, nb_jobs, 0, 12, 1);
373 static int filter_slice_gbrap12_pl(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
375 return filter_slice_rgba16_planar(ctx, arg, jobnr, nb_jobs, 1, 12, 1);
378 static int filter_slice_gbrp14_pl(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
380 return filter_slice_rgba16_planar(ctx, arg, jobnr, nb_jobs, 0, 14, 1);
383 static int filter_slice_gbrp16_pl(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
385 return filter_slice_rgba16_planar(ctx, arg, jobnr, nb_jobs, 0, 16, 1);
388 static int filter_slice_gbrap16_pl(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
390 return filter_slice_rgba16_planar(ctx, arg, jobnr, nb_jobs, 1, 16, 1);
393 static av_always_inline int filter_slice_rgba_packed(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs,
394 int have_alpha, int step, int pl)
396 ColorChannelMixerContext *s = ctx->priv;
397 ThreadData *td = arg;
398 AVFrame *in = td->in;
399 AVFrame *out = td->out;
400 const float l = s->preserve_lightness;
401 const float sr = s->sr;
402 const float sg = s->sg;
403 const float sb = s->sb;
404 const int slice_start = (out->height * jobnr) / nb_jobs;
405 const int slice_end = (out->height * (jobnr+1)) / nb_jobs;
406 const uint8_t roffset = s->rgba_map[R];
407 const uint8_t goffset = s->rgba_map[G];
408 const uint8_t boffset = s->rgba_map[B];
409 const uint8_t aoffset = s->rgba_map[A];
410 const uint8_t *srcrow = in->data[0] + slice_start * in->linesize[0];
411 uint8_t *dstrow = out->data[0] + slice_start * out->linesize[0];
414 for (i = slice_start; i < slice_end; i++) {
415 const uint8_t *src = srcrow;
416 uint8_t *dst = dstrow;
418 for (j = 0; j < out->width * step; j += step) {
419 const uint8_t rin = src[j + roffset];
420 const uint8_t gin = src[j + goffset];
421 const uint8_t bin = src[j + boffset];
422 const uint8_t ain = src[j + aoffset];
423 int rout, gout, bout;
427 lin = FFMAX3(rin, gin, bin) + FFMIN3(rin, gin, bin);
429 rout = s->lut[R][R][rin] +
432 (have_alpha == 1 ? s->lut[R][A][ain] : 0);
433 gout = s->lut[G][R][rin] +
436 (have_alpha == 1 ? s->lut[G][A][ain] : 0);
437 bout = s->lut[B][R][rin] +
440 (have_alpha == 1 ? s->lut[B][A][ain] : 0);
443 float frout = rout / sr;
444 float fgout = gout / sg;
445 float fbout = bout / sb;
446 float lout = FFMAX3(frout, fgout, fbout) + FFMIN3(frout, fgout, fbout);
448 preservel(&frout, &fgout, &fbout, lin, lout);
450 rout = lrintf(lerpf(rout, frout, l));
451 gout = lrintf(lerpf(gout, fgout, l));
452 bout = lrintf(lerpf(bout, fbout, l));
455 dst[j + roffset] = av_clip_uint8(rout);
456 dst[j + goffset] = av_clip_uint8(gout);
457 dst[j + boffset] = av_clip_uint8(bout);
459 if (have_alpha == 1) {
460 dst[j + aoffset] = av_clip_uint8(s->lut[A][R][rin] +
464 } else if (have_alpha == -1 && in != out)
465 dst[j + aoffset] = 0;
468 srcrow += in->linesize[0];
469 dstrow += out->linesize[0];
475 static av_always_inline int filter_slice_rgba16_packed(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs,
476 int have_alpha, int step, int pl)
478 ColorChannelMixerContext *s = ctx->priv;
479 ThreadData *td = arg;
480 AVFrame *in = td->in;
481 AVFrame *out = td->out;
482 const float l = s->preserve_lightness;
483 const float sr = s->sr;
484 const float sg = s->sg;
485 const float sb = s->sb;
486 const int slice_start = (out->height * jobnr) / nb_jobs;
487 const int slice_end = (out->height * (jobnr+1)) / nb_jobs;
488 const uint8_t roffset = s->rgba_map[R];
489 const uint8_t goffset = s->rgba_map[G];
490 const uint8_t boffset = s->rgba_map[B];
491 const uint8_t aoffset = s->rgba_map[A];
492 const uint8_t *srcrow = in->data[0] + slice_start * in->linesize[0];
493 uint8_t *dstrow = out->data[0] + slice_start * out->linesize[0];
496 for (i = slice_start; i < slice_end; i++) {
497 const uint16_t *src = (const uint16_t *)srcrow;
498 uint16_t *dst = (uint16_t *)dstrow;
500 for (j = 0; j < out->width * step; j += step) {
501 const uint16_t rin = src[j + roffset];
502 const uint16_t gin = src[j + goffset];
503 const uint16_t bin = src[j + boffset];
504 const uint16_t ain = src[j + aoffset];
505 int rout, gout, bout;
509 lin = FFMAX3(rin, gin, bin) + FFMIN3(rin, gin, bin);
511 rout = s->lut[R][R][rin] +
514 (have_alpha == 1 ? s->lut[R][A][ain] : 0);
515 gout = s->lut[G][R][rin] +
518 (have_alpha == 1 ? s->lut[G][A][ain] : 0);
519 bout = s->lut[B][R][rin] +
522 (have_alpha == 1 ? s->lut[B][A][ain] : 0);
525 float frout = rout / sr;
526 float fgout = gout / sg;
527 float fbout = bout / sb;
528 float lout = FFMAX3(frout, fgout, fbout) + FFMIN3(frout, fgout, fbout);
530 preservel(&frout, &fgout, &fbout, lin, lout);
532 rout = lrintf(lerpf(rout, frout, l));
533 gout = lrintf(lerpf(gout, fgout, l));
534 bout = lrintf(lerpf(bout, fbout, l));
537 dst[j + roffset] = av_clip_uint16(rout);
538 dst[j + goffset] = av_clip_uint16(gout);
539 dst[j + boffset] = av_clip_uint16(bout);
541 if (have_alpha == 1) {
542 dst[j + aoffset] = av_clip_uint16(s->lut[A][R][rin] +
549 srcrow += in->linesize[0];
550 dstrow += out->linesize[0];
556 static int filter_slice_rgba64(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
558 return filter_slice_rgba16_packed(ctx, arg, jobnr, nb_jobs, 1, 4, 0);
561 static int filter_slice_rgb48(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
563 return filter_slice_rgba16_packed(ctx, arg, jobnr, nb_jobs, 0, 3, 0);
566 static int filter_slice_rgba64_pl(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
568 return filter_slice_rgba16_packed(ctx, arg, jobnr, nb_jobs, 1, 4, 1);
571 static int filter_slice_rgb48_pl(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
573 return filter_slice_rgba16_packed(ctx, arg, jobnr, nb_jobs, 0, 3, 1);
576 static int filter_slice_rgba(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
578 return filter_slice_rgba_packed(ctx, arg, jobnr, nb_jobs, 1, 4, 0);
581 static int filter_slice_rgb24(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
583 return filter_slice_rgba_packed(ctx, arg, jobnr, nb_jobs, 0, 3, 0);
586 static int filter_slice_rgb0(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
588 return filter_slice_rgba_packed(ctx, arg, jobnr, nb_jobs, -1, 4, 0);
591 static int filter_slice_rgba_pl(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
593 return filter_slice_rgba_packed(ctx, arg, jobnr, nb_jobs, 1, 4, 1);
596 static int filter_slice_rgb24_pl(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
598 return filter_slice_rgba_packed(ctx, arg, jobnr, nb_jobs, 0, 3, 1);
601 static int filter_slice_rgb0_pl(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
603 return filter_slice_rgba_packed(ctx, arg, jobnr, nb_jobs, -1, 4, 1);
606 static int config_output(AVFilterLink *outlink)
608 AVFilterContext *ctx = outlink->src;
609 ColorChannelMixerContext *s = ctx->priv;
610 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(outlink->format);
611 const int depth = desc->comp[0].depth;
612 int i, j, size, *buffer = s->buffer;
614 ff_fill_rgba_map(s->rgba_map, outlink->format);
618 s->buffer = buffer = av_malloc(16 * size * sizeof(*s->buffer));
620 return AVERROR(ENOMEM);
622 for (i = 0; i < 4; i++)
623 for (j = 0; j < 4; j++, buffer += size)
624 s->lut[i][j] = buffer;
627 s->sr = s->rr + s->rg + s->rb + s->ra;
628 s->sg = s->gr + s->gg + s->gb + s->ga;
629 s->sb = s->br + s->bg + s->bb + s->ba;
631 if (fabs(s->sr) <= DBL_EPSILON)
634 if (fabs(s->sg) <= DBL_EPSILON)
637 if (fabs(s->sb) <= DBL_EPSILON)
640 for (i = 0; i < size; i++) {
641 s->lut[R][R][i] = lrint(i * s->rr);
642 s->lut[R][G][i] = lrint(i * s->rg);
643 s->lut[R][B][i] = lrint(i * s->rb);
644 s->lut[R][A][i] = lrint(i * s->ra);
646 s->lut[G][R][i] = lrint(i * s->gr);
647 s->lut[G][G][i] = lrint(i * s->gg);
648 s->lut[G][B][i] = lrint(i * s->gb);
649 s->lut[G][A][i] = lrint(i * s->ga);
651 s->lut[B][R][i] = lrint(i * s->br);
652 s->lut[B][G][i] = lrint(i * s->bg);
653 s->lut[B][B][i] = lrint(i * s->bb);
654 s->lut[B][A][i] = lrint(i * s->ba);
656 s->lut[A][R][i] = lrint(i * s->ar);
657 s->lut[A][G][i] = lrint(i * s->ag);
658 s->lut[A][B][i] = lrint(i * s->ab);
659 s->lut[A][A][i] = lrint(i * s->aa);
662 switch (outlink->format) {
663 case AV_PIX_FMT_BGR24:
664 case AV_PIX_FMT_RGB24:
665 s->filter_slice[0] = filter_slice_rgb24;
666 s->filter_slice[1] = filter_slice_rgb24_pl;
668 case AV_PIX_FMT_0BGR:
669 case AV_PIX_FMT_0RGB:
670 case AV_PIX_FMT_BGR0:
671 case AV_PIX_FMT_RGB0:
672 s->filter_slice[0] = filter_slice_rgb0;
673 s->filter_slice[1] = filter_slice_rgb0_pl;
675 case AV_PIX_FMT_ABGR:
676 case AV_PIX_FMT_ARGB:
677 case AV_PIX_FMT_BGRA:
678 case AV_PIX_FMT_RGBA:
679 s->filter_slice[0] = filter_slice_rgba;
680 s->filter_slice[1] = filter_slice_rgba_pl;
682 case AV_PIX_FMT_BGR48:
683 case AV_PIX_FMT_RGB48:
684 s->filter_slice[0] = filter_slice_rgb48;
685 s->filter_slice[1] = filter_slice_rgb48_pl;
687 case AV_PIX_FMT_BGRA64:
688 case AV_PIX_FMT_RGBA64:
689 s->filter_slice[0] = filter_slice_rgba64;
690 s->filter_slice[1] = filter_slice_rgba64_pl;
692 case AV_PIX_FMT_GBRP:
693 s->filter_slice[0] = filter_slice_gbrp;
694 s->filter_slice[1] = filter_slice_gbrp_pl;
696 case AV_PIX_FMT_GBRAP:
697 s->filter_slice[0] = filter_slice_gbrap;
698 s->filter_slice[1] = filter_slice_gbrap_pl;
700 case AV_PIX_FMT_GBRP9:
701 s->filter_slice[0] = filter_slice_gbrp9;
702 s->filter_slice[1] = filter_slice_gbrp9_pl;
704 case AV_PIX_FMT_GBRP10:
705 s->filter_slice[0] = filter_slice_gbrp10;
706 s->filter_slice[1] = filter_slice_gbrp10_pl;
708 case AV_PIX_FMT_GBRAP10:
709 s->filter_slice[0] = filter_slice_gbrap10;
710 s->filter_slice[1] = filter_slice_gbrap10_pl;
712 case AV_PIX_FMT_GBRP12:
713 s->filter_slice[0] = filter_slice_gbrp12;
714 s->filter_slice[1] = filter_slice_gbrp12_pl;
716 case AV_PIX_FMT_GBRAP12:
717 s->filter_slice[0] = filter_slice_gbrap12;
718 s->filter_slice[1] = filter_slice_gbrap12_pl;
720 case AV_PIX_FMT_GBRP14:
721 s->filter_slice[0] = filter_slice_gbrp14;
722 s->filter_slice[1] = filter_slice_gbrp14_pl;
724 case AV_PIX_FMT_GBRP16:
725 s->filter_slice[0] = filter_slice_gbrp16;
726 s->filter_slice[1] = filter_slice_gbrp16_pl;
728 case AV_PIX_FMT_GBRAP16:
729 s->filter_slice[0] = filter_slice_gbrap16;
730 s->filter_slice[1] = filter_slice_gbrap16_pl;
737 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
739 AVFilterContext *ctx = inlink->dst;
740 ColorChannelMixerContext *s = ctx->priv;
741 AVFilterLink *outlink = ctx->outputs[0];
742 const int pl = s->preserve_lightness > 0.;
746 if (av_frame_is_writable(in)) {
749 out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
752 return AVERROR(ENOMEM);
754 av_frame_copy_props(out, in);
759 ctx->internal->execute(ctx, s->filter_slice[pl], &td, NULL, FFMIN(outlink->h, ff_filter_get_nb_threads(ctx)));
763 return ff_filter_frame(outlink, out);
766 static int process_command(AVFilterContext *ctx, const char *cmd, const char *args,
767 char *res, int res_len, int flags)
769 int ret = ff_filter_process_command(ctx, cmd, args, res, res_len, flags);
774 return config_output(ctx->outputs[0]);
777 static av_cold void uninit(AVFilterContext *ctx)
779 ColorChannelMixerContext *s = ctx->priv;
781 av_freep(&s->buffer);
784 static const AVFilterPad colorchannelmixer_inputs[] = {
787 .type = AVMEDIA_TYPE_VIDEO,
788 .filter_frame = filter_frame,
793 static const AVFilterPad colorchannelmixer_outputs[] = {
796 .type = AVMEDIA_TYPE_VIDEO,
797 .config_props = config_output,
802 const AVFilter ff_vf_colorchannelmixer = {
803 .name = "colorchannelmixer",
804 .description = NULL_IF_CONFIG_SMALL("Adjust colors by mixing color channels."),
805 .priv_size = sizeof(ColorChannelMixerContext),
806 .priv_class = &colorchannelmixer_class,
808 .query_formats = query_formats,
809 .inputs = colorchannelmixer_inputs,
810 .outputs = colorchannelmixer_outputs,
811 .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC | AVFILTER_FLAG_SLICE_THREADS,
812 .process_command = process_command,