]> git.sesse.net Git - vlc/blobdiff - plugins/dvd/input_dvd.c
* Fixed DVD input under Win32.
[vlc] / plugins / dvd / input_dvd.c
index f771789467b26d1bfe334da61025728eeb5853bb..9185331a6e3ca93f824727835bf62496d2cf350e 100644 (file)
@@ -1,5 +1,4 @@
-/*****************************************************************************
- * input_dvd.c: DVD raw reading plugin.
+/* input_dvd.c: DVD raw reading plugin.
  *****************************************************************************
  * This plugins should handle all the known specificities of the DVD format,
  * especially the 2048 bytes logical block size.
@@ -9,7 +8,7 @@
  *  -dvd_udf to find files
  *****************************************************************************
  * Copyright (C) 1998-2001 VideoLAN
- * $Id: input_dvd.c,v 1.107 2001/12/19 10:00:00 massiot Exp $
+ * $Id: input_dvd.c,v 1.126 2002/03/03 17:34:27 xav Exp $
  *
  * Author: Stéphane Borel <stef@via.ecp.fr>
  *
  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
  *****************************************************************************/
 
-#define MODULE_NAME dvd
-#include "modules_inner.h"
-
 /*****************************************************************************
  * Preamble
  *****************************************************************************/
-#include "defs.h"
-
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.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 <videolan/dvdcss.h>
 #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_dvd.h"
 #include "dvd_ifo.h"
 #include "dvd_summary.h"
+#include "iso_lang.h"
 
 #include "debug.h"
 
-#include "modules.h"
-#include "modules_export.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)
+/* how many packets DVDDemux will read in each loop */
+#define DVD_READ_ONCE 64
 
 /*****************************************************************************
  * Local prototypes
  *****************************************************************************/
+
 /* called from outside */
-static int  DVDProbe        ( probedata_t *p_data );
-static void DVDInit         ( struct input_thread_s * );
-static void DVDEnd          ( struct input_thread_s * );
-static void DVDOpen         ( struct input_thread_s * );
+static int  DVDOpen         ( struct input_thread_s * );
 static void DVDClose        ( struct input_thread_s * );
 static int  DVDSetArea      ( struct input_thread_s *, struct input_area_s * );
 static int  DVDSetProgram   ( struct input_thread_s *, pgrm_descriptor_t * );
-static int  DVDRead         ( struct input_thread_s *, data_packet_t ** );
+static int  DVDRead         ( struct input_thread_s *, byte_t *, size_t );
 static void DVDSeek         ( struct input_thread_s *, off_t );
+
 static int  DVDRewind       ( struct input_thread_s * );
+static int  DVDDemux        ( struct input_thread_s * );
+static int  DVDInit         ( struct input_thread_s * );
+static void DVDEnd          ( struct input_thread_s * );
 
 /* called only inside */
+static void DVDLaunchDecoders( input_thread_t * p_input );
 static int  DVDChooseAngle( thread_dvd_data_t * );
 static int  DVDFindCell( thread_dvd_data_t * );
 static int  DVDFindSector( thread_dvd_data_t * );
 static int  DVDChapterSelect( thread_dvd_data_t *, int );
 
-/*****************************************************************************
- * 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, 150 );
-DECLARE_BUFFERS_NEWPES( FLAGS, NB_LIFO );
-DECLARE_BUFFERS_DELETEPES( FLAGS, NB_LIFO, 150 );
-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 = DVDProbe;
-    input.pf_init             = DVDInit;
+#define input p_function_list->functions.access
     input.pf_open             = DVDOpen;
     input.pf_close            = DVDClose;
-    input.pf_end              = DVDEnd;
-    input.pf_init_bit_stream  = InitBitstream;
     input.pf_read             = DVDRead;
     input.pf_set_area         = DVDSetArea;
     input.pf_set_program      = DVDSetProgram;
-    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           = DVDRewind;
     input.pf_seek             = DVDSeek;
 #undef input
 }
 
+void _M( demux_getfunctions)( function_list_t * p_function_list )
+{
+#define demux p_function_list->functions.demux
+    demux.pf_init             = DVDInit;
+    demux.pf_end              = DVDEnd;
+    demux.pf_demux            = DVDDemux;
+    demux.pf_rewind           = DVDRewind;
+#undef demux
+}
+
 /*
- * Data reading functions
+ * Data demux functions
  */
 
 /*****************************************************************************
- * DVDProbe: verifies that the stream is a PS stream
+ * DVDInit: initializes DVD structures
  *****************************************************************************/
