]> git.sesse.net Git - vlc/blobdiff - modules/access/vcd/vcd.c
* INSTALL.win32: added a small note about running vlc under the msvc debugger.
[vlc] / modules / access / vcd / vcd.c
index 901170698ffc4afd41a606c4bff9fc3d227048db..57e1cbf534f479344e134126ae312cffa30296be 100644 (file)
@@ -2,7 +2,7 @@
  * vcd.c : VCD input module for vlc
  *****************************************************************************
  * Copyright (C) 2000 VideoLAN
- * $Id: vcd.c,v 1.7 2002/10/15 19:56:59 gbazin Exp $
+ * $Id: vcd.c,v 1.22 2003/05/22 12:00:57 gbazin Exp $
  *
  * Author: Johan Bilien <jobi@via.ecp.fr>
  *
@@ -10,7 +10,7 @@
  * it under the terms of the GNU General Public License as published by
  * the Free Software Foundation; either version 2 of the License, or
  * (at your option) any later version.
- * 
+ *
  * This program is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
@@ -38,7 +38,7 @@
 
 #include <string.h>
 
-#include "vcd.h"
+#include "cdrom.h"
 
 /* how many blocks VCDRead will read in each loop */
 #define VCD_BLOCKS_ONCE 20
 typedef struct thread_vcd_data_s
 {
     vcddev_t    *vcddev;                            /* vcd device descriptor */
-    int         nb_tracks;                          /* Nb of tracks (titles) */
+    int         i_nb_tracks;                        /* Nb of tracks (titles) */
     int         i_track;                                    /* Current track */
     int         i_sector;                                  /* Current Sector */
     int *       p_sectors;                                  /* Track sectors */
+    int         i_entries_nb;                      /* Number of entry points */
+    int *       p_entries;                                   /* Entry points */
+    vlc_bool_t  b_valid_ep;                       /* Valid entry points flag */
     vlc_bool_t  b_end_of_track;           /* If the end of track was reached */
 
 } thread_vcd_data_t;
@@ -67,12 +70,13 @@ static int  VCDRead         ( input_thread_t *, byte_t *, size_t );
 static void VCDSeek         ( input_thread_t *, off_t );
 static int  VCDSetArea      ( input_thread_t *, input_area_t * );
 static int  VCDSetProgram   ( input_thread_t *, pgrm_descriptor_t * );
+static int  VCDEntryPoints  ( input_thread_t * );
 
 /*****************************************************************************
  * Module descriptior
  *****************************************************************************/
 vlc_module_begin();
-    set_description( _("VCD input module") );
+    set_description( _("VCD input") );
     set_capability( "access", 80 );
     set_callbacks( VCDOpen, VCDClose );
     add_shortcut( "svcd" );
@@ -97,26 +101,16 @@ static int VCDOpen( vlc_object_t *p_this )
     input_area_t *          p_area;
     int                     i_title = 1;
     int                     i_chapter = 1;
-
-    p_input->pf_read = VCDRead;
-    p_input->pf_seek = VCDSeek;
-    p_input->pf_set_area = VCDSetArea;
-    p_input->pf_set_program = VCDSetProgram;
-
-#ifdef WIN32
-    /* On Win32 we want the VCD access plugin to be explicitly requested,
-     * we end up with lots of problems otherwise */
-    if( !p_input->psz_access || !*p_input->psz_access ) return( -1 );
-#endif
+    vcddev_t                *vcddev;
 
     /* parse the options passed in command line : */
     psz_orig = psz_parser = psz_source = strdup( p_input->psz_name );
-    
+
     if( !psz_orig )
     {
         return( -1 );
     }
+
     while( *psz_parser && *psz_parser != '@' )
     {
         psz_parser++;
@@ -150,53 +144,59 @@ static int VCDOpen( vlc_object_t *p_this )
         if( !psz_source ) return -1;
     }
 
-    p_vcd = malloc( sizeof(thread_vcd_data_t) );
+    /* Open VCD */
+    if( !(vcddev = ioctl_Open( p_this, psz_source )) )
+    {
+        msg_Warn( p_input, "could not open %s", psz_source );
+        free( psz_source );
+        return -1;
+    }
 
