libavfilter/vf_fps.c
54c5dd89
 /*
cfb398bf
  * Copyright 2007 Bobby Bingham
  * Copyright 2012 Robert Nagy <ronag89 gmail com>
  * Copyright 2012 Anton Khirnov <anton khirnov net>
  *
c836f28f
  * This file is part of FFmpeg.
54c5dd89
  *
c836f28f
  * FFmpeg is free software; you can redistribute it and/or
54c5dd89
  * 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.
  *
c836f28f
  * FFmpeg is distributed in the hope that it will be useful,
54c5dd89
  * 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
c836f28f
  * License along with FFmpeg; if not, write to the Free Software
54c5dd89
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
 /**
  * @file
  * a filter enforcing given constant framerate
  */
 
cb8f70c9
 #include <float.h>
8f8bc923
 #include <stdint.h>
cb8f70c9
 
1d9c2dc8
 #include "libavutil/common.h"
54c5dd89
 #include "libavutil/fifo.h"
 #include "libavutil/mathematics.h"
 #include "libavutil/opt.h"
 #include "libavutil/parseutils.h"
 
eea64ef4
 #define FF_INTERNAL_FIELDS 1
 #include "framequeue.h"
54c5dd89
 #include "avfilter.h"
803391f7
 #include "internal.h"
 #include "video.h"
54c5dd89
 
62bdec80
 enum EOFAction {
     EOF_ACTION_ROUND,
     EOF_ACTION_PASS,
     EOF_ACTION_NB
 };
 
54c5dd89
 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
 
545a0b80
     double start_time;      ///< pts, in seconds, of the expected first frame
 
54c5dd89
     AVRational framerate;   ///< target framerate
77a72d34
     int rounding;           ///< AVRounding method for timestamps
62bdec80
     int eof_action;         ///< action performed for last frame in FIFO
