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