From 6724eb3dae7b491d5bac3903cf4b7dc4ff037e6c Mon Sep 17 00:00:00 2001
From: Thomas Mundt <loudmax@yahoo.de>
Date: Wed, 22 Mar 2017 00:14:53 +0100
Subject: [PATCH] avfilter/vf_fps: fix duration

Fix Ticket #2674
This makes the fps filter duplicate or drop the last frame depending on the input and output frame rates. Tested with examples from ticket 2674 and other files at various frame rates.

Signed-off-by: Thomas Mundt <loudmax@yahoo.de>
---
 libavfilter/vf_fps.c | 23 ++++++++++++++++++-----
 1 file changed, 18 insertions(+), 5 deletions(-)

diff --git a/libavfilter/vf_fps.c b/libavfilter/vf_fps.c
index 20ccd79..ade3698 100644
--- a/libavfilter/vf_fps.c
+++ b/libavfilter/vf_fps.c
@@ -134,16 +134,29 @@ static int request_frame(AVFilterLink *outlink)
     if (ret == AVERROR_EOF && av_fifo_size(s->fifo)) {
         int i;
         for (i = 0; av_fifo_size(s->fifo); i++) {
+            int j, delta;
             AVFrame *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;
+            delta = (int)(0.4999 + av_q2d(av_div_q(outlink->frame_rate,
+                                                   ctx->inputs[0]->frame_rate)));
 
-            if ((ret = ff_filter_frame(outlink, buf)) < 0)
-                return ret;
+            if (delta < 1) {
+                s->drop++;
+                continue;
+            }
+            for (j = 0; j < delta; j++) {
+                AVFrame *out = av_frame_clone(buf);
+                out->pts = av_rescale_q(s->first_pts, ctx->inputs[0]->time_base,
+                                        outlink->time_base) + s->frames_out;
 
-            s->frames_out++;
+                if ((ret = ff_filter_frame(outlink, out)) < 0)
+                    return ret;
+
+                s->frames_out++;
+                if (j)
+                    s->dup++;
+            }
         }
         return 0;
     }
-- 
2.7.4.windows.1

