]> git.sesse.net Git - vlc/blobdiff - plugins/dvdread/input_dvdread.c
* ./extras/MacOSX_dvdioctl: removed outdated files.
[vlc] / plugins / dvdread / input_dvdread.c
index 6391b0902e973f1db83ac73e440ea47a84e72bb9..883cdce64c5724fd887bd7b8651f2468e370edf0 100644 (file)
@@ -6,7 +6,7 @@
  * It depends on: libdvdread for ifo files and block reading.
  *****************************************************************************
  * Copyright (C) 2001 VideoLAN
- * $Id: input_dvdread.c,v 1.9 2001/12/29 15:40:53 massiot Exp $
+ * $Id: input_dvdread.c,v 1.34 2002/03/18 19:14:52 sam Exp $
  *
  * Author: Stéphane Borel <stef@via.ecp.fr>
  *
  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
  *****************************************************************************/
 
-#define MODULE_NAME dvdread
-#include "modules_inner.h"
-
 /*****************************************************************************
  * Preamble
  *****************************************************************************/
-#include "defs.h"
-
 #include <stdio.h>
 #include <stdlib.h>
 
+#include <videolan/vlc.h>
+
 #ifdef HAVE_UNISTD_H
 #   include <unistd.h>
 #endif
 
 #include <fcntl.h>
 #include <sys/types.h>
+#include <sys/stat.h>
 #include <string.h>
 #include <errno.h>
 #include <assert.h>
 #   include <sys/uio.h>                                      /* struct iovec */
 #endif
 
-
-#include "common.h"
-#include "intf_msg.h"
-#include "threads.h"
-#include "mtime.h"
-#include "iso_lang.h"
-#include "tests.h"
-
 #if defined( WIN32 )
 #   include "input_iovec.h"
 #endif
 
 #include "input_dvdread.h"
 
-#include "debug.h"
+#include "iso_lang.h"
 
-#include "modules.h"
-#include "modules_export.h"
+#include "debug.h"
 
 /* how many blocks DVDRead will read in each loop */
 #define DVD_BLOCK_READ_ONCE 64
-#define DVD_DATA_READ_ONCE  (4 * DVD_BLOCK_READ_ONCE)
 
 /*****************************************************************************
  * Local prototypes
  *****************************************************************************/
 /* called from outside */
-static int  DvdReadProbe    ( probedata_t *p_data );
-static void DvdReadInit     ( struct input_thread_s * );
+static int  DvdReadInit     ( struct input_thread_s * );
 static void DvdReadEnd      ( struct input_thread_s * );
-static void DvdReadOpen     ( struct input_thread_s * );
+static int  DvdReadDemux    ( struct input_thread_s * );
+static int  DvdReadRewind   ( struct input_thread_s * );
+
+static int  DvdReadOpen     ( struct input_thread_s * );
 static void DvdReadClose    ( struct input_thread_s * );
 static int  DvdReadSetArea  ( struct input_thread_s *, struct input_area_s * );
-static int  DvdReadRead     ( struct input_thread_s *, data_packet_t ** );
+static int  DvdReadSetProgram( struct input_thread_s *, pgrm_descriptor_t * );
+static int  DvdReadRead     ( struct input_thread_s *, byte_t *, size_t );
 static void DvdReadSeek     ( struct input_thread_s *, off_t );
-static int  DvdReadRewind   ( struct input_thread_s * );
 
 /* called only from here */
+static void DvdReadLauchDecoders( input_thread_t * p_input );
 static void DvdReadHandleDSI( thread_dvd_data_t * p_dvd, u8 * p_data );
 static void DvdReadFindCell ( thread_dvd_data_t * p_dvd );
 
-/*****************************************************************************
- * Declare a buffer manager
- *****************************************************************************/
-#define FLAGS           BUFFERS_UNIQUE_SIZE
-#define NB_LIFO         1
-DECLARE_BUFFERS_SHARED( FLAGS, NB_LIFO );
-DECLARE_BUFFERS_INIT( FLAGS, NB_LIFO );
-DECLARE_BUFFERS_END_SHARED( FLAGS, NB_LIFO );
-DECLARE_BUFFERS_NEWPACKET_SHARED( FLAGS, NB_LIFO );
-DECLARE_BUFFERS_DELETEPACKET_SHARED( FLAGS, NB_LIFO, 1000 );
-DECLARE_BUFFERS_NEWPES( FLAGS, NB_LIFO );
-DECLARE_BUFFERS_DELETEPES( FLAGS, NB_LIFO, 1000 );
-DECLARE_BUFFERS_TOIO( FLAGS, DVD_LB_SIZE );
-DECLARE_BUFFERS_SHAREBUFFER( FLAGS );
-
 /*****************************************************************************
  * Functions exported as capabilities. They are declared as static so that
  * we don't pollute the namespace too much.
  *****************************************************************************/
