]> git.sesse.net Git - vlc/blob - plugins/dvdread/input_dvdread.c
*Some fixes for dvdread plugin.
[vlc] / plugins / dvdread / input_dvdread.c
1 /*****************************************************************************
2  * input_dvdread.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 VideoLAN
9  * $Id: input_dvdread.c,v 1.13 2001/12/30 22:10:26 stef 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 <videolan/vlc.h>
38
39 #ifdef HAVE_UNISTD_H
40 #   include <unistd.h>
41 #endif
42
43 #include <fcntl.h>
44 #include <sys/types.h>
45 #include <string.h>
46 #include <errno.h>
47 #include <assert.h>
48
49 #ifdef STRNCASECMP_IN_STRINGS_H
50 #   include <strings.h>
51 #endif
52
53 #if defined( WIN32 )
54 #   include <io.h>                                                 /* read() */
55 #else
56 #   include <sys/uio.h>                                      /* struct iovec */
57 #endif
58
59 #if defined( WIN32 )
60 #   include "input_iovec.h"
61 #endif
62
63 #include "stream_control.h"
64 #include "input_ext-intf.h"
65 #include "input_ext-dec.h"
66 #include "input_ext-plugins.h"
67
68 #include "input_dvdread.h"
69
70 #include "iso_lang.h"
71
72 #include "debug.h"
73
74 /* how many blocks DVDRead will read in each loop */
75 #define DVD_BLOCK_READ_ONCE 64
76 #define DVD_DATA_READ_ONCE  (4 * DVD_BLOCK_READ_ONCE)
77
78 /*****************************************************************************
79  * Local prototypes
80  *****************************************************************************/
81 /* called from outside */
82 static int  DvdReadProbe    ( probedata_t *p_data );
83 static void DvdReadInit     ( struct input_thread_s * );
84 static void DvdReadEnd      ( struct input_thread_s * );
85 static void DvdReadOpen     ( struct input_thread_s * );
86 static void DvdReadClose    ( struct input_thread_s * );
87 static int  DvdReadSetArea  ( struct input_thread_s *, struct input_area_s * );
88 static int  DvdReadSetProgram( struct input_thread_s *, pgrm_descriptor_t * );
89 static int  DvdReadRead     ( struct input_thread_s *, data_packet_t ** );
90 static void DvdReadSeek     ( struct input_thread_s *, off_t );
91 static int  DvdReadRewind   ( struct input_thread_s * );
92
93 /* called only from here */
94 static void DvdReadHandleDSI( thread_dvd_data_t * p_dvd, u8 * p_data );
95 static void DvdReadFindCell ( thread_dvd_data_t * p_dvd );
96
97 /*****************************************************************************
98  * Declare a buffer manager
99  *****************************************************************************/
100 #define FLAGS           BUFFERS_UNIQUE_SIZE
101 #define NB_LIFO         1
102 DECLARE_BUFFERS_SHARED( FLAGS, NB_LIFO );
103 DECLARE_BUFFERS_INIT( FLAGS, NB_LIFO );
104 DECLARE_BUFFERS_END_SHARED( FLAGS, NB_LIFO );
105 DECLARE_BUFFERS_NEWPACKET_SHARED( FLAGS, NB_LIFO );
106 DECLARE_BUFFERS_DELETEPACKET_SHARED( FLAGS, NB_LIFO, 1000 );
107 DECLARE_BUFFERS_NEWPES( FLAGS, NB_LIFO );
108 DECLARE_BUFFERS_DELETEPES( FLAGS, NB_LIFO, 1000 );
109 DECLARE_BUFFERS_TOIO( FLAGS, DVD_VIDEO_LB_LEN );
110 DECLARE_BUFFERS_SHAREBUFFER( FLAGS );
111
112 /*****************************************************************************
113  * Functions exported as capabilities. They are declared as static so that
114  * we don't pollute the namespace too much.
115  *****************************************************************************/
116 void _M( input_getfunctions )( function_list_t * p_function_list )
117 {
118 #define input p_function_list->functions.input
119     p_function_list->pf_probe = DvdReadProbe;
120     input.pf_init             = DvdReadInit;
121     input.pf_open             = DvdReadOpen;
122     input.pf_close            = DvdReadClose;
123     input.pf_end              = DvdReadEnd;
124     input.pf_init_bit_stream  = InitBitstream;
125     input.pf_read             = DvdReadRead;
126     input.pf_set_area         = DvdReadSetArea;
127     input.pf_set_program      = DvdReadSetProgram;
128     input.pf_demux            = input_DemuxPS;
129     input.pf_new_packet       = input_NewPacket;
130     input.pf_new_pes          = input_NewPES;
131     input.pf_delete_packet    = input_DeletePacket;
132     input.pf_delete_pes       = input_DeletePES;
133     input.pf_rewind           = DvdReadRewind;
134     input.pf_seek             = DvdReadSeek;
135
136 #undef input
137 }
138
139 /*
140  * Data reading functions
141  */
142
143 /*****************************************************************************
144  * DvdReadProbe: verifies that the stream is a PS stream
145  *****************************************************************************/
146 static int DvdReadProbe( probedata_t *p_data )
147 {
148     input_thread_t * p_input = (input_thread_t *)p_data;
149
150     char * psz_name = p_input->p_source;
151     int i_score = 5;
152
153     if( ( strlen(psz_name) > 8 ) && !strncasecmp( psz_name, "dvdread:", 8 ) )
154     {
155         /* If the user specified "dvdread:" then he probably wants
156          * to use libdvdread */
157         i_score = 100;
158         psz_name += 8;
159     }
160
161     return( i_score );
162 }
163
164 /*****************************************************************************
165  * DvdReadInit: initializes DVD structures
166  *****************************************************************************/
167 static void DvdReadInit( input_thread_t * p_input )
168 {
169     thread_dvd_data_t *  p_dvd;
170     input_area_t *       p_area;
171     int                  i_title;
172     int                  i_chapter;
173     int                  i;
174
175     p_dvd = malloc( sizeof(thread_dvd_data_t) );
176     if( p_dvd == NULL )
177     {
178         intf_ErrMsg( "dvdread error: out of memory" );
179         p_input->b_error = 1;
180         return;
181     }
182
183     /* we take the pointer to dvd_reader_t back  */
184     p_dvd->p_dvdread = (dvd_reader_t *)p_input->p_plugin_data;
185     p_dvd->p_title = NULL;
186     p_dvd->p_vts_file = NULL;
187
188     p_input->p_plugin_data = (void *)p_dvd;
189
190     if( (p_input->p_method_data = input_BuffersInit()) == NULL )
191     {
192         free( p_dvd );
193         p_input->b_error = 1;
194         return;
195     }
196
197     /* We read DVD_BLOCK_READ_ONCE in each loop, so the input will receive
198      * DVD_DATA_READ_ONCE at most */
199     p_dvd->i_block_once = DVD_BLOCK_READ_ONCE;
200
201     /* Ifo allocation & initialisation */
202     if( ! ( p_dvd->p_vmg_file = ifoOpen( p_dvd->p_dvdread, 0 ) ) )
203     {
204         intf_ErrMsg( "dvdread error: can't open VMG info" );
205         input_BuffersEnd( p_input->p_method_data );
206         free( p_dvd );
207         p_input->b_error = 1;
208         return;
209     }
210     intf_WarnMsg( 2, "dvdread info: VMG opened" );
211
212     /* Set stream and area data */
213     vlc_mutex_lock( &p_input->stream.stream_lock );
214
215     /* Initialize ES structures */
216     input_InitStream( p_input, sizeof( stream_ps_data_t ) );
217
218     /* disc input method */
219     p_input->stream.i_method = INPUT_METHOD_DVD;
220
221 #define tt_srpt p_dvd->p_vmg_file->tt_srpt
222     intf_WarnMsg( 2, "dvdread info: number of titles: %d", tt_srpt->nr_of_srpts );
223
224 #define area p_input->stream.pp_areas
225     /* We start from 1 here since the default area 0
226      * is reserved for video_ts.vob */
227     for( i = 1 ; i <= tt_srpt->nr_of_srpts ; i++ )
228     {
229         input_AddArea( p_input );
230
231         /* Titles are Program Chains */
232         area[i]->i_id = i;
233
234         /* Absolute start offset and size
235          * We can only set that with vts ifo, so we do it during the
236          * first call to DVDSetArea */
237         area[i]->i_start = 0;
238         area[i]->i_size = 0;
239
240         /* Number of chapters */
241         area[i]->i_part_nb = tt_srpt->title[i-1].nr_of_ptts;
242         area[i]->i_part = 1;
243
244         /* Number of angles */
245         area[i]->i_angle_nb = 0;
246         area[i]->i_angle = 1;
247
248         area[i]->i_plugin_data = tt_srpt->title[i-1].title_set_nr;
249     }
250 #undef area
251
252     /* Get requested title - if none try the first title */
253     i_title = main_GetIntVariable( INPUT_TITLE_VAR, 1 );
254     if( i_title <= 0 || i_title > tt_srpt->nr_of_srpts )
255     {
256         i_title = 1;
257     }
258
259 #undef tt_srpt
260
261     /* Get requested chapter - if none defaults to first one */
262     i_chapter = main_GetIntVariable( INPUT_CHAPTER_VAR, 1 );
263     if( i_chapter <= 0 )
264     {
265         i_chapter = 1;
266     }
267
268     p_input->stream.pp_areas[i_title]->i_part = i_chapter;
269
270     p_area = p_input->stream.pp_areas[i_title];
271
272     /* set title, chapter, audio and subpic */
273     DvdReadSetArea( p_input, p_area );
274
275     vlc_mutex_unlock( &p_input->stream.stream_lock );
276
277     return;
278 }
279
280 /*****************************************************************************
281  * DvdReadOpen: open libdvdread
282  *****************************************************************************/
283 static void DvdReadOpen( struct input_thread_s *p_input )
284 {
285     dvd_reader_t    *p_dvdread;
286
287     vlc_mutex_lock( &p_input->stream.stream_lock );
288
289     /* If we are here we can control the pace... */
290     p_input->stream.b_pace_control = 1;
291
292     p_input->stream.b_seekable = 1;
293     p_input->stream.p_selected_area->i_size = 0;
294
295     p_input->stream.p_selected_area->i_tell = 0;
296
297     vlc_mutex_unlock( &p_input->stream.stream_lock );
298
299     /* XXX: put this shit in an access plugin */
300     if( strlen( p_input->p_source ) > 8
301          && !strncasecmp( p_input->p_source, "dvdread:", 8 ) )
302     {
303         p_dvdread = DVDOpen( p_input->p_source + 8 );
304     }
305     else
306     {
307         p_dvdread = DVDOpen( p_input->p_source );
308     }
309
310     if( ! p_dvdread )
311     {
312         p_input->b_error = 1;
313         return;
314     }
315
316     /* the vlc input doesn't handle the file descriptor with libdvdread */
317     p_input->i_handle = -1;
318
319     /* p_dvdread is stored in p_plugin_data until we have
320      * malloc'ed a dvd_thread_data_t */
321     p_input->p_plugin_data = (void *) p_dvdread;
322 }
323
324 /*****************************************************************************
325  * DvdReadClose: close libdvdread
326  *****************************************************************************/
327 static void DvdReadClose( struct input_thread_s *p_input )
328 {
329     DVDClose( ((thread_dvd_data_t*)p_input->p_plugin_data)->p_dvdread );
330     free( p_input->p_plugin_data );
331     p_input->p_plugin_data = NULL;
332
333 }
334
335 /*****************************************************************************
336  * DvdReadEnd: frees unused data
337  *****************************************************************************/
338 static void DvdReadEnd( input_thread_t * p_input )
339 {
340     thread_dvd_data_t *     p_dvd;
341
342     p_dvd = (thread_dvd_data_t *)p_input->p_plugin_data;
343
344     /* close libdvdread */
345     DVDCloseFile( p_dvd->p_title );
346     ifoClose( p_dvd->p_vts_file );
347     ifoClose( p_dvd->p_vmg_file );
348
349     input_BuffersEnd( p_input->p_method_data );
350 }
351
352 /*****************************************************************************
353  * DvdReadSetProgram: Does nothing, a DVD is mono-program
354  *****************************************************************************/
355 static int DvdReadSetProgram( input_thread_t * p_input,
356                               pgrm_descriptor_t * p_program )
357 {
358     return 0;
359 }
360
361 #define p_pgc         p_dvd->p_cur_pgc
362
363 /*****************************************************************************
364  * DvdReadSetArea: initialize input data for title x, chapter y.
365  * It should be called for each user navigation request.
366  *****************************************************************************
367  * Take care that i_title starts from 0 (vmg) and i_chapter start from 1.
368  * Note that you have to take the lock before entering here.
369  *****************************************************************************/
370 static int DvdReadSetArea( input_thread_t * p_input, input_area_t * p_area )
371 {
372     thread_dvd_data_t *  p_dvd;
373     int                  pgc_id = 0;
374     int                  pgn = 0;
375
376     p_dvd = (thread_dvd_data_t*)p_input->p_plugin_data;
377
378     /* we can't use the interface slider until initilization is complete */
379     p_input->stream.b_seekable = 0;
380
381     if( p_area != p_input->stream.p_selected_area )
382     {
383         es_descriptor_t *    p_es;
384         int                  i_cell = 0;
385         int                  i_audio_nb = 0;
386         int                  i_spu_nb = 0;
387         int                  i;
388
389 #define p_vmg         p_dvd->p_vmg_file
390 #define p_vts         p_dvd->p_vts_file
391         if( p_dvd->p_title != NULL )
392         {
393             DVDCloseFile( p_dvd->p_title );
394         }
395
396         if( p_vts != NULL )
397         {
398             ifoClose( p_vts );
399         }
400
401         /* Reset the Chapter position of the old title */
402         p_input->stream.p_selected_area->i_part = 1;
403
404         /*
405          *  We have to load all title information
406          */
407         /* Change the default area */
408         p_input->stream.p_selected_area = p_area;
409
410         intf_WarnMsg( 12, "dvdread: open VTS %d, for title %d",
411             p_vmg->tt_srpt->title[ p_area->i_id - 1 ].title_set_nr,
412             p_area->i_id );
413
414         /* ifo vts */
415         if( ! ( p_vts = ifoOpen( p_dvd->p_dvdread,
416                 p_vmg->tt_srpt->title[ p_area->i_id - 1 ].title_set_nr ) ) )
417         {
418             intf_ErrMsg( "dvdread error: fatal error in vts ifo" );
419             ifoClose( p_vmg );
420             DVDClose( p_dvd->p_dvdread );
421             p_input->b_error = 1;
422             return -1;
423         }
424
425         /* title position inside the selected vts */
426         p_dvd->i_ttn = p_vmg->tt_srpt->title[ p_area->i_id - 1 ].vts_ttn;
427
428         /*
429          * Set selected title start
430          */
431         pgc_id = p_vts->vts_ptt_srpt->title[p_dvd->i_ttn-1].ptt[0].pgcn;
432         pgn = p_vts->vts_ptt_srpt->title[p_dvd->i_ttn-1].ptt[0].pgn;
433         p_pgc = p_vts->vts_pgcit->pgci_srp[ pgc_id - 1 ].pgc;
434         i_cell = p_pgc->program_map[ pgn - 1 ] - 1;
435
436         p_area->i_start =
437             LB2OFF( p_dvd->p_cur_pgc->cell_playback[ i_cell ].first_sector );
438
439         intf_WarnMsg( 3, "dvdread: start %d vts_title %d pgc %d pgn %d",
440                          p_area->i_id, p_dvd->i_ttn, pgc_id, pgn );
441
442         /*
443          * Find title end
444          */
445         i_cell = p_dvd->p_cur_pgc->nr_of_cells - 1;
446
447         p_dvd->i_end_block = p_pgc->cell_playback[ i_cell ].last_sector;
448         p_area->i_size = LB2OFF( p_dvd->i_end_block )- p_area->i_start;
449
450         intf_WarnMsg( 12, "dvdread: start %lld size %lld end %d",
451                           p_area->i_start , p_area->i_size, p_dvd->i_end_block );
452
453         /*
454          * Set properties for current chapter
455          */
456         /* Remeber current chapter */
457         p_dvd->i_chapter = p_area->i_part;
458         p_dvd->b_eoc = 0;
459
460         pgc_id = p_vts->vts_ptt_srpt->title[
461                     p_dvd->i_ttn-1].ptt[p_area->i_part-1].pgcn;
462         pgn = p_vts->vts_ptt_srpt->title[
463                     p_dvd->i_ttn-1].ptt[p_area->i_part-1].pgn;
464
465         p_pgc = p_vts->vts_pgcit->pgci_srp[pgc_id-1].pgc;
466         p_dvd->i_pack_len = 0;
467         p_dvd->i_next_cell = p_dvd->i_cur_cell = p_pgc->program_map[pgn-1] - 1;
468         DvdReadFindCell( p_dvd );
469
470         p_dvd->i_next_vobu = p_dvd->i_cur_block =
471             p_pgc->cell_playback[p_dvd->i_cur_cell].first_sector;
472
473         /*
474          * Angle management
475          */
476         p_area->i_angle_nb = p_vmg->tt_srpt->title[p_area->i_id-1].nr_of_angles;
477         p_area->i_angle = main_GetIntVariable( INPUT_ANGLE_VAR, 1 );
478
479         if( ( p_area->i_angle <= 0 ) || p_area->i_angle > p_area->i_angle_nb )
480         {
481             p_area->i_angle = 1;
482         }
483         p_dvd->i_angle = p_area->i_angle;
484         p_dvd->i_angle_nb = p_area->i_angle_nb;
485
486         /*
487          * We've got enough info, time to open the title set data.
488          */
489         if( ! ( p_dvd->p_title = DVDOpenFile( p_dvd->p_dvdread,
490             p_vmg->tt_srpt->title[ p_area->i_id - 1 ].title_set_nr,
491             DVD_READ_TITLE_VOBS ) ) )
492         {
493             intf_ErrMsg( "dvdread error: can't open title (VTS_%02d_1.VOB)",
494                          p_vmg->tt_srpt->title[p_area->i_id-1].title_set_nr );
495             ifoClose( p_vts );
496             ifoClose( p_vmg );
497             DVDClose( p_dvd->p_dvdread );
498             return -1;
499         }
500
501 //        IfoPrintTitle( p_dvd );
502
503         /*
504          * Destroy obsolete ES by reinitializing program 0
505          * and find all ES in title with ifo data
506          */
507         if( p_input->stream.pp_programs != NULL )
508         {
509             /* We don't use input_EndStream here since
510              * we keep area structures */
511
512             for( i = 0 ; i < p_input->stream.i_selected_es_number ; i++ )
513             {
514                 input_UnselectES( p_input, p_input->stream.pp_selected_es[i] );
515             }
516
517             free( p_input->stream.pp_selected_es );
518             input_DelProgram( p_input, p_input->stream.p_selected_program );
519
520             p_input->stream.pp_selected_es = NULL;
521             p_input->stream.i_selected_es_number = 0;
522         }
523
524         input_AddProgram( p_input, 0, sizeof( stream_ps_data_t ) );
525         p_input->stream.p_selected_program = p_input->stream.pp_programs[0];
526
527         /* No PSM to read in DVD mode, we already have all information */
528         p_input->stream.p_selected_program->b_is_ok = 1;
529
530         p_es = NULL;
531
532         /* ES 0 -> video MPEG2 */
533 //        IfoPrintVideo( p_dvd );
534
535         p_es = input_AddES( p_input, p_input->stream.p_selected_program, 0xe0, 0 );
536         p_es->i_stream_id = 0xe0;
537         p_es->i_type = MPEG2_VIDEO_ES;
538         p_es->i_cat = VIDEO_ES;
539         if( p_main->b_video )
540         {
541             input_SelectES( p_input, p_es );
542         }
543
544 #define audio_control \
545     p_dvd->p_vts_file->vts_pgcit->pgci_srp[pgc_id-1].pgc->audio_control[i-1]
546         /* Audio ES, in the order they appear in .ifo */
547         for( i = 1 ; i <= p_vts->vtsi_mat->nr_of_vts_audio_streams ; i++ )
548         {
549             int i_position = 0;
550             u16 i_id;
551
552 //            IfoPrintAudio( p_dvd, i );
553
554             /* audio channel is active if first byte is 0x80 */
555             if( audio_control & 0x8000 )
556             {
557                 i_audio_nb++;
558                 i_position = ( audio_control & 0x7F00 ) >> 8;
559
560             intf_WarnMsg( 12, "dvd audio position  %d", i_position );
561                 switch( p_vts->vtsi_mat->vts_audio_attr[i-1].audio_format )
562                 {
563                 case 0x00:              /* AC3 */
564                     i_id = ( ( 0x80 + i_position ) << 8 ) | 0xbd;
565                     p_es = input_AddES( p_input,
566                                p_input->stream.p_selected_program, i_id, 0 );
567                     p_es->i_stream_id = 0xbd;
568                     p_es->i_type = AC3_AUDIO_ES;
569                     p_es->b_audio = 1;
570                     p_es->i_cat = AUDIO_ES;
571                     strcpy( p_es->psz_desc, DecodeLanguage( hton16(
572                         p_vts->vtsi_mat->vts_audio_attr[i-1].lang_code ) ) ); 
573                     strcat( p_es->psz_desc, " (ac3)" );
574
575                     break;
576                 case 0x02:
577                 case 0x03:              /* MPEG audio */
578                     i_id = 0xc0 + i_position;
579                     p_es = input_AddES( p_input,
580                                     p_input->stream.p_selected_program, i_id, 0 );
581                     p_es->i_stream_id = i_id;
582                     p_es->i_type = MPEG2_AUDIO_ES;
583                     p_es->b_audio = 1;
584                     p_es->i_cat = AUDIO_ES;
585                     strcpy( p_es->psz_desc, DecodeLanguage( hton16(
586                         p_vts->vtsi_mat->vts_audio_attr[i-1].lang_code ) ) ); 
587                     strcat( p_es->psz_desc, " (mpeg)" );
588
589                     break;
590                 case 0x04:              /* LPCM */
591
592                     i_id = ( ( 0xa0 + i_position ) << 8 ) | 0xbd;
593                     p_es = input_AddES( p_input,
594                                     p_input->stream.p_selected_program, i_id, 0 );
595                     p_es->i_stream_id = i_id;
596                     p_es->i_type = LPCM_AUDIO_ES;
597                     p_es->b_audio = 1;
598                     p_es->i_cat = AUDIO_ES;
599                     strcpy( p_es->psz_desc, DecodeLanguage( hton16(
600                         p_vts->vtsi_mat->vts_audio_attr[i-1].lang_code ) ) ); 
601                     strcat( p_es->psz_desc, " (lpcm)" );
602
603                     break;
604                 case 0x06:              /* DTS */
605                     i_id = ( ( 0x88 + i_position ) << 8 ) | 0xbd;
606                     intf_ErrMsg( "dvd warning: DTS audio not handled yet"
607                                  "(0x%x)", i_id );
608                     break;
609                 default:
610                     i_id = 0;
611                     intf_ErrMsg( "dvd warning: unknown audio type %.2x",
612                              p_vts->vtsi_mat->vts_audio_attr[i-1].audio_format );
613                 }
614             }
615         }
616 #undef audio_control
617 #define spu_control \
618     p_dvd->p_vts_file->vts_pgcit->pgci_srp[pgc_id-1].pgc->subp_control[i-1]
619
620         /* Sub Picture ES */
621
622         for( i = 1 ; i <= p_vts->vtsi_mat->nr_of_vts_subp_streams; i++ )
623         {
624             int i_position = 0;
625             u16 i_id;
626
627 //            IfoPrintSpu( p_dvd, i );
628             intf_WarnMsg( 12, "dvd spu %d 0x%02x", i, spu_control );
629
630             if( spu_control & 0x80000000 )
631             {
632                 i_spu_nb++;
633
634                 /*  there are several streams for one spu */
635                 if(  p_vts->vtsi_mat->vts_video_attr.display_aspect_ratio )
636                 {
637                     /* 16:9 */
638                     switch( p_vts->vtsi_mat->vts_video_attr.permitted_df )
639                     {
640                     case 1:
641                         i_position = spu_control & 0xff;
642                         break;
643                     case 2:
644                         i_position = ( spu_control >> 8 ) & 0xff;
645                         break;
646                     default:
647                         i_position = ( spu_control >> 16 ) & 0xff;
648                         break;
649                     }
650                 }
651                 else
652                 {
653                     /* 4:3 */
654                     i_position = ( spu_control >> 24 ) & 0x7F;
655                 }
656
657                 i_id = ( ( 0x20 + i_position ) << 8 ) | 0xbd;
658                 p_es = input_AddES( p_input,
659                                     p_input->stream.p_selected_program, i_id, 0 );
660                 p_es->i_stream_id = 0xbd;
661                 p_es->i_type = DVD_SPU_ES;
662                 p_es->i_cat = SPU_ES;
663                 strcpy( p_es->psz_desc, DecodeLanguage( hton16(
664                     p_vts->vtsi_mat->vts_subp_attr[i-1].lang_code ) ) ); 
665             }
666         }
667 #undef spu_control
668         if( p_main->b_audio )
669         {
670             /* For audio: first one if none or a not existing one specified */
671             int i_audio = main_GetIntVariable( INPUT_CHANNEL_VAR, 1 );
672             if( i_audio < 0 || i_audio > i_audio_nb )
673             {
674                 main_PutIntVariable( INPUT_CHANNEL_VAR, 1 );
675                 i_audio = 1;
676             }
677             if( i_audio > 0 && i_audio_nb > 0 )
678             {
679                 if( main_GetIntVariable( AOUT_SPDIF_VAR, 0 ) ||
680                     ( main_GetIntVariable( INPUT_AUDIO_VAR, 0 ) ==
681                       REQUESTED_AC3 ) )
682                 {
683                     int     i_ac3 = i_audio;
684                     while( ( p_input->stream.pp_es[i_ac3]->i_type !=
685                              AC3_AUDIO_ES ) && ( i_ac3 <=
686                              p_vts->vtsi_mat->nr_of_vts_audio_streams ) )
687                     {
688                         i_ac3++;
689                     }
690                     if( p_input->stream.pp_es[i_ac3]->i_type == AC3_AUDIO_ES )
691                     {
692                         input_SelectES( p_input,
693                                         p_input->stream.pp_es[i_ac3] );
694                     }
695                 }
696                 else
697                 {
698                     input_SelectES( p_input,
699                                     p_input->stream.pp_es[i_audio] );
700                 }
701             }
702         }
703
704         if( p_main->b_video )
705         {
706             /* for spu, default is none */
707             int i_spu = main_GetIntVariable( INPUT_SUBTITLE_VAR, 0 );
708             if( i_spu < 0 || i_spu > i_spu_nb )
709             {
710                 main_PutIntVariable( INPUT_SUBTITLE_VAR, 0 );
711                 i_spu = 0;
712             }
713             if( i_spu > 0 && i_spu_nb > 0 )
714             {
715                 i_spu += p_vts->vtsi_mat->nr_of_vts_audio_streams;
716                 input_SelectES( p_input, p_input->stream.pp_es[i_spu] );
717             }
718         }
719     } /* i_title >= 0 */
720     else
721     {
722         p_area = p_input->stream.p_selected_area;
723     }
724
725     /*
726      * Chapter selection
727      */
728
729     if( p_area->i_part != p_dvd->i_chapter )
730     {
731         if( ( p_area->i_part > 0 ) &&
732             ( p_area->i_part <= p_area->i_part_nb ))
733         {
734             p_dvd->i_ttn = p_vmg->tt_srpt->title[p_area->i_id-1].vts_ttn;
735             pgc_id = p_vts->vts_ptt_srpt->title[
736                         p_dvd->i_ttn-1].ptt[p_area->i_part-1].pgcn;
737             pgn = p_vts->vts_ptt_srpt->title[
738                         p_dvd->i_ttn-1].ptt[p_area->i_part-1].pgn;
739
740             p_pgc = p_vts->vts_pgcit->pgci_srp[ pgc_id - 1 ].pgc;
741
742             p_dvd->i_cur_cell = p_pgc->program_map[ pgn - 1 ] - 1;
743             p_dvd->i_chapter = p_area->i_part;
744             DvdReadFindCell( p_dvd );
745
746             p_dvd->i_pack_len = 0;
747             p_dvd->i_next_vobu = p_dvd->i_cur_block =
748                     p_pgc->cell_playback[p_dvd->i_cur_cell].first_sector;
749         }
750         else
751         {
752             p_area->i_part = p_dvd->i_chapter;
753         }
754     }
755 #undef p_vts
756 #undef p_vmg
757
758     if( p_area->i_angle != p_dvd->i_angle )
759     {
760         p_dvd->i_angle = p_area->i_angle;
761
762         intf_WarnMsg( 3, "dvd info: angle %d selected", p_area->i_angle );
763     }
764     /* warn interface that something has changed */
765     p_area->i_tell = LB2OFF( p_dvd->i_next_vobu ) - p_area->i_start;
766     p_input->stream.b_seekable = 1;
767     p_input->stream.b_changed = 1;
768
769     return 0;
770 }
771
772
773 /*****************************************************************************
774  * DvdReadRead: reads data packets into the netlist.
775  *****************************************************************************
776  * Returns -1 in case of error, 0 if everything went well, and 1 in case of
777  * EOF.
778  *****************************************************************************/
779 static int DvdReadRead( input_thread_t * p_input,
780                         data_packet_t ** pp_data )
781 {
782     thread_dvd_data_t *     p_dvd;
783     u8                      p_data[DVD_VIDEO_LB_LEN];
784     struct iovec            p_vec[DVD_DATA_READ_ONCE];
785     u8 *                    pi_cur;
786     int                     i_blocks;
787     int                     i_read;
788     int                     i_iovec;
789     int                     i_packet_size;
790     int                     i_packet;
791     int                     i_pos;
792     data_packet_t *         p_data_p;
793     boolean_t               b_eot = 0;
794
795     p_dvd = (thread_dvd_data_t *)p_input->p_plugin_data;
796
797     *pp_data = NULL;
798
799     /*
800      * Playback by cell in this pgc, starting at the cell for our chapter.
801      */
802
803     /* 
804      * End of pack, we select the following one
805      */
806     if( ! p_dvd->i_pack_len )
807     {
808         /*
809          * Read NAV packet.
810          */
811         if( ( i_read = DVDReadBlocks( p_dvd->p_title, p_dvd->i_next_vobu,
812                        1, p_data ) ) != 1 )
813         {
814             intf_ErrMsg( "dvdread error: read failed for block %d",
815                          p_dvd->i_next_vobu );
816             return -1;
817         }
818
819         /* basic check to be sure we don't have a empty title
820          * go to next title if so */
821         assert( p_data[41] == 0xbf && p_data[1027] == 0xbf );
822         if( p_data[41] == 0xbf && p_data[1027] == 0xbf )
823         {
824             /*
825              * Parse the contained dsi packet.
826              */
827
828             DvdReadHandleDSI( p_dvd, p_data );
829
830             /* End of File */
831             if( p_dvd->i_next_vobu >= p_dvd->i_end_block + 1 )
832             {
833                 return 1;
834             }
835
836             assert( p_dvd->i_pack_len < 1024 );
837             /* FIXME: Ugly kludge: we send the pack block to the input for it
838              * sometimes has a zero scr and restart the sync */
839             //p_dvd->i_cur_block ++;
840             p_dvd->i_pack_len++;
841         }
842         else
843         {
844             b_eot = 1;
845             p_dvd->i_pack_len = 0;
846             return 1;
847         }
848     }
849
850     /*
851      * Compute the number of blocks to read
852      */
853     i_blocks = p_dvd->i_pack_len >= DVD_BLOCK_READ_ONCE
854              ? DVD_BLOCK_READ_ONCE : p_dvd->i_pack_len;
855     p_dvd->i_pack_len -= i_blocks;
856
857     /* Get iovecs */
858     *pp_data = p_data_p = input_BuffersToIO( p_input->p_method_data, p_vec,
859                                              DVD_DATA_READ_ONCE );
860
861     if ( p_data_p == NULL )
862     {
863         return( -1 );
864     }
865
866     /* Reads from DVD */
867     i_read = DVDReadVBlocks( p_dvd->p_title, p_dvd->i_cur_block,
868                              i_blocks, p_vec );
869     if( i_read != i_blocks )
870     {
871         intf_ErrMsg( "dvdread error: read failed for %d/%d blocks at 0x%02x",
872                      i_read, i_blocks, p_dvd->i_cur_block );
873         return -1;
874     }
875
876     p_dvd->i_cur_block += i_read;
877 /*
878     intf_WarnMsg( 12, "dvdread i_blocks: %d len: %d current: 0x%02x", i_read, p_dvd->i_pack_len, p_dvd->i_cur_block );
879 */
880     i_packet = 0;
881
882     /* Read headers to compute payload length */
883     for( i_iovec = 0 ; i_iovec < i_read ; i_iovec++ )
884     {
885         data_packet_t * p_current = p_data_p;
886         i_pos = 0;
887
888         while( i_pos < DVD_VIDEO_LB_LEN )
889         {
890             pi_cur = (u8*)p_vec[i_iovec].iov_base + i_pos;
891
892             /*default header */
893             if( U32_AT( pi_cur ) != 0x1BA )
894             {
895                 /* That's the case for all packets, except pack header. */
896                 i_packet_size = U16_AT( pi_cur + 4 );
897             }
898             else
899             {
900                 /* MPEG-2 Pack header. */
901                 i_packet_size = 8;
902             }
903             if( i_pos != 0 )
904             {
905                 *pp_data = input_ShareBuffer( p_input->p_method_data,
906                                               p_current );
907             }
908             else
909             {
910                 *pp_data = p_data_p;
911                 p_data_p = p_data_p->p_next;
912             }
913
914             (*pp_data)->p_payload_start = (*pp_data)->p_demux_start =
915                     (*pp_data)->p_demux_start + i_pos;
916             
917
918             (*pp_data)->p_payload_end =
919                     (*pp_data)->p_payload_start + i_packet_size + 6;
920
921 //            pp_packets[i_packet]->p_next = NULL;
922 //            pp_packets[i_packet]->b_discard_payload = 0;
923
924             i_packet++;
925             i_pos += i_packet_size + 6;
926             pp_data = &(*pp_data)->p_next;
927         }
928     }
929
930     p_input->pf_delete_packet( p_input->p_method_data, p_data_p );
931     *pp_data = NULL;
932
933     while( p_data_p != NULL )
934     {
935         data_packet_t * p_next = p_data_p->p_next;
936         p_input->pf_delete_packet( p_input->p_method_data, p_data_p );
937         p_data_p = p_next;
938     }
939
940     vlc_mutex_lock( &p_input->stream.stream_lock );
941
942     p_input->stream.p_selected_area->i_tell =
943         LB2OFF( p_dvd->i_cur_block ) -
944             p_input->stream.p_selected_area->i_start;
945
946     if( p_dvd->b_eoc )
947     {
948         /* We modify i_part only at end of chapter not to erase
949          * some modification from the interface */
950         p_input->stream.p_selected_area->i_part = p_dvd->i_chapter;
951         p_dvd->b_eoc = 0;
952     }
953     
954     if( p_input->stream.p_selected_area->i_tell
955             >= p_input->stream.p_selected_area->i_size || b_eot )
956     {
957         if( ( p_input->stream.p_selected_area->i_id + 1 ) >= 
958                         p_input->stream.i_area_nb )
959         {
960             /* EOF */
961             vlc_mutex_unlock( &p_input->stream.stream_lock );
962             return 1;
963         }
964
965         /* EOT */
966         intf_WarnMsg( 4, "dvd info: new title" );
967         DvdReadSetArea( p_input, p_input->stream.pp_areas[
968                         p_input->stream.p_selected_area->i_id+1] );
969         vlc_mutex_unlock( &p_input->stream.stream_lock );
970         return 0;
971     }
972
973     vlc_mutex_unlock( &p_input->stream.stream_lock );
974
975     return 0;
976 }
977 #undef p_pgc
978
979 /*****************************************************************************
980  * DVDRewind : reads a stream backward
981  *****************************************************************************/
982 static int DvdReadRewind( input_thread_t * p_input )
983 {
984     return( -1 );
985 }
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     int                     i_lb;
998     int                     i_tmp;
999     int                     i_chapter = 0;
1000     int                     i_cell = 0;
1001     int                     i_vobu = 0;
1002     int                     i_sub_cell = 0;
1003
1004     i_off += p_input->stream.p_selected_area->i_start;
1005     i_lb = OFF2LB( i_off );
1006     p_dvd = ( thread_dvd_data_t * )p_input->p_plugin_data;
1007
1008     /* find cell */
1009     while( p_dvd->p_cur_pgc->cell_playback[i_cell].last_sector < i_lb )
1010     {
1011         i_cell++;
1012     }
1013
1014     /* find chapter */
1015     do
1016     {
1017         pgc_t *     p_pgc;
1018         int         pgc_id, pgn;
1019
1020         i_chapter++;
1021         pgc_id = p_dvd->p_vts_file->vts_ptt_srpt->title[
1022                     p_dvd->i_ttn-1].ptt[i_chapter-1].pgcn;
1023         pgn = p_dvd->p_vts_file->vts_ptt_srpt->title[
1024                     p_dvd->i_ttn-1].ptt[i_chapter-1].pgn;
1025
1026         p_pgc = p_dvd->p_vts_file->vts_pgcit->pgci_srp[pgc_id-1].pgc;
1027         i_tmp = p_pgc->program_map[pgn-1];
1028
1029     } while( i_tmp <= i_cell );
1030
1031     /* find vobu */
1032     while( p_dvd->p_vts_file->vts_vobu_admap->vobu_start_sectors[i_vobu]
1033             <= i_lb )
1034     {
1035         i_vobu++;
1036     }
1037
1038     /* find sub_cell */
1039     while( p_dvd->p_vts_file->vts_c_adt->cell_adr_table[i_sub_cell].start_sector <
1040             p_dvd->p_vts_file->vts_vobu_admap->vobu_start_sectors[i_vobu-1] )
1041     {
1042         i_sub_cell++;
1043     }
1044
1045 /*
1046     intf_WarnMsg(12, "cell %d i_sub_cell %d chapter %d vobu %d cell_sector %d vobu_sector %d sub_cell_sector %d",
1047             i_cell, i_sub_cell,i_chapter, i_vobu,
1048             p_dvd->p_cur_pgc->cell_playback[i_cell].first_sector,
1049             p_dvd->p_vts_file->vts_vobu_admap->vobu_start_sectors[i_vobu],
1050             p_dvd->p_vts_file->vts_c_adt->cell_adr_table[i_sub_cell-1].start_sector);
1051 */
1052     p_dvd->i_cur_block = i_lb;
1053     p_dvd->i_next_vobu =
1054         p_dvd->p_vts_file->vts_vobu_admap->vobu_start_sectors[i_vobu];
1055     p_dvd->i_pack_len = p_dvd->i_next_vobu - i_lb;
1056     p_dvd->i_cur_cell = i_cell;
1057     p_dvd->i_chapter = i_chapter;
1058     DvdReadFindCell( p_dvd );
1059
1060     p_input->stream.p_selected_area->i_tell =
1061         LB2OFF ( p_dvd->i_cur_block )
1062          - p_input->stream.p_selected_area->i_start;
1063     p_input->stream.p_selected_area->i_part = p_dvd->i_chapter;
1064
1065     return;
1066 }
1067
1068 /*****************************************************************************
1069  * DvdReadHandleDSI
1070  *****************************************************************************/
1071 static void DvdReadHandleDSI( thread_dvd_data_t * p_dvd, u8 * p_data )
1072 {
1073     navRead_DSI( &(p_dvd->dsi_pack), &(p_data[ DSI_START_BYTE ]) );
1074
1075     /*
1076      * Determine where we go next.  These values are the ones we mostly
1077      * care about.
1078      */
1079     p_dvd->i_cur_block = p_dvd->dsi_pack.dsi_gi.nv_pck_lbn;
1080
1081     /*
1082      * If we're not at the end of this cell, we can determine the next
1083      * VOBU to display using the VOBU_SRI information section of the
1084      * DSI.  Using this value correctly follows the current angle,
1085      * avoiding the doubled scenes in The Matrix, and makes our life
1086      * really happy.
1087      */
1088     if( p_dvd->dsi_pack.vobu_sri.next_vobu != SRI_END_OF_CELL )
1089     {
1090 #if 1
1091         switch( ( p_dvd->dsi_pack.sml_pbi.category & 0xf000 ) >> 12 )
1092         {
1093             case 0x4:
1094                 /* interleaved unit with no angle */
1095                 if( p_dvd->dsi_pack.sml_pbi.ilvu_sa != -1 )
1096                 {
1097                     p_dvd->i_next_vobu = p_dvd->i_cur_block +
1098                         p_dvd->dsi_pack.sml_pbi.ilvu_sa;
1099                     p_dvd->i_pack_len = p_dvd->dsi_pack.sml_pbi.ilvu_ea;
1100                 }
1101                 else
1102                 {
1103                     p_dvd->i_next_vobu = p_dvd->i_cur_block +
1104                         p_dvd->dsi_pack.dsi_gi.vobu_ea + 1;
1105                     p_dvd->i_pack_len = p_dvd->dsi_pack.dsi_gi.vobu_ea;
1106                 }
1107                 break;
1108             case 0x5:
1109                 /* vobu is end of ilvu */
1110                 if( p_dvd->dsi_pack.sml_agli.data[p_dvd->i_angle-1].address )
1111                 {
1112                     p_dvd->i_next_vobu = p_dvd->i_cur_block +
1113                         p_dvd->dsi_pack.sml_agli.data[p_dvd->i_angle-1].address;
1114                     p_dvd->i_pack_len = p_dvd->dsi_pack.sml_pbi.ilvu_ea;
1115
1116                     break;
1117                 }
1118             case 0x6:
1119                 /* vobu is beginning of ilvu */
1120             case 0x9:
1121                 /* next scr is 0 */
1122             case 0xa:
1123                 /* entering interleaved section */
1124             case 0x8:
1125                 /* non interleaved cells in interleaved section */
1126             default:
1127                 p_dvd->i_next_vobu = p_dvd->i_cur_block +
1128                     ( p_dvd->dsi_pack.vobu_sri.next_vobu & 0x7fffffff );
1129                 p_dvd->i_pack_len = p_dvd->dsi_pack.dsi_gi.vobu_ea;
1130                 break;
1131         }
1132 #else
1133         p_dvd->i_next_vobu = p_dvd->i_cur_block +
1134             ( p_dvd->dsi_pack.vobu_sri.next_vobu & 0x7fffffff );
1135         p_dvd->i_pack_len = p_dvd->dsi_pack.dsi_gi.vobu_ea;
1136 #endif
1137     }
1138     else
1139     {
1140         p_dvd->i_cur_cell = p_dvd->i_next_cell;
1141         DvdReadFindCell( p_dvd );
1142
1143         p_dvd->i_pack_len = p_dvd->dsi_pack.dsi_gi.vobu_ea;
1144         p_dvd->i_next_vobu =
1145             p_dvd->p_cur_pgc->cell_playback[p_dvd->i_cur_cell].first_sector;
1146     }
1147
1148 #if 0
1149     intf_WarnMsg( 12, "scr %d lbn 0x%02x vobu_ea %d vob_id %d c_id %d",
1150             p_dvd->dsi_pack.dsi_gi.nv_pck_scr,
1151             p_dvd->dsi_pack.dsi_gi.nv_pck_lbn,
1152             p_dvd->dsi_pack.dsi_gi.vobu_ea,
1153             p_dvd->dsi_pack.dsi_gi.vobu_vob_idn,
1154             p_dvd->dsi_pack.dsi_gi.vobu_c_idn );
1155
1156     intf_WarnMsg( 12, "cat 0x%02x ilvu_ea %d ilvu_sa %d size %d", 
1157             p_dvd->dsi_pack.sml_pbi.category,
1158             p_dvd->dsi_pack.sml_pbi.ilvu_ea,
1159             p_dvd->dsi_pack.sml_pbi.ilvu_sa,
1160             p_dvd->dsi_pack.sml_pbi.size );
1161
1162     intf_WarnMsg( 12, "next_vobu %d next_ilvu1 %d next_ilvu2 %d",
1163             p_dvd->dsi_pack.vobu_sri.next_vobu & 0x7fffffff,
1164             p_dvd->dsi_pack.sml_agli.data[ p_dvd->i_angle - 1 ].address,
1165             p_dvd->dsi_pack.sml_agli.data[ p_dvd->i_angle ].address);
1166 #endif
1167 }
1168
1169 /*****************************************************************************
1170  * DvdReadFindCell
1171  *****************************************************************************/
1172 static void DvdReadFindCell( thread_dvd_data_t * p_dvd )
1173 {
1174     int         pgc_id, pgn;
1175     int         i = 0;
1176     pgc_t *     p_pgc;
1177 #define cell p_dvd->p_cur_pgc->cell_playback
1178     if( cell[p_dvd->i_cur_cell].block_type == BLOCK_TYPE_ANGLE_BLOCK )
1179     {
1180 #if 0
1181         p_dvd->i_next_cell = p_dvd->i_cur_cell + p_dvd->i_angle_nb;
1182         p_dvd->i_cur_cell += p_dvd->i_angle - 1;
1183 #else
1184         p_dvd->i_cur_cell += p_dvd->i_angle - 1;
1185
1186         while( cell[p_dvd->i_cur_cell+i].block_mode != BLOCK_MODE_LAST_CELL )
1187         {
1188             i++;
1189         }
1190         p_dvd->i_next_cell = p_dvd->i_cur_cell + i + 1;
1191 #endif
1192     }
1193     else
1194     {
1195         p_dvd->i_next_cell = p_dvd->i_cur_cell + 1;
1196     }
1197 #undef cell
1198     pgc_id = p_dvd->p_vts_file->vts_ptt_srpt->title[
1199                 p_dvd->i_ttn-1].ptt[p_dvd->i_chapter-1].pgcn;
1200     pgn = p_dvd->p_vts_file->vts_ptt_srpt->title[
1201                 p_dvd->i_ttn-1].ptt[p_dvd->i_chapter-1].pgn;
1202     p_pgc = p_dvd->p_vts_file->vts_pgcit->pgci_srp[pgc_id-1].pgc;
1203
1204     if( p_pgc->program_map[pgn-1] <= p_dvd->i_cur_cell )
1205     {
1206         p_dvd->i_chapter++;
1207         p_dvd->b_eoc = 1;
1208     }
1209 }