FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavfilter/vf_mpdecimate.c
Date: 2022-12-05 03:11:11
Exec Total Coverage
Lines: 63 71 88.7%
Functions: 6 6 100.0%
Branches: 34 44 77.3%

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 "formats.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 hsub, vsub; ///< chroma subsampling values
50 AVFrame *ref; ///< reference picture
51 av_pixelutils_sad_fn sad; ///< sum of absolute difference function
52 } DecimateContext;
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 49 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 49 DecimateContext *decimate = ctx->priv;
77
78 int x, y;
79 49 int d, c = 0;
80 49 int t = (w/16)*(h/16)*decimate->frac;
81
82 /* compute difference for blocks of 8x8 bytes */
83
2/2
✓ Branch 0 taken 1200 times.
✓ Branch 1 taken 30 times.
1230 for (y = 0; y < h-7; y += 4) {
84
2/2
✓ Branch 0 taken 68137 times.
✓ Branch 1 taken 1181 times.
69318 for (x = 8; x < w-7; x += 4) {
85 68137 d = decimate->sad(cur + y*cur_linesize + x, cur_linesize,
86 68137 ref + y*ref_linesize + x, ref_linesize);
87
2/2
✓ Branch 0 taken 19 times.
✓ Branch 1 taken 68118 times.
68137 if (d > decimate->hi) {
88 19 av_log(ctx, AV_LOG_DEBUG, "%d>=hi ", d);
89 19 return 1;
90 }
91
2/2
✓ Branch 0 taken 43 times.
✓ Branch 1 taken 68075 times.
68118 if (d > decimate->lo) {
92 43 c++;
93
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 43 times.
43 if (c > t) {
94 av_log(ctx, AV_LOG_DEBUG, "lo:%d>=%d ", c, t);
95 return 1;
96 }
97 }
98 }
99 }
100
101 30 av_log(ctx, AV_LOG_DEBUG, "lo:%d<%d ", c, t);
102 30 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 */
109 29 static int decimate_frame(AVFilterContext *ctx,
110 AVFrame *cur, AVFrame *ref)
111 {
112 29 DecimateContext *decimate = ctx->priv;
113 int plane;
114
115
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 29 times.
29 if (decimate->max_drop_count > 0 &&
116 decimate->drop_count >= decimate->max_drop_count)
117 return 0;
118
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 29 times.
29 if (decimate->max_drop_count < 0 &&
119 (decimate->drop_count-1) > decimate->max_drop_count)
120 return 0;
121
122
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++) {
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
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;
129
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;
130
2/2
✓ Branch 0 taken 19 times.
✓ Branch 1 taken 30 times.
49 if (diff_planes(ctx,
131 cur->data[plane], cur->linesize[plane],
132 ref->data[plane], ref->linesize[plane],
133 49 AV_CEIL_RSHIFT(ref->width, hsub),
134 49 AV_CEIL_RSHIFT(ref->height, vsub))) {
135 19 emms_c();
136 19 return 0;
137 }
138 }
139
140 10 emms_c();
141 10 return 1;
142 }
143
144 2 static av_cold int init(AVFilterContext *ctx)
145 {
146 2 DecimateContext *decimate = ctx->priv;
147
148 2 decimate->sad = av_pixelutils_get_sad_fn(3, 3, 0, ctx); // 8x8, not aligned on blocksize
149
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (!decimate->sad)
150 return AVERROR(EINVAL);
151
152 2 av_log(ctx, AV_LOG_VERBOSE, "max_drop_count:%d hi:%d lo:%d frac:%f\n",
153 2 decimate->max_drop_count, decimate->hi, decimate->lo, decimate->frac);
154
155 2 return 0;
156 }
157
158 2 static av_cold void uninit(AVFilterContext *ctx)
159 {
160 2 DecimateContext *decimate = ctx->priv;
161 2 av_frame_free(&decimate->ref);
162 2 }
163
164 static const enum AVPixelFormat pix_fmts[] = {
165 AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV422P,
166 AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV411P,
167 AV_PIX_FMT_YUV410P, AV_PIX_FMT_YUV440P,
168 AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ422P,
169 AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ440P,
170 AV_PIX_FMT_YUVA420P,
171
172 AV_PIX_FMT_GBRP,
173
174 AV_PIX_FMT_YUVA444P,
175 AV_PIX_FMT_YUVA422P,
176
177 AV_PIX_FMT_NONE
178 };
179
180 1 static int config_input(AVFilterLink *inlink)
181 {
182 1 AVFilterContext *ctx = inlink->dst;
183 1 DecimateContext *decimate = ctx->priv;
184 1 const AVPixFmtDescriptor *pix_desc = av_pix_fmt_desc_get(inlink->format);
185 1 decimate->hsub = pix_desc->log2_chroma_w;
186 1 decimate->vsub = pix_desc->log2_chroma_h;
187
188 1 return 0;
189 }
190
191 30 static int filter_frame(AVFilterLink *inlink, AVFrame *cur)
192 {
193 30 DecimateContext *decimate = inlink->dst->priv;
194 30 AVFilterLink *outlink = inlink->dst->outputs[0];
195 int ret;
196
197
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)) {
198 10 decimate->drop_count = FFMAX(1, decimate->drop_count+1);
199 } else {
200 20 av_frame_free(&decimate->ref);
201 20 decimate->ref = cur;
202 20 decimate->drop_count = FFMIN(-1, decimate->drop_count-1);
203
204
1/2
✗ Branch 2 not taken.
✓ Branch 3 taken 20 times.
20 if ((ret = ff_filter_frame(outlink, av_frame_clone(cur))) < 0)
205 return ret;
206 }
207
208 30 av_log(inlink->dst, AV_LOG_DEBUG,
209 "%s pts:%s pts_time:%s drop_count:%d\n",
210
2/2
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 20 times.
30 decimate->drop_count > 0 ? "drop" : "keep",
211 30 av_ts2str(cur->pts), av_ts2timestr(cur->pts, &inlink->time_base),
212 decimate->drop_count);
213
214
2/2
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 20 times.
30 if (decimate->drop_count > 0)
215 10 av_frame_free(&cur);
216
217 30 return 0;
218 }
219
220 static const AVFilterPad mpdecimate_inputs[] = {
221 {
222 .name = "default",
223 .type = AVMEDIA_TYPE_VIDEO,
224 .config_props = config_input,
225 .filter_frame = filter_frame,
226 },
227 };
228
229 static const AVFilterPad mpdecimate_outputs[] = {
230 {
231 .name = "default",
232 .type = AVMEDIA_TYPE_VIDEO,
233 },
234 };
235
236 const AVFilter ff_vf_mpdecimate = {
237 .name = "mpdecimate",
238 .description = NULL_IF_CONFIG_SMALL("Remove near-duplicate frames."),
239 .init = init,
240 .uninit = uninit,
241 .priv_size = sizeof(DecimateContext),
242 .priv_class = &mpdecimate_class,
243 FILTER_INPUTS(mpdecimate_inputs),
244 FILTER_OUTPUTS(mpdecimate_outputs),
245 FILTER_PIXFMTS_ARRAY(pix_fmts),
246 };
247