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.5 2001/02/08 17:44:12 massiot 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 #if defined( HAVE_SYS_DVDIO_H ) || defined( LINUX_DVD )
180 /* CSS authentication and keys */
181 if( ( p_method->b_encrypted = DVDCheckCSS( p_input ) ) )
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" );
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;
209 i_start = lseek( p_input->i_handle, i_start, SEEK_SET );
210 intf_Msg( "VOB start at : %lld", (long long)i_start );
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 ) );
216 if( p_input->stream.b_seekable )
218 stream_ps_data_t * p_demux_data =
219 (stream_ps_data_t *)p_input->stream.pp_programs[0]->p_demux_data;
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;
225 while( !p_input->b_die && !p_input->b_error
226 && !p_demux_data->b_has_PSM )
229 data_packet_t * pp_packets[INPUT_READ_ONCE];
231 i_result = DVDRead( p_input, pp_packets );
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 );
242 p_input->b_error = 1;
246 for( i = 0; i < INPUT_READ_ONCE && pp_packets[i] != NULL; i++ )
248 /* FIXME: use i_p_config_t */
249 input_ParsePS( p_input, pp_packets[i] );
250 input_NetlistDeletePacket( p_input->p_method_data,
255 if( p_input->stream.i_tell > INPUT_PREPARSE_LENGTH )
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 )
265 /* (The PSM decoder will care about spawning the decoders) */
266 p_input->stream.pp_programs[0]->b_is_ok = 1;
271 /* (We have to do it ourselves) */
274 /* FIXME: we should do multiple passes in case an audio type
277 i_es < p_input->stream.pp_programs[0]->i_es_number;
280 #define p_es p_input->stream.pp_programs[0]->pp_es[i_es]
281 switch( p_es->i_type )
285 input_SelectES( p_input, p_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 ) )
295 main_PutIntVariable( INPUT_DVD_AUDIO_VAR,
298 input_SelectES( p_input, p_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 ) )
308 main_PutIntVariable( INPUT_DVD_AUDIO_VAR,
311 input_SelectES( p_input, p_es );
316 if( main_GetIntVariable( INPUT_DVD_SUBTITLE_VAR, -1 )
317 == ((p_es->i_id & 0x1F00) >> 8) )
319 input_SelectES( p_input, p_es );
332 input_DumpStream( p_input );
334 vlc_mutex_unlock( &p_input->stream.stream_lock );
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 );
346 /*****************************************************************************
347 * DVDOpen : open the dvd device
348 *****************************************************************************/
349 static void DVDOpen( input_thread_t * p_input )
351 intf_Msg( "input: opening DVD %s", p_input->p_source );
353 p_input->i_handle = open( p_input->p_source, O_RDONLY | O_NONBLOCK );
355 if( p_input->i_handle == -1 )
357 intf_ErrMsg( "input error: cannot open device (%s)", strerror(errno) );
358 p_input->b_error = 1;
362 vlc_mutex_lock( &p_input->stream.stream_lock );
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;
369 vlc_mutex_unlock( &p_input->stream.stream_lock );
372 /*****************************************************************************
373 * DVDClose : close a file descriptor
374 *****************************************************************************/
375 static void DVDClose( input_thread_t * p_input )
377 close( p_input->i_handle );
382 /*****************************************************************************
383 * DVDEnd: frees unused data
384 *****************************************************************************/
385 static void DVDEnd( input_thread_t * p_input )
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 );
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
400 *****************************************************************************/
401 static int DVDRead( input_thread_t * p_input,
402 data_packet_t ** pp_packets )
404 thread_dvd_data_t * p_method;
405 netlist_t * p_netlist;
406 struct iovec * p_vec;
407 struct data_packet_s * p_data;
413 boolean_t b_first_packet;
415 p_method = ( thread_dvd_data_t * ) p_input->p_plugin_data;
416 p_netlist = ( netlist_t * ) p_input->p_method_data;
418 /* Get an iovec pointer */
419 if( ( p_vec = input_NetlistGetiovec( p_netlist, &p_data ) ) == NULL )
421 intf_ErrMsg( "DVD: read error" );
426 readv( p_input->i_handle, p_vec, p_method->i_read_once );
428 #if defined( HAVE_SYS_DVDIO_H ) || defined( LINUX_DVD )
429 if( p_method->b_encrypted )
431 for( i=0 ; i<p_method->i_read_once ; i++ )
434 p_method->css.p_title_key[p_method->i_title].key,
436 ((u8*)(p_vec[i].iov_base))[0x14] &= 0x8F;
441 /* Update netlist indexes */
442 input_NetlistMviovec( p_netlist, p_method->i_read_once );
445 /* Read headers to compute payload length */
446 for( i = 0 ; i < p_method->i_read_once ; i++ )
450 while( i_pos < p_netlist->i_buffer_size )
452 pi_cur = (u8*)(p_vec[i].iov_base + i_pos);
454 if( U32_AT( pi_cur ) != 0x1BA )
456 /* That's the case for all packets, except pack header. */
457 i_packet_size = U16_AT( pi_cur + 4 );
462 if( ( pi_cur[4] & 0xC0 ) == 0x40 )
467 else if( ( pi_cur[4] & 0xF0 ) == 0x20 )
474 intf_ErrMsg( "Unable to determine stream type" );
480 p_data->b_discard_payload = 0;
485 p_data = input_NetlistNewPacket( p_netlist ,
487 memcpy( p_data->p_buffer,
488 p_vec[i].iov_base + i_pos , i_packet_size + 6 );
491 p_data->p_payload_end = p_data->p_payload_start + i_packet_size + 6;
492 pp_packets[i_packet] = p_data;
494 i_pos += i_packet_size + 6;
497 pp_packets[i_packet] = NULL;
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 );
507 /*****************************************************************************
508 * DVDRewind : reads a stream backward
509 *****************************************************************************/
510 static int DVDRewind( input_thread_t * p_input )
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
519 *****************************************************************************/
520 static void DVDSeek( input_thread_t * p_input, off_t i_off )