]> git.sesse.net Git - ffmpeg/blob - libavfilter/vf_geq.c
avfilter/vf_geq: allow user to set interpolation method
[ffmpeg] / libavfilter / vf_geq.c
1 /*
2  * Copyright (C) 2006 Michael Niedermayer <michaelni@gmx.at>
3  * Copyright (C) 2012 Clément Bœsch <u pkh me>
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/avassert.h"
30 #include "libavutil/avstring.h"
31 #include "libavutil/eval.h"
32 #include "libavutil/opt.h"
33 #include "libavutil/pixdesc.h"
34 #include "internal.h"
35
36 enum InterpolationMethods {
37     INTERP_NEAREST,
38     INTERP_BILINEAR,
39     NB_INTERP
40 };
41
42 static const char *const var_names[] = {   "X",   "Y",   "W",   "H",   "N",   "SW",   "SH",   "T",        NULL };
43 enum                                   { VAR_X, VAR_Y, VAR_W, VAR_H, VAR_N, VAR_SW, VAR_SH, VAR_T, VAR_VARS_NB };
44
45 typedef struct GEQContext {
46     const AVClass *class;
47     AVExpr *e[4];               ///< expressions for each plane
48     char *expr_str[4+3];        ///< expression strings for each plane
49     AVFrame *picref;            ///< current input buffer
50     uint8_t *dst;               ///< reference pointer to the 8bits output
51     uint16_t *dst16;            ///< reference pointer to the 16bits output
52     double values[VAR_VARS_NB]; ///< expression values
53     int hsub, vsub;             ///< chroma subsampling
54     int planes;                 ///< number of planes
55     int interpolation;
56     int is_rgb;
57     int bps;
58 } GEQContext;
59
60 enum { Y = 0, U, V, A, G, B, R };
61
62 #define OFFSET(x) offsetof(GEQContext, x)
63 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
64
65 static const AVOption geq_options[] = {
66     { "lum_expr",   "set luminance expression",   OFFSET(expr_str[Y]), AV_OPT_TYPE_STRING, {.str=NULL}, CHAR_MIN, CHAR_MAX, FLAGS },
67     { "lum",        "set luminance expression",   OFFSET(expr_str[Y]), AV_OPT_TYPE_STRING, {.str=NULL}, CHAR_MIN, CHAR_MAX, FLAGS },
68     { "cb_expr",    "set chroma blue expression", OFFSET(expr_str[U]), AV_OPT_TYPE_STRING, {.str=NULL}, CHAR_MIN, CHAR_MAX, FLAGS },
69     { "cb",         "set chroma blue expression", OFFSET(expr_str[U]), AV_OPT_TYPE_STRING, {.str=NULL}, CHAR_MIN, CHAR_MAX, FLAGS },
70     { "cr_expr",    "set chroma red expression",  OFFSET(expr_str[V]), AV_OPT_TYPE_STRING, {.str=NULL}, CHAR_MIN, CHAR_MAX, FLAGS },
71     { "cr",         "set chroma red expression",  OFFSET(expr_str[V]), AV_OPT_TYPE_STRING, {.str=NULL}, CHAR_MIN, CHAR_MAX, FLAGS },
72     { "alpha_expr", "set alpha expression",       OFFSET(expr_str[A]), AV_OPT_TYPE_STRING, {.str=NULL}, CHAR_MIN, CHAR_MAX, FLAGS },
73     { "a",          "set alpha expression",       OFFSET(expr_str[A]), AV_OPT_TYPE_STRING, {.str=NULL}, CHAR_MIN, CHAR_MAX, FLAGS },
74     { "red_expr",   "set red expression",         OFFSET(expr_str[R]), AV_OPT_TYPE_STRING, {.str=NULL}, CHAR_MIN, CHAR_MAX, FLAGS },
75     { "r",          "set red expression",         OFFSET(expr_str[R]), AV_OPT_TYPE_STRING, {.str=NULL}, CHAR_MIN, CHAR_MAX, FLAGS },
76     { "green_expr", "set green expression",       OFFSET(expr_str[G]), AV_OPT_TYPE_STRING, {.str=NULL}, CHAR_MIN, CHAR_MAX, FLAGS },
77     { "g",          "set green expression",       OFFSET(expr_str[G]), AV_OPT_TYPE_STRING, {.str=NULL}, CHAR_MIN, CHAR_MAX, FLAGS },
78     { "blue_expr",  "set blue expression",        OFFSET(expr_str[B]), AV_OPT_TYPE_STRING, {.str=NULL}, CHAR_MIN, CHAR_MAX, FLAGS },
79     { "b",          "set blue expression",        OFFSET(expr_str[B]), AV_OPT_TYPE_STRING, {.str=NULL}, CHAR_MIN, CHAR_MAX, FLAGS },
80     { "interpolation","set interpolation method", OFFSET(interpolation), AV_OPT_TYPE_INT, {.i64=INTERP_BILINEAR}, 0, NB_INTERP-1, FLAGS, "interp" },
81     { "i",          "set interpolation method",   OFFSET(interpolation), AV_OPT_TYPE_INT, {.i64=INTERP_BILINEAR}, 0, NB_INTERP-1, FLAGS, "interp" },
82     { "nearest",    "nearest interpolation",      0,                   AV_OPT_TYPE_CONST, {.i64=INTERP_NEAREST},  0, 0, FLAGS, "interp" },
83     { "n",          "nearest interpolation",      0,                   AV_OPT_TYPE_CONST, {.i64=INTERP_NEAREST},  0, 0, FLAGS, "interp" },
84     { "bilinear",   "bilinear interpolation",     0,                   AV_OPT_TYPE_CONST, {.i64=INTERP_BILINEAR}, 0, 0, FLAGS, "interp" },
85     { "b",          "bilinear interpolation",     0,                   AV_OPT_TYPE_CONST, {.i64=INTERP_BILINEAR}, 0, 0, FLAGS, "interp" },
86     {NULL},
87 };
88
89 AVFILTER_DEFINE_CLASS(geq);
90
91 static inline double getpix(void *priv, double x, double y, int plane)
92 {
93     int xi, yi;
94     GEQContext *geq = priv;
95     AVFrame *picref = geq->picref;
96     const uint8_t *src = picref->data[plane];
97     int linesize = picref->linesize[plane];
98     const int w = (plane == 1 || plane == 2) ? AV_CEIL_RSHIFT(picref->width,  geq->hsub) : picref->width;
99     const int h = (plane == 1 || plane == 2) ? AV_CEIL_RSHIFT(picref->height, geq->vsub) : picref->height;
100
101     if (!src)
102         return 0;
103
104     if (geq->interpolation == INTERP_BILINEAR) {
105     xi = x = av_clipd(x, 0, w - 2);
106     yi = y = av_clipd(y, 0, h - 2);
107
108     x -= xi;
109     y -= yi;
110
111     if (geq->bps > 8) {
112         const uint16_t *src16 = (const uint16_t*)src;
113         linesize /= 2;
114
115         return (1-y)*((1-x)*src16[xi +  yi    * linesize] + x*src16[xi + 1 +  yi    * linesize])
116               +   y *((1-x)*src16[xi + (yi+1) * linesize] + x*src16[xi + 1 + (yi+1) * linesize]);
117     } else {
118         return (1-y)*((1-x)*src[xi +  yi    * linesize] + x*src[xi + 1 +  yi    * linesize])
119               +   y *((1-x)*src[xi + (yi+1) * linesize] + x*src[xi + 1 + (yi+1) * linesize]);
120     }
121     } else {
122         xi = av_clipd(x, 0, w - 1);
123         yi = av_clipd(y, 0, h - 1);
124
125         if (geq->bps > 8) {
126             const uint16_t *src16 = (const uint16_t*)src;
127             linesize /= 2;
128
129             return src16[xi + yi * linesize];
130         } else {
131             return src[xi + yi * linesize];
132         }
133     }
134 }
135
136 //TODO: cubic interpolate
137 //TODO: keep the last few frames
138 static double lum(void *priv, double x, double y) { return getpix(priv, x, y, 0); }
139 static double  cb(void *priv, double x, double y) { return getpix(priv, x, y, 1); }
140 static double  cr(void *priv, double x, double y) { return getpix(priv, x, y, 2); }
141 static double alpha(void *priv, double x, double y) { return getpix(priv, x, y, 3); }
142
143 static av_cold int geq_init(AVFilterContext *ctx)
144 {
145     GEQContext *geq = ctx->priv;
146     int plane, ret = 0;
147
148     if (!geq->expr_str[Y] && !geq->expr_str[G] && !geq->expr_str[B] && !geq->expr_str[R]) {
149         av_log(ctx, AV_LOG_ERROR, "A luminance or RGB expression is mandatory\n");
150         ret = AVERROR(EINVAL);
151         goto end;
152     }
153     geq->is_rgb = !geq->expr_str[Y];
154
155     if ((geq->expr_str[Y] || geq->expr_str[U] || geq->expr_str[V]) && (geq->expr_str[G] || geq->expr_str[B] || geq->expr_str[R])) {
156         av_log(ctx, AV_LOG_ERROR, "Either YCbCr or RGB but not both must be specified\n");
157         ret = AVERROR(EINVAL);
158         goto end;
159     }
160
161     if (!geq->expr_str[U] && !geq->expr_str[V]) {
162         /* No chroma at all: fallback on luma */
163         geq->expr_str[U] = av_strdup(geq->expr_str[Y]);
164         geq->expr_str[V] = av_strdup(geq->expr_str[Y]);
165     } else {
166         /* One chroma unspecified, fallback on the other */
167         if (!geq->expr_str[U]) geq->expr_str[U] = av_strdup(geq->expr_str[V]);
168         if (!geq->expr_str[V]) geq->expr_str[V] = av_strdup(geq->expr_str[U]);
169     }
170
171     if (!geq->expr_str[A]) {
172         char bps_string[8];
173         snprintf(bps_string, sizeof(bps_string), "%d", (1<<geq->bps) - 1);
174         geq->expr_str[A] = av_strdup(bps_string);
175     }
176     if (!geq->expr_str[G])
177         geq->expr_str[G] = av_strdup("g(X,Y)");
178     if (!geq->expr_str[B])
179         geq->expr_str[B] = av_strdup("b(X,Y)");
180     if (!geq->expr_str[R])
181         geq->expr_str[R] = av_strdup("r(X,Y)");
182
183     if (geq->is_rgb ?
184             (!geq->expr_str[G] || !geq->expr_str[B] || !geq->expr_str[R])
185                     :
186             (!geq->expr_str[U] || !geq->expr_str[V] || !geq->expr_str[A])) {
187         ret = AVERROR(ENOMEM);
188         goto end;
189     }
190
191     for (plane = 0; plane < 4; plane++) {
192         static double (*p[])(void *, double, double) = { lum, cb, cr, alpha };
193         static const char *const func2_yuv_names[]    = { "lum", "cb", "cr", "alpha", "p", NULL };
194         static const char *const func2_rgb_names[]    = { "g", "b", "r", "alpha", "p", NULL };
195         const char *const *func2_names       = geq->is_rgb ? func2_rgb_names : func2_yuv_names;
196         double (*func2[])(void *, double, double) = { lum, cb, cr, alpha, p[plane], NULL };
197
198         ret = av_expr_parse(&geq->e[plane], geq->expr_str[plane < 3 && geq->is_rgb ? plane+4 : plane], var_names,
199                             NULL, NULL, func2_names, func2, 0, ctx);
200         if (ret < 0)
201             break;
202     }
203
204 end:
205     return ret;
206 }
207
208 static int geq_query_formats(AVFilterContext *ctx)
209 {
210     GEQContext *geq = ctx->priv;
211     static const enum AVPixelFormat yuv_pix_fmts[] = {
212         AV_PIX_FMT_YUV444P,  AV_PIX_FMT_YUV422P,  AV_PIX_FMT_YUV420P,
213         AV_PIX_FMT_YUV411P,  AV_PIX_FMT_YUV410P,  AV_PIX_FMT_YUV440P,
214         AV_PIX_FMT_YUVA444P, AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUVA420P,
215         AV_PIX_FMT_GRAY8,
216         AV_PIX_FMT_YUV444P9,  AV_PIX_FMT_YUV422P9,  AV_PIX_FMT_YUV420P9,
217         AV_PIX_FMT_YUVA444P9, AV_PIX_FMT_YUVA422P9, AV_PIX_FMT_YUVA420P9,
218         AV_PIX_FMT_YUV444P10,  AV_PIX_FMT_YUV422P10,  AV_PIX_FMT_YUV420P10,
219         AV_PIX_FMT_YUV440P10,
220         AV_PIX_FMT_YUVA444P10, AV_PIX_FMT_YUVA422P10, AV_PIX_FMT_YUVA420P10,
221         AV_PIX_FMT_GRAY9, AV_PIX_FMT_GRAY10,
222         AV_PIX_FMT_YUV444P12,  AV_PIX_FMT_YUV422P12,  AV_PIX_FMT_YUV420P12,
223         AV_PIX_FMT_GRAY12, AV_PIX_FMT_GRAY14,
224         AV_PIX_FMT_YUV444P14,  AV_PIX_FMT_YUV422P14,  AV_PIX_FMT_YUV420P14,
225         AV_PIX_FMT_YUV444P16,  AV_PIX_FMT_YUV422P16,  AV_PIX_FMT_YUV420P16,
226         AV_PIX_FMT_YUVA444P16, AV_PIX_FMT_YUVA422P16, AV_PIX_FMT_YUVA420P16,
227         AV_PIX_FMT_GRAY16,
228         AV_PIX_FMT_NONE
229     };
230     static const enum AVPixelFormat rgb_pix_fmts[] = {
231         AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRAP,
232         AV_PIX_FMT_GBRP9,
233         AV_PIX_FMT_GBRP10, AV_PIX_FMT_GBRAP10,
234         AV_PIX_FMT_GBRP12, AV_PIX_FMT_GBRAP12,
235         AV_PIX_FMT_GBRP14,
236         AV_PIX_FMT_GBRP16, AV_PIX_FMT_GBRAP16,
237         AV_PIX_FMT_NONE
238     };
239     AVFilterFormats *fmts_list;
240
241     if (geq->is_rgb) {
242         fmts_list = ff_make_format_list(rgb_pix_fmts);
243     } else
244         fmts_list = ff_make_format_list(yuv_pix_fmts);
245     if (!fmts_list)
246         return AVERROR(ENOMEM);
247     return ff_set_common_formats(ctx, fmts_list);
248 }
249
250 static int geq_config_props(AVFilterLink *inlink)
251 {
252     GEQContext *geq = inlink->dst->priv;
253     const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
254
255     av_assert0(desc);
256
257     geq->hsub = desc->log2_chroma_w;
258     geq->vsub = desc->log2_chroma_h;
259     geq->bps = desc->comp[0].depth;
260     geq->planes = desc->nb_components;
261     return 0;
262 }
263
264 typedef struct ThreadData {
265     int height;
266     int width;
267     int plane;
268     int linesize;
269 } ThreadData;
270
271 static int slice_geq_filter(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
272 {
273     GEQContext *geq = ctx->priv;
274     ThreadData *td = arg;
275     const int height = td->height;
276     const int width = td->width;
277     const int plane = td->plane;
278     const int linesize = td->linesize;
279     const int slice_start = (height *  jobnr) / nb_jobs;
280     const int slice_end = (height * (jobnr+1)) / nb_jobs;
281     int x, y;
282     uint8_t *ptr;
283     uint16_t *ptr16;
284
285     double values[VAR_VARS_NB];
286     values[VAR_W] = geq->values[VAR_W];
287     values[VAR_H] = geq->values[VAR_H];
288     values[VAR_N] = geq->values[VAR_N];
289     values[VAR_SW] = geq->values[VAR_SW];
290     values[VAR_SH] = geq->values[VAR_SH];
291     values[VAR_T] = geq->values[VAR_T];
292
293     if (geq->bps == 8) {
294         for (y = slice_start; y < slice_end; y++) {
295             ptr = geq->dst + linesize * y;
296             values[VAR_Y] = y;
297
298             for (x = 0; x < width; x++) {
299                 values[VAR_X] = x;
300                 ptr[x] = av_expr_eval(geq->e[plane], values, geq);
301             }
302             ptr += linesize;
303         }
304     }
305     else {
306         for (y = slice_start; y < slice_end; y++) {
307             ptr16 = geq->dst16 + (linesize/2) * y;
308             values[VAR_Y] = y;
309             for (x = 0; x < width; x++) {
310                 values[VAR_X] = x;
311                 ptr16[x] = av_expr_eval(geq->e[plane], values, geq);
312             }
313         }
314     }
315
316     return 0;
317 }
318
319 static int geq_filter_frame(AVFilterLink *inlink, AVFrame *in)
320 {
321     int plane;
322     AVFilterContext *ctx = inlink->dst;
323     const int nb_threads = ff_filter_get_nb_threads(ctx);
324     GEQContext *geq = ctx->priv;
325     AVFilterLink *outlink = inlink->dst->outputs[0];
326     AVFrame *out;
327
328     geq->values[VAR_N] = inlink->frame_count_out,
329     geq->values[VAR_T] = in->pts == AV_NOPTS_VALUE ? NAN : in->pts * av_q2d(inlink->time_base),
330
331     geq->picref = in;
332     out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
333     if (!out) {
334         av_frame_free(&in);
335         return AVERROR(ENOMEM);
336     }
337     av_frame_copy_props(out, in);
338
339     for (plane = 0; plane < geq->planes && out->data[plane]; plane++) {
340         const int width = (plane == 1 || plane == 2) ? AV_CEIL_RSHIFT(inlink->w, geq->hsub) : inlink->w;
341         const int height = (plane == 1 || plane == 2) ? AV_CEIL_RSHIFT(inlink->h, geq->vsub) : inlink->h;
342         const int linesize = out->linesize[plane];
343         ThreadData td;
344
345         geq->dst = out->data[plane];
346         geq->dst16 = (uint16_t*)out->data[plane];
347
348         geq->values[VAR_W]  = width;
349         geq->values[VAR_H]  = height;
350         geq->values[VAR_SW] = width / (double)inlink->w;
351         geq->values[VAR_SH] = height / (double)inlink->h;
352
353         td.width = width;
354         td.height = height;
355         td.plane = plane;
356         td.linesize = linesize;
357
358         ctx->internal->execute(ctx, slice_geq_filter, &td, NULL, FFMIN(height, nb_threads));
359     }
360
361     av_frame_free(&geq->picref);
362     return ff_filter_frame(outlink, out);
363 }
364
365 static av_cold void geq_uninit(AVFilterContext *ctx)
366 {
367     int i;
368     GEQContext *geq = ctx->priv;
369
370     for (i = 0; i < FF_ARRAY_ELEMS(geq->e); i++)
371         av_expr_free(geq->e[i]);
372 }
373
374 static const AVFilterPad geq_inputs[] = {
375     {
376         .name         = "default",
377         .type         = AVMEDIA_TYPE_VIDEO,
378         .config_props = geq_config_props,
379         .filter_frame = geq_filter_frame,
380     },
381     { NULL }
382 };
383
384 static const AVFilterPad geq_outputs[] = {
385     {
386         .name = "default",
387         .type = AVMEDIA_TYPE_VIDEO,
388     },
389     { NULL }
390 };
391
392 AVFilter ff_vf_geq = {
393     .name          = "geq",
394     .description   = NULL_IF_CONFIG_SMALL("Apply generic equation to each pixel."),
395     .priv_size     = sizeof(GEQContext),
396     .init          = geq_init,
397     .uninit        = geq_uninit,
398     .query_formats = geq_query_formats,
399     .inputs        = geq_inputs,
400     .outputs       = geq_outputs,
401     .priv_class    = &geq_class,
402     .flags         = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC | AVFILTER_FLAG_SLICE_THREADS,
403 };