Skip to content

Commit 7e783fa

Browse files
committed
avfilter/vf_blackdetect: add alpha option
Check the alpha plane for (almost) transparent frames, instead of checking the luma channel for almost black frames. Signed-off-by: Niklas Haas <[email protected]> Sponsored-by: nxtedition
1 parent 02f45a7 commit 7e783fa

File tree

2 files changed

+44
-13
lines changed

2 files changed

+44
-13
lines changed

doc/filters.texi

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8687,6 +8687,12 @@ the input video format, the range is [0-255] for YUV full-range
86878687
formats and [16-235] for YUV non full-range formats.
86888688

86898689
Default value is 0.10.
8690+
8691+
@item alpha
8692+
If true, check the alpha channel instead of the luma channel. Detects frames
8693+
which are (almost) transparent, instead of frames which are almost black.
8694+
8695+
Default value is disabled.
86908696
@end table
86918697

86928698
The following example sets the maximum pixel threshold to the minimum

libavfilter/vf_blackdetect.c

Lines changed: 38 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#include "libavutil/timestamp.h"
3232
#include "avfilter.h"
3333
#include "filters.h"
34+
#include "formats.h"
3435
#include "video.h"
3536

3637
typedef struct BlackDetectContext {
@@ -45,6 +46,7 @@ typedef struct BlackDetectContext {
4546
double picture_black_ratio_th;
4647
double pixel_black_th;
4748
unsigned int pixel_black_th_i;
49+
int alpha;
4850

4951
unsigned int nb_black_pixels; ///< number of black pixels counted so far
5052
AVRational time_base;
@@ -63,6 +65,7 @@ static const AVOption blackdetect_options[] = {
6365
{ "pic_th", "set the picture black ratio threshold", OFFSET(picture_black_ratio_th), AV_OPT_TYPE_DOUBLE, {.dbl=.98}, 0, 1, FLAGS },
6466
{ "pixel_black_th", "set the pixel black threshold", OFFSET(pixel_black_th), AV_OPT_TYPE_DOUBLE, {.dbl=.10}, 0, 1, FLAGS },
6567
{ "pix_th", "set the pixel black threshold", OFFSET(pixel_black_th), AV_OPT_TYPE_DOUBLE, {.dbl=.10}, 0, 1, FLAGS },
68+
{ "alpha", "check alpha instead of luma", OFFSET(alpha), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS },
6669
{ NULL }
6770
};
6871

@@ -71,11 +74,21 @@ AVFILTER_DEFINE_CLASS(blackdetect);
7174
#define YUVJ_FORMATS \
7275
AV_PIX_FMT_YUVJ411P, AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ440P
7376

77+
#define YUVA_FORMATS \
78+
AV_PIX_FMT_YUVA420P, AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUVA444P, \
79+
AV_PIX_FMT_YUVA444P9, AV_PIX_FMT_YUVA444P10, AV_PIX_FMT_YUVA444P12, AV_PIX_FMT_YUVA444P16, \
80+
AV_PIX_FMT_YUVA422P9, AV_PIX_FMT_YUVA422P10, AV_PIX_FMT_YUVA422P12, AV_PIX_FMT_YUVA422P16, \
81+
AV_PIX_FMT_YUVA420P9, AV_PIX_FMT_YUVA420P10, AV_PIX_FMT_YUVA420P16
82+
7483
static const enum AVPixelFormat yuvj_formats[] = {
7584
YUVJ_FORMATS, AV_PIX_FMT_NONE
7685
};
7786

78-
static const enum AVPixelFormat pix_fmts[] = {
87+
static const enum AVPixelFormat yuva_formats[] = {
88+
YUVA_FORMATS, AV_PIX_FMT_NONE
89+
};
90+
91+
static const enum AVPixelFormat yuv_formats[] = {
7992
AV_PIX_FMT_GRAY8,
8093
AV_PIX_FMT_YUV410P, AV_PIX_FMT_YUV411P,
8194
AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV422P,
@@ -91,13 +104,23 @@ static const enum AVPixelFormat pix_fmts[] = {
91104
AV_PIX_FMT_YUV440P12,
92105
AV_PIX_FMT_YUV444P14, AV_PIX_FMT_YUV422P14, AV_PIX_FMT_YUV420P14,
93106
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
107+
YUVA_FORMATS, AV_PIX_FMT_NONE
99108
};
100109

110+
static int query_format(const AVFilterContext *ctx,
111+
AVFilterFormatsConfig **cfg_in,
112+
AVFilterFormatsConfig **cfg_out)
113+
{
114+
const BlackDetectContext *s = ctx->priv;
115+
AVFilterFormats *formats;
116+
if (s->alpha)
117+
formats = ff_make_format_list(yuva_formats);
118+
else
119+
formats = ff_make_format_list(yuv_formats);
120+
121+
return ff_set_common_formats2(ctx, cfg_in, cfg_out, formats);
122+
}
123+
101124
static int config_input(AVFilterLink *inlink)
102125
{
103126
AVFilterContext *ctx = inlink->dst;
@@ -114,9 +137,9 @@ static int config_input(AVFilterLink *inlink)
114137
return AVERROR(ENOMEM);
115138

116139
av_log(s, AV_LOG_VERBOSE,
117-
"black_min_duration:%s pixel_black_th:%f picture_black_ratio_th:%f\n",
140+
"black_min_duration:%s pixel_black_th:%f picture_black_ratio_th:%f alpha:%d\n",
118141
av_ts2timestr(s->black_min_duration, &s->time_base),
119-
s->pixel_black_th, s->picture_black_ratio_th);
142+
s->pixel_black_th, s->picture_black_ratio_th, s->alpha);
120143
return 0;
121144
}
122145

@@ -140,7 +163,8 @@ static int black_counter(AVFilterContext *ctx, void *arg,
140163
const unsigned int threshold = s->pixel_black_th_i;
141164
unsigned int *counterp = &s->counter[jobnr];
142165
AVFrame *in = arg;
143-
const int linesize = in->linesize[0];
166+
const int plane = s->alpha ? 3 : 0;
167+
const int linesize = in->linesize[plane];
144168
const int w = in->width;
145169
const int h = in->height;
146170
const int start = (h * jobnr) / nb_jobs;
@@ -149,15 +173,15 @@ static int black_counter(AVFilterContext *ctx, void *arg,
149173
unsigned int counter = 0;
150174

151175
if (s->depth == 8) {
152-
const uint8_t *p = in->data[0] + start * linesize;
176+
const uint8_t *p = in->data[plane] + start * linesize;
153177

154178
for (int i = 0; i < size; i++) {
155179
for (int x = 0; x < w; x++)
156180
counter += p[x] <= threshold;
157181
p += linesize;
158182
}
159183
} else {
160-
const uint16_t *p = (const uint16_t *)(in->data[0] + start * linesize);
184+
const uint16_t *p = (const uint16_t *)(in->data[plane] + start * linesize);
161185

162186
for (int i = 0; i < size; i++) {
163187
for (int x = 0; x < w; x++)
@@ -180,7 +204,8 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *picref)
180204
const int max = (1 << s->depth) - 1;
181205
const int factor = (1 << (s->depth - 8));
182206
const int full = picref->color_range == AVCOL_RANGE_JPEG ||
183-
ff_fmt_is_in(picref->format, yuvj_formats);
207+
ff_fmt_is_in(picref->format, yuvj_formats) ||
208+
s->alpha;
184209

185210
s->pixel_black_th_i = full ? s->pixel_black_th * max :
186211
// luminance_minimum_value + pixel_black_th * luminance_range_size
@@ -252,6 +277,6 @@ const FFFilter ff_vf_blackdetect = {
252277
.priv_size = sizeof(BlackDetectContext),
253278
FILTER_INPUTS(blackdetect_inputs),
254279
FILTER_OUTPUTS(ff_video_default_filterpad),
255-
FILTER_PIXFMTS_ARRAY(pix_fmts),
280+
FILTER_QUERY_FUNC2(query_format),
256281
.uninit = uninit,
257282
};

0 commit comments

Comments
 (0)