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.15 2001/02/20 02:53:13 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 DVDSetArea ( struct input_thread_s *, int, int, int, int );
245 static int DVDRewind ( struct input_thread_s * );
247 /*****************************************************************************
248 * Functions exported as capabilities. They are declared as static so that
249 * we don't pollute the namespace too much.
250 *****************************************************************************/
251 void input_getfunctions( function_list_t * p_function_list )
253 #define input p_function_list->functions.input
254 p_function_list->pf_probe = DVDProbe;
255 input.pf_init = DVDInit;
256 input.pf_open = input_FileOpen;
257 input.pf_close = input_FileClose;
258 input.pf_end = DVDEnd;
259 input.pf_read = DVDRead;
260 input.pf_set_area = DVDSetArea;
261 input.pf_demux = input_DemuxPS;
262 input.pf_new_packet = input_NetlistNewPacket;
263 input.pf_new_pes = input_NetlistNewPES;
264 input.pf_delete_packet = input_NetlistDeletePacket;
265 input.pf_delete_pes = input_NetlistDeletePES;
266 input.pf_rewind = DVDRewind;
267 input.pf_seek = DVDSeek;
272 * Local tools to decode some data in ifo
275 /*****************************************************************************
276 * Language: gives the long language name from the two-letters ISO-639 code
277 *****************************************************************************/
278 static char * Language( char * p_code )
282 while( !memcmp( lang_tbl[i].p_code, p_code, 2 ) && lang_tbl[i].p_lang_long )
287 return lang_tbl[i].p_lang_long;
291 * Data reading functions
294 /*****************************************************************************
295 * DVDProbe: verifies that the stream is a PS stream
296 *****************************************************************************/
297 static int DVDProbe( probedata_t *p_data )
299 input_thread_t * p_input = (input_thread_t *)p_data;
301 char * psz_name = p_input->p_source;
305 if( TestMethod( INPUT_METHOD_VAR, "dvd" ) )
310 if( ( strlen(psz_name) > 4 ) && !strncasecmp( psz_name, "dvd:", 4 ) )
312 /* If the user specified "dvd:" then it's probably a DVD */
317 i_handle = open( psz_name, 0 );
327 /*****************************************************************************
328 * DVDCheckCSS: check the stream
329 *****************************************************************************/
330 static int DVDCheckCSS( input_thread_t * p_input )
332 #if defined( HAVE_SYS_DVDIO_H ) || defined( LINUX_DVD )
333 return CSSTest( p_input->i_handle );
335 /* DVD ioctls unavailable.
336 * FIXME: Check the stream to see whether it is encrypted or not
337 * to give and accurate error message */
342 /*****************************************************************************
343 * DVDSetArea: initialize input data for title x, chapter y.
344 * It should be called for each user navigation request, and to change
345 * audio or sub-picture streams.
347 * Take care that i_title and i_chapter start from 0.
348 *****************************************************************************/
349 static int DVDSetArea( input_thread_t * p_input,
350 int i_title, int i_chapter,
351 int i_audio, int i_spu )
353 thread_dvd_data_t * p_method;
354 es_descriptor_t * p_es;
366 p_method = (thread_dvd_data_t*)p_input->p_plugin_data;
368 /* Ifo structures reading */
369 p_method->ifo.i_title = i_title;
370 IfoReadVTS( &(p_method->ifo) );
371 intf_WarnMsg( 2, "Ifo: VTS initialized" );
373 #if defined( HAVE_SYS_DVDIO_H ) || defined( LINUX_DVD )
374 if( p_method->b_encrypted )
376 p_method->css.i_title = i_title;
377 p_method->css.i_title_pos =
378 p_method->ifo.vts.i_pos +
379 p_method->ifo.vts.mat.i_tt_vobs_ssector * DVD_LB_SIZE;
380 CSSGetKey( &(p_method->css) );
381 intf_WarnMsg( 2, "CSS: VTS key initialized" );
385 /* Set selected title start and size */
386 p_pgc = &p_method->ifo.vts.pgci_ti.p_srp[0].pgc;
388 /* Find cell index in Program chain */
389 i_index = p_pgc->prg_map.pi_entry_cell[i_chapter] - 1;
391 /* Search for cell_index in cell adress_table */
393 while( p_pgc->p_cell_pos_inf[i_index].i_vob_id >
394 p_method->ifo.vts.c_adt.p_cell_inf[i_cell].i_vob_id )
398 while( p_pgc->p_cell_pos_inf[i_index].i_cell_id >
399 p_method->ifo.vts.c_adt.p_cell_inf[i_cell].i_cell_id )
403 i_start_cell = i_cell;
404 i_end_cell = i_start_cell + p_pgc->i_cell_nb - 1;
406 intf_WarnMsg( 3, "DVD: Start cell: %d End Cell: %d",
407 i_start_cell, i_end_cell );
409 p_method->i_start_cell = i_start_cell;
410 p_method->i_end_cell = i_end_cell;
412 /* start is : beginning of vts + offset to vobs + offset to vob x */
413 i_start = p_method->ifo.vts.i_pos + DVD_LB_SIZE *
414 ( p_method->ifo.vts.mat.i_tt_vobs_ssector +
415 p_method->ifo.vts.c_adt.p_cell_inf[i_start_cell].i_ssector );
417 i_start = lseek( p_input->i_handle, i_start, SEEK_SET );
418 intf_WarnMsg( 2, "DVD: VOBstart at: %lld", i_start );
421 ( p_method->ifo.vts.c_adt.p_cell_inf[i_end_cell].i_esector -
422 p_method->ifo.vts.c_adt.p_cell_inf[i_start_cell].i_ssector + 1 )
424 intf_WarnMsg( 2, "DVD: stream size: %lld", i_size );
429 vlc_mutex_lock( &p_input->stream.stream_lock );
431 /* ES 0 -> video MPEG2 */
432 intf_WarnMsg( 1, "DVD: Video MPEG2 stream" );
433 p_es = input_AddES( p_input, p_input->stream.pp_programs[0], 0xe0, 0 );
434 p_es->i_stream_id = 0xe0;
435 p_es->i_type = MPEG2_VIDEO_ES;
436 input_SelectES( p_input, p_es );
438 /* Audio ES, in the order they appear in .ifo */
439 i_nb = p_method->ifo.vts.mat.i_audio_nb;
440 intf_WarnMsg( 1, "DVD: Audio streams %d", i_nb );
441 for( i = 0 ; i < i_nb ; i++ )
443 i_id = ( ( 0x80 + i ) << 8 ) | 0xbd;
444 p_es = input_AddES( p_input,
445 p_input->stream.pp_programs[0], i_id, 0 );
446 p_es->i_stream_id = 0xbd;
447 p_es->i_type = AC3_AUDIO_ES;
449 // p_es->psz_desc = p_method->ifo.vts.mat.pi_audio_attr[i];
452 input_SelectES( p_input, p_es );
457 i_nb = p_method->ifo.vts.mat.i_subpic_nb;
458 intf_WarnMsg( 1, "DVD: Subpic streams %d", i_nb );
459 for( i = 0 ; i < i_nb ; i++ )
461 i_id = ( ( 0x20 + i ) << 8 ) | 0xbd;
462 p_es = input_AddES( p_input,
463 p_input->stream.pp_programs[0], i_id, 0 );
464 p_es->i_stream_id = 0xbd;
465 p_es->i_type = DVD_SPU_ES;
466 // p_es->psz_desc = p_method->ifo.vts.mat.pi_subpic_attr[i];
469 input_SelectES( p_input, p_es );
474 /* area definition */
475 p_input->stream.pp_areas[0]->i_start = i_start;
476 p_input->stream.pp_areas[0]->i_size = i_size;
478 /* No PSM to read in DVD mode */
479 p_input->stream.pp_programs[0]->b_is_ok = 1;
481 vlc_mutex_unlock( &p_input->stream.stream_lock );
485 if( p_input->stream.b_seekable )
487 stream_ps_data_t * p_demux_data =
488 (stream_ps_data_t *)p_input->stream.pp_programs[0]->p_demux_data;
490 /* Pre-parse the stream to gather stream_descriptor_t. */
491 p_input->stream.pp_programs[0]->b_is_ok = 0;
492 p_demux_data->i_PSM_version = EMPTY_PSM_VERSION;
494 while( !p_input->b_die && !p_input->b_error
495 && !p_demux_data->b_has_PSM )
498 data_packet_t * pp_packets[INPUT_READ_ONCE];
500 i_result = DVDRead( p_input, pp_packets );
504 vlc_mutex_lock( &p_input->stream.stream_lock );
505 p_input->stream.pp_programs[0]->b_is_ok = 1;
506 vlc_mutex_unlock( &p_input->stream.stream_lock );
511 p_input->b_error = 1;
515 for( i = 0; i < INPUT_READ_ONCE && pp_packets[i] != NULL; i++ )
517 /* FIXME: use i_p_config_t */
518 input_ParsePS( p_input, pp_packets[i] );
519 input_NetlistDeletePacket( p_input->p_method_data,
524 if( p_input->stream.pp_areas[0]->i_tell > INPUT_PREPARSE_LENGTH )
529 lseek( p_input->i_handle, i_start, SEEK_SET );
530 vlc_mutex_lock( &p_input->stream.stream_lock );
532 /* i_tell is an indicator from the beginning of the stream,
534 p_input->stream.pp_areas[0]->i_tell = 0;
536 if( p_demux_data->b_has_PSM )
538 /* (The PSM decoder will care about spawning the decoders) */
539 p_input->stream.pp_programs[0]->b_is_ok = 1;
544 /* (We have to do it ourselves) */
547 /* FIXME: we should do multiple passes in case an audio type
550 i_es < p_input->stream.pp_programs[0]->i_es_number;
553 #define p_es p_input->stream.pp_programs[0]->pp_es[i_es]
554 switch( p_es->i_type )
558 input_SelectES( p_input, p_es );
563 if( main_GetIntVariable( INPUT_CHANNEL_VAR, 0 )
564 == (p_es->i_id & 0x1F) )
565 switch( main_GetIntVariable( INPUT_AUDIO_VAR, 0 ) )
568 main_PutIntVariable( INPUT_AUDIO_VAR,
571 input_SelectES( p_input, p_es );
576 if( main_GetIntVariable( INPUT_CHANNEL_VAR, 0 )
577 == ((p_es->i_id & 0xF00) >> 8) )
578 switch( main_GetIntVariable( INPUT_AUDIO_VAR, 0 ) )
581 main_PutIntVariable( INPUT_AUDIO_VAR,
584 input_SelectES( p_input, p_es );
589 if( main_GetIntVariable( INPUT_SUBTITLE_VAR, -1 )
590 == ((p_es->i_id & 0x1F00) >> 8) )
592 input_SelectES( p_input, p_es );
605 input_DumpStream( p_input );
608 /* FIXME : ugly kludge */
609 p_input->stream.pp_areas[0]->i_size = i_size;
611 vlc_mutex_unlock( &p_input->stream.stream_lock );
615 /* The programs will be added when we read them. */
616 vlc_mutex_lock( &p_input->stream.stream_lock );
617 p_input->stream.pp_programs[0]->b_is_ok = 0;
619 /* FIXME : ugly kludge */
620 p_input->stream.pp_areas[0]->i_size = i_size;
622 vlc_mutex_unlock( &p_input->stream.stream_lock );
629 /*****************************************************************************
630 * DVDInit: initializes DVD structures
631 *****************************************************************************/
632 static void DVDInit( input_thread_t * p_input )
634 thread_dvd_data_t * p_method;
637 if( (p_method = malloc( sizeof(thread_dvd_data_t) )) == NULL )
639 intf_ErrMsg( "Out of memory" );
640 p_input->b_error = 1;
644 p_input->p_plugin_data = (void *)p_method;
645 p_input->p_method_data = NULL;
647 p_method->i_fd = p_input->i_handle;
648 /* FIXME: read several packets once */
649 p_method->i_read_once = 1;
650 p_method->b_encrypted = DVDCheckCSS( p_input );
652 lseek( p_input->i_handle, 0, SEEK_SET );
654 /* Reading structures initialisation */
655 input_NetlistInit( p_input, 4096, 4096, DVD_LB_SIZE,
656 p_method->i_read_once );
657 intf_WarnMsg( 2, "DVD: Netlist initialized" );
659 /* Ifo initialisation */
660 p_method->ifo = IfoInit( p_input->i_handle );
661 intf_WarnMsg( 2, "Ifo: VMG initialized" );
663 /* CSS initialisation */
664 if( p_method->b_encrypted )
667 #if defined( HAVE_SYS_DVDIO_H ) || defined( LINUX_DVD )
668 p_method->css = CSSInit( p_input->i_handle );
670 if( ( p_input->b_error = p_method->css.b_error ) )
672 intf_ErrMsg( "CSS fatal error" );
675 intf_WarnMsg( 2, "CSS: initialized" );
677 intf_ErrMsg( "Unscrambling not supported" );
678 p_input->b_error = 1;
683 /* Initialize ES structures */
684 input_InitStream( p_input, sizeof( stream_ps_data_t ) );
685 input_AddProgram( p_input, 0, sizeof( stream_ps_data_t ) );
687 /* Set stream and area data */
688 vlc_mutex_lock( &p_input->stream.stream_lock );
690 /* FIXME: We consider here that one title is one title set
691 * it is not true !!! */
693 #define area p_input->stream.pp_areas
694 /* We start from 1 here since area 0 is reserved for video_ts.vob */
695 for( i = 1 ; i < p_method->ifo.vmg.mat.i_tts_nb ; i++ )
697 input_AddArea( p_input );
699 /* Should not be so simple eventually :
700 * are title Program Chains, or still something else ? */
703 /* Absolute start offset and size
704 * We can only set that with vts ifo, so we do it during the
705 * first call to DVDSetArea */
706 area[i]->i_start = 0;
709 /* Number of chapter */
710 area[i]->i_part_nb = 0;
712 /* Offset to vts_i_0.ifo */
713 area[i]->i_plugin_data = p_method->ifo.i_off +
714 ( p_method->ifo.vmg.ptt_srpt.p_tts[i-1].i_ssector * DVD_LB_SIZE );
718 vlc_mutex_unlock( &p_input->stream.stream_lock );
720 /* By default, set all parameters to 0 */
721 /* FIXME: wrong kludge to get first title number */
722 DVDSetArea( p_input, p_method->ifo.vmg.ptt_srpt.p_tts[0].i_tts_nb - 1,
728 /*****************************************************************************
729 * DVDEnd: frees unused data
730 *****************************************************************************/
731 static void DVDEnd( input_thread_t * p_input )
733 /* FIXME: check order of calls */
734 // CSSEnd( p_input );
735 // IfoEnd( (ifo_t*)(&p_input->p_plugin_data->ifo ) );
736 free( p_input->stream.p_demux_data );
737 free( p_input->p_plugin_data );
738 input_NetlistEnd( p_input );
741 /*****************************************************************************
742 * DVDRead: reads data packets into the netlist.
743 *****************************************************************************
744 * Returns -1 in case of error, 0 if everything went well, and 1 in case of
746 *****************************************************************************/
747 static int DVDRead( input_thread_t * p_input,
748 data_packet_t ** pp_packets )
750 thread_dvd_data_t * p_method;
751 netlist_t * p_netlist;
752 struct iovec * p_vec;
753 struct data_packet_s * p_data;
759 boolean_t b_first_packet;
761 p_method = ( thread_dvd_data_t * ) p_input->p_plugin_data;
762 p_netlist = ( netlist_t * ) p_input->p_method_data;
764 /* Get an iovec pointer */
765 if( ( p_vec = input_NetlistGetiovec( p_netlist ) ) == NULL )
767 intf_ErrMsg( "DVD: read error" );
772 readv( p_input->i_handle, p_vec, p_method->i_read_once );
774 #if defined( HAVE_SYS_DVDIO_H ) || defined( LINUX_DVD )
775 if( p_method->b_encrypted )
777 for( i=0 ; i<p_method->i_read_once ; i++ )
779 CSSDescrambleSector( p_method->css.pi_title_key,
781 ((u8*)(p_vec[i].iov_base))[0x14] &= 0x8F;
786 /* Update netlist indexes */
787 input_NetlistMviovec( p_netlist, p_method->i_read_once, &p_data );
790 /* Read headers to compute payload length */
791 for( i = 0 ; i < p_method->i_read_once ; i++ )
795 while( i_pos < p_netlist->i_buffer_size )
797 pi_cur = (u8*)(p_vec[i].iov_base + i_pos);
799 if( U32_AT( pi_cur ) != 0x1BA )
801 /* That's the case for all packets, except pack header. */
802 i_packet_size = U16_AT( pi_cur + 4 );
807 if( ( pi_cur[4] & 0xC0 ) == 0x40 )
812 else if( ( pi_cur[4] & 0xF0 ) == 0x20 )
819 intf_ErrMsg( "Unable to determine stream type" );
825 p_data->b_discard_payload = 0;
830 p_data = input_NetlistNewPacket( p_netlist ,
832 memcpy( p_data->p_buffer,
833 p_vec[i].iov_base + i_pos , i_packet_size + 6 );
836 p_data->p_payload_end = p_data->p_payload_start + i_packet_size + 6;
837 pp_packets[i_packet] = p_data;
839 i_pos += i_packet_size + 6;
842 pp_packets[i_packet] = NULL;
844 vlc_mutex_lock( &p_input->stream.stream_lock );
845 p_input->stream.pp_areas[0]->i_tell += p_method->i_read_once *DVD_LB_SIZE;
846 vlc_mutex_unlock( &p_input->stream.stream_lock );
852 /*****************************************************************************
853 * DVDRewind : reads a stream backward
854 *****************************************************************************/
855 static int DVDRewind( input_thread_t * p_input )
860 /*****************************************************************************
861 * DVDSeek : Goes to a given position on the stream ; this one is used by the
862 * input and translate chronological position from input to logical postion
864 *****************************************************************************/
865 static void DVDSeek( input_thread_t * p_input, off_t i_off )
867 thread_dvd_data_t * p_method;
870 p_method = ( thread_dvd_data_t * )p_input->p_plugin_data;
872 /* We have to take care of offset of beginning of title */
873 i_pos = i_off + p_input->stream.pp_areas[0]->i_start;
875 /* With DVD, we have to be on a sector boundary */
876 i_pos = i_pos & (~0x7ff);
878 i_pos = lseek( p_input->i_handle, i_pos, SEEK_SET );
880 p_input->stream.pp_areas[0]->i_tell = i_pos - p_input->stream.pp_areas[0]->i_start;