]> git.sesse.net Git - vlc/blobdiff - plugins/dvd/input_dvd.c
* Added a dummy libdvdcss so that the DVD plugin can be used without
[vlc] / plugins / dvd / input_dvd.c
index 8172c0e9dc228d2ac1c01289e30970ab0ec7556b..8757b6b71f8463b7cf7a0ba5a00ee96b2197523d 100644 (file)
@@ -5,12 +5,12 @@
  * especially the 2048 bytes logical block size.
  * It depends on:
  *  -input_netlist used to read packets
+ *  -libdvdcss for access and unscrambling
  *  -dvd_ifo for ifo parsing and analyse
- *  -dvd_css for unscrambling
  *  -dvd_udf to find files
  *****************************************************************************
  * Copyright (C) 1998-2001 VideoLAN
- * $Id: input_dvd.c,v 1.65 2001/05/31 03:57:54 sam Exp $
+ * $Id: input_dvd.c,v 1.84 2001/08/06 13:28:00 sam 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"
 
-#ifdef HAVE_CSS
-#   define MODULE_NAME dvd
-#else /* HAVE_CSS */
-#   define MODULE_NAME dvdnocss
-#endif /* HAVE_CSS */
-
-#include "modules_inner.h"
-
 #include <stdio.h>
 #include <stdlib.h>
 
 #   include <unistd.h>
 #endif
 
-#if !defined( WIN32 )
-#   include <netinet/in.h>
-#endif
-
 #include <fcntl.h>
 #include <sys/types.h>
 #include <string.h>
 #endif
 
 #if defined( WIN32 )
-#   include <io.h>
-#   include "input_iovec.h"
+#   include <io.h>                                                 /* read() */
 #else
 #   include <sys/uio.h>                                      /* struct iovec */
 #endif
 
+#ifdef GOD_DAMN_DMCA
+#   include "dummy_dvdcss.h"
+#else
+#   include <videolan/dvdcss.h>
+#endif
+
 #include "config.h"
 #include "common.h"
 #include "threads.h"
 #include "mtime.h"
 #include "tests.h"
 
+#if defined( WIN32 )
+#   include "input_iovec.h"
+#endif
+
 #include "intf_msg.h"
 
 #include "main.h"
 #include "stream_control.h"
 #include "input_ext-intf.h"
 #include "input_ext-dec.h"
+#include "input_ext-plugins.h"
 
-#include "input.h"
-
+#include "input_dvd.h"
 #include "dvd_netlist.h"
 #include "dvd_ifo.h"
-#include "dvd_css.h"
 #include "dvd_summary.h"
-#include "input_dvd.h"
-#include "mpeg_system.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)
+
+/* Size of netlist */
+#define DVD_NETLIST_SIZE    256
 
 /*****************************************************************************
  * Local prototypes
 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 void DVDClose    ( struct input_thread_s * );
 static int  DVDSetArea  ( struct input_thread_s *, struct input_area_s * );
 static int  DVDRead     ( struct input_thread_s *, data_packet_t ** );
 static void DVDSeek     ( struct input_thread_s *, off_t );
@@ -123,9 +130,10 @@ void _M( input_getfunctions )( function_list_t * p_function_list )
 #define input p_function_list->functions.input
     p_function_list->pf_probe = DVDProbe;
     input.pf_init             = DVDInit;
-    input.pf_open             = NULL; /* Set in DVDInit */
-    input.pf_close            = NULL;
+    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_demux            = input_DemuxPS;
@@ -150,52 +158,20 @@ static int DVDProbe( probedata_t *p_data )
     input_thread_t * p_input = (input_thread_t *)p_data;
 
     char * psz_name = p_input->p_source;
-    int i_handle;
     int i_score = 5;
-#if defined( WIN32 )
-    char buf[7];
-#endif
 
     if( TestMethod( INPUT_METHOD_VAR, "dvd" ) )
     {
-#ifdef HAVE_CSS
         return( 999 );
-#else /* HAVE_CSS */
-        return( 998 );
-#endif /* HAVE_CSS */
     }
 
     if( ( strlen(psz_name) > 4 ) && !strncasecmp( psz_name, "dvd:", 4 ) )
     {
         /* If the user specified "dvd:" then it's probably a DVD */
-#ifdef HAVE_CSS
         i_score = 100;
-#else /* HAVE_CSS */
-        i_score = 90;
-#endif /* HAVE_CSS */
         psz_name += 4;
     }
 
