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/pixdesc.h"
30 #include "libavutil/timestamp.h"
35 typedef struct BlackDetectContext {
37 double black_min_duration_time; ///< minimum duration of detected black, in seconds
38 int64_t black_min_duration; ///< minimum duration of detected black, expressed in timebase units
39 int64_t black_start; ///< pts start time of the first black picture
40 int64_t black_end; ///< pts end time of the last black picture
41 int64_t last_picref_pts; ///< pts of the last input picture
44 double picture_black_ratio_th;
45 double pixel_black_th;
46 unsigned int pixel_black_th_i;
48 unsigned int nb_black_pixels; ///< number of black pixels counted so far
52 unsigned int *counter;
55 #define OFFSET(x) offsetof(BlackDetectContext, x)
56 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
58 static const AVOption blackdetect_options[] = {
59 { "d", "set minimum detected black duration in seconds", OFFSET(black_min_duration_time), AV_OPT_TYPE_DOUBLE, {.dbl=2}, 0, DBL_MAX, FLAGS },
60 { "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 },
61 { "picture_black_ratio_th", "set the picture black ratio threshold", OFFSET(picture_black_ratio_th), AV_OPT_TYPE_DOUBLE, {.dbl=.98}, 0, 1, FLAGS },
62 { "pic_th", "set the picture black ratio threshold", OFFSET(picture_black_ratio_th), AV_OPT_TYPE_DOUBLE, {.dbl=.98}, 0, 1, FLAGS },
63 { "pixel_black_th", "set the pixel black threshold", OFFSET(pixel_black_th), AV_OPT_TYPE_DOUBLE, {.dbl=.10}, 0, 1, FLAGS },
64 { "pix_th", "set the pixel black threshold", OFFSET(pixel_black_th), AV_OPT_TYPE_DOUBLE, {.dbl=.10}, 0, 1, FLAGS },
68 AVFILTER_DEFINE_CLASS(blackdetect);
70 #define YUVJ_FORMATS \
71 AV_PIX_FMT_YUVJ411P, AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ440P
73 static const enum AVPixelFormat yuvj_formats[] = {
74 YUVJ_FORMATS, AV_PIX_FMT_NONE
77 static const enum AVPixelFormat pix_fmts[] = {
79 AV_PIX_FMT_YUV410P, AV_PIX_FMT_YUV411P,
80 AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV422P,
81 AV_PIX_FMT_YUV440P, AV_PIX_FMT_YUV444P,
82 AV_PIX_FMT_NV12, AV_PIX_FMT_NV21,
84 AV_PIX_FMT_GRAY10, AV_PIX_FMT_GRAY12, AV_PIX_FMT_GRAY14,
86 AV_PIX_FMT_YUV420P9, AV_PIX_FMT_YUV422P9, AV_PIX_FMT_YUV444P9,
87 AV_PIX_FMT_YUV420P10, AV_PIX_FMT_YUV422P10, AV_PIX_FMT_YUV444P10,
89 AV_PIX_FMT_YUV444P12, AV_PIX_FMT_YUV422P12, AV_PIX_FMT_YUV420P12,
91 AV_PIX_FMT_YUV444P14, AV_PIX_FMT_YUV422P14, AV_PIX_FMT_YUV420P14,
92 AV_PIX_FMT_YUV420P16, AV_PIX_FMT_YUV422P16, AV_PIX_FMT_YUV444P16,
93 AV_PIX_FMT_YUVA420P, AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUVA444P,
94 AV_PIX_FMT_YUVA444P9, AV_PIX_FMT_YUVA444P10, AV_PIX_FMT_YUVA444P12, AV_PIX_FMT_YUVA444P16,
95 AV_PIX_FMT_YUVA422P9, AV_PIX_FMT_YUVA422P10, AV_PIX_FMT_YUVA422P12, AV_PIX_FMT_YUVA422P16,
96 AV_PIX_FMT_YUVA420P9, AV_PIX_FMT_YUVA420P10, AV_PIX_FMT_YUVA420P16,
100 static int config_input(AVFilterLink *inlink)
102 AVFilterContext *ctx = inlink->dst;
103 BlackDetectContext *s = ctx->priv;
104 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
105 const int depth = desc->comp[0].depth;
108 s->nb_threads = ff_filter_get_nb_threads(ctx);
109 s->time_base = inlink->time_base;
110 s->black_min_duration = s->black_min_duration_time / av_q2d(s->time_base);
111 s->counter = av_calloc(s->nb_threads, sizeof(*s->counter));
113 return AVERROR(ENOMEM);
115 av_log(s, AV_LOG_VERBOSE,
116 "black_min_duration:%s pixel_black_th:%f picture_black_ratio_th:%f\n",
117 av_ts2timestr(s->black_min_duration, &s->time_base),
118 s->pixel_black_th, s->picture_black_ratio_th);
122 static void check_black_end(AVFilterContext *ctx)
124 BlackDetectContext *s = ctx->priv;
126 if ((s->black_end - s->black_start) >= s->black_min_duration) {
127 av_log(s, AV_LOG_INFO,
128 "black_start:%s black_end:%s black_duration:%s\n",
129 av_ts2timestr(s->black_start, &s->time_base),
130 av_ts2timestr(s->black_end, &s->time_base),
131 av_ts2timestr(s->black_end - s->black_start, &s->time_base));
135 static int black_counter(AVFilterContext *ctx, void *arg,
136 int jobnr, int nb_jobs)
138 BlackDetectContext *s = ctx->priv;
139 const unsigned int threshold = s->pixel_black_th_i;
140 unsigned int *counterp = &s->counter[jobnr];
142 const int linesize = in->linesize[0];
143 const int w = in->width;
144 const int h = in->height;
145 const int start = (h * jobnr) / nb_jobs;
146 const int end = (h * (jobnr+1)) / nb_jobs;
147 const int size = end - start;
148 unsigned int counter = 0;
151 const uint8_t *p = in->data[0] + start * linesize;
153 for (int i = 0; i < size; i++) {
154 for (int x = 0; x < w; x++)
155 counter += p[x] <= threshold;
159 const uint16_t *p = (const uint16_t *)(in->data[0] + start * linesize);
161 for (int i = 0; i < size; i++) {
162 for (int x = 0; x < w; x++)
163 counter += p[x] <= threshold;
173 static int filter_frame(AVFilterLink *inlink, AVFrame *picref)
175 AVFilterContext *ctx = inlink->dst;
176 BlackDetectContext *s = ctx->priv;
177 double picture_black_ratio = 0;
178 const int max = (1 << s->depth) - 1;
179 const int factor = (1 << (s->depth - 8));
180 const int full = picref->color_range == AVCOL_RANGE_JPEG ||
181 ff_fmt_is_in(picref->format, yuvj_formats);
183 s->pixel_black_th_i = full ? s->pixel_black_th * max :
184 // luminance_minimum_value + pixel_black_th * luminance_range_size
185 16 * factor + s->pixel_black_th * (235 - 16) * factor;
187 ff_filter_execute(ctx, black_counter, picref, NULL,
188 FFMIN(inlink->h, s->nb_threads));
190 for (int i = 0; i < s->nb_threads; i++)
191 s->nb_black_pixels += s->counter[i];
193 picture_black_ratio = (double)s->nb_black_pixels / (inlink->w * inlink->h);
195 av_log(ctx, AV_LOG_DEBUG,
196 "frame:%"PRId64" picture_black_ratio:%f pts:%s t:%s type:%c\n",
197 inlink->frame_count_out, picture_black_ratio,
198 av_ts2str(picref->pts), av_ts2timestr(picref->pts, &s->time_base),
199 av_get_picture_type_char(picref->pict_type));
201 if (picture_black_ratio >= s->picture_black_ratio_th) {
202 if (!s->black_started) {
203 /* black starts here */
204 s->black_started = 1;
205 s->black_start = picref->pts;
206 av_dict_set(&picref->metadata, "lavfi.black_start",
207 av_ts2timestr(s->black_start, &s->time_base), 0);
209 } else if (s->black_started) {
210 /* black ends here */
211 s->black_started = 0;
212 s->black_end = picref->pts;
213 check_black_end(ctx);
214 av_dict_set(&picref->metadata, "lavfi.black_end",
215 av_ts2timestr(s->black_end, &s->time_base), 0);
218 s->last_picref_pts = picref->pts;
219 s->nb_black_pixels = 0;
220 return ff_filter_frame(inlink->dst->outputs[0], picref);
223 static av_cold void uninit(AVFilterContext *ctx)
225 BlackDetectContext *s = ctx->priv;
227 av_freep(&s->counter);
229 if (s->black_started) {
230 // FIXME: black_end should be set to last_picref_pts + last_picref_duration
231 s->black_end = s->last_picref_pts;
232 check_black_end(ctx);
236 static const AVFilterPad blackdetect_inputs[] = {
239 .type = AVMEDIA_TYPE_VIDEO,
240 .config_props = config_input,
241 .filter_frame = filter_frame,
245 const AVFilter ff_vf_blackdetect = {
246 .name = "blackdetect",
247 .description = NULL_IF_CONFIG_SMALL("Detect video intervals that are (almost) black."),
248 .priv_size = sizeof(BlackDetectContext),
249 FILTER_INPUTS(blackdetect_inputs),
250 FILTER_OUTPUTS(ff_video_default_filterpad),
251 FILTER_PIXFMTS_ARRAY(pix_fmts),
253 .priv_class = &blackdetect_class,
254 .flags = AVFILTER_FLAG_SLICE_THREADS | AVFILTER_FLAG_METADATA_ONLY,