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