-#if !defined( WIN32 )
-    i_handle = open( psz_name, 0 );
-    if( i_handle == -1 )
-    {
-        return( 0 );
-    }
-    close( i_handle );
-#else
-    snprintf( buf, 7, "\\\\.\\%c:", psz_name[0] );
-    (HANDLE) i_handle = CreateFile( i_score < 90 ? psz_name : buf, 
-                        GENERIC_READ | GENERIC_WRITE,
-                        FILE_SHARE_READ | FILE_SHARE_WRITE,
-                        NULL, OPEN_EXISTING, 0, NULL );
-    if( (HANDLE) i_handle == INVALID_HANDLE_VALUE )
-    {
-        return( 0 );
-    }
-    CloseHandle( (HANDLE) i_handle );
-#endif
-
     return( i_score );
 }
 
@@ -221,41 +197,28 @@ static void DVDInit( input_thread_t * p_input )
     p_input->p_plugin_data = (void *)p_dvd;
     p_input->p_method_data = NULL;
 
-    p_dvd->i_fd = p_input->i_handle;
+    p_dvd->dvdhandle = (dvdcss_handle) p_input->i_handle;
 
-    /* reading several block once seems to cause lock-up
-     * when using input_ToggleES
-     * who wrote thez damn buggy piece of shit ??? --stef */
-    p_dvd->i_block_once = 32;
-    p_input->i_read_once = 128;
+    dvdcss_seek( p_dvd->dvdhandle, 0 );
 
-    i = CSSTest( p_input->i_handle );
-
-    if( i < 0 )
-    {
-        intf_ErrMsg( "dvd error: error in css" );
-        free( p_dvd );
-        p_input->b_error = 1;
-        return;
-    }
-
-    p_dvd->b_encrypted = i;
-
-#if !defined( WIN32 )
-    lseek( p_input->i_handle, 0, SEEK_SET );
-#else
-    SetFilePointer( (HANDLE) p_input->i_handle, 0, 0, FILE_BEGIN );
-#endif
+    /* 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;
 
     /* Reading structures initialisation */
     p_input->p_method_data =
-        DVDNetlistInit( 2048, 4096, 2048, DVD_LB_SIZE, p_dvd->i_block_once );
+        DVDNetlistInit( DVD_NETLIST_SIZE, 2 * DVD_NETLIST_SIZE,
+                        DVD_NETLIST_SIZE, DVD_LB_SIZE, p_dvd->i_block_once );
     intf_WarnMsg( 2, "dvd info: netlist initialized" );
 
     /* 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;
     }
@@ -263,37 +226,13 @@ static void DVDInit( input_thread_t * p_input )
     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;
     }
 
-    /* CSS initialisation */
-    if( p_dvd->b_encrypted )
-    {
-        p_dvd->p_css = malloc( sizeof(css_t) );
-        if( p_dvd->p_css == NULL )
-        {
-            intf_ErrMsg( "dvd error: couldn't create css structure" );
-            free( p_dvd );
-            p_input->b_error = 1;
-            return;
-        }
-
-        p_dvd->p_css->i_agid = 0;
-
-        if( CSSInit( p_input->i_handle, p_dvd->p_css ) < 0 )
-        {
-            intf_ErrMsg( "dvd error: fatal failure in css" );
-            free( p_dvd->p_css );
-            free( p_dvd );
-            p_input->b_error = 1;
-            return;
-        }
-
-        intf_WarnMsg( 2, "dvd info: css initialized" );
-    }
-
     /* Set stream and area data */
     vlc_mutex_lock( &p_input->stream.stream_lock );
 
@@ -331,8 +270,8 @@ static void DVDInit( input_thread_t * p_input )
         area[i]->i_angle = 1;
 
         /* Offset to vts_i_0.ifo */
