]> git.sesse.net Git - ffmpeg/blob - libavfilter/vf_blackdetect.c
Merge remote-tracking branch 'qatar/master'
[ffmpeg] / libavfilter / vf_blackdetect.c
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/timestamp.h"
30 #include "avfilter.h"
31 #include "internal.h"
32
33 typedef struct {
34     const AVClass *class;
35     double  black_min_duration_time; ///< minimum duration of detected black, in seconds
36     int64_t black_min_duration;      ///< minimum duration of detected black, expressed in timebase units
37     int64_t black_start;             ///< pts start time of the first black picture
38     int64_t black_end;               ///< pts end time of the last black picture
39     int black_started;
40
41     double       picture_black_ratio_th;
42     double       pixel_black_th;
43     unsigned int pixel_black_th_i;
44
45     unsigned int frame_count;       ///< frame number
46     unsigned int nb_black_pixels;   ///< number of black pixels counted so far
47 } BlackDetectContext;
48
49 #define OFFSET(x) offsetof(BlackDetectContext, x)
50 static const AVOption blackdetect_options[] = {
51     { "d",                  "set minimum detected black duration in seconds", OFFSET(black_min_duration_time), AV_OPT_TYPE_DOUBLE, {.dbl=2}, 0, DBL_MAX},
52     { "black_min_duration", "set minimum detected black duration in seconds", OFFSET(black_min_duration_time), AV_OPT_TYPE_DOUBLE, {.dbl=2}, 0, DBL_MAX},
53     { "picture_black_ratio_th", "set the picture black ratio threshold", OFFSET(picture_black_ratio_th), AV_OPT_TYPE_DOUBLE, {.dbl=.98}, 0, 1},
54     { "pic_th",                 "set the picture black ratio threshold", OFFSET(picture_black_ratio_th), AV_OPT_TYPE_DOUBLE, {.dbl=.98}, 0, 1},
55     { "pixel_black_th", "set the pixel black threshold", OFFSET(pixel_black_th), AV_OPT_TYPE_DOUBLE, {.dbl=.10}, 0, 1},
56     { "pix_th",         "set the pixel black threshold", OFFSET(pixel_black_th), AV_OPT_TYPE_DOUBLE, {.dbl=.10}, 0, 1},
57     { NULL },
58 };
59
60 static const char *blackdetect_get_name(void *ctx)
61 {
62     return "blackdetect";
63 }
64
65 static const AVClass blackdetect_class = {
66     .class_name = "BlackDetectContext",
67     .item_name  = blackdetect_get_name,
68     .option     = blackdetect_options,
69 };
70
71 #define YUVJ_FORMATS \
72     PIX_FMT_YUVJ420P, PIX_FMT_YUVJ422P, PIX_FMT_YUVJ444P, PIX_FMT_YUVJ440P
73
74 static enum PixelFormat yuvj_formats[] = {
75     YUVJ_FORMATS, PIX_FMT_NONE
76 };
77
78 static int query_formats(AVFilterContext *ctx)
79 {
80     static const enum PixelFormat pix_fmts[] = {
81         PIX_FMT_YUV410P, PIX_FMT_YUV420P, PIX_FMT_GRAY8, PIX_FMT_NV12,
82         PIX_FMT_NV21, PIX_FMT_YUV444P, PIX_FMT_YUV422P, PIX_FMT_YUV411P,
83         YUVJ_FORMATS,
84         PIX_FMT_NONE
85     };
86
87     avfilter_set_common_pixel_formats(ctx, avfilter_make_format_list(pix_fmts));
88     return 0;
89 }
90
91 static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque)
92 {
93     int ret;
94     BlackDetectContext *blackdetect = ctx->priv;
95
96     blackdetect->class = &blackdetect_class;
97     av_opt_set_defaults(blackdetect);
98
99     if ((ret = av_set_options_string(blackdetect, args, "=", ":")) < 0) {
100         av_log(ctx, AV_LOG_ERROR, "Error parsing options string: '%s'\n", args);
101         return ret;
102     }
103
104     return 0;
105 }
106
107 static int config_input(AVFilterLink *inlink)
108 {
109     AVFilterContext *ctx = inlink->dst;
110     BlackDetectContext *blackdetect = ctx->priv;
111
112     blackdetect->black_min_duration =
113         blackdetect->black_min_duration_time / av_q2d(inlink->time_base);
114
115     blackdetect->pixel_black_th_i = ff_fmt_is_in(inlink->format, yuvj_formats) ?
116         // luminance_minimum_value + pixel_black_th * luminance_range_size
117              blackdetect->pixel_black_th *  255 :
118         16 + blackdetect->pixel_black_th * (235 - 16);
119
120     av_log(blackdetect, AV_LOG_INFO,
121            "black_min_duration:%s pixel_black_th:%f pixel_black_th_i:%d picture_black_ratio_th:%f\n",
122            av_ts2timestr(blackdetect->black_min_duration, &inlink->time_base),
123            blackdetect->pixel_black_th, blackdetect->pixel_black_th_i,
124            blackdetect->picture_black_ratio_th);
125     return 0;
126 }
127
128 static void draw_slice(AVFilterLink *inlink, int y, int h, int slice_dir)
129 {
130     AVFilterContext *ctx = inlink->dst;
131     BlackDetectContext *blackdetect = ctx->priv;
132     AVFilterBufferRef *picref = inlink->cur_buf;
133     int x, i;
134     const uint8_t *p = picref->data[0] + y * picref->linesize[0];
135
136     for (i = 0; i < h; i++) {
137         for (x = 0; x < inlink->w; x++)
138             blackdetect->nb_black_pixels += p[x] <= blackdetect->pixel_black_th_i;
139         p += picref->linesize[0];
140     }
141
142     avfilter_draw_slice(ctx->outputs[0], y, h, slice_dir);
143 }
144
145 static void end_frame(AVFilterLink *inlink)
146 {
147     AVFilterContext *ctx = inlink->dst;
148     BlackDetectContext *blackdetect = ctx->priv;
149     AVFilterBufferRef *picref = inlink->cur_buf;
150     double picture_black_ratio = 0;
151
152     picture_black_ratio = (double)blackdetect->nb_black_pixels / (inlink->w * inlink->h);
153
154     av_log(ctx, AV_LOG_DEBUG,
155            "frame:%u picture_black_ratio:%f pos:%"PRId64" pts:%s t:%s type:%c\n",
156            blackdetect->frame_count, picture_black_ratio,
157            picref->pos, av_ts2str(picref->pts),
158            av_ts2timestr(blackdetect->black_start, &inlink->time_base),
159            av_get_picture_type_char(picref->video->pict_type));
160
161     if (picture_black_ratio >= blackdetect->picture_black_ratio_th) {
162         if (!blackdetect->black_started) {
163             /* black starts here */
164             blackdetect->black_started = 1;
165             blackdetect->black_start = picref->pts;
166         }
167     } else if (blackdetect->black_started) {
168         /* black ends here */
169         blackdetect->black_started = 0;
170         blackdetect->black_end = picref->pts;
171
172         if ((blackdetect->black_end - blackdetect->black_start) >= blackdetect->black_min_duration) {
173             av_log(blackdetect, AV_LOG_INFO,
174                    "black_start:%s black_end:%s black_duration:%s\n",
175                    av_ts2timestr(blackdetect->black_start, &inlink->time_base),
176                    av_ts2timestr(blackdetect->black_end, &inlink->time_base),
177                    av_ts2timestr(blackdetect->black_end - blackdetect->black_start, &inlink->time_base));
178         }
179     }
180
181     blackdetect->frame_count++;
182     blackdetect->nb_black_pixels = 0;
183     avfilter_unref_buffer(picref);
184     avfilter_end_frame(inlink->dst->outputs[0]);
185 }
186
187 AVFilter avfilter_vf_blackdetect = {
188     .name          = "blackdetect",
189     .description   = NULL_IF_CONFIG_SMALL("Detect video intervals that are (almost) black."),
190     .priv_size     = sizeof(BlackDetectContext),
191     .init          = init,
192     .query_formats = query_formats,
193
194     .inputs = (const AVFilterPad[]) {
195         { .name             = "default",
196           .type             = AVMEDIA_TYPE_VIDEO,
197           .config_props     = config_input,
198           .draw_slice       = draw_slice,
199           .get_video_buffer = avfilter_null_get_video_buffer,
200           .start_frame      = ff_null_start_frame_keep_ref,
201           .end_frame        = end_frame, },
202         { .name = NULL }
203     },
204
205     .outputs = (const AVFilterPad[]) {
206         { .name             = "default",
207           .type             = AVMEDIA_TYPE_VIDEO },
208         { .name = NULL }
209     },
210 };