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.10 2001/02/14 04:11:01 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 DVDEnd ( struct input_thread_s * );
84 static void DVDSeek ( struct input_thread_s *, off_t );
85 static int DVDRewind ( struct input_thread_s * );
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 )
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;
111 * Data reading functions
114 /*****************************************************************************
115 * DVDProbe: verifies that the stream is a PS stream
116 *****************************************************************************/
117 static int DVDProbe( probedata_t *p_data )
119 if( TestMethod( INPUT_METHOD_VAR, "dvd" ) )
127 /*****************************************************************************
128 * DVDCheckCSS: check the stream
129 *****************************************************************************/
130 static int DVDCheckCSS( input_thread_t * p_input )
132 #if defined( HAVE_SYS_DVDIO_H ) || defined( LINUX_DVD )
133 return CSSTest( p_input->i_handle );
135 /* DVD ioctls unavailable.
136 * FIXME: Check the stream to see whether it is encrypted or not
137 * to give and accurate error message */
142 /*****************************************************************************
143 * DVDInit: initializes DVD structures
144 *****************************************************************************/
145 static void DVDInit( input_thread_t * p_input )
147 thread_dvd_data_t * p_method;
150 int i_cell, i_cell_1, i_start_cell, i_end_cell;
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;
166 p_method->b_encrypted = DVDCheckCSS( p_input );
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 );
177 /* CSS initialisation */
178 if( p_method->b_encrypted )
181 #if defined( HAVE_SYS_DVDIO_H ) || defined( LINUX_DVD )
182 p_method->css = CSSInit( p_input->i_handle );
184 if( ( p_input->b_error = p_method->css.b_error ) )
186 intf_ErrMsg( "CSS fatal error" );
190 intf_ErrMsg( "Unscrambling not supported" );
191 p_input->b_error = 1;
196 /* Ifo structures reading */
197 IfoRead( &(p_method->ifo) );
198 intf_WarnMsg( 3, "Ifo: Initialized" );
201 if( p_method->b_encrypted )
204 #if defined( HAVE_SYS_DVDIO_H ) || defined( LINUX_DVD )
207 p_method->css.i_title_nb = p_method->ifo.vmg.mat.i_tts_nb;
209 if( (p_method->css.p_title_key =
210 malloc( p_method->css.i_title_nb *sizeof(title_key_t) ) ) == NULL )
212 intf_ErrMsg( "Out of memory" );
213 p_input->b_error = 1;
217 for( i=0 ; i<p_method->css.i_title_nb ; i++ )
219 p_method->css.p_title_key[i].i =
220 p_method->ifo.p_vts[i].i_pos +
221 p_method->ifo.p_vts[i].mat.i_tt_vobs_ssector * DVD_LB_SIZE;
224 CSSGetKeys( &(p_method->css) );
226 intf_WarnMsg( 3, "CSS: initialized" );
228 intf_ErrMsg( "Unscrambling not supported" );
229 p_input->b_error = 1;
234 /* FIXME: Kludge beginning and end of the stream in vts_01_1.vob */
236 /* Determines which vob contains the movie */
242 /* Loop on the number of vobs */
243 while( p_method->ifo.p_vts[0].c_adt.p_cell_inf[i_cell].i_vob_id <=
244 p_method->ifo.p_vts[0].c_adt.i_vob_nb )
248 /* Loop to find the number of cells in the vob */
252 if( i_cell >= p_method->ifo.p_vts[0].c_adt.i_cell_nb )
257 while( p_method->ifo.p_vts[0].c_adt.p_cell_inf[i_cell-1].i_cell_id <
258 p_method->ifo.p_vts[0].c_adt.p_cell_inf[i_cell].i_cell_id );
261 if( p_method->ifo.p_vts[0].c_adt.p_cell_inf[i_cell-1].i_cell_id >
262 p_method->ifo.p_vts[0].c_adt.p_cell_inf[i_end_cell].i_cell_id )
264 i_start_cell = i_cell_1;
265 i_end_cell = i_cell - 1;
269 /* The preceding does not work with all DVD, so we give the
270 * last cell of the title as end */
271 i_end_cell = p_method->ifo.p_vts[0].c_adt.i_cell_nb - 1;
273 intf_WarnMsg( 2, "DVD: Start cell: %d End Cell: %d",
274 i_start_cell, i_end_cell );
276 p_method->i_start_cell = i_start_cell;
277 p_method->i_end_cell = i_end_cell;
279 /* start is : beginning of vts + offset to vobs + offset to vob x */
280 i_start = p_method->ifo.p_vts[0].i_pos + DVD_LB_SIZE *
281 ( p_method->ifo.p_vts[0].mat.i_tt_vobs_ssector +
282 p_method->ifo.p_vts[0].c_adt.p_cell_inf[i_start_cell].i_ssector );
283 p_method->i_start_byte = i_start;
285 i_start = lseek( p_input->i_handle, i_start, SEEK_SET );
286 intf_WarnMsg( 3, "DVD: VOBstart at: %lld", i_start );
289 ( p_method->ifo.p_vts[0].c_adt.p_cell_inf[i_end_cell].i_esector -
290 p_method->ifo.p_vts[0].c_adt.p_cell_inf[i_start_cell].i_ssector + 1 )
292 intf_WarnMsg( 3, "DVD: stream size: %lld", i_size );
295 /* Initialize ES structures */
296 input_InitStream( p_input, sizeof( stream_ps_data_t ) );
297 input_AddProgram( p_input, 0, sizeof( stream_ps_data_t ) );
299 if( p_input->stream.b_seekable )
301 stream_ps_data_t * p_demux_data =
302 (stream_ps_data_t *)p_input->stream.pp_programs[0]->p_demux_data;
304 /* Pre-parse the stream to gather stream_descriptor_t. */
305 p_input->stream.pp_programs[0]->b_is_ok = 0;
306 p_demux_data->i_PSM_version = EMPTY_PSM_VERSION;
308 while( !p_input->b_die && !p_input->b_error
309 && !p_demux_data->b_has_PSM )
312 data_packet_t * pp_packets[INPUT_READ_ONCE];
314 i_result = DVDRead( p_input, pp_packets );
318 vlc_mutex_lock( &p_input->stream.stream_lock );
319 p_input->stream.pp_programs[0]->b_is_ok = 1;
320 vlc_mutex_unlock( &p_input->stream.stream_lock );
325 p_input->b_error = 1;
329 for( i = 0; i < INPUT_READ_ONCE && pp_packets[i] != NULL; i++ )
331 /* FIXME: use i_p_config_t */
332 input_ParsePS( p_input, pp_packets[i] );
333 input_NetlistDeletePacket( p_input->p_method_data,
338 if( p_input->stream.i_tell > INPUT_PREPARSE_LENGTH )
343 lseek( p_input->i_handle, i_start, SEEK_SET );
344 vlc_mutex_lock( &p_input->stream.stream_lock );
346 /* i_tell is an indicator from the beginning of the stream,
348 p_input->stream.i_tell = 0;
350 if( p_demux_data->b_has_PSM )
352 /* (The PSM decoder will care about spawning the decoders) */
353 p_input->stream.pp_programs[0]->b_is_ok = 1;
358 /* (We have to do it ourselves) */
361 /* FIXME: we should do multiple passes in case an audio type
364 i_es < p_input->stream.pp_programs[0]->i_es_number;
367 #define p_es p_input->stream.pp_programs[0]->pp_es[i_es]
368 switch( p_es->i_type )
372 input_SelectES( p_input, p_es );
377 if( main_GetIntVariable( INPUT_CHANNEL_VAR, 0 )
378 == (p_es->i_id & 0x1F) )
379 switch( main_GetIntVariable( INPUT_AUDIO_VAR, 0 ) )
382 main_PutIntVariable( INPUT_AUDIO_VAR,
385 input_SelectES( p_input, p_es );
390 if( main_GetIntVariable( INPUT_CHANNEL_VAR, 0 )
391 == ((p_es->i_id & 0xF00) >> 8) )
392 switch( main_GetIntVariable( INPUT_AUDIO_VAR, 0 ) )
395 main_PutIntVariable( INPUT_AUDIO_VAR,
398 input_SelectES( p_input, p_es );
403 if( main_GetIntVariable( INPUT_SUBTITLE_VAR, -1 )
404 == ((p_es->i_id & 0x1F00) >> 8) )
406 input_SelectES( p_input, p_es );
419 input_DumpStream( p_input );
422 /* FIXME : ugly kludge */
423 p_input->stream.i_size = i_size;
425 vlc_mutex_unlock( &p_input->stream.stream_lock );
429 /* The programs will be added when we read them. */
430 vlc_mutex_lock( &p_input->stream.stream_lock );
431 p_input->stream.pp_programs[0]->b_is_ok = 0;
433 /* FIXME : ugly kludge */
434 p_input->stream.i_size = i_size;
436 vlc_mutex_unlock( &p_input->stream.stream_lock );
441 /*****************************************************************************
442 * DVDEnd: frees unused data
443 *****************************************************************************/
444 static void DVDEnd( input_thread_t * p_input )
446 /* FIXME: check order of calls */
447 // CSSEnd( p_input );
448 // IfoEnd( (ifo_t*)(&p_input->p_plugin_data->ifo ) );
449 free( p_input->stream.p_demux_data );
450 free( p_input->p_plugin_data );
451 input_NetlistEnd( p_input );
454 /*****************************************************************************
455 * DVDRead: reads data packets into the netlist.
456 *****************************************************************************
457 * Returns -1 in case of error, 0 if everything went well, and 1 in case of
459 *****************************************************************************/
460 static int DVDRead( input_thread_t * p_input,
461 data_packet_t ** pp_packets )
463 thread_dvd_data_t * p_method;
464 netlist_t * p_netlist;
465 struct iovec * p_vec;
466 struct data_packet_s * p_data;
472 boolean_t b_first_packet;
474 p_method = ( thread_dvd_data_t * ) p_input->p_plugin_data;
475 p_netlist = ( netlist_t * ) p_input->p_method_data;
477 /* Get an iovec pointer */
478 if( ( p_vec = input_NetlistGetiovec( p_netlist, &p_data ) ) == NULL )
480 intf_ErrMsg( "DVD: read error" );
485 readv( p_input->i_handle, p_vec, p_method->i_read_once );
487 #if defined( HAVE_SYS_DVDIO_H ) || defined( LINUX_DVD )
488 if( p_method->b_encrypted )
490 for( i=0 ; i<p_method->i_read_once ; i++ )
493 p_method->css.p_title_key[p_method->i_title].key,
495 ((u8*)(p_vec[i].iov_base))[0x14] &= 0x8F;
500 /* Update netlist indexes */
501 input_NetlistMviovec( p_netlist, p_method->i_read_once );
504 /* Read headers to compute payload length */
505 for( i = 0 ; i < p_method->i_read_once ; i++ )
509 while( i_pos < p_netlist->i_buffer_size )
511 pi_cur = (u8*)(p_vec[i].iov_base + i_pos);
513 if( U32_AT( pi_cur ) != 0x1BA )
515 /* That's the case for all packets, except pack header. */
516 i_packet_size = U16_AT( pi_cur + 4 );
521 if( ( pi_cur[4] & 0xC0 ) == 0x40 )
526 else if( ( pi_cur[4] & 0xF0 ) == 0x20 )
533 intf_ErrMsg( "Unable to determine stream type" );
539 p_data->b_discard_payload = 0;
544 p_data = input_NetlistNewPacket( p_netlist ,
546 memcpy( p_data->p_buffer,
547 p_vec[i].iov_base + i_pos , i_packet_size + 6 );
550 p_data->p_payload_end = p_data->p_payload_start + i_packet_size + 6;
551 pp_packets[i_packet] = p_data;
553 i_pos += i_packet_size + 6;
556 pp_packets[i_packet] = NULL;
558 vlc_mutex_lock( &p_input->stream.stream_lock );
559 p_input->stream.i_tell += p_method->i_read_once *DVD_LB_SIZE;
560 vlc_mutex_unlock( &p_input->stream.stream_lock );
566 /*****************************************************************************
567 * DVDRewind : reads a stream backward
568 *****************************************************************************/
569 static int DVDRewind( input_thread_t * p_input )
574 /*****************************************************************************
575 * DVDSeek : Goes to a given position on the stream ; this one is used by the
576 * input and translate chronological position from input to logical postion
578 *****************************************************************************/
579 static void DVDSeek( input_thread_t * p_input, off_t i_off )
581 thread_dvd_data_t * p_method;
584 p_method = ( thread_dvd_data_t * )p_input->p_plugin_data;
586 /* We have to take care of offset of beginning of title */
587 i_pos = i_off + p_method->i_start_byte;
589 /* With DVD, we have to be on a sector boundary */
590 i_pos = i_pos & (~0x7ff);
592 i_pos = lseek( p_input->i_handle, i_pos, SEEK_SET );
594 p_input->stream.i_tell = i_pos - p_method->i_start_byte;