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"
23 #include "drawutils.h"
35 double rr, rg, rb, ra;
36 double gr, gg, gb, ga;
37 double br, bg, bb, ba;
38 double ar, ag, ab, aa;
45 } ColorChannelMixerContext;
47 #define OFFSET(x) offsetof(ColorChannelMixerContext, x)
48 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
49 static const AVOption colorchannelmixer_options[] = {
50 { "rr", "set the red gain for the red channel", OFFSET(rr), AV_OPT_TYPE_DOUBLE, {.dbl=1}, -2, 2, FLAGS },
51 { "rg", "set the green gain for the red channel", OFFSET(rg), AV_OPT_TYPE_DOUBLE, {.dbl=0}, -2, 2, FLAGS },
52 { "rb", "set the blue gain for the red channel", OFFSET(rb), AV_OPT_TYPE_DOUBLE, {.dbl=0}, -2, 2, FLAGS },
53 { "ra", "set the alpha gain for the red channel", OFFSET(ra), AV_OPT_TYPE_DOUBLE, {.dbl=0}, -2, 2, FLAGS },
54 { "gr", "set the red gain for the green channel", OFFSET(gr), AV_OPT_TYPE_DOUBLE, {.dbl=0}, -2, 2, FLAGS },
55 { "gg", "set the green gain for the green channel", OFFSET(gg), AV_OPT_TYPE_DOUBLE, {.dbl=1}, -2, 2, FLAGS },
56 { "gb", "set the blue gain for the green channel", OFFSET(gb), AV_OPT_TYPE_DOUBLE, {.dbl=0}, -2, 2, FLAGS },
57 { "ga", "set the alpha gain for the green channel", OFFSET(ga), AV_OPT_TYPE_DOUBLE, {.dbl=0}, -2, 2, FLAGS },
58 { "br", "set the red gain for the blue channel", OFFSET(br), AV_OPT_TYPE_DOUBLE, {.dbl=0}, -2, 2, FLAGS },
59 { "bg", "set the green gain for the blue channel", OFFSET(bg), AV_OPT_TYPE_DOUBLE, {.dbl=0}, -2, 2, FLAGS },
60 { "bb", "set the blue gain for the blue channel", OFFSET(bb), AV_OPT_TYPE_DOUBLE, {.dbl=1}, -2, 2, FLAGS },
61 { "ba", "set the alpha gain for the blue channel", OFFSET(ba), AV_OPT_TYPE_DOUBLE, {.dbl=0}, -2, 2, FLAGS },
62 { "ar", "set the red gain for the alpha channel", OFFSET(ar), AV_OPT_TYPE_DOUBLE, {.dbl=0}, -2, 2, FLAGS },
63 { "ag", "set the green gain for the alpha channel", OFFSET(ag), AV_OPT_TYPE_DOUBLE, {.dbl=0}, -2, 2, FLAGS },
64 { "ab", "set the blue gain for the alpha channel", OFFSET(ab), AV_OPT_TYPE_DOUBLE, {.dbl=0}, -2, 2, FLAGS },
65 { "aa", "set the alpha gain for the alpha channel", OFFSET(aa), AV_OPT_TYPE_DOUBLE, {.dbl=1}, -2, 2, FLAGS },
69 AVFILTER_DEFINE_CLASS(colorchannelmixer);
71 static int query_formats(AVFilterContext *ctx)
73 static const enum AVPixelFormat pix_fmts[] = {
74 AV_PIX_FMT_RGB24, AV_PIX_FMT_BGR24,
75 AV_PIX_FMT_RGBA, AV_PIX_FMT_BGRA,
76 AV_PIX_FMT_ARGB, AV_PIX_FMT_ABGR,
77 AV_PIX_FMT_0RGB, AV_PIX_FMT_0BGR,
78 AV_PIX_FMT_RGB0, AV_PIX_FMT_BGR0,
79 AV_PIX_FMT_RGB48, AV_PIX_FMT_BGR48,
80 AV_PIX_FMT_RGBA64, AV_PIX_FMT_BGRA64,
84 AVFilterFormats *fmts_list = ff_make_format_list(pix_fmts);
86 return AVERROR(ENOMEM);
87 return ff_set_common_formats(ctx, fmts_list);
90 static int config_output(AVFilterLink *outlink)
92 AVFilterContext *ctx = outlink->src;
93 ColorChannelMixerContext *s = ctx->priv;
94 int i, j, size, *buffer;
96 ff_fill_rgba_map(s->rgba_map, outlink->format);
98 switch (outlink->format) {
99 case AV_PIX_FMT_RGB48:
100 case AV_PIX_FMT_BGR48:
101 case AV_PIX_FMT_RGBA64:
102 case AV_PIX_FMT_BGRA64:
109 s->buffer = buffer = av_malloc(16 * size * sizeof(*s->buffer));
111 return AVERROR(ENOMEM);
113 for (i = 0; i < 4; i++)
114 for (j = 0; j < 4; j++, buffer += size)
115 s->lut[i][j] = buffer;
117 for (i = 0; i < size; i++) {
118 s->lut[R][R][i] = round(i * s->rr);
119 s->lut[R][G][i] = round(i * s->rg);
120 s->lut[R][B][i] = round(i * s->rb);
121 s->lut[R][A][i] = round(i * s->ra);
123 s->lut[G][R][i] = round(i * s->gr);
124 s->lut[G][G][i] = round(i * s->gg);
125 s->lut[G][B][i] = round(i * s->gb);
126 s->lut[G][A][i] = round(i * s->ga);
128 s->lut[B][R][i] = round(i * s->br);
129 s->lut[B][G][i] = round(i * s->bg);
130 s->lut[B][B][i] = round(i * s->bb);
131 s->lut[B][A][i] = round(i * s->ba);
133 s->lut[A][R][i] = round(i * s->ar);
134 s->lut[A][G][i] = round(i * s->ag);
135 s->lut[A][B][i] = round(i * s->ab);
136 s->lut[A][A][i] = round(i * s->aa);
142 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
144 AVFilterContext *ctx = inlink->dst;
145 ColorChannelMixerContext *s = ctx->priv;
146 AVFilterLink *outlink = ctx->outputs[0];
147 const uint8_t roffset = s->rgba_map[R];
148 const uint8_t goffset = s->rgba_map[G];
149 const uint8_t boffset = s->rgba_map[B];
150 const uint8_t aoffset = s->rgba_map[A];
151 const uint8_t *srcrow = in->data[0];
156 if (av_frame_is_writable(in)) {
159 out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
162 return AVERROR(ENOMEM);
164 av_frame_copy_props(out, in);
167 dstrow = out->data[0];
168 switch (outlink->format) {
169 case AV_PIX_FMT_BGR24:
170 case AV_PIX_FMT_RGB24:
171 for (i = 0; i < outlink->h; i++) {
172 const uint8_t *src = srcrow;
173 uint8_t *dst = dstrow;
175 for (j = 0; j < outlink->w * 3; j += 3) {
176 const uint8_t rin = src[j + roffset];
177 const uint8_t gin = src[j + goffset];
178 const uint8_t bin = src[j + boffset];
180 dst[j + roffset] = av_clip_uint8(s->lut[R][R][rin] +
183 dst[j + goffset] = av_clip_uint8(s->lut[G][R][rin] +
186 dst[j + boffset] = av_clip_uint8(s->lut[B][R][rin] +
191 srcrow += in->linesize[0];
192 dstrow += out->linesize[0];
195 case AV_PIX_FMT_0BGR:
196 case AV_PIX_FMT_0RGB:
197 case AV_PIX_FMT_BGR0:
198 case AV_PIX_FMT_RGB0:
199 for (i = 0; i < outlink->h; i++) {
200 const uint8_t *src = srcrow;
201 uint8_t *dst = dstrow;
203 for (j = 0; j < outlink->w * 4; j += 4) {
204 const uint8_t rin = src[j + roffset];
205 const uint8_t gin = src[j + goffset];
206 const uint8_t bin = src[j + boffset];
208 dst[j + roffset] = av_clip_uint8(s->lut[R][R][rin] +
211 dst[j + goffset] = av_clip_uint8(s->lut[G][R][rin] +
214 dst[j + boffset] = av_clip_uint8(s->lut[B][R][rin] +
218 dst[j + aoffset] = 0;
221 srcrow += in->linesize[0];
222 dstrow += out->linesize[0];
225 case AV_PIX_FMT_ABGR:
226 case AV_PIX_FMT_ARGB:
227 case AV_PIX_FMT_BGRA:
228 case AV_PIX_FMT_RGBA:
229 for (i = 0; i < outlink->h; i++) {
230 const uint8_t *src = srcrow;
231 uint8_t *dst = dstrow;
233 for (j = 0; j < outlink->w * 4; j += 4) {
234 const uint8_t rin = src[j + roffset];
235 const uint8_t gin = src[j + goffset];
236 const uint8_t bin = src[j + boffset];
237 const uint8_t ain = src[j + aoffset];
239 dst[j + roffset] = av_clip_uint8(s->lut[R][R][rin] +
243 dst[j + goffset] = av_clip_uint8(s->lut[G][R][rin] +
247 dst[j + boffset] = av_clip_uint8(s->lut[B][R][rin] +
251 dst[j + aoffset] = av_clip_uint8(s->lut[A][R][rin] +
257 srcrow += in->linesize[0];
258 dstrow += out->linesize[0];
261 case AV_PIX_FMT_BGR48:
262 case AV_PIX_FMT_RGB48:
263 for (i = 0; i < outlink->h; i++) {
264 const uint16_t *src = (const uint16_t *)srcrow;
265 uint16_t *dst = (uint16_t *)dstrow;
267 for (j = 0; j < outlink->w * 3; j += 3) {
268 const uint16_t rin = src[j + roffset];
269 const uint16_t gin = src[j + goffset];
270 const uint16_t bin = src[j + boffset];
272 dst[j + roffset] = av_clip_uint16(s->lut[R][R][rin] +
275 dst[j + goffset] = av_clip_uint16(s->lut[G][R][rin] +
278 dst[j + boffset] = av_clip_uint16(s->lut[B][R][rin] +
283 srcrow += in->linesize[0];
284 dstrow += out->linesize[0];
287 case AV_PIX_FMT_BGRA64:
288 case AV_PIX_FMT_RGBA64:
289 for (i = 0; i < outlink->h; i++) {
290 const uint16_t *src = (const uint16_t *)srcrow;
291 uint16_t *dst = (uint16_t *)dstrow;
293 for (j = 0; j < outlink->w * 4; j += 4) {
294 const uint16_t rin = src[j + roffset];
295 const uint16_t gin = src[j + goffset];
296 const uint16_t bin = src[j + boffset];
297 const uint16_t ain = src[j + aoffset];
299 dst[j + roffset] = av_clip_uint16(s->lut[R][R][rin] +
303 dst[j + goffset] = av_clip_uint16(s->lut[G][R][rin] +
307 dst[j + boffset] = av_clip_uint16(s->lut[B][R][rin] +
311 dst[j + aoffset] = av_clip_uint16(s->lut[A][R][rin] +
317 srcrow += in->linesize[0];
318 dstrow += out->linesize[0];
324 return ff_filter_frame(ctx->outputs[0], out);
327 static av_cold void uninit(AVFilterContext *ctx)
329 ColorChannelMixerContext *s = ctx->priv;
331 av_freep(&s->buffer);
334 static const AVFilterPad colorchannelmixer_inputs[] = {
337 .type = AVMEDIA_TYPE_VIDEO,
338 .filter_frame = filter_frame,
343 static const AVFilterPad colorchannelmixer_outputs[] = {
346 .type = AVMEDIA_TYPE_VIDEO,
347 .config_props = config_output,
352 AVFilter ff_vf_colorchannelmixer = {
353 .name = "colorchannelmixer",
354 .description = NULL_IF_CONFIG_SMALL("Adjust colors by mixing color channels."),
355 .priv_size = sizeof(ColorChannelMixerContext),
356 .priv_class = &colorchannelmixer_class,
358 .query_formats = query_formats,
359 .inputs = colorchannelmixer_inputs,
360 .outputs = colorchannelmixer_outputs,
361 .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC,