]> git.sesse.net Git - vlc/blob - plugins/dvd/input_dvd.c
* Portability : changed off64_t to off_t and activated appropriate
[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.5 2001/02/08 17:44:12 massiot 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 #if defined( HAVE_SYS_DVDIO_H ) || defined( LINUX_DVD )
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 #endif
204
205     /* FIXME: Kludge beginning of vts_01_1.vob */
206     i_start = p_method->ifo.p_vts[0].i_pos +
207               p_method->ifo.p_vts[0].mat.i_tt_vobs_ssector *DVD_LB_SIZE;
208
209     i_start = lseek( p_input->i_handle, i_start, SEEK_SET );
210     intf_Msg( "VOB start at : %lld", (long long)i_start );
211
212     /* Initialize ES structures */
213     input_InitStream( p_input, sizeof( stream_ps_data_t ) );
214     input_AddProgram( p_input, 0, sizeof( stream_ps_data_t ) );
215
216     if( p_input->stream.b_seekable )
217     {
218         stream_ps_data_t * p_demux_data =
219              (stream_ps_data_t *)p_input->stream.pp_programs[0]->p_demux_data;
220
221         /* Pre-parse the stream to gather stream_descriptor_t. */
222         p_input->stream.pp_programs[0]->b_is_ok = 0;
223         p_demux_data->i_PSM_version = EMPTY_PSM_VERSION;
224
225         while( !p_input->b_die && !p_input->b_error
226                 && !p_demux_data->b_has_PSM )
227         {
228             int                 i_result, i;
229             data_packet_t *     pp_packets[INPUT_READ_ONCE];
230
231             i_result = DVDRead( p_input, pp_packets );
232             if( i_result == 1 )
233             {
234                 /* EOF */
235                 vlc_mutex_lock( &p_input->stream.stream_lock );
236                 p_input->stream.pp_programs[0]->b_is_ok = 1;
237                 vlc_mutex_unlock( &p_input->stream.stream_lock );
238                 break;
239             }
240             if( i_result == -1 )
241             {
242                 p_input->b_error = 1;
243                 break;
244             }
245
246             for( i = 0; i < INPUT_READ_ONCE && pp_packets[i] != NULL; i++ )
247             {
248                 /* FIXME: use i_p_config_t */
249                 input_ParsePS( p_input, pp_packets[i] );
250                 input_NetlistDeletePacket( p_input->p_method_data,
251                                            pp_packets[i] );
252             }
253
254             /* File too big. */
255             if( p_input->stream.i_tell > INPUT_PREPARSE_LENGTH )
256             {
257                 break;
258             }
259         }
260         lseek( p_input->i_handle, i_start, SEEK_SET );
261         vlc_mutex_lock( &p_input->stream.stream_lock );
262         p_input->stream.i_tell = 0;
263         if( p_demux_data->b_has_PSM )
264         {
265             /* (The PSM decoder will care about spawning the decoders) */
266             p_input->stream.pp_programs[0]->b_is_ok = 1;
267         }
268 #ifdef AUTO_SPAWN
269         else
270         {
271             /* (We have to do it ourselves) */
272             int                 i_es;
273
274             /* FIXME: we should do multiple passes in case an audio type
275              * is not present */
276             for( i_es = 0;
277                  i_es < p_input->stream.pp_programs[0]->i_es_number;
278                  i_es++ )
279             {
280 #define p_es p_input->stream.pp_programs[0]->pp_es[i_es]
281                 switch( p_es->i_type )
282                 {
283                     case MPEG1_VIDEO_ES:
284                     case MPEG2_VIDEO_ES:
285                         input_SelectES( p_input, p_es );
286                         break;
287
288                     case MPEG1_AUDIO_ES:
289                     case MPEG2_AUDIO_ES:
290                         if( main_GetIntVariable( INPUT_DVD_CHANNEL_VAR, 0 )
291                                 == (p_es->i_id & 0x1F) )
292                         switch( main_GetIntVariable( INPUT_DVD_AUDIO_VAR, 0 ) )
293                         {
294                         case 0:
295                             main_PutIntVariable( INPUT_DVD_AUDIO_VAR,
296                                                  REQUESTED_MPEG );
297                         case REQUESTED_MPEG:
298                             input_SelectES( p_input, p_es );
299                         }
300                         break;
301
302                     case AC3_AUDIO_ES:
303                         if( main_GetIntVariable( INPUT_DVD_CHANNEL_VAR, 0 )
304                                 == ((p_es->i_id & 0xF00) >> 8) )
305                         switch( main_GetIntVariable( INPUT_DVD_AUDIO_VAR, 0 ) )
306                         {
307                         case 0:
308                             main_PutIntVariable( INPUT_DVD_AUDIO_VAR,
309                                                  REQUESTED_AC3 );
310                         case REQUESTED_AC3:
311                             input_SelectES( p_input, p_es );
312                         }
313                         break;
314
315                     case DVD_SPU_ES:
316                         if( main_GetIntVariable( INPUT_DVD_SUBTITLE_VAR, -1 )
317                                 == ((p_es->i_id & 0x1F00) >> 8) )
318                         {
319                             input_SelectES( p_input, p_es );
320                         }
321                         break;
322
323                     case LPCM_AUDIO_ES:
324                         /* FIXME ! */
325                         break;
326                 }
327             }
328                     
329         }
330 #endif
331 #ifdef STATS
332         input_DumpStream( p_input );
333 #endif
334         vlc_mutex_unlock( &p_input->stream.stream_lock );
335     }
336     else
337     {
338         /* The programs will be added when we read them. */
339         vlc_mutex_lock( &p_input->stream.stream_lock );
340         p_input->stream.pp_programs[0]->b_is_ok = 0;
341         vlc_mutex_unlock( &p_input->stream.stream_lock );
342     }
343
344 }
345
346 /*****************************************************************************
347  * DVDOpen : open the dvd device
348  *****************************************************************************/
349 static void DVDOpen( input_thread_t * p_input )
350 {
351     intf_Msg( "input: opening DVD %s", p_input->p_source );
352
353     p_input->i_handle = open( p_input->p_source, O_RDONLY | O_NONBLOCK );
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 defined( HAVE_SYS_DVDIO_H ) || defined( LINUX_DVD )
429     if( p_method->b_encrypted )
430     {
431         for( i=0 ; i<p_method->i_read_once ; i++ )
432         {
433             CSSDescrambleSector(
434                         p_method->css.p_title_key[p_method->i_title].key, 
435                         p_vec[i].iov_base );
436             ((u8*)(p_vec[i].iov_base))[0x14] &= 0x8F;
437         }
438     }
439 #endif
440
441     /* Update netlist indexes */
442     input_NetlistMviovec( p_netlist, p_method->i_read_once );
443
444     i_packet = 0;
445     /* Read headers to compute payload length */
446     for( i = 0 ; i < p_method->i_read_once ; i++ )
447     {
448         i_pos = 0;
449         b_first_packet = 1;
450         while( i_pos < p_netlist->i_buffer_size )
451         {
452             pi_cur = (u8*)(p_vec[i].iov_base + i_pos);
453             /*default header */
454             if( U32_AT( pi_cur ) != 0x1BA )
455             {
456                 /* That's the case for all packets, except pack header. */
457                 i_packet_size = U16_AT( pi_cur + 4 );
458             }
459             else
460             {
461                 /* Pack header. */
462                 if( ( pi_cur[4] & 0xC0 ) == 0x40 )
463                 {
464                     /* MPEG-2 */
465                     i_packet_size = 8;
466                 }
467                 else if( ( pi_cur[4] & 0xF0 ) == 0x20 )
468                 {
469                     /* MPEG-1 */
470                     i_packet_size = 6;
471                 }
472                 else
473                 {
474                     intf_ErrMsg( "Unable to determine stream type" );
475                     return( -1 );
476                 }
477             }
478             if( b_first_packet )
479             {
480                 p_data->b_discard_payload = 0;
481                 b_first_packet = 0;
482             }
483             else
484             { 
485                 p_data = input_NetlistNewPacket( p_netlist ,
486                                                  i_packet_size + 6 );
487                 memcpy( p_data->p_buffer,
488                         p_vec[i].iov_base + i_pos , i_packet_size + 6 );
489             }
490
491             p_data->p_payload_end = p_data->p_payload_start + i_packet_size + 6;
492             pp_packets[i_packet] = p_data;
493             i_packet++;
494             i_pos += i_packet_size + 6;
495         }
496     }
497     pp_packets[i_packet] = NULL;
498
499     vlc_mutex_lock( &p_input->stream.stream_lock );
500     p_input->stream.i_tell += p_method->i_read_once *DVD_LB_SIZE;
501     vlc_mutex_unlock( &p_input->stream.stream_lock );
502
503     return( 0 );
504 }
505
506
507 /*****************************************************************************
508  * DVDRewind : reads a stream backward
509  *****************************************************************************/
510 static int DVDRewind( input_thread_t * p_input )
511 {
512     return( -1 );
513 }
514
515 /*****************************************************************************
516  * DVDSeek : Goes to a given position on the stream ; this one is used by the 
517  * input and translate chronological position from input to logical postion
518  * on the device
519  *****************************************************************************/
520 static void DVDSeek( input_thread_t * p_input, off_t i_off )
521 {
522     return;
523 }