2012-03-02 17:51:26 +03:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2012 Stefano Sabatini
|
|
|
|
*
|
|
|
|
* This file is part of FFmpeg.
|
|
|
|
*
|
|
|
|
* FFmpeg is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* FFmpeg is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with FFmpeg; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @file
|
|
|
|
* Video black detector, loosely based on blackframe with extended
|
|
|
|
* syntax and features
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <float.h>
|
|
|
|
#include "libavutil/opt.h"
|
2021-01-29 15:34:36 +02:00
|
|
|
#include "libavutil/pixdesc.h"
|
2012-03-02 17:51:26 +03:00
|
|
|
#include "libavutil/timestamp.h"
|
|
|
|
#include "avfilter.h"
|
|
|
|
#include "internal.h"
|
|
|
|
|
2017-05-12 20:00:49 +02:00
|
|
|
typedef struct BlackDetectContext {
|
2012-03-02 17:51:26 +03:00
|
|
|
const AVClass *class;
|
|
|
|
double black_min_duration_time; ///< minimum duration of detected black, in seconds
|
|
|
|
int64_t black_min_duration; ///< minimum duration of detected black, expressed in timebase units
|
|
|
|
int64_t black_start; ///< pts start time of the first black picture
|
|
|
|
int64_t black_end; ///< pts end time of the last black picture
|
2012-06-21 02:15:52 +03:00
|
|
|
int64_t last_picref_pts; ///< pts of the last input picture
|
2012-03-02 17:51:26 +03:00
|
|
|
int black_started;
|
|
|
|
|
|
|
|
double picture_black_ratio_th;
|
|
|
|
double pixel_black_th;
|
|
|
|
unsigned int pixel_black_th_i;
|
|
|
|
|
|
|
|
unsigned int nb_black_pixels; ///< number of black pixels counted so far
|
2021-01-29 15:20:12 +02:00
|
|
|
AVRational time_base;
|
2021-01-29 15:34:36 +02:00
|
|
|
int depth;
|
2021-01-30 17:04:28 +02:00
|
|
|
int nb_threads;
|
|
|
|
unsigned int *counter;
|
2012-03-02 17:51:26 +03:00
|
|
|
} BlackDetectContext;
|
|
|
|
|
|
|
|
#define OFFSET(x) offsetof(BlackDetectContext, x)
|
2012-08-13 14:40:01 +03:00
|
|
|
#define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
|
|
|
|
|
2012-03-02 17:51:26 +03:00
|
|
|
static const AVOption blackdetect_options[] = {
|
2012-08-13 14:40:01 +03:00
|
|
|
{ "d", "set minimum detected black duration in seconds", OFFSET(black_min_duration_time), AV_OPT_TYPE_DOUBLE, {.dbl=2}, 0, DBL_MAX, FLAGS },
|
|
|
|
{ "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 },
|
|
|
|
{ "picture_black_ratio_th", "set the picture black ratio threshold", OFFSET(picture_black_ratio_th), AV_OPT_TYPE_DOUBLE, {.dbl=.98}, 0, 1, FLAGS },
|
|
|
|
{ "pic_th", "set the picture black ratio threshold", OFFSET(picture_black_ratio_th), AV_OPT_TYPE_DOUBLE, {.dbl=.98}, 0, 1, FLAGS },
|
|
|
|
{ "pixel_black_th", "set the pixel black threshold", OFFSET(pixel_black_th), AV_OPT_TYPE_DOUBLE, {.dbl=.10}, 0, 1, FLAGS },
|
|
|
|
{ "pix_th", "set the pixel black threshold", OFFSET(pixel_black_th), AV_OPT_TYPE_DOUBLE, {.dbl=.10}, 0, 1, FLAGS },
|
2013-09-07 15:13:50 +03:00
|
|
|
{ NULL }
|
2012-03-02 17:51:26 +03:00
|
|
|
};
|
|
|
|
|
2012-06-22 15:33:09 +03:00
|
|
|
AVFILTER_DEFINE_CLASS(blackdetect);
|
2012-03-02 17:51:26 +03:00
|
|
|
|
|
|
|
#define YUVJ_FORMATS \
|
2013-07-05 18:29:13 +03:00
|
|
|
AV_PIX_FMT_YUVJ411P, AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ440P
|
2012-03-02 17:51:26 +03:00
|
|
|
|
2014-08-29 19:42:47 +03:00
|
|
|
static const enum AVPixelFormat yuvj_formats[] = {
|
2012-10-08 21:54:00 +03:00
|
|
|
YUVJ_FORMATS, AV_PIX_FMT_NONE
|
2012-03-02 17:51:26 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
static int query_formats(AVFilterContext *ctx)
|
|
|
|
{
|
2012-10-08 21:54:00 +03:00
|
|
|
static const enum AVPixelFormat pix_fmts[] = {
|
2013-07-05 18:29:13 +03:00
|
|
|
AV_PIX_FMT_GRAY8,
|
|
|
|
AV_PIX_FMT_YUV410P, AV_PIX_FMT_YUV411P,
|
|
|
|
AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV422P,
|
|
|
|
AV_PIX_FMT_YUV440P, AV_PIX_FMT_YUV444P,
|
|
|
|
AV_PIX_FMT_NV12, AV_PIX_FMT_NV21,
|
2012-03-02 17:51:26 +03:00
|
|
|
YUVJ_FORMATS,
|
2021-01-29 15:34:36 +02:00
|
|
|
AV_PIX_FMT_GRAY10, AV_PIX_FMT_GRAY12, AV_PIX_FMT_GRAY14,
|
|
|
|
AV_PIX_FMT_GRAY16,
|
|
|
|
AV_PIX_FMT_YUV420P9, AV_PIX_FMT_YUV422P9, AV_PIX_FMT_YUV444P9,
|
|
|
|
AV_PIX_FMT_YUV420P10, AV_PIX_FMT_YUV422P10, AV_PIX_FMT_YUV444P10,
|
|
|
|
AV_PIX_FMT_YUV440P10,
|
|
|
|
AV_PIX_FMT_YUV444P12, AV_PIX_FMT_YUV422P12, AV_PIX_FMT_YUV420P12,
|
|
|
|
AV_PIX_FMT_YUV440P12,
|
|
|
|
AV_PIX_FMT_YUV444P14, AV_PIX_FMT_YUV422P14, AV_PIX_FMT_YUV420P14,
|
|
|
|
AV_PIX_FMT_YUV420P16, AV_PIX_FMT_YUV422P16, AV_PIX_FMT_YUV444P16,
|
|
|
|
AV_PIX_FMT_YUVA420P, AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUVA444P,
|
|
|
|
AV_PIX_FMT_YUVA444P9, AV_PIX_FMT_YUVA444P10, AV_PIX_FMT_YUVA444P12, AV_PIX_FMT_YUVA444P16,
|
|
|
|
AV_PIX_FMT_YUVA422P9, AV_PIX_FMT_YUVA422P10, AV_PIX_FMT_YUVA422P12, AV_PIX_FMT_YUVA422P16,
|
|
|
|
AV_PIX_FMT_YUVA420P9, AV_PIX_FMT_YUVA420P10, AV_PIX_FMT_YUVA420P16,
|
2012-10-08 21:54:00 +03:00
|
|
|
AV_PIX_FMT_NONE
|
2012-03-02 17:51:26 +03:00
|
|
|
};
|
|
|
|
|
2015-04-03 19:55:18 +02:00
|
|
|
AVFilterFormats *fmts_list = ff_make_format_list(pix_fmts);
|
|
|
|
if (!fmts_list)
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
return ff_set_common_formats(ctx, fmts_list);
|
2012-03-02 17:51:26 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static int config_input(AVFilterLink *inlink)
|
|
|
|
{
|
|
|
|
AVFilterContext *ctx = inlink->dst;
|
2021-01-29 15:14:14 +02:00
|
|
|
BlackDetectContext *s = ctx->priv;
|
2021-01-29 15:34:36 +02:00
|
|
|
const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
|
|
|
|
const int depth = desc->comp[0].depth;
|
|
|
|
const int max = (1 << depth) - 1;
|
|
|
|
const int factor = (1 << (depth - 8));
|
2012-03-02 17:51:26 +03:00
|
|
|
|
2021-01-29 15:34:36 +02:00
|
|
|
s->depth = depth;
|
2021-01-30 17:04:28 +02:00
|
|
|
s->nb_threads = ff_filter_get_nb_threads(ctx);
|
2021-01-29 15:20:12 +02:00
|
|
|
s->time_base = inlink->time_base;
|
|
|
|
s->black_min_duration = s->black_min_duration_time / av_q2d(s->time_base);
|
2021-01-30 17:04:28 +02:00
|
|
|
s->counter = av_calloc(s->nb_threads, sizeof(*s->counter));
|
|
|
|
if (!s->counter)
|
|
|
|
return AVERROR(ENOMEM);
|
2012-03-02 17:51:26 +03:00
|
|
|
|
2021-01-29 15:14:14 +02:00
|
|
|
s->pixel_black_th_i = ff_fmt_is_in(inlink->format, yuvj_formats) ?
|
2012-03-02 17:51:26 +03:00
|
|
|
// luminance_minimum_value + pixel_black_th * luminance_range_size
|
2021-01-29 15:34:36 +02:00
|
|
|
s->pixel_black_th * max :
|
|
|
|
16 * factor + s->pixel_black_th * (235 - 16) * factor;
|
2012-03-02 17:51:26 +03:00
|
|
|
|
2021-01-29 15:14:14 +02:00
|
|
|
av_log(s, AV_LOG_VERBOSE,
|
2012-03-02 17:51:26 +03:00
|
|
|
"black_min_duration:%s pixel_black_th:%f pixel_black_th_i:%d picture_black_ratio_th:%f\n",
|
2021-01-29 15:20:12 +02:00
|
|
|
av_ts2timestr(s->black_min_duration, &s->time_base),
|
2021-01-29 15:14:14 +02:00
|
|
|
s->pixel_black_th, s->pixel_black_th_i,
|
|
|
|
s->picture_black_ratio_th);
|
2012-03-02 17:51:26 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-06-21 02:15:52 +03:00
|
|
|
static void check_black_end(AVFilterContext *ctx)
|
|
|
|
{
|
2021-01-29 15:14:14 +02:00
|
|
|
BlackDetectContext *s = ctx->priv;
|
2012-06-21 02:15:52 +03:00
|
|
|
|
2021-01-29 15:14:14 +02:00
|
|
|
if ((s->black_end - s->black_start) >= s->black_min_duration) {
|
|
|
|
av_log(s, AV_LOG_INFO,
|
2012-06-21 02:15:52 +03:00
|
|
|
"black_start:%s black_end:%s black_duration:%s\n",
|
2021-01-29 15:20:12 +02:00
|
|
|
av_ts2timestr(s->black_start, &s->time_base),
|
|
|
|
av_ts2timestr(s->black_end, &s->time_base),
|
|
|
|
av_ts2timestr(s->black_end - s->black_start, &s->time_base));
|
2012-06-21 02:15:52 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-30 17:04:28 +02:00
|
|
|
static int black_counter(AVFilterContext *ctx, void *arg,
|
|
|
|
int jobnr, int nb_jobs)
|
2012-03-02 17:51:26 +03:00
|
|
|
{
|
2021-01-29 15:14:14 +02:00
|
|
|
BlackDetectContext *s = ctx->priv;
|
2021-01-30 17:04:28 +02:00
|
|
|
const unsigned int threshold = s->pixel_black_th_i;
|
|
|
|
unsigned int *counterp = &s->counter[jobnr];
|
|
|
|
AVFrame *in = arg;
|
|
|
|
const int linesize = in->linesize[0];
|
|
|
|
const int w = in->width;
|
|
|
|
const int h = in->height;
|
|
|
|
const int start = (h * jobnr) / nb_jobs;
|
|
|
|
const int end = (h * (jobnr+1)) / nb_jobs;
|
|
|
|
const int size = end - start;
|
|
|
|
unsigned int counter = 0;
|
2012-03-02 17:51:26 +03:00
|
|
|
|
2021-01-29 15:34:36 +02:00
|
|
|
if (s->depth == 8) {
|
2021-01-30 17:04:28 +02:00
|
|
|
const uint8_t *p = in->data[0] + start * linesize;
|
2021-01-29 15:34:36 +02:00
|
|
|
|
2021-01-30 17:04:28 +02:00
|
|
|
for (int i = 0; i < size; i++) {
|
|
|
|
for (int x = 0; x < w; x++)
|
|
|
|
counter += p[x] <= threshold;
|
|
|
|
p += linesize;
|
2021-01-29 15:34:36 +02:00
|
|
|
}
|
|
|
|
} else {
|
2021-01-30 17:04:28 +02:00
|
|
|
const uint16_t *p = (const uint16_t *)(in->data[0] + start * linesize);
|
2021-01-29 15:34:36 +02:00
|
|
|
|
2021-01-30 17:04:28 +02:00
|
|
|
for (int i = 0; i < size; i++) {
|
|
|
|
for (int x = 0; x < w; x++)
|
|
|
|
counter += p[x] <= threshold;
|
|
|
|
p += linesize / 2;
|
2021-01-29 15:34:36 +02:00
|
|
|
}
|
2012-03-02 17:51:26 +03:00
|
|
|
}
|
|
|
|
|
2021-01-30 17:04:28 +02:00
|
|
|
*counterp = counter;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int filter_frame(AVFilterLink *inlink, AVFrame *picref)
|
|
|
|
{
|
|
|
|
AVFilterContext *ctx = inlink->dst;
|
|
|
|
BlackDetectContext *s = ctx->priv;
|
|
|
|
double picture_black_ratio = 0;
|
|
|
|
|
|
|
|
ctx->internal->execute(ctx, black_counter, picref, NULL,
|
|
|
|
FFMIN(inlink->h, s->nb_threads));
|
|
|
|
|
|
|
|
for (int i = 0; i < s->nb_threads; i++)
|
|
|
|
s->nb_black_pixels += s->counter[i];
|
|
|
|
|
2021-01-29 15:14:14 +02:00
|
|
|
picture_black_ratio = (double)s->nb_black_pixels / (inlink->w * inlink->h);
|
2012-03-02 17:51:26 +03:00
|
|
|
|
|
|
|
av_log(ctx, AV_LOG_DEBUG,
|
2013-04-15 18:42:04 +03:00
|
|
|
"frame:%"PRId64" picture_black_ratio:%f pts:%s t:%s type:%c\n",
|
2016-08-30 15:28:41 +02:00
|
|
|
inlink->frame_count_out, picture_black_ratio,
|
2021-01-29 15:20:12 +02:00
|
|
|
av_ts2str(picref->pts), av_ts2timestr(picref->pts, &s->time_base),
|
2013-03-10 03:30:30 +03:00
|
|
|
av_get_picture_type_char(picref->pict_type));
|
2012-03-02 17:51:26 +03:00
|
|
|
|
2021-01-29 15:14:14 +02:00
|
|
|
if (picture_black_ratio >= s->picture_black_ratio_th) {
|
|
|
|
if (!s->black_started) {
|
2012-03-02 17:51:26 +03:00
|
|
|
/* black starts here */
|
2021-01-29 15:14:14 +02:00
|
|
|
s->black_started = 1;
|
|
|
|
s->black_start = picref->pts;
|
2017-04-22 10:57:18 +02:00
|
|
|
av_dict_set(&picref->metadata, "lavfi.black_start",
|
2021-01-29 15:20:12 +02:00
|
|
|
av_ts2timestr(s->black_start, &s->time_base), 0);
|
2012-03-02 17:51:26 +03:00
|
|
|
}
|
2021-01-29 15:14:14 +02:00
|
|
|
} else if (s->black_started) {
|
2012-03-02 17:51:26 +03:00
|
|
|
/* black ends here */
|
2021-01-29 15:14:14 +02:00
|
|
|
s->black_started = 0;
|
|
|
|
s->black_end = picref->pts;
|
2012-06-21 02:15:52 +03:00
|
|
|
check_black_end(ctx);
|
2017-04-22 10:57:18 +02:00
|
|
|
av_dict_set(&picref->metadata, "lavfi.black_end",
|
2021-01-29 15:20:12 +02:00
|
|
|
av_ts2timestr(s->black_end, &s->time_base), 0);
|
2012-03-02 17:51:26 +03:00
|
|
|
}
|
|
|
|
|
2021-01-29 15:14:14 +02:00
|
|
|
s->last_picref_pts = picref->pts;
|
|
|
|
s->nb_black_pixels = 0;
|
2012-12-02 02:58:59 +03:00
|
|
|
return ff_filter_frame(inlink->dst->outputs[0], picref);
|
2012-03-02 17:51:26 +03:00
|
|
|
}
|
|
|
|
|
2021-01-29 15:20:12 +02:00
|
|
|
static av_cold void uninit(AVFilterContext *ctx)
|
|
|
|
{
|
|
|
|
BlackDetectContext *s = ctx->priv;
|
|
|
|
|
2021-01-30 17:04:28 +02:00
|
|
|
av_freep(&s->counter);
|
|
|
|
|
2021-01-29 15:20:12 +02:00
|
|
|
if (s->black_started) {
|
|
|
|
// FIXME: black_end should be set to last_picref_pts + last_picref_duration
|
|
|
|
s->black_end = s->last_picref_pts;
|
|
|
|
check_black_end(ctx);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-28 22:01:59 +03:00
|
|
|
static const AVFilterPad blackdetect_inputs[] = {
|
|
|
|
{
|
2013-09-07 15:13:50 +03:00
|
|
|
.name = "default",
|
|
|
|
.type = AVMEDIA_TYPE_VIDEO,
|
|
|
|
.config_props = config_input,
|
|
|
|
.filter_frame = filter_frame,
|
2012-11-28 22:01:59 +03:00
|
|
|
},
|
|
|
|
{ NULL }
|
|
|
|
};
|
|
|
|
|
|
|
|
static const AVFilterPad blackdetect_outputs[] = {
|
|
|
|
{
|
|
|
|
.name = "default",
|
|
|
|
.type = AVMEDIA_TYPE_VIDEO,
|
|
|
|
},
|
|
|
|
{ NULL }
|
|
|
|
};
|
|
|
|
|
2021-04-19 18:33:56 +02:00
|
|
|
const AVFilter ff_vf_blackdetect = {
|
2012-03-02 17:51:26 +03:00
|
|
|
.name = "blackdetect",
|
|
|
|
.description = NULL_IF_CONFIG_SMALL("Detect video intervals that are (almost) black."),
|
|
|
|
.priv_size = sizeof(BlackDetectContext),
|
|
|
|
.query_formats = query_formats,
|
2012-11-28 22:01:59 +03:00
|
|
|
.inputs = blackdetect_inputs,
|
|
|
|
.outputs = blackdetect_outputs,
|
2021-01-29 15:20:12 +02:00
|
|
|
.uninit = uninit,
|
2012-11-28 22:01:59 +03:00
|
|
|
.priv_class = &blackdetect_class,
|
2021-01-30 17:04:28 +02:00
|
|
|
.flags = AVFILTER_FLAG_SLICE_THREADS,
|
2012-03-02 17:51:26 +03:00
|
|
|
};
|