2 * Copyright (c) 2012 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
22 * @file framestep filter, inspired on libmpcodecs/vf_framestep.c by
23 * Daniele Fornighieri <guru AT digitalfantasy it>.
31 int frame_step, frame_count, frame_selected;
34 static av_cold int init(AVFilterContext *ctx, const char *args)
36 FrameStepContext *framestep = ctx->priv;
41 n = strtol(args, &tailptr, 10);
42 if (*tailptr || n <= 0 || n >= INT_MAX) {
43 av_log(ctx, AV_LOG_ERROR,
44 "Invalid argument '%s', must be a positive integer <= INT_MAX\n", args);
45 return AVERROR(EINVAL);
49 framestep->frame_step = n;
53 static int config_output_props(AVFilterLink *outlink)
55 AVFilterContext *ctx = outlink->src;
56 FrameStepContext *framestep = ctx->priv;
57 AVFilterLink *inlink = ctx->inputs[0];
60 av_div_q(inlink->frame_rate, (AVRational){framestep->frame_step, 1});
62 av_log(ctx, AV_LOG_VERBOSE, "step:%d frame_rate:%d/%d(%f) -> frame_rate:%d/%d(%f)\n",
63 framestep->frame_step,
64 inlink->frame_rate.num, inlink->frame_rate.den, av_q2d(inlink->frame_rate),
65 outlink->frame_rate.num, outlink->frame_rate.den, av_q2d(outlink->frame_rate));
69 static int filter_frame(AVFilterLink *inlink, AVFilterBufferRef *ref)
71 FrameStepContext *framestep = inlink->dst->priv;
73 if (!(framestep->frame_count++ % framestep->frame_step)) {
74 framestep->frame_selected = 1;
75 return ff_filter_frame(inlink->dst->outputs[0], ref);
77 framestep->frame_selected = 0;
78 avfilter_unref_buffer(ref);
83 static int request_frame(AVFilterLink *outlink)
85 FrameStepContext *framestep = outlink->src->priv;
86 AVFilterLink *inlink = outlink->src->inputs[0];
89 framestep->frame_selected = 0;
91 ret = ff_request_frame(inlink);
92 } while (!framestep->frame_selected && ret >= 0);
97 static const AVFilterPad framestep_inputs[] = {
100 .type = AVMEDIA_TYPE_VIDEO,
101 .get_video_buffer = ff_null_get_video_buffer,
102 .filter_frame = filter_frame,
107 static const AVFilterPad framestep_outputs[] = {
110 .type = AVMEDIA_TYPE_VIDEO,
111 .config_props = config_output_props,
112 .request_frame = request_frame,
117 AVFilter avfilter_vf_framestep = {
119 .description = NULL_IF_CONFIG_SMALL("Select one frame every N frames."),
121 .priv_size = sizeof(FrameStepContext),
122 .inputs = framestep_inputs,
123 .outputs = framestep_outputs,