54c5dd89
 
     /* 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
42d621d1
 #define F AV_OPT_FLAG_FILTERING_PARAM
c17808ce
 static const AVOption fps_options[] = {
5fce4753
     { "fps", "A string describing desired output framerate", OFFSET(framerate), AV_OPT_TYPE_VIDEO_RATE, { .str = "25" }, 0, INT_MAX, V|F },
0a499d6a
     { "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|F },
77a72d34
     { "round", "set rounding method for timestamps", OFFSET(rounding), AV_OPT_TYPE_INT, { .i64 = AV_ROUND_NEAR_INF }, 0, 5, V|F, "round" },
0a499d6a
         { "zero", "round towards 0",                 0, AV_OPT_TYPE_CONST, { .i64 = AV_ROUND_ZERO     }, 0, 0, V|F, "round" },
         { "inf",  "round away from 0",               0, AV_OPT_TYPE_CONST, { .i64 = AV_ROUND_INF      }, 0, 0, V|F, "round" },
         { "down", "round towards -infty",            0, AV_OPT_TYPE_CONST, { .i64 = AV_ROUND_DOWN     }, 0, 0, V|F, "round" },
         { "up",   "round towards +infty",            0, AV_OPT_TYPE_CONST, { .i64 = AV_ROUND_UP       }, 0, 0, V|F, "round" },
         { "near", "round to nearest",                0, AV_OPT_TYPE_CONST, { .i64 = AV_ROUND_NEAR_INF }, 0, 0, V|F, "round" },
62bdec80
     { "eof_action", "action performed for last frame", OFFSET(eof_action), AV_OPT_TYPE_INT, { .i64 = EOF_ACTION_ROUND }, 0, EOF_ACTION_NB-1, V|F, "eof_action" },
         { "round", "round similar to other frames",  0, AV_OPT_TYPE_CONST, { .i64 = EOF_ACTION_ROUND }, 0, 0, V|F, "eof_action" },
         { "pass",  "pass through last frame",        0, AV_OPT_TYPE_CONST, { .i64 = EOF_ACTION_PASS  }, 0, 0, V|F, "eof_action" },
b211607b
     { NULL }
54c5dd89
 };
 
c17808ce
 AVFILTER_DEFINE_CLASS(fps);
54c5dd89
 
d69a4177
 static av_cold int init(AVFilterContext *ctx)
54c5dd89
 {
     FPSContext *s = ctx->priv;
 
ce051cee
     if (!(s->fifo = av_fifo_alloc_array(2, sizeof(AVFrame*))))
54c5dd89
         return AVERROR(ENOMEM);
 
545a0b80
     s->first_pts    = AV_NOPTS_VALUE;
7727be79
 
54c5dd89
     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)) {
7e350379
         AVFrame *tmp;
54c5dd89
         av_fifo_generic_read(fifo, &tmp, sizeof(tmp), NULL);
7e350379
         av_frame_free(&tmp);
54c5dd89
     }
 }
 
 static av_cold void uninit(AVFilterContext *ctx)
 {
     FPSContext *s = ctx->priv;
     if (s->fifo) {
a05a44e2
         s->drop += av_fifo_size(s->fifo) / sizeof(AVFrame*);
54c5dd89
         flush_fifo(s->fifo);
70b63419
         av_fifo_freep(&s->fifo);
54c5dd89
     }
 
     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;
 
16dc5f20
     link->time_base = av_inv_q(s->framerate);
0d249316
     link->frame_rate= s->framerate;
54c5dd89
     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;
4bc7eb2d
     int ret;
54c5dd89
 
4bc7eb2d
     ret = ff_request_frame(ctx->inputs[0]);
54c5dd89
 
     /* flush the fifo */
     if (ret == AVERROR_EOF && av_fifo_size(s->fifo)) {
         int i;
         for (i = 0; av_fifo_size(s->fifo); i++) {
7e350379
             AVFrame *buf;
54c5dd89
 
             av_fifo_generic_read(s->fifo, &buf, sizeof(buf), NULL);
eea64ef4
             if (av_fifo_size(s->fifo)) {
                 buf->pts = av_rescale_q(s->first_pts, ctx->inputs[0]->time_base,
                                         outlink->time_base) + s->frames_out;
 
                 if ((ret = ff_filter_frame(outlink, buf)) < 0)
                     return ret;
 
                 s->frames_out++;
             } else {
                 /* This is the last frame, we may have to duplicate it to match
                  * the last frame duration */
                 int j;
62bdec80
                 int eof_rounding = (s->eof_action == EOF_ACTION_PASS) ? AV_ROUND_UP : s->rounding;
eea64ef4
                 int delta = av_rescale_q_rnd(ctx->inputs[0]->current_pts - s->first_pts,
                                              ctx->inputs[0]->time_base,
62bdec80
                                              outlink->time_base, eof_rounding) - s->frames_out;
                 av_log(ctx, AV_LOG_DEBUG, "EOF frames_out:%d delta:%d\n", s->frames_out, delta);
eea64ef4
                 /* if the delta is equal to 1, it means we just need to output
                  * the last frame. Greater than 1 means we will need duplicate
                  * delta-1 frames */
                 if (delta > 0 ) {
                     for (j = 0; j < delta; j++) {
                         AVFrame *dup = av_frame_clone(buf);
 
                         av_log(ctx, AV_LOG_DEBUG, "Duplicating frame.\n");
                         dup->pts = av_rescale_q(s->first_pts, ctx->inputs[0]->time_base,
                                                 outlink->time_base) + s->frames_out;
 
                         if ((ret = ff_filter_frame(outlink, dup)) < 0)
                             return ret;
 
                         s->frames_out++;
                         if (j > 0) s->dup++;
                     }
42a41c39
                     av_frame_free(&buf);
eea64ef4
                 } else {
                     /* for delta less or equal to 0, we should drop the frame,
                      * otherwise, we will have one or more extra frames */
                     av_frame_free(&buf);
                     s->drop++;
                 }
             }
54c5dd89
         }
         return 0;
     }
 
     return ret;
 }
 
7e350379
 static int write_to_fifo(AVFifoBuffer *fifo, AVFrame *buf)
54c5dd89
 {
     int ret;
 
     if (!av_fifo_space(fifo) &&
d515e9c2
         (ret = av_fifo_realloc2(fifo, 2*av_fifo_size(fifo)))) {
7e350379
         av_frame_free(&buf);
54c5dd89
         return ret;
d515e9c2
     }
54c5dd89
 
     av_fifo_generic_write(fifo, &buf, sizeof(buf), NULL);
     return 0;
 }
 
7e350379
 static int filter_frame(AVFilterLink *inlink, AVFrame *buf)