-        area[i]->i_plugin_data = p_dvd->p_ifo->i_off +
-                       ( title_inf.p_attr[i-1].i_start_sector * DVD_LB_SIZE );
+        area[i]->i_plugin_data = p_dvd->p_ifo->i_start +
+                       title_inf.p_attr[i-1].i_start_sector;
     }   
 #undef area
 
@@ -364,6 +303,54 @@ static void DVDInit( input_thread_t * p_input )
     return;
 }
 
+/*****************************************************************************
+ * DVDOpen: open dvd
+ *****************************************************************************/
+static void DVDOpen( struct input_thread_s *p_input )
+{
+    dvdcss_handle dvdhandle;
+
+    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 ) > 4
+         && !strncasecmp( p_input->p_source, "dvd:", 4 ) )
+    {
+        dvdhandle = dvdcss_open( p_input->p_source + 4, DVDCSS_INIT_QUIET );
+    }
+    else
+    {
+        dvdhandle = dvdcss_open( p_input->p_source, DVDCSS_INIT_QUIET );
+    }
+
+    if( dvdhandle == NULL )
+    {
+        p_input->b_error = 1;
+        return;
+    }
+
+    p_input->i_handle = (int) dvdhandle;
+}
+
+/*****************************************************************************
+ * DVDClose: close dvd
+ *****************************************************************************/
+static void DVDClose( struct input_thread_s *p_input )
+{
+    /* Clean up libdvdcss */
+    dvdcss_close( (dvdcss_handle) p_input->i_handle );
+}
+
 /*****************************************************************************
  * DVDEnd: frees unused data
  *****************************************************************************/
@@ -375,13 +362,10 @@ static void DVDEnd( input_thread_t * p_input )
     p_dvd = (thread_dvd_data_t*)p_input->p_plugin_data;
     p_netlist = (dvd_netlist_t *)p_input->p_method_data;
 
-    if( p_dvd->b_encrypted )
-    {
-        free( p_dvd->p_css );
-    }
-
     IfoDestroy( p_dvd->p_ifo );
+
     free( p_dvd );
+
     DVDNetlistEnd( p_netlist );
 }
 
@@ -401,7 +385,6 @@ static int DVDSetArea( input_thread_t * p_input, input_area_t * p_area )
     int                  i_audio;
     int                  i_spu;
     int                  i;
-    int                  j;
 
     p_dvd = (thread_dvd_data_t*)p_input->p_plugin_data;
 
@@ -410,6 +393,8 @@ static int DVDSetArea( input_thread_t * p_input, input_area_t * p_area )
 
     if( p_area != p_input->stream.p_selected_area )
     {
+        /* Reset the Chapter position of the old title */
+        p_input->stream.p_selected_area->i_part = 0;
 
         /*
          *  We have to load all title information
@@ -418,9 +403,6 @@ static int DVDSetArea( input_thread_t * p_input, input_area_t * p_area )
         p_input->stream.p_selected_area =
                     p_input->stream.pp_areas[p_area->i_id];
 
-        /* release the lock to to let the interface go */
-//        vlc_mutex_unlock( &p_input->stream.stream_lock );
-
         /* title number: it is not vts nb!,
          * it is what appears in the interface list */
         p_dvd->i_title = p_area->i_id;
@@ -445,37 +427,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_WarnMsg( 3, "dvd: title %d vts_title %d pgc %d",
-                        p_dvd->i_title,
-                        i_vts_title,
-                        p_dvd->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 );
 
-        /* css title key for current vts */
-        if( p_dvd->b_encrypted )
-        {
-            /* this one is vts number */
-            p_dvd->p_css->i_title =
-                    vmg.title_inf.p_attr[p_dvd->i_title-1].i_title_set_num;
-            p_dvd->p_css->i_title_pos =
-                    vts.i_pos +
-                    vts.manager_inf.i_title_vob_start_sector * DVD_LB_SIZE;
-
-            j = CSSGetKey( p_input->i_handle, p_dvd->p_css );
-            if( j < 0 )
-            {
-                intf_ErrMsg( "dvd error: fatal error in vts css key" );
-                free( p_dvd );
-                p_input->b_error = 1;
-                return -1;
-            }
-            else if( j > 0 )
-            {
-                intf_ErrMsg( "dvd error: css decryption unavailable" );
-                free( p_dvd );
-                p_input->b_error = 1;
-                return -1;
-            }
-        }
+        /*
+         * Tell libdvdcss we changed title
+         */
+        dvdcss_title( p_dvd->dvdhandle,
+                      vts.i_pos + vts.manager_inf.i_title_vob_start_sector );
 
         /*
          * Angle management
@@ -491,9 +450,9 @@ static int DVDSetArea( input_thread_t * p_input, input_area_t * p_area )
          * Set selected title start and size
          */
         
-        /* title set offset */
-        p_dvd->i_title_start = vts.i_pos + DVD_LB_SIZE *
-                      (off_t)( vts.manager_inf.i_title_vob_start_sector );
+        /* title set offset XXX: convert to block values */
+        p_dvd->i_title_start =
+            vts.i_pos + vts.manager_inf.i_title_vob_start_sector;
 
         /* last video cell */
         p_dvd->i_cell = 0;
@@ -514,10 +473,7 @@ static int DVDSetArea( input_thread_t * p_input, input_area_t * p_area )
         }
 
         p_dvd->i_sector = 0;
