]> git.sesse.net Git - vlc/blob - plugins/dvd/input_dvd.c
-Change in the code to detect beginning of movie. It is much better now
[vlc] / plugins / dvd / input_dvd.c
1 /*****************************************************************************
2  * input_dvd.c: DVD raw reading 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:
7  *  -input_netlist used to read packets
8  *  -dvd_ifo for ifo parsing and analyse
9  *  -dvd_css for unscrambling
10  *  -dvd_udf to find files
11  *****************************************************************************
12  * Copyright (C) 1998-2001 VideoLAN
13  * $Id: input_dvd.c,v 1.14 2001/02/19 03:12:26 stef Exp $
14  *
15  * Author: Stéphane Borel <stef@via.ecp.fr>
16  *
17  * This program is free software; you can redistribute it and/or modify
18  * it under the terms of the GNU General Public License as published by
19  * the Free Software Foundation; either version 2 of the License, or
20  * (at your option) any later version.
21  * 
22  * This program is distributed in the hope that it will be useful,
23  * but WITHOUT ANY WARRANTY; without even the implied warranty of
24  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25  * GNU General Public License for more details.
26  *
27  * You should have received a copy of the GNU General Public License
28  * along with this program; if not, write to the Free Software
29  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
30  *****************************************************************************/
31
32 /*****************************************************************************
33  * Preamble
34  *****************************************************************************/
35 #include "defs.h"
36
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <unistd.h>
40 #include <netinet/in.h>
41
42 #include <fcntl.h>
43 #include <sys/types.h>
44 #include <sys/uio.h>
45
46 #include <string.h>
47 #include <errno.h>
48 #include <malloc.h>
49
50 #include "config.h"
51 #include "common.h"
52 #include "threads.h"
53 #include "mtime.h"
54 #include "tests.h"
55
56 #include "intf_msg.h"
57
58 #include "main.h"
59
60 #include "stream_control.h"
61 #include "input_ext-intf.h"
62 #include "input_ext-dec.h"
63
64 #include "input.h"
65 #include "input_netlist.h"
66
67 #include "dvd_ifo.h"
68 #include "dvd_css.h"
69 #include "input_dvd.h"
70 #include "mpeg_system.h"
71
72 #include "debug.h"
73
74 #include "modules.h"
75
76 /*****************************************************************************
77  * Local tables
78  *****************************************************************************/
79 static struct
80 {
81         char    p_code[3];
82     char    p_lang_long[20];
83 } lang_tbl[] =
84     {
85
86     /* The ISO 639 language codes.
87      * Language names with * prefix are not spelled in their own language 
88      */
89         {"  ", "Not Specified"},
90         {"aa", "*Afar"},
91         {"ab", "*Abkhazian"},
92         {"af", "*Afrikaans"},
93         {"am", "*Amharic"},
94         {"ar", "*Arabic"},
95         {"as", "*Assamese"},
96         {"ay", "*Aymara"},
97         {"az", "*Azerbaijani"},
98         {"ba", "*Bashkir"},
99         {"be", "*Byelorussian"},
100         {"bg", "*Bulgarian"},
101         {"bh", "*Bihari"},
102         {"bi", "*Bislama"},
103         {"bn", "*Bengali; Bangla"},
104         {"bo", "*Tibetan"},
105         {"br", "*Breton"},
106         {"ca", "*Catalan"},
107         {"co", "*Corsican"},
108         {"cs", "*Czech(Ceske)"},
109         {"cy", "*Welsh"},
110         {"da", "Dansk"},
111         {"de", "Deutsch"},
112         {"dz", "*Bhutani"},
113         {"el", "*Greek"},
114         {"en", "English"},
115         {"eo", "*Esperanto"},
116         {"es", "Espanol"},
117         {"et", "*Estonian"},
118         {"eu", "*Basque"},
119         {"fa", "*Persian"},
120         {"fi", "Suomi"},
121         {"fj", "*Fiji"},
122         {"fo", "*Faroese"},
123         {"fr", "Francais"},
124         {"fy", "*Frisian"},
125         {"ga", "*Irish"},
126         {"gd", "*Scots Gaelic"},
127         {"gl", "*Galician"},
128         {"gn", "*Guarani"},
129         {"gu", "*Gujarati"},
130         {"ha", "*Hausa"},
131         {"he", "*Hebrew"},                              // formerly iw
132         {"hi", "*Hindi"},
133         {"hr", "Hrvatski"},                             // Croatian
134         {"hu", "Magyar"},
135         {"hy", "*Armenian"},
136         {"ia", "*Interlingua"},
137         {"id", "*Indonesian"},                          // formerly in
138         {"ie", "*Interlingue"},
139         {"ik", "*Inupiak"},
140         {"in", "*Indonesian"},                          // replaced by id
141         {"is", "Islenska"},
142         {"it", "Italiano"},
143         {"iu", "*Inuktitut"},
144         {"iw", "*Hebrew"},                              // replaced by he
145         {"ja", "*Japanese"},
146         {"ji", "*Yiddish"},                             // replaced by yi
147         {"jw", "*Javanese"},
148         {"ka", "*Georgian"},
149         {"kk", "*Kazakh"},
150         {"kl", "*Greenlandic"},
151         {"km", "*Cambodian"},
152         {"kn", "*Kannada"},
153         {"ko", "*Korean"},
154         {"ks", "*Kashmiri"},
155         {"ku", "*Kurdish"},
156         {"ky", "*Kirghiz"},
157         {"la", "*Latin"},
158         {"ln", "*Lingala"},
159         {"lo", "*Laothian"},
160         {"lt", "*Lithuanian"},
161         {"lv", "*Latvian, Lettish"},
162         {"mg", "*Malagasy"},
163         {"mi", "*Maori"},
164         {"mk", "*Macedonian"},
165         {"ml", "*Malayalam"},
166         {"mn", "*Mongolian"},
167         {"mo", "*Moldavian"},
168         {"mr", "*Marathi"},
169         {"ms", "*Malay"},
170         {"mt", "*Maltese"},
171         {"my", "*Burmese"},
172         {"na", "*Nauru"},
173         {"ne", "*Nepali"},
174         {"nl", "Nederlands"},
175         {"no", "Norsk"},
176         {"oc", "*Occitan"},
177         {"om", "*(Afan) Oromo"},
178         {"or", "*Oriya"},
179         {"pa", "*Punjabi"},
180         {"pl", "*Polish"},
181         {"ps", "*Pashto, Pushto"},
182         {"pt", "Portugues"},
183         {"qu", "*Quechua"},
184         {"rm", "*Rhaeto-Romance"},
185         {"rn", "*Kirundi"},
186         {"ro", "*Romanian"},
187         {"ru", "*Russian"},
188         {"rw", "*Kinyarwanda"},
189         {"sa", "*Sanskrit"},
190         {"sd", "*Sindhi"},
191         {"sg", "*Sangho"},
192         {"sh", "*Serbo-Croatian"},
193         {"si", "*Sinhalese"},
194         {"sk", "*Slovak"},
195         {"sl", "*Slovenian"},
196         {"sm", "*Samoan"},
197         {"sn", "*Shona"},
198         {"so", "*Somali"},
199         {"sq", "*Albanian"},
200         {"sr", "*Serbian"},
201         {"ss", "*Siswati"},
202         {"st", "*Sesotho"},
203         {"su", "*Sundanese"},
204         {"sv", "Svenska"},
205         {"sw", "*Swahili"},
206         {"ta", "*Tamil"},
207         {"te", "*Telugu"},
208         {"tg", "*Tajik"},
209         {"th", "*Thai"},
210         {"ti", "*Tigrinya"},
211         {"tk", "*Turkmen"},
212         {"tl", "*Tagalog"},
213         {"tn", "*Setswana"},
214         {"to", "*Tonga"},
215         {"tr", "*Turkish"},
216         {"ts", "*Tsonga"},
217         {"tt", "*Tatar"},
218         {"tw", "*Twi"},
219         {"ug", "*Uighur"},
220         {"uk", "*Ukrainian"},
221         {"ur", "*Urdu"},
222         {"uz", "*Uzbek"},
223         {"vi", "*Vietnamese"},
224         {"vo", "*Volapuk"},
225         {"wo", "*Wolof"},
226         {"xh", "*Xhosa"},
227         {"yi", "*Yiddish"},                             // formerly ji
228         {"yo", "*Yoruba"},
229         {"za", "*Zhuang"},
230         {"zh", "*Chinese"},
231         {"zu", "*Zulu"},
232         {"\0", ""}
233     };
234
235 /*****************************************************************************
236  * Local prototypes
237  *****************************************************************************/
238 static int  DVDProbe    ( probedata_t *p_data );
239 static int  DVDCheckCSS ( struct input_thread_s * );
240 static int  DVDRead     ( struct input_thread_s *, data_packet_t ** );
241 static void DVDInit     ( struct input_thread_s * );
242 static void DVDEnd      ( struct input_thread_s * );
243 static void DVDSeek     ( struct input_thread_s *, off_t );
244 static int  DVDRewind   ( struct input_thread_s * );
245
246 /*****************************************************************************
247  * Functions exported as capabilities. They are declared as static so that
248  * we don't pollute the namespace too much.
249  *****************************************************************************/
250 void input_getfunctions( function_list_t * p_function_list )
251 {
252 #define input p_function_list->functions.input
253     p_function_list->pf_probe = DVDProbe;
254     input.pf_init             = DVDInit;
255     input.pf_open             = input_FileOpen;
256     input.pf_close            = input_FileClose;
257     input.pf_end              = DVDEnd;
258     input.pf_read             = DVDRead;
259     input.pf_demux            = input_DemuxPS;
260     input.pf_new_packet       = input_NetlistNewPacket;
261     input.pf_new_pes          = input_NetlistNewPES;
262     input.pf_delete_packet    = input_NetlistDeletePacket;
263     input.pf_delete_pes       = input_NetlistDeletePES;
264     input.pf_rewind           = DVDRewind;
265     input.pf_seek             = DVDSeek;
266 #undef input
267 }
268
269 /*
270  * Local tools to decode some data in ifo
271  */
272
273 /*****************************************************************************
274  * Language: gives the long language name from the two-letters ISO-639 code
275  *****************************************************************************/
276 static char * Language( char * p_code )
277 {
278     int     i = 0;
279
280     while( !memcmp( lang_tbl[i].p_code, p_code, 2 ) && lang_tbl[i].p_lang_long )
281     {
282         i++;
283     }
284
285     return lang_tbl[i].p_lang_long;
286 }
287
288 /*
289  * Data reading functions
290  */
291
292 /*****************************************************************************
293  * DVDProbe: verifies that the stream is a PS stream
294  *****************************************************************************/
295 static int DVDProbe( probedata_t *p_data )
296 {
297     input_thread_t * p_input = (input_thread_t *)p_data;
298
299     char * psz_name = p_input->p_source;
300     int i_handle;
301     int i_score = 5;
302
303     if( TestMethod( INPUT_METHOD_VAR, "dvd" ) )
304     {
305         return( 999 );
306     }
307
308     if( ( strlen(psz_name) > 4 ) && !strncasecmp( psz_name, "dvd:", 4 ) )
309     {
310         /* If the user specified "dvd:" then it's probably a DVD */
311         i_score = 100;
312         psz_name += 4;
313     }
314
315     i_handle = open( psz_name, 0 );
316     if( i_handle == -1 )
317     {
318         return( 0 );
319     }
320     close( i_handle );
321
322     return( i_score );
323 }
324
325 /*****************************************************************************
326  * DVDCheckCSS: check the stream
327  *****************************************************************************/
328 static int DVDCheckCSS( input_thread_t * p_input )
329 {
330 #if defined( HAVE_SYS_DVDIO_H ) || defined( LINUX_DVD )
331     return CSSTest( p_input->i_handle );
332 #else
333     /* DVD ioctls unavailable.
334      * FIXME: Check the stream to see whether it is encrypted or not 
335      * to give and accurate error message */
336     return 0;
337 #endif
338 }
339
340 /*****************************************************************************
341  * DVDSetRegion: initialize input data for title x, chapter y.
342  * It should be called for each user navigation request, and to change
343  * audio or sub-picture streams.
344  * ---
345  * Take care that i_title and i_chapter start from 0.
346  *****************************************************************************/
347 static int DVDSetRegionParameters( input_thread_t * p_input,
348                                    int i_title, int i_chapter,
349                                    int i_audio, int i_spu )
350 {
351     thread_dvd_data_t *  p_method;
352     off_t                i_start;
353     off_t                i_size;
354     pgc_t *              p_pgc;
355     int                  i_start_cell;
356     int                  i_end_cell;
357     int                  i_index;
358     int                  i_cell;
359     
360
361     p_method = (thread_dvd_data_t*)p_input->p_plugin_data;
362
363     /* Set selected title start and size */
364     p_input->stream.i_title = i_title;
365
366     p_pgc = &p_method->ifo.p_vts[i_title].pgci_ti.p_srp[0].pgc;
367
368     /* Find cell index in Program chain */
369     i_index = p_pgc->prg_map.pi_entry_cell[i_chapter] - 1;
370
371     /* Search for cell_index in cell adress_table */
372     i_cell = 0;
373     while( p_pgc->p_cell_pos_inf[i_index].i_vob_id >
374            p_method->ifo.p_vts[i_title].c_adt.p_cell_inf[i_cell].i_vob_id )
375     {
376         i_cell++;
377     }
378     while( p_pgc->p_cell_pos_inf[i_index].i_cell_id >
379            p_method->ifo.p_vts[i_title].c_adt.p_cell_inf[i_cell].i_cell_id )
380     {
381         i_cell++;
382     }
383     i_start_cell = i_cell;
384     i_end_cell = i_start_cell + p_pgc->i_cell_nb - 1;
385
386     intf_WarnMsg( 2, "DVD: Start cell: %d End Cell: %d",
387                                             i_start_cell, i_end_cell );
388
389     p_method->i_start_cell = i_start_cell;
390     p_method->i_end_cell = i_end_cell;
391
392     /* start is : beginning of vts + offset to vobs + offset to vob x */
393     i_start = p_method->ifo.p_vts[i_title].i_pos + DVD_LB_SIZE *
394             ( p_method->ifo.p_vts[i_title].mat.i_tt_vobs_ssector +
395               p_method->ifo.p_vts[i_title].c_adt.p_cell_inf[i_start_cell].i_ssector );
396     p_method->i_start_byte = i_start;
397                                                     
398     i_start = lseek( p_input->i_handle, i_start, SEEK_SET );
399     intf_WarnMsg( 3, "DVD: VOBstart at: %lld", i_start );
400
401     i_size = (off_t)
402         ( p_method->ifo.p_vts[i_title].c_adt.p_cell_inf[i_end_cell].i_esector -
403           p_method->ifo.p_vts[i_title].c_adt.p_cell_inf[i_start_cell].i_ssector + 1 )
404         *DVD_LB_SIZE;
405     intf_WarnMsg( 3, "DVD: stream size: %lld", i_size );
406
407     if( p_input->stream.b_seekable )
408     {
409         stream_ps_data_t * p_demux_data =
410              (stream_ps_data_t *)p_input->stream.pp_programs[0]->p_demux_data;
411
412         /* Pre-parse the stream to gather stream_descriptor_t. */
413         p_input->stream.pp_programs[0]->b_is_ok = 0;
414         p_demux_data->i_PSM_version = EMPTY_PSM_VERSION;
415
416         while( !p_input->b_die && !p_input->b_error
417                 && !p_demux_data->b_has_PSM )
418         {
419             int                 i_result, i;
420             data_packet_t *     pp_packets[INPUT_READ_ONCE];
421
422             i_result = DVDRead( p_input, pp_packets );
423             if( i_result == 1 )
424             {
425                 /* EOF */
426                 vlc_mutex_lock( &p_input->stream.stream_lock );
427                 p_input->stream.pp_programs[0]->b_is_ok = 1;
428                 vlc_mutex_unlock( &p_input->stream.stream_lock );
429                 break;
430             }
431             if( i_result == -1 )
432             {
433                 p_input->b_error = 1;
434                 break;
435             }
436
437             for( i = 0; i < INPUT_READ_ONCE && pp_packets[i] != NULL; i++ )
438             {
439                 /* FIXME: use i_p_config_t */
440                 input_ParsePS( p_input, pp_packets[i] );
441                 input_NetlistDeletePacket( p_input->p_method_data,
442                                            pp_packets[i] );
443             }
444
445             /* File too big. */
446             if( p_input->stream.i_tell > INPUT_PREPARSE_LENGTH )
447             {
448                 break;
449             }
450         }
451         lseek( p_input->i_handle, i_start, SEEK_SET );
452         vlc_mutex_lock( &p_input->stream.stream_lock );
453
454         /* i_tell is an indicator from the beginning of the stream,
455          * not of the DVD */
456         p_input->stream.i_tell = 0;
457
458         if( p_demux_data->b_has_PSM )
459         {
460             /* (The PSM decoder will care about spawning the decoders) */
461             p_input->stream.pp_programs[0]->b_is_ok = 1;
462         }
463 #ifdef AUTO_SPAWN
464         else
465         {
466             /* (We have to do it ourselves) */
467             int                 i_es;
468
469             /* FIXME: we should do multiple passes in case an audio type
470              * is not present */
471             for( i_es = 0;
472                  i_es < p_input->stream.pp_programs[0]->i_es_number;
473                  i_es++ )
474             {
475 #define p_es p_input->stream.pp_programs[0]->pp_es[i_es]
476                 switch( p_es->i_type )
477                 {
478                     case MPEG1_VIDEO_ES:
479                     case MPEG2_VIDEO_ES:
480                         input_SelectES( p_input, p_es );
481                         break;
482
483                     case MPEG1_AUDIO_ES:
484                     case MPEG2_AUDIO_ES:
485                         if( main_GetIntVariable( INPUT_CHANNEL_VAR, 0 )
486                                 == (p_es->i_id & 0x1F) )
487                         switch( main_GetIntVariable( INPUT_AUDIO_VAR, 0 ) )
488                         {
489                         case 0:
490                             main_PutIntVariable( INPUT_AUDIO_VAR,
491                                                  REQUESTED_MPEG );
492                         case REQUESTED_MPEG:
493                             input_SelectES( p_input, p_es );
494                         }
495                         break;
496
497                     case AC3_AUDIO_ES:
498                         if( main_GetIntVariable( INPUT_CHANNEL_VAR, 0 )
499                                 == ((p_es->i_id & 0xF00) >> 8) )
500                         switch( main_GetIntVariable( INPUT_AUDIO_VAR, 0 ) )
501                         {
502                         case 0:
503                             main_PutIntVariable( INPUT_AUDIO_VAR,
504                                                  REQUESTED_AC3 );
505                         case REQUESTED_AC3:
506                             input_SelectES( p_input, p_es );
507                         }
508                         break;
509
510                     case DVD_SPU_ES:
511                         if( main_GetIntVariable( INPUT_SUBTITLE_VAR, -1 )
512                                 == ((p_es->i_id & 0x1F00) >> 8) )
513                         {
514                             input_SelectES( p_input, p_es );
515                         }
516                         break;
517
518                     case LPCM_AUDIO_ES:
519                         /* FIXME ! */
520                         break;
521                 }
522             }
523                     
524         }
525 #endif
526 #ifdef STATS
527         input_DumpStream( p_input );
528 #endif
529
530         /* FIXME : ugly kludge */
531         p_input->stream.i_size = i_size;
532
533         vlc_mutex_unlock( &p_input->stream.stream_lock );
534     }
535     else
536     {
537         /* The programs will be added when we read them. */
538         vlc_mutex_lock( &p_input->stream.stream_lock );
539         p_input->stream.pp_programs[0]->b_is_ok = 0;
540
541         /* FIXME : ugly kludge */
542         p_input->stream.i_size = i_size;
543
544         vlc_mutex_unlock( &p_input->stream.stream_lock );
545     }
546   
547
548     return 0;
549 }
550
551 /*****************************************************************************
552  * DVDInit: initializes DVD structures
553  *****************************************************************************/
554 static void DVDInit( input_thread_t * p_input )
555 {
556     thread_dvd_data_t *  p_method;
557
558     if( (p_method = malloc( sizeof(thread_dvd_data_t) )) == NULL )
559     {
560         intf_ErrMsg( "Out of memory" );
561         p_input->b_error = 1;
562         return;
563     }
564
565     p_input->p_plugin_data = (void *)p_method;
566     p_input->p_method_data = NULL;
567
568     p_method->i_fd = p_input->i_handle;
569     /* FIXME: read several packets once */
570     p_method->i_read_once = 1; 
571     p_method->b_encrypted = DVDCheckCSS( p_input );
572
573     lseek( p_input->i_handle, 0, SEEK_SET );
574
575     /* Reading structures initialisation */
576     input_NetlistInit( p_input, 4096, 4096, DVD_LB_SIZE,
577                        p_method->i_read_once ); 
578
579     /* Ifo initialisation */
580     p_method->ifo = IfoInit( p_input->i_handle );
581
582     /* CSS initialisation */
583     if( p_method->b_encrypted )
584     {
585
586 #if defined( HAVE_SYS_DVDIO_H ) || defined( LINUX_DVD )
587         p_method->css = CSSInit( p_input->i_handle );
588
589         if( ( p_input->b_error = p_method->css.b_error ) )
590         {
591             intf_ErrMsg( "CSS fatal error" );
592             return;
593         }
594 #else
595         intf_ErrMsg( "Unscrambling not supported" );
596         p_input->b_error = 1;
597         return;
598 #endif
599     }
600
601     /* Ifo structures reading */
602     IfoRead( &(p_method->ifo) );
603     intf_WarnMsg( 3, "Ifo: Initialized" );
604
605     /* CSS title keys decryption */
606     if( p_method->b_encrypted )
607     {
608
609 #if defined( HAVE_SYS_DVDIO_H ) || defined( LINUX_DVD )
610         int   i;
611
612         p_method->css.i_title_nb = p_method->ifo.vmg.mat.i_tts_nb;
613
614         if( (p_method->css.p_title_key =
615             malloc( p_method->css.i_title_nb *sizeof(title_key_t) ) ) == NULL )
616         {
617             intf_ErrMsg( "Out of memory" );
618             p_input->b_error = 1;
619             return;
620         }
621
622         for( i=0 ; i<p_method->css.i_title_nb ; i++ )
623         {
624             p_method->css.p_title_key[i].i =
625                 p_method->ifo.p_vts[i].i_pos +
626                 p_method->ifo.p_vts[i].mat.i_tt_vobs_ssector * DVD_LB_SIZE;
627         }
628
629         CSSGetKeys( &(p_method->css) );
630
631         intf_WarnMsg( 3, "CSS: initialized" );
632 #else
633         intf_ErrMsg( "Unscrambling not supported" );
634         p_input->b_error = 1;
635         return;
636 #endif
637     }
638
639     /* Initialize ES structures */
640     input_InitStream( p_input, sizeof( stream_ps_data_t ) );
641     input_AddProgram( p_input, 0, sizeof( stream_ps_data_t ) );
642
643     /* Set stream data */
644     vlc_mutex_lock( &p_input->stream.stream_lock );
645     p_input->stream.i_title_nb = p_method->ifo.vmg.mat.i_tts_nb;
646     vlc_mutex_unlock( &p_input->stream.stream_lock );
647
648     /* By default, set all parameters to 0 */
649     /* FIXME: wrong kludge to get title nb */
650     DVDSetRegionParameters( p_input, 
651                             p_method->ifo.vmg.ptt_srpt.p_tts[0].i_tts_nb - 1,
652                             0, 0, 0 );
653
654     return;
655 }
656
657 /*****************************************************************************
658  * DVDEnd: frees unused data
659  *****************************************************************************/
660 static void DVDEnd( input_thread_t * p_input )
661 {
662     /* FIXME: check order of calls */
663 //    CSSEnd( p_input );
664 //    IfoEnd( (ifo_t*)(&p_input->p_plugin_data->ifo ) );
665     free( p_input->stream.p_demux_data );
666     free( p_input->p_plugin_data );
667     input_NetlistEnd( p_input );
668 }
669
670 /*****************************************************************************
671  * DVDRead: reads data packets into the netlist.
672  *****************************************************************************
673  * Returns -1 in case of error, 0 if everything went well, and 1 in case of
674  * EOF.
675  *****************************************************************************/
676 static int DVDRead( input_thread_t * p_input,
677                    data_packet_t ** pp_packets )
678 {
679     thread_dvd_data_t *     p_method;
680     netlist_t *             p_netlist;
681     struct iovec *          p_vec;
682     struct data_packet_s *  p_data;
683     u8 *                    pi_cur;
684     int                     i_packet_size;
685     int                     i_packet;
686     int                     i_pos;
687     int                     i;
688     boolean_t               b_first_packet;
689
690     p_method = ( thread_dvd_data_t * ) p_input->p_plugin_data;
691     p_netlist = ( netlist_t * ) p_input->p_method_data;
692
693     /* Get an iovec pointer */
694     if( ( p_vec = input_NetlistGetiovec( p_netlist ) ) == NULL )
695     {
696         intf_ErrMsg( "DVD: read error" );
697         return -1;
698     }
699
700     /* Reads from DVD */
701     readv( p_input->i_handle, p_vec, p_method->i_read_once );
702
703 #if defined( HAVE_SYS_DVDIO_H ) || defined( LINUX_DVD )
704     if( p_method->b_encrypted )
705     {
706         for( i=0 ; i<p_method->i_read_once ; i++ )
707         {
708             CSSDescrambleSector(
709                         p_method->css.p_title_key[p_input->stream.i_title].key, 
710                         p_vec[i].iov_base );
711             ((u8*)(p_vec[i].iov_base))[0x14] &= 0x8F;
712         }
713     }
714 #endif
715
716     /* Update netlist indexes */
717     input_NetlistMviovec( p_netlist, p_method->i_read_once, &p_data );
718
719     i_packet = 0;
720     /* Read headers to compute payload length */
721     for( i = 0 ; i < p_method->i_read_once ; i++ )
722     {
723         i_pos = 0;
724         b_first_packet = 1;
725         while( i_pos < p_netlist->i_buffer_size )
726         {
727             pi_cur = (u8*)(p_vec[i].iov_base + i_pos);
728             /*default header */
729             if( U32_AT( pi_cur ) != 0x1BA )
730             {
731                 /* That's the case for all packets, except pack header. */
732                 i_packet_size = U16_AT( pi_cur + 4 );
733             }
734             else
735             {
736                 /* Pack header. */
737                 if( ( pi_cur[4] & 0xC0 ) == 0x40 )
738                 {
739                     /* MPEG-2 */
740                     i_packet_size = 8;
741                 }
742                 else if( ( pi_cur[4] & 0xF0 ) == 0x20 )
743                 {
744                     /* MPEG-1 */
745                     i_packet_size = 6;
746                 }
747                 else
748                 {
749                     intf_ErrMsg( "Unable to determine stream type" );
750                     return( -1 );
751                 }
752             }
753             if( b_first_packet )
754             {
755                 p_data->b_discard_payload = 0;
756                 b_first_packet = 0;
757             }
758             else
759             { 
760                 p_data = input_NetlistNewPacket( p_netlist ,
761                                                  i_packet_size + 6 );
762                 memcpy( p_data->p_buffer,
763                         p_vec[i].iov_base + i_pos , i_packet_size + 6 );
764             }
765
766             p_data->p_payload_end = p_data->p_payload_start + i_packet_size + 6;
767             pp_packets[i_packet] = p_data;
768             i_packet++;
769             i_pos += i_packet_size + 6;
770         }
771     }
772     pp_packets[i_packet] = NULL;
773
774     vlc_mutex_lock( &p_input->stream.stream_lock );
775     p_input->stream.i_tell += p_method->i_read_once *DVD_LB_SIZE;
776     vlc_mutex_unlock( &p_input->stream.stream_lock );
777
778     return( 0 );
779 }
780
781
782 /*****************************************************************************
783  * DVDRewind : reads a stream backward
784  *****************************************************************************/
785 static int DVDRewind( input_thread_t * p_input )
786 {
787     return( -1 );
788 }
789
790 /*****************************************************************************
791  * DVDSeek : Goes to a given position on the stream ; this one is used by the 
792  * input and translate chronological position from input to logical postion
793  * on the device
794  *****************************************************************************/
795 static void DVDSeek( input_thread_t * p_input, off_t i_off )
796 {
797     thread_dvd_data_t *     p_method;
798     off_t                   i_pos;
799     
800     p_method = ( thread_dvd_data_t * )p_input->p_plugin_data;
801
802     /* We have to take care of offset of beginning of title */
803     i_pos = i_off + p_method->i_start_byte;
804
805     /* With DVD, we have to be on a sector boundary */
806     i_pos = i_pos & (~0x7ff);
807
808     i_pos = lseek( p_input->i_handle, i_pos, SEEK_SET );
809
810     p_input->stream.i_tell = i_pos - p_method->i_start_byte;
811
812     return;
813 }