[FFmpeg-devel] libavformat/mux: Fix audio_preload

Submitted by Andreas Rheinhardt on June 21, 2019, 4:51 p.m.

Details

Message ID 20190621165154.60441-1-andreas.rheinhardt@gmail.com
State New
Headers show

Commit Message

Andreas Rheinhardt June 21, 2019, 4:51 p.m.
Commit 31f9032b added the audio_preload feature; its goal is to
interleave audio earlier than the rest. Unfortunately, it has never ever
worked, because the check for whether a packet should be interleaved
before or after another packet was completely wrong: When audio_preload
vanishes, interleave_compare_dts returns 1 if the new packet should be
interleaved earlier than the packet it is compared with and that is what
the rest of the code expects. But the codepath used when audio_preload is
set does the opposite.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
---
 libavformat/mux.c     | 7 +++----
 libavformat/version.h | 2 +-
 2 files changed, 4 insertions(+), 5 deletions(-)

Comments

Michael Niedermayer June 22, 2019, 8:09 a.m.
On Fri, Jun 21, 2019 at 06:51:54PM +0200, Andreas Rheinhardt wrote:
> Commit 31f9032b added the audio_preload feature; its goal is to
> interleave audio earlier than the rest. Unfortunately, it has never ever
> worked, because the check for whether a packet should be interleaved
> before or after another packet was completely wrong: When audio_preload
> vanishes, interleave_compare_dts returns 1 if the new packet should be
> interleaved earlier than the packet it is compared with and that is what
> the rest of the code expects. But the codepath used when audio_preload is
> set does the opposite.
> 
> Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
> ---
>  libavformat/mux.c     | 7 +++----
>  libavformat/version.h | 2 +-
>  2 files changed, 4 insertions(+), 5 deletions(-)
> 
> diff --git a/libavformat/mux.c b/libavformat/mux.c
> index 83fe1de78f..e954c0326b 100644
> --- a/libavformat/mux.c
> +++ b/libavformat/mux.c
> @@ -1005,11 +1005,10 @@ static int interleave_compare_dts(AVFormatContext *s, AVPacket *next,
>          int64_t ts = av_rescale_q(pkt ->dts, st ->time_base, AV_TIME_BASE_Q) - s->audio_preload*(st ->codecpar->codec_type == AVMEDIA_TYPE_AUDIO);
>          int64_t ts2= av_rescale_q(next->dts, st2->time_base, AV_TIME_BASE_Q) - s->audio_preload*(st2->codecpar->codec_type == AVMEDIA_TYPE_AUDIO);
>          if (ts == ts2) {
> -            ts= ( pkt ->dts* st->time_base.num*AV_TIME_BASE - s->audio_preload*(int64_t)(st ->codecpar->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->codecpar->codec_type == AVMEDIA_TYPE_AUDIO)*st2->time_base.den)* st->time_base.den;
> -            ts2=0;
> +            ts  = (pkt ->dts* st->time_base.num*AV_TIME_BASE - s->audio_preload*(int64_t)(st ->codecpar->codec_type == AVMEDIA_TYPE_AUDIO)* st->time_base.den)*st2->time_base.den;
> +            ts2 = (next->dts*st2->time_base.num*AV_TIME_BASE - s->audio_preload*(int64_t)(st2->codecpar->codec_type == AVMEDIA_TYPE_AUDIO)*st2->time_base.den)* st->time_base.den;

this is incorrect, the computation has to stay within one of the 2 variables
so that overflows cancel out. It needs to use uint64_t though

>          }
> -        comp= (ts>ts2) - (ts<ts2);
> +        comp = (ts2 > ts) - (ts2 < ts);

this change looks correct


[...]

Patch hide | download patch | download mbox

diff --git a/libavformat/mux.c b/libavformat/mux.c
index 83fe1de78f..e954c0326b 100644
--- a/libavformat/mux.c
+++ b/libavformat/mux.c
@@ -1005,11 +1005,10 @@  static int interleave_compare_dts(AVFormatContext *s, AVPacket *next,
         int64_t ts = av_rescale_q(pkt ->dts, st ->time_base, AV_TIME_BASE_Q) - s->audio_preload*(st ->codecpar->codec_type == AVMEDIA_TYPE_AUDIO);
         int64_t ts2= av_rescale_q(next->dts, st2->time_base, AV_TIME_BASE_Q) - s->audio_preload*(st2->codecpar->codec_type == AVMEDIA_TYPE_AUDIO);
         if (ts == ts2) {
-            ts= ( pkt ->dts* st->time_base.num*AV_TIME_BASE - s->audio_preload*(int64_t)(st ->codecpar->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->codecpar->codec_type == AVMEDIA_TYPE_AUDIO)*st2->time_base.den)* st->time_base.den;
-            ts2=0;
+            ts  = (pkt ->dts* st->time_base.num*AV_TIME_BASE - s->audio_preload*(int64_t)(st ->codecpar->codec_type == AVMEDIA_TYPE_AUDIO)* st->time_base.den)*st2->time_base.den;
+            ts2 = (next->dts*st2->time_base.num*AV_TIME_BASE - s->audio_preload*(int64_t)(st2->codecpar->codec_type == AVMEDIA_TYPE_AUDIO)*st2->time_base.den)* st->time_base.den;
         }
-        comp= (ts>ts2) - (ts<ts2);
+        comp = (ts2 > ts) - (ts2 < ts);
     }
 
     if (comp == 0)
diff --git a/libavformat/version.h b/libavformat/version.h
index 52dd95f5c6..39b00f62ab 100644
--- a/libavformat/version.h
+++ b/libavformat/version.h
@@ -33,7 +33,7 @@ 
 // Also please add any ticket numbers that you believe might be affected here
 #define LIBAVFORMAT_VERSION_MAJOR  58
 #define LIBAVFORMAT_VERSION_MINOR  28
-#define LIBAVFORMAT_VERSION_MICRO 100
+#define LIBAVFORMAT_VERSION_MICRO 101
 
 #define LIBAVFORMAT_VERSION_INT AV_VERSION_INT(LIBAVFORMAT_VERSION_MAJOR, \
                                                LIBAVFORMAT_VERSION_MINOR, \