From 7a4de1cae459fab89b84ab172f7112a6cd544664 Mon Sep 17 00:00:00 2001
From: Heesuk Jung <heesuk.jung@lge.com>
Date: Wed, 10 Oct 2012 05:35:41 -0700
Subject: [PATCH] Use sample size in case incorrect timestamps for aac in AVI
 (Ticket #1755)

In some case for aac in AVI, avidec extracts wrong PTS value.
(www.ffmpeg.org/trac/ffmpeg/ticket/1755)
In getDuration function, I suggest using sample size instead of len.

I have done regression test in our media pool.

Test result of 11 problematic files is ok after patch apply.
(11 files have fixed sample size in AVI Stream Header)

Regression test reseult of 4 non-problematic files is ok after patch apply.
(4 files have variable sample size in AVI Stream Header)
---
 libavformat/avidec.c |   16 ++++++++++------
 1 file changed, 10 insertions(+), 6 deletions(-)

diff --git a/libavformat/avidec.c b/libavformat/avidec.c
index b4c5fe8..43d80ce 100644
--- a/libavformat/avidec.c
+++ b/libavformat/avidec.c
@@ -115,9 +115,13 @@ static int guess_ni_flag(AVFormatContext *s);
            (tag >> 24) & 0xff,                          \
            size)
 
-static inline int get_duration(AVIStream *ast, int len){
+static inline int get_duration(AVIStream *ast, int len, enum AVCodecID codecId) {
     if(ast->sample_size){
-        return len;
+        if (codecId == AV_CODEC_ID_AAC) {
+            return ast->sample_size;
+        } else {
+            return len;
+        }
     }else if (ast->dshow_block_align){
         return (len + ast->dshow_block_align - 1)/ast->dshow_block_align;
     }else
@@ -208,7 +212,7 @@ static int read_braindead_odml_indx(AVFormatContext *s, int frame_num){
             if(last_pos != pos && (len || !ast->sample_size))
                 av_add_index_entry(st, pos, ast->cum_len, len, 0, key ? AVINDEX_KEYFRAME : 0);
 
-            ast->cum_len += get_duration(ast, len);
+            ast->cum_len += get_duration(ast, len, st->codec->codec_id);
             last_pos= pos;
         }else{
             int64_t offset, pos;
@@ -990,7 +994,7 @@ start_sync:
                /*|| (st->discard >= AVDISCARD_NONKEY && !(pkt->flags & AV_PKT_FLAG_KEY))*/ //FIXME needs a little reordering
                || st->discard >= AVDISCARD_ALL){
                 if (!exit_early) {
-                    ast->frame_offset += get_duration(ast, size);
+                    ast->frame_offset += get_duration(ast, size, st->codec->codec_id);
                 }
                 avio_skip(pb, size);
                 goto start_sync;
@@ -1206,7 +1210,7 @@ resync:
             } else {
                 pkt->flags |= AV_PKT_FLAG_KEY;
             }
-            ast->frame_offset += get_duration(ast, pkt->size);
+            ast->frame_offset += get_duration(ast, pkt->size, st->codec->codec_id);
         }
         ast->remaining -= err;
         if(!ast->remaining){
@@ -1301,7 +1305,7 @@ static int avi_read_idx1(AVFormatContext *s, int size)
             av_add_index_entry(st, pos, ast->cum_len, len, 0, (flags&AVIIF_INDEX) ? AVINDEX_KEYFRAME : 0);
             last_idx= pos;
         }
-        ast->cum_len += get_duration(ast, len);
+        ast->cum_len += get_duration(ast, len, st->codec->codec_id);
         last_pos= pos;
         anykey |= flags&AVIIF_INDEX;
     }
-- 
1.7.9.5

