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.42 2001/04/11 04:31:59 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 *****************************************************************************/
38 #define MODULE_NAME dvd-css
40 #define MODULE_NAME dvd-nocss
42 #include "modules_inner.h"
47 #include <netinet/in.h>
50 #include <sys/types.h>
66 #include "stream_control.h"
67 #include "input_ext-intf.h"
68 #include "input_ext-dec.h"
72 #include "dvd_netlist.h"
75 #include "input_dvd.h"
76 #include "mpeg_system.h"
82 /*****************************************************************************
84 *****************************************************************************/
93 /* The ISO 639 language codes.
94 * Language names with * prefix are not spelled in their own language
96 { " ", "Not Specified" },
98 { "ab", "*Abkhazian" },
99 { "af", "*Afrikaans" },
100 { "am", "*Amharic" },
102 { "as", "*Assamese" },
104 { "az", "*Azerbaijani" },
105 { "ba", "*Bashkir" },
106 { "be", "*Byelorussian" },
107 { "bg", "*Bulgarian" },
109 { "bi", "*Bislama" },
110 { "bn", "*Bengali; Bangla" },
111 { "bo", "*Tibetan" },
113 { "ca", "*Catalan" },
114 { "co", "*Corsican" },
115 { "cs", "*Czech(Ceske)" },
119 { "dz", "*Bhutani" },
122 { "eo", "*Esperanto" },
124 { "et", "*Estonian" },
126 { "fa", "*Persian" },
129 { "fo", "*Faroese" },
130 { "fr", "Francais" },
131 { "fy", "*Frisian" },
133 { "gd", "*Scots Gaelic" },
134 { "gl", "*Galician" },
135 { "gn", "*Guarani" },
136 { "gu", "*Gujarati" },
138 { "he", "*Hebrew" }, /* formerly iw */
140 { "hr", "Hrvatski" }, /* Croatian */
142 { "hy", "*Armenian" },
143 { "ia", "*Interlingua" },
144 { "id", "*Indonesian" }, /* formerly in */
145 { "ie", "*Interlingue" },
146 { "ik", "*Inupiak" },
147 { "in", "*Indonesian" }, /* replaced by id */
148 { "is", "Islenska" },
149 { "it", "Italiano" },
150 { "iu", "*Inuktitut" },
151 { "iw", "*Hebrew" }, /* replaced by he */
152 { "ja", "*Japanese" },
153 { "ji", "*Yiddish" }, /* replaced by yi */
154 { "jw", "*Javanese" },
155 { "ka", "*Georgian" },
157 { "kl", "*Greenlandic" },
158 { "km", "*Cambodian" },
159 { "kn", "*Kannada" },
161 { "ks", "*Kashmiri" },
162 { "ku", "*Kurdish" },
163 { "ky", "*Kirghiz" },
165 { "ln", "*Lingala" },
166 { "lo", "*Laothian" },
167 { "lt", "*Lithuanian" },
168 { "lv", "*Latvian, Lettish" },
169 { "mg", "*Malagasy" },
171 { "mk", "*Macedonian" },
172 { "ml", "*Malayalam" },
173 { "mn", "*Mongolian" },
174 { "mo", "*Moldavian" },
175 { "mr", "*Marathi" },
177 { "mt", "*Maltese" },
178 { "my", "*Burmese" },
181 { "nl", "Nederlands" },
183 { "oc", "*Occitan" },
184 { "om", "*(Afan) Oromo" },
186 { "pa", "*Punjabi" },
188 { "ps", "*Pashto, Pushto" },
189 { "pt", "Portugues" },
190 { "qu", "*Quechua" },
191 { "rm", "*Rhaeto-Romance" },
192 { "rn", "*Kirundi" },
193 { "ro", "*Romanian" },
194 { "ru", "*Russian" },
195 { "rw", "*Kinyarwanda" },
196 { "sa", "*Sanskrit" },
199 { "sh", "*Serbo-Croatian" },
200 { "si", "*Sinhalese" },
202 { "sl", "*Slovenian" },
206 { "sq", "*Albanian" },
207 { "sr", "*Serbian" },
208 { "ss", "*Siswati" },
209 { "st", "*Sesotho" },
210 { "su", "*Sundanese" },
212 { "sw", "*Swahili" },
217 { "ti", "*Tigrinya" },
218 { "tk", "*Turkmen" },
219 { "tl", "*Tagalog" },
220 { "tn", "*Setswana" },
222 { "tr", "*Turkish" },
227 { "uk", "*Ukrainian" },
230 { "vi", "*Vietnamese" },
231 { "vo", "*Volapuk" },
234 { "yi", "*Yiddish" }, /* formerly ji */
237 { "zh", "*Chinese" },
242 /*****************************************************************************
244 *****************************************************************************/
245 static int DVDProbe ( probedata_t *p_data );
246 static int DVDRead ( struct input_thread_s *, data_packet_t ** );
247 static void DVDInit ( struct input_thread_s * );
248 static void DVDEnd ( struct input_thread_s * );
249 static void DVDSeek ( struct input_thread_s *, off_t );
250 static int DVDSetArea ( struct input_thread_s *, struct input_area_s * );
251 static int DVDRewind ( struct input_thread_s * );
253 /*****************************************************************************
254 * Functions exported as capabilities. They are declared as static so that
255 * we don't pollute the namespace too much.
256 *****************************************************************************/
257 void _M( input_getfunctions )( function_list_t * p_function_list )
259 #define input p_function_list->functions.input
260 p_function_list->pf_probe = DVDProbe;
261 input.pf_init = DVDInit;
262 input.pf_open = input_FileOpen;
263 input.pf_close = input_FileClose;
264 input.pf_end = DVDEnd;
265 input.pf_read = DVDRead;
266 input.pf_set_area = DVDSetArea;
267 input.pf_demux = input_DemuxPS;
268 input.pf_new_packet = DVDNewPacket;
269 input.pf_new_pes = DVDNewPES;
270 input.pf_delete_packet = DVDDeletePacket;
271 input.pf_delete_pes = DVDDeletePES;
272 input.pf_rewind = DVDRewind;
273 input.pf_seek = DVDSeek;
278 * Local tools to decode some data in ifo
281 /*****************************************************************************
282 * Language: gives the long language name from the two-letters ISO-639 code
283 *****************************************************************************/
284 static char * Language( u16 i_code )
288 while( memcmp( lang_tbl[i].p_code, &i_code, 2 ) &&
289 lang_tbl[i].p_lang_long[0] )
294 return lang_tbl[i].p_lang_long;
298 * Data reading functions
301 /*****************************************************************************
302 * DVDProbe: verifies that the stream is a PS stream
303 *****************************************************************************/
304 static int DVDProbe( probedata_t *p_data )
306 input_thread_t * p_input = (input_thread_t *)p_data;
308 char * psz_name = p_input->p_source;
312 if( TestMethod( INPUT_METHOD_VAR, "dvd" ) )
318 #endif /* HAVE_CSS */
321 if( ( strlen(psz_name) > 4 ) && !strncasecmp( psz_name, "dvd:", 4 ) )
323 /* If the user specified "dvd:" then it's probably a DVD */
328 #endif /* HAVE_CSS */
332 i_handle = open( psz_name, 0 );
342 /*****************************************************************************
343 * DVDFindCell: adjust the title cell index with the program cell
344 *****************************************************************************/
345 static int DVDFindCell( thread_dvd_data_t * p_dvd )
351 p_dvd->p_ifo->vts.title_unit.p_title[p_dvd->i_program_chain-1].title
352 #define cell p_dvd->p_ifo->vts.cell_inf
354 i_cell = p_dvd->i_cell;
355 i_index = p_dvd->i_prg_cell;
357 while( ( ( title.p_cell_pos[i_index].i_vob_id !=
358 cell.p_cell_map[i_cell].i_vob_id ) ||
359 ( title.p_cell_pos[i_index].i_cell_id !=
360 cell.p_cell_map[i_cell].i_cell_id ) ) &&
361 ( i_cell < cell.i_cell_nb ) )
366 intf_WarnMsg( 1, "FindCell: i_cell %d i_index %d found %d nb %d",
372 if( i_cell == cell.i_cell_nb )
374 intf_ErrMsg( "dvd error: can't find cell" );
379 p_dvd->i_cell = i_cell;
386 /*****************************************************************************
387 * DVDFindSector: find cell index in adress map from index in
388 * information table program map and give corresponding sectors.
389 *****************************************************************************/
390 static int DVDFindSector( thread_dvd_data_t * p_dvd )
393 p_dvd->p_ifo->vts.title_unit.p_title[p_dvd->i_program_chain-1].title
395 if( p_dvd->i_sector > title.p_cell_play[p_dvd->i_prg_cell].i_end_sector )
400 if( DVDFindCell( p_dvd ) < 0 )
402 intf_ErrMsg( "dvd error: can't find sector" );
406 /* Find start and end sectors of new cell */
407 p_dvd->i_sector = MAX(
408 p_dvd->p_ifo->vts.cell_inf.p_cell_map[p_dvd->i_cell].i_start_sector,
409 title.p_cell_play[p_dvd->i_prg_cell].i_start_sector );
410 p_dvd->i_end_sector = MIN(
411 p_dvd->p_ifo->vts.cell_inf.p_cell_map[p_dvd->i_cell].i_end_sector,
412 title.p_cell_play[p_dvd->i_prg_cell].i_end_sector );
414 intf_WarnMsg( 1, "cell: %d sector1: 0x%x end1: 0x%x\n"
415 "index: %d sector2: 0x%x end2: 0x%x",
417 p_dvd->p_ifo->vts.cell_inf.p_cell_map[p_dvd->i_cell].i_start_sector,
418 p_dvd->p_ifo->vts.cell_inf.p_cell_map[p_dvd->i_cell].i_end_sector,
420 title.p_cell_play[p_dvd->i_prg_cell].i_start_sector,
421 title.p_cell_play[p_dvd->i_prg_cell].i_end_sector );
427 /*****************************************************************************
428 * DVDChapterSelect: find the cell corresponding to requested chapter
429 * When called to find chapter 1, also sets title size and end.
430 *****************************************************************************/
431 static int DVDChapterSelect( thread_dvd_data_t * p_dvd, int i_chapter )
434 p_dvd->p_ifo->vts.title_unit.p_title[p_dvd->i_program_chain-1].title
436 /* Find cell index in Program chain for current chapter */
437 p_dvd->i_prg_cell = title.chapter_map.pi_start_cell[i_chapter-1] - 1;
441 /* Search for cell_index in cell adress_table and initialize start sector */
442 DVDFindSector( p_dvd );
444 /* start is : beginning of vts vobs + offset to vob x */
445 p_dvd->i_start = p_dvd->i_title_start +
446 DVD_LB_SIZE * (off_t)( p_dvd->i_sector );
448 /* Position the fd pointer on the right address */
449 p_dvd->i_start = lseek( p_dvd->i_fd, p_dvd->i_start, SEEK_SET );
451 p_dvd->i_chapter = i_chapter;
456 /*****************************************************************************
457 * DVDSetArea: initialize input data for title x, chapter y.
458 * It should be called for each user navigation request, and to change
459 * audio or sub-picture streams.
461 * Take care that i_title starts from 0 (vmg) and i_chapter start from 1.
462 * i_audio, i_spu start from 1 ; 0 means off.
463 * A negative value for an argument means it does not change
464 *****************************************************************************/
465 static int DVDSetArea( input_thread_t * p_input, input_area_t * p_area )
467 thread_dvd_data_t * p_dvd;
468 es_descriptor_t * p_es;
479 p_dvd = (thread_dvd_data_t*)p_input->p_plugin_data;
481 vlc_mutex_lock( &p_input->stream.stream_lock );
483 if( p_area != p_input->stream.p_selected_area )
487 * We have to load all title information
489 /* Change the default area */
490 p_input->stream.p_selected_area =
491 p_input->stream.pp_areas[p_area->i_id];
493 /* title number: it is not vts nb!,
494 * it is what appears in the interface list */
495 p_dvd->i_title = p_area->i_id;
496 p_dvd->p_ifo->i_title = p_dvd->i_title;
499 if( IfoTitleSet( p_dvd->p_ifo ) < 0 )
501 intf_ErrMsg( "dvd error: fatal error in vts ifo" );
503 p_input->b_error = 1;
507 //intf_WarnMsg( 3, "cell nb %d", p_dvd->p_ifo->vts.title_unit.p_title[p_dvd->i_program_chain-1].title.i_cell_nb );
508 #define vmg p_dvd->p_ifo->vmg
509 #define vts p_dvd->p_ifo->vts
510 /* title position inside the selected vts */
512 vmg.title_inf.p_attr[p_dvd->i_title-1].i_title_num;
513 p_dvd->i_program_chain =
514 vts.title_inf.p_title_start[p_dvd->i_vts_title-1].i_program_chain_num;
516 intf_WarnMsg( 1, "dvd: title %d vts_title %d pgc %d",
519 p_dvd->i_program_chain );
521 /* css title key for current vts */
522 if( p_dvd->b_encrypted )
524 /* this one is vts number */
525 p_dvd->p_css->i_title =
526 vmg.title_inf.p_attr[p_dvd->i_title-1].i_title_set_num;
527 p_dvd->p_css->i_title_pos =
529 vts.manager_inf.i_title_vob_start_sector * DVD_LB_SIZE;
531 j = CSSGetKey( p_input->i_handle, p_dvd->p_css );
534 intf_ErrMsg( "dvd error: fatal error in vts css key" );
536 p_input->b_error = 1;
541 intf_ErrMsg( "dvd error: css decryption unavailable" );
543 p_input->b_error = 1;
549 * Set selected title start and size
552 /* title set offset */
553 p_dvd->i_title_start = vts.i_pos + DVD_LB_SIZE *
554 (off_t)( vts.manager_inf.i_title_vob_start_sector );
556 /* last video cell */
558 p_dvd->i_prg_cell = -1 +
559 vts.title_unit.p_title[p_dvd->i_program_chain-1].title.i_cell_nb;
561 intf_WarnMsg( 3, "cell nb %d", vts.title_unit.p_title[p_dvd->i_program_chain-1].title.i_cell_nb );
563 DVDFindCell( p_dvd );
565 /* temporary hack to fix size in some dvds */
566 if( p_dvd->i_cell >= vts.cell_inf.i_cell_nb )
568 p_dvd->i_cell = vts.cell_inf.i_cell_nb - 1;
572 p_dvd->i_size = DVD_LB_SIZE *
573 (off_t)( vts.cell_inf.p_cell_map[p_dvd->i_cell].i_end_sector );
575 DVDChapterSelect( p_dvd, 1 );
577 p_dvd->i_size -= (off_t)( p_dvd->i_sector + 1 ) *DVD_LB_SIZE;
579 intf_WarnMsg( 2, "dvd info: title: %d", p_dvd->i_title );
580 intf_WarnMsg( 2, "dvd info: vobstart at: %lld", p_dvd->i_start );
581 intf_WarnMsg( 2, "dvd info: stream size: %lld", p_dvd->i_size );
582 intf_WarnMsg( 2, "dvd info: number of chapters: %d",
583 vmg.title_inf.p_attr[p_dvd->i_title-1].i_chapter_nb );
585 /* Area definition */
586 p_input->stream.p_selected_area->i_start = p_dvd->i_start;
587 p_input->stream.p_selected_area->i_size = p_dvd->i_size;
590 * Destroy obsolete ES by reinitializing program 0
591 * and find all ES in title with ifo data
593 if( p_input->stream.pp_programs != NULL )
595 /* We don't use input_EndStream here since
596 * we keep area structures */
598 for( i = 0 ; i < p_input->stream.i_selected_es_number ; i++ )
600 input_UnselectES( p_input, p_input->stream.pp_selected_es[i] );
603 input_DelProgram( p_input, p_input->stream.pp_programs[0] );
605 p_input->stream.pp_selected_es = NULL;
606 p_input->stream.i_selected_es_number = 0;
609 input_AddProgram( p_input, 0, sizeof( stream_ps_data_t ) );
611 /* No PSM to read in DVD mode, we already have all information */
612 p_input->stream.pp_programs[0]->b_is_ok = 1;
613 p_input->stream.pp_programs[0]->i_synchro_state = SYNCHRO_START;
617 /* ES 0 -> video MPEG2 */
618 p_es = input_AddES( p_input, p_input->stream.pp_programs[0], 0xe0, 0 );
619 p_es->i_stream_id = 0xe0;
620 p_es->i_type = MPEG2_VIDEO_ES;
621 p_es->i_cat = VIDEO_ES;
622 intf_WarnMsg( 1, "dvd info: video mpeg2 stream" );
623 if( p_main->b_video )
625 input_SelectES( p_input, p_es );
628 /* Audio ES, in the order they appear in .ifo */
633 for( i = 1 ; i <= vts.manager_inf.i_audio_nb ; i++ )
637 intf_WarnMsg( 1, "Audio %d: %x %x %x %x %x %x %x %x %x %x %x %x", i,
638 vts.manager_inf.p_audio_attr[i-1].i_num_channels,
639 vts.manager_inf.p_audio_attr[i-1].i_coding_mode,
640 vts.manager_inf.p_audio_attr[i-1].i_multichannel_extension,
641 vts.manager_inf.p_audio_attr[i-1].i_type,
642 vts.manager_inf.p_audio_attr[i-1].i_appl_mode,
643 vts.manager_inf.p_audio_attr[i-1].i_foo,
644 vts.manager_inf.p_audio_attr[i-1].i_test,
645 vts.manager_inf.p_audio_attr[i-1].i_bar,
646 vts.manager_inf.p_audio_attr[i-1].i_quantization,
647 vts.manager_inf.p_audio_attr[i-1].i_sample_freq,
648 vts.manager_inf.p_audio_attr[i-1].i_lang_code,
649 vts.manager_inf.p_audio_attr[i-1].i_caption );
652 switch( vts.manager_inf.p_audio_attr[i-1].i_coding_mode )
655 i_id = ( ( i_ac3 + i ) << 8 ) | 0xbd;
656 p_es = input_AddES( p_input,
657 p_input->stream.pp_programs[0], i_id, 0 );
658 p_es->i_stream_id = 0xbd;
659 p_es->i_type = AC3_AUDIO_ES;
661 p_es->i_cat = AUDIO_ES;
662 strcpy( p_es->psz_desc, Language( hton16(
663 vts.manager_inf.p_audio_attr[i-1].i_lang_code ) ) );
664 strcat( p_es->psz_desc, " (ac3)" );
666 intf_WarnMsg( 1, "dvd info: audio stream %d %s\t(0x%x)",
667 i, p_es->psz_desc, i_id );
671 case 0x03: /* MPEG audio */
673 p_es = input_AddES( p_input,
674 p_input->stream.pp_programs[0], i_id, 0 );
675 p_es->i_stream_id = i_id;
676 p_es->i_type = MPEG2_AUDIO_ES;
678 p_es->i_cat = AUDIO_ES;
679 strcpy( p_es->psz_desc, Language( hton16(
680 vts.manager_inf.p_audio_attr[i-1].i_lang_code ) ) );
681 strcat( p_es->psz_desc, " (mpeg)" );
683 intf_WarnMsg( 1, "dvd info: audio stream %d %s\t(0x%x)",
684 i, p_es->psz_desc, i_id );
687 case 0x04: /* LPCM */
689 intf_ErrMsg( "dvd warning: LPCM audio not handled yet" );
694 intf_ErrMsg( "dvd warning: DTS audio not handled yet" );
698 intf_ErrMsg( "dvd warning: unknown audio type %.2x",
699 vts.manager_inf.p_audio_attr[i-1].i_coding_mode );
708 for( i = 1 ; i <= vts.manager_inf.i_spu_nb; i++ )
712 i_id = ( i_sub_pic++ << 8 ) | 0xbd;
713 p_es = input_AddES( p_input,
714 p_input->stream.pp_programs[0], i_id, 0 );
715 p_es->i_stream_id = 0xbd;
716 p_es->i_type = DVD_SPU_ES;
717 p_es->i_cat = SPU_ES;
718 strcpy( p_es->psz_desc, Language( hton16(
719 vts.manager_inf.p_spu_attr[i-1].i_lang_code ) ) );
720 intf_WarnMsg( 1, "dvd info: spu stream %d %s\t(0x%x)",
721 i, p_es->psz_desc, i_id );
723 /* The before the last spu has a 0x0 prefix */
725 ( vts.manager_inf.p_spu_attr[i].i_prefix == 0 );
729 if( p_main->b_audio )
731 /* For audio: first one if none or a not existing one specified */
732 i_audio = main_GetIntVariable( INPUT_CHANNEL_VAR, 1 );
733 if( i_audio < 0 || i_audio > vts.manager_inf.i_audio_nb )
735 main_PutIntVariable( INPUT_CHANNEL_VAR, 1 );
738 if( i_audio > 0 && vts.manager_inf.i_audio_nb > 0 )
740 input_SelectES( p_input, p_input->stream.pp_es[i_audio] );
744 if( p_main->b_video )
746 /* for spu, default is none */
747 i_spu = main_GetIntVariable( INPUT_SUBTITLE_VAR, 0 );
748 if( i_spu < 0 || i_spu > vts.manager_inf.i_spu_nb )
750 main_PutIntVariable( INPUT_CHANNEL_VAR, 1 );
753 if( i_spu > 0 && vts.manager_inf.i_spu_nb > 0 )
755 i_spu += vts.manager_inf.i_audio_nb;
756 input_SelectES( p_input, p_input->stream.pp_es[i_spu] );
762 p_area = p_input->stream.p_selected_area;
769 if( ( p_area->i_part > 0 ) &&
770 ( p_area->i_part <= p_area->i_part_nb ) )
772 DVDChapterSelect( p_dvd, p_area->i_part );
774 p_input->stream.p_selected_area->i_tell = p_dvd->i_start -
776 p_input->stream.p_selected_area->i_part = p_dvd->i_chapter;
778 intf_WarnMsg( 2, "dvd info: chapter %d start at: %lld",
779 p_area->i_part, p_area->i_tell );
782 vlc_mutex_unlock( &p_input->stream.stream_lock );
789 /*****************************************************************************
790 * DVDInit: initializes DVD structures
791 *****************************************************************************/
792 static void DVDInit( input_thread_t * p_input )
794 thread_dvd_data_t * p_dvd;
795 input_area_t * p_area;
800 /* I don't want DVDs to start playing immediately */
801 // p_input->stream.i_new_status = PAUSE_S;
803 p_dvd = malloc( sizeof(thread_dvd_data_t) );
806 intf_ErrMsg( "dvd error: out of memory" );
807 p_input->b_error = 1;
811 p_input->p_plugin_data = (void *)p_dvd;
812 p_input->p_method_data = NULL;
814 p_dvd->i_fd = p_input->i_handle;
816 /* reading several block once seems to cause lock-up
817 * when using input_ToggleES
818 * who wrote thez damn buggy piece of shit ??? --stef */
819 p_dvd->i_block_once = 1;//32;
820 p_input->i_read_once = 8;//128;
822 i = CSSTest( p_input->i_handle );
827 p_input->b_error = 1;
831 p_dvd->b_encrypted = i;
833 lseek( p_input->i_handle, 0, SEEK_SET );
835 /* Reading structures initialisation */
836 p_input->p_method_data =
837 DVDNetlistInit( 2048, 8192, 2048, DVD_LB_SIZE, p_dvd->i_block_once );
838 intf_WarnMsg( 2, "dvd info: netlist initialized" );
840 /* Ifo allocation & initialisation */
841 if( IfoCreate( p_dvd ) < 0 )
843 intf_ErrMsg( "dvd error: allcation error in IFO" );
844 p_input->b_error = 1;
848 if( IfoInit( p_dvd->p_ifo ) < 0 )
850 intf_ErrMsg( "dvd error: fatal failure in IFO" );
852 p_input->b_error = 1;
856 /* CSS initialisation */
857 if( p_dvd->b_encrypted )
859 p_dvd->p_css = malloc( sizeof(css_t) );
860 if( p_dvd->p_css == NULL )
862 intf_ErrMsg( "dvd error: couldn't create CSS structure" );
864 p_input->b_error = 1;
868 if( CSSInit( p_input->i_handle, p_dvd->p_css ) < 0 )
870 intf_ErrMsg( "dvd error: fatal failure in CSS" );
871 free( p_dvd->p_css );
873 p_input->b_error = 1;
877 intf_WarnMsg( 2, "dvd info: CSS initialized" );
880 /* Set stream and area data */
881 vlc_mutex_lock( &p_input->stream.stream_lock );
883 /* Initialize ES structures */
884 input_InitStream( p_input, sizeof( stream_ps_data_t ) );
886 #define title_inf p_dvd->p_ifo->vmg.title_inf
887 intf_WarnMsg( 2, "dvd info: number of titles: %d", title_inf.i_title_nb );
889 #define area p_input->stream.pp_areas
890 /* We start from 1 here since area 0 is reserved for video_ts.vob */
891 for( i = 1 ; i <= title_inf.i_title_nb ; i++ )
893 input_AddArea( p_input );
895 /* Titles are Program Chains */
898 /* Absolute start offset and size
899 * We can only set that with vts ifo, so we do it during the
900 * first call to DVDSetArea */
901 area[i]->i_start = 0;
904 /* Number of chapters */
905 area[i]->i_part_nb = title_inf.p_attr[i-1].i_chapter_nb;
908 /* Offset to vts_i_0.ifo */
909 area[i]->i_plugin_data = p_dvd->p_ifo->i_off +
910 ( title_inf.p_attr[i-1].i_start_sector * DVD_LB_SIZE );
914 /* Get requested title - if none try the first title */
915 i_title = main_GetIntVariable( INPUT_TITLE_VAR, 1 );
916 if( i_title <= 0 || i_title > title_inf.i_title_nb )
921 /* Get requested chapter - if none defaults to first one */
922 i_chapter = main_GetIntVariable( INPUT_CHAPTER_VAR, 1 );
928 p_input->stream.pp_areas[i_title]->i_part = i_chapter;
930 p_area = p_input->stream.pp_areas[i_title];
932 vlc_mutex_unlock( &p_input->stream.stream_lock );
934 /* set title, chapter, audio and subpic */
935 DVDSetArea( p_input, p_area );
940 /*****************************************************************************
941 * DVDEnd: frees unused data
942 *****************************************************************************/
943 static void DVDEnd( input_thread_t * p_input )
945 thread_dvd_data_t * p_dvd;
946 dvd_netlist_t * p_netlist;
948 p_dvd = (thread_dvd_data_t*)p_input->p_plugin_data;
949 p_netlist = (dvd_netlist_t *)p_input->p_method_data;
951 if( p_dvd->b_encrypted )
953 free( p_dvd->p_css );
956 IfoEnd( p_dvd->p_ifo );
958 DVDNetlistEnd( p_netlist );
961 /*****************************************************************************
962 * DVDRead: reads data packets into the netlist.
963 *****************************************************************************
964 * Returns -1 in case of error, 0 if everything went well, and 1 in case of
966 *****************************************************************************/
967 static int DVDRead( input_thread_t * p_input,
968 data_packet_t ** pp_packets )
970 thread_dvd_data_t * p_dvd;
971 dvd_netlist_t * p_netlist;
972 struct iovec * p_vec;
973 struct data_packet_s * pp_data[p_input->i_read_once];
985 p_dvd = (thread_dvd_data_t *)p_input->p_plugin_data;
986 p_netlist = (dvd_netlist_t *)p_input->p_method_data;
988 p_dvd->p_ifo->vts.title_unit.p_title[p_dvd->i_program_chain-1].title
990 /* Get an iovec pointer */
991 if( ( p_vec = DVDGetiovec( p_netlist ) ) == NULL )
993 intf_ErrMsg( "DVD: read error" );
997 i_block_once = p_dvd->i_end_sector - p_dvd->i_sector + 1;
999 /* Get the position of the next cell if we're at cell end */
1000 if( i_block_once <= 0 )
1004 /* Find cell index in adress map */
1005 if( DVDFindSector( p_dvd ) < 0 )
1007 pp_packets[0] = NULL;
1008 intf_ErrMsg( "dvd error: can't find next cell" );
1012 /* Position the fd pointer on the right address */
1013 i_off = lseek( p_dvd->i_fd,
1014 p_dvd->i_title_start +
1015 (off_t)( p_dvd->i_sector ) *DVD_LB_SIZE, SEEK_SET );
1017 /* update chapter : it will be easier when we have navigation
1019 if( title.chapter_map.pi_start_cell[p_dvd->i_chapter-1] <=
1025 vlc_mutex_lock( &p_input->stream.stream_lock );
1027 p_input->stream.p_selected_area->i_tell = i_off -
1028 p_input->stream.p_selected_area->i_start;
1029 p_input->stream.p_selected_area->i_part = p_dvd->i_chapter;
1031 /* the synchro has to be reinitialized when we change cell */
1032 p_input->stream.pp_programs[0]->i_synchro_state = SYNCHRO_START;
1034 vlc_mutex_unlock( &p_input->stream.stream_lock );
1036 i_block_once = p_dvd->i_end_sector - p_dvd->i_sector + 1;
1039 /* the number of blocks read is the maw between the requested
1040 * value and the leaving block in the cell */
1041 if( i_block_once > p_dvd->i_block_once )
1043 i_block_once = p_dvd->i_block_once;
1045 //intf_WarnMsg( 2, "Sector: 0x%x Read: %d Chapter: %d", p_dvd->i_sector, i_block_once, p_dvd->i_chapter );
1047 p_netlist->i_read_once = i_block_once;
1049 /* Reads from DVD */
1050 i_read_bytes = readv( p_dvd->i_fd, p_vec, i_block_once );
1051 i_read_blocks = ( i_read_bytes + 0x7ff ) >> 11;
1053 /* Update netlist indexes */
1054 DVDMviovec( p_netlist, i_read_blocks, pp_data );
1056 /* Update global position */
1057 p_dvd->i_sector += i_read_blocks;
1061 /* Read headers to compute payload length */
1062 for( i_iovec = 0 ; i_iovec < i_read_blocks ; i_iovec++ )
1064 if( p_dvd->b_encrypted )
1066 CSSDescrambleSector( p_dvd->p_css->pi_title_key,
1067 p_vec[i_iovec].iov_base );
1068 ((u8*)(p_vec[i_iovec].iov_base))[0x14] &= 0x8F;
1073 while( i_pos < p_netlist->i_buffer_size )
1075 pi_cur = (u8*)(p_vec[i_iovec].iov_base + i_pos);
1078 if( U32_AT( pi_cur ) != 0x1BA )
1080 /* That's the case for all packets, except pack header. */
1081 i_packet_size = U16_AT( pi_cur + 4 );
1082 pp_packets[i_packet] = DVDNewPtr( p_netlist );
1087 if( ( pi_cur[4] & 0xC0 ) == 0x40 )
1092 else if( ( pi_cur[4] & 0xF0 ) == 0x20 )
1099 intf_ErrMsg( "Unable to determine stream type" );
1103 pp_packets[i_packet] = pp_data[i_iovec];
1107 (*pp_data[i_iovec]->pi_refcount)++;
1109 pp_packets[i_packet]->pi_refcount = pp_data[i_iovec]->pi_refcount;
1111 pp_packets[i_packet]->p_buffer = pp_data[i_iovec]->p_buffer;
1113 pp_packets[i_packet]->p_payload_start =
1114 pp_packets[i_packet]->p_buffer + i_pos;
1116 pp_packets[i_packet]->p_payload_end =
1117 pp_packets[i_packet]->p_payload_start + i_packet_size + 6;
1119 pp_packets[i_packet]->p_next = NULL;
1120 pp_packets[i_packet]->b_discard_payload = 0;
1123 i_pos += i_packet_size + 6;
1127 pp_packets[i_packet] = NULL;
1129 vlc_mutex_lock( &p_input->stream.stream_lock );
1131 p_input->stream.p_selected_area->i_tell += i_read_bytes;
1132 b_eof = p_input->stream.p_selected_area->i_tell < p_dvd->i_size ? 0 : 1;
1134 vlc_mutex_unlock( &p_input->stream.stream_lock );
1136 if( ( i_read_blocks == i_block_once ) && ( !b_eof ) )
1148 /*****************************************************************************
1149 * DVDRewind : reads a stream backward
1150 *****************************************************************************/
1151 static int DVDRewind( input_thread_t * p_input )
1156 /*****************************************************************************
1157 * DVDSeek : Goes to a given position on the stream ; this one is used by the
1158 * input and translate chronological position from input to logical postion
1161 * The lock should be taken before calling this function.
1162 *****************************************************************************/
1163 static void DVDSeek( input_thread_t * p_input, off_t i_off )
1165 thread_dvd_data_t * p_dvd;
1171 p_dvd = ( thread_dvd_data_t * )p_input->p_plugin_data;
1173 p_dvd->p_ifo->vts.title_unit.p_title[p_dvd->i_program_chain-1].title
1175 /* we have to take care of offset of beginning of title */
1176 i_pos = i_off + p_input->stream.p_selected_area->i_start
1177 - p_dvd->i_title_start;
1179 /* update navigation data */
1180 p_dvd->i_sector = i_pos >> 11;
1185 /* parse vobu address map to find program cell */
1186 while( title.p_cell_play[i_prg_cell].i_end_sector < p_dvd->i_sector )
1191 p_dvd->i_prg_cell = i_prg_cell;
1194 /* Find first title cell which is inside program cell */
1195 DVDFindCell( p_dvd );
1197 i_cell = p_dvd->i_cell;
1198 #define cell p_dvd->p_ifo->vts.cell_inf.p_cell_map[i_cell]
1199 /* parse cell address map to find title cell containing sector */
1200 while( cell.i_end_sector < p_dvd->i_sector )
1205 p_dvd->i_cell = i_cell;
1207 p_dvd->i_end_sector = MIN(
1209 title.p_cell_play[p_dvd->i_prg_cell].i_end_sector );
1211 /* update chapter */
1212 while( title.chapter_map.pi_start_cell[i_chapter-1] <= p_dvd->i_prg_cell )
1217 p_dvd->i_chapter = i_chapter;
1218 p_input->stream.p_selected_area->i_part = p_dvd->i_chapter;
1220 p_input->stream.p_selected_area->i_tell =
1221 lseek( p_dvd->i_fd, p_dvd->i_title_start +
1222 (off_t)( p_dvd->i_sector ) *DVD_LB_SIZE, SEEK_SET ) -
1223 p_input->stream.p_selected_area->i_start;
1225 intf_WarnMsg( 1, "Program Cell: %d Cell: %d Chapter: %d",
1226 i_prg_cell, i_cell, p_dvd->i_chapter );