[FFmpeg-devel] [PATCH] avformat/aacdec: don't immediately abort if an ADTS frame is not found

James Almer jamrial at gmail.com
Mon Sep 4 05:47:36 EEST 2017


Skip the invalid data in an attempt to find one instead, and continue decoding from there.

Fixes ticket #6634

Signed-off-by: James Almer <jamrial at gmail.com>
---
 libavformat/aacdec.c | 41 +++++++++++++++++++++++++++++------------
 1 file changed, 29 insertions(+), 12 deletions(-)

diff --git a/libavformat/aacdec.c b/libavformat/aacdec.c
index 364b33404f..7aacc88560 100644
--- a/libavformat/aacdec.c
+++ b/libavformat/aacdec.c
@@ -77,10 +77,29 @@ static int adts_aac_probe(AVProbeData *p)
         return 0;
 }
 
+static int resync(AVFormatContext *s)
+{
+    uint16_t state;
+
+    state = avio_r8(s->pb);
+    while (!avio_feof(s->pb) && avio_tell(s->pb) < s->probesize) {
+        state = (state << 8) | avio_r8(s->pb);
+        if ((state >> 4) != 0xFFF)
+            continue;
+        avio_seek(s->pb, -2, SEEK_CUR);
+        break;
+    }
+
+    if ((state >> 4) != 0xFFF)
+        return avio_feof(s->pb) ? AVERROR_EOF : AVERROR_INVALIDDATA;
+
+    return 0;
+}
+
 static int adts_aac_read_header(AVFormatContext *s)
 {
     AVStream *st;
-    uint16_t state;
+    int ret;
 
     st = avformat_new_stream(s, NULL);
     if (!st)
@@ -99,16 +118,9 @@ static int adts_aac_read_header(AVFormatContext *s)
     }
 
     // skip data until the first ADTS frame is found
-    state = avio_r8(s->pb);
-    while (!avio_feof(s->pb) && avio_tell(s->pb) < s->probesize) {
-        state = (state << 8) | avio_r8(s->pb);
-        if ((state >> 4) != 0xFFF)
-            continue;
-        avio_seek(s->pb, -2, SEEK_CUR);
-        break;
-    }
-    if ((state >> 4) != 0xFFF)
-        return AVERROR_INVALIDDATA;
+    ret = resync(s);
+    if (ret < 0)
+        return ret;
 
     // LCM of all possible ADTS sample rates
     avpriv_set_pts_info(st, 64, 1, 28224000);
@@ -129,8 +141,13 @@ static int adts_aac_read_packet(AVFormatContext *s, AVPacket *pkt)
     }
 
     if ((AV_RB16(pkt->data) >> 4) != 0xfff) {
+        // try resyncing until we find an ADTS frame.
         av_packet_unref(pkt);
-        return AVERROR_INVALIDDATA;
+        avio_seek(s->pb, -ret, SEEK_CUR);
+        ret = resync(s);
+        if (ret < 0)
+            return ret;
+        return adts_aac_read_packet(s, pkt);
     }
 
     fsize = (AV_RB32(pkt->data + 3) >> 13) & 0x1FFF;
-- 
2.13.3



More information about the ffmpeg-devel mailing list