OSDN Git Service

LinGui: Changes required by latest x264
[handbrake-jp/handbrake-jp-git.git] / libhb / dvd.c
index b181402..c21a245 100644 (file)
@@ -1,7 +1,7 @@
 /* $Id: dvd.c,v 1.12 2005/11/25 15:05:25 titer Exp $
 
    This file is part of the HandBrake source code.
-   Homepage: <http://handbrake.m0k.org/>.
+   Homepage: <http://handbrake.fr/>.
    It may be used under the terms of the GNU General Public License. */
 
 #include "hb.h"
@@ -83,7 +83,10 @@ hb_dvd_t * hb_dvd_init( char * path )
     /* Open device */
     if( !( d->reader = DVDOpen( path ) ) )
     {
-        hb_error( "dvd: DVDOpen failed (%s)", path );
+        /*
+         * Not an error, may be a stream - which we'll try in a moment.
+         */
+        hb_log( "dvd: not a dvd - trying as a stream/file instead" );
         goto fail;
     }
 
@@ -122,7 +125,7 @@ hb_title_t * hb_dvd_title_scan( hb_dvd_t * d, int t )
     hb_title_t   * title;
     ifo_handle_t * vts = NULL;
     int            pgc_id, pgn, i;
-    hb_chapter_t * chapter, * chapter_old;
+    hb_chapter_t * chapter;
     int            c;
     uint64_t       duration;
     float          duration_correction;
@@ -154,7 +157,7 @@ hb_title_t * hb_dvd_title_scan( hb_dvd_t * d, int t )
         hb_error("Invalid VTS (title set) number: %i", title->vts);
         goto fail;
     }
-    
+
     hb_log( "scan: opening IFO for VTS %d", title->vts );
     if( !( vts = ifoOpen( d->reader, title->vts ) ) )
     {
@@ -177,7 +180,13 @@ hb_title_t * hb_dvd_title_scan( hb_dvd_t * d, int t )
         hb_error( "scan: pgc not valid, skipping" );
         goto fail;
     }
+
+    if( pgn <= 0 || pgn > 99 )
+    {
+        hb_error( "scan: pgn %d not valid, skipping", pgn );
+        goto fail;
+    }
+
     /* Start cell */
     title->cell_start  = d->pgc->program_map[pgn-1] - 1;
     title->block_start = d->pgc->cell_playback[title->cell_start].first_sector;
