LCOV - code coverage report
Current view: top level - libavfilter - vf_decimate.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 0 195 0.0 %
Date: 2017-12-16 01:21:47 Functions: 0 9 0.0 %

          Line data    Source code
       1             : /*
       2             :  * Copyright (c) 2012 Fredrik Mellbin
       3             :  * Copyright (c) 2013 Clément Bœsch
       4             :  *
       5             :  * This file is part of FFmpeg.
       6             :  *
       7             :  * FFmpeg is free software; you can redistribute it and/or
       8             :  * modify it under the terms of the GNU Lesser General Public
       9             :  * License as published by the Free Software Foundation; either
      10             :  * version 2.1 of the License, or (at your option) any later version.
      11             :  *
      12             :  * FFmpeg is distributed in the hope that it will be useful,
      13             :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      14             :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      15             :  * Lesser General Public License for more details.
      16             :  *
      17             :  * You should have received a copy of the GNU Lesser General Public
      18             :  * License along with FFmpeg; if not, write to the Free Software
      19             :  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
      20             :  */
      21             : 
      22             : #include "libavutil/opt.h"
      23             : #include "libavutil/pixdesc.h"
      24             : #include "libavutil/timestamp.h"
      25             : #include "avfilter.h"
      26             : #include "internal.h"
      27             : 
      28             : #define INPUT_MAIN     0
      29             : #define INPUT_CLEANSRC 1
      30             : 
      31             : struct qitem {
      32             :     AVFrame *frame;
      33             :     int64_t maxbdiff;
      34             :     int64_t totdiff;
      35             : };
      36             : 
      37             : typedef struct DecimateContext {
      38             :     const AVClass *class;
      39             :     struct qitem *queue;    ///< window of cycle frames and the associated data diff
      40             :     int fid;                ///< current frame id in the queue
      41             :     int filled;             ///< 1 if the queue is filled, 0 otherwise
      42             :     AVFrame *last;          ///< last frame from the previous queue
      43             :     AVFrame **clean_src;    ///< frame queue for the clean source
      44             :     int got_frame[2];       ///< frame request flag for each input stream
      45             :     AVRational ts_unit;     ///< timestamp units for the output frames
      46             :     int64_t start_pts;      ///< base for output timestamps
      47             :     uint32_t eof;           ///< bitmask for end of stream
      48             :     int hsub, vsub;         ///< chroma subsampling values
      49             :     int depth;
      50             :     int nxblocks, nyblocks;
      51             :     int bdiffsize;
      52             :     int64_t *bdiffs;
      53             : 
      54             :     /* options */
      55             :     int cycle;
      56             :     double dupthresh_flt;
      57             :     double scthresh_flt;
      58             :     int64_t dupthresh;
      59             :     int64_t scthresh;
      60             :     int blockx, blocky;
      61             :     int ppsrc;
      62             :     int chroma;
      63             : } DecimateContext;
      64             : 
      65             : #define OFFSET(x) offsetof(DecimateContext, x)
      66             : #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
      67             : 
      68             : static const AVOption decimate_options[] = {
      69             :     { "cycle",     "set the number of frame from which one will be dropped", OFFSET(cycle), AV_OPT_TYPE_INT, {.i64 = 5}, 2, 25, FLAGS },
      70             :     { "dupthresh", "set duplicate threshold",    OFFSET(dupthresh_flt), AV_OPT_TYPE_DOUBLE, {.dbl =  1.1}, 0, 100, FLAGS },
      71             :     { "scthresh",  "set scene change threshold", OFFSET(scthresh_flt),  AV_OPT_TYPE_DOUBLE, {.dbl = 15.0}, 0, 100, FLAGS },
      72             :     { "blockx",    "set the size of the x-axis blocks used during metric calculations", OFFSET(blockx), AV_OPT_TYPE_INT, {.i64 = 32}, 4, 1<<9, FLAGS },
      73             :     { "blocky",    "set the size of the y-axis blocks used during metric calculations", OFFSET(blocky), AV_OPT_TYPE_INT, {.i64 = 32}, 4, 1<<9, FLAGS },
      74             :     { "ppsrc",     "mark main input as a pre-processed input and activate clean source input stream", OFFSET(ppsrc), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS },
      75             :     { "chroma",    "set whether or not chroma is considered in the metric calculations", OFFSET(chroma), AV_OPT_TYPE_BOOL, {.i64=1}, 0, 1, FLAGS },
      76             :     { NULL }
      77             : };
      78             : 
      79             : AVFILTER_DEFINE_CLASS(decimate);
      80             : 
      81           0 : static void calc_diffs(const DecimateContext *dm, struct qitem *q,
      82             :                        const AVFrame *f1, const AVFrame *f2)
      83             : {
      84           0 :     int64_t maxdiff = -1;
      85           0 :     int64_t *bdiffs = dm->bdiffs;
      86             :     int plane, i, j;
      87             : 
      88           0 :     memset(bdiffs, 0, dm->bdiffsize * sizeof(*bdiffs));
      89             : 
      90           0 :     for (plane = 0; plane < (dm->chroma && f1->data[2] ? 3 : 1); plane++) {
      91             :         int x, y, xl;
      92           0 :         const int linesize1 = f1->linesize[plane];
      93           0 :         const int linesize2 = f2->linesize[plane];
      94           0 :         const uint8_t *f1p = f1->data[plane];
      95           0 :         const uint8_t *f2p = f2->data[plane];
      96           0 :         int width    = plane ? AV_CEIL_RSHIFT(f1->width,  dm->hsub) : f1->width;
      97           0 :         int height   = plane ? AV_CEIL_RSHIFT(f1->height, dm->vsub) : f1->height;
      98           0 :         int hblockx  = dm->blockx / 2;
      99           0 :         int hblocky  = dm->blocky / 2;
     100             : 
     101           0 :         if (plane) {
     102           0 :             hblockx >>= dm->hsub;
     103           0 :             hblocky >>= dm->vsub;
     104             :         }
     105             : 
     106           0 :         for (y = 0; y < height; y++) {
     107           0 :             int ydest = y / hblocky;
     108           0 :             int xdest = 0;
     109             : 
     110             : #define CALC_DIFF(nbits) do {                               \
     111             :     for (x = 0; x < width; x += hblockx) {                  \
     112             :         int64_t acc = 0;                                    \
     113             :         int m = FFMIN(width, x + hblockx);                  \
     114             :         for (xl = x; xl < m; xl++)                          \
     115             :             acc += abs(((const uint##nbits##_t *)f1p)[xl] - \
     116             :                        ((const uint##nbits##_t *)f2p)[xl]); \
     117             :         bdiffs[ydest * dm->nxblocks + xdest] += acc;        \
     118             :         xdest++;                                            \
     119             :     }                                                       \
     120             : } while (0)
     121           0 :             if (dm->depth == 8) CALC_DIFF(8);
     122           0 :             else                CALC_DIFF(16);
     123             : 
     124           0 :             f1p += linesize1;
     125           0 :             f2p += linesize2;
     126             :         }
     127             :     }
     128             : 
     129           0 :     for (i = 0; i < dm->nyblocks - 1; i++) {
     130           0 :         for (j = 0; j < dm->nxblocks - 1; j++) {
     131           0 :             int64_t tmp = bdiffs[      i * dm->nxblocks + j    ]
     132           0 :                         + bdiffs[      i * dm->nxblocks + j + 1]
     133           0 :                         + bdiffs[(i + 1) * dm->nxblocks + j    ]
     134           0 :                         + bdiffs[(i + 1) * dm->nxblocks + j + 1];
     135           0 :             if (tmp > maxdiff)
     136           0 :                 maxdiff = tmp;
     137             :         }
     138             :     }
     139             : 
     140           0 :     q->totdiff = 0;
     141           0 :     for (i = 0; i < dm->bdiffsize; i++)
     142           0 :         q->totdiff += bdiffs[i];
     143           0 :     q->maxbdiff = maxdiff;
     144           0 : }
     145             : 
     146           0 : static int filter_frame(AVFilterLink *inlink, AVFrame *in)
     147             : {
     148           0 :     int scpos = -1, duppos = -1;
     149           0 :     int drop = INT_MIN, i, lowest = 0, ret;
     150           0 :     AVFilterContext *ctx  = inlink->dst;
     151           0 :     AVFilterLink *outlink = ctx->outputs[0];
     152           0 :     DecimateContext *dm   = ctx->priv;
     153             :     AVFrame *prv;
     154             : 
     155             :     /* update frames queue(s) */
     156           0 :     if (FF_INLINK_IDX(inlink) == INPUT_MAIN) {
     157           0 :         dm->queue[dm->fid].frame = in;
     158           0 :         dm->got_frame[INPUT_MAIN] = 1;
     159             :     } else {
     160           0 :         dm->clean_src[dm->fid] = in;
     161           0 :         dm->got_frame[INPUT_CLEANSRC] = 1;
     162             :     }
     163           0 :     if (!dm->got_frame[INPUT_MAIN] || (dm->ppsrc && !dm->got_frame[INPUT_CLEANSRC]))
     164           0 :         return 0;
     165           0 :     dm->got_frame[INPUT_MAIN] = dm->got_frame[INPUT_CLEANSRC] = 0;
     166             : 
     167           0 :     if (dm->ppsrc)
     168           0 :         in = dm->clean_src[dm->fid];
     169             : 
     170           0 :     if (in) {
     171             :         /* update frame metrics */
     172           0 :         prv = dm->fid ? (dm->ppsrc ? dm->clean_src[dm->fid - 1] : dm->queue[dm->fid - 1].frame) : dm->last;
     173           0 :         if (!prv) {
     174           0 :             dm->queue[dm->fid].maxbdiff = INT64_MAX;
     175           0 :             dm->queue[dm->fid].totdiff  = INT64_MAX;
     176             :         } else {
     177           0 :             calc_diffs(dm, &dm->queue[dm->fid], prv, in);
     178             :         }
     179           0 :         if (++dm->fid != dm->cycle)
     180           0 :             return 0;
     181           0 :         av_frame_free(&dm->last);
     182           0 :         dm->last = av_frame_clone(in);
     183           0 :         dm->fid = 0;
     184             : 
     185             :         /* we have a complete cycle, select the frame to drop */
     186           0 :         lowest = 0;
     187           0 :         for (i = 0; i < dm->cycle; i++) {
     188           0 :             if (dm->queue[i].totdiff > dm->scthresh)
     189           0 :                 scpos = i;
     190           0 :             if (dm->queue[i].maxbdiff < dm->queue[lowest].maxbdiff)
     191           0 :                 lowest = i;
     192             :         }
     193           0 :         if (dm->queue[lowest].maxbdiff < dm->dupthresh)
     194           0 :             duppos = lowest;
     195           0 :         drop = scpos >= 0 && duppos < 0 ? scpos : lowest;
     196             :     }
     197             : 
     198             :     /* metrics debug */
     199           0 :     if (av_log_get_level() >= AV_LOG_DEBUG) {
     200           0 :         av_log(ctx, AV_LOG_DEBUG, "1/%d frame drop:\n", dm->cycle);
     201           0 :         for (i = 0; i < dm->cycle && dm->queue[i].frame; i++) {
     202           0 :             av_log(ctx, AV_LOG_DEBUG,"  #%d: totdiff=%08"PRIx64" maxbdiff=%08"PRIx64"%s%s%s%s\n",
     203           0 :                    i + 1, dm->queue[i].totdiff, dm->queue[i].maxbdiff,
     204             :                    i == scpos  ? " sc"     : "",
     205             :                    i == duppos ? " dup"    : "",
     206             :                    i == lowest ? " lowest" : "",
     207             :                    i == drop   ? " [DROP]" : "");
     208             :         }
     209             :     }
     210             : 
     211             :     /* push all frames except the drop */
     212           0 :     ret = 0;
     213           0 :     for (i = 0; i < dm->cycle && dm->queue[i].frame; i++) {
     214           0 :         if (i == drop) {
     215           0 :             if (dm->ppsrc)
     216           0 :                 av_frame_free(&dm->clean_src[i]);
     217           0 :             av_frame_free(&dm->queue[i].frame);
     218             :         } else {
     219           0 :             AVFrame *frame = dm->queue[i].frame;
     220           0 :             if (frame->pts != AV_NOPTS_VALUE && dm->start_pts == AV_NOPTS_VALUE)
     221           0 :                 dm->start_pts = frame->pts;
     222           0 :             if (dm->ppsrc) {
     223           0 :                 av_frame_free(&frame);
     224           0 :                 frame = dm->clean_src[i];
     225             :             }
     226           0 :             frame->pts = av_rescale_q(outlink->frame_count_in, dm->ts_unit, (AVRational){1,1}) +
     227           0 :                          (dm->start_pts == AV_NOPTS_VALUE ? 0 : dm->start_pts);
     228           0 :             ret = ff_filter_frame(outlink, frame);
     229           0 :             if (ret < 0)
     230           0 :                 break;
     231             :         }
     232             :     }
     233             : 
     234           0 :     return ret;
     235             : }
     236             : 
     237           0 : static int config_input(AVFilterLink *inlink)
     238             : {
     239             :     int max_value;
     240           0 :     AVFilterContext *ctx = inlink->dst;
     241           0 :     DecimateContext *dm = ctx->priv;
     242           0 :     const AVPixFmtDescriptor *pix_desc = av_pix_fmt_desc_get(inlink->format);
     243           0 :     const int w = inlink->w;
     244           0 :     const int h = inlink->h;
     245             : 
     246           0 :     dm->hsub      = pix_desc->log2_chroma_w;
     247           0 :     dm->vsub      = pix_desc->log2_chroma_h;
     248           0 :     dm->depth     = pix_desc->comp[0].depth;
     249           0 :     max_value     = (1 << dm->depth) - 1;
     250           0 :     dm->scthresh  = (int64_t)(((int64_t)max_value *          w * h          * dm->scthresh_flt)  / 100);
     251           0 :     dm->dupthresh = (int64_t)(((int64_t)max_value * dm->blockx * dm->blocky * dm->dupthresh_flt) / 100);
     252           0 :     dm->nxblocks  = (w + dm->blockx/2 - 1) / (dm->blockx/2);
     253           0 :     dm->nyblocks  = (h + dm->blocky/2 - 1) / (dm->blocky/2);
     254           0 :     dm->bdiffsize = dm->nxblocks * dm->nyblocks;
     255           0 :     dm->bdiffs    = av_malloc_array(dm->bdiffsize, sizeof(*dm->bdiffs));
     256           0 :     dm->queue     = av_calloc(dm->cycle, sizeof(*dm->queue));
     257             : 
     258           0 :     if (!dm->bdiffs || !dm->queue)
     259           0 :         return AVERROR(ENOMEM);
     260             : 
     261           0 :     if (dm->ppsrc) {
     262           0 :         dm->clean_src = av_calloc(dm->cycle, sizeof(*dm->clean_src));
     263           0 :         if (!dm->clean_src)
     264           0 :             return AVERROR(ENOMEM);
     265             :     }
     266             : 
     267           0 :     return 0;
     268             : }
     269             : 
     270           0 : static av_cold int decimate_init(AVFilterContext *ctx)
     271             : {
     272           0 :     DecimateContext *dm = ctx->priv;
     273           0 :     AVFilterPad pad = {
     274           0 :         .name         = av_strdup("main"),
     275             :         .type         = AVMEDIA_TYPE_VIDEO,
     276             :         .filter_frame = filter_frame,
     277             :         .config_props = config_input,
     278             :     };
     279             :     int ret;
     280             : 
     281           0 :     if (!pad.name)
     282           0 :         return AVERROR(ENOMEM);
     283           0 :     if ((ret = ff_insert_inpad(ctx, INPUT_MAIN, &pad)) < 0) {
     284           0 :         av_freep(&pad.name);
     285           0 :         return ret;
     286             :     }
     287             : 
     288           0 :     if (dm->ppsrc) {
     289           0 :         pad.name = av_strdup("clean_src");
     290           0 :         pad.config_props = NULL;
     291           0 :         if (!pad.name)
     292           0 :             return AVERROR(ENOMEM);
     293           0 :         if ((ret = ff_insert_inpad(ctx, INPUT_CLEANSRC, &pad)) < 0) {
     294           0 :             av_freep(&pad.name);
     295           0 :             return ret;
     296             :         }
     297             :     }
     298             : 
     299           0 :     if ((dm->blockx & (dm->blockx - 1)) ||
     300           0 :         (dm->blocky & (dm->blocky - 1))) {
     301           0 :         av_log(ctx, AV_LOG_ERROR, "blockx and blocky settings must be power of two\n");
     302           0 :         return AVERROR(EINVAL);
     303             :     }
     304             : 
     305           0 :     dm->start_pts = AV_NOPTS_VALUE;
     306             : 
     307           0 :     return 0;
     308             : }
     309             : 
     310           0 : static av_cold void decimate_uninit(AVFilterContext *ctx)
     311             : {
     312             :     int i;
     313           0 :     DecimateContext *dm = ctx->priv;
     314             : 
     315           0 :     av_frame_free(&dm->last);
     316           0 :     av_freep(&dm->bdiffs);
     317           0 :     av_freep(&dm->queue);
     318           0 :     av_freep(&dm->clean_src);
     319           0 :     for (i = 0; i < ctx->nb_inputs; i++)
     320           0 :         av_freep(&ctx->input_pads[i].name);
     321           0 : }
     322             : 
     323           0 : static int request_inlink(AVFilterContext *ctx, int lid)
     324             : {
     325           0 :     int ret = 0;
     326           0 :     DecimateContext *dm = ctx->priv;
     327             : 
     328           0 :     if (!dm->got_frame[lid]) {
     329           0 :         AVFilterLink *inlink = ctx->inputs[lid];
     330           0 :         ret = ff_request_frame(inlink);
     331           0 :         if (ret == AVERROR_EOF) { // flushing
     332           0 :             dm->eof |= 1 << lid;
     333           0 :             ret = filter_frame(inlink, NULL);
     334             :         }
     335             :     }
     336           0 :     return ret;
     337             : }
     338             : 
     339           0 : static int request_frame(AVFilterLink *outlink)
     340             : {
     341             :     int ret;
     342           0 :     AVFilterContext *ctx = outlink->src;
     343           0 :     DecimateContext *dm = ctx->priv;
     344           0 :     const uint32_t eof_mask = 1<<INPUT_MAIN | dm->ppsrc<<INPUT_CLEANSRC;
     345             : 
     346           0 :     if ((dm->eof & eof_mask) == eof_mask) // flush done?
     347           0 :         return AVERROR_EOF;
     348           0 :     if ((ret = request_inlink(ctx, INPUT_MAIN)) < 0)
     349           0 :         return ret;
     350           0 :     if (dm->ppsrc && (ret = request_inlink(ctx, INPUT_CLEANSRC)) < 0)
     351           0 :         return ret;
     352           0 :     return 0;
     353             : }
     354             : 
     355           0 : static int query_formats(AVFilterContext *ctx)
     356             : {
     357             :     static const enum AVPixelFormat pix_fmts[] = {
     358             : #define PF_NOALPHA(suf) AV_PIX_FMT_YUV420##suf,  AV_PIX_FMT_YUV422##suf,  AV_PIX_FMT_YUV444##suf
     359             : #define PF_ALPHA(suf)   AV_PIX_FMT_YUVA420##suf, AV_PIX_FMT_YUVA422##suf, AV_PIX_FMT_YUVA444##suf
     360             : #define PF(suf)         PF_NOALPHA(suf), PF_ALPHA(suf)
     361             :         PF(P), PF(P9), PF(P10), PF_NOALPHA(P12), PF_NOALPHA(P14), PF(P16),
     362             :         AV_PIX_FMT_YUV440P, AV_PIX_FMT_YUV411P, AV_PIX_FMT_YUV410P,
     363             :         AV_PIX_FMT_GRAY8, AV_PIX_FMT_GRAY16,
     364             :         AV_PIX_FMT_NONE
     365             :     };
     366           0 :     AVFilterFormats *fmts_list = ff_make_format_list(pix_fmts);
     367           0 :     if (!fmts_list)
     368           0 :         return AVERROR(ENOMEM);
     369           0 :     return ff_set_common_formats(ctx, fmts_list);
     370             : }
     371             : 
     372           0 : static int config_output(AVFilterLink *outlink)
     373             : {
     374           0 :     AVFilterContext *ctx = outlink->src;
     375           0 :     DecimateContext *dm = ctx->priv;
     376           0 :     const AVFilterLink *inlink =
     377           0 :         ctx->inputs[dm->ppsrc ? INPUT_CLEANSRC : INPUT_MAIN];
     378           0 :     AVRational fps = inlink->frame_rate;
     379             : 
     380           0 :     if (!fps.num || !fps.den) {
     381           0 :         av_log(ctx, AV_LOG_ERROR, "The input needs a constant frame rate; "
     382             :                "current rate of %d/%d is invalid\n", fps.num, fps.den);
     383           0 :         return AVERROR(EINVAL);
     384             :     }
     385           0 :     fps = av_mul_q(fps, (AVRational){dm->cycle - 1, dm->cycle});
     386           0 :     av_log(ctx, AV_LOG_VERBOSE, "FPS: %d/%d -> %d/%d\n",
     387             :            inlink->frame_rate.num, inlink->frame_rate.den, fps.num, fps.den);
     388           0 :     outlink->time_base  = inlink->time_base;
     389           0 :     outlink->frame_rate = fps;
     390           0 :     outlink->sample_aspect_ratio = inlink->sample_aspect_ratio;
     391           0 :     outlink->w = inlink->w;
     392           0 :     outlink->h = inlink->h;
     393           0 :     dm->ts_unit = av_inv_q(av_mul_q(fps, outlink->time_base));
     394           0 :     return 0;
     395             : }
     396             : 
     397             : static const AVFilterPad decimate_outputs[] = {
     398             :     {
     399             :         .name          = "default",
     400             :         .type          = AVMEDIA_TYPE_VIDEO,
     401             :         .request_frame = request_frame,
     402             :         .config_props  = config_output,
     403             :     },
     404             :     { NULL }
     405             : };
     406             : 
     407             : AVFilter ff_vf_decimate = {
     408             :     .name          = "decimate",
     409             :     .description   = NULL_IF_CONFIG_SMALL("Decimate frames (post field matching filter)."),
     410             :     .init          = decimate_init,
     411             :     .uninit        = decimate_uninit,
     412             :     .priv_size     = sizeof(DecimateContext),
     413             :     .query_formats = query_formats,
     414             :     .outputs       = decimate_outputs,
     415             :     .priv_class    = &decimate_class,
     416             :     .flags         = AVFILTER_FLAG_DYNAMIC_INPUTS,
     417             : };

Generated by: LCOV version 1.13