FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavfilter/vf_mpdecimate.c
Date: 2024-04-25 15:36:26
Exec Total Coverage
Lines: 65 77 84.4%
Functions: 6 6 100.0%
Branches: 37 52 71.2%

Line Branch Exec Source
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 "video.h"
34
35 typedef struct DecimateContext {
36 const AVClass *class;
37 int lo, hi; ///< lower and higher threshold number of differences
38 ///< values for 8x8 blocks
39
40 float frac; ///< threshold of changed pixels over the total fraction
41
42 int max_drop_count; ///< if positive: maximum number of sequential frames to drop
43 ///< if negative: minimum number of frames between two drops
44
45 int drop_count; ///< if positive: number of frames sequentially dropped
46 ///< if negative: number of sequential frames which were not dropped
47
48 int max_keep_count; ///< number of similar frames to ignore before to start dropping them
49 int keep_count; ///< number of similar frames already ignored
50
51 int hsub, vsub; ///< chroma subsampling values
52 AVFrame *ref; ///< reference picture
53 av_pixelutils_sad_fn sad; ///< sum of absolute difference function
54 } DecimateContext;
55
56 #define OFFSET(x) offsetof(DecimateContext, x)
57 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
58
59 static const AVOption mpdecimate_options[] = {
60 { "max", "set the maximum number of consecutive dropped frames (positive), or the minimum interval between dropped frames (negative)",
61 OFFSET(max_drop_count), AV_OPT_TYPE_INT, {.i64=0}, INT_MIN, INT_MAX, FLAGS },
62 { "keep", "set the number of similar consecutive frames to be kept before starting to drop similar frames",
63 OFFSET(max_keep_count), AV_OPT_TYPE_INT, {.i64=0}, 0, INT_MAX, FLAGS },
64 { "hi", "set high dropping threshold", OFFSET(hi), AV_OPT_TYPE_INT, {.i64=64*12}, INT_MIN, INT_MAX, FLAGS },
65 { "lo", "set low dropping threshold", OFFSET(lo), AV_OPT_TYPE_INT, {.i64=64*5}, INT_MIN, INT_MAX, FLAGS },
66 { "frac", "set fraction dropping threshold", OFFSET(frac), AV_OPT_TYPE_FLOAT, {.dbl=0.33}, 0, 1, FLAGS },
67 { NULL }
68 };
69
70 AVFILTER_DEFINE_CLASS(mpdecimate);
71
72 /**
73 * Return 1 if the two planes are different, 0 otherwise.
74 */
75 49 static int diff_planes(AVFilterContext *ctx,
76 uint8_t *cur, int cur_linesize,
77 uint8_t *ref, int ref_linesize,
78 int w, int h)
79 {
80 49 DecimateContext *decimate = ctx->priv;
81
82 int x, y;
83 49 int d, c = 0;
84 49 int t = (w/16)*(h/16)*decimate->frac;
85
86 /* compute difference for blocks of 8x8 bytes */
87
2/2
✓ Branch 0 taken 1200 times.
✓ Branch 1 taken 30 times.
1230 for (y = 0; y < h-7; y += 4) {
88
2/2
✓ Branch 0 taken 68137 times.
✓ Branch 1 taken 1181 times.
69318 for (x = 8; x < w-7; x += 4) {
89 68137 d = decimate->sad(cur + y*cur_linesize + x, cur_linesize,
90 68137 ref + y*ref_linesize + x, ref_linesize);
91
2/2
✓ Branch 0 taken 19 times.
✓ Branch 1 taken 68118 times.
68137 if (d > decimate->hi) {
92 19 av_log(ctx, AV_LOG_DEBUG, "%d>=hi ", d);
93 19 return 1;
94 }
95
2/2
✓ Branch 0 taken 43 times.
✓ Branch 1 taken 68075 times.
68118 if (d > decimate->lo) {
96 43 c++;
97
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 43 times.
43 if (c > t) {
98 av_log(ctx, AV_LOG_DEBUG, "lo:%d>=%d ", c, t);
99 return 1;
100 }
101 }
102 }
103 }
104
105 30 av_log(ctx, AV_LOG_DEBUG, "lo:%d<%d ", c, t);
106 30 return 0;
107 }
108
109 /**
110 * Tell if the frame should be decimated, for example if it is no much
111 * different with respect to the reference frame ref.
112 */
113 29 static int decimate_frame(AVFilterContext *ctx,
114 AVFrame *cur, AVFrame *ref)
115 {
116 29 DecimateContext *decimate = ctx->priv;
117 int plane;
118
119
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 29 times.
29 if (decimate->max_keep_count > 0 &&
120 decimate->keep_count > -1 &&
121 decimate->keep_count < decimate->max_keep_count) {
122 decimate->keep_count++;
123 return 0;
124 }
125
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 29 times.
29 if (decimate->max_drop_count > 0 &&
126 decimate->drop_count >= decimate->max_drop_count)
127 return 0;
128
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 29 times.
29 if (decimate->max_drop_count < 0 &&
129 (decimate->drop_count-1) > decimate->max_drop_count)
130 return 0;
131
132
3/4
✓ Branch 0 taken 49 times.
✓ Branch 1 taken 10 times.
✓ Branch 2 taken 49 times.
✗ Branch 3 not taken.
59 for (plane = 0; ref->data[plane] && ref->linesize[plane]; plane++) {
133 /* use 8x8 SAD even on subsampled planes. The blocks won't match up with
134 * luma blocks, but hopefully nobody is depending on this to catch
135 * localized chroma changes that wouldn't exceed the thresholds when
136 * diluted by using what's effectively a larger block size.
137 */
138
4/4
✓ Branch 0 taken 39 times.
✓ Branch 1 taken 10 times.
✓ Branch 2 taken 10 times.
✓ Branch 3 taken 29 times.
49 int vsub = plane == 1 || plane == 2 ? decimate->vsub : 0;
139
4/4
✓ Branch 0 taken 39 times.
✓ Branch 1 taken 10 times.
✓ Branch 2 taken 10 times.
✓ Branch 3 taken 29 times.
49 int hsub = plane == 1 || plane == 2 ? decimate->hsub : 0;
140
2/2
✓ Branch 0 taken 19 times.
✓ Branch 1 taken 30 times.
49 if (diff_planes(ctx,
141 cur->data[plane], cur->linesize[plane],
142 ref->data[plane], ref->linesize[plane],
143 49 AV_CEIL_RSHIFT(ref->width, hsub),
144 49 AV_CEIL_RSHIFT(ref->height, vsub)))
145 19 return 0;
146 }
147
148 10 return 1;
149 }
150
151 2 static av_cold int init(AVFilterContext *ctx)
152 {
153 2 DecimateContext *decimate = ctx->priv;
154
155 2 decimate->sad = av_pixelutils_get_sad_fn(3, 3, 0, ctx); // 8x8, not aligned on blocksize
156
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (!decimate->sad)
157 return AVERROR(EINVAL);
158
159 2 av_log(ctx, AV_LOG_VERBOSE, "max_drop_count:%d hi:%d lo:%d frac:%f\n",
160 2 decimate->max_drop_count, decimate->hi, decimate->lo, decimate->frac);
161
162 2 return 0;
163 }
164
165 2 static av_cold void uninit(AVFilterContext *ctx)
166 {
167 2 DecimateContext *decimate = ctx->priv;
168 2 av_frame_free(&decimate->ref);
169 2 }
170
171 static const enum AVPixelFormat pix_fmts[] = {
172 AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV422P,
173 AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV411P,
174 AV_PIX_FMT_YUV410P, AV_PIX_FMT_YUV440P,
175 AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ422P,
176 AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ440P,
177 AV_PIX_FMT_YUVA420P,
178
179 AV_PIX_FMT_GBRP,
180
181 AV_PIX_FMT_YUVA444P,
182 AV_PIX_FMT_YUVA422P,
183
184 AV_PIX_FMT_NONE
185 };
186
187 1 static int config_input(AVFilterLink *inlink)
188 {
189 1 AVFilterContext *ctx = inlink->dst;
190 1 DecimateContext *decimate = ctx->priv;
191 1 const AVPixFmtDescriptor *pix_desc = av_pix_fmt_desc_get(inlink->format);
192 1 decimate->hsub = pix_desc->log2_chroma_w;
193 1 decimate->vsub = pix_desc->log2_chroma_h;
194
195 1 return 0;
196 }
197
198 30 static int filter_frame(AVFilterLink *inlink, AVFrame *cur)
199 {
200 30 DecimateContext *decimate = inlink->dst->priv;
201 30 AVFilterLink *outlink = inlink->dst->outputs[0];
202 int ret;
203
204
4/4
✓ Branch 0 taken 29 times.
✓ Branch 1 taken 1 times.
✓ Branch 3 taken 10 times.
✓ Branch 4 taken 19 times.
30 if (decimate->ref && decimate_frame(inlink->dst, cur, decimate->ref)) {
205 10 decimate->drop_count = FFMAX(1, decimate->drop_count+1);
206 10 decimate->keep_count = -1; // do not keep any more frames until non-similar frames are detected
207 } else {
208 20 av_frame_free(&decimate->ref);
209 20 decimate->ref = cur;
210 20 decimate->drop_count = FFMIN(-1, decimate->drop_count-1);
211
2/2
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 10 times.
20 if (decimate->keep_count < 0) // re-enable counting similiar frames to ignore before dropping
212 10 decimate->keep_count = 0;
213
214
1/2
✗ Branch 2 not taken.
✓ Branch 3 taken 20 times.
20 if ((ret = ff_filter_frame(outlink, av_frame_clone(cur))) < 0)
215 return ret;
216 }
217
218 30 av_log(inlink->dst, AV_LOG_DEBUG,
219 "%s pts:%s pts_time:%s drop_count:%d keep_count:%d\n",
220
2/2
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 20 times.
30 decimate->drop_count > 0 ? "drop" : "keep",
221 30 av_ts2str(cur->pts), av_ts2timestr(cur->pts, &inlink->time_base),
222 decimate->drop_count,
223 decimate->keep_count);
224
225
2/2
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 20 times.
30 if (decimate->drop_count > 0)
226 10 av_frame_free(&cur);
227
228 30 return 0;
229 }
230
231 static const AVFilterPad mpdecimate_inputs[] = {
232 {
233 .name = "default",
234 .type = AVMEDIA_TYPE_VIDEO,
235 .config_props = config_input,
236 .filter_frame = filter_frame,
237 },
238 };
239
240 const AVFilter ff_vf_mpdecimate = {
241 .name = "mpdecimate",
242 .description = NULL_IF_CONFIG_SMALL("Remove near-duplicate frames."),
243 .init = init,
244 .uninit = uninit,
245 .priv_size = sizeof(DecimateContext),
246 .priv_class = &mpdecimate_class,
247 FILTER_INPUTS(mpdecimate_inputs),
248 FILTER_OUTPUTS(ff_video_default_filterpad),
249 FILTER_PIXFMTS_ARRAY(pix_fmts),
250 };
251