]> git.sesse.net Git - ffmpeg/blob - libavfilter/vf_framestep.c
Merge commit '7c5012127fb7e18f0616011257bb4248f6a8b608'
[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;
39
40     framestep->frame_step = 1;
41
42     if (args) {
43         n = strtol(args, &tailptr, 10);
44         if (*tailptr || n <= 0 || n >= INT_MAX) {
45             av_log(ctx, AV_LOG_ERROR,
46                    "Invalid argument '%s', must be a positive integer <= INT_MAX\n", args);
47             return AVERROR(EINVAL);
48         }
49     }
50
51     framestep->frame_step = n;
52     return 0;
53 }
54
55 static int config_output_props(AVFilterLink *outlink)
56 {
57     AVFilterContext *ctx = outlink->src;
58     FrameStepContext *framestep = ctx->priv;
59     AVFilterLink *inlink = ctx->inputs[0];
60
61     outlink->frame_rate =
62         av_div_q(inlink->frame_rate, (AVRational){framestep->frame_step, 1});
63
64     av_log(ctx, AV_LOG_VERBOSE, "step:%d frame_rate:%d/%d(%f) -> frame_rate:%d/%d(%f)\n",
65            framestep->frame_step,
66            inlink->frame_rate.num, inlink->frame_rate.den, av_q2d(inlink->frame_rate),
67            outlink->frame_rate.num, outlink->frame_rate.den, av_q2d(outlink->frame_rate));
68     return 0;
69 }
70
71 static int start_frame(AVFilterLink *inlink, AVFilterBufferRef *ref)
72 {
73     FrameStepContext *framestep = inlink->dst->priv;
74
75     framestep->frame_selected = 0;
76     if (!(framestep->frame_count++ % framestep->frame_step)) {
77         inlink->cur_buf = NULL;
78         framestep->frame_selected = 1;
79         return ff_start_frame(inlink->dst->outputs[0], ref);
80     }
81     return 0;
82 }
83
84 static int draw_slice(AVFilterLink *inlink, int y, int h, int slice_dir)
85 {
86     FrameStepContext *framestep = inlink->dst->priv;
87
88     if (framestep->frame_selected)
89         return ff_draw_slice(inlink->dst->outputs[0], y, h, slice_dir);
90     return 0;
91 }
92
93 static int end_frame(AVFilterLink *inlink)
94 {
95     FrameStepContext *framestep = inlink->dst->priv;
96
97     if (framestep->frame_selected)
98         return ff_end_frame(inlink->dst->outputs[0]);
99     return 0;
100 }
101
102 static int request_frame(AVFilterLink *outlink)
103 {
104     FrameStepContext *framestep = outlink->src->priv;
105     AVFilterLink *inlink = outlink->src->inputs[0];
106     int ret;
107
108     framestep->frame_selected = 0;
109     do {
110         ret = ff_request_frame(inlink);
111     } while (!framestep->frame_selected && ret >= 0);
112
113     return ret;
114 }
115
116 AVFilter avfilter_vf_framestep = {
117     .name      = "framestep",
118     .description = NULL_IF_CONFIG_SMALL("Select one frame every N frames."),
119     .init      = init,
120     .priv_size = sizeof(FrameStepContext),
121
122     .inputs = (const AVFilterPad[]) {
123         {
124             .name             = "default",
125             .type             = AVMEDIA_TYPE_VIDEO,
126             .get_video_buffer = ff_null_get_video_buffer,
127             .start_frame      = start_frame,
128             .draw_slice       = draw_slice,
129             .end_frame        = end_frame,
130         },
131         { .name = NULL }
132     },
133     .outputs = (const AVFilterPad[]) {
134         {
135             .name             = "default",
136             .type             = AVMEDIA_TYPE_VIDEO,
137             .config_props     = config_output_props,
138             .request_frame    = request_frame,
139         },
140         { .name = NULL }
141     },
142 };