vf_mpdecimate.c
Go to the documentation of this file.
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 "avfilter.h"
32 #include "internal.h"
33 #include "formats.h"
34 #include "video.h"
35 
36 typedef struct {
37  const AVClass *class;
38  int lo, hi; ///< lower and higher threshold number of differences
39  ///< values for 8x8 blocks
40 
41  float frac; ///< threshold of changed pixels over the total fraction
42 
43  int max_drop_count; ///< if positive: maximum number of sequential frames to drop
44  ///< if negative: minimum number of frames between two drops
45 
46  int drop_count; ///< if positive: number of frames sequentially dropped
47  ///< if negative: number of sequential frames which were not dropped
48 
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
54 
55 #define OFFSET(x) offsetof(DecimateContext, x)
56 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
57 
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 },
64  { NULL }
65 };
66 
67 AVFILTER_DEFINE_CLASS(mpdecimate);
68 
69 /**
70  * Return 1 if the two planes are different, 0 otherwise.
71  */
72 static int diff_planes(AVFilterContext *ctx,
73  uint8_t *cur, uint8_t *ref, int linesize,
74  int w, int h)
75 {
76  DecimateContext *decimate = ctx->priv;
77  DSPContext *dspctx = &decimate->dspctx;
78 
79  int x, y;
80  int d, c = 0;
81  int t = (w/16)*(h/16)*decimate->frac;
82  int16_t block[8*8];
83 
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,
88  cur+x+y*linesize,
89  ref+x+y*linesize, linesize);
90  d = dspctx->sum_abs_dctelem(block);
91  if (d > decimate->hi)
92  return 1;
93  if (d > decimate->lo) {
94  c++;
95  if (c > t)
96  return 1;
97  }
98  }
99  }
100  return 0;
101 }
102 
103 /**
104  * Tell if the frame should be decimated, for example if it is no much
105  * different with respect to the reference frame ref.
106  */
108  AVFrame *cur, AVFrame *ref)
109 {
110  DecimateContext *decimate = ctx->priv;
111  int plane;
112 
113  if (decimate->max_drop_count > 0 &&
114  decimate->drop_count >= decimate->max_drop_count)
115  return 0;
116  if (decimate->max_drop_count < 0 &&
117  (decimate->drop_count-1) > decimate->max_drop_count)
118  return 0;
119 
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;
123  if (diff_planes(ctx,
124  cur->data[plane], ref->data[plane], ref->linesize[plane],
125  ref->width>>hsub, ref->height>>vsub))
126  return 0;
127  }
128 
129  return 1;
130 }
131 
132 static av_cold int init(AVFilterContext *ctx)
133 {
134  DecimateContext *decimate = ctx->priv;
135 
136  av_log(ctx, AV_LOG_VERBOSE, "max_drop_count:%d hi:%d lo:%d frac:%f\n",
137  decimate->max_drop_count, decimate->hi, decimate->lo, decimate->frac);
138 
139  decimate->avctx = avcodec_alloc_context3(NULL);
140  if (!decimate->avctx)
141  return AVERROR(ENOMEM);
142  avpriv_dsputil_init(&decimate->dspctx, decimate->avctx);
143 
144  return 0;
145 }
146 
147 static av_cold void uninit(AVFilterContext *ctx)
148 {
149  DecimateContext *decimate = ctx->priv;
150  av_frame_free(&decimate->ref);
151  if (decimate->avctx) {
152  avcodec_close(decimate->avctx);
153  av_freep(&decimate->avctx);
154  }
155 }
156 
158 {
159  static const enum AVPixelFormat pix_fmts[] = {
167  };
168 
170 
171  return 0;
172 }
173 
174 static int config_input(AVFilterLink *inlink)
175 {
176  AVFilterContext *ctx = inlink->dst;
177  DecimateContext *decimate = ctx->priv;
178  const AVPixFmtDescriptor *pix_desc = av_pix_fmt_desc_get(inlink->format);
179  decimate->hsub = pix_desc->log2_chroma_w;
180  decimate->vsub = pix_desc->log2_chroma_h;
181 
182  return 0;
183 }
184 
185 static int filter_frame(AVFilterLink *inlink, AVFrame *cur)
186 {
187  DecimateContext *decimate = inlink->dst->priv;
188  AVFilterLink *outlink = inlink->dst->outputs[0];
189  int ret;
190 
191  if (decimate->ref && decimate_frame(inlink->dst, cur, decimate->ref)) {
192  decimate->drop_count = FFMAX(1, decimate->drop_count+1);
193  } else {
194  av_frame_free(&decimate->ref);
195  decimate->ref = cur;
196  decimate->drop_count = FFMIN(-1, decimate->drop_count-1);
197 
198  if (ret = ff_filter_frame(outlink, av_frame_clone(cur)) < 0)
199  return ret;
200  }
201 
202  av_log(inlink->dst, AV_LOG_DEBUG,
203  "%s pts:%s pts_time:%s drop_count:%d\n",
204  decimate->drop_count > 0 ? "drop" : "keep",
205  av_ts2str(cur->pts), av_ts2timestr(cur->pts, &inlink->time_base),
206  decimate->drop_count);
207 
208  if (decimate->drop_count > 0)
209  av_frame_free(&cur);
210 
211  return 0;
212 }
213 
214 static int request_frame(AVFilterLink *outlink)
215 {
216  DecimateContext *decimate = outlink->src->priv;
217  AVFilterLink *inlink = outlink->src->inputs[0];
218  int ret;
219 
220  do {
221  ret = ff_request_frame(inlink);
222  } while (decimate->drop_count > 0 && ret >= 0);
223 
224  return ret;
225 }
226 
227 static const AVFilterPad mpdecimate_inputs[] = {
228  {
229  .name = "default",
230  .type = AVMEDIA_TYPE_VIDEO,
231  .get_video_buffer = ff_null_get_video_buffer,
232  .config_props = config_input,
233  .filter_frame = filter_frame,
234  },
235  { NULL }
236 };
237 
238 static const AVFilterPad mpdecimate_outputs[] = {
239  {
240  .name = "default",
241  .type = AVMEDIA_TYPE_VIDEO,
242  .request_frame = request_frame,
243  },
244  { NULL }
245 };
246 
248  .name = "mpdecimate",
249  .description = NULL_IF_CONFIG_SMALL("Remove near-duplicate frames."),
250  .init = init,
251  .uninit = uninit,
252 
253  .priv_size = sizeof(DecimateContext),
255  .inputs = mpdecimate_inputs,
256  .outputs = mpdecimate_outputs,
257  .priv_class = &mpdecimate_class,
258 };
int(* sum_abs_dctelem)(int16_t *block)
Definition: dsputil.h:135
AVFrame * ref
reference picture
Definition: vf_mpdecimate.c:50
static const AVFilterPad mpdecimate_inputs[]
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:1778
This structure describes decoded (raw) audio or video data.
Definition: frame.h:76
FIXME Range Coding of cr are ref
Definition: snow.txt:367
AVOption.
Definition: opt.h:251
planar YUV 4:4:4, 24bpp, (1 Cr & Cb sample per 1x1 Y samples)
Definition: pixfmt.h:73
static const AVFilterPad outputs[]
Definition: af_ashowinfo.c:117
external API header
static int filter_frame(AVFilterLink *inlink, AVFrame *cur)
AVFrame * ff_null_get_video_buffer(AVFilterLink *link, int w, int h)
Definition: video.c:35
AVFILTER_DEFINE_CLASS(mpdecimate)
float frac
threshold of changed pixels over the total fraction
Definition: vf_mpdecimate.c:41
uint8_t log2_chroma_w
Amount to shift the luma width right to find the chroma width.
Definition: pixdesc.h:66
output residual component w
void av_freep(void *arg)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc() and set the pointer ...
Definition: mem.c:198
AVFilterFormats * ff_make_format_list(const int *fmts)
Create a list of supported formats.
Definition: formats.c:308
set threshold d
int max_drop_count
if positive: maximum number of sequential frames to drop if negative: minimum number of frames betwee...
Definition: vf_mpdecimate.c:43
const char * name
Pad name.
AVFilterLink ** inputs
array of pointers to input links
Definition: avfilter.h:532
int hi
lower and higher threshold number of differences values for 8x8 blocks
Definition: vf_mpdecimate.c:38
planar YUV 4:2:0, 20bpp, (1 Cr & Cb sample per 2x2 Y & A samples)
Definition: pixfmt.h:105
uint8_t
it can be given away to ff_start_frame *A reference passed to ff_filter_frame(or the deprecated ff_start_frame) is given away and must no longer be used.*A reference created with avfilter_ref_buffer belongs to the code that created it.*A reference obtained with ff_get_video_buffer or ff_get_audio_buffer belongs to the code that requested it.*A reference given as return value by the get_video_buffer or get_audio_buffer method is given away and must no longer be used.Link reference fields---------------------The AVFilterLink structure has a few AVFilterBufferRef fields.The cur_buf and out_buf were used with the deprecated start_frame/draw_slice/end_frame API and should no longer be used.src_buf
#define av_cold
Definition: attributes.h:78
AVOptions.
timestamp utils, mostly useful for debugging/logging purposes
static const AVOption mpdecimate_options[]
Definition: vf_mpdecimate.c:58
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
Definition: frame.h:159
planar YUV 4:4:0 full scale (JPEG), deprecated in favor of PIX_FMT_YUV440P and setting color_range ...
Definition: pixfmt.h:104
planar YUV 4:2:2, 16bpp, full scale (JPEG), deprecated in favor of PIX_FMT_YUV422P and setting color_...
Definition: pixfmt.h:81
void ff_set_common_formats(AVFilterContext *ctx, AVFilterFormats *formats)
A helper for query_formats() which sets all links to the same list of formats.
Definition: formats.c:545
static int decimate_frame(AVFilterContext *ctx, AVFrame *cur, AVFrame *ref)
Tell if the frame should be decimated, for example if it is no much different with respect to the ref...
A filter pad used for either input or output.
Discrete Time axis x
int avcodec_close(AVCodecContext *avctx)
Close a given AVCodecContext and free all the data associated with it (but not the AVCodecContext its...
int width
width and height of the video frame
Definition: frame.h:122
uint8_t log2_chroma_h
Amount to shift the luma height right to find the chroma height.
Definition: pixdesc.h:75
#define av_ts2timestr(ts, tb)
Convenience macro, the return value should be used only directly in function arguments but never stan...
Definition: timestamp.h:72
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
void * priv
private data for use by the filter
Definition: avfilter.h:545
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:246
#define OFFSET(x)
Definition: vf_mpdecimate.c:55
#define FFMAX(a, b)
Definition: common.h:56
planar YUV 4:2:2, 16bpp, (1 Cr & Cb sample per 2x1 Y samples)
Definition: pixfmt.h:72
#define AV_LOG_VERBOSE
Definition: log.h:157
#define FFMIN(a, b)
Definition: common.h:58
planar YUV 4:2:0, 12bpp, full scale (JPEG), deprecated in favor of PIX_FMT_YUV420P and setting color_...
Definition: pixfmt.h:80
AVCodecContext * avcodec_alloc_context3(const AVCodec *codec)
Allocate an AVCodecContext and set its fields to default values.
ret
Definition: avfilter.c:821
t
Definition: genspecsines3.m:6
AVFilter avfilter_vf_mpdecimate
AVFrame * av_frame_clone(AVFrame *src)
Create a new frame that references the same data as src.
Definition: frame.c:317
NULL
Definition: eval.c:55
static av_cold void uninit(AVFilterContext *ctx)
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:101
AVCodecContext * avctx
codec context required for the DSPContext
Definition: vf_mpdecimate.c:52
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
Definition: pixdesc.h:55
main external API structure.
planar YUV 4:1:0, 9bpp, (1 Cr & Cb sample per 4x4 Y samples)
Definition: pixfmt.h:74
Describe the class of an AVClass context structure.
Definition: log.h:50
Filter definition.
Definition: avfilter.h:436
static int query_formats(AVFilterContext *ctx)
int vsub
chroma subsampling values
Definition: vf_decimate.c:48
static int request_frame(AVFilterLink *outlink)
const char * name
filter name
Definition: avfilter.h:437
static const AVFilterPad mpdecimate_outputs[]
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFilterBuffer structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later.That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another.Buffer references ownership and permissions
AVFilterLink ** outputs
array of pointers to output links
Definition: avfilter.h:539
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:87
int drop_count
if positive: number of frames sequentially dropped if negative: number of sequential frames which wer...
Definition: vf_mpdecimate.c:46
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition: pixfmt.h:68
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:162
static av_cold int init(AVFilterContext *ctx)
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:108
static double c[64]
planar YUV 4:4:4, 24bpp, full scale (JPEG), deprecated in favor of PIX_FMT_YUV444P and setting color_...
Definition: pixfmt.h:82
planar YUV 4:1:1, 12bpp, (1 Cr & Cb sample per 4x1 Y samples)
Definition: pixfmt.h:75
function y
Definition: D.m:1
DSP utils.
#define FLAGS
Definition: vf_mpdecimate.c:56
#define av_ts2str(ts)
Convenience macro, the return value should be used only directly in function arguments but never stan...
Definition: timestamp.h:50
static int config_input(AVFilterLink *inlink)
An instance of a filter.
Definition: avfilter.h:524
DSPContext dspctx
context providing optimized diff routines
Definition: vf_mpdecimate.c:51
int height
Definition: frame.h:122
planar YUV 4:4:0 (1 Cr & Cb sample per 1x2 Y samples)
Definition: pixfmt.h:103
av_cold void avpriv_dsputil_init(DSPContext *c, AVCodecContext *avctx)
Definition: dsputil.c:2932
int ff_request_frame(AVFilterLink *link)
Request an input frame from the filter at the other end of the link.
Definition: avfilter.c:319
internal API functions
AVPixelFormat
Pixel format.
Definition: pixfmt.h:66
these buffered frames must be flushed immediately if a new input produces new the filter must not call request_frame to get more It must just process the frame or queue it The task of requesting more frames is left to the filter s request_frame method or the application If a filter has several inputs
void(* diff_pixels)(int16_t *block, const uint8_t *s1, const uint8_t *s2, int stride)
Definition: dsputil.h:130
static int diff_planes(AVFilterContext *ctx, uint8_t *cur, uint8_t *ref, int linesize, int w, int h)
Return 1 if the two planes are different, 0 otherwise.
Definition: vf_mpdecimate.c:72
DSPContext.
Definition: dsputil.h:127