[FFmpeg-devel] [PATCH 1/2] lavf: add audio_preload option, this allows interleaving audio earlier

Michael Niedermayer michaelni at gmx.at
Wed Nov 30 00:29:13 CET 2011


Signed-off-by: Michael Niedermayer <michaelni at gmx.at>
---
 libavformat/avformat.h |    7 +++++++
 libavformat/options.c  |    1 +
 libavformat/utils.c    |   10 ++++++++++
 3 files changed, 18 insertions(+), 0 deletions(-)

diff --git a/libavformat/avformat.h b/libavformat/avformat.h
index 752645e..b281406 100644
--- a/libavformat/avformat.h
+++ b/libavformat/avformat.h
@@ -1078,6 +1078,13 @@ typedef struct AVFormatContext {
      */
     int ts_id;
 
+    /**
+     * Audio preload in micro seconds.
+     * Note, not all formats support this and unpredictable things may happen if its used when not supported.
+     * - encoding: Set by user via AVOptions (NO direct access)
+     * - decoding: unused
+     */
+    int audio_preload;
 
     /*****************************************************************
      * All fields below this line are not part of the public API. They
diff --git a/libavformat/options.c b/libavformat/options.c
index 3290f92..144aa1a 100644
--- a/libavformat/options.c
+++ b/libavformat/options.c
@@ -116,6 +116,7 @@ static const AVOption options[]={
 {"careful", NULL, 0, AV_OPT_TYPE_CONST, {.dbl = FF_ER_CAREFUL }, INT_MIN, INT_MAX, D, "fer"},
 {"explode", "abort decoding on error recognition", 0, AV_OPT_TYPE_CONST, {.dbl = FF_ER_EXPLODE }, INT_MIN, INT_MAX, D, "fer"},
 {"fpsprobesize", "number of frames used to probe fps", OFFSET(fps_probe_size), AV_OPT_TYPE_INT, {.dbl = -1}, -1, INT_MAX-1, D},
+{"audio_preload", "microseconds by which audio packets should be interleaved earlier", OFFSET(audio_preload), AV_OPT_TYPE_INT, {.dbl = 0}, 0, INT_MAX-1, E},
 {NULL},
 };
 
diff --git a/libavformat/utils.c b/libavformat/utils.c
index 13d7928..633cf78 100644
--- a/libavformat/utils.c
+++ b/libavformat/utils.c
@@ -3305,6 +3305,16 @@ static int ff_interleave_compare_dts(AVFormatContext *s, AVPacket *next, AVPacke
     AVStream *st2= s->streams[ next->stream_index];
     int comp = av_compare_ts(next->dts, st2->time_base, pkt->dts,
                              st->time_base);
+    if(s->audio_preload && ((st->codec->codec_type == AVMEDIA_TYPE_AUDIO) != (st2->codec->codec_type == AVMEDIA_TYPE_AUDIO))){
+        int64_t ts = av_rescale_q(pkt ->dts, st ->time_base, AV_TIME_BASE_Q) - s->audio_preload*(st ->codec->codec_type == AVMEDIA_TYPE_AUDIO);
+        int64_t ts2= av_rescale_q(next->dts, st2->time_base, AV_TIME_BASE_Q) - s->audio_preload*(st2->codec->codec_type == AVMEDIA_TYPE_AUDIO);
+        if(ts == ts2){
+            ts= ( pkt ->dts* st->time_base.num*AV_TIME_BASE - s->audio_preload*(int64_t)(st ->codec->codec_type == AVMEDIA_TYPE_AUDIO)* st->time_base.den)*st2->time_base.den
+               -( next->dts*st2->time_base.num*AV_TIME_BASE - s->audio_preload*(int64_t)(st2->codec->codec_type == AVMEDIA_TYPE_AUDIO)*st2->time_base.den)* st->time_base.den;
+            ts2=0;
+        }
+        comp= (ts>ts2) - (ts<ts2);
+    }
 
     if (comp == 0)
         return pkt->stream_index < next->stream_index;
-- 
1.7.5.4



More information about the ffmpeg-devel mailing list