@@ -251,28 +260,28 @@ hb_title_t * hb_dvd_title_scan( hb_dvd_t * d, int t )
         {
             case 0x00:
                 audio->id    = ( ( 0x80 + position ) << 8 ) | 0xbd;
-                audio->codec = HB_ACODEC_AC3;
+                audio->config.in.codec = HB_ACODEC_AC3;
                 break;
 
             case 0x02:
             case 0x03:
                 audio->id    = 0xc0 + position;
-                audio->codec = HB_ACODEC_MPGA;
+                audio->config.in.codec = HB_ACODEC_MPGA;
                 break;
 
             case 0x04:
                 audio->id    = ( ( 0xa0 + position ) << 8 ) | 0xbd;
-                audio->codec = HB_ACODEC_LPCM;
+                audio->config.in.codec = HB_ACODEC_LPCM;
                 break;
 
             case 0x06:
                 audio->id    = ( ( 0x88 + position ) << 8 ) | 0xbd;
-                audio->codec = HB_ACODEC_DCA;
+                audio->config.in.codec = HB_ACODEC_DCA;
                 break;
 
             default:
                 audio->id    = 0;
-                audio->codec = 0;
+                audio->config.in.codec = 0;
                 hb_log( "scan: unknown audio codec (%x)",
                         audio_format );
                 break;
@@ -302,19 +311,20 @@ hb_title_t * hb_dvd_title_scan( hb_dvd_t * d, int t )
 
         lang = lang_for_code( vts->vtsi_mat->vts_audio_attr[i].lang_code );
 
-        snprintf( audio->lang, sizeof( audio->lang ), "%s (%s)",
+        snprintf( audio->config.lang.description, sizeof( audio->config.lang.description ), "%s (%s)",
             strlen(lang->native_name) ? lang->native_name : lang->eng_name,
-            audio->codec == HB_ACODEC_AC3 ? "AC3" : ( audio->codec ==
-                HB_ACODEC_DCA ? "DTS" : ( audio->codec ==
+            audio->config.in.codec == HB_ACODEC_AC3 ? "AC3" : ( audio->config.in.codec ==
+                HB_ACODEC_DCA ? "DTS" : ( audio->config.in.codec ==
                 HB_ACODEC_MPGA ? "MPEG" : "LPCM" ) ) );
-        snprintf( audio->lang_simple, sizeof( audio->lang_simple ), "%s",
+        snprintf( audio->config.lang.simple, sizeof( audio->config.lang.simple ), "%s",
                   strlen(lang->native_name) ? lang->native_name : lang->eng_name );
-        snprintf( audio->iso639_2, sizeof( audio->iso639_2 ), "%s",
+        snprintf( audio->config.lang.iso639_2, sizeof( audio->config.lang.iso639_2 ), "%s",
                   lang->iso639_2);
 
         hb_log( "scan: id=%x, lang=%s, 3cc=%s", audio->id,
-                audio->lang, audio->iso639_2 );
+                audio->config.lang.description, audio->config.lang.iso639_2 );
 
+        audio->config.in.track = i;
         hb_list_add( title->list_audio, audio );
     }
 
@@ -478,17 +488,17 @@ hb_title_t * hb_dvd_title_scan( hb_dvd_t * d, int t )
     switch( vts->vtsi_mat->vts_video_attr.display_aspect_ratio )
     {
         case 0:
-            title->aspect = HB_ASPECT_BASE * 4 / 3;
+            title->container_aspect = 4. / 3.;
             break;
         case 3:
-            title->aspect = HB_ASPECT_BASE * 16 / 9;
+            title->container_aspect = 16. / 9.;
             break;
         default:
             hb_log( "scan: unknown aspect" );
             goto fail;
     }
 
-    hb_log( "scan: aspect = %d", title->aspect );
+    hb_log( "scan: aspect = %g", title->aspect );
 
     /* This title is ok so far */
     goto cleanup;
@@ -637,7 +647,7 @@ int hb_dvd_seek( hb_dvd_t * d, float f )
 int is_nav_pack( unsigned char *buf )
 {
     /*
-     * The NAV Pack is comprised of the PCI Packet and DSI Packet, both 
+     * The NAV Pack is comprised of the PCI Packet and DSI Packet, both
      * of these start at known offsets and start with a special identifier.
      *
      * NAV = {
@@ -694,7 +704,7 @@ int hb_dvd_read( hb_dvd_t * d, hb_buffer_t * b )
         // should check and discover we're at eof.
         if ( d->cell_cur > d->cell_end )
             return 0;
-        
+
         for( ;; )
         {
             int block, pack_len, next_vobu, read_retry;
@@ -715,14 +725,22 @@ int hb_dvd_read( hb_dvd_t * d, hb_buffer_t * b )
 
             if( read_retry == 3 )
             {
-                hb_log( "dvd: Unrecoverable Read Error from DVD (blk: %d)", d->next_vobu );
-                return 0;
+                hb_log( "dvd: vobu read error blk %d - skipping to cell %d",
+                        d->next_vobu, d->cell_next );
+                d->cell_cur  = d->cell_next;
+                if ( d->cell_cur > d->cell_end )
+                    return 0;
+                d->in_cell = 0;
+                d->next_vobu = d->pgc->cell_playback[d->cell_cur].first_sector;
+                FindNextCell( d );
+                d->cell_overlap = 1;
+                continue;
             }
 
-            if ( !is_nav_pack( b->data ) ) { 
+            if ( !is_nav_pack( b->data ) ) {
                 (d->next_vobu)++;
                 if( d->in_sync == 1 ) {
-                    hb_log("dvd: Lost sync, searching for NAV pack at blk %d", 
+                    hb_log("dvd: Lost sync, searching for NAV pack at blk %d",
                            d->next_vobu);
                     d->in_sync = 0;
                 }
@@ -730,7 +748,7 @@ int hb_dvd_read( hb_dvd_t * d, hb_buffer_t * b )
             }
 
             navRead_DSI( &dsi_pack, &b->data[DSI_START_BYTE] );
-            
+
             if ( d->in_sync == 0 && d->cur_cell_id &&
                  (d->cur_vob_id != dsi_pack.dsi_gi.vobu_vob_idn ||
                   d->cur_cell_id != dsi_pack.dsi_gi.vobu_c_idn ) )
@@ -841,7 +859,7 @@ int hb_dvd_read( hb_dvd_t * d, hb_buffer_t * b )
             if ( d->pgc->cell_playback[d->cell_cur].first_sector < dsi_pack.dsi_gi.nv_pck_lbn &&
                  d->pgc->cell_playback[d->cell_cur].last_sector >= dsi_pack.dsi_gi.nv_pck_lbn )
             {
-                hb_log( "dvd: null prev_vobu in cell %d at block %d", d->cell_cur, 
+                hb_log( "dvd: null prev_vobu in cell %d at block %d", d->cell_cur,
                         d->block );
                 // treat like end-of-cell then go directly to start of next cell.
                 d->cell_cur  = d->cell_next;
@@ -853,11 +871,14 @@ int hb_dvd_read( hb_dvd_t * d, hb_buffer_t * b )
             }
             else
             {
-                hb_log( "dvd: Beginning of Cell (%d) at block %d", d->cell_cur, 
-                       d->block );
+                if ( d->block != d->pgc->cell_playback[d->cell_cur].first_sector )
+                {
+                    hb_log( "dvd: beginning of cell %d at block %d", d->cell_cur,
+                           d->block );
+                }
                 if( d->in_cell )
                 {
-                    hb_error( "dvd: Assuming missed End of Cell (%d)", d->cell_cur );
+                    hb_error( "dvd: assuming missed end of cell %d", d->cell_cur );
                     d->cell_cur  = d->cell_next;
                     d->next_vobu = d->pgc->cell_playback[d->cell_cur].first_sector;
                     FindNextCell( d );
@@ -879,15 +900,19 @@ int hb_dvd_read( hb_dvd_t * d, hb_buffer_t * b )
 
         if( ( dsi_pack.vobu_sri.next_vobu & (1 << 31 ) ) == 0 ||
             ( dsi_pack.vobu_sri.next_vobu & 0x3fffffff ) == 0x3fffffff )
-        { 
-            hb_log( "dvd: End of Cell (%d) at block %d", d->cell_cur, 
-                    d->block );
+        {
+            if ( d->block <= d->pgc->cell_playback[d->cell_cur].first_sector ||
+                 d->block > d->pgc->cell_playback[d->cell_cur].last_sector )
+            {
+                hb_log( "dvd: end of cell %d at block %d", d->cell_cur,
+                        d->block );
+            }
             d->cell_cur  = d->cell_next;
             d->in_cell = 0;
             d->next_vobu = d->pgc->cell_playback[d->cell_cur].first_sector;
             FindNextCell( d );
             d->cell_overlap = 1;
-            
+
         }
     }
     else
@@ -947,16 +972,16 @@ int hb_dvd_chapter( hb_dvd_t * d )
 /***********************************************************************
  * hb_dvd_is_break
  ***********************************************************************
- * Returns 1 if the current block is a new chapter start
+ * Returns chapter number if the current block is a new chapter start
  **********************************************************************/
 int hb_dvd_is_break( hb_dvd_t * d )
 {
-    int     i, j;
+    int     i;
     int     pgc_id, pgn;
        int     nr_of_ptts = d->ifo->vts_ptt_srpt->title[d->ttn-1].nr_of_ptts;
     pgc_t * pgc;
-    int     cell, chapter_length, cell_end;
-    
+    int     cell;
+
     for( i = nr_of_ptts - 1;
          i > 0;
          i-- )
@@ -973,11 +998,10 @@ int hb_dvd_is_break( hb_dvd_t * d )
         // This must not match against the start cell.
         if( pgc->cell_playback[cell].first_sector == d->block && cell != d->cell_start )
         {
-            hb_log("dvd: Chapter Break Cell Found");
-            return 1;
+            return i + 1;
         }
     }
-    
+
     return 0;
 }
 
@@ -1023,8 +1047,8 @@ static void FindNextCell( hb_dvd_t * d )
              i++;
         }
         d->cell_next = d->cell_cur + i + 1;
-        hb_log( "dvd: Skipping multi-angle cells %d-%d", 
-                d->cell_cur, 
+        hb_log( "dvd: Skipping multi-angle cells %d-%d",
+                d->cell_cur,
                 d->cell_next - 1 );
     }
     else