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