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"
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
41 double picture_black_ratio_th;
42 double pixel_black_th;
43 unsigned int pixel_black_th_i;
45 unsigned int frame_count; ///< frame number
46 unsigned int nb_black_pixels; ///< number of black pixels counted so far
49 #define OFFSET(x) offsetof(BlackDetectContext, x)
50 static const AVOption blackdetect_options[] = {
51 { "d", "set minimum detected black duration in seconds", OFFSET(black_min_duration_time), AV_OPT_TYPE_DOUBLE, {.dbl=2}, 0, DBL_MAX},
52 { "black_min_duration", "set minimum detected black duration in seconds", OFFSET(black_min_duration_time), AV_OPT_TYPE_DOUBLE, {.dbl=2}, 0, DBL_MAX},
53 { "picture_black_ratio_th", "set the picture black ratio threshold", OFFSET(picture_black_ratio_th), AV_OPT_TYPE_DOUBLE, {.dbl=.98}, 0, 1},
54 { "pic_th", "set the picture black ratio threshold", OFFSET(picture_black_ratio_th), AV_OPT_TYPE_DOUBLE, {.dbl=.98}, 0, 1},
55 { "pixel_black_th", "set the pixel black threshold", OFFSET(pixel_black_th), AV_OPT_TYPE_DOUBLE, {.dbl=.10}, 0, 1},
56 { "pix_th", "set the pixel black threshold", OFFSET(pixel_black_th), AV_OPT_TYPE_DOUBLE, {.dbl=.10}, 0, 1},
60 static const AVClass blackdetect_class = {
61 .class_name = "blackdetect",
62 .item_name = av_default_item_name,
63 .option = blackdetect_options,
64 .version = LIBAVUTIL_VERSION_INT,
65 .category = AV_CLASS_CATEGORY_FILTER,
68 #define YUVJ_FORMATS \
69 PIX_FMT_YUVJ420P, PIX_FMT_YUVJ422P, PIX_FMT_YUVJ444P, PIX_FMT_YUVJ440P
71 static enum PixelFormat yuvj_formats[] = {
72 YUVJ_FORMATS, PIX_FMT_NONE
75 static int query_formats(AVFilterContext *ctx)
77 static const enum PixelFormat pix_fmts[] = {
78 PIX_FMT_YUV410P, PIX_FMT_YUV420P, PIX_FMT_GRAY8, PIX_FMT_NV12,
79 PIX_FMT_NV21, PIX_FMT_YUV444P, PIX_FMT_YUV422P, PIX_FMT_YUV411P,
84 ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
88 static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque)
91 BlackDetectContext *blackdetect = ctx->priv;
93 blackdetect->class = &blackdetect_class;
94 av_opt_set_defaults(blackdetect);
96 if ((ret = av_set_options_string(blackdetect, args, "=", ":")) < 0) {
97 av_log(ctx, AV_LOG_ERROR, "Error parsing options string: '%s'\n", args);
104 static int config_input(AVFilterLink *inlink)
106 AVFilterContext *ctx = inlink->dst;
107 BlackDetectContext *blackdetect = ctx->priv;
109 blackdetect->black_min_duration =
110 blackdetect->black_min_duration_time / av_q2d(inlink->time_base);
112 blackdetect->pixel_black_th_i = ff_fmt_is_in(inlink->format, yuvj_formats) ?
113 // luminance_minimum_value + pixel_black_th * luminance_range_size
114 blackdetect->pixel_black_th * 255 :
115 16 + blackdetect->pixel_black_th * (235 - 16);
117 av_log(blackdetect, AV_LOG_INFO,
118 "black_min_duration:%s pixel_black_th:%f pixel_black_th_i:%d picture_black_ratio_th:%f\n",
119 av_ts2timestr(blackdetect->black_min_duration, &inlink->time_base),
120 blackdetect->pixel_black_th, blackdetect->pixel_black_th_i,
121 blackdetect->picture_black_ratio_th);
125 static void draw_slice(AVFilterLink *inlink, int y, int h, int slice_dir)
127 AVFilterContext *ctx = inlink->dst;
128 BlackDetectContext *blackdetect = ctx->priv;
129 AVFilterBufferRef *picref = inlink->cur_buf;
131 const uint8_t *p = picref->data[0] + y * picref->linesize[0];
133 for (i = 0; i < h; i++) {
134 for (x = 0; x < inlink->w; x++)
135 blackdetect->nb_black_pixels += p[x] <= blackdetect->pixel_black_th_i;
136 p += picref->linesize[0];
139 ff_draw_slice(ctx->outputs[0], y, h, slice_dir);
142 static void end_frame(AVFilterLink *inlink)
144 AVFilterContext *ctx = inlink->dst;
145 BlackDetectContext *blackdetect = ctx->priv;
146 AVFilterBufferRef *picref = inlink->cur_buf;
147 double picture_black_ratio = 0;
149 picture_black_ratio = (double)blackdetect->nb_black_pixels / (inlink->w * inlink->h);
151 av_log(ctx, AV_LOG_DEBUG,
152 "frame:%u picture_black_ratio:%f pos:%"PRId64" pts:%s t:%s type:%c\n",
153 blackdetect->frame_count, picture_black_ratio,
154 picref->pos, av_ts2str(picref->pts),
155 av_ts2timestr(blackdetect->black_start, &inlink->time_base),
156 av_get_picture_type_char(picref->video->pict_type));
158 if (picture_black_ratio >= blackdetect->picture_black_ratio_th) {
159 if (!blackdetect->black_started) {
160 /* black starts here */
161 blackdetect->black_started = 1;
162 blackdetect->black_start = picref->pts;
164 } else if (blackdetect->black_started) {
165 /* black ends here */
166 blackdetect->black_started = 0;
167 blackdetect->black_end = picref->pts;
169 if ((blackdetect->black_end - blackdetect->black_start) >= blackdetect->black_min_duration) {
170 av_log(blackdetect, AV_LOG_INFO,
171 "black_start:%s black_end:%s black_duration:%s\n",
172 av_ts2timestr(blackdetect->black_start, &inlink->time_base),
173 av_ts2timestr(blackdetect->black_end, &inlink->time_base),
174 av_ts2timestr(blackdetect->black_end - blackdetect->black_start, &inlink->time_base));
178 blackdetect->frame_count++;
179 blackdetect->nb_black_pixels = 0;
180 avfilter_unref_buffer(picref);
181 ff_end_frame(inlink->dst->outputs[0]);
184 AVFilter avfilter_vf_blackdetect = {
185 .name = "blackdetect",
186 .description = NULL_IF_CONFIG_SMALL("Detect video intervals that are (almost) black."),
187 .priv_size = sizeof(BlackDetectContext),
189 .query_formats = query_formats,
191 .inputs = (const AVFilterPad[]) {
193 .type = AVMEDIA_TYPE_VIDEO,
194 .config_props = config_input,
195 .draw_slice = draw_slice,
196 .get_video_buffer = ff_null_get_video_buffer,
197 .start_frame = ff_null_start_frame_keep_ref,
198 .end_frame = end_frame, },
202 .outputs = (const AVFilterPad[]) {
204 .type = AVMEDIA_TYPE_VIDEO },