+    p_vcd = malloc( sizeof(thread_vcd_data_t) );
     if( p_vcd == NULL )
     {
         msg_Err( p_input, "out of memory" );
         free( psz_source );
         return -1;
     }
-    
+    free( psz_source );
+
+    p_vcd->vcddev = vcddev;
     p_input->p_access_data = (void *)p_vcd;
-    
+
     p_input->i_mtu = VCD_DATA_ONCE;
-   
-    vlc_mutex_lock( &p_input->stream.stream_lock );
 
+    vlc_mutex_lock( &p_input->stream.stream_lock );
     p_input->stream.b_pace_control = 1;
-
     p_input->stream.b_seekable = 1;
     p_input->stream.p_selected_area->i_size = 0;
     p_input->stream.p_selected_area->i_tell = 0;
-
     vlc_mutex_unlock( &p_input->stream.stream_lock );
 
-    if( !(p_vcd->vcddev = ioctl_Open( p_this, psz_source )) )
-    {
-        msg_Err( p_input, "could not open %s", psz_source );
-        free( psz_source );
-        free( p_vcd );
-        return -1;
-    }
-
     /* We read the Table Of Content information */
-    p_vcd->nb_tracks = ioctl_GetTracksMap( VLC_OBJECT(p_input),
+    p_vcd->i_nb_tracks = ioctl_GetTracksMap( VLC_OBJECT(p_input),
                                            p_vcd->vcddev, &p_vcd->p_sectors );
-    free( psz_source );
-    if( p_vcd->nb_tracks < 0 )
+    if( p_vcd->i_nb_tracks < 0 )
         msg_Err( p_input, "unable to count tracks" );
-    else if( p_vcd->nb_tracks <= 1 )
+    else if( p_vcd->i_nb_tracks <= 1 )
         msg_Err( p_input, "no movie tracks found" );
-    if( p_vcd->nb_tracks <= 1)
+    if( p_vcd->i_nb_tracks <= 1)
     {
-        //input_BuffersEnd( p_input, p_input->p_method_data ); /* ??? */
         ioctl_Close( p_this, p_vcd->vcddev );
         free( p_vcd );
         return -1;
     }
 
+    /* Allocate the entry points table */
+    p_vcd->p_entries = malloc( p_vcd->i_nb_tracks * sizeof( int ) );
+
+    if( p_vcd->p_entries == NULL )
+    {
+        msg_Err( p_input, "not enough memory" );
+        ioctl_Close( p_this, p_vcd->vcddev );
+        free( p_vcd );
+    }
+
     /* Set stream and area data */
     vlc_mutex_lock( &p_input->stream.stream_lock );
 
@@ -206,34 +206,50 @@ static int VCDOpen( vlc_object_t *p_this )
     /* disc input method */
     p_input->stream.i_method = INPUT_METHOD_VCD;
 
+    p_input->stream.i_area_nb = 1;
+
 #define area p_input->stream.pp_areas
-    for( i = 1 ; i <= p_vcd->nb_tracks - 1 ; i++ )
+    for( i = 1 ; i <= p_vcd->i_nb_tracks - 1 ; i++ )
     {
-        input_AddArea( p_input );
-
         /* Titles are Program Chains */
-        area[i]->i_id = i;
+        input_AddArea( p_input, i, 1 );
 
         /* Absolute start offset and size */
         area[i]->i_start = (off_t)p_vcd->p_sectors[i] * (off_t)VCD_DATA_SIZE;
         area[i]->i_size = (off_t)(p_vcd->p_sectors[i+1] - p_vcd->p_sectors[i])
                            * (off_t)VCD_DATA_SIZE;
 
-        /* Number of chapters */
-        area[i]->i_part_nb = 0;   /* will be the entry points */
+        /* Default Chapter */
         area[i]->i_part = 1;
 
-        area[i]->i_plugin_data = p_vcd->p_sectors[i];
+        /* i_plugin_data is used to store which entry point is the first
+         * of the track (area) */
+        area[i]->i_plugin_data = 0;
     }
 #undef area
 
     p_area = p_input->stream.pp_areas[i_title];
 
+    p_vcd->b_valid_ep = 1;
+    if( VCDEntryPoints( p_input ) < 0 )
+    {
+        msg_Warn( p_input, "could not read entry points, will not use them" );
+        p_vcd->b_valid_ep = 0;
+    }
+
     VCDSetArea( p_input, p_area );
 
     vlc_mutex_unlock( &p_input->stream.stream_lock );
 
-    p_input->psz_demux = "ps";
+    if( !p_input->psz_demux || !*p_input->psz_demux )
+    {
+        p_input->psz_demux = "ps";
+    }
+
+    p_input->pf_read = VCDRead;
+    p_input->pf_seek = VCDSeek;
+    p_input->pf_set_area = VCDSetArea;
+    p_input->pf_set_program = VCDSetProgram;
 
     return 0;
 }
