]> git.sesse.net Git - vlc/blob - plugins/dvd/input_dvd.c
-Much cleaning in css code. It should work fine 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.8 2001/02/12 09:58:06 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         if( ( p_input->b_error = p_method->css.b_error ) )
187         {
188             fprintf( stderr, " boaruf \n" );
189             return;
190         }
191
192         p_method->css.i_title_nb = p_method->ifo.vmg.mat.i_tts_nb;
193         if( (p_method->css.p_title_key =
194             malloc( p_method->css.i_title_nb *sizeof(title_key_t) ) ) == NULL )
195         {
196             intf_ErrMsg( "Out of memory" );
197             p_input->b_error = 1;
198             return;
199         }
200         for( i=0 ; i<p_method->css.i_title_nb ; i++ )
201         {
202             p_method->css.p_title_key[i].i =
203                     p_method->ifo.p_vts[i].i_pos +
204                     p_method->ifo.p_vts[i].mat.i_tt_vobs_ssector *DVD_LB_SIZE;
205         }
206         CSSGetKeys( &(p_method->css) );
207         intf_Msg( "CSS: Initialized" );
208 #else
209         intf_ErrMsg( "Unscrambling not supported" );
210 #endif
211     }
212
213     /* FIXME: Kludge beginning of vts_01_1.vob */
214     i_start = p_method->ifo.p_vts[0].i_pos +
215               p_method->ifo.p_vts[0].mat.i_tt_vobs_ssector *DVD_LB_SIZE;
216
217     i_start = lseek( p_input->i_handle, i_start, SEEK_SET );
218     intf_Msg( "VOB start at : %lld", (long long)i_start );
219
220     /* Initialize ES structures */
221     input_InitStream( p_input, sizeof( stream_ps_data_t ) );
222     input_AddProgram( p_input, 0, sizeof( stream_ps_data_t ) );
223
224     if( p_input->stream.b_seekable )
225     {
226         stream_ps_data_t * p_demux_data =
227              (stream_ps_data_t *)p_input->stream.pp_programs[0]->p_demux_data;
228
229         /* Pre-parse the stream to gather stream_descriptor_t. */
230         p_input->stream.pp_programs[0]->b_is_ok = 0;
231         p_demux_data->i_PSM_version = EMPTY_PSM_VERSION;
232
233         while( !p_input->b_die && !p_input->b_error
234                 && !p_demux_data->b_has_PSM )
235         {
236             int                 i_result, i;
237             data_packet_t *     pp_packets[INPUT_READ_ONCE];
238
239             i_result = DVDRead( p_input, pp_packets );
240             if( i_result == 1 )
241             {
242                 /* EOF */
243                 vlc_mutex_lock( &p_input->stream.stream_lock );
244                 p_input->stream.pp_programs[0]->b_is_ok = 1;
245                 vlc_mutex_unlock( &p_input->stream.stream_lock );
246                 break;
247             }
248             if( i_result == -1 )
249             {
250                 p_input->b_error = 1;
251                 break;
252             }
253
254             for( i = 0; i < INPUT_READ_ONCE && pp_packets[i] != NULL; i++ )
255             {
256                 /* FIXME: use i_p_config_t */
257                 input_ParsePS( p_input, pp_packets[i] );
258                 input_NetlistDeletePacket( p_input->p_method_data,
259                                            pp_packets[i] );
260             }
261
262             /* File too big. */
263             if( p_input->stream.i_tell > INPUT_PREPARSE_LENGTH )
264             {
265                 break;
266             }
267         }
268         lseek( p_input->i_handle, i_start, SEEK_SET );
269         vlc_mutex_lock( &p_input->stream.stream_lock );
270         p_input->stream.i_tell = 0;
271         if( p_demux_data->b_has_PSM )
272         {
273             /* (The PSM decoder will care about spawning the decoders) */
274             p_input->stream.pp_programs[0]->b_is_ok = 1;
275         }
276 #ifdef AUTO_SPAWN
277         else
278         {
279             /* (We have to do it ourselves) */
280             int                 i_es;
281
282             /* FIXME: we should do multiple passes in case an audio type
283              * is not present */
284             for( i_es = 0;
285                  i_es < p_input->stream.pp_programs[0]->i_es_number;
286                  i_es++ )
287             {
288 #define p_es p_input->stream.pp_programs[0]->pp_es[i_es]
289                 switch( p_es->i_type )
290                 {
291                     case MPEG1_VIDEO_ES:
292                     case MPEG2_VIDEO_ES:
293                         input_SelectES( p_input, p_es );
294                         break;
295
296                     case MPEG1_AUDIO_ES:
297                     case MPEG2_AUDIO_ES:
298                         if( main_GetIntVariable( INPUT_CHANNEL_VAR, 0 )
299                                 == (p_es->i_id & 0x1F) )
300                         switch( main_GetIntVariable( INPUT_AUDIO_VAR, 0 ) )
301                         {
302                         case 0:
303                             main_PutIntVariable( INPUT_AUDIO_VAR,
304                                                  REQUESTED_MPEG );
305                         case REQUESTED_MPEG:
306                             input_SelectES( p_input, p_es );
307                         }
308                         break;
309
310                     case AC3_AUDIO_ES:
311                         if( main_GetIntVariable( INPUT_CHANNEL_VAR, 0 )
312                                 == ((p_es->i_id & 0xF00) >> 8) )
313                         switch( main_GetIntVariable( INPUT_AUDIO_VAR, 0 ) )
314                         {
315                         case 0:
316                             main_PutIntVariable( INPUT_AUDIO_VAR,
317                                                  REQUESTED_AC3 );
318                         case REQUESTED_AC3:
319                             input_SelectES( p_input, p_es );
320                         }
321                         break;
322
323                     case DVD_SPU_ES:
324                         if( main_GetIntVariable( INPUT_SUBTITLE_VAR, -1 )
325                                 == ((p_es->i_id & 0x1F00) >> 8) )
326                         {
327                             input_SelectES( p_input, p_es );
328                         }
329                         break;
330
331                     case LPCM_AUDIO_ES:
332                         /* FIXME ! */
333                         break;
334                 }
335             }
336                     
337         }
338 #endif
339 #ifdef STATS
340         input_DumpStream( p_input );
341 #endif
342         vlc_mutex_unlock( &p_input->stream.stream_lock );
343     }
344     else
345     {
346         /* The programs will be added when we read them. */
347         vlc_mutex_lock( &p_input->stream.stream_lock );
348         p_input->stream.pp_programs[0]->b_is_ok = 0;
349         vlc_mutex_unlock( &p_input->stream.stream_lock );
350     }
351
352 }
353
354 /*****************************************************************************
355  * DVDOpen : open the dvd device
356  *****************************************************************************/
357 static void DVDOpen( input_thread_t * p_input )
358 {
359     intf_Msg( "input: opening DVD %s", p_input->p_source );
360
361     p_input->i_handle = open( p_input->p_source, O_RDONLY | O_NONBLOCK );
362
363     if( p_input->i_handle == -1 )
364     {
365         intf_ErrMsg( "input error: cannot open device (%s)", strerror(errno) );
366         p_input->b_error = 1;
367         return;
368     }
369
370     vlc_mutex_lock( &p_input->stream.stream_lock );
371
372     p_input->stream.b_pace_control = 1;
373     p_input->stream.b_seekable = 1;
374     p_input->stream.i_size = 0;
375     p_input->stream.i_tell = 0;
376
377     vlc_mutex_unlock( &p_input->stream.stream_lock );
378 }
379
380 /*****************************************************************************
381  * DVDClose : close a file descriptor
382  *****************************************************************************/
383 static void DVDClose( input_thread_t * p_input )
384 {
385     close( p_input->i_handle );
386
387     return;
388 }
389
390 /*****************************************************************************
391  * DVDEnd: frees unused data
392  *****************************************************************************/
393 static void DVDEnd( input_thread_t * p_input )
394 {
395     /* FIXME: check order of calls */
396 //    CSSEnd( p_input );
397 //    IfoEnd( (ifo_t*)(&p_input->p_plugin_data->ifo ) );
398     free( p_input->stream.p_demux_data );
399     free( p_input->p_plugin_data );
400     input_NetlistEnd( p_input );
401 }
402
403 /*****************************************************************************
404  * DVDRead: reads data packets into the netlist.
405  *****************************************************************************
406  * Returns -1 in case of error, 0 if everything went well, and 1 in case of
407  * EOF.
408  *****************************************************************************/
409 static int DVDRead( input_thread_t * p_input,
410                    data_packet_t ** pp_packets )
411 {
412     thread_dvd_data_t *     p_method;
413     netlist_t *             p_netlist;
414     struct iovec *          p_vec;
415     struct data_packet_s *  p_data;
416     u8 *                    pi_cur;
417     int                     i_packet_size;
418     int                     i_packet;
419     int                     i_pos;
420     int                     i;
421     boolean_t               b_first_packet;
422
423     p_method = ( thread_dvd_data_t * ) p_input->p_plugin_data;
424     p_netlist = ( netlist_t * ) p_input->p_method_data;
425
426     /* Get an iovec pointer */
427     if( ( p_vec = input_NetlistGetiovec( p_netlist, &p_data ) ) == NULL )
428     {
429         intf_ErrMsg( "DVD: read error" );
430         return -1;
431     }
432
433     /* Reads from DVD */
434     readv( p_input->i_handle, p_vec, p_method->i_read_once );
435
436 #if defined( HAVE_SYS_DVDIO_H ) || defined( LINUX_DVD )
437     if( p_method->b_encrypted )
438     {
439         for( i=0 ; i<p_method->i_read_once ; i++ )
440         {
441             CSSDescrambleSector(
442                         p_method->css.p_title_key[p_method->i_title].key, 
443                         p_vec[i].iov_base );
444             ((u8*)(p_vec[i].iov_base))[0x14] &= 0x8F;
445         }
446     }
447 #endif
448
449     /* Update netlist indexes */
450     input_NetlistMviovec( p_netlist, p_method->i_read_once );
451
452     i_packet = 0;
453     /* Read headers to compute payload length */
454     for( i = 0 ; i < p_method->i_read_once ; i++ )
455     {
456         i_pos = 0;
457         b_first_packet = 1;
458         while( i_pos < p_netlist->i_buffer_size )
459         {
460             pi_cur = (u8*)(p_vec[i].iov_base + i_pos);
461             /*default header */
462             if( U32_AT( pi_cur ) != 0x1BA )
463             {
464                 /* That's the case for all packets, except pack header. */
465                 i_packet_size = U16_AT( pi_cur + 4 );
466             }
467             else
468             {
469                 /* Pack header. */
470                 if( ( pi_cur[4] & 0xC0 ) == 0x40 )
471                 {
472                     /* MPEG-2 */
473                     i_packet_size = 8;
474                 }
475                 else if( ( pi_cur[4] & 0xF0 ) == 0x20 )
476                 {
477                     /* MPEG-1 */
478                     i_packet_size = 6;
479                 }
480                 else
481                 {
482                     intf_ErrMsg( "Unable to determine stream type" );
483                     return( -1 );
484                 }
485             }
486             if( b_first_packet )
487             {
488                 p_data->b_discard_payload = 0;
489                 b_first_packet = 0;
490             }
491             else
492             { 
493                 p_data = input_NetlistNewPacket( p_netlist ,
494                                                  i_packet_size + 6 );
495                 memcpy( p_data->p_buffer,
496                         p_vec[i].iov_base + i_pos , i_packet_size + 6 );
497             }
498
499             p_data->p_payload_end = p_data->p_payload_start + i_packet_size + 6;
500             pp_packets[i_packet] = p_data;
501             i_packet++;
502             i_pos += i_packet_size + 6;
503         }
504     }
505     pp_packets[i_packet] = NULL;
506
507     vlc_mutex_lock( &p_input->stream.stream_lock );
508     p_input->stream.i_tell += p_method->i_read_once *DVD_LB_SIZE;
509     vlc_mutex_unlock( &p_input->stream.stream_lock );
510
511     return( 0 );
512 }
513
514
515 /*****************************************************************************
516  * DVDRewind : reads a stream backward
517  *****************************************************************************/
518 static int DVDRewind( input_thread_t * p_input )
519 {
520     return( -1 );
521 }
522
523 /*****************************************************************************
524  * DVDSeek : Goes to a given position on the stream ; this one is used by the 
525  * input and translate chronological position from input to logical postion
526  * on the device
527  *****************************************************************************/
528 static void DVDSeek( input_thread_t * p_input, off_t i_off )
529 {
530     return;
531 }