| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* | ||
| 2 | * This file is part of FFmpeg. | ||
| 3 | * | ||
| 4 | * FFmpeg is free software; you can redistribute it and/or | ||
| 5 | * modify it under the terms of the GNU Lesser General Public | ||
| 6 | * License as published by the Free Software Foundation; either | ||
| 7 | * version 2.1 of the License, or (at your option) any later version. | ||
| 8 | * | ||
| 9 | * FFmpeg is distributed in the hope that it will be useful, | ||
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
| 12 | * Lesser General Public License for more details. | ||
| 13 | * | ||
| 14 | * You should have received a copy of the GNU Lesser General Public | ||
| 15 | * License along with FFmpeg; if not, write to the Free Software | ||
| 16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | ||
| 17 | */ | ||
| 18 | |||
| 19 | /** | ||
| 20 | * @file | ||
| 21 | * video scene change detection filter | ||
| 22 | */ | ||
| 23 | |||
| 24 | #include "libavutil/imgutils.h" | ||
| 25 | #include "libavutil/opt.h" | ||
| 26 | #include "libavutil/pixdesc.h" | ||
| 27 | #include "libavutil/timestamp.h" | ||
| 28 | |||
| 29 | #include "avfilter.h" | ||
| 30 | #include "filters.h" | ||
| 31 | #include "scene_sad.h" | ||
| 32 | #include "video.h" | ||
| 33 | |||
| 34 | typedef struct SCDetContext { | ||
| 35 | const AVClass *class; | ||
| 36 | |||
| 37 | ptrdiff_t width[4]; | ||
| 38 | ptrdiff_t height[4]; | ||
| 39 | int nb_planes; | ||
| 40 | int bitdepth; | ||
| 41 | ff_scene_sad_fn sad; | ||
| 42 | double prev_mafd; | ||
| 43 | double scene_score; | ||
| 44 | AVFrame *prev_picref; | ||
| 45 | double threshold; | ||
| 46 | int sc_pass; | ||
| 47 | } SCDetContext; | ||
| 48 | |||
| 49 | #define OFFSET(x) offsetof(SCDetContext, x) | ||
| 50 | #define V AV_OPT_FLAG_VIDEO_PARAM | ||
| 51 | #define F AV_OPT_FLAG_FILTERING_PARAM | ||
| 52 | |||
| 53 | static const AVOption scdet_options[] = { | ||
| 54 | { "threshold", "set scene change detect threshold", OFFSET(threshold), AV_OPT_TYPE_DOUBLE, {.dbl = 10.}, 0, 100., V|F }, | ||
| 55 | { "t", "set scene change detect threshold", OFFSET(threshold), AV_OPT_TYPE_DOUBLE, {.dbl = 10.}, 0, 100., V|F }, | ||
| 56 | { "sc_pass", "Set the flag to pass scene change frames", OFFSET(sc_pass), AV_OPT_TYPE_BOOL, {.i64 = 0 }, 0, 1, V|F }, | ||
| 57 | { "s", "Set the flag to pass scene change frames", OFFSET(sc_pass), AV_OPT_TYPE_BOOL, {.i64 = 0 }, 0, 1, V|F }, | ||
| 58 | {NULL} | ||
| 59 | }; | ||
| 60 | |||
| 61 | AVFILTER_DEFINE_CLASS(scdet); | ||
| 62 | |||
| 63 | static const enum AVPixelFormat pix_fmts[] = { | ||
| 64 | AV_PIX_FMT_RGB24, AV_PIX_FMT_BGR24, | ||
| 65 | AV_PIX_FMT_ARGB, AV_PIX_FMT_RGBA, | ||
| 66 | AV_PIX_FMT_ABGR, AV_PIX_FMT_BGRA, | ||
| 67 | AV_PIX_FMT_RGB48, AV_PIX_FMT_BGR48, | ||
| 68 | AV_PIX_FMT_RGBA64, AV_PIX_FMT_BGRA64, | ||
| 69 | |||
| 70 | AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRP9, AV_PIX_FMT_GBRP10, | ||
| 71 | AV_PIX_FMT_GBRP12, AV_PIX_FMT_GBRP14, AV_PIX_FMT_GBRP16, | ||
| 72 | AV_PIX_FMT_GBRAP, AV_PIX_FMT_GBRAP10, AV_PIX_FMT_GBRAP12, | ||
| 73 | AV_PIX_FMT_GBRAP14, AV_PIX_FMT_GBRAP16, | ||
| 74 | AV_PIX_FMT_GBRP10MSB, AV_PIX_FMT_GBRP12MSB, | ||
| 75 | |||
| 76 | AV_PIX_FMT_GRAY8, AV_PIX_FMT_GRAY9, AV_PIX_FMT_GRAY10, | ||
| 77 | AV_PIX_FMT_GRAY12, AV_PIX_FMT_GRAY14, AV_PIX_FMT_GRAY16, | ||
| 78 | AV_PIX_FMT_YA8, AV_PIX_FMT_YA16, | ||
| 79 | |||
| 80 | AV_PIX_FMT_YUV410P, AV_PIX_FMT_YUV411P, AV_PIX_FMT_YUVJ411P, | ||
| 81 | AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUVJ420P, | ||
| 82 | AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUVJ422P, | ||
| 83 | AV_PIX_FMT_YUV440P, AV_PIX_FMT_YUVJ440P, | ||
| 84 | AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUVJ444P, | ||
| 85 | AV_PIX_FMT_YUV420P9, AV_PIX_FMT_YUV422P9, AV_PIX_FMT_YUV444P9, | ||
| 86 | AV_PIX_FMT_YUV420P10, AV_PIX_FMT_YUV422P10, | ||
| 87 | AV_PIX_FMT_YUV440P10, AV_PIX_FMT_YUV444P10, | ||
| 88 | AV_PIX_FMT_YUV420P12, AV_PIX_FMT_YUV422P12, | ||
| 89 | AV_PIX_FMT_YUV440P12, AV_PIX_FMT_YUV444P12, | ||
| 90 | AV_PIX_FMT_YUV420P14, AV_PIX_FMT_YUV422P14, AV_PIX_FMT_YUV444P14, | ||
| 91 | AV_PIX_FMT_YUV420P16, AV_PIX_FMT_YUV422P16, AV_PIX_FMT_YUV444P16, | ||
| 92 | |||
| 93 | AV_PIX_FMT_YUVA420P, AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUVA444P, | ||
| 94 | AV_PIX_FMT_YUVA420P9, AV_PIX_FMT_YUVA422P9, AV_PIX_FMT_YUVA444P9, | ||
| 95 | AV_PIX_FMT_YUVA420P10, AV_PIX_FMT_YUVA422P10, AV_PIX_FMT_YUVA444P10, | ||
| 96 | AV_PIX_FMT_YUVA422P12, AV_PIX_FMT_YUVA444P12, | ||
| 97 | AV_PIX_FMT_YUVA420P16, AV_PIX_FMT_YUVA422P16, AV_PIX_FMT_YUVA444P16, | ||
| 98 | |||
| 99 | AV_PIX_FMT_YUV444P10MSB, AV_PIX_FMT_YUV444P12MSB, | ||
| 100 | |||
| 101 | AV_PIX_FMT_NV12, AV_PIX_FMT_NV21, AV_PIX_FMT_NV16, | ||
| 102 | AV_PIX_FMT_NV24, AV_PIX_FMT_NV42, AV_PIX_FMT_NV20, | ||
| 103 | AV_PIX_FMT_P010, AV_PIX_FMT_P210, AV_PIX_FMT_P410, | ||
| 104 | AV_PIX_FMT_P012, AV_PIX_FMT_P212, AV_PIX_FMT_P412, | ||
| 105 | AV_PIX_FMT_P016, AV_PIX_FMT_P216, AV_PIX_FMT_P416, | ||
| 106 | |||
| 107 | AV_PIX_FMT_NONE | ||
| 108 | }; | ||
| 109 | |||
| 110 | 2 | static int config_input(AVFilterLink *inlink) | |
| 111 | { | ||
| 112 | 2 | AVFilterContext *ctx = inlink->dst; | |
| 113 | 2 | SCDetContext *s = ctx->priv; | |
| 114 | 2 | const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format); | |
| 115 | 6 | int is_yuv = !(desc->flags & AV_PIX_FMT_FLAG_RGB) && | |
| 116 |
2/4✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
|
4 | (desc->flags & AV_PIX_FMT_FLAG_PLANAR) && |
| 117 |
1/2✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
|
2 | desc->nb_components >= 3; |
| 118 | |||
| 119 | /* SAD scores are normalize by the bit-depth, so MSB-aligned formats are | ||
| 120 | * equivalent to their corresponding full depth representation */ | ||
| 121 | 2 | s->bitdepth = desc->comp[0].depth + desc->comp[0].shift; | |
| 122 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
|
2 | s->nb_planes = is_yuv ? 1 : av_pix_fmt_count_planes(inlink->format); |
| 123 | |||
| 124 |
2/2✓ Branch 0 taken 8 times.
✓ Branch 1 taken 2 times.
|
10 | for (int plane = 0; plane < 4; plane++) { |
| 125 | 8 | ptrdiff_t line_size = av_image_get_linesize(inlink->format, inlink->w, plane); | |
| 126 | 8 | s->width[plane] = line_size >> (s->bitdepth > 8); | |
| 127 |
4/4✓ Branch 0 taken 6 times.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 4 times.
|
8 | s->height[plane] = inlink->h >> ((plane == 1 || plane == 2) ? desc->log2_chroma_h : 0); |
| 128 | } | ||
| 129 | |||
| 130 | 2 | s->sad = ff_scene_sad_get_fn(s->bitdepth); | |
| 131 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
|
2 | if (!s->sad) |
| 132 | ✗ | return AVERROR(EINVAL); | |
| 133 | |||
| 134 | 2 | return 0; | |
| 135 | } | ||
| 136 | |||
| 137 | 3 | static av_cold void uninit(AVFilterContext *ctx) | |
| 138 | { | ||
| 139 | 3 | SCDetContext *s = ctx->priv; | |
| 140 | |||
| 141 | 3 | av_frame_free(&s->prev_picref); | |
| 142 | 3 | } | |
| 143 | |||
| 144 | 1700 | static double get_scene_score(AVFilterContext *ctx, AVFrame *frame) | |
| 145 | { | ||
| 146 | 1700 | double ret = 0; | |
| 147 | 1700 | SCDetContext *s = ctx->priv; | |
| 148 | 1700 | AVFrame *prev_picref = s->prev_picref; | |
| 149 | |||
| 150 |
3/4✓ Branch 0 taken 1698 times.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 1698 times.
✗ Branch 3 not taken.
|
1700 | if (prev_picref && frame->height == prev_picref->height |
| 151 |
1/2✓ Branch 0 taken 1698 times.
✗ Branch 1 not taken.
|
1698 | && frame->width == prev_picref->width) { |
| 152 | 1698 | uint64_t sad = 0; | |
| 153 | double mafd, diff; | ||
| 154 | 1698 | uint64_t count = 0; | |
| 155 | |||
| 156 |
2/2✓ Branch 0 taken 1698 times.
✓ Branch 1 taken 1698 times.
|
3396 | for (int plane = 0; plane < s->nb_planes; plane++) { |
| 157 | uint64_t plane_sad; | ||
| 158 | 1698 | s->sad(prev_picref->data[plane], prev_picref->linesize[plane], | |
| 159 | 1698 | frame->data[plane], frame->linesize[plane], | |
| 160 | s->width[plane], s->height[plane], &plane_sad); | ||
| 161 | 1698 | sad += plane_sad; | |
| 162 | 1698 | count += s->width[plane] * s->height[plane]; | |
| 163 | } | ||
| 164 | |||
| 165 | 1698 | mafd = (double)sad * 100. / count / (1ULL << s->bitdepth); | |
| 166 | 1698 | diff = fabs(mafd - s->prev_mafd); | |
| 167 |
2/2✓ Branch 0 taken 1441 times.
✓ Branch 1 taken 257 times.
|
1698 | ret = av_clipf(FFMIN(mafd, diff), 0, 100.); |
| 168 | 1698 | s->prev_mafd = mafd; | |
| 169 | 1698 | av_frame_free(&prev_picref); | |
| 170 | } | ||
| 171 | 1700 | s->prev_picref = av_frame_clone(frame); | |
| 172 | 1700 | return ret; | |
| 173 | } | ||
| 174 | |||
| 175 | 3416 | static int set_meta(SCDetContext *s, AVFrame *frame, const char *key, const char *value) | |
| 176 | { | ||
| 177 | 3416 | return av_dict_set(&frame->metadata, key, value, 0); | |
| 178 | } | ||
| 179 | |||
| 180 | 2107 | static int activate(AVFilterContext *ctx) | |
| 181 | { | ||
| 182 | int ret; | ||
| 183 | 2107 | AVFilterLink *inlink = ctx->inputs[0]; | |
| 184 | 2107 | AVFilterLink *outlink = ctx->outputs[0]; | |
| 185 | 2107 | SCDetContext *s = ctx->priv; | |
| 186 | AVFrame *frame; | ||
| 187 | |||
| 188 |
2/2✓ Branch 1 taken 1 times.
✓ Branch 2 taken 2106 times.
|
2107 | FF_FILTER_FORWARD_STATUS_BACK(outlink, inlink); |
| 189 | |||
| 190 | 2106 | ret = ff_inlink_consume_frame(inlink, &frame); | |
| 191 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2106 times.
|
2106 | if (ret < 0) |
| 192 | ✗ | return ret; | |
| 193 | |||
| 194 |
2/2✓ Branch 0 taken 1700 times.
✓ Branch 1 taken 406 times.
|
2106 | if (frame) { |
| 195 | char buf[64]; | ||
| 196 | 1700 | s->scene_score = get_scene_score(ctx, frame); | |
| 197 | 1700 | snprintf(buf, sizeof(buf), "%0.3f", s->prev_mafd); | |
| 198 | 1700 | set_meta(s, frame, "lavfi.scd.mafd", buf); | |
| 199 | 1700 | snprintf(buf, sizeof(buf), "%0.3f", s->scene_score); | |
| 200 | 1700 | set_meta(s, frame, "lavfi.scd.score", buf); | |
| 201 | |||
| 202 |
2/2✓ Branch 0 taken 16 times.
✓ Branch 1 taken 1684 times.
|
1700 | if (s->scene_score >= s->threshold) { |
| 203 | 16 | av_log(ctx, AV_LOG_INFO, "lavfi.scd.score: %.3f, lavfi.scd.time: %s\n", | |
| 204 | 16 | s->scene_score, av_ts2timestr(frame->pts, &inlink->time_base)); | |
| 205 | 16 | set_meta(s, frame, "lavfi.scd.time", | |
| 206 | 16 | av_ts2timestr(frame->pts, &inlink->time_base)); | |
| 207 | } | ||
| 208 |
2/2✓ Branch 0 taken 1308 times.
✓ Branch 1 taken 392 times.
|
1700 | if (s->sc_pass) { |
| 209 |
2/2✓ Branch 0 taken 11 times.
✓ Branch 1 taken 1297 times.
|
1308 | if (s->scene_score >= s->threshold) |
| 210 | 403 | return ff_filter_frame(outlink, frame); | |
| 211 | else { | ||
| 212 | 1297 | av_frame_free(&frame); | |
| 213 | } | ||
| 214 | } else | ||
| 215 | 392 | return ff_filter_frame(outlink, frame); | |
| 216 | } | ||
| 217 | |||
| 218 |
2/2✓ Branch 1 taken 2 times.
✓ Branch 2 taken 1701 times.
|
1703 | FF_FILTER_FORWARD_STATUS(inlink, outlink); |
| 219 |
1/2✓ Branch 1 taken 1701 times.
✗ Branch 2 not taken.
|
1701 | FF_FILTER_FORWARD_WANTED(outlink, inlink); |
| 220 | |||
| 221 | ✗ | return FFERROR_NOT_READY; | |
| 222 | } | ||
| 223 | |||
| 224 | static const AVFilterPad scdet_inputs[] = { | ||
| 225 | { | ||
| 226 | .name = "default", | ||
| 227 | .type = AVMEDIA_TYPE_VIDEO, | ||
| 228 | .config_props = config_input, | ||
| 229 | }, | ||
| 230 | }; | ||
| 231 | |||
| 232 | const FFFilter ff_vf_scdet = { | ||
| 233 | .p.name = "scdet", | ||
| 234 | .p.description = NULL_IF_CONFIG_SMALL("Detect video scene change"), | ||
| 235 | .p.priv_class = &scdet_class, | ||
| 236 | .p.flags = AVFILTER_FLAG_METADATA_ONLY, | ||
| 237 | .priv_size = sizeof(SCDetContext), | ||
| 238 | .uninit = uninit, | ||
| 239 | FILTER_INPUTS(scdet_inputs), | ||
| 240 | FILTER_OUTPUTS(ff_video_default_filterpad), | ||
| 241 | FILTER_PIXFMTS_ARRAY(pix_fmts), | ||
| 242 | .activate = activate, | ||
| 243 | }; | ||
| 244 |