vf_fps.c 9.06 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 148 149 150 151
            if ((ret = ff_start_frame(outlink, buf)) < 0 ||
                (ret = ff_draw_slice(outlink, 0, outlink->h, 1)) < 0 ||
                (ret = ff_end_frame(outlink)) < 0)
                return ret;

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

    return ret;
}

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

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

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

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

183
    inlink->cur_buf = NULL;
Anton Khirnov's avatar
Anton Khirnov committed
184 185 186 187
    s->frames_in++;
    /* discard frames until we get the first timestamp */
    if (s->pts == AV_NOPTS_VALUE) {
        if (buf->pts != AV_NOPTS_VALUE) {
Anton Khirnov's avatar
Anton Khirnov committed
188 189 190 191
            ret = write_to_fifo(s->fifo, buf);
            if (ret < 0)
                return ret;

Anton Khirnov's avatar
Anton Khirnov committed
192 193 194 195 196 197 198
            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++;
        }
199
        return 0;
Anton Khirnov's avatar
Anton Khirnov committed
200 201 202 203
    }

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

    /* 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);
221
        ret = write_to_fifo(s->fifo, tmp);
Anton Khirnov's avatar
Anton Khirnov committed
222 223

        avfilter_unref_buffer(buf);
224
        return ret;
Anton Khirnov's avatar
Anton Khirnov committed
225 226 227 228 229 230 231 232 233
    }

    /* 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) {
Anton Khirnov's avatar
Anton Khirnov committed
234 235
            AVFilterBufferRef *dup = avfilter_ref_buffer(buf_out, AV_PERM_READ);

Anton Khirnov's avatar
Anton Khirnov committed
236
            av_log(ctx, AV_LOG_DEBUG, "Duplicating frame.\n");
Anton Khirnov's avatar
Anton Khirnov committed
237 238 239 240 241 242 243 244 245 246 247
            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
248 249 250 251 252 253
            s->dup++;
        }

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

254 255 256 257 258 259 260
        if ((ret = ff_start_frame(outlink, buf_out)) < 0 ||
            (ret = ff_draw_slice(outlink, 0, outlink->h, 1)) < 0 ||
            (ret = ff_end_frame(outlink)) < 0) {
            avfilter_unref_bufferp(&buf);
            return ret;
        }

Anton Khirnov's avatar
Anton Khirnov committed
261 262 263 264
        s->frames_out++;
    }
    flush_fifo(s->fifo);

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

    return ret;
Anton Khirnov's avatar
Anton Khirnov committed
269 270
}

271
static int null_start_frame(AVFilterLink *link, AVFilterBufferRef *buf)
Anton Khirnov's avatar
Anton Khirnov committed
272
{
273
    return 0;
Anton Khirnov's avatar
Anton Khirnov committed
274 275
}

276
static int null_draw_slice(AVFilterLink *link, int y, int h, int slice_dir)
Anton Khirnov's avatar
Anton Khirnov committed
277
{
278
    return 0;
Anton Khirnov's avatar
Anton Khirnov committed
279 280 281 282 283 284 285 286 287 288 289
}

AVFilter avfilter_vf_fps = {
    .name        = "fps",
    .description = NULL_IF_CONFIG_SMALL("Force constant framerate"),

    .init      = init,
    .uninit    = uninit,

    .priv_size = sizeof(FPSContext),

290 291 292 293 294 295 296 297 298 299 300
    .inputs    = (const AVFilterPad[]) {{ .name            = "default",
                                          .type            = AVMEDIA_TYPE_VIDEO,
                                          .start_frame     = null_start_frame,
                                          .draw_slice      = null_draw_slice,
                                          .end_frame       = end_frame, },
                                        { .name = NULL}},
    .outputs   = (const AVFilterPad[]) {{ .name            = "default",
                                          .type            = AVMEDIA_TYPE_VIDEO,
                                          .request_frame   = request_frame,
                                          .config_props    = config_props},
                                        { .name = NULL}},
Anton Khirnov's avatar
Anton Khirnov committed
301
};