* ALL: experimental code for stream (dvd) navigation through object variables.
[vlc.git] / modules / access / dvd / access.c
1 /* access.c: DVD access plugin.
2  *****************************************************************************
3  * This plugins should handle all the known specificities of the DVD format,
4  * especially the 2048 bytes logical block size.
5  * It depends on:
6  *  -libdvdcss for access and unscrambling
7  *  -ifo.* for ifo parsing and analyse
8  *  -udf.* to find files
9  *****************************************************************************
10  * Copyright (C) 1998-2001 VideoLAN
11  * $Id: access.c,v 1.11 2003/03/11 23:56:53 gbazin Exp $
12  *
13  * Author: St├ęphane Borel <stef@via.ecp.fr>
14  *
15  * This program is free software; you can redistribute it and/or modify
16  * it under the terms of the GNU General Public License as published by
17  * the Free Software Foundation; either version 2 of the License, or
18  * (at your option) any later version.
19  *
20  * This program is distributed in the hope that it will be useful,
21  * but WITHOUT ANY WARRANTY; without even the implied warranty of
22  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23  * GNU General Public License for more details.
24  *
25  * You should have received a copy of the GNU General Public License
26  * along with this program; if not, write to the Free Software
27  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
28  *****************************************************************************/
29
30 /*****************************************************************************
31  * Preamble
32  *****************************************************************************/
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <string.h>
36
37 #include <vlc/vlc.h>
38 #include <vlc/input.h>
39
40 #include "../../demux/mpeg/system.h"
41
42 #ifdef HAVE_UNISTD_H
43 #   include <unistd.h>
44 #endif
45
46 #include <fcntl.h>
47 #include <sys/types.h>
48 #include <sys/stat.h>
49 #include <string.h>
50 #include <errno.h>
51
52 #ifdef STRNCASECMP_IN_STRINGS_H
53 #   include <strings.h>
54 #endif
55
56 #ifdef GOD_DAMN_DMCA
57 #   include "dvdcss.h"
58 #else
59 #   include <dvdcss/dvdcss.h>
60 #endif
61
62 #include "dvd.h"
63 #include "es.h"
64 #include "seek.h"
65 #include "ifo.h"
66 #include "summary.h"
67 #include "iso_lang.h"
68
69 /*****************************************************************************
70  * Local prototypes
71  *****************************************************************************/
72
73 /* called from outside */
74 static int  DVDSetArea      ( input_thread_t *, input_area_t * );
75 static int  DVDSetProgram   ( input_thread_t *, pgrm_descriptor_t * );
76 static ssize_t DVDRead      ( input_thread_t *, byte_t *, size_t );
77 static void DVDSeek         ( input_thread_t *, off_t );
78
79 static char * DVDParse( input_thread_t * );
80
81 /*
82  * Data access functions
83  */
84
85 #define DVDTell   LB2OFF( p_dvd->i_vts_start + p_dvd->i_vts_lb ) \
86                   - p_input->stream.p_selected_area->i_start
87
88 /*****************************************************************************
89  * DVDOpen: open dvd
90  *****************************************************************************/
91 int E_(DVDOpen) ( vlc_object_t *p_this )
92 {
93     input_thread_t *     p_input = (input_thread_t *)p_this;
94     char *               psz_device;
95     thread_dvd_data_t *  p_dvd;
96     input_area_t *       p_area;
97     int                  i;
98     char *               psz_dvdcss_env;
99
100     p_dvd = malloc( sizeof(thread_dvd_data_t) );
101     if( p_dvd == NULL )
102     {
103         msg_Err( p_input, "out of memory" );
104         return -1;
105     }
106     p_input->p_access_data = (void *)p_dvd;
107
108     p_input->pf_read = DVDRead;
109     p_input->pf_seek = DVDSeek;
110     p_input->pf_set_area = DVDSetArea;
111     p_input->pf_set_program = DVDSetProgram;
112
113     /* Parse command line */
114     if( !( psz_device = DVDParse( p_input ) ) )
115     {
116         free( p_dvd );
117         return -1;
118     }
119
120     /*
121      * set up input
122      */
123     p_input->i_mtu = 0;
124
125     /* override environment variable DVDCSS_METHOD with config option
126      * (FIXME: this creates a small memory leak) */
127     psz_dvdcss_env = config_GetPsz( p_input, "dvdcss-method" );
128     if( psz_dvdcss_env && *psz_dvdcss_env )
129     {
130         char *psz_env;
131
132         psz_env = malloc( strlen("DVDCSS_METHOD=") +
133                           strlen( psz_dvdcss_env ) + 1 );
134
135         if( !psz_env )
136         {
137             free( p_dvd );
138             return -1;
139         }
140
141         sprintf( psz_env, "%s%s", "DVDCSS_METHOD=", psz_dvdcss_env );
142
143         putenv( psz_env );
144     }
145     if( psz_dvdcss_env ) free( psz_dvdcss_env );
146
147     /*
148      *  get plugin ready
149      */
150     p_dvd->dvdhandle = dvdcss_open( psz_device );
151
152     /* free allocated string */
153     free( psz_device );
154
155     if( p_dvd->dvdhandle == NULL )
156     {
157         msg_Err( p_input, "dvdcss cannot open device" );
158         free( p_dvd );
159         return -1;
160     }
161
162     if( dvdcss_seek( p_dvd->dvdhandle, 0, DVDCSS_NOFLAGS ) < 0 )
163     {
164         msg_Err( p_input, "%s", dvdcss_error( p_dvd->dvdhandle ) );
165         dvdcss_close( p_dvd->dvdhandle );
166         free( p_dvd );
167         return -1;
168     }
169
170     /* Ifo allocation & initialisation */
171     if( IfoCreate( p_dvd ) < 0 )
172     {
173         msg_Err( p_input, "allcation error in ifo" );
174         dvdcss_close( p_dvd->dvdhandle );
175         free( p_dvd );
176         return -1;
177     }
178
179     if( IfoInit( p_dvd->p_ifo ) < 0 )
180     {
181         msg_Err( p_input, "fatal failure in ifo" );
182         IfoDestroy( p_dvd->p_ifo );
183         dvdcss_close( p_dvd->dvdhandle );
184         free( p_dvd );
185         return -1;
186     }
187
188     /* Set stream and area data */
189     vlc_mutex_lock( &p_input->stream.stream_lock );
190
191     p_input->stream.i_method = INPUT_METHOD_DVD;
192     p_input->stream.b_pace_control = 1;
193     p_input->stream.b_seekable = 1;
194     p_input->stream.b_connected = 1;
195     p_input->stream.p_selected_area->i_size = 0;
196     p_input->stream.p_selected_area->i_tell = 0;
197
198     /* Initialize ES structures */
199     input_InitStream( p_input, sizeof( stream_ps_data_t ) );
200
201 #define title_inf p_dvd->p_ifo->vmg.title_inf
202     msg_Dbg( p_input, "number of titles: %d", title_inf.i_title_nb );
203
204 #define area p_input->stream.pp_areas
205     /* We start from 1 here since the default area 0
206      * is reserved for video_ts.vob */
207     for( i = 1 ; i <= title_inf.i_title_nb ; i++ )
208     {
209         /* Titles are Program Chains */
210         input_AddArea( p_input, i, title_inf.p_attr[i-1].i_chapter_nb );
211
212         /* Absolute start offset and size
213          * We can only set that with vts ifo, so we do it during the
214          * first call to DVDSetArea */
215         area[i]->i_start = 0;
216         area[i]->i_size = 0;
217
218         /* Default Chapter */
219         area[i]->i_part = 1;
220
221         /* Offset to vts_i_0.ifo */
222         area[i]->i_plugin_data = p_dvd->p_ifo->i_start +
223                        title_inf.p_attr[i-1].i_start_sector;
224     }
225 #undef area
226
227     p_dvd->i_title = p_dvd->i_title <= title_inf.i_title_nb ?
228                      p_dvd->i_title : 1;
229 #undef title_inf
230
231     p_area = p_input->stream.pp_areas[p_dvd->i_title];
232
233     p_area->i_part = p_dvd->i_chapter <= p_area->i_part_nb ?
234                      p_dvd->i_chapter : 1;
235     p_dvd->i_chapter = 1;
236
237     p_dvd->b_new_chapter = 0;
238     p_dvd->i_audio_nb = 0;
239     p_dvd->i_spu_nb = 0;
240
241     /* set title, chapter, audio and subpic */
242     if( DVDSetArea( p_input, p_area ) < 0 )
243     {
244         vlc_mutex_unlock( &p_input->stream.stream_lock );
245         IfoDestroy( p_dvd->p_ifo );
246         dvdcss_close( p_dvd->dvdhandle );
247         free( p_dvd );
248         return -1;
249     }
250
251     vlc_mutex_unlock( &p_input->stream.stream_lock );
252
253     if( !p_input->psz_demux || !*p_input->psz_demux )
254     {
255         p_input->psz_demux = "dvdold";
256     }
257
258     return 0;
259 }
260
261 /*****************************************************************************
262  * DVDClose: close dvd
263  *****************************************************************************/
264 void E_(DVDClose) ( vlc_object_t *p_this )
265 {
266     input_thread_t * p_input = (input_thread_t *)p_this;
267     thread_dvd_data_t *p_dvd = (thread_dvd_data_t*)p_input->p_access_data;
268
269     /* This is a very nasty side-effect in the DVD plug-in : language
270      * selection here influences language selection of other streams. So
271      * unset those variables (may not be what the user wants).
272      * FIXME FIXME FIXME FIXME FIXME FIXME FIXME --Meuuh */
273     config_PutInt( p_input, "audio-channel", -1 );
274     config_PutInt( p_input, "spu-channel", -1 );
275
276     IfoDestroy( p_dvd->p_ifo );
277     dvdcss_close( p_dvd->dvdhandle );
278     free( p_dvd );
279 }
280
281 /*****************************************************************************
282  * DVDSetProgram: used to change angle
283  *****************************************************************************/
284 static int DVDSetProgram( input_thread_t    * p_input,
285                           pgrm_descriptor_t * p_program )
286 {
287     if( p_input->stream.p_selected_program != p_program )
288     {
289         thread_dvd_data_t *  p_dvd;
290         int                  i_angle;
291         vlc_value_t          val;
292
293         p_dvd   = (thread_dvd_data_t*)(p_input->p_access_data);
294         i_angle = p_program->i_number;
295
296         /* DVD is actually mono-program: we only need the current angle
297          * number, so copy the data between programs */
298         memcpy( p_program,
299                 p_input->stream.p_selected_program,
300                 sizeof(pgrm_descriptor_t) );
301         p_program->i_number                = i_angle;
302         p_input->stream.p_selected_program = p_program;
303
304 #define title \
305     p_dvd->p_ifo->vts.title_unit.p_title[p_dvd->i_title_id-1].title
306         if( title.p_cell_play[p_dvd->i_prg_cell].i_category & 0xf000 )
307         {
308             if( ( p_program->i_number - p_dvd->i_angle ) < 0 )
309             {
310                 /* we have to go backwards */
311                 p_dvd->i_map_cell = 0;
312             }
313             p_dvd->i_prg_cell += ( p_program->i_number - p_dvd->i_angle );
314             p_dvd->i_map_cell =  CellPrg2Map( p_dvd );
315             p_dvd->i_map_cell += p_dvd->i_angle_cell;
316             p_dvd->i_vts_lb   =  CellFirstSector( p_dvd );
317             p_dvd->i_last_lb  =  CellLastSector( p_dvd );
318             p_dvd->i_angle    =  p_program->i_number;
319         }
320         else
321         {
322             p_dvd->i_angle    =  p_program->i_number;
323         }
324 #undef title
325         msg_Dbg( p_input, "angle %d selected", p_dvd->i_angle );
326
327         /* Update the navigation variables without triggering a callback */
328         val.i_int = p_program->i_number;
329         var_Change( p_input, "program", VLC_VAR_SETVALUE, &val );
330     }
331
332     return 0;
333 }
334
335 /*****************************************************************************
336  * DVDSetArea: initialize input data for title x, chapter y.
337  * It should be called for each user navigation request.
338  *****************************************************************************
339  * Take care that i_title starts from 0 (vmg) and i_chapter start from 1.
340  * Note that you have to take the lock before entering here.
341  *****************************************************************************/
342 #define vmg p_dvd->p_ifo->vmg
343 #define vts p_dvd->p_ifo->vts
344
345 static void DVDFlushStream( input_thread_t * p_input )
346 {
347     if( p_input->stream.pp_programs != NULL )
348     {
349         /* We don't use input_EndStream here since
350          * we keep area structures */
351         while( p_input->stream.i_es_number )
352         {
353             input_DelES( p_input, p_input->stream.pp_es[0] );
354         }
355
356         while( p_input->stream.i_pgrm_number )
357         {
358             input_DelProgram( p_input, p_input->stream.pp_programs[0] );
359         }
360
361         if( p_input->stream.pp_selected_es )
362         {
363             free( p_input->stream.pp_selected_es );
364             p_input->stream.pp_selected_es = NULL;
365         }
366         p_input->stream.i_selected_es_number = 0;
367     }
368
369     return;
370 }
371
372 static int DVDReadAngle( input_thread_t * p_input )
373 {
374     thread_dvd_data_t * p_dvd;
375     int                 i_angle_nb;
376     int                 i;
377
378     p_dvd      = (thread_dvd_data_t*)(p_input->p_access_data);
379     i_angle_nb = vmg.title_inf.p_attr[p_dvd->i_title-1].i_angle_nb;
380
381     input_AddProgram( p_input, 1, sizeof( stream_ps_data_t ) );
382     p_input->stream.p_selected_program = p_input->stream.pp_programs[0];
383
384     for( i = 1 ; i < i_angle_nb ; i++ )
385     {
386         input_AddProgram( p_input, i+1, 0 );
387     }
388
389     return i_angle_nb;
390 }
391
392 static int DVDSetArea( input_thread_t * p_input, input_area_t * p_area )
393 {
394     thread_dvd_data_t *  p_dvd;
395     vlc_value_t          val;
396
397     p_dvd = (thread_dvd_data_t*)(p_input->p_access_data);
398
399     /* we can't use the interface slider until initilization is complete */
400     p_input->stream.b_seekable = 0;
401
402     if( p_area != p_input->stream.p_selected_area )
403     {
404         int     i_vts_title;
405         u32     i_first;
406         u32     i_last;
407         unsigned int i;
408
409         /* Reset the Chapter position of the old title */
410         p_input->stream.p_selected_area->i_part = 1;
411         p_input->stream.p_selected_area         = p_area;
412
413         /*
414          *  We have to load all title information
415          */
416
417         /* title number as it appears in the interface list */
418         p_dvd->i_title      = p_area->i_id;
419         p_dvd->i_chapter_nb = p_area->i_part_nb;
420
421         if( IfoTitleSet( p_dvd->p_ifo, p_dvd->i_title ) < 0 )
422         {
423             msg_Err( p_input, "fatal error in vts ifo" );
424             free( p_dvd );
425             return -1;
426         }
427
428         /* title position inside the selected vts */
429         i_vts_title       = vmg.title_inf.p_attr[p_dvd->i_title-1].i_title_num;
430         p_dvd->i_title_id =
431             vts.title_inf.p_title_start[i_vts_title-1].i_title_id;
432
433         msg_Dbg( p_input, "title %d vts_title %d pgc %d",
434                           p_dvd->i_title, i_vts_title, p_dvd->i_title_id );
435
436         /* title set offset XXX: convert to block values */
437         p_dvd->i_vts_start =
438             vts.i_pos + vts.manager_inf.i_title_vob_start_sector;
439
440         /* last cell */
441         p_dvd->i_prg_cell = -1 +
442             vts.title_unit.p_title[p_dvd->i_title_id-1].title.i_cell_nb;
443         p_dvd->i_map_cell = 0;
444         p_dvd->i_map_cell = CellPrg2Map( p_dvd );
445         i_last            = CellLastSector( p_dvd );
446
447         /* first cell */
448         p_dvd->i_prg_cell   = 0;
449         p_dvd->i_map_cell   = 0;
450         p_dvd->i_angle_cell = 0;
451         p_dvd->i_map_cell   = CellPrg2Map    ( p_dvd );
452         p_dvd->i_vts_lb     = CellFirstSector( p_dvd );
453         p_dvd->i_last_lb    = CellLastSector ( p_dvd );
454
455         /* Force libdvdcss to check its title key.
456          * It is only useful for title cracking method. Methods using the
457          * decrypted disc key are fast enough to check the key at each seek */
458         i_first = dvdcss_seek( p_dvd->dvdhandle,
459                                p_dvd->i_vts_start + p_dvd->i_vts_lb,
460                                DVDCSS_SEEK_KEY );
461         if( i_first < 0 )
462         {
463             msg_Err( p_input, "%s", dvdcss_error( p_dvd->dvdhandle ) );
464             return -1;
465         }
466
467         /* Area definition */
468         p_input->stream.p_selected_area->i_start = LB2OFF( i_first );
469         p_input->stream.p_selected_area->i_size  =
470                                         LB2OFF( i_last + 1 - p_dvd->i_vts_lb );
471
472         /* Destroy obsolete ES by reinitializing programs */
473         DVDFlushStream( p_input );
474
475         /* Angle management: angles are handled through programs */
476         p_dvd->i_angle_nb = DVDReadAngle( p_input );
477         if( ( p_dvd->i_angle <= 0 ) || p_dvd->i_angle > p_dvd->i_angle_nb )
478         {
479             p_dvd->i_angle = 1;
480         }
481
482         DVDSetProgram( p_input,
483                        p_input->stream.pp_programs[p_dvd->i_angle-1] );
484
485         msg_Dbg( p_input, "title first %i, last %i, size %i",
486                           i_first, i_last, i_last + 1 - p_dvd->i_vts_lb );
487         IfoPrintTitle( p_dvd );
488
489         /* No PSM to read in DVD mode, we already have all information */
490         p_input->stream.p_selected_program->b_is_ok = 1;
491
492         /* Find all ES in title with ifo data */
493         DVDReadVideo( p_input );
494         DVDReadAudio( p_input );
495         DVDReadSPU  ( p_input );
496
497         if( p_input->p_demux )
498         {
499             DVDLaunchDecoders( p_input );
500         }
501
502         /* Update the navigation variables without triggering a callback */
503         val.i_int = p_area->i_id;
504         var_Change( p_input, "title", VLC_VAR_SETVALUE, &val );
505         var_Change( p_input, "chapter", VLC_VAR_CLEARCHOICES, NULL );
506         for( i = 1; i <= p_area->i_part_nb; i++ )
507         {
508             val.i_int = i;
509             var_Change( p_input, "chapter", VLC_VAR_ADDCHOICE, &val );
510         }
511
512     } /* i_title >= 0 */
513     else
514     {
515         p_area = p_input->stream.p_selected_area;
516     }
517
518     /* Chapter selection */
519     p_dvd->i_chapter = DVDSetChapter( p_dvd, p_area->i_part );
520
521     p_input->stream.p_selected_area->i_tell = DVDTell;
522
523     /* warn interface that something has changed */
524     p_input->stream.b_seekable = 1;
525     p_input->stream.b_changed  = 1;
526
527     /* Update the navigation variables without triggering a callback */
528     val.i_int = p_area->i_part;
529     var_Change( p_input, "chapter", VLC_VAR_SETVALUE, &val );
530
531     return 0;
532 }
533 #undef vts
534 #undef vmg
535
536 #define title \
537     p_dvd->p_ifo->vts.title_unit.p_title[p_dvd->i_title_id-1].title
538
539 /*****************************************************************************
540  * DVDRead: reads data packets.
541  *****************************************************************************
542  * Returns -1 in case of error, 0 in case of EOF, otherwise the number of
543  * bytes.
544  *****************************************************************************/
545 static ssize_t DVDRead( input_thread_t * p_input,
546                         byte_t * p_buffer, size_t i_count )
547 {
548     thread_dvd_data_t *     p_dvd;
549     int                     i_read;
550     int                     i_blocks;
551     int                     i_block_once = 0;
552
553     p_dvd = (thread_dvd_data_t *)(p_input->p_access_data);
554
555     i_read = 0;
556     i_blocks = OFF2LB(i_count);
557
558     while( i_blocks )
559     {
560         i_block_once = LbMaxOnce( p_dvd );
561         if( i_block_once > i_blocks )
562         {
563             i_block_once = i_blocks;
564         }
565         else if( i_block_once <= 0 )
566         {
567             /* EOT */
568             break;
569         }
570
571         if( i_block_once != dvdcss_read( p_dvd->dvdhandle, p_buffer,
572                                          i_block_once, DVDCSS_READ_DECRYPT ) )
573         {
574             return -1;
575         }
576
577         i_blocks -= i_block_once;
578         i_read += i_block_once;
579         p_buffer += LB2OFF( i_block_once );
580
581         /* Update global position */
582         p_dvd->i_vts_lb += i_block_once;
583     }
584
585     vlc_mutex_lock( &p_input->stream.stream_lock );
586
587     if( p_dvd->b_new_chapter )
588     {
589         p_input->stream.p_selected_area->i_part = p_dvd->i_chapter;
590         p_dvd->b_new_chapter                    = 0;
591     }
592
593     if( ( p_input->stream.p_selected_area->i_tell + LB2OFF( i_read )
594             >= p_input->stream.p_selected_area->i_size )
595        || ( i_block_once  <= 0 ) )
596     {
597         if( ( p_dvd->i_title + 1 ) >= p_input->stream.i_area_nb )
598         {
599             /* EOF */
600             vlc_mutex_unlock( &p_input->stream.stream_lock );
601             return 0;
602         }
603
604         /* EOT */
605         msg_Dbg( p_input, "new title" );
606         p_dvd->i_title++;
607         DVDSetArea( p_input, p_input->stream.pp_areas[p_dvd->i_title] );
608     }
609
610     vlc_mutex_unlock( &p_input->stream.stream_lock );
611
612     return LB2OFF( i_read );
613 }
614
615 /*****************************************************************************
616  * DVDSeek : Goes to a given position on the stream.
617  *****************************************************************************
618  * This one is used by the input and translate chronological position from
619  * input to logical position on the device.
620  * The lock should be taken before calling this function.
621  *****************************************************************************/
622 static void DVDSeek( input_thread_t * p_input, off_t i_off )
623 {
624     thread_dvd_data_t *     p_dvd;
625
626     p_dvd = ( thread_dvd_data_t * )(p_input->p_access_data);
627
628     vlc_mutex_lock( &p_input->stream.stream_lock );
629     p_dvd->i_vts_lb = OFF2LB(i_off + p_input->stream.p_selected_area->i_start)
630                        - p_dvd->i_vts_start;
631     vlc_mutex_unlock( &p_input->stream.stream_lock );
632
633     p_dvd->i_prg_cell = Lb2CellPrg( p_dvd );
634     p_dvd->i_map_cell = Lb2CellMap( p_dvd );
635
636     if( CellIsInterleaved( p_dvd ) )
637     {
638         /* if we're inside a multi-angle zone, we have to choose i_sector
639          * in the current angle ; we can't do it all the time since cells
640          * can be very wide out of such zones */
641         p_dvd->i_vts_lb = CellFirstSector( p_dvd );
642     }
643
644     p_dvd->i_last_lb  = CellLastSector( p_dvd );
645     p_dvd->i_chapter  = CellPrg2Chapter( p_dvd );
646
647     if( dvdcss_seek( p_dvd->dvdhandle, p_dvd->i_vts_start + p_dvd->i_vts_lb,
648                      DVDCSS_SEEK_MPEG ) < 0 )
649     {
650         msg_Err( p_input, "%s", dvdcss_error( p_dvd->dvdhandle ) );
651         p_input->b_error = 1;
652         return;
653     }
654
655     vlc_mutex_lock( &p_input->stream.stream_lock );
656     p_input->stream.p_selected_area->i_part = p_dvd->i_chapter;
657     p_input->stream.p_selected_area->i_tell = DVDTell;
658     vlc_mutex_unlock( &p_input->stream.stream_lock );
659
660     msg_Dbg( p_input, "program cell: %d cell: %d chapter: %d tell "I64Fd,
661              p_dvd->i_prg_cell, p_dvd->i_map_cell, p_dvd->i_chapter, DVDTell );
662
663     return;
664 }
665
666 /*****************************************************************************
667  * DVDParse: parse command line
668  *****************************************************************************/
669 static char * DVDParse( input_thread_t * p_input )
670 {
671     thread_dvd_data_t *  p_dvd;
672     struct stat          stat_info;
673     char *               psz_parser;
674     char *               psz_device;
675     char *               psz_raw;
676     char *               psz_next;
677     vlc_bool_t           b_options = 0;
678     int                  i_title = 1;
679     int                  i_chapter = 1;
680     int                  i_angle = 1;
681     int                  i;
682
683     p_dvd = (thread_dvd_data_t*)(p_input->p_access_data);
684
685 #ifdef WIN32
686     /* On Win32 we want the DVD access plugin to be explicitly requested,
687      * we end up with lots of problems otherwise */
688     if( !p_input->psz_access || !*p_input->psz_access ) return NULL;
689 #endif
690
691     psz_parser = psz_device = strdup( p_input->psz_name );
692     if( !psz_parser )
693     {
694         return NULL;
695     }
696
697     /* Parse input string :
698      * [device][@rawdevice][@[title][,[chapter][,angle]]] */
699     while( *psz_parser && *psz_parser != '@' )
700     {
701         psz_parser++;
702     }
703
704     if( *psz_parser == '@' )
705     {
706         /* Maybe found raw device or option list */
707         *psz_parser = '\0';
708         psz_raw = ++psz_parser;
709     }
710     else
711     {
712         psz_raw = "";
713     }
714
715     if( *psz_parser && !strtol( psz_parser, NULL, 10 ) )
716     {
717         /* what we've found is either a raw device or a partial option
718          * list e.g. @,29 or both a device and a list ; search end of string */
719         while( *psz_parser && *psz_parser != '@' )
720         {
721             psz_parser++;
722         }
723
724         if( *psz_parser == '@' )
725         {
726             /* found end of raw device, and beginning of options */
727             *psz_parser = '\0';
728             ++psz_parser;
729             b_options = 1;
730         }
731         else
732         {
733             psz_parser = psz_raw + 1;
734             for( i=0 ; i<3 ; i++ )
735             {
736                 if( !*psz_parser )
737                 {
738                     /* we have only a raw device */
739                     break;
740                 }
741                 if( strtol( psz_parser, NULL, 10 ) )
742                 {
743                     /* we have only a partial list of options, no device */
744                     psz_parser = psz_raw;
745                     psz_raw = "";
746                     b_options = 1;
747                     break;
748                 }
749                 psz_parser++;
750             }
751         }
752     }
753     else
754     {
755         /* found beginning of options ; no raw device specified */
756         psz_raw = "";
757         b_options = 1;
758     }
759
760     if( b_options )
761     {
762         /* Found options */
763         i_title = (int)strtol( psz_parser, &psz_next, 10 );
764         if( *psz_next )
765         {
766             psz_parser = psz_next + 1;
767             i_chapter = (int)strtol( psz_parser, &psz_next, 10 );
768             if( *psz_next )
769             {
770                 i_angle = (int)strtol( psz_next + 1, NULL, 10 );
771             }
772         }
773
774         p_dvd->i_title = i_title ? i_title : 1;
775         p_dvd->i_chapter = i_chapter ? i_chapter : 1;
776         p_dvd->i_angle = i_angle ? i_angle : 1;
777     }
778
779     if( *psz_raw )
780     {
781         if( *psz_raw )
782         {
783             /* check the raw device */
784             if( stat( psz_raw, &stat_info ) == -1 )
785             {
786                 msg_Warn( p_input, "cannot stat() raw device `%s' (%s)",
787                                    psz_raw, strerror(errno));
788                 /* put back '@' */
789                 *(psz_raw - 1) = '@';
790                 psz_raw = "";
791             }
792             else
793             {
794                 char * psz_env;
795
796 #ifndef WIN32
797                 if( !S_ISCHR(stat_info.st_mode) )
798                 {
799                     msg_Warn( p_input, "raw device %s is"
800                               " not a valid char device", psz_raw );
801                     /* put back '@' */
802                     *(psz_raw - 1) = '@';
803                     psz_raw = "";
804                 }
805                 else
806 #endif
807                 {
808                     psz_env = malloc( strlen("DVDCSS_RAW_DEVICE=")
809                                     + strlen( psz_raw ) + 1 );
810                     sprintf( psz_env, "DVDCSS_RAW_DEVICE=%s", psz_raw );
811                     putenv( psz_env );
812                 }
813             }
814         }
815         else
816         {
817             psz_raw = "";
818         }
819     }
820
821     if( !*psz_device )
822     {
823         free( psz_device );
824
825         if( !p_input->psz_access )
826         {
827             /* no device and no access specified: we probably don't want DVD */
828             return NULL;
829         }
830         psz_device = config_GetPsz( p_input, "dvd" );
831     }
832
833 #ifndef WIN32
834     /* check block device */
835     if( stat( psz_device, &stat_info ) == -1 )
836     {
837         msg_Warn( p_input, "cannot stat() device `%s' (%s)",
838                   psz_device, strerror(errno));
839         free( psz_device );
840         return NULL;
841     }
842
843     if( !S_ISBLK(stat_info.st_mode) && !S_ISCHR(stat_info.st_mode) )
844     {
845         msg_Warn( p_input,
846                   "dvd module discarded (not a valid block device)" );
847         free( psz_device );
848         return NULL;
849     }
850 #endif
851
852     msg_Dbg( p_input, "dvd=%s raw=%s title=%d chapter=%d angle=%d",
853                       psz_device, psz_raw, p_dvd->i_title,
854                       p_dvd->i_chapter, p_dvd->i_angle );
855
856     return psz_device;
857 }