]> git.sesse.net Git - vlc/blob - modules/access/dvdread/input.c
2f206dcad4bf7255c87a3109f5bc7e1eeac74ea8
[vlc] / modules / access / dvdread / input.c
1 /*****************************************************************************
2  * input.c: DvdRead plugin.
3  *****************************************************************************
4  * This plugins should handle all the known specificities of the DVD format,
5  * especially the 2048 bytes logical block size.
6  * It depends on: libdvdread for ifo files and block reading.
7  *****************************************************************************
8  * Copyright (C) 2001, 2003 VideoLAN
9  * $Id: input.c,v 1.24 2004/03/03 20:39:51 gbazin Exp $
10  *
11  * Author: Stéphane Borel <stef@via.ecp.fr>
12  *
13  * Some code taken form the play_title.c by Billy Biggs <vektor@dumbterm.net>
14  * in libdvdread.
15  *
16  * This program is free software; you can redistribute it and/or modify
17  * it under the terms of the GNU General Public License as published by
18  * the Free Software Foundation; either version 2 of the License, or
19  * (at your option) any later version.
20  *
21  * This program is distributed in the hope that it will be useful,
22  * but WITHOUT ANY WARRANTY; without even the implied warranty of
23  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24  * GNU General Public License for more details.
25  *
26  * You should have received a copy of the GNU General Public License
27  * along with this program; if not, write to the Free Software
28  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
29  *****************************************************************************/
30
31 /*****************************************************************************
32  * Preamble
33  *****************************************************************************/
34 #include <stdio.h>
35 #include <stdlib.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 #include <assert.h>
52
53 #ifdef STRNCASECMP_IN_STRINGS_H
54 #   include <strings.h>
55 #endif
56
57 #if defined( WIN32 )
58 #   include <io.h>                                                 /* read() */
59 #endif
60
61 #include <dvdread/dvd_reader.h>
62 #include <dvdread/ifo_types.h>
63 #include <dvdread/ifo_read.h>
64 #include <dvdread/nav_read.h>
65 #include <dvdread/nav_print.h>
66
67 #include "input.h"
68
69 #include "iso_lang.h"
70
71 /* how many blocks DVDRead will read in each loop */
72 #define DVD_BLOCK_READ_ONCE 64
73
74 /*****************************************************************************
75  * Private structure
76  *****************************************************************************/
77 struct demux_sys_t
78 {
79     module_t *   p_module;
80     mpeg_demux_t mpeg;
81 };
82
83 /*****************************************************************************
84  * Local prototypes
85  *****************************************************************************/
86 /* called from outside */
87 static int     DvdReadDemux    ( input_thread_t * );
88 static int     DvdReadRewind   ( input_thread_t * );
89
90 static int     DvdReadSetArea    ( input_thread_t *, input_area_t * );
91 static int     DvdReadSetProgram ( input_thread_t *, pgrm_descriptor_t * );
92 static ssize_t DvdReadRead       ( input_thread_t *, byte_t *, size_t );
93 static void    DvdReadSeek       ( input_thread_t *, off_t );
94
95 /* called only from here */
96 static void    DvdReadLauchDecoders( input_thread_t * p_input );
97 static void    DvdReadHandleDSI( thread_dvd_data_t * p_dvd, uint8_t * p_data );
98 static void    DvdReadFindCell ( thread_dvd_data_t * p_dvd );
99
100 /*
101  * Data demux functions
102  */
103
104 /*****************************************************************************
105  * InitDVD: initialize DVD structures
106  *****************************************************************************/
107 int E_(InitDVD) ( vlc_object_t *p_this )
108 {
109     input_thread_t *p_input = (input_thread_t *)p_this;
110     demux_sys_t *   p_demux;
111
112     if( p_input->stream.i_method != INPUT_METHOD_DVD )
113     {
114         return VLC_EGENERIC;
115     }
116
117     p_demux = p_input->p_demux_data = malloc( sizeof(demux_sys_t ) );
118     if( p_demux == NULL )
119     {
120         return VLC_ENOMEM;
121     }
122
123     p_input->p_private = (void*)&p_demux->mpeg;
124     p_demux->p_module = module_Need( p_input, "mpeg-system", NULL, 0 );
125     if( p_demux->p_module == NULL )
126     {
127         free( p_input->p_demux_data );
128         return VLC_ENOMOD;
129     }
130
131     p_input->pf_demux = DvdReadDemux;
132     p_input->pf_demux_control = demux_vaControlDefault;
133     p_input->pf_rewind = NULL;
134
135     vlc_mutex_lock( &p_input->stream.stream_lock );
136
137     DvdReadLauchDecoders( p_input );
138
139     vlc_mutex_unlock( &p_input->stream.stream_lock );
140
141     return VLC_SUCCESS;
142 }
143
144 /*****************************************************************************
145  * EndDVD: end DVD structures
146  *****************************************************************************/
147 void E_(EndDVD) ( vlc_object_t *p_this )
148 {
149     input_thread_t *p_input = (input_thread_t *)p_this;
150
151     module_Unneed( p_input, p_input->p_demux_data->p_module );
152     free( p_input->p_demux_data );
153 }
154
155 /*****************************************************************************
156  * DvdReadDemux
157  *****************************************************************************/
158 #define PEEK( SIZE )                                                        \
159     i_result = input_Peek( p_input, &p_peek, SIZE );                        \
160     if( i_result == -1 )                                                    \
161     {                                                                       \
162         return -1;                                                          \
163     }                                                                       \
164     else if( i_result < SIZE )                                              \
165     {                                                                       \
166         /* EOF */                                                           \
167         return 0;                                                           \
168     }
169
170 static int DvdReadDemux( input_thread_t * p_input )
171 {
172     int                 i;
173     byte_t *            p_peek;
174     data_packet_t *     p_data;
175     ssize_t             i_result;
176     int                 i_packet_size;
177
178
179     /* Read headers to compute payload length */
180     for( i = 0 ; i < DVD_BLOCK_READ_ONCE ; i++ )
181     {
182
183         /* Read what we believe to be a packet header. */
184         PEEK( 4 );
185
186         /* Default header */
187         if( U32_AT( p_peek ) != 0x1BA )
188         {
189             /* That's the case for all packets, except pack header. */
190             i_packet_size = U16_AT( p_peek + 4 );
191         }
192         else
193         {
194             /* MPEG-2 Pack header. */
195             i_packet_size = 8;
196         }
197
198         /* Fetch a packet of the appropriate size. */
199         i_result = input_SplitBuffer( p_input, &p_data, i_packet_size + 6 );
200         if( i_result <= 0 )
201         {
202             return( i_result );
203         }
204
205         /* In MPEG-2 pack headers we still have to read stuffing bytes. */
206         if( (p_data->p_demux_start[3] == 0xBA) && (i_packet_size == 8) )
207         {
208             ssize_t i_stuffing = (p_data->p_demux_start[13] & 0x7);
209             /* Force refill of the input buffer - though we don't care
210              * about p_peek. Please note that this is unoptimized. */
211             PEEK( i_stuffing );
212             p_input->p_current_data += i_stuffing;
213         }
214
215         p_input->p_demux_data->mpeg.pf_demux_ps( p_input, p_data );
216
217     }
218
219     return i;
220 }
221
222 /*****************************************************************************
223  * DVDRewind : reads a stream backward
224  *****************************************************************************/
225 static int DvdReadRewind( input_thread_t * p_input )
226 {
227     return -1;
228 }
229
230 /*
231  * Data access functions
232  */
233
234 /*****************************************************************************
235  * OpenDVD: open libdvdread
236  *****************************************************************************/
237 int E_(OpenDVD) ( vlc_object_t *p_this )
238 {
239     input_thread_t *        p_input = (input_thread_t *)p_this;
240     char *                  psz_parser;
241     char *                  psz_source;
242     char *                  psz_next;
243     struct stat             stat_info;
244     thread_dvd_data_t *     p_dvd;
245     dvd_reader_t *          p_dvdread;
246     input_area_t *          p_area;
247     unsigned int            i_title = 1;
248     unsigned int            i_chapter = 1;
249     unsigned int            i_angle = 1;
250     unsigned int            i;
251
252     psz_source = strdup( p_input->psz_name );
253     if( psz_source == NULL )
254     {
255         return VLC_ENOMEM;
256     }
257
258     p_input->pf_read = DvdReadRead;
259     p_input->pf_seek = DvdReadSeek;
260     p_input->pf_set_area = DvdReadSetArea;
261     p_input->pf_set_program = DvdReadSetProgram;
262
263     /* Start with the end, because you could have :
264      * dvdread:/Volumes/my@toto/VIDEO_TS@1,1
265      * (yes, this is kludgy). */
266     for ( psz_parser = psz_source + strlen(psz_source) - 1;
267           psz_parser >= psz_source && *psz_parser != '@';
268           psz_parser-- );
269
270     if( psz_parser >= psz_source && *psz_parser == '@' )
271     {
272         /* Found options */
273         *psz_parser = '\0';
274         ++psz_parser;
275
276         i_title = (int)strtol( psz_parser, &psz_next, 10 );
277         if( *psz_next )
278         {
279             psz_parser = psz_next + 1;
280             i_chapter = (int)strtol( psz_parser, &psz_next, 10 );
281             if( *psz_next )
282             {
283                 i_angle = (int)strtol( psz_next + 1, NULL, 10 );
284             }
285         }
286
287         i_title = i_title ? i_title : 1;
288         i_chapter = i_chapter ? i_chapter : 1;
289         i_angle = i_angle ? i_angle : 1;
290     }
291
292     if( !*psz_source )
293     {
294         free( psz_source );
295         if( !p_input->psz_access )
296         {
297             return VLC_EGENERIC;
298         }
299         psz_source = config_GetPsz( p_input, "dvd" );
300         if( !psz_source ) return VLC_EGENERIC;
301     }
302
303     if( stat( psz_source, &stat_info ) == -1 )
304     {
305         msg_Warn( p_input, "cannot stat() source `%s' (%s)",
306                            psz_source, strerror(errno) );
307         free( psz_source );
308         return VLC_EGENERIC;
309     }
310     if( !S_ISBLK(stat_info.st_mode) &&
311         !S_ISCHR(stat_info.st_mode) &&
312         !S_ISDIR(stat_info.st_mode) )
313     {
314         msg_Warn( p_input, "dvdread module discarded (not a valid source)" );
315         free( psz_source );
316         return VLC_EGENERIC;
317     }
318
319     msg_Dbg( p_input, "dvdroot=%s title=%d chapter=%d angle=%d",
320                       psz_source, i_title, i_chapter, i_angle );
321
322
323     p_dvdread = DVDOpen( psz_source );
324
325     /* free allocated strings */
326     free( psz_source );
327
328     if( ! p_dvdread )
329     {
330         msg_Err( p_input, "libdvdcss cannot open source" );
331         return VLC_EGENERIC;
332     }
333
334     /* set up input  */
335     p_input->i_mtu = 0;
336
337     p_dvd = malloc( sizeof(thread_dvd_data_t) );
338     if( p_dvd == NULL )
339     {
340         msg_Err( p_input, "out of memory" );
341         return VLC_ENOMEM;
342     }
343
344     p_dvd->p_dvdread = p_dvdread;
345     p_dvd->p_title = NULL;
346     p_dvd->p_vts_file = NULL;
347
348
349     p_input->p_access_data = (void *)p_dvd;
350
351     /* Ifo allocation & initialisation */
352     if( ! ( p_dvd->p_vmg_file = ifoOpen( p_dvd->p_dvdread, 0 ) ) )
353     {
354         msg_Warn( p_input, "cannot open VMG info" );
355         free( p_dvd );
356         return VLC_EGENERIC;
357     }
358     msg_Dbg( p_input, "VMG opened" );
359
360     /* Set stream and area data */
361     vlc_mutex_lock( &p_input->stream.stream_lock );
362
363     p_input->stream.i_method = INPUT_METHOD_DVD;
364
365     /* If we are here we can control the pace... */
366     p_input->stream.b_pace_control = VLC_TRUE;
367     p_input->stream.b_seekable = VLC_TRUE;
368
369     p_input->stream.p_selected_area->i_size = 0;
370     p_input->stream.p_selected_area->i_tell = 0;
371
372     /* Initialize ES structures */
373     input_InitStream( p_input, sizeof( stream_ps_data_t ) );
374
375     /* disc input method */
376     p_input->stream.i_method = INPUT_METHOD_DVD;
377
378 #define tt_srpt p_dvd->p_vmg_file->tt_srpt
379     msg_Dbg( p_input, "number of titles: %d", tt_srpt->nr_of_srpts );
380
381 #define area p_input->stream.pp_areas
382     /* We start from 1 here since the default area 0
383      * is reserved for video_ts.vob */
384     for( i = 1 ; i <= tt_srpt->nr_of_srpts ; i++ )
385     {
386         /* Titles are Program Chains */
387         input_AddArea( p_input, i, tt_srpt->title[i-1].nr_of_ptts );
388
389         /* Absolute start offset and size
390          * We can only set that with vts ifo, so we do it during the
391          * first call to DVDSetArea */
392         area[i]->i_start = 0;
393         area[i]->i_size = 0;
394
395         /* Default Chapter */
396         area[i]->i_part = 1;
397
398         area[i]->i_plugin_data = tt_srpt->title[i-1].title_set_nr;
399     }
400 #undef area
401
402     p_dvd->i_title = i_title <= tt_srpt->nr_of_srpts ? i_title : 1;
403 #undef tt_srpt
404
405     p_area = p_input->stream.pp_areas[p_dvd->i_title];
406     p_dvd->i_chapter = i_chapter;
407
408     p_dvd->i_chapter = i_chapter < p_area->i_part_nb ? i_chapter : 1;
409     p_area->i_part = p_dvd->i_chapter;
410
411     p_dvd->i_angle = i_angle;
412
413     /* set title, chapter, audio and subpic */
414     if( DvdReadSetArea( p_input, p_area ) )
415     {
416         vlc_mutex_unlock( &p_input->stream.stream_lock );
417         return VLC_EGENERIC;
418     }
419
420     vlc_mutex_unlock( &p_input->stream.stream_lock );
421
422     if( !p_input->psz_demux || !*p_input->psz_demux )
423     {
424         p_input->psz_demux = "dvdread";
425     }
426
427     return VLC_SUCCESS;
428 }
429
430 /*****************************************************************************
431  * CloseDVD: close libdvdread
432  *****************************************************************************/
433 void E_(CloseDVD) ( vlc_object_t *p_this )
434 {
435     input_thread_t *    p_input = (input_thread_t *)p_this;
436     thread_dvd_data_t * p_dvd = (thread_dvd_data_t *)p_input->p_access_data;
437
438     /* This is a very nasty side-effect in the DVD plug-in : language
439      * selection here influences language selection of other streams. So
440      * unset those variables (may not be what the user wants).
441      * FIXME FIXME FIXME FIXME FIXME FIXME FIXME --Meuuh */
442     config_PutInt( p_input, "audio-channel", -1 );
443     config_PutInt( p_input, "spu-channel", -1 );
444
445     /* close libdvdread */
446     DVDCloseFile( p_dvd->p_title );
447     ifoClose( p_dvd->p_vts_file );
448     ifoClose( p_dvd->p_vmg_file );
449
450     DVDClose( p_dvd->p_dvdread );
451     free( p_dvd );
452     p_input->p_access_data = NULL;
453
454 }
455
456 /*****************************************************************************
457  * DvdReadSetProgram: Does nothing, a DVD is mono-program
458  *****************************************************************************/
459 static int DvdReadSetProgram( input_thread_t * p_input,
460                               pgrm_descriptor_t * p_program )
461 {
462     if( p_input->stream.p_selected_program != p_program )
463     {
464         thread_dvd_data_t *  p_dvd;
465         vlc_value_t val;
466
467         p_dvd = (thread_dvd_data_t*)(p_input->p_access_data);
468         p_dvd->i_angle = p_program->i_number;
469
470         memcpy( p_program, p_input->stream.p_selected_program,
471                 sizeof(pgrm_descriptor_t) );
472         p_program->i_number = p_dvd->i_angle;
473         p_input->stream.p_selected_program = p_program;
474
475         msg_Dbg( p_input, "angle %d selected", p_dvd->i_angle );
476
477         /* Update the navigation variables without triggering a callback */
478         val.i_int = p_program->i_number;
479         var_Change( p_input, "program", VLC_VAR_SETVALUE, &val, NULL );
480     }
481
482     return VLC_SUCCESS;
483 }
484
485 #define p_pgc         p_dvd->p_cur_pgc
486
487 /*****************************************************************************
488  * DvdReadSetArea: initialize input data for title x, chapter y.
489  * It should be called for each user navigation request.
490  *****************************************************************************
491  * Take care that i_title starts from 0 (vmg) and i_chapter start from 1.
492  * Note that you have to take the lock before entering here.
493  *****************************************************************************/
494 static int DvdReadSetArea( input_thread_t * p_input, input_area_t * p_area )
495 {
496     thread_dvd_data_t *  p_dvd;
497     int                  pgc_id = 0;
498     int                  pgn = 0;
499     vlc_value_t          val;
500
501     p_dvd = (thread_dvd_data_t*)p_input->p_access_data;
502
503     /* we can't use the interface slider until initilization is complete */
504     p_input->stream.b_seekable = VLC_FALSE;
505
506     if( p_area != p_input->stream.p_selected_area )
507     {
508         es_descriptor_t *    p_es;
509         unsigned int         i_cell = 0;
510         unsigned int         i_audio_nb = 0;
511         unsigned int         i_spu_nb = 0;
512         unsigned int         i;
513
514 #define p_vmg         p_dvd->p_vmg_file
515 #define p_vts         p_dvd->p_vts_file
516         if( p_dvd->p_title != NULL )
517         {
518             DVDCloseFile( p_dvd->p_title );
519         }
520
521         if( p_vts != NULL )
522         {
523             ifoClose( p_vts );
524         }
525
526         /* Reset the Chapter position of the old title */
527         p_input->stream.p_selected_area->i_part = 1;
528
529         /*
530          *  We have to load all title information
531          */
532         /* Change the default area */
533         p_input->stream.p_selected_area = p_area;
534
535         msg_Dbg( p_input, "open VTS %d, for title %d",
536             p_vmg->tt_srpt->title[ p_area->i_id - 1 ].title_set_nr,
537             p_area->i_id );
538
539         /* ifo vts */
540         if( ! ( p_vts = ifoOpen( p_dvd->p_dvdread,
541                 p_vmg->tt_srpt->title[ p_area->i_id - 1 ].title_set_nr ) ) )
542         {
543             msg_Err( p_input, "fatal error in vts ifo" );
544             ifoClose( p_vmg );
545             DVDClose( p_dvd->p_dvdread );
546             return VLC_EGENERIC;
547         }
548
549         /* title position inside the selected vts */
550         p_dvd->i_ttn = p_vmg->tt_srpt->title[ p_area->i_id - 1 ].vts_ttn;
551
552         /*
553          * Set selected title start
554          */
555         pgc_id = p_vts->vts_ptt_srpt->title[p_dvd->i_ttn-1].ptt[0].pgcn;
556         pgn = p_vts->vts_ptt_srpt->title[p_dvd->i_ttn-1].ptt[0].pgn;
557         p_pgc = p_vts->vts_pgcit->pgci_srp[ pgc_id - 1 ].pgc;
558         i_cell = p_pgc->program_map[ pgn - 1 ] - 1;
559
560         p_area->i_start =
561             LB2OFF( p_dvd->p_cur_pgc->cell_playback[ i_cell ].first_sector );
562
563         msg_Dbg( p_input, "start %d vts_title %d pgc %d pgn %d",
564                   p_area->i_id, p_dvd->i_ttn, pgc_id, pgn );
565
566         /*
567          * Find title end
568          */
569         i_cell = p_dvd->p_cur_pgc->nr_of_cells - 1;
570
571         p_dvd->i_end_block = p_pgc->cell_playback[ i_cell ].last_sector;
572         p_area->i_size = LB2OFF( p_dvd->i_end_block )- p_area->i_start;
573
574         msg_Dbg( p_input, "start "I64Fd" size "I64Fd" end %d",
575                   p_area->i_start , p_area->i_size, p_dvd->i_end_block );
576
577         /*
578          * Set properties for current chapter
579          */
580         /* Remeber current chapter */
581         p_dvd->i_chapter = p_area->i_part;
582         p_dvd->b_eoc = VLC_FALSE;
583
584         pgc_id = p_vts->vts_ptt_srpt->title[
585                     p_dvd->i_ttn-1].ptt[p_area->i_part-1].pgcn;
586         pgn = p_vts->vts_ptt_srpt->title[
587                     p_dvd->i_ttn-1].ptt[p_area->i_part-1].pgn;
588
589         p_pgc = p_vts->vts_pgcit->pgci_srp[pgc_id-1].pgc;
590         p_dvd->i_pack_len = 0;
591         p_dvd->i_next_cell = p_dvd->i_cur_cell = p_pgc->program_map[pgn-1] - 1;
592         DvdReadFindCell( p_dvd );
593
594         p_dvd->i_next_vobu = p_dvd->i_cur_block =
595             p_pgc->cell_playback[p_dvd->i_cur_cell].first_sector;
596
597         /*
598          * Angle management
599          */
600         p_dvd->i_angle_nb = p_vmg->tt_srpt->title[p_area->i_id-1].nr_of_angles;
601
602         if( p_dvd->i_angle > p_dvd->i_angle_nb )
603         {
604             p_dvd->i_angle = 1;
605         }
606
607         /*
608          * We've got enough info, time to open the title set data.
609          */
610         if( ! ( p_dvd->p_title = DVDOpenFile( p_dvd->p_dvdread,
611             p_vmg->tt_srpt->title[ p_area->i_id - 1 ].title_set_nr,
612             DVD_READ_TITLE_VOBS ) ) )
613         {
614             msg_Err( p_input, "cannot open title (VTS_%02d_1.VOB)",
615                      p_vmg->tt_srpt->title[p_area->i_id-1].title_set_nr );
616             ifoClose( p_vts );
617             ifoClose( p_vmg );
618             DVDClose( p_dvd->p_dvdread );
619             return VLC_EGENERIC;
620         }
621
622 //        IfoPrintTitle( p_dvd );
623
624         /*
625          * Destroy obsolete ES by reinitializing program 0
626          * and find all ES in title with ifo data
627          */
628         if( p_input->stream.pp_programs != NULL )
629         {
630             /* We don't use input_EndStream here since
631              * we keep area structures */
632
633             while( p_input->stream.i_es_number )
634             {
635                 input_DelES( p_input, p_input->stream.pp_es[0] );
636             }
637
638             while( p_input->stream.i_pgrm_number )
639             {
640                 input_DelProgram( p_input, p_input->stream.pp_programs[0] );
641             }
642
643             if( p_input->stream.pp_selected_es )
644             {
645                 free( p_input->stream.pp_selected_es );
646                 p_input->stream.pp_selected_es = NULL;
647             }
648             p_input->stream.i_selected_es_number = 0;
649         }
650
651         input_AddProgram( p_input, 1, sizeof( stream_ps_data_t ) );
652         p_input->stream.p_selected_program = p_input->stream.pp_programs[0];
653
654         for( i = 1 ; i < p_dvd->i_angle_nb ; i++ )
655         {
656             input_AddProgram( p_input, i+1, 0 );
657         }
658
659         DvdReadSetProgram( p_input,
660                            p_input->stream.pp_programs[p_dvd->i_angle-1] );
661
662         /* No PSM to read in DVD mode, we already have all information */
663         p_input->stream.p_selected_program->b_is_ok = VLC_TRUE;
664
665         p_es = NULL;
666
667         /* ES 0 -> video MPEG2 */
668 //        IfoPrintVideo( p_dvd );
669
670         p_es = input_AddES( p_input, NULL, 0xe0, VIDEO_ES, NULL, 0 );
671         p_es->i_stream_id = 0xe0;
672         p_es->i_fourcc = VLC_FOURCC('m','p','g','v');
673
674 #define audio_control \
675     p_dvd->p_vts_file->vts_pgcit->pgci_srp[pgc_id-1].pgc->audio_control[i-1]
676         /* Audio ES, in the order they appear in .ifo */
677         for( i = 1 ; i <= p_vts->vtsi_mat->nr_of_vts_audio_streams ; i++ )
678         {
679             int i_position = 0;
680             uint16_t i_id;
681
682 //            IfoPrintAudio( p_dvd, i );
683
684             /* audio channel is active if first byte is 0x80 */
685             if( audio_control & 0x8000 )
686             {
687                 i_audio_nb++;
688                 i_position = ( audio_control & 0x7F00 ) >> 8;
689
690                 msg_Dbg( p_input, "audio position  %d", i_position );
691                 switch( p_vts->vtsi_mat->vts_audio_attr[i-1].audio_format )
692                 {
693                 case 0x00:              /* A52 */
694                     i_id = ( ( 0x80 + i_position ) << 8 ) | 0xbd;
695                     p_es = input_AddES( p_input, NULL, i_id, AUDIO_ES,
696                                         DecodeLanguage(
697                         p_vts->vtsi_mat->vts_audio_attr[i-1].lang_code ), 0 );
698                     p_es->i_stream_id = 0xbd;
699                     p_es->i_fourcc = VLC_FOURCC('a','5','2','b');
700
701                     break;
702                 case 0x02:
703                 case 0x03:              /* MPEG audio */
704                     i_id = 0xc0 + i_position;
705                     p_es = input_AddES( p_input, NULL, i_id, AUDIO_ES,
706                                         DecodeLanguage(
707                         p_vts->vtsi_mat->vts_audio_attr[i-1].lang_code ), 0 );
708                     p_es->i_stream_id = i_id;
709                     p_es->i_fourcc = VLC_FOURCC('m','p','g','a');
710
711                     break;
712                 case 0x04:              /* LPCM */
713
714                     i_id = ( ( 0xa0 + i_position ) << 8 ) | 0xbd;
715                     p_es = input_AddES( p_input, NULL, i_id, AUDIO_ES,
716                                         DecodeLanguage(
717                         p_vts->vtsi_mat->vts_audio_attr[i-1].lang_code ), 0 );
718                     p_es->i_stream_id = i_id;
719                     p_es->i_fourcc = VLC_FOURCC('l','p','c','b');
720
721                     break;
722                 case 0x06:              /* DTS */
723                     i_id = ( ( 0x88 + i_position ) << 8 ) | 0xbd;
724                     msg_Err( p_input, "DTS audio not handled yet"
725                                       "(0x%x)", i_id );
726                     break;
727                 default:
728                     i_id = 0;
729                     msg_Err( p_input, "unknown audio type %.2x",
730                           p_vts->vtsi_mat->vts_audio_attr[i-1].audio_format );
731                 }
732             }
733         }
734 #undef audio_control
735 #define spu_control \
736     p_dvd->p_vts_file->vts_pgcit->pgci_srp[pgc_id-1].pgc->subp_control[i-1]
737
738         /* Sub Picture ES */
739
740         for( i = 1 ; i <= p_vts->vtsi_mat->nr_of_vts_subp_streams; i++ )
741         {
742             int i_position = 0;
743             uint16_t i_id;
744
745 //            IfoPrintSpu( p_dvd, i );
746             msg_Dbg( p_input, "spu %d 0x%02x", i, spu_control );
747
748             if( spu_control & 0x80000000 )
749             {
750                 i_spu_nb++;
751
752                 /*  there are several streams for one spu */
753                 if(  p_vts->vtsi_mat->vts_video_attr.display_aspect_ratio )
754                 {
755                     /* 16:9 */
756                     switch( p_vts->vtsi_mat->vts_video_attr.permitted_df )
757                     {
758                     case 1:
759                         i_position = spu_control & 0xff;
760                         break;
761                     case 2:
762                         i_position = ( spu_control >> 8 ) & 0xff;
763                         break;
764                     default:
765                         i_position = ( spu_control >> 16 ) & 0xff;
766                         break;
767                     }
768                 }
769                 else
770                 {
771                     /* 4:3 */
772                     i_position = ( spu_control >> 24 ) & 0x7F;
773                 }
774
775                 i_id = ( ( 0x20 + i_position ) << 8 ) | 0xbd;
776                 p_es = input_AddES( p_input, NULL, i_id, SPU_ES,
777                                     DecodeLanguage(
778                     p_vts->vtsi_mat->vts_subp_attr[i-1].lang_code ), 0 );
779                 p_es->i_stream_id = 0xbd;
780                 p_es->i_fourcc = VLC_FOURCC('s','p','u','b');
781             }
782         }
783 #undef spu_control
784
785         /* FIXME: hack to check that the demuxer is ready, and set
786          * the decoders */
787         if( p_input->p_demux )
788         {
789             DvdReadLauchDecoders( p_input );
790         }
791
792         /* Update the navigation variables without triggering a callback */
793         val.i_int = p_area->i_id;
794         var_Change( p_input, "title", VLC_VAR_SETVALUE, &val, NULL );
795         var_Change( p_input, "chapter", VLC_VAR_CLEARCHOICES, NULL, NULL );
796         for( i = 1; i <= p_area->i_part_nb; i++ )
797         {
798             val.i_int = i;
799             var_Change( p_input, "chapter", VLC_VAR_ADDCHOICE, &val, NULL );
800         }
801
802     } /* i_title >= 0 */
803     else
804     {
805         p_area = p_input->stream.p_selected_area;
806     }
807
808     /*
809      * Chapter selection
810      */
811
812     if( p_area->i_part != p_dvd->i_chapter )
813     {
814         if( ( p_area->i_part > 0 ) &&
815             ( p_area->i_part <= p_area->i_part_nb ))
816         {
817             p_dvd->i_ttn = p_vmg->tt_srpt->title[p_area->i_id-1].vts_ttn;
818             pgc_id = p_vts->vts_ptt_srpt->title[
819                         p_dvd->i_ttn-1].ptt[p_area->i_part-1].pgcn;
820             pgn = p_vts->vts_ptt_srpt->title[
821                         p_dvd->i_ttn-1].ptt[p_area->i_part-1].pgn;
822
823             p_pgc = p_vts->vts_pgcit->pgci_srp[ pgc_id - 1 ].pgc;
824
825             p_dvd->i_cur_cell = p_pgc->program_map[ pgn - 1 ] - 1;
826             p_dvd->i_chapter = p_area->i_part;
827             DvdReadFindCell( p_dvd );
828
829             p_dvd->i_pack_len = 0;
830             p_dvd->i_next_vobu = p_dvd->i_cur_block =
831                     p_pgc->cell_playback[p_dvd->i_cur_cell].first_sector;
832         }
833         else
834         {
835             p_area->i_part = p_dvd->i_chapter;
836         }
837     }
838 #undef p_vts
839 #undef p_vmg
840
841     /* warn interface that something has changed */
842     p_area->i_tell = LB2OFF( p_dvd->i_next_vobu ) - p_area->i_start;
843     p_input->stream.b_seekable = VLC_TRUE;
844     p_input->stream.b_changed = VLC_TRUE;
845
846     /* Update the navigation variables without triggering a callback */
847     val.i_int = p_area->i_part;
848     var_Change( p_input, "chapter", VLC_VAR_SETVALUE, &val, NULL );
849
850     return VLC_SUCCESS;
851 }
852
853
854 /*****************************************************************************
855  * DvdReadRead: reads data packets.
856  *****************************************************************************
857  * Returns -1 in case of error, 0 in case of EOF, otherwise the number of
858  * bytes.
859  *****************************************************************************/
860 static ssize_t DvdReadRead( input_thread_t * p_input,
861                             byte_t * p_buffer, size_t i_count )
862 {
863     thread_dvd_data_t *     p_dvd;
864     byte_t *                p_buf;
865     unsigned int            i_blocks_once;
866     unsigned int            i_blocks;
867     int                     i_read;
868     int                     i_read_total;
869     vlc_bool_t              b_eot = VLC_FALSE;
870
871     p_dvd = (thread_dvd_data_t *)p_input->p_access_data;
872     p_buf = p_buffer;
873
874     /*
875      * Playback by cell in this pgc, starting at the cell for our chapter.
876      */
877     i_blocks = OFF2LB( i_count );
878     i_read_total = 0;
879     i_read = 0;
880
881     while( i_blocks )
882     {
883         /*
884          * End of pack, we select the following one
885          */
886         if( ! p_dvd->i_pack_len )
887         {
888             /*
889              * Read NAV packet.
890              */
891             if( ( i_read = DVDReadBlocks( p_dvd->p_title, p_dvd->i_next_vobu,
892                            1, p_buf ) ) != 1 )
893             {
894                 msg_Err( p_input, "read failed for block %d",
895                                   p_dvd->i_next_vobu );
896                 return -1;
897             }
898
899             /* basic check to be sure we don't have a empty title
900              * go to next title if so */
901             //assert( p_buffer[41] == 0xbf && p_buffer[1027] == 0xbf );
902
903             /*
904              * Parse the contained dsi packet.
905              */
906
907             DvdReadHandleDSI( p_dvd, p_buf );
908
909             /* End of title */
910             if( p_dvd->i_next_vobu >= p_dvd->i_end_block + 1 )
911             {
912                 b_eot = 1;
913                 break;
914             }
915
916             assert( p_dvd->i_pack_len < 1024 );
917             /* FIXME: Ugly kludge: we send the pack block to the input for it
918              * sometimes has a zero scr and restart the sync */
919             p_dvd->i_cur_block ++;
920             //p_dvd->i_pack_len++;
921
922             i_read_total++;
923             p_buf += DVD_VIDEO_LB_LEN;
924             i_blocks--;
925         }
926
927         /*
928          * Compute the number of blocks to read
929          */
930         i_blocks_once = __MIN( p_dvd->i_pack_len, i_blocks );
931         p_dvd->i_pack_len -= i_blocks_once;
932
933         /* Reads from DVD */
934         i_read = DVDReadBlocks( p_dvd->p_title, p_dvd->i_cur_block,
935                                 i_blocks_once, p_buf );
936         if( (unsigned int)i_read != i_blocks_once )
937         {
938             msg_Err( p_input, "read failed for %d/%d blocks at 0x%02x",
939                               i_read, i_blocks_once, p_dvd->i_cur_block );
940             return -1;
941         }
942
943         i_blocks -= i_read;
944         i_read_total += i_read;
945         p_dvd->i_cur_block += i_read;
946         p_buf += LB2OFF( i_read );
947
948     }
949 /*
950     msg_Dbg( p_input, "i_blocks: %d len: %d current: 0x%02x", i_read, p_dvd->i_pack_len, p_dvd->i_cur_block );
951 */
952
953     vlc_mutex_lock( &p_input->stream.stream_lock );
954
955     if( p_dvd->b_eoc )
956     {
957         /* We modify i_part only at end of chapter not to erase
958          * some modification from the interface */
959         p_input->stream.p_selected_area->i_part = p_dvd->i_chapter;
960         p_dvd->b_eoc = VLC_FALSE;
961     }
962
963     if( ( LB2OFF( p_dvd->i_cur_block )
964           - p_input->stream.p_selected_area->i_start )
965             >= p_input->stream.p_selected_area->i_size || b_eot )
966     {
967         if( ( p_input->stream.p_selected_area->i_id + 1 ) >=
968                         p_input->stream.i_area_nb )
969         {
970             /* EOF */
971             vlc_mutex_unlock( &p_input->stream.stream_lock );
972             return 0;
973         }
974
975         /* EOT */
976         msg_Dbg( p_input, "new title" );
977         DvdReadSetArea( p_input, p_input->stream.pp_areas[
978                         p_input->stream.p_selected_area->i_id+1] );
979     }
980
981     vlc_mutex_unlock( &p_input->stream.stream_lock );
982
983     return LB2OFF( i_read_total );
984 }
985 #undef p_pgc
986
987 /*****************************************************************************
988  * DvdReadSeek : Goes to a given position on the stream.
989  *****************************************************************************
990  * This one is used by the input and translate chronological position from
991  * input to logical position on the device.
992  * The lock should be taken before calling this function.
993  *****************************************************************************/
994 static void DvdReadSeek( input_thread_t * p_input, off_t i_off )
995 {
996     thread_dvd_data_t *     p_dvd;
997     unsigned int            i_lb;
998     unsigned int            i_tmp;
999     unsigned int            i_chapter = 0;
1000     unsigned int            i_cell = 0;
1001     unsigned int            i_vobu = 0;
1002     unsigned int            i_sub_cell = 0;
1003
1004     vlc_mutex_lock( &p_input->stream.stream_lock );
1005     i_off += p_input->stream.p_selected_area->i_start;
1006     vlc_mutex_unlock( &p_input->stream.stream_lock );
1007
1008     i_lb = OFF2LB( i_off );
1009     p_dvd = ( thread_dvd_data_t * )p_input->p_access_data;
1010
1011     /* find cell */
1012     while( p_dvd->p_cur_pgc->cell_playback[i_cell].last_sector < i_lb )
1013     {
1014         i_cell++;
1015     }
1016
1017     /* find chapter */
1018     do
1019     {
1020         pgc_t *     p_pgc;
1021         int         pgc_id, pgn;
1022
1023         i_chapter++;
1024         pgc_id = p_dvd->p_vts_file->vts_ptt_srpt->title[
1025                     p_dvd->i_ttn-1].ptt[i_chapter-1].pgcn;
1026         pgn = p_dvd->p_vts_file->vts_ptt_srpt->title[
1027                     p_dvd->i_ttn-1].ptt[i_chapter-1].pgn;
1028
1029         p_pgc = p_dvd->p_vts_file->vts_pgcit->pgci_srp[pgc_id-1].pgc;
1030         i_tmp = p_pgc->program_map[pgn-1];
1031
1032     } while( i_tmp <= i_cell );
1033
1034     /* find vobu */
1035     while( p_dvd->p_vts_file->vts_vobu_admap->vobu_start_sectors[i_vobu]
1036             <= i_lb )
1037     {
1038         i_vobu++;
1039     }
1040
1041     /* find sub_cell */
1042     while( p_dvd->p_vts_file->vts_c_adt->cell_adr_table[i_sub_cell].start_sector <
1043             p_dvd->p_vts_file->vts_vobu_admap->vobu_start_sectors[i_vobu-1] )
1044     {
1045         i_sub_cell++;
1046     }
1047
1048 /*
1049     msg_Dbg( p_input, "cell %d i_sub_cell %d chapter %d vobu %d cell_sector %d vobu_sector %d sub_cell_sector %d",
1050             i_cell, i_sub_cell,i_chapter, i_vobu,
1051             p_dvd->p_cur_pgc->cell_playback[i_cell].first_sector,
1052             p_dvd->p_vts_file->vts_vobu_admap->vobu_start_sectors[i_vobu],
1053             p_dvd->p_vts_file->vts_c_adt->cell_adr_table[i_sub_cell-1].start_sector);
1054 */
1055     p_dvd->i_cur_block = i_lb;
1056     p_dvd->i_next_vobu =
1057         p_dvd->p_vts_file->vts_vobu_admap->vobu_start_sectors[i_vobu];
1058     p_dvd->i_pack_len = p_dvd->i_next_vobu - i_lb;
1059     p_dvd->i_cur_cell = i_cell;
1060     p_dvd->i_chapter = i_chapter;
1061     DvdReadFindCell( p_dvd );
1062
1063     vlc_mutex_lock( &p_input->stream.stream_lock );
1064     p_input->stream.p_selected_area->i_tell =
1065         LB2OFF ( p_dvd->i_cur_block )
1066          - p_input->stream.p_selected_area->i_start;
1067     p_input->stream.p_selected_area->i_part = p_dvd->i_chapter;
1068     vlc_mutex_unlock( &p_input->stream.stream_lock );
1069
1070     return;
1071 }
1072
1073 /*****************************************************************************
1074  * DvdReadHandleDSI
1075  *****************************************************************************/
1076 static void DvdReadHandleDSI( thread_dvd_data_t * p_dvd, uint8_t * p_data )
1077 {
1078     navRead_DSI( &(p_dvd->dsi_pack), &(p_data[ DSI_START_BYTE ]) );
1079
1080     /*
1081      * Determine where we go next.  These values are the ones we mostly
1082      * care about.
1083      */
1084     p_dvd->i_cur_block = p_dvd->dsi_pack.dsi_gi.nv_pck_lbn;
1085
1086     /*
1087      * If we're not at the end of this cell, we can determine the next
1088      * VOBU to display using the VOBU_SRI information section of the
1089      * DSI.  Using this value correctly follows the current angle,
1090      * avoiding the doubled scenes in The Matrix, and makes our life
1091      * really happy.
1092      */
1093     if( p_dvd->dsi_pack.vobu_sri.next_vobu != SRI_END_OF_CELL )
1094     {
1095 #if 1
1096         switch( ( p_dvd->dsi_pack.sml_pbi.category & 0xf000 ) >> 12 )
1097         {
1098             case 0x4:
1099                 /* interleaved unit with no angle */
1100                 if( p_dvd->dsi_pack.sml_pbi.ilvu_sa != 0 )
1101                 {
1102                     p_dvd->i_next_vobu = p_dvd->i_cur_block +
1103                         p_dvd->dsi_pack.sml_pbi.ilvu_sa;
1104                     p_dvd->i_pack_len = p_dvd->dsi_pack.sml_pbi.ilvu_ea;
1105                 }
1106                 else
1107                 {
1108                     p_dvd->i_next_vobu = p_dvd->i_cur_block +
1109                         p_dvd->dsi_pack.dsi_gi.vobu_ea + 1;
1110                     p_dvd->i_pack_len = p_dvd->dsi_pack.dsi_gi.vobu_ea;
1111                 }
1112                 break;
1113             case 0x5:
1114                 /* vobu is end of ilvu */
1115                 if( p_dvd->dsi_pack.sml_agli.data[p_dvd->i_angle-1].address )
1116                 {
1117                     p_dvd->i_next_vobu = p_dvd->i_cur_block +
1118                         p_dvd->dsi_pack.sml_agli.data[p_dvd->i_angle-1].address;
1119                     p_dvd->i_pack_len = p_dvd->dsi_pack.sml_pbi.ilvu_ea;
1120
1121                     break;
1122                 }
1123             case 0x6:
1124                 /* vobu is beginning of ilvu */
1125             case 0x9:
1126                 /* next scr is 0 */
1127             case 0xa:
1128                 /* entering interleaved section */
1129             case 0x8:
1130                 /* non interleaved cells in interleaved section */
1131             default:
1132                 p_dvd->i_next_vobu = p_dvd->i_cur_block +
1133                     ( p_dvd->dsi_pack.vobu_sri.next_vobu & 0x7fffffff );
1134                 p_dvd->i_pack_len = p_dvd->dsi_pack.dsi_gi.vobu_ea;
1135                 break;
1136         }
1137 #else
1138         p_dvd->i_next_vobu = p_dvd->i_cur_block +
1139             ( p_dvd->dsi_pack.vobu_sri.next_vobu & 0x7fffffff );
1140         p_dvd->i_pack_len = p_dvd->dsi_pack.dsi_gi.vobu_ea;
1141 #endif
1142     }
1143     else
1144     {
1145         p_dvd->i_cur_cell = p_dvd->i_next_cell;
1146         DvdReadFindCell( p_dvd );
1147
1148         p_dvd->i_pack_len = p_dvd->dsi_pack.dsi_gi.vobu_ea;
1149         p_dvd->i_next_vobu =
1150             p_dvd->p_cur_pgc->cell_playback[p_dvd->i_cur_cell].first_sector;
1151     }
1152
1153 #if 0
1154     msg_Dbg( p_input, 12, "scr %d lbn 0x%02x vobu_ea %d vob_id %d c_id %d",
1155              p_dvd->dsi_pack.dsi_gi.nv_pck_scr,
1156              p_dvd->dsi_pack.dsi_gi.nv_pck_lbn,
1157              p_dvd->dsi_pack.dsi_gi.vobu_ea,
1158              p_dvd->dsi_pack.dsi_gi.vobu_vob_idn,
1159              p_dvd->dsi_pack.dsi_gi.vobu_c_idn );
1160
1161     msg_Dbg( p_input, 12, "cat 0x%02x ilvu_ea %d ilvu_sa %d size %d",
1162              p_dvd->dsi_pack.sml_pbi.category,
1163              p_dvd->dsi_pack.sml_pbi.ilvu_ea,
1164              p_dvd->dsi_pack.sml_pbi.ilvu_sa,
1165              p_dvd->dsi_pack.sml_pbi.size );
1166
1167     msg_Dbg( p_input, 12, "next_vobu %d next_ilvu1 %d next_ilvu2 %d",
1168              p_dvd->dsi_pack.vobu_sri.next_vobu & 0x7fffffff,
1169              p_dvd->dsi_pack.sml_agli.data[ p_dvd->i_angle - 1 ].address,
1170              p_dvd->dsi_pack.sml_agli.data[ p_dvd->i_angle ].address);
1171 #endif
1172 }
1173
1174 /*****************************************************************************
1175  * DvdReadFindCell
1176  *****************************************************************************/
1177 static void DvdReadFindCell( thread_dvd_data_t * p_dvd )
1178 {
1179     int         pgc_id, pgn;
1180     int         i = 0;
1181     pgc_t *     p_pgc;
1182 #define cell p_dvd->p_cur_pgc->cell_playback
1183     if( cell[p_dvd->i_cur_cell].block_type == BLOCK_TYPE_ANGLE_BLOCK )
1184     {
1185 #if 0
1186         p_dvd->i_next_cell = p_dvd->i_cur_cell + p_dvd->i_angle_nb;
1187         p_dvd->i_cur_cell += p_dvd->i_angle - 1;
1188 #else
1189         p_dvd->i_cur_cell += p_dvd->i_angle - 1;
1190
1191         while( cell[p_dvd->i_cur_cell+i].block_mode != BLOCK_MODE_LAST_CELL )
1192         {
1193             i++;
1194         }
1195         p_dvd->i_next_cell = p_dvd->i_cur_cell + i + 1;
1196 #endif
1197     }
1198     else
1199     {
1200         p_dvd->i_next_cell = p_dvd->i_cur_cell + 1;
1201     }
1202 #undef cell
1203     pgc_id = p_dvd->p_vts_file->vts_ptt_srpt->title[
1204                 p_dvd->i_ttn-1].ptt[p_dvd->i_chapter-1].pgcn;
1205     pgn = p_dvd->p_vts_file->vts_ptt_srpt->title[
1206                 p_dvd->i_ttn-1].ptt[p_dvd->i_chapter-1].pgn;
1207     p_pgc = p_dvd->p_vts_file->vts_pgcit->pgci_srp[pgc_id-1].pgc;
1208
1209     if( p_pgc->program_map[pgn-1] <= p_dvd->i_cur_cell )
1210     {
1211         p_dvd->i_chapter++;
1212         p_dvd->b_eoc = VLC_TRUE;
1213     }
1214 }
1215
1216 /*****************************************************************************
1217  * DvdReadLaunchDecoders
1218  *****************************************************************************/
1219 static void DvdReadLauchDecoders( input_thread_t * p_input )
1220 {
1221     thread_dvd_data_t * p_dvd = (thread_dvd_data_t*)(p_input->p_access_data);
1222     int i_audio, i_spu;
1223
1224     input_SelectES( p_input, p_input->stream.pp_es[0] );
1225
1226     /* For audio: first one if none or a not existing one specified */
1227     i_audio = config_GetInt( p_input, "audio-channel" );
1228     if( i_audio < 0 /*|| i_audio > i_audio_nb*/ )
1229     {
1230         config_PutInt( p_input, "audio-channel", 1 );
1231         i_audio = 1;
1232     }
1233     if( i_audio > 0/* && i_audio_nb > 0*/ )
1234     {
1235         if( config_GetInt( p_input, "audio-type" ) == REQUESTED_A52 )
1236         {
1237             int     i_a52 = i_audio;
1238             while( ( p_input->stream.pp_es[i_a52]->i_fourcc !=
1239                    VLC_FOURCC('a','5','2','b') ) && ( i_a52 <=
1240                    p_dvd->p_vts_file->vtsi_mat->nr_of_vts_audio_streams ) )
1241             {
1242                 i_a52++;
1243             }
1244             if( p_input->stream.pp_es[i_a52]->i_fourcc
1245                  == VLC_FOURCC('a','5','2','b') )
1246             {
1247                 input_SelectES( p_input, p_input->stream.pp_es[i_a52] );
1248             }
1249         }
1250         else
1251         {
1252             input_SelectES( p_input, p_input->stream.pp_es[i_audio] );
1253         }
1254     }
1255
1256     /* for spu, default is none */
1257     i_spu = config_GetInt( p_input, "spu-channel" );
1258     if( i_spu < 0 /*|| i_spu > i_spu_nb*/ )
1259     {
1260         config_PutInt( p_input, "spu-channel", 0 );
1261         i_spu = 0;
1262     }
1263     if( i_spu > 0 /*&& i_spu_nb > 0*/ )
1264     {
1265         i_spu += p_dvd->p_vts_file->vtsi_mat->nr_of_vts_audio_streams;
1266         input_SelectES( p_input, p_input->stream.pp_es[i_spu] );
1267     }
1268 }