-        p_dvd->i_size = DVD_LB_SIZE *
-          (off_t)( vts.cell_inf.p_cell_map[p_dvd->i_cell].i_end_sector );
-        intf_WarnMsg( 2, "dvd info: stream size 1: %lld @ %d", p_dvd->i_size,
-                      vts.cell_inf.p_cell_map[p_dvd->i_cell].i_end_sector );
+        p_dvd->i_size = vts.cell_inf.p_cell_map[p_dvd->i_cell].i_end_sector;
 
         if( DVDChapterSelect( p_dvd, 1 ) < 0 )
         {
@@ -526,18 +482,24 @@ static int DVDSetArea( input_thread_t * p_input, input_area_t * p_area )
             return -1;
         }
 
-        p_dvd->i_size -= (off_t)( p_dvd->i_sector + 1 ) *DVD_LB_SIZE;
+        p_dvd->i_size -= p_dvd->i_sector + 1;
 
         IfoPrintTitle( p_dvd );
 
-//        vlc_mutex_lock( &p_input->stream.stream_lock );
-
         /* Area definition */
-        p_input->stream.p_selected_area->i_start = p_dvd->i_start;
-        p_input->stream.p_selected_area->i_size = p_dvd->i_size;
+        p_input->stream.p_selected_area->i_start = LB2OFF( p_dvd->i_start );
+        p_input->stream.p_selected_area->i_size = LB2OFF( p_dvd->i_size );
         p_input->stream.p_selected_area->i_angle_nb = p_dvd->i_angle_nb;
         p_input->stream.p_selected_area->i_angle = p_dvd->i_angle;
 
+#if 0
+        /* start at the beginning of the title */
+        /* FIXME: create a conf option to select whether to restart
+         * title or not */
+        p_input->stream.p_selected_area->i_tell = 0;
+        p_input->stream.p_selected_area->i_part = 1;
+#endif
+
         /*
          * Destroy obsolete ES by reinitializing program 0
          * and find all ES in title with ifo data
@@ -562,7 +524,6 @@ static int DVDSetArea( input_thread_t * p_input, input_area_t * p_area )
 
         /* 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.pp_programs[0]->i_synchro_state = SYNCHRO_START;
 
         p_es = NULL;
 
@@ -573,12 +534,10 @@ 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;
-        intf_WarnMsg( 1, "dvd info: video mpeg2 stream" );
         if( p_main->b_video )
         {
             input_SelectES( p_input, p_es );
         }
-        intf_WarnMsg( 4, "dvd info: video selected" );
 
 #define audio_status \
     vts.title_unit.p_title[p_dvd->i_title_id-1].title.pi_audio_status[i-1]
@@ -604,9 +563,6 @@ static int DVDSetArea( input_thread_t * p_input, input_area_t * p_area )
                         vts.manager_inf.p_audio_attr[i-1].i_lang_code ) ) ); 
                     strcat( p_es->psz_desc, " (ac3)" );
     
-                    intf_WarnMsg( 3, "dvd info: audio stream %d %s\t(0x%x)",
-                                  i, p_es->psz_desc, i_id );
-    
                     break;
                 case 0x02:
                 case 0x03:              /* MPEG audio */
