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