Subversion Repositories Kolibri OS

Rev

Go to most recent revision | Blame | Last modification | View Log | RSS feed

  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 {
  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.     double ts_unit;         ///< timestamp units for the output frames
  46.     uint32_t eof;           ///< bitmask for end of stream
  47.     int hsub, vsub;         ///< chroma subsampling values
  48.     int depth;
  49.     int nxblocks, nyblocks;
  50.     int bdiffsize;
  51.     int64_t *bdiffs;
  52.  
  53.     /* options */
  54.     int cycle;
  55.     double dupthresh_flt;
  56.     double scthresh_flt;
  57.     int64_t dupthresh;
  58.     int64_t scthresh;
  59.     int blockx, blocky;
  60.     int ppsrc;
  61.     int chroma;
  62. } DecimateContext;
  63.  
  64. #define OFFSET(x) offsetof(DecimateContext, x)
  65. #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
  66.  
  67. static const AVOption decimate_options[] = {
  68.     { "cycle",     "set the number of frame from which one will be dropped", OFFSET(cycle), AV_OPT_TYPE_INT, {.i64 = 5}, 2, 25, FLAGS },
  69.     { "dupthresh", "set duplicate threshold",    OFFSET(dupthresh_flt), AV_OPT_TYPE_DOUBLE, {.dbl =  1.1}, 0, 100, FLAGS },
  70.     { "scthresh",  "set scene change threshold", OFFSET(scthresh_flt),  AV_OPT_TYPE_DOUBLE, {.dbl = 15.0}, 0, 100, FLAGS },
  71.     { "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 },
  72.     { "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 },
  73.     { "ppsrc",     "mark main input as a pre-processed input and activate clean source input stream", OFFSET(ppsrc), AV_OPT_TYPE_INT, {.i64=0}, 0, 1, FLAGS },
  74.     { "chroma",    "set whether or not chroma is considered in the metric calculations", OFFSET(chroma), AV_OPT_TYPE_INT, {.i64=1}, 0, 1, FLAGS },
  75.     { NULL }
  76. };
  77.  
  78. AVFILTER_DEFINE_CLASS(decimate);
  79.  
  80. static void calc_diffs(const DecimateContext *dm, struct qitem *q,
  81.                        const AVFrame *f1, const AVFrame *f2)
  82. {
  83.     int64_t maxdiff = -1;
  84.     int64_t *bdiffs = dm->bdiffs;
  85.     int plane, i, j;
  86.  
  87.     memset(bdiffs, 0, dm->bdiffsize * sizeof(*bdiffs));
  88.  
  89.     for (plane = 0; plane < (dm->chroma && f1->data[2] ? 3 : 1); plane++) {
  90.         int x, y, xl;
  91.         const int linesize1 = f1->linesize[plane];
  92.         const int linesize2 = f2->linesize[plane];
  93.         const uint8_t *f1p = f1->data[plane];
  94.         const uint8_t *f2p = f2->data[plane];
  95.         int width    = plane ? FF_CEIL_RSHIFT(f1->width,  dm->hsub) : f1->width;
  96.         int height   = plane ? FF_CEIL_RSHIFT(f1->height, dm->vsub) : f1->height;
  97.         int hblockx  = dm->blockx / 2;
  98.         int hblocky  = dm->blocky / 2;
  99.  
  100.         if (plane) {
  101.             hblockx >>= dm->hsub;
  102.             hblocky >>= dm->vsub;
  103.         }
  104.  
  105.         for (y = 0; y < height; y++) {
  106.             int ydest = y / hblocky;
  107.             int xdest = 0;
  108.  
  109. #define CALC_DIFF(nbits) do {                               \
  110.     for (x = 0; x < width; x += hblockx) {                  \
  111.         int64_t acc = 0;                                    \
  112.         int m = FFMIN(width, x + hblockx);                  \
  113.         for (xl = x; xl < m; xl++)                          \
  114.             acc += abs(((const uint##nbits##_t *)f1p)[xl] - \
  115.                        ((const uint##nbits##_t *)f2p)[xl]); \
  116.         bdiffs[ydest * dm->nxblocks + xdest] += acc;        \
  117.         xdest++;                                            \
  118.     }                                                       \
  119. } while (0)
  120.             if (dm->depth == 8) CALC_DIFF(8);
  121.             else                CALC_DIFF(16);
  122.  
  123.             f1p += linesize1;
  124.             f2p += linesize2;
  125.         }
  126.     }
  127.  
  128.     for (i = 0; i < dm->nyblocks - 1; i++) {
  129.         for (j = 0; j < dm->nxblocks - 1; j++) {
  130.             int64_t tmp = bdiffs[      i * dm->nxblocks + j    ]
  131.                         + bdiffs[      i * dm->nxblocks + j + 1]
  132.                         + bdiffs[(i + 1) * dm->nxblocks + j    ]
  133.                         + bdiffs[(i + 1) * dm->nxblocks + j + 1];
  134.             if (tmp > maxdiff)
  135.                 maxdiff = tmp;
  136.         }
  137.     }
  138.  
  139.     q->totdiff = 0;
  140.     for (i = 0; i < dm->bdiffsize; i++)
  141.         q->totdiff += bdiffs[i];
  142.     q->maxbdiff = maxdiff;
  143. }
  144.  
  145. static int filter_frame(AVFilterLink *inlink, AVFrame *in)
  146. {
  147.     int scpos = -1, duppos = -1;
  148.     int drop = INT_MIN, i, lowest = 0, ret;
  149.     AVFilterContext *ctx  = inlink->dst;
  150.     AVFilterLink *outlink = ctx->outputs[0];
  151.     DecimateContext *dm   = ctx->priv;
  152.     AVFrame *prv;
  153.  
  154.     /* update frames queue(s) */
  155.     if (FF_INLINK_IDX(inlink) == INPUT_MAIN) {
  156.         dm->queue[dm->fid].frame = in;
  157.         dm->got_frame[INPUT_MAIN] = 1;
  158.     } else {
  159.         dm->clean_src[dm->fid] = in;
  160.         dm->got_frame[INPUT_CLEANSRC] = 1;
  161.     }
  162.     if (!dm->got_frame[INPUT_MAIN] || (dm->ppsrc && !dm->got_frame[INPUT_CLEANSRC]))
  163.         return 0;
  164.     dm->got_frame[INPUT_MAIN] = dm->got_frame[INPUT_CLEANSRC] = 0;
  165.  
  166.     if (in) {
  167.         /* update frame metrics */
  168.         prv = dm->fid ? dm->queue[dm->fid - 1].frame : dm->last;
  169.         if (!prv)
  170.             prv = in;
  171.         calc_diffs(dm, &dm->queue[dm->fid], prv, in);
  172.         if (++dm->fid != dm->cycle)
  173.             return 0;
  174.         av_frame_free(&dm->last);
  175.         dm->last = av_frame_clone(in);
  176.         dm->fid = 0;
  177.  
  178.         /* we have a complete cycle, select the frame to drop */
  179.         lowest = 0;
  180.         for (i = 0; i < dm->cycle; i++) {
  181.             if (dm->queue[i].totdiff > dm->scthresh)
  182.                 scpos = i;
  183.             if (dm->queue[i].maxbdiff < dm->queue[lowest].maxbdiff)
  184.                 lowest = i;
  185.         }
  186.         if (dm->queue[lowest].maxbdiff < dm->dupthresh)
  187.             duppos = lowest;
  188.         drop = scpos >= 0 && duppos < 0 ? scpos : lowest;
  189.     }
  190.  
  191.     /* metrics debug */
  192.     if (av_log_get_level() >= AV_LOG_DEBUG) {
  193.         av_log(ctx, AV_LOG_DEBUG, "1/%d frame drop:\n", dm->cycle);
  194.         for (i = 0; i < dm->cycle && dm->queue[i].frame; i++) {
  195.             av_log(ctx, AV_LOG_DEBUG,"  #%d: totdiff=%08"PRIx64" maxbdiff=%08"PRIx64"%s%s%s%s\n",
  196.                    i + 1, dm->queue[i].totdiff, dm->queue[i].maxbdiff,
  197.                    i == scpos  ? " sc"     : "",
  198.                    i == duppos ? " dup"    : "",
  199.                    i == lowest ? " lowest" : "",
  200.                    i == drop   ? " [DROP]" : "");
  201.         }
  202.     }
  203.  
  204.     /* push all frames except the drop */
  205.     ret = 0;
  206.     for (i = 0; i < dm->cycle && dm->queue[i].frame; i++) {
  207.         if (i == drop) {
  208.             if (dm->ppsrc)
  209.                 av_frame_free(&dm->clean_src[i]);
  210.             av_frame_free(&dm->queue[i].frame);
  211.         } else {
  212.             AVFrame *frame = dm->queue[i].frame;
  213.             if (dm->ppsrc) {
  214.                 av_frame_free(&frame);
  215.                 frame = dm->clean_src[i];
  216.             }
  217.             frame->pts = outlink->frame_count * dm->ts_unit;
  218.             ret = ff_filter_frame(outlink, frame);
  219.             if (ret < 0)
  220.                 break;
  221.         }
  222.     }
  223.  
  224.     return ret;
  225. }
  226.  
  227. static int config_input(AVFilterLink *inlink)
  228. {
  229.     int max_value;
  230.     AVFilterContext *ctx = inlink->dst;
  231.     DecimateContext *dm = ctx->priv;
  232.     const AVPixFmtDescriptor *pix_desc = av_pix_fmt_desc_get(inlink->format);
  233.     const int w = inlink->w;
  234.     const int h = inlink->h;
  235.  
  236.     dm->hsub      = pix_desc->log2_chroma_w;
  237.     dm->vsub      = pix_desc->log2_chroma_h;
  238.     dm->depth     = pix_desc->comp[0].depth_minus1 + 1;
  239.     max_value     = (1 << dm->depth) - 1;
  240.     dm->scthresh  = (int64_t)(((int64_t)max_value *          w * h          * dm->scthresh_flt)  / 100);
  241.     dm->dupthresh = (int64_t)(((int64_t)max_value * dm->blockx * dm->blocky * dm->dupthresh_flt) / 100);
  242.     dm->nxblocks  = (w + dm->blockx/2 - 1) / (dm->blockx/2);
  243.     dm->nyblocks  = (h + dm->blocky/2 - 1) / (dm->blocky/2);
  244.     dm->bdiffsize = dm->nxblocks * dm->nyblocks;
  245.     dm->bdiffs    = av_malloc(dm->bdiffsize * sizeof(*dm->bdiffs));
  246.     dm->queue     = av_calloc(dm->cycle, sizeof(*dm->queue));
  247.  
  248.     if (!dm->bdiffs || !dm->queue)
  249.         return AVERROR(ENOMEM);
  250.  
  251.     if (dm->ppsrc) {
  252.         dm->clean_src = av_calloc(dm->cycle, sizeof(*dm->clean_src));
  253.         if (!dm->clean_src)
  254.             return AVERROR(ENOMEM);
  255.     }
  256.  
  257.     return 0;
  258. }
  259.  
  260. static av_cold int decimate_init(AVFilterContext *ctx)
  261. {
  262.     const DecimateContext *dm = ctx->priv;
  263.     AVFilterPad pad = {
  264.         .name         = av_strdup("main"),
  265.         .type         = AVMEDIA_TYPE_VIDEO,
  266.         .filter_frame = filter_frame,
  267.         .config_props = config_input,
  268.     };
  269.  
  270.     if (!pad.name)
  271.         return AVERROR(ENOMEM);
  272.     ff_insert_inpad(ctx, INPUT_MAIN, &pad);
  273.  
  274.     if (dm->ppsrc) {
  275.         pad.name = av_strdup("clean_src");
  276.         pad.config_props = NULL;
  277.         if (!pad.name)
  278.             return AVERROR(ENOMEM);
  279.         ff_insert_inpad(ctx, INPUT_CLEANSRC, &pad);
  280.     }
  281.  
  282.     if ((dm->blockx & (dm->blockx - 1)) ||
  283.         (dm->blocky & (dm->blocky - 1))) {
  284.         av_log(ctx, AV_LOG_ERROR, "blockx and blocky settings must be power of two\n");
  285.         return AVERROR(EINVAL);
  286.     }
  287.  
  288.     return 0;
  289. }
  290.  
  291. static av_cold void decimate_uninit(AVFilterContext *ctx)
  292. {
  293.     int i;
  294.     DecimateContext *dm = ctx->priv;
  295.  
  296.     av_frame_free(&dm->last);
  297.     av_freep(&dm->bdiffs);
  298.     av_freep(&dm->queue);
  299.     av_freep(&dm->clean_src);
  300.     for (i = 0; i < ctx->nb_inputs; i++)
  301.         av_freep(&ctx->input_pads[i].name);
  302. }
  303.  
  304. static int request_inlink(AVFilterContext *ctx, int lid)
  305. {
  306.     int ret = 0;
  307.     DecimateContext *dm = ctx->priv;
  308.  
  309.     if (!dm->got_frame[lid]) {
  310.         AVFilterLink *inlink = ctx->inputs[lid];
  311.         ret = ff_request_frame(inlink);
  312.         if (ret == AVERROR_EOF) { // flushing
  313.             dm->eof |= 1 << lid;
  314.             ret = filter_frame(inlink, NULL);
  315.         }
  316.     }
  317.     return ret;
  318. }
  319.  
  320. static int request_frame(AVFilterLink *outlink)
  321. {
  322.     int ret;
  323.     AVFilterContext *ctx = outlink->src;
  324.     DecimateContext *dm = ctx->priv;
  325.     const uint32_t eof_mask = 1<<INPUT_MAIN | dm->ppsrc<<INPUT_CLEANSRC;
  326.  
  327.     if ((dm->eof & eof_mask) == eof_mask) // flush done?
  328.         return AVERROR_EOF;
  329.     if ((ret = request_inlink(ctx, INPUT_MAIN)) < 0)
  330.         return ret;
  331.     if (dm->ppsrc && (ret = request_inlink(ctx, INPUT_CLEANSRC)) < 0)
  332.         return ret;
  333.     return 0;
  334. }
  335.  
  336. static int query_formats(AVFilterContext *ctx)
  337. {
  338.     static const enum AVPixelFormat pix_fmts[] = {
  339. #define PF_NOALPHA(suf) AV_PIX_FMT_YUV420##suf,  AV_PIX_FMT_YUV422##suf,  AV_PIX_FMT_YUV444##suf
  340. #define PF_ALPHA(suf)   AV_PIX_FMT_YUVA420##suf, AV_PIX_FMT_YUVA422##suf, AV_PIX_FMT_YUVA444##suf
  341. #define PF(suf)         PF_NOALPHA(suf), PF_ALPHA(suf)
  342.         PF(P), PF(P9), PF(P10), PF_NOALPHA(P12), PF_NOALPHA(P14), PF(P16),
  343.         AV_PIX_FMT_YUV440P, AV_PIX_FMT_YUV411P, AV_PIX_FMT_YUV410P,
  344.         AV_PIX_FMT_GRAY8, AV_PIX_FMT_GRAY16,
  345.         AV_PIX_FMT_NONE
  346.     };
  347.     ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
  348.     return 0;
  349. }
  350.  
  351. static int config_output(AVFilterLink *outlink)
  352. {
  353.     AVFilterContext *ctx = outlink->src;
  354.     DecimateContext *dm = ctx->priv;
  355.     const AVFilterLink *inlink =
  356.         ctx->inputs[dm->ppsrc ? INPUT_CLEANSRC : INPUT_MAIN];
  357.     AVRational fps = inlink->frame_rate;
  358.  
  359.     if (!fps.num || !fps.den) {
  360.         av_log(ctx, AV_LOG_ERROR, "The input needs a constant frame rate; "
  361.                "current rate of %d/%d is invalid\n", fps.num, fps.den);
  362.         return AVERROR(EINVAL);
  363.     }
  364.     fps = av_mul_q(fps, (AVRational){dm->cycle - 1, dm->cycle});
  365.     av_log(ctx, AV_LOG_VERBOSE, "FPS: %d/%d -> %d/%d\n",
  366.            inlink->frame_rate.num, inlink->frame_rate.den, fps.num, fps.den);
  367.     outlink->flags |= FF_LINK_FLAG_REQUEST_LOOP;
  368.     outlink->time_base  = inlink->time_base;
  369.     outlink->frame_rate = fps;
  370.     outlink->sample_aspect_ratio = inlink->sample_aspect_ratio;
  371.     outlink->w = inlink->w;
  372.     outlink->h = inlink->h;
  373.     dm->ts_unit = av_q2d(av_inv_q(av_mul_q(fps, outlink->time_base)));
  374.     return 0;
  375. }
  376.  
  377. static const AVFilterPad decimate_outputs[] = {
  378.     {
  379.         .name          = "default",
  380.         .type          = AVMEDIA_TYPE_VIDEO,
  381.         .request_frame = request_frame,
  382.         .config_props  = config_output,
  383.     },
  384.     { NULL }
  385. };
  386.  
  387. AVFilter avfilter_vf_decimate = {
  388.     .name          = "decimate",
  389.     .description   = NULL_IF_CONFIG_SMALL("Decimate frames (post field matching filter)."),
  390.     .init          = decimate_init,
  391.     .uninit        = decimate_uninit,
  392.     .priv_size     = sizeof(DecimateContext),
  393.     .query_formats = query_formats,
  394.     .outputs       = decimate_outputs,
  395.     .priv_class    = &decimate_class,
  396.     .flags         = AVFILTER_FLAG_DYNAMIC_INPUTS,
  397. };
  398.