-void _M( input_getfunctions )( function_list_t * p_function_list )
+void _M( access_getfunctions )( function_list_t * p_function_list )
 {
-#define input p_function_list->functions.input
-    p_function_list->pf_probe = DvdReadProbe;
-    input.pf_init             = DvdReadInit;
-    input.pf_open             = DvdReadOpen;
-    input.pf_close            = DvdReadClose;
-    input.pf_end              = DvdReadEnd;
-    input.pf_init_bit_stream  = InitBitstream;
-    input.pf_read             = DvdReadRead;
-    input.pf_set_area         = DvdReadSetArea;
-    input.pf_demux            = input_DemuxPS;
-    input.pf_new_packet       = input_NewPacket;
-    input.pf_new_pes          = input_NewPES;
-    input.pf_delete_packet    = input_DeletePacket;
-    input.pf_delete_pes       = input_DeletePES;
-    input.pf_rewind           = DvdReadRewind;
-    input.pf_seek             = DvdReadSeek;
-#undef input
+#define access p_function_list->functions.access
+    access.pf_open             = DvdReadOpen;
+    access.pf_close            = DvdReadClose;
+    access.pf_read             = DvdReadRead;
+    access.pf_set_area         = DvdReadSetArea;
+    access.pf_set_program      = DvdReadSetProgram;
+    access.pf_seek             = DvdReadSeek;
+#undef access
+}
+
+void _M( demux_getfunctions )( function_list_t * p_function_list )
+{
+#define demux p_function_list->functions.demux
+    demux.pf_init             = DvdReadInit;
+    demux.pf_end              = DvdReadEnd;
+    demux.pf_demux            = DvdReadDemux;
+    demux.pf_rewind           = DvdReadRewind;
+#undef demux
 }
 
 /*
- * Data reading functions
+ * Data demux functions
  */
 
 /*****************************************************************************
- * DvdReadProbe: verifies that the stream is a PS stream
+ * DvdReadInit: initializes DVD structures
  *****************************************************************************/
-static int DvdReadProbe( probedata_t *p_data )
+static int DvdReadInit( input_thread_t * p_input )
 {
-    input_thread_t * p_input = (input_thread_t *)p_data;
+    if( p_input->stream.i_method != INPUT_METHOD_DVD )
+    {
+        return -1;
+    }
 
-    char * psz_name = p_input->p_source;
-    int i_score = 5;
+    vlc_mutex_lock( &p_input->stream.stream_lock );
+    
+    DvdReadLauchDecoders( p_input );
+    
+    vlc_mutex_unlock( &p_input->stream.stream_lock );
 
-    if( TestMethod( INPUT_METHOD_VAR, "dvdread" ) )
-    {
-        return( 999 );
+    return 0;
+}
+
+/*****************************************************************************
+ * DvdReadEnd: frees unused data
+ *****************************************************************************/
+static void DvdReadEnd( input_thread_t * p_input )
+{
+}
+
+/*****************************************************************************
+ * DvdReadDemux
+ *****************************************************************************/
+#define PEEK( SIZE )                                                        \
+    i_result = input_Peek( p_input, &p_peek, SIZE );                        \
+    if( i_result == -1 )                                                    \
+    {                                                                       \
+        return( -1 );                                                       \
+    }                                                                       \
+    else if( i_result < SIZE )                                              \
+    {                                                                       \
+        /* EOF */                                                           \
+        return( 0 );                                                        \
     }
 
-    if( ( strlen(psz_name) > 8 ) && !strncasecmp( psz_name, "dvdread:", 8 ) )
+static int DvdReadDemux( input_thread_t * p_input )
+{
+    int                 i;
+    byte_t *            p_peek;
+    data_packet_t *     p_data;
+    ssize_t             i_result;
+    int                 i_packet_size;
+
+
+    /* Read headers to compute payload length */
+    for( i = 0 ; i < DVD_BLOCK_READ_ONCE ; i++ )
     {
-        /* If the user specified "dvdread:" then he probably wants
-         * to use libdvdread */
-        i_score = 100;
-        psz_name += 4;
+
+        /* Read what we believe to be a packet header. */
+        PEEK( 4 );
+            
+        /* Default header */ 
+        if( U32_AT( p_peek ) != 0x1BA )
+        {
+            /* That's the case for all packets, except pack header. */
+            i_packet_size = U16_AT( p_peek + 4 );
+        }
+        else
+        {
+            /* MPEG-2 Pack header. */
+            i_packet_size = 8;
+        }
+
+        /* Fetch a packet of the appropriate size. */
+        i_result = input_SplitBuffer( p_input, &p_data, i_packet_size + 6 );
+        if( i_result <= 0 )
+        {
+            return( i_result );
+        }
+
+        /* In MPEG-2 pack headers we still have to read stuffing bytes. */
+        if( (p_data->p_demux_start[3] == 0xBA) && (i_packet_size == 8) )
+        {
+            size_t i_stuffing = (p_data->p_demux_start[13] & 0x7);
+            /* Force refill of the input buffer - though we don't care
+             * about p_peek. Please note that this is unoptimized. */
+            PEEK( i_stuffing );
+            p_input->p_current_data += i_stuffing;
+        }
+
+        input_DemuxPS( p_input, p_data );
+     
     }
 
-    return( i_score );
+    return i;
 }
 
 /*****************************************************************************
- * DvdReadInit: initializes DVD structures
+ * DVDRewind : reads a stream backward
  *****************************************************************************/
-static void DvdReadInit( input_thread_t * p_input )
+static int DvdReadRewind( input_thread_t * p_input )
 {
-    thread_dvd_data_t *  p_dvd;
-    input_area_t *       p_area;
-    int                  i_title;
-    int                  i_chapter;
-    int                  i;
+    return( -1 );
+}
+
+/*
+ * Data access functions
+ */
+
+/*****************************************************************************
+ * DvdReadOpen: open libdvdread
+ *****************************************************************************/
+static int DvdReadOpen( struct input_thread_s *p_input )
+{
+    char *                  psz_orig;
+    char *                  psz_parser;
+    char *                  psz_source;
+    char *                  psz_next;
+    struct stat             stat_info;
+    thread_dvd_data_t *     p_dvd;
+    dvd_reader_t *          p_dvdread;
+    input_area_t *          p_area;
+    int                     i_title = 1;
+    int                     i_chapter = 1;
+    int                     i_angle = 1;
+    int                     i;
+
+    psz_orig = psz_parser = psz_source = strdup( p_input->psz_name );
+    if( !psz_orig )
+    {
+        return( -1 );
+    }
+
+    while( *psz_parser && *psz_parser != '@' )
+    {
+        psz_parser++;
+    }
+
+    if( *psz_parser == '@' )
+    {
+        /* Found options */
+        *psz_parser = '\0';
+        ++psz_parser;
+
+        i_title = (int)strtol( psz_parser, &psz_next, 10 );
+        if( *psz_next )
+        {
+            psz_parser = psz_next + 1;
+            i_chapter = (int)strtol( psz_parser, &psz_next, 10 );
+            if( *psz_next )
+            {
+                i_angle = (int)strtol( psz_next + 1, NULL, 10 );
+            }
+        }
+
+        i_title = i_title ? i_title : 1;
+        i_chapter = i_chapter ? i_chapter : 1;
+        i_angle = i_angle ? i_angle : 1;
+    }
+
+    if( !*psz_source )
+    {
+        if( !p_input->psz_access )
+        {
+            free( psz_orig );
+            return -1;
+        }
+        psz_source = config_GetPszVariable( "dvd_device" );
+    }
+
+    if( stat( psz_source, &stat_info ) == -1 )
+    {
+        intf_ErrMsg( "input error: cannot stat() source `%s' (%s)",
+                     psz_source, strerror(errno));
+        return( -1 );
+    }
+    if( !S_ISBLK(stat_info.st_mode) &&
+        !S_ISCHR(stat_info.st_mode) &&
+        !S_ISDIR(stat_info.st_mode) )
+    {
+        intf_WarnMsg( 3, "input : DvdRead plugin discarded"
+                         " (not a valid source)" );
+        return -1;
+    }
+    
+    intf_WarnMsg( 2, "input: dvdroot=%s title=%d chapter=%d angle=%d",
+                  psz_source, i_title, i_chapter, i_angle );
+    
+
+    p_dvdread = DVDOpen( psz_source );
+
+    /* free allocated strings */
+    if( psz_source != psz_orig )
+        free( psz_source );
+    free( psz_orig );
+
+    if( ! p_dvdread )
+    {
+        intf_ErrMsg( "dvdread error: libdvdcss can't open source" );
+        return -1;
+    }
+
+    /* set up input  */
+    p_input->i_mtu = 0;
 
     p_dvd = malloc( sizeof(thread_dvd_data_t) );
     if( p_dvd == NULL )
     {
         intf_ErrMsg( "dvdread error: out of memory" );
-        p_input->b_error = 1;
-        return;
+        return -1;
     }
 
-    /* we take the pointer to dvd_reader_t back  */
-    p_dvd->p_dvdread = (dvd_reader_t *)p_input->p_plugin_data;
+    p_dvd->p_dvdread = p_dvdread;
     p_dvd->p_title = NULL;
     p_dvd->p_vts_file = NULL;
 
-    p_input->p_plugin_data = (void *)p_dvd;
-
-    if( (p_input->p_method_data = input_BuffersInit()) == NULL )
-    {
-        p_input->b_error = 1;
-        return;
-    }
 
-    /* We read DVD_BLOCK_READ_ONCE in each loop, so the input will receive
-     * DVD_DATA_READ_ONCE at most */
-    p_dvd->i_block_once = DVD_BLOCK_READ_ONCE;
+    p_input->p_access_data = (void *)p_dvd;
 
     /* Ifo allocation & initialisation */
     if( ! ( p_dvd->p_vmg_file = ifoOpen( p_dvd->p_dvdread, 0 ) ) )
     {
         intf_ErrMsg( "dvdread error: can't open VMG info" );
-        DVDClose( p_dvd->p_dvdread );
         free( p_dvd );
-        p_input->b_error = 1;
-        return;
+        return -1;
     }
     intf_WarnMsg( 2, "dvdread info: VMG opened" );
 
     /* Set stream and area data */
     vlc_mutex_lock( &p_input->stream.stream_lock );
 
+    p_input->stream.i_method = INPUT_METHOD_DVD;
+
+    /* If we are here we can control the pace... */
+    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;
+
     /* Initialize ES structures */
     input_InitStream( p_input, sizeof( stream_ps_data_t ) );
 
@@ -254,84 +391,33 @@ static void DvdReadInit( input_thread_t * p_input )
         area[i]->i_part_nb = tt_srpt->title[i-1].nr_of_ptts;
         area[i]->i_part = 1;
 
-        /* Number of angles */
-        area[i]->i_angle_nb = 0;
-        area[i]->i_angle = 1;
-
         area[i]->i_plugin_data = tt_srpt->title[i-1].title_set_nr;
     }
 #undef area
 
-    /* Get requested title - if none try the first title */
-    i_title = main_GetIntVariable( INPUT_TITLE_VAR, 1 );
-    if( i_title <= 0 || i_title > tt_srpt->nr_of_srpts )
-    {
-        i_title = 1;
-    }
-
+    p_dvd->i_title = i_title <= tt_srpt->nr_of_srpts ? i_title : 1;
 #undef tt_srpt
 
-    /* Get requested chapter - if none defaults to first one */
-    i_chapter = main_GetIntVariable( INPUT_CHAPTER_VAR, 1 );
-    if( i_chapter <= 0 )
-    {
-        i_chapter = 1;
-    }
-
-    p_input->stream.pp_areas[i_title]->i_part = i_chapter;
+    p_area = p_input->stream.pp_areas[p_dvd->i_title];
+    p_dvd->i_chapter = i_chapter;
 
-    p_area = p_input->stream.pp_areas[i_title];
+    p_dvd->i_chapter = i_chapter < p_area->i_part_nb ? i_chapter : 1;
+    p_area->i_part = p_dvd->i_chapter;
+    
+    p_dvd->i_angle = i_angle;
 
     /* set title, chapter, audio and subpic */
-    DvdReadSetArea( p_input, p_area );
-
-    vlc_mutex_unlock( &p_input->stream.stream_lock );
-
-    return;
-}
-
-/*****************************************************************************
- * DvdReadOpen: open libdvdread
- *****************************************************************************/
-static void DvdReadOpen( struct input_thread_s *p_input )
-{
-    dvd_reader_t    *p_dvdread;
-
-    vlc_mutex_lock( &p_input->stream.stream_lock );
-
-    /* If we are here we can control the pace... */
-    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 );
-
-    /* XXX: put this shit in an access plugin */
-    if( strlen( p_input->p_source ) > 8
-         && !strncasecmp( p_input->p_source, "dvdread:", 8 ) )
+    if( DvdReadSetArea( p_input, p_area ) )
     {
-        p_dvdread = DVDOpen( p_input->p_source + 8 );
-    }
-    else
-    {
-        p_dvdread = DVDOpen( p_input->p_source );
+        vlc_mutex_unlock( &p_input->stream.stream_lock );
+        return -1;
     }
 
-    if( ! p_dvdread )
-    {
-        p_input->b_error = 1;
-        return;
-    }
+    vlc_mutex_unlock( &p_input->stream.stream_lock );
 
-    /* the vlc input doesn't handle the file descriptor with libdvdread */
-    p_input->i_handle = -1;
+    p_input->psz_demux = "dvdread";
 
-    /* p_dvdread is stored in p_plugin_data until we have
-     * malloc'ed a dvd_thread_data_t */
-    p_input->p_plugin_data = (void *) p_dvdread;
+    return 0;
 }
 
 /*****************************************************************************
@@ -339,27 +425,43 @@ static void DvdReadOpen( struct input_thread_s *p_input )
  *****************************************************************************/
 static void DvdReadClose( struct input_thread_s *p_input )
 {
-    DVDClose( ((thread_dvd_data_t*)p_input->p_plugin_data)->p_dvdread );
-    free( p_input->p_plugin_data );
-    p_input->p_plugin_data = NULL;
+    thread_dvd_data_t *     p_dvd;
+
+    p_dvd = (thread_dvd_data_t *)p_input->p_access_data;
+
+    /* close libdvdread */
+    DVDCloseFile( p_dvd->p_title );
+    ifoClose( p_dvd->p_vts_file );
+    ifoClose( p_dvd->p_vmg_file );
+
+    DVDClose( p_dvd->p_dvdread );
+    free( p_dvd );
+    p_input->p_access_data = NULL;
 
 }
 
 /*****************************************************************************
- * DvdReadEnd: frees unused data
+ * DvdReadSetProgram: Does nothing, a DVD is mono-program
  *****************************************************************************/
-static void DvdReadEnd( input_thread_t * p_input )
+static int DvdReadSetProgram( input_thread_t * p_input,
+                              pgrm_descriptor_t * p_program )
 {
-    thread_dvd_data_t *     p_dvd;
+    if( p_input->stream.p_selected_program != p_program )
+    {
+        thread_dvd_data_t *  p_dvd;
+    
+        p_dvd = (thread_dvd_data_t*)(p_input->p_access_data);
+        p_dvd->i_angle = p_program->i_number;
 
-    p_dvd = (thread_dvd_data_t *)p_input->p_plugin_data;
+        memcpy( p_program, p_input->stream.p_selected_program,
+                sizeof(pgrm_descriptor_t) );
+        p_program->i_number = p_dvd->i_angle;
+        p_input->stream.p_selected_program = p_program;
 
-    /* close libdvdread */
-    DVDCloseFile( p_dvd->p_title );
-    ifoClose( p_dvd->p_vts_file );
-    ifoClose( p_dvd->p_vmg_file );
+        intf_WarnMsg( 3, "dvd info: angle %d selected", p_dvd->i_angle );
+    }
 
-    input_BuffersEnd( p_input->p_method_data );
+    return 0;
 }
 
 #define p_pgc         p_dvd->p_cur_pgc
@@ -377,7 +479,7 @@ static int DvdReadSetArea( input_thread_t * p_input, input_area_t * p_area )
     int                  pgc_id = 0;
     int                  pgn = 0;
 
-    p_dvd = (thread_dvd_data_t*)p_input->p_plugin_data;
+    p_dvd = (thread_dvd_data_t*)p_input->p_access_data;
 
     /* we can't use the interface slider until initilization is complete */
     p_input->stream.b_seekable = 0;
@@ -422,7 +524,6 @@ static int DvdReadSetArea( input_thread_t * p_input, input_area_t * p_area )
             intf_ErrMsg( "dvdread error: fatal error in vts ifo" );
             ifoClose( p_vmg );
             DVDClose( p_dvd->p_dvdread );
-            p_input->b_error = 1;
             return -1;
         }
 
@@ -477,15 +578,12 @@ static int DvdReadSetArea( input_thread_t * p_input, input_area_t * p_area )
         /*
          * Angle management
          */
-        p_area->i_angle_nb = p_vmg->tt_srpt->title[p_area->i_id-1].nr_of_angles;
-        p_area->i_angle = main_GetIntVariable( INPUT_ANGLE_VAR, 1 );
+        p_dvd->i_angle_nb = p_vmg->tt_srpt->title[p_area->i_id-1].nr_of_angles;
 
-        if( ( p_area->i_angle <= 0 ) || p_area->i_angle > p_area->i_angle_nb )
+        if( p_dvd->i_angle > p_dvd->i_angle_nb )
         {
-            p_area->i_angle = 1;
+            p_dvd->i_angle = 1;
         }
