| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* | ||
| 2 | * Copyright (c) 2011 Stefano Sabatini | ||
| 3 | * | ||
| 4 | * This file is part of FFmpeg. | ||
| 5 | * | ||
| 6 | * FFmpeg is free software; you can redistribute it and/or | ||
| 7 | * modify it under the terms of the GNU Lesser General Public | ||
| 8 | * License as published by the Free Software Foundation; either | ||
| 9 | * version 2.1 of the License, or (at your option) any later version. | ||
| 10 | * | ||
| 11 | * FFmpeg is distributed in the hope that it will be useful, | ||
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
| 14 | * Lesser General Public License for more details. | ||
| 15 | * | ||
| 16 | * You should have received a copy of the GNU Lesser General Public | ||
| 17 | * License along with FFmpeg; if not, write to the Free Software | ||
| 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | ||
| 19 | */ | ||
| 20 | |||
| 21 | /** | ||
| 22 | * @file | ||
| 23 | * filter for selecting which frame passes in the filterchain | ||
| 24 | */ | ||
| 25 | |||
| 26 | #include "config_components.h" | ||
| 27 | |||
| 28 | #include "libavutil/avstring.h" | ||
| 29 | #include "libavutil/eval.h" | ||
| 30 | #include "libavutil/fifo.h" | ||
| 31 | #include "libavutil/imgutils.h" | ||
| 32 | #include "libavutil/internal.h" | ||
| 33 | #include "libavutil/opt.h" | ||
| 34 | #include "libavutil/pixdesc.h" | ||
| 35 | #include "avfilter.h" | ||
| 36 | #include "audio.h" | ||
| 37 | #include "filters.h" | ||
| 38 | #include "formats.h" | ||
| 39 | #include "video.h" | ||
| 40 | #include "scene_sad.h" | ||
| 41 | |||
| 42 | static const char *const var_names[] = { | ||
| 43 | "TB", ///< timebase | ||
| 44 | |||
| 45 | "pts", ///< original pts in the file of the frame | ||
| 46 | "start_pts", ///< first PTS in the stream, expressed in TB units | ||
| 47 | "prev_pts", ///< previous frame PTS | ||
| 48 | "prev_selected_pts", ///< previous selected frame PTS | ||
| 49 | |||
| 50 | "t", ///< timestamp expressed in seconds | ||
| 51 | "start_t", ///< first PTS in the stream, expressed in seconds | ||
| 52 | "prev_t", ///< previous frame time | ||
| 53 | "prev_selected_t", ///< previously selected time | ||
| 54 | |||
| 55 | "pict_type", ///< the type of picture in the movie | ||
| 56 | "I", | ||
| 57 | "P", | ||
| 58 | "B", | ||
| 59 | "S", | ||
| 60 | "SI", | ||
| 61 | "SP", | ||
| 62 | "BI", | ||
| 63 | "PICT_TYPE_I", | ||
| 64 | "PICT_TYPE_P", | ||
| 65 | "PICT_TYPE_B", | ||
| 66 | "PICT_TYPE_S", | ||
| 67 | "PICT_TYPE_SI", | ||
| 68 | "PICT_TYPE_SP", | ||
| 69 | "PICT_TYPE_BI", | ||
| 70 | |||
| 71 | "interlace_type", ///< the frame interlace type | ||
| 72 | "PROGRESSIVE", | ||
| 73 | "TOPFIRST", | ||
| 74 | "BOTTOMFIRST", | ||
| 75 | |||
| 76 | "consumed_samples_n",///< number of samples consumed by the filter (only audio) | ||
| 77 | "samples_n", ///< number of samples in the current frame (only audio) | ||
| 78 | "sample_rate", ///< sample rate (only audio) | ||
| 79 | |||
| 80 | "n", ///< frame number (starting from zero) | ||
| 81 | "selected_n", ///< selected frame number (starting from zero) | ||
| 82 | "prev_selected_n", ///< number of the last selected frame | ||
| 83 | |||
| 84 | "key", ///< tell if the frame is a key frame | ||
| 85 | |||
| 86 | "scene", | ||
| 87 | |||
| 88 | "concatdec_select", ///< frame is within the interval set by the concat demuxer | ||
| 89 | |||
| 90 | "ih", ///< ih: Represents the height of the input video frame. | ||
| 91 | "iw", ///< iw: Represents the width of the input video frame. | ||
| 92 | |||
| 93 | "view", | ||
| 94 | |||
| 95 | NULL | ||
| 96 | }; | ||
| 97 | |||
| 98 | enum var_name { | ||
| 99 | VAR_TB, | ||
| 100 | |||
| 101 | VAR_PTS, | ||
| 102 | VAR_START_PTS, | ||
| 103 | VAR_PREV_PTS, | ||
| 104 | VAR_PREV_SELECTED_PTS, | ||
| 105 | |||
| 106 | VAR_T, | ||
| 107 | VAR_START_T, | ||
| 108 | VAR_PREV_T, | ||
| 109 | VAR_PREV_SELECTED_T, | ||
| 110 | |||
| 111 | VAR_PICT_TYPE, | ||
| 112 | VAR_I, | ||
| 113 | VAR_P, | ||
| 114 | VAR_B, | ||
| 115 | VAR_S, | ||
| 116 | VAR_SI, | ||
| 117 | VAR_SP, | ||
| 118 | VAR_BI, | ||
| 119 | VAR_PICT_TYPE_I, | ||
| 120 | VAR_PICT_TYPE_P, | ||
| 121 | VAR_PICT_TYPE_B, | ||
| 122 | VAR_PICT_TYPE_S, | ||
| 123 | VAR_PICT_TYPE_SI, | ||
| 124 | VAR_PICT_TYPE_SP, | ||
| 125 | VAR_PICT_TYPE_BI, | ||
| 126 | |||
| 127 | VAR_INTERLACE_TYPE, | ||
| 128 | VAR_INTERLACE_TYPE_P, | ||
| 129 | VAR_INTERLACE_TYPE_T, | ||
| 130 | VAR_INTERLACE_TYPE_B, | ||
| 131 | |||
| 132 | VAR_CONSUMED_SAMPLES_N, | ||
| 133 | VAR_SAMPLES_N, | ||
| 134 | VAR_SAMPLE_RATE, | ||
| 135 | |||
| 136 | VAR_N, | ||
| 137 | VAR_SELECTED_N, | ||
| 138 | VAR_PREV_SELECTED_N, | ||
| 139 | |||
| 140 | VAR_KEY, | ||
| 141 | |||
| 142 | VAR_SCENE, | ||
| 143 | |||
| 144 | VAR_CONCATDEC_SELECT, | ||
| 145 | |||
| 146 | VAR_IH, | ||
| 147 | VAR_IW, | ||
| 148 | |||
| 149 | VAR_VIEW, | ||
| 150 | |||
| 151 | VAR_VARS_NB | ||
| 152 | }; | ||
| 153 | |||
| 154 | typedef struct SelectContext { | ||
| 155 | const AVClass *class; | ||
| 156 | char *expr_str; | ||
| 157 | AVExpr *expr; | ||
| 158 | double var_values[VAR_VARS_NB]; | ||
| 159 | int bitdepth; | ||
| 160 | int nb_planes; | ||
| 161 | ptrdiff_t width[4]; | ||
| 162 | ptrdiff_t height[4]; | ||
| 163 | int do_scene_detect; ///< 1 if the expression requires scene detection variables, 0 otherwise | ||
| 164 | ff_scene_sad_fn sad; ///< Sum of the absolute difference function (scene detect only) | ||
| 165 | double prev_mafd; ///< previous MAFD (scene detect only) | ||
| 166 | AVFrame *prev_picref; ///< previous frame (scene detect only) | ||
| 167 | double select; | ||
| 168 | int select_out; ///< mark the selected output pad index | ||
| 169 | int nb_outputs; | ||
| 170 | } SelectContext; | ||
| 171 | |||
| 172 | #define OFFSET(x) offsetof(SelectContext, x) | ||
| 173 | #define DEFINE_OPTIONS(filt_name, FLAGS) \ | ||
| 174 | static const AVOption filt_name##_options[] = { \ | ||
| 175 | { "expr", "set an expression to use for selecting frames", OFFSET(expr_str), AV_OPT_TYPE_STRING, { .str = "1" }, .flags=FLAGS }, \ | ||
| 176 | { "e", "set an expression to use for selecting frames", OFFSET(expr_str), AV_OPT_TYPE_STRING, { .str = "1" }, .flags=FLAGS }, \ | ||
| 177 | { "outputs", "set the number of outputs", OFFSET(nb_outputs), AV_OPT_TYPE_INT, {.i64 = 1}, 1, INT_MAX, .flags=FLAGS }, \ | ||
| 178 | { "n", "set the number of outputs", OFFSET(nb_outputs), AV_OPT_TYPE_INT, {.i64 = 1}, 1, INT_MAX, .flags=FLAGS }, \ | ||
| 179 | { NULL } \ | ||
| 180 | } | ||
| 181 | |||
| 182 | 8 | static av_cold int init(AVFilterContext *ctx) | |
| 183 | { | ||
| 184 | 8 | SelectContext *select = ctx->priv; | |
| 185 | int i, ret; | ||
| 186 | |||
| 187 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 8 times.
|
8 | if ((ret = av_expr_parse(&select->expr, select->expr_str, |
| 188 | var_names, NULL, NULL, NULL, NULL, 0, ctx)) < 0) { | ||
| 189 | ✗ | av_log(ctx, AV_LOG_ERROR, "Error while parsing expression '%s'\n", | |
| 190 | select->expr_str); | ||
| 191 | ✗ | return ret; | |
| 192 | } | ||
| 193 | 8 | select->do_scene_detect = !!strstr(select->expr_str, "scene"); | |
| 194 | |||
| 195 |
2/2✓ Branch 0 taken 13 times.
✓ Branch 1 taken 8 times.
|
21 | for (i = 0; i < select->nb_outputs; i++) { |
| 196 | 13 | AVFilterPad pad = { 0 }; | |
| 197 | |||
| 198 | 13 | pad.name = av_asprintf("output%d", i); | |
| 199 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 13 times.
|
13 | if (!pad.name) |
| 200 | ✗ | return AVERROR(ENOMEM); | |
| 201 | 13 | pad.type = ctx->filter->inputs[0].type; | |
| 202 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 13 times.
|
13 | if ((ret = ff_append_outpad_free_name(ctx, &pad)) < 0) |
| 203 | ✗ | return ret; | |
| 204 | } | ||
| 205 | |||
| 206 | 8 | return 0; | |
| 207 | } | ||
| 208 | |||
| 209 | #define INTERLACE_TYPE_P 0 | ||
| 210 | #define INTERLACE_TYPE_T 1 | ||
| 211 | #define INTERLACE_TYPE_B 2 | ||
| 212 | |||
| 213 | 5 | static int config_input(AVFilterLink *inlink) | |
| 214 | { | ||
| 215 | 5 | SelectContext *select = inlink->dst->priv; | |
| 216 | 5 | const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format); | |
| 217 | 15 | int is_yuv = !(desc->flags & AV_PIX_FMT_FLAG_RGB) && | |
| 218 |
3/4✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
✓ Branch 3 taken 1 times.
|
9 | (desc->flags & AV_PIX_FMT_FLAG_PLANAR) && |
| 219 |
1/2✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
|
4 | desc->nb_components >= 3; |
| 220 | |||
| 221 | 5 | select->bitdepth = desc->comp[0].depth; | |
| 222 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 4 times.
|
5 | select->nb_planes = is_yuv ? 1 : av_pix_fmt_count_planes(inlink->format); |
| 223 | |||
| 224 |
2/2✓ Branch 0 taken 5 times.
✓ Branch 1 taken 5 times.
|
10 | for (int plane = 0; plane < select->nb_planes; plane++) { |
| 225 | 5 | ptrdiff_t line_size = av_image_get_linesize(inlink->format, inlink->w, plane); | |
| 226 | 5 | int vsub = desc->log2_chroma_h; | |
| 227 | |||
| 228 | 5 | select->width[plane] = line_size >> (select->bitdepth > 8); | |
| 229 |
2/4✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 5 times.
|
5 | select->height[plane] = plane == 1 || plane == 2 ? AV_CEIL_RSHIFT(inlink->h, vsub) : inlink->h; |
| 230 | } | ||
| 231 | |||
| 232 | 5 | select->var_values[VAR_N] = 0.0; | |
| 233 | 5 | select->var_values[VAR_SELECTED_N] = 0.0; | |
| 234 | |||
| 235 | 5 | select->var_values[VAR_TB] = av_q2d(inlink->time_base); | |
| 236 | |||
| 237 | 5 | select->var_values[VAR_PREV_SELECTED_N] = NAN; | |
| 238 | 5 | select->var_values[VAR_PREV_PTS] = NAN; | |
| 239 | 5 | select->var_values[VAR_PREV_SELECTED_PTS] = NAN; | |
| 240 | 5 | select->var_values[VAR_PREV_SELECTED_T] = NAN; | |
| 241 | 5 | select->var_values[VAR_PREV_T] = NAN; | |
| 242 | 5 | select->var_values[VAR_START_PTS] = NAN; | |
| 243 | 5 | select->var_values[VAR_START_T] = NAN; | |
| 244 | |||
| 245 | 5 | select->var_values[VAR_I] = AV_PICTURE_TYPE_I; | |
| 246 | 5 | select->var_values[VAR_P] = AV_PICTURE_TYPE_P; | |
| 247 | 5 | select->var_values[VAR_B] = AV_PICTURE_TYPE_B; | |
| 248 | 5 | select->var_values[VAR_SI] = AV_PICTURE_TYPE_SI; | |
| 249 | 5 | select->var_values[VAR_SP] = AV_PICTURE_TYPE_SP; | |
| 250 | 5 | select->var_values[VAR_BI] = AV_PICTURE_TYPE_BI; | |
| 251 | 5 | select->var_values[VAR_PICT_TYPE_I] = AV_PICTURE_TYPE_I; | |
| 252 | 5 | select->var_values[VAR_PICT_TYPE_P] = AV_PICTURE_TYPE_P; | |
| 253 | 5 | select->var_values[VAR_PICT_TYPE_B] = AV_PICTURE_TYPE_B; | |
| 254 | 5 | select->var_values[VAR_PICT_TYPE_SI] = AV_PICTURE_TYPE_SI; | |
| 255 | 5 | select->var_values[VAR_PICT_TYPE_SP] = AV_PICTURE_TYPE_SP; | |
| 256 | 5 | select->var_values[VAR_PICT_TYPE_BI] = AV_PICTURE_TYPE_BI; | |
| 257 | |||
| 258 | 5 | select->var_values[VAR_INTERLACE_TYPE_P] = INTERLACE_TYPE_P; | |
| 259 | 5 | select->var_values[VAR_INTERLACE_TYPE_T] = INTERLACE_TYPE_T; | |
| 260 | 5 | select->var_values[VAR_INTERLACE_TYPE_B] = INTERLACE_TYPE_B; | |
| 261 | |||
| 262 | 5 | select->var_values[VAR_PICT_TYPE] = NAN; | |
| 263 | 5 | select->var_values[VAR_INTERLACE_TYPE] = NAN; | |
| 264 | 5 | select->var_values[VAR_SCENE] = NAN; | |
| 265 | 5 | select->var_values[VAR_CONSUMED_SAMPLES_N] = NAN; | |
| 266 | 5 | select->var_values[VAR_SAMPLES_N] = NAN; | |
| 267 | |||
| 268 | 5 | select->var_values[VAR_IH] = NAN; | |
| 269 | 5 | select->var_values[VAR_IW] = NAN; | |
| 270 | |||
| 271 | 5 | select->var_values[VAR_SAMPLE_RATE] = | |
| 272 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 5 times.
|
5 | inlink->type == AVMEDIA_TYPE_AUDIO ? inlink->sample_rate : NAN; |
| 273 | |||
| 274 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 4 times.
|
5 | if (CONFIG_SELECT_FILTER && select->do_scene_detect) { |
| 275 | 1 | select->sad = ff_scene_sad_get_fn(select->bitdepth); | |
| 276 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (!select->sad) |
| 277 | ✗ | return AVERROR(EINVAL); | |
| 278 | } | ||
| 279 | 5 | return 0; | |
| 280 | } | ||
| 281 | |||
| 282 | 1308 | static double get_scene_score(AVFilterContext *ctx, AVFrame *frame) | |
| 283 | { | ||
| 284 | 1308 | double ret = 0; | |
| 285 | 1308 | SelectContext *select = ctx->priv; | |
| 286 | 1308 | AVFrame *prev_picref = select->prev_picref; | |
| 287 | |||
| 288 |
2/2✓ Branch 0 taken 1307 times.
✓ Branch 1 taken 1 times.
|
1308 | if (prev_picref && |
| 289 |
1/2✓ Branch 0 taken 1307 times.
✗ Branch 1 not taken.
|
1307 | frame->height == prev_picref->height && |
| 290 |
1/2✓ Branch 0 taken 1307 times.
✗ Branch 1 not taken.
|
1307 | frame->width == prev_picref->width) { |
| 291 | 1307 | uint64_t sad = 0; | |
| 292 | double mafd, diff; | ||
| 293 | 1307 | uint64_t count = 0; | |
| 294 | |||
| 295 |
2/2✓ Branch 0 taken 1307 times.
✓ Branch 1 taken 1307 times.
|
2614 | for (int plane = 0; plane < select->nb_planes; plane++) { |
| 296 | uint64_t plane_sad; | ||
| 297 | 1307 | select->sad(prev_picref->data[plane], prev_picref->linesize[plane], | |
| 298 | 1307 | frame->data[plane], frame->linesize[plane], | |
| 299 | select->width[plane], select->height[plane], &plane_sad); | ||
| 300 | 1307 | sad += plane_sad; | |
| 301 | 1307 | count += select->width[plane] * select->height[plane]; | |
| 302 | } | ||
| 303 | |||
| 304 | 1307 | mafd = (double)sad / count / (1ULL << (select->bitdepth - 8)); | |
| 305 | 1307 | diff = fabs(mafd - select->prev_mafd); | |
| 306 |
2/2✓ Branch 0 taken 1060 times.
✓ Branch 1 taken 247 times.
|
1307 | ret = av_clipf(FFMIN(mafd, diff) / 100., 0, 1); |
| 307 | 1307 | select->prev_mafd = mafd; | |
| 308 | 1307 | av_frame_free(&prev_picref); | |
| 309 | } | ||
| 310 | 1308 | select->prev_picref = av_frame_clone(frame); | |
| 311 | 1308 | return ret; | |
| 312 | } | ||
| 313 | |||
| 314 | 1958 | static double get_concatdec_select(AVFrame *frame, int64_t pts) | |
| 315 | { | ||
| 316 | 1958 | AVDictionary *metadata = frame->metadata; | |
| 317 | 1958 | AVDictionaryEntry *start_time_entry = av_dict_get(metadata, "lavf.concatdec.start_time", NULL, 0); | |
| 318 | 1958 | AVDictionaryEntry *duration_entry = av_dict_get(metadata, "lavf.concatdec.duration", NULL, 0); | |
| 319 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1958 times.
|
1958 | if (start_time_entry) { |
| 320 | ✗ | int64_t start_time = strtoll(start_time_entry->value, NULL, 10); | |
| 321 | ✗ | if (pts >= start_time) { | |
| 322 | ✗ | if (duration_entry) { | |
| 323 | ✗ | int64_t duration = strtoll(duration_entry->value, NULL, 10); | |
| 324 | ✗ | if (pts < start_time + duration) | |
| 325 | ✗ | return -1; | |
| 326 | else | ||
| 327 | ✗ | return 0; | |
| 328 | } | ||
| 329 | ✗ | return -1; | |
| 330 | } | ||
| 331 | ✗ | return 0; | |
| 332 | } | ||
| 333 | 1958 | return NAN; | |
| 334 | } | ||
| 335 | |||
| 336 | 1958 | static void select_frame(AVFilterContext *ctx, AVFrame *frame) | |
| 337 | { | ||
| 338 | 1958 | SelectContext *select = ctx->priv; | |
| 339 | 1958 | AVFilterLink *inlink = ctx->inputs[0]; | |
| 340 | 1958 | FilterLink *inl = ff_filter_link(inlink); | |
| 341 | const AVFrameSideData *sd; | ||
| 342 | double res; | ||
| 343 | |||
| 344 |
2/2✓ Branch 0 taken 5 times.
✓ Branch 1 taken 1953 times.
|
1958 | if (isnan(select->var_values[VAR_START_PTS])) |
| 345 |
1/2✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
|
5 | select->var_values[VAR_START_PTS] = TS2D(frame->pts); |
| 346 |
2/2✓ Branch 0 taken 5 times.
✓ Branch 1 taken 1953 times.
|
1958 | if (isnan(select->var_values[VAR_START_T])) |
| 347 |
1/2✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
|
5 | select->var_values[VAR_START_T] = TS2D(frame->pts) * av_q2d(inlink->time_base); |
| 348 | |||
| 349 | 1958 | select->var_values[VAR_N ] = inl->frame_count_out - 1; | |
| 350 |
1/2✓ Branch 0 taken 1958 times.
✗ Branch 1 not taken.
|
1958 | select->var_values[VAR_PTS] = TS2D(frame->pts); |
| 351 |
1/2✓ Branch 0 taken 1958 times.
✗ Branch 1 not taken.
|
1958 | select->var_values[VAR_T ] = TS2D(frame->pts) * av_q2d(inlink->time_base); |
| 352 |
2/2✓ Branch 0 taken 678 times.
✓ Branch 1 taken 1280 times.
|
1958 | select->var_values[VAR_KEY] = !!(frame->flags & AV_FRAME_FLAG_KEY); |
| 353 | 1958 | select->var_values[VAR_CONCATDEC_SELECT] = get_concatdec_select(frame, av_rescale_q(frame->pts, inlink->time_base, AV_TIME_BASE_Q)); | |
| 354 | |||
| 355 |
1/3✗ Branch 0 not taken.
✓ Branch 1 taken 1958 times.
✗ Branch 2 not taken.
|
1958 | switch (inlink->type) { |
| 356 | ✗ | case AVMEDIA_TYPE_AUDIO: | |
| 357 | ✗ | select->var_values[VAR_SAMPLES_N] = frame->nb_samples; | |
| 358 | ✗ | break; | |
| 359 | |||
| 360 | 1958 | case AVMEDIA_TYPE_VIDEO: | |
| 361 | 1958 | select->var_values[VAR_IH] = frame->height; | |
| 362 | 1958 | select->var_values[VAR_IW] = frame->width; | |
| 363 | |||
| 364 | 1958 | select->var_values[VAR_INTERLACE_TYPE] = | |
| 365 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1958 times.
|
1958 | !(frame->flags & AV_FRAME_FLAG_INTERLACED) ? INTERLACE_TYPE_P : |
| 366 | ✗ | (frame->flags & AV_FRAME_FLAG_TOP_FIELD_FIRST) ? INTERLACE_TYPE_T : INTERLACE_TYPE_B; | |
| 367 | 1958 | select->var_values[VAR_PICT_TYPE] = frame->pict_type; | |
| 368 |
2/2✓ Branch 0 taken 1308 times.
✓ Branch 1 taken 650 times.
|
1958 | if (select->do_scene_detect) { |
| 369 | char buf[32]; | ||
| 370 | 1308 | select->var_values[VAR_SCENE] = get_scene_score(ctx, frame); | |
| 371 | // TODO: document metadata | ||
| 372 | 1308 | snprintf(buf, sizeof(buf), "%f", select->var_values[VAR_SCENE]); | |
| 373 | 1308 | av_dict_set(&frame->metadata, "lavfi.scene_score", buf, 0); | |
| 374 | } | ||
| 375 | |||
| 376 | 1958 | sd = av_frame_side_data_get(frame->side_data, frame->nb_side_data, | |
| 377 | AV_FRAME_DATA_VIEW_ID); | ||
| 378 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1958 times.
|
1958 | select->var_values[VAR_VIEW] = sd ? *(int*)sd->data : NAN; |
| 379 | 1958 | break; | |
| 380 | } | ||
| 381 | |||
| 382 | 1958 | select->select = res = av_expr_eval(select->expr, select->var_values, NULL); | |
| 383 | 1958 | av_log(inlink->dst, AV_LOG_DEBUG, | |
| 384 | "n:%f pts:%f t:%f key:%d", | ||
| 385 | select->var_values[VAR_N], | ||
| 386 | select->var_values[VAR_PTS], | ||
| 387 | select->var_values[VAR_T], | ||
| 388 | 1958 | !!(frame->flags & AV_FRAME_FLAG_KEY)); | |
| 389 | |||
| 390 |
1/3✓ Branch 0 taken 1958 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
|
1958 | switch (inlink->type) { |
| 391 | 1958 | case AVMEDIA_TYPE_VIDEO: | |
| 392 | 1958 | av_log(inlink->dst, AV_LOG_DEBUG, " interlace_type:%c pict_type:%c scene:%f", | |
| 393 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1958 times.
|
1958 | !(frame->flags & AV_FRAME_FLAG_INTERLACED) ? 'P' : |
| 394 | ✗ | (frame->flags & AV_FRAME_FLAG_TOP_FIELD_FIRST) ? 'T' : 'B', | |
| 395 | 1958 | av_get_picture_type_char(frame->pict_type), | |
| 396 | select->var_values[VAR_SCENE]); | ||
| 397 | 1958 | break; | |
| 398 | ✗ | case AVMEDIA_TYPE_AUDIO: | |
| 399 | ✗ | av_log(inlink->dst, AV_LOG_DEBUG, " samples_n:%d consumed_samples_n:%f", | |
| 400 | frame->nb_samples, | ||
| 401 | select->var_values[VAR_CONSUMED_SAMPLES_N]); | ||
| 402 | ✗ | break; | |
| 403 | } | ||
| 404 | |||
| 405 |
2/2✓ Branch 0 taken 1355 times.
✓ Branch 1 taken 603 times.
|
1958 | if (res == 0) { |
| 406 | 1355 | select->select_out = -1; /* drop */ | |
| 407 |
2/4✓ Branch 0 taken 603 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 603 times.
|
603 | } else if (isnan(res) || res < 0) { |
| 408 | ✗ | select->select_out = 0; /* first output */ | |
| 409 | } else { | ||
| 410 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 603 times.
|
603 | select->select_out = FFMIN(ceilf(res)-1, select->nb_outputs-1); /* other outputs */ |
| 411 | } | ||
| 412 | |||
| 413 | 1958 | av_log(inlink->dst, AV_LOG_DEBUG, " -> select:%f select_out:%d\n", res, select->select_out); | |
| 414 | |||
| 415 |
2/2✓ Branch 0 taken 603 times.
✓ Branch 1 taken 1355 times.
|
1958 | if (res) { |
| 416 | 603 | select->var_values[VAR_PREV_SELECTED_N] = select->var_values[VAR_N]; | |
| 417 | 603 | select->var_values[VAR_PREV_SELECTED_PTS] = select->var_values[VAR_PTS]; | |
| 418 | 603 | select->var_values[VAR_PREV_SELECTED_T] = select->var_values[VAR_T]; | |
| 419 | 603 | select->var_values[VAR_SELECTED_N] += 1.0; | |
| 420 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 603 times.
|
603 | if (inlink->type == AVMEDIA_TYPE_AUDIO) |
| 421 | ✗ | select->var_values[VAR_CONSUMED_SAMPLES_N] += frame->nb_samples; | |
| 422 | } | ||
| 423 | |||
| 424 | 1958 | select->var_values[VAR_PREV_PTS] = select->var_values[VAR_PTS]; | |
| 425 | 1958 | select->var_values[VAR_PREV_T] = select->var_values[VAR_T]; | |
| 426 | 1958 | } | |
| 427 | |||
| 428 | 2626 | static int activate(AVFilterContext *ctx) | |
| 429 | { | ||
| 430 | 2626 | SelectContext *select = ctx->priv; | |
| 431 | 2626 | AVFilterLink *inlink = ctx->inputs[0]; | |
| 432 | AVFrame *in; | ||
| 433 | 2626 | int nb_eofs = 0; | |
| 434 | int ret; | ||
| 435 | |||
| 436 |
2/2✓ Branch 0 taken 4780 times.
✓ Branch 1 taken 2626 times.
|
7406 | for (int i = 0; i < ctx->nb_outputs; i++) |
| 437 | 4780 | nb_eofs += ff_outlink_get_status(ctx->outputs[i]) == AVERROR_EOF; | |
| 438 | |||
| 439 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2626 times.
|
2626 | if (nb_eofs == ctx->nb_outputs) { |
| 440 | ✗ | ff_inlink_set_status(inlink, AVERROR_EOF); | |
| 441 | ✗ | return 0; | |
| 442 | } | ||
| 443 | |||
| 444 |
2/2✓ Branch 1 taken 1958 times.
✓ Branch 2 taken 2023 times.
|
3981 | while ((ret = ff_inlink_consume_frame(inlink, &in))) { |
| 445 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1958 times.
|
1958 | if (ret < 0) |
| 446 | ✗ | return ret; | |
| 447 | 1958 | select_frame(ctx, in); | |
| 448 |
3/4✓ Branch 0 taken 603 times.
✓ Branch 1 taken 1355 times.
✓ Branch 3 taken 603 times.
✗ Branch 4 not taken.
|
1958 | if (select->select && !ff_outlink_get_status(ctx->outputs[select->select_out])) |
| 449 | 603 | return ff_filter_frame(ctx->outputs[select->select_out], in); | |
| 450 | 1355 | av_frame_free(&in); | |
| 451 | }; | ||
| 452 | |||
| 453 |
4/4✓ Branch 1 taken 5 times.
✓ Branch 2 taken 2018 times.
✓ Branch 4 taken 8 times.
✓ Branch 5 taken 5 times.
|
2031 | FF_FILTER_FORWARD_STATUS_ALL(inlink, ctx); |
| 454 |
4/4✓ Branch 1 taken 2017 times.
✓ Branch 2 taken 776 times.
✓ Branch 4 taken 2793 times.
✓ Branch 5 taken 1 times.
|
2794 | FF_FILTER_FORWARD_WANTED_ANY(ctx, inlink); |
| 455 | |||
| 456 | 1 | return FFERROR_NOT_READY; | |
| 457 | } | ||
| 458 | |||
| 459 | 8 | static av_cold void uninit(AVFilterContext *ctx) | |
| 460 | { | ||
| 461 | 8 | SelectContext *select = ctx->priv; | |
| 462 | |||
| 463 | 8 | av_expr_free(select->expr); | |
| 464 | 8 | select->expr = NULL; | |
| 465 | |||
| 466 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 7 times.
|
8 | if (select->do_scene_detect) { |
| 467 | 1 | av_frame_free(&select->prev_picref); | |
| 468 | } | ||
| 469 | 8 | } | |
| 470 | |||
| 471 | #if CONFIG_ASELECT_FILTER | ||
| 472 | |||
| 473 | DEFINE_OPTIONS(aselect, AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM); | ||
| 474 | AVFILTER_DEFINE_CLASS(aselect); | ||
| 475 | |||
| 476 | ✗ | static av_cold int aselect_init(AVFilterContext *ctx) | |
| 477 | { | ||
| 478 | ✗ | SelectContext *select = ctx->priv; | |
| 479 | int ret; | ||
| 480 | |||
| 481 | ✗ | if ((ret = init(ctx)) < 0) | |
| 482 | ✗ | return ret; | |
| 483 | |||
| 484 | ✗ | if (select->do_scene_detect) { | |
| 485 | ✗ | av_log(ctx, AV_LOG_ERROR, "Scene detection is ignored in aselect filter\n"); | |
| 486 | ✗ | return AVERROR(EINVAL); | |
| 487 | } | ||
| 488 | |||
| 489 | ✗ | return 0; | |
| 490 | } | ||
| 491 | |||
| 492 | static const AVFilterPad avfilter_af_aselect_inputs[] = { | ||
| 493 | { | ||
| 494 | .name = "default", | ||
| 495 | .type = AVMEDIA_TYPE_AUDIO, | ||
| 496 | .config_props = config_input, | ||
| 497 | }, | ||
| 498 | }; | ||
| 499 | |||
| 500 | const FFFilter ff_af_aselect = { | ||
| 501 | .p.name = "aselect", | ||
| 502 | .p.description = NULL_IF_CONFIG_SMALL("Select audio frames to pass in output."), | ||
| 503 | .p.priv_class = &aselect_class, | ||
| 504 | .p.flags = AVFILTER_FLAG_DYNAMIC_OUTPUTS, | ||
| 505 | .init = aselect_init, | ||
| 506 | .uninit = uninit, | ||
| 507 | .priv_size = sizeof(SelectContext), | ||
| 508 | FILTER_INPUTS(avfilter_af_aselect_inputs), | ||
| 509 | }; | ||
| 510 | #endif /* CONFIG_ASELECT_FILTER */ | ||
| 511 | |||
| 512 | #if CONFIG_SELECT_FILTER | ||
| 513 | |||
| 514 | 5 | static int query_formats(const AVFilterContext *ctx, | |
| 515 | AVFilterFormatsConfig **cfg_in, | ||
| 516 | AVFilterFormatsConfig **cfg_out) | ||
| 517 | { | ||
| 518 | 5 | const SelectContext *select = ctx->priv; | |
| 519 | |||
| 520 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 4 times.
|
5 | if (select->do_scene_detect) { |
| 521 | static const enum AVPixelFormat pix_fmts[] = { | ||
| 522 | AV_PIX_FMT_RGB24, AV_PIX_FMT_BGR24, AV_PIX_FMT_RGBA, | ||
| 523 | AV_PIX_FMT_ABGR, AV_PIX_FMT_BGRA, AV_PIX_FMT_GRAY8, | ||
| 524 | AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUVJ420P, | ||
| 525 | AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUVJ422P, | ||
| 526 | AV_PIX_FMT_YUV420P10, | ||
| 527 | AV_PIX_FMT_NONE | ||
| 528 | }; | ||
| 529 | 1 | return ff_set_common_formats_from_list2(ctx, cfg_in, cfg_out, pix_fmts); | |
| 530 | } | ||
| 531 | 4 | return 0; | |
| 532 | } | ||
| 533 | |||
| 534 | DEFINE_OPTIONS(select, AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM); | ||
| 535 | AVFILTER_DEFINE_CLASS(select); | ||
| 536 | |||
| 537 | 8 | static av_cold int select_init(AVFilterContext *ctx) | |
| 538 | { | ||
| 539 | int ret; | ||
| 540 | |||
| 541 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 8 times.
|
8 | if ((ret = init(ctx)) < 0) |
| 542 | ✗ | return ret; | |
| 543 | |||
| 544 | 8 | return 0; | |
| 545 | } | ||
| 546 | |||
| 547 | static const AVFilterPad avfilter_vf_select_inputs[] = { | ||
| 548 | { | ||
| 549 | .name = "default", | ||
| 550 | .type = AVMEDIA_TYPE_VIDEO, | ||
| 551 | .config_props = config_input, | ||
| 552 | }, | ||
| 553 | }; | ||
| 554 | |||
| 555 | const FFFilter ff_vf_select = { | ||
| 556 | .p.name = "select", | ||
| 557 | .p.description = NULL_IF_CONFIG_SMALL("Select video frames to pass in output."), | ||
| 558 | .p.priv_class = &select_class, | ||
| 559 | .p.flags = AVFILTER_FLAG_DYNAMIC_OUTPUTS | AVFILTER_FLAG_METADATA_ONLY, | ||
| 560 | .init = select_init, | ||
| 561 | .uninit = uninit, | ||
| 562 | .activate = activate, | ||
| 563 | .priv_size = sizeof(SelectContext), | ||
| 564 | FILTER_INPUTS(avfilter_vf_select_inputs), | ||
| 565 | FILTER_QUERY_FUNC2(query_formats), | ||
| 566 | }; | ||
| 567 | #endif /* CONFIG_SELECT_FILTER */ | ||
| 568 |