OSDN Git Service

disable asserts in libdvdnav except when configured with --debug=max
[handbrake-jp/handbrake-jp-git.git] / libhb / stream.c
index ff593b2..998aac4 100644 (file)
@@ -207,6 +207,7 @@ static void ffmpeg_close( hb_stream_t *d );
 static hb_title_t *ffmpeg_title_scan( hb_stream_t *stream );
 static int ffmpeg_read( hb_stream_t *stream, hb_buffer_t *buf );
 static int ffmpeg_seek( hb_stream_t *stream, float frac );
+static int ffmpeg_seek_ts( hb_stream_t *stream, int64_t ts );
 
 /*
  * streams have a bunch of state that's learned during the scan. We don't
@@ -353,9 +354,16 @@ 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);
+    // 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). 
+    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)
@@ -369,6 +377,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) )
     {
@@ -394,12 +403,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;
 }
@@ -652,6 +666,7 @@ hb_title_t * hb_stream_title_scan(hb_stream_t *stream)
 
     // 'Barebones Title'
     hb_title_t *aTitle = hb_title_init( stream->path, 0 );
+    aTitle->type = HB_STREAM_TYPE;
     aTitle->index = 1;
 
        // Copy part of the stream path to the title name
@@ -1190,6 +1205,37 @@ int hb_stream_read( hb_stream_t * src_stream, hb_buffer_t * b )
                 ep = b->data + b->alloc;
             }
             *cp++ = c;
+            // Non-video streams can emulate start codes, so we need
+            // to inspect PES packets and skip over their data
+            // sections to avoid mis-detection of the next pack header.
+            if ( ( strt_code >> 8 ) == 0x000001 &&
+                 ( strt_code & 0xff ) >= 0xbb )
+            {
+                int len = 0;
+                c = getc_unlocked( src_stream->file_handle );
+                if ( c == EOF )
+                    break;
+                len = c << 8;
+                c = getc_unlocked( src_stream->file_handle );
+                if ( c == EOF )
+                    break;
+                len |= c;
+                if ( cp+len+2 > ep )
+                {
+                    // need to expand the buffer
+                    int curSize = cp - b->data;
+                    if ( curSize * 2 > curSize+len+2 )
+                        hb_buffer_realloc( b, curSize * 2 );
+                    else
+                        hb_buffer_realloc( b, curSize + len + 2 );
+                    cp = b->data + curSize;
+                    ep = b->data + b->alloc;
+                }
+                *cp++ = len >> 8;
+                *cp++ = len & 0xff;
+                fread( cp, 1, len, src_stream->file_handle );
+                cp += len;
+            }
         }
         funlockfile( src_stream->file_handle );
 
@@ -1199,7 +1245,8 @@ int hb_stream_read( hb_stream_t * src_stream, hb_buffer_t * b )
         if ( c != EOF )
         {
             fseeko( src_stream->file_handle, -4, SEEK_CUR );
-            b->size -= 4;
+            // Only 3 of the 4 bytes read were added to the buffer.
+            b->size -= 3;
         }
         return 1;
     }
@@ -1240,6 +1287,17 @@ int hb_stream_seek_chapter( hb_stream_t * stream, int chapter_num )
     {
         av_seek_frame( stream->ffmpeg_ic, -1, pos, 0);
     }
+    else
+    {
+        // ffmpeg has a bug that causes the first PTS after
+        // av_find_stream_info() is called to be incorrect.
+        // av_find_stream_info is called whenever opening a file
+        // with ffmpeg.  av_seek_frame clears the condition
+        // that causes the problem. since hb_stream_seek_chapter
+        // is called before we start reading, make sure
+        // we do a seek here.
+        av_seek_frame( stream->ffmpeg_ic, -1, 0LL, AVSEEK_FLAG_BACKWARD );
+    }
     return 1;
 }
 
@@ -1308,6 +1366,15 @@ int hb_stream_seek( hb_stream_t * stream, float f )
     return 1;
 }
 
+int hb_stream_seek_ts( hb_stream_t * stream, int64_t ts )
+{
+       if ( stream->hb_stream_type == ffmpeg )
+    {
+        return ffmpeg_seek_ts( stream, ts );
+    }
+    return -1;
+}
+
 static const char* make_upper( const char* s )
 {
     static char name[8];
@@ -2702,6 +2769,7 @@ static hb_title_t *ffmpeg_title_scan( hb_stream_t *stream )
 
     // 'Barebones Title'
     hb_title_t *title = hb_title_init( stream->path, 0 );
+    title->type = HB_STREAM_TYPE;
     title->index = 1;
 
        // Copy part of the stream path to the title name
@@ -2927,6 +2995,10 @@ static int ffmpeg_read( hb_stream_t *stream, hb_buffer_t *buf )
     {
         buf->start = buf->renderOffset;
     }
+    else if ( buf->renderOffset == -1 && buf->start >= 0 )
+    {
+        buf->renderOffset = buf->start;
+    }
 
     /*
      * Check to see whether this video buffer is on a chapter
@@ -2977,3 +3049,16 @@ static int ffmpeg_seek( hb_stream_t *stream, float frac )
     }
     return 1;
 }
+
+// Assumes that we are always seeking forward
+static int ffmpeg_seek_ts( hb_stream_t *stream, int64_t ts )
+{
+    AVFormatContext *ic = stream->ffmpeg_ic;
+    int64_t pos;
+
+    pos = ts * AV_TIME_BASE / 90000;
+    stream->need_keyframe = 1;
+    // Seek to the nearest timestamp before that requested where
+    // there is an I-frame
+    return av_seek_frame( ic, -1, pos, AVSEEK_FLAG_BACKWARD );
+}