]> git.sesse.net Git - ffmpeg/blob - libavfilter/vf_fps.c
lavfi: add showwaves filter
[ffmpeg] / libavfilter / vf_fps.c
1 /*
2  * Copyright 2007 Bobby Bingham
3  * Copyright 2012 Robert Nagy <ronag89 gmail com>
4  * Copyright 2012 Anton Khirnov <anton khirnov net>
5  *
6  * This file is part of FFmpeg.
7  *
8  * FFmpeg is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * FFmpeg is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with FFmpeg; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21  */
22
23 /**
24  * @file
25  * a filter enforcing given constant framerate
26  */
27
28 #include "libavutil/fifo.h"
29 #include "libavutil/mathematics.h"
30 #include "libavutil/opt.h"
31 #include "libavutil/parseutils.h"
32
33 #include "avfilter.h"
34 #include "internal.h"
35 #include "video.h"
36
37 typedef struct FPSContext {
38     const AVClass *class;
39
40     AVFifoBuffer *fifo;     ///< store frames until we get two successive timestamps
41
42     /* timestamps in input timebase */
43     int64_t first_pts;      ///< pts of the first frame that arrived on this filter
44     int64_t pts;            ///< pts of the first frame currently in the fifo
45
46     AVRational framerate;   ///< target framerate
47     char *fps;              ///< a string describing target framerate
48
49     /* statistics */
50     int frames_in;             ///< number of frames on input
51     int frames_out;            ///< number of frames on output
52     int dup;                   ///< number of frames duplicated
53     int drop;                  ///< number of framed dropped
54 } FPSContext;
55
56 #define OFFSET(x) offsetof(FPSContext, x)
57 #define V AV_OPT_FLAG_VIDEO_PARAM
58 static const AVOption options[] = {
59     { "fps", "A string describing desired output framerate", OFFSET(fps), AV_OPT_TYPE_STRING, { .str = "25" }, .flags = V },
60     { NULL },
61 };
62
63 static const AVClass class = {
64     .class_name = "fps",
65     .item_name  = av_default_item_name,
66     .option     = options,
67     .version    = LIBAVUTIL_VERSION_INT,
68     .category   = AV_CLASS_CATEGORY_FILTER,
69 };
70
71 static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque)
72 {
73     FPSContext *s = ctx->priv;
74     int ret;
75
76     s->class = &class;
77     av_opt_set_defaults(s);
78
79     if ((ret = av_set_options_string(s, args, "=", ":")) < 0) {
80         av_log(ctx, AV_LOG_ERROR, "Error parsing the options string %s.\n",
81                args);
82         return ret;
83     }
84
85     if ((ret = av_parse_video_rate(&s->framerate, s->fps)) < 0) {
86         av_log(ctx, AV_LOG_ERROR, "Error parsing framerate %s.\n", s->fps);
87         return ret;
88     }
89     av_opt_free(s);
90
91     if (!(s->fifo = av_fifo_alloc(2*sizeof(AVFilterBufferRef*))))
92         return AVERROR(ENOMEM);
93
94     av_log(ctx, AV_LOG_VERBOSE, "fps=%d/%d\n", s->framerate.num, s->framerate.den);
95     return 0;
96 }
97
98 static void flush_fifo(AVFifoBuffer *fifo)
99 {
100     while (av_fifo_size(fifo)) {
101         AVFilterBufferRef *tmp;
102         av_fifo_generic_read(fifo, &tmp, sizeof(tmp), NULL);
103         avfilter_unref_buffer(tmp);
104     }
105 }
106
107 static av_cold void uninit(AVFilterContext *ctx)
108 {
109     FPSContext *s = ctx->priv;
110     if (s->fifo) {
111         flush_fifo(s->fifo);
112         av_fifo_free(s->fifo);
113     }
114
115     av_log(ctx, AV_LOG_VERBOSE, "%d frames in, %d frames out; %d frames dropped, "
116            "%d frames duplicated.\n", s->frames_in, s->frames_out, s->drop, s->dup);
117 }
118
119 static int config_props(AVFilterLink* link)
120 {
121     FPSContext   *s = link->src->priv;
122
123     link->time_base = (AVRational){ s->framerate.den, s->framerate.num };
124     link->frame_rate= s->framerate;
125     link->w         = link->src->inputs[0]->w;
126     link->h         = link->src->inputs[0]->h;
127     s->pts          = AV_NOPTS_VALUE;
128
129     return 0;
130 }
131
132 static int request_frame(AVFilterLink *outlink)
133 {
134     AVFilterContext *ctx = outlink->src;
135     FPSContext        *s = ctx->priv;
136     int frames_out = s->frames_out;
137     int ret = 0;
138
139     while (ret >= 0 && s->frames_out == frames_out)
140         ret = ff_request_frame(ctx->inputs[0]);
141
142     /* flush the fifo */
143     if (ret == AVERROR_EOF && av_fifo_size(s->fifo)) {
144         int i;
145         for (i = 0; av_fifo_size(s->fifo); i++) {
146             AVFilterBufferRef *buf;
147
148             av_fifo_generic_read(s->fifo, &buf, sizeof(buf), NULL);
149             buf->pts = av_rescale_q(s->first_pts, ctx->inputs[0]->time_base,
150                                     outlink->time_base) + s->frames_out;
151
152             ff_start_frame(outlink, buf);
153             ff_draw_slice(outlink, 0, outlink->h, 1);
154             ff_end_frame(outlink);
155             s->frames_out++;
156         }
157         return 0;
158     }
159
160     return ret;
161 }
162
163 static int write_to_fifo(AVFifoBuffer *fifo, AVFilterBufferRef *buf)
164 {
165     int ret;
166
167     if (!av_fifo_space(fifo) &&
168         (ret = av_fifo_realloc2(fifo, 2*av_fifo_size(fifo))))
169         return ret;
170
171     av_fifo_generic_write(fifo, &buf, sizeof(buf), NULL);
172     return 0;
173 }
174
175 static void end_frame(AVFilterLink *inlink)
176 {
177     AVFilterContext    *ctx = inlink->dst;
178     FPSContext           *s = ctx->priv;
179     AVFilterLink   *outlink = ctx->outputs[0];
180     AVFilterBufferRef  *buf = inlink->cur_buf;
181     int64_t delta;
182     int i;
183
184     s->frames_in++;
185     /* discard frames until we get the first timestamp */
186     if (s->pts == AV_NOPTS_VALUE) {
187         if (buf->pts != AV_NOPTS_VALUE) {
188             write_to_fifo(s->fifo, buf);
189             s->first_pts = s->pts = buf->pts;
190         } else {
191             av_log(ctx, AV_LOG_WARNING, "Discarding initial frame(s) with no "
192                    "timestamp.\n");
193             avfilter_unref_buffer(buf);
194             s->drop++;
195         }
196         return;
197     }
198
199     /* now wait for the next timestamp */
200     if (buf->pts == AV_NOPTS_VALUE) {
201         write_to_fifo(s->fifo, buf);
202         return;
203     }
204
205     /* number of output frames */
206     delta = av_rescale_q(buf->pts - s->pts, inlink->time_base,
207                          outlink->time_base);
208
209     if (delta < 1) {
210         /* drop the frame and everything buffered except the first */
211         AVFilterBufferRef *tmp;
212         int drop = av_fifo_size(s->fifo)/sizeof(AVFilterBufferRef*);
213
214         av_log(ctx, AV_LOG_DEBUG, "Dropping %d frame(s).\n", drop);
215         s->drop += drop;
216
217         av_fifo_generic_read(s->fifo, &tmp, sizeof(tmp), NULL);
218         flush_fifo(s->fifo);
219         write_to_fifo(s->fifo, tmp);
220
221         avfilter_unref_buffer(buf);
222         return;
223     }
224
225     /* can output >= 1 frames */
226     for (i = 0; i < delta; i++) {
227         AVFilterBufferRef *buf_out;
228         av_fifo_generic_read(s->fifo, &buf_out, sizeof(buf_out), NULL);
229
230         /* duplicate the frame if needed */
231         if (!av_fifo_size(s->fifo) && i < delta - 1) {
232             av_log(ctx, AV_LOG_DEBUG, "Duplicating frame.\n");
233             write_to_fifo(s->fifo, avfilter_ref_buffer(buf_out, AV_PERM_READ));
234             s->dup++;
235         }
236
237         buf_out->pts = av_rescale_q(s->first_pts, inlink->time_base,
238                                     outlink->time_base) + s->frames_out;
239
240         ff_start_frame(outlink, buf_out);
241         ff_draw_slice(outlink, 0, outlink->h, 1);
242         ff_end_frame(outlink);
243         s->frames_out++;
244     }
245     flush_fifo(s->fifo);
246
247     write_to_fifo(s->fifo, buf);
248     s->pts = s->first_pts + av_rescale_q(s->frames_out, outlink->time_base, inlink->time_base);
249 }
250
251 static void null_start_frame(AVFilterLink *link, AVFilterBufferRef *buf)
252 {
253 }
254
255 static void null_draw_slice(AVFilterLink *link, int y, int h, int slice_dir)
256 {
257 }
258
259 AVFilter avfilter_vf_fps = {
260     .name        = "fps",
261     .description = NULL_IF_CONFIG_SMALL("Force constant framerate"),
262
263     .init      = init,
264     .uninit    = uninit,
265
266     .priv_size = sizeof(FPSContext),
267
268     .inputs    = (AVFilterPad[]) {{ .name            = "default",
269                                     .type            = AVMEDIA_TYPE_VIDEO,
270                                     .start_frame     = null_start_frame,
271                                     .draw_slice      = null_draw_slice,
272                                     .end_frame       = end_frame, },
273                                   { .name = NULL}},
274     .outputs   = (AVFilterPad[]) {{ .name            = "default",
275                                     .type            = AVMEDIA_TYPE_VIDEO,
276                                     .request_frame   = request_frame,
277                                     .config_props    = config_props},
278                                   { .name = NULL}},
279 };