]> git.sesse.net Git - ffmpeg/blob - libavfilter/vf_geq.c
Merge commit '9d4da474f5f40b019cb4cb931c8499deee586174'
[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];          ///< expression strings for each plane
39     int framenum;               ///< frame counter
40     AVFilterBufferRef *picref;  ///< current input buffer
41     int hsub, vsub;             ///< chroma subsampling
42     int planes;                 ///< number of planes
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     {NULL},
54 };
55
56 AVFILTER_DEFINE_CLASS(geq);
57
58 static inline double getpix(void *priv, double x, double y, int plane)
59 {
60     int xi, yi;
61     GEQContext *geq = priv;
62     AVFilterBufferRef *picref = geq->picref;
63     const uint8_t *src = picref->data[plane];
64     const int linesize = picref->linesize[plane];
65     const int w = picref->video->w >> ((plane == 1 || plane == 2) ? geq->hsub : 0);
66     const int h = picref->video->h >> ((plane == 1 || plane == 2) ? geq->vsub : 0);
67
68     if (!src)
69         return 0;
70
71     xi = x = av_clipf(x, 0, w - 2);
72     yi = y = av_clipf(y, 0, h - 2);
73
74     x -= xi;
75     y -= yi;
76
77     return (1-y)*((1-x)*src[xi +  yi    * linesize] + x*src[xi + 1 +  yi    * linesize])
78           +   y *((1-x)*src[xi + (yi+1) * linesize] + x*src[xi + 1 + (yi+1) * linesize]);
79 }
80
81 //TODO: cubic interpolate
82 //TODO: keep the last few frames
83 static double lum(void *priv, double x, double y) { return getpix(priv, x, y, 0); }
84 static double  cb(void *priv, double x, double y) { return getpix(priv, x, y, 1); }
85 static double  cr(void *priv, double x, double y) { return getpix(priv, x, y, 2); }
86 static double alpha(void *priv, double x, double y) { return getpix(priv, x, y, 3); }
87
88 static const char *const var_names[] = {   "X",   "Y",   "W",   "H",   "N",   "SW",   "SH",   "T",        NULL };
89 enum                                   { VAR_X, VAR_Y, VAR_W, VAR_H, VAR_N, VAR_SW, VAR_SH, VAR_T, VAR_VARS_NB };
90
91 static av_cold int geq_init(AVFilterContext *ctx, const char *args)
92 {
93     GEQContext *geq = ctx->priv;
94     int plane, ret = 0;
95     static const char *shorthand[] = { "lum_expr", "cb_expr", "cr_expr", "alpha_expr", NULL };
96
97     geq->class = &geq_class;
98     av_opt_set_defaults(geq);
99
100     if ((ret = av_opt_set_from_string(geq, args, shorthand, "=", ":")) < 0)
101         return ret;
102
103     if (!geq->expr_str[0]) {
104         av_log(ctx, AV_LOG_ERROR, "Luminance expression is mandatory\n");
105         ret = AVERROR(EINVAL);
106         goto end;
107     }
108
109     if (!geq->expr_str[1] && !geq->expr_str[2]) {
110         /* No chroma at all: fallback on luma */
111         geq->expr_str[1] = av_strdup(geq->expr_str[0]);
112         geq->expr_str[2] = av_strdup(geq->expr_str[0]);
113     } else {
114         /* One chroma unspecified, fallback on the other */
115         if (!geq->expr_str[1]) geq->expr_str[1] = av_strdup(geq->expr_str[2]);
116         if (!geq->expr_str[2]) geq->expr_str[2] = av_strdup(geq->expr_str[1]);
117     }
118
119     if (!geq->expr_str[3])
120         geq->expr_str[3] = av_strdup("255");
121
122     if (!geq->expr_str[1] || !geq->expr_str[2] || !geq->expr_str[3]) {
123         ret = AVERROR(ENOMEM);
124         goto end;
125     }
126
127     for (plane = 0; plane < 4; plane++) {
128         static double (*p[])(void *, double, double) = { lum, cb, cr, alpha };
129         static const char *const func2_names[]    = { "lum", "cb", "cr", "alpha", "p", NULL };
130         double (*func2[])(void *, double, double) = { lum, cb, cr, alpha, p[plane], NULL };
131
132         ret = av_expr_parse(&geq->e[plane], geq->expr_str[plane], var_names,
133                             NULL, NULL, func2_names, func2, 0, ctx);
134         if (ret < 0)
135             break;
136     }
137
138 end:
139     return ret;
140 }
141
142 static int geq_query_formats(AVFilterContext *ctx)
143 {
144     static const enum PixelFormat pix_fmts[] = {
145         AV_PIX_FMT_YUV444P,  AV_PIX_FMT_YUV422P,  AV_PIX_FMT_YUV420P,
146         AV_PIX_FMT_YUV411P,  AV_PIX_FMT_YUV410P,  AV_PIX_FMT_YUV440P,
147         AV_PIX_FMT_YUVA444P, AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUVA420P,
148         AV_PIX_FMT_GRAY8,
149         AV_PIX_FMT_NONE
150     };
151     ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
152     return 0;
153 }
154
155 static int geq_config_props(AVFilterLink *inlink)
156 {
157     GEQContext *geq = inlink->dst->priv;
158     const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
159
160     geq->hsub = desc->log2_chroma_w;
161     geq->vsub = desc->log2_chroma_h;
162     geq->planes = desc->nb_components;
163     return 0;
164 }
165
166 static int geq_filter_frame(AVFilterLink *inlink, AVFilterBufferRef *in)
167 {
168     int plane;
169     GEQContext *geq = inlink->dst->priv;
170     AVFilterLink *outlink = inlink->dst->outputs[0];
171     AVFilterBufferRef *out;
172     double values[VAR_VARS_NB] = {
173         [VAR_N] = geq->framenum++,
174         [VAR_T] = in->pts == AV_NOPTS_VALUE ? NAN : in->pts * av_q2d(inlink->time_base),
175     };
176
177     geq->picref = in;
178     out = ff_get_video_buffer(outlink, AV_PERM_WRITE, outlink->w, outlink->h);
179     if (!out) {
180         avfilter_unref_bufferp(&in);
181         return AVERROR(ENOMEM);
182     }
183     avfilter_copy_buffer_ref_props(out, in);
184
185     for (plane = 0; plane < geq->planes && out->data[plane]; plane++) {
186         int x, y;
187         uint8_t *dst = out->data[plane];
188         const int linesize = out->linesize[plane];
189         const int w = inlink->w >> ((plane == 1 || plane == 2) ? geq->hsub : 0);
190         const int h = inlink->h >> ((plane == 1 || plane == 2) ? geq->vsub : 0);
191
192         values[VAR_W]  = w;
193         values[VAR_H]  = h;
194         values[VAR_SW] = w / (double)inlink->w;
195         values[VAR_SH] = h / (double)inlink->h;
196
197         for (y = 0; y < h; y++) {
198             values[VAR_Y] = y;
199             for (x = 0; x < w; x++) {
200                 values[VAR_X] = x;
201                 dst[x] = av_expr_eval(geq->e[plane], values, geq);
202             }
203             dst += linesize;
204         }
205     }
206
207     avfilter_unref_bufferp(&geq->picref);
208     return ff_filter_frame(outlink, out);
209 }
210
211 static av_cold void geq_uninit(AVFilterContext *ctx)
212 {
213     int i;
214     GEQContext *geq = ctx->priv;
215
216     for (i = 0; i < FF_ARRAY_ELEMS(geq->e); i++)
217         av_expr_free(geq->e[i]);
218     av_opt_free(geq);
219 }
220
221 static const AVFilterPad geq_inputs[] = {
222     {
223         .name         = "default",
224         .type         = AVMEDIA_TYPE_VIDEO,
225         .config_props = geq_config_props,
226         .filter_frame = geq_filter_frame,
227         .min_perms    = AV_PERM_READ,
228     },
229     { NULL }
230 };
231
232 static const AVFilterPad geq_outputs[] = {
233     {
234         .name = "default",
235         .type = AVMEDIA_TYPE_VIDEO,
236     },
237     { NULL }
238 };
239
240 AVFilter avfilter_vf_geq = {
241     .name          = "geq",
242     .description   = NULL_IF_CONFIG_SMALL("Apply generic equation to each pixel."),
243     .priv_size     = sizeof(GEQContext),
244     .init          = geq_init,
245     .uninit        = geq_uninit,
246     .query_formats = geq_query_formats,
247     .inputs        = geq_inputs,
248     .outputs       = geq_outputs,
249     .priv_class    = &geq_class,
250 };