]> git.sesse.net Git - ffmpeg/blob - libavfilter/vf_colorlevels.c
Merge commit '72025ac36c740f031d7e413041fdfe97087c83c4'
[ffmpeg] / libavfilter / vf_colorlevels.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 "libavutil/imgutils.h"
22 #include "libavutil/opt.h"
23 #include "libavutil/pixdesc.h"
24 #include "avfilter.h"
25 #include "drawutils.h"
26 #include "formats.h"
27 #include "internal.h"
28 #include "video.h"
29
30 #define R 0
31 #define G 1
32 #define B 2
33 #define A 3
34
35 typedef struct {
36     double in_min, in_max;
37     double out_min, out_max;
38 } Range;
39
40 typedef struct {
41     const AVClass *class;
42     Range range[4];
43     int nb_comp;
44     int bpp;
45     int step;
46     uint8_t rgba_map[4];
47     int linesize;
48 } ColorLevelsContext;
49
50 #define OFFSET(x) offsetof(ColorLevelsContext, x)
51 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
52 static const AVOption colorlevels_options[] = {
53     { "rimin", "set input red black point",    OFFSET(range[R].in_min),  AV_OPT_TYPE_DOUBLE, {.dbl=0}, -1, 1, FLAGS },
54     { "gimin", "set input green black point",  OFFSET(range[G].in_min),  AV_OPT_TYPE_DOUBLE, {.dbl=0}, -1, 1, FLAGS },
55     { "bimin", "set input blue black point",   OFFSET(range[B].in_min),  AV_OPT_TYPE_DOUBLE, {.dbl=0}, -1, 1, FLAGS },
56     { "aimin", "set input alpha black point",  OFFSET(range[A].in_min),  AV_OPT_TYPE_DOUBLE, {.dbl=0}, -1, 1, FLAGS },
57     { "rimax", "set input red white point",    OFFSET(range[R].in_max),  AV_OPT_TYPE_DOUBLE, {.dbl=1}, -1, 1, FLAGS },
58     { "gimax", "set input green white point",  OFFSET(range[G].in_max),  AV_OPT_TYPE_DOUBLE, {.dbl=1}, -1, 1, FLAGS },
59     { "bimax", "set input blue white point",   OFFSET(range[B].in_max),  AV_OPT_TYPE_DOUBLE, {.dbl=1}, -1, 1, FLAGS },
60     { "aimax", "set input alpha white point",  OFFSET(range[A].in_max),  AV_OPT_TYPE_DOUBLE, {.dbl=1}, -1, 1, FLAGS },
61     { "romin", "set output red black point",   OFFSET(range[R].out_min), AV_OPT_TYPE_DOUBLE, {.dbl=0},  0, 1, FLAGS },
62     { "gomin", "set output green black point", OFFSET(range[G].out_min), AV_OPT_TYPE_DOUBLE, {.dbl=0},  0, 1, FLAGS },
63     { "bomin", "set output blue black point",  OFFSET(range[B].out_min), AV_OPT_TYPE_DOUBLE, {.dbl=0},  0, 1, FLAGS },
64     { "aomin", "set output alpha black point", OFFSET(range[A].out_min), AV_OPT_TYPE_DOUBLE, {.dbl=0},  0, 1, FLAGS },
65     { "romax", "set output red white point",   OFFSET(range[R].out_max), AV_OPT_TYPE_DOUBLE, {.dbl=1},  0, 1, FLAGS },
66     { "gomax", "set output green white point", OFFSET(range[G].out_max), AV_OPT_TYPE_DOUBLE, {.dbl=1},  0, 1, FLAGS },
67     { "bomax", "set output blue white point",  OFFSET(range[B].out_max), AV_OPT_TYPE_DOUBLE, {.dbl=1},  0, 1, FLAGS },
68     { "aomax", "set output alpha white point", OFFSET(range[A].out_max), AV_OPT_TYPE_DOUBLE, {.dbl=1},  0, 1, FLAGS },
69     { NULL }
70 };
71
72 AVFILTER_DEFINE_CLASS(colorlevels);
73
74 static int query_formats(AVFilterContext *ctx)
75 {
76     static const enum AVPixelFormat pix_fmts[] = {
77         AV_PIX_FMT_0RGB,  AV_PIX_FMT_0BGR,
78         AV_PIX_FMT_ARGB,  AV_PIX_FMT_ABGR,
79         AV_PIX_FMT_RGB0,  AV_PIX_FMT_BGR0,
80         AV_PIX_FMT_RGB24, AV_PIX_FMT_BGR24,
81         AV_PIX_FMT_RGB48, AV_PIX_FMT_BGR48,
82         AV_PIX_FMT_RGBA64, AV_PIX_FMT_BGRA64,
83         AV_PIX_FMT_RGBA,  AV_PIX_FMT_BGRA,
84         AV_PIX_FMT_NONE
85     };
86
87     ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
88     return 0;
89 }
90
91 static int config_input(AVFilterLink *inlink)
92 {
93     AVFilterContext *ctx = inlink->dst;
94     ColorLevelsContext *s = ctx->priv;
95     const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
96
97     s->nb_comp = desc->nb_components;
98     s->bpp = (desc->comp[0].depth_minus1 + 1) >> 3;
99     s->step = (av_get_padded_bits_per_pixel(desc) >> 3) / s->bpp;
100     s->linesize = inlink->w * s->step;
101     ff_fill_rgba_map(s->rgba_map, inlink->format);
102
103     return 0;
104 }
105
106 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
107 {
108     AVFilterContext *ctx = inlink->dst;
109     ColorLevelsContext *s = ctx->priv;
110     AVFilterLink *outlink = ctx->outputs[0];
111     const int step = s->step;
112     AVFrame *out;
113     int x, y, i;
114
115     if (av_frame_is_writable(in)) {
116         out = in;
117     } else {
118         out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
119         if (!out) {
120             av_frame_free(&in);
121             return AVERROR(ENOMEM);
122         }
123         av_frame_copy_props(out, in);
124     }
125
126     switch (s->bpp) {
127     case 1:
128         for (i = 0; i < s->nb_comp; i++) {
129             Range *r = &s->range[i];
130             const uint8_t offset = s->rgba_map[i];
131             const uint8_t *srcrow = in->data[0];
132             uint8_t *dstrow = out->data[0];
133             int imin = round(r->in_min  * UINT8_MAX);
134             int imax = round(r->in_max  * UINT8_MAX);
135             int omin = round(r->out_min * UINT8_MAX);
136             int omax = round(r->out_max * UINT8_MAX);
137             double coeff;
138
139             if (imin < 0) {
140                 imin = UINT8_MAX;
141                 for (y = 0; y < inlink->h; y++) {
142                     const uint8_t *src = srcrow;
143
144                     for (x = 0; x < s->linesize; x += step)
145                         imin = FFMIN(imin, src[x + offset]);
146                     srcrow += in->linesize[0];
147                 }
148             }
149             if (imax < 0) {
150                 srcrow = in->data[0];
151                 imax = 0;
152                 for (y = 0; y < inlink->h; y++) {
153                     const uint8_t *src = srcrow;
154
155                     for (x = 0; x < s->linesize; x += step)
156                         imax = FFMAX(imax, src[x + offset]);
157                     srcrow += in->linesize[0];
158                 }
159             }
160
161             srcrow = in->data[0];
162             coeff = (omax - omin) / (double)(imax - imin);
163             for (y = 0; y < inlink->h; y++) {
164                 const uint8_t *src = srcrow;
165                 uint8_t *dst = dstrow;
166
167                 for (x = 0; x < s->linesize; x += step)
168                     dst[x + offset] = av_clip_uint8((src[x + offset] - imin) * coeff + omin);
169                 dstrow += out->linesize[0];
170                 srcrow += in->linesize[0];
171             }
172         }
173         break;
174     case 2:
175         for (i = 0; i < s->nb_comp; i++) {
176             Range *r = &s->range[i];
177             const uint8_t offset = s->rgba_map[i];
178             const uint8_t *srcrow = in->data[0];
179             uint8_t *dstrow = out->data[0];
180             int imin = round(r->in_min  * UINT16_MAX);
181             int imax = round(r->in_max  * UINT16_MAX);
182             int omin = round(r->out_min * UINT16_MAX);
183             int omax = round(r->out_max * UINT16_MAX);
184             double coeff;
185
186             if (imin < 0) {
187                 imin = UINT16_MAX;
188                 for (y = 0; y < inlink->h; y++) {
189                     const uint16_t *src = (const uint16_t *)srcrow;
190
191                     for (x = 0; x < s->linesize; x += step)
192                         imin = FFMIN(imin, src[x + offset]);
193                     srcrow += in->linesize[0];
194                 }
195             }
196             if (imax < 0) {
197                 srcrow = in->data[0];
198                 imax = 0;
199                 for (y = 0; y < inlink->h; y++) {
200                     const uint16_t *src = (const uint16_t *)srcrow;
201
202                     for (x = 0; x < s->linesize; x += step)
203                         imax = FFMAX(imax, src[x + offset]);
204                     srcrow += in->linesize[0];
205                 }
206             }
207
208             srcrow = in->data[0];
209             coeff = (omax - omin) / (double)(imax - imin);
210             for (y = 0; y < inlink->h; y++) {
211                 const uint16_t *src = (const uint16_t*)srcrow;
212                 uint16_t *dst = (uint16_t *)dstrow;
213
214                 for (x = 0; x < s->linesize; x += step)
215                     dst[x + offset] = av_clip_uint16((src[x + offset] - imin) * coeff + omin);
216                 dstrow += out->linesize[0];
217                 srcrow += in->linesize[0];
218             }
219         }
220     }
221
222     if (in != out)
223         av_frame_free(&in);
224     return ff_filter_frame(outlink, out);
225 }
226
227 static const AVFilterPad colorlevels_inputs[] = {
228     {
229         .name         = "default",
230         .type         = AVMEDIA_TYPE_VIDEO,
231         .filter_frame = filter_frame,
232         .config_props = config_input,
233     },
234     { NULL }
235 };
236
237 static const AVFilterPad colorlevels_outputs[] = {
238     {
239         .name = "default",
240         .type = AVMEDIA_TYPE_VIDEO,
241     },
242     { NULL }
243 };
244
245 AVFilter ff_vf_colorlevels = {
246     .name          = "colorlevels",
247     .description   = NULL_IF_CONFIG_SMALL("Adjust the color levels."),
248     .priv_size     = sizeof(ColorLevelsContext),
249     .priv_class    = &colorlevels_class,
250     .query_formats = query_formats,
251     .inputs        = colorlevels_inputs,
252     .outputs       = colorlevels_outputs,
253     .flags         = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC,
254 };