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.14 2001/02/19 03:12:26 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 *****************************************************************************/
86 /* The ISO 639 language codes.
87 * Language names with * prefix are not spelled in their own language
89 {" ", "Not Specified"},
97 {"az", "*Azerbaijani"},
99 {"be", "*Byelorussian"},
100 {"bg", "*Bulgarian"},
103 {"bn", "*Bengali; Bangla"},
108 {"cs", "*Czech(Ceske)"},
115 {"eo", "*Esperanto"},
126 {"gd", "*Scots Gaelic"},
131 {"he", "*Hebrew"}, // formerly iw
133 {"hr", "Hrvatski"}, // Croatian
136 {"ia", "*Interlingua"},
137 {"id", "*Indonesian"}, // formerly in
138 {"ie", "*Interlingue"},
140 {"in", "*Indonesian"}, // replaced by id
143 {"iu", "*Inuktitut"},
144 {"iw", "*Hebrew"}, // replaced by he
146 {"ji", "*Yiddish"}, // replaced by yi
150 {"kl", "*Greenlandic"},
151 {"km", "*Cambodian"},
160 {"lt", "*Lithuanian"},
161 {"lv", "*Latvian, Lettish"},
164 {"mk", "*Macedonian"},
165 {"ml", "*Malayalam"},
166 {"mn", "*Mongolian"},
167 {"mo", "*Moldavian"},
174 {"nl", "Nederlands"},
177 {"om", "*(Afan) Oromo"},
181 {"ps", "*Pashto, Pushto"},
184 {"rm", "*Rhaeto-Romance"},
188 {"rw", "*Kinyarwanda"},
192 {"sh", "*Serbo-Croatian"},
193 {"si", "*Sinhalese"},
195 {"sl", "*Slovenian"},
203 {"su", "*Sundanese"},
220 {"uk", "*Ukrainian"},
223 {"vi", "*Vietnamese"},
227 {"yi", "*Yiddish"}, // formerly ji
235 /*****************************************************************************
237 *****************************************************************************/
238 static int DVDProbe ( probedata_t *p_data );
239 static int DVDCheckCSS ( struct input_thread_s * );
240 static int DVDRead ( struct input_thread_s *, data_packet_t ** );
241 static void DVDInit ( struct input_thread_s * );
242 static void DVDEnd ( struct input_thread_s * );
243 static void DVDSeek ( struct input_thread_s *, off_t );
244 static int DVDRewind ( struct input_thread_s * );
246 /*****************************************************************************
247 * Functions exported as capabilities. They are declared as static so that
248 * we don't pollute the namespace too much.
249 *****************************************************************************/
250 void input_getfunctions( function_list_t * p_function_list )
252 #define input p_function_list->functions.input
253 p_function_list->pf_probe = DVDProbe;
254 input.pf_init = DVDInit;
255 input.pf_open = input_FileOpen;
256 input.pf_close = input_FileClose;
257 input.pf_end = DVDEnd;
258 input.pf_read = DVDRead;
259 input.pf_demux = input_DemuxPS;
260 input.pf_new_packet = input_NetlistNewPacket;
261 input.pf_new_pes = input_NetlistNewPES;
262 input.pf_delete_packet = input_NetlistDeletePacket;
263 input.pf_delete_pes = input_NetlistDeletePES;
264 input.pf_rewind = DVDRewind;
265 input.pf_seek = DVDSeek;
270 * Local tools to decode some data in ifo
273 /*****************************************************************************
274 * Language: gives the long language name from the two-letters ISO-639 code
275 *****************************************************************************/
276 static char * Language( char * p_code )
280 while( !memcmp( lang_tbl[i].p_code, p_code, 2 ) && lang_tbl[i].p_lang_long )
285 return lang_tbl[i].p_lang_long;
289 * Data reading functions
292 /*****************************************************************************
293 * DVDProbe: verifies that the stream is a PS stream
294 *****************************************************************************/
295 static int DVDProbe( probedata_t *p_data )
297 input_thread_t * p_input = (input_thread_t *)p_data;
299 char * psz_name = p_input->p_source;
303 if( TestMethod( INPUT_METHOD_VAR, "dvd" ) )
308 if( ( strlen(psz_name) > 4 ) && !strncasecmp( psz_name, "dvd:", 4 ) )
310 /* If the user specified "dvd:" then it's probably a DVD */
315 i_handle = open( psz_name, 0 );
325 /*****************************************************************************
326 * DVDCheckCSS: check the stream
327 *****************************************************************************/
328 static int DVDCheckCSS( input_thread_t * p_input )
330 #if defined( HAVE_SYS_DVDIO_H ) || defined( LINUX_DVD )
331 return CSSTest( p_input->i_handle );
333 /* DVD ioctls unavailable.
334 * FIXME: Check the stream to see whether it is encrypted or not
335 * to give and accurate error message */
340 /*****************************************************************************
341 * DVDSetRegion: initialize input data for title x, chapter y.
342 * It should be called for each user navigation request, and to change
343 * audio or sub-picture streams.
345 * Take care that i_title and i_chapter start from 0.
346 *****************************************************************************/
347 static int DVDSetRegionParameters( input_thread_t * p_input,
348 int i_title, int i_chapter,
349 int i_audio, int i_spu )
351 thread_dvd_data_t * p_method;
361 p_method = (thread_dvd_data_t*)p_input->p_plugin_data;
363 /* Set selected title start and size */
364 p_input->stream.i_title = i_title;
366 p_pgc = &p_method->ifo.p_vts[i_title].pgci_ti.p_srp[0].pgc;
368 /* Find cell index in Program chain */
369 i_index = p_pgc->prg_map.pi_entry_cell[i_chapter] - 1;
371 /* Search for cell_index in cell adress_table */
373 while( p_pgc->p_cell_pos_inf[i_index].i_vob_id >
374 p_method->ifo.p_vts[i_title].c_adt.p_cell_inf[i_cell].i_vob_id )
378 while( p_pgc->p_cell_pos_inf[i_index].i_cell_id >
379 p_method->ifo.p_vts[i_title].c_adt.p_cell_inf[i_cell].i_cell_id )
383 i_start_cell = i_cell;
384 i_end_cell = i_start_cell + p_pgc->i_cell_nb - 1;
386 intf_WarnMsg( 2, "DVD: Start cell: %d End Cell: %d",
387 i_start_cell, i_end_cell );
389 p_method->i_start_cell = i_start_cell;
390 p_method->i_end_cell = i_end_cell;
392 /* start is : beginning of vts + offset to vobs + offset to vob x */
393 i_start = p_method->ifo.p_vts[i_title].i_pos + DVD_LB_SIZE *
394 ( p_method->ifo.p_vts[i_title].mat.i_tt_vobs_ssector +
395 p_method->ifo.p_vts[i_title].c_adt.p_cell_inf[i_start_cell].i_ssector );
396 p_method->i_start_byte = i_start;
398 i_start = lseek( p_input->i_handle, i_start, SEEK_SET );
399 intf_WarnMsg( 3, "DVD: VOBstart at: %lld", i_start );
402 ( p_method->ifo.p_vts[i_title].c_adt.p_cell_inf[i_end_cell].i_esector -
403 p_method->ifo.p_vts[i_title].c_adt.p_cell_inf[i_start_cell].i_ssector + 1 )
405 intf_WarnMsg( 3, "DVD: stream size: %lld", i_size );
407 if( p_input->stream.b_seekable )
409 stream_ps_data_t * p_demux_data =
410 (stream_ps_data_t *)p_input->stream.pp_programs[0]->p_demux_data;
412 /* Pre-parse the stream to gather stream_descriptor_t. */
413 p_input->stream.pp_programs[0]->b_is_ok = 0;
414 p_demux_data->i_PSM_version = EMPTY_PSM_VERSION;
416 while( !p_input->b_die && !p_input->b_error
417 && !p_demux_data->b_has_PSM )
420 data_packet_t * pp_packets[INPUT_READ_ONCE];
422 i_result = DVDRead( p_input, pp_packets );
426 vlc_mutex_lock( &p_input->stream.stream_lock );
427 p_input->stream.pp_programs[0]->b_is_ok = 1;
428 vlc_mutex_unlock( &p_input->stream.stream_lock );
433 p_input->b_error = 1;
437 for( i = 0; i < INPUT_READ_ONCE && pp_packets[i] != NULL; i++ )
439 /* FIXME: use i_p_config_t */
440 input_ParsePS( p_input, pp_packets[i] );
441 input_NetlistDeletePacket( p_input->p_method_data,
446 if( p_input->stream.i_tell > INPUT_PREPARSE_LENGTH )
451 lseek( p_input->i_handle, i_start, SEEK_SET );
452 vlc_mutex_lock( &p_input->stream.stream_lock );
454 /* i_tell is an indicator from the beginning of the stream,
456 p_input->stream.i_tell = 0;
458 if( p_demux_data->b_has_PSM )
460 /* (The PSM decoder will care about spawning the decoders) */
461 p_input->stream.pp_programs[0]->b_is_ok = 1;
466 /* (We have to do it ourselves) */
469 /* FIXME: we should do multiple passes in case an audio type
472 i_es < p_input->stream.pp_programs[0]->i_es_number;
475 #define p_es p_input->stream.pp_programs[0]->pp_es[i_es]
476 switch( p_es->i_type )
480 input_SelectES( p_input, p_es );
485 if( main_GetIntVariable( INPUT_CHANNEL_VAR, 0 )
486 == (p_es->i_id & 0x1F) )
487 switch( main_GetIntVariable( INPUT_AUDIO_VAR, 0 ) )
490 main_PutIntVariable( INPUT_AUDIO_VAR,
493 input_SelectES( p_input, p_es );
498 if( main_GetIntVariable( INPUT_CHANNEL_VAR, 0 )
499 == ((p_es->i_id & 0xF00) >> 8) )
500 switch( main_GetIntVariable( INPUT_AUDIO_VAR, 0 ) )
503 main_PutIntVariable( INPUT_AUDIO_VAR,
506 input_SelectES( p_input, p_es );
511 if( main_GetIntVariable( INPUT_SUBTITLE_VAR, -1 )
512 == ((p_es->i_id & 0x1F00) >> 8) )
514 input_SelectES( p_input, p_es );
527 input_DumpStream( p_input );
530 /* FIXME : ugly kludge */
531 p_input->stream.i_size = i_size;
533 vlc_mutex_unlock( &p_input->stream.stream_lock );
537 /* The programs will be added when we read them. */
538 vlc_mutex_lock( &p_input->stream.stream_lock );
539 p_input->stream.pp_programs[0]->b_is_ok = 0;
541 /* FIXME : ugly kludge */
542 p_input->stream.i_size = i_size;
544 vlc_mutex_unlock( &p_input->stream.stream_lock );
551 /*****************************************************************************
552 * DVDInit: initializes DVD structures
553 *****************************************************************************/
554 static void DVDInit( input_thread_t * p_input )
556 thread_dvd_data_t * p_method;
558 if( (p_method = malloc( sizeof(thread_dvd_data_t) )) == NULL )
560 intf_ErrMsg( "Out of memory" );
561 p_input->b_error = 1;
565 p_input->p_plugin_data = (void *)p_method;
566 p_input->p_method_data = NULL;
568 p_method->i_fd = p_input->i_handle;
569 /* FIXME: read several packets once */
570 p_method->i_read_once = 1;
571 p_method->b_encrypted = DVDCheckCSS( p_input );
573 lseek( p_input->i_handle, 0, SEEK_SET );
575 /* Reading structures initialisation */
576 input_NetlistInit( p_input, 4096, 4096, DVD_LB_SIZE,
577 p_method->i_read_once );
579 /* Ifo initialisation */
580 p_method->ifo = IfoInit( p_input->i_handle );
582 /* CSS initialisation */
583 if( p_method->b_encrypted )
586 #if defined( HAVE_SYS_DVDIO_H ) || defined( LINUX_DVD )
587 p_method->css = CSSInit( p_input->i_handle );
589 if( ( p_input->b_error = p_method->css.b_error ) )
591 intf_ErrMsg( "CSS fatal error" );
595 intf_ErrMsg( "Unscrambling not supported" );
596 p_input->b_error = 1;
601 /* Ifo structures reading */
602 IfoRead( &(p_method->ifo) );
603 intf_WarnMsg( 3, "Ifo: Initialized" );
605 /* CSS title keys decryption */
606 if( p_method->b_encrypted )
609 #if defined( HAVE_SYS_DVDIO_H ) || defined( LINUX_DVD )
612 p_method->css.i_title_nb = p_method->ifo.vmg.mat.i_tts_nb;
614 if( (p_method->css.p_title_key =
615 malloc( p_method->css.i_title_nb *sizeof(title_key_t) ) ) == NULL )
617 intf_ErrMsg( "Out of memory" );
618 p_input->b_error = 1;
622 for( i=0 ; i<p_method->css.i_title_nb ; i++ )
624 p_method->css.p_title_key[i].i =
625 p_method->ifo.p_vts[i].i_pos +
626 p_method->ifo.p_vts[i].mat.i_tt_vobs_ssector * DVD_LB_SIZE;
629 CSSGetKeys( &(p_method->css) );
631 intf_WarnMsg( 3, "CSS: initialized" );
633 intf_ErrMsg( "Unscrambling not supported" );
634 p_input->b_error = 1;
639 /* Initialize ES structures */
640 input_InitStream( p_input, sizeof( stream_ps_data_t ) );
641 input_AddProgram( p_input, 0, sizeof( stream_ps_data_t ) );
643 /* Set stream data */
644 vlc_mutex_lock( &p_input->stream.stream_lock );
645 p_input->stream.i_title_nb = p_method->ifo.vmg.mat.i_tts_nb;
646 vlc_mutex_unlock( &p_input->stream.stream_lock );
648 /* By default, set all parameters to 0 */
649 /* FIXME: wrong kludge to get title nb */
650 DVDSetRegionParameters( p_input,
651 p_method->ifo.vmg.ptt_srpt.p_tts[0].i_tts_nb - 1,
657 /*****************************************************************************
658 * DVDEnd: frees unused data
659 *****************************************************************************/
660 static void DVDEnd( input_thread_t * p_input )
662 /* FIXME: check order of calls */
663 // CSSEnd( p_input );
664 // IfoEnd( (ifo_t*)(&p_input->p_plugin_data->ifo ) );
665 free( p_input->stream.p_demux_data );
666 free( p_input->p_plugin_data );
667 input_NetlistEnd( p_input );
670 /*****************************************************************************
671 * DVDRead: reads data packets into the netlist.
672 *****************************************************************************
673 * Returns -1 in case of error, 0 if everything went well, and 1 in case of
675 *****************************************************************************/
676 static int DVDRead( input_thread_t * p_input,
677 data_packet_t ** pp_packets )
679 thread_dvd_data_t * p_method;
680 netlist_t * p_netlist;
681 struct iovec * p_vec;
682 struct data_packet_s * p_data;
688 boolean_t b_first_packet;
690 p_method = ( thread_dvd_data_t * ) p_input->p_plugin_data;
691 p_netlist = ( netlist_t * ) p_input->p_method_data;
693 /* Get an iovec pointer */
694 if( ( p_vec = input_NetlistGetiovec( p_netlist ) ) == NULL )
696 intf_ErrMsg( "DVD: read error" );
701 readv( p_input->i_handle, p_vec, p_method->i_read_once );
703 #if defined( HAVE_SYS_DVDIO_H ) || defined( LINUX_DVD )
704 if( p_method->b_encrypted )
706 for( i=0 ; i<p_method->i_read_once ; i++ )
709 p_method->css.p_title_key[p_input->stream.i_title].key,
711 ((u8*)(p_vec[i].iov_base))[0x14] &= 0x8F;
716 /* Update netlist indexes */
717 input_NetlistMviovec( p_netlist, p_method->i_read_once, &p_data );
720 /* Read headers to compute payload length */
721 for( i = 0 ; i < p_method->i_read_once ; i++ )
725 while( i_pos < p_netlist->i_buffer_size )
727 pi_cur = (u8*)(p_vec[i].iov_base + i_pos);
729 if( U32_AT( pi_cur ) != 0x1BA )
731 /* That's the case for all packets, except pack header. */
732 i_packet_size = U16_AT( pi_cur + 4 );
737 if( ( pi_cur[4] & 0xC0 ) == 0x40 )
742 else if( ( pi_cur[4] & 0xF0 ) == 0x20 )
749 intf_ErrMsg( "Unable to determine stream type" );
755 p_data->b_discard_payload = 0;
760 p_data = input_NetlistNewPacket( p_netlist ,
762 memcpy( p_data->p_buffer,
763 p_vec[i].iov_base + i_pos , i_packet_size + 6 );
766 p_data->p_payload_end = p_data->p_payload_start + i_packet_size + 6;
767 pp_packets[i_packet] = p_data;
769 i_pos += i_packet_size + 6;
772 pp_packets[i_packet] = NULL;
774 vlc_mutex_lock( &p_input->stream.stream_lock );
775 p_input->stream.i_tell += p_method->i_read_once *DVD_LB_SIZE;
776 vlc_mutex_unlock( &p_input->stream.stream_lock );
782 /*****************************************************************************
783 * DVDRewind : reads a stream backward
784 *****************************************************************************/
785 static int DVDRewind( input_thread_t * p_input )
790 /*****************************************************************************
791 * DVDSeek : Goes to a given position on the stream ; this one is used by the
792 * input and translate chronological position from input to logical postion
794 *****************************************************************************/
795 static void DVDSeek( input_thread_t * p_input, off_t i_off )
797 thread_dvd_data_t * p_method;
800 p_method = ( thread_dvd_data_t * )p_input->p_plugin_data;
802 /* We have to take care of offset of beginning of title */
803 i_pos = i_off + p_method->i_start_byte;
805 /* With DVD, we have to be on a sector boundary */
806 i_pos = i_pos & (~0x7ff);
808 i_pos = lseek( p_input->i_handle, i_pos, SEEK_SET );
810 p_input->stream.i_tell = i_pos - p_method->i_start_byte;