-static int DVDProbe( probedata_t *p_data )
+static int DVDInit( input_thread_t * p_input )
 {
-    input_thread_t * p_input = (input_thread_t *)p_data;
-
-    char * psz_name = p_input->p_source;
-    int i_score = 5;
 
-    if( TestMethod( INPUT_METHOD_VAR, "dvd" ) )
+    if( strncmp( p_input->p_access_module->psz_name, "dvd", 3 ) )
     {
-        return( 999 );
+        return -1;
     }
 
-    if( ( strlen(psz_name) > 4 ) && !strncasecmp( psz_name, "dvd:", 4 ) )
+    vlc_mutex_lock( &p_input->stream.stream_lock );
+    
+    DVDLaunchDecoders( p_input );
+    
+    vlc_mutex_unlock( &p_input->stream.stream_lock );
+
+    return 0;
+}
+
+/*****************************************************************************
+ * DVDEnd: frees unused data
+ *****************************************************************************/
+static void DVDEnd( input_thread_t * p_input )
+{
+}
+
+/*****************************************************************************
+ * DVDDemux
+ *****************************************************************************/
+#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 );                                                        \
+    }
+
+static int DVDDemux( 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_READ_ONCE ; i++ )
     {
-        /* If the user specified "dvd:" then it's probably a DVD */
-        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;
+}
 
-    return( i_score );
+/*****************************************************************************
+ * DVDRewind : reads a stream backward
+ *****************************************************************************/
+static int DVDRewind( input_thread_t * p_input )
+{
+    return( -1 );
 }
 
+
+
+/*
+ * Data access functions
+ */
+
 /*****************************************************************************
- * DVDInit: initializes DVD structures
+ * DVDOpen: open dvd
  *****************************************************************************/
-static void DVDInit( input_thread_t * p_input )
+static int DVDOpen( struct input_thread_s *p_input )
 {
+    struct stat          stat_info;
+    char *               psz_parser = p_input->psz_name;
+    char *               psz_device = p_input->psz_name;
+    dvdcss_handle        dvdhandle;
     thread_dvd_data_t *  p_dvd;
     input_area_t *       p_area;
     int                  i_title;
     int                  i_chapter;
     int                  i;
 
-    p_dvd = malloc( sizeof(thread_dvd_data_t) );
-    if( p_dvd == NULL )
+    /* Parse input string : device[@rawdevice] */
+    while( *psz_parser && *psz_parser != '@' )
     {
-        intf_ErrMsg( "dvd error: out of memory" );
-        p_input->b_error = 1;
-        return;
+        psz_parser++;
     }
 
-    p_input->p_plugin_data = (void *)p_dvd;
+    if( *psz_parser == '@' )
+    {
+        /* Found raw device */
+        *psz_parser = '\0';
+        psz_parser++;
+
+        config_PutPszVariable( "DVDCSS_RAW_DEVICE", psz_parser );
+    }
 
-    if( (p_input->p_method_data = input_BuffersInit()) == NULL )
+    if( stat( psz_device, &stat_info ) == -1 )
     {
-        p_input->b_error = 1;
-        return;
+        intf_ErrMsg( "input error: cannot stat() device `%s' (%s)",
+                     psz_device, strerror(errno));
+        return( -1 );                    
+    }
+    
+#ifndef WIN32    
+    if( !S_ISBLK(stat_info.st_mode) && !S_ISCHR(stat_info.st_mode) )
+    {
+        intf_WarnMsg( 3, "input : DVD plugin discarded"
+                         " (not a valid block device)" );
+        return -1;
     }
+#endif
+
+    intf_WarnMsg( 2, "input: dvd=%s raw=%s", psz_device, psz_parser );
+    
+    /* 
+     * set up input
+     */ 
+    p_input->i_mtu = 0;
+
+    vlc_mutex_lock( &p_input->stream.stream_lock );
 
-    p_dvd->dvdhandle = (dvdcss_handle) p_input->p_handle;
+    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;
+
+    vlc_mutex_unlock( &p_input->stream.stream_lock );
+
+    /*
+     *  get plugin ready
+     */ 
+    dvdhandle = dvdcss_open( psz_device );
+
+    if( dvdhandle == NULL )
+    {
+        intf_ErrMsg( "dvd error: dvdcss can't open device" );
+        return -1;
+    }
+
+    p_dvd = malloc( sizeof(thread_dvd_data_t) );
+    if( p_dvd == NULL )
+    {
+        intf_ErrMsg( "dvd error: out of memory" );
+        return -1;
+    }
+
+    p_dvd->dvdhandle = (dvdcss_handle) dvdhandle;
+    p_input->p_access_data = (void *)p_dvd;
 
     if( dvdcss_seek( p_dvd->dvdhandle, 0, DVDCSS_NOFLAGS ) < 0 )
     {
         intf_ErrMsg( "dvd error: %s", dvdcss_error( p_dvd->dvdhandle ) );
-        p_input->b_error = 1;
-        return;
+        return -1;
     }
 
-    /* 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;
-    /* this value mustn't be modifed */
-    p_input->i_read_once = DVD_DATA_READ_ONCE;
-
     /* Ifo allocation & initialisation */
     if( IfoCreate( p_dvd ) < 0 )
     {
         intf_ErrMsg( "dvd error: allcation error in ifo" );
-        dvdcss_close( p_dvd->dvdhandle );
         free( p_dvd );
-        p_input->b_error = 1;
-        return;
+        return -1;
     }
 
     if( IfoInit( p_dvd->p_ifo ) < 0 )
     {
         intf_ErrMsg( "dvd error: fatal failure in ifo" );
         IfoDestroy( p_dvd->p_ifo );
-        dvdcss_close( p_dvd->dvdhandle );
         free( p_dvd );
-        p_input->b_error = 1;
-        return;
+        return -1;
     }
 
     /* Set stream and area data */
@@ -287,7 +393,7 @@ static void DVDInit( input_thread_t * p_input )
 #undef area
 
     /* Get requested title - if none try the first title */
-    i_title = main_GetIntVariable( INPUT_TITLE_VAR, 1 );
+    i_title = config_GetIntVariable( INPUT_TITLE_VAR );
     if( i_title <= 0 || i_title > title_inf.i_title_nb )
     {
         i_title = 1;
@@ -296,107 +402,44 @@ static void DVDInit( input_thread_t * p_input )
 #undef title_inf
 
     /* Get requested chapter - if none defaults to first one */
-    i_chapter = main_GetIntVariable( INPUT_CHAPTER_VAR, 1 );
+    i_chapter = config_GetIntVariable( INPUT_CHAPTER_VAR );
     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[i_title];
+    p_area->i_part = i_chapter;
 
     /* set title, chapter, audio and subpic */
-    DVDSetArea( p_input, p_area );
-
-    vlc_mutex_unlock( &p_input->stream.stream_lock );
-
-    return;
-}
-
-/*****************************************************************************
- * DVDOpen: open dvd
- *****************************************************************************/
-static void DVDOpen( struct input_thread_s *p_input )
-{
-    dvdcss_handle dvdhandle;
-    char * psz_parser;
-    char * psz_device;
-
-    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 );
-
-    /* Parse input string : dvd:device[:rawdevice] */
-    if( strlen( p_input->p_source ) > 4
-         && !strncasecmp( p_input->p_source, "dvd:", 4 ) )
-    {
-        psz_parser = psz_device = p_input->p_source + 4;
-    }
-    else
+    if( DVDSetArea( p_input, p_area ) )
     {
-        psz_parser = psz_device = p_input->p_source;
-    }
-
-    while( *psz_parser && *psz_parser != '@' )
-    {
-        psz_parser++;
-    }
-
-    if( *psz_parser == '@' )
-    {
-        /* Found raw device */
-        *psz_parser = '\0';
-        psz_parser++;
-
-        main_PutPszVariable( "DVDCSS_RAW_DEVICE", psz_parser );
+        vlc_mutex_unlock( &p_input->stream.stream_lock );
+        return -1;
     }
 
-    intf_WarnMsg( 2, "input: dvd=%s raw=%s", psz_device, psz_parser );
-
-    dvdhandle = dvdcss_open( psz_device );
+    vlc_mutex_unlock( &p_input->stream.stream_lock );
 
-    if( dvdhandle == NULL )
-    {
-        intf_ErrMsg( "dvd error: dvdcss can't open device" );
-        p_input->b_error = 1;
-        return;
-    }
+    return 0;
 
-    p_input->p_handle = (void *) dvdhandle;
 }
 
 /*****************************************************************************
  * DVDClose: close dvd
  *****************************************************************************/
 static void DVDClose( struct input_thread_s *p_input )
-{
-    /* Clean up libdvdcss */
-    dvdcss_close( (dvdcss_handle) p_input->p_handle );
-}
-
-/*****************************************************************************
- * DVDEnd: frees unused data
- *****************************************************************************/
-static void DVDEnd( input_thread_t * p_input )
 {
     thread_dvd_data_t *     p_dvd;
 
-    p_dvd = (thread_dvd_data_t*)p_input->p_plugin_data;
+    p_dvd = (thread_dvd_data_t*)p_input->p_access_data;
 
     IfoDestroy( p_dvd->p_ifo );
 
+    p_input->p_access_data = (void *)(p_dvd->dvdhandle);
     free( p_dvd );
 
-    input_BuffersEnd( p_input->p_method_data );
+    /* Clean up libdvdcss */
+    dvdcss_close( (dvdcss_handle) p_input->p_access_data );
 }
 
 /*****************************************************************************
@@ -423,11 +466,9 @@ static int DVDSetArea( input_thread_t * p_input, input_area_t * p_area )
     int                  i_vts_title;
     int                  i_audio_nb = 0;
     int                  i_spu_nb = 0;
-    int                  i_audio;
-    int                  i_spu;
     int                  i;
 
-    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;
@@ -468,15 +509,14 @@ static int DVDSetArea( input_thread_t * p_input, input_area_t * p_area )
         p_dvd->i_title_id =
             vts.title_inf.p_title_start[i_vts_title-1].i_title_id;
 
-        intf_WarnMsgImm( 3, "dvd: title %d vts_title %d pgc %d",
-                         p_dvd->i_title, i_vts_title, p_dvd->i_title_id );
-
+        intf_WarnMsg( 3, "dvd: title %d vts_title %d pgc %d",
+                      p_dvd->i_title, i_vts_title, p_dvd->i_title_id );
 
         /*
          * Angle management
          */
         p_dvd->i_angle_nb = vmg.title_inf.p_attr[p_dvd->i_title-1].i_angle_nb;
-        p_dvd->i_angle = main_GetIntVariable( INPUT_ANGLE_VAR, 1 );
+        p_dvd->i_angle = config_GetIntVariable( INPUT_ANGLE_VAR );
         if( ( p_dvd->i_angle <= 0 ) || p_dvd->i_angle > p_dvd->i_angle_nb )
         {
             p_dvd->i_angle = 1;
@@ -570,6 +610,7 @@ static int DVDSetArea( input_thread_t * p_input, input_area_t * p_area )
 
         input_AddProgram( p_input, 0, sizeof( stream_ps_data_t ) );
         p_input->stream.p_selected_program = p_input->stream.pp_programs[0]; 
+//        p_input->stream.p_new_program = p_input->stream.pp_programs[0]; 
 
         /* No PSM to read in DVD mode, we already have all information */
         p_input->stream.p_selected_program->b_is_ok = 1;
@@ -584,11 +625,7 @@ static int DVDSetArea( input_thread_t * p_input, input_area_t * p_area )
         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_status \
     vts.title_unit.p_title[p_dvd->i_title_id-1].title.pi_audio_status[i-1]
         /* Audio ES, in the order they appear in .ifo */
@@ -709,57 +746,14 @@ static int DVDSetArea( input_thread_t * p_input, input_area_t * p_area )
             }
         }
 #undef spu_status
