vf_fps.c 8.88 KB
Newer Older
Anton Khirnov's avatar
Anton Khirnov committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
/*
 * This file is part of Libav.
 *
 * Libav is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * Libav is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with Libav; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */

/**
 * @file
 * a filter enforcing given constant framerate
 */

24
#include <float.h>
25
#include <stdint.h>
26

27
#include "libavutil/common.h"
Anton Khirnov's avatar
Anton Khirnov committed
28 29 30 31 32 33
#include "libavutil/fifo.h"
#include "libavutil/mathematics.h"
#include "libavutil/opt.h"
#include "libavutil/parseutils.h"

#include "avfilter.h"
34 35
#include "internal.h"
#include "video.h"
Anton Khirnov's avatar
Anton Khirnov committed
36 37 38 39 40 41 42 43 44 45

typedef struct FPSContext {
    const AVClass *class;

    AVFifoBuffer *fifo;     ///< store frames until we get two successive timestamps

    /* timestamps in input timebase */
    int64_t first_pts;      ///< pts of the first frame that arrived on this filter
    int64_t pts;            ///< pts of the first frame currently in the fifo

46 47
    double start_time;      ///< pts, in seconds, of the expected first frame

Anton Khirnov's avatar
Anton Khirnov committed
48 49 50 51 52 53 54 55 56 57 58 59 60 61
    AVRational framerate;   ///< target framerate
    char *fps;              ///< a string describing target framerate

    /* statistics */
    int frames_in;             ///< number of frames on input
    int frames_out;            ///< number of frames on output
    int dup;                   ///< number of frames duplicated
    int drop;                  ///< number of framed dropped
} FPSContext;

#define OFFSET(x) offsetof(FPSContext, x)
#define V AV_OPT_FLAG_VIDEO_PARAM
static const AVOption options[] = {
    { "fps", "A string describing desired output framerate", OFFSET(fps), AV_OPT_TYPE_STRING, { .str = "25" }, .flags = V },
62
    { "start_time", "Assume the first PTS should be this value.", OFFSET(start_time), AV_OPT_TYPE_DOUBLE, { .dbl = DBL_MAX}, -DBL_MAX, DBL_MAX, V },
Anton Khirnov's avatar
Anton Khirnov committed
63 64 65 66 67 68 69 70 71 72
    { NULL },
};

static const AVClass class = {
    .class_name = "FPS filter",
    .item_name  = av_default_item_name,
    .option     = options,
    .version    = LIBAVUTIL_VERSION_INT,
};

73
static av_cold int init(AVFilterContext *ctx)
Anton Khirnov's avatar
Anton Khirnov committed
74 75 76 77 78 79 80 81 82
{
    FPSContext *s = ctx->priv;
    int ret;

    if ((ret = av_parse_video_rate(&s->framerate, s->fps)) < 0) {
        av_log(ctx, AV_LOG_ERROR, "Error parsing framerate %s.\n", s->fps);
        return ret;
    }

83
    if (!(s->fifo = av_fifo_alloc(2*sizeof(AVFrame*))))
Anton Khirnov's avatar
Anton Khirnov committed
84 85
        return AVERROR(ENOMEM);

86
    s->pts          = AV_NOPTS_VALUE;
87
    s->first_pts    = AV_NOPTS_VALUE;
88

Anton Khirnov's avatar
Anton Khirnov committed
89 90 91 92 93 94 95
    av_log(ctx, AV_LOG_VERBOSE, "fps=%d/%d\n", s->framerate.num, s->framerate.den);
    return 0;
}

static void flush_fifo(AVFifoBuffer *fifo)
{
    while (av_fifo_size(fifo)) {
96
        AVFrame *tmp;
Anton Khirnov's avatar
Anton Khirnov committed
97
        av_fifo_generic_read(fifo, &tmp, sizeof(tmp), NULL);
98
        av_frame_free(&tmp);
Anton Khirnov's avatar
Anton Khirnov committed
99 100 101 102 103 104 105
    }
}

static av_cold void uninit(AVFilterContext *ctx)
{
    FPSContext *s = ctx->priv;
    if (s->fifo) {
106
        s->drop += av_fifo_size(s->fifo) / sizeof(AVFilterBufferRef*);
Anton Khirnov's avatar
Anton Khirnov committed
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
        flush_fifo(s->fifo);
        av_fifo_free(s->fifo);
    }

    av_log(ctx, AV_LOG_VERBOSE, "%d frames in, %d frames out; %d frames dropped, "
           "%d frames duplicated.\n", s->frames_in, s->frames_out, s->drop, s->dup);
}

