FFmpeg
Loading...
Searching...
No Matches
vf_blackframe.c
Go to the documentation of this file.
1/*
2 * Copyright (c) 2010 Stefano Sabatini
3 * Copyright (c) 2006 Ivo van Poorten
4 * Copyright (c) 2006 Julian Hall
5 * Copyright (c) 2002-2003 Brian J. Murrell
6 *
7 * This file is part of FFmpeg.
8 *
9 * FFmpeg is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * FFmpeg is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with FFmpeg; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 */
23
24/**
25 * @file
26 * Search for black frames to detect scene transitions.
27 * Ported from MPlayer libmpcodecs/vf_blackframe.c.
28 */
29
30#include <stdio.h>
31#include <inttypes.h>
32#include <stdatomic.h>
33
34#include "libavutil/internal.h"
35#include "libavutil/opt.h"
36#include "avfilter.h"
37#include "filters.h"
38#include "video.h"
39
40typedef struct BlackFrameContext {
41 const AVClass *class;
42 int bamount; ///< black amount
43 int bthresh; ///< black threshold
44 unsigned int frame; ///< frame number
45 atomic_uint nblack; ///< number of black pixels counted so far
46 unsigned int last_keyframe; ///< frame number of the last received key-frame
48
49typedef struct ThreadData {
50 const uint8_t *data;
51 int linesize;
53 int width;
54 int height;
57
63
64#define SET_META(key, format, value) \
65 snprintf(buf, sizeof(buf), format, value); \
66 av_dict_set(metadata, key, buf, 0)
67
68static int blackframe_slice(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
69{
70 ThreadData *td = arg;
71 int slice_start = ff_slice_pos(td->height, jobnr, nb_jobs);
72 int slice_end = ff_slice_pos(td->height, jobnr + 1, nb_jobs);
73 const uint8_t *p;
74 unsigned int black_pixels_count = 0;
75
76 p = td->data + slice_start * td->linesize;
77
78 for (int y = slice_start; y < slice_end; y++) {
79 for (int x = 0; x < td->width; x++)
80 black_pixels_count += p[x] < td->bthresh;
81 p += td->linesize;
82 }
83
84 atomic_fetch_add_explicit(&td->s->nblack, black_pixels_count, memory_order_relaxed);
85 return 0;
86}
87
89{
90 AVFilterContext *ctx = inlink->dst;
91 BlackFrameContext *s = ctx->priv;
92 ThreadData td;
93 int pblack = 0;
94 int nb_threads = ff_filter_get_nb_threads(ctx);
95 int nb_jobs = FFMIN(inlink->h, nb_threads);
97 char buf[32];
98
99 atomic_init(&s->nblack, 0);
100
101 td.data = frame->data[0];
102 td.linesize = frame->linesize[0];
103 td.width = inlink->w;
104 td.height = inlink->h;
105 td.bthresh = s->bthresh;
106 td.s = s;
107
109
110 if (frame->flags & AV_FRAME_FLAG_KEY)
111 s->last_keyframe = s->frame;
112
113 pblack = atomic_load_explicit(&s->nblack, memory_order_relaxed) * 100 / (inlink->w * inlink->h);
114 if (pblack >= s->bamount) {
115 metadata = &frame->metadata;
116
117 av_log(ctx, AV_LOG_INFO, "frame:%u pblack:%u pts:%"PRId64" t:%f "
118 "type:%c last_keyframe:%d\n",
119 s->frame, pblack, frame->pts,
120 frame->pts == AV_NOPTS_VALUE ? -1 : frame->pts * av_q2d(inlink->time_base),
121 av_get_picture_type_char(frame->pict_type), s->last_keyframe);
122
123 SET_META("lavfi.blackframe.pblack", "%u", pblack);
124 }
125
126 s->frame++;
127 return ff_filter_frame(inlink->dst->outputs[0], frame);
128}
129
130#define OFFSET(x) offsetof(BlackFrameContext, x)
131#define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
132static const AVOption blackframe_options[] = {
133 { "amount", "percentage of the pixels that have to be below the threshold "
134 "for the frame to be considered black", OFFSET(bamount), AV_OPT_TYPE_INT, { .i64 = 98 }, 0, 100, FLAGS },
135 { "threshold", "threshold below which a pixel value is considered black",
136 OFFSET(bthresh), AV_OPT_TYPE_INT, { .i64 = 32 }, 0, 255, FLAGS },
137 { "thresh", "threshold below which a pixel value is considered black",
138 OFFSET(bthresh), AV_OPT_TYPE_INT, { .i64 = 32 }, 0, 255, FLAGS },
139 { NULL }
140};
141
143
145 {
146 .name = "default",
147 .type = AVMEDIA_TYPE_VIDEO,
148 .filter_frame = filter_frame,
149 },
150};
151
153 .p.name = "blackframe",
154 .p.description = NULL_IF_CONFIG_SMALL("Detect frames that are (almost) black."),
155 .p.priv_class = &blackframe_class,
157 .priv_size = sizeof(BlackFrameContext),
161};
const FFFilter ff_vf_blackframe
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition avfilter.c:1068
int ff_filter_execute(AVFilterContext *ctx, avfilter_action_func *func, void *arg, int *ret, int nb_jobs)
Definition avfilter.c:1696
int ff_filter_get_nb_threads(AVFilterContext *ctx)
Get number of threads for current filter instance.
Definition avfilter.c:846
Main libavfilter public API header.
static int FUNC metadata(CodedBitstreamContext *ctx, RWContext *rw, APVRawMetadata *current)
#define s(width, name)
Definition cbs_vp9.c:198
#define FLAGS
Definition cmdutils.c:598
#define NULL
Definition coverity.c:32
static AVFrame * frame
static int filter_frame(DBEDecodeContext *s, AVFrame *frame)
Definition dolby_e.c:1067
#define atomic_fetch_add_explicit(object, operand, order)
Definition stdatomic.h:149
#define atomic_load_explicit(object, order)
Definition stdatomic.h:96
intptr_t atomic_uint
Definition stdatomic.h:56
#define atomic_init(obj, value)
Definition stdatomic.h:33
@ AV_OPT_TYPE_INT
Underlying C type is int.
Definition opt.h:258
#define AVFILTER_FLAG_SLICE_THREADS
The filter supports multithreading by splitting frames into multiple parts and processing them concur...
Definition avfilter.h:166
#define AVFILTER_FLAG_METADATA_ONLY
The filter is a "metadata" filter - it does not modify the frame data in any way.
Definition avfilter.h:182
#define AV_FRAME_FLAG_KEY
A flag to mark frames that are keyframes.
Definition frame.h:687
#define AV_LOG_INFO
Standard information.
Definition log.h:221
static double av_q2d(AVRational a)
Convert an AVRational to a double.
Definition rational.h:104
@ AVMEDIA_TYPE_VIDEO
Definition avutil.h:200
char av_get_picture_type_char(enum AVPictureType pict_type)
Return a single letter to describe the given picture type pict_type.
Definition utils.c:40
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition avutil.h:247
const char * arg
Definition jacosubdec.c:65
#define FILTER_INPUTS(array)
Definition filters.h:264
#define FILTER_OUTPUTS(array)
Definition filters.h:265
static int ff_slice_pos(int total, int jobnr, int nb_jobs)
Compute the boundary index for a slice when work of size total is split into nb_jobs slices.
Definition filters.h:763
#define FILTER_PIXFMTS_ARRAY(array)
Definition filters.h:244
#define AVFILTER_DEFINE_CLASS(fname)
Definition filters.h:478
common internal API header
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition internal.h:88
static enum AVPixelFormat pix_fmts[]
Definition libkvazaar.c:296
#define FFMIN(a, b)
Definition macros.h:49
static int slice_end(AVCodecContext *avctx, AVFrame *pict, int *got_output)
Handle slice ends.
Definition mpeg12dec.c:1697
AVOptions.
AVPixelFormat
Pixel format.
Definition pixfmt.h:71
@ AV_PIX_FMT_NV12
planar YUV 4:2:0, 12bpp, 1 plane for Y and 1 plane for the UV components, which are interleaved (firs...
Definition pixfmt.h:96
@ AV_PIX_FMT_NONE
Definition pixfmt.h:72
@ AV_PIX_FMT_YUV420P
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition pixfmt.h:73
@ AV_PIX_FMT_NV21
as above, but U and V bytes are swapped
Definition pixfmt.h:97
@ AV_PIX_FMT_YUV422P
planar YUV 4:2:2, 16bpp, (1 Cr & Cb sample per 2x1 Y samples)
Definition pixfmt.h:77
@ AV_PIX_FMT_GRAY8
Y , 8bpp.
Definition pixfmt.h:81
@ AV_PIX_FMT_YUV410P
planar YUV 4:1:0, 9bpp, (1 Cr & Cb sample per 4x4 Y samples)
Definition pixfmt.h:79
@ AV_PIX_FMT_YUV411P
planar YUV 4:1:1, 12bpp, (1 Cr & Cb sample per 4x1 Y samples)
Definition pixfmt.h:80
@ AV_PIX_FMT_YUV444P
planar YUV 4:4:4, 24bpp, (1 Cr & Cb sample per 1x1 Y samples)
Definition pixfmt.h:78
Describe the class of an AVClass context structure.
Definition log.h:76
An instance of a filter.
Definition avfilter.h:273
AVFilterLink ** outputs
array of pointers to output links
Definition avfilter.h:285
A filter pad used for either input or output.
Definition filters.h:40
This structure describes decoded (raw) audio or video data.
Definition frame.h:472
AVOption.
Definition opt.h:428
int bamount
black amount
unsigned int last_keyframe
frame number of the last received key-frame
unsigned int frame
frame number
atomic_uint nblack
number of black pixels counted so far
int bthresh
black threshold
Used for passing data between threads.
Definition dsddec.c:71
const void ** s
const uint8_t * data
#define av_log(a,...)
static AVFormatContext * ctx
Definition movenc.c:49
static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
static const AVFilterPad avfilter_vf_blackframe_inputs[]
#define SET_META(key, format, value)
static const AVOption blackframe_options[]
#define OFFSET(x)
static int blackframe_slice(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
const AVFilterPad ff_video_default_filterpad[1]
An AVFilterPad array whose only entry has name "default" and is of type AVMEDIA_TYPE_VIDEO.
Definition video.c:37
static int slice_start(SliceContext *sc, VVCContext *s, VVCFrameContext *fc, const CodedBitstreamUnit *unit, const int is_first_slice)
Definition dec.c:844