Originally committed as revision 25447 to svn://svn.ffmpeg.org/ffmpeg/trunk
Stefano Sabatini authored on 2010/10/12 18:22:32... | ... |
@@ -190,6 +190,41 @@ crop=in_w-100:in_h-100:100:100 |
190 | 190 |
"crop=in_w/2:in_h/2:y:10+10*sin(n/10)" |
191 | 191 |
@end example |
192 | 192 |
|
193 |
+@section cropdetect |
|
194 |
+ |
|
195 |
+Auto-detect crop size. |
|
196 |
+ |
|
197 |
+Calculate necessary cropping parameters and prints the recommended |
|
198 |
+parameters through the logging system. The detected dimensions |
|
199 |
+correspond to the non-black area of the input video. |
|
200 |
+ |
|
201 |
+It accepts the syntax: |
|
202 |
+@example |
|
203 |
+cropdetect[=@var{limit}:@var{round}[:@var{reset}]] |
|
204 |
+@end example |
|
205 |
+ |
|
206 |
+@table @option |
|
207 |
+ |
|
208 |
+@item limit |
|
209 |
+Threshold, which can be optionally specified from nothing (0) to |
|
210 |
+everything (255), defaults to 24. |
|
211 |
+ |
|
212 |
+@item round |
|
213 |
+Value which the width/height should be divisible by, defaults to |
|
214 |
+16. The offset is automatically adjusted to center the video. Use 2 to |
|
215 |
+get only even dimensions (needed for 4:2:2 video). 16 is best when |
|
216 |
+encoding to most video codecs. |
|
217 |
+ |
|
218 |
+@item reset |
|
219 |
+Counter that determines after how many frames cropdetect will reset |
|
220 |
+the previously detected largest video area and start over to detect |
|
221 |
+the current optimal crop area. Defaults to 0. |
|
222 |
+ |
|
223 |
+This can be useful when channel logos distort the video area. 0 |
|
224 |
+indicates never reset and return the largest area encountered during |
|
225 |
+playback. |
|
226 |
+@end table |
|
227 |
+ |
|
193 | 228 |
@section drawbox |
194 | 229 |
|
195 | 230 |
Draw a colored box on the input image. |
... | ... |
@@ -23,6 +23,7 @@ OBJS-$(CONFIG_ANULLSINK_FILTER) += asink_anullsink.o |
23 | 23 |
OBJS-$(CONFIG_ASPECT_FILTER) += vf_aspect.o |
24 | 24 |
OBJS-$(CONFIG_BLACKFRAME_FILTER) += vf_blackframe.o |
25 | 25 |
OBJS-$(CONFIG_CROP_FILTER) += vf_crop.o |
26 |
+OBJS-$(CONFIG_CROPDETECT_FILTER) += vf_cropdetect.o |
|
26 | 27 |
OBJS-$(CONFIG_DRAWBOX_FILTER) += vf_drawbox.o |
27 | 28 |
OBJS-$(CONFIG_FIFO_FILTER) += vf_fifo.o |
28 | 29 |
OBJS-$(CONFIG_FORMAT_FILTER) += vf_format.o |
... | ... |
@@ -43,6 +43,7 @@ void avfilter_register_all(void) |
43 | 43 |
REGISTER_FILTER (ASPECT, aspect, vf); |
44 | 44 |
REGISTER_FILTER (BLACKFRAME, blackframe, vf); |
45 | 45 |
REGISTER_FILTER (CROP, crop, vf); |
46 |
+ REGISTER_FILTER (CROPDETECT, cropdetect, vf); |
|
46 | 47 |
REGISTER_FILTER (DRAWBOX, drawbox, vf); |
47 | 48 |
REGISTER_FILTER (FIFO, fifo, vf); |
48 | 49 |
REGISTER_FILTER (FORMAT, format, vf); |
... | ... |
@@ -25,8 +25,8 @@ |
25 | 25 |
#include "libavutil/avutil.h" |
26 | 26 |
|
27 | 27 |
#define LIBAVFILTER_VERSION_MAJOR 1 |
28 |
-#define LIBAVFILTER_VERSION_MINOR 50 |
|
29 |
-#define LIBAVFILTER_VERSION_MICRO 1 |
|
28 |
+#define LIBAVFILTER_VERSION_MINOR 51 |
|
29 |
+#define LIBAVFILTER_VERSION_MICRO 0 |
|
30 | 30 |
|
31 | 31 |
#define LIBAVFILTER_VERSION_INT AV_VERSION_INT(LIBAVFILTER_VERSION_MAJOR, \ |
32 | 32 |
LIBAVFILTER_VERSION_MINOR, \ |
33 | 33 |
new file mode 100644 |
... | ... |
@@ -0,0 +1,214 @@ |
0 |
+/* |
|
1 |
+ * Copyright (C) 2002 A'rpi |
|
2 |
+ * This file is part of FFmpeg. |
|
3 |
+ * |
|
4 |
+ * FFmpeg is free software; you can redistribute it and/or modify |
|
5 |
+ * it under the terms of the GNU General Public License as published by |
|
6 |
+ * the Free Software Foundation; either version 2 of the License, or |
|
7 |
+ * (at your option) any later version. |
|
8 |
+ * |
|
9 |
+ * FFmpeg is distributed in the hope that it will be useful, |
|
10 |
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 |
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
12 |
+ * GNU General Public License for more details. |
|
13 |
+ * |
|
14 |
+ * You should have received a copy of the GNU General Public License along |
|
15 |
+ * with FFmpeg; if not, write to the Free Software Foundation, Inc., |
|
16 |
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
|
17 |
+ */ |
|
18 |
+ |
|
19 |
+/** |
|
20 |
+ * @file |
|
21 |
+ * border detection filter |
|
22 |
+ * Ported from MPlayer libmpcodecs/vf_cropdetect.c. |
|
23 |
+ */ |
|
24 |
+ |
|
25 |
+#include "libavcore/imgutils.h" |
|
26 |
+#include "avfilter.h" |
|
27 |
+ |
|
28 |
+typedef struct { |
|
29 |
+ int x1, y1, x2, y2; |
|
30 |
+ int limit; |
|
31 |
+ int round; |
|
32 |
+ int reset_count; |
|
33 |
+ int frame_nb; |
|
34 |
+ int max_pixsteps[4]; |
|
35 |
+} CropDetectContext; |
|
36 |
+ |
|
37 |
+static int query_formats(AVFilterContext *ctx) |
|
38 |
+{ |
|
39 |
+ static const enum PixelFormat pix_fmts[] = { |
|
40 |
+ PIX_FMT_YUV420P, PIX_FMT_YUVJ420P, |
|
41 |
+ PIX_FMT_YUV422P, PIX_FMT_YUVJ422P, |
|
42 |
+ PIX_FMT_YUV444P, PIX_FMT_YUVJ444P, |
|
43 |
+ PIX_FMT_YUV411P, PIX_FMT_GRAY8, |
|
44 |
+ PIX_FMT_NV12, PIX_FMT_NV21, |
|
45 |
+ PIX_FMT_NONE |
|
46 |
+ }; |
|
47 |
+ |
|
48 |
+ avfilter_set_common_formats(ctx, avfilter_make_format_list(pix_fmts)); |
|
49 |
+ return 0; |
|
50 |
+} |
|
51 |
+ |
|
52 |
+static int checkline(void *ctx, const unsigned char *src, int stride, int len, int bpp) |
|
53 |
+{ |
|
54 |
+ int total = 0; |
|
55 |
+ int div = len; |
|
56 |
+ |
|
57 |
+ switch (bpp) { |
|
58 |
+ case 1: |
|
59 |
+ while (--len >= 0) { |
|
60 |
+ total += src[0]; |
|
61 |
+ src += stride; |
|
62 |
+ } |
|
63 |
+ break; |
|
64 |
+ case 3: |
|
65 |
+ case 4: |
|
66 |
+ while (--len >= 0) { |
|
67 |
+ total += src[0] + src[1] + src[2]; |
|
68 |
+ src += stride; |
|
69 |
+ } |
|
70 |
+ div *= 3; |
|
71 |
+ break; |
|
72 |
+ } |
|
73 |
+ total /= div; |
|
74 |
+ |
|
75 |
+ av_log(ctx, AV_LOG_DEBUG, "total:%d\n", total); |
|
76 |
+ return total; |
|
77 |
+} |
|
78 |
+ |
|
79 |
+static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque) |
|
80 |
+{ |
|
81 |
+ CropDetectContext *cd = ctx->priv; |
|
82 |
+ |
|
83 |
+ cd->limit = 24; |
|
84 |
+ cd->round = 0; |
|
85 |
+ cd->reset_count = 0; |
|
86 |
+ cd->frame_nb = -2; |
|
87 |
+ |
|
88 |
+ if (args) |
|
89 |
+ sscanf(args, "%d:%d:%d", &cd->limit, &cd->round, &cd->reset_count); |
|
90 |
+ |
|
91 |
+ av_log(ctx, AV_LOG_INFO, "limit:%d round:%d reset_count:%d\n", |
|
92 |
+ cd->limit, cd->round, cd->reset_count); |
|
93 |
+ |
|
94 |
+ return 0; |
|
95 |
+} |
|
96 |
+ |
|
97 |
+static int config_input(AVFilterLink *inlink) |
|
98 |
+{ |
|
99 |
+ AVFilterContext *ctx = inlink->dst; |
|
100 |
+ CropDetectContext *cd = ctx->priv; |
|
101 |
+ |
|
102 |
+ av_image_fill_max_pixsteps(cd->max_pixsteps, NULL, |
|
103 |
+ &av_pix_fmt_descriptors[inlink->format]); |
|
104 |
+ |
|
105 |
+ cd->x1 = inlink->w - 1; |
|
106 |
+ cd->y1 = inlink->h - 1; |
|
107 |
+ cd->x2 = 0; |
|
108 |
+ cd->y2 = 0; |
|
109 |
+ |
|
110 |
+ return 0; |
|
111 |
+} |
|
112 |
+ |
|
113 |
+static void end_frame(AVFilterLink *inlink) |
|
114 |
+{ |
|
115 |
+ AVFilterContext *ctx = inlink->dst; |
|
116 |
+ CropDetectContext *cd = ctx->priv; |
|
117 |
+ AVFilterBufferRef *picref = inlink->cur_buf; |
|
118 |
+ int bpp = cd->max_pixsteps[0]; |
|
119 |
+ int w, h, x, y, shrink_by; |
|
120 |
+ |
|
121 |
+ // ignore first 2 frames - they may be empty |
|
122 |
+ if (++cd->frame_nb > 0) { |
|
123 |
+ // Reset the crop area every reset_count frames, if reset_count is > 0 |
|
124 |
+ if (cd->reset_count > 0 && cd->frame_nb > cd->reset_count) { |
|
125 |
+ cd->x1 = picref->video->w-1; |
|
126 |
+ cd->y1 = picref->video->h-1; |
|
127 |
+ cd->x2 = 0; |
|
128 |
+ cd->y2 = 0; |
|
129 |
+ cd->frame_nb = 1; |
|
130 |
+ } |
|
131 |
+ |
|
132 |
+ for (y = 0; y < cd->y1; y++) { |
|
133 |
+ if (checkline(ctx, picref->data[0] + picref->linesize[0] * y, bpp, picref->video->w, bpp) > cd->limit) { |
|
134 |
+ cd->y1 = y; |
|
135 |
+ break; |
|
136 |
+ } |
|
137 |
+ } |
|
138 |
+ |
|
139 |
+ for (y = picref->video->h-1; y > cd->y2; y--) { |
|
140 |
+ if (checkline(ctx, picref->data[0] + picref->linesize[0] * y, bpp, picref->video->w, bpp) > cd->limit) { |
|
141 |
+ cd->y2 = y; |
|
142 |
+ break; |
|
143 |
+ } |
|
144 |
+ } |
|
145 |
+ |
|
146 |
+ for (y = 0; y < cd->x1; y++) { |
|
147 |
+ if (checkline(ctx, picref->data[0] + bpp*y, picref->linesize[0], picref->video->h, bpp) > cd->limit) { |
|
148 |
+ cd->x1 = y; |
|
149 |
+ break; |
|
150 |
+ } |
|
151 |
+ } |
|
152 |
+ |
|
153 |
+ for (y = picref->video->w-1; y > cd->x2; y--) { |
|
154 |
+ if (checkline(ctx, picref->data[0] + bpp*y, picref->linesize[0], picref->video->h, bpp) > cd->limit) { |
|
155 |
+ cd->x2 = y; |
|
156 |
+ break; |
|
157 |
+ } |
|
158 |
+ } |
|
159 |
+ |
|
160 |
+ // round x and y (up), important for yuv colorspaces |
|
161 |
+ // make sure they stay rounded! |
|
162 |
+ x = (cd->x1+1) & ~1; |
|
163 |
+ y = (cd->y1+1) & ~1; |
|
164 |
+ |
|
165 |
+ w = cd->x2 - x + 1; |
|
166 |
+ h = cd->y2 - y + 1; |
|
167 |
+ |
|
168 |
+ // w and h must be divisible by 2 as well because of yuv |
|
169 |
+ // colorspace problems. |
|
170 |
+ if (cd->round <= 1) |
|
171 |
+ cd->round = 16; |
|
172 |
+ if (cd->round % 2) |
|
173 |
+ cd->round *= 2; |
|
174 |
+ |
|
175 |
+ shrink_by = w % cd->round; |
|
176 |
+ w -= shrink_by; |
|
177 |
+ x += (shrink_by/2 + 1) & ~1; |
|
178 |
+ |
|
179 |
+ shrink_by = h % cd->round; |
|
180 |
+ h -= shrink_by; |
|
181 |
+ y += (shrink_by/2 + 1) & ~1; |
|
182 |
+ |
|
183 |
+ av_log(ctx, AV_LOG_INFO, |
|
184 |
+ "x1:%d x2:%d y1:%d y2:%d w:%d h:%d x:%d y:%d pos:%"PRId64" pts:%f crop=%d:%d:%d:%d\n", |
|
185 |
+ cd->x1, cd->x2, cd->y1, cd->y2, w, h, x, y, picref->pos, |
|
186 |
+ picref->pts == AV_NOPTS_VALUE ? -1 : (double)picref->pts / AV_TIME_BASE, |
|
187 |
+ w, h, x, y); |
|
188 |
+ } |
|
189 |
+ |
|
190 |
+ avfilter_end_frame(inlink->dst->outputs[0]); |
|
191 |
+} |
|
192 |
+ |
|
193 |
+AVFilter avfilter_vf_cropdetect = { |
|
194 |
+ .name = "cropdetect", |
|
195 |
+ .description = NULL_IF_CONFIG_SMALL("Auto-detect crop size."), |
|
196 |
+ |
|
197 |
+ .priv_size = sizeof(CropDetectContext), |
|
198 |
+ .init = init, |
|
199 |
+ |
|
200 |
+ .query_formats = query_formats, |
|
201 |
+ |
|
202 |
+ .inputs = (AVFilterPad[]) {{ .name = "default", |
|
203 |
+ .type = AVMEDIA_TYPE_VIDEO, |
|
204 |
+ .config_props = config_input, |
|
205 |
+ .get_video_buffer = avfilter_null_get_video_buffer, |
|
206 |
+ .start_frame = avfilter_null_start_frame, |
|
207 |
+ .end_frame = end_frame, }, |
|
208 |
+ { .name = NULL}}, |
|
209 |
+ |
|
210 |
+ .outputs = (AVFilterPad[]) {{ .name = "default", |
|
211 |
+ .type = AVMEDIA_TYPE_VIDEO }, |
|
212 |
+ { .name = NULL}}, |
|
213 |
+}; |