static int config_props(AVFilterLink* link)
{
    FPSContext   *s = link->src->priv;

    link->time_base = (AVRational){ s->framerate.den, s->framerate.num };
    link->w         = link->src->inputs[0]->w;
    link->h         = link->src->inputs[0]->h;

    return 0;
}

static int request_frame(AVFilterLink *outlink)
{
    AVFilterContext *ctx = outlink->src;
    FPSContext        *s = ctx->priv;
    int frames_out = s->frames_out;
    int ret = 0;

    while (ret >= 0 && s->frames_out == frames_out)
134
        ret = ff_request_frame(ctx->inputs[0]);
Anton Khirnov's avatar
Anton Khirnov committed
135 136 137 138 139

    /* flush the fifo */
    if (ret == AVERROR_EOF && av_fifo_size(s->fifo)) {
        int i;
        for (i = 0; av_fifo_size(s->fifo); i++) {
140
            AVFrame *buf;
Anton Khirnov's avatar
Anton Khirnov committed
141 142 143 144 145

            av_fifo_generic_read(s->fifo, &buf, sizeof(buf), NULL);
            buf->pts = av_rescale_q(s->first_pts, ctx->inputs[0]->time_base,
                                    outlink->time_base) + s->frames_out;

146
            if ((ret = ff_filter_frame(outlink, buf)) < 0)
147 148
                return ret;

Anton Khirnov's avatar
Anton Khirnov committed
149 150 151 152 153 154 155 156
            s->frames_out++;
        }
        return 0;
    }

    return ret;
}

157
static int write_to_fifo(AVFifoBuffer *fifo, AVFrame *buf)
Anton Khirnov's avatar
Anton Khirnov committed
158 159 160 161
{
    int ret;

    if (!av_fifo_space(fifo) &&
162
        (ret = av_fifo_realloc2(fifo, 2*av_fifo_size(fifo)))) {
163
        av_frame_free(&buf);
Anton Khirnov's avatar
Anton Khirnov committed
164
        return ret;
165
    }
Anton Khirnov's avatar
Anton Khirnov committed
166 167 168 169 170

    av_fifo_generic_write(fifo, &buf, sizeof(buf), NULL);
    return 0;
}

