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 {
60 float luminance[SIZE];
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);
72 #define OFFSET(x) offsetof(DeflickerContext, x)
73 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
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" },
90 AVFILTER_DEFINE_CLASS(deflicker);
92 static int query_formats(AVFilterContext *ctx)
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,
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,
112 AVFilterFormats *formats = ff_make_format_list(pixel_fmts);
114 return AVERROR(ENOMEM);
115 return ff_set_common_formats(ctx, formats);
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)
125 for (y = 0; y < h; y++) {
126 for (x = 0; x < w; x++) {
127 dst[x] = av_clip_uint8(src[x] * f);
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)
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;
148 for (y = 0; y < h; y++) {
149 for (x = 0; x < w; x++) {
150 dst[x] = av_clip(src[x] * f, 0, max);
153 dst += dst_linesize / 2;
154 src += src_linesize / 2;
160 static float calc_avgy8(AVFilterContext *ctx, AVFrame *in)
162 DeflickerContext *s = ctx->priv;
163 const uint8_t *src = in->data[0];
167 memset(s->histogram, 0, (1 << s->depth) * sizeof(*s->histogram));
169 for (y = 0; y < s->planeheight[0]; y++) {
170 for (x = 0; x < s->planewidth[0]; x++) {
171 s->histogram[src[x]]++;
173 src += in->linesize[0];
176 for (y = 0; y < 1 << s->depth; y++) {
177 sum += s->histogram[y] * y;
180 return 1.0f * sum / (s->planeheight[0] * s->planewidth[0]);
183 static float calc_avgy16(AVFilterContext *ctx, AVFrame *in)
185 DeflickerContext *s = ctx->priv;
186 const uint16_t *src = (const uint16_t *)in->data[0];
190 memset(s->histogram, 0, (1 << s->depth) * sizeof(*s->histogram));
192 for (y = 0; y < s->planeheight[0]; y++) {
193 for (x = 0; x < s->planewidth[0]; x++) {
194 s->histogram[src[x]]++;
196 src += in->linesize[0] / 2;
199 for (y = 0; y < 1 << s->depth; y++) {
200 sum += s->histogram[y] * y;
203 return 1.0f * sum / (s->planeheight[0] * s->planewidth[0]);
206 static void get_am_factor(AVFilterContext *ctx, float *f)
208 DeflickerContext *s = ctx->priv;
213 for (y = 0; y < s->size; y++) {
214 *f += s->luminance[y];
218 *f /= s->luminance[0];
221 static void get_gm_factor(AVFilterContext *ctx, float *f)
223 DeflickerContext *s = ctx->priv;
228 for (y = 0; y < s->size; y++) {
229 *f *= s->luminance[y];
232 *f = pow(*f, 1.0f / s->size);
233 *f /= s->luminance[0];
236 static void get_hm_factor(AVFilterContext *ctx, float *f)
238 DeflickerContext *s = ctx->priv;
243 for (y = 0; y < s->size; y++) {
244 *f += 1.0f / s->luminance[y];
248 *f /= s->luminance[0];
251 static void get_qm_factor(AVFilterContext *ctx, float *f)
253 DeflickerContext *s = ctx->priv;
258 for (y = 0; y < s->size; y++) {
259 *f += s->luminance[y] * s->luminance[y];
264 *f /= s->luminance[0];
267 static void get_cm_factor(AVFilterContext *ctx, float *f)
269 DeflickerContext *s = ctx->priv;
274 for (y = 0; y < s->size; y++) {
275 *f += s->luminance[y] * s->luminance[y] * s->luminance[y];
280 *f /= s->luminance[0];
283 static void get_pm_factor(AVFilterContext *ctx, float *f)
285 DeflickerContext *s = ctx->priv;
290 for (y = 0; y < s->size; y++) {
291 *f += powf(s->luminance[y], s->size);
295 *f = powf(*f, 1.0f / s->size);
296 *f /= s->luminance[0];
299 static int comparef(const void *a, const void *b)
301 const float *aa = a, *bb = b;
302 return round(aa - bb);
305 static void get_median_factor(AVFilterContext *ctx, float *f)
307 DeflickerContext *s = ctx->priv;
309 memcpy(s->sorted, s->luminance, sizeof(s->sorted));
310 AV_QSORT(s->sorted, s->size, float, comparef);
312 *f = s->sorted[s->size >> 1] / s->luminance[0];
315 static int config_input(AVFilterLink *inlink)
317 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
318 AVFilterContext *ctx = inlink->dst;
319 DeflickerContext *s = ctx->priv;
321 s->nb_planes = desc->nb_components;
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;
328 s->depth = desc->comp[0].depth;
330 s->deflicker = deflicker8;
331 s->calc_avgy = calc_avgy8;
333 s->deflicker = deflicker16;
334 s->calc_avgy = calc_avgy16;
337 s->histogram = av_calloc(1 << s->depth, sizeof(*s->histogram));
339 return AVERROR(ENOMEM);
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;
354 static int filter_frame(AVFilterLink *inlink, AVFrame *buf)
356 AVFilterContext *ctx = inlink->dst;
357 AVFilterLink *outlink = ctx->outputs[0];
358 DeflickerContext *s = ctx->priv;
359 AVDictionary **metadata;
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);
371 in = ff_bufqueue_peek(&s->q, 0);
373 out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
376 return AVERROR(ENOMEM);
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]);
388 av_frame_copy_props(out, in);
389 metadata = &out->metadata;
393 snprintf(value, sizeof(value), "%f", s->luminance[0]);
394 av_dict_set(metadata, "lavfi.deflicker.luminance", value, 0);
396 snprintf(value, sizeof(value), "%f", s->luminance[0] * f);
397 av_dict_set(metadata, "lavfi.deflicker.new_luminance", value, 0);
399 snprintf(value, sizeof(value), "%f", f - 1.0f);
400 av_dict_set(metadata, "lavfi.deflicker.relative_change", value, 0);
403 in = ff_bufqueue_get(&s->q);
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);
409 return ff_filter_frame(outlink, out);
412 static int request_frame(AVFilterLink *outlink)
414 AVFilterContext *ctx = outlink->src;
415 DeflickerContext *s = ctx->priv;
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));
422 return AVERROR(ENOMEM);
425 ret = filter_frame(ctx->inputs[0], buf);
432 static av_cold void uninit(AVFilterContext *ctx)
434 DeflickerContext *s = ctx->priv;
436 ff_bufqueue_discard_all(&s->q);
437 av_freep(&s->histogram);
440 static const AVFilterPad inputs[] = {
443 .type = AVMEDIA_TYPE_VIDEO,
444 .filter_frame = filter_frame,
445 .config_props = config_input,
450 static const AVFilterPad outputs[] = {
453 .type = AVMEDIA_TYPE_VIDEO,
454 .request_frame = request_frame,
459 AVFilter ff_vf_deflicker = {
461 .description = NULL_IF_CONFIG_SMALL("Remove temporal frame luminance variations."),
462 .priv_size = sizeof(DeflickerContext),
463 .priv_class = &deflicker_class,
465 .query_formats = query_formats,