]> git.sesse.net Git - ffmpeg/blob - libavfilter/vf_deflicker.c
Merge commit '8dfba25ce89b62c80ba83e2116d549176c376144'
[ffmpeg] / libavfilter / vf_deflicker.c
1 /*
2  * Copyright (c) 2017 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 "libavutil/qsort.h"
25 #include "avfilter.h"
26
27 #define FF_BUFQUEUE_SIZE 129
28 #include "bufferqueue.h"
29
30 #include "formats.h"
31 #include "internal.h"
32 #include "video.h"
33
34 #define SIZE FF_BUFQUEUE_SIZE
35
36 enum smooth_mode {
37     ARITHMETIC_MEAN,
38     GEOMETRIC_MEAN,
39     HARMONIC_MEAN,
40     QUADRATIC_MEAN,
41     CUBIC_MEAN,
42     POWER_MEAN,
43     MEDIAN,
44     NB_SMOOTH_MODE,
45 };
46
47 typedef struct DeflickerContext {
48     const AVClass *class;
49
50     int size;
51     int mode;
52
53     int eof;
54     int depth;
55     int nb_planes;
56     int planewidth[4];
57     int planeheight[4];
58
59     uint64_t *histogram;
60     float luminance[SIZE];
61     float sorted[SIZE];
62
63     struct FFBufQueue q;
64     int available;
65
66     void (*get_factor)(AVFilterContext *ctx, float *f);
67     float (*calc_avgy)(AVFilterContext *ctx, AVFrame *in);
68     int (*deflicker)(AVFilterContext *ctx, const uint8_t *src, ptrdiff_t src_linesize,
69                      uint8_t *dst, ptrdiff_t dst_linesize, int w, int h, float f);
70 } DeflickerContext;
71
72 #define OFFSET(x) offsetof(DeflickerContext, x)
73 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
74
75 static const AVOption deflicker_options[] = {
76     { "size",  "set how many frames to use",  OFFSET(size), AV_OPT_TYPE_INT, {.i64=5}, 2, SIZE, FLAGS },
77     { "s",     "set how many frames to use",  OFFSET(size), AV_OPT_TYPE_INT, {.i64=5}, 2, SIZE, FLAGS },
78     { "mode",  "set how to smooth luminance", OFFSET(mode), AV_OPT_TYPE_INT, {.i64=0}, 0, NB_SMOOTH_MODE-1, FLAGS, "mode" },
79     { "m",     "set how to smooth luminance", OFFSET(mode), AV_OPT_TYPE_INT, {.i64=0}, 0, NB_SMOOTH_MODE-1, FLAGS, "mode" },
80         { "am",      "arithmetic mean", 0, AV_OPT_TYPE_CONST, {.i64=ARITHMETIC_MEAN},  0, 0, FLAGS, "mode" },
81         { "gm",      "geometric mean",  0, AV_OPT_TYPE_CONST, {.i64=GEOMETRIC_MEAN},   0, 0, FLAGS, "mode" },
82         { "hm",      "harmonic mean",   0, AV_OPT_TYPE_CONST, {.i64=HARMONIC_MEAN},    0, 0, FLAGS, "mode" },
83         { "qm",      "quadratic mean",  0, AV_OPT_TYPE_CONST, {.i64=QUADRATIC_MEAN},   0, 0, FLAGS, "mode" },
84         { "cm",      "cubic mean",      0, AV_OPT_TYPE_CONST, {.i64=CUBIC_MEAN},       0, 0, FLAGS, "mode" },
85         { "pm",      "power mean",      0, AV_OPT_TYPE_CONST, {.i64=POWER_MEAN},       0, 0, FLAGS, "mode" },
86         { "median",  "median",          0, AV_OPT_TYPE_CONST, {.i64=MEDIAN},           0, 0, FLAGS, "mode" },
87     { NULL }
88 };
89
90 AVFILTER_DEFINE_CLASS(deflicker);
91
92 static int query_formats(AVFilterContext *ctx)
93 {
94     static const enum AVPixelFormat pixel_fmts[] = {
95         AV_PIX_FMT_GRAY8, AV_PIX_FMT_GRAY10,
96         AV_PIX_FMT_GRAY12, AV_PIX_FMT_GRAY16,
97         AV_PIX_FMT_YUV410P, AV_PIX_FMT_YUV411P,
98         AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV422P,
99         AV_PIX_FMT_YUV440P, AV_PIX_FMT_YUV444P,
100         AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ422P,
101         AV_PIX_FMT_YUVJ440P, AV_PIX_FMT_YUVJ444P,
102         AV_PIX_FMT_YUVJ411P,
103         AV_PIX_FMT_YUV420P9, AV_PIX_FMT_YUV422P9, AV_PIX_FMT_YUV444P9,
104         AV_PIX_FMT_YUV420P10, AV_PIX_FMT_YUV422P10, AV_PIX_FMT_YUV444P10,
105         AV_PIX_FMT_YUV440P10,
106         AV_PIX_FMT_YUV444P12, AV_PIX_FMT_YUV422P12, AV_PIX_FMT_YUV420P12,
107         AV_PIX_FMT_YUV440P12,
108         AV_PIX_FMT_YUV444P14, AV_PIX_FMT_YUV422P14, AV_PIX_FMT_YUV420P14,
109         AV_PIX_FMT_YUV420P16, AV_PIX_FMT_YUV422P16, AV_PIX_FMT_YUV444P16,
110         AV_PIX_FMT_NONE
111     };
112     AVFilterFormats *formats = ff_make_format_list(pixel_fmts);
113     if (!formats)
114         return AVERROR(ENOMEM);
115     return ff_set_common_formats(ctx, formats);
116 }
117
118 static int deflicker8(AVFilterContext *ctx,
119                       const uint8_t *src, ptrdiff_t src_linesize,
120                       uint8_t *dst, ptrdiff_t dst_linesize,
121                       int w, int h, float f)
122 {
123     int x, y;
124
125     for (y = 0; y < h; y++) {
126         for (x = 0; x < w; x++) {
127             dst[x] = av_clip_uint8(src[x] * f);
128         }
129
130         dst += dst_linesize;
131         src += src_linesize;
132     }
133
134     return 0;
135 }
136
137 static int deflicker16(AVFilterContext *ctx,
138                        const uint8_t *ssrc, ptrdiff_t src_linesize,
139                        uint8_t *ddst, ptrdiff_t dst_linesize,
140                        int w, int h, float f)
141 {
142     DeflickerContext *s = ctx->priv;
143     const uint16_t *src = (const uint16_t *)ssrc;
144     uint16_t *dst = (uint16_t *)ddst;
145     const int max = (1 << s->depth) - 1;
146     int x, y;
147
148     for (y = 0; y < h; y++) {
149         for (x = 0; x < w; x++) {
150             dst[x] = av_clip(src[x] * f, 0, max);
151         }
152
153         dst += dst_linesize / 2;
154         src += src_linesize / 2;
155     }
156
157     return 0;
158 }
159
160 static float calc_avgy8(AVFilterContext *ctx, AVFrame *in)
161 {
162     DeflickerContext *s = ctx->priv;
163     const uint8_t *src = in->data[0];
164     int64_t sum = 0;
165     int y, x;
166
167     memset(s->histogram, 0, (1 << s->depth) * sizeof(*s->histogram));
168
169     for (y = 0; y < s->planeheight[0]; y++) {
170         for (x = 0; x < s->planewidth[0]; x++) {
171             s->histogram[src[x]]++;
172         }
173         src += in->linesize[0];
174     }
175
176     for (y = 0; y < 1 << s->depth; y++) {
177         sum += s->histogram[y] * y;
178     }
179
180     return 1.0f * sum / (s->planeheight[0] * s->planewidth[0]);
181 }
182
183 static float calc_avgy16(AVFilterContext *ctx, AVFrame *in)
184 {
185     DeflickerContext *s = ctx->priv;
186     const uint16_t *src = (const uint16_t *)in->data[0];
187     int64_t sum = 0;
188     int y, x;
189
190     memset(s->histogram, 0, (1 << s->depth) * sizeof(*s->histogram));
191
192     for (y = 0; y < s->planeheight[0]; y++) {
193         for (x = 0; x < s->planewidth[0]; x++) {
194             s->histogram[src[x]]++;
195         }
196         src += in->linesize[0] / 2;
197     }
198
199     for (y = 0; y < 1 << s->depth; y++) {
200         sum += s->histogram[y] * y;
201     }
202
203     return 1.0f * sum / (s->planeheight[0] * s->planewidth[0]);
204 }
205
206 static void get_am_factor(AVFilterContext *ctx, float *f)
207 {
208     DeflickerContext *s = ctx->priv;
209     int y;
210
211     *f = 0.0f;
212
213     for (y = 0; y < s->size; y++) {
214         *f += s->luminance[y];
215     }
216
217     *f /= s->size;
218     *f /= s->luminance[0];
219 }
220
221 static void get_gm_factor(AVFilterContext *ctx, float *f)
222 {
223     DeflickerContext *s = ctx->priv;
224     int y;
225
226     *f = 1;
227
228     for (y = 0; y < s->size; y++) {
229         *f *= s->luminance[y];
230     }
231
232     *f = pow(*f, 1.0f / s->size);
233     *f /= s->luminance[0];
234 }
235
236 static void get_hm_factor(AVFilterContext *ctx, float *f)
237 {
238     DeflickerContext *s = ctx->priv;
239     int y;
240
241     *f = 0.0f;
242
243     for (y = 0; y < s->size; y++) {
244         *f += 1.0f / s->luminance[y];
245     }
246
247     *f = s->size / *f;
248     *f /= s->luminance[0];
249 }
250
251 static void get_qm_factor(AVFilterContext *ctx, float *f)
252 {
253     DeflickerContext *s = ctx->priv;
254     int y;
255
256     *f = 0.0f;
257
258     for (y = 0; y < s->size; y++) {
259         *f += s->luminance[y] * s->luminance[y];
260     }
261
262     *f /= s->size;
263     *f  = sqrtf(*f);
264     *f /= s->luminance[0];
265 }
266
267 static void get_cm_factor(AVFilterContext *ctx, float *f)
268 {
269     DeflickerContext *s = ctx->priv;
270     int y;
271
272     *f = 0.0f;
273
274     for (y = 0; y < s->size; y++) {
275         *f += s->luminance[y] * s->luminance[y] * s->luminance[y];
276     }
277
278     *f /= s->size;
279     *f  = cbrtf(*f);
280     *f /= s->luminance[0];
281 }
282
283 static void get_pm_factor(AVFilterContext *ctx, float *f)
284 {
285     DeflickerContext *s = ctx->priv;
286     int y;
287
288     *f = 0.0f;
289
290     for (y = 0; y < s->size; y++) {
291         *f += powf(s->luminance[y], s->size);
292     }
293
294     *f /= s->size;
295     *f  = powf(*f, 1.0f / s->size);
296     *f /= s->luminance[0];
297 }
298
299 static int comparef(const void *a, const void *b)
300 {
301     const float *aa = a, *bb = b;
302     return round(aa - bb);
303 }
304
305 static void get_median_factor(AVFilterContext *ctx, float *f)
306 {
307     DeflickerContext *s = ctx->priv;
308
309     memcpy(s->sorted, s->luminance, sizeof(s->sorted));
310     AV_QSORT(s->sorted, s->size, float, comparef);
311
312     *f = s->sorted[s->size >> 1] / s->luminance[0];
313 }
314
315 static int config_input(AVFilterLink *inlink)
316 {
317     const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
318     AVFilterContext *ctx = inlink->dst;
319     DeflickerContext *s = ctx->priv;
320
321     s->nb_planes = desc->nb_components;
322
323     s->planeheight[1] = s->planeheight[2] = AV_CEIL_RSHIFT(inlink->h, desc->log2_chroma_h);
324     s->planeheight[0] = s->planeheight[3] = inlink->h;
325     s->planewidth[1]  = s->planewidth[2]  = AV_CEIL_RSHIFT(inlink->w, desc->log2_chroma_w);
326     s->planewidth[0]  = s->planewidth[3]  = inlink->w;
327
328     s->depth = desc->comp[0].depth;
329     if (s->depth == 8) {
330         s->deflicker = deflicker8;
331         s->calc_avgy = calc_avgy8;
332     } else {
333         s->deflicker = deflicker16;
334         s->calc_avgy = calc_avgy16;
335     }
336
337     s->histogram = av_calloc(1 << s->depth, sizeof(*s->histogram));
338     if (!s->histogram)
339         return AVERROR(ENOMEM);
340
341     switch (s->mode) {
342     case MEDIAN:          s->get_factor = get_median_factor; break;
343     case ARITHMETIC_MEAN: s->get_factor = get_am_factor;     break;
344     case GEOMETRIC_MEAN:  s->get_factor = get_gm_factor;     break;
345     case HARMONIC_MEAN:   s->get_factor = get_hm_factor;     break;
346     case QUADRATIC_MEAN:  s->get_factor = get_qm_factor;     break;
347     case CUBIC_MEAN:      s->get_factor = get_cm_factor;     break;
348     case POWER_MEAN:      s->get_factor = get_pm_factor;     break;
349     }
350
351     return 0;
352 }
353
354 static int filter_frame(AVFilterLink *inlink, AVFrame *buf)
355 {
356     AVFilterContext *ctx = inlink->dst;
357     AVFilterLink *outlink = ctx->outputs[0];
358     DeflickerContext *s = ctx->priv;
359     AVDictionary **metadata;
360     AVFrame *out, *in;
361     float f;
362     int y;
363
364     if (s->q.available < s->size && !s->eof) {
365         s->luminance[s->available] = s->calc_avgy(ctx, buf);
366         ff_bufqueue_add(ctx, &s->q, buf);
367         s->available++;
368         return 0;
369     }
370
371     in = ff_bufqueue_peek(&s->q, 0);
372
373     out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
374     if (!out) {
375         av_frame_free(&buf);
376         return AVERROR(ENOMEM);
377     }
378
379     s->get_factor(ctx, &f);
380     s->deflicker(ctx, in->data[0], in->linesize[0], out->data[0], out->linesize[0],
381                  outlink->w, outlink->h, f);
382     for (y = 1; y < s->nb_planes; y++) {
383         av_image_copy_plane(out->data[y], out->linesize[y],
384                             in->data[y], in->linesize[y],
385                             s->planewidth[y] * (1 + (s->depth > 8)), s->planeheight[y]);
386     }
387
388     av_frame_copy_props(out, in);
389     metadata = &out->metadata;
390     if (metadata) {
391         uint8_t value[128];
392
393         snprintf(value, sizeof(value), "%f", s->luminance[0]);
394         av_dict_set(metadata, "lavfi.deflicker.luminance", value, 0);
395
396         snprintf(value, sizeof(value), "%f", s->luminance[0] * f);
397         av_dict_set(metadata, "lavfi.deflicker.new_luminance", value, 0);
398
399         snprintf(value, sizeof(value), "%f", f - 1.0f);
400         av_dict_set(metadata, "lavfi.deflicker.relative_change", value, 0);
401     }
402
403     in = ff_bufqueue_get(&s->q);
404     av_frame_free(&in);
405     memmove(&s->luminance[0], &s->luminance[1], sizeof(*s->luminance) * (s->size - 1));
406     s->luminance[s->available - 1] = s->calc_avgy(ctx, buf);
407     ff_bufqueue_add(ctx, &s->q, buf);
408
409     return ff_filter_frame(outlink, out);
410 }
411
412 static int request_frame(AVFilterLink *outlink)
413 {
414     AVFilterContext *ctx = outlink->src;
415     DeflickerContext *s = ctx->priv;
416     int ret;
417
418     ret = ff_request_frame(ctx->inputs[0]);
419     if (ret == AVERROR_EOF && s->available > 0) {
420         AVFrame *buf = av_frame_clone(ff_bufqueue_peek(&s->q, s->size - 1));
421         if (!buf)
422             return AVERROR(ENOMEM);
423
424         s->eof = 1;
425         ret = filter_frame(ctx->inputs[0], buf);
426         s->available--;
427     }
428
429     return ret;
430 }
431
432 static av_cold void uninit(AVFilterContext *ctx)
433 {
434     DeflickerContext *s = ctx->priv;
435
436     ff_bufqueue_discard_all(&s->q);
437     av_freep(&s->histogram);
438 }
439
440 static const AVFilterPad inputs[] = {
441     {
442         .name         = "default",
443         .type         = AVMEDIA_TYPE_VIDEO,
444         .filter_frame = filter_frame,
445         .config_props = config_input,
446     },
447     { NULL }
448 };
449
450 static const AVFilterPad outputs[] = {
451     {
452         .name          = "default",
453         .type          = AVMEDIA_TYPE_VIDEO,
454         .request_frame = request_frame,
455     },
456     { NULL }
457 };
458
459 AVFilter ff_vf_deflicker = {
460     .name          = "deflicker",
461     .description   = NULL_IF_CONFIG_SMALL("Remove temporal frame luminance variations."),
462     .priv_size     = sizeof(DeflickerContext),
463     .priv_class    = &deflicker_class,
464     .uninit        = uninit,
465     .query_formats = query_formats,
466     .inputs        = inputs,
467     .outputs       = outputs,
468 };