changeset 329:d20a3caaef29 libavformat

better get_packet_size and probe
author michael
date Tue, 09 Dec 2003 00:17:43 +0000
parents 137b4ce31b0f
children c393ab803bcd
files mpegts.c
diffstat 1 files changed, 48 insertions(+), 17 deletions(-) [+]
line wrap: on
line diff
--- a/mpegts.c	Mon Dec 08 17:54:40 2003 +0000
+++ b/mpegts.c	Tue Dec 09 00:17:43 2003 +0000
@@ -218,24 +218,45 @@
     ts->pids[pid] = NULL;
 }
 
+static int analyze(const uint8_t *buf, int size, int packet_size, int *index){
+    int stat[packet_size];
+    int i;
+    int x=0;
+    int best_score=0;
+
+    memset(stat, 0, packet_size*sizeof(int));
+
+    for(x=i=0; i<size; i++){
+        if(buf[i] == 0x47){
+            stat[x]++;
+            if(stat[x] > best_score){
+                best_score= stat[x];
+                if(index) *index= x;
+            }
+        }
+
+        x++;
+        if(x == packet_size) x= 0;
+    }
+
+    return best_score;
+}
+
 /* autodetect fec presence. Must have at least 1024 bytes  */
 static int get_packet_size(const uint8_t *buf, int size)
 {
-    int i;
+    int score, fec_score;
 
     if (size < (TS_FEC_PACKET_SIZE * 5 + 1))
         return -1;
-    for(i=0;i<5;i++) {
-        if (buf[i * TS_PACKET_SIZE] != 0x47)
-            goto try_fec;
-    }
-    return TS_PACKET_SIZE;
- try_fec:
-    for(i=0;i<5;i++) {
-        if (buf[i * TS_FEC_PACKET_SIZE] != 0x47)
-            return -1;
-    }
-    return TS_FEC_PACKET_SIZE;
+        
+    score    = analyze(buf, size, TS_PACKET_SIZE, NULL);
+    fec_score= analyze(buf, size, TS_FEC_PACKET_SIZE, NULL);
+//    av_log(NULL, AV_LOG_DEBUG, "score: %d, fec_score: %d \n", score, fec_score);
+    
+    if     (score > fec_score) return TS_PACKET_SIZE;
+    else if(score < fec_score) return TS_FEC_PACKET_SIZE;
+    else                       return -1;
 }
 
 typedef struct SectionHeader {
@@ -988,11 +1009,21 @@
 static int mpegts_probe(AVProbeData *p)
 {
 #if 1
-    int size;
-    size = get_packet_size(p->buf, p->buf_size);
-    if (size < 0)
-        return 0;
-    return AVPROBE_SCORE_MAX - 1;
+    const int size= p->buf_size;
+    int score, fec_score;
+#define CHECK_COUNT 10
+    
+    if (size < (TS_FEC_PACKET_SIZE * CHECK_COUNT))
+        return -1;
+    
+    score    = analyze(p->buf, TS_PACKET_SIZE    *CHECK_COUNT, TS_PACKET_SIZE, NULL);
+    fec_score= analyze(p->buf, TS_FEC_PACKET_SIZE*CHECK_COUNT, TS_FEC_PACKET_SIZE, NULL);
+//    av_log(NULL, AV_LOG_DEBUG, "score: %d, fec_score: %d \n", score, fec_score);
+  
+// we need a clear definition for the returned score otherwise things will become messy sooner or later
+    if     (score > fec_score && score > 6) return AVPROBE_SCORE_MAX + score     - CHECK_COUNT;
+    else if(                 fec_score > 6) return AVPROBE_SCORE_MAX + fec_score - CHECK_COUNT;
+    else                                    return -1;
 #else
     /* only use the extension for safer guess */
     if (match_ext(p->filename, "ts"))