2 * Copyright (c) 2015 Arwa Arif <arwaarif1994@gmail.com>
4 * This file is part of FFmpeg.
6 * FFmpeg is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU Lesser General Public License as published
8 * by the Free Software Foundation; either version 2.1 of the License,
9 * 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
23 * FFT domain filtering.
26 #include "libavfilter/internal.h"
27 #include "libavutil/common.h"
28 #include "libavutil/imgutils.h"
29 #include "libavutil/opt.h"
30 #include "libavutil/pixdesc.h"
31 #include "libavcodec/avfft.h"
32 #include "libavutil/eval.h"
42 typedef struct FFTFILTContext {
48 int planewidth[MAX_PLANES];
49 int planeheight[MAX_PLANES];
51 RDFTContext *hrdft[MAX_PLANES];
52 RDFTContext *vrdft[MAX_PLANES];
53 RDFTContext *ihrdft[MAX_PLANES];
54 RDFTContext *ivrdft[MAX_PLANES];
55 int rdft_hbits[MAX_PLANES];
56 int rdft_vbits[MAX_PLANES];
57 size_t rdft_hlen[MAX_PLANES];
58 size_t rdft_vlen[MAX_PLANES];
59 FFTSample *rdft_hdata[MAX_PLANES];
60 FFTSample *rdft_vdata[MAX_PLANES];
63 char *weight_str[MAX_PLANES];
64 AVExpr *weight_expr[MAX_PLANES];
65 double *weight[MAX_PLANES];
69 static const char *const var_names[] = { "X", "Y", "W", "H", "N", NULL };
70 enum { VAR_X, VAR_Y, VAR_W, VAR_H, VAR_N, VAR_VARS_NB };
74 #define OFFSET(x) offsetof(FFTFILTContext, x)
75 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
77 static const AVOption fftfilt_options[] = {
78 { "dc_Y", "adjust gain in Y plane", OFFSET(dc[Y]), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1000, FLAGS },
79 { "dc_U", "adjust gain in U plane", OFFSET(dc[U]), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1000, FLAGS },
80 { "dc_V", "adjust gain in V plane", OFFSET(dc[V]), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1000, FLAGS },
81 { "weight_Y", "set luminance expression in Y plane", OFFSET(weight_str[Y]), AV_OPT_TYPE_STRING, {.str = "1"}, CHAR_MIN, CHAR_MAX, FLAGS },
82 { "weight_U", "set chrominance expression in U plane", OFFSET(weight_str[U]), AV_OPT_TYPE_STRING, {.str = NULL}, CHAR_MIN, CHAR_MAX, FLAGS },
83 { "weight_V", "set chrominance expression in V plane", OFFSET(weight_str[V]), AV_OPT_TYPE_STRING, {.str = NULL}, CHAR_MIN, CHAR_MAX, FLAGS },
84 { "eval", "specify when to evaluate expressions", OFFSET(eval_mode), AV_OPT_TYPE_INT, {.i64 = EVAL_MODE_INIT}, 0, EVAL_MODE_NB-1, FLAGS, "eval" },
85 { "init", "eval expressions once during initialization", 0, AV_OPT_TYPE_CONST, {.i64=EVAL_MODE_INIT}, .flags = FLAGS, .unit = "eval" },
86 { "frame", "eval expressions per-frame", 0, AV_OPT_TYPE_CONST, {.i64=EVAL_MODE_FRAME}, .flags = FLAGS, .unit = "eval" },
90 AVFILTER_DEFINE_CLASS(fftfilt);
92 static inline double lum(void *priv, double x, double y, int plane)
94 FFTFILTContext *s = priv;
95 return s->rdft_vdata[plane][(int)x * s->rdft_vlen[plane] + (int)y];
98 static double weight_Y(void *priv, double x, double y) { return lum(priv, x, y, Y); }
99 static double weight_U(void *priv, double x, double y) { return lum(priv, x, y, U); }
100 static double weight_V(void *priv, double x, double y) { return lum(priv, x, y, V); }
102 static void copy_rev (FFTSample *dest, int w, int w2)
106 for (i = w; i < w + (w2-w)/2; i++)
107 dest[i] = dest[2*w - i - 1];
110 dest[i] = dest[w2 - i];
113 /*Horizontal pass - RDFT*/
114 static void rdft_horizontal(FFTFILTContext *s, AVFrame *in, int w, int h, int plane)
118 for (i = 0; i < h; i++) {
119 for (j = 0; j < w; j++)
120 s->rdft_hdata[plane][i * s->rdft_hlen[plane] + j] = *(in->data[plane] + in->linesize[plane] * i + j);
122 copy_rev(s->rdft_hdata[plane] + i * s->rdft_hlen[plane], w, s->rdft_hlen[plane]);
125 for (i = 0; i < h; i++)
126 av_rdft_calc(s->hrdft[plane], s->rdft_hdata[plane] + i * s->rdft_hlen[plane]);
129 /*Vertical pass - RDFT*/
130 static void rdft_vertical(FFTFILTContext *s, int h, int plane)
134 for (i = 0; i < s->rdft_hlen[plane]; i++) {
135 for (j = 0; j < h; j++)
136 s->rdft_vdata[plane][i * s->rdft_vlen[plane] + j] =
137 s->rdft_hdata[plane][j * s->rdft_hlen[plane] + i];
138 copy_rev(s->rdft_vdata[plane] + i * s->rdft_vlen[plane], h, s->rdft_vlen[plane]);
141 for (i = 0; i < s->rdft_hlen[plane]; i++)
142 av_rdft_calc(s->vrdft[plane], s->rdft_vdata[plane] + i * s->rdft_vlen[plane]);
144 /*Vertical pass - IRDFT*/
145 static void irdft_vertical(FFTFILTContext *s, int h, int plane)
149 for (i = 0; i < s->rdft_hlen[plane]; i++)
150 av_rdft_calc(s->ivrdft[plane], s->rdft_vdata[plane] + i * s->rdft_vlen[plane]);
152 for (i = 0; i < s->rdft_hlen[plane]; i++)
153 for (j = 0; j < h; j++)
154 s->rdft_hdata[plane][j * s->rdft_hlen[plane] + i] =
155 s->rdft_vdata[plane][i * s->rdft_vlen[plane] + j];
158 /*Horizontal pass - IRDFT*/
159 static void irdft_horizontal(FFTFILTContext *s, AVFrame *out, int w, int h, int plane)
163 for (i = 0; i < h; i++)
164 av_rdft_calc(s->ihrdft[plane], s->rdft_hdata[plane] + i * s->rdft_hlen[plane]);
166 for (i = 0; i < h; i++)
167 for (j = 0; j < w; j++)
168 *(out->data[plane] + out->linesize[plane] * i + j) = av_clip(s->rdft_hdata[plane][i
169 *s->rdft_hlen[plane] + j] * 4 /
170 (s->rdft_hlen[plane] *
171 s->rdft_vlen[plane]), 0, 255);
174 static av_cold int initialize(AVFilterContext *ctx)
176 FFTFILTContext *s = ctx->priv;
179 if (!s->dc[U] && !s->dc[V]) {
183 if (!s->dc[U]) s->dc[U] = s->dc[V];
184 if (!s->dc[V]) s->dc[V] = s->dc[U];
187 if (!s->weight_str[U] && !s->weight_str[V]) {
188 s->weight_str[U] = av_strdup(s->weight_str[Y]);
189 s->weight_str[V] = av_strdup(s->weight_str[Y]);
191 if (!s->weight_str[U]) s->weight_str[U] = av_strdup(s->weight_str[V]);
192 if (!s->weight_str[V]) s->weight_str[V] = av_strdup(s->weight_str[U]);
195 for (plane = 0; plane < 3; plane++) {
196 static double (*p[])(void *, double, double) = { weight_Y, weight_U, weight_V };
197 const char *const func2_names[] = {"weight_Y", "weight_U", "weight_V", NULL };
198 double (*func2[])(void *, double, double) = { weight_Y, weight_U, weight_V, p[plane], NULL };
200 ret = av_expr_parse(&s->weight_expr[plane], s->weight_str[plane], var_names,
201 NULL, NULL, func2_names, func2, 0, ctx);
208 static void do_eval(FFTFILTContext *s, AVFilterLink *inlink, int plane)
210 double values[VAR_VARS_NB];
213 values[VAR_N] = inlink->frame_count_out;
214 values[VAR_W] = s->planewidth[plane];
215 values[VAR_H] = s->planeheight[plane];
217 for (i = 0; i < s->rdft_hlen[plane]; i++) {
219 for (j = 0; j < s->rdft_vlen[plane]; j++) {
221 s->weight[plane][i * s->rdft_vlen[plane] + j] =
222 av_expr_eval(s->weight_expr[plane], values, s);
227 static int config_props(AVFilterLink *inlink)
229 FFTFILTContext *s = inlink->dst->priv;
230 const AVPixFmtDescriptor *desc;
231 int rdft_hbits, rdft_vbits, i, plane;
233 desc = av_pix_fmt_desc_get(inlink->format);
234 s->depth = desc->comp[0].depth;
235 s->planewidth[1] = s->planewidth[2] = AV_CEIL_RSHIFT(inlink->w, desc->log2_chroma_w);
236 s->planewidth[0] = s->planewidth[3] = inlink->w;
237 s->planeheight[1] = s->planeheight[2] = AV_CEIL_RSHIFT(inlink->h, desc->log2_chroma_h);
238 s->planeheight[0] = s->planeheight[3] = inlink->h;
240 s->nb_planes = av_pix_fmt_count_planes(inlink->format);
242 for (i = 0; i < desc->nb_components; i++) {
243 int w = s->planewidth[i];
244 int h = s->planeheight[i];
246 /* RDFT - Array initialization for Horizontal pass*/
247 for (rdft_hbits = 1; 1 << rdft_hbits < w*10/9; rdft_hbits++);
248 s->rdft_hbits[i] = rdft_hbits;
249 s->rdft_hlen[i] = 1 << rdft_hbits;
250 if (!(s->rdft_hdata[i] = av_malloc_array(h, s->rdft_hlen[i] * sizeof(FFTSample))))
251 return AVERROR(ENOMEM);
253 if (!(s->hrdft[i] = av_rdft_init(s->rdft_hbits[i], DFT_R2C)))
254 return AVERROR(ENOMEM);
255 if (!(s->ihrdft[i] = av_rdft_init(s->rdft_hbits[i], IDFT_C2R)))
256 return AVERROR(ENOMEM);
258 /* RDFT - Array initialization for Vertical pass*/
259 for (rdft_vbits = 1; 1 << rdft_vbits < h*10/9; rdft_vbits++);
260 s->rdft_vbits[i] = rdft_vbits;
261 s->rdft_vlen[i] = 1 << rdft_vbits;
262 if (!(s->rdft_vdata[i] = av_malloc_array(s->rdft_hlen[i], s->rdft_vlen[i] * sizeof(FFTSample))))
263 return AVERROR(ENOMEM);
265 if (!(s->vrdft[i] = av_rdft_init(s->rdft_vbits[i], DFT_R2C)))
266 return AVERROR(ENOMEM);
267 if (!(s->ivrdft[i] = av_rdft_init(s->rdft_vbits[i], IDFT_C2R)))
268 return AVERROR(ENOMEM);
271 /*Luminance value - Array initialization*/
272 for (plane = 0; plane < 3; plane++) {
273 if(!(s->weight[plane] = av_malloc_array(s->rdft_hlen[plane], s->rdft_vlen[plane] * sizeof(double))))
274 return AVERROR(ENOMEM);
276 if (s->eval_mode == EVAL_MODE_INIT)
277 do_eval(s, inlink, plane);
282 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
284 AVFilterContext *ctx = inlink->dst;
285 AVFilterLink *outlink = inlink->dst->outputs[0];
286 FFTFILTContext *s = ctx->priv;
290 out = ff_get_video_buffer(outlink, inlink->w, inlink->h);
293 return AVERROR(ENOMEM);
296 av_frame_copy_props(out, in);
298 for (plane = 0; plane < s->nb_planes; plane++) {
299 int w = s->planewidth[plane];
300 int h = s->planeheight[plane];
302 if (s->eval_mode == EVAL_MODE_FRAME)
303 do_eval(s, inlink, plane);
305 rdft_horizontal(s, in, w, h, plane);
306 rdft_vertical(s, h, plane);
308 /*Change user defined parameters*/
309 for (i = 0; i < s->rdft_hlen[plane]; i++)
310 for (j = 0; j < s->rdft_vlen[plane]; j++)
311 s->rdft_vdata[plane][i * s->rdft_vlen[plane] + j] *=
312 s->weight[plane][i * s->rdft_vlen[plane] + j];
314 s->rdft_vdata[plane][0] += s->rdft_hlen[plane] * s->rdft_vlen[plane] * s->dc[plane];
316 irdft_vertical(s, h, plane);
317 irdft_horizontal(s, out, w, h, plane);
321 return ff_filter_frame(outlink, out);
324 static av_cold void uninit(AVFilterContext *ctx)
326 FFTFILTContext *s = ctx->priv;
328 for (i = 0; i < MAX_PLANES; i++) {
329 av_free(s->rdft_hdata[i]);
330 av_free(s->rdft_vdata[i]);
331 av_expr_free(s->weight_expr[i]);
332 av_free(s->weight[i]);
333 av_rdft_end(s->hrdft[i]);
334 av_rdft_end(s->ihrdft[i]);
335 av_rdft_end(s->vrdft[i]);
336 av_rdft_end(s->ivrdft[i]);
340 static int query_formats(AVFilterContext *ctx)
342 static const enum AVPixelFormat pixel_fmts_fftfilt[] = {
344 AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUVJ444P,
345 AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUVJ420P,
346 AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUVJ422P,
350 AVFilterFormats *fmts_list = ff_make_format_list(pixel_fmts_fftfilt);
352 return AVERROR(ENOMEM);
353 return ff_set_common_formats(ctx, fmts_list);
356 static const AVFilterPad fftfilt_inputs[] = {
359 .type = AVMEDIA_TYPE_VIDEO,
360 .config_props = config_props,
361 .filter_frame = filter_frame,
366 static const AVFilterPad fftfilt_outputs[] = {
369 .type = AVMEDIA_TYPE_VIDEO,
374 AVFilter ff_vf_fftfilt = {
376 .description = NULL_IF_CONFIG_SMALL("Apply arbitrary expressions to pixels in frequency domain."),
377 .priv_size = sizeof(FFTFILTContext),
378 .priv_class = &fftfilt_class,
379 .inputs = fftfilt_inputs,
380 .outputs = fftfilt_outputs,
381 .query_formats = query_formats,
384 .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC,