... | ... |
@@ -2400,6 +2400,26 @@ For example: |
2400 | 2400 |
will create two separate outputs from the same input, one cropped and |
2401 | 2401 |
one padded. |
2402 | 2402 |
|
2403 |
+@section thumbnail |
|
2404 |
+Select the most representative frame in a given sequence of consecutive frames. |
|
2405 |
+ |
|
2406 |
+It accepts as argument the frames batch size to analyze (default @var{N}=100); |
|
2407 |
+in a set of @var{N} frames, the filter will pick one of them, and then handle |
|
2408 |
+the next batch of @var{N} frames until the end. |
|
2409 |
+ |
|
2410 |
+Since the filter keeps track of the whole frames sequence, a bigger @var{N} |
|
2411 |
+value will result in a higher memory usage, so a high value is not recommended. |
|
2412 |
+ |
|
2413 |
+The following example extract one picture each 50 frames: |
|
2414 |
+@example |
|
2415 |
+thumbnail=50 |
|
2416 |
+@end example |
|
2417 |
+ |
|
2418 |
+Complete example of a thumbnail creation with @command{ffmpeg}: |
|
2419 |
+@example |
|
2420 |
+ffmpeg -i in.avi -vf thumbnail,scale=300:200 -frames:v 1 out.png |
|
2421 |
+@end example |
|
2422 |
+ |
|
2403 | 2423 |
@section transpose |
2404 | 2424 |
|
2405 | 2425 |
Transpose rows with columns in the input video and optionally flip it. |
... | ... |
@@ -79,6 +79,7 @@ OBJS-$(CONFIG_SETTB_FILTER) += vf_settb.o |
79 | 79 |
OBJS-$(CONFIG_SHOWINFO_FILTER) += vf_showinfo.o |
80 | 80 |
OBJS-$(CONFIG_SLICIFY_FILTER) += vf_slicify.o |
81 | 81 |
OBJS-$(CONFIG_SPLIT_FILTER) += vf_split.o |
82 |
+OBJS-$(CONFIG_THUMBNAIL_FILTER) += vf_thumbnail.o |
|
82 | 83 |
OBJS-$(CONFIG_TRANSPOSE_FILTER) += vf_transpose.o |
83 | 84 |
OBJS-$(CONFIG_UNSHARP_FILTER) += vf_unsharp.o |
84 | 85 |
OBJS-$(CONFIG_VFLIP_FILTER) += vf_vflip.o |
... | ... |
@@ -89,6 +89,7 @@ void avfilter_register_all(void) |
89 | 89 |
REGISTER_FILTER (SHOWINFO, showinfo, vf); |
90 | 90 |
REGISTER_FILTER (SLICIFY, slicify, vf); |
91 | 91 |
REGISTER_FILTER (SPLIT, split, vf); |
92 |
+ REGISTER_FILTER (THUMBNAIL, thumbnail, vf); |
|
92 | 93 |
REGISTER_FILTER (TRANSPOSE, transpose, vf); |
93 | 94 |
REGISTER_FILTER (UNSHARP, unsharp, vf); |
94 | 95 |
REGISTER_FILTER (VFLIP, vflip, vf); |
95 | 96 |
new file mode 100644 |
... | ... |
@@ -0,0 +1,243 @@ |
0 |
+/* |
|
1 |
+ * Copyright (c) 2011 Smartjog S.A.S, Clément Bœsch <clement.boesch@smartjog.com> |
|
2 |
+ * |
|
3 |
+ * This file is part of FFmpeg. |
|
4 |
+ * |
|
5 |
+ * FFmpeg is free software; you can redistribute it and/or |
|
6 |
+ * modify it under the terms of the GNU Lesser General Public |
|
7 |
+ * License as published by the Free Software Foundation; either |
|
8 |
+ * version 2.1 of the License, or (at your option) any later version. |
|
9 |
+ * |
|
10 |
+ * FFmpeg is distributed in the hope that it will be useful, |
|
11 |
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12 |
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
13 |
+ * Lesser General Public License for more details. |
|
14 |
+ * |
|
15 |
+ * You should have received a copy of the GNU Lesser General Public |
|
16 |
+ * License along with FFmpeg; if not, write to the Free Software |
|
17 |
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
|
18 |
+ */ |
|
19 |
+ |
|
20 |
+/** |
|
21 |
+ * @file |
|
22 |
+ * Potential thumbnail lookup filter to reduce the risk of an inappropriate |
|
23 |
+ * selection (such as a black frame) we could get with an absolute seek. |
|
24 |
+ * |
|
25 |
+ * Simplified version of algorithm by Vadim Zaliva <lord@crocodile.org>. |
|
26 |
+ * @url http://notbrainsurgery.livejournal.com/29773.html |
|
27 |
+ */ |
|
28 |
+ |
|
29 |
+#include "avfilter.h" |
|
30 |
+ |
|
31 |
+#define HIST_SIZE (3*256) |
|
32 |
+ |
|
33 |
+struct thumb_frame { |
|
34 |
+ AVFilterBufferRef *buf; ///< cached frame |
|
35 |
+ int histogram[HIST_SIZE]; ///< RGB color distribution histogram of the frame |
|
36 |
+}; |
|
37 |
+ |
|
38 |
+typedef struct { |
|
39 |
+ int n; ///< current frame |
|
40 |
+ int n_frames; ///< number of frames for analysis |
|
41 |
+ struct thumb_frame *frames; ///< the n_frames frames |
|
42 |
+} ThumbContext; |
|
43 |
+ |
|
44 |
+static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque) |
|
45 |
+{ |
|
46 |
+ ThumbContext *thumb = ctx->priv; |
|
47 |
+ |
|
48 |
+ if (!args) { |
|
49 |
+ thumb->n_frames = 100; |
|
50 |
+ } else { |
|
51 |
+ int n = sscanf(args, "%d", &thumb->n_frames); |
|
52 |
+ if (n != 1 || thumb->n_frames < 2) { |
|
53 |
+ thumb->n_frames = 0; |
|
54 |
+ av_log(ctx, AV_LOG_ERROR, |
|
55 |
+ "Invalid number of frames specified (minimum is 2).\n"); |
|
56 |
+ return AVERROR(ENOMEM); |
|
57 |
+ } |
|
58 |
+ } |
|
59 |
+ thumb->frames = av_calloc(thumb->n_frames, sizeof(*thumb->frames)); |
|
60 |
+ if (!thumb->frames) { |
|
61 |
+ av_log(ctx, AV_LOG_ERROR, |
|
62 |
+ "Allocation failure, try to lower the number of frames\n"); |
|
63 |
+ return AVERROR(ENOMEM); |
|
64 |
+ } |
|
65 |
+ av_log(ctx, AV_LOG_INFO, "batch size: %d frames\n", thumb->n_frames); |
|
66 |
+ return 0; |
|
67 |
+} |
|
68 |
+ |
|
69 |
+static void draw_slice(AVFilterLink *inlink, int y, int h, int slice_dir) |
|
70 |
+{ |
|
71 |
+ int i, j; |
|
72 |
+ AVFilterContext *ctx = inlink->dst; |
|
73 |
+ ThumbContext *thumb = ctx->priv; |
|
74 |
+ int *hist = thumb->frames[thumb->n].histogram; |
|
75 |
+ AVFilterBufferRef *picref = inlink->cur_buf; |
|
76 |
+ const uint8_t *p = picref->data[0] + y * picref->linesize[0]; |
|
77 |
+ |
|
78 |
+ // update current frame RGB histogram |
|
79 |
+ for (j = 0; j < h; j++) { |
|
80 |
+ for (i = 0; i < inlink->w; i++) { |
|
81 |
+ hist[0*256 + p[i*3 ]]++; |
|
82 |
+ hist[1*256 + p[i*3 + 1]]++; |
|
83 |
+ hist[2*256 + p[i*3 + 2]]++; |
|
84 |
+ } |
|
85 |
+ p += picref->linesize[0]; |
|
86 |
+ } |
|
87 |
+} |
|
88 |
+ |
|
89 |
+/** |
|
90 |
+ * @brief Compute Sum-square deviation to estimate "closeness". |
|
91 |
+ * @param hist color distribution histogram |
|
92 |
+ * @param median average color distribution histogram |
|
93 |
+ * @return sum of squared errors |
|
94 |
+ */ |
|
95 |
+static double frame_sum_square_err(const int *hist, const double *median) |
|
96 |
+{ |
|
97 |
+ int i; |
|
98 |
+ double err, sum_sq_err = 0; |
|
99 |
+ |
|
100 |
+ for (i = 0; i < HIST_SIZE; i++) { |
|
101 |
+ err = median[i] - (double)hist[i]; |
|
102 |
+ sum_sq_err += err*err; |
|
103 |
+ } |
|
104 |
+ return sum_sq_err; |
|
105 |
+} |
|
106 |
+ |
|
107 |
+static void end_frame(AVFilterLink *inlink) |
|
108 |
+{ |
|
109 |
+ int i, j, best_frame_idx = 0; |
|
110 |
+ double avg_hist[HIST_SIZE] = {0}, sq_err, min_sq_err = -1; |
|
111 |
+ AVFilterLink *outlink = inlink->dst->outputs[0]; |
|
112 |
+ ThumbContext *thumb = inlink->dst->priv; |
|
113 |
+ AVFilterContext *ctx = inlink->dst; |
|
114 |
+ AVFilterBufferRef *picref; |
|
115 |
+ |
|
116 |
+ // keep a reference of each frame |
|
117 |
+ thumb->frames[thumb->n].buf = inlink->cur_buf; |
|
118 |
+ |
|
119 |
+ // no selection until the buffer of N frames is filled up |
|
120 |
+ if (thumb->n < thumb->n_frames - 1) { |
|
121 |
+ thumb->n++; |
|
122 |
+ return; |
|
123 |
+ } |
|
124 |
+ |
|
125 |
+ // average histogram of the N frames |
|
126 |
+ for (j = 0; j < FF_ARRAY_ELEMS(avg_hist); j++) { |
|
127 |
+ for (i = 0; i < thumb->n_frames; i++) |
|
128 |
+ avg_hist[j] += (double)thumb->frames[i].histogram[j]; |
|
129 |
+ avg_hist[j] /= thumb->n_frames; |
|
130 |
+ } |
|
131 |
+ |
|
132 |
+ // find the frame closer to the average using the sum of squared errors |
|
133 |
+ for (i = 0; i < thumb->n_frames; i++) { |
|
134 |
+ sq_err = frame_sum_square_err(thumb->frames[i].histogram, avg_hist); |
|
135 |
+ if (i == 0 || sq_err < min_sq_err) |
|
136 |
+ best_frame_idx = i, min_sq_err = sq_err; |
|
137 |
+ } |
|
138 |
+ |
|
139 |
+ // free and reset everything (except the best frame buffer) |
|
140 |
+ for (i = 0; i < thumb->n_frames; i++) { |
|
141 |
+ memset(thumb->frames[i].histogram, 0, sizeof(thumb->frames[i].histogram)); |
|
142 |
+ if (i == best_frame_idx) |
|
143 |
+ continue; |
|
144 |
+ avfilter_unref_buffer(thumb->frames[i].buf); |
|
145 |
+ thumb->frames[i].buf = NULL; |
|
146 |
+ } |
|
147 |
+ thumb->n = 0; |
|
148 |
+ |
|
149 |
+ // raise the chosen one |
|
150 |
+ picref = thumb->frames[best_frame_idx].buf; |
|
151 |
+ av_log(ctx, AV_LOG_INFO, "frame id #%d (pts_time=%f) selected\n", |
|
152 |
+ best_frame_idx, picref->pts * av_q2d(inlink->time_base)); |
|
153 |
+ avfilter_start_frame(outlink, picref); |
|
154 |
+ thumb->frames[best_frame_idx].buf = NULL; |
|
155 |
+ avfilter_draw_slice(outlink, 0, inlink->h, 1); |
|
156 |
+ avfilter_end_frame(outlink); |
|
157 |
+} |
|
158 |
+ |
|
159 |
+static av_cold void uninit(AVFilterContext *ctx) |
|
160 |
+{ |
|
161 |
+ int i; |
|
162 |
+ ThumbContext *thumb = ctx->priv; |
|
163 |
+ for (i = 0; i < thumb->n_frames && thumb->frames[i].buf; i++) { |
|
164 |
+ avfilter_unref_buffer(thumb->frames[i].buf); |
|
165 |
+ thumb->frames[i].buf = NULL; |
|
166 |
+ } |
|
167 |
+ av_freep(&thumb->frames); |
|
168 |
+} |
|
169 |
+ |
|
170 |
+static void null_start_frame(AVFilterLink *link, AVFilterBufferRef *picref) { } |
|
171 |
+ |
|
172 |
+static int request_frame(AVFilterLink *link) |
|
173 |
+{ |
|
174 |
+ ThumbContext *thumb = link->src->priv; |
|
175 |
+ |
|
176 |
+ /* loop until a frame thumbnail is available (when a frame is queued, |
|
177 |
+ * thumb->n is reset to zero) */ |
|
178 |
+ while (thumb->n) { |
|
179 |
+ int ret = avfilter_request_frame(link->src->inputs[0]); |
|
180 |
+ if (ret < 0) |
|
181 |
+ return ret; |
|
182 |
+ } |
|
183 |
+ return 0; |
|
184 |
+} |
|
185 |
+ |
|
186 |
+static int poll_frame(AVFilterLink *link) |
|
187 |
+{ |
|
188 |
+ ThumbContext *thumb = link->src->priv; |
|
189 |
+ AVFilterLink *inlink = link->src->inputs[0]; |
|
190 |
+ int ret, available_frames = avfilter_poll_frame(inlink); |
|
191 |
+ |
|
192 |
+ /* If the input link is not able to provide any frame, we can't do anything |
|
193 |
+ * at the moment and thus have zero thumbnail available. */ |
|
194 |
+ if (!available_frames) |
|
195 |
+ return 0; |
|
196 |
+ |
|
197 |
+ /* Since at least one frame is available and the next frame will allow us |
|
198 |
+ * to compute a thumbnail, we can return 1 frame. */ |
|
199 |
+ if (thumb->n == thumb->n_frames - 1) |
|
200 |
+ return 1; |
|
201 |
+ |
|
202 |
+ /* we have some frame(s) available in the input link, but not yet enough to |
|
203 |
+ * output a thumbnail, so we request more */ |
|
204 |
+ ret = avfilter_request_frame(inlink); |
|
205 |
+ return ret < 0 ? ret : 0; |
|
206 |
+} |
|
207 |
+ |
|
208 |
+static int query_formats(AVFilterContext *ctx) |
|
209 |
+{ |
|
210 |
+ static const enum PixelFormat pix_fmts[] = { |
|
211 |
+ PIX_FMT_RGB24, PIX_FMT_BGR24, |
|
212 |
+ PIX_FMT_NONE |
|
213 |
+ }; |
|
214 |
+ avfilter_set_common_pixel_formats(ctx, avfilter_make_format_list(pix_fmts)); |
|
215 |
+ return 0; |
|
216 |
+} |
|
217 |
+ |
|
218 |
+AVFilter avfilter_vf_thumbnail = { |
|
219 |
+ .name = "thumbnail", |
|
220 |
+ .description = NULL_IF_CONFIG_SMALL("Select the most representative frame in a given sequence of consecutive frames."), |
|
221 |
+ .priv_size = sizeof(ThumbContext), |
|
222 |
+ .init = init, |
|
223 |
+ .uninit = uninit, |
|
224 |
+ .query_formats = query_formats, |
|
225 |
+ .inputs = (const AVFilterPad[]) { |
|
226 |
+ { .name = "default", |
|
227 |
+ .type = AVMEDIA_TYPE_VIDEO, |
|
228 |
+ .get_video_buffer = avfilter_null_get_video_buffer, |
|
229 |
+ .start_frame = null_start_frame, |
|
230 |
+ .draw_slice = draw_slice, |
|
231 |
+ .end_frame = end_frame, |
|
232 |
+ },{ .name = NULL } |
|
233 |
+ }, |
|
234 |
+ .outputs = (const AVFilterPad[]) { |
|
235 |
+ { .name = "default", |
|
236 |
+ .type = AVMEDIA_TYPE_VIDEO, |
|
237 |
+ .request_frame = request_frame, |
|
238 |
+ .poll_frame = poll_frame, |
|
239 |
+ .rej_perms = AV_PERM_REUSE2, |
|
240 |
+ },{ .name = NULL } |
|
241 |
+ }, |
|
242 |
+}; |