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"
34 typedef struct BlackDetectContext {
36 double black_min_duration_time; ///< minimum duration of detected black, in seconds
37 int64_t black_min_duration; ///< minimum duration of detected black, expressed in timebase units
38 int64_t black_start; ///< pts start time of the first black picture
39 int64_t black_end; ///< pts end time of the last black picture
40 int64_t last_picref_pts; ///< pts of the last input picture
43 double picture_black_ratio_th;
44 double pixel_black_th;
45 unsigned int pixel_black_th_i;
47 unsigned int nb_black_pixels; ///< number of black pixels counted so far
51 unsigned int *counter;
54 #define OFFSET(x) offsetof(BlackDetectContext, x)
55 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
57 static const AVOption blackdetect_options[] = {
58 { "d", "set minimum detected black duration in seconds", OFFSET(black_min_duration_time), AV_OPT_TYPE_DOUBLE, {.dbl=2}, 0, DBL_MAX, FLAGS },
59 { "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 },
60 { "picture_black_ratio_th", "set the picture black ratio threshold", OFFSET(picture_black_ratio_th), AV_OPT_TYPE_DOUBLE, {.dbl=.98}, 0, 1, FLAGS },
61 { "pic_th", "set the picture black ratio threshold", OFFSET(picture_black_ratio_th), AV_OPT_TYPE_DOUBLE, {.dbl=.98}, 0, 1, FLAGS },
62 { "pixel_black_th", "set the pixel black threshold", OFFSET(pixel_black_th), AV_OPT_TYPE_DOUBLE, {.dbl=.10}, 0, 1, FLAGS },
63 { "pix_th", "set the pixel black threshold", OFFSET(pixel_black_th), AV_OPT_TYPE_DOUBLE, {.dbl=.10}, 0, 1, FLAGS },
67 AVFILTER_DEFINE_CLASS(blackdetect);
69 #define YUVJ_FORMATS \
70 AV_PIX_FMT_YUVJ411P, AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ440P
72 static const enum AVPixelFormat yuvj_formats[] = {
73 YUVJ_FORMATS, AV_PIX_FMT_NONE
76 static int query_formats(AVFilterContext *ctx)
78 static const enum AVPixelFormat pix_fmts[] = {
80 AV_PIX_FMT_YUV410P, AV_PIX_FMT_YUV411P,
81 AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV422P,
82 AV_PIX_FMT_YUV440P, AV_PIX_FMT_YUV444P,
83 AV_PIX_FMT_NV12, AV_PIX_FMT_NV21,
85 AV_PIX_FMT_GRAY10, AV_PIX_FMT_GRAY12, AV_PIX_FMT_GRAY14,
87 AV_PIX_FMT_YUV420P9, AV_PIX_FMT_YUV422P9, AV_PIX_FMT_YUV444P9,
88 AV_PIX_FMT_YUV420P10, AV_PIX_FMT_YUV422P10, AV_PIX_FMT_YUV444P10,
90 AV_PIX_FMT_YUV444P12, AV_PIX_FMT_YUV422P12, AV_PIX_FMT_YUV420P12,
92 AV_PIX_FMT_YUV444P14, AV_PIX_FMT_YUV422P14, AV_PIX_FMT_YUV420P14,
93 AV_PIX_FMT_YUV420P16, AV_PIX_FMT_YUV422P16, AV_PIX_FMT_YUV444P16,
94 AV_PIX_FMT_YUVA420P, AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUVA444P,
95 AV_PIX_FMT_YUVA444P9, AV_PIX_FMT_YUVA444P10, AV_PIX_FMT_YUVA444P12, AV_PIX_FMT_YUVA444P16,
96 AV_PIX_FMT_YUVA422P9, AV_PIX_FMT_YUVA422P10, AV_PIX_FMT_YUVA422P12, AV_PIX_FMT_YUVA422P16,
97 AV_PIX_FMT_YUVA420P9, AV_PIX_FMT_YUVA420P10, AV_PIX_FMT_YUVA420P16,
101 AVFilterFormats *fmts_list = ff_make_format_list(pix_fmts);
103 return AVERROR(ENOMEM);
104 return ff_set_common_formats(ctx, fmts_list);
107 static int config_input(AVFilterLink *inlink)
109 AVFilterContext *ctx = inlink->dst;
110 BlackDetectContext *s = ctx->priv;
111 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
112 const int depth = desc->comp[0].depth;
113 const int max = (1 << depth) - 1;
114 const int factor = (1 << (depth - 8));
117 s->nb_threads = ff_filter_get_nb_threads(ctx);
118 s->time_base = inlink->time_base;
119 s->black_min_duration = s->black_min_duration_time / av_q2d(s->time_base);
120 s->counter = av_calloc(s->nb_threads, sizeof(*s->counter));
122 return AVERROR(ENOMEM);
124 s->pixel_black_th_i = ff_fmt_is_in(inlink->format, yuvj_formats) ?
125 // luminance_minimum_value + pixel_black_th * luminance_range_size
126 s->pixel_black_th * max :
127 16 * factor + s->pixel_black_th * (235 - 16) * factor;
129 av_log(s, AV_LOG_VERBOSE,
130 "black_min_duration:%s pixel_black_th:%f pixel_black_th_i:%d picture_black_ratio_th:%f\n",
131 av_ts2timestr(s->black_min_duration, &s->time_base),
132 s->pixel_black_th, s->pixel_black_th_i,
133 s->picture_black_ratio_th);
137 static void check_black_end(AVFilterContext *ctx)
139 BlackDetectContext *s = ctx->priv;
141 if ((s->black_end - s->black_start) >= s->black_min_duration) {
142 av_log(s, AV_LOG_INFO,
143 "black_start:%s black_end:%s black_duration:%s\n",
144 av_ts2timestr(s->black_start, &s->time_base),
145 av_ts2timestr(s->black_end, &s->time_base),
146 av_ts2timestr(s->black_end - s->black_start, &s->time_base));
150 static int black_counter(AVFilterContext *ctx, void *arg,
151 int jobnr, int nb_jobs)
153 BlackDetectContext *s = ctx->priv;
154 const unsigned int threshold = s->pixel_black_th_i;
155 unsigned int *counterp = &s->counter[jobnr];
157 const int linesize = in->linesize[0];
158 const int w = in->width;
159 const int h = in->height;
160 const int start = (h * jobnr) / nb_jobs;
161 const int end = (h * (jobnr+1)) / nb_jobs;
162 const int size = end - start;
163 unsigned int counter = 0;
166 const uint8_t *p = in->data[0] + start * linesize;
168 for (int i = 0; i < size; i++) {
169 for (int x = 0; x < w; x++)
170 counter += p[x] <= threshold;
174 const uint16_t *p = (const uint16_t *)(in->data[0] + start * linesize);
176 for (int i = 0; i < size; i++) {
177 for (int x = 0; x < w; x++)
178 counter += p[x] <= threshold;
188 static int filter_frame(AVFilterLink *inlink, AVFrame *picref)
190 AVFilterContext *ctx = inlink->dst;
191 BlackDetectContext *s = ctx->priv;
192 double picture_black_ratio = 0;
194 ctx->internal->execute(ctx, black_counter, picref, NULL,
195 FFMIN(inlink->h, s->nb_threads));
197 for (int i = 0; i < s->nb_threads; i++)
198 s->nb_black_pixels += s->counter[i];
200 picture_black_ratio = (double)s->nb_black_pixels / (inlink->w * inlink->h);
202 av_log(ctx, AV_LOG_DEBUG,
203 "frame:%"PRId64" picture_black_ratio:%f pts:%s t:%s type:%c\n",
204 inlink->frame_count_out, picture_black_ratio,
205 av_ts2str(picref->pts), av_ts2timestr(picref->pts, &s->time_base),
206 av_get_picture_type_char(picref->pict_type));
208 if (picture_black_ratio >= s->picture_black_ratio_th) {
209 if (!s->black_started) {
210 /* black starts here */
211 s->black_started = 1;
212 s->black_start = picref->pts;
213 av_dict_set(&picref->metadata, "lavfi.black_start",
214 av_ts2timestr(s->black_start, &s->time_base), 0);
216 } else if (s->black_started) {
217 /* black ends here */
218 s->black_started = 0;
219 s->black_end = picref->pts;
220 check_black_end(ctx);
221 av_dict_set(&picref->metadata, "lavfi.black_end",
222 av_ts2timestr(s->black_end, &s->time_base), 0);
225 s->last_picref_pts = picref->pts;
226 s->nb_black_pixels = 0;
227 return ff_filter_frame(inlink->dst->outputs[0], picref);
230 static av_cold void uninit(AVFilterContext *ctx)
232 BlackDetectContext *s = ctx->priv;
234 av_freep(&s->counter);
236 if (s->black_started) {
237 // FIXME: black_end should be set to last_picref_pts + last_picref_duration
238 s->black_end = s->last_picref_pts;
239 check_black_end(ctx);
243 static const AVFilterPad blackdetect_inputs[] = {
246 .type = AVMEDIA_TYPE_VIDEO,
247 .config_props = config_input,
248 .filter_frame = filter_frame,
253 static const AVFilterPad blackdetect_outputs[] = {
256 .type = AVMEDIA_TYPE_VIDEO,
261 const AVFilter ff_vf_blackdetect = {
262 .name = "blackdetect",
263 .description = NULL_IF_CONFIG_SMALL("Detect video intervals that are (almost) black."),
264 .priv_size = sizeof(BlackDetectContext),
265 .query_formats = query_formats,
266 .inputs = blackdetect_inputs,
267 .outputs = blackdetect_outputs,
269 .priv_class = &blackdetect_class,
270 .flags = AVFILTER_FLAG_SLICE_THREADS,