FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
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/pixelutils.h"
30 #include "libavutil/timestamp.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  av_pixelutils_sad_fn sad; ///< sum of absolute difference function
53 
54 #define OFFSET(x) offsetof(DecimateContext, x)
55 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
56 
57 static const AVOption mpdecimate_options[] = {
58  { "max", "set the maximum number of consecutive dropped frames (positive), or the minimum interval between dropped frames (negative)",
59  OFFSET(max_drop_count), AV_OPT_TYPE_INT, {.i64=0}, INT_MIN, INT_MAX, FLAGS },
60  { "hi", "set high dropping threshold", OFFSET(hi), AV_OPT_TYPE_INT, {.i64=64*12}, INT_MIN, INT_MAX, FLAGS },
61  { "lo", "set low dropping threshold", OFFSET(lo), AV_OPT_TYPE_INT, {.i64=64*5}, INT_MIN, INT_MAX, FLAGS },
62  { "frac", "set fraction dropping threshold", OFFSET(frac), AV_OPT_TYPE_FLOAT, {.dbl=0.33}, 0, 1, FLAGS },
63  { NULL }
64 };
65 
66 AVFILTER_DEFINE_CLASS(mpdecimate);
67 
68 /**
69  * Return 1 if the two planes are different, 0 otherwise.
70  */
71 static int diff_planes(AVFilterContext *ctx,
72  uint8_t *cur, int cur_linesize,
73  uint8_t *ref, int ref_linesize,
74  int w, int h)
75 {
76  DecimateContext *decimate = ctx->priv;
77 
78  int x, y;
79  int d, c = 0;
80  int t = (w/16)*(h/16)*decimate->frac;
81 
82  /* compute difference for blocks of 8x8 bytes */
83  for (y = 0; y < h-7; y += 4) {
84  for (x = 8; x < w-7; x += 4) {
85  d = decimate->sad(cur + y*cur_linesize + x, cur_linesize,
86  ref + y*ref_linesize + x, ref_linesize);
87  if (d > decimate->hi) {
88  av_log(ctx, AV_LOG_DEBUG, "%d>=hi ", d);
89  return 1;
90  }
91  if (d > decimate->lo) {
92  c++;
93  if (c > t) {
94  av_log(ctx, AV_LOG_DEBUG, "lo:%d>=%d ", c, t);
95  return 1;
96  }
97  }
98  }
99  }
100 
101  av_log(ctx, AV_LOG_DEBUG, "lo:%d<%d ", c, t);
102  return 0;
103 }
104 
105 /**
106  * Tell if the frame should be decimated, for example if it is no much
107  * different with respect to the reference frame ref.
108  */
110  AVFrame *cur, AVFrame *ref)
111 {
112  DecimateContext *decimate = ctx->priv;
113  int plane;
114 
115  if (decimate->max_drop_count > 0 &&
116  decimate->drop_count >= decimate->max_drop_count)
117  return 0;
118  if (decimate->max_drop_count < 0 &&
119  (decimate->drop_count-1) > decimate->max_drop_count)
120  return 0;
121 
122  for (plane = 0; ref->data[plane] && ref->linesize[plane]; plane++) {
123  /* use 8x8 SAD even on subsampled planes. The blocks won't match up with
124  * luma blocks, but hopefully nobody is depending on this to catch
125  * localized chroma changes that wouldn't exceed the thresholds when
126  * diluted by using what's effectively a larger block size.
127  */
128  int vsub = plane == 1 || plane == 2 ? decimate->vsub : 0;
129  int hsub = plane == 1 || plane == 2 ? decimate->hsub : 0;
130  if (diff_planes(ctx,
131  cur->data[plane], cur->linesize[plane],
132  ref->data[plane], ref->linesize[plane],
133  FF_CEIL_RSHIFT(ref->width, hsub),
134  FF_CEIL_RSHIFT(ref->height, vsub)))
135  return 0;
136  }
137 
138  return 1;
139 }
140 
141 static av_cold int init(AVFilterContext *ctx)
142 {
143  DecimateContext *decimate = ctx->priv;
144 
145  decimate->sad = av_pixelutils_get_sad_fn(3, 3, 0, ctx); // 8x8, not aligned on blocksize
146  if (!decimate->sad)
147  return AVERROR(EINVAL);
148 
149  av_log(ctx, AV_LOG_VERBOSE, "max_drop_count:%d hi:%d lo:%d frac:%f\n",
150  decimate->max_drop_count, decimate->hi, decimate->lo, decimate->frac);
151 
152  return 0;
153 }
154 
155 static av_cold void uninit(AVFilterContext *ctx)
156 {
157  DecimateContext *decimate = ctx->priv;
158  av_frame_free(&decimate->ref);
159 }
160 
162 {
163  static const enum AVPixelFormat pix_fmts[] = {
170 
172 
175 
177  };
178  AVFilterFormats *fmts_list = ff_make_format_list(pix_fmts);
179  if (!fmts_list)
180  return AVERROR(ENOMEM);
181  return ff_set_common_formats(ctx, fmts_list);
182 }
183 
184 static int config_input(AVFilterLink *inlink)
185 {
186  AVFilterContext *ctx = inlink->dst;
187  DecimateContext *decimate = ctx->priv;
188  const AVPixFmtDescriptor *pix_desc = av_pix_fmt_desc_get(inlink->format);
189  decimate->hsub = pix_desc->log2_chroma_w;
190  decimate->vsub = pix_desc->log2_chroma_h;
191 
192  return 0;
193 }
194 
195 static int filter_frame(AVFilterLink *inlink, AVFrame *cur)
196 {
197  DecimateContext *decimate = inlink->dst->priv;
198  AVFilterLink *outlink = inlink->dst->outputs[0];
199  int ret;
200 
201  if (decimate->ref && decimate_frame(inlink->dst, cur, decimate->ref)) {
202  decimate->drop_count = FFMAX(1, decimate->drop_count+1);
203  } else {
204  av_frame_free(&decimate->ref);
205  decimate->ref = cur;
206  decimate->drop_count = FFMIN(-1, decimate->drop_count-1);
207 
208  if ((ret = ff_filter_frame(outlink, av_frame_clone(cur))) < 0)
209  return ret;
210  }
211 
212  av_log(inlink->dst, AV_LOG_DEBUG,
213  "%s pts:%s pts_time:%s drop_count:%d\n",
214  decimate->drop_count > 0 ? "drop" : "keep",
215  av_ts2str(cur->pts), av_ts2timestr(cur->pts, &inlink->time_base),
216  decimate->drop_count);
217 
218  if (decimate->drop_count > 0)
219  av_frame_free(&cur);
220 
221  return 0;
222 }
223 
224 static int request_frame(AVFilterLink *outlink)
225 {
226  DecimateContext *decimate = outlink->src->priv;
227  AVFilterLink *inlink = outlink->src->inputs[0];
228  int ret;
229 
230  do {
231  ret = ff_request_frame(inlink);
232  } while (decimate->drop_count > 0 && ret >= 0);
233 
234  return ret;
235 }
236 
237 static const AVFilterPad mpdecimate_inputs[] = {
238  {
239  .name = "default",
240  .type = AVMEDIA_TYPE_VIDEO,
241  .config_props = config_input,
242  .filter_frame = filter_frame,
243  },
244  { NULL }
245 };
246 
247 static const AVFilterPad mpdecimate_outputs[] = {
248  {
249  .name = "default",
250  .type = AVMEDIA_TYPE_VIDEO,
251  .request_frame = request_frame,
252  },
253  { NULL }
254 };
255 
257  .name = "mpdecimate",
258  .description = NULL_IF_CONFIG_SMALL("Remove near-duplicate frames."),
259  .init = init,
260  .uninit = uninit,
261  .priv_size = sizeof(DecimateContext),
262  .priv_class = &mpdecimate_class,
264  .inputs = mpdecimate_inputs,
265  .outputs = mpdecimate_outputs,
266 };
int plane
Definition: avisynth_c.h:291
#define NULL
Definition: coverity.c:32
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:2129
This structure describes decoded (raw) audio or video data.
Definition: frame.h:171
AVOption.
Definition: opt.h:255
planar YUV 4:4:4, 24bpp, (1 Cr & Cb sample per 1x1 Y samples)
Definition: pixfmt.h:68
static const AVFilterPad outputs[]
Definition: af_ashowinfo.c:248
Main libavfilter public API header.
static int filter_frame(AVFilterLink *inlink, AVFrame *cur)
planar GBR 4:4:4 24bpp
Definition: pixfmt.h:188
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:80
AVFilterFormats * ff_make_format_list(const int *fmts)
Create a list of supported formats.
Definition: formats.c:283
av_pixelutils_sad_fn sad
sum of absolute difference function
Definition: vf_mpdecimate.c:51
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.
Definition: internal.h:69
AVFilterLink ** inputs
array of pointers to input links
Definition: avfilter.h:641
int hi
lower and higher threshold number of differences values for 8x8 blocks
Definition: vf_mpdecimate.c:38
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:1158
planar YUV 4:2:0, 20bpp, (1 Cr & Cb sample per 2x2 Y & A samples)
Definition: pixfmt.h:103
uint8_t
#define av_cold
Definition: attributes.h:74
AVOptions.
timestamp utils, mostly useful for debugging/logging purposes
static const AVOption mpdecimate_options[]
Definition: vf_mpdecimate.c:57
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
Definition: frame.h:257
planar YUV 4:4:0 full scale (JPEG), deprecated in favor of AV_PIX_FMT_YUV440P and setting color_range...
Definition: pixfmt.h:102
planar YUV 4:2:2, 16bpp, full scale (JPEG), deprecated in favor of AV_PIX_FMT_YUV422P and setting col...
Definition: pixfmt.h:76
#define AV_LOG_VERBOSE
Detailed information.
Definition: log.h:192
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...
#define av_log(a,...)
A filter pad used for either input or output.
Definition: internal.h:63
planar YUV 4:2:2 24bpp, (1 Cr & Cb sample per 2x1 Y & A samples)
Definition: pixfmt.h:281
int width
width and height of the video frame
Definition: frame.h:220
int 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:542
uint8_t log2_chroma_h
Amount to shift the luma height right to find the chroma height.
Definition: pixdesc.h:89
#define AVERROR(e)
Definition: error.h:43
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:148
#define av_ts2timestr(ts, tb)
Convenience macro, the return value should be used only directly in function arguments but never stan...
Definition: timestamp.h:76
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:175
void * priv
private data for use by the filter
Definition: avfilter.h:654
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:197
int(* av_pixelutils_sad_fn)(const uint8_t *src1, ptrdiff_t stride1, const uint8_t *src2, ptrdiff_t stride2)
Sum of abs(src1[x] - src2[x])
Definition: pixelutils.h:29
#define OFFSET(x)
Definition: vf_mpdecimate.c:54
#define FFMAX(a, b)
Definition: common.h:79
planar YUV 4:2:2, 16bpp, (1 Cr & Cb sample per 2x1 Y samples)
Definition: pixfmt.h:67
#define FFMIN(a, b)
Definition: common.h:81
float y
planar YUV 4:2:0, 12bpp, full scale (JPEG), deprecated in favor of AV_PIX_FMT_YUV420P and setting col...
Definition: pixfmt.h:75
#define FF_CEIL_RSHIFT(a, b)
Definition: common.h:57
AVFrame * av_frame_clone(const AVFrame *src)
Create a new frame that references the same data as src.
Definition: frame.c:451
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:199
planar YUV 4:4:4 32bpp, (1 Cr & Cb sample per 1x1 Y & A samples)
Definition: pixfmt.h:280
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
Definition: pixdesc.h:69
planar YUV 4:1:0, 9bpp, (1 Cr & Cb sample per 4x4 Y samples)
Definition: pixfmt.h:69
av_pixelutils_sad_fn av_pixelutils_get_sad_fn(int w_bits, int h_bits, int aligned, void *log_ctx)
Get a potentially optimized pointer to a Sum-of-absolute-differences function (see the av_pixelutils_...
Definition: pixelutils.c:64
Describe the class of an AVClass context structure.
Definition: log.h:67
Filter definition.
Definition: avfilter.h:470
static int query_formats(AVFilterContext *ctx)
static const AVFilterPad inputs[]
Definition: af_ashowinfo.c:239
int vsub
chroma subsampling values
Definition: vf_decimate.c:48
static int request_frame(AVFilterLink *outlink)
const char * name
Filter name.
Definition: avfilter.h:474
static const AVFilterPad mpdecimate_outputs[]
AVFilterLink ** outputs
array of pointers to output links
Definition: avfilter.h:648
static enum AVPixelFormat pix_fmts[]
Definition: libkvazaar.c:209
static int diff_planes(AVFilterContext *ctx, uint8_t *cur, int cur_linesize, uint8_t *ref, int ref_linesize, int w, int h)
Return 1 if the two planes are different, 0 otherwise.
Definition: vf_mpdecimate.c:71
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:182
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:63
static av_cold int init(AVFilterContext *ctx)
static double c[64]
planar YUV 4:4:4, 24bpp, full scale (JPEG), deprecated in favor of AV_PIX_FMT_YUV444P and setting col...
Definition: pixfmt.h:77
planar YUV 4:1:1, 12bpp, (1 Cr & Cb sample per 4x1 Y samples)
Definition: pixfmt.h:70
#define FLAGS
Definition: vf_mpdecimate.c:55
#define av_ts2str(ts)
Convenience macro, the return value should be used only directly in function arguments but never stan...
Definition: timestamp.h:54
AVFilter ff_vf_mpdecimate
static int config_input(AVFilterLink *inlink)
A list of supported formats for one end of a filter link.
Definition: formats.h:64
An instance of a filter.
Definition: avfilter.h:633
int height
Definition: frame.h:220
planar YUV 4:4:0 (1 Cr & Cb sample per 1x2 Y samples)
Definition: pixfmt.h:101
int ff_request_frame(AVFilterLink *link)
Request an input frame from the filter at the other end of the link.
Definition: avfilter.c:343
internal API functions
AVPixelFormat
Pixel format.
Definition: pixfmt.h:61