FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavfilter/vf_blackdetect.c
Date: 2024-07-17 14:05:47
Exec Total Coverage
Lines: 0 88 0.0%
Functions: 0 5 0.0%
Branches: 0 30 0.0%

Line Branch Exec Source
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/mem.h"
29 #include "libavutil/opt.h"
30 #include "libavutil/pixdesc.h"
31 #include "libavutil/timestamp.h"
32 #include "avfilter.h"
33 #include "internal.h"
34 #include "video.h"
35
36 typedef struct BlackDetectContext {
37 const AVClass *class;
38 double black_min_duration_time; ///< minimum duration of detected black, in seconds
39 int64_t black_min_duration; ///< minimum duration of detected black, expressed in timebase units
40 int64_t black_start; ///< pts start time of the first black picture
41 int64_t black_end; ///< pts end time of the last black picture
42 int64_t last_picref_pts; ///< pts of the last input picture
43 int black_started;
44
45 double picture_black_ratio_th;
46 double pixel_black_th;
47 unsigned int pixel_black_th_i;
48
49 unsigned int nb_black_pixels; ///< number of black pixels counted so far
50 AVRational time_base;
51 int depth;
52 int nb_threads;
53 unsigned int *counter;
54 } BlackDetectContext;
55
56 #define OFFSET(x) offsetof(BlackDetectContext, x)
57 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
58
59 static const AVOption blackdetect_options[] = {
60 { "d", "set minimum detected black duration in seconds", OFFSET(black_min_duration_time), AV_OPT_TYPE_DOUBLE, {.dbl=2}, 0, DBL_MAX, FLAGS },
61 { "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 },
62 { "picture_black_ratio_th", "set the picture black ratio threshold", OFFSET(picture_black_ratio_th), AV_OPT_TYPE_DOUBLE, {.dbl=.98}, 0, 1, FLAGS },
63 { "pic_th", "set the picture black ratio threshold", OFFSET(picture_black_ratio_th), AV_OPT_TYPE_DOUBLE, {.dbl=.98}, 0, 1, FLAGS },
64 { "pixel_black_th", "set the pixel black threshold", OFFSET(pixel_black_th), AV_OPT_TYPE_DOUBLE, {.dbl=.10}, 0, 1, FLAGS },
65 { "pix_th", "set the pixel black threshold", OFFSET(pixel_black_th), AV_OPT_TYPE_DOUBLE, {.dbl=.10}, 0, 1, FLAGS },
66 { NULL }
67 };
68
69 AVFILTER_DEFINE_CLASS(blackdetect);
70
71 #define YUVJ_FORMATS \
72 AV_PIX_FMT_YUVJ411P, AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ440P
73
74 static const enum AVPixelFormat yuvj_formats[] = {
75 YUVJ_FORMATS, AV_PIX_FMT_NONE
76 };
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 static int config_input(AVFilterLink *inlink)
102 {
103 AVFilterContext *ctx = inlink->dst;
104 BlackDetectContext *s = ctx->priv;
105 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
106 const int depth = desc->comp[0].depth;
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 av_log(s, AV_LOG_VERBOSE,
117 "black_min_duration:%s pixel_black_th:%f picture_black_ratio_th:%f\n",
118 av_ts2timestr(s->black_min_duration, &s->time_base),
119 s->pixel_black_th, s->picture_black_ratio_th);
120 return 0;
121 }
122
123 static void check_black_end(AVFilterContext *ctx)
124 {
125 BlackDetectContext *s = ctx->priv;
126
127 if ((s->black_end - s->black_start) >= s->black_min_duration) {
128 av_log(s, AV_LOG_INFO,
129 "black_start:%s black_end:%s black_duration:%s\n",
130 av_ts2timestr(s->black_start, &s->time_base),
131 av_ts2timestr(s->black_end, &s->time_base),
132 av_ts2timestr(s->black_end - s->black_start, &s->time_base));
133 }
134 }
135
136 static int black_counter(AVFilterContext *ctx, void *arg,
137 int jobnr, int nb_jobs)
138 {
139 BlackDetectContext *s = ctx->priv;
140 const unsigned int threshold = s->pixel_black_th_i;
141 unsigned int *counterp = &s->counter[jobnr];
142 AVFrame *in = arg;
143 const int linesize = in->linesize[0];
144 const int w = in->width;
145 const int h = in->height;
146 const int start = (h * jobnr) / nb_jobs;
147 const int end = (h * (jobnr+1)) / nb_jobs;
148 const int size = end - start;
149 unsigned int counter = 0;
150
151 if (s->depth == 8) {
152 const uint8_t *p = in->data[0] + start * linesize;
153
154 for (int i = 0; i < size; i++) {
155 for (int x = 0; x < w; x++)
156 counter += p[x] <= threshold;
157 p += linesize;
158 }
159 } else {
160 const uint16_t *p = (const uint16_t *)(in->data[0] + start * linesize);
161
162 for (int i = 0; i < size; i++) {
163 for (int x = 0; x < w; x++)
164 counter += p[x] <= threshold;
165 p += linesize / 2;
166 }
167 }
168
169 *counterp = counter;
170
171 return 0;
172 }
173
174 static int filter_frame(AVFilterLink *inlink, AVFrame *picref)
175 {
176 AVFilterContext *ctx = inlink->dst;
177 BlackDetectContext *s = ctx->priv;
178 double picture_black_ratio = 0;
179 const int max = (1 << s->depth) - 1;
180 const int factor = (1 << (s->depth - 8));
181 const int full = picref->color_range == AVCOL_RANGE_JPEG ||
182 ff_fmt_is_in(picref->format, yuvj_formats);
183
184 s->pixel_black_th_i = full ? s->pixel_black_th * max :
185 // luminance_minimum_value + pixel_black_th * luminance_range_size
186 16 * factor + s->pixel_black_th * (235 - 16) * factor;
187
188 ff_filter_execute(ctx, black_counter, picref, NULL,
189 FFMIN(inlink->h, s->nb_threads));
190
191 for (int i = 0; i < s->nb_threads; i++)
192 s->nb_black_pixels += s->counter[i];
193
194 picture_black_ratio = (double)s->nb_black_pixels / (inlink->w * inlink->h);
195
196 av_log(ctx, AV_LOG_DEBUG,
197 "frame:%"PRId64" picture_black_ratio:%f pts:%s t:%s type:%c\n",
198 inlink->frame_count_out, picture_black_ratio,
199 av_ts2str(picref->pts), av_ts2timestr(picref->pts, &s->time_base),
200 av_get_picture_type_char(picref->pict_type));
201
202 if (picture_black_ratio >= s->picture_black_ratio_th) {
203 if (!s->black_started) {
204 /* black starts here */
205 s->black_started = 1;
206 s->black_start = picref->pts;
207 av_dict_set(&picref->metadata, "lavfi.black_start",
208 av_ts2timestr(s->black_start, &s->time_base), 0);
209 }
210 } else if (s->black_started) {
211 /* black ends here */
212 s->black_started = 0;
213 s->black_end = picref->pts;
214 check_black_end(ctx);
215 av_dict_set(&picref->metadata, "lavfi.black_end",
216 av_ts2timestr(s->black_end, &s->time_base), 0);
217 }
218
219 s->last_picref_pts = picref->pts;
220 s->nb_black_pixels = 0;
221 return ff_filter_frame(inlink->dst->outputs[0], picref);
222 }
223
224 static av_cold void uninit(AVFilterContext *ctx)
225 {
226 BlackDetectContext *s = ctx->priv;
227
228 av_freep(&s->counter);
229
230 if (s->black_started) {
231 // FIXME: black_end should be set to last_picref_pts + last_picref_duration
232 s->black_end = s->last_picref_pts;
233 check_black_end(ctx);
234 }
235 }
236
237 static const AVFilterPad blackdetect_inputs[] = {
238 {
239 .name = "default",
240 .type = AVMEDIA_TYPE_VIDEO,
241 .config_props = config_input,
242 .filter_frame = filter_frame,
243 },
244 };
245
246 const AVFilter ff_vf_blackdetect = {
247 .name = "blackdetect",
248 .description = NULL_IF_CONFIG_SMALL("Detect video intervals that are (almost) black."),
249 .priv_size = sizeof(BlackDetectContext),
250 FILTER_INPUTS(blackdetect_inputs),
251 FILTER_OUTPUTS(ff_video_default_filterpad),
252 FILTER_PIXFMTS_ARRAY(pix_fmts),
253 .uninit = uninit,
254 .priv_class = &blackdetect_class,
255 .flags = AVFILTER_FLAG_SLICE_THREADS | AVFILTER_FLAG_METADATA_ONLY,
256 };
257