FFmpeg
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/emms.h"
28 #include "libavutil/opt.h"
29 #include "libavutil/pixdesc.h"
30 #include "libavutil/pixelutils.h"
31 #include "libavutil/timestamp.h"
32 #include "avfilter.h"
33 #include "internal.h"
34 #include "video.h"
35 
36 typedef struct DecimateContext {
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 max_keep_count; ///< number of similar frames to ignore before to start dropping them
50  int keep_count; ///< number of similar frames already ignored
51 
52  int hsub, vsub; ///< chroma subsampling values
53  AVFrame *ref; ///< reference picture
54  av_pixelutils_sad_fn sad; ///< sum of absolute difference function
56 
57 #define OFFSET(x) offsetof(DecimateContext, x)
58 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
59 
60 static 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  { "keep", "set the number of similar consecutive frames to be kept before starting to drop similar frames",
64  OFFSET(max_keep_count), AV_OPT_TYPE_INT, {.i64=0}, 0, INT_MAX, FLAGS },
65  { "hi", "set high dropping threshold", OFFSET(hi), AV_OPT_TYPE_INT, {.i64=64*12}, INT_MIN, INT_MAX, FLAGS },
66  { "lo", "set low dropping threshold", OFFSET(lo), AV_OPT_TYPE_INT, {.i64=64*5}, INT_MIN, INT_MAX, FLAGS },
67  { "frac", "set fraction dropping threshold", OFFSET(frac), AV_OPT_TYPE_FLOAT, {.dbl=0.33}, 0, 1, FLAGS },
68  { NULL }
69 };
70 
71 AVFILTER_DEFINE_CLASS(mpdecimate);
72 
73 /**
74  * Return 1 if the two planes are different, 0 otherwise.
75  */
77  uint8_t *cur, int cur_linesize,
78  uint8_t *ref, int ref_linesize,
79  int w, int h)
80 {
81  DecimateContext *decimate = ctx->priv;
82 
83  int x, y;
84  int d, c = 0;
85  int t = (w/16)*(h/16)*decimate->frac;
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  d = decimate->sad(cur + y*cur_linesize + x, cur_linesize,
91  ref + y*ref_linesize + x, ref_linesize);
92  if (d > decimate->hi) {
93  av_log(ctx, AV_LOG_DEBUG, "%d>=hi ", d);
94  return 1;
95  }
96  if (d > decimate->lo) {
97  c++;
98  if (c > t) {
99  av_log(ctx, AV_LOG_DEBUG, "lo:%d>=%d ", c, t);
100  return 1;
101  }
102  }
103  }
104  }
105 
106  av_log(ctx, AV_LOG_DEBUG, "lo:%d<%d ", c, t);
107  return 0;
108 }
109 
110 /**
111  * Tell if the frame should be decimated, for example if it is no much
112  * different with respect to the reference frame ref.
113  */
115  AVFrame *cur, AVFrame *ref)
116 {
117  DecimateContext *decimate = ctx->priv;
118  int plane;
119 
120  if (decimate->max_keep_count > 0 &&
121  decimate->keep_count > -1 &&
122  decimate->keep_count < decimate->max_keep_count) {
123  decimate->keep_count++;
124  return 0;
125  }
126  if (decimate->max_drop_count > 0 &&
127  decimate->drop_count >= decimate->max_drop_count)
128  return 0;
129  if (decimate->max_drop_count < 0 &&
130  (decimate->drop_count-1) > decimate->max_drop_count)
131  return 0;
132 
133  for (plane = 0; ref->data[plane] && ref->linesize[plane]; plane++) {
134  /* use 8x8 SAD even on subsampled planes. The blocks won't match up with
135  * luma blocks, but hopefully nobody is depending on this to catch
136  * localized chroma changes that wouldn't exceed the thresholds when
137  * diluted by using what's effectively a larger block size.
138  */
139  int vsub = plane == 1 || plane == 2 ? decimate->vsub : 0;
140  int hsub = plane == 1 || plane == 2 ? decimate->hsub : 0;
141  if (diff_planes(ctx,
142  cur->data[plane], cur->linesize[plane],
143  ref->data[plane], ref->linesize[plane],
144  AV_CEIL_RSHIFT(ref->width, hsub),
145  AV_CEIL_RSHIFT(ref->height, vsub))) {
146  emms_c();
147  return 0;
148  }
149  }
150 
151  emms_c();
152  return 1;
153 }
154 
156 {
157  DecimateContext *decimate = ctx->priv;
158 
159  decimate->sad = av_pixelutils_get_sad_fn(3, 3, 0, ctx); // 8x8, not aligned on blocksize
160  if (!decimate->sad)
161  return AVERROR(EINVAL);
162 
163  av_log(ctx, AV_LOG_VERBOSE, "max_drop_count:%d hi:%d lo:%d frac:%f\n",
164  decimate->max_drop_count, decimate->hi, decimate->lo, decimate->frac);
165 
166  return 0;
167 }
168 
170 {
171  DecimateContext *decimate = ctx->priv;
172  av_frame_free(&decimate->ref);
173 }
174 
175 static const enum AVPixelFormat pix_fmts[] = {
182 
184 
187 
189 };
190 
192 {
193  AVFilterContext *ctx = inlink->dst;
194  DecimateContext *decimate = ctx->priv;
195  const AVPixFmtDescriptor *pix_desc = av_pix_fmt_desc_get(inlink->format);
196  decimate->hsub = pix_desc->log2_chroma_w;
197  decimate->vsub = pix_desc->log2_chroma_h;
198 
199  return 0;
200 }
201 
203 {
204  DecimateContext *decimate = inlink->dst->priv;
205  AVFilterLink *outlink = inlink->dst->outputs[0];
206  int ret;
207 
208  if (decimate->ref && decimate_frame(inlink->dst, cur, decimate->ref)) {
209  decimate->drop_count = FFMAX(1, decimate->drop_count+1);
210  decimate->keep_count = -1; // do not keep any more frames until non-similar frames are detected
211  } else {
212  av_frame_free(&decimate->ref);
213  decimate->ref = cur;
214  decimate->drop_count = FFMIN(-1, decimate->drop_count-1);
215  if (decimate->keep_count < 0) // re-enable counting similiar frames to ignore before dropping
216  decimate->keep_count = 0;
217 
218  if ((ret = ff_filter_frame(outlink, av_frame_clone(cur))) < 0)
219  return ret;
220  }
221 
222  av_log(inlink->dst, AV_LOG_DEBUG,
223  "%s pts:%s pts_time:%s drop_count:%d keep_count:%d\n",
224  decimate->drop_count > 0 ? "drop" : "keep",
225  av_ts2str(cur->pts), av_ts2timestr(cur->pts, &inlink->time_base),
226  decimate->drop_count,
227  decimate->keep_count);
228 
229  if (decimate->drop_count > 0)
230  av_frame_free(&cur);
231 
232  return 0;
233 }
234 
235 static const AVFilterPad mpdecimate_inputs[] = {
236  {
237  .name = "default",
238  .type = AVMEDIA_TYPE_VIDEO,
239  .config_props = config_input,
240  .filter_frame = filter_frame,
241  },
242 };
243 
245  .name = "mpdecimate",
246  .description = NULL_IF_CONFIG_SMALL("Remove near-duplicate frames."),
247  .init = init,
248  .uninit = uninit,
249  .priv_size = sizeof(DecimateContext),
250  .priv_class = &mpdecimate_class,
254 };
DecimateContext::lo
int lo
Definition: vf_mpdecimate.c:38
AVPixelFormat
AVPixelFormat
Pixel format.
Definition: pixfmt.h:64
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame 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. Frame references ownership and permissions
opt.h
ff_filter_frame
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:978
av_pix_fmt_desc_get
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:2964
FILTER_PIXFMTS_ARRAY
#define FILTER_PIXFMTS_ARRAY(array)
Definition: internal.h:172
inlink
The exact code depends on how similar the blocks are and how related they are to the and needs to apply these operations to the correct inlink or outlink if there are several Macros are available to factor that when no extra processing is inlink
Definition: filter_design.txt:212
av_frame_free
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:100
av_pixelutils_sad_fn
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:28
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:340
pixdesc.h
AVFrame::pts
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
Definition: frame.h:452
w
uint8_t w
Definition: llviddspenc.c:38
AVOption
AVOption.
Definition: opt.h:251
AV_LOG_VERBOSE
#define AV_LOG_VERBOSE
Detailed information.
Definition: log.h:196
DecimateContext::vsub
int vsub
chroma subsampling values
Definition: vf_decimate.c:50
AV_PIX_FMT_YUV440P
@ AV_PIX_FMT_YUV440P
planar YUV 4:4:0 (1 Cr & Cb sample per 1x2 Y samples)
Definition: pixfmt.h:99
FFMAX
#define FFMAX(a, b)
Definition: macros.h:47
AVFilter::name
const char * name
Filter name.
Definition: avfilter.h:170
video.h
DecimateContext::hsub
int hsub
Definition: vf_decimate.c:50
AVFrame::data
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:361
hsub
static void hsub(htype *dst, const htype *src, int bins)
Definition: vf_median.c:73
DecimateContext::drop_count
int drop_count
if positive: number of frames sequentially dropped if negative: number of sequential frames which wer...
Definition: vf_mpdecimate.c:46
config_input
static int config_input(AVFilterLink *inlink)
Definition: vf_mpdecimate.c:191
DecimateContext::keep_count
int keep_count
number of similar frames already ignored
Definition: vf_mpdecimate.c:50
AVFilterPad
A filter pad used for either input or output.
Definition: internal.h:47
av_cold
#define av_cold
Definition: attributes.h:90
ff_video_default_filterpad
const AVFilterPad ff_video_default_filterpad[1]
An AVFilterPad array whose only entry has name "default" and is of type AVMEDIA_TYPE_VIDEO.
Definition: video.c:36
filter_frame
static int filter_frame(AVFilterLink *inlink, AVFrame *cur)
Definition: vf_mpdecimate.c:202
AV_PIX_FMT_YUVJ422P
@ AV_PIX_FMT_YUVJ422P
planar YUV 4:2:2, 16bpp, full scale (JPEG), deprecated in favor of AV_PIX_FMT_YUV422P and setting col...
Definition: pixfmt.h:79
emms_c
#define emms_c()
Definition: emms.h:63
AV_PIX_FMT_YUVA420P
@ AV_PIX_FMT_YUVA420P
planar YUV 4:2:0, 20bpp, (1 Cr & Cb sample per 2x2 Y & A samples)
Definition: pixfmt.h:101
AV_CEIL_RSHIFT
#define AV_CEIL_RSHIFT(a, b)
Definition: common.h:51
AV_LOG_DEBUG
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:201
FLAGS
#define FLAGS
Definition: vf_mpdecimate.c:58
ctx
AVFormatContext * ctx
Definition: movenc.c:48
pix_fmts
static enum AVPixelFormat pix_fmts[]
Definition: vf_mpdecimate.c:175
av_frame_clone
AVFrame * av_frame_clone(const AVFrame *src)
Create a new frame that references the same data as src.
Definition: frame.c:609
AVPixFmtDescriptor::log2_chroma_w
uint8_t log2_chroma_w
Amount to shift the luma width right to find the chroma width.
Definition: pixdesc.h:80
AV_PIX_FMT_YUV420P
@ AV_PIX_FMT_YUV420P
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition: pixfmt.h:66
FILTER_INPUTS
#define FILTER_INPUTS(array)
Definition: internal.h:192
AV_PIX_FMT_YUVJ444P
@ AV_PIX_FMT_YUVJ444P
planar YUV 4:4:4, 24bpp, full scale (JPEG), deprecated in favor of AV_PIX_FMT_YUV444P and setting col...
Definition: pixfmt.h:80
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:66
NULL
#define NULL
Definition: coverity.c:32
ff_vf_mpdecimate
const AVFilter ff_vf_mpdecimate
Definition: vf_mpdecimate.c:244
OFFSET
#define OFFSET(x)
Definition: vf_mpdecimate.c:57
AV_PIX_FMT_YUVJ420P
@ AV_PIX_FMT_YUVJ420P
planar YUV 4:2:0, 12bpp, full scale (JPEG), deprecated in favor of AV_PIX_FMT_YUV420P and setting col...
Definition: pixfmt.h:78
DecimateContext::ref
AVFrame * ref
reference picture
Definition: vf_mpdecimate.c:53
c
Undefined Behavior In the C some operations are like signed integer dereferencing freed accessing outside allocated Undefined Behavior must not occur in a C it is not safe even if the output of undefined operations is unused The unsafety may seem nit picking but Optimizing compilers have in fact optimized code on the assumption that no undefined Behavior occurs Optimizing code based on wrong assumptions can and has in some cases lead to effects beyond the output of computations The signed integer overflow problem in speed critical code Code which is highly optimized and works with signed integers sometimes has the problem that often the output of the computation does not c
Definition: undefined.txt:32
av_ts2timestr
#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
NULL_IF_CONFIG_SMALL
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:106
mpdecimate_options
static const AVOption mpdecimate_options[]
Definition: vf_mpdecimate.c:60
pixelutils.h
init
static av_cold int init(AVFilterContext *ctx)
Definition: vf_mpdecimate.c:155
AV_PIX_FMT_YUVA444P
@ AV_PIX_FMT_YUVA444P
planar YUV 4:4:4 32bpp, (1 Cr & Cb sample per 1x1 Y & A samples)
Definition: pixfmt.h:167
internal.h
AV_OPT_TYPE_FLOAT
@ AV_OPT_TYPE_FLOAT
Definition: opt.h:228
DecimateContext::sad
av_pixelutils_sad_fn sad
sum of absolute difference function
Definition: vf_mpdecimate.c:54
DecimateContext::max_drop_count
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
emms.h
DecimateContext::frac
float frac
threshold of changed pixels over the total fraction
Definition: vf_mpdecimate.c:41
FFMIN
#define FFMIN(a, b)
Definition: macros.h:49
AV_PIX_FMT_YUVJ440P
@ AV_PIX_FMT_YUVJ440P
planar YUV 4:4:0 full scale (JPEG), deprecated in favor of AV_PIX_FMT_YUV440P and setting color_range
Definition: pixfmt.h:100
AVFilterPad::name
const char * name
Pad name.
Definition: internal.h:53
AVFilter
Filter definition.
Definition: avfilter.h:166
DecimateContext::hi
int hi
lower and higher threshold number of differences values for 8x8 blocks
Definition: vf_mpdecimate.c:38
ret
ret
Definition: filter_design.txt:187
AVFILTER_DEFINE_CLASS
AVFILTER_DEFINE_CLASS(mpdecimate)
DecimateContext::max_keep_count
int max_keep_count
number of similar frames to ignore before to start dropping them
Definition: vf_mpdecimate.c:49
AV_PIX_FMT_NONE
@ AV_PIX_FMT_NONE
Definition: pixfmt.h:65
AV_OPT_TYPE_INT
@ AV_OPT_TYPE_INT
Definition: opt.h:225
avfilter.h
av_pixelutils_get_sad_fn
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:72
ref
static int ref[MAX_W *MAX_W]
Definition: jpeg2000dwt.c:112
AV_PIX_FMT_YUV444P
@ AV_PIX_FMT_YUV444P
planar YUV 4:4:4, 24bpp, (1 Cr & Cb sample per 1x1 Y samples)
Definition: pixfmt.h:71
AVFilterContext
An instance of a filter.
Definition: avfilter.h:397
AV_PIX_FMT_GBRP
@ AV_PIX_FMT_GBRP
planar GBR 4:4:4 24bpp
Definition: pixfmt.h:158
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
AV_PIX_FMT_YUV422P
@ AV_PIX_FMT_YUV422P
planar YUV 4:2:2, 16bpp, (1 Cr & Cb sample per 2x1 Y samples)
Definition: pixfmt.h:70
AVPixFmtDescriptor
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
Definition: pixdesc.h:69
FILTER_OUTPUTS
#define FILTER_OUTPUTS(array)
Definition: internal.h:193
AV_PIX_FMT_YUV411P
@ AV_PIX_FMT_YUV411P
planar YUV 4:1:1, 12bpp, (1 Cr & Cb sample per 4x1 Y samples)
Definition: pixfmt.h:73
DecimateContext
Definition: vf_decimate.c:38
mpdecimate_inputs
static const AVFilterPad mpdecimate_inputs[]
Definition: vf_mpdecimate.c:235
uninit
static av_cold void uninit(AVFilterContext *ctx)
Definition: vf_mpdecimate.c:169
d
d
Definition: ffmpeg_filter.c:368
timestamp.h
AVFrame::linesize
int linesize[AV_NUM_DATA_POINTERS]
For video, a positive or negative value, which is typically indicating the size in bytes of each pict...
Definition: frame.h:385
AV_PIX_FMT_YUV410P
@ AV_PIX_FMT_YUV410P
planar YUV 4:1:0, 9bpp, (1 Cr & Cb sample per 4x4 Y samples)
Definition: pixfmt.h:72
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
av_ts2str
#define av_ts2str(ts)
Convenience macro, the return value should be used only directly in function arguments but never stan...
Definition: timestamp.h:54
h
h
Definition: vp9dsp_template.c:2038
decimate_frame
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...
Definition: vf_mpdecimate.c:114
AVPixFmtDescriptor::log2_chroma_h
uint8_t log2_chroma_h
Amount to shift the luma height right to find the chroma height.
Definition: pixdesc.h:89
AV_PIX_FMT_YUVA422P
@ AV_PIX_FMT_YUVA422P
planar YUV 4:2:2 24bpp, (1 Cr & Cb sample per 2x1 Y & A samples)
Definition: pixfmt.h:166
diff_planes
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:76