OSDN Git Service

try harder to identify program streams that do not begin with a pack header
authorjstebbins <jstebbins@b64f7644-9d1e-0410-96f1-a4d463321fa5>
Fri, 12 Feb 2010 23:27:29 +0000 (23:27 +0000)
committerjstebbins <jstebbins@b64f7644-9d1e-0410-96f1-a4d463321fa5>
Fri, 12 Feb 2010 23:27:29 +0000 (23:27 +0000)
search deeper into the file for a pack header followed by another start code
prefix.  Fixes problem with file cut by StreamClip on non-pack header boundary

git-svn-id: svn://localhost/HandBrake/trunk@3118 b64f7644-9d1e-0410-96f1-a4d463321fa5

libhb/stream.c

index 07f07c7..29f2928 100644 (file)
@@ -354,9 +354,17 @@ static int hb_stream_check_for_ts(const uint8_t *buf)
 
 static int hb_stream_check_for_ps(const uint8_t *buf)
 {
-    // program streams should start with a PACK then some other mpeg start
-    // code (usually a SYS but that might be missing if we only have a clip).
-    return check_ps_sync(buf) && check_ps_sc(buf);
+    // transport streams should have a sync byte every 188 bytes.
+    // search the first 8KB of buf looking for at least 8 consecutive
+    // correctly located sync patterns.
+    int offset = 0;
+
+    for ( offset = 0; offset < 8*1024-24; ++offset )
+    {
+        if ( check_ps_sync( &buf[offset] ) && check_ps_sc( &buf[offset] ) )
+            return 1;
+    }
+    return 0;
 }
 
 static int hb_stream_check_for_dvd_ps(const uint8_t *buf)
@@ -370,6 +378,7 @@ static int hb_stream_check_for_dvd_ps(const uint8_t *buf)
 static int hb_stream_get_type(hb_stream_t *stream)
 {
     uint8_t buf[2048*4];
+    int i = 64;
 
     if ( fread(buf, 1, sizeof(buf), stream->file_handle) == sizeof(buf) )
     {
@@ -395,12 +404,17 @@ static int hb_stream_get_type(hb_stream_t *stream)
             stream->hb_stream_type = dvd_program;
             return 1;
         }
-        if ( hb_stream_check_for_ps(buf) != 0 )
+        do
         {
-            hb_log("file is MPEG Program Stream");
-            stream->hb_stream_type = program;
-            return 1;
-        }
+            if ( hb_stream_check_for_ps(buf) != 0 )
+            {
+                hb_log("file is MPEG Program Stream");
+                stream->hb_stream_type = program;
+                return 1;
+            }
+            // Seek back to handle start codes that run over end of last buffer
+            fseek( stream->file_handle, -28, SEEK_CUR );
+        } while ( --i && fread(buf, 1, sizeof(buf), stream->file_handle) == sizeof(buf) );
     }
     return 0;
 }