-        if( p_main->b_audio )
+    
+        /* FIXME: hack to check that the demuxer is ready, and set
+        * the decoders */
+        if( p_input->p_demux_module )
         {
-            /* For audio: first one if none or a not existing one specified */
-            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 <=
-                             vts.manager_inf.i_audio_nb ) )
-                    {
-                        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] );
-                }
-            }
+            DVDLaunchDecoders( p_input );
         }
 
-        if( p_main->b_video )
-        {
-            /* for spu, default is none */
-            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 += vts.manager_inf.i_audio_nb;
-                input_SelectES( p_input, p_input->stream.pp_es[i_spu] );
-            }
-        }
     } /* i_title >= 0 */
     else
     {
@@ -831,169 +825,109 @@ static int DVDSetArea( input_thread_t * p_input, input_area_t * p_area )
 
 
 /*****************************************************************************
- * DVDRead: reads data packets into the netlist.
+ * DVDRead: reads data packets.
  *****************************************************************************
- * Returns -1 in case of error, 0 if everything went well, and 1 in case of
- * EOF.
+ * Returns -1 in case of error, 0 in case of EOF, otherwise the number of
+ * bytes.
  *****************************************************************************/
 static int DVDRead( input_thread_t * p_input,
-                    data_packet_t ** pp_packets )
+                    byte_t * p_buffer, size_t i_count )
 {
     thread_dvd_data_t *     p_dvd;
-    struct iovec            p_vec[DVD_DATA_READ_ONCE];
-    u8 *                    pi_cur;
     int                     i_block_once;
-    int                     i_packet_size;
-    int                     i_iovec;
-    int                     i_packet;
-    int                     i_pos;
     int                     i_read_blocks;
+    int                     i_read_total;
     int                     i_sector;
-    boolean_t               b_eof;
-    boolean_t               b_eot;
+    int                     i_blocks;
     boolean_t               b_eoc;
-    data_packet_t *         p_data;
 
-    p_dvd = (thread_dvd_data_t *)p_input->p_plugin_data;
+    p_dvd = (thread_dvd_data_t *)(p_input->p_access_data);
 
+    i_sector = 0;
+    i_read_total = 0;
+    i_read_blocks = 0;
     b_eoc = 0;
-    i_sector = p_dvd->i_title_start + p_dvd->i_sector;
-    i_block_once = p_dvd->i_end_sector - p_dvd->i_sector + 1;
 
+    i_blocks = OFF2LB(i_count);
 
-    /* Get the position of the next cell if we're at cell end */
-    if( i_block_once <= 0 )
+    while( i_blocks )
     {
-        int     i_angle;
-
-        p_dvd->i_cell++;
-        p_dvd->i_angle_cell++;
+        i_sector = p_dvd->i_title_start + p_dvd->i_sector;
+        i_block_once = p_dvd->i_end_sector - p_dvd->i_sector + 1;
 
-        /* Find cell index in adress map */
-        if( DVDFindSector( p_dvd ) < 0 )
+        /* Get the position of the next cell if we're at cell end */
+        if( i_block_once <= 0 )
         {
-            pp_packets[0] = NULL;
-            intf_ErrMsg( "dvd error: can't find next cell" );
-            return 1;
-        }
+            int     i_angle;
 
-        /* Position the fd pointer on the right address */
-        if( ( i_sector = dvdcss_seek( p_dvd->dvdhandle,
-                                      p_dvd->i_title_start + p_dvd->i_sector,
-                                      DVDCSS_SEEK_MPEG ) ) < 0 )
-        {
-            intf_ErrMsg( "dvd error: %s", dvdcss_error( p_dvd->dvdhandle ) );
-            return -1;
-        }
+            p_dvd->i_cell++;
+            p_dvd->i_angle_cell++;
 
-        /* update chapter : it will be easier when we have navigation
-         * ES support */
-        if( p_dvd->i_chapter < ( p_dvd->i_chapter_nb - 1 ) )
-        {
-            if( title.p_cell_play[p_dvd->i_prg_cell].i_category & 0xf000 )
-            {
-                i_angle = p_dvd->i_angle - 1;
-            }
-            else
+            /* Find cell index in adress map */
+            if( DVDFindSector( p_dvd ) < 0 )
             {
-                i_angle = 0;
+                intf_ErrMsg( "dvd error: can't find next cell" );
+                return 1;
             }
-            if( title.chapter_map.pi_start_cell[p_dvd->i_chapter] <=
-                ( p_dvd->i_prg_cell - i_angle + 1 ) )
-            {
-                p_dvd->i_chapter++;
-                b_eoc = 1;
-            }
-        }
-
-        i_block_once = p_dvd->i_end_sector - p_dvd->i_sector + 1;
-    }
-
-    /* The number of blocks read is the max between the requested
-     * value and the leaving block in the cell */
-    if( i_block_once > p_dvd->i_block_once )
-    {
-        i_block_once = p_dvd->i_block_once;
-    }
-/*
-intf_WarnMsg( 2, "Sector: 0x%x Read: %d Chapter: %d", p_dvd->i_sector, i_block_once, p_dvd->i_chapter );
-*/
-
-    /* Get iovecs */
-    p_data = input_BuffersToIO( p_input->p_method_data, p_vec,
-                                DVD_DATA_READ_ONCE );
-
-    if ( p_data == NULL )
-    {
-        return( -1 );
-    }
-
-    /* Reads from DVD */
-    i_read_blocks = dvdcss_readv( p_dvd->dvdhandle, p_vec,
-                                  i_block_once, DVDCSS_READ_DECRYPT );
-
-    /* Update global position */
-    p_dvd->i_sector += i_read_blocks;
-
-    i_packet = 0;
-
-    /* Read headers to compute payload length */
-    for( i_iovec = 0 ; i_iovec < i_read_blocks ; i_iovec++ )
-    {
-        data_packet_t * p_current = p_data;
-        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
+            /* Position the fd pointer on the right address */
+            if( ( i_sector = dvdcss_seek( p_dvd->dvdhandle,
+                                p_dvd->i_title_start + p_dvd->i_sector,
+                                DVDCSS_SEEK_MPEG ) ) < 0 )
             {
-                /* MPEG-2 Pack header. */
-                i_packet_size = 8;
-            }
-            if( i_pos != 0 )
-            {
-                pp_packets[i_packet] = input_ShareBuffer( 
-                        p_input->p_method_data, p_current );
+                intf_ErrMsg( "dvd error: %s", dvdcss_error( p_dvd->dvdhandle ) );
+                return -1;
             }
-            else
+
+            /* update chapter : it will be easier when we have navigation
+             * ES support */
+            if( p_dvd->i_chapter < ( p_dvd->i_chapter_nb - 1 ) )
             {
-                pp_packets[i_packet] = p_data;
-                p_data = p_data->p_next;
+                if( title.p_cell_play[p_dvd->i_prg_cell].i_category & 0xf000 )
+                {
+                    i_angle = p_dvd->i_angle - 1;
+                }
+                else
+                {
+                    i_angle = 0;
+                }
+                if( title.chapter_map.pi_start_cell[p_dvd->i_chapter] <=
+                    ( p_dvd->i_prg_cell - i_angle + 1 ) )
+                {
+                    p_dvd->i_chapter++;
+                    b_eoc = 1;
+                }
             }
 
-            pp_packets[i_packet]->p_payload_start =
-                    pp_packets[i_packet]->p_demux_start =
-                    pp_packets[i_packet]->p_demux_start + i_pos;
-
-            pp_packets[i_packet]->p_payload_end =
-                    pp_packets[i_packet]->p_payload_start + i_packet_size + 6;
+            i_block_once = p_dvd->i_end_sector - p_dvd->i_sector + 1;
+        }
 
-            i_packet++;
-            i_pos += i_packet_size + 6;
+        /* The number of blocks read is the max between the requested
+         * value and the leaving block in the cell */
+        if( i_block_once > i_blocks )
+        {
+            i_block_once = i_blocks;
         }
-    }
+    /*
+    intf_WarnMsg( 2, "Sector: 0x%x Read: %d Chapter: %d", p_dvd->i_sector, i_block_once, p_dvd->i_chapter );
+    */
 
