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