changeset 1308:866d43ed0a67 libavformat

allow ffmpeg to read mp3s beginning with partial frames Patch by Andreas Oman andreas A olebyn P nu Original thread: Date: Sep 10, 2006 7:26 AM Subject: Re: [Ffmpeg-devel] [PATCH] allow ffmpeg to read mp3s beginning with partial frames
author gpoirier
date Sun, 10 Sep 2006 20:31:58 +0000
parents 59dbb78518d4
children 59b0e373c549
files mp3.c
diffstat 1 files changed, 27 insertions(+), 15 deletions(-) [+]
line wrap: on
line diff
--- a/mp3.c	Sun Sep 10 20:21:40 2006 +0000
+++ b/mp3.c	Sun Sep 10 20:31:58 2006 +0000
@@ -17,6 +17,7 @@
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 #include "avformat.h"
+#include "mpegaudio.h"
 
 #define ID3_HEADER_SIZE 10
 #define ID3_TAG_SIZE 128
@@ -243,27 +244,38 @@
 
 static int mp3_read_probe(AVProbeData *p)
 {
-    int d;
+    int max_frames;
+    int fsize, frames;
+    uint32_t header;
+    uint8_t *buf, *buf2, *end;
+    AVCodecContext avctx;
 
-    if(p->buf_size < 4)
+    if(p->buf_size < ID3_HEADER_SIZE)
         return 0;
 
-    if(p->buf[0] == 'I' && p->buf[1] == 'D' && p->buf[2] == '3' &&
-       p->buf[3] < 5)
+    if(id3_match(p->buf))
         return AVPROBE_SCORE_MAX;
 
-    if(p->buf[0] != 0xff)
-        return 0;
+    max_frames = 0;
+    buf = p->buf;
+    end = buf + FFMIN(4096, p->buf_size - sizeof(uint32_t));
+
+    for(; buf < end; buf++) {
+        buf2 = buf;
 
-    d = p->buf[1];
-    if((d & 0xe0) != 0xe0 || ((d & 0x18) == 0x08 || (d & 0x06) == 0))
-        return 0;
-
-    d = p->buf[2];
-    if((d & 0xf0) == 0xf0 || (d & 0x0c) == 0x0c)
-        return 0;
-
-    return AVPROBE_SCORE_MAX;
+        for(frames = 0; buf < end; frames++) {
+            header = (buf2[0] << 24) | (buf2[1] << 16) | (buf2[2] << 8) | buf2[3];
+            fsize = mpa_decode_header(&avctx, header);
+            if(fsize < 0)
+                break;
+            buf2 += fsize;
+        }
+        max_frames = FFMAX(max_frames, frames);
+    }
+    if     (max_frames>=3) return AVPROBE_SCORE_MAX/2+1;
+    else if(max_frames==2) return AVPROBE_SCORE_MAX/4;
+    else if(max_frames==1) return 1;
+    else                   return 0;
 }
 
 static int mp3_read_header(AVFormatContext *s,