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
config_input(AVFilterLink * inlink)75 static int config_input(AVFilterLink *inlink)
76 {
77 AVFilterContext *ctx = inlink->dst;
78 SCDetContext *s = ctx->priv;
79 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
80 int is_yuv = !(desc->flags & AV_PIX_FMT_FLAG_RGB) &&
81 (desc->flags & AV_PIX_FMT_FLAG_PLANAR) &&
82 desc->nb_components >= 3;
83
84 s->bitdepth = desc->comp[0].depth;
85 s->nb_planes = is_yuv ? 1 : av_pix_fmt_count_planes(inlink->format);
86
87 for (int plane = 0; plane < 4; plane++) {
88 ptrdiff_t line_size = av_image_get_linesize(inlink->format, inlink->w, plane);
89 s->width[plane] = line_size >> (s->bitdepth > 8);
90 s->height[plane] = inlink->h >> ((plane == 1 || plane == 2) ? desc->log2_chroma_h : 0);
91 }
92
93 s->sad = ff_scene_sad_get_fn(s->bitdepth == 8 ? 8 : 16);
94 if (!s->sad)
95 return AVERROR(EINVAL);
96
97 return 0;
98 }
99
uninit(AVFilterContext * ctx)100 static av_cold void uninit(AVFilterContext *ctx)
101 {
102 SCDetContext *s = ctx->priv;
103
104 av_frame_free(&s->prev_picref);
105 }
106
get_scene_score(AVFilterContext * ctx,AVFrame * frame)107 static double get_scene_score(AVFilterContext *ctx, AVFrame *frame)
108 {
109 double ret = 0;
110 SCDetContext *s = ctx->priv;
111 AVFrame *prev_picref = s->prev_picref;
112
113 if (prev_picref && frame->height == prev_picref->height
114 && frame->width == prev_picref->width) {
115 uint64_t sad = 0;
116 double mafd, diff;
117 uint64_t count = 0;
118
119 for (int plane = 0; plane < s->nb_planes; plane++) {
120 uint64_t plane_sad;
121 s->sad(prev_picref->data[plane], prev_picref->linesize[plane],
122 frame->data[plane], frame->linesize[plane],
123 s->width[plane], s->height[plane], &plane_sad);
124 sad += plane_sad;
125 count += s->width[plane] * s->height[plane];
126 }
127
128 emms_c();
129 mafd = (double)sad * 100. / count / (1ULL << s->bitdepth);
130 diff = fabs(mafd - s->prev_mafd);
131 ret = av_clipf(FFMIN(mafd, diff), 0, 100.);
132 s->prev_mafd = mafd;
133 av_frame_free(&prev_picref);
134 }
135 s->prev_picref = av_frame_clone(frame);
136 return ret;
137 }
138
set_meta(SCDetContext * s,AVFrame * frame,const char * key,const char * value)139 static int set_meta(SCDetContext *s, AVFrame *frame, const char *key, const char *value)
140 {
141 return av_dict_set(&frame->metadata, key, value, 0);
142 }
143
activate(AVFilterContext * ctx)144 static int activate(AVFilterContext *ctx)
145 {
146 int ret;
147 AVFilterLink *inlink = ctx->inputs[0];
148 AVFilterLink *outlink = ctx->outputs[0];
149 SCDetContext *s = ctx->priv;
150 AVFrame *frame;
151
152 FF_FILTER_FORWARD_STATUS_BACK(outlink, inlink);
153
154 ret = ff_inlink_consume_frame(inlink, &frame);
155 if (ret < 0)
156 return ret;
157
158 if (frame) {
159 char buf[64];
160 s->scene_score = get_scene_score(ctx, frame);
161 snprintf(buf, sizeof(buf), "%0.3f", s->prev_mafd);
162 set_meta(s, frame, "lavfi.scd.mafd", buf);
163 snprintf(buf, sizeof(buf), "%0.3f", s->scene_score);
164 set_meta(s, frame, "lavfi.scd.score", buf);
165
166 if (s->scene_score > s->threshold) {
167 av_log(s, AV_LOG_INFO, "lavfi.scd.score: %.3f, lavfi.scd.time: %s\n",
168 s->scene_score, av_ts2timestr(frame->pts, &inlink->time_base));
169 set_meta(s, frame, "lavfi.scd.time",
170 av_ts2timestr(frame->pts, &inlink->time_base));
171 }
172 if (s->sc_pass) {
173 if (s->scene_score > s->threshold)
174 return ff_filter_frame(outlink, frame);
175 else {
176 av_frame_free(&frame);
177 }
178 } else
179 return ff_filter_frame(outlink, frame);
180 }
181
182 FF_FILTER_FORWARD_STATUS(inlink, outlink);
183 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