1/*
2 * Copyright (c) 2003 Rich Felker
3 * Copyright (c) 2012 Stefano Sabatini
4 *
5 * This file is part of FFmpeg.
6 *
7 * FFmpeg is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (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
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with FFmpeg; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 */
21
22/**
23 * @file mpdecimate filter, ported from libmpcodecs/vf_decimate.c by
24 * Rich Felker.
25 */
26
27#include "libavutil/opt.h"
28#include "libavutil/pixdesc.h"
29#include "libavutil/timestamp.h"
30#include "libavcodec/dsputil.h"
31#include "libavcodec/pixblockdsp.h"
32#include "avfilter.h"
33#include "internal.h"
34#include "formats.h"
35#include "video.h"
36
37typedef struct {
38    const AVClass *class;
39    int lo, hi;                    ///< lower and higher threshold number of differences
40                                   ///< values for 8x8 blocks
41
42    float frac;                    ///< threshold of changed pixels over the total fraction
43
44    int max_drop_count;            ///< if positive: maximum number of sequential frames to drop
45                                   ///< if negative: minimum number of frames between two drops
46
47    int drop_count;                ///< if positive: number of frames sequentially dropped
48                                   ///< if negative: number of sequential frames which were not dropped
49
50    int hsub, vsub;                ///< chroma subsampling values
51    AVFrame *ref;                  ///< reference picture
52    DSPContext dspctx;             ///< context providing optimized diff routines
53    PixblockDSPContext pdsp;
54    AVCodecContext *avctx;         ///< codec context required for the DSPContext
55} DecimateContext;
56
57#define OFFSET(x) offsetof(DecimateContext, x)
58#define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
59
60static const AVOption mpdecimate_options[] = {
61    { "max",  "set the maximum number of consecutive dropped frames (positive), or the minimum interval between dropped frames (negative)",
62      OFFSET(max_drop_count), AV_OPT_TYPE_INT, {.i64=0}, INT_MIN, INT_MAX, FLAGS },
63    { "hi",   "set high dropping threshold", OFFSET(hi), AV_OPT_TYPE_INT, {.i64=64*12}, INT_MIN, INT_MAX, FLAGS },
64    { "lo",   "set low dropping threshold", OFFSET(lo), AV_OPT_TYPE_INT, {.i64=64*5}, INT_MIN, INT_MAX, FLAGS },
65    { "frac", "set fraction dropping threshold",  OFFSET(frac), AV_OPT_TYPE_FLOAT, {.dbl=0.33}, 0, 1, FLAGS },
66    { NULL }
67};
68
69AVFILTER_DEFINE_CLASS(mpdecimate);
70
71/**
72 * Return 1 if the two planes are different, 0 otherwise.
73 */
74static int diff_planes(AVFilterContext *ctx,
75                       uint8_t *cur, uint8_t *ref, int linesize,
76                       int w, int h)
77{
78    DecimateContext *decimate = ctx->priv;
79    DSPContext *dspctx = &decimate->dspctx;
80    PixblockDSPContext *pdsp = &decimate->pdsp;
81
82    int x, y;
83    int d, c = 0;
84    int t = (w/16)*(h/16)*decimate->frac;
85    int16_t block[8*8];
86
87    /* compute difference for blocks of 8x8 bytes */
88    for (y = 0; y < h-7; y += 4) {
89        for (x = 8; x < w-7; x += 4) {
90            pdsp->diff_pixels(block,
91                                cur+x+y*linesize,
92                                ref+x+y*linesize, linesize);
93            d = dspctx->sum_abs_dctelem(block);
94            if (d > decimate->hi)
95                return 1;
96            if (d > decimate->lo) {
97                c++;
98                if (c > t)
99                    return 1;
100            }
101        }
102    }
103    return 0;
104}
105
106/**
107 * Tell if the frame should be decimated, for example if it is no much
108 * different with respect to the reference frame ref.
109 */
110static int decimate_frame(AVFilterContext *ctx,
111                          AVFrame *cur, AVFrame *ref)
112{
113    DecimateContext *decimate = ctx->priv;
114    int plane;
115
116    if (decimate->max_drop_count > 0 &&
117        decimate->drop_count >= decimate->max_drop_count)
118        return 0;
119    if (decimate->max_drop_count < 0 &&
120        (decimate->drop_count-1) > decimate->max_drop_count)
121        return 0;
122
123    for (plane = 0; ref->data[plane] && ref->linesize[plane]; plane++) {
124        int vsub = plane == 1 || plane == 2 ? decimate->vsub : 0;
125        int hsub = plane == 1 || plane == 2 ? decimate->hsub : 0;
126        if (diff_planes(ctx,
127                        cur->data[plane], ref->data[plane], ref->linesize[plane],
128                        FF_CEIL_RSHIFT(ref->width,  hsub),
129                        FF_CEIL_RSHIFT(ref->height, vsub)))
130            return 0;
131    }
132
133    return 1;
134}
135
136static av_cold int init(AVFilterContext *ctx)
137{
138    DecimateContext *decimate = ctx->priv;
139
140    av_log(ctx, AV_LOG_VERBOSE, "max_drop_count:%d hi:%d lo:%d frac:%f\n",
141           decimate->max_drop_count, decimate->hi, decimate->lo, decimate->frac);
142
143    decimate->avctx = avcodec_alloc_context3(NULL);
144    if (!decimate->avctx)
145        return AVERROR(ENOMEM);
146    avpriv_dsputil_init(&decimate->dspctx, decimate->avctx);
147    ff_pixblockdsp_init(&decimate->pdsp, decimate->avctx);
148
149    return 0;
150}
151
152static av_cold void uninit(AVFilterContext *ctx)
153{
154    DecimateContext *decimate = ctx->priv;
155    av_frame_free(&decimate->ref);
156    if (decimate->avctx) {
157        avcodec_close(decimate->avctx);
158        av_freep(&decimate->avctx);
159    }
160}
161
162static int query_formats(AVFilterContext *ctx)
163{
164    static const enum AVPixelFormat pix_fmts[] = {
165        AV_PIX_FMT_YUV444P,      AV_PIX_FMT_YUV422P,
166        AV_PIX_FMT_YUV420P,      AV_PIX_FMT_YUV411P,
167        AV_PIX_FMT_YUV410P,      AV_PIX_FMT_YUV440P,
168        AV_PIX_FMT_YUVJ444P,     AV_PIX_FMT_YUVJ422P,
169        AV_PIX_FMT_YUVJ420P,     AV_PIX_FMT_YUVJ440P,
170        AV_PIX_FMT_YUVA420P,
171        AV_PIX_FMT_NONE
172    };
173
174    ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
175
176    return 0;
177}
178
179static int config_input(AVFilterLink *inlink)
180{
181    AVFilterContext *ctx = inlink->dst;
182    DecimateContext *decimate = ctx->priv;
183    const AVPixFmtDescriptor *pix_desc = av_pix_fmt_desc_get(inlink->format);
184    decimate->hsub = pix_desc->log2_chroma_w;
185    decimate->vsub = pix_desc->log2_chroma_h;
186
187    return 0;
188}
189
190static int filter_frame(AVFilterLink *inlink, AVFrame *cur)
191{
192    DecimateContext *decimate = inlink->dst->priv;
193    AVFilterLink *outlink = inlink->dst->outputs[0];
194    int ret;
195
196    if (decimate->ref && decimate_frame(inlink->dst, cur, decimate->ref)) {
197        decimate->drop_count = FFMAX(1, decimate->drop_count+1);
198    } else {
199        av_frame_free(&decimate->ref);
200        decimate->ref = cur;
201        decimate->drop_count = FFMIN(-1, decimate->drop_count-1);
202
203        if (ret = ff_filter_frame(outlink, av_frame_clone(cur)) < 0)
204            return ret;
205    }
206
207    av_log(inlink->dst, AV_LOG_DEBUG,
208           "%s pts:%s pts_time:%s drop_count:%d\n",
209           decimate->drop_count > 0 ? "drop" : "keep",
210           av_ts2str(cur->pts), av_ts2timestr(cur->pts, &inlink->time_base),
211           decimate->drop_count);
212
213    if (decimate->drop_count > 0)
214        av_frame_free(&cur);
215
216    return 0;
217}
218
219static int request_frame(AVFilterLink *outlink)
220{
221    DecimateContext *decimate = outlink->src->priv;
222    AVFilterLink *inlink = outlink->src->inputs[0];
223    int ret;
224
225    do {
226        ret = ff_request_frame(inlink);
227    } while (decimate->drop_count > 0 && ret >= 0);
228
229    return ret;
230}
231
232static const AVFilterPad mpdecimate_inputs[] = {
233    {
234        .name         = "default",
235        .type         = AVMEDIA_TYPE_VIDEO,
236        .config_props = config_input,
237        .filter_frame = filter_frame,
238    },
239    { NULL }
240};
241
242static const AVFilterPad mpdecimate_outputs[] = {
243    {
244        .name          = "default",
245        .type          = AVMEDIA_TYPE_VIDEO,
246        .request_frame = request_frame,
247    },
248    { NULL }
249};
250
251AVFilter ff_vf_mpdecimate = {
252    .name          = "mpdecimate",
253    .description   = NULL_IF_CONFIG_SMALL("Remove near-duplicate frames."),
254    .init          = init,
255    .uninit        = uninit,
256    .priv_size     = sizeof(DecimateContext),
257    .priv_class    = &mpdecimate_class,
258    .query_formats = query_formats,
259    .inputs        = mpdecimate_inputs,
260    .outputs       = mpdecimate_outputs,
261};
262