-    pp_packets[i_packet] = NULL;
+        /* Reads from DVD */
+        i_read_blocks = dvdcss_read( p_dvd->dvdhandle, p_buffer,
+                                     i_block_once, DVDCSS_READ_DECRYPT );
 
-    while( p_data != NULL )
-    {
-        data_packet_t * p_next = p_data->p_next;
-        p_input->pf_delete_packet( p_input->p_method_data, p_data );
-        p_data = p_next;
+        i_blocks -= i_read_blocks;
+        p_buffer += LB2OFF( i_read_blocks );
+        i_read_total += i_read_blocks;
+
+        /* Update global position */
+        p_dvd->i_sector += i_read_blocks;
     }
 
     vlc_mutex_lock( &p_input->stream.stream_lock );
 
     p_input->stream.p_selected_area->i_tell =
-        LB2OFF( i_sector + i_read_blocks ) -
+        LB2OFF( i_sector + i_read_total ) -
         p_input->stream.p_selected_area->i_start;
     if( b_eoc )
     {
@@ -1002,41 +936,32 @@ intf_WarnMsg( 2, "Sector: 0x%x Read: %d Chapter: %d", p_dvd->i_sector, i_block_o
         p_input->stream.p_selected_area->i_part = p_dvd->i_chapter;
     }
 
-    b_eot = !( p_input->stream.p_selected_area->i_tell
-                  < p_input->stream.p_selected_area->i_size );
-    b_eof = b_eot && ( ( p_dvd->i_title + 1 ) >= p_input->stream.i_area_nb );
-
-    if( b_eof )
+    if( p_input->stream.p_selected_area->i_tell
+            >= p_input->stream.p_selected_area->i_size )
     {
-        vlc_mutex_unlock( &p_input->stream.stream_lock );
-        return 1;
-    }
+        if( ( p_dvd->i_title + 1 ) >= p_input->stream.i_area_nb )
+        {
+            /* EOF */
+            vlc_mutex_unlock( &p_input->stream.stream_lock );
+            return 0;
+        }
 
-    if( b_eot )
-    {
+        /* EOT */
         intf_WarnMsg( 4, "dvd info: new title" );
         p_dvd->i_title++;
         DVDSetArea( p_input, p_input->stream.pp_areas[p_dvd->i_title] );
         vlc_mutex_unlock( &p_input->stream.stream_lock );
-        return 0;
+        return LB2OFF( i_read_total );
     }
 
     vlc_mutex_unlock( &p_input->stream.stream_lock );
-
-    if( i_read_blocks == i_block_once )
+/*
+    if( i_read_blocks != i_block_once )
     {
-        return 0;
+        return -1;
     }
-
-    return -1;
-}
-
-/*****************************************************************************
- * DVDRewind : reads a stream backward
- *****************************************************************************/
-static int DVDRewind( input_thread_t * p_input )
-{
-    return( -1 );
+*/
+    return LB2OFF( i_read_total );
 }
 
 /*****************************************************************************
@@ -1055,11 +980,13 @@ static void DVDSeek( input_thread_t * p_input, off_t i_off )
     int                     i_chapter;
     int                     i_angle;
     
-    p_dvd = ( thread_dvd_data_t * )p_input->p_plugin_data;
+    p_dvd = ( thread_dvd_data_t * )(p_input->p_access_data);
 
+    vlc_mutex_lock( &p_input->stream.stream_lock );
     /* we have to take care of offset of beginning of title */
     p_dvd->i_sector = OFF2LB(i_off + p_input->stream.p_selected_area->i_start)
                        - p_dvd->i_title_start;
+    vlc_mutex_unlock( &p_input->stream.stream_lock );
 
     i_prg_cell = 0;
     i_chapter = 0;
@@ -1105,12 +1032,12 @@ static void DVDSeek( input_thread_t * p_input, off_t i_off )
      * can be very wide out of such zones */
     if( title.p_cell_play[p_dvd->i_prg_cell].i_category & 0xf000 )
     {
-        p_dvd->i_sector = MAX(
+        p_dvd->i_sector = __MAX(
                 cell.i_start_sector,
                 title.p_cell_play[p_dvd->i_prg_cell].i_start_sector );
     }
 
-    p_dvd->i_end_sector = MIN(
+    p_dvd->i_end_sector = __MIN(
             cell.i_end_sector,
             title.p_cell_play[p_dvd->i_prg_cell].i_end_sector );
 #undef cell
@@ -1138,7 +1065,6 @@ static void DVDSeek( input_thread_t * p_input, off_t i_off )
     }
 
     p_dvd->i_chapter = i_chapter;
-    p_input->stream.p_selected_area->i_part = p_dvd->i_chapter;
 
     if( ( i_block = dvdcss_seek( p_dvd->dvdhandle,
                                  p_dvd->i_title_start + p_dvd->i_sector,
@@ -1149,13 +1075,15 @@ static void DVDSeek( input_thread_t * p_input, off_t i_off )
         return;
     }
 
+    vlc_mutex_lock( &p_input->stream.stream_lock );
+    p_input->stream.p_selected_area->i_part = p_dvd->i_chapter;
     p_input->stream.p_selected_area->i_tell =
         LB2OFF ( i_block ) - p_input->stream.p_selected_area->i_start;
+    vlc_mutex_unlock( &p_input->stream.stream_lock );
 
     intf_WarnMsg( 7, "Program Cell: %d Cell: %d Chapter: %d",
                      p_dvd->i_prg_cell, p_dvd->i_cell, p_dvd->i_chapter );
 
-
     return;
 }
 
@@ -1226,10 +1154,10 @@ static int DVDFindSector( thread_dvd_data_t * p_dvd )
     
     /* Find start and end sectors of new cell */
 #if 1
-    p_dvd->i_sector = MAX(
+    p_dvd->i_sector = __MAX(
          p_dvd->p_ifo->vts.cell_inf.p_cell_map[p_dvd->i_cell].i_start_sector,
          title.p_cell_play[p_dvd->i_prg_cell].i_start_sector );
-    p_dvd->i_end_sector = MIN(
+    p_dvd->i_end_sector = __MIN(
          p_dvd->p_ifo->vts.cell_inf.p_cell_map[p_dvd->i_cell].i_end_sector,
          title.p_cell_play[p_dvd->i_prg_cell].i_end_sector );
 #else
@@ -1317,3 +1245,74 @@ static int DVDChooseAngle( thread_dvd_data_t * p_dvd )
 }
 
 #undef title
+/*****************************************************************************
+ * DVDLaunchDecoders
+ *****************************************************************************/
+static void DVDLaunchDecoders( input_thread_t * p_input )
+{
+    thread_dvd_data_t *  p_dvd;
+    int                  i_audio;
+    int                  i_spu;
+
+    p_dvd = (thread_dvd_data_t*)(p_input->p_access_data);
+
+    /* Select Video stream (always 0) */
+    if( p_main->b_video )
+    {
+        input_SelectES( p_input, p_input->stream.pp_es[0] );
+    }
+
+    /* Select audio stream */
+    if( p_main->b_audio )
+    {
+        /* For audio: first one if none or a not existing one specified */
+        i_audio = config_GetIntVariable( INPUT_CHANNEL_VAR );
+        if( i_audio < 0 /*|| i_audio > i_audio_nb*/ )
+        {
+            config_PutIntVariable( INPUT_CHANNEL_VAR, 1 );
+            i_audio = 1;
+        }
+        if( i_audio > 0 /*&& i_audio_nb > 0*/ )
+        {
+            if( config_GetIntVariable( AOUT_SPDIF_VAR ) ||
+                ( config_GetIntVariable( INPUT_AUDIO_VAR ) ==
+                  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_ifo->vts.manager_inf.i_audio_nb ) )
+                {
+                    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] );
+            }
+        }
+    }
+
+    /* Select subtitle */
+    if( p_main->b_video )
+    {
+        /* for spu, default is none */
+        i_spu = config_GetIntVariable( INPUT_SUBTITLE_VAR );
+        if( i_spu < 0 /*|| i_spu > i_spu_nb*/ )
+        {
+            config_PutIntVariable( INPUT_SUBTITLE_VAR, 0 );
+            i_spu = 0;
+        }
+        if( i_spu > 0 /* && i_spu_nb > 0*/ )
+        {
+            i_spu += p_dvd->p_ifo->vts.manager_inf.i_audio_nb;
+            input_SelectES( p_input, p_input->stream.pp_es[i_spu] );
+        }
+    }
+}