FFmpeg
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 
40 typedef 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 
49 typedef struct ThreadData {
50  const uint8_t *data;
51  int linesize;
52  int bthresh;
53  int width;
54  int height;
56 } ThreadData;
57 
58 static const enum AVPixelFormat pix_fmts[] = {
62 };
63 
64 #define SET_META(key, format, value) \
65  snprintf(buf, sizeof(buf), format, value); \
66  av_dict_set(metadata, key, buf, 0)
67 
68 static int blackframe_slice(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
69 {
70  ThreadData *td = arg;
71  int slice_start = (td->height * jobnr) / nb_jobs;
72  int slice_end = (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 
108  ff_filter_execute(ctx, blackframe_slice, &td, NULL, nb_jobs);
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
132 static 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 
142 AVFILTER_DEFINE_CLASS(blackframe);
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 };
AVPixelFormat
AVPixelFormat
Pixel format.
Definition: pixfmt.h:71
opt.h
FILTER_PIXFMTS_ARRAY
#define FILTER_PIXFMTS_ARRAY(array)
Definition: filters.h:243
ff_filter_frame
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:1067
inlink
The exact code depends on how similar the blocks are and how related they are to the and needs to apply these operations to the correct inlink or outlink if there are several Macros are available to factor that when no extra processing is inlink
Definition: filter_design.txt:212
ThreadData::s
BlackFrameContext * s
Definition: vf_blackframe.c:55
FILTER_INPUTS
#define FILTER_INPUTS(array)
Definition: filters.h:263
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:427
AVOption
AVOption.
Definition: opt.h:429
AVDictionary
Definition: dict.c:32
AVFilter::name
const char * name
Filter name.
Definition: avfilter.h:220
filter_frame
static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
Definition: vf_blackframe.c:88
video.h
ThreadData::width
int width
Definition: vf_avgblur.c:65
blackframe_slice
static int blackframe_slice(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
Definition: vf_blackframe.c:68
pix_fmts
static enum AVPixelFormat pix_fmts[]
Definition: vf_blackframe.c:58
slice_end
static int slice_end(AVCodecContext *avctx, AVFrame *pict, int *got_output)
Handle slice ends.
Definition: mpeg12dec.c:1693
AVFilterPad
A filter pad used for either input or output.
Definition: filters.h:39
AV_FRAME_FLAG_KEY
#define AV_FRAME_FLAG_KEY
A flag to mark frames that are keyframes.
Definition: frame.h:642
ff_video_default_filterpad
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
FFFilter
Definition: filters.h:266
s
#define s(width, name)
Definition: cbs_vp9.c:198
ThreadData::height
int height
Definition: vf_avgblur.c:64
av_q2d
static double av_q2d(AVRational a)
Convert an AVRational to a double.
Definition: rational.h:104
ThreadData::linesize
int linesize
Definition: vf_avgblur.c:68
filters.h
ctx
AVFormatContext * ctx
Definition: movenc.c:49
AV_PIX_FMT_YUV420P
@ AV_PIX_FMT_YUV420P
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition: pixfmt.h:73
FILTER_OUTPUTS
#define FILTER_OUTPUTS(array)
Definition: filters.h:264
arg
const char * arg
Definition: jacosubdec.c:65
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:76
metadata
Stream codec metadata
Definition: ogg-flac-chained-meta.txt:2
SET_META
#define SET_META(key, format, value)
Definition: vf_blackframe.c:64
NULL
#define NULL
Definition: coverity.c:32
avfilter_vf_blackframe_inputs
static const AVFilterPad avfilter_vf_blackframe_inputs[]
Definition: vf_blackframe.c:144
AV_PIX_FMT_GRAY8
@ AV_PIX_FMT_GRAY8
Y , 8bpp.
Definition: pixfmt.h:81
atomic_load_explicit
#define atomic_load_explicit(object, order)
Definition: stdatomic.h:96
BlackFrameContext::bthresh
int bthresh
black threshold
Definition: vf_blackframe.c:43
NULL_IF_CONFIG_SMALL
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:94
ThreadData::bthresh
int bthresh
Definition: vf_blackframe.c:52
atomic_fetch_add_explicit
#define atomic_fetch_add_explicit(object, operand, order)
Definition: stdatomic.h:149
AV_NOPTS_VALUE
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition: avutil.h:247
BlackFrameContext::bamount
int bamount
black amount
Definition: vf_blackframe.c:42
ff_vf_blackframe
const FFFilter ff_vf_blackframe
Definition: vf_blackframe.c:152
AV_LOG_INFO
#define AV_LOG_INFO
Standard information.
Definition: log.h:221
FLAGS
#define FLAGS
Definition: vf_blackframe.c:131
av_get_picture_type_char
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
internal.h
blackframe_options
static const AVOption blackframe_options[]
Definition: vf_blackframe.c:132
ff_filter_get_nb_threads
int ff_filter_get_nb_threads(AVFilterContext *ctx)
Get number of threads for current filter instance.
Definition: avfilter.c:845
ThreadData
Used for passing data between threads.
Definition: dsddec.c:71
BlackFrameContext::frame
unsigned int frame
frame number
Definition: vf_blackframe.c:44
FFMIN
#define FFMIN(a, b)
Definition: macros.h:49
AV_PIX_FMT_NV21
@ AV_PIX_FMT_NV21
as above, but U and V bytes are swapped
Definition: pixfmt.h:97
AVFilterPad::name
const char * name
Pad name.
Definition: filters.h:45
OFFSET
#define OFFSET(x)
Definition: vf_blackframe.c:130
slice_start
static int slice_start(SliceContext *sc, VVCContext *s, VVCFrameContext *fc, const CodedBitstreamUnit *unit, const int is_first_slice)
Definition: dec.c:844
AV_PIX_FMT_NV12
@ 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
frame
these buffered frames must be flushed immediately if a new input produces new the filter must not call request_frame to get more It must just process the frame or queue it The task of requesting more frames is left to the filter s request_frame method or the application If a filter has several the filter must be ready for frames arriving randomly on any input any filter with several inputs will most likely require some kind of queuing mechanism It is perfectly acceptable to have a limited queue and to drop frames when the inputs are too unbalanced request_frame For filters that do not use the this method is called when a frame is wanted on an output For a it should directly call filter_frame on the corresponding output For a if there are queued frames already one of these frames should be pushed If the filter should request a frame on one of its repeatedly until at least one frame has been pushed Return or at least make progress towards producing a frame
Definition: filter_design.txt:265
BlackFrameContext::nblack
atomic_uint nblack
number of black pixels counted so far
Definition: vf_blackframe.c:45
ff_filter_execute
int ff_filter_execute(AVFilterContext *ctx, avfilter_action_func *func, void *arg, int *ret, int nb_jobs)
Definition: avfilter.c:1693
AV_PIX_FMT_NONE
@ AV_PIX_FMT_NONE
Definition: pixfmt.h:72
AV_OPT_TYPE_INT
@ AV_OPT_TYPE_INT
Underlying C type is int.
Definition: opt.h:259
avfilter.h
AVFILTER_FLAG_METADATA_ONLY
#define AVFILTER_FLAG_METADATA_ONLY
The filter is a "metadata" filter - it does not modify the frame data in any way.
Definition: avfilter.h:183
atomic_uint
intptr_t atomic_uint
Definition: stdatomic.h:56
Windows::Graphics::DirectX::Direct3D11::p
IDirect3DDxgiInterfaceAccess _COM_Outptr_ void ** p
Definition: vsrc_gfxcapture_winrt.hpp:53
AV_PIX_FMT_YUV444P
@ AV_PIX_FMT_YUV444P
planar YUV 4:4:4, 24bpp, (1 Cr & Cb sample per 1x1 Y samples)
Definition: pixfmt.h:78
AVFilterContext
An instance of a filter.
Definition: avfilter.h:274
ThreadData::s
const void ** s
Definition: af_crystalizer.c:49
AVFILTER_FLAG_SLICE_THREADS
#define AVFILTER_FLAG_SLICE_THREADS
The filter supports multithreading by splitting frames into multiple parts and processing them concur...
Definition: avfilter.h:167
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:200
FFFilter::p
AVFilter p
The public AVFilter.
Definition: filters.h:270
AV_PIX_FMT_YUV422P
@ 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_YUV411P
@ 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_YUV410P
@ AV_PIX_FMT_YUV410P
planar YUV 4:1:0, 9bpp, (1 Cr & Cb sample per 4x4 Y samples)
Definition: pixfmt.h:79
BlackFrameContext::last_keyframe
unsigned int last_keyframe
frame number of the last received key-frame
Definition: vf_blackframe.c:46
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
atomic_init
#define atomic_init(obj, value)
Definition: stdatomic.h:33
BlackFrameContext
Definition: vf_blackframe.c:40
ThreadData::data
const uint8_t * data
Definition: vf_blackframe.c:50
AVFILTER_DEFINE_CLASS
AVFILTER_DEFINE_CLASS(blackframe)