2 * Copyright (c) 2003 Rich Felker
3 * Copyright (c) 2012 Stefano Sabatini
5 * This file is part of FFmpeg.
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.
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.
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.
23 * @file mpdecimate filter, ported from libmpcodecs/vf_decimate.c by
27 #include "libavutil/opt.h"
28 #include "libavutil/pixdesc.h"
29 #include "libavutil/timestamp.h"
30 #include "libavcodec/dsputil.h"
38 int lo, hi; ///< lower and higher threshold number of differences
39 ///< values for 8x8 blocks
41 float frac; ///< threshold of changed pixels over the total fraction
43 int max_drop_count; ///< if positive: maximum number of sequential frames to drop
44 ///< if negative: minimum number of frames between two drops
46 int drop_count; ///< if positive: number of frames sequentially dropped
47 ///< if negative: number of sequential frames which were not dropped
49 int hsub, vsub; ///< chroma subsampling values
50 AVFrame *ref; ///< reference picture
51 DSPContext dspctx; ///< context providing optimized diff routines
52 AVCodecContext *avctx; ///< codec context required for the DSPContext
55 #define OFFSET(x) offsetof(DecimateContext, x)
56 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
58 static const AVOption mpdecimate_options[] = {
59 { "max", "set the maximum number of consecutive dropped frames (positive), or the minimum interval between dropped frames (negative)",
60 OFFSET(max_drop_count), AV_OPT_TYPE_INT, {.i64=0}, INT_MIN, INT_MAX, FLAGS },
61 { "hi", "set high dropping threshold", OFFSET(hi), AV_OPT_TYPE_INT, {.i64=64*12}, INT_MIN, INT_MAX, FLAGS },
62 { "lo", "set low dropping threshold", OFFSET(lo), AV_OPT_TYPE_INT, {.i64=64*5}, INT_MIN, INT_MAX, FLAGS },
63 { "frac", "set fraction dropping threshold", OFFSET(frac), AV_OPT_TYPE_FLOAT, {.dbl=0.33}, 0, 1, FLAGS },
67 AVFILTER_DEFINE_CLASS(mpdecimate);
70 * Return 1 if the two planes are different, 0 otherwise.
72 static int diff_planes(AVFilterContext *ctx,
73 uint8_t *cur, uint8_t *ref, int linesize,
76 DecimateContext *decimate = ctx->priv;
77 DSPContext *dspctx = &decimate->dspctx;
81 int t = (w/16)*(h/16)*decimate->frac;
84 /* compute difference for blocks of 8x8 bytes */
85 for (y = 0; y < h-7; y += 4) {
86 for (x = 8; x < w-7; x += 4) {
87 dspctx->diff_pixels(block,
89 ref+x+y*linesize, linesize);
90 d = dspctx->sum_abs_dctelem(block);
93 if (d > decimate->lo) {
104 * Tell if the frame should be decimated, for example if it is no much
105 * different with respect to the reference frame ref.
107 static int decimate_frame(AVFilterContext *ctx,
108 AVFrame *cur, AVFrame *ref)
110 DecimateContext *decimate = ctx->priv;
113 if (decimate->max_drop_count > 0 &&
114 decimate->drop_count >= decimate->max_drop_count)
116 if (decimate->max_drop_count < 0 &&
117 (decimate->drop_count-1) > decimate->max_drop_count)
120 for (plane = 0; ref->data[plane] && ref->linesize[plane]; plane++) {
121 int vsub = plane == 1 || plane == 2 ? decimate->vsub : 0;
122 int hsub = plane == 1 || plane == 2 ? decimate->hsub : 0;
124 cur->data[plane], ref->data[plane], ref->linesize[plane],
125 FF_CEIL_RSHIFT(ref->width, hsub),
126 FF_CEIL_RSHIFT(ref->height, vsub)))
133 static av_cold int init(AVFilterContext *ctx)
135 DecimateContext *decimate = ctx->priv;
137 av_log(ctx, AV_LOG_VERBOSE, "max_drop_count:%d hi:%d lo:%d frac:%f\n",
138 decimate->max_drop_count, decimate->hi, decimate->lo, decimate->frac);
140 decimate->avctx = avcodec_alloc_context3(NULL);
141 if (!decimate->avctx)
142 return AVERROR(ENOMEM);
143 avpriv_dsputil_init(&decimate->dspctx, decimate->avctx);
148 static av_cold void uninit(AVFilterContext *ctx)
150 DecimateContext *decimate = ctx->priv;
151 av_frame_free(&decimate->ref);
152 if (decimate->avctx) {
153 avcodec_close(decimate->avctx);
154 av_freep(&decimate->avctx);
158 static int query_formats(AVFilterContext *ctx)
160 static const enum AVPixelFormat pix_fmts[] = {
161 AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV422P,
162 AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV411P,
163 AV_PIX_FMT_YUV410P, AV_PIX_FMT_YUV440P,
164 AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ422P,
165 AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ440P,
170 ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
175 static int config_input(AVFilterLink *inlink)
177 AVFilterContext *ctx = inlink->dst;
178 DecimateContext *decimate = ctx->priv;
179 const AVPixFmtDescriptor *pix_desc = av_pix_fmt_desc_get(inlink->format);
180 decimate->hsub = pix_desc->log2_chroma_w;
181 decimate->vsub = pix_desc->log2_chroma_h;
186 static int filter_frame(AVFilterLink *inlink, AVFrame *cur)
188 DecimateContext *decimate = inlink->dst->priv;
189 AVFilterLink *outlink = inlink->dst->outputs[0];
192 if (decimate->ref && decimate_frame(inlink->dst, cur, decimate->ref)) {
193 decimate->drop_count = FFMAX(1, decimate->drop_count+1);
195 av_frame_free(&decimate->ref);
197 decimate->drop_count = FFMIN(-1, decimate->drop_count-1);
199 if (ret = ff_filter_frame(outlink, av_frame_clone(cur)) < 0)
203 av_log(inlink->dst, AV_LOG_DEBUG,
204 "%s pts:%s pts_time:%s drop_count:%d\n",
205 decimate->drop_count > 0 ? "drop" : "keep",
206 av_ts2str(cur->pts), av_ts2timestr(cur->pts, &inlink->time_base),
207 decimate->drop_count);
209 if (decimate->drop_count > 0)
215 static int request_frame(AVFilterLink *outlink)
217 DecimateContext *decimate = outlink->src->priv;
218 AVFilterLink *inlink = outlink->src->inputs[0];
222 ret = ff_request_frame(inlink);
223 } while (decimate->drop_count > 0 && ret >= 0);
228 static const AVFilterPad mpdecimate_inputs[] = {
231 .type = AVMEDIA_TYPE_VIDEO,
232 .config_props = config_input,
233 .filter_frame = filter_frame,
238 static const AVFilterPad mpdecimate_outputs[] = {
241 .type = AVMEDIA_TYPE_VIDEO,
242 .request_frame = request_frame,
247 AVFilter ff_vf_mpdecimate = {
248 .name = "mpdecimate",
249 .description = NULL_IF_CONFIG_SMALL("Remove near-duplicate frames."),
252 .priv_size = sizeof(DecimateContext),
253 .priv_class = &mpdecimate_class,
254 .query_formats = query_formats,
255 .inputs = mpdecimate_inputs,
256 .outputs = mpdecimate_outputs,