]> git.sesse.net Git - ffmpeg/blob - libavfilter/vf_framestep.c
Merge commit '4521645b1aee9e9ad8f5cea7b2392cd5f6ffcd26'
[ffmpeg] / libavfilter / vf_framestep.c
1 /*
2  * Copyright (c) 2012 Stefano Sabatini
3  *
4  * This file is part of FFmpeg.
5  *
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.
10  *
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.
15  *
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
19  */
20
21 /**
22  * @file framestep filter, inspired on libmpcodecs/vf_framestep.c by
23  * Daniele Fornighieri <guru AT digitalfantasy it>.
24  */
25
26 #include "avfilter.h"
27 #include "internal.h"
28 #include "video.h"
29
30 typedef struct {
31     int frame_step, frame_count, frame_selected;
32 } FrameStepContext;
33
34 static av_cold int init(AVFilterContext *ctx, const char *args)
35 {
36     FrameStepContext *framestep = ctx->priv;
37     char *tailptr;
38     long int n = 1;
39
40     if (args) {
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);
46         }
47     }
48
49     framestep->frame_step = n;
50     return 0;
51 }
52
53 static int config_output_props(AVFilterLink *outlink)
54 {
55     AVFilterContext *ctx = outlink->src;
56     FrameStepContext *framestep = ctx->priv;
57     AVFilterLink *inlink = ctx->inputs[0];
58
59     outlink->frame_rate =
60         av_div_q(inlink->frame_rate, (AVRational){framestep->frame_step, 1});
61
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));
66     return 0;
67 }
68
69 static int start_frame(AVFilterLink *inlink, AVFilterBufferRef *ref)
70 {
71     FrameStepContext *framestep = inlink->dst->priv;
72
73     framestep->frame_selected = 0;
74     if (!(framestep->frame_count++ % framestep->frame_step)) {
75         inlink->cur_buf = NULL;
76         framestep->frame_selected = 1;
77         return ff_start_frame(inlink->dst->outputs[0], ref);
78     }
79     return 0;
80 }
81
82 static int draw_slice(AVFilterLink *inlink, int y, int h, int slice_dir)
83 {
84     FrameStepContext *framestep = inlink->dst->priv;
85
86     if (framestep->frame_selected)
87         return ff_draw_slice(inlink->dst->outputs[0], y, h, slice_dir);
88     return 0;
89 }
90
91 static int end_frame(AVFilterLink *inlink)
92 {
93     FrameStepContext *framestep = inlink->dst->priv;
94
95     if (framestep->frame_selected)
96         return ff_end_frame(inlink->dst->outputs[0]);
97     return 0;
98 }
99
100 static int request_frame(AVFilterLink *outlink)
101 {
102     FrameStepContext *framestep = outlink->src->priv;
103     AVFilterLink *inlink = outlink->src->inputs[0];
104     int ret;
105
106     framestep->frame_selected = 0;
107     do {
108         ret = ff_request_frame(inlink);
109     } while (!framestep->frame_selected && ret >= 0);
110
111     return ret;
112 }
113
114 AVFilter avfilter_vf_framestep = {
115     .name      = "framestep",
116     .description = NULL_IF_CONFIG_SMALL("Select one frame every N frames."),
117     .init      = init,
118     .priv_size = sizeof(FrameStepContext),
119
120     .inputs = (const AVFilterPad[]) {
121         {
122             .name             = "default",
123             .type             = AVMEDIA_TYPE_VIDEO,
124             .get_video_buffer = ff_null_get_video_buffer,
125             .start_frame      = start_frame,
126             .draw_slice       = draw_slice,
127             .end_frame        = end_frame,
128         },
129         { .name = NULL }
130     },
131     .outputs = (const AVFilterPad[]) {
132         {
133             .name             = "default",
134             .type             = AVMEDIA_TYPE_VIDEO,
135             .config_props     = config_output_props,
136             .request_frame    = request_frame,
137         },
138         { .name = NULL }
139     },
140 };