2 * Copyright (c) 2012 Stefano Sabatini
4 * This file is part of FFmpeg.
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.
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.
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
23 * Video black detector, loosely based on blackframe with extended
28 #include "libavutil/opt.h"
29 #include "libavutil/timestamp.h"
33 typedef struct BlackDetectContext {
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
42 double picture_black_ratio_th;
43 double pixel_black_th;
44 unsigned int pixel_black_th_i;
46 unsigned int nb_black_pixels; ///< number of black pixels counted so far
50 #define OFFSET(x) offsetof(BlackDetectContext, x)
51 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
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 },
63 AVFILTER_DEFINE_CLASS(blackdetect);
65 #define YUVJ_FORMATS \
66 AV_PIX_FMT_YUVJ411P, AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ440P
68 static const enum AVPixelFormat yuvj_formats[] = {
69 YUVJ_FORMATS, AV_PIX_FMT_NONE
72 static int query_formats(AVFilterContext *ctx)
74 static const enum AVPixelFormat pix_fmts[] = {
76 AV_PIX_FMT_YUV410P, AV_PIX_FMT_YUV411P,
77 AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV422P,
78 AV_PIX_FMT_YUV440P, AV_PIX_FMT_YUV444P,
79 AV_PIX_FMT_NV12, AV_PIX_FMT_NV21,
84 AVFilterFormats *fmts_list = ff_make_format_list(pix_fmts);
86 return AVERROR(ENOMEM);
87 return ff_set_common_formats(ctx, fmts_list);
90 static int config_input(AVFilterLink *inlink)
92 AVFilterContext *ctx = inlink->dst;
93 BlackDetectContext *s = ctx->priv;
95 s->time_base = inlink->time_base;
96 s->black_min_duration = s->black_min_duration_time / av_q2d(s->time_base);
98 s->pixel_black_th_i = ff_fmt_is_in(inlink->format, yuvj_formats) ?
99 // luminance_minimum_value + pixel_black_th * luminance_range_size
100 s->pixel_black_th * 255 :
101 16 + s->pixel_black_th * (235 - 16);
103 av_log(s, AV_LOG_VERBOSE,
104 "black_min_duration:%s pixel_black_th:%f pixel_black_th_i:%d picture_black_ratio_th:%f\n",
105 av_ts2timestr(s->black_min_duration, &s->time_base),
106 s->pixel_black_th, s->pixel_black_th_i,
107 s->picture_black_ratio_th);
111 static void check_black_end(AVFilterContext *ctx)
113 BlackDetectContext *s = ctx->priv;
115 if ((s->black_end - s->black_start) >= s->black_min_duration) {
116 av_log(s, AV_LOG_INFO,
117 "black_start:%s black_end:%s black_duration:%s\n",
118 av_ts2timestr(s->black_start, &s->time_base),
119 av_ts2timestr(s->black_end, &s->time_base),
120 av_ts2timestr(s->black_end - s->black_start, &s->time_base));
124 static int filter_frame(AVFilterLink *inlink, AVFrame *picref)
126 AVFilterContext *ctx = inlink->dst;
127 BlackDetectContext *s = ctx->priv;
128 double picture_black_ratio = 0;
129 const uint8_t *p = picref->data[0];
132 for (i = 0; i < inlink->h; i++) {
133 for (x = 0; x < inlink->w; x++)
134 s->nb_black_pixels += p[x] <= s->pixel_black_th_i;
135 p += picref->linesize[0];
138 picture_black_ratio = (double)s->nb_black_pixels / (inlink->w * inlink->h);
140 av_log(ctx, AV_LOG_DEBUG,
141 "frame:%"PRId64" picture_black_ratio:%f pts:%s t:%s type:%c\n",
142 inlink->frame_count_out, picture_black_ratio,
143 av_ts2str(picref->pts), av_ts2timestr(picref->pts, &s->time_base),
144 av_get_picture_type_char(picref->pict_type));
146 if (picture_black_ratio >= s->picture_black_ratio_th) {
147 if (!s->black_started) {
148 /* black starts here */
149 s->black_started = 1;
150 s->black_start = picref->pts;
151 av_dict_set(&picref->metadata, "lavfi.black_start",
152 av_ts2timestr(s->black_start, &s->time_base), 0);
154 } else if (s->black_started) {
155 /* black ends here */
156 s->black_started = 0;
157 s->black_end = picref->pts;
158 check_black_end(ctx);
159 av_dict_set(&picref->metadata, "lavfi.black_end",
160 av_ts2timestr(s->black_end, &s->time_base), 0);
163 s->last_picref_pts = picref->pts;
164 s->nb_black_pixels = 0;
165 return ff_filter_frame(inlink->dst->outputs[0], picref);
168 static av_cold void uninit(AVFilterContext *ctx)
170 BlackDetectContext *s = ctx->priv;
172 if (s->black_started) {
173 // FIXME: black_end should be set to last_picref_pts + last_picref_duration
174 s->black_end = s->last_picref_pts;
175 check_black_end(ctx);
179 static const AVFilterPad blackdetect_inputs[] = {
182 .type = AVMEDIA_TYPE_VIDEO,
183 .config_props = config_input,
184 .filter_frame = filter_frame,
189 static const AVFilterPad blackdetect_outputs[] = {
192 .type = AVMEDIA_TYPE_VIDEO,
197 AVFilter ff_vf_blackdetect = {
198 .name = "blackdetect",
199 .description = NULL_IF_CONFIG_SMALL("Detect video intervals that are (almost) black."),
200 .priv_size = sizeof(BlackDetectContext),
201 .query_formats = query_formats,
202 .inputs = blackdetect_inputs,
203 .outputs = blackdetect_outputs,
205 .priv_class = &blackdetect_class,