FFmpeg
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
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 av_cold int init(AVFilterContext *ctx, const char *args)
86 {
87  int ret;
88  BlackDetectContext *blackdetect = ctx->priv;
89 
90  blackdetect->class = &blackdetect_class;
91  av_opt_set_defaults(blackdetect);
92 
93  if ((ret = av_set_options_string(blackdetect, args, "=", ":")) < 0)
94  return ret;
95 
96  return 0;
97 }
98 
99 static int config_input(AVFilterLink *inlink)
100 {
101  AVFilterContext *ctx = inlink->dst;
102  BlackDetectContext *blackdetect = ctx->priv;
103 
104  blackdetect->black_min_duration =
105  blackdetect->black_min_duration_time / av_q2d(inlink->time_base);
106 
107  blackdetect->pixel_black_th_i = ff_fmt_is_in(inlink->format, yuvj_formats) ?
108  // luminance_minimum_value + pixel_black_th * luminance_range_size
109  blackdetect->pixel_black_th * 255 :
110  16 + blackdetect->pixel_black_th * (235 - 16);
111 
112  av_log(blackdetect, AV_LOG_VERBOSE,
113  "black_min_duration:%s pixel_black_th:%f pixel_black_th_i:%d picture_black_ratio_th:%f\n",
114  av_ts2timestr(blackdetect->black_min_duration, &inlink->time_base),
115  blackdetect->pixel_black_th, blackdetect->pixel_black_th_i,
116  blackdetect->picture_black_ratio_th);
117  return 0;
118 }
119 
121 {
122  BlackDetectContext *blackdetect = ctx->priv;
123  AVFilterLink *inlink = ctx->inputs[0];
124 
125  if ((blackdetect->black_end - blackdetect->black_start) >= blackdetect->black_min_duration) {
126  av_log(blackdetect, AV_LOG_INFO,
127  "black_start:%s black_end:%s black_duration:%s\n",
128  av_ts2timestr(blackdetect->black_start, &inlink->time_base),
129  av_ts2timestr(blackdetect->black_end, &inlink->time_base),
130  av_ts2timestr(blackdetect->black_end - blackdetect->black_start, &inlink->time_base));
131  }
132 }
133 
134 static int request_frame(AVFilterLink *outlink)
135 {
136  AVFilterContext *ctx = outlink->src;
137  BlackDetectContext *blackdetect = ctx->priv;
138  AVFilterLink *inlink = ctx->inputs[0];
139  int ret = ff_request_frame(inlink);
140 
141  if (ret == AVERROR_EOF && blackdetect->black_started) {
142  // FIXME: black_end should be set to last_picref_pts + last_picref_duration
143  blackdetect->black_end = blackdetect->last_picref_pts;
144  check_black_end(ctx);
145  }
146  return ret;
147 }
148 
149 static int filter_frame(AVFilterLink *inlink, AVFilterBufferRef *picref)
150 {
151  AVFilterContext *ctx = inlink->dst;
152  BlackDetectContext *blackdetect = ctx->priv;
153  double picture_black_ratio = 0;
154  const uint8_t *p = picref->data[0];
155  int x, i;
156 
157  for (i = 0; i < inlink->h; i++) {
158  for (x = 0; x < inlink->w; x++)
159  blackdetect->nb_black_pixels += p[x] <= blackdetect->pixel_black_th_i;
160  p += picref->linesize[0];
161  }
162 
163  picture_black_ratio = (double)blackdetect->nb_black_pixels / (inlink->w * inlink->h);
164 
165  av_log(ctx, AV_LOG_DEBUG,
166  "frame:%u picture_black_ratio:%f pos:%"PRId64" pts:%s t:%s type:%c\n",
167  blackdetect->frame_count, picture_black_ratio,
168  picref->pos, av_ts2str(picref->pts), av_ts2timestr(picref->pts, &inlink->time_base),
170 
171  if (picture_black_ratio >= blackdetect->picture_black_ratio_th) {
172  if (!blackdetect->black_started) {
173  /* black starts here */
174  blackdetect->black_started = 1;
175  blackdetect->black_start = picref->pts;
176  }
177  } else if (blackdetect->black_started) {
178  /* black ends here */
179  blackdetect->black_started = 0;
180  blackdetect->black_end = picref->pts;
181  check_black_end(ctx);
182  }
183 
184  blackdetect->last_picref_pts = picref->pts;
185  blackdetect->frame_count++;
186  blackdetect->nb_black_pixels = 0;
187  return ff_filter_frame(inlink->dst->outputs[0], picref);
188 }
189 
190 static const AVFilterPad blackdetect_inputs[] = {
191  {
192  .name = "default",
193  .type = AVMEDIA_TYPE_VIDEO,
194  .config_props = config_input,
195  .get_video_buffer = ff_null_get_video_buffer,
196  .filter_frame = filter_frame,
197  },
198  { NULL }
199 };
200 
202  {
203  .name = "default",
204  .type = AVMEDIA_TYPE_VIDEO,
205  .request_frame = request_frame,
206  },
207  { NULL }
208 };
209 
211  .name = "blackdetect",
212  .description = NULL_IF_CONFIG_SMALL("Detect video intervals that are (almost) black."),
213  .priv_size = sizeof(BlackDetectContext),
214  .init = init,
216  .inputs = blackdetect_inputs,
217  .outputs = blackdetect_outputs,
218  .priv_class = &blackdetect_class,
219 };