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.19 2001/02/22 08:44:45 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 #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( u16 i_code )
285 while( memcmp( lang_tbl[i].p_code, &i_code, 2 ) &&
286 lang_tbl[i].p_lang_long[0] )
291 return lang_tbl[i].p_lang_long;
295 * Data reading functions
298 /*****************************************************************************
299 * DVDProbe: verifies that the stream is a PS stream
300 *****************************************************************************/
301 static int DVDProbe( probedata_t *p_data )
303 input_thread_t * p_input = (input_thread_t *)p_data;
305 char * psz_name = p_input->p_source;
309 if( TestMethod( INPUT_METHOD_VAR, "dvd" ) )
314 if( ( strlen(psz_name) > 4 ) && !strncasecmp( psz_name, "dvd:", 4 ) )
316 /* If the user specified "dvd:" then it's probably a DVD */
321 i_handle = open( psz_name, 0 );
331 /*****************************************************************************
332 * DVDCheckCSS: check the stream
333 *****************************************************************************/
334 static int DVDCheckCSS( input_thread_t * p_input )
336 return CSSTest( p_input->i_handle );
339 /*****************************************************************************
340 * DVDChapterSelect: find the cell corresponding to requested chapter
341 *****************************************************************************/
342 static int DVDChapterSelect( thread_dvd_data_t * p_dvd, int i_chapter )
350 p_pgc = &p_dvd->ifo.vts.pgci_ti.p_srp[0].pgc;
352 /* Find cell index in Program chain for current chapter */
353 i_index = p_pgc->prg_map.pi_entry_cell[i_chapter-1] - 1;
355 /* Search for cell_index in cell adress_table */
357 while( p_pgc->p_cell_pos_inf[i_index].i_vob_id >
358 p_dvd->ifo.vts.c_adt.p_cell_inf[i_cell].i_vob_id )
362 while( p_pgc->p_cell_pos_inf[i_index].i_cell_id >
363 p_dvd->ifo.vts.c_adt.p_cell_inf[i_cell].i_cell_id )
368 i_start_cell = i_cell;
370 p_dvd->i_start = p_dvd->ifo.vts.i_pos + DVD_LB_SIZE *
371 (off_t)( p_dvd->ifo.vts.mat.i_tt_vobs_ssector +
372 p_dvd->ifo.vts.c_adt.p_cell_inf[i_start_cell].i_ssector );
376 i_end_cell = i_start_cell + p_pgc->i_cell_nb - 1;
377 p_dvd->i_size = (off_t)DVD_LB_SIZE *
378 ( p_dvd->ifo.vts.c_adt.p_cell_inf[i_end_cell].i_esector -
379 p_dvd->ifo.vts.c_adt.p_cell_inf[i_start_cell].i_ssector + 1 );
380 p_dvd->i_chapter_nb = p_pgc->i_cell_nb;
381 intf_WarnMsg( 3, "DVD: Start cell: %d End Cell: %d",
382 i_start_cell, i_end_cell );
388 /*****************************************************************************
389 * DVDSetArea: initialize input data for title x, chapter y.
390 * It should be called for each user navigation request, and to change
391 * audio or sub-picture streams.
393 * Take care that i_title starts from 0 (vmg) and i_chapter start from 1.
394 * i_audio, i_spu start from 1 ; 0 means off.
395 * A negative value for an argument means it does not change
396 *****************************************************************************/
397 static int DVDSetArea( input_thread_t * p_input,
398 int i_title, int i_chapter,
399 int i_audio, int i_spu )
401 thread_dvd_data_t * p_method;
402 es_descriptor_t * p_es;
412 p_method = (thread_dvd_data_t*)p_input->p_plugin_data;
414 vlc_mutex_lock( &p_input->stream.stream_lock );
419 * We have to load all title information
422 /* Change the default area */
423 p_input->stream.p_selected_area = p_input->stream.pp_areas[i_title];
425 /* Ifo VTS, and CSS reading */
426 p_method->ifo.i_title = i_title;
427 IfoReadVTS( &(p_method->ifo) );
428 intf_WarnMsg( 2, "Ifo: VTS initialized" );
430 if( p_method->b_encrypted )
432 p_method->css.i_title = i_title;
433 p_method->css.i_title_pos =
434 p_method->ifo.vts.i_pos +
435 p_method->ifo.vts.mat.i_tt_vobs_ssector * DVD_LB_SIZE;
436 CSSGetKey( &(p_method->css) );
437 intf_WarnMsg( 2, "CSS: VTS key initialized" );
441 * Set selected title start and size
443 DVDChapterSelect( p_method, 1 );
445 /* start is : beginning of vts + offset to vobs + offset to vob x */
448 lseek( p_input->i_handle, p_method->i_start, SEEK_SET );
450 intf_WarnMsg( 2, "DVD: vobstart at: %lld", p_method->i_start );
451 intf_WarnMsg( 2, "DVD: stream size: %lld", p_method->i_size );
452 intf_WarnMsg( 2, "DVD: number of chapters: %lld",
453 p_method->i_chapter_nb );
455 /* Area definition */
456 p_input->stream.p_selected_area->i_start = p_method->i_start;
457 p_input->stream.p_selected_area->i_size = p_method->i_size;
458 p_input->stream.p_selected_area->i_part_nb = p_method->i_chapter_nb;
461 * Destroy obsolete ES by reinitializing program 0
462 * and find all ES in title with ifo data
464 if( p_input->stream.pp_programs != NULL )
466 input_DelProgram( p_input, p_input->stream.pp_programs[0] );
469 input_AddProgram( p_input, 0, sizeof( stream_ps_data_t ) );
473 /* ES 0 -> video MPEG2 */
474 p_es = input_AddES( p_input, p_input->stream.pp_programs[0], 0xe0, 0 );
475 p_es->i_stream_id = 0xe0;
476 p_es->i_type = MPEG2_VIDEO_ES;
477 input_SelectES( p_input, p_es );
478 intf_WarnMsg( 1, "DVD: Video MPEG2 stream" );
480 /* Audio ES, in the order they appear in .ifo */
481 i_nb = p_method->ifo.vts.mat.i_audio_nb;
486 for( i = 1 ; i <= i_nb ; i++ )
490 fprintf( stderr, "Audio %d: %x %x %x %x %x %x\n", i,
491 p_method->ifo.vts.mat.p_audio_atrt[i].i_coding_mode,
492 p_method->ifo.vts.mat.p_audio_atrt[i].i_multichannel_extension,
493 p_method->ifo.vts.mat.p_audio_atrt[i].i_type,
494 p_method->ifo.vts.mat.p_audio_atrt[i].i_appl_mode,
495 p_method->ifo.vts.mat.p_audio_atrt[i].i_foo,
496 p_method->ifo.vts.mat.p_audio_atrt[i].i_bar );
499 switch( p_method->ifo.vts.mat.p_audio_atrt[i].i_coding_mode )
502 i_id = ( ( 0x7f + i ) << 8 ) | 0xbd;
503 p_es = input_AddES( p_input,
504 p_input->stream.pp_programs[0], i_id, 0 );
505 p_es->i_stream_id = 0xbd;
506 p_es->i_type = AC3_AUDIO_ES;
508 strcpy( p_es->psz_desc, Language( hton16(
509 p_method->ifo.vts.mat.p_audio_atrt[i-1].i_lang_code ) ) );
513 case 0x03: /* MPEG audio */
515 p_es = input_AddES( p_input,
516 p_input->stream.pp_programs[0], i_id, 0 );
517 p_es->i_stream_id = i_id;
518 p_es->i_type = MPEG2_AUDIO_ES;
520 strcpy( p_es->psz_desc, Language( hton16(
521 p_method->ifo.vts.mat.p_audio_atrt[i-1].i_lang_code ) ) );
524 case 0x04: /* LPCM */
526 intf_ErrMsg( "DVD: LPCM audio not handled yet" );
530 intf_ErrMsg( "DVD: DTS audio not handled yet" );
534 intf_ErrMsg( "DVD: unkown audio" );
537 intf_WarnMsg( 1, "DVD: Audio stream %d %s\t(0x%x)",
538 i, p_es->psz_desc, i_id );
542 i_nb = p_method->ifo.vts.mat.i_subpic_nb;
546 for( i = 1 ; i <= i_nb ; i++ )
550 i_id = ( i_sub_pic++ << 8 ) | 0xbd;
551 p_es = input_AddES( p_input,
552 p_input->stream.pp_programs[0], i_id, 0 );
553 p_es->i_stream_id = 0xbd;
554 p_es->i_type = DVD_SPU_ES;
555 strcpy( p_es->psz_desc, Language( hton16(
556 p_method->ifo.vts.mat.p_subpic_atrt[i-1].i_lang_code ) ) );
557 intf_WarnMsg( 1, "DVD: SPU stream %d %s\t(0x%x)",
558 i, p_es->psz_desc, i_id );
560 /* The before the last spu has a 0x0 prefix */
562 ( p_method->ifo.vts.mat.p_subpic_atrt[i].i_prefix == 0 );
569 * Select requested ES
571 if( ( i_audio >= 0 ) || ( i_title >= 0 ) )
574 /* Audio: we check it is in the range and
575 * default it to the first if not */
576 if( i_audio > p_method->ifo.vts.mat.i_audio_nb )
581 p_es = p_input->stream.pp_programs[0]->pp_es[i_audio];
583 /* We can only have one audio channel */
584 /* Look for a preselected one */
586 for( i = 0 ; i < p_input->stream.i_selected_es_number ; i++ )
588 if( p_input->stream.pp_selected_es[i]->b_audio )
598 if( p_input->stream.pp_selected_es[i_index] != p_es )
600 input_UnSelectES( p_input,
601 p_input->stream.pp_selected_es[i_index] );
602 input_SelectES( p_input, p_es );
603 intf_WarnMsg( 1, "DVD: Audio %d selected -> %s (0x%x)",
604 i_audio, p_es->psz_desc, p_es->i_id );
609 input_SelectES( p_input, p_es );
610 intf_WarnMsg( 1, "DVD: Audio %d selected -> %s (0x%x)",
611 i_audio, p_es->psz_desc, p_es->i_id );
615 if( ( i_spu >= 0 ) || ( i_title >= 0 ) )
617 /* For spu: no one if none or a not existed one requested */
618 if( ( i_spu <= p_method->ifo.vts.mat.i_subpic_nb ) && ( i_spu > 0 ) )
620 input_SelectES( p_input, ( p_es = p_input->stream.pp_programs[0]->
621 pp_es[ i_spu + p_method->ifo.vts.mat.i_audio_nb ] ) );
623 intf_WarnMsg( 1, "DVD: SPU %d selected -> %s (0x%x)",
624 i_spu, p_es->psz_desc, p_es->i_id );
632 if( ( i_chapter > 0 ) &&
633 ( i_chapter <= p_input->stream.p_selected_area->i_part_nb ) )
635 DVDChapterSelect( p_method, i_chapter );
637 p_input->stream.p_selected_area->i_part = i_chapter;
639 DVDSeek( p_input, p_method->i_start -
640 p_input->stream.p_selected_area->i_start );
642 intf_WarnMsg( 2, "DVD: Chapter %d start at: %lld", i_chapter,
643 p_input->stream.p_selected_area->i_tell );
646 /* No PSM to read in DVD mode, we already have all information */
647 p_input->stream.pp_programs[0]->b_is_ok = 1;
649 vlc_mutex_unlock( &p_input->stream.stream_lock );
654 /*****************************************************************************
655 * DVDInit: initializes DVD structures
656 *****************************************************************************/
657 static void DVDInit( input_thread_t * p_input )
659 thread_dvd_data_t * p_method;
666 if( (p_method = malloc( sizeof(thread_dvd_data_t) )) == NULL )
668 intf_ErrMsg( "Out of memory" );
669 p_input->b_error = 1;
673 p_input->p_plugin_data = (void *)p_method;
674 p_input->p_method_data = NULL;
676 p_method->i_fd = p_input->i_handle;
677 /* FIXME: read several packets once */
678 p_method->i_read_once = 1;
679 p_method->b_encrypted = DVDCheckCSS( p_input );
681 lseek( p_input->i_handle, 0, SEEK_SET );
683 /* Reading structures initialisation */
684 input_NetlistInit( p_input, 4096, 4096, DVD_LB_SIZE,
685 p_method->i_read_once );
686 intf_WarnMsg( 2, "DVD: Netlist initialized" );
688 /* Ifo initialisation */
689 p_method->ifo = IfoInit( p_input->i_handle );
690 intf_WarnMsg( 2, "Ifo: VMG initialized" );
692 /* CSS initialisation */
693 if( p_method->b_encrypted )
695 p_method->css = CSSInit( p_input->i_handle );
697 if( ( p_input->b_error = p_method->css.b_error ) )
699 intf_ErrMsg( "CSS fatal error" );
702 intf_WarnMsg( 2, "CSS: initialized" );
705 /* Initialize ES structures */
706 input_InitStream( p_input, sizeof( stream_ps_data_t ) );
708 /* Set stream and area data */
709 vlc_mutex_lock( &p_input->stream.stream_lock );
711 /* FIXME: We consider here that one title is one title set
712 * it is not true !!! */
714 intf_WarnMsg( 2, "DVD: Number of titles: %d\n",
715 p_method->ifo.vmg.mat.i_tts_nb );
717 #define area p_input->stream.pp_areas
718 /* We start from 1 here since area 0 is reserved for video_ts.vob */
719 for( i = 1 ; i <= p_method->ifo.vmg.mat.i_tts_nb ; i++ )
721 input_AddArea( p_input );
723 /* Should not be so simple eventually :
724 * are title Program Chains, or still something else ? */
727 /* Absolute start offset and size
728 * We can only set that with vts ifo, so we do it during the
729 * first call to DVDSetArea */
730 area[i]->i_start = 0;
733 /* Number of chapter */
734 area[i]->i_part_nb = 0;
736 /* Offset to vts_i_0.ifo */
737 area[i]->i_plugin_data = p_method->ifo.i_off +
738 ( p_method->ifo.vmg.ptt_srpt.p_tts[i-1].i_ssector * DVD_LB_SIZE );
742 vlc_mutex_unlock( &p_input->stream.stream_lock );
744 /* Get requested title - if none try to find one where is the movie */
745 i_title = main_GetIntVariable( INPUT_TITLE_VAR,
746 p_method->ifo.vmg.ptt_srpt.p_tts[0].i_tts_nb );
747 if( i_title <= 0 || i_title >= p_method->ifo.vmg.mat.i_tts_nb )
749 i_title = p_method->ifo.vmg.ptt_srpt.p_tts[0].i_tts_nb;
752 /* Get requested chapter - if none defaults to first one */
753 i_chapter = main_GetIntVariable( INPUT_CHAPTER_VAR, 1 );
759 /* For audio: first one if none or a not existing one specified */
760 i_audio = main_GetIntVariable( INPUT_CHANNEL_VAR, 1 );
763 main_PutIntVariable( INPUT_CHANNEL_VAR, 1 );
767 i_spu = main_GetIntVariable( INPUT_SUBTITLE_VAR, 0 );
769 DVDSetArea( p_input, i_title, i_chapter, i_audio, i_spu );
774 /*****************************************************************************
775 * DVDEnd: frees unused data
776 *****************************************************************************/
777 static void DVDEnd( input_thread_t * p_input )
779 /* FIXME: check order of calls */
780 // CSSEnd( p_input );
781 // IfoEnd( (ifo_t*)(&p_input->p_plugin_data->ifo ) );
782 free( p_input->stream.p_demux_data );
783 free( p_input->p_plugin_data );
784 input_NetlistEnd( p_input );
787 /*****************************************************************************
788 * DVDRead: reads data packets into the netlist.
789 *****************************************************************************
790 * Returns -1 in case of error, 0 if everything went well, and 1 in case of
792 *****************************************************************************/
793 static int DVDRead( input_thread_t * p_input,
794 data_packet_t ** pp_packets )
797 data_packet_t * p_data;
798 size_t i_packet_size;
799 int i_packet, i_error;
800 thread_dvd_data_t * p_method;
802 p_method = (thread_dvd_data_t *)p_input->p_plugin_data;
804 memset( pp_packets, 0, INPUT_READ_ONCE * sizeof(data_packet_t *) );
805 for( i_packet = 0; i_packet < INPUT_READ_ONCE; i_packet++ )
807 /* Read what we believe to be a packet header. */
808 if( (i_error = SafeRead( p_input, p_header, 6 )) )
813 if( (U32_AT(p_header) & 0xFFFFFF00) != 0x100L )
815 /* This is not the startcode of a packet. Read the stream
816 * until we find one. */
817 u32 i_startcode = U32_AT(p_header);
823 /* It is common for MPEG-1 streams to pad with zeros
824 * (although it is forbidden by the recommendation), so
825 * don't bother everybody in this case. */
826 intf_WarnMsg( 1, "Garbage at input (%x)", i_startcode );
829 while( (i_startcode & 0xFFFFFF00) != 0x100L )
832 if( (i_nb = SafeRead( p_input, &i_dummy, 1 )) != 0 )
834 i_startcode |= i_dummy;
843 *(u32 *)p_header = U32_AT(&i_startcode);
844 if( (i_error = SafeRead( p_input, p_header + 4, 2 )) )
850 if( U32_AT(p_header) != 0x1BA )
852 /* That's the case for all packets, except pack header. */
853 i_packet_size = U16_AT(&p_header[4]);
858 if( (p_header[4] & 0xC0) == 0x40 )
863 else if( (p_header[4] & 0xF0) == 0x20 )
870 intf_ErrMsg( "Unable to determine stream type" );
875 /* Fetch a packet of the appropriate size. */
876 if( (p_data = NewPacket( p_input, i_packet_size + 6 )) == NULL )
878 intf_ErrMsg( "Out of memory" );
882 /* Copy the header we already read. */
883 memcpy( p_data->p_buffer, p_header, 6 );
885 /* Read the remaining of the packet. */
886 if( i_packet_size && (i_error =
887 SafeRead( p_input, p_data->p_buffer + 6, i_packet_size )) )
892 /* In MPEG-2 pack headers we still have to read stuffing bytes. */
893 if( U32_AT(p_header) == 0x1BA )
895 if( i_packet_size == 8 && (p_data->p_buffer[13] & 0x7) != 0 )
897 /* MPEG-2 stuffing bytes */
899 if( (i_error = SafeRead( p_input, p_garbage,
900 p_data->p_buffer[13] & 0x7)) )
907 /* Give the packet to the other input stages. */
908 pp_packets[i_packet] = p_data;
915 /*****************************************************************************
916 * DVDRewind : reads a stream backward
917 *****************************************************************************/
918 static int DVDRewind( input_thread_t * p_input )
923 /*****************************************************************************
924 * DVDSeek : Goes to a given position on the stream ; this one is used by the
925 * input and translate chronological position from input to logical postion
927 *****************************************************************************/
928 static void DVDSeek( input_thread_t * p_input, off_t i_off )
930 thread_dvd_data_t * p_method;
933 p_method = ( thread_dvd_data_t * )p_input->p_plugin_data;
935 /* We have to take care of offset of beginning of title */
936 i_pos = i_off + p_input->stream.p_selected_area->i_start;
938 /* With DVD, we have to be on a sector boundary */
939 i_pos = i_pos & (~0x7ff);
941 i_pos = lseek( p_input->i_handle, i_pos, SEEK_SET );
943 p_input->stream.p_selected_area->i_tell = i_pos -
944 p_input->stream.p_selected_area->i_start;