FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavfilter/vf_decimate.c
Date: 2022-12-05 03:11:11
Exec Total Coverage
Lines: 166 218 76.1%
Functions: 6 6 100.0%
Branches: 99 188 52.7%

Line Branch Exec Source
1 /*
2 * Copyright (c) 2012 Fredrik Mellbin
3 * Copyright (c) 2013 Clément Bœsch
4 *
5 * This file is part of FFmpeg.
6 *
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (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 GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22 #include "libavutil/opt.h"
23 #include "libavutil/pixdesc.h"
24 #include "libavutil/timestamp.h"
25 #include "avfilter.h"
26 #include "filters.h"
27 #include "internal.h"
28
29 #define INPUT_MAIN 0
30 #define INPUT_CLEANSRC 1
31
32 struct qitem {
33 AVFrame *frame;
34 int64_t maxbdiff;
35 int64_t totdiff;
36 };
37
38 typedef struct DecimateContext {
39 const AVClass *class;
40 struct qitem *queue; ///< window of cycle frames and the associated data diff
41 int fid; ///< current frame id in the queue
42 int filled; ///< 1 if the queue is filled, 0 otherwise
43 AVFrame *last; ///< last frame from the previous queue
44 AVFrame **clean_src; ///< frame queue for the clean source
45 int got_frame[2]; ///< frame request flag for each input stream
46 int64_t last_pts; ///< last output timestamp
47 int64_t start_pts; ///< base for output timestamps
48 uint32_t eof; ///< bitmask for end of stream
49 int hsub, vsub; ///< chroma subsampling values
50 int depth;
51 int nxblocks, nyblocks;
52 int bdiffsize;
53 int64_t *bdiffs;
54
55 /* options */
56 int cycle;
57 double dupthresh_flt;
58 double scthresh_flt;
59 int64_t dupthresh;
60 int64_t scthresh;
61 int blockx, blocky;
62 int ppsrc;
63 int chroma;
64 } DecimateContext;
65
66 #define OFFSET(x) offsetof(DecimateContext, x)
67 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
68
69 static const AVOption decimate_options[] = {
70 { "cycle", "set the number of frame from which one will be dropped", OFFSET(cycle), AV_OPT_TYPE_INT, {.i64 = 5}, 2, 25, FLAGS },
71 { "dupthresh", "set duplicate threshold", OFFSET(dupthresh_flt), AV_OPT_TYPE_DOUBLE, {.dbl = 1.1}, 0, 100, FLAGS },
72 { "scthresh", "set scene change threshold", OFFSET(scthresh_flt), AV_OPT_TYPE_DOUBLE, {.dbl = 15.0}, 0, 100, FLAGS },
73 { "blockx", "set the size of the x-axis blocks used during metric calculations", OFFSET(blockx), AV_OPT_TYPE_INT, {.i64 = 32}, 4, 1<<9, FLAGS },
74 { "blocky", "set the size of the y-axis blocks used during metric calculations", OFFSET(blocky), AV_OPT_TYPE_INT, {.i64 = 32}, 4, 1<<9, FLAGS },
75 { "ppsrc", "mark main input as a pre-processed input and activate clean source input stream", OFFSET(ppsrc), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS },
76 { "chroma", "set whether or not chroma is considered in the metric calculations", OFFSET(chroma), AV_OPT_TYPE_BOOL, {.i64=1}, 0, 1, FLAGS },
77 { NULL }
78 };
79
80 AVFILTER_DEFINE_CLASS(decimate);
81
82 1437 static void calc_diffs(const DecimateContext *dm, struct qitem *q,
83 const AVFrame *f1, const AVFrame *f2)
84 {
85 1437 int64_t maxdiff = -1;
86 1437 int64_t *bdiffs = dm->bdiffs;
87 int plane, i, j;
88
89 1437 memset(bdiffs, 0, dm->bdiffsize * sizeof(*bdiffs));
90
91
4/6
✓ Branch 0 taken 5748 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 5748 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 4311 times.
✓ Branch 5 taken 1437 times.
5748 for (plane = 0; plane < (dm->chroma && f1->data[2] ? 3 : 1); plane++) {
92 int x, y, xl;
93 4311 const int linesize1 = f1->linesize[plane];
94 4311 const int linesize2 = f2->linesize[plane];
95 4311 const uint8_t *f1p = f1->data[plane];
96 4311 const uint8_t *f2p = f2->data[plane];
97
2/2
✓ Branch 0 taken 2874 times.
✓ Branch 1 taken 1437 times.
4311 int width = plane ? AV_CEIL_RSHIFT(f1->width, dm->hsub) : f1->width;
98
2/2
✓ Branch 0 taken 2874 times.
✓ Branch 1 taken 1437 times.
4311 int height = plane ? AV_CEIL_RSHIFT(f1->height, dm->vsub) : f1->height;
99 4311 int hblockx = dm->blockx / 2;
100 4311 int hblocky = dm->blocky / 2;
101
102
2/2
✓ Branch 0 taken 2874 times.
✓ Branch 1 taken 1437 times.
4311 if (plane) {
103 2874 hblockx >>= dm->hsub;
104 2874 hblocky >>= dm->vsub;
105 }
106
107
2/2
✓ Branch 0 taken 689760 times.
✓ Branch 1 taken 4311 times.
694071 for (y = 0; y < height; y++) {
108 689760 int ydest = y / hblocky;
109 689760 int xdest = 0;
110
111 #define CALC_DIFF(nbits) do { \
112 for (x = 0; x < width; x += hblockx) { \
113 int64_t acc = 0; \
114 int m = FFMIN(width, x + hblockx); \
115 for (xl = x; xl < m; xl++) \
116 acc += abs(((const uint##nbits##_t *)f1p)[xl] - \
117 ((const uint##nbits##_t *)f2p)[xl]); \
118 bdiffs[ydest * dm->nxblocks + xdest] += acc; \
119 xdest++; \
120 } \
121 } while (0)
122
5/6
✓ Branch 0 taken 689760 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 165542400 times.
✓ Branch 3 taken 13795200 times.
✓ Branch 4 taken 13795200 times.
✓ Branch 5 taken 689760 times.
180027360 if (dm->depth == 8) CALC_DIFF(8);
123 else CALC_DIFF(16);
124
125 689760 f1p += linesize1;
126 689760 f2p += linesize2;
127 }
128 }
129
130
2/2
✓ Branch 0 taken 20118 times.
✓ Branch 1 taken 1437 times.
21555 for (i = 0; i < dm->nyblocks - 1; i++) {
131
2/2
✓ Branch 0 taken 382242 times.
✓ Branch 1 taken 20118 times.
402360 for (j = 0; j < dm->nxblocks - 1; j++) {
132 382242 int64_t tmp = bdiffs[ i * dm->nxblocks + j ]
133 382242 + bdiffs[ i * dm->nxblocks + j + 1]
134 382242 + bdiffs[(i + 1) * dm->nxblocks + j ]
135 382242 + bdiffs[(i + 1) * dm->nxblocks + j + 1];
136
2/2
✓ Branch 0 taken 7419 times.
✓ Branch 1 taken 374823 times.
382242 if (tmp > maxdiff)
137 7419 maxdiff = tmp;
138 }
139 }
140
141 1437 q->totdiff = 0;
142
2/2
✓ Branch 0 taken 431100 times.
✓ Branch 1 taken 1437 times.
432537 for (i = 0; i < dm->bdiffsize; i++)
143 431100 q->totdiff += bdiffs[i];
144 1437 q->maxbdiff = maxdiff;
145 1437 }
146
147 1443 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
148 {
149 1443 int scpos = -1, duppos = -1;
150 1443 int drop = INT_MIN, i, lowest = 0, ret;
151 1443 AVFilterContext *ctx = inlink->dst;
152 1443 AVFilterLink *outlink = ctx->outputs[0];
153 1443 DecimateContext *dm = ctx->priv;
154 AVFrame *prv;
155
156 /* update frames queue(s) */
157
1/2
✓ Branch 0 taken 1443 times.
✗ Branch 1 not taken.
1443 if (FF_INLINK_IDX(inlink) == INPUT_MAIN) {
158 1443 dm->queue[dm->fid].frame = in;
159 1443 dm->got_frame[INPUT_MAIN] = 1;
160 } else {
161 dm->clean_src[dm->fid] = in;
162 dm->got_frame[INPUT_CLEANSRC] = 1;
163 }
164
2/6
✓ Branch 0 taken 1443 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1443 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
1443 if (!dm->got_frame[INPUT_MAIN] || (dm->ppsrc && !dm->got_frame[INPUT_CLEANSRC]))
165 return 0;
166 1443 dm->got_frame[INPUT_MAIN] = dm->got_frame[INPUT_CLEANSRC] = 0;
167
168
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1443 times.
1443 if (dm->ppsrc)
169 in = dm->queue[dm->fid].frame;
170
171
2/2
✓ Branch 0 taken 1440 times.
✓ Branch 1 taken 3 times.
1443 if (in) {
172 /* update frame metrics */
173
2/2
✓ Branch 0 taken 1080 times.
✓ Branch 1 taken 360 times.
1440 prv = dm->fid ? dm->queue[dm->fid - 1].frame : dm->last;
174
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 1437 times.
1440 if (!prv) {
175 3 dm->queue[dm->fid].maxbdiff = INT64_MAX;
176 3 dm->queue[dm->fid].totdiff = INT64_MAX;
177 } else {
178 1437 calc_diffs(dm, &dm->queue[dm->fid], prv, in);
179 }
180
2/2
✓ Branch 0 taken 1080 times.
✓ Branch 1 taken 360 times.
1440 if (++dm->fid != dm->cycle)
181 1080 return 0;
182 360 av_frame_free(&dm->last);
183 360 dm->last = av_frame_clone(in);
184 360 dm->fid = 0;
185
186 /* we have a complete cycle, select the frame to drop */
187 360 lowest = 0;
188
2/2
✓ Branch 0 taken 1440 times.
✓ Branch 1 taken 360 times.
1800 for (i = 0; i < dm->cycle; i++) {
189
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 1437 times.
1440 if (dm->queue[i].totdiff > dm->scthresh)
190 3 scpos = i;
191
2/2
✓ Branch 0 taken 425 times.
✓ Branch 1 taken 1015 times.
1440 if (dm->queue[i].maxbdiff < dm->queue[lowest].maxbdiff)
192 425 lowest = i;
193 }
194
1/2
✓ Branch 0 taken 360 times.
✗ Branch 1 not taken.
360 if (dm->queue[lowest].maxbdiff < dm->dupthresh)
195 360 duppos = lowest;
196
3/4
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 357 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 3 times.
360 drop = scpos >= 0 && duppos < 0 ? scpos : lowest;
197 }
198
199 /* metrics debug */
200
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 363 times.
363 if (av_log_get_level() >= AV_LOG_DEBUG) {
201 av_log(ctx, AV_LOG_DEBUG, "1/%d frame drop:\n", dm->cycle);
202 for (i = 0; i < dm->cycle && dm->queue[i].frame; i++) {
203 av_log(ctx, AV_LOG_DEBUG," #%d: totdiff=%08"PRIx64" maxbdiff=%08"PRIx64"%s%s%s%s\n",
204 i + 1, dm->queue[i].totdiff, dm->queue[i].maxbdiff,
205 i == scpos ? " sc" : "",
206 i == duppos ? " dup" : "",
207 i == lowest ? " lowest" : "",
208 i == drop ? " [DROP]" : "");
209 }
210 }
211
212 /* push all frames except the drop */
213 363 ret = 0;
214
4/4
✓ Branch 0 taken 1443 times.
✓ Branch 1 taken 360 times.
✓ Branch 2 taken 1440 times.
✓ Branch 3 taken 3 times.
1803 for (i = 0; i < dm->cycle && dm->queue[i].frame; i++) {
215 1440 AVRational in_tb = ctx->inputs[INPUT_MAIN]->time_base;
216
2/2
✓ Branch 0 taken 360 times.
✓ Branch 1 taken 1080 times.
1440 if (i == drop) {
217
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 360 times.
360 if (dm->ppsrc)
218 av_frame_free(&dm->clean_src[i]);
219 360 av_frame_free(&dm->queue[i].frame);
220 } else {
221 1080 AVFrame *frame = dm->queue[i].frame;
222 1080 dm->queue[i].frame = NULL;
223
3/4
✓ Branch 0 taken 1080 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✓ Branch 3 taken 1077 times.
1080 if (frame->pts != AV_NOPTS_VALUE && dm->start_pts == AV_NOPTS_VALUE)
224 3 dm->start_pts = av_rescale_q(frame->pts, in_tb, outlink->time_base);
225
226
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1080 times.
1080 if (dm->ppsrc) {
227 av_frame_free(&frame);
228 frame = dm->clean_src[i];
229 if (!frame)
230 continue;
231 dm->clean_src[i] = NULL;
232 }
233 2160 frame->pts = outlink->frame_count_in +
234
1/2
✓ Branch 0 taken 1080 times.
✗ Branch 1 not taken.
1080 (dm->start_pts == AV_NOPTS_VALUE ? 0 : dm->start_pts);
235 1080 frame->duration = 1;
236 1080 dm->last_pts = frame->pts;
237 1080 ret = ff_filter_frame(outlink, frame);
238
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1080 times.
1080 if (ret < 0)
239 break;
240 }
241 }
242
243 363 return ret;
244 }
245
246 1801 static int activate(AVFilterContext *ctx)
247 {
248 1801 DecimateContext *dm = ctx->priv;
249 1801 AVFrame *frame = NULL;
250 1801 int ret = 0, status;
251 int64_t pts;
252
253
1/4
✗ Branch 1 not taken.
✓ Branch 2 taken 1801 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
1801 FF_FILTER_FORWARD_STATUS_BACK_ALL(ctx->outputs[0], ctx);
254
255
4/6
✓ Branch 0 taken 1801 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1801 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1440 times.
✓ Branch 5 taken 361 times.
3602 if ((dm->got_frame[INPUT_MAIN] == 0) && !(dm->eof & (1 << INPUT_MAIN)) &&
256 1801 (ret = ff_inlink_consume_frame(ctx->inputs[INPUT_MAIN], &frame)) > 0) {
257 1440 ret = filter_frame(ctx->inputs[INPUT_MAIN], frame);
258
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1440 times.
1440 if (ret < 0)
259 return ret;
260 }
261
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1801 times.
1801 if (ret < 0)
262 return ret;
263
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1801 times.
1801 if (dm->ppsrc &&
264 (dm->got_frame[INPUT_CLEANSRC] == 0) && !(dm->eof & (1 << INPUT_CLEANSRC)) &&
265 (ret = ff_inlink_consume_frame(ctx->inputs[INPUT_CLEANSRC], &frame)) > 0) {
266 ret = filter_frame(ctx->inputs[INPUT_CLEANSRC], frame);
267 if (ret < 0)
268 return ret;
269 }
270
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1801 times.
1801 if (ret < 0) {
271 return ret;
272
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1801 times.
1801 } else if (dm->eof == ((1 << INPUT_MAIN) | (dm->ppsrc << INPUT_CLEANSRC))) {
273 ff_outlink_set_status(ctx->outputs[0], AVERROR_EOF, dm->last_pts);
274 return 0;
275
3/4
✓ Branch 0 taken 1801 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 3 times.
✓ Branch 4 taken 1798 times.
1801 } else if (!(dm->eof & (1 << INPUT_MAIN)) && ff_inlink_acknowledge_status(ctx->inputs[INPUT_MAIN], &status, &pts)) {
276
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 if (status == AVERROR_EOF) { // flushing
277 3 dm->eof |= 1 << INPUT_MAIN;
278
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (dm->ppsrc)
279 filter_frame(ctx->inputs[INPUT_CLEANSRC], NULL);
280 3 filter_frame(ctx->inputs[INPUT_MAIN], NULL);
281 3 ff_outlink_set_status(ctx->outputs[0], AVERROR_EOF, dm->last_pts);
282 3 return 0;
283 }
284
1/6
✗ Branch 0 not taken.
✓ Branch 1 taken 1798 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
1798 } else if (dm->ppsrc && !(dm->eof & (1 << INPUT_CLEANSRC)) && ff_inlink_acknowledge_status(ctx->inputs[INPUT_CLEANSRC], &status, &pts)) {
285 if (status == AVERROR_EOF) { // flushing
286 dm->eof |= 1 << INPUT_CLEANSRC;
287 filter_frame(ctx->inputs[INPUT_MAIN], NULL);
288 filter_frame(ctx->inputs[INPUT_CLEANSRC], NULL);
289 ff_outlink_set_status(ctx->outputs[0], AVERROR_EOF, dm->last_pts);
290 return 0;
291 }
292 }
293
294
3/4
✓ Branch 1 taken 600 times.
✓ Branch 2 taken 1198 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 600 times.
1798 if (ff_inlink_queued_frames(ctx->inputs[INPUT_MAIN]) > 0 && (!dm->ppsrc ||
295 (dm->ppsrc && ff_inlink_queued_frames(ctx->inputs[INPUT_CLEANSRC]) > 0))) {
296 600 ff_filter_set_ready(ctx, 100);
297
2/2
✓ Branch 1 taken 840 times.
✓ Branch 2 taken 358 times.
1198 } else if (ff_outlink_frame_wanted(ctx->outputs[0])) {
298
1/2
✓ Branch 0 taken 840 times.
✗ Branch 1 not taken.
840 if (dm->got_frame[INPUT_MAIN] == 0)
299 840 ff_inlink_request_frame(ctx->inputs[INPUT_MAIN]);
300
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 840 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
840 if (dm->ppsrc && (dm->got_frame[INPUT_CLEANSRC] == 0))
301 ff_inlink_request_frame(ctx->inputs[INPUT_CLEANSRC]);
302 }
303 1798 return 0;
304 }
305
306 6 static av_cold int decimate_init(AVFilterContext *ctx)
307 {
308 6 DecimateContext *dm = ctx->priv;
309 6 AVFilterPad pad = {
310 .name = "main",
311 .type = AVMEDIA_TYPE_VIDEO,
312 };
313 int ret;
314
315
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 6 times.
6 if ((ret = ff_append_inpad(ctx, &pad)) < 0)
316 return ret;
317
318
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 if (dm->ppsrc) {
319 pad.name = "clean_src";
320 pad.config_props = NULL;
321 if ((ret = ff_append_inpad(ctx, &pad)) < 0)
322 return ret;
323 }
324
325
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
6 if ((dm->blockx & (dm->blockx - 1)) ||
326
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 (dm->blocky & (dm->blocky - 1))) {
327 av_log(ctx, AV_LOG_ERROR, "blockx and blocky settings must be power of two\n");
328 return AVERROR(EINVAL);
329 }
330
331 6 dm->start_pts = AV_NOPTS_VALUE;
332
333 6 return 0;
334 }
335
336 6 static av_cold void decimate_uninit(AVFilterContext *ctx)
337 {
338 int i;
339 6 DecimateContext *dm = ctx->priv;
340
341 6 av_frame_free(&dm->last);
342 6 av_freep(&dm->bdiffs);
343
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 3 times.
6 if (dm->queue) {
344
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 3 times.
15 for (i = 0; i < dm->cycle; i++)
345 12 av_frame_free(&dm->queue[i].frame);
346 }
347 6 av_freep(&dm->queue);
348
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 if (dm->clean_src) {
349 for (i = 0; i < dm->cycle; i++)
350 av_frame_free(&dm->clean_src[i]);
351 }
352 6 av_freep(&dm->clean_src);
353 6 }
354
355 static const enum AVPixelFormat pix_fmts[] = {
356 #define PF_NOALPHA(suf) AV_PIX_FMT_YUV420##suf, AV_PIX_FMT_YUV422##suf, AV_PIX_FMT_YUV444##suf
357 #define PF_ALPHA(suf) AV_PIX_FMT_YUVA420##suf, AV_PIX_FMT_YUVA422##suf, AV_PIX_FMT_YUVA444##suf
358 #define PF(suf) PF_NOALPHA(suf), PF_ALPHA(suf)
359 PF(P), PF(P9), PF(P10), PF_NOALPHA(P12), PF_NOALPHA(P14), PF(P16),
360 AV_PIX_FMT_YUV440P10, AV_PIX_FMT_YUV440P12,
361 AV_PIX_FMT_YUV440P, AV_PIX_FMT_YUV411P, AV_PIX_FMT_YUV410P,
362 AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ440P, AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ420P,
363 AV_PIX_FMT_YUVJ411P,
364 AV_PIX_FMT_GRAY8, AV_PIX_FMT_GRAY9, AV_PIX_FMT_GRAY10, AV_PIX_FMT_GRAY12, AV_PIX_FMT_GRAY14,
365 AV_PIX_FMT_GRAY16,
366 AV_PIX_FMT_NONE
367 };
368
369 3 static int config_output(AVFilterLink *outlink)
370 {
371 3 AVFilterContext *ctx = outlink->src;
372 3 DecimateContext *dm = ctx->priv;
373 3 const AVFilterLink *inlink = ctx->inputs[INPUT_MAIN];
374 3 AVRational fps = inlink->frame_rate;
375 int max_value;
376 3 const AVPixFmtDescriptor *pix_desc = av_pix_fmt_desc_get(inlink->format);
377 3 const int w = inlink->w;
378 3 const int h = inlink->h;
379
380 3 dm->hsub = pix_desc->log2_chroma_w;
381 3 dm->vsub = pix_desc->log2_chroma_h;
382 3 dm->depth = pix_desc->comp[0].depth;
383 3 max_value = (1 << dm->depth) - 1;
384 3 dm->scthresh = (int64_t)(((int64_t)max_value * w * h * dm->scthresh_flt) / 100);
385 3 dm->dupthresh = (int64_t)(((int64_t)max_value * dm->blockx * dm->blocky * dm->dupthresh_flt) / 100);
386 3 dm->nxblocks = (w + dm->blockx/2 - 1) / (dm->blockx/2);
387 3 dm->nyblocks = (h + dm->blocky/2 - 1) / (dm->blocky/2);
388 3 dm->bdiffsize = dm->nxblocks * dm->nyblocks;
389 3 dm->bdiffs = av_malloc_array(dm->bdiffsize, sizeof(*dm->bdiffs));
390 3 dm->queue = av_calloc(dm->cycle, sizeof(*dm->queue));
391
392
2/4
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 3 times.
3 if (!dm->bdiffs || !dm->queue)
393 return AVERROR(ENOMEM);
394
395
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (dm->ppsrc) {
396 dm->clean_src = av_calloc(dm->cycle, sizeof(*dm->clean_src));
397 if (!dm->clean_src)
398 return AVERROR(ENOMEM);
399 }
400
401
2/4
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 3 times.
3 if (!fps.num || !fps.den) {
402 av_log(ctx, AV_LOG_ERROR, "The input needs a constant frame rate; "
403 "current rate of %d/%d is invalid\n", fps.num, fps.den);
404 return AVERROR(EINVAL);
405 }
406 3 fps = av_mul_q(fps, (AVRational){dm->cycle - 1, dm->cycle});
407 3 av_log(ctx, AV_LOG_VERBOSE, "FPS: %d/%d -> %d/%d\n",
408 3 inlink->frame_rate.num, inlink->frame_rate.den, fps.num, fps.den);
409 3 outlink->time_base = av_inv_q(fps);
410 3 outlink->frame_rate = fps;
411 3 outlink->sample_aspect_ratio = inlink->sample_aspect_ratio;
412
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (dm->ppsrc) {
413 outlink->w = ctx->inputs[INPUT_CLEANSRC]->w;
414 outlink->h = ctx->inputs[INPUT_CLEANSRC]->h;
415 } else {
416 3 outlink->w = inlink->w;
417 3 outlink->h = inlink->h;
418 }
419 3 return 0;
420 }
421
422 static const AVFilterPad decimate_outputs[] = {
423 {
424 .name = "default",
425 .type = AVMEDIA_TYPE_VIDEO,
426 .config_props = config_output,
427 },
428 };
429
430 const AVFilter ff_vf_decimate = {
431 .name = "decimate",
432 .description = NULL_IF_CONFIG_SMALL("Decimate frames (post field matching filter)."),
433 .init = decimate_init,
434 .activate = activate,
435 .uninit = decimate_uninit,
436 .priv_size = sizeof(DecimateContext),
437 FILTER_OUTPUTS(decimate_outputs),
438 FILTER_PIXFMTS_ARRAY(pix_fmts),
439 .priv_class = &decimate_class,
440 .flags = AVFILTER_FLAG_DYNAMIC_INPUTS,
441 };
442