-        p_dvd->i_angle = p_area->i_angle;
-        p_dvd->i_angle_nb = p_area->i_angle_nb;
 
         /*
          * We've got enough info, time to open the title set data.
@@ -513,36 +611,47 @@ static int DvdReadSetArea( input_thread_t * p_input, input_area_t * p_area )
             /* We don't use input_EndStream here since
              * we keep area structures */
 
-            for( i = 0 ; i < p_input->stream.i_selected_es_number ; i++ )
+            while( p_input->stream.i_es_number )
             {
-                input_UnselectES( p_input, p_input->stream.pp_selected_es[i] );
+                input_DelES( p_input, p_input->stream.pp_es[0] );
             }
 
-            free( p_input->stream.pp_selected_es );
-            input_DelProgram( p_input, p_input->stream.pp_programs[0] );
+            while( p_input->stream.i_pgrm_number )
+            {
+                input_DelProgram( p_input, p_input->stream.pp_programs[0] );
+            }
 
-            p_input->stream.pp_selected_es = NULL;
+            if( p_input->stream.pp_selected_es )
+            {
+                free( p_input->stream.pp_selected_es );
+                p_input->stream.pp_selected_es = NULL;
+            }
             p_input->stream.i_selected_es_number = 0;
         }
 
-        input_AddProgram( p_input, 0, sizeof( stream_ps_data_t ) );
+        input_AddProgram( p_input, 1, sizeof( stream_ps_data_t ) );
+        p_input->stream.p_selected_program = p_input->stream.pp_programs[0];
+
+        for( i = 1 ; i < p_dvd->i_angle_nb ; i++ )
+        {
+            input_AddProgram( p_input, i+1, 0 );
+        }
+        
+        DvdReadSetProgram( p_input,
+                           p_input->stream.pp_programs[p_dvd->i_angle-1] ); 
 
         /* No PSM to read in DVD mode, we already have all information */
-        p_input->stream.pp_programs[0]->b_is_ok = 1;
+        p_input->stream.p_selected_program->b_is_ok = 1;
 
         p_es = NULL;
 
         /* ES 0 -> video MPEG2 */
 //        IfoPrintVideo( p_dvd );
 
-        p_es = input_AddES( p_input, p_input->stream.pp_programs[0], 0xe0, 0 );
+        p_es = input_AddES( p_input, NULL, 0xe0, 0 );
         p_es->i_stream_id = 0xe0;
         p_es->i_type = MPEG2_VIDEO_ES;
         p_es->i_cat = VIDEO_ES;
-        if( p_main->b_video )
-        {
-            input_SelectES( p_input, p_es );
-        }
 
 #define audio_control \
     p_dvd->p_vts_file->vts_pgcit->pgci_srp[pgc_id-1].pgc->audio_control[i-1]
@@ -565,8 +674,7 @@ static int DvdReadSetArea( input_thread_t * p_input, input_area_t * p_area )
                 {
                 case 0x00:              /* AC3 */
                     i_id = ( ( 0x80 + i_position ) << 8 ) | 0xbd;
-                    p_es = input_AddES( p_input,
-                               p_input->stream.pp_programs[0], i_id, 0 );
+                    p_es = input_AddES( p_input, NULL, i_id, 0 );
                     p_es->i_stream_id = 0xbd;
                     p_es->i_type = AC3_AUDIO_ES;
                     p_es->b_audio = 1;
@@ -579,8 +687,7 @@ static int DvdReadSetArea( input_thread_t * p_input, input_area_t * p_area )
                 case 0x02:
                 case 0x03:              /* MPEG audio */
                     i_id = 0xc0 + i_position;
-                    p_es = input_AddES( p_input,
-                                    p_input->stream.pp_programs[0], i_id, 0 );
+                    p_es = input_AddES( p_input, NULL, i_id, 0 );
                     p_es->i_stream_id = i_id;
                     p_es->i_type = MPEG2_AUDIO_ES;
                     p_es->b_audio = 1;
@@ -593,8 +700,7 @@ static int DvdReadSetArea( input_thread_t * p_input, input_area_t * p_area )
                 case 0x04:              /* LPCM */
 
                     i_id = ( ( 0xa0 + i_position ) << 8 ) | 0xbd;
-                    p_es = input_AddES( p_input,
-                                    p_input->stream.pp_programs[0], i_id, 0 );
+                    p_es = input_AddES( p_input, NULL, i_id, 0 );
                     p_es->i_stream_id = i_id;
                     p_es->i_type = LPCM_AUDIO_ES;
                     p_es->b_audio = 1;
@@ -658,8 +764,7 @@ static int DvdReadSetArea( input_thread_t * p_input, input_area_t * p_area )
                 }
 
                 i_id = ( ( 0x20 + i_position ) << 8 ) | 0xbd;
-                p_es = input_AddES( p_input,
-                                    p_input->stream.pp_programs[0], i_id, 0 );
+                p_es = input_AddES( p_input, NULL, i_id, 0 );
                 p_es->i_stream_id = 0xbd;
                 p_es->i_type = DVD_SPU_ES;
                 p_es->i_cat = SPU_ES;
@@ -668,57 +773,14 @@ static int DvdReadSetArea( input_thread_t * p_input, input_area_t * p_area )
             }
         }
 #undef spu_control