@@ -621,9 +577,6 @@ static int DVDSetArea( input_thread_t * p_input, input_area_t * p_area )
                         vts.manager_inf.p_audio_attr[i-1].i_lang_code ) ) ); 
                     strcat( p_es->psz_desc, " (mpeg)" );
     
-                    intf_WarnMsg( 3, "dvd info: audio stream %d %s\t(0x%x)",
-                                  i, p_es->psz_desc, i_id );
-    
                     break;
                 case 0x04:              /* LPCM */
     
@@ -638,8 +591,6 @@ static int DVDSetArea( input_thread_t * p_input, input_area_t * p_area )
                         vts.manager_inf.p_audio_attr[i-1].i_lang_code ) ) ); 
                     strcat( p_es->psz_desc, " (lpcm)" );
     
-                    intf_WarnMsg( 3, "dvd info: audio stream %d %s\t(0x%x)",
-                                  i, p_es->psz_desc, i_id );
                     break;
                 case 0x06:              /* DTS */
                     i_id = ( ( 0x88 + audio_status.i_position ) << 8 ) | 0xbd;
@@ -698,8 +649,6 @@ static int DVDSetArea( input_thread_t * p_input, input_area_t * p_area )
                 p_es->i_cat = SPU_ES;
                 strcpy( p_es->psz_desc, IfoLanguage( hton16(
                     vts.manager_inf.p_spu_attr[i-1].i_lang_code ) ) ); 
-                intf_WarnMsg( 3, "dvd info: spu stream %d %s\t(0x%x)",
-                              i, p_es->psz_desc, i_id );
             }
         }
 #undef spu_status
@@ -745,7 +694,6 @@ static int DVDSetArea( input_thread_t * p_input, input_area_t * p_area )
      * Chapter selection
      */
 
-    
     if( p_area->i_part != p_dvd->i_chapter )
     {
         if( ( p_area->i_part > 0 ) &&
@@ -758,11 +706,11 @@ static int DVDSetArea( input_thread_t * p_input, input_area_t * p_area )
                 return -1;
             }
     
-            p_input->stream.p_selected_area->i_tell = p_dvd->i_start -
-                                                      p_area->i_start;
+            p_input->stream.p_selected_area->i_tell =
+                                   LB2OFF( p_dvd->i_start ) - p_area->i_start;
             p_input->stream.p_selected_area->i_part = p_dvd->i_chapter;
     
-            intf_WarnMsg( 2, "dvd info: chapter %d start at: %lld",
+            intf_WarnMsg( 4, "dvd info: chapter %d start at: %lld",
                                         p_area->i_part, p_area->i_tell );
         }
         else
@@ -793,15 +741,13 @@ static int DVDSetArea( input_thread_t * p_input, input_area_t * p_area )
             p_dvd->i_angle = p_area->i_angle;
         }
 
-        intf_WarnMsg( 2, "dvd info: angle %d selected", p_area->i_angle );
+        intf_WarnMsg( 3, "dvd info: angle %d selected", p_area->i_angle );
     }
 
     /* warn interface that something has changed */
     p_input->stream.b_seekable = 1;
     p_input->stream.b_changed = 1;
 
-    p_input->stream.pp_programs[0]->i_synchro_state = SYNCHRO_REINIT;
-
     return 0;
 }
 
