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
21 #include "libavutil/opt.h"
22 #include "libavutil/pixdesc.h"
24 #include "drawutils.h"
34 typedef struct ThreadData {
38 typedef struct ColorChannelMixerContext {
40 double rr, rg, rb, ra;
41 double gr, gg, gb, ga;
42 double br, bg, bb, ba;
43 double ar, ag, ab, aa;
51 int (*filter_slice)(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs);
52 } ColorChannelMixerContext;
54 #define OFFSET(x) offsetof(ColorChannelMixerContext, x)
55 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_RUNTIME_PARAM
57 static const AVOption colorchannelmixer_options[] = {
58 { "rr", "set the red gain for the red channel", OFFSET(rr), AV_OPT_TYPE_DOUBLE, {.dbl=1}, -2, 2, FLAGS },
59 { "rg", "set the green gain for the red channel", OFFSET(rg), AV_OPT_TYPE_DOUBLE, {.dbl=0}, -2, 2, FLAGS },
60 { "rb", "set the blue gain for the red channel", OFFSET(rb), AV_OPT_TYPE_DOUBLE, {.dbl=0}, -2, 2, FLAGS },
61 { "ra", "set the alpha gain for the red channel", OFFSET(ra), AV_OPT_TYPE_DOUBLE, {.dbl=0}, -2, 2, FLAGS },
62 { "gr", "set the red gain for the green channel", OFFSET(gr), AV_OPT_TYPE_DOUBLE, {.dbl=0}, -2, 2, FLAGS },
63 { "gg", "set the green gain for the green channel", OFFSET(gg), AV_OPT_TYPE_DOUBLE, {.dbl=1}, -2, 2, FLAGS },
64 { "gb", "set the blue gain for the green channel", OFFSET(gb), AV_OPT_TYPE_DOUBLE, {.dbl=0}, -2, 2, FLAGS },
65 { "ga", "set the alpha gain for the green channel", OFFSET(ga), AV_OPT_TYPE_DOUBLE, {.dbl=0}, -2, 2, FLAGS },
66 { "br", "set the red gain for the blue channel", OFFSET(br), AV_OPT_TYPE_DOUBLE, {.dbl=0}, -2, 2, FLAGS },
67 { "bg", "set the green gain for the blue channel", OFFSET(bg), AV_OPT_TYPE_DOUBLE, {.dbl=0}, -2, 2, FLAGS },
68 { "bb", "set the blue gain for the blue channel", OFFSET(bb), AV_OPT_TYPE_DOUBLE, {.dbl=1}, -2, 2, FLAGS },
69 { "ba", "set the alpha gain for the blue channel", OFFSET(ba), AV_OPT_TYPE_DOUBLE, {.dbl=0}, -2, 2, FLAGS },
70 { "ar", "set the red gain for the alpha channel", OFFSET(ar), AV_OPT_TYPE_DOUBLE, {.dbl=0}, -2, 2, FLAGS },
71 { "ag", "set the green gain for the alpha channel", OFFSET(ag), AV_OPT_TYPE_DOUBLE, {.dbl=0}, -2, 2, FLAGS },
72 { "ab", "set the blue gain for the alpha channel", OFFSET(ab), AV_OPT_TYPE_DOUBLE, {.dbl=0}, -2, 2, FLAGS },
73 { "aa", "set the alpha gain for the alpha channel", OFFSET(aa), AV_OPT_TYPE_DOUBLE, {.dbl=1}, -2, 2, FLAGS },
77 AVFILTER_DEFINE_CLASS(colorchannelmixer);
79 static int query_formats(AVFilterContext *ctx)
81 static const enum AVPixelFormat pix_fmts[] = {
82 AV_PIX_FMT_RGB24, AV_PIX_FMT_BGR24,
83 AV_PIX_FMT_RGBA, AV_PIX_FMT_BGRA,
84 AV_PIX_FMT_ARGB, AV_PIX_FMT_ABGR,
85 AV_PIX_FMT_0RGB, AV_PIX_FMT_0BGR,
86 AV_PIX_FMT_RGB0, AV_PIX_FMT_BGR0,
87 AV_PIX_FMT_RGB48, AV_PIX_FMT_BGR48,
88 AV_PIX_FMT_RGBA64, AV_PIX_FMT_BGRA64,
89 AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRAP,
91 AV_PIX_FMT_GBRP10, AV_PIX_FMT_GBRAP10,
92 AV_PIX_FMT_GBRP12, AV_PIX_FMT_GBRAP12,
94 AV_PIX_FMT_GBRP16, AV_PIX_FMT_GBRAP16,
98 AVFilterFormats *fmts_list = ff_make_format_list(pix_fmts);
100 return AVERROR(ENOMEM);
101 return ff_set_common_formats(ctx, fmts_list);
104 static av_always_inline int filter_slice_rgba_planar(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs,
107 ColorChannelMixerContext *s = ctx->priv;
108 ThreadData *td = arg;
109 AVFrame *in = td->in;
110 AVFrame *out = td->out;
111 const int slice_start = (out->height * jobnr) / nb_jobs;
112 const int slice_end = (out->height * (jobnr+1)) / nb_jobs;
113 const uint8_t *srcg = in->data[0] + slice_start * in->linesize[0];
114 const uint8_t *srcb = in->data[1] + slice_start * in->linesize[1];
115 const uint8_t *srcr = in->data[2] + slice_start * in->linesize[2];
116 const uint8_t *srca = in->data[3] + slice_start * in->linesize[3];
117 uint8_t *dstg = out->data[0] + slice_start * out->linesize[0];
118 uint8_t *dstb = out->data[1] + slice_start * out->linesize[1];
119 uint8_t *dstr = out->data[2] + slice_start * out->linesize[2];
120 uint8_t *dsta = out->data[3] + slice_start * out->linesize[3];
123 for (i = slice_start; i < slice_end; i++) {
124 for (j = 0; j < out->width; j++) {
125 const uint8_t rin = srcr[j];
126 const uint8_t gin = srcg[j];
127 const uint8_t bin = srcb[j];
128 const uint8_t ain = have_alpha ? srca[j] : 0;
130 dstr[j] = av_clip_uint8(s->lut[R][R][rin] +
133 (have_alpha == 1 ? s->lut[R][A][ain] : 0));
134 dstg[j] = av_clip_uint8(s->lut[G][R][rin] +
137 (have_alpha == 1 ? s->lut[G][A][ain] : 0));
138 dstb[j] = av_clip_uint8(s->lut[B][R][rin] +
141 (have_alpha == 1 ? s->lut[B][A][ain] : 0));
142 if (have_alpha == 1) {
143 dsta[j] = av_clip_uint8(s->lut[A][R][rin] +
150 srcg += in->linesize[0];
151 srcb += in->linesize[1];
152 srcr += in->linesize[2];
153 srca += in->linesize[3];
154 dstg += out->linesize[0];
155 dstb += out->linesize[1];
156 dstr += out->linesize[2];
157 dsta += out->linesize[3];
163 static av_always_inline int filter_slice_rgba16_planar(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs,
164 int have_alpha, int depth)
166 ColorChannelMixerContext *s = ctx->priv;
167 ThreadData *td = arg;
168 AVFrame *in = td->in;
169 AVFrame *out = td->out;
170 const int slice_start = (out->height * jobnr) / nb_jobs;
171 const int slice_end = (out->height * (jobnr+1)) / nb_jobs;
172 const uint16_t *srcg = (const uint16_t *)(in->data[0] + slice_start * in->linesize[0]);
173 const uint16_t *srcb = (const uint16_t *)(in->data[1] + slice_start * in->linesize[1]);
174 const uint16_t *srcr = (const uint16_t *)(in->data[2] + slice_start * in->linesize[2]);
175 const uint16_t *srca = (const uint16_t *)(in->data[3] + slice_start * in->linesize[3]);
176 uint16_t *dstg = (uint16_t *)(out->data[0] + slice_start * out->linesize[0]);
177 uint16_t *dstb = (uint16_t *)(out->data[1] + slice_start * out->linesize[1]);
178 uint16_t *dstr = (uint16_t *)(out->data[2] + slice_start * out->linesize[2]);
179 uint16_t *dsta = (uint16_t *)(out->data[3] + slice_start * out->linesize[3]);
182 for (i = slice_start; i < slice_end; i++) {
183 for (j = 0; j < out->width; j++) {
184 const uint16_t rin = srcr[j];
185 const uint16_t gin = srcg[j];
186 const uint16_t bin = srcb[j];
187 const uint16_t ain = have_alpha ? srca[j] : 0;
189 dstr[j] = av_clip_uintp2(s->lut[R][R][rin] +
192 (have_alpha == 1 ? s->lut[R][A][ain] : 0), depth);
193 dstg[j] = av_clip_uintp2(s->lut[G][R][rin] +
196 (have_alpha == 1 ? s->lut[G][A][ain] : 0), depth);
197 dstb[j] = av_clip_uintp2(s->lut[B][R][rin] +
200 (have_alpha == 1 ? s->lut[B][A][ain] : 0), depth);
201 if (have_alpha == 1) {
202 dsta[j] = av_clip_uintp2(s->lut[A][R][rin] +
205 s->lut[A][A][ain], depth);
209 srcg += in->linesize[0] / 2;
210 srcb += in->linesize[1] / 2;
211 srcr += in->linesize[2] / 2;
212 srca += in->linesize[3] / 2;
213 dstg += out->linesize[0] / 2;
214 dstb += out->linesize[1] / 2;
215 dstr += out->linesize[2] / 2;
216 dsta += out->linesize[3] / 2;
222 static int filter_slice_gbrp(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
224 return filter_slice_rgba_planar(ctx, arg, jobnr, nb_jobs, 0);
227 static int filter_slice_gbrap(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
229 return filter_slice_rgba_planar(ctx, arg, jobnr, nb_jobs, 1);
232 static int filter_slice_gbrp9(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
234 return filter_slice_rgba16_planar(ctx, arg, jobnr, nb_jobs, 0, 9);
237 static int filter_slice_gbrp10(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
239 return filter_slice_rgba16_planar(ctx, arg, jobnr, nb_jobs, 0, 10);
242 static int filter_slice_gbrap10(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
244 return filter_slice_rgba16_planar(ctx, arg, jobnr, nb_jobs, 1, 10);
247 static int filter_slice_gbrp12(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
249 return filter_slice_rgba16_planar(ctx, arg, jobnr, nb_jobs, 0, 12);
252 static int filter_slice_gbrap12(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
254 return filter_slice_rgba16_planar(ctx, arg, jobnr, nb_jobs, 1, 12);
257 static int filter_slice_gbrp14(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
259 return filter_slice_rgba16_planar(ctx, arg, jobnr, nb_jobs, 0, 14);
262 static int filter_slice_gbrp16(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
264 return filter_slice_rgba16_planar(ctx, arg, jobnr, nb_jobs, 0, 16);
267 static int filter_slice_gbrap16(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
269 return filter_slice_rgba16_planar(ctx, arg, jobnr, nb_jobs, 1, 16);
272 static av_always_inline int filter_slice_rgba_packed(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs,
273 int have_alpha, int step)
275 ColorChannelMixerContext *s = ctx->priv;
276 ThreadData *td = arg;
277 AVFrame *in = td->in;
278 AVFrame *out = td->out;
279 const int slice_start = (out->height * jobnr) / nb_jobs;
280 const int slice_end = (out->height * (jobnr+1)) / nb_jobs;
281 const uint8_t roffset = s->rgba_map[R];
282 const uint8_t goffset = s->rgba_map[G];
283 const uint8_t boffset = s->rgba_map[B];
284 const uint8_t aoffset = s->rgba_map[A];
285 const uint8_t *srcrow = in->data[0] + slice_start * in->linesize[0];
286 uint8_t *dstrow = out->data[0] + slice_start * out->linesize[0];
289 for (i = slice_start; i < slice_end; i++) {
290 const uint8_t *src = srcrow;
291 uint8_t *dst = dstrow;
293 for (j = 0; j < out->width * step; j += step) {
294 const uint8_t rin = src[j + roffset];
295 const uint8_t gin = src[j + goffset];
296 const uint8_t bin = src[j + boffset];
297 const uint8_t ain = src[j + aoffset];
299 dst[j + roffset] = av_clip_uint8(s->lut[R][R][rin] +
302 (have_alpha == 1 ? s->lut[R][A][ain] : 0));
303 dst[j + goffset] = av_clip_uint8(s->lut[G][R][rin] +
306 (have_alpha == 1 ? s->lut[G][A][ain] : 0));
307 dst[j + boffset] = av_clip_uint8(s->lut[B][R][rin] +
310 (have_alpha == 1 ? s->lut[B][A][ain] : 0));
311 if (have_alpha == 1) {
312 dst[j + aoffset] = av_clip_uint8(s->lut[A][R][rin] +
316 } else if (have_alpha == -1 && in != out)
317 dst[j + aoffset] = 0;
320 srcrow += in->linesize[0];
321 dstrow += out->linesize[0];
327 static av_always_inline int filter_slice_rgba16_packed(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs,
328 int have_alpha, int step)
330 ColorChannelMixerContext *s = ctx->priv;
331 ThreadData *td = arg;
332 AVFrame *in = td->in;
333 AVFrame *out = td->out;
334 const int slice_start = (out->height * jobnr) / nb_jobs;
335 const int slice_end = (out->height * (jobnr+1)) / nb_jobs;
336 const uint8_t roffset = s->rgba_map[R];
337 const uint8_t goffset = s->rgba_map[G];
338 const uint8_t boffset = s->rgba_map[B];
339 const uint8_t aoffset = s->rgba_map[A];
340 const uint8_t *srcrow = in->data[0] + slice_start * in->linesize[0];
341 uint8_t *dstrow = out->data[0] + slice_start * out->linesize[0];
344 for (i = slice_start; i < slice_end; i++) {
345 const uint16_t *src = (const uint16_t *)srcrow;
346 uint16_t *dst = (uint16_t *)dstrow;
348 for (j = 0; j < out->width * step; j += step) {
349 const uint16_t rin = src[j + roffset];
350 const uint16_t gin = src[j + goffset];
351 const uint16_t bin = src[j + boffset];
352 const uint16_t ain = src[j + aoffset];
354 dst[j + roffset] = av_clip_uint16(s->lut[R][R][rin] +
357 (have_alpha == 1 ? s->lut[R][A][ain] : 0));
358 dst[j + goffset] = av_clip_uint16(s->lut[G][R][rin] +
361 (have_alpha == 1 ? s->lut[G][A][ain] : 0));
362 dst[j + boffset] = av_clip_uint16(s->lut[B][R][rin] +
365 (have_alpha == 1 ? s->lut[B][A][ain] : 0));
366 if (have_alpha == 1) {
367 dst[j + aoffset] = av_clip_uint16(s->lut[A][R][rin] +
374 srcrow += in->linesize[0];
375 dstrow += out->linesize[0];
381 static int filter_slice_rgba64(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
383 return filter_slice_rgba16_packed(ctx, arg, jobnr, nb_jobs, 1, 4);
386 static int filter_slice_rgb48(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
388 return filter_slice_rgba16_packed(ctx, arg, jobnr, nb_jobs, 0, 3);
391 static int filter_slice_rgba(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
393 return filter_slice_rgba_packed(ctx, arg, jobnr, nb_jobs, 1, 4);
396 static int filter_slice_rgb24(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
398 return filter_slice_rgba_packed(ctx, arg, jobnr, nb_jobs, 0, 3);
401 static int filter_slice_rgb0(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
403 return filter_slice_rgba_packed(ctx, arg, jobnr, nb_jobs, -1, 4);
406 static int config_output(AVFilterLink *outlink)
408 AVFilterContext *ctx = outlink->src;
409 ColorChannelMixerContext *s = ctx->priv;
410 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(outlink->format);
411 const int depth = desc->comp[0].depth;
412 int i, j, size, *buffer = s->buffer;
414 ff_fill_rgba_map(s->rgba_map, outlink->format);
418 s->buffer = buffer = av_malloc(16 * size * sizeof(*s->buffer));
420 return AVERROR(ENOMEM);
422 for (i = 0; i < 4; i++)
423 for (j = 0; j < 4; j++, buffer += size)
424 s->lut[i][j] = buffer;
427 for (i = 0; i < size; i++) {
428 s->lut[R][R][i] = lrint(i * s->rr);
429 s->lut[R][G][i] = lrint(i * s->rg);
430 s->lut[R][B][i] = lrint(i * s->rb);
431 s->lut[R][A][i] = lrint(i * s->ra);
433 s->lut[G][R][i] = lrint(i * s->gr);
434 s->lut[G][G][i] = lrint(i * s->gg);
435 s->lut[G][B][i] = lrint(i * s->gb);
436 s->lut[G][A][i] = lrint(i * s->ga);
438 s->lut[B][R][i] = lrint(i * s->br);
439 s->lut[B][G][i] = lrint(i * s->bg);
440 s->lut[B][B][i] = lrint(i * s->bb);
441 s->lut[B][A][i] = lrint(i * s->ba);
443 s->lut[A][R][i] = lrint(i * s->ar);
444 s->lut[A][G][i] = lrint(i * s->ag);
445 s->lut[A][B][i] = lrint(i * s->ab);
446 s->lut[A][A][i] = lrint(i * s->aa);
449 switch (outlink->format) {
450 case AV_PIX_FMT_BGR24:
451 case AV_PIX_FMT_RGB24:
452 s->filter_slice = filter_slice_rgb24;
454 case AV_PIX_FMT_0BGR:
455 case AV_PIX_FMT_0RGB:
456 case AV_PIX_FMT_BGR0:
457 case AV_PIX_FMT_RGB0:
458 s->filter_slice = filter_slice_rgb0;
460 case AV_PIX_FMT_ABGR:
461 case AV_PIX_FMT_ARGB:
462 case AV_PIX_FMT_BGRA:
463 case AV_PIX_FMT_RGBA:
464 s->filter_slice = filter_slice_rgba;
466 case AV_PIX_FMT_BGR48:
467 case AV_PIX_FMT_RGB48:
468 s->filter_slice = filter_slice_rgb48;
470 case AV_PIX_FMT_BGRA64:
471 case AV_PIX_FMT_RGBA64:
472 s->filter_slice = filter_slice_rgba64;
474 case AV_PIX_FMT_GBRP:
475 s->filter_slice = filter_slice_gbrp;
477 case AV_PIX_FMT_GBRAP:
478 s->filter_slice = filter_slice_gbrap;
480 case AV_PIX_FMT_GBRP9:
481 s->filter_slice = filter_slice_gbrp9;
483 case AV_PIX_FMT_GBRP10:
484 s->filter_slice = filter_slice_gbrp10;
486 case AV_PIX_FMT_GBRAP10:
487 s->filter_slice = filter_slice_gbrap10;
489 case AV_PIX_FMT_GBRP12:
490 s->filter_slice = filter_slice_gbrp12;
492 case AV_PIX_FMT_GBRAP12:
493 s->filter_slice = filter_slice_gbrap12;
495 case AV_PIX_FMT_GBRP14:
496 s->filter_slice = filter_slice_gbrp14;
498 case AV_PIX_FMT_GBRP16:
499 s->filter_slice = filter_slice_gbrp16;
501 case AV_PIX_FMT_GBRAP16:
502 s->filter_slice = filter_slice_gbrap16;
509 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
511 AVFilterContext *ctx = inlink->dst;
512 ColorChannelMixerContext *s = ctx->priv;
513 AVFilterLink *outlink = ctx->outputs[0];
517 if (av_frame_is_writable(in)) {
520 out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
523 return AVERROR(ENOMEM);
525 av_frame_copy_props(out, in);
530 ctx->internal->execute(ctx, s->filter_slice, &td, NULL, FFMIN(outlink->h, ff_filter_get_nb_threads(ctx)));
534 return ff_filter_frame(outlink, out);
537 static int process_command(AVFilterContext *ctx, const char *cmd, const char *args,
538 char *res, int res_len, int flags)
540 int ret = ff_filter_process_command(ctx, cmd, args, res, res_len, flags);
545 return config_output(ctx->outputs[0]);
548 static av_cold void uninit(AVFilterContext *ctx)
550 ColorChannelMixerContext *s = ctx->priv;
552 av_freep(&s->buffer);
555 static const AVFilterPad colorchannelmixer_inputs[] = {
558 .type = AVMEDIA_TYPE_VIDEO,
559 .filter_frame = filter_frame,
564 static const AVFilterPad colorchannelmixer_outputs[] = {
567 .type = AVMEDIA_TYPE_VIDEO,
568 .config_props = config_output,
573 AVFilter ff_vf_colorchannelmixer = {
574 .name = "colorchannelmixer",
575 .description = NULL_IF_CONFIG_SMALL("Adjust colors by mixing color channels."),
576 .priv_size = sizeof(ColorChannelMixerContext),
577 .priv_class = &colorchannelmixer_class,
579 .query_formats = query_formats,
580 .inputs = colorchannelmixer_inputs,
581 .outputs = colorchannelmixer_outputs,
582 .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC | AVFILTER_FLAG_SLICE_THREADS,
583 .process_command = process_command,