2 * Copyright (c) 2017 Paul B Mahol
4 * This file is part of FFmpeg.
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.
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
21 #include "libavutil/imgutils.h"
22 #include "libavutil/opt.h"
23 #include "libavutil/pixdesc.h"
24 #include "libavutil/qsort.h"
27 #define FF_BUFQUEUE_SIZE 129
28 #include "bufferqueue.h"
34 #define SIZE FF_BUFQUEUE_SIZE
47 typedef struct DeflickerContext {
61 float luminance[SIZE];
67 void (*get_factor)(AVFilterContext *ctx, float *f);
68 float (*calc_avgy)(AVFilterContext *ctx, AVFrame *in);
69 int (*deflicker)(AVFilterContext *ctx, const uint8_t *src, ptrdiff_t src_linesize,
70 uint8_t *dst, ptrdiff_t dst_linesize, int w, int h, float f);
73 #define OFFSET(x) offsetof(DeflickerContext, x)
74 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
76 static const AVOption deflicker_options[] = {
77 { "size", "set how many frames to use", OFFSET(size), AV_OPT_TYPE_INT, {.i64=5}, 2, SIZE, FLAGS },
78 { "s", "set how many frames to use", OFFSET(size), AV_OPT_TYPE_INT, {.i64=5}, 2, SIZE, FLAGS },
79 { "mode", "set how to smooth luminance", OFFSET(mode), AV_OPT_TYPE_INT, {.i64=0}, 0, NB_SMOOTH_MODE-1, FLAGS, "mode" },
80 { "m", "set how to smooth luminance", OFFSET(mode), AV_OPT_TYPE_INT, {.i64=0}, 0, NB_SMOOTH_MODE-1, FLAGS, "mode" },
81 { "am", "arithmetic mean", 0, AV_OPT_TYPE_CONST, {.i64=ARITHMETIC_MEAN}, 0, 0, FLAGS, "mode" },
82 { "gm", "geometric mean", 0, AV_OPT_TYPE_CONST, {.i64=GEOMETRIC_MEAN}, 0, 0, FLAGS, "mode" },
83 { "hm", "harmonic mean", 0, AV_OPT_TYPE_CONST, {.i64=HARMONIC_MEAN}, 0, 0, FLAGS, "mode" },
84 { "qm", "quadratic mean", 0, AV_OPT_TYPE_CONST, {.i64=QUADRATIC_MEAN}, 0, 0, FLAGS, "mode" },
85 { "cm", "cubic mean", 0, AV_OPT_TYPE_CONST, {.i64=CUBIC_MEAN}, 0, 0, FLAGS, "mode" },
86 { "pm", "power mean", 0, AV_OPT_TYPE_CONST, {.i64=POWER_MEAN}, 0, 0, FLAGS, "mode" },
87 { "median", "median", 0, AV_OPT_TYPE_CONST, {.i64=MEDIAN}, 0, 0, FLAGS, "mode" },
88 { "bypass", "leave frames unchanged", OFFSET(bypass), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS },
92 AVFILTER_DEFINE_CLASS(deflicker);
94 static int query_formats(AVFilterContext *ctx)
96 static const enum AVPixelFormat pixel_fmts[] = {
97 AV_PIX_FMT_GRAY8, AV_PIX_FMT_GRAY9, AV_PIX_FMT_GRAY10,
98 AV_PIX_FMT_GRAY12, AV_PIX_FMT_GRAY14, AV_PIX_FMT_GRAY16,
99 AV_PIX_FMT_YUV410P, AV_PIX_FMT_YUV411P,
100 AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV422P,
101 AV_PIX_FMT_YUV440P, AV_PIX_FMT_YUV444P,
102 AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ422P,
103 AV_PIX_FMT_YUVJ440P, AV_PIX_FMT_YUVJ444P,
105 AV_PIX_FMT_YUV420P9, AV_PIX_FMT_YUV422P9, AV_PIX_FMT_YUV444P9,
106 AV_PIX_FMT_YUV420P10, AV_PIX_FMT_YUV422P10, AV_PIX_FMT_YUV444P10,
107 AV_PIX_FMT_YUV440P10,
108 AV_PIX_FMT_YUV444P12, AV_PIX_FMT_YUV422P12, AV_PIX_FMT_YUV420P12,
109 AV_PIX_FMT_YUV440P12,
110 AV_PIX_FMT_YUV444P14, AV_PIX_FMT_YUV422P14, AV_PIX_FMT_YUV420P14,
111 AV_PIX_FMT_YUV420P16, AV_PIX_FMT_YUV422P16, AV_PIX_FMT_YUV444P16,
114 AVFilterFormats *formats = ff_make_format_list(pixel_fmts);
116 return AVERROR(ENOMEM);
117 return ff_set_common_formats(ctx, formats);
120 static int deflicker8(AVFilterContext *ctx,
121 const uint8_t *src, ptrdiff_t src_linesize,
122 uint8_t *dst, ptrdiff_t dst_linesize,
123 int w, int h, float f)
127 for (y = 0; y < h; y++) {
128 for (x = 0; x < w; x++) {
129 dst[x] = av_clip_uint8(src[x] * f);
139 static int deflicker16(AVFilterContext *ctx,
140 const uint8_t *ssrc, ptrdiff_t src_linesize,
141 uint8_t *ddst, ptrdiff_t dst_linesize,
142 int w, int h, float f)
144 DeflickerContext *s = ctx->priv;
145 const uint16_t *src = (const uint16_t *)ssrc;
146 uint16_t *dst = (uint16_t *)ddst;
147 const int max = (1 << s->depth) - 1;
150 for (y = 0; y < h; y++) {
151 for (x = 0; x < w; x++) {
152 dst[x] = av_clip(src[x] * f, 0, max);
155 dst += dst_linesize / 2;
156 src += src_linesize / 2;
162 static float calc_avgy8(AVFilterContext *ctx, AVFrame *in)
164 DeflickerContext *s = ctx->priv;
165 const uint8_t *src = in->data[0];
169 memset(s->histogram, 0, (1 << s->depth) * sizeof(*s->histogram));
171 for (y = 0; y < s->planeheight[0]; y++) {
172 for (x = 0; x < s->planewidth[0]; x++) {
173 s->histogram[src[x]]++;
175 src += in->linesize[0];
178 for (y = 0; y < 1 << s->depth; y++) {
179 sum += s->histogram[y] * y;
182 return 1.0f * sum / (s->planeheight[0] * s->planewidth[0]);
185 static float calc_avgy16(AVFilterContext *ctx, AVFrame *in)
187 DeflickerContext *s = ctx->priv;
188 const uint16_t *src = (const uint16_t *)in->data[0];
192 memset(s->histogram, 0, (1 << s->depth) * sizeof(*s->histogram));
194 for (y = 0; y < s->planeheight[0]; y++) {
195 for (x = 0; x < s->planewidth[0]; x++) {
196 s->histogram[src[x]]++;
198 src += in->linesize[0] / 2;
201 for (y = 0; y < 1 << s->depth; y++) {
202 sum += s->histogram[y] * y;
205 return 1.0f * sum / (s->planeheight[0] * s->planewidth[0]);
208 static void get_am_factor(AVFilterContext *ctx, float *f)
210 DeflickerContext *s = ctx->priv;
215 for (y = 0; y < s->size; y++) {
216 *f += s->luminance[y];
220 *f /= s->luminance[0];
223 static void get_gm_factor(AVFilterContext *ctx, float *f)
225 DeflickerContext *s = ctx->priv;
230 for (y = 0; y < s->size; y++) {
231 *f *= s->luminance[y];
234 *f = pow(*f, 1.0f / s->size);
235 *f /= s->luminance[0];
238 static void get_hm_factor(AVFilterContext *ctx, float *f)
240 DeflickerContext *s = ctx->priv;
245 for (y = 0; y < s->size; y++) {
246 *f += 1.0f / s->luminance[y];
250 *f /= s->luminance[0];
253 static void get_qm_factor(AVFilterContext *ctx, float *f)
255 DeflickerContext *s = ctx->priv;
260 for (y = 0; y < s->size; y++) {
261 *f += s->luminance[y] * s->luminance[y];
266 *f /= s->luminance[0];
269 static void get_cm_factor(AVFilterContext *ctx, float *f)
271 DeflickerContext *s = ctx->priv;
276 for (y = 0; y < s->size; y++) {
277 *f += s->luminance[y] * s->luminance[y] * s->luminance[y];
282 *f /= s->luminance[0];
285 static void get_pm_factor(AVFilterContext *ctx, float *f)
287 DeflickerContext *s = ctx->priv;
292 for (y = 0; y < s->size; y++) {
293 *f += powf(s->luminance[y], s->size);
297 *f = powf(*f, 1.0f / s->size);
298 *f /= s->luminance[0];
301 static int comparef(const void *a, const void *b)
303 const float *aa = a, *bb = b;
304 return round(aa - bb);
307 static void get_median_factor(AVFilterContext *ctx, float *f)
309 DeflickerContext *s = ctx->priv;
311 memcpy(s->sorted, s->luminance, sizeof(s->sorted));
312 AV_QSORT(s->sorted, s->size, float, comparef);
314 *f = s->sorted[s->size >> 1] / s->luminance[0];
317 static int config_input(AVFilterLink *inlink)
319 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
320 AVFilterContext *ctx = inlink->dst;
321 DeflickerContext *s = ctx->priv;
323 s->nb_planes = desc->nb_components;
325 s->planeheight[1] = s->planeheight[2] = AV_CEIL_RSHIFT(inlink->h, desc->log2_chroma_h);
326 s->planeheight[0] = s->planeheight[3] = inlink->h;
327 s->planewidth[1] = s->planewidth[2] = AV_CEIL_RSHIFT(inlink->w, desc->log2_chroma_w);
328 s->planewidth[0] = s->planewidth[3] = inlink->w;
330 s->depth = desc->comp[0].depth;
332 s->deflicker = deflicker8;
333 s->calc_avgy = calc_avgy8;
335 s->deflicker = deflicker16;
336 s->calc_avgy = calc_avgy16;
339 s->histogram = av_calloc(1 << s->depth, sizeof(*s->histogram));
341 return AVERROR(ENOMEM);
344 case MEDIAN: s->get_factor = get_median_factor; break;
345 case ARITHMETIC_MEAN: s->get_factor = get_am_factor; break;
346 case GEOMETRIC_MEAN: s->get_factor = get_gm_factor; break;
347 case HARMONIC_MEAN: s->get_factor = get_hm_factor; break;
348 case QUADRATIC_MEAN: s->get_factor = get_qm_factor; break;
349 case CUBIC_MEAN: s->get_factor = get_cm_factor; break;
350 case POWER_MEAN: s->get_factor = get_pm_factor; break;
356 static int filter_frame(AVFilterLink *inlink, AVFrame *buf)
358 AVFilterContext *ctx = inlink->dst;
359 AVFilterLink *outlink = ctx->outputs[0];
360 DeflickerContext *s = ctx->priv;
361 AVDictionary **metadata;
366 if (s->q.available < s->size && !s->eof) {
367 s->luminance[s->available] = s->calc_avgy(ctx, buf);
368 ff_bufqueue_add(ctx, &s->q, buf);
373 in = ff_bufqueue_peek(&s->q, 0);
375 out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
378 return AVERROR(ENOMEM);
381 s->get_factor(ctx, &f);
383 s->deflicker(ctx, in->data[0], in->linesize[0], out->data[0], out->linesize[0],
384 outlink->w, outlink->h, f);
385 for (y = 1 - s->bypass; y < s->nb_planes; y++) {
386 av_image_copy_plane(out->data[y], out->linesize[y],
387 in->data[y], in->linesize[y],
388 s->planewidth[y] * (1 + (s->depth > 8)), s->planeheight[y]);
391 av_frame_copy_props(out, in);
392 metadata = &out->metadata;
396 snprintf(value, sizeof(value), "%f", s->luminance[0]);
397 av_dict_set(metadata, "lavfi.deflicker.luminance", value, 0);
399 snprintf(value, sizeof(value), "%f", s->luminance[0] * f);
400 av_dict_set(metadata, "lavfi.deflicker.new_luminance", value, 0);
402 snprintf(value, sizeof(value), "%f", f - 1.0f);
403 av_dict_set(metadata, "lavfi.deflicker.relative_change", value, 0);
406 in = ff_bufqueue_get(&s->q);
408 memmove(&s->luminance[0], &s->luminance[1], sizeof(*s->luminance) * (s->size - 1));
409 s->luminance[s->available - 1] = s->calc_avgy(ctx, buf);
410 ff_bufqueue_add(ctx, &s->q, buf);
412 return ff_filter_frame(outlink, out);
415 static int request_frame(AVFilterLink *outlink)
417 AVFilterContext *ctx = outlink->src;
418 DeflickerContext *s = ctx->priv;
421 ret = ff_request_frame(ctx->inputs[0]);
422 if (ret == AVERROR_EOF && s->available > 0) {
423 AVFrame *buf = ff_bufqueue_peek(&s->q, s->available - 1);
425 return AVERROR(ENOMEM);
426 buf = av_frame_clone(buf);
428 return AVERROR(ENOMEM);
431 ret = filter_frame(ctx->inputs[0], buf);
438 static av_cold void uninit(AVFilterContext *ctx)
440 DeflickerContext *s = ctx->priv;
442 ff_bufqueue_discard_all(&s->q);
443 av_freep(&s->histogram);
446 static const AVFilterPad inputs[] = {
449 .type = AVMEDIA_TYPE_VIDEO,
450 .filter_frame = filter_frame,
451 .config_props = config_input,
456 static const AVFilterPad outputs[] = {
459 .type = AVMEDIA_TYPE_VIDEO,
460 .request_frame = request_frame,
465 AVFilter ff_vf_deflicker = {
467 .description = NULL_IF_CONFIG_SMALL("Remove temporal frame luminance variations."),
468 .priv_size = sizeof(DeflickerContext),
469 .priv_class = &deflicker_class,
471 .query_formats = query_formats,