-        if( p_main->b_audio )
-        {
-            /* For audio: first one if none or a not existing one specified */
-            int i_audio = main_GetIntVariable( INPUT_CHANNEL_VAR, 1 );
-            if( i_audio < 0 || i_audio > i_audio_nb )
-            {
-                main_PutIntVariable( INPUT_CHANNEL_VAR, 1 );
-                i_audio = 1;
-            }
-            if( i_audio > 0 && i_audio_nb > 0 )
-            {
-                if( main_GetIntVariable( AOUT_SPDIF_VAR, 0 ) ||
-                    ( main_GetIntVariable( INPUT_AUDIO_VAR, 0 ) ==
-                      REQUESTED_AC3 ) )
-                {
-                    int     i_ac3 = i_audio;
-                    while( ( p_input->stream.pp_es[i_ac3]->i_type !=
-                             AC3_AUDIO_ES ) && ( i_ac3 <=
-                             p_vts->vtsi_mat->nr_of_vts_audio_streams ) )
-                    {
-                        i_ac3++;
-                    }
-                    if( p_input->stream.pp_es[i_ac3]->i_type == AC3_AUDIO_ES )
-                    {
-                        input_SelectES( p_input,
-                                        p_input->stream.pp_es[i_ac3] );
-                    }
-                }
-                else
-                {
-                    input_SelectES( p_input,
-                                    p_input->stream.pp_es[i_audio] );
-                }
-            }
-        }
 
-        if( p_main->b_video )
+        /* FIXME: hack to check that the demuxer is ready, and set
+         * the decoders */
+        if( p_input->p_demux_module )
         {
-            /* for spu, default is none */
-            int i_spu = main_GetIntVariable( INPUT_SUBTITLE_VAR, 0 );
-            if( i_spu < 0 || i_spu > i_spu_nb )
-            {
-                main_PutIntVariable( INPUT_SUBTITLE_VAR, 0 );
-                i_spu = 0;
-            }
-            if( i_spu > 0 && i_spu_nb > 0 )
-            {
-                i_spu += p_vts->vtsi_mat->nr_of_vts_audio_streams;
-                input_SelectES( p_input, p_input->stream.pp_es[i_spu] );
-            }
+            DvdReadLauchDecoders( p_input );
         }
+            
     } /* i_title >= 0 */
     else
     {
@@ -758,12 +820,6 @@ static int DvdReadSetArea( input_thread_t * p_input, input_area_t * p_area )
 #undef p_vts
 #undef p_vmg
 
-    if( p_area->i_angle != p_dvd->i_angle )
-    {
-        p_dvd->i_angle = p_area->i_angle;
-
-        intf_WarnMsg( 3, "dvd info: angle %d selected", p_area->i_angle );
-    }
     /* warn interface that something has changed */
     p_area->i_tell = LB2OFF( p_dvd->i_next_vobu ) - p_area->i_start;
     p_input->stream.b_seekable = 1;
@@ -780,145 +836,97 @@ static int DvdReadSetArea( input_thread_t * p_input, input_area_t * p_area )
  * EOF.
  *****************************************************************************/
 static int DvdReadRead( input_thread_t * p_input,
-                        data_packet_t ** pp_data )
+                        byte_t * p_buffer, size_t i_count )
 {
     thread_dvd_data_t *     p_dvd;
-    u8                      p_data[DVD_VIDEO_LB_LEN];
-    struct iovec            p_vec[DVD_DATA_READ_ONCE];
-    u8 *                    pi_cur;
+    byte_t *                p_buf;
+    int                     i_blocks_once;
     int                     i_blocks;
     int                     i_read;
-    int                     i_iovec;
-    int                     i_packet_size;
-    int                     i_packet;
-    int                     i_pos;
-    data_packet_t *         p_data_p;
-
-    p_dvd = (thread_dvd_data_t *)p_input->p_plugin_data;
+    int                     i_read_total;
+    boolean_t               b_eot = 0;
 
-    *pp_data = NULL;
+    p_dvd = (thread_dvd_data_t *)p_input->p_access_data;
+    p_buf = p_buffer;
 
     /*
      * Playback by cell in this pgc, starting at the cell for our chapter.
      */
+    i_blocks = OFF2LB( i_count );
+    i_read_total = 0;
+    i_read = 0;
 
-    /* 
-     * End of pack, we select the following one
-     */
-    if( ! p_dvd->i_pack_len )
+    while( i_blocks )
     {
-        /*
-         * Read NAV packet.
+        /* 
+         * End of pack, we select the following one
          */
-        if( ( i_read = DVDReadBlocks( p_dvd->p_title, p_dvd->i_next_vobu,
-                       1, p_data ) ) != 1 )
+        if( ! p_dvd->i_pack_len )
         {
-            intf_ErrMsg( "dvdread error: read failed for block %d",
-                         p_dvd->i_next_vobu );
-            return -1;
-        }
-
-        assert( p_data[41] == 0xbf && p_data[1027] == 0xbf );
-
-        /*
-         * Parse the contained dsi packet.
-         */
+            /*
+             * Read NAV packet.
+             */
+            if( ( i_read = DVDReadBlocks( p_dvd->p_title, p_dvd->i_next_vobu,
+                           1, p_buf ) ) != 1 )
+            {
+                intf_ErrMsg( "dvdread error: read failed for block %d",
+                             p_dvd->i_next_vobu );
+                return -1;
+            }
 
-        DvdReadHandleDSI( p_dvd, p_data );
+            /* basic check to be sure we don't have a empty title
+             * go to next title if so */
+            //assert( p_buffer[41] == 0xbf && p_buffer[1027] == 0xbf );
+            
+            /*
+             * Parse the contained dsi packet.
+             */
 
-        /* End of File */
-        if( p_dvd->i_next_vobu >= p_dvd->i_end_block + 1 )
-        {
-            return 1;
-        }
+            DvdReadHandleDSI( p_dvd, p_buf );
 
-        assert( p_dvd->i_pack_len < 1024 );
-        /* Ugly kludge: we send the pack block to the input for it
-         * sometimes has a zero scr and restart the sync */
-//        p_dvd->i_cur_block ++;
-        p_dvd->i_pack_len++;
+            /* End of File */
+            if( p_dvd->i_next_vobu >= p_dvd->i_end_block + 1 )
+            {
+                return 1;
+            }
 
-    }
+            assert( p_dvd->i_pack_len < 1024 );
+            /* FIXME: Ugly kludge: we send the pack block to the input for it
+             * sometimes has a zero scr and restart the sync */
+            p_dvd->i_cur_block ++;
+            //p_dvd->i_pack_len++;
 
-    /*
-     * Compute the number of blocks to read
-     */
-    i_blocks = p_dvd->i_pack_len >= DVD_BLOCK_READ_ONCE
-             ? DVD_BLOCK_READ_ONCE : p_dvd->i_pack_len;
-    p_dvd->i_pack_len -= i_blocks;
+            i_read_total++;
+            p_buf += DVD_VIDEO_LB_LEN;
+            i_blocks--;
+        }
 
