FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavfilter/vf_blackdetect.c
Date: 2025-07-28 20:30:09
Exec Total Coverage
Lines: 0 83 0.0%
Functions: 0 6 0.0%
Branches: 0 26 0.0%

Line Branch Exec Source
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/mem.h"
29 #include "libavutil/opt.h"
30 #include "libavutil/pixdesc.h"
31 #include "libavutil/timestamp.h"
32 #include "avfilter.h"
33 #include "filters.h"
34 #include "formats.h"
35 #include "video.h"
36 #include "vf_blackdetect.h"
37
38 typedef struct BlackDetectContext {
39 const AVClass *class;
40 double black_min_duration_time; ///< minimum duration of detected black, in seconds
41 int64_t black_min_duration; ///< minimum duration of detected black, expressed in timebase units
42 int64_t black_start; ///< pts start time of the first black picture
43 int64_t black_end; ///< pts end time of the last black picture
44 int64_t last_picref_pts; ///< pts of the last input picture
45 int black_started;
46
47 double picture_black_ratio_th;
48 double pixel_black_th;
49 unsigned int pixel_black_th_i;
50 int alpha;
51
52 unsigned int nb_black_pixels; ///< number of black pixels counted so far
53 AVRational time_base;
54 int depth;
55 int nb_threads;
56 unsigned int *counter;
57
58 ff_blackdetect_fn func;
59 } BlackDetectContext;
60
61 #define OFFSET(x) offsetof(BlackDetectContext, x)
62 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
63
64 static const AVOption blackdetect_options[] = {
65 { "d", "set minimum detected black duration in seconds", OFFSET(black_min_duration_time), AV_OPT_TYPE_DOUBLE, {.dbl=2}, 0, DBL_MAX, FLAGS },
66 { "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 },
67 { "picture_black_ratio_th", "set the picture black ratio threshold", OFFSET(picture_black_ratio_th), AV_OPT_TYPE_DOUBLE, {.dbl=.98}, 0, 1, FLAGS },
68 { "pic_th", "set the picture black ratio threshold", OFFSET(picture_black_ratio_th), AV_OPT_TYPE_DOUBLE, {.dbl=.98}, 0, 1, FLAGS },
69 { "pixel_black_th", "set the pixel black threshold", OFFSET(pixel_black_th), AV_OPT_TYPE_DOUBLE, {.dbl=.10}, 0, 1, FLAGS },
70 { "pix_th", "set the pixel black threshold", OFFSET(pixel_black_th), AV_OPT_TYPE_DOUBLE, {.dbl=.10}, 0, 1, FLAGS },
71 { "alpha", "check alpha instead of luma", OFFSET(alpha), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS },
72 { NULL }
73 };
74
75 AVFILTER_DEFINE_CLASS(blackdetect);
76
77 #define YUVJ_FORMATS \
78 AV_PIX_FMT_YUVJ411P, AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ440P
79
80 #define YUVA_FORMATS \
81 AV_PIX_FMT_YUVA420P, AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUVA444P, \
82 AV_PIX_FMT_YUVA444P9, AV_PIX_FMT_YUVA444P10, AV_PIX_FMT_YUVA444P12, AV_PIX_FMT_YUVA444P16, \
83 AV_PIX_FMT_YUVA422P9, AV_PIX_FMT_YUVA422P10, AV_PIX_FMT_YUVA422P12, AV_PIX_FMT_YUVA422P16, \
84 AV_PIX_FMT_YUVA420P9, AV_PIX_FMT_YUVA420P10, AV_PIX_FMT_YUVA420P16
85
86 static const enum AVPixelFormat yuvj_formats[] = {
87 YUVJ_FORMATS, AV_PIX_FMT_NONE
88 };
89
90 static const enum AVPixelFormat yuva_formats[] = {
91 YUVA_FORMATS, AV_PIX_FMT_NONE
92 };
93
94 static const enum AVPixelFormat yuv_formats[] = {
95 AV_PIX_FMT_GRAY8,
96 AV_PIX_FMT_YUV410P, AV_PIX_FMT_YUV411P,
97 AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV422P,
98 AV_PIX_FMT_YUV440P, AV_PIX_FMT_YUV444P,
99 AV_PIX_FMT_NV12, AV_PIX_FMT_NV21,
100 YUVJ_FORMATS,
101 AV_PIX_FMT_GRAY10, AV_PIX_FMT_GRAY12, AV_PIX_FMT_GRAY14,
102 AV_PIX_FMT_GRAY16,
103 AV_PIX_FMT_YUV420P9, AV_PIX_FMT_YUV422P9, AV_PIX_FMT_YUV444P9,
104 AV_PIX_FMT_YUV420P10, AV_PIX_FMT_YUV422P10, AV_PIX_FMT_YUV444P10,
105 AV_PIX_FMT_YUV440P10,
106 AV_PIX_FMT_YUV444P12, AV_PIX_FMT_YUV422P12, AV_PIX_FMT_YUV420P12,
107 AV_PIX_FMT_YUV440P12,
108 AV_PIX_FMT_YUV444P14, AV_PIX_FMT_YUV422P14, AV_PIX_FMT_YUV420P14,
109 AV_PIX_FMT_YUV420P16, AV_PIX_FMT_YUV422P16, AV_PIX_FMT_YUV444P16,
110 YUVA_FORMATS, AV_PIX_FMT_NONE
111 };
112
113 static int query_format(const AVFilterContext *ctx,
114 AVFilterFormatsConfig **cfg_in,
115 AVFilterFormatsConfig **cfg_out)
116 {
117 const BlackDetectContext *s = ctx->priv;
118 AVFilterFormats *formats;
119 if (s->alpha)
120 formats = ff_make_format_list(yuva_formats);
121 else
122 formats = ff_make_format_list(yuv_formats);
123
124 return ff_set_common_formats2(ctx, cfg_in, cfg_out, formats);
125 }
126
127 static int config_input(AVFilterLink *inlink)
128 {
129 AVFilterContext *ctx = inlink->dst;
130 BlackDetectContext *s = ctx->priv;
131 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
132 const int depth = desc->comp[0].depth;
133
134 s->depth = depth;
135 s->nb_threads = ff_filter_get_nb_threads(ctx);
136 s->time_base = inlink->time_base;
137 s->black_min_duration = s->black_min_duration_time / av_q2d(s->time_base);
138 s->counter = av_calloc(s->nb_threads, sizeof(*s->counter));
139 s->func = ff_blackdetect_get_fn(depth);
140 if (!s->counter)
141 return AVERROR(ENOMEM);
142
143 av_log(s, AV_LOG_VERBOSE,
144 "black_min_duration:%s pixel_black_th:%f picture_black_ratio_th:%f alpha:%d\n",
145 av_ts2timestr(s->black_min_duration, &s->time_base),
146 s->pixel_black_th, s->picture_black_ratio_th, s->alpha);
147 return 0;
148 }
149
150 static void check_black_end(AVFilterContext *ctx)
151 {
152 BlackDetectContext *s = ctx->priv;
153
154 if ((s->black_end - s->black_start) >= s->black_min_duration) {
155 av_log(s, AV_LOG_INFO,
156 "black_start:%s black_end:%s black_duration:%s\n",
157 av_ts2timestr(s->black_start, &s->time_base),
158 av_ts2timestr(s->black_end, &s->time_base),
159 av_ts2timestr(s->black_end - s->black_start, &s->time_base));
160 }
161 }
162
163 static int black_counter(AVFilterContext *ctx, void *arg,
164 int jobnr, int nb_jobs)
165 {
166 BlackDetectContext *s = ctx->priv;
167 const AVFrame *in = arg;
168 const int plane = s->alpha ? 3 : 0;
169 const int linesize = in->linesize[plane];
170 const int h = in->height;
171 const int start = (h * jobnr) / nb_jobs;
172 const int end = (h * (jobnr+1)) / nb_jobs;
173
174 s->counter[jobnr] = s->func(in->data[plane] + start * linesize,
175 linesize, in->width, end - start,
176 s->pixel_black_th_i);
177
178 return 0;
179 }
180
181 static int filter_frame(AVFilterLink *inlink, AVFrame *picref)
182 {
183 FilterLink *inl = ff_filter_link(inlink);
184 AVFilterContext *ctx = inlink->dst;
185 BlackDetectContext *s = ctx->priv;
186 double picture_black_ratio = 0;
187 const int max = (1 << s->depth) - 1;
188 const int factor = (1 << (s->depth - 8));
189 const int full = picref->color_range == AVCOL_RANGE_JPEG ||
190 ff_fmt_is_in(picref->format, yuvj_formats) ||
191 s->alpha;
192
193 s->pixel_black_th_i = full ? s->pixel_black_th * max :
194 // luminance_minimum_value + pixel_black_th * luminance_range_size
195 16 * factor + s->pixel_black_th * (235 - 16) * factor;
196
197 ff_filter_execute(ctx, black_counter, picref, NULL,
198 FFMIN(inlink->h, s->nb_threads));
199
200 for (int i = 0; i < s->nb_threads; i++)
201 s->nb_black_pixels += s->counter[i];
202
203 picture_black_ratio = (double)s->nb_black_pixels / (inlink->w * inlink->h);
204
205 av_log(ctx, AV_LOG_DEBUG,
206 "frame:%"PRId64" picture_black_ratio:%f pts:%s t:%s type:%c\n",
207 inl->frame_count_out, picture_black_ratio,
208 av_ts2str(picref->pts), av_ts2timestr(picref->pts, &s->time_base),
209 av_get_picture_type_char(picref->pict_type));
210
211 if (picture_black_ratio >= s->picture_black_ratio_th) {
212 if (!s->black_started) {
213 /* black starts here */
214 s->black_started = 1;
215 s->black_start = picref->pts;
216 av_dict_set(&picref->metadata, "lavfi.black_start",
217 av_ts2timestr(s->black_start, &s->time_base), 0);
218 }
219 } else if (s->black_started) {
220 /* black ends here */
221 s->black_started = 0;
222 s->black_end = picref->pts;
223 check_black_end(ctx);
224 av_dict_set(&picref->metadata, "lavfi.black_end",
225 av_ts2timestr(s->black_end, &s->time_base), 0);
226 }
227
228 s->last_picref_pts = picref->pts;
229 s->nb_black_pixels = 0;
230 return ff_filter_frame(inlink->dst->outputs[0], picref);
231 }
232
233 static av_cold void uninit(AVFilterContext *ctx)
234 {
235 BlackDetectContext *s = ctx->priv;
236
237 av_freep(&s->counter);
238
239 if (s->black_started) {
240 // FIXME: black_end should be set to last_picref_pts + last_picref_duration
241 s->black_end = s->last_picref_pts;
242 check_black_end(ctx);
243 }
244 }
245
246 static const AVFilterPad blackdetect_inputs[] = {
247 {
248 .name = "default",
249 .type = AVMEDIA_TYPE_VIDEO,
250 .config_props = config_input,
251 .filter_frame = filter_frame,
252 },
253 };
254
255 const FFFilter ff_vf_blackdetect = {
256 .p.name = "blackdetect",
257 .p.description = NULL_IF_CONFIG_SMALL("Detect video intervals that are (almost) black."),
258 .p.priv_class = &blackdetect_class,
259 .p.flags = AVFILTER_FLAG_SLICE_THREADS | AVFILTER_FLAG_METADATA_ONLY,
260 .priv_size = sizeof(BlackDetectContext),
261 FILTER_INPUTS(blackdetect_inputs),
262 FILTER_OUTPUTS(ff_video_default_filterpad),
263 FILTER_QUERY_FUNC2(query_format),
264 .uninit = uninit,
265 };
266