]> git.sesse.net Git - vlc/blob - modules/access/dvd/access.c
* ./configure.ac.in: removed -W in favour of -Wtraditional.
[vlc] / 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.6 2002/12/06 16:34:04 sam 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, "dvd-css-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.p_selected_area->i_size = 0;
195     p_input->stream.p_selected_area->i_tell = 0;
196
197     /* Initialize ES structures */
198     input_InitStream( p_input, sizeof( stream_ps_data_t ) );
199
200 #define title_inf p_dvd->p_ifo->vmg.title_inf
201     msg_Dbg( p_input, "number of titles: %d", title_inf.i_title_nb );
202
203 #define area p_input->stream.pp_areas
204     /* We start from 1 here since the default area 0
205      * is reserved for video_ts.vob */
206     for( i = 1 ; i <= title_inf.i_title_nb ; i++ )
207     {
208         input_AddArea( p_input );
209
210         /* Titles are Program Chains */
211         area[i]->i_id = i;
212
213         /* Absolute start offset and size
214          * We can only set that with vts ifo, so we do it during the
215          * first call to DVDSetArea */
216         area[i]->i_start = 0;
217         area[i]->i_size = 0;
218
219         /* Number of chapters */
220         area[i]->i_part_nb = title_inf.p_attr[i-1].i_chapter_nb;
221         area[i]->i_part = 1;
222
223         /* Offset to vts_i_0.ifo */
224         area[i]->i_plugin_data = p_dvd->p_ifo->i_start +
225                        title_inf.p_attr[i-1].i_start_sector;
226     }
227 #undef area
228
229     p_dvd->i_title = p_dvd->i_title <= title_inf.i_title_nb ?
230                      p_dvd->i_title : 1;
231 #undef title_inf
232
233     p_area = p_input->stream.pp_areas[p_dvd->i_title];
234
235     p_area->i_part = p_dvd->i_chapter <= p_area->i_part_nb ?
236                      p_dvd->i_chapter : 1;
237     p_dvd->i_chapter = 1;
238
239     p_dvd->b_new_chapter = 0;
240     p_dvd->i_audio_nb = 0;
241     p_dvd->i_spu_nb = 0;
242
243     /* set title, chapter, audio and subpic */
244     if( DVDSetArea( p_input, p_area ) < 0 )
245     {
246         vlc_mutex_unlock( &p_input->stream.stream_lock );
247         IfoDestroy( p_dvd->p_ifo );
248         dvdcss_close( p_dvd->dvdhandle );
249         free( p_dvd );
250         return -1;
251     }
252
253     vlc_mutex_unlock( &p_input->stream.stream_lock );
254
255     if( !p_input->psz_demux || !*p_input->psz_demux )
256     {
257         p_input->psz_demux = "dvdold";
258     }
259
260     return 0;
261 }
262
263 /*****************************************************************************
264  * DVDClose: close dvd
265  *****************************************************************************/
266 void E_(DVDClose) ( vlc_object_t *p_this )
267 {
268     input_thread_t * p_input = (input_thread_t *)p_this;
269     thread_dvd_data_t *p_dvd = (thread_dvd_data_t*)p_input->p_access_data;
270
271     IfoDestroy( p_dvd->p_ifo );
272     dvdcss_close( p_dvd->dvdhandle );
273     free( p_dvd );
274 }
275
276 /*****************************************************************************
277  * DVDSetProgram: used to change angle
278  *****************************************************************************/
279 static int DVDSetProgram( input_thread_t    * p_input,
280                           pgrm_descriptor_t * p_program )
281 {
282     if( p_input->stream.p_selected_program != p_program )
283     {
284         thread_dvd_data_t *  p_dvd;
285         int                  i_angle;
286
287         p_dvd   = (thread_dvd_data_t*)(p_input->p_access_data);
288         i_angle = p_program->i_number;
289
290         /* DVD is actually mono-program: we only need the current angle
291          * number, so copy the data between programs */
292         memcpy( p_program,
293                 p_input->stream.p_selected_program,
294                 sizeof(pgrm_descriptor_t) );
295         p_program->i_number                = i_angle;
296         p_input->stream.p_selected_program = p_program;
297
298 #define title \
299     p_dvd->p_ifo->vts.title_unit.p_title[p_dvd->i_title_id-1].title
300         if( title.p_cell_play[p_dvd->i_prg_cell].i_category & 0xf000 )
301         {
302             if( ( p_program->i_number - p_dvd->i_angle ) < 0 )
303             {
304                 /* we have to go backwards */
305                 p_dvd->i_map_cell = 0;
306             }
307             p_dvd->i_prg_cell += ( p_program->i_number - p_dvd->i_angle );
308             p_dvd->i_map_cell =  CellPrg2Map( p_dvd );
309             p_dvd->i_map_cell += p_dvd->i_angle_cell;
310             p_dvd->i_vts_lb   =  CellFirstSector( p_dvd );
311             p_dvd->i_last_lb  =  CellLastSector( p_dvd );
312             p_dvd->i_angle    =  p_program->i_number;
313         }
314         else
315         {
316             p_dvd->i_angle    =  p_program->i_number;
317         }
318 #undef title
319         msg_Dbg( p_input, "angle %d selected", p_dvd->i_angle );
320     }
321
322     return 0;
323 }
324
325 /*****************************************************************************
326  * DVDSetArea: initialize input data for title x, chapter y.
327  * It should be called for each user navigation request.
328  *****************************************************************************
329  * Take care that i_title starts from 0 (vmg) and i_chapter start from 1.
330  * Note that you have to take the lock before entering here.
331  *****************************************************************************/
332 #define vmg p_dvd->p_ifo->vmg
333 #define vts p_dvd->p_ifo->vts
334
335 static void DVDFlushStream( input_thread_t * p_input )
336 {
337     if( p_input->stream.pp_programs != NULL )
338     {
339         /* We don't use input_EndStream here since
340          * we keep area structures */
341         while( p_input->stream.i_es_number )
342         {
343             input_DelES( p_input, p_input->stream.pp_es[0] );
344         }
345
346         while( p_input->stream.i_pgrm_number )
347         {
348             input_DelProgram( p_input, p_input->stream.pp_programs[0] );
349         }
350
351         if( p_input->stream.pp_selected_es )
352         {
353             free( p_input->stream.pp_selected_es );
354             p_input->stream.pp_selected_es = NULL;
355         }
356         p_input->stream.i_selected_es_number = 0;
357     }
358
359     return;
360 }
361
362 static int DVDReadAngle( input_thread_t * p_input )
363 {
364     thread_dvd_data_t * p_dvd;
365     int                 i_angle_nb;
366     int                 i;
367
368     p_dvd      = (thread_dvd_data_t*)(p_input->p_access_data);
369     i_angle_nb = vmg.title_inf.p_attr[p_dvd->i_title-1].i_angle_nb;
370
371     input_AddProgram( p_input, 1, sizeof( stream_ps_data_t ) );
372     p_input->stream.p_selected_program = p_input->stream.pp_programs[0];
373
374     for( i = 1 ; i < i_angle_nb ; i++ )
375     {
376         input_AddProgram( p_input, i+1, 0 );
377     }
378
379     return i_angle_nb;
380 }
381
382 static int DVDSetArea( input_thread_t * p_input, input_area_t * p_area )
383 {
384     thread_dvd_data_t *  p_dvd;
385
386     p_dvd = (thread_dvd_data_t*)(p_input->p_access_data);
387
388     /* we can't use the interface slider until initilization is complete */
389     p_input->stream.b_seekable = 0;
390
391     if( p_area != p_input->stream.p_selected_area )
392     {
393         int     i_vts_title;
394         u32     i_first;
395         u32     i_last;
396
397         /* Reset the Chapter position of the old title */
398         p_input->stream.p_selected_area->i_part = 1;
399         p_input->stream.p_selected_area         = p_area;
400
401         /*
402          *  We have to load all title information
403          */
404
405         /* title number as it appears in the interface list */
406         p_dvd->i_title      = p_area->i_id;
407         p_dvd->i_chapter_nb = p_area->i_part_nb;
408
409         if( IfoTitleSet( p_dvd->p_ifo, p_dvd->i_title ) < 0 )
410         {
411             msg_Err( p_input, "fatal error in vts ifo" );
412             free( p_dvd );
413             return -1;
414         }
415
416         /* title position inside the selected vts */
417         i_vts_title       = vmg.title_inf.p_attr[p_dvd->i_title-1].i_title_num;
418         p_dvd->i_title_id =
419             vts.title_inf.p_title_start[i_vts_title-1].i_title_id;
420
421         msg_Dbg( p_input, "title %d vts_title %d pgc %d",
422                           p_dvd->i_title, i_vts_title, p_dvd->i_title_id );
423
424         /* title set offset XXX: convert to block values */
425         p_dvd->i_vts_start =
426             vts.i_pos + vts.manager_inf.i_title_vob_start_sector;
427
428         /* last cell */
429         p_dvd->i_prg_cell = -1 +
430             vts.title_unit.p_title[p_dvd->i_title_id-1].title.i_cell_nb;
431         p_dvd->i_map_cell = 0;
432         p_dvd->i_map_cell = CellPrg2Map( p_dvd );
433         i_last            = CellLastSector( p_dvd );
434
435         /* first cell */
436         p_dvd->i_prg_cell   = 0;
437         p_dvd->i_map_cell   = 0;
438         p_dvd->i_angle_cell = 0;
439         p_dvd->i_map_cell   = CellPrg2Map    ( p_dvd );
440         p_dvd->i_vts_lb     = CellFirstSector( p_dvd );
441         p_dvd->i_last_lb    = CellLastSector ( p_dvd );
442
443         /* Force libdvdcss to check its title key.
444          * It is only useful for title cracking method. Methods using the
445          * decrypted disc key are fast enough to check the key at each seek */
446         i_first = dvdcss_seek( p_dvd->dvdhandle,
447                                p_dvd->i_vts_start + p_dvd->i_vts_lb,
448                                DVDCSS_SEEK_KEY );
449         if( i_first < 0 )
450         {
451             msg_Err( p_input, "%s", dvdcss_error( p_dvd->dvdhandle ) );
452             return -1;
453         }
454
455         /* Area definition */
456         p_input->stream.p_selected_area->i_start = LB2OFF( i_first );
457         p_input->stream.p_selected_area->i_size  =
458                                         LB2OFF( i_last + 1 - p_dvd->i_vts_lb );
459
460         /* Destroy obsolete ES by reinitializing programs */
461         DVDFlushStream( p_input );
462
463         /* Angle management: angles are handled through programs */
464         p_dvd->i_angle_nb = DVDReadAngle( p_input );
465         if( ( p_dvd->i_angle <= 0 ) || p_dvd->i_angle > p_dvd->i_angle_nb )
466         {
467             p_dvd->i_angle = 1;
468         }
469
470         DVDSetProgram( p_input,
471                        p_input->stream.pp_programs[p_dvd->i_angle-1] );
472
473         msg_Dbg( p_input, "title first %i, last %i, size %i",
474                           i_first, i_last, i_last + 1 - p_dvd->i_vts_lb );
475         IfoPrintTitle( p_dvd );
476
477         /* No PSM to read in DVD mode, we already have all information */
478         p_input->stream.p_selected_program->b_is_ok = 1;
479
480         /* Find all ES in title with ifo data */
481         DVDReadVideo( p_input );
482         DVDReadAudio( p_input );
483         DVDReadSPU  ( p_input );
484
485         if( p_input->p_demux )
486         {
487             DVDLaunchDecoders( p_input );
488         }
489
490     } /* i_title >= 0 */
491     else
492     {
493         p_area = p_input->stream.p_selected_area;
494     }
495
496     /* Chapter selection */
497     p_dvd->i_chapter = DVDSetChapter( p_dvd, p_area->i_part );
498
499     p_input->stream.p_selected_area->i_tell = DVDTell;
500
501     /* warn interface that something has changed */
502     p_input->stream.b_seekable = 1;
503     p_input->stream.b_changed  = 1;
504
505     return 0;
506 }
507 #undef vts
508 #undef vmg
509
510 #define title \
511     p_dvd->p_ifo->vts.title_unit.p_title[p_dvd->i_title_id-1].title
512
513 /*****************************************************************************
514  * DVDRead: reads data packets.
515  *****************************************************************************
516  * Returns -1 in case of error, 0 in case of EOF, otherwise the number of
517  * bytes.
518  *****************************************************************************/
519 static ssize_t DVDRead( input_thread_t * p_input,
520                         byte_t * p_buffer, size_t i_count )
521 {
522     thread_dvd_data_t *     p_dvd;
523     int                     i_read;
524     int                     i_blocks;
525     int                     i_block_once = 0;
526
527     p_dvd = (thread_dvd_data_t *)(p_input->p_access_data);
528
529     i_read = 0;
530     i_blocks = OFF2LB(i_count);
531
532     while( i_blocks )
533     {
534         i_block_once = LbMaxOnce( p_dvd );
535         if( i_block_once > i_blocks )
536         {
537             i_block_once = i_blocks;
538         }
539         else if( i_block_once <= 0 )
540         {
541             /* EOT */
542             break;
543         }
544
545         if( i_block_once != dvdcss_read( p_dvd->dvdhandle, p_buffer,
546                                          i_block_once, DVDCSS_READ_DECRYPT ) )
547         {
548             return -1;
549         }
550
551         i_blocks -= i_block_once;
552         i_read += i_block_once;
553         p_buffer += LB2OFF( i_block_once );
554
555         /* Update global position */
556         p_dvd->i_vts_lb += i_block_once;
557     }
558
559     vlc_mutex_lock( &p_input->stream.stream_lock );
560
561     if( p_dvd->b_new_chapter )
562     {
563         p_input->stream.p_selected_area->i_part = p_dvd->i_chapter;
564         p_dvd->b_new_chapter                    = 0;
565     }
566
567     if( ( p_input->stream.p_selected_area->i_tell + LB2OFF( i_read )
568             >= p_input->stream.p_selected_area->i_size )
569        || ( i_block_once  <= 0 ) )
570     {
571         if( ( p_dvd->i_title + 1 ) >= p_input->stream.i_area_nb )
572         {
573             /* EOF */
574             vlc_mutex_unlock( &p_input->stream.stream_lock );
575             return 0;
576         }
577
578         /* EOT */
579         msg_Dbg( p_input, "new title" );
580         p_dvd->i_title++;
581         DVDSetArea( p_input, p_input->stream.pp_areas[p_dvd->i_title] );
582     }
583
584     vlc_mutex_unlock( &p_input->stream.stream_lock );
585
586     return LB2OFF( i_read );
587 }
588
589 /*****************************************************************************
590  * DVDSeek : Goes to a given position on the stream.
591  *****************************************************************************
592  * This one is used by the input and translate chronological position from
593  * input to logical position on the device.
594  * The lock should be taken before calling this function.
595  *****************************************************************************/
596 static void DVDSeek( input_thread_t * p_input, off_t i_off )
597 {
598     thread_dvd_data_t *     p_dvd;
599
600     p_dvd = ( thread_dvd_data_t * )(p_input->p_access_data);
601
602     vlc_mutex_lock( &p_input->stream.stream_lock );
603     p_dvd->i_vts_lb = OFF2LB(i_off + p_input->stream.p_selected_area->i_start)
604                        - p_dvd->i_vts_start;
605     vlc_mutex_unlock( &p_input->stream.stream_lock );
606
607     p_dvd->i_prg_cell = Lb2CellPrg( p_dvd );
608     p_dvd->i_map_cell = Lb2CellMap( p_dvd );
609
610     if( CellIsInterleaved( p_dvd ) )
611     {
612         /* if we're inside a multi-angle zone, we have to choose i_sector
613          * in the current angle ; we can't do it all the time since cells
614          * can be very wide out of such zones */
615         p_dvd->i_vts_lb = CellFirstSector( p_dvd );
616     }
617
618     p_dvd->i_last_lb  = CellLastSector( p_dvd );
619     p_dvd->i_chapter  = CellPrg2Chapter( p_dvd );
620
621     if( dvdcss_seek( p_dvd->dvdhandle, p_dvd->i_vts_start + p_dvd->i_vts_lb,
622                      DVDCSS_SEEK_MPEG ) < 0 )
623     {
624         msg_Err( p_input, "%s", dvdcss_error( p_dvd->dvdhandle ) );
625         p_input->b_error = 1;
626         return;
627     }
628
629     vlc_mutex_lock( &p_input->stream.stream_lock );
630     p_input->stream.p_selected_area->i_part = p_dvd->i_chapter;
631     p_input->stream.p_selected_area->i_tell = DVDTell;
632     vlc_mutex_unlock( &p_input->stream.stream_lock );
633
634     msg_Dbg( p_input, "program cell: %d cell: %d chapter: %d tell "I64Fd,
635              p_dvd->i_prg_cell, p_dvd->i_map_cell, p_dvd->i_chapter, DVDTell );
636
637     return;
638 }
639
640 /*****************************************************************************
641  * DVDParse: parse command line
642  *****************************************************************************/
643 static char * DVDParse( input_thread_t * p_input )
644 {
645     thread_dvd_data_t *  p_dvd;
646     struct stat          stat_info;
647     char *               psz_parser;
648     char *               psz_device;
649     char *               psz_raw;
650     char *               psz_next;
651     vlc_bool_t           b_options = 0;
652     int                  i_title = 1;
653     int                  i_chapter = 1;
654     int                  i_angle = 1;
655     int                  i;
656
657     p_dvd = (thread_dvd_data_t*)(p_input->p_access_data);
658
659 #ifdef WIN32
660     /* On Win32 we want the DVD access plugin to be explicitly requested,
661      * we end up with lots of problems otherwise */
662     if( !p_input->psz_access || !*p_input->psz_access ) return NULL;
663 #endif
664
665     psz_parser = psz_device = strdup( p_input->psz_name );
666     if( !psz_parser )
667     {
668         return NULL;
669     }
670
671     /* Parse input string :
672      * [device][@rawdevice][@[title][,[chapter][,angle]]] */
673     while( *psz_parser && *psz_parser != '@' )
674     {
675         psz_parser++;
676     }
677
678     if( *psz_parser == '@' )
679     {
680         /* Maybe found raw device or option list */
681         *psz_parser = '\0';
682         psz_raw = ++psz_parser;
683     }
684     else
685     {
686         psz_raw = "";
687     }
688
689     if( *psz_parser && !strtol( psz_parser, NULL, 10 ) )
690     {
691         /* what we've found is either a raw device or a partial option
692          * list e.g. @,29 or both a device and a list ; search end of string */
693         while( *psz_parser && *psz_parser != '@' )
694         {
695             psz_parser++;
696         }
697
698         if( *psz_parser == '@' )
699         {
700             /* found end of raw device, and beginning of options */
701             *psz_parser = '\0';
702             ++psz_parser;
703             b_options = 1;
704         }
705         else
706         {
707             psz_parser = psz_raw + 1;
708             for( i=0 ; i<3 ; i++ )
709             {
710                 if( !*psz_parser )
711                 {
712                     /* we have only a raw device */
713                     break;
714                 }
715                 if( strtol( psz_parser, NULL, 10 ) )
716                 {
717                     /* we have only a partial list of options, no device */
718                     psz_parser = psz_raw;
719                     psz_raw = "";
720                     b_options = 1;
721                     break;
722                 }
723                 psz_parser++;
724             }
725         }
726     }
727     else
728     {
729         /* found beginning of options ; no raw device specified */
730         psz_raw = "";
731         b_options = 1;
732     }
733
734     if( b_options )
735     {
736         /* Found options */
737         i_title = (int)strtol( psz_parser, &psz_next, 10 );
738         if( *psz_next )
739         {
740             psz_parser = psz_next + 1;
741             i_chapter = (int)strtol( psz_parser, &psz_next, 10 );
742             if( *psz_next )
743             {
744                 i_angle = (int)strtol( psz_next + 1, NULL, 10 );
745             }
746         }
747
748         p_dvd->i_title = i_title ? i_title : 1;
749         p_dvd->i_chapter = i_chapter ? i_chapter : 1;
750         p_dvd->i_angle = i_angle ? i_angle : 1;
751     }
752
753     if( *psz_raw )
754     {
755         if( *psz_raw )
756         {
757             /* check the raw device */
758             if( stat( psz_raw, &stat_info ) == -1 )
759             {
760                 msg_Warn( p_input, "cannot stat() raw device `%s' (%s)",
761                                    psz_raw, strerror(errno));
762                 /* put back '@' */
763                 *(psz_raw - 1) = '@';
764                 psz_raw = "";
765             }
766             else
767             {
768                 char * psz_env;
769
770 #ifndef WIN32
771                 if( !S_ISCHR(stat_info.st_mode) )
772                 {
773                     msg_Warn( p_input, "raw device %s is"
774                               " not a valid char device", psz_raw );
775                     /* put back '@' */
776                     *(psz_raw - 1) = '@';
777                     psz_raw = "";
778                 }
779                 else
780 #endif
781                 {
782                     psz_env = malloc( strlen("DVDCSS_RAW_DEVICE=")
783                                     + strlen( psz_raw ) + 1 );
784                     sprintf( psz_env, "DVDCSS_RAW_DEVICE=%s", psz_raw );
785                     putenv( psz_env );
786                 }
787             }
788         }
789         else
790         {
791             psz_raw = "";
792         }
793     }
794
795     if( !*psz_device )
796     {
797         free( psz_device );
798
799         if( !p_input->psz_access )
800         {
801             /* no device and no access specified: we probably don't want DVD */
802             return NULL;
803         }
804         psz_device = config_GetPsz( p_input, "dvd" );
805     }
806
807 #ifndef WIN32
808     /* check block device */
809     if( stat( psz_device, &stat_info ) == -1 )
810     {
811         msg_Err( p_input, "cannot stat() device `%s' (%s)",
812                           psz_device, strerror(errno));
813         free( psz_device );
814         return NULL;
815     }
816
817     if( !S_ISBLK(stat_info.st_mode) && !S_ISCHR(stat_info.st_mode) )
818     {
819         msg_Warn( p_input,
820                   "dvd module discarded (not a valid block device)" );
821         free( psz_device );
822         return NULL;
823     }
824 #endif
825
826     msg_Dbg( p_input, "dvd=%s raw=%s title=%d chapter=%d angle=%d",
827                       psz_device, psz_raw, p_dvd->i_title,
828                       p_dvd->i_chapter, p_dvd->i_angle );
829
830     return psz_device;
831 }