-    /* Get iovecs */
-    *pp_data = p_data_p = input_BuffersToIO( p_input->p_method_data, p_vec,
-                                             DVD_DATA_READ_ONCE );
+        /*
+         * Compute the number of blocks to read
+         */
+        i_blocks_once = p_dvd->i_pack_len >= i_blocks
+                 ? i_blocks : p_dvd->i_pack_len;
+        p_dvd->i_pack_len -= i_blocks_once;
+
+        /* Reads from DVD */
+        i_read = DVDReadBlocks( p_dvd->p_title, p_dvd->i_cur_block,
+                                i_blocks_once, p_buf );
+        if( i_read != i_blocks_once )
+        {
+            intf_ErrMsg( "dvdread error: read failed for %d/%d blocks at 0x%02x",
+                         i_read, i_blocks_once, p_dvd->i_cur_block );
+            return -1;
+        }
 
-    if ( p_data_p == NULL )
-    {
-        return( -1 );
-    }
+        i_blocks -= i_read;
+        i_read_total += i_read;
+        p_dvd->i_cur_block += i_read;
+        p_buf += LB2OFF( i_read );
 
-    /* Reads from DVD */
-    i_read = DVDReadVBlocks( p_dvd->p_title, p_dvd->i_cur_block,
-                             i_blocks, p_vec );
-    if( i_read != i_blocks )
-    {
-        intf_ErrMsg( "dvdread error: read failed for %d/%d blocks at 0x%02x",
-                     i_read, i_blocks, p_dvd->i_cur_block );
-        return -1;
     }
-
-    p_dvd->i_cur_block += i_read;
 /*
     intf_WarnMsg( 12, "dvdread i_blocks: %d len: %d current: 0x%02x", i_read, p_dvd->i_pack_len, p_dvd->i_cur_block );
 */
-    i_packet = 0;
-
-    /* Read headers to compute payload length */
-    for( i_iovec = 0 ; i_iovec < i_read ; i_iovec++ )
-    {
-        data_packet_t * p_current = p_data_p;
-        i_pos = 0;
-
-        while( i_pos < DVD_LB_SIZE )
-        {
-            pi_cur = (u8*)p_vec[i_iovec].iov_base + i_pos;
-
-            /*default header */
-            if( U32_AT( pi_cur ) != 0x1BA )
-            {
-                /* That's the case for all packets, except pack header. */
-                i_packet_size = U16_AT( pi_cur + 4 );
-            }
-            else
-            {
-                /* MPEG-2 Pack header. */
-                i_packet_size = 8;
-            }
-            if( i_pos != 0 )
-            {
-                *pp_data = input_ShareBuffer( p_input->p_method_data,
-                                              p_current );
-            }
-            else
-            {
-                *pp_data = p_data_p;
-                p_data_p = p_data_p->p_next;
-            }
-
-            (*pp_data)->p_payload_start = (*pp_data)->p_demux_start =
-                    (*pp_data)->p_demux_start + i_pos;
-            
-
-            (*pp_data)->p_payload_end =
-                    (*pp_data)->p_payload_start + i_packet_size + 6;
-
-            i_packet++;
-            i_pos += i_packet_size + 6;
-            pp_data = &(*pp_data)->p_next;
-        }
-    }
-
-    p_input->pf_delete_packet( p_input->p_method_data, p_data_p );
-    *pp_data = NULL;
 
     vlc_mutex_lock( &p_input->stream.stream_lock );
 
@@ -933,22 +941,32 @@ static int DvdReadRead( input_thread_t * p_input,
         p_input->stream.p_selected_area->i_part = p_dvd->i_chapter;
         p_dvd->b_eoc = 0;
     }
+    
+    if( p_input->stream.p_selected_area->i_tell
+            >= p_input->stream.p_selected_area->i_size || b_eot )
+    {
+        if( ( p_input->stream.p_selected_area->i_id + 1 ) >= 
+                        p_input->stream.i_area_nb )
+        {
+            /* EOF */
+            vlc_mutex_unlock( &p_input->stream.stream_lock );
+            return 1;
+        }
 
+        /* EOT */
+        intf_WarnMsg( 4, "dvd info: new title" );
+        DvdReadSetArea( p_input, p_input->stream.pp_areas[
+                        p_input->stream.p_selected_area->i_id+1] );
+        vlc_mutex_unlock( &p_input->stream.stream_lock );
+        return 0;
+    }
 
     vlc_mutex_unlock( &p_input->stream.stream_lock );
 
-    return 0;
+    return LB2OFF( i_read_total );
 }
 #undef p_pgc
 
