• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
query_formats(AVFilterContext * ctx)76 static int query_formats(AVFilterContext *ctx)
77 {
78     static const enum AVPixelFormat pix_fmts[] = {
79         AV_PIX_FMT_GRAY8,
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,
84         YUVJ_FORMATS,
85         AV_PIX_FMT_GRAY10, AV_PIX_FMT_GRAY12, AV_PIX_FMT_GRAY14,
86         AV_PIX_FMT_GRAY16,
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,
89         AV_PIX_FMT_YUV440P10,
90         AV_PIX_FMT_YUV444P12, AV_PIX_FMT_YUV422P12, AV_PIX_FMT_YUV420P12,
91         AV_PIX_FMT_YUV440P12,
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,
98         AV_PIX_FMT_NONE
99     };
100 
101     AVFilterFormats *fmts_list = ff_make_format_list(pix_fmts);
102     if (!fmts_list)
103         return AVERROR(ENOMEM);
104     return ff_set_common_formats(ctx, fmts_list);
105 }
106 
config_input(AVFilterLink * inlink)107 static int config_input(AVFilterLink *inlink)
108 {
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));
115 
116     s->depth = depth;
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));
121     if (!s->counter)
122         return AVERROR(ENOMEM);
123 
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;
128 
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);
134     return 0;
135 }
136 
check_black_end(AVFilterContext * ctx)137 static void check_black_end(AVFilterContext *ctx)
138 {
139     BlackDetectContext *s = ctx->priv;
140 
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));
147     }
148 }
149 
black_counter(AVFilterContext * ctx,void * arg,int jobnr,int nb_jobs)150 static int black_counter(AVFilterContext *ctx, void *arg,
151                          int jobnr, int nb_jobs)
152 {
153     BlackDetectContext *s = ctx->priv;
154     const unsigned int threshold = s->pixel_black_th_i;
155     unsigned int *counterp = &s->counter[jobnr];
156     AVFrame *in = arg;
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;
164 
165     if (s->depth == 8) {
166         const uint8_t *p = 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;
172         }
173     } else {
174         const uint16_t *p = (const uint16_t *)(in->data[0] + start * linesize);
175 
176         for (int i = 0; i < size; i++) {
177             for (int x = 0; x < w; x++)
178                 counter += p[x] <= threshold;
179             p += linesize / 2;
180         }
181     }
182 
183     *counterp = counter;
184 
185     return 0;
186 }
187 
filter_frame(AVFilterLink * inlink,AVFrame * picref)188 static int filter_frame(AVFilterLink *inlink, AVFrame *picref)
189 {
190     AVFilterContext *ctx = inlink->dst;
191     BlackDetectContext *s = ctx->priv;
192     double picture_black_ratio = 0;
193 
194     ctx->internal->execute(ctx, black_counter, picref, NULL,
195                            FFMIN(inlink->h, s->nb_threads));
196 
197     for (int i = 0; i < s->nb_threads; i++)
198         s->nb_black_pixels += s->counter[i];
199 
200     picture_black_ratio = (double)s->nb_black_pixels / (inlink->w * inlink->h);
201 
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));
207 
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);
215         }
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);
223     }
224 
225     s->last_picref_pts = picref->pts;
226     s->nb_black_pixels = 0;
227     return ff_filter_frame(inlink->dst->outputs[0], picref);
228 }
229 
uninit(AVFilterContext * ctx)230 static av_cold void uninit(AVFilterContext *ctx)
231 {
232     BlackDetectContext *s = ctx->priv;
233 
234     av_freep(&s->counter);
235 
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);
240     }
241 }
242 
243 static const AVFilterPad blackdetect_inputs[] = {
244     {
245         .name          = "default",
246         .type          = AVMEDIA_TYPE_VIDEO,
247         .config_props  = config_input,
248         .filter_frame  = filter_frame,
249     },
250     { NULL }
251 };
252 
253 static const AVFilterPad blackdetect_outputs[] = {
254     {
255         .name          = "default",
256         .type          = AVMEDIA_TYPE_VIDEO,
257     },
258     { NULL }
259 };
260 
261 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,
268     .uninit        = uninit,
269     .priv_class    = &blackdetect_class,
270     .flags         = AVFILTER_FLAG_SLICE_THREADS,
271 };
272