]> git.sesse.net Git - ffmpeg/blob - libavfilter/vf_geq.c
Merge remote-tracking branch 'qatar/master'
[ffmpeg] / libavfilter / vf_geq.c
1 /*
2  * Copyright (C) 2006 Michael Niedermayer <michaelni@gmx.at>
3  * Copyright (C) 2012 Clément Bœsch <ubitux@gmail.com>
4  *
5  * This file is part of FFmpeg.
6  *
7  * FFmpeg is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * FFmpeg is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License along
18  * with FFmpeg; if not, write to the Free Software Foundation, Inc.,
19  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20  */
21
22 /**
23  * @file
24  * Generic equation change filter
25  * Originally written by Michael Niedermayer for the MPlayer project, and
26  * ported by Clément Bœsch for FFmpeg.
27  */
28
29 #include "libavutil/avstring.h"
30 #include "libavutil/eval.h"
31 #include "libavutil/opt.h"
32 #include "libavutil/pixdesc.h"
33 #include "internal.h"
34
35 typedef struct {
36     const AVClass *class;
37     AVExpr *e[4];               ///< expressions for each plane
38     char *expr_str[4+3];        ///< expression strings for each plane
39     AVFrame *picref;            ///< current input buffer
40     int hsub, vsub;             ///< chroma subsampling
41     int planes;                 ///< number of planes
42     int is_rgb;
43 } GEQContext;
44
45 #define OFFSET(x) offsetof(GEQContext, x)
46 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
47
48 static const AVOption geq_options[] = {
49     { "lum_expr",   "set luminance expression",   OFFSET(expr_str[0]), AV_OPT_TYPE_STRING, {.str=NULL}, CHAR_MIN, CHAR_MAX, FLAGS },
50     { "cb_expr",    "set chroma blue expression", OFFSET(expr_str[1]), AV_OPT_TYPE_STRING, {.str=NULL}, CHAR_MIN, CHAR_MAX, FLAGS },
51     { "cr_expr",    "set chroma red expression",  OFFSET(expr_str[2]), AV_OPT_TYPE_STRING, {.str=NULL}, CHAR_MIN, CHAR_MAX, FLAGS },
52     { "alpha_expr", "set alpha expression",       OFFSET(expr_str[3]), AV_OPT_TYPE_STRING, {.str=NULL}, CHAR_MIN, CHAR_MAX, FLAGS },
53
54     { "r",          "set red expression",   OFFSET(expr_str[6]), AV_OPT_TYPE_STRING, {.str=NULL}, CHAR_MIN, CHAR_MAX, FLAGS },
55     { "g",          "set green expression", OFFSET(expr_str[4]), AV_OPT_TYPE_STRING, {.str=NULL}, CHAR_MIN, CHAR_MAX, FLAGS },
56     { "b",          "set blue expression",  OFFSET(expr_str[5]), AV_OPT_TYPE_STRING, {.str=NULL}, CHAR_MIN, CHAR_MAX, FLAGS },
57     {NULL},
58 };
59
60 AVFILTER_DEFINE_CLASS(geq);
61
62 static inline double getpix(void *priv, double x, double y, int plane)
63 {
64     int xi, yi;
65     GEQContext *geq = priv;
66     AVFrame *picref = geq->picref;
67     const uint8_t *src = picref->data[plane];
68     const int linesize = picref->linesize[plane];
69     const int w = picref->width  >> ((plane == 1 || plane == 2) ? geq->hsub : 0);
70     const int h = picref->height >> ((plane == 1 || plane == 2) ? geq->vsub : 0);
71
72     if (!src)
73         return 0;
74
75     xi = x = av_clipf(x, 0, w - 2);
76     yi = y = av_clipf(y, 0, h - 2);
77
78     x -= xi;
79     y -= yi;
80
81     return (1-y)*((1-x)*src[xi +  yi    * linesize] + x*src[xi + 1 +  yi    * linesize])
82           +   y *((1-x)*src[xi + (yi+1) * linesize] + x*src[xi + 1 + (yi+1) * linesize]);
83 }
84
85 //TODO: cubic interpolate
86 //TODO: keep the last few frames
87 static double lum(void *priv, double x, double y) { return getpix(priv, x, y, 0); }
88 static double  cb(void *priv, double x, double y) { return getpix(priv, x, y, 1); }
89 static double  cr(void *priv, double x, double y) { return getpix(priv, x, y, 2); }
90 static double alpha(void *priv, double x, double y) { return getpix(priv, x, y, 3); }
91
92 static const char *const var_names[] = {   "X",   "Y",   "W",   "H",   "N",   "SW",   "SH",   "T",        NULL };
93 enum                                   { VAR_X, VAR_Y, VAR_W, VAR_H, VAR_N, VAR_SW, VAR_SH, VAR_T, VAR_VARS_NB };
94
95 static av_cold int geq_init(AVFilterContext *ctx)
96 {
97     GEQContext *geq = ctx->priv;
98     int plane, ret = 0;
99
100     if (!geq->expr_str[0] && !geq->expr_str[4] && !geq->expr_str[5] && !geq->expr_str[6]) {
101         av_log(ctx, AV_LOG_ERROR, "A luminance or RGB expression is mandatory\n");
102         ret = AVERROR(EINVAL);
103         goto end;
104     }
105     geq->is_rgb = !geq->expr_str[0];
106
107     if ((geq->expr_str[0] || geq->expr_str[1] || geq->expr_str[2]) && (geq->expr_str[4] || geq->expr_str[5] || geq->expr_str[6])) {
108         av_log(ctx, AV_LOG_ERROR, "Either YCbCr or RGB but not both must be specified\n");
109         ret = AVERROR(EINVAL);
110         goto end;
111     }
112
113     if (!geq->expr_str[1] && !geq->expr_str[2]) {
114         /* No chroma at all: fallback on luma */
115         geq->expr_str[1] = av_strdup(geq->expr_str[0]);
116         geq->expr_str[2] = av_strdup(geq->expr_str[0]);
117     } else {
118         /* One chroma unspecified, fallback on the other */
119         if (!geq->expr_str[1]) geq->expr_str[1] = av_strdup(geq->expr_str[2]);
120         if (!geq->expr_str[2]) geq->expr_str[2] = av_strdup(geq->expr_str[1]);
121     }
122
123     if (!geq->expr_str[3])
124         geq->expr_str[3] = av_strdup("255");
125     if (!geq->expr_str[4])
126         geq->expr_str[4] = av_strdup("g(X,Y)");
127     if (!geq->expr_str[5])
128         geq->expr_str[5] = av_strdup("b(X,Y)");
129     if (!geq->expr_str[6])
130         geq->expr_str[6] = av_strdup("r(X,Y)");
131
132     if (geq->is_rgb ?
133             (!geq->expr_str[4] || !geq->expr_str[5] || !geq->expr_str[6])
134                     :
135             (!geq->expr_str[1] || !geq->expr_str[2] || !geq->expr_str[3])) {
136         ret = AVERROR(ENOMEM);
137         goto end;
138     }
139
140     for (plane = 0; plane < 4; plane++) {
141         static double (*p[])(void *, double, double) = { lum, cb, cr, alpha };
142         static const char *const func2_yuv_names[]    = { "lum", "cb", "cr", "alpha", "p", NULL };
143         static const char *const func2_rgb_names[]    = { "g", "b", "r", "alpha", "p", NULL };
144         const char *const *func2_names       = geq->is_rgb ? func2_rgb_names : func2_yuv_names;
145         double (*func2[])(void *, double, double) = { lum, cb, cr, alpha, p[plane], NULL };
146
147         ret = av_expr_parse(&geq->e[plane], geq->expr_str[plane < 3 && geq->is_rgb ? plane+4 : plane], var_names,
148                             NULL, NULL, func2_names, func2, 0, ctx);
149         if (ret < 0)
150             break;
151     }
152
153 end:
154     return ret;
155 }
156
157 static int geq_query_formats(AVFilterContext *ctx)
158 {
159     GEQContext *geq = ctx->priv;
160     static const enum PixelFormat yuv_pix_fmts[] = {
161         AV_PIX_FMT_YUV444P,  AV_PIX_FMT_YUV422P,  AV_PIX_FMT_YUV420P,
162         AV_PIX_FMT_YUV411P,  AV_PIX_FMT_YUV410P,  AV_PIX_FMT_YUV440P,
163         AV_PIX_FMT_YUVA444P, AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUVA420P,
164         AV_PIX_FMT_GRAY8,
165         AV_PIX_FMT_NONE
166     };
167     static const enum PixelFormat rgb_pix_fmts[] = {
168         AV_PIX_FMT_GBRP,
169         AV_PIX_FMT_NONE
170     };
171     if (geq->is_rgb) {
172         ff_set_common_formats(ctx, ff_make_format_list(rgb_pix_fmts));
173     } else
174         ff_set_common_formats(ctx, ff_make_format_list(yuv_pix_fmts));
175     return 0;
176 }
177
178 static int geq_config_props(AVFilterLink *inlink)
179 {
180     GEQContext *geq = inlink->dst->priv;
181     const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
182
183     geq->hsub = desc->log2_chroma_w;
184     geq->vsub = desc->log2_chroma_h;
185     geq->planes = desc->nb_components;
186     return 0;
187 }
188
189 static int geq_filter_frame(AVFilterLink *inlink, AVFrame *in)
190 {
191     int plane;
192     GEQContext *geq = inlink->dst->priv;
193     AVFilterLink *outlink = inlink->dst->outputs[0];
194     AVFrame *out;
195     double values[VAR_VARS_NB] = {
196         [VAR_N] = inlink->frame_count,
197         [VAR_T] = in->pts == AV_NOPTS_VALUE ? NAN : in->pts * av_q2d(inlink->time_base),
198     };
199
200     geq->picref = in;
201     out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
202     if (!out) {
203         av_frame_free(&in);
204         return AVERROR(ENOMEM);
205     }
206     av_frame_copy_props(out, in);
207
208     for (plane = 0; plane < geq->planes && out->data[plane]; plane++) {
209         int x, y;
210         uint8_t *dst = out->data[plane];
211         const int linesize = out->linesize[plane];
212         const int w = inlink->w >> ((plane == 1 || plane == 2) ? geq->hsub : 0);
213         const int h = inlink->h >> ((plane == 1 || plane == 2) ? geq->vsub : 0);
214
215         values[VAR_W]  = w;
216         values[VAR_H]  = h;
217         values[VAR_SW] = w / (double)inlink->w;
218         values[VAR_SH] = h / (double)inlink->h;
219
220         for (y = 0; y < h; y++) {
221             values[VAR_Y] = y;
222             for (x = 0; x < w; x++) {
223                 values[VAR_X] = x;
224                 dst[x] = av_expr_eval(geq->e[plane], values, geq);
225             }
226             dst += linesize;
227         }
228     }
229
230     av_frame_free(&geq->picref);
231     return ff_filter_frame(outlink, out);
232 }
233
234 static av_cold void geq_uninit(AVFilterContext *ctx)
235 {
236     int i;
237     GEQContext *geq = ctx->priv;
238
239     for (i = 0; i < FF_ARRAY_ELEMS(geq->e); i++)
240         av_expr_free(geq->e[i]);
241 }
242
243 static const AVFilterPad geq_inputs[] = {
244     {
245         .name         = "default",
246         .type         = AVMEDIA_TYPE_VIDEO,
247         .config_props = geq_config_props,
248         .filter_frame = geq_filter_frame,
249     },
250     { NULL }
251 };
252
253 static const AVFilterPad geq_outputs[] = {
254     {
255         .name = "default",
256         .type = AVMEDIA_TYPE_VIDEO,
257     },
258     { NULL }
259 };
260
261 AVFilter avfilter_vf_geq = {
262     .name          = "geq",
263     .description   = NULL_IF_CONFIG_SMALL("Apply generic equation to each pixel."),
264     .priv_size     = sizeof(GEQContext),
265     .init          = geq_init,
266     .uninit        = geq_uninit,
267     .query_formats = geq_query_formats,
268     .inputs        = geq_inputs,
269     .outputs       = geq_outputs,
270     .priv_class    = &geq_class,
271 };