-/*****************************************************************************
- * DVDRewind : reads a stream backward
- *****************************************************************************/
-static int DvdReadRewind( input_thread_t * p_input )
-{
-    return( -1 );
-}
-
 /*****************************************************************************
  * DvdReadSeek : Goes to a given position on the stream.
  *****************************************************************************
@@ -966,9 +984,12 @@ static void DvdReadSeek( input_thread_t * p_input, off_t i_off )
     int                     i_vobu = 0;
     int                     i_sub_cell = 0;
 
+    vlc_mutex_lock( &p_input->stream.stream_lock );
     i_off += p_input->stream.p_selected_area->i_start;
+    vlc_mutex_unlock( &p_input->stream.stream_lock );
+    
     i_lb = OFF2LB( i_off );
-    p_dvd = ( thread_dvd_data_t * )p_input->p_plugin_data;
+    p_dvd = ( thread_dvd_data_t * )p_input->p_access_data;
 
     /* find cell */
     while( p_dvd->p_cur_pgc->cell_playback[i_cell].last_sector < i_lb )
@@ -984,9 +1005,9 @@ static void DvdReadSeek( input_thread_t * p_input, off_t i_off )
 
         i_chapter++;
         pgc_id = p_dvd->p_vts_file->vts_ptt_srpt->title[
-                    p_dvd->i_ttn-1].ptt[i_chapter].pgcn;
+                    p_dvd->i_ttn-1].ptt[i_chapter-1].pgcn;
         pgn = p_dvd->p_vts_file->vts_ptt_srpt->title[
-                    p_dvd->i_ttn-1].ptt[i_chapter].pgn;
+                    p_dvd->i_ttn-1].ptt[i_chapter-1].pgn;
 
         p_pgc = p_dvd->p_vts_file->vts_pgcit->pgci_srp[pgc_id-1].pgc;
         i_tmp = p_pgc->program_map[pgn-1];
@@ -1022,10 +1043,12 @@ static void DvdReadSeek( input_thread_t * p_input, off_t i_off )
     p_dvd->i_chapter = i_chapter;
     DvdReadFindCell( p_dvd );
 
+    vlc_mutex_lock( &p_input->stream.stream_lock );
     p_input->stream.p_selected_area->i_tell =
         LB2OFF ( p_dvd->i_cur_block )
          - p_input->stream.p_selected_area->i_start;
     p_input->stream.p_selected_area->i_part = p_dvd->i_chapter;
+    vlc_mutex_unlock( &p_input->stream.stream_lock );
 
     return;
 }
@@ -1161,14 +1184,79 @@ static void DvdReadFindCell( thread_dvd_data_t * p_dvd )
     }
 #undef cell
     pgc_id = p_dvd->p_vts_file->vts_ptt_srpt->title[
-                p_dvd->i_ttn-1].ptt[p_dvd->i_chapter].pgcn;
+                p_dvd->i_ttn-1].ptt[p_dvd->i_chapter-1].pgcn;
     pgn = p_dvd->p_vts_file->vts_ptt_srpt->title[
-                p_dvd->i_ttn-1].ptt[p_dvd->i_chapter].pgn;
+                p_dvd->i_ttn-1].ptt[p_dvd->i_chapter-1].pgn;
     p_pgc = p_dvd->p_vts_file->vts_pgcit->pgci_srp[pgc_id-1].pgc;
 
     if( p_pgc->program_map[pgn-1] <= p_dvd->i_cur_cell )
     {
         p_dvd->i_chapter++;
-       p_dvd->b_eoc = 1;
+        p_dvd->b_eoc = 1;
+    }
+}
+
+/*****************************************************************************
+ * DvdReadLaunchDecoders
+ *****************************************************************************/
+static void DvdReadLauchDecoders( input_thread_t * p_input )
+{
+    thread_dvd_data_t *  p_dvd;
+    
+    p_dvd = (thread_dvd_data_t*)(p_input->p_access_data);            
+            
+    if( p_main->b_video )
+    {
+        input_SelectES( p_input, p_input->stream.pp_es[0] );
+    }
+
+    if( p_main->b_audio )
+    {
+        /* For audio: first one if none or a not existing one specified */
+        int i_audio = config_GetIntVariable( "input_channel" );
+        if( i_audio < 0 /*|| i_audio > i_audio_nb*/ )
+        {
+            config_PutIntVariable( "input_channel", 1 );
+            i_audio = 1;
+        }
+        if( i_audio > 0/* && i_audio_nb > 0*/ )
+        {
+            if( config_GetIntVariable( "input_audio" ) == REQUESTED_AC3 )
+            {
+                int     i_ac3 = i_audio;
+                while( ( p_input->stream.pp_es[i_ac3]->i_type !=
+                       AC3_AUDIO_ES ) && ( i_ac3 <=
+                       p_dvd->p_vts_file->vtsi_mat->nr_of_vts_audio_streams ) )
+                {
+                    i_ac3++;
+                }
+                if( p_input->stream.pp_es[i_ac3]->i_type == AC3_AUDIO_ES )
+                {
+                    input_SelectES( p_input,
+                                    p_input->stream.pp_es[i_ac3] );
+                }
+            }
+            else
+            {
+                input_SelectES( p_input,
+                                p_input->stream.pp_es[i_audio] );
+            }
+        }
+    }
+
+    if( p_main->b_video )
+    {
+        /* for spu, default is none */
+        int i_spu = config_GetIntVariable( "input_subtitle" );
+        if( i_spu < 0 /*|| i_spu > i_spu_nb*/ )
+        {
+            config_PutIntVariable( "input_subtitle", 0 );
+            i_spu = 0;
+        }
+        if( i_spu > 0 /*&& i_spu_nb > 0*/ )
+        {
+            i_spu += p_dvd->p_vts_file->vtsi_mat->nr_of_vts_audio_streams;
+            input_SelectES( p_input, p_input->stream.pp_es[i_spu] );
+        }
     }
 }