171
static int filter_frame(AVFilterLink *inlink, AVFrame *buf)
Anton Khirnov's avatar
Anton Khirnov committed
172 173 174 175 176
{
    AVFilterContext    *ctx = inlink->dst;
    FPSContext           *s = ctx->priv;
    AVFilterLink   *outlink = ctx->outputs[0];
    int64_t delta;
177
    int i, ret;
Anton Khirnov's avatar
Anton Khirnov committed
178 179 180 181 182

    s->frames_in++;
    /* discard frames until we get the first timestamp */
    if (s->pts == AV_NOPTS_VALUE) {
        if (buf->pts != AV_NOPTS_VALUE) {
183 184 185 186
            ret = write_to_fifo(s->fifo, buf);
            if (ret < 0)
                return ret;

187
            if (s->start_time != DBL_MAX) {
188 189 190 191 192 193 194 195 196 197
                double first_pts = s->start_time * AV_TIME_BASE;
                first_pts = FFMIN(FFMAX(first_pts, INT64_MIN), INT64_MAX);
                s->first_pts = s->pts = av_rescale_q(first_pts, AV_TIME_BASE_Q,
                                                     inlink->time_base);
                av_log(ctx, AV_LOG_VERBOSE, "Set first pts to (in:%"PRId64" out:%"PRId64")\n",
                       s->first_pts, av_rescale_q(first_pts, AV_TIME_BASE_Q,
                                                  outlink->time_base));
            } else {
                s->first_pts = s->pts = buf->pts;
            }
Anton Khirnov's avatar
Anton Khirnov committed
198 199 200
        } else {
            av_log(ctx, AV_LOG_WARNING, "Discarding initial frame(s) with no "
                   "timestamp.\n");
201
            av_frame_free(&buf);
Anton Khirnov's avatar
Anton Khirnov committed
202 203
            s->drop++;
        }
204
        return 0;
Anton Khirnov's avatar
Anton Khirnov committed
205 206 207 208
    }

    /* now wait for the next timestamp */
    if (buf->pts == AV_NOPTS_VALUE) {
209
        return write_to_fifo(s->fifo, buf);
Anton Khirnov's avatar
Anton Khirnov committed
210 211 212 213 214 215 216 217
    }

    /* number of output frames */
    delta = av_rescale_q(buf->pts - s->pts, inlink->time_base,
                         outlink->time_base);

    if (delta < 1) {
        /* drop the frame and everything buffered except the first */
218 219
        AVFrame *tmp;
        int drop = av_fifo_size(s->fifo)/sizeof(AVFrame*);
Anton Khirnov's avatar
Anton Khirnov committed
220 221 222 223 224 225

        av_log(ctx, AV_LOG_DEBUG, "Dropping %d frame(s).\n", drop);
        s->drop += drop;

        av_fifo_generic_read(s->fifo, &tmp, sizeof(tmp), NULL);
        flush_fifo(s->fifo);
226
        ret = write_to_fifo(s->fifo, tmp);
Anton Khirnov's avatar
Anton Khirnov committed
227

228
        av_frame_free(&buf);
229
        return ret;
Anton Khirnov's avatar
Anton Khirnov committed
230 231 232 233
    }

    /* can output >= 1 frames */
    for (i = 0; i < delta; i++) {
234
        AVFrame *buf_out;
Anton Khirnov's avatar
Anton Khirnov committed
235 236 237 238
        av_fifo_generic_read(s->fifo, &buf_out, sizeof(buf_out), NULL);

        /* duplicate the frame if needed */
        if (!av_fifo_size(s->fifo) && i < delta - 1) {
239
            AVFrame *dup = av_frame_clone(buf_out);
240

Anton Khirnov's avatar
Anton Khirnov committed
241
            av_log(ctx, AV_LOG_DEBUG, "Duplicating frame.\n");
242 243 244 245 246 247
            if (dup)
                ret = write_to_fifo(s->fifo, dup);
            else
                ret = AVERROR(ENOMEM);

            if (ret < 0) {
248 249
                av_frame_free(&buf_out);
                av_frame_free(&buf);
250 251 252
                return ret;
            }

Anton Khirnov's avatar
Anton Khirnov committed
253 254 255 256 257 258
            s->dup++;
        }

        buf_out->pts = av_rescale_q(s->first_pts, inlink->time_base,
                                    outlink->time_base) + s->frames_out;

259
        if ((ret = ff_filter_frame(outlink, buf_out)) < 0) {
260
            av_frame_free(&buf);
261 262 263
            return ret;
        }

Anton Khirnov's avatar
Anton Khirnov committed
264 265 266 267
        s->frames_out++;
    }
    flush_fifo(s->fifo);

268
    ret = write_to_fifo(s->fifo, buf);
Anton Khirnov's avatar
Anton Khirnov committed
269
    s->pts = s->first_pts + av_rescale_q(s->frames_out, outlink->time_base, inlink->time_base);
270 271

    return ret;
Anton Khirnov's avatar
Anton Khirnov committed
272 273
}

274 275 276 277
static const AVFilterPad avfilter_vf_fps_inputs[] = {
    {
        .name        = "default",
        .type        = AVMEDIA_TYPE_VIDEO,
278
        .filter_frame = filter_frame,
279 280 281 282 283 284 285 286 287 288 289 290 291 292
    },
    { NULL }
};

static const AVFilterPad avfilter_vf_fps_outputs[] = {
    {
        .name          = "default",
        .type          = AVMEDIA_TYPE_VIDEO,
        .request_frame = request_frame,
        .config_props  = config_props
    },
    { NULL }
};

293
AVFilter ff_vf_fps = {
Anton Khirnov's avatar
Anton Khirnov committed
294 295 296 297 298 299 300
    .name        = "fps",
    .description = NULL_IF_CONFIG_SMALL("Force constant framerate"),

    .init      = init,
    .uninit    = uninit,

    .priv_size = sizeof(FPSContext),
301
    .priv_class = &class,
Anton Khirnov's avatar
Anton Khirnov committed
302

303 304
    .inputs    = avfilter_vf_fps_inputs,
    .outputs   = avfilter_vf_fps_outputs,
Anton Khirnov's avatar
Anton Khirnov committed
305
};