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