]> git.sesse.net Git - ffmpeg/blob - libavfilter/vf_colorchannelmixer.c
avfilter/vf_colorchannelmixer: add option to preserve lightness
[ffmpeg] / libavfilter / vf_colorchannelmixer.c
1 /*
2  * Copyright (c) 2013 Paul B Mahol
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 #include <float.h>
22
23 #include "libavutil/opt.h"
24 #include "libavutil/pixdesc.h"
25 #include "avfilter.h"
26 #include "drawutils.h"
27 #include "formats.h"
28 #include "internal.h"
29 #include "video.h"
30
31 #define R 0
32 #define G 1
33 #define B 2
34 #define A 3
35
36 typedef struct ThreadData {
37     AVFrame *in, *out;
38 } ThreadData;
39
40 typedef struct ColorChannelMixerContext {
41     const AVClass *class;
42     double rr, rg, rb, ra;
43     double gr, gg, gb, ga;
44     double br, bg, bb, ba;
45     double ar, ag, ab, aa;
46     double sr, sg, sb;
47     int preserve_lightness;
48
49     int *lut[4][4];
50
51     int *buffer;
52
53     uint8_t rgba_map[4];
54
55     int (*filter_slice[2])(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs);
56 } ColorChannelMixerContext;
57
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
60
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 },
79     { NULL }
80 };
81
82 AVFILTER_DEFINE_CLASS(colorchannelmixer);
83
84 static int query_formats(AVFilterContext *ctx)
85 {
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,
95         AV_PIX_FMT_GBRP9,
96         AV_PIX_FMT_GBRP10, AV_PIX_FMT_GBRAP10,
97         AV_PIX_FMT_GBRP12, AV_PIX_FMT_GBRAP12,
98         AV_PIX_FMT_GBRP14,
99         AV_PIX_FMT_GBRP16, AV_PIX_FMT_GBRAP16,
100         AV_PIX_FMT_NONE
101     };
102
103     AVFilterFormats *fmts_list = ff_make_format_list(pix_fmts);
104     if (!fmts_list)
105         return AVERROR(ENOMEM);
106     return ff_set_common_formats(ctx, fmts_list);
107 }
108
109 static void preservel(float *r, float *g, float *b, float lin, float lout)
110 {
111     *r *= lout / lin;
112     *g *= lout / lin;
113     *b *= lout / lin;
114 }
115
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)
118 {
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];
133     int i, j;
134
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;
142             float lin;
143
144             if (pl)
145                 lin = FFMAX3(rin, gin, bin) / 255.f + FFMIN3(rin, gin, bin) / 255.f;
146
147             rout = s->lut[R][R][rin] +
148                    s->lut[R][G][gin] +
149                    s->lut[R][B][bin] +
150                    (have_alpha == 1 ? s->lut[R][A][ain] : 0);
151             gout = s->lut[G][R][rin] +
152                    s->lut[G][G][gin] +
153                    s->lut[G][B][bin] +
154                    (have_alpha == 1 ? s->lut[G][A][ain] : 0);
155             bout = s->lut[B][R][rin] +
156                    s->lut[B][G][gin] +
157                    s->lut[B][B][bin] +
158                    (have_alpha == 1 ? s->lut[B][A][ain] : 0);
159
160             if (pl) {
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);
165
166                 preservel(&frout, &fgout, &fbout, lin, lout);
167
168                 rout = lrintf(frout * 255.f);
169                 gout = lrintf(fgout * 255.f);
170                 bout = lrintf(fbout * 255.f);
171             }
172
173             dstr[j] = av_clip_uint8(rout);
174             dstg[j] = av_clip_uint8(gout);
175             dstb[j] = av_clip_uint8(bout);
176
177             if (have_alpha == 1) {
178                 dsta[j] = av_clip_uint8(s->lut[A][R][rin] +
179                                         s->lut[A][G][gin] +
180                                         s->lut[A][B][bin] +
181                                         s->lut[A][A][ain]);
182             }
183         }
184
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];
193     }
194
195     return 0;
196 }
197
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)
200 {
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;
217     int i, j;
218
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;
226             float lin;
227
228             if (pl)
229                 lin = FFMAX3(rin, gin, bin) * scale + FFMIN3(rin, gin, bin) * scale;
230
231             rout = s->lut[R][R][rin] +
232                    s->lut[R][G][gin] +
233                    s->lut[R][B][bin] +
234                    (have_alpha == 1 ? s->lut[R][A][ain] : 0);
235             gout = s->lut[G][R][rin] +
236                    s->lut[G][G][gin] +
237                    s->lut[G][B][bin] +
238                    (have_alpha == 1 ? s->lut[G][A][ain] : 0);
239             bout = s->lut[B][R][rin] +
240                    s->lut[B][G][gin] +
241                    s->lut[B][B][bin] +
242                    (have_alpha == 1 ? s->lut[B][A][ain] : 0);
243
244             if (pl) {
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);
249
250                 preservel(&frout, &fgout, &fbout, lin, lout);
251
252                 rout = lrintf(frout * factor);
253                 gout = lrintf(fgout * factor);
254                 bout = lrintf(fbout * factor);
255             }
256
257             dstr[j] = av_clip_uintp2(rout, depth);
258             dstg[j] = av_clip_uintp2(gout, depth);
259             dstb[j] = av_clip_uintp2(bout, depth);
260
261             if (have_alpha == 1) {
262                 dsta[j] = av_clip_uintp2(s->lut[A][R][rin] +
263                                          s->lut[A][G][gin] +
264                                          s->lut[A][B][bin] +
265                                          s->lut[A][A][ain], depth);
266             }
267         }
268
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;
277     }
278
279     return 0;
280 }
281
282 static int filter_slice_gbrp(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
283 {
284     return filter_slice_rgba_planar(ctx, arg, jobnr, nb_jobs, 0, 0);
285 }
286
287 static int filter_slice_gbrap(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
288 {
289     return filter_slice_rgba_planar(ctx, arg, jobnr, nb_jobs, 1, 0);
290 }
291
292 static int filter_slice_gbrp_pl(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
293 {
294     return filter_slice_rgba_planar(ctx, arg, jobnr, nb_jobs, 0, 1);
295 }
296
297 static int filter_slice_gbrap_pl(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
298 {
299     return filter_slice_rgba_planar(ctx, arg, jobnr, nb_jobs, 1, 1);
300 }
301
302 static int filter_slice_gbrp9(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
303 {
304     return filter_slice_rgba16_planar(ctx, arg, jobnr, nb_jobs, 0, 9, 0);
305 }
306
307 static int filter_slice_gbrp10(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
308 {
309     return filter_slice_rgba16_planar(ctx, arg, jobnr, nb_jobs, 0, 10, 0);
310 }
311
312 static int filter_slice_gbrap10(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
313 {
314     return filter_slice_rgba16_planar(ctx, arg, jobnr, nb_jobs, 1, 10, 0);
315 }
316
317 static int filter_slice_gbrp12(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
318 {
319     return filter_slice_rgba16_planar(ctx, arg, jobnr, nb_jobs, 0, 12, 0);
320 }
321
322 static int filter_slice_gbrap12(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
323 {
324     return filter_slice_rgba16_planar(ctx, arg, jobnr, nb_jobs, 1, 12, 0);
325 }
326
327 static int filter_slice_gbrp14(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
328 {
329     return filter_slice_rgba16_planar(ctx, arg, jobnr, nb_jobs, 0, 14, 0);
330 }
331
332 static int filter_slice_gbrp16(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
333 {
334     return filter_slice_rgba16_planar(ctx, arg, jobnr, nb_jobs, 0, 16, 0);
335 }
336
337 static int filter_slice_gbrap16(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
338 {
339     return filter_slice_rgba16_planar(ctx, arg, jobnr, nb_jobs, 1, 16, 0);
340 }
341
342 static int filter_slice_gbrp9_pl(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
343 {
344     return filter_slice_rgba16_planar(ctx, arg, jobnr, nb_jobs, 0, 9, 1);
345 }
346
347 static int filter_slice_gbrp10_pl(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
348 {
349     return filter_slice_rgba16_planar(ctx, arg, jobnr, nb_jobs, 0, 10, 1);
350 }
351
352 static int filter_slice_gbrap10_pl(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
353 {
354     return filter_slice_rgba16_planar(ctx, arg, jobnr, nb_jobs, 1, 10, 1);
355 }
356
357 static int filter_slice_gbrp12_pl(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
358 {
359     return filter_slice_rgba16_planar(ctx, arg, jobnr, nb_jobs, 0, 12, 1);
360 }
361
362 static int filter_slice_gbrap12_pl(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
363 {
364     return filter_slice_rgba16_planar(ctx, arg, jobnr, nb_jobs, 1, 12, 1);
365 }
366
367 static int filter_slice_gbrp14_pl(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
368 {
369     return filter_slice_rgba16_planar(ctx, arg, jobnr, nb_jobs, 0, 14, 1);
370 }
371
372 static int filter_slice_gbrp16_pl(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
373 {
374     return filter_slice_rgba16_planar(ctx, arg, jobnr, nb_jobs, 0, 16, 1);
375 }
376
377 static int filter_slice_gbrap16_pl(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
378 {
379     return filter_slice_rgba16_planar(ctx, arg, jobnr, nb_jobs, 1, 16, 1);
380 }
381
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)
384 {
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];
397     int i, j;
398
399     for (i = slice_start; i < slice_end; i++) {
400         const uint8_t *src = srcrow;
401         uint8_t *dst = dstrow;
402
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;
409             float lin;
410
411             if (pl)
412                 lin = FFMAX3(rin, gin, bin) / 255.f + FFMIN3(rin, gin, bin) / 255.f;
413
414             rout = s->lut[R][R][rin] +
415                    s->lut[R][G][gin] +
416                    s->lut[R][B][bin] +
417                    (have_alpha == 1 ? s->lut[R][A][ain] : 0);
418             gout = s->lut[G][R][rin] +
419                    s->lut[G][G][gin] +
420                    s->lut[G][B][bin] +
421                    (have_alpha == 1 ? s->lut[G][A][ain] : 0);
422             bout = s->lut[B][R][rin] +
423                    s->lut[B][G][gin] +
424                    s->lut[B][B][bin] +
425                    (have_alpha == 1 ? s->lut[B][A][ain] : 0);
426
427             if (pl) {
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);
432
433                 preservel(&frout, &fgout, &fbout, lin, lout);
434
435                 rout = lrintf(frout * 255.f);
436                 gout = lrintf(fgout * 255.f);
437                 bout = lrintf(fbout * 255.f);
438             }
439
440             dst[j + roffset] = av_clip_uint8(rout);
441             dst[j + goffset] = av_clip_uint8(gout);
442             dst[j + boffset] = av_clip_uint8(bout);
443
444             if (have_alpha == 1) {
445                 dst[j + aoffset] = av_clip_uint8(s->lut[A][R][rin] +
446                                                  s->lut[A][G][gin] +
447                                                  s->lut[A][B][bin] +
448                                                  s->lut[A][A][ain]);
449             } else if (have_alpha == -1 && in != out)
450                 dst[j + aoffset] = 0;
451         }
452
453         srcrow += in->linesize[0];
454         dstrow += out->linesize[0];
455     }
456
457     return 0;
458 }
459
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)
462 {
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];
475     int i, j;
476
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;
480
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;
487             float lin;
488
489             if (pl)
490                 lin = FFMAX3(rin, gin, bin) / 65535.f + FFMIN3(rin, gin, bin) / 65535.f;
491
492             rout = s->lut[R][R][rin] +
493                    s->lut[R][G][gin] +
494                    s->lut[R][B][bin] +
495                    (have_alpha == 1 ? s->lut[R][A][ain] : 0);
496             gout = s->lut[G][R][rin] +
497                    s->lut[G][G][gin] +
498                    s->lut[G][B][bin] +
499                    (have_alpha == 1 ? s->lut[G][A][ain] : 0);
500             bout = s->lut[B][R][rin] +
501                    s->lut[B][G][gin] +
502                    s->lut[B][B][bin] +
503                    (have_alpha == 1 ? s->lut[B][A][ain] : 0);
504
505             if (pl) {
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);
510
511                 preservel(&frout, &fgout, &fbout, lin, lout);
512
513                 rout = lrintf(frout * 65535.f);
514                 gout = lrintf(fgout * 65535.f);
515                 bout = lrintf(fbout * 65535.f);
516             }
517
518             dst[j + roffset] = av_clip_uint16(rout);
519             dst[j + goffset] = av_clip_uint16(gout);
520             dst[j + boffset] = av_clip_uint16(bout);
521
522             if (have_alpha == 1) {
523                 dst[j + aoffset] = av_clip_uint16(s->lut[A][R][rin] +
524                                                   s->lut[A][G][gin] +
525                                                   s->lut[A][B][bin] +
526                                                   s->lut[A][A][ain]);
527             }
528         }
529
530         srcrow += in->linesize[0];
531         dstrow += out->linesize[0];
532     }
533
534     return 0;
535 }
536
537 static int filter_slice_rgba64(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
538 {
539     return filter_slice_rgba16_packed(ctx, arg, jobnr, nb_jobs, 1, 4, 0);
540 }
541
542 static int filter_slice_rgb48(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
543 {
544     return filter_slice_rgba16_packed(ctx, arg, jobnr, nb_jobs, 0, 3, 0);
545 }
546
547 static int filter_slice_rgba64_pl(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
548 {
549     return filter_slice_rgba16_packed(ctx, arg, jobnr, nb_jobs, 1, 4, 1);
550 }
551
552 static int filter_slice_rgb48_pl(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
553 {
554     return filter_slice_rgba16_packed(ctx, arg, jobnr, nb_jobs, 0, 3, 1);
555 }
556
557 static int filter_slice_rgba(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
558 {
559     return filter_slice_rgba_packed(ctx, arg, jobnr, nb_jobs, 1, 4, 0);
560 }
561
562 static int filter_slice_rgb24(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
563 {
564     return filter_slice_rgba_packed(ctx, arg, jobnr, nb_jobs, 0, 3, 0);
565 }
566
567 static int filter_slice_rgb0(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
568 {
569     return filter_slice_rgba_packed(ctx, arg, jobnr, nb_jobs, -1, 4, 0);
570 }
571
572 static int filter_slice_rgba_pl(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
573 {
574     return filter_slice_rgba_packed(ctx, arg, jobnr, nb_jobs, 1, 4, 1);
575 }
576
577 static int filter_slice_rgb24_pl(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
578 {
579     return filter_slice_rgba_packed(ctx, arg, jobnr, nb_jobs, 0, 3, 1);
580 }
581
582 static int filter_slice_rgb0_pl(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
583 {
584     return filter_slice_rgba_packed(ctx, arg, jobnr, nb_jobs, -1, 4, 1);
585 }
586
587 static int config_output(AVFilterLink *outlink)
588 {
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;
594
595     ff_fill_rgba_map(s->rgba_map, outlink->format);
596
597     size = 1 << depth;
598     if (!s->buffer) {
599         s->buffer = buffer = av_malloc(16 * size * sizeof(*s->buffer));
600         if (!s->buffer)
601             return AVERROR(ENOMEM);
602
603         for (i = 0; i < 4; i++)
604             for (j = 0; j < 4; j++, buffer += size)
605                 s->lut[i][j] = buffer;
606     }
607
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;
611
612     if (fabs(s->sr) <= DBL_EPSILON)
613         s->sr = 1.;
614
615     if (fabs(s->sg) <= DBL_EPSILON)
616         s->sg = 1.;
617
618     if (fabs(s->sb) <= DBL_EPSILON)
619         s->sb = 1.;
620
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);
626
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);
631
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);
636
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);
641     }
642
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;
648         break;
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;
655         break;
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;
662         break;
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;
667         break;
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;
672         break;
673     case AV_PIX_FMT_GBRP:
674         s->filter_slice[0] = filter_slice_gbrp;
675         s->filter_slice[1] = filter_slice_gbrp_pl;
676         break;
677     case AV_PIX_FMT_GBRAP:
678         s->filter_slice[0] = filter_slice_gbrap;
679         s->filter_slice[1] = filter_slice_gbrap_pl;
680         break;
681     case AV_PIX_FMT_GBRP9:
682         s->filter_slice[0] = filter_slice_gbrp9;
683         s->filter_slice[1] = filter_slice_gbrp9_pl;
684         break;
685     case AV_PIX_FMT_GBRP10:
686         s->filter_slice[0] = filter_slice_gbrp10;
687         s->filter_slice[1] = filter_slice_gbrp10_pl;
688         break;
689     case AV_PIX_FMT_GBRAP10:
690         s->filter_slice[0] = filter_slice_gbrap10;
691         s->filter_slice[1] = filter_slice_gbrap10_pl;
692         break;
693     case AV_PIX_FMT_GBRP12:
694         s->filter_slice[0] = filter_slice_gbrp12;
695         s->filter_slice[1] = filter_slice_gbrp12_pl;
696         break;
697     case AV_PIX_FMT_GBRAP12:
698         s->filter_slice[0] = filter_slice_gbrap12;
699         s->filter_slice[1] = filter_slice_gbrap12_pl;
700         break;
701     case AV_PIX_FMT_GBRP14:
702         s->filter_slice[0] = filter_slice_gbrp14;
703         s->filter_slice[1] = filter_slice_gbrp14_pl;
704         break;
705     case AV_PIX_FMT_GBRP16:
706         s->filter_slice[0] = filter_slice_gbrp16;
707         s->filter_slice[1] = filter_slice_gbrp16_pl;
708         break;
709     case AV_PIX_FMT_GBRAP16:
710         s->filter_slice[0] = filter_slice_gbrap16;
711         s->filter_slice[1] = filter_slice_gbrap16_pl;
712         break;
713     }
714
715     return 0;
716 }
717
718 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
719 {
720     AVFilterContext *ctx = inlink->dst;
721     ColorChannelMixerContext *s = ctx->priv;
722     AVFilterLink *outlink = ctx->outputs[0];
723     const int pl = s->preserve_lightness;
724     ThreadData td;
725     AVFrame *out;
726
727     if (av_frame_is_writable(in)) {
728         out = in;
729     } else {
730         out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
731         if (!out) {
732             av_frame_free(&in);
733             return AVERROR(ENOMEM);
734         }
735         av_frame_copy_props(out, in);
736     }
737
738     td.in = in;
739     td.out = out;
740     ctx->internal->execute(ctx, s->filter_slice[pl], &td, NULL, FFMIN(outlink->h, ff_filter_get_nb_threads(ctx)));
741
742     if (in != out)
743         av_frame_free(&in);
744     return ff_filter_frame(outlink, out);
745 }
746
747 static int process_command(AVFilterContext *ctx, const char *cmd, const char *args,
748                            char *res, int res_len, int flags)
749 {
750     int ret = ff_filter_process_command(ctx, cmd, args, res, res_len, flags);
751
752     if (ret < 0)
753         return ret;
754
755     return config_output(ctx->outputs[0]);
756 }
757
758 static av_cold void uninit(AVFilterContext *ctx)
759 {
760     ColorChannelMixerContext *s = ctx->priv;
761
762     av_freep(&s->buffer);
763 }
764
765 static const AVFilterPad colorchannelmixer_inputs[] = {
766     {
767         .name         = "default",
768         .type         = AVMEDIA_TYPE_VIDEO,
769         .filter_frame = filter_frame,
770     },
771     { NULL }
772 };
773
774 static const AVFilterPad colorchannelmixer_outputs[] = {
775     {
776         .name         = "default",
777         .type         = AVMEDIA_TYPE_VIDEO,
778         .config_props = config_output,
779     },
780     { NULL }
781 };
782
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,
788     .uninit        = uninit,
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,
794 };