FFmpeg  4.4.6
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/avassert.h"
25 #include "libavutil/imgutils.h"
26 #include "libavutil/opt.h"
27 #include "libavutil/pixdesc.h"
28 #include "libavutil/timestamp.h"
29 
30 #include "avfilter.h"
31 #include "filters.h"
32 #include "scene_sad.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;
42  double prev_mafd;
43  double scene_score;
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, {.dbl = 0 }, 0, 1, V|F },
57  { "s", "Set the flag to pass scene change frames", OFFSET(sc_pass), AV_OPT_TYPE_BOOL, {.dbl = 0 }, 0, 1, V|F },
58  {NULL}
59 };
60 
62 
64 {
65  static const enum AVPixelFormat pix_fmts[] = {
76  };
77 
79  if (!fmts_list)
80  return AVERROR(ENOMEM);
81  return ff_set_common_formats(ctx, fmts_list);
82 }
83 
84 static int config_input(AVFilterLink *inlink)
85 {
86  AVFilterContext *ctx = inlink->dst;
87  SCDetContext *s = ctx->priv;
89  int is_yuv = !(desc->flags & AV_PIX_FMT_FLAG_RGB) &&
90  (desc->flags & AV_PIX_FMT_FLAG_PLANAR) &&
91  desc->nb_components >= 3;
92 
93  s->bitdepth = desc->comp[0].depth;
94  s->nb_planes = is_yuv ? 1 : av_pix_fmt_count_planes(inlink->format);
95 
96  for (int plane = 0; plane < 4; plane++) {
97  ptrdiff_t line_size = av_image_get_linesize(inlink->format, inlink->w, plane);
98  s->width[plane] = line_size >> (s->bitdepth > 8);
99  s->height[plane] = inlink->h >> ((plane == 1 || plane == 2) ? desc->log2_chroma_h : 0);
100  }
101 
102  s->sad = ff_scene_sad_get_fn(s->bitdepth == 8 ? 8 : 16);
103  if (!s->sad)
104  return AVERROR(EINVAL);
105 
106  return 0;
107 }
108 
110 {
111  SCDetContext *s = ctx->priv;
112 
113  av_frame_free(&s->prev_picref);
114 }
115 
117 {
118  double ret = 0;
119  SCDetContext *s = ctx->priv;
120  AVFrame *prev_picref = s->prev_picref;
121 
122  if (prev_picref && frame->height == prev_picref->height
123  && frame->width == prev_picref->width) {
124  uint64_t sad = 0;
125  double mafd, diff;
126  uint64_t count = 0;
127 
128  for (int plane = 0; plane < s->nb_planes; plane++) {
129  uint64_t plane_sad;
130  s->sad(prev_picref->data[plane], prev_picref->linesize[plane],
131  frame->data[plane], frame->linesize[plane],
132  s->width[plane], s->height[plane], &plane_sad);
133  sad += plane_sad;
134  count += s->width[plane] * s->height[plane];
135  }
136 
137  emms_c();
138  mafd = (double)sad * 100. / count / (1ULL << s->bitdepth);
139  diff = fabs(mafd - s->prev_mafd);
140  ret = av_clipf(FFMIN(mafd, diff), 0, 100.);
141  s->prev_mafd = mafd;
142  av_frame_free(&prev_picref);
143  }
144  s->prev_picref = av_frame_clone(frame);
145  return ret;
146 }
147 
148 static int set_meta(SCDetContext *s, AVFrame *frame, const char *key, const char *value)
149 {
150  return av_dict_set(&frame->metadata, key, value, 0);
151 }
152 
154 {
155  int ret;
156  AVFilterLink *inlink = ctx->inputs[0];
157  AVFilterLink *outlink = ctx->outputs[0];
158  SCDetContext *s = ctx->priv;
159  AVFrame *frame;
160 
161  FF_FILTER_FORWARD_STATUS_BACK(outlink, inlink);
162 
163  ret = ff_inlink_consume_frame(inlink, &frame);
164  if (ret < 0)
165  return ret;
166 
167  if (frame) {
168  char buf[64];
169  s->scene_score = get_scene_score(ctx, frame);
170  snprintf(buf, sizeof(buf), "%0.3f", s->prev_mafd);
171  set_meta(s, frame, "lavfi.scd.mafd", buf);
172  snprintf(buf, sizeof(buf), "%0.3f", s->scene_score);
173  set_meta(s, frame, "lavfi.scd.score", buf);
174 
175  if (s->scene_score > s->threshold) {
176  av_log(s, AV_LOG_INFO, "lavfi.scd.score: %.3f, lavfi.scd.time: %s\n",
177  s->scene_score, av_ts2timestr(frame->pts, &inlink->time_base));
178  set_meta(s, frame, "lavfi.scd.time",
179  av_ts2timestr(frame->pts, &inlink->time_base));
180  }
181  if (s->sc_pass) {
182  if (s->scene_score > s->threshold)
183  return ff_filter_frame(outlink, frame);
184  else {
186  }
187  } else
188  return ff_filter_frame(outlink, frame);
189  }
190 
191  FF_FILTER_FORWARD_STATUS(inlink, outlink);
192  FF_FILTER_FORWARD_WANTED(outlink, inlink);
193 
194  return FFERROR_NOT_READY;
195 }
196 
197 static const AVFilterPad scdet_inputs[] = {
198  {
199  .name = "default",
200  .type = AVMEDIA_TYPE_VIDEO,
201  .config_props = config_input,
202  },
203  { NULL }
204 };
205 
206 static const AVFilterPad scdet_outputs[] = {
207  {
208  .name = "default",
209  .type = AVMEDIA_TYPE_VIDEO,
210  },
211  { NULL }
212 };
213 
215  .name = "scdet",
216  .description = NULL_IF_CONFIG_SMALL("Detect video scene change"),
217  .priv_size = sizeof(SCDetContext),
218  .priv_class = &scdet_class,
219  .uninit = uninit,
221  .inputs = scdet_inputs,
223  .activate = activate,
224 };
static const AVFilterPad inputs[]
Definition: af_acontrast.c:193
static const AVFilterPad outputs[]
Definition: af_acontrast.c:203
#define av_cold
Definition: attributes.h:88
simple assert() macros that are a bit more flexible than ISO C assert().
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:1096
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:1494
Main libavfilter public API header.
#define s(width, name)
Definition: cbs_vp9.c:257
#define FFMIN(a, b)
Definition: common.h:105
#define av_clipf
Definition: common.h:170
#define NULL
Definition: coverity.c:32
static __device__ float fabs(float a)
Definition: cuda_runtime.h:182
static AVFrame * frame
double value
Definition: eval.c:98
#define FF_FILTER_FORWARD_WANTED(outlink, inlink)
Forward the frame_wanted_out flag from an output link to an input link.
Definition: filters.h:254
#define FF_FILTER_FORWARD_STATUS(inlink, outlink)
Acknowledge the status on an input link and forward it to an output link.
Definition: filters.h:226
#define FFERROR_NOT_READY
Filters implementation helper functions.
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:199
int ff_set_common_formats(AVFilterContext *ctx, AVFilterFormats *formats)
A helper for query_formats() which sets all links to the same list of formats.
Definition: formats.c:587
AVFilterFormats * ff_make_format_list(const int *fmts)
Create a list of supported formats.
Definition: formats.c:286
@ AV_OPT_TYPE_DOUBLE
Definition: opt.h:227
@ AV_OPT_TYPE_BOOL
Definition: opt.h:242
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:70
#define AVERROR(e)
Definition: error.h:43
AVFrame * av_frame_clone(const AVFrame *src)
Create a new frame that references the same data as src.
Definition: frame.c:540
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:203
#define AV_LOG_INFO
Standard information.
Definition: log.h:205
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
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
const char * key
misc image utilities
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:117
#define emms_c()
Definition: internal.h:54
static enum AVPixelFormat pix_fmts[]
Definition: libkvazaar.c:309
const char * desc
Definition: libsvtav1.c:79
AVOptions.
int av_pix_fmt_count_planes(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:2613
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:2573
#define AV_PIX_FMT_FLAG_RGB
The pixel format contains RGB-like data (as opposed to YUV/grayscale).
Definition: pixdesc.h:148
#define AV_PIX_FMT_FLAG_PLANAR
At least one pixel component is not in the first data plane.
Definition: pixdesc.h:144
#define AV_PIX_FMT_YUV444P12
Definition: pixfmt.h:406
#define AV_PIX_FMT_YUV444P9
Definition: pixfmt.h:398
#define AV_PIX_FMT_YUV420P10
Definition: pixfmt.h:399
#define AV_PIX_FMT_YUV422P9
Definition: pixfmt.h:397
#define AV_PIX_FMT_YUV420P12
Definition: pixfmt.h:403
#define AV_PIX_FMT_YUV422P12
Definition: pixfmt.h:404
#define AV_PIX_FMT_YUV422P10
Definition: pixfmt.h:400
#define AV_PIX_FMT_YUV420P9
Definition: pixfmt.h:396
AVPixelFormat
Pixel format.
Definition: pixfmt.h:64
@ AV_PIX_FMT_NONE
Definition: pixfmt.h:65
@ AV_PIX_FMT_RGB24
packed RGB 8:8:8, 24bpp, RGBRGB...
Definition: pixfmt.h:68
@ AV_PIX_FMT_YUV420P
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition: pixfmt.h:66
@ AV_PIX_FMT_YUV440P
planar YUV 4:4:0 (1 Cr & Cb sample per 1x2 Y samples)
Definition: pixfmt.h:99
@ AV_PIX_FMT_YUV422P
planar YUV 4:2:2, 16bpp, (1 Cr & Cb sample per 2x1 Y samples)
Definition: pixfmt.h:70
@ AV_PIX_FMT_BGRA
packed BGRA 8:8:8:8, 32bpp, BGRABGRA...
Definition: pixfmt.h:95
@ AV_PIX_FMT_GRAY8
Y , 8bpp.
Definition: pixfmt.h:74
@ AV_PIX_FMT_ABGR
packed ABGR 8:8:8:8, 32bpp, ABGRABGR...
Definition: pixfmt.h:94
@ 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
@ AV_PIX_FMT_RGBA
packed RGBA 8:8:8:8, 32bpp, RGBARGBA...
Definition: pixfmt.h:93
@ AV_PIX_FMT_YUV444P
planar YUV 4:4:4, 24bpp, (1 Cr & Cb sample per 1x1 Y samples)
Definition: pixfmt.h:71
@ 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
@ AV_PIX_FMT_BGR24
packed RGB 8:8:8, 24bpp, BGRBGR...
Definition: pixfmt.h:69
@ 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_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
#define AV_PIX_FMT_YUV444P10
Definition: pixfmt.h:402
ff_scene_sad_fn ff_scene_sad_get_fn(int depth)
Definition: scene_sad.c:59
Scene SAD functions.
void(* ff_scene_sad_fn)(SCENE_SAD_PARAMS)
Definition: scene_sad.h:34
#define snprintf
Definition: snprintf.h:34
Describe the class of an AVClass context structure.
Definition: log.h:67
An instance of a filter.
Definition: avfilter.h:341
A list of supported formats for one end of a filter link.
Definition: formats.h:65
A filter pad used for either input or output.
Definition: internal.h:54
const char * name
Pad name.
Definition: internal.h:60
Filter definition.
Definition: avfilter.h:145
const char * name
Filter name.
Definition: avfilter.h:149
This structure describes decoded (raw) audio or video data.
Definition: frame.h:318
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
Definition: frame.h:411
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:332
int width
Definition: frame.h:376
int height
Definition: frame.h:376
AVDictionary * metadata
metadata.
Definition: frame.h:604
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:349
AVOption.
Definition: opt.h:248
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
Definition: pixdesc.h:81
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
int bitdepth
Definition: vf_scdet.c:40
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
int sc_pass
Definition: vf_scdet.c:46
#define av_log(a,...)
AVFormatContext * ctx
Definition: movenc.c:48
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:76
static av_always_inline int diff(const uint32_t a, const uint32_t b)
#define F
Definition: vf_scdet.c:51
static int query_formats(AVFilterContext *ctx)
Definition: vf_scdet.c:63
static int config_input(AVFilterLink *inlink)
Definition: vf_scdet.c:84
static double get_scene_score(AVFilterContext *ctx, AVFrame *frame)
Definition: vf_scdet.c:116
static int set_meta(SCDetContext *s, AVFrame *frame, const char *key, const char *value)
Definition: vf_scdet.c:148
static int activate(AVFilterContext *ctx)
Definition: vf_scdet.c:153
static av_cold void uninit(AVFilterContext *ctx)
Definition: vf_scdet.c:109
static const AVFilterPad scdet_inputs[]
Definition: vf_scdet.c:197
AVFILTER_DEFINE_CLASS(scdet)
#define OFFSET(x)
Definition: vf_scdet.c:49
static const AVFilterPad scdet_outputs[]
Definition: vf_scdet.c:206
static const AVOption scdet_options[]
Definition: vf_scdet.c:53
AVFilter ff_vf_scdet
Definition: vf_scdet.c:214
#define V
Definition: vf_scdet.c:50