FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavfilter/vf_scdet.c
Date: 2022-12-09 07:38:14
Exec Total Coverage
Lines: 67 71 94.4%
Functions: 5 5 100.0%
Branches: 32 44 72.7%

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
33 typedef struct SCDetContext {
34 const AVClass *class;
35
36 ptrdiff_t width[4];
37 ptrdiff_t height[4];
38 int nb_planes;
39 int bitdepth;
40 ff_scene_sad_fn sad;
41 double prev_mafd;
42 double scene_score;
43 AVFrame *prev_picref;
44 double threshold;
45 int sc_pass;
46 } SCDetContext;
47
48 #define OFFSET(x) offsetof(SCDetContext, x)
49 #define V AV_OPT_FLAG_VIDEO_PARAM
50 #define F AV_OPT_FLAG_FILTERING_PARAM
51
52 static const AVOption scdet_options[] = {
53 { "threshold", "set scene change detect threshold", OFFSET(threshold), AV_OPT_TYPE_DOUBLE, {.dbl = 10.}, 0, 100., V|F },
54 { "t", "set scene change detect threshold", OFFSET(threshold), AV_OPT_TYPE_DOUBLE, {.dbl = 10.}, 0, 100., V|F },
55 { "sc_pass", "Set the flag to pass scene change frames", OFFSET(sc_pass), AV_OPT_TYPE_BOOL, {.dbl = 0 }, 0, 1, V|F },
56 { "s", "Set the flag to pass scene change frames", OFFSET(sc_pass), AV_OPT_TYPE_BOOL, {.dbl = 0 }, 0, 1, V|F },
57 {NULL}
58 };
59
60 AVFILTER_DEFINE_CLASS(scdet);
61
62 static const enum AVPixelFormat pix_fmts[] = {
63 AV_PIX_FMT_RGB24, AV_PIX_FMT_BGR24, AV_PIX_FMT_RGBA,
64 AV_PIX_FMT_ABGR, AV_PIX_FMT_BGRA, AV_PIX_FMT_GRAY8,
65 AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUVJ420P,
66 AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUVJ422P,
67 AV_PIX_FMT_YUV440P, AV_PIX_FMT_YUVJ440P,
68 AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUVJ444P,
69 AV_PIX_FMT_YUV420P9, AV_PIX_FMT_YUV420P10, AV_PIX_FMT_YUV420P12,
70 AV_PIX_FMT_YUV422P9, AV_PIX_FMT_YUV422P10, AV_PIX_FMT_YUV422P12,
71 AV_PIX_FMT_YUV444P9, AV_PIX_FMT_YUV444P10, AV_PIX_FMT_YUV444P12,
72 AV_PIX_FMT_NONE
73 };
74
75 1 static int config_input(AVFilterLink *inlink)
76 {
77 1 AVFilterContext *ctx = inlink->dst;
78 1 SCDetContext *s = ctx->priv;
79 1 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
80 3 int is_yuv = !(desc->flags & AV_PIX_FMT_FLAG_RGB) &&
81
2/4
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
2 (desc->flags & AV_PIX_FMT_FLAG_PLANAR) &&
82
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 desc->nb_components >= 3;
83
84 1 s->bitdepth = desc->comp[0].depth;
85
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 s->nb_planes = is_yuv ? 1 : av_pix_fmt_count_planes(inlink->format);
86
87
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 1 times.
5 for (int plane = 0; plane < 4; plane++) {
88 4 ptrdiff_t line_size = av_image_get_linesize(inlink->format, inlink->w, plane);
89 4 s->width[plane] = line_size >> (s->bitdepth > 8);
90
4/4
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 2 times.
4 s->height[plane] = inlink->h >> ((plane == 1 || plane == 2) ? desc->log2_chroma_h : 0);
91 }
92
93
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 s->sad = ff_scene_sad_get_fn(s->bitdepth == 8 ? 8 : 16);
94
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (!s->sad)
95 return AVERROR(EINVAL);
96
97 1 return 0;
98 }
99
100 1 static av_cold void uninit(AVFilterContext *ctx)
101 {
102 1 SCDetContext *s = ctx->priv;
103
104 1 av_frame_free(&s->prev_picref);
105 1 }
106
107 1308 static double get_scene_score(AVFilterContext *ctx, AVFrame *frame)
108 {
109 1308 double ret = 0;
110 1308 SCDetContext *s = ctx->priv;
111 1308 AVFrame *prev_picref = s->prev_picref;
112
113
3/4
✓ Branch 0 taken 1307 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 1307 times.
✗ Branch 3 not taken.
1308 if (prev_picref && frame->height == prev_picref->height
114
1/2
✓ Branch 0 taken 1307 times.
✗ Branch 1 not taken.
1307 && frame->width == prev_picref->width) {
115 1307 uint64_t sad = 0;
116 double mafd, diff;
117 1307 uint64_t count = 0;
118
119
2/2
✓ Branch 0 taken 1307 times.
✓ Branch 1 taken 1307 times.
2614 for (int plane = 0; plane < s->nb_planes; plane++) {
120 uint64_t plane_sad;
121 1307 s->sad(prev_picref->data[plane], prev_picref->linesize[plane],
122 1307 frame->data[plane], frame->linesize[plane],
123 s->width[plane], s->height[plane], &plane_sad);
124 1307 sad += plane_sad;
125 1307 count += s->width[plane] * s->height[plane];
126 }
127
128 1307 emms_c();
129 1307 mafd = (double)sad * 100. / count / (1ULL << s->bitdepth);
130 1307 diff = fabs(mafd - s->prev_mafd);
131
2/2
✓ Branch 0 taken 1060 times.
✓ Branch 1 taken 247 times.
1307 ret = av_clipf(FFMIN(mafd, diff), 0, 100.);
132 1307 s->prev_mafd = mafd;
133 1307 av_frame_free(&prev_picref);
134 }
135 1308 s->prev_picref = av_frame_clone(frame);
136 1308 return ret;
137 }
138
139 2627 static int set_meta(SCDetContext *s, AVFrame *frame, const char *key, const char *value)
140 {
141 2627 return av_dict_set(&frame->metadata, key, value, 0);
142 }
143
144 1321 static int activate(AVFilterContext *ctx)
145 {
146 int ret;
147 1321 AVFilterLink *inlink = ctx->inputs[0];
148 1321 AVFilterLink *outlink = ctx->outputs[0];
149 1321 SCDetContext *s = ctx->priv;
150 AVFrame *frame;
151
152
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1321 times.
1321 FF_FILTER_FORWARD_STATUS_BACK(outlink, inlink);
153
154 1321 ret = ff_inlink_consume_frame(inlink, &frame);
155
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1321 times.
1321 if (ret < 0)
156 return ret;
157
158
2/2
✓ Branch 0 taken 1308 times.
✓ Branch 1 taken 13 times.
1321 if (frame) {
159 char buf[64];
160 1308 s->scene_score = get_scene_score(ctx, frame);
161 1308 snprintf(buf, sizeof(buf), "%0.3f", s->prev_mafd);
162 1308 set_meta(s, frame, "lavfi.scd.mafd", buf);
163 1308 snprintf(buf, sizeof(buf), "%0.3f", s->scene_score);
164 1308 set_meta(s, frame, "lavfi.scd.score", buf);
165
166
2/2
✓ Branch 0 taken 11 times.
✓ Branch 1 taken 1297 times.
1308 if (s->scene_score > s->threshold) {
167 11 av_log(s, AV_LOG_INFO, "lavfi.scd.score: %.3f, lavfi.scd.time: %s\n",
168 11 s->scene_score, av_ts2timestr(frame->pts, &inlink->time_base));
169 11 set_meta(s, frame, "lavfi.scd.time",
170 11 av_ts2timestr(frame->pts, &inlink->time_base));
171 }
172
1/2
✓ Branch 0 taken 1308 times.
✗ Branch 1 not taken.
1308 if (s->sc_pass) {
173
2/2
✓ Branch 0 taken 11 times.
✓ Branch 1 taken 1297 times.
1308 if (s->scene_score > s->threshold)
174 11 return ff_filter_frame(outlink, frame);
175 else {
176 1297 av_frame_free(&frame);
177 }
178 } else
179 return ff_filter_frame(outlink, frame);
180 }
181
182
2/2
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 1309 times.
1310 FF_FILTER_FORWARD_STATUS(inlink, outlink);
183
1/2
✓ Branch 1 taken 1309 times.
✗ Branch 2 not taken.
1309 FF_FILTER_FORWARD_WANTED(outlink, inlink);
184
185 return FFERROR_NOT_READY;
186 }
187
188 static const AVFilterPad scdet_inputs[] = {
189 {
190 .name = "default",
191 .type = AVMEDIA_TYPE_VIDEO,
192 .config_props = config_input,
193 },
194 };
195
196 static const AVFilterPad scdet_outputs[] = {
197 {
198 .name = "default",
199 .type = AVMEDIA_TYPE_VIDEO,
200 },
201 };
202
203 const AVFilter ff_vf_scdet = {
204 .name = "scdet",
205 .description = NULL_IF_CONFIG_SMALL("Detect video scene change"),
206 .priv_size = sizeof(SCDetContext),
207 .priv_class = &scdet_class,
208 .uninit = uninit,
209 .flags = AVFILTER_FLAG_METADATA_ONLY,
210 FILTER_INPUTS(scdet_inputs),
211 FILTER_OUTPUTS(scdet_outputs),
212 FILTER_PIXFMTS_ARRAY(pix_fmts),
213 .activate = activate,
214 };
215