FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavfilter/vf_decimate.c
Date: 2024-07-24 19:24:46
Exec Total Coverage
Lines: 170 225 75.6%
Functions: 6 6 100.0%
Branches: 104 200 52.0%

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