LCOV - code coverage report
Current view: top level - libavfilter - vf_deflicker.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 0 187 0.0 %
Date: 2017-12-12 03:56:30 Functions: 0 17 0.0 %

          Line data    Source code
       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             :     int bypass;
      53             : 
      54             :     int eof;
      55             :     int depth;
      56             :     int nb_planes;
      57             :     int planewidth[4];
      58             :     int planeheight[4];
      59             : 
      60             :     uint64_t *histogram;
      61             :     float luminance[SIZE];
      62             :     float sorted[SIZE];
      63             : 
      64             :     struct FFBufQueue q;
      65             :     int available;
      66             : 
      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);
      71             : } DeflickerContext;
      72             : 
      73             : #define OFFSET(x) offsetof(DeflickerContext, x)
      74             : #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
      75             : 
      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 },
      89             :     { NULL }
      90             : };
      91             : 
      92             : AVFILTER_DEFINE_CLASS(deflicker);
      93             : 
      94           0 : static int query_formats(AVFilterContext *ctx)
      95             : {
      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_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,
     104             :         AV_PIX_FMT_YUVJ411P,
     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,
     112             :         AV_PIX_FMT_NONE
     113             :     };
     114           0 :     AVFilterFormats *formats = ff_make_format_list(pixel_fmts);
     115           0 :     if (!formats)
     116           0 :         return AVERROR(ENOMEM);
     117           0 :     return ff_set_common_formats(ctx, formats);
     118             : }
     119             : 
     120           0 : 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)
     124             : {
     125             :     int x, y;
     126             : 
     127           0 :     for (y = 0; y < h; y++) {
     128           0 :         for (x = 0; x < w; x++) {
     129           0 :             dst[x] = av_clip_uint8(src[x] * f);
     130             :         }
     131             : 
     132           0 :         dst += dst_linesize;
     133           0 :         src += src_linesize;
     134             :     }
     135             : 
     136           0 :     return 0;
     137             : }
     138             : 
     139           0 : 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)
     143             : {
     144           0 :     DeflickerContext *s = ctx->priv;
     145           0 :     const uint16_t *src = (const uint16_t *)ssrc;
     146           0 :     uint16_t *dst = (uint16_t *)ddst;
     147           0 :     const int max = (1 << s->depth) - 1;
     148             :     int x, y;
     149             : 
     150           0 :     for (y = 0; y < h; y++) {
     151           0 :         for (x = 0; x < w; x++) {
     152           0 :             dst[x] = av_clip(src[x] * f, 0, max);
     153             :         }
     154             : 
     155           0 :         dst += dst_linesize / 2;
     156           0 :         src += src_linesize / 2;
     157             :     }
     158             : 
     159           0 :     return 0;
     160             : }
     161             : 
     162           0 : static float calc_avgy8(AVFilterContext *ctx, AVFrame *in)
     163             : {
     164           0 :     DeflickerContext *s = ctx->priv;
     165           0 :     const uint8_t *src = in->data[0];
     166           0 :     int64_t sum = 0;
     167             :     int y, x;
     168             : 
     169           0 :     memset(s->histogram, 0, (1 << s->depth) * sizeof(*s->histogram));
     170             : 
     171           0 :     for (y = 0; y < s->planeheight[0]; y++) {
     172           0 :         for (x = 0; x < s->planewidth[0]; x++) {
     173           0 :             s->histogram[src[x]]++;
     174             :         }
     175           0 :         src += in->linesize[0];
     176             :     }
     177             : 
     178           0 :     for (y = 0; y < 1 << s->depth; y++) {
     179           0 :         sum += s->histogram[y] * y;
     180             :     }
     181             : 
     182           0 :     return 1.0f * sum / (s->planeheight[0] * s->planewidth[0]);
     183             : }
     184             : 
     185           0 : static float calc_avgy16(AVFilterContext *ctx, AVFrame *in)
     186             : {
     187           0 :     DeflickerContext *s = ctx->priv;
     188           0 :     const uint16_t *src = (const uint16_t *)in->data[0];
     189           0 :     int64_t sum = 0;
     190             :     int y, x;
     191             : 
     192           0 :     memset(s->histogram, 0, (1 << s->depth) * sizeof(*s->histogram));
     193             : 
     194           0 :     for (y = 0; y < s->planeheight[0]; y++) {
     195           0 :         for (x = 0; x < s->planewidth[0]; x++) {
     196           0 :             s->histogram[src[x]]++;
     197             :         }
     198           0 :         src += in->linesize[0] / 2;
     199             :     }
     200             : 
     201           0 :     for (y = 0; y < 1 << s->depth; y++) {
     202           0 :         sum += s->histogram[y] * y;
     203             :     }
     204             : 
     205           0 :     return 1.0f * sum / (s->planeheight[0] * s->planewidth[0]);
     206             : }
     207             : 
     208           0 : static void get_am_factor(AVFilterContext *ctx, float *f)
     209             : {
     210           0 :     DeflickerContext *s = ctx->priv;
     211             :     int y;
     212             : 
     213           0 :     *f = 0.0f;
     214             : 
     215           0 :     for (y = 0; y < s->size; y++) {
     216           0 :         *f += s->luminance[y];
     217             :     }
     218             : 
     219           0 :     *f /= s->size;
     220           0 :     *f /= s->luminance[0];
     221           0 : }
     222             : 
     223           0 : static void get_gm_factor(AVFilterContext *ctx, float *f)
     224             : {
     225           0 :     DeflickerContext *s = ctx->priv;
     226             :     int y;
     227             : 
     228           0 :     *f = 1;
     229             : 
     230           0 :     for (y = 0; y < s->size; y++) {
     231           0 :         *f *= s->luminance[y];
     232             :     }
     233             : 
     234           0 :     *f = pow(*f, 1.0f / s->size);
     235           0 :     *f /= s->luminance[0];
     236           0 : }
     237             : 
     238           0 : static void get_hm_factor(AVFilterContext *ctx, float *f)
     239             : {
     240           0 :     DeflickerContext *s = ctx->priv;
     241             :     int y;
     242             : 
     243           0 :     *f = 0.0f;
     244             : 
     245           0 :     for (y = 0; y < s->size; y++) {
     246           0 :         *f += 1.0f / s->luminance[y];
     247             :     }
     248             : 
     249           0 :     *f = s->size / *f;
     250           0 :     *f /= s->luminance[0];
     251           0 : }
     252             : 
     253           0 : static void get_qm_factor(AVFilterContext *ctx, float *f)
     254             : {
     255           0 :     DeflickerContext *s = ctx->priv;
     256             :     int y;
     257             : 
     258           0 :     *f = 0.0f;
     259             : 
     260           0 :     for (y = 0; y < s->size; y++) {
     261           0 :         *f += s->luminance[y] * s->luminance[y];
     262             :     }
     263             : 
     264           0 :     *f /= s->size;
     265           0 :     *f  = sqrtf(*f);
     266           0 :     *f /= s->luminance[0];
     267           0 : }
     268             : 
     269           0 : static void get_cm_factor(AVFilterContext *ctx, float *f)
     270             : {
     271           0 :     DeflickerContext *s = ctx->priv;
     272             :     int y;
     273             : 
     274           0 :     *f = 0.0f;
     275             : 
     276           0 :     for (y = 0; y < s->size; y++) {
     277           0 :         *f += s->luminance[y] * s->luminance[y] * s->luminance[y];
     278             :     }
     279             : 
     280           0 :     *f /= s->size;
     281           0 :     *f  = cbrtf(*f);
     282           0 :     *f /= s->luminance[0];
     283           0 : }
     284             : 
     285           0 : static void get_pm_factor(AVFilterContext *ctx, float *f)
     286             : {
     287           0 :     DeflickerContext *s = ctx->priv;
     288             :     int y;
     289             : 
     290           0 :     *f = 0.0f;
     291             : 
     292           0 :     for (y = 0; y < s->size; y++) {
     293           0 :         *f += powf(s->luminance[y], s->size);
     294             :     }
     295             : 
     296           0 :     *f /= s->size;
     297           0 :     *f  = powf(*f, 1.0f / s->size);
     298           0 :     *f /= s->luminance[0];
     299           0 : }
     300             : 
     301           0 : static int comparef(const void *a, const void *b)
     302             : {
     303           0 :     const float *aa = a, *bb = b;
     304           0 :     return round(aa - bb);
     305             : }
     306             : 
     307           0 : static void get_median_factor(AVFilterContext *ctx, float *f)
     308             : {
     309           0 :     DeflickerContext *s = ctx->priv;
     310             : 
     311           0 :     memcpy(s->sorted, s->luminance, sizeof(s->sorted));
     312           0 :     AV_QSORT(s->sorted, s->size, float, comparef);
     313             : 
     314           0 :     *f = s->sorted[s->size >> 1] / s->luminance[0];
     315           0 : }
     316             : 
     317           0 : static int config_input(AVFilterLink *inlink)
     318             : {
     319           0 :     const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
     320           0 :     AVFilterContext *ctx = inlink->dst;
     321           0 :     DeflickerContext *s = ctx->priv;
     322             : 
     323           0 :     s->nb_planes = desc->nb_components;
     324             : 
     325           0 :     s->planeheight[1] = s->planeheight[2] = AV_CEIL_RSHIFT(inlink->h, desc->log2_chroma_h);
     326           0 :     s->planeheight[0] = s->planeheight[3] = inlink->h;
     327           0 :     s->planewidth[1]  = s->planewidth[2]  = AV_CEIL_RSHIFT(inlink->w, desc->log2_chroma_w);
     328           0 :     s->planewidth[0]  = s->planewidth[3]  = inlink->w;
     329             : 
     330           0 :     s->depth = desc->comp[0].depth;
     331           0 :     if (s->depth == 8) {
     332           0 :         s->deflicker = deflicker8;
     333           0 :         s->calc_avgy = calc_avgy8;
     334             :     } else {
     335           0 :         s->deflicker = deflicker16;
     336           0 :         s->calc_avgy = calc_avgy16;
     337             :     }
     338             : 
     339           0 :     s->histogram = av_calloc(1 << s->depth, sizeof(*s->histogram));
     340           0 :     if (!s->histogram)
     341           0 :         return AVERROR(ENOMEM);
     342             : 
     343           0 :     switch (s->mode) {
     344           0 :     case MEDIAN:          s->get_factor = get_median_factor; break;
     345           0 :     case ARITHMETIC_MEAN: s->get_factor = get_am_factor;     break;
     346           0 :     case GEOMETRIC_MEAN:  s->get_factor = get_gm_factor;     break;
     347           0 :     case HARMONIC_MEAN:   s->get_factor = get_hm_factor;     break;
     348           0 :     case QUADRATIC_MEAN:  s->get_factor = get_qm_factor;     break;
     349           0 :     case CUBIC_MEAN:      s->get_factor = get_cm_factor;     break;
     350           0 :     case POWER_MEAN:      s->get_factor = get_pm_factor;     break;
     351             :     }
     352             : 
     353           0 :     return 0;
     354             : }
     355             : 
     356           0 : static int filter_frame(AVFilterLink *inlink, AVFrame *buf)
     357             : {
     358           0 :     AVFilterContext *ctx = inlink->dst;
     359           0 :     AVFilterLink *outlink = ctx->outputs[0];
     360           0 :     DeflickerContext *s = ctx->priv;
     361             :     AVDictionary **metadata;
     362             :     AVFrame *out, *in;
     363             :     float f;
     364             :     int y;
     365             : 
     366           0 :     if (s->q.available < s->size && !s->eof) {
     367           0 :         s->luminance[s->available] = s->calc_avgy(ctx, buf);
     368           0 :         ff_bufqueue_add(ctx, &s->q, buf);
     369           0 :         s->available++;
     370           0 :         return 0;
     371             :     }
     372             : 
     373           0 :     in = ff_bufqueue_peek(&s->q, 0);
     374             : 
     375           0 :     out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
     376           0 :     if (!out) {
     377           0 :         av_frame_free(&buf);
     378           0 :         return AVERROR(ENOMEM);
     379             :     }
     380             : 
     381           0 :     s->get_factor(ctx, &f);
     382           0 :     if (!s->bypass)
     383           0 :         s->deflicker(ctx, in->data[0], in->linesize[0], out->data[0], out->linesize[0],
     384             :                      outlink->w, outlink->h, f);
     385           0 :     for (y = 1 - s->bypass; y < s->nb_planes; y++) {
     386           0 :         av_image_copy_plane(out->data[y], out->linesize[y],
     387           0 :                             in->data[y], in->linesize[y],
     388           0 :                             s->planewidth[y] * (1 + (s->depth > 8)), s->planeheight[y]);
     389             :     }
     390             : 
     391           0 :     av_frame_copy_props(out, in);
     392           0 :     metadata = &out->metadata;
     393           0 :     if (metadata) {
     394             :         uint8_t value[128];
     395             : 
     396           0 :         snprintf(value, sizeof(value), "%f", s->luminance[0]);
     397           0 :         av_dict_set(metadata, "lavfi.deflicker.luminance", value, 0);
     398             : 
     399           0 :         snprintf(value, sizeof(value), "%f", s->luminance[0] * f);
     400           0 :         av_dict_set(metadata, "lavfi.deflicker.new_luminance", value, 0);
     401             : 
     402           0 :         snprintf(value, sizeof(value), "%f", f - 1.0f);
     403           0 :         av_dict_set(metadata, "lavfi.deflicker.relative_change", value, 0);
     404             :     }
     405             : 
     406           0 :     in = ff_bufqueue_get(&s->q);
     407           0 :     av_frame_free(&in);
     408           0 :     memmove(&s->luminance[0], &s->luminance[1], sizeof(*s->luminance) * (s->size - 1));
     409           0 :     s->luminance[s->available - 1] = s->calc_avgy(ctx, buf);
     410           0 :     ff_bufqueue_add(ctx, &s->q, buf);
     411             : 
     412           0 :     return ff_filter_frame(outlink, out);
     413             : }
     414             : 
     415           0 : static int request_frame(AVFilterLink *outlink)
     416             : {
     417           0 :     AVFilterContext *ctx = outlink->src;
     418           0 :     DeflickerContext *s = ctx->priv;
     419             :     int ret;
     420             : 
     421           0 :     ret = ff_request_frame(ctx->inputs[0]);
     422           0 :     if (ret == AVERROR_EOF && s->available > 0) {
     423           0 :         AVFrame *buf = av_frame_clone(ff_bufqueue_peek(&s->q, s->size - 1));
     424           0 :         if (!buf)
     425           0 :             return AVERROR(ENOMEM);
     426             : 
     427           0 :         s->eof = 1;
     428           0 :         ret = filter_frame(ctx->inputs[0], buf);
     429           0 :         s->available--;
     430             :     }
     431             : 
     432           0 :     return ret;
     433             : }
     434             : 
     435           0 : static av_cold void uninit(AVFilterContext *ctx)
     436             : {
     437           0 :     DeflickerContext *s = ctx->priv;
     438             : 
     439           0 :     ff_bufqueue_discard_all(&s->q);
     440           0 :     av_freep(&s->histogram);
     441           0 : }
     442             : 
     443             : static const AVFilterPad inputs[] = {
     444             :     {
     445             :         .name         = "default",
     446             :         .type         = AVMEDIA_TYPE_VIDEO,
     447             :         .filter_frame = filter_frame,
     448             :         .config_props = config_input,
     449             :     },
     450             :     { NULL }
     451             : };
     452             : 
     453             : static const AVFilterPad outputs[] = {
     454             :     {
     455             :         .name          = "default",
     456             :         .type          = AVMEDIA_TYPE_VIDEO,
     457             :         .request_frame = request_frame,
     458             :     },
     459             :     { NULL }
     460             : };
     461             : 
     462             : AVFilter ff_vf_deflicker = {
     463             :     .name          = "deflicker",
     464             :     .description   = NULL_IF_CONFIG_SMALL("Remove temporal frame luminance variations."),
     465             :     .priv_size     = sizeof(DeflickerContext),
     466             :     .priv_class    = &deflicker_class,
     467             :     .uninit        = uninit,
     468             :     .query_formats = query_formats,
     469             :     .inputs        = inputs,
     470             :     .outputs       = outputs,
     471             : };

Generated by: LCOV version 1.13