[FFmpeg-devel] [PATCH] vf_fps: when reading EOF, using current_pts to duplicate the last frame if needed.
Thierry Foucu
tfoucu at gmail.com
Tue Sep 12 04:25:08 EEST 2017
Fix ticket #2674
Tested with examples from ticket 2674.
---
Update the Patch with the correct number of duplicate showing.
For exmaple, in case we up-sample one second video at 24fps by 2, we should be getting 24 duplicate frames.
ffmpeg -loglevel verbose -i 24fps.avi -vf fps=48 -f null -
ffmpeg version N-87234-g9a32769f5e Copyright (c) 2000-2017 the FFmpeg developers
built with gcc 4.8 (Ubuntu 4.8.4-2ubuntu1~14.04.3)
configuration:
libavutil 55. 74.100 / 55. 74.100
libavcodec 57.105.100 / 57.105.100
libavformat 57. 81.100 / 57. 81.100
libavdevice 57. 8.100 / 57. 8.100
libavfilter 6.103.100 / 6.103.100
libswscale 4. 7.103 / 4. 7.103
libswresample 2. 8.100 / 2. 8.100
Input #0, avi, from '24fps.avi':
Metadata:
encoder : Lavf57.81.100
Duration: 00:00:01.00, start: 0.000000, bitrate: 368 kb/s
Stream #0:0: Video: mpeg4 (Simple Profile), 1 reference frame (FMP4 / 0x34504D46), yuv420p(left), 320x240 [SAR 1:1 DAR 4:3], 24 fps, 24 tbr, 24 tbn, 24 tbc
Stream mapping:
Stream #0:0 -> #0:0 (mpeg4 (native) -> wrapped_avframe (native))
Press [q] to stop, [?] for help
[Parsed_fps_0 @ 0x25ea6a0] fps=48/1
[graph 0 input from stream 0:0 @ 0x275cc00] w:320 h:240 pixfmt:yuv420p tb:1/24 fr:24/1 sar:1/1 sws_param:flags=2
Output #0, null, to 'pipe:':
Metadata:
encoder : Lavf57.81.100
Stream #0:0: Video: wrapped_avframe, 1 reference frame, yuv420p(left), 320x240 [SAR 1:1 DAR 4:3], q=2-31, 200 kb/s, 48 fps, 48 tbn, 48 tbc
Metadata:
encoder : Lavc57.105.100 wrapped_avframe
No more output streams to write to, finishing.
frame= 48 fps=0.0 q=-0.0 Lsize=N/A time=00:00:01.00 bitrate=N/A speed=99.3x
video:25kB audio:0kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: unknown
Input file #0 (24fps.avi):
Input stream #0:0 (video): 24 packets read (39693 bytes); 24 frames decoded;
Total: 24 packets (39693 bytes) demuxed
Output file #0 (pipe:):
Output stream #0:0 (video): 48 frames encoded; 48 packets muxed (25344 bytes);
Total: 48 packets (25344 bytes) muxed
[Parsed_fps_0 @ 0x25ea6a0] 24 frames in, 48 frames out; 0 frames dropped, 24 frames duplicated.
libavfilter/vf_fps.c | 44 +++++++++++++++++++++++++++++++++++++++-----
tests/ref/fate/filter-fps | 6 ++++++
tests/ref/fate/filter-fps-r | 4 ++++
tests/ref/fate/m4v-cfr | 1 -
4 files changed, 49 insertions(+), 6 deletions(-)
diff --git a/libavfilter/vf_fps.c b/libavfilter/vf_fps.c
index 20ccd797d1..09fc66a73c 100644
--- a/libavfilter/vf_fps.c
+++ b/libavfilter/vf_fps.c
@@ -34,6 +34,8 @@
#include "libavutil/opt.h"
#include "libavutil/parseutils.h"
+#define FF_INTERNAL_FIELDS 1
+#include "framequeue.h"
#include "avfilter.h"
#include "internal.h"
#include "video.h"
@@ -137,13 +139,45 @@ static int request_frame(AVFilterLink *outlink)
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;
+ 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;
+ if ((ret = ff_filter_frame(outlink, buf)) < 0)
+ return ret;
- s->frames_out++;
+ s->frames_out++;
+ } else {
+ /* This is the last frame, we may have to duplicate it to match
+ * the last frame duration */
+ int j;
+ int delta = av_rescale_q_rnd(ctx->inputs[0]->current_pts - s->first_pts,
+ ctx->inputs[0]->time_base,
+ outlink->time_base, s->rounding) - s->frames_out ;
+ /* 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++;
+ }
+ } 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++;
+ }
+ }
}
return 0;
}
diff --git a/tests/ref/fate/filter-fps b/tests/ref/fate/filter-fps
index 55712cfb1c..242fb04e85 100644
--- a/tests/ref/fate/filter-fps
+++ b/tests/ref/fate/filter-fps
@@ -85,3 +85,9 @@
0, 79, 79, 1, 30576, 0xa2fcd06f
0, 80, 80, 1, 30576, 0xa2fcd06f
0, 81, 81, 1, 30576, 0xd4150aad
+0, 82, 82, 1, 30576, 0xd4150aad
+0, 83, 83, 1, 30576, 0xd4150aad
+0, 84, 84, 1, 30576, 0xd4150aad
+0, 85, 85, 1, 30576, 0xd4150aad
+0, 86, 86, 1, 30576, 0xd4150aad
+0, 87, 87, 1, 30576, 0xd4150aad
diff --git a/tests/ref/fate/filter-fps-r b/tests/ref/fate/filter-fps-r
index 826b1ed6c6..c1bc7d1547 100644
--- a/tests/ref/fate/filter-fps-r
+++ b/tests/ref/fate/filter-fps-r
@@ -72,3 +72,7 @@
0, 79, 79, 1, 30576, 0xa2fcd06f
0, 80, 80, 1, 30576, 0xa2fcd06f
0, 82, 82, 1, 30576, 0xd4150aad
+0, 83, 83, 1, 30576, 0xd4150aad
+0, 84, 84, 1, 30576, 0xd4150aad
+0, 85, 85, 1, 30576, 0xd4150aad
+0, 86, 86, 1, 30576, 0xd4150aad
diff --git a/tests/ref/fate/m4v-cfr b/tests/ref/fate/m4v-cfr
index 4eee84d01b..e2d02032fe 100644
--- a/tests/ref/fate/m4v-cfr
+++ b/tests/ref/fate/m4v-cfr
@@ -44,4 +44,3 @@
0, 38, 38, 1, 115200, 0xf30825d5
0, 39, 39, 1, 115200, 0xe3c944a1
0, 40, 40, 1, 115200, 0x8fec4420
-0, 41, 41, 1, 115200, 0x9381fdab
--
2.14.1.581.gf28d330327-goog
More information about the ffmpeg-devel
mailing list