@@ -256,7 +272,7 @@ static void VCDClose( vlc_object_t *p_this )
  * Returns -1 in case of error, 0 in case of EOF, otherwise the number of
  * bytes.
  *****************************************************************************/
-static int VCDRead( input_thread_t * p_input, byte_t * p_buffer, 
+static int VCDRead( input_thread_t * p_input, byte_t * p_buffer,
                      size_t i_len )
 {
     thread_vcd_data_t *     p_vcd;
@@ -273,10 +289,11 @@ static int VCDRead( input_thread_t * p_input, byte_t * p_buffer,
 
     i_blocks = i_len / VCD_DATA_SIZE;
 
-    for ( i_index = 0 ; i_index < i_blocks ; i_index++ ) 
+    for ( i_index = 0 ; i_index < i_blocks ; i_index++ )
     {
-        if ( ioctl_ReadSector( VLC_OBJECT(p_input), p_vcd->vcddev,
-                    p_vcd->i_sector, p_buffer + i_index * VCD_DATA_SIZE ) < 0 )
+        if ( ioctl_ReadSectors( VLC_OBJECT(p_input), p_vcd->vcddev,
+             p_vcd->i_sector, p_buffer + i_index * VCD_DATA_SIZE, 1,
+             VCD_TYPE ) < 0 )
         {
             msg_Err( p_input, "could not read sector %d", p_vcd->i_sector );
             return -1;
@@ -286,44 +303,63 @@ static int VCDRead( input_thread_t * p_input, byte_t * p_buffer,
         if ( p_vcd->i_sector == p_vcd->p_sectors[p_vcd->i_track + 1] )
         {
             input_area_t *p_area;
-            
-            if ( p_vcd->i_track >= p_vcd->nb_tracks - 1 )
+
+            if ( p_vcd->i_track >= p_vcd->i_nb_tracks - 1 )
                 return 0; /* EOF */
-            
+
+            vlc_mutex_lock( &p_input->stream.stream_lock );
             p_area = p_input->stream.pp_areas[
                     p_input->stream.p_selected_area->i_id + 1 ];
-            
+
             msg_Dbg( p_input, "new title" );
-            
+
             p_area->i_part = 1;
             VCDSetArea( p_input, p_area );
-    
+            vlc_mutex_unlock( &p_input->stream.stream_lock );
         }
+
+        /* Update chapter */
+        else if( p_vcd->b_valid_ep &&
+                /* FIXME kludge so that read does not update chapter
+                 * when a manual chapter change was requested and not
+                 * yet accomplished */
+                !p_input->stream.p_new_area )
+        {
+            int i_entry;
+
+            vlc_mutex_lock( &p_input->stream.stream_lock );
+            i_entry = p_input->stream.p_selected_area->i_plugin_data
+                /* 1st entry point of the track (area)*/
+                        + p_input->stream.p_selected_area->i_part - 1;
+            if( i_entry + 1 < p_vcd->i_entries_nb &&
+                    p_vcd->i_sector >= p_vcd->p_entries[i_entry + 1] )
+            {
+                msg_Dbg( p_input, "new chapter" );
+                p_input->stream.p_selected_area->i_part ++;
+            }
+            vlc_mutex_unlock( &p_input->stream.stream_lock );
+        }
+
         i_read += VCD_DATA_SIZE;
     }
-    
+
     if ( i_len % VCD_DATA_SIZE ) /* this should not happen */
-    { 
-        if ( ioctl_ReadSector( VLC_OBJECT(p_input), p_vcd->vcddev,
-                               p_vcd->i_sector, p_last_sector ) < 0 )
+    {
+        if ( ioctl_ReadSectors( VLC_OBJECT(p_input), p_vcd->vcddev,
+             p_vcd->i_sector, p_last_sector, 1, VCD_TYPE ) < 0 )
         {
             msg_Err( p_input, "could not read sector %d", p_vcd->i_sector );
             return -1;
         }
-        
+
         p_input->p_vlc->pf_memcpy( p_buffer + i_blocks * VCD_DATA_SIZE,
                                    p_last_sector, i_len % VCD_DATA_SIZE );
         i_read += i_len % VCD_DATA_SIZE;
     }
-    
-    p_input->stream.p_selected_area->i_tell = 
-        (off_t)p_vcd->i_sector * (off_t)VCD_DATA_SIZE
-         - p_input->stream.p_selected_area->i_start;
 
     return i_read;
 }
 
-
 /*****************************************************************************
  * VCDSetProgram: Does nothing since a VCD is mono_program
  *****************************************************************************/
@@ -333,7 +369,6 @@ static int VCDSetProgram( input_thread_t * p_input,
     return 0;
 }
 
-
 /*****************************************************************************
  * VCDSetArea: initialize input data for title x, chapter y.
  * It should be called for each user navigation request.
@@ -341,6 +376,7 @@ static int VCDSetProgram( input_thread_t * p_input,
 static int VCDSetArea( input_thread_t * p_input, input_area_t * p_area )
 {
     thread_vcd_data_t *     p_vcd;
+    vlc_value_t val;
 
     p_vcd = (thread_vcd_data_t*)p_input->p_access_data;
 
@@ -349,6 +385,8 @@ static int VCDSetArea( input_thread_t * p_input, input_area_t * p_area )
 
     if( p_area != p_input->stream.p_selected_area )
     {
+        unsigned int i;
+
         /* Reset the Chapter position of the current title */
         p_input->stream.p_selected_area->i_part = 1;
         p_input->stream.p_selected_area->i_tell = 0;
@@ -360,29 +398,158 @@ static int VCDSetArea( input_thread_t * p_input, input_area_t * p_area )
         /* The first track is not a valid one  */
         p_vcd->i_track = p_area->i_id;
         p_vcd->i_sector = p_vcd->p_sectors[p_vcd->i_track];
+
+        /* Update the navigation variables without triggering a callback */
+        val.i_int = p_area->i_id;
+        var_Change( p_input, "title", VLC_VAR_SETVALUE, &val, NULL );
+        var_Change( p_input, "chapter", VLC_VAR_CLEARCHOICES, NULL, NULL );
+        for( i = 1; i <= p_area->i_part_nb; i++ )
+        {
+            val.i_int = i;
+            var_Change( p_input, "chapter", VLC_VAR_ADDCHOICE, &val, NULL );
+        }
     }
 
+    if( p_vcd->b_valid_ep )
+    {
+        int i_entry = p_area->i_plugin_data /* 1st entry point of
+                                               the track (area)*/
+                            + p_area->i_part - 1;
+        p_vcd->i_sector = p_vcd->p_entries[i_entry];
+    }
+    else
+        p_vcd->i_sector = p_vcd->p_sectors[p_vcd->i_track];
+
+    p_input->stream.p_selected_area->i_tell =
+        (off_t)p_vcd->i_sector * (off_t)VCD_DATA_SIZE
+         - p_input->stream.p_selected_area->i_start;
+
     /* warn interface that something has changed */
     p_input->stream.b_seekable = 1;
     p_input->stream.b_changed = 1;
 
+    /* Update the navigation variables without triggering a callback */
+    val.i_int = p_area->i_part;
+    var_Change( p_input, "chapter", VLC_VAR_SETVALUE, &val, NULL );
+
     return 0;
 }
 
-
 /****************************************************************************
  * VCDSeek
  ****************************************************************************/
 static void VCDSeek( input_thread_t * p_input, off_t i_off )
 {
-    thread_vcd_data_t *               p_vcd;
+    thread_vcd_data_t * p_vcd;
+    unsigned int i_index;
 
     p_vcd = (thread_vcd_data_t *) p_input->p_access_data;
 
     p_vcd->i_sector = p_vcd->p_sectors[p_vcd->i_track]
                        + i_off / (off_t)VCD_DATA_SIZE;
 
-    p_input->stream.p_selected_area->i_tell = 
+    vlc_mutex_lock( &p_input->stream.stream_lock );
+#define p_area p_input->stream.p_selected_area
+    /* Find chapter */
+    if( p_vcd->b_valid_ep )
+    {
+        for( i_index = 0 ; i_index < p_area->i_part_nb - 1 ; i_index ++ )
+        {
+            if( p_vcd->i_sector < p_vcd->p_entries[p_area->i_plugin_data
+                + i_index + 1] )
+            {
+                p_area->i_part = i_index;
+                break;
+            }
+        }
+    }
+#undef p_area
+
+    p_input->stream.p_selected_area->i_tell =
         (off_t)p_vcd->i_sector * (off_t)VCD_DATA_SIZE
          - p_input->stream.p_selected_area->i_start;
+    vlc_mutex_unlock( &p_input->stream.stream_lock );
+}
+
+/*****************************************************************************
+ * VCDEntryPoints: Reads the information about the entry points on the disc.
+ *****************************************************************************/
+static int VCDEntryPoints( input_thread_t * p_input )
+{
+    thread_vcd_data_t *               p_vcd;
+    byte_t *                          p_sector;
+    entries_sect_t                    entries;
+    uint16_t                          i_nb;
+    int                               i, i_entry_index = 0;
+    int                               i_previous_track = -1;
+
+    p_vcd = (thread_vcd_data_t *) p_input->p_access_data;
+
+    p_sector = malloc( VCD_DATA_SIZE * sizeof( byte_t ) );
+    if( p_sector == NULL )
+    {
+        msg_Err( p_input, "not enough memory for entry points treatment" );
+        return -1;
+    }
+
+    if( ioctl_ReadSectors( VLC_OBJECT(p_input), p_vcd->vcddev,
+        VCD_ENTRIES_SECTOR, p_sector, 1, VCD_TYPE ) < 0 )
+    {
+        msg_Err( p_input, "could not read entry points sector" );
+        free( p_sector );
+        return( -1 );
+    }
+
+    memcpy( &entries, p_sector, CD_SECTOR_SIZE );
+    free( p_sector );
+
+    if( (i_nb = U16_AT( &entries.i_entries_nb )) > 500 )
+    {
+        msg_Err( p_input, "invalid entry points number" );
+        return( -1 );
+    }
+
+    p_vcd->p_entries = malloc( sizeof( int ) * i_nb );
+    if( p_vcd->p_entries == NULL )
+    {
+        msg_Err( p_input, "not enough memory for entry points treatment" );
+        return -1;
+    }
+
+    if( strncmp( entries.psz_id, "ENTRYVCD", sizeof( entries.psz_id ) )
+     && strncmp( entries.psz_id, "ENTRYSVD", sizeof( entries.psz_id ) ))
+    {
+        msg_Err( p_input, "unrecognized entry points format" );
+        free( p_vcd->p_entries );
+        return -1;
+    }
+
+    p_vcd->i_entries_nb = 0;
+
+#define i_track BCD_TO_BIN(entries.entry[i].i_track)
+    for( i = 0 ; i < i_nb ; i++ )
+    {
+        if( i_track <= p_input->stream.i_area_nb )
+        {
+            p_vcd->p_entries[i_entry_index] =
+                (MSF_TO_LBA2( BCD_TO_BIN( entries.entry[i].msf.minute ),
+                              BCD_TO_BIN( entries.entry[i].msf.second ),
+                              BCD_TO_BIN( entries.entry[i].msf.frame  ) ));
+            p_input->stream.pp_areas[i_track-1]->i_part_nb ++;
+            /* if this entry belongs to a new track */
+            if( i_track != i_previous_track )
+            {
+                /* i_plugin_data is used to store the first entry of the area*/
+                p_input->stream.pp_areas[i_track-1]->i_plugin_data =
+                                                            i_entry_index;
+                i_previous_track = i_track;
+            }
+            i_entry_index ++;
+            p_vcd->i_entries_nb ++;
+        }
+        else
+            msg_Warn( p_input, "wrong track number found in entry points" );
+    }
+#undef i_track
+    return 0;
 }