[FFmpeg-devel,2/2] avcodec/realtime_bsf: add option to scale speed

Submitted by Moritz Barsnick on Dec. 25, 2018, 8:41 p.m.

Details

Message ID 4d43a17210ff1f31a5d53a6784520fa93dd625c0.1545769801.git.barsnick@gmx.net
State New
Headers show

Commit Message

Moritz Barsnick Dec. 25, 2018, 8:41 p.m.
Signed-off-by: Moritz Barsnick <barsnick@gmx.net>
---
 doc/bitstream_filters.texi | 4 ++++
 libavcodec/realtime_bsf.c  | 7 +++++--
 2 files changed, 9 insertions(+), 2 deletions(-)

Patch hide | download patch | download mbox

diff --git a/doc/bitstream_filters.texi b/doc/bitstream_filters.texi
index 8c71100460..860c555c3f 100644
--- a/doc/bitstream_filters.texi
+++ b/doc/bitstream_filters.texi
@@ -610,6 +610,10 @@  It accepts the following options:
 @item limit
 Time limit for the pauses. Any pause longer than that will be considered
 a timestamp discontinuity and reset the timer. Default is 2 seconds.
+@item speed
+Speed factor for processing. The value must be a float larger than zero.
+Values larger than 1.0 will speed up processing, smaller will slow it down.
+The @var{limit} is automatically adapted accordingly. Default is 1.0.
 @end table
 
 @section remove_extra
diff --git a/libavcodec/realtime_bsf.c b/libavcodec/realtime_bsf.c
index eb4b0b69ed..31a648f96c 100644
--- a/libavcodec/realtime_bsf.c
+++ b/libavcodec/realtime_bsf.c
@@ -24,11 +24,13 @@ 
 #include "libavutil/opt.h"
 #include "avcodec.h"
 #include "bsf.h"
+#include <float.h>
 
 typedef struct RealtimeContext {
     const AVClass *class;
     int64_t delta;
     int64_t limit;
+    double speed;
     unsigned inited;
 } RealtimeContext;
 
@@ -42,7 +44,7 @@  static int realtime_filter(AVBSFContext *bsf, AVPacket *pkt)
         return ret;
 
     if (pkt->pts != AV_NOPTS_VALUE) {
-        int64_t pts = av_rescale_q(pkt->pts, bsf->time_base_in, AV_TIME_BASE_Q);
+        int64_t pts = av_rescale_q(pkt->pts, bsf->time_base_in, AV_TIME_BASE_Q) / ctx->speed;
         int64_t now = av_gettime_relative();
         int64_t sleep = pts - now + ctx->delta;
         if (!ctx->inited) {
@@ -50,7 +52,7 @@  static int realtime_filter(AVBSFContext *bsf, AVPacket *pkt)
             sleep = 0;
             ctx->delta = now - pts;
         }
-        if (sleep > ctx->limit || sleep < -ctx->limit) {
+        if (sleep > ctx->limit / ctx->speed || sleep < -ctx->limit / ctx->speed) {
             av_log(ctx, AV_LOG_WARNING,
                    "time discontinuity detected: %"PRIi64" us, resetting\n",
                    sleep);
@@ -72,6 +74,7 @@  static int realtime_filter(AVBSFContext *bsf, AVPacket *pkt)
 #define FLAGS (AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_BSF_PARAM)
 static const AVOption options[] = {
     { "limit", "sleep time limit", OFFSET(limit), AV_OPT_TYPE_DURATION, { .i64 = 2000000 }, 0, INT64_MAX, FLAGS },
+    { "speed", "speed factor", OFFSET(speed), AV_OPT_TYPE_DOUBLE, { .dbl = 1.0 }, DBL_MIN, DBL_MAX, FLAGS },
     { NULL },
 };