vf_fps.c 8.32 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 "libavutil/common.h"
Anton Khirnov's avatar
Anton Khirnov committed
25 26 27 28 29 30
#include "libavutil/fifo.h"
#include "libavutil/mathematics.h"
#include "libavutil/opt.h"
#include "libavutil/parseutils.h"

#include "avfilter.h"
31 32
#include "internal.h"
#include "video.h"
Anton Khirnov's avatar
Anton Khirnov committed
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66

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

    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 },
    { NULL },
};

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

67
static av_cold int init(AVFilterContext *ctx, const char *args)
Anton Khirnov's avatar
Anton Khirnov committed
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 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 134
{
    FPSContext *s = ctx->priv;
    int ret;

    s->class = &class;
    av_opt_set_defaults(s);

    if ((ret = av_set_options_string(s, args, "=", ":")) < 0) {
        av_log(ctx, AV_LOG_ERROR, "Error parsing the options string %s.\n",
               args);
        return 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;
    }
    av_opt_free(s);

    if (!(s->fifo = av_fifo_alloc(2*sizeof(AVFilterBufferRef*))))
        return AVERROR(ENOMEM);

    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)) {
        AVFilterBufferRef *tmp;
        av_fifo_generic_read(fifo, &tmp, sizeof(tmp), NULL);
        avfilter_unref_buffer(tmp);
    }
}

static av_cold void uninit(AVFilterContext *ctx)
{
    FPSContext *s = ctx->priv;
    if (s->fifo) {
        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;
    s->pts          = AV_NOPTS_VALUE;

    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)
135
        ret = ff_request_frame(ctx->inputs[0]);
Anton Khirnov's avatar
Anton Khirnov committed
136 137 138 139 140 141 142 143 144 145 146

    /* flush the fifo */
    if (ret == AVERROR_EOF && av_fifo_size(s->fifo)) {
        int i;
        for (i = 0; av_fifo_size(s->fifo); i++) {
            AVFilterBufferRef *buf;

            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;

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

Anton Khirnov's avatar
Anton Khirnov committed
150 151 152 153 154 155 156 157 158 159 160 161 162
            s->frames_out++;
        }
        return 0;
    }

    return ret;
}

static int write_to_fifo(AVFifoBuffer *fifo, AVFilterBufferRef *buf)
{
    int ret;

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

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

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

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

Anton Khirnov's avatar
Anton Khirnov committed
188 189 190 191 192 193 194
            s->first_pts = s->pts = buf->pts;
        } else {
            av_log(ctx, AV_LOG_WARNING, "Discarding initial frame(s) with no "
                   "timestamp.\n");
            avfilter_unref_buffer(buf);
            s->drop++;
        }
195
        return 0;
Anton Khirnov's avatar
Anton Khirnov committed
196 197 198 199
    }

    /* now wait for the next timestamp */
    if (buf->pts == AV_NOPTS_VALUE) {
200
        return write_to_fifo(s->fifo, buf);
Anton Khirnov's avatar
Anton Khirnov committed
201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
    }

    /* 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 */
        AVFilterBufferRef *tmp;
        int drop = av_fifo_size(s->fifo)/sizeof(AVFilterBufferRef*);

        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);
217
        ret = write_to_fifo(s->fifo, tmp);
Anton Khirnov's avatar
Anton Khirnov committed
218 219

        avfilter_unref_buffer(buf);
220
        return ret;
Anton Khirnov's avatar
Anton Khirnov committed
221 222 223 224 225 226 227 228 229
    }

    /* can output >= 1 frames */
    for (i = 0; i < delta; i++) {
        AVFilterBufferRef *buf_out;
        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) {
230 231
            AVFilterBufferRef *dup = avfilter_ref_buffer(buf_out, AV_PERM_READ);

Anton Khirnov's avatar
Anton Khirnov committed
232
            av_log(ctx, AV_LOG_DEBUG, "Duplicating frame.\n");
233 234 235 236 237 238 239 240 241 242 243
            if (dup)
                ret = write_to_fifo(s->fifo, dup);
            else
                ret = AVERROR(ENOMEM);

            if (ret < 0) {
                avfilter_unref_bufferp(&buf_out);
                avfilter_unref_bufferp(&buf);
                return ret;
            }

Anton Khirnov's avatar
Anton Khirnov committed
244 245 246 247 248 249
            s->dup++;
        }

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

250
        if ((ret = ff_filter_frame(outlink, buf_out)) < 0) {
251 252 253 254
            avfilter_unref_bufferp(&buf);
            return ret;
        }

Anton Khirnov's avatar
Anton Khirnov committed
255 256 257 258
        s->frames_out++;
    }
    flush_fifo(s->fifo);

259
    ret = write_to_fifo(s->fifo, buf);
Anton Khirnov's avatar
Anton Khirnov committed
260
    s->pts = s->first_pts + av_rescale_q(s->frames_out, outlink->time_base, inlink->time_base);
261 262

    return ret;
Anton Khirnov's avatar
Anton Khirnov committed
263 264
}

265 266 267 268
static const AVFilterPad avfilter_vf_fps_inputs[] = {
    {
        .name        = "default",
        .type        = AVMEDIA_TYPE_VIDEO,
269
        .filter_frame = filter_frame,
270 271 272 273 274 275 276 277 278 279 280 281 282 283
    },
    { NULL }
};

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

Anton Khirnov's avatar
Anton Khirnov committed
284 285 286 287 288 289 290 291 292
AVFilter avfilter_vf_fps = {
    .name        = "fps",
    .description = NULL_IF_CONFIG_SMALL("Force constant framerate"),

    .init      = init,
    .uninit    = uninit,

    .priv_size = sizeof(FPSContext),

293 294
    .inputs    = avfilter_vf_fps_inputs,
    .outputs   = avfilter_vf_fps_outputs,
Anton Khirnov's avatar
Anton Khirnov committed
295
};