See thread:
Subject: [FFmpeg-devel] [PATCH] Port MPlayer blackframe filter.
Date: Sun, 26 Sep 2010 01:10:40 +0200
Originally committed as revision 25214 to svn://svn.ffmpeg.org/ffmpeg/trunk
... | ... |
@@ -71,6 +71,27 @@ build. |
71 | 71 |
|
72 | 72 |
Below is a description of the currently available video filters. |
73 | 73 |
|
74 |
+@section blackframe |
|
75 |
+ |
|
76 |
+Detect frames that are (almost) completely black. Can be useful to |
|
77 |
+detect chapter transitions or commercials. Output lines consist of |
|
78 |
+the frame number of the detected frame, the percentage of blackness, |
|
79 |
+the position in the file if known or -1 and the timestamp in seconds. |
|
80 |
+ |
|
81 |
+In order to display the output lines, you need to set the loglevel at |
|
82 |
+least to the AV_LOG_INFO value. |
|
83 |
+ |
|
84 |
+The filter accepts the syntax: |
|
85 |
+@example |
|
86 |
+blackframe[=@var{amount}:[@var{threshold}]] |
|
87 |
+@end example |
|
88 |
+ |
|
89 |
+@var{amount} is the percentage of the pixels that have to be below the |
|
90 |
+threshold, and defaults to 98. |
|
91 |
+ |
|
92 |
+@var{threshold} is the threshold below which a pixel value is |
|
93 |
+considered black, and defaults to 32. |
|
94 |
+ |
|
74 | 95 |
@section crop |
75 | 96 |
|
76 | 97 |
Crop the input video to @var{out_w}:@var{out_h}:@var{x}:@var{y}. |
... | ... |
@@ -21,6 +21,7 @@ OBJS-$(CONFIG_ANULLSRC_FILTER) += asrc_anullsrc.o |
21 | 21 |
OBJS-$(CONFIG_ANULLSINK_FILTER) += asink_anullsink.o |
22 | 22 |
|
23 | 23 |
OBJS-$(CONFIG_ASPECT_FILTER) += vf_aspect.o |
24 |
+OBJS-$(CONFIG_BLACKFRAME_FILTER) += vf_blackframe.o |
|
24 | 25 |
OBJS-$(CONFIG_CROP_FILTER) += vf_crop.o |
25 | 26 |
OBJS-$(CONFIG_FIFO_FILTER) += vf_fifo.o |
26 | 27 |
OBJS-$(CONFIG_FORMAT_FILTER) += vf_format.o |
... | ... |
@@ -41,6 +41,7 @@ void avfilter_register_all(void) |
41 | 41 |
REGISTER_FILTER (ANULLSINK, anullsink, asink); |
42 | 42 |
|
43 | 43 |
REGISTER_FILTER (ASPECT, aspect, vf); |
44 |
+ REGISTER_FILTER (BLACKFRAME, blackframe, vf); |
|
44 | 45 |
REGISTER_FILTER (CROP, crop, vf); |
45 | 46 |
REGISTER_FILTER (FIFO, fifo, vf); |
46 | 47 |
REGISTER_FILTER (FORMAT, format, vf); |
... | ... |
@@ -25,7 +25,7 @@ |
25 | 25 |
#include "libavutil/avutil.h" |
26 | 26 |
|
27 | 27 |
#define LIBAVFILTER_VERSION_MAJOR 1 |
28 |
-#define LIBAVFILTER_VERSION_MINOR 45 |
|
28 |
+#define LIBAVFILTER_VERSION_MINOR 46 |
|
29 | 29 |
#define LIBAVFILTER_VERSION_MICRO 0 |
30 | 30 |
|
31 | 31 |
#define LIBAVFILTER_VERSION_INT AV_VERSION_INT(LIBAVFILTER_VERSION_MAJOR, \ |
32 | 32 |
new file mode 100644 |
... | ... |
@@ -0,0 +1,129 @@ |
0 |
+/* |
|
1 |
+ * Copyright (C) 2010 Stefano Sabatini |
|
2 |
+ * Copyright (C) 2006 Ivo van Poorten |
|
3 |
+ * Copyright (C) 2006 Julian Hall |
|
4 |
+ * Copyright (C) 2002-2003 Brian J. Murrell |
|
5 |
+ * |
|
6 |
+ * This file is part of FFmpeg. |
|
7 |
+ * |
|
8 |
+ * FFmpeg is free software; you can redistribute it and/or modify |
|
9 |
+ * it under the terms of the GNU General Public License as published by |
|
10 |
+ * the Free Software Foundation; either version 2 of the License, or |
|
11 |
+ * (at your option) any later version. |
|
12 |
+ * |
|
13 |
+ * FFmpeg is distributed in the hope that it will be useful, |
|
14 |
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
15 |
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
16 |
+ * GNU General Public License for more details. |
|
17 |
+ * |
|
18 |
+ * You should have received a copy of the GNU General Public License along |
|
19 |
+ * with FFmpeg; if not, write to the Free Software Foundation, Inc., |
|
20 |
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
|
21 |
+ */ |
|
22 |
+ |
|
23 |
+/** |
|
24 |
+ * @file |
|
25 |
+ * Search for black frames to detect scene transitions. |
|
26 |
+ * Ported from MPlayer libmpcodecs/vf_blackframe.c. |
|
27 |
+ */ |
|
28 |
+ |
|
29 |
+#include "avfilter.h" |
|
30 |
+ |
|
31 |
+typedef struct { |
|
32 |
+ unsigned int bamount; ///< black amount |
|
33 |
+ unsigned int bthresh; ///< black threshold |
|
34 |
+ unsigned int frame; ///< frame number |
|
35 |
+ unsigned int nblack; ///< number of black pixels counted so far |
|
36 |
+} BlackFrameContext; |
|
37 |
+ |
|
38 |
+static int query_formats(AVFilterContext *ctx) |
|
39 |
+{ |
|
40 |
+ static const enum PixelFormat pix_fmts[] = { |
|
41 |
+ PIX_FMT_YUV410P, PIX_FMT_YUV420P, PIX_FMT_GRAY8, PIX_FMT_NV12, |
|
42 |
+ PIX_FMT_NV21, PIX_FMT_YUV444P, PIX_FMT_YUV422P, PIX_FMT_YUV411P, |
|
43 |
+ PIX_FMT_NONE |
|
44 |
+ }; |
|
45 |
+ |
|
46 |
+ avfilter_set_common_formats(ctx, avfilter_make_format_list(pix_fmts)); |
|
47 |
+ return 0; |
|
48 |
+} |
|
49 |
+ |
|
50 |
+static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque) |
|
51 |
+{ |
|
52 |
+ BlackFrameContext *blackframe = ctx->priv; |
|
53 |
+ |
|
54 |
+ blackframe->bamount = 98; |
|
55 |
+ blackframe->bthresh = 32; |
|
56 |
+ blackframe->nblack = 0; |
|
57 |
+ blackframe->frame = 0; |
|
58 |
+ |
|
59 |
+ if (args) |
|
60 |
+ sscanf(args, "%u:%u", &blackframe->bamount, &blackframe->bthresh); |
|
61 |
+ |
|
62 |
+ av_log(ctx, AV_LOG_INFO, "bamount:%u bthresh:%u\n", |
|
63 |
+ blackframe->bamount, blackframe->bthresh); |
|
64 |
+ |
|
65 |
+ if (blackframe->bamount > 100 || blackframe->bthresh > 255) { |
|
66 |
+ av_log(ctx, AV_LOG_ERROR, "Too big value for bamount (max is 100) or bthresh (max is 255)\n"); |
|
67 |
+ return AVERROR(EINVAL); |
|
68 |
+ } |
|
69 |
+ |
|
70 |
+ return 0; |
|
71 |
+} |
|
72 |
+ |
|
73 |
+static void draw_slice(AVFilterLink *inlink, int y, int h, int slice_dir) |
|
74 |
+{ |
|
75 |
+ AVFilterContext *ctx = inlink->dst; |
|
76 |
+ BlackFrameContext *blackframe = ctx->priv; |
|
77 |
+ AVFilterBufferRef *picref = inlink->cur_buf; |
|
78 |
+ int x, i; |
|
79 |
+ uint8_t *p = picref->data[0] + y * picref->linesize[0]; |
|
80 |
+ |
|
81 |
+ for (i = 0; i < h; i++) { |
|
82 |
+ for (x = 0; x < inlink->w; x++) |
|
83 |
+ blackframe->nblack += p[x] < blackframe->bthresh; |
|
84 |
+ p += picref->linesize[0]; |
|
85 |
+ } |
|
86 |
+ |
|
87 |
+ avfilter_draw_slice(ctx->outputs[0], y, h, slice_dir); |
|
88 |
+} |
|
89 |
+ |
|
90 |
+static void end_frame(AVFilterLink *inlink) |
|
91 |
+{ |
|
92 |
+ AVFilterContext *ctx = inlink->dst; |
|
93 |
+ BlackFrameContext *blackframe = ctx->priv; |
|
94 |
+ AVFilterBufferRef *picref = inlink->cur_buf; |
|
95 |
+ int pblack = 0; |
|
96 |
+ |
|
97 |
+ pblack = blackframe->nblack * 100 / (inlink->w * inlink->h); |
|
98 |
+ if (pblack >= blackframe->bamount) |
|
99 |
+ av_log(ctx, AV_LOG_INFO, "frame:%u pblack:%u pos:%"PRId64" pts:%f\n", |
|
100 |
+ blackframe->frame, pblack, picref->pos, |
|
101 |
+ picref->pts == AV_NOPTS_VALUE ? -1 : (double)picref->pts / AV_TIME_BASE); |
|
102 |
+ |
|
103 |
+ blackframe->frame++; |
|
104 |
+ blackframe->nblack = 0; |
|
105 |
+ avfilter_end_frame(inlink->dst->outputs[0]); |
|
106 |
+} |
|
107 |
+ |
|
108 |
+AVFilter avfilter_vf_blackframe = { |
|
109 |
+ .name = "blackframe", |
|
110 |
+ .description = NULL_IF_CONFIG_SMALL("Detect frames that are (almost) black."), |
|
111 |
+ |
|
112 |
+ .priv_size = sizeof(BlackFrameContext), |
|
113 |
+ .init = init, |
|
114 |
+ |
|
115 |
+ .query_formats = query_formats, |
|
116 |
+ |
|
117 |
+ .inputs = (AVFilterPad[]) {{ .name = "default", |
|
118 |
+ .type = AVMEDIA_TYPE_VIDEO, |
|
119 |
+ .draw_slice = draw_slice, |
|
120 |
+ .get_video_buffer = avfilter_null_get_video_buffer, |
|
121 |
+ .start_frame = avfilter_null_start_frame, |
|
122 |
+ .end_frame = end_frame, }, |
|
123 |
+ { .name = NULL}}, |
|
124 |
+ |
|
125 |
+ .outputs = (AVFilterPad[]) {{ .name = "default", |
|
126 |
+ .type = AVMEDIA_TYPE_VIDEO }, |
|
127 |
+ { .name = NULL}}, |
|
128 |
+}; |