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.18 2001/02/20 23:30:15 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 #define MODULE_NAME dvd
33 #include "modules_inner.h"
35 /*****************************************************************************
37 *****************************************************************************/
43 #include <netinet/in.h>
46 #include <sys/types.h>
63 #include "stream_control.h"
64 #include "input_ext-intf.h"
65 #include "input_ext-dec.h"
68 #include "input_netlist.h"
72 #include "input_dvd.h"
73 #include "mpeg_system.h"
79 /*****************************************************************************
81 *****************************************************************************/
89 /* The ISO 639 language codes.
90 * Language names with * prefix are not spelled in their own language
92 {" ", "Not Specified"},
100 {"az", "*Azerbaijani"},
102 {"be", "*Byelorussian"},
103 {"bg", "*Bulgarian"},
106 {"bn", "*Bengali; Bangla"},
111 {"cs", "*Czech(Ceske)"},
118 {"eo", "*Esperanto"},
129 {"gd", "*Scots Gaelic"},
134 {"he", "*Hebrew"}, // formerly iw
136 {"hr", "Hrvatski"}, // Croatian
139 {"ia", "*Interlingua"},
140 {"id", "*Indonesian"}, // formerly in
141 {"ie", "*Interlingue"},
143 {"in", "*Indonesian"}, // replaced by id
146 {"iu", "*Inuktitut"},
147 {"iw", "*Hebrew"}, // replaced by he
149 {"ji", "*Yiddish"}, // replaced by yi
153 {"kl", "*Greenlandic"},
154 {"km", "*Cambodian"},
163 {"lt", "*Lithuanian"},
164 {"lv", "*Latvian, Lettish"},
167 {"mk", "*Macedonian"},
168 {"ml", "*Malayalam"},
169 {"mn", "*Mongolian"},
170 {"mo", "*Moldavian"},
177 {"nl", "Nederlands"},
180 {"om", "*(Afan) Oromo"},
184 {"ps", "*Pashto, Pushto"},
187 {"rm", "*Rhaeto-Romance"},
191 {"rw", "*Kinyarwanda"},
195 {"sh", "*Serbo-Croatian"},
196 {"si", "*Sinhalese"},
198 {"sl", "*Slovenian"},
206 {"su", "*Sundanese"},
223 {"uk", "*Ukrainian"},
226 {"vi", "*Vietnamese"},
230 {"yi", "*Yiddish"}, // formerly ji
238 /*****************************************************************************
240 *****************************************************************************/
241 static int DVDProbe ( probedata_t *p_data );
242 static int DVDCheckCSS ( struct input_thread_s * );
243 static int DVDRead ( struct input_thread_s *, data_packet_t ** );
244 static void DVDInit ( struct input_thread_s * );
245 static void DVDEnd ( struct input_thread_s * );
246 static void DVDSeek ( struct input_thread_s *, off_t );
247 static int DVDSetArea ( struct input_thread_s *, int, int, int, int );
248 static int DVDRewind ( struct input_thread_s * );
250 /*****************************************************************************
251 * Functions exported as capabilities. They are declared as static so that
252 * we don't pollute the namespace too much.
253 *****************************************************************************/
254 void _M( input_getfunctions )( function_list_t * p_function_list )
256 #define input p_function_list->functions.input
257 p_function_list->pf_probe = DVDProbe;
258 input.pf_init = DVDInit;
259 input.pf_open = input_FileOpen;
260 input.pf_close = input_FileClose;
261 input.pf_end = DVDEnd;
262 input.pf_read = DVDRead;
263 input.pf_set_area = DVDSetArea;
264 input.pf_demux = input_DemuxPS;
265 input.pf_new_packet = input_NetlistNewPacket;
266 input.pf_new_pes = input_NetlistNewPES;
267 input.pf_delete_packet = input_NetlistDeletePacket;
268 input.pf_delete_pes = input_NetlistDeletePES;
269 input.pf_rewind = DVDRewind;
270 input.pf_seek = DVDSeek;
275 * Local tools to decode some data in ifo
278 /*****************************************************************************
279 * Language: gives the long language name from the two-letters ISO-639 code
280 *****************************************************************************/
281 static char * Language( char * p_code )
285 while( !memcmp( lang_tbl[i].p_code, p_code, 2 ) && lang_tbl[i].p_lang_long )
290 return lang_tbl[i].p_lang_long;
294 * Data reading functions
297 /*****************************************************************************
298 * DVDProbe: verifies that the stream is a PS stream
299 *****************************************************************************/
300 static int DVDProbe( probedata_t *p_data )
302 input_thread_t * p_input = (input_thread_t *)p_data;
304 char * psz_name = p_input->p_source;
308 if( TestMethod( INPUT_METHOD_VAR, "dvd" ) )
313 if( ( strlen(psz_name) > 4 ) && !strncasecmp( psz_name, "dvd:", 4 ) )
315 /* If the user specified "dvd:" then it's probably a DVD */
320 i_handle = open( psz_name, 0 );
330 /*****************************************************************************
331 * DVDCheckCSS: check the stream
332 *****************************************************************************/
333 static int DVDCheckCSS( input_thread_t * p_input )
335 return CSSTest( p_input->i_handle );
338 /*****************************************************************************
339 * DVDSetArea: initialize input data for title x, chapter y.
340 * It should be called for each user navigation request, and to change
341 * audio or sub-picture streams.
343 * Take care that i_title and i_chapter start from 0.
344 *****************************************************************************/
345 static int DVDSetArea( input_thread_t * p_input,
346 int i_title, int i_chapter,
347 int i_audio, int i_spu )
349 thread_dvd_data_t * p_method;
350 // es_descriptor_t * p_es;
362 p_method = (thread_dvd_data_t*)p_input->p_plugin_data;
364 /* Ifo structures reading */
365 p_method->ifo.i_title = i_title;
366 IfoReadVTS( &(p_method->ifo) );
367 intf_WarnMsg( 2, "Ifo: VTS initialized" );
369 if( p_method->b_encrypted )
371 p_method->css.i_title = i_title;
372 p_method->css.i_title_pos =
373 p_method->ifo.vts.i_pos +
374 p_method->ifo.vts.mat.i_tt_vobs_ssector * DVD_LB_SIZE;
375 CSSGetKey( &(p_method->css) );
376 intf_WarnMsg( 2, "CSS: VTS key initialized" );
379 /* Set selected title start and size */
380 p_pgc = &p_method->ifo.vts.pgci_ti.p_srp[0].pgc;
382 /* Find cell index in Program chain */
383 i_index = p_pgc->prg_map.pi_entry_cell[i_chapter] - 1;
385 /* Search for cell_index in cell adress_table */
387 while( p_pgc->p_cell_pos_inf[i_index].i_vob_id >
388 p_method->ifo.vts.c_adt.p_cell_inf[i_cell].i_vob_id )
392 while( p_pgc->p_cell_pos_inf[i_index].i_cell_id >
393 p_method->ifo.vts.c_adt.p_cell_inf[i_cell].i_cell_id )
397 i_start_cell = i_cell;
398 i_end_cell = i_start_cell + p_pgc->i_cell_nb - 1;
400 intf_WarnMsg( 3, "DVD: Start cell: %d End Cell: %d",
401 i_start_cell, i_end_cell );
403 p_method->i_start_cell = i_start_cell;
404 p_method->i_end_cell = i_end_cell;
406 /* start is : beginning of vts + offset to vobs + offset to vob x */
407 i_start = p_method->ifo.vts.i_pos + DVD_LB_SIZE *
408 ( p_method->ifo.vts.mat.i_tt_vobs_ssector +
409 p_method->ifo.vts.c_adt.p_cell_inf[i_start_cell].i_ssector );
411 i_start = lseek( p_input->i_handle, i_start, SEEK_SET );
412 intf_WarnMsg( 2, "DVD: VOBstart at: %lld", i_start );
415 ( p_method->ifo.vts.c_adt.p_cell_inf[i_end_cell].i_esector -
416 p_method->ifo.vts.c_adt.p_cell_inf[i_start_cell].i_ssector + 1 )
418 intf_WarnMsg( 2, "DVD: stream size: %lld", i_size );
423 vlc_mutex_lock( &p_input->stream.stream_lock );
425 /* ES 0 -> video MPEG2 */
426 intf_WarnMsg( 1, "DVD: Video MPEG2 stream" );
427 p_es = input_AddES( p_input, p_input->stream.pp_programs[0], 0xe0, 0 );
428 p_es->i_stream_id = 0xe0;
429 p_es->i_type = MPEG2_VIDEO_ES;
430 input_SelectES( p_input, p_es );
432 /* Audio ES, in the order they appear in .ifo */
433 i_nb = p_method->ifo.vts.mat.i_audio_nb;
434 intf_WarnMsg( 1, "DVD: Audio streams %d", i_nb );
435 for( i = 0 ; i < i_nb ; i++ )
437 i_id = ( ( 0x80 + i ) << 8 ) | 0xbd;
438 p_es = input_AddES( p_input,
439 p_input->stream.pp_programs[0], i_id, 0 );
440 p_es->i_stream_id = 0xbd;
441 p_es->i_type = AC3_AUDIO_ES;
443 // p_es->psz_desc = p_method->ifo.vts.mat.pi_audio_attr[i];
446 input_SelectES( p_input, p_es );
451 i_nb = p_method->ifo.vts.mat.i_subpic_nb;
452 intf_WarnMsg( 1, "DVD: Subpic streams %d", i_nb );
453 for( i = 0 ; i < i_nb ; i++ )
455 i_id = ( ( 0x20 + i ) << 8 ) | 0xbd;
456 p_es = input_AddES( p_input,
457 p_input->stream.pp_programs[0], i_id, 0 );
458 p_es->i_stream_id = 0xbd;
459 p_es->i_type = DVD_SPU_ES;
460 // p_es->psz_desc = p_method->ifo.vts.mat.pi_subpic_attr[i];
463 input_SelectES( p_input, p_es );
468 /* area definition */
469 p_input->stream.pp_areas[i_title]->i_start = i_start;
470 p_input->stream.pp_areas[i_title]->i_size = i_size;
472 /* No PSM to read in DVD mode */
473 p_input->stream.pp_programs[0]->b_is_ok = 1;
475 /* Init has been successfull ; change the default area */
476 p_input->stream.p_selected_area = p_input->stream.pp_areas[i_title];
478 vlc_mutex_unlock( &p_input->stream.stream_lock );
481 p_input->stream.p_selected_area = p_input->stream.pp_areas[0];
483 if( p_input->stream.b_seekable )
485 stream_ps_data_t * p_demux_data =
486 (stream_ps_data_t *)p_input->stream.pp_programs[0]->p_demux_data;
488 /* Pre-parse the stream to gather stream_descriptor_t. */
489 p_input->stream.pp_programs[0]->b_is_ok = 0;
490 p_demux_data->i_PSM_version = EMPTY_PSM_VERSION;
492 while( !p_input->b_die && !p_input->b_error
493 && !p_demux_data->b_has_PSM )
496 data_packet_t * pp_packets[INPUT_READ_ONCE];
498 i_result = DVDRead( p_input, pp_packets );
502 vlc_mutex_lock( &p_input->stream.stream_lock );
503 p_input->stream.pp_programs[0]->b_is_ok = 1;
504 vlc_mutex_unlock( &p_input->stream.stream_lock );
509 p_input->b_error = 1;
513 for( i = 0; i < INPUT_READ_ONCE && pp_packets[i] != NULL; i++ )
515 /* FIXME: use i_p_config_t */
516 input_ParsePS( p_input, pp_packets[i] );
517 input_NetlistDeletePacket( p_input->p_method_data,
522 if( p_input->stream.p_selected_area->i_tell >
523 INPUT_PREPARSE_LENGTH )
528 lseek( p_input->i_handle, i_start, SEEK_SET );
529 vlc_mutex_lock( &p_input->stream.stream_lock );
531 /* i_tell is an indicator from the beginning of the stream,
533 p_input->stream.p_selected_area->i_tell = 0;
535 if( p_demux_data->b_has_PSM )
537 /* (The PSM decoder will care about spawning the decoders) */
538 p_input->stream.pp_programs[0]->b_is_ok = 1;
543 /* (We have to do it ourselves) */
546 /* FIXME: we should do multiple passes in case an audio type
549 i_es < p_input->stream.pp_programs[0]->i_es_number;
552 #define p_es p_input->stream.pp_programs[0]->pp_es[i_es]
553 switch( p_es->i_type )
557 input_SelectES( p_input, p_es );
562 if( main_GetIntVariable( INPUT_CHANNEL_VAR, 0 )
563 == (p_es->i_id & 0x1F) )
564 switch( main_GetIntVariable( INPUT_AUDIO_VAR, 0 ) )
567 main_PutIntVariable( INPUT_AUDIO_VAR,
570 input_SelectES( p_input, p_es );
575 if( main_GetIntVariable( INPUT_CHANNEL_VAR, 0 )
576 == ((p_es->i_id & 0xF00) >> 8) )
577 switch( main_GetIntVariable( INPUT_AUDIO_VAR, 0 ) )
580 main_PutIntVariable( INPUT_AUDIO_VAR,
583 input_SelectES( p_input, p_es );
588 if( main_GetIntVariable( INPUT_SUBTITLE_VAR, -1 )
589 == ((p_es->i_id & 0x1F00) >> 8) )
591 input_SelectES( p_input, p_es );
604 input_DumpStream( p_input );
607 /* FIXME : ugly kludge */
608 p_input->stream.p_selected_area->i_size = i_size;
610 vlc_mutex_unlock( &p_input->stream.stream_lock );
614 /* The programs will be added when we read them. */
615 vlc_mutex_lock( &p_input->stream.stream_lock );
616 p_input->stream.pp_programs[0]->b_is_ok = 0;
618 /* FIXME : ugly kludge */
619 p_input->stream.p_selected_area->i_size = i_size;
621 vlc_mutex_unlock( &p_input->stream.stream_lock );
628 /*****************************************************************************
629 * DVDInit: initializes DVD structures
630 *****************************************************************************/
631 static void DVDInit( input_thread_t * p_input )
633 thread_dvd_data_t * p_method;
636 if( (p_method = malloc( sizeof(thread_dvd_data_t) )) == NULL )
638 intf_ErrMsg( "Out of memory" );
639 p_input->b_error = 1;
643 p_input->p_plugin_data = (void *)p_method;
644 p_input->p_method_data = NULL;
646 p_method->i_fd = p_input->i_handle;
647 /* FIXME: read several packets once */
648 p_method->i_read_once = 1;
649 p_method->b_encrypted = DVDCheckCSS( p_input );
651 lseek( p_input->i_handle, 0, SEEK_SET );
653 /* Reading structures initialisation */
654 input_NetlistInit( p_input, 4096, 4096, DVD_LB_SIZE,
655 p_method->i_read_once );
656 intf_WarnMsg( 2, "DVD: Netlist initialized" );
658 /* Ifo initialisation */
659 p_method->ifo = IfoInit( p_input->i_handle );
660 intf_WarnMsg( 2, "Ifo: VMG initialized" );
662 /* CSS initialisation */
663 if( p_method->b_encrypted )
665 p_method->css = CSSInit( p_input->i_handle );
667 if( ( p_input->b_error = p_method->css.b_error ) )
669 intf_ErrMsg( "CSS fatal error" );
672 intf_WarnMsg( 2, "CSS: initialized" );
675 /* Initialize ES structures */
676 input_InitStream( p_input, sizeof( stream_ps_data_t ) );
677 input_AddProgram( p_input, 0, sizeof( stream_ps_data_t ) );
679 /* Set stream and area data */
680 vlc_mutex_lock( &p_input->stream.stream_lock );
682 /* FIXME: We consider here that one title is one title set
683 * it is not true !!! */
685 #define area p_input->stream.pp_areas
686 /* We start from 1 here since area 0 is reserved for video_ts.vob */
687 for( i = 1 ; i < p_method->ifo.vmg.mat.i_tts_nb ; i++ )
689 input_AddArea( p_input );
691 /* Should not be so simple eventually :
692 * are title Program Chains, or still something else ? */
695 /* Absolute start offset and size
696 * We can only set that with vts ifo, so we do it during the
697 * first call to DVDSetArea */
698 area[i]->i_start = 0;
701 /* Number of chapter */
702 area[i]->i_part_nb = 0;
704 /* Offset to vts_i_0.ifo */
705 area[i]->i_plugin_data = p_method->ifo.i_off +
706 ( p_method->ifo.vmg.ptt_srpt.p_tts[i-1].i_ssector * DVD_LB_SIZE );
710 vlc_mutex_unlock( &p_input->stream.stream_lock );
712 /* By default, set all parameters to 0 */
713 /* FIXME: wrong kludge to get first title number */
714 DVDSetArea( p_input, p_method->ifo.vmg.ptt_srpt.p_tts[0].i_tts_nb - 1,
720 /*****************************************************************************
721 * DVDEnd: frees unused data
722 *****************************************************************************/
723 static void DVDEnd( input_thread_t * p_input )
725 /* FIXME: check order of calls */
726 // CSSEnd( p_input );
727 // IfoEnd( (ifo_t*)(&p_input->p_plugin_data->ifo ) );
728 free( p_input->stream.p_demux_data );
729 free( p_input->p_plugin_data );
730 input_NetlistEnd( p_input );
733 /*****************************************************************************
734 * DVDRead: reads data packets into the netlist.
735 *****************************************************************************
736 * Returns -1 in case of error, 0 if everything went well, and 1 in case of
738 *****************************************************************************/
739 static int DVDRead( input_thread_t * p_input,
740 data_packet_t ** pp_packets )
742 thread_dvd_data_t * p_method;
743 netlist_t * p_netlist;
744 struct iovec * p_vec;
745 struct data_packet_s * p_data;
751 boolean_t b_first_packet;
753 p_method = ( thread_dvd_data_t * ) p_input->p_plugin_data;
754 p_netlist = ( netlist_t * ) p_input->p_method_data;
756 /* Get an iovec pointer */
757 if( ( p_vec = input_NetlistGetiovec( p_netlist ) ) == NULL )
759 intf_ErrMsg( "DVD: read error" );
764 readv( p_input->i_handle, p_vec, p_method->i_read_once );
766 if( p_method->b_encrypted )
768 for( i=0 ; i<p_method->i_read_once ; i++ )
770 CSSDescrambleSector( p_method->css.pi_title_key,
772 ((u8*)(p_vec[i].iov_base))[0x14] &= 0x8F;
776 /* Update netlist indexes */
777 input_NetlistMviovec( p_netlist, p_method->i_read_once, &p_data );
780 /* Read headers to compute payload length */
781 for( i = 0 ; i < p_method->i_read_once ; i++ )
785 while( i_pos < p_netlist->i_buffer_size )
787 pi_cur = (u8*)(p_vec[i].iov_base + i_pos);
789 if( U32_AT( pi_cur ) != 0x1BA )
791 /* That's the case for all packets, except pack header. */
792 i_packet_size = U16_AT( pi_cur + 4 );
797 if( ( pi_cur[4] & 0xC0 ) == 0x40 )
802 else if( ( pi_cur[4] & 0xF0 ) == 0x20 )
809 intf_ErrMsg( "Unable to determine stream type" );
815 p_data->b_discard_payload = 0;
820 p_data = input_NetlistNewPacket( p_netlist ,
822 memcpy( p_data->p_buffer,
823 p_vec[i].iov_base + i_pos , i_packet_size + 6 );
826 p_data->p_payload_end = p_data->p_payload_start + i_packet_size + 6;
827 pp_packets[i_packet] = p_data;
829 i_pos += i_packet_size + 6;
832 pp_packets[i_packet] = NULL;
834 vlc_mutex_lock( &p_input->stream.stream_lock );
835 p_input->stream.p_selected_area->i_tell +=
836 p_method->i_read_once *DVD_LB_SIZE;
837 vlc_mutex_unlock( &p_input->stream.stream_lock );
843 /*****************************************************************************
844 * DVDRewind : reads a stream backward
845 *****************************************************************************/
846 static int DVDRewind( input_thread_t * p_input )
851 /*****************************************************************************
852 * DVDSeek : Goes to a given position on the stream ; this one is used by the
853 * input and translate chronological position from input to logical postion
855 *****************************************************************************/
856 static void DVDSeek( input_thread_t * p_input, off_t i_off )
858 thread_dvd_data_t * p_method;
861 p_method = ( thread_dvd_data_t * )p_input->p_plugin_data;
863 /* We have to take care of offset of beginning of title */
864 i_pos = i_off + p_input->stream.p_selected_area->i_start;
866 /* With DVD, we have to be on a sector boundary */
867 i_pos = i_pos & (~0x7ff);
869 i_pos = lseek( p_input->i_handle, i_pos, SEEK_SET );
871 p_input->stream.p_selected_area->i_tell = i_pos -
872 p_input->stream.p_selected_area->i_start;