FFmpeg
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 
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;
41  double prev_mafd;
42  double scene_score;
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 
61 
62 static const enum AVPixelFormat pix_fmts[] = {
73 };
74 
76 {
77  AVFilterContext *ctx = inlink->dst;
78  SCDetContext *s = ctx->priv;
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 
101 {
102  SCDetContext *s = ctx->priv;
103 
104  av_frame_free(&s->prev_picref);
105 }
106 
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 
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 
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 
153 
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 {
177  }
178  } else
179  return ff_filter_frame(outlink, frame);
180  }
181 
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 
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,
213  .activate = activate,
214 };
AVPixelFormat
AVPixelFormat
Pixel format.
Definition: pixfmt.h:64
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
opt.h
V
#define V
Definition: vf_scdet.c:49
set_meta
static int set_meta(SCDetContext *s, AVFrame *frame, const char *key, const char *value)
Definition: vf_scdet.c:139
ff_filter_frame
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:969
av_pix_fmt_desc_get
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:2888
FFERROR_NOT_READY
return FFERROR_NOT_READY
Definition: filter_design.txt:204
FILTER_PIXFMTS_ARRAY
#define FILTER_PIXFMTS_ARRAY(array)
Definition: internal.h:174
inlink
The exact code depends on how similar the blocks are and how related they are to the and needs to apply these operations to the correct inlink or outlink if there are several Macros are available to factor that when no extra processing is inlink
Definition: filter_design.txt:212
pix_fmts
static enum AVPixelFormat pix_fmts[]
Definition: vf_scdet.c:62
av_frame_free
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:99
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:330
pixdesc.h
AVFrame::width
int width
Definition: frame.h:402
AVOption
AVOption.
Definition: opt.h:251
AV_PIX_FMT_YUV420P10
#define AV_PIX_FMT_YUV420P10
Definition: pixfmt.h:459
F
#define F
Definition: vf_scdet.c:50
AV_PIX_FMT_BGR24
@ AV_PIX_FMT_BGR24
packed RGB 8:8:8, 24bpp, BGRBGR...
Definition: pixfmt.h:69
AV_PIX_FMT_BGRA
@ AV_PIX_FMT_BGRA
packed BGRA 8:8:8:8, 32bpp, BGRABGRA...
Definition: pixfmt.h:95
AV_PIX_FMT_YUV440P
@ AV_PIX_FMT_YUV440P
planar YUV 4:4:0 (1 Cr & Cb sample per 1x2 Y samples)
Definition: pixfmt.h:99
AVFilter::name
const char * name
Filter name.
Definition: avfilter.h:165
FF_FILTER_FORWARD_STATUS_BACK
#define FF_FILTER_FORWARD_STATUS_BACK(outlink, inlink)
Forward the status on an output link to an input link.
Definition: filters.h:199
AVFILTER_DEFINE_CLASS
AVFILTER_DEFINE_CLASS(scdet)
AVFrame::data
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:351
SCDetContext::prev_mafd
double prev_mafd
Definition: vf_scdet.c:41
config_input
static int config_input(AVFilterLink *inlink)
Definition: vf_scdet.c:75
ff_inlink_consume_frame
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:1364
av_pix_fmt_count_planes
int av_pix_fmt_count_planes(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:2928
SCDetContext::sad
ff_scene_sad_fn sad
Definition: vf_scdet.c:40
SCDetContext::width
ptrdiff_t width[4]
Definition: vf_scdet.c:36
AV_PIX_FMT_YUV422P9
#define AV_PIX_FMT_YUV422P9
Definition: pixfmt.h:457
SCDetContext::nb_planes
int nb_planes
Definition: vf_scdet.c:38
AVFilterPad
A filter pad used for either input or output.
Definition: internal.h:49
AV_PIX_FMT_YUV444P10
#define AV_PIX_FMT_YUV444P10
Definition: pixfmt.h:462
av_cold
#define av_cold
Definition: attributes.h:90
AV_PIX_FMT_YUVJ422P
@ 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:79
s
#define s(width, name)
Definition: cbs_vp9.c:256
AV_OPT_TYPE_DOUBLE
@ AV_OPT_TYPE_DOUBLE
Definition: opt.h:227
filters.h
AV_PIX_FMT_YUV420P9
#define AV_PIX_FMT_YUV420P9
Definition: pixfmt.h:456
ctx
AVFormatContext * ctx
Definition: movenc.c:48
ff_vf_scdet
const AVFilter ff_vf_scdet
Definition: vf_scdet.c:203
av_frame_clone
AVFrame * av_frame_clone(const AVFrame *src)
Create a new frame that references the same data as src.
Definition: frame.c:465
AV_PIX_FMT_YUV420P
@ AV_PIX_FMT_YUV420P
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition: pixfmt.h:66
key
const char * key
Definition: hwcontext_opencl.c:174
FILTER_INPUTS
#define FILTER_INPUTS(array)
Definition: internal.h:194
AV_PIX_FMT_YUVJ444P
@ 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:80
AV_PIX_FMT_RGBA
@ AV_PIX_FMT_RGBA
packed RGBA 8:8:8:8, 32bpp, RGBARGBA...
Definition: pixfmt.h:93
ff_scene_sad_get_fn
ff_scene_sad_fn ff_scene_sad_get_fn(int depth)
Definition: scene_sad.c:59
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:66
fabs
static __device__ float fabs(float a)
Definition: cuda_runtime.h:182
NULL
#define NULL
Definition: coverity.c:32
AV_PIX_FMT_YUVJ420P
@ 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:78
double
double
Definition: af_crystalizer.c:132
AV_PIX_FMT_YUV422P10
#define AV_PIX_FMT_YUV422P10
Definition: pixfmt.h:460
av_clipf
av_clipf
Definition: af_crystalizer.c:122
AV_PIX_FMT_GRAY8
@ AV_PIX_FMT_GRAY8
Y , 8bpp.
Definition: pixfmt.h:74
get_scene_score
static double get_scene_score(AVFilterContext *ctx, AVFrame *frame)
Definition: vf_scdet.c:107
AV_PIX_FMT_ABGR
@ AV_PIX_FMT_ABGR
packed ABGR 8:8:8:8, 32bpp, ABGRABGR...
Definition: pixfmt.h:94
scene_sad.h
uninit
static av_cold void uninit(AVFilterContext *ctx)
Definition: vf_scdet.c:100
AV_PIX_FMT_RGB24
@ AV_PIX_FMT_RGB24
packed RGB 8:8:8, 24bpp, RGBRGB...
Definition: pixfmt.h:68
av_ts2timestr
#define av_ts2timestr(ts, tb)
Convenience macro, the return value should be used only directly in function arguments but never stan...
Definition: timestamp.h:76
NULL_IF_CONFIG_SMALL
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:115
AV_PIX_FMT_FLAG_RGB
#define AV_PIX_FMT_FLAG_RGB
The pixel format contains RGB-like data (as opposed to YUV/grayscale).
Definition: pixdesc.h:136
AV_PIX_FMT_YUV422P12
#define AV_PIX_FMT_YUV422P12
Definition: pixfmt.h:464
AV_PIX_FMT_YUV444P12
#define AV_PIX_FMT_YUV444P12
Definition: pixfmt.h:466
ff_scene_sad_fn
void(* ff_scene_sad_fn)(SCENE_SAD_PARAMS)
Definition: scene_sad.h:34
SCDetContext::bitdepth
int bitdepth
Definition: vf_scdet.c:39
diff
static av_always_inline int diff(const struct color_info *a, const struct color_info *b, const int trans_thresh)
Definition: vf_paletteuse.c:162
FF_FILTER_FORWARD_WANTED
FF_FILTER_FORWARD_WANTED(outlink, inlink)
AV_LOG_INFO
#define AV_LOG_INFO
Standard information.
Definition: log.h:191
av_image_get_linesize
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
SCDetContext::threshold
double threshold
Definition: vf_scdet.c:44
value
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf default value
Definition: writing_filters.txt:86
FFMIN
#define FFMIN(a, b)
Definition: macros.h:49
AV_PIX_FMT_YUVJ440P
@ 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:100
AVFilterPad::name
const char * name
Pad name.
Definition: internal.h:55
AV_PIX_FMT_YUV444P9
#define AV_PIX_FMT_YUV444P9
Definition: pixfmt.h:458
scdet_outputs
static const AVFilterPad scdet_outputs[]
Definition: vf_scdet.c:196
AVFilter
Filter definition.
Definition: avfilter.h:161
ret
ret
Definition: filter_design.txt:187
OFFSET
#define OFFSET(x)
Definition: vf_scdet.c:48
frame
these buffered frames must be flushed immediately if a new input produces new the filter must not call request_frame to get more It must just process the frame or queue it The task of requesting more frames is left to the filter s request_frame method or the application If a filter has several the filter must be ready for frames arriving randomly on any input any filter with several inputs will most likely require some kind of queuing mechanism It is perfectly acceptable to have a limited queue and to drop frames when the inputs are too unbalanced request_frame For filters that do not use the this method is called when a frame is wanted on an output For a it should directly call filter_frame on the corresponding output For a if there are queued frames already one of these frames should be pushed If the filter should request a frame on one of its repeatedly until at least one frame has been pushed Return or at least make progress towards producing a frame
Definition: filter_design.txt:264
SCDetContext::scene_score
double scene_score
Definition: vf_scdet.c:42
activate
static int activate(AVFilterContext *ctx)
Definition: vf_scdet.c:144
AV_PIX_FMT_YUV420P12
#define AV_PIX_FMT_YUV420P12
Definition: pixfmt.h:463
scdet_options
static const AVOption scdet_options[]
Definition: vf_scdet.c:52
AVFrame::height
int height
Definition: frame.h:402
SCDetContext::prev_picref
AVFrame * prev_picref
Definition: vf_scdet.c:43
SCDetContext::height
ptrdiff_t height[4]
Definition: vf_scdet.c:37
AV_PIX_FMT_NONE
@ AV_PIX_FMT_NONE
Definition: pixfmt.h:65
avfilter.h
AVFILTER_FLAG_METADATA_ONLY
#define AVFILTER_FLAG_METADATA_ONLY
The filter is a "metadata" filter - it does not modify the frame data in any way.
Definition: avfilter.h:133
AV_PIX_FMT_FLAG_PLANAR
#define AV_PIX_FMT_FLAG_PLANAR
At least one pixel component is not in the first data plane.
Definition: pixdesc.h:132
AV_PIX_FMT_YUV444P
@ AV_PIX_FMT_YUV444P
planar YUV 4:4:4, 24bpp, (1 Cr & Cb sample per 1x1 Y samples)
Definition: pixfmt.h:71
AVFilterContext
An instance of a filter.
Definition: avfilter.h:392
desc
const char * desc
Definition: libsvtav1.c:83
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
SCDetContext::sc_pass
int sc_pass
Definition: vf_scdet.c:45
AV_PIX_FMT_YUV422P
@ AV_PIX_FMT_YUV422P
planar YUV 4:2:2, 16bpp, (1 Cr & Cb sample per 2x1 Y samples)
Definition: pixfmt.h:70
AVPixFmtDescriptor
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
Definition: pixdesc.h:69
FF_FILTER_FORWARD_STATUS
FF_FILTER_FORWARD_STATUS(inlink, outlink)
AV_OPT_TYPE_BOOL
@ AV_OPT_TYPE_BOOL
Definition: opt.h:244
FILTER_OUTPUTS
#define FILTER_OUTPUTS(array)
Definition: internal.h:195
av_dict_set
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
imgutils.h
timestamp.h
flags
#define flags(name, subs,...)
Definition: cbs_av1.c:561
AVFrame::linesize
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:375
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
scdet_inputs
static const AVFilterPad scdet_inputs[]
Definition: vf_scdet.c:188
snprintf
#define snprintf
Definition: snprintf.h:34
SCDetContext
Definition: vf_scdet.c:33