]> git.sesse.net Git - vlc/blob - plugins/dvd/input_dvd.c
Begining of TS Input
[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.11 2001/02/14 15:58:29 henri 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 prototypes
78  *****************************************************************************/
79 static int  DVDProbe    ( probedata_t *p_data );
80 static int  DVDCheckCSS ( struct input_thread_s * );
81 static int  DVDRead     ( struct input_thread_s *, data_packet_t ** );
82 static void DVDInit     ( struct input_thread_s * );
83 static void DVDEnd      ( struct input_thread_s * );
84 static void DVDSeek     ( struct input_thread_s *, off_t );
85 static int  DVDRewind   ( struct input_thread_s * );
86
87 /*****************************************************************************
88  * Functions exported as capabilities. They are declared as static so that
89  * we don't pollute the namespace too much.
90  *****************************************************************************/
91 void input_getfunctions( function_list_t * p_function_list )
92 {
93 #define input p_function_list->functions.input
94     p_function_list->pf_probe = DVDProbe;
95     input.pf_init             = DVDInit;
96     input.pf_open             = input_FileOpen;
97     input.pf_close            = input_FileClose;
98     input.pf_end              = DVDEnd;
99     input.pf_read             = DVDRead;
100     input.pf_demux            = input_DemuxPS;
101     input.pf_new_packet       = input_NetlistNewPacket;
102     input.pf_new_pes          = input_NetlistNewPES;
103     input.pf_delete_packet    = input_NetlistDeletePacket;
104     input.pf_delete_pes       = input_NetlistDeletePES;
105     input.pf_rewind           = DVDRewind;
106     input.pf_seek             = DVDSeek;
107 #undef input
108 }
109
110 /*
111  * Data reading functions
112  */
113
114 /*****************************************************************************
115  * DVDProbe: verifies that the stream is a PS stream
116  *****************************************************************************/
117 static int DVDProbe( probedata_t *p_data )
118 {
119     if( TestMethod( INPUT_METHOD_VAR, "dvd" ) )
120     {
121         return( 999 );
122     }
123
124     return 5;
125 }
126
127 /*****************************************************************************
128  * DVDCheckCSS: check the stream
129  *****************************************************************************/
130 static int DVDCheckCSS( input_thread_t * p_input )
131 {
132 #if defined( HAVE_SYS_DVDIO_H ) || defined( LINUX_DVD )
133     return CSSTest( p_input->i_handle );
134 #else
135     /* DVD ioctls unavailable.
136      * FIXME: Check the stream to see whether it is encrypted or not 
137      * to give and accurate error message */
138     return 0;
139 #endif
140 }
141
142 /*****************************************************************************
143  * DVDInit: initializes DVD structures
144  *****************************************************************************/
145 static void DVDInit( input_thread_t * p_input )
146 {
147     thread_dvd_data_t *  p_method;
148     off_t                i_start;
149     off_t                i_size;
150     int                  i_cell, i_cell_1, i_start_cell, i_end_cell;
151
152     if( (p_method = malloc( sizeof(thread_dvd_data_t) )) == NULL )
153     {
154         intf_ErrMsg( "Out of memory" );
155         p_input->b_error = 1;
156         return;
157     }
158
159     p_input->p_plugin_data = (void *)p_method;
160     p_input->p_method_data = NULL;
161
162     p_method->i_fd = p_input->i_handle;
163     /* FIXME: read several packets once */
164     p_method->i_read_once = 1; 
165     p_method->i_title = 0;
166     p_method->b_encrypted = DVDCheckCSS( p_input );
167
168     lseek( p_input->i_handle, 0, SEEK_SET );
169
170     /* Reading structures initialisation */
171     input_NetlistInit( p_input, 4096, 4096, DVD_LB_SIZE,
172                        p_method->i_read_once ); 
173
174     /* Ifo initialisation */
175     p_method->ifo = IfoInit( p_input->i_handle );
176
177     /* CSS initialisation */
178     if( p_method->b_encrypted )
179     {
180
181 #if defined( HAVE_SYS_DVDIO_H ) || defined( LINUX_DVD )
182         p_method->css = CSSInit( p_input->i_handle );
183
184         if( ( p_input->b_error = p_method->css.b_error ) )
185         {
186             intf_ErrMsg( "CSS fatal error" );
187             return;
188         }
189 #else
190         intf_ErrMsg( "Unscrambling not supported" );
191         p_input->b_error = 1;
192         return;
193 #endif
194     }
195
196     /* Ifo structures reading */
197     IfoRead( &(p_method->ifo) );
198     intf_WarnMsg( 3, "Ifo: Initialized" );
199
200     /* CSS title keys */
201     if( p_method->b_encrypted )
202     {
203
204 #if defined( HAVE_SYS_DVDIO_H ) || defined( LINUX_DVD )
205         int   i;
206
207         p_method->css.i_title_nb = p_method->ifo.vmg.mat.i_tts_nb;
208
209         if( (p_method->css.p_title_key =
210             malloc( p_method->css.i_title_nb *sizeof(title_key_t) ) ) == NULL )
211         {
212             intf_ErrMsg( "Out of memory" );
213             p_input->b_error = 1;
214             return;
215         }
216
217         for( i=0 ; i<p_method->css.i_title_nb ; i++ )
218         {
219             p_method->css.p_title_key[i].i =
220                 p_method->ifo.p_vts[i].i_pos +
221                 p_method->ifo.p_vts[i].mat.i_tt_vobs_ssector * DVD_LB_SIZE;
222         }
223
224         CSSGetKeys( &(p_method->css) );
225
226         intf_WarnMsg( 3, "CSS: initialized" );
227 #else
228         intf_ErrMsg( "Unscrambling not supported" );
229         p_input->b_error = 1;
230         return;
231 #endif
232     }
233
234     /* FIXME: Kludge beginning and end of the stream in vts_01_1.vob */
235
236     /* Determines which vob contains the movie */
237     i_cell = 0;
238     i_cell_1 = 0;
239     i_start_cell = 0;
240     i_end_cell = 0;
241
242     /* Loop on the number of vobs */
243     while( p_method->ifo.p_vts[0].c_adt.p_cell_inf[i_cell].i_vob_id <=
244            p_method->ifo.p_vts[0].c_adt.i_vob_nb )
245     {
246         i_cell_1 = i_cell;
247
248         /* Loop to find the number of cells in the vob */
249         do
250         {
251             i_cell++;
252             if( i_cell >= p_method->ifo.p_vts[0].c_adt.i_cell_nb )
253             {
254                 break;
255             }
256         }
257         while( p_method->ifo.p_vts[0].c_adt.p_cell_inf[i_cell-1].i_cell_id <
258                p_method->ifo.p_vts[0].c_adt.p_cell_inf[i_cell].i_cell_id );
259
260
261         if( p_method->ifo.p_vts[0].c_adt.p_cell_inf[i_cell-1].i_cell_id >
262             p_method->ifo.p_vts[0].c_adt.p_cell_inf[i_end_cell].i_cell_id )
263         {
264             i_start_cell = i_cell_1;
265             i_end_cell = i_cell - 1;
266         }
267     }
268
269     /* The preceding does not work with all DVD, so we give the
270      * last cell of the title as end */
271     i_end_cell = p_method->ifo.p_vts[0].c_adt.i_cell_nb - 1;
272
273     intf_WarnMsg( 2, "DVD: Start cell: %d End Cell: %d",
274                                             i_start_cell, i_end_cell );
275
276     p_method->i_start_cell = i_start_cell;
277     p_method->i_end_cell = i_end_cell;
278
279     /* start is : beginning of vts + offset to vobs + offset to vob x */
280     i_start = p_method->ifo.p_vts[0].i_pos + DVD_LB_SIZE *
281             ( p_method->ifo.p_vts[0].mat.i_tt_vobs_ssector +
282               p_method->ifo.p_vts[0].c_adt.p_cell_inf[i_start_cell].i_ssector );
283     p_method->i_start_byte = i_start;
284                                                     
285     i_start = lseek( p_input->i_handle, i_start, SEEK_SET );
286     intf_WarnMsg( 3, "DVD: VOBstart at: %lld", i_start );
287
288     i_size = (off_t)
289         ( p_method->ifo.p_vts[0].c_adt.p_cell_inf[i_end_cell].i_esector -
290           p_method->ifo.p_vts[0].c_adt.p_cell_inf[i_start_cell].i_ssector + 1 )
291         *DVD_LB_SIZE;
292     intf_WarnMsg( 3, "DVD: stream size: %lld", i_size );
293
294
295     /* Initialize ES structures */
296     input_InitStream( p_input, sizeof( stream_ps_data_t ) );
297     input_AddProgram( p_input, 0, sizeof( stream_ps_data_t ) );
298
299     if( p_input->stream.b_seekable )
300     {
301         stream_ps_data_t * p_demux_data =
302              (stream_ps_data_t *)p_input->stream.pp_programs[0]->p_demux_data;
303
304         /* Pre-parse the stream to gather stream_descriptor_t. */
305         p_input->stream.pp_programs[0]->b_is_ok = 0;
306         p_demux_data->i_PSM_version = EMPTY_PSM_VERSION;
307
308         while( !p_input->b_die && !p_input->b_error
309                 && !p_demux_data->b_has_PSM )
310         {
311             int                 i_result, i;
312             data_packet_t *     pp_packets[INPUT_READ_ONCE];
313
314             i_result = DVDRead( p_input, pp_packets );
315             if( i_result == 1 )
316             {
317                 /* EOF */
318                 vlc_mutex_lock( &p_input->stream.stream_lock );
319                 p_input->stream.pp_programs[0]->b_is_ok = 1;
320                 vlc_mutex_unlock( &p_input->stream.stream_lock );
321                 break;
322             }
323             if( i_result == -1 )
324             {
325                 p_input->b_error = 1;
326                 break;
327             }
328
329             for( i = 0; i < INPUT_READ_ONCE && pp_packets[i] != NULL; i++ )
330             {
331                 /* FIXME: use i_p_config_t */
332                 input_ParsePS( p_input, pp_packets[i] );
333                 input_NetlistDeletePacket( p_input->p_method_data,
334                                            pp_packets[i] );
335             }
336
337             /* File too big. */
338             if( p_input->stream.i_tell > INPUT_PREPARSE_LENGTH )
339             {
340                 break;
341             }
342         }
343         lseek( p_input->i_handle, i_start, SEEK_SET );
344         vlc_mutex_lock( &p_input->stream.stream_lock );
345
346         /* i_tell is an indicator from the beginning of the stream,
347          * not of the DVD */
348         p_input->stream.i_tell = 0;
349
350         if( p_demux_data->b_has_PSM )
351         {
352             /* (The PSM decoder will care about spawning the decoders) */
353             p_input->stream.pp_programs[0]->b_is_ok = 1;
354         }
355 #ifdef AUTO_SPAWN
356         else
357         {
358             /* (We have to do it ourselves) */
359             int                 i_es;
360
361             /* FIXME: we should do multiple passes in case an audio type
362              * is not present */
363             for( i_es = 0;
364                  i_es < p_input->stream.pp_programs[0]->i_es_number;
365                  i_es++ )
366             {
367 #define p_es p_input->stream.pp_programs[0]->pp_es[i_es]
368                 switch( p_es->i_type )
369                 {
370                     case MPEG1_VIDEO_ES:
371                     case MPEG2_VIDEO_ES:
372                         input_SelectES( p_input, p_es );
373                         break;
374
375                     case MPEG1_AUDIO_ES:
376                     case MPEG2_AUDIO_ES:
377                         if( main_GetIntVariable( INPUT_CHANNEL_VAR, 0 )
378                                 == (p_es->i_id & 0x1F) )
379                         switch( main_GetIntVariable( INPUT_AUDIO_VAR, 0 ) )
380                         {
381                         case 0:
382                             main_PutIntVariable( INPUT_AUDIO_VAR,
383                                                  REQUESTED_MPEG );
384                         case REQUESTED_MPEG:
385                             input_SelectES( p_input, p_es );
386                         }
387                         break;
388
389                     case AC3_AUDIO_ES:
390                         if( main_GetIntVariable( INPUT_CHANNEL_VAR, 0 )
391                                 == ((p_es->i_id & 0xF00) >> 8) )
392                         switch( main_GetIntVariable( INPUT_AUDIO_VAR, 0 ) )
393                         {
394                         case 0:
395                             main_PutIntVariable( INPUT_AUDIO_VAR,
396                                                  REQUESTED_AC3 );
397                         case REQUESTED_AC3:
398                             input_SelectES( p_input, p_es );
399                         }
400                         break;
401
402                     case DVD_SPU_ES:
403                         if( main_GetIntVariable( INPUT_SUBTITLE_VAR, -1 )
404                                 == ((p_es->i_id & 0x1F00) >> 8) )
405                         {
406                             input_SelectES( p_input, p_es );
407                         }
408                         break;
409
410                     case LPCM_AUDIO_ES:
411                         /* FIXME ! */
412                         break;
413                 }
414             }
415                     
416         }
417 #endif
418 #ifdef STATS
419         input_DumpStream( p_input );
420 #endif
421
422         /* FIXME : ugly kludge */
423         p_input->stream.i_size = i_size;
424
425         vlc_mutex_unlock( &p_input->stream.stream_lock );
426     }
427     else
428     {
429         /* The programs will be added when we read them. */
430         vlc_mutex_lock( &p_input->stream.stream_lock );
431         p_input->stream.pp_programs[0]->b_is_ok = 0;
432
433         /* FIXME : ugly kludge */
434         p_input->stream.i_size = i_size;
435
436         vlc_mutex_unlock( &p_input->stream.stream_lock );
437     }
438
439 }
440
441 /*****************************************************************************
442  * DVDEnd: frees unused data
443  *****************************************************************************/
444 static void DVDEnd( input_thread_t * p_input )
445 {
446     /* FIXME: check order of calls */
447 //    CSSEnd( p_input );
448 //    IfoEnd( (ifo_t*)(&p_input->p_plugin_data->ifo ) );
449     free( p_input->stream.p_demux_data );
450     free( p_input->p_plugin_data );
451     input_NetlistEnd( p_input );
452 }
453
454 /*****************************************************************************
455  * DVDRead: reads data packets into the netlist.
456  *****************************************************************************
457  * Returns -1 in case of error, 0 if everything went well, and 1 in case of
458  * EOF.
459  *****************************************************************************/
460 static int DVDRead( input_thread_t * p_input,
461                    data_packet_t ** pp_packets )
462 {
463     thread_dvd_data_t *     p_method;
464     netlist_t *             p_netlist;
465     struct iovec *          p_vec;
466     struct data_packet_s *  p_data;
467     u8 *                    pi_cur;
468     int                     i_packet_size;
469     int                     i_packet;
470     int                     i_pos;
471     int                     i;
472     boolean_t               b_first_packet;
473
474     p_method = ( thread_dvd_data_t * ) p_input->p_plugin_data;
475     p_netlist = ( netlist_t * ) p_input->p_method_data;
476
477     /* Get an iovec pointer */
478     if( ( p_vec = input_NetlistGetiovec( p_netlist ) ) == NULL )
479     {
480         intf_ErrMsg( "DVD: read error" );
481         return -1;
482     }
483
484     /* Reads from DVD */
485     readv( p_input->i_handle, p_vec, p_method->i_read_once );
486
487 #if defined( HAVE_SYS_DVDIO_H ) || defined( LINUX_DVD )
488     if( p_method->b_encrypted )
489     {
490         for( i=0 ; i<p_method->i_read_once ; i++ )
491         {
492             CSSDescrambleSector(
493                         p_method->css.p_title_key[p_method->i_title].key, 
494                         p_vec[i].iov_base );
495             ((u8*)(p_vec[i].iov_base))[0x14] &= 0x8F;
496         }
497     }
498 #endif
499
500     /* Update netlist indexes */
501     input_NetlistMviovec( p_netlist, p_method->i_read_once, &p_data );
502
503     i_packet = 0;
504     /* Read headers to compute payload length */
505     for( i = 0 ; i < p_method->i_read_once ; i++ )
506     {
507         i_pos = 0;
508         b_first_packet = 1;
509         while( i_pos < p_netlist->i_buffer_size )
510         {
511             pi_cur = (u8*)(p_vec[i].iov_base + i_pos);
512             /*default header */
513             if( U32_AT( pi_cur ) != 0x1BA )
514             {
515                 /* That's the case for all packets, except pack header. */
516                 i_packet_size = U16_AT( pi_cur + 4 );
517             }
518             else
519             {
520                 /* Pack header. */
521                 if( ( pi_cur[4] & 0xC0 ) == 0x40 )
522                 {
523                     /* MPEG-2 */
524                     i_packet_size = 8;
525                 }
526                 else if( ( pi_cur[4] & 0xF0 ) == 0x20 )
527                 {
528                     /* MPEG-1 */
529                     i_packet_size = 6;
530                 }
531                 else
532                 {
533                     intf_ErrMsg( "Unable to determine stream type" );
534                     return( -1 );
535                 }
536             }
537             if( b_first_packet )
538             {
539                 p_data->b_discard_payload = 0;
540                 b_first_packet = 0;
541             }
542             else
543             { 
544                 p_data = input_NetlistNewPacket( p_netlist ,
545                                                  i_packet_size + 6 );
546                 memcpy( p_data->p_buffer,
547                         p_vec[i].iov_base + i_pos , i_packet_size + 6 );
548             }
549
550             p_data->p_payload_end = p_data->p_payload_start + i_packet_size + 6;
551             pp_packets[i_packet] = p_data;
552             i_packet++;
553             i_pos += i_packet_size + 6;
554         }
555     }
556     pp_packets[i_packet] = NULL;
557
558     vlc_mutex_lock( &p_input->stream.stream_lock );
559     p_input->stream.i_tell += p_method->i_read_once *DVD_LB_SIZE;
560     vlc_mutex_unlock( &p_input->stream.stream_lock );
561
562     return( 0 );
563 }
564
565
566 /*****************************************************************************
567  * DVDRewind : reads a stream backward
568  *****************************************************************************/
569 static int DVDRewind( input_thread_t * p_input )
570 {
571     return( -1 );
572 }
573
574 /*****************************************************************************
575  * DVDSeek : Goes to a given position on the stream ; this one is used by the 
576  * input and translate chronological position from input to logical postion
577  * on the device
578  *****************************************************************************/
579 static void DVDSeek( input_thread_t * p_input, off_t i_off )
580 {
581     thread_dvd_data_t *     p_method;
582     off_t                   i_pos;
583     
584     p_method = ( thread_dvd_data_t * )p_input->p_plugin_data;
585
586     /* We have to take care of offset of beginning of title */
587     i_pos = i_off + p_method->i_start_byte;
588
589     /* With DVD, we have to be on a sector boundary */
590     i_pos = i_pos & (~0x7ff);
591
592     i_pos = lseek( p_input->i_handle, i_pos, SEEK_SET );
593
594     p_input->stream.i_tell = i_pos - p_method->i_start_byte;
595
596     return;
597 }