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.12 2001/02/16 09:25:03 sam 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 input_thread_t * p_input = (input_thread_t *)p_data;
121 char * psz_name = p_input->p_source;
125 if( TestMethod( INPUT_METHOD_VAR, "dvd" ) )
130 if( ( strlen(psz_name) > 4 ) && !strncasecmp( psz_name, "dvd:", 4 ) )
132 /* If the user specified "dvd:" then it's probably a DVD */
137 i_handle = open( psz_name, 0 );
147 /*****************************************************************************
148 * DVDCheckCSS: check the stream
149 *****************************************************************************/
150 static int DVDCheckCSS( input_thread_t * p_input )
152 #if defined( HAVE_SYS_DVDIO_H ) || defined( LINUX_DVD )
153 return CSSTest( p_input->i_handle );
155 /* DVD ioctls unavailable.
156 * FIXME: Check the stream to see whether it is encrypted or not
157 * to give and accurate error message */
162 /*****************************************************************************
163 * DVDInit: initializes DVD structures
164 *****************************************************************************/
165 static void DVDInit( input_thread_t * p_input )
167 thread_dvd_data_t * p_method;
170 int i_cell, i_cell_1, i_start_cell, i_end_cell;
172 if( (p_method = malloc( sizeof(thread_dvd_data_t) )) == NULL )
174 intf_ErrMsg( "Out of memory" );
175 p_input->b_error = 1;
179 p_input->p_plugin_data = (void *)p_method;
180 p_input->p_method_data = NULL;
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 );
188 lseek( p_input->i_handle, 0, SEEK_SET );
190 /* Reading structures initialisation */
191 input_NetlistInit( p_input, 4096, 4096, DVD_LB_SIZE,
192 p_method->i_read_once );
194 /* Ifo initialisation */
195 p_method->ifo = IfoInit( p_input->i_handle );
197 /* CSS initialisation */
198 if( p_method->b_encrypted )
201 #if defined( HAVE_SYS_DVDIO_H ) || defined( LINUX_DVD )
202 p_method->css = CSSInit( p_input->i_handle );
204 if( ( p_input->b_error = p_method->css.b_error ) )
206 intf_ErrMsg( "CSS fatal error" );
210 intf_ErrMsg( "Unscrambling not supported" );
211 p_input->b_error = 1;
216 /* Ifo structures reading */
217 IfoRead( &(p_method->ifo) );
218 intf_WarnMsg( 3, "Ifo: Initialized" );
221 if( p_method->b_encrypted )
224 #if defined( HAVE_SYS_DVDIO_H ) || defined( LINUX_DVD )
227 p_method->css.i_title_nb = p_method->ifo.vmg.mat.i_tts_nb;
229 if( (p_method->css.p_title_key =
230 malloc( p_method->css.i_title_nb *sizeof(title_key_t) ) ) == NULL )
232 intf_ErrMsg( "Out of memory" );
233 p_input->b_error = 1;
237 for( i=0 ; i<p_method->css.i_title_nb ; i++ )
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;
244 CSSGetKeys( &(p_method->css) );
246 intf_WarnMsg( 3, "CSS: initialized" );
248 intf_ErrMsg( "Unscrambling not supported" );
249 p_input->b_error = 1;
254 /* FIXME: Kludge beginning and end of the stream in vts_01_1.vob */
256 /* Determines which vob contains the movie */
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 )
268 /* Loop to find the number of cells in the vob */
272 if( i_cell >= p_method->ifo.p_vts[0].c_adt.i_cell_nb )
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 );
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 )
284 i_start_cell = i_cell_1;
285 i_end_cell = i_cell - 1;
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;
293 intf_WarnMsg( 2, "DVD: Start cell: %d End Cell: %d",
294 i_start_cell, i_end_cell );
296 p_method->i_start_cell = i_start_cell;
297 p_method->i_end_cell = i_end_cell;
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;
305 i_start = lseek( p_input->i_handle, i_start, SEEK_SET );
306 intf_WarnMsg( 3, "DVD: VOBstart at: %lld", i_start );
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 )
312 intf_WarnMsg( 3, "DVD: stream size: %lld", i_size );
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 ) );
319 if( p_input->stream.b_seekable )
321 stream_ps_data_t * p_demux_data =
322 (stream_ps_data_t *)p_input->stream.pp_programs[0]->p_demux_data;
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;
328 while( !p_input->b_die && !p_input->b_error
329 && !p_demux_data->b_has_PSM )
332 data_packet_t * pp_packets[INPUT_READ_ONCE];
334 i_result = DVDRead( p_input, pp_packets );
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 );
345 p_input->b_error = 1;
349 for( i = 0; i < INPUT_READ_ONCE && pp_packets[i] != NULL; i++ )
351 /* FIXME: use i_p_config_t */
352 input_ParsePS( p_input, pp_packets[i] );
353 input_NetlistDeletePacket( p_input->p_method_data,
358 if( p_input->stream.i_tell > INPUT_PREPARSE_LENGTH )
363 lseek( p_input->i_handle, i_start, SEEK_SET );
364 vlc_mutex_lock( &p_input->stream.stream_lock );
366 /* i_tell is an indicator from the beginning of the stream,
368 p_input->stream.i_tell = 0;
370 if( p_demux_data->b_has_PSM )
372 /* (The PSM decoder will care about spawning the decoders) */
373 p_input->stream.pp_programs[0]->b_is_ok = 1;
378 /* (We have to do it ourselves) */
381 /* FIXME: we should do multiple passes in case an audio type
384 i_es < p_input->stream.pp_programs[0]->i_es_number;
387 #define p_es p_input->stream.pp_programs[0]->pp_es[i_es]
388 switch( p_es->i_type )
392 input_SelectES( p_input, p_es );
397 if( main_GetIntVariable( INPUT_CHANNEL_VAR, 0 )
398 == (p_es->i_id & 0x1F) )
399 switch( main_GetIntVariable( INPUT_AUDIO_VAR, 0 ) )
402 main_PutIntVariable( INPUT_AUDIO_VAR,
405 input_SelectES( p_input, p_es );
410 if( main_GetIntVariable( INPUT_CHANNEL_VAR, 0 )
411 == ((p_es->i_id & 0xF00) >> 8) )
412 switch( main_GetIntVariable( INPUT_AUDIO_VAR, 0 ) )
415 main_PutIntVariable( INPUT_AUDIO_VAR,
418 input_SelectES( p_input, p_es );
423 if( main_GetIntVariable( INPUT_SUBTITLE_VAR, -1 )
424 == ((p_es->i_id & 0x1F00) >> 8) )
426 input_SelectES( p_input, p_es );
439 input_DumpStream( p_input );
442 /* FIXME : ugly kludge */
443 p_input->stream.i_size = i_size;
445 vlc_mutex_unlock( &p_input->stream.stream_lock );
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;
453 /* FIXME : ugly kludge */
454 p_input->stream.i_size = i_size;
456 vlc_mutex_unlock( &p_input->stream.stream_lock );
461 /*****************************************************************************
462 * DVDEnd: frees unused data
463 *****************************************************************************/
464 static void DVDEnd( input_thread_t * p_input )
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 );
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
479 *****************************************************************************/
480 static int DVDRead( input_thread_t * p_input,
481 data_packet_t ** pp_packets )
483 thread_dvd_data_t * p_method;
484 netlist_t * p_netlist;
485 struct iovec * p_vec;
486 struct data_packet_s * p_data;
492 boolean_t b_first_packet;
494 p_method = ( thread_dvd_data_t * ) p_input->p_plugin_data;
495 p_netlist = ( netlist_t * ) p_input->p_method_data;
497 /* Get an iovec pointer */
498 if( ( p_vec = input_NetlistGetiovec( p_netlist ) ) == NULL )
500 intf_ErrMsg( "DVD: read error" );
505 readv( p_input->i_handle, p_vec, p_method->i_read_once );
507 #if defined( HAVE_SYS_DVDIO_H ) || defined( LINUX_DVD )
508 if( p_method->b_encrypted )
510 for( i=0 ; i<p_method->i_read_once ; i++ )
513 p_method->css.p_title_key[p_method->i_title].key,
515 ((u8*)(p_vec[i].iov_base))[0x14] &= 0x8F;
520 /* Update netlist indexes */
521 input_NetlistMviovec( p_netlist, p_method->i_read_once, &p_data );
524 /* Read headers to compute payload length */
525 for( i = 0 ; i < p_method->i_read_once ; i++ )
529 while( i_pos < p_netlist->i_buffer_size )
531 pi_cur = (u8*)(p_vec[i].iov_base + i_pos);
533 if( U32_AT( pi_cur ) != 0x1BA )
535 /* That's the case for all packets, except pack header. */
536 i_packet_size = U16_AT( pi_cur + 4 );
541 if( ( pi_cur[4] & 0xC0 ) == 0x40 )
546 else if( ( pi_cur[4] & 0xF0 ) == 0x20 )
553 intf_ErrMsg( "Unable to determine stream type" );
559 p_data->b_discard_payload = 0;
564 p_data = input_NetlistNewPacket( p_netlist ,
566 memcpy( p_data->p_buffer,
567 p_vec[i].iov_base + i_pos , i_packet_size + 6 );
570 p_data->p_payload_end = p_data->p_payload_start + i_packet_size + 6;
571 pp_packets[i_packet] = p_data;
573 i_pos += i_packet_size + 6;
576 pp_packets[i_packet] = NULL;
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 );
586 /*****************************************************************************
587 * DVDRewind : reads a stream backward
588 *****************************************************************************/
589 static int DVDRewind( input_thread_t * p_input )
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
598 *****************************************************************************/
599 static void DVDSeek( input_thread_t * p_input, off_t i_off )
601 thread_dvd_data_t * p_method;
604 p_method = ( thread_dvd_data_t * )p_input->p_plugin_data;
606 /* We have to take care of offset of beginning of title */
607 i_pos = i_off + p_method->i_start_byte;
609 /* With DVD, we have to be on a sector boundary */
610 i_pos = i_pos & (~0x7ff);
612 i_pos = lseek( p_input->i_handle, i_pos, SEEK_SET );
614 p_input->stream.i_tell = i_pos - p_method->i_start_byte;