@@ -818,46 +764,24 @@ static int DVDRead( input_thread_t * p_input,
     thread_dvd_data_t *     p_dvd;
     dvd_netlist_t *         p_netlist;
     struct iovec *          p_vec;
-    struct data_packet_s ** pp_data;
+    struct data_packet_s *  pp_data[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_bytes;
     int                     i_read_blocks;
-    off_t                   i_off;
+    int                     i_sector;
     boolean_t               b_eof;
     boolean_t               b_eot;
-
-    pp_data = (struct data_packet_s **) malloc( p_input->i_read_once *
-                                        sizeof( struct data_packet_s * ) );
-    if( pp_data == NULL )
-    {
-        intf_ErrMsg( "dvd error: out of memory" );
-        return -1;
-    }
-
-    pp_data = (struct data_packet_s **) malloc( p_input->i_read_once *
-                                        sizeof( struct data_packet_s * ) );
-    if( pp_data == NULL )
-    {
-        intf_ErrMsg( "dvd error: out of memory" );
-        return -1;
-    }
+    boolean_t               b_eoc;
 
     p_dvd = (thread_dvd_data_t *)p_input->p_plugin_data;
     p_netlist = (dvd_netlist_t *)p_input->p_method_data;
 
-    /* Get an iovec pointer */
-    if( ( p_vec = DVDGetiovec( p_netlist ) ) == NULL )
-    {
-        intf_ErrMsg( "dvd error: can't get iovec" );
-        free( pp_data );
-        return -1;
-    }
-
+    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;
 
     /* Get the position of the next cell if we're at cell end */
@@ -873,14 +797,12 @@ static int DVDRead( input_thread_t * p_input,
         {
             pp_packets[0] = NULL;
             intf_ErrMsg( "dvd error: can't find next cell" );
-            free( pp_data );
             return 1;
         }
 
         /* Position the fd pointer on the right address */
-        i_off = lseek( p_dvd->i_fd,
-                       p_dvd->i_title_start +
-                       (off_t)( p_dvd->i_sector ) *DVD_LB_SIZE, SEEK_SET );
+        i_sector = dvdcss_seek( p_dvd->dvdhandle,
+                                p_dvd->i_title_start + p_dvd->i_sector );
 
         /* update chapter : it will be easier when we have navigation
          * ES support */
@@ -898,42 +820,37 @@ static int DVDRead( input_thread_t * p_input,
                 ( p_dvd->i_prg_cell - i_angle + 1 ) )
             {
                 p_dvd->i_chapter++;
+                b_eoc = 1;
             }
         }
 
-        vlc_mutex_lock( &p_input->stream.stream_lock );
-
-        p_input->stream.p_selected_area->i_tell = i_off -
-                                    p_input->stream.p_selected_area->i_start;
-        p_input->stream.p_selected_area->i_part = p_dvd->i_chapter;
-
-        /* the synchro has to be reinitialized when we change cell */
-        p_input->stream.pp_programs[0]->i_synchro_state = SYNCHRO_REINIT;
-
-        vlc_mutex_unlock( &p_input->stream.stream_lock );
-
         i_block_once = p_dvd->i_end_sector - p_dvd->i_sector + 1;
     }
 
-    /* the number of blocks read is the maw between the requested
+    /* 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 );
-
+/*
+intf_WarnMsg( 2, "Sector: 0x%x Read: %d Chapter: %d", p_dvd->i_sector, i_block_once, p_dvd->i_chapter );
+*/
     p_netlist->i_read_once = i_block_once;
 
+    /* Get an iovec pointer */
+    if( ( p_vec = DVDGetiovec( p_netlist ) ) == NULL )
+    {
+        intf_ErrMsg( "dvd error: can't get iovec" );
+        return -1;
+    }
+
     /* Reads from DVD */
-#if !defined( WIN32 )
-    i_read_bytes = readv( p_dvd->i_fd, p_vec, i_block_once );
-#else
-    i_read_bytes = ReadFileV( p_dvd->i_fd, p_vec, i_block_once );
-#endif
-    i_read_blocks = ( i_read_bytes + 0x7ff ) >> 11;
+    i_read_blocks = dvdcss_readv( p_dvd->dvdhandle, p_vec,
+                                  i_block_once, DVDCSS_READ_DECRYPT );
 
-    /* Update netlist indexes */
+    /* Update netlist indexes: we don't do it in DVDGetiovec since we
+     * need know the real number of blocks read */
     DVDMviovec( p_netlist, i_read_blocks, pp_data );
 
     /* Update global position */
@@ -944,13 +861,6 @@ static int DVDRead( input_thread_t * p_input,
     /* Read headers to compute payload length */
     for( i_iovec = 0 ; i_iovec < i_read_blocks ; i_iovec++ )
     {
-        if( p_dvd->b_encrypted )
-        {
-            CSSDescrambleSector( p_dvd->p_css->pi_title_key, 
-                                 p_vec[i_iovec].iov_base );
-            ((u8*)(p_vec[i_iovec].iov_base))[0x14] &= 0x8F;
-        }
-
         i_pos = 0;
 
         while( i_pos < p_netlist->i_buffer_size )
@@ -966,24 +876,8 @@ static int DVDRead( input_thread_t * p_input,
             }
             else
             {
-                /* Pack header. */
-                if( ( pi_cur[4] & 0xC0 ) == 0x40 )
-                {
-                    /* MPEG-2 */
-                    i_packet_size = 8;
-                }
-                else if( ( pi_cur[4] & 0xF0 ) == 0x20 )
-                {
-                    /* MPEG-1 */
-                    i_packet_size = 6;
-                }
-                else
-                {
-                    intf_ErrMsg( "Unable to determine stream type" );
-                    free( pp_data );
-                    return( -1 );
-                }
-
+                /* MPEG-2 Pack header. */
+                i_packet_size = 8;
                 pp_packets[i_packet] = pp_data[i_iovec];
 
             }
@@ -1012,18 +906,23 @@ static int DVDRead( input_thread_t * p_input,
 
     vlc_mutex_lock( &p_input->stream.stream_lock );
 
-    p_input->stream.p_selected_area->i_tell += i_read_bytes;
-    b_eot = !( p_input->stream.p_selected_area->i_tell < p_dvd->i_size );
-    b_eof = b_eot && ( ( p_dvd->i_title + 1 ) >= p_input->stream.i_area_nb );
-
-    vlc_mutex_unlock( &p_input->stream.stream_lock );
-
-    free( pp_data );
+    p_input->stream.p_selected_area->i_tell =
+        LB2OFF( i_sector + i_read_blocks ) -
+        p_input->stream.p_selected_area->i_start;
+    if( b_eoc )
+    {
+        /* We modify i_part only at end of chapter not to erase
+         * some modification from the interface */
+        p_input->stream.p_selected_area->i_part = p_dvd->i_chapter;
+    }
 
-    free( pp_data );
+    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 )
     {
+        vlc_mutex_unlock( &p_input->stream.stream_lock );
         return 1;
     }
 
@@ -1031,12 +930,13 @@ static int DVDRead( input_thread_t * p_input,
     {
         intf_WarnMsg( 4, "dvd info: new title" );
         p_dvd->i_title++;
-        vlc_mutex_lock( &p_input->stream.stream_lock );
         DVDSetArea( p_input, p_input->stream.pp_areas[p_dvd->i_title] );
         vlc_mutex_unlock( &p_input->stream.stream_lock );
         return 0;
     }
 
+    vlc_mutex_unlock( &p_input->stream.stream_lock );
+
     if( i_read_blocks == i_block_once )
     {
         return 0;
@@ -1063,7 +963,6 @@ static int DVDRewind( input_thread_t * p_input )
 static void DVDSeek( input_thread_t * p_input, off_t i_off )
 {
     thread_dvd_data_t *     p_dvd;
-    off_t                   i_pos;
     int                     i_prg_cell;
     int                     i_cell;
     int                     i_chapter;
@@ -1072,11 +971,8 @@ static void DVDSeek( input_thread_t * p_input, off_t i_off )
     p_dvd = ( thread_dvd_data_t * )p_input->p_plugin_data;
 
     /* we have to take care of offset of beginning of title */
-    i_pos = i_off + p_input->stream.p_selected_area->i_start
-                  - p_dvd->i_title_start;
-
-    /* update navigation data */
-    p_dvd->i_sector = i_pos >> 11;
+    p_dvd->i_sector = OFF2LB(i_off + p_input->stream.p_selected_area->i_start)
+                       - p_dvd->i_title_start;
 
     i_prg_cell = 0;
     i_chapter = 0;
@@ -1140,32 +1036,31 @@ static void DVDSeek( input_thread_t * p_input, off_t i_off )
     {
         i_angle = 0;
     }
-    while( ( title.chapter_map.pi_start_cell[i_chapter] <=
-                ( p_dvd->i_prg_cell - i_angle + 1 ) ) &&
-           ( i_chapter < ( p_dvd->i_chapter_nb - 1 ) ) )
+    if( p_dvd->i_chapter_nb > 1 )
+    {
+        while( ( title.chapter_map.pi_start_cell[i_chapter] <=
+                    ( p_dvd->i_prg_cell - i_angle + 1 ) ) &&
+               ( i_chapter < ( p_dvd->i_chapter_nb - 1 ) ) )
+        {
+            i_chapter++;
+        }
+    }
+    else
     {
-        i_chapter++;
+        i_chapter = 1;
     }
 
     p_dvd->i_chapter = i_chapter;
     p_input->stream.p_selected_area->i_part = p_dvd->i_chapter;
 
-#if !defined( WIN32 )
     p_input->stream.p_selected_area->i_tell =
-                lseek( p_dvd->i_fd, p_dvd->i_title_start +
-                       (off_t)( p_dvd->i_sector ) *DVD_LB_SIZE, SEEK_SET ) -
-                p_input->stream.p_selected_area->i_start;
-#else
-    p_input->stream.p_selected_area->i_tell =
-                SetFilePointer( (HANDLE) p_dvd->i_fd, p_dvd->i_title_start +
-                        (off_t)( p_dvd->i_sector ) *DVD_LB_SIZE, NULL, FILE_BEGIN) -
-                         p_input->stream.p_selected_area->i_start;
+        LB2OFF ( dvdcss_seek( p_dvd->dvdhandle, p_dvd->i_title_start
+                                                 + p_dvd->i_sector ) )
+         - p_input->stream.p_selected_area->i_start;
 
-#endif
-/*
-    intf_WarnMsg( 3, "Program Cell: %d Cell: %d Chapter: %d",
+    intf_WarnMsg( 7, "Program Cell: %d Cell: %d Chapter: %d",
                      p_dvd->i_prg_cell, p_dvd->i_cell, p_dvd->i_chapter );
-*/
+
 
     return;
 }
@@ -1198,7 +1093,7 @@ static int DVDFindCell( thread_dvd_data_t * p_dvd )
     }
 
 /*
-intf_WarnMsg( 3, "FindCell: i_cell %d i_index %d found %d nb %d",
+intf_WarnMsg( 7, "FindCell: i_cell %d i_index %d found %d nb %d",
                     p_dvd->i_cell,
                     p_dvd->i_prg_cell,
                     i_cell,
@@ -1249,7 +1144,7 @@ static int DVDFindSector( thread_dvd_data_t * p_dvd )
 #endif
 
 /*
-    intf_WarnMsg( 3, "cell: %d sector1: 0x%x end1: 0x%x\n"
+    intf_WarnMsg( 7, "cell: %d sector1: 0x%x end1: 0x%x\n"
                    "index: %d sector2: 0x%x end2: 0x%x\n"
                    "category: 0x%x ilvu end: 0x%x vobu start 0x%x", 
         p_dvd->i_cell,
@@ -1288,16 +1183,10 @@ static int DVDChapterSelect( thread_dvd_data_t * p_dvd, int i_chapter )
     }
 
     /* start is : beginning of vts vobs + offset to vob x */
-    p_dvd->i_start = p_dvd->i_title_start +
-                         DVD_LB_SIZE * (off_t)( p_dvd->i_sector );
+    p_dvd->i_start = p_dvd->i_title_start + p_dvd->i_sector;
 
     /* Position the fd pointer on the right address */
-#if !defined( WIN32 )
-    p_dvd->i_start = lseek( p_dvd->i_fd, p_dvd->i_start, SEEK_SET );
-#else
-    p_dvd->i_start = SetFilePointer( (HANDLE) p_dvd->i_fd,
-                        p_dvd->i_start, NULL, FILE_BEGIN );
-#endif
+    p_dvd->i_start = dvdcss_seek( p_dvd->dvdhandle, p_dvd->i_start );
 
     p_dvd->i_chapter = i_chapter;
     return 0;