1 /*****************************************************************************
2 * input_dvd.c: DVD raw reading plugin.
4 * This plugins should handle all the known specificities of the DVD format,
5 * especially the 2048 bytes logical block size.
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 $
15 * Author: Stéphane Borel <stef@via.ecp.fr>
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.
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.
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 *****************************************************************************/
32 /*****************************************************************************
34 *****************************************************************************/
40 #include <netinet/in.h>
43 #include <sys/types.h>
60 #include "stream_control.h"
61 #include "input_ext-intf.h"
62 #include "input_ext-dec.h"
65 #include "input_netlist.h"
69 #include "input_dvd.h"
70 #include "mpeg_system.h"
76 /*****************************************************************************
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 * );
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 )
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;
113 * Data reading functions
116 /*****************************************************************************
117 * DVDProbe: verifies that the stream is a PS stream
118 *****************************************************************************/
119 static int DVDProbe( probedata_t *p_data )
121 if( TestMethod( INPUT_METHOD_VAR, "dvd" ) )
129 /*****************************************************************************
130 * DVDCheckCSS: check the stream
131 *****************************************************************************/
132 static int DVDCheckCSS( input_thread_t * p_input )
134 #if defined( HAVE_SYS_DVDIO_H ) || defined( LINUX_DVD )
135 return CSSTest( p_input->i_handle );
137 /* DVD ioctls unavailable.
138 * FIXME: Check the stream to see whether it is encrypted or not
139 * to give and accurate error message */
144 /*****************************************************************************
145 * DVDInit: initializes DVD structures
146 *****************************************************************************/
147 static void DVDInit( input_thread_t * p_input )
149 thread_dvd_data_t * p_method;
152 if( (p_method = malloc( sizeof(thread_dvd_data_t) )) == NULL )
154 intf_ErrMsg( "Out of memory" );
155 p_input->b_error = 1;
159 p_input->p_plugin_data = (void *)p_method;
160 p_input->p_method_data = NULL;
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;
168 lseek( p_input->i_handle, 0, SEEK_SET );
170 /* Reading structures initialisation */
171 input_NetlistInit( p_input, 4096, 4096, DVD_LB_SIZE,
172 p_method->i_read_once );
174 /* Ifo initialisation */
175 p_method->ifo = IfoInit( p_input->i_handle );
176 IfoRead( &(p_method->ifo) );
177 intf_Msg( "Ifo: Initialized" );
179 /* CSS authentication and keys */
180 if( ( p_method->b_encrypted = DVDCheckCSS( p_input ) ) )
182 #if defined( HAVE_SYS_DVDIO_H ) || defined( LINUX_DVD )
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 )
190 intf_ErrMsg( "Out of memory" );
191 p_input->b_error = 1;
194 for( i=0 ; i<p_method->css.i_title_nb ; i++ )
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;
200 CSSGetKeys( &(p_method->css) );
201 intf_Msg( "CSS: Initialized" );
203 intf_ErrMsg( "Unscrambling not supported" );
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;
211 i_start = lseek( p_input->i_handle, i_start, SEEK_SET );
212 intf_Msg( "VOB start at : %lld", (long long)i_start );
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 ) );
218 if( p_input->stream.b_seekable )
220 stream_ps_data_t * p_demux_data =
221 (stream_ps_data_t *)p_input->stream.pp_programs[0]->p_demux_data;
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;
227 while( !p_input->b_die && !p_input->b_error
228 && !p_demux_data->b_has_PSM )
231 data_packet_t * pp_packets[INPUT_READ_ONCE];
233 i_result = DVDRead( p_input, pp_packets );
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 );
244 p_input->b_error = 1;
248 for( i = 0; i < INPUT_READ_ONCE && pp_packets[i] != NULL; i++ )
250 /* FIXME: use i_p_config_t */
251 input_ParsePS( p_input, pp_packets[i] );
252 input_NetlistDeletePacket( p_input->p_method_data,
257 if( p_input->stream.i_tell > INPUT_PREPARSE_LENGTH )
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 )
267 /* (The PSM decoder will care about spawning the decoders) */
268 p_input->stream.pp_programs[0]->b_is_ok = 1;
273 /* (We have to do it ourselves) */
276 /* FIXME: we should do multiple passes in case an audio type
279 i_es < p_input->stream.pp_programs[0]->i_es_number;
282 #define p_es p_input->stream.pp_programs[0]->pp_es[i_es]
283 switch( p_es->i_type )
287 input_SelectES( p_input, p_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 ) )
297 main_PutIntVariable( INPUT_DVD_AUDIO_VAR,
300 input_SelectES( p_input, p_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 ) )
310 main_PutIntVariable( INPUT_DVD_AUDIO_VAR,
313 input_SelectES( p_input, p_es );
318 if( main_GetIntVariable( INPUT_DVD_SUBTITLE_VAR, -1 )
319 == ((p_es->i_id & 0x1F00) >> 8) )
321 input_SelectES( p_input, p_es );
334 input_DumpStream( p_input );
336 vlc_mutex_unlock( &p_input->stream.stream_lock );
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 );
348 /*****************************************************************************
349 * DVDOpen : open the dvd device
350 *****************************************************************************/
351 static void DVDOpen( input_thread_t * p_input )
353 intf_Msg( "input: opening DVD %s", p_input->p_source );
355 p_input->i_handle = open( p_input->p_source, O_RDONLY | O_NONBLOCK );
357 if( p_input->i_handle == -1 )
359 intf_ErrMsg( "input error: cannot open device (%s)", strerror(errno) );
360 p_input->b_error = 1;
364 vlc_mutex_lock( &p_input->stream.stream_lock );
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;
371 vlc_mutex_unlock( &p_input->stream.stream_lock );
374 /*****************************************************************************
375 * DVDClose : close a file descriptor
376 *****************************************************************************/
377 static void DVDClose( input_thread_t * p_input )
379 close( p_input->i_handle );
384 /*****************************************************************************
385 * DVDEnd: frees unused data
386 *****************************************************************************/
387 static void DVDEnd( input_thread_t * p_input )
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 );
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
402 *****************************************************************************/
403 static int DVDRead( input_thread_t * p_input,
404 data_packet_t ** pp_packets )
406 thread_dvd_data_t * p_method;
407 netlist_t * p_netlist;
408 struct iovec * p_vec;
409 struct data_packet_s * p_data;
415 boolean_t b_first_packet;
417 p_method = ( thread_dvd_data_t * ) p_input->p_plugin_data;
418 p_netlist = ( netlist_t * ) p_input->p_method_data;
420 /* Get an iovec pointer */
421 if( ( p_vec = input_NetlistGetiovec( p_netlist, &p_data ) ) == NULL )
423 intf_ErrMsg( "DVD: read error" );
428 readv( p_input->i_handle, p_vec, p_method->i_read_once );
430 #if defined( HAVE_SYS_DVDIO_H ) || defined( LINUX_DVD )
431 if( p_method->b_encrypted )
433 for( i=0 ; i<p_method->i_read_once ; i++ )
436 p_method->css.p_title_key[p_method->i_title].key,
438 ((u8*)(p_vec[i].iov_base))[0x14] &= 0x8F;
443 /* Update netlist indexes */
444 input_NetlistMviovec( p_netlist, p_method->i_read_once );
447 /* Read headers to compute payload length */
448 for( i = 0 ; i < p_method->i_read_once ; i++ )
452 while( i_pos < p_netlist->i_buffer_size )
454 pi_cur = (u8*)(p_vec[i].iov_base + i_pos);
456 if( U32_AT( pi_cur ) != 0x1BA )
458 /* That's the case for all packets, except pack header. */
459 i_packet_size = U16_AT( pi_cur + 4 );
464 if( ( pi_cur[4] & 0xC0 ) == 0x40 )
469 else if( ( pi_cur[4] & 0xF0 ) == 0x20 )
476 intf_ErrMsg( "Unable to determine stream type" );
482 p_data->b_discard_payload = 0;
487 p_data = input_NetlistNewPacket( p_netlist ,
489 memcpy( p_data->p_buffer,
490 p_vec[i].iov_base + i_pos , i_packet_size + 6 );
493 p_data->p_payload_end = p_data->p_payload_start + i_packet_size + 6;
494 pp_packets[i_packet] = p_data;
496 i_pos += i_packet_size + 6;
499 pp_packets[i_packet] = NULL;
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 );
509 /*****************************************************************************
510 * DVDRewind : reads a stream backward
511 *****************************************************************************/
512 static int DVDRewind( input_thread_t * p_input )
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
521 *****************************************************************************/
522 static void DVDSeek( input_thread_t * p_input, off_t i_off )