yading@11
|
1 /*
|
yading@11
|
2 * Copyright (c) 2011 Smartjog S.A.S, Clément Bœsch <clement.boesch@smartjog.com>
|
yading@11
|
3 *
|
yading@11
|
4 * This file is part of FFmpeg.
|
yading@11
|
5 *
|
yading@11
|
6 * FFmpeg is free software; you can redistribute it and/or
|
yading@11
|
7 * modify it under the terms of the GNU Lesser General Public
|
yading@11
|
8 * License as published by the Free Software Foundation; either
|
yading@11
|
9 * version 2.1 of the License, or (at your option) any later version.
|
yading@11
|
10 *
|
yading@11
|
11 * FFmpeg is distributed in the hope that it will be useful,
|
yading@11
|
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
yading@11
|
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
yading@11
|
14 * Lesser General Public License for more details.
|
yading@11
|
15 *
|
yading@11
|
16 * You should have received a copy of the GNU Lesser General Public
|
yading@11
|
17 * License along with FFmpeg; if not, write to the Free Software
|
yading@11
|
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
yading@11
|
19 */
|
yading@11
|
20
|
yading@11
|
21 /**
|
yading@11
|
22 * @file
|
yading@11
|
23 * Potential thumbnail lookup filter to reduce the risk of an inappropriate
|
yading@11
|
24 * selection (such as a black frame) we could get with an absolute seek.
|
yading@11
|
25 *
|
yading@11
|
26 * Simplified version of algorithm by Vadim Zaliva <lord@crocodile.org>.
|
yading@11
|
27 * @see http://notbrainsurgery.livejournal.com/29773.html
|
yading@11
|
28 */
|
yading@11
|
29
|
yading@11
|
30 #include "libavutil/opt.h"
|
yading@11
|
31 #include "avfilter.h"
|
yading@11
|
32 #include "internal.h"
|
yading@11
|
33
|
yading@11
|
34 #define HIST_SIZE (3*256)
|
yading@11
|
35
|
yading@11
|
36 struct thumb_frame {
|
yading@11
|
37 AVFrame *buf; ///< cached frame
|
yading@11
|
38 int histogram[HIST_SIZE]; ///< RGB color distribution histogram of the frame
|
yading@11
|
39 };
|
yading@11
|
40
|
yading@11
|
41 typedef struct {
|
yading@11
|
42 const AVClass *class;
|
yading@11
|
43 int n; ///< current frame
|
yading@11
|
44 int n_frames; ///< number of frames for analysis
|
yading@11
|
45 struct thumb_frame *frames; ///< the n_frames frames
|
yading@11
|
46 AVRational tb; ///< copy of the input timebase to ease access
|
yading@11
|
47 } ThumbContext;
|
yading@11
|
48
|
yading@11
|
49 #define OFFSET(x) offsetof(ThumbContext, x)
|
yading@11
|
50 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
|
yading@11
|
51
|
yading@11
|
52 static const AVOption thumbnail_options[] = {
|
yading@11
|
53 { "n", "set the frames batch size", OFFSET(n_frames), AV_OPT_TYPE_INT, {.i64=100}, 2, INT_MAX, FLAGS },
|
yading@11
|
54 { NULL }
|
yading@11
|
55 };
|
yading@11
|
56
|
yading@11
|
57 AVFILTER_DEFINE_CLASS(thumbnail);
|
yading@11
|
58
|
yading@11
|
59 static av_cold int init(AVFilterContext *ctx)
|
yading@11
|
60 {
|
yading@11
|
61 ThumbContext *thumb = ctx->priv;
|
yading@11
|
62
|
yading@11
|
63 thumb->frames = av_calloc(thumb->n_frames, sizeof(*thumb->frames));
|
yading@11
|
64 if (!thumb->frames) {
|
yading@11
|
65 av_log(ctx, AV_LOG_ERROR,
|
yading@11
|
66 "Allocation failure, try to lower the number of frames\n");
|
yading@11
|
67 return AVERROR(ENOMEM);
|
yading@11
|
68 }
|
yading@11
|
69 av_log(ctx, AV_LOG_VERBOSE, "batch size: %d frames\n", thumb->n_frames);
|
yading@11
|
70 return 0;
|
yading@11
|
71 }
|
yading@11
|
72
|
yading@11
|
73 /**
|
yading@11
|
74 * @brief Compute Sum-square deviation to estimate "closeness".
|
yading@11
|
75 * @param hist color distribution histogram
|
yading@11
|
76 * @param median average color distribution histogram
|
yading@11
|
77 * @return sum of squared errors
|
yading@11
|
78 */
|
yading@11
|
79 static double frame_sum_square_err(const int *hist, const double *median)
|
yading@11
|
80 {
|
yading@11
|
81 int i;
|
yading@11
|
82 double err, sum_sq_err = 0;
|
yading@11
|
83
|
yading@11
|
84 for (i = 0; i < HIST_SIZE; i++) {
|
yading@11
|
85 err = median[i] - (double)hist[i];
|
yading@11
|
86 sum_sq_err += err*err;
|
yading@11
|
87 }
|
yading@11
|
88 return sum_sq_err;
|
yading@11
|
89 }
|
yading@11
|
90
|
yading@11
|
91 static AVFrame *get_best_frame(AVFilterContext *ctx)
|
yading@11
|
92 {
|
yading@11
|
93 AVFrame *picref;
|
yading@11
|
94 ThumbContext *thumb = ctx->priv;
|
yading@11
|
95 int i, j, best_frame_idx = 0;
|
yading@11
|
96 int nb_frames = thumb->n;
|
yading@11
|
97 double avg_hist[HIST_SIZE] = {0}, sq_err, min_sq_err = -1;
|
yading@11
|
98
|
yading@11
|
99 // average histogram of the N frames
|
yading@11
|
100 for (j = 0; j < FF_ARRAY_ELEMS(avg_hist); j++) {
|
yading@11
|
101 for (i = 0; i < nb_frames; i++)
|
yading@11
|
102 avg_hist[j] += (double)thumb->frames[i].histogram[j];
|
yading@11
|
103 avg_hist[j] /= nb_frames;
|
yading@11
|
104 }
|
yading@11
|
105
|
yading@11
|
106 // find the frame closer to the average using the sum of squared errors
|
yading@11
|
107 for (i = 0; i < nb_frames; i++) {
|
yading@11
|
108 sq_err = frame_sum_square_err(thumb->frames[i].histogram, avg_hist);
|
yading@11
|
109 if (i == 0 || sq_err < min_sq_err)
|
yading@11
|
110 best_frame_idx = i, min_sq_err = sq_err;
|
yading@11
|
111 }
|
yading@11
|
112
|
yading@11
|
113 // free and reset everything (except the best frame buffer)
|
yading@11
|
114 for (i = 0; i < nb_frames; i++) {
|
yading@11
|
115 memset(thumb->frames[i].histogram, 0, sizeof(thumb->frames[i].histogram));
|
yading@11
|
116 if (i != best_frame_idx)
|
yading@11
|
117 av_frame_free(&thumb->frames[i].buf);
|
yading@11
|
118 }
|
yading@11
|
119 thumb->n = 0;
|
yading@11
|
120
|
yading@11
|
121 // raise the chosen one
|
yading@11
|
122 picref = thumb->frames[best_frame_idx].buf;
|
yading@11
|
123 av_log(ctx, AV_LOG_INFO, "frame id #%d (pts_time=%f) selected "
|
yading@11
|
124 "from a set of %d images\n", best_frame_idx,
|
yading@11
|
125 picref->pts * av_q2d(thumb->tb), nb_frames);
|
yading@11
|
126 thumb->frames[best_frame_idx].buf = NULL;
|
yading@11
|
127
|
yading@11
|
128 return picref;
|
yading@11
|
129 }
|
yading@11
|
130
|
yading@11
|
131 static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
|
yading@11
|
132 {
|
yading@11
|
133 int i, j;
|
yading@11
|
134 AVFilterContext *ctx = inlink->dst;
|
yading@11
|
135 ThumbContext *thumb = ctx->priv;
|
yading@11
|
136 AVFilterLink *outlink = ctx->outputs[0];
|
yading@11
|
137 int *hist = thumb->frames[thumb->n].histogram;
|
yading@11
|
138 const uint8_t *p = frame->data[0];
|
yading@11
|
139
|
yading@11
|
140 // keep a reference of each frame
|
yading@11
|
141 thumb->frames[thumb->n].buf = frame;
|
yading@11
|
142
|
yading@11
|
143 // update current frame RGB histogram
|
yading@11
|
144 for (j = 0; j < inlink->h; j++) {
|
yading@11
|
145 for (i = 0; i < inlink->w; i++) {
|
yading@11
|
146 hist[0*256 + p[i*3 ]]++;
|
yading@11
|
147 hist[1*256 + p[i*3 + 1]]++;
|
yading@11
|
148 hist[2*256 + p[i*3 + 2]]++;
|
yading@11
|
149 }
|
yading@11
|
150 p += frame->linesize[0];
|
yading@11
|
151 }
|
yading@11
|
152
|
yading@11
|
153 // no selection until the buffer of N frames is filled up
|
yading@11
|
154 thumb->n++;
|
yading@11
|
155 if (thumb->n < thumb->n_frames)
|
yading@11
|
156 return 0;
|
yading@11
|
157
|
yading@11
|
158 return ff_filter_frame(outlink, get_best_frame(ctx));
|
yading@11
|
159 }
|
yading@11
|
160
|
yading@11
|
161 static av_cold void uninit(AVFilterContext *ctx)
|
yading@11
|
162 {
|
yading@11
|
163 int i;
|
yading@11
|
164 ThumbContext *thumb = ctx->priv;
|
yading@11
|
165 for (i = 0; i < thumb->n_frames && thumb->frames[i].buf; i++)
|
yading@11
|
166 av_frame_free(&thumb->frames[i].buf);
|
yading@11
|
167 av_freep(&thumb->frames);
|
yading@11
|
168 }
|
yading@11
|
169
|
yading@11
|
170 static int request_frame(AVFilterLink *link)
|
yading@11
|
171 {
|
yading@11
|
172 AVFilterContext *ctx = link->src;
|
yading@11
|
173 ThumbContext *thumb = ctx->priv;
|
yading@11
|
174
|
yading@11
|
175 /* loop until a frame thumbnail is available (when a frame is queued,
|
yading@11
|
176 * thumb->n is reset to zero) */
|
yading@11
|
177 do {
|
yading@11
|
178 int ret = ff_request_frame(ctx->inputs[0]);
|
yading@11
|
179 if (ret == AVERROR_EOF && thumb->n) {
|
yading@11
|
180 ret = ff_filter_frame(link, get_best_frame(ctx));
|
yading@11
|
181 if (ret < 0)
|
yading@11
|
182 return ret;
|
yading@11
|
183 ret = AVERROR_EOF;
|
yading@11
|
184 }
|
yading@11
|
185 if (ret < 0)
|
yading@11
|
186 return ret;
|
yading@11
|
187 } while (thumb->n);
|
yading@11
|
188 return 0;
|
yading@11
|
189 }
|
yading@11
|
190
|
yading@11
|
191 static int config_props(AVFilterLink *inlink)
|
yading@11
|
192 {
|
yading@11
|
193 AVFilterContext *ctx = inlink->dst;
|
yading@11
|
194 ThumbContext *thumb = ctx->priv;
|
yading@11
|
195
|
yading@11
|
196 thumb->tb = inlink->time_base;
|
yading@11
|
197 return 0;
|
yading@11
|
198 }
|
yading@11
|
199
|
yading@11
|
200 static int query_formats(AVFilterContext *ctx)
|
yading@11
|
201 {
|
yading@11
|
202 static const enum AVPixelFormat pix_fmts[] = {
|
yading@11
|
203 AV_PIX_FMT_RGB24, AV_PIX_FMT_BGR24,
|
yading@11
|
204 AV_PIX_FMT_NONE
|
yading@11
|
205 };
|
yading@11
|
206 ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
|
yading@11
|
207 return 0;
|
yading@11
|
208 }
|
yading@11
|
209
|
yading@11
|
210 static const AVFilterPad thumbnail_inputs[] = {
|
yading@11
|
211 {
|
yading@11
|
212 .name = "default",
|
yading@11
|
213 .type = AVMEDIA_TYPE_VIDEO,
|
yading@11
|
214 .config_props = config_props,
|
yading@11
|
215 .get_video_buffer = ff_null_get_video_buffer,
|
yading@11
|
216 .filter_frame = filter_frame,
|
yading@11
|
217 },
|
yading@11
|
218 { NULL }
|
yading@11
|
219 };
|
yading@11
|
220
|
yading@11
|
221 static const AVFilterPad thumbnail_outputs[] = {
|
yading@11
|
222 {
|
yading@11
|
223 .name = "default",
|
yading@11
|
224 .type = AVMEDIA_TYPE_VIDEO,
|
yading@11
|
225 .request_frame = request_frame,
|
yading@11
|
226 },
|
yading@11
|
227 { NULL }
|
yading@11
|
228 };
|
yading@11
|
229
|
yading@11
|
230 AVFilter avfilter_vf_thumbnail = {
|
yading@11
|
231 .name = "thumbnail",
|
yading@11
|
232 .description = NULL_IF_CONFIG_SMALL("Select the most representative frame in a given sequence of consecutive frames."),
|
yading@11
|
233 .priv_size = sizeof(ThumbContext),
|
yading@11
|
234 .init = init,
|
yading@11
|
235 .uninit = uninit,
|
yading@11
|
236 .query_formats = query_formats,
|
yading@11
|
237 .inputs = thumbnail_inputs,
|
yading@11
|
238 .outputs = thumbnail_outputs,
|
yading@11
|
239 .priv_class = &thumbnail_class,
|
yading@11
|
240 };
|