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