FFmpeg
Loading...
Searching...
No Matches
vf_scdet.c
Go to the documentation of this file.
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
34typedef struct SCDetContext {
35 const AVClass *class;
36
37 ptrdiff_t width[4];
38 ptrdiff_t height[4];
42 double prev_mafd;
45 double threshold;
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
53static 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
62
63static const enum AVPixelFormat pix_fmts[] = {
69
75
79
92
98
100
106
108};
109
110static int config_input(AVFilterLink *inlink)
111{
112 AVFilterContext *ctx = inlink->dst;
113 SCDetContext *s = ctx->priv;
115 int is_yuv = !(desc->flags & AV_PIX_FMT_FLAG_RGB) &&
116 (desc->flags & AV_PIX_FMT_FLAG_PLANAR) &&
117 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 s->bitdepth = desc->comp[0].depth + desc->comp[0].shift;
122 s->nb_planes = is_yuv ? 1 : av_pix_fmt_count_planes(inlink->format);
123
124 for (int plane = 0; plane < 4; plane++) {
125 ptrdiff_t line_size = av_image_get_linesize(inlink->format, inlink->w, plane);
126 s->width[plane] = line_size >> (s->bitdepth > 8);
127 s->height[plane] = inlink->h >> ((plane == 1 || plane == 2) ? desc->log2_chroma_h : 0);
128 }
129
130 s->sad = ff_scene_sad_get_fn(s->bitdepth);
131 if (!s->sad)
132 return AVERROR(EINVAL);
133
134 return 0;
135}
136
138{
139 SCDetContext *s = ctx->priv;
140
141 av_frame_free(&s->prev_picref);
142}
143
145{
146 double ret = 0;
147 SCDetContext *s = ctx->priv;
148 AVFrame *prev_picref = s->prev_picref;
149
150 if (prev_picref && frame->height == prev_picref->height
151 && frame->width == prev_picref->width) {
152 uint64_t sad = 0;
153 double mafd, diff;
154 uint64_t count = 0;
155
156 for (int plane = 0; plane < s->nb_planes; plane++) {
157 uint64_t plane_sad;
158 s->sad(prev_picref->data[plane], prev_picref->linesize[plane],
159 frame->data[plane], frame->linesize[plane],
160 s->width[plane], s->height[plane], &plane_sad);
161 sad += plane_sad;
162 count += s->width[plane] * s->height[plane];
163 }
164
165 mafd = (double)sad * 100. / count / (1ULL << s->bitdepth);
166 diff = fabs(mafd - s->prev_mafd);
167 ret = av_clipf(FFMIN(mafd, diff), 0, 100.);
168 s->prev_mafd = mafd;
169 av_frame_free(&prev_picref);
170 }
171 s->prev_picref = av_frame_clone(frame);
172 return ret;
173}
174
175static int set_meta(SCDetContext *s, AVFrame *frame, const char *key, const char *value)
176{
177 return av_dict_set(&frame->metadata, key, value, 0);
178}
179
181{
182 int ret;
183 AVFilterLink *inlink = ctx->inputs[0];
184 AVFilterLink *outlink = ctx->outputs[0];
185 SCDetContext *s = ctx->priv;
186 AVFrame *frame;
187
188 FF_FILTER_FORWARD_STATUS_BACK(outlink, inlink);
189
190 ret = ff_inlink_consume_frame(inlink, &frame);
191 if (ret < 0)
192 return ret;
193
194 if (frame) {
195 char buf[64];
196 s->scene_score = get_scene_score(ctx, frame);
197 snprintf(buf, sizeof(buf), "%0.3f", s->prev_mafd);
198 set_meta(s, frame, "lavfi.scd.mafd", buf);
199 snprintf(buf, sizeof(buf), "%0.3f", s->scene_score);
200 set_meta(s, frame, "lavfi.scd.score", buf);
201
202 if (s->scene_score >= s->threshold) {
203 av_log(ctx, AV_LOG_INFO, "lavfi.scd.score: %.3f, lavfi.scd.time: %s\n",
204 s->scene_score, av_ts2timestr(frame->pts, &inlink->time_base));
205 set_meta(s, frame, "lavfi.scd.time",
206 av_ts2timestr(frame->pts, &inlink->time_base));
207 }
208 if (s->sc_pass) {
209 if (s->scene_score >= s->threshold)
210 return ff_filter_frame(outlink, frame);
211 else {
213 }
214 } else
215 return ff_filter_frame(outlink, frame);
216 }
217
218 FF_FILTER_FORWARD_STATUS(inlink, outlink);
219 FF_FILTER_FORWARD_WANTED(outlink, inlink);
220
221 return FFERROR_NOT_READY;
222}
223
224static const AVFilterPad scdet_inputs[] = {
225 {
226 .name = "default",
227 .type = AVMEDIA_TYPE_VIDEO,
228 .config_props = config_input,
229 },
230};
231
233 .p.name = "scdet",
234 .p.description = NULL_IF_CONFIG_SMALL("Detect video scene change"),
235 .p.priv_class = &scdet_class,
237 .priv_size = sizeof(SCDetContext),
238 .uninit = uninit,
242 .activate = activate,
243};
static int config_input(AVFilterLink *inlink)
const FFFilter ff_vf_scdet
Definition vf_scdet.c:232
static AVFormatContext * ctx
#define V
Definition avdct.c:32
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition avfilter.c:1068
int ff_inlink_consume_frame(AVFilterLink *link, AVFrame **rframe)
Take a frame from the link's FIFO and update the link's stats.
Definition avfilter.c:1520
Main libavfilter public API header.
#define s(width, name)
Definition cbs_vp9.c:198
#define av_clipf
Definition common.h:145
#define NULL
Definition coverity.c:32
static __device__ float fabs(float a)
static AVFrame * frame
#define F(x)
double value
Definition eval.c:102
const char * key
@ AV_OPT_TYPE_DOUBLE
Underlying C type is double.
Definition opt.h:266
@ AV_OPT_TYPE_BOOL
Underlying C type is int.
Definition opt.h:326
#define AVFILTER_FLAG_METADATA_ONLY
The filter is a "metadata" filter - it does not modify the frame data in any way.
Definition avfilter.h:182
int av_dict_set(AVDictionary **pm, const char *key, const char *value, int flags)
Set the given entry in *pm, overwriting an existing entry.
Definition dict.c:86
#define AVERROR(e)
Definition error.h:45
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition frame.c:64
AVFrame * av_frame_clone(const AVFrame *src)
Create a new frame that references the same data as src.
Definition frame.c:483
#define AV_LOG_INFO
Standard information.
Definition log.h:221
@ AVMEDIA_TYPE_VIDEO
Definition avutil.h:200
int av_image_get_linesize(enum AVPixelFormat pix_fmt, int width, int plane)
Compute the size of an image line with format pix_fmt and width width for the plane plane.
Definition imgutils.c:76
misc image utilities
static av_cold void uninit(AVBitStreamFilterContext *ctx)
static int activate(AVBitStreamFilterContext *ctx)
#define FILTER_INPUTS(array)
Definition filters.h:264
#define FILTER_OUTPUTS(array)
Definition filters.h:265
#define FF_FILTER_FORWARD_WANTED(outlink, inlink)
Forward the frame_wanted_out flag from an output link to an input link.
Definition filters.h:694
#define FILTER_PIXFMTS_ARRAY(array)
Definition filters.h:244
#define FF_FILTER_FORWARD_STATUS(inlink, outlink)
Acknowledge the status on an input link and forward it to an output link.
Definition filters.h:666
#define FFERROR_NOT_READY
Filters implementation helper functions and internal structures.
Definition filters.h:34
#define FF_FILTER_FORWARD_STATUS_BACK(outlink, inlink)
Forward the status on an output link to an input link.
Definition filters.h:639
#define AVFILTER_DEFINE_CLASS(fname)
Definition filters.h:478
ff_scene_sad_fn ff_scene_sad_get_fn(int depth)
Definition scene_sad.c:59
#define av_cold
Definition attributes.h:117
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition internal.h:88
static enum AVPixelFormat pix_fmts[]
Definition libkvazaar.c:296
const char * desc
Definition libsvtav1.c:83
#define FFMIN(a, b)
Definition macros.h:49
AVOptions.
int av_pix_fmt_count_planes(enum AVPixelFormat pix_fmt)
Definition pixdesc.c:3500
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition pixdesc.c:3460
#define AV_PIX_FMT_FLAG_RGB
The pixel format contains RGB-like data (as opposed to YUV/grayscale).
Definition pixdesc.h:136
#define AV_PIX_FMT_FLAG_PLANAR
At least one pixel component is not in the first data plane.
Definition pixdesc.h:132
#define AV_PIX_FMT_GBRAP12
Definition pixfmt.h:569
#define AV_PIX_FMT_YUV420P16
Definition pixfmt.h:556
#define AV_PIX_FMT_P212
Definition pixfmt.h:624
#define AV_PIX_FMT_YUV444P12
Definition pixfmt.h:552
#define AV_PIX_FMT_YUV444P9
Definition pixfmt.h:544
#define AV_PIX_FMT_YUV420P10
Definition pixfmt.h:545
#define AV_PIX_FMT_YUV440P12
Definition pixfmt.h:551
#define AV_PIX_FMT_GRAY9
Definition pixfmt.h:524
#define AV_PIX_FMT_GBRAP16
Definition pixfmt.h:571
#define AV_PIX_FMT_GBRP9
Definition pixfmt.h:563
#define AV_PIX_FMT_YUV422P9
Definition pixfmt.h:543
#define AV_PIX_FMT_P412
Definition pixfmt.h:625
#define AV_PIX_FMT_P210
Definition pixfmt.h:622
#define AV_PIX_FMT_YUVA444P10
Definition pixfmt.h:598
#define AV_PIX_FMT_BGR48
Definition pixfmt.h:536
#define AV_PIX_FMT_P012
Definition pixfmt.h:609
#define AV_PIX_FMT_YUVA420P16
Definition pixfmt.h:601
#define AV_PIX_FMT_YA16
Definition pixfmt.h:530
#define AV_PIX_FMT_P216
Definition pixfmt.h:626
#define AV_PIX_FMT_YUV420P12
Definition pixfmt.h:549
#define AV_PIX_FMT_YUVA420P10
Definition pixfmt.h:596
#define AV_PIX_FMT_YUVA422P9
Definition pixfmt.h:594
#define AV_PIX_FMT_YUV422P12
Definition pixfmt.h:550
#define AV_PIX_FMT_GBRAP14
Definition pixfmt.h:570
#define AV_PIX_FMT_P010
Definition pixfmt.h:608
#define AV_PIX_FMT_P016
Definition pixfmt.h:610
#define AV_PIX_FMT_GBRP10
Definition pixfmt.h:564
#define AV_PIX_FMT_YUV422P10
Definition pixfmt.h:546
#define AV_PIX_FMT_YUV444P12MSB
Definition pixfmt.h:561
#define AV_PIX_FMT_GRAY12
Definition pixfmt.h:526
#define AV_PIX_FMT_NV20
Definition pixfmt.h:606
#define AV_PIX_FMT_RGBA64
Definition pixfmt.h:535
#define AV_PIX_FMT_GBRP12
Definition pixfmt.h:565
#define AV_PIX_FMT_YUV420P9
Definition pixfmt.h:542
#define AV_PIX_FMT_RGB48
Definition pixfmt.h:531
#define AV_PIX_FMT_YUVA420P9
Definition pixfmt.h:593
#define AV_PIX_FMT_YUVA422P10
Definition pixfmt.h:597
#define AV_PIX_FMT_P410
Definition pixfmt.h:623
#define AV_PIX_FMT_YUV420P14
Definition pixfmt.h:553
AVPixelFormat
Pixel format.
Definition pixfmt.h:71
@ AV_PIX_FMT_NV12
planar YUV 4:2:0, 12bpp, 1 plane for Y and 1 plane for the UV components, which are interleaved (firs...
Definition pixfmt.h:96
@ AV_PIX_FMT_NONE
Definition pixfmt.h:72
@ AV_PIX_FMT_RGB24
packed RGB 8:8:8, 24bpp, RGBRGB...
Definition pixfmt.h:75
@ AV_PIX_FMT_NV42
as above, but U and V bytes are swapped
Definition pixfmt.h:372
@ AV_PIX_FMT_YUV420P
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition pixfmt.h:73
@ AV_PIX_FMT_YUV440P
planar YUV 4:4:0 (1 Cr & Cb sample per 1x2 Y samples)
Definition pixfmt.h:106
@ AV_PIX_FMT_NV21
as above, but U and V bytes are swapped
Definition pixfmt.h:97
@ AV_PIX_FMT_YUV422P
planar YUV 4:2:2, 16bpp, (1 Cr & Cb sample per 2x1 Y samples)
Definition pixfmt.h:77
@ AV_PIX_FMT_ARGB
packed ARGB 8:8:8:8, 32bpp, ARGBARGB...
Definition pixfmt.h:99
@ AV_PIX_FMT_BGRA
packed BGRA 8:8:8:8, 32bpp, BGRABGRA...
Definition pixfmt.h:102
@ AV_PIX_FMT_GRAY8
Y , 8bpp.
Definition pixfmt.h:81
@ AV_PIX_FMT_ABGR
packed ABGR 8:8:8:8, 32bpp, ABGRABGR...
Definition pixfmt.h:101
@ AV_PIX_FMT_YUVA420P
planar YUV 4:2:0, 20bpp, (1 Cr & Cb sample per 2x2 Y & A samples)
Definition pixfmt.h:108
@ AV_PIX_FMT_YUVJ440P
planar YUV 4:4:0 full scale (JPEG), deprecated in favor of AV_PIX_FMT_YUV440P and setting color_range
Definition pixfmt.h:107
@ AV_PIX_FMT_YUV410P
planar YUV 4:1:0, 9bpp, (1 Cr & Cb sample per 4x4 Y samples)
Definition pixfmt.h:79
@ AV_PIX_FMT_NV24
planar YUV 4:4:4, 24bpp, 1 plane for Y and 1 plane for the UV components, which are interleaved (firs...
Definition pixfmt.h:371
@ AV_PIX_FMT_YUV411P
planar YUV 4:1:1, 12bpp, (1 Cr & Cb sample per 4x1 Y samples)
Definition pixfmt.h:80
@ AV_PIX_FMT_NV16
interleaved chroma YUV 4:2:2, 16bpp, (1 Cr & Cb sample per 2x1 Y samples)
Definition pixfmt.h:198
@ AV_PIX_FMT_RGBA
packed RGBA 8:8:8:8, 32bpp, RGBARGBA...
Definition pixfmt.h:100
@ AV_PIX_FMT_YUV444P
planar YUV 4:4:4, 24bpp, (1 Cr & Cb sample per 1x1 Y samples)
Definition pixfmt.h:78
@ AV_PIX_FMT_YUVA444P
planar YUV 4:4:4 32bpp, (1 Cr & Cb sample per 1x1 Y & A samples)
Definition pixfmt.h:174
@ AV_PIX_FMT_YUVJ411P
planar YUV 4:1:1, 12bpp, (1 Cr & Cb sample per 4x1 Y samples) full scale (JPEG), deprecated in favor ...
Definition pixfmt.h:283
@ AV_PIX_FMT_GBRAP
planar GBRA 4:4:4:4 32bpp
Definition pixfmt.h:212
@ AV_PIX_FMT_YUVJ422P
planar YUV 4:2:2, 16bpp, full scale (JPEG), deprecated in favor of AV_PIX_FMT_YUV422P and setting col...
Definition pixfmt.h:86
@ AV_PIX_FMT_YUVA422P
planar YUV 4:2:2 24bpp, (1 Cr & Cb sample per 2x1 Y & A samples)
Definition pixfmt.h:173
@ AV_PIX_FMT_BGR24
packed RGB 8:8:8, 24bpp, BGRBGR...
Definition pixfmt.h:76
@ AV_PIX_FMT_GBRP
planar GBR 4:4:4 24bpp
Definition pixfmt.h:165
@ AV_PIX_FMT_YA8
8 bits gray, 8 bits alpha
Definition pixfmt.h:140
@ AV_PIX_FMT_YUVJ444P
planar YUV 4:4:4, 24bpp, full scale (JPEG), deprecated in favor of AV_PIX_FMT_YUV444P and setting col...
Definition pixfmt.h:87
@ AV_PIX_FMT_YUVJ420P
planar YUV 4:2:0, 12bpp, full scale (JPEG), deprecated in favor of AV_PIX_FMT_YUV420P and setting col...
Definition pixfmt.h:85
#define AV_PIX_FMT_YUVA422P12
Definition pixfmt.h:599
#define AV_PIX_FMT_YUV422P14
Definition pixfmt.h:554
#define AV_PIX_FMT_P416
Definition pixfmt.h:627
#define AV_PIX_FMT_GRAY10
Definition pixfmt.h:525
#define AV_PIX_FMT_GBRP12MSB
Definition pixfmt.h:575
#define AV_PIX_FMT_GRAY14
Definition pixfmt.h:527
#define AV_PIX_FMT_YUV422P16
Definition pixfmt.h:557
#define AV_PIX_FMT_BGRA64
Definition pixfmt.h:540
#define AV_PIX_FMT_YUV440P10
Definition pixfmt.h:547
#define AV_PIX_FMT_GRAY16
Definition pixfmt.h:528
#define AV_PIX_FMT_GBRAP10
Definition pixfmt.h:568
#define AV_PIX_FMT_YUVA444P16
Definition pixfmt.h:603
#define AV_PIX_FMT_YUVA422P16
Definition pixfmt.h:602
#define AV_PIX_FMT_GBRP16
Definition pixfmt.h:567
#define AV_PIX_FMT_YUV444P14
Definition pixfmt.h:555
#define AV_PIX_FMT_YUVA444P9
Definition pixfmt.h:595
#define AV_PIX_FMT_GBRP14
Definition pixfmt.h:566
#define AV_PIX_FMT_YUVA444P12
Definition pixfmt.h:600
#define AV_PIX_FMT_GBRP10MSB
Definition pixfmt.h:574
#define AV_PIX_FMT_YUV444P16
Definition pixfmt.h:558
#define AV_PIX_FMT_YUV444P10MSB
Definition pixfmt.h:560
#define AV_PIX_FMT_YUV444P10
Definition pixfmt.h:548
Scene SAD functions.
void(* ff_scene_sad_fn)(SCENE_SAD_PARAMS)
Definition scene_sad.h:34
Describe the class of an AVClass context structure.
Definition log.h:76
An instance of a filter.
Definition avfilter.h:273
A filter pad used for either input or output.
Definition filters.h:40
This structure describes decoded (raw) audio or video data.
Definition frame.h:472
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition frame.h:493
int width
Definition frame.h:544
int height
Definition frame.h:544
int linesize[AV_NUM_DATA_POINTERS]
For video, a positive or negative value, which is typically indicating the size in bytes of each pict...
Definition frame.h:517
AVOption.
Definition opt.h:428
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
Definition pixdesc.h:69
double prev_mafd
Definition vf_scdet.c:42
ff_scene_sad_fn sad
Definition vf_scdet.c:41
double threshold
Definition vf_scdet.c:45
ptrdiff_t width[4]
Definition vf_scdet.c:37
ptrdiff_t height[4]
Definition vf_scdet.c:38
int nb_planes
Definition vf_scdet.c:39
double scene_score
Definition vf_scdet.c:43
AVFrame * prev_picref
Definition vf_scdet.c:44
#define av_log(a,...)
timestamp utils, mostly useful for debugging/logging purposes
#define av_ts2timestr(ts, tb)
Convenience macro, the return value should be used only directly in function arguments but never stan...
Definition timestamp.h:83
static av_always_inline int diff(const struct color_info *a, const struct color_info *b, const int trans_thresh)
static int config_input(AVFilterLink *inlink)
Definition vf_scdet.c:110
static double get_scene_score(AVFilterContext *ctx, AVFrame *frame)
Definition vf_scdet.c:144
static int set_meta(SCDetContext *s, AVFrame *frame, const char *key, const char *value)
Definition vf_scdet.c:175
static int activate(AVFilterContext *ctx)
Definition vf_scdet.c:180
static av_cold void uninit(AVFilterContext *ctx)
Definition vf_scdet.c:137
static const AVFilterPad scdet_inputs[]
Definition vf_scdet.c:224
#define OFFSET(x)
Definition vf_scdet.c:49
static const AVOption scdet_options[]
Definition vf_scdet.c:53
const AVFilterPad ff_video_default_filterpad[1]
An AVFilterPad array whose only entry has name "default" and is of type AVMEDIA_TYPE_VIDEO.
Definition video.c:37