54c5dd89
 {
     AVFilterContext    *ctx = inlink->dst;
     FPSContext           *s = ctx->priv;
     AVFilterLink   *outlink = ctx->outputs[0];
     int64_t delta;
d4f89906
     int i, ret;
54c5dd89
 
     s->frames_in++;
     /* discard frames until we get the first timestamp */
8f33810e
     if (s->first_pts == AV_NOPTS_VALUE) {
54c5dd89
         if (buf->pts != AV_NOPTS_VALUE) {
043800a9
             ret = write_to_fifo(s->fifo, buf);
             if (ret < 0)
                 return ret;
 
8b79a458
             if (s->start_time != DBL_MAX && s->start_time != AV_NOPTS_VALUE) {
545a0b80
                 double first_pts = s->start_time * AV_TIME_BASE;
                 first_pts = FFMIN(FFMAX(first_pts, INT64_MIN), INT64_MAX);
8f33810e
                 s->first_pts = av_rescale_q(first_pts, AV_TIME_BASE_Q,
545a0b80
                                                      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 {
8f33810e
                 s->first_pts = buf->pts;
545a0b80
             }
54c5dd89
         } else {
             av_log(ctx, AV_LOG_WARNING, "Discarding initial frame(s) with no "
                    "timestamp.\n");
7e350379
             av_frame_free(&buf);
54c5dd89
             s->drop++;
         }
d4f89906
         return 0;
54c5dd89
     }
 
     /* now wait for the next timestamp */
cdd5df81
     if (buf->pts == AV_NOPTS_VALUE || av_fifo_size(s->fifo) <= 0) {
d4f89906
         return write_to_fifo(s->fifo, buf);
54c5dd89
     }
 
     /* number of output frames */
8f33810e
     delta = av_rescale_q_rnd(buf->pts - s->first_pts, inlink->time_base,
                              outlink->time_base, s->rounding) - s->frames_out ;
54c5dd89
 
     if (delta < 1) {
7801a54e
         /* drop everything buffered except the last */
7e350379
         int drop = av_fifo_size(s->fifo)/sizeof(AVFrame*);
54c5dd89
 
         av_log(ctx, AV_LOG_DEBUG, "Dropping %d frame(s).\n", drop);
         s->drop += drop;
 
         flush_fifo(s->fifo);
7f02fcd9
         ret = write_to_fifo(s->fifo, buf);
54c5dd89
 
d4f89906
         return ret;
54c5dd89
     }
 
     /* can output >= 1 frames */
     for (i = 0; i < delta; i++) {
7e350379
         AVFrame *buf_out;
54c5dd89
         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) {
7e350379
             AVFrame *dup = av_frame_clone(buf_out);
043800a9
 
54c5dd89
             av_log(ctx, AV_LOG_DEBUG, "Duplicating frame.\n");
043800a9
             if (dup)
                 ret = write_to_fifo(s->fifo, dup);
             else
                 ret = AVERROR(ENOMEM);
 
             if (ret < 0) {
7e350379
                 av_frame_free(&buf_out);
                 av_frame_free(&buf);
043800a9
                 return ret;
             }
 
54c5dd89
             s->dup++;
         }
 
         buf_out->pts = av_rescale_q(s->first_pts, inlink->time_base,
                                     outlink->time_base) + s->frames_out;
 
92b57e8d
         if ((ret = ff_filter_frame(outlink, buf_out)) < 0) {
7e350379
             av_frame_free(&buf);
3825b526
             return ret;
         }
 
54c5dd89
         s->frames_out++;
     }
     flush_fifo(s->fifo);
 
d4f89906
     ret = write_to_fifo(s->fifo, buf);
 
     return ret;
54c5dd89
 }
 
568c70e7
 static const AVFilterPad avfilter_vf_fps_inputs[] = {
     {
b211607b
         .name         = "default",
         .type         = AVMEDIA_TYPE_VIDEO,
92b57e8d
         .filter_frame = filter_frame,
568c70e7
     },
     { NULL }
 };
 
 static const AVFilterPad avfilter_vf_fps_outputs[] = {
     {
         .name          = "default",
         .type          = AVMEDIA_TYPE_VIDEO,
         .request_frame = request_frame,
         .config_props  = config_props
     },
     { NULL }
 };
 
cd43ca04
 AVFilter ff_vf_fps = {
54c5dd89
     .name        = "fps",
dfac37af
     .description = NULL_IF_CONFIG_SMALL("Force constant framerate."),
b211607b
     .init        = init,
     .uninit      = uninit,
     .priv_size   = sizeof(FPSContext),
     .priv_class  = &fps_class,
     .inputs      = avfilter_vf_fps_inputs,
     .outputs     = avfilter_vf_fps_outputs,
54c5dd89
 };