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