vf_blackdetect.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2012 Stefano Sabatini
3  *
4  * This file is part of FFmpeg.
5  *
6  * FFmpeg is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * FFmpeg is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with FFmpeg; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20 
21 /**
22  * @file
23  * Video black detector, loosely based on blackframe with extended
24  * syntax and features
25  */
26 
27 #include <float.h>
28 #include "libavutil/opt.h"
29 #include "libavutil/timestamp.h"
30 #include "avfilter.h"
31 #include "internal.h"
32 
33 typedef struct {
34  const AVClass *class;
35  double black_min_duration_time; ///< minimum duration of detected black, in seconds
36  int64_t black_min_duration; ///< minimum duration of detected black, expressed in timebase units
37  int64_t black_start; ///< pts start time of the first black picture
38  int64_t black_end; ///< pts end time of the last black picture
39  int64_t last_picref_pts; ///< pts of the last input picture
41 
44  unsigned int pixel_black_th_i;
45 
46  unsigned int frame_count; ///< frame number
47  unsigned int nb_black_pixels; ///< number of black pixels counted so far
49 
50 #define OFFSET(x) offsetof(BlackDetectContext, x)
51 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
52 
53 static const AVOption blackdetect_options[] = {
54  { "d", "set minimum detected black duration in seconds", OFFSET(black_min_duration_time), AV_OPT_TYPE_DOUBLE, {.dbl=2}, 0, DBL_MAX, FLAGS },
55  { "black_min_duration", "set minimum detected black duration in seconds", OFFSET(black_min_duration_time), AV_OPT_TYPE_DOUBLE, {.dbl=2}, 0, DBL_MAX, FLAGS },
56  { "picture_black_ratio_th", "set the picture black ratio threshold", OFFSET(picture_black_ratio_th), AV_OPT_TYPE_DOUBLE, {.dbl=.98}, 0, 1, FLAGS },
57  { "pic_th", "set the picture black ratio threshold", OFFSET(picture_black_ratio_th), AV_OPT_TYPE_DOUBLE, {.dbl=.98}, 0, 1, FLAGS },
58  { "pixel_black_th", "set the pixel black threshold", OFFSET(pixel_black_th), AV_OPT_TYPE_DOUBLE, {.dbl=.10}, 0, 1, FLAGS },
59  { "pix_th", "set the pixel black threshold", OFFSET(pixel_black_th), AV_OPT_TYPE_DOUBLE, {.dbl=.10}, 0, 1, FLAGS },
60  { NULL },
61 };
62 
63 AVFILTER_DEFINE_CLASS(blackdetect);
64 
65 #define YUVJ_FORMATS \
66  AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ440P
67 
68 static enum AVPixelFormat yuvj_formats[] = {
70 };
71 
73 {
74  static const enum AVPixelFormat pix_fmts[] = {
79  };
80 
82  return 0;
83 }
84 
85 static int config_input(AVFilterLink *inlink)
86 {
87  AVFilterContext *ctx = inlink->dst;
88  BlackDetectContext *blackdetect = ctx->priv;
89 
90  blackdetect->black_min_duration =
91  blackdetect->black_min_duration_time / av_q2d(inlink->time_base);
92 
93  blackdetect->pixel_black_th_i = ff_fmt_is_in(inlink->format, yuvj_formats) ?
94  // luminance_minimum_value + pixel_black_th * luminance_range_size
95  blackdetect->pixel_black_th * 255 :
96  16 + blackdetect->pixel_black_th * (235 - 16);
97 
98  av_log(blackdetect, AV_LOG_VERBOSE,
99  "black_min_duration:%s pixel_black_th:%f pixel_black_th_i:%d picture_black_ratio_th:%f\n",
100  av_ts2timestr(blackdetect->black_min_duration, &inlink->time_base),
101  blackdetect->pixel_black_th, blackdetect->pixel_black_th_i,
102  blackdetect->picture_black_ratio_th);
103  return 0;
104 }
105 
107 {
108  BlackDetectContext *blackdetect = ctx->priv;
109  AVFilterLink *inlink = ctx->inputs[0];
110 
111  if ((blackdetect->black_end - blackdetect->black_start) >= blackdetect->black_min_duration) {
112  av_log(blackdetect, AV_LOG_INFO,
113  "black_start:%s black_end:%s black_duration:%s\n",
114  av_ts2timestr(blackdetect->black_start, &inlink->time_base),
115  av_ts2timestr(blackdetect->black_end, &inlink->time_base),
116  av_ts2timestr(blackdetect->black_end - blackdetect->black_start, &inlink->time_base));
117  }
118 }
119 
120 static int request_frame(AVFilterLink *outlink)
121 {
122  AVFilterContext *ctx = outlink->src;
123  BlackDetectContext *blackdetect = ctx->priv;
124  AVFilterLink *inlink = ctx->inputs[0];
125  int ret = ff_request_frame(inlink);
126 
127  if (ret == AVERROR_EOF && blackdetect->black_started) {
128  // FIXME: black_end should be set to last_picref_pts + last_picref_duration
129  blackdetect->black_end = blackdetect->last_picref_pts;
130  check_black_end(ctx);
131  }
132  return ret;
133 }
134 
135 static int filter_frame(AVFilterLink *inlink, AVFrame *picref)
136 {
137  AVFilterContext *ctx = inlink->dst;
138  BlackDetectContext *blackdetect = ctx->priv;
139  double picture_black_ratio = 0;
140  const uint8_t *p = picref->data[0];
141  int x, i;
142 
143  for (i = 0; i < inlink->h; i++) {
144  for (x = 0; x < inlink->w; x++)
145  blackdetect->nb_black_pixels += p[x] <= blackdetect->pixel_black_th_i;
146  p += picref->linesize[0];
147  }
148 
149  picture_black_ratio = (double)blackdetect->nb_black_pixels / (inlink->w * inlink->h);
150 
151  av_log(ctx, AV_LOG_DEBUG,
152  "frame:%u picture_black_ratio:%f pts:%s t:%s type:%c\n",
153  blackdetect->frame_count, picture_black_ratio,
154  av_ts2str(picref->pts), av_ts2timestr(picref->pts, &inlink->time_base),
156 
157  if (picture_black_ratio >= blackdetect->picture_black_ratio_th) {
158  if (!blackdetect->black_started) {
159  /* black starts here */
160  blackdetect->black_started = 1;
161  blackdetect->black_start = picref->pts;
162  }
163  } else if (blackdetect->black_started) {
164  /* black ends here */
165  blackdetect->black_started = 0;
166  blackdetect->black_end = picref->pts;
167  check_black_end(ctx);
168  }
169 
170  blackdetect->last_picref_pts = picref->pts;
171  blackdetect->frame_count++;
172  blackdetect->nb_black_pixels = 0;
173  return ff_filter_frame(inlink->dst->outputs[0], picref);
174 }
175 
176 static const AVFilterPad blackdetect_inputs[] = {
177  {
178  .name = "default",
179  .type = AVMEDIA_TYPE_VIDEO,
180  .config_props = config_input,
181  .get_video_buffer = ff_null_get_video_buffer,
182  .filter_frame = filter_frame,
183  },
184  { NULL }
185 };
186 
188  {
189  .name = "default",
190  .type = AVMEDIA_TYPE_VIDEO,
191  .request_frame = request_frame,
192  },
193  { NULL }
194 };
195 
197  .name = "blackdetect",
198  .description = NULL_IF_CONFIG_SMALL("Detect video intervals that are (almost) black."),
199  .priv_size = sizeof(BlackDetectContext),
201  .inputs = blackdetect_inputs,
202  .outputs = blackdetect_outputs,
203  .priv_class = &blackdetect_class,
204 };
This structure describes decoded (raw) audio or video data.
Definition: frame.h:76
AVOption.
Definition: opt.h:251
planar YUV 4:4:4, 24bpp, (1 Cr & Cb sample per 1x1 Y samples)
Definition: pixfmt.h:73
static const AVFilterPad outputs[]
Definition: af_ashowinfo.c:117
external API header
AVFrame * ff_null_get_video_buffer(AVFilterLink *link, int w, int h)
Definition: video.c:35
AVFilterFormats * ff_make_format_list(const int *fmts)
Create a list of supported formats.
Definition: formats.c:308
unsigned int nb_black_pixels
number of black pixels counted so far
const char * name
Pad name.
AVFilterLink ** inputs
array of pointers to input links
Definition: avfilter.h:532
static const AVFilterPad blackdetect_inputs[]
uint8_t
it can be given away to ff_start_frame *A reference passed to ff_filter_frame(or the deprecated ff_start_frame) is given away and must no longer be used.*A reference created with avfilter_ref_buffer belongs to the code that created it.*A reference obtained with ff_get_video_buffer or ff_get_audio_buffer belongs to the code that requested it.*A reference given as return value by the get_video_buffer or get_audio_buffer method is given away and must no longer be used.Link reference fields---------------------The AVFilterLink structure has a few AVFilterBufferRef fields.The cur_buf and out_buf were used with the deprecated start_frame/draw_slice/end_frame API and should no longer be used.src_buf
AVOptions.
static int config_input(AVFilterLink *inlink)
timestamp utils, mostly useful for debugging/logging purposes
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
Definition: frame.h:159
static int query_formats(AVFilterContext *ctx)
static double av_q2d(AVRational a)
Convert rational to double.
Definition: rational.h:69
char av_get_picture_type_char(enum AVPictureType pict_type)
Return a single letter to describe the given picture type pict_type.
#define AVERROR_EOF
End of file.
Definition: error.h:55
void 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:545
int ff_fmt_is_in(int fmt, const int *fmts)
Tell is a format is contained in the provided list terminated by -1.
Definition: formats.c:254
int64_t black_min_duration
minimum duration of detected black, expressed in timebase units
double picture_black_ratio_th
A filter pad used for either input or output.
unsigned int frame_count
frame number
Discrete Time axis x
#define av_ts2timestr(ts, tb)
Convenience macro, the return value should be used only directly in function arguments but never stan...
Definition: timestamp.h:72
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
void * priv
private data for use by the filter
Definition: avfilter.h:545
planar YUV 4:2:0, 12bpp, 1 plane for Y and 1 plane for the UV components, which are interleaved (firs...
Definition: pixfmt.h:93
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:246
#define OFFSET(x)
planar YUV 4:2:2, 16bpp, (1 Cr & Cb sample per 2x1 Y samples)
Definition: pixfmt.h:72
static const AVFilterPad blackdetect_outputs[]
#define AV_LOG_VERBOSE
Definition: log.h:157
as above, but U and V bytes are swapped
Definition: pixfmt.h:94
enum AVPictureType pict_type
Picture type of the frame.
Definition: frame.h:144
double black_min_duration_time
minimum duration of detected black, in seconds
int64_t last_picref_pts
pts of the last input picture
ret
Definition: avfilter.c:821
#define FLAGS
unsigned int pixel_black_th_i
AVFILTER_DEFINE_CLASS(blackdetect)
static void check_black_end(AVFilterContext *ctx)
NULL
Definition: eval.c:55
static enum AVPixelFormat yuvj_formats[]
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:101
#define YUVJ_FORMATS
AVFilter avfilter_vf_blackdetect
planar YUV 4:1:0, 9bpp, (1 Cr & Cb sample per 4x4 Y samples)
Definition: pixfmt.h:74
int64_t black_end
pts end time of the last black picture
Describe the class of an AVClass context structure.
Definition: log.h:50
Filter definition.
Definition: avfilter.h:436
synthesis window for stochastic i
const char * name
filter name
Definition: avfilter.h:437
AVFilterLink ** outputs
array of pointers to output links
Definition: avfilter.h:539
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:87
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition: pixfmt.h:68
Y , 8bpp.
Definition: pixfmt.h:76
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:162
planar YUV 4:1:1, 12bpp, (1 Cr & Cb sample per 4x1 Y samples)
Definition: pixfmt.h:75
int64_t black_start
pts start time of the first black picture
#define av_ts2str(ts)
Convenience macro, the return value should be used only directly in function arguments but never stan...
Definition: timestamp.h:50
An instance of a filter.
Definition: avfilter.h:524
#define AV_LOG_INFO
Definition: log.h:156
static int request_frame(AVFilterLink *outlink)
int ff_request_frame(AVFilterLink *link)
Request an input frame from the filter at the other end of the link.
Definition: avfilter.c:319
static const AVOption blackdetect_options[]
internal API functions
AVPixelFormat
Pixel format.
Definition: pixfmt.h:66
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 inputs
static int filter_frame(AVFilterLink *inlink, AVFrame *picref)