yading@10
|
1 /*
|
yading@10
|
2 * Copyright (c) 2012 Stefano Sabatini
|
yading@10
|
3 *
|
yading@10
|
4 * This file is part of FFmpeg.
|
yading@10
|
5 *
|
yading@10
|
6 * FFmpeg is free software; you can redistribute it and/or
|
yading@10
|
7 * modify it under the terms of the GNU Lesser General Public
|
yading@10
|
8 * License as published by the Free Software Foundation; either
|
yading@10
|
9 * version 2.1 of the License, or (at your option) any later version.
|
yading@10
|
10 *
|
yading@10
|
11 * FFmpeg is distributed in the hope that it will be useful,
|
yading@10
|
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
yading@10
|
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
yading@10
|
14 * Lesser General Public License for more details.
|
yading@10
|
15 *
|
yading@10
|
16 * You should have received a copy of the GNU Lesser General Public
|
yading@10
|
17 * License along with FFmpeg; if not, write to the Free Software
|
yading@10
|
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
yading@10
|
19 */
|
yading@10
|
20
|
yading@10
|
21 /**
|
yading@10
|
22 * @file
|
yading@10
|
23 * Video black detector, loosely based on blackframe with extended
|
yading@10
|
24 * syntax and features
|
yading@10
|
25 */
|
yading@10
|
26
|
yading@10
|
27 #include <float.h>
|
yading@10
|
28 #include "libavutil/opt.h"
|
yading@10
|
29 #include "libavutil/timestamp.h"
|
yading@10
|
30 #include "avfilter.h"
|
yading@10
|
31 #include "internal.h"
|
yading@10
|
32
|
yading@10
|
33 typedef struct {
|
yading@10
|
34 const AVClass *class;
|
yading@10
|
35 double black_min_duration_time; ///< minimum duration of detected black, in seconds
|
yading@10
|
36 int64_t black_min_duration; ///< minimum duration of detected black, expressed in timebase units
|
yading@10
|
37 int64_t black_start; ///< pts start time of the first black picture
|
yading@10
|
38 int64_t black_end; ///< pts end time of the last black picture
|
yading@10
|
39 int64_t last_picref_pts; ///< pts of the last input picture
|
yading@10
|
40 int black_started;
|
yading@10
|
41
|
yading@10
|
42 double picture_black_ratio_th;
|
yading@10
|
43 double pixel_black_th;
|
yading@10
|
44 unsigned int pixel_black_th_i;
|
yading@10
|
45
|
yading@10
|
46 unsigned int frame_count; ///< frame number
|
yading@10
|
47 unsigned int nb_black_pixels; ///< number of black pixels counted so far
|
yading@10
|
48 } BlackDetectContext;
|
yading@10
|
49
|
yading@10
|
50 #define OFFSET(x) offsetof(BlackDetectContext, x)
|
yading@10
|
51 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
|
yading@10
|
52
|
yading@10
|
53 static const AVOption blackdetect_options[] = {
|
yading@10
|
54 { "d", "set minimum detected black duration in seconds", OFFSET(black_min_duration_time), AV_OPT_TYPE_DOUBLE, {.dbl=2}, 0, DBL_MAX, FLAGS },
|
yading@10
|
55 { "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 },
|
yading@10
|
56 { "picture_black_ratio_th", "set the picture black ratio threshold", OFFSET(picture_black_ratio_th), AV_OPT_TYPE_DOUBLE, {.dbl=.98}, 0, 1, FLAGS },
|
yading@10
|
57 { "pic_th", "set the picture black ratio threshold", OFFSET(picture_black_ratio_th), AV_OPT_TYPE_DOUBLE, {.dbl=.98}, 0, 1, FLAGS },
|
yading@10
|
58 { "pixel_black_th", "set the pixel black threshold", OFFSET(pixel_black_th), AV_OPT_TYPE_DOUBLE, {.dbl=.10}, 0, 1, FLAGS },
|
yading@10
|
59 { "pix_th", "set the pixel black threshold", OFFSET(pixel_black_th), AV_OPT_TYPE_DOUBLE, {.dbl=.10}, 0, 1, FLAGS },
|
yading@10
|
60 { NULL },
|
yading@10
|
61 };
|
yading@10
|
62
|
yading@10
|
63 AVFILTER_DEFINE_CLASS(blackdetect);
|
yading@10
|
64
|
yading@10
|
65 #define YUVJ_FORMATS \
|
yading@10
|
66 AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ440P
|
yading@10
|
67
|
yading@10
|
68 static enum AVPixelFormat yuvj_formats[] = {
|
yading@10
|
69 YUVJ_FORMATS, AV_PIX_FMT_NONE
|
yading@10
|
70 };
|
yading@10
|
71
|
yading@10
|
72 static int query_formats(AVFilterContext *ctx)
|
yading@10
|
73 {
|
yading@10
|
74 static const enum AVPixelFormat pix_fmts[] = {
|
yading@10
|
75 AV_PIX_FMT_YUV410P, AV_PIX_FMT_YUV420P, AV_PIX_FMT_GRAY8, AV_PIX_FMT_NV12,
|
yading@10
|
76 AV_PIX_FMT_NV21, AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV411P,
|
yading@10
|
77 YUVJ_FORMATS,
|
yading@10
|
78 AV_PIX_FMT_NONE
|
yading@10
|
79 };
|
yading@10
|
80
|
yading@10
|
81 ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
|
yading@10
|
82 return 0;
|
yading@10
|
83 }
|
yading@10
|
84
|
yading@10
|
85 static int config_input(AVFilterLink *inlink)
|
yading@10
|
86 {
|
yading@10
|
87 AVFilterContext *ctx = inlink->dst;
|
yading@10
|
88 BlackDetectContext *blackdetect = ctx->priv;
|
yading@10
|
89
|
yading@10
|
90 blackdetect->black_min_duration =
|
yading@10
|
91 blackdetect->black_min_duration_time / av_q2d(inlink->time_base);
|
yading@10
|
92
|
yading@10
|
93 blackdetect->pixel_black_th_i = ff_fmt_is_in(inlink->format, yuvj_formats) ?
|
yading@10
|
94 // luminance_minimum_value + pixel_black_th * luminance_range_size
|
yading@10
|
95 blackdetect->pixel_black_th * 255 :
|
yading@10
|
96 16 + blackdetect->pixel_black_th * (235 - 16);
|
yading@10
|
97
|
yading@10
|
98 av_log(blackdetect, AV_LOG_VERBOSE,
|
yading@10
|
99 "black_min_duration:%s pixel_black_th:%f pixel_black_th_i:%d picture_black_ratio_th:%f\n",
|
yading@10
|
100 av_ts2timestr(blackdetect->black_min_duration, &inlink->time_base),
|
yading@10
|
101 blackdetect->pixel_black_th, blackdetect->pixel_black_th_i,
|
yading@10
|
102 blackdetect->picture_black_ratio_th);
|
yading@10
|
103 return 0;
|
yading@10
|
104 }
|
yading@10
|
105
|
yading@10
|
106 static void check_black_end(AVFilterContext *ctx)
|
yading@10
|
107 {
|
yading@10
|
108 BlackDetectContext *blackdetect = ctx->priv;
|
yading@10
|
109 AVFilterLink *inlink = ctx->inputs[0];
|
yading@10
|
110
|
yading@10
|
111 if ((blackdetect->black_end - blackdetect->black_start) >= blackdetect->black_min_duration) {
|
yading@10
|
112 av_log(blackdetect, AV_LOG_INFO,
|
yading@10
|
113 "black_start:%s black_end:%s black_duration:%s\n",
|
yading@10
|
114 av_ts2timestr(blackdetect->black_start, &inlink->time_base),
|
yading@10
|
115 av_ts2timestr(blackdetect->black_end, &inlink->time_base),
|
yading@10
|
116 av_ts2timestr(blackdetect->black_end - blackdetect->black_start, &inlink->time_base));
|
yading@10
|
117 }
|
yading@10
|
118 }
|
yading@10
|
119
|
yading@10
|
120 static int request_frame(AVFilterLink *outlink)
|
yading@10
|
121 {
|
yading@10
|
122 AVFilterContext *ctx = outlink->src;
|
yading@10
|
123 BlackDetectContext *blackdetect = ctx->priv;
|
yading@10
|
124 AVFilterLink *inlink = ctx->inputs[0];
|
yading@10
|
125 int ret = ff_request_frame(inlink);
|
yading@10
|
126
|
yading@10
|
127 if (ret == AVERROR_EOF && blackdetect->black_started) {
|
yading@10
|
128 // FIXME: black_end should be set to last_picref_pts + last_picref_duration
|
yading@10
|
129 blackdetect->black_end = blackdetect->last_picref_pts;
|
yading@10
|
130 check_black_end(ctx);
|
yading@10
|
131 }
|
yading@10
|
132 return ret;
|
yading@10
|
133 }
|
yading@10
|
134
|
yading@10
|
135 static int filter_frame(AVFilterLink *inlink, AVFrame *picref)
|
yading@10
|
136 {
|
yading@10
|
137 AVFilterContext *ctx = inlink->dst;
|
yading@10
|
138 BlackDetectContext *blackdetect = ctx->priv;
|
yading@10
|
139 double picture_black_ratio = 0;
|
yading@10
|
140 const uint8_t *p = picref->data[0];
|
yading@10
|
141 int x, i;
|
yading@10
|
142
|
yading@10
|
143 for (i = 0; i < inlink->h; i++) {
|
yading@10
|
144 for (x = 0; x < inlink->w; x++)
|
yading@10
|
145 blackdetect->nb_black_pixels += p[x] <= blackdetect->pixel_black_th_i;
|
yading@10
|
146 p += picref->linesize[0];
|
yading@10
|
147 }
|
yading@10
|
148
|
yading@10
|
149 picture_black_ratio = (double)blackdetect->nb_black_pixels / (inlink->w * inlink->h);
|
yading@10
|
150
|
yading@10
|
151 av_log(ctx, AV_LOG_DEBUG,
|
yading@10
|
152 "frame:%u picture_black_ratio:%f pts:%s t:%s type:%c\n",
|
yading@10
|
153 blackdetect->frame_count, picture_black_ratio,
|
yading@10
|
154 av_ts2str(picref->pts), av_ts2timestr(picref->pts, &inlink->time_base),
|
yading@10
|
155 av_get_picture_type_char(picref->pict_type));
|
yading@10
|
156
|
yading@10
|
157 if (picture_black_ratio >= blackdetect->picture_black_ratio_th) {
|
yading@10
|
158 if (!blackdetect->black_started) {
|
yading@10
|
159 /* black starts here */
|
yading@10
|
160 blackdetect->black_started = 1;
|
yading@10
|
161 blackdetect->black_start = picref->pts;
|
yading@10
|
162 }
|
yading@10
|
163 } else if (blackdetect->black_started) {
|
yading@10
|
164 /* black ends here */
|
yading@10
|
165 blackdetect->black_started = 0;
|
yading@10
|
166 blackdetect->black_end = picref->pts;
|
yading@10
|
167 check_black_end(ctx);
|
yading@10
|
168 }
|
yading@10
|
169
|
yading@10
|
170 blackdetect->last_picref_pts = picref->pts;
|
yading@10
|
171 blackdetect->frame_count++;
|
yading@10
|
172 blackdetect->nb_black_pixels = 0;
|
yading@10
|
173 return ff_filter_frame(inlink->dst->outputs[0], picref);
|
yading@10
|
174 }
|
yading@10
|
175
|
yading@10
|
176 static const AVFilterPad blackdetect_inputs[] = {
|
yading@10
|
177 {
|
yading@10
|
178 .name = "default",
|
yading@10
|
179 .type = AVMEDIA_TYPE_VIDEO,
|
yading@10
|
180 .config_props = config_input,
|
yading@10
|
181 .get_video_buffer = ff_null_get_video_buffer,
|
yading@10
|
182 .filter_frame = filter_frame,
|
yading@10
|
183 },
|
yading@10
|
184 { NULL }
|
yading@10
|
185 };
|
yading@10
|
186
|
yading@10
|
187 static const AVFilterPad blackdetect_outputs[] = {
|
yading@10
|
188 {
|
yading@10
|
189 .name = "default",
|
yading@10
|
190 .type = AVMEDIA_TYPE_VIDEO,
|
yading@10
|
191 .request_frame = request_frame,
|
yading@10
|
192 },
|
yading@10
|
193 { NULL }
|
yading@10
|
194 };
|
yading@10
|
195
|
yading@10
|
196 AVFilter avfilter_vf_blackdetect = {
|
yading@10
|
197 .name = "blackdetect",
|
yading@10
|
198 .description = NULL_IF_CONFIG_SMALL("Detect video intervals that are (almost) black."),
|
yading@10
|
199 .priv_size = sizeof(BlackDetectContext),
|
yading@10
|
200 .query_formats = query_formats,
|
yading@10
|
201 .inputs = blackdetect_inputs,
|
yading@10
|
202 .outputs = blackdetect_outputs,
|
yading@10
|
203 .priv_class = &blackdetect_class,
|
yading@10
|
204 };
|