2 * Copyright (c) 2011 Stefano Sabatini
4 * This file is part of FFmpeg.
6 * FFmpeg is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * FFmpeg is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with FFmpeg; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 * filter for selecting which frame passes in the filterchain
26 #include "libavutil/eval.h"
27 #include "libavutil/fifo.h"
28 #include "libavutil/internal.h"
35 #include "libavcodec/dsputil.h"
38 static const char *const var_names[] = {
41 "pts", ///< original pts in the file of the frame
42 "start_pts", ///< first PTS in the stream, expressed in TB units
43 "prev_pts", ///< previous frame PTS
44 "prev_selected_pts", ///< previous selected frame PTS
46 "t", ///< first PTS in seconds
47 "start_t", ///< first PTS in the stream, expressed in seconds
48 "prev_t", ///< previous frame time
49 "prev_selected_t", ///< previously selected time
51 "pict_type", ///< the type of picture in the movie
60 "interlace_type", ///< the frame interlace type
65 "n", ///< frame number (starting from zero)
66 "selected_n", ///< selected frame number (starting from zero)
67 "prev_selected_n", ///< number of the last selected frame
69 "key", ///< tell if the frame is a key frame
70 "pos", ///< original position in the file of the frame
83 VAR_PREV_SELECTED_PTS,
100 VAR_INTERLACE_TYPE_P,
101 VAR_INTERLACE_TYPE_T,
102 VAR_INTERLACE_TYPE_B,
120 double var_values[VAR_VARS_NB];
121 int do_scene_detect; ///< 1 if the expression requires scene detection variables, 0 otherwise
123 AVCodecContext *avctx; ///< codec context required for the DSPContext (scene detect only)
124 DSPContext c; ///< context providing optimized SAD methods (scene detect only)
125 double prev_mafd; ///< previous MAFD (scene detect only)
127 AVFilterBufferRef *prev_picref; ///< previous frame (scene detect only)
130 AVFifoBuffer *pending_frames; ///< FIFO buffer of video frames
133 static av_cold int init(AVFilterContext *ctx, const char *args)
135 SelectContext *select = ctx->priv;
138 if ((ret = av_expr_parse(&select->expr, args ? args : "1",
139 var_names, NULL, NULL, NULL, NULL, 0, ctx)) < 0) {
140 av_log(ctx, AV_LOG_ERROR, "Error while parsing expression '%s'\n", args);
144 select->pending_frames = av_fifo_alloc(FIFO_SIZE*sizeof(AVFilterBufferRef*));
145 if (!select->pending_frames) {
146 av_log(ctx, AV_LOG_ERROR, "Failed to allocate pending frames buffer.\n");
147 return AVERROR(ENOMEM);
150 select->do_scene_detect = args && strstr(args, "scene");
151 if (select->do_scene_detect && !CONFIG_AVCODEC) {
152 av_log(ctx, AV_LOG_ERROR, "Scene detection is not available without libavcodec.\n");
153 return AVERROR(EINVAL);
158 #define INTERLACE_TYPE_P 0
159 #define INTERLACE_TYPE_T 1
160 #define INTERLACE_TYPE_B 2
162 static int config_input(AVFilterLink *inlink)
164 SelectContext *select = inlink->dst->priv;
166 select->var_values[VAR_N] = 0.0;
167 select->var_values[VAR_SELECTED_N] = 0.0;
169 select->var_values[VAR_TB] = av_q2d(inlink->time_base);
171 select->var_values[VAR_PREV_PTS] = NAN;
172 select->var_values[VAR_PREV_SELECTED_PTS] = NAN;
173 select->var_values[VAR_PREV_SELECTED_T] = NAN;
174 select->var_values[VAR_START_PTS] = NAN;
175 select->var_values[VAR_START_T] = NAN;
177 select->var_values[VAR_PICT_TYPE_I] = AV_PICTURE_TYPE_I;
178 select->var_values[VAR_PICT_TYPE_P] = AV_PICTURE_TYPE_P;
179 select->var_values[VAR_PICT_TYPE_B] = AV_PICTURE_TYPE_B;
180 select->var_values[VAR_PICT_TYPE_SI] = AV_PICTURE_TYPE_SI;
181 select->var_values[VAR_PICT_TYPE_SP] = AV_PICTURE_TYPE_SP;
183 select->var_values[VAR_INTERLACE_TYPE_P] = INTERLACE_TYPE_P;
184 select->var_values[VAR_INTERLACE_TYPE_T] = INTERLACE_TYPE_T;
185 select->var_values[VAR_INTERLACE_TYPE_B] = INTERLACE_TYPE_B;
187 if (CONFIG_AVCODEC && select->do_scene_detect) {
188 select->avctx = avcodec_alloc_context3(NULL);
190 return AVERROR(ENOMEM);
191 dsputil_init(&select->c, select->avctx);
197 static double get_scene_score(AVFilterContext *ctx, AVFilterBufferRef *picref)
200 SelectContext *select = ctx->priv;
201 AVFilterBufferRef *prev_picref = select->prev_picref;
204 picref->video->h == prev_picref->video->h &&
205 picref->video->w == prev_picref->video->w &&
206 picref->linesize[0] == prev_picref->linesize[0]) {
207 int x, y, nb_sad = 0;
210 uint8_t *p1 = picref->data[0];
211 uint8_t *p2 = prev_picref->data[0];
212 const int linesize = picref->linesize[0];
214 for (y = 0; y < picref->video->h - 8; y += 8) {
215 for (x = 0; x < picref->video->w*3 - 8; x += 8) {
216 sad += select->c.sad[1](select, p1 + x, p2 + x,
224 mafd = nb_sad ? sad / nb_sad : 0;
225 diff = fabs(mafd - select->prev_mafd);
226 ret = av_clipf(FFMIN(mafd, diff) / 100., 0, 1);
227 select->prev_mafd = mafd;
228 avfilter_unref_buffer(prev_picref);
230 select->prev_picref = avfilter_ref_buffer(picref, ~0);
235 #define D2TS(d) (isnan(d) ? AV_NOPTS_VALUE : (int64_t)(d))
236 #define TS2D(ts) ((ts) == AV_NOPTS_VALUE ? NAN : (double)(ts))
238 static int select_frame(AVFilterContext *ctx, AVFilterBufferRef *picref)
240 SelectContext *select = ctx->priv;
241 AVFilterLink *inlink = ctx->inputs[0];
244 if (CONFIG_AVCODEC && select->do_scene_detect) {
246 select->var_values[VAR_SCENE] = get_scene_score(ctx, picref);
247 // TODO: document metadata
248 snprintf(buf, sizeof(buf), "%f", select->var_values[VAR_SCENE]);
249 av_dict_set(&picref->metadata, "lavfi.scene_score", buf, 0);
251 if (isnan(select->var_values[VAR_START_PTS]))
252 select->var_values[VAR_START_PTS] = TS2D(picref->pts);
253 if (isnan(select->var_values[VAR_START_T]))
254 select->var_values[VAR_START_T] = TS2D(picref->pts) * av_q2d(inlink->time_base);
256 select->var_values[VAR_PTS] = TS2D(picref->pts);
257 select->var_values[VAR_T ] = TS2D(picref->pts) * av_q2d(inlink->time_base);
258 select->var_values[VAR_POS] = picref->pos == -1 ? NAN : picref->pos;
259 select->var_values[VAR_PREV_PTS] = TS2D(picref ->pts);
261 select->var_values[VAR_INTERLACE_TYPE] =
262 !picref->video->interlaced ? INTERLACE_TYPE_P :
263 picref->video->top_field_first ? INTERLACE_TYPE_T : INTERLACE_TYPE_B;
264 select->var_values[VAR_PICT_TYPE] = picref->video->pict_type;
266 res = av_expr_eval(select->expr, select->var_values, NULL);
267 av_log(inlink->dst, AV_LOG_DEBUG,
268 "n:%d pts:%d t:%f pos:%d interlace_type:%c key:%d pict_type:%c "
270 (int)select->var_values[VAR_N],
271 (int)select->var_values[VAR_PTS],
272 select->var_values[VAR_T],
273 (int)select->var_values[VAR_POS],
274 select->var_values[VAR_INTERLACE_TYPE] == INTERLACE_TYPE_P ? 'P' :
275 select->var_values[VAR_INTERLACE_TYPE] == INTERLACE_TYPE_T ? 'T' :
276 select->var_values[VAR_INTERLACE_TYPE] == INTERLACE_TYPE_B ? 'B' : '?',
277 (int)select->var_values[VAR_KEY],
278 av_get_picture_type_char(select->var_values[VAR_PICT_TYPE]),
281 select->var_values[VAR_N] += 1.0;
284 select->var_values[VAR_PREV_SELECTED_N] = select->var_values[VAR_N];
285 select->var_values[VAR_PREV_SELECTED_PTS] = select->var_values[VAR_PTS];
286 select->var_values[VAR_PREV_SELECTED_T] = select->var_values[VAR_T];
287 select->var_values[VAR_SELECTED_N] += 1.0;
292 static int start_frame(AVFilterLink *inlink, AVFilterBufferRef *picref)
294 SelectContext *select = inlink->dst->priv;
296 select->select = select_frame(inlink->dst, picref);
297 if (select->select) {
298 AVFilterBufferRef *buf_out;
299 /* frame was requested through poll_frame */
300 if (select->cache_frames) {
301 if (!av_fifo_space(select->pending_frames))
302 av_log(inlink->dst, AV_LOG_ERROR,
303 "Buffering limit reached, cannot cache more frames\n");
305 av_fifo_generic_write(select->pending_frames, &picref,
306 sizeof(picref), NULL);
309 buf_out = avfilter_ref_buffer(picref, ~0);
311 return AVERROR(ENOMEM);
312 return ff_start_frame(inlink->dst->outputs[0], buf_out);
318 static int draw_slice(AVFilterLink *inlink, int y, int h, int slice_dir)
320 SelectContext *select = inlink->dst->priv;
322 if (select->select && !select->cache_frames)
323 return ff_draw_slice(inlink->dst->outputs[0], y, h, slice_dir);
327 static int end_frame(AVFilterLink *inlink)
329 SelectContext *select = inlink->dst->priv;
331 if (select->select) {
332 if (select->cache_frames)
334 return ff_end_frame(inlink->dst->outputs[0]);
339 static int request_frame(AVFilterLink *outlink)
341 AVFilterContext *ctx = outlink->src;
342 SelectContext *select = ctx->priv;
343 AVFilterLink *inlink = outlink->src->inputs[0];
346 if (av_fifo_size(select->pending_frames)) {
347 AVFilterBufferRef *picref;
350 av_fifo_generic_read(select->pending_frames, &picref, sizeof(picref), NULL);
351 if ((ret = ff_start_frame(outlink, picref)) < 0 ||
352 (ret = ff_draw_slice(outlink, 0, outlink->h, 1)) < 0 ||
353 (ret = ff_end_frame(outlink)) < 0);
358 while (!select->select) {
359 int ret = ff_request_frame(inlink);
367 static int poll_frame(AVFilterLink *outlink)
369 SelectContext *select = outlink->src->priv;
370 AVFilterLink *inlink = outlink->src->inputs[0];
373 if (!av_fifo_size(select->pending_frames)) {
374 if ((count = ff_poll_frame(inlink)) <= 0)
376 /* request frame from input, and apply select condition to it */
377 select->cache_frames = 1;
378 while (count-- && av_fifo_space(select->pending_frames)) {
379 ret = ff_request_frame(inlink);
383 select->cache_frames = 0;
386 return av_fifo_size(select->pending_frames)/sizeof(AVFilterBufferRef *);
389 static av_cold void uninit(AVFilterContext *ctx)
391 SelectContext *select = ctx->priv;
392 AVFilterBufferRef *picref;
394 av_expr_free(select->expr);
397 while (select->pending_frames &&
398 av_fifo_generic_read(select->pending_frames, &picref, sizeof(picref), NULL) == sizeof(picref))
399 avfilter_unref_buffer(picref);
400 av_fifo_free(select->pending_frames);
401 select->pending_frames = NULL;
403 if (select->do_scene_detect) {
404 avfilter_unref_bufferp(&select->prev_picref);
406 avcodec_close(select->avctx);
407 av_freep(&select->avctx);
412 static int query_formats(AVFilterContext *ctx)
414 SelectContext *select = ctx->priv;
416 if (!select->do_scene_detect) {
417 return ff_default_query_formats(ctx);
419 static const enum AVPixelFormat pix_fmts[] = {
420 AV_PIX_FMT_RGB24, AV_PIX_FMT_BGR24,
423 ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
428 static const AVFilterPad avfilter_vf_select_inputs[] = {
431 .type = AVMEDIA_TYPE_VIDEO,
432 .get_video_buffer = ff_null_get_video_buffer,
433 .min_perms = AV_PERM_PRESERVE,
434 .config_props = config_input,
435 .start_frame = start_frame,
436 .draw_slice = draw_slice,
437 .end_frame = end_frame
442 static const AVFilterPad avfilter_vf_select_outputs[] = {
445 .type = AVMEDIA_TYPE_VIDEO,
446 .poll_frame = poll_frame,
447 .request_frame = request_frame,
452 AVFilter avfilter_vf_select = {
454 .description = NULL_IF_CONFIG_SMALL("Select frames to pass in output."),
457 .query_formats = query_formats,
459 .priv_size = sizeof(SelectContext),
461 .inputs = avfilter_vf_select_inputs,
462 .outputs = avfilter_vf_select_outputs,