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.49 2001/04/22 00:08:25 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 *****************************************************************************/
38 #define MODULE_NAME dvd
40 #define MODULE_NAME dvdnocss
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 /* called from outside */
246 static int DVDProbe ( probedata_t *p_data );
247 static void DVDInit ( struct input_thread_s * );
248 static void DVDEnd ( struct input_thread_s * );
249 static int DVDSetArea ( struct input_thread_s *, struct input_area_s * );
250 static int DVDRead ( struct input_thread_s *, data_packet_t ** );
251 static void DVDSeek ( struct input_thread_s *, off_t );
252 static int DVDRewind ( struct input_thread_s * );
254 /* called only inside */
255 static char * Language( u16 );
256 static int DVDChooseAngle( thread_dvd_data_t * );
257 static int DVDFindCell( thread_dvd_data_t * );
258 static int DVDFindSector( thread_dvd_data_t * );
259 static int DVDChapterSelect( thread_dvd_data_t *, int );
261 /*****************************************************************************
262 * Functions exported as capabilities. They are declared as static so that
263 * we don't pollute the namespace too much.
264 *****************************************************************************/
265 void _M( input_getfunctions )( function_list_t * p_function_list )
267 #define input p_function_list->functions.input
268 p_function_list->pf_probe = DVDProbe;
269 input.pf_init = DVDInit;
270 input.pf_open = input_FileOpen;
271 input.pf_close = input_FileClose;
272 input.pf_end = DVDEnd;
273 input.pf_read = DVDRead;
274 input.pf_set_area = DVDSetArea;
275 input.pf_demux = input_DemuxPS;
276 input.pf_new_packet = DVDNewPacket;
277 input.pf_new_pes = DVDNewPES;
278 input.pf_delete_packet = DVDDeletePacket;
279 input.pf_delete_pes = DVDDeletePES;
280 input.pf_rewind = DVDRewind;
281 input.pf_seek = DVDSeek;
286 * Local tools to decode some data in ifo
289 /*****************************************************************************
290 * Language: gives the long language name from the two-letters ISO-639 code
291 *****************************************************************************/
292 static char * Language( u16 i_code )
296 while( memcmp( lang_tbl[i].p_code, &i_code, 2 ) &&
297 lang_tbl[i].p_lang_long[0] )
302 return lang_tbl[i].p_lang_long;
306 * Data reading functions
309 /*****************************************************************************
310 * DVDProbe: verifies that the stream is a PS stream
311 *****************************************************************************/
312 static int DVDProbe( probedata_t *p_data )
314 input_thread_t * p_input = (input_thread_t *)p_data;
316 char * psz_name = p_input->p_source;
320 if( TestMethod( INPUT_METHOD_VAR, "dvd" ) )
326 #endif /* HAVE_CSS */
329 if( ( strlen(psz_name) > 4 ) && !strncasecmp( psz_name, "dvd:", 4 ) )
331 /* If the user specified "dvd:" then it's probably a DVD */
336 #endif /* HAVE_CSS */
340 i_handle = open( psz_name, 0 );
350 /*****************************************************************************
351 * DVDInit: initializes DVD structures
352 *****************************************************************************/
353 static void DVDInit( input_thread_t * p_input )
355 thread_dvd_data_t * p_dvd;
356 input_area_t * p_area;
361 /* I don't want DVDs to start playing immediately */
362 // p_input->stream.i_new_status = PAUSE_S;
364 p_dvd = malloc( sizeof(thread_dvd_data_t) );
367 intf_ErrMsg( "dvd error: out of memory" );
368 p_input->b_error = 1;
372 p_input->p_plugin_data = (void *)p_dvd;
373 p_input->p_method_data = NULL;
375 p_dvd->i_fd = p_input->i_handle;
377 /* reading several block once seems to cause lock-up
378 * when using input_ToggleES
379 * who wrote thez damn buggy piece of shit ??? --stef */
380 p_dvd->i_block_once = 1;//32;
381 p_input->i_read_once = 8;//128;
383 i = CSSTest( p_input->i_handle );
388 p_input->b_error = 1;
392 p_dvd->b_encrypted = i;
394 lseek( p_input->i_handle, 0, SEEK_SET );
396 /* Reading structures initialisation */
397 p_input->p_method_data =
398 DVDNetlistInit( 2048, 8192, 2048, DVD_LB_SIZE, p_dvd->i_block_once );
399 intf_WarnMsg( 2, "dvd info: netlist initialized" );
401 /* Ifo allocation & initialisation */
402 if( IfoCreate( p_dvd ) < 0 )
404 intf_ErrMsg( "dvd error: allcation error in ifo" );
405 p_input->b_error = 1;
409 if( IfoInit( p_dvd->p_ifo ) < 0 )
411 intf_ErrMsg( "dvd error: fatal failure in ifo" );
413 p_input->b_error = 1;
417 /* CSS initialisation */
418 if( p_dvd->b_encrypted )
420 p_dvd->p_css = malloc( sizeof(css_t) );
421 if( p_dvd->p_css == NULL )
423 intf_ErrMsg( "dvd error: couldn't create css structure" );
425 p_input->b_error = 1;
429 p_dvd->p_css->i_agid = 0;
431 if( CSSInit( p_input->i_handle, p_dvd->p_css ) < 0 )
433 intf_ErrMsg( "dvd error: fatal failure in css" );
434 free( p_dvd->p_css );
436 p_input->b_error = 1;
440 intf_WarnMsg( 2, "dvd info: css initialized" );
443 /* Set stream and area data */
444 vlc_mutex_lock( &p_input->stream.stream_lock );
446 /* Initialize ES structures */
447 input_InitStream( p_input, sizeof( stream_ps_data_t ) );
449 /* disc input method */
450 p_input->stream.i_method = INPUT_METHOD_DVD;
452 #define title_inf p_dvd->p_ifo->vmg.title_inf
453 intf_WarnMsg( 2, "dvd info: number of titles: %d", title_inf.i_title_nb );
455 #define area p_input->stream.pp_areas
456 /* We start from 1 here since the default area 0
457 * is reserved for video_ts.vob */
458 for( i = 1 ; i <= title_inf.i_title_nb ; i++ )
460 input_AddArea( p_input );
462 /* Titles are Program Chains */
465 /* Absolute start offset and size
466 * We can only set that with vts ifo, so we do it during the
467 * first call to DVDSetArea */
468 area[i]->i_start = 0;
471 /* Number of chapters */
472 area[i]->i_part_nb = title_inf.p_attr[i-1].i_chapter_nb;
475 /* Number of angles */
476 area[i]->i_angle_nb = 0;
477 area[i]->i_angle = 1;
479 /* Offset to vts_i_0.ifo */
480 area[i]->i_plugin_data = p_dvd->p_ifo->i_off +
481 ( title_inf.p_attr[i-1].i_start_sector * DVD_LB_SIZE );
485 /* Get requested title - if none try the first title */
486 i_title = main_GetIntVariable( INPUT_TITLE_VAR, 1 );
487 if( i_title <= 0 || i_title > title_inf.i_title_nb )
494 /* Get requested chapter - if none defaults to first one */
495 i_chapter = main_GetIntVariable( INPUT_CHAPTER_VAR, 1 );
501 p_input->stream.pp_areas[i_title]->i_part = i_chapter;
503 p_area = p_input->stream.pp_areas[i_title];
505 vlc_mutex_unlock( &p_input->stream.stream_lock );
507 /* set title, chapter, audio and subpic */
508 DVDSetArea( p_input, p_area );
513 /*****************************************************************************
514 * DVDEnd: frees unused data
515 *****************************************************************************/
516 static void DVDEnd( input_thread_t * p_input )
518 thread_dvd_data_t * p_dvd;
519 dvd_netlist_t * p_netlist;
521 p_dvd = (thread_dvd_data_t*)p_input->p_plugin_data;
522 p_netlist = (dvd_netlist_t *)p_input->p_method_data;
524 if( p_dvd->b_encrypted )
526 free( p_dvd->p_css );
529 IfoDestroy( p_dvd->p_ifo );
531 DVDNetlistEnd( p_netlist );
534 /*****************************************************************************
535 * DVDSetArea: initialize input data for title x, chapter y.
536 * It should be called for each user navigation request, and to change
537 * audio or sub-picture streams.
539 * Take care that i_title starts from 0 (vmg) and i_chapter start from 1.
540 * i_audio, i_spu start from 1 ; 0 means off.
541 * A negative value for an argument means it does not change
542 *****************************************************************************/
543 static int DVDSetArea( input_thread_t * p_input, input_area_t * p_area )
545 thread_dvd_data_t * p_dvd;
546 es_descriptor_t * p_es;
557 p_dvd = (thread_dvd_data_t*)p_input->p_plugin_data;
559 vlc_mutex_lock( &p_input->stream.stream_lock );
561 if( p_area != p_input->stream.p_selected_area )
565 * We have to load all title information
567 /* Change the default area */
568 p_input->stream.p_selected_area =
569 p_input->stream.pp_areas[p_area->i_id];
571 // vlc_mutex_unlock( &p_input->stream.stream_lock );
573 /* title number: it is not vts nb!,
574 * it is what appears in the interface list */
575 p_dvd->i_title = p_area->i_id;
576 p_dvd->p_ifo->i_title = p_dvd->i_title;
578 /* uodate title environnement variable so that we don't
579 * loop on the same title forever */
580 main_PutIntVariable( INPUT_TITLE_VAR, p_dvd->i_title + 1 );
583 if( IfoTitleSet( p_dvd->p_ifo ) < 0 )
585 intf_ErrMsg( "dvd error: fatal error in vts ifo" );
587 p_input->b_error = 1;
591 #define vmg p_dvd->p_ifo->vmg
592 #define vts p_dvd->p_ifo->vts
593 /* title position inside the selected vts */
595 vmg.title_inf.p_attr[p_dvd->i_title-1].i_title_num;
597 vts.title_inf.p_title_start[p_dvd->i_vts_title-1].i_title_id;
599 intf_WarnMsg( 1, "dvd: title %d vts_title %d pgc %d",
604 /* css title key for current vts */
605 if( p_dvd->b_encrypted )
607 /* this one is vts number */
608 p_dvd->p_css->i_title =
609 vmg.title_inf.p_attr[p_dvd->i_title-1].i_title_set_num;
610 p_dvd->p_css->i_title_pos =
612 vts.manager_inf.i_title_vob_start_sector * DVD_LB_SIZE;
614 j = CSSGetKey( p_input->i_handle, p_dvd->p_css );
617 intf_ErrMsg( "dvd error: fatal error in vts css key" );
619 p_input->b_error = 1;
624 intf_ErrMsg( "dvd error: css decryption unavailable" );
626 p_input->b_error = 1;
634 p_dvd->i_angle_nb = vmg.title_inf.p_attr[p_dvd->i_title-1].i_angle_nb;
635 p_dvd->i_angle = main_GetIntVariable( INPUT_ANGLE_VAR, 1 );
636 if( ( p_dvd->i_angle <= 0 ) || p_dvd->i_angle > p_dvd->i_angle_nb )
642 * Set selected title start and size
645 /* title set offset */
646 p_dvd->i_title_start = vts.i_pos + DVD_LB_SIZE *
647 (off_t)( vts.manager_inf.i_title_vob_start_sector );
649 /* last video cell */
651 p_dvd->i_prg_cell = -1 +
652 vts.title_unit.p_title[p_dvd->i_title_id-1].title.i_cell_nb;
654 if( DVDFindCell( p_dvd ) < 0 )
656 intf_ErrMsg( "dvd error: can't find title end" );
657 p_input->b_error = 1;
661 /* temporary hack to fix size in some dvds */
662 if( p_dvd->i_cell >= vts.cell_inf.i_cell_nb )
664 p_dvd->i_cell = vts.cell_inf.i_cell_nb - 1;
669 p_dvd->i_size = DVD_LB_SIZE *
670 (off_t)( vts.cell_inf.p_cell_map[p_dvd->i_cell].i_end_sector );
671 intf_WarnMsg( 2, "dvd info: stream size 1: %lld @ %d", p_dvd->i_size,
672 vts.cell_inf.p_cell_map[p_dvd->i_cell].i_end_sector );
674 if( DVDChapterSelect( p_dvd, 1 ) < 0 )
676 intf_ErrMsg( "dvd error: can't find first chapter" );
677 p_input->b_error = 1;
681 p_dvd->i_size -= (off_t)( p_dvd->i_sector + 1 ) *DVD_LB_SIZE;
683 intf_WarnMsg( 2, "dvd info: title: %d", p_dvd->i_title );
684 intf_WarnMsg( 2, "dvd info: vobstart at: %lld", p_dvd->i_start );
685 intf_WarnMsg( 2, "dvd info: stream size: %lld", p_dvd->i_size );
686 intf_WarnMsg( 2, "dvd info: number of chapters: %d",
687 vmg.title_inf.p_attr[p_dvd->i_title-1].i_chapter_nb );
688 intf_WarnMsg( 2, "dvd info: number of angles: %d", p_dvd->i_angle_nb );
690 // vlc_mutex_lock( &p_input->stream.stream_lock );
692 /* Area definition */
693 p_input->stream.p_selected_area->i_start = p_dvd->i_start;
694 p_input->stream.p_selected_area->i_size = p_dvd->i_size;
695 p_input->stream.p_selected_area->i_angle_nb = p_dvd->i_angle_nb;
696 p_input->stream.p_selected_area->i_angle = p_dvd->i_angle;
699 * Destroy obsolete ES by reinitializing program 0
700 * and find all ES in title with ifo data
702 if( p_input->stream.pp_programs != NULL )
704 /* We don't use input_EndStream here since
705 * we keep area structures */
707 for( i = 0 ; i < p_input->stream.i_selected_es_number ; i++ )
709 input_UnselectES( p_input, p_input->stream.pp_selected_es[i] );
712 input_DelProgram( p_input, p_input->stream.pp_programs[0] );
714 p_input->stream.pp_selected_es = NULL;
715 p_input->stream.i_selected_es_number = 0;
718 input_AddProgram( p_input, 0, sizeof( stream_ps_data_t ) );
720 /* No PSM to read in DVD mode, we already have all information */
721 p_input->stream.pp_programs[0]->b_is_ok = 1;
722 p_input->stream.pp_programs[0]->i_synchro_state = SYNCHRO_START;
726 /* ES 0 -> video MPEG2 */
727 p_es = input_AddES( p_input, p_input->stream.pp_programs[0], 0xe0, 0 );
728 p_es->i_stream_id = 0xe0;
729 p_es->i_type = MPEG2_VIDEO_ES;
730 p_es->i_cat = VIDEO_ES;
731 intf_WarnMsg( 1, "dvd info: video mpeg2 stream" );
732 if( p_main->b_video )
734 input_SelectES( p_input, p_es );
737 /* Audio ES, in the order they appear in .ifo */
742 for( i = 1 ; i <= vts.manager_inf.i_audio_nb ; i++ )
746 intf_WarnMsg( 1, "Audio %d: %x %x %x %x %x %x %x %x %x %x %x %x", i,
747 vts.manager_inf.p_audio_attr[i-1].i_num_channels,
748 vts.manager_inf.p_audio_attr[i-1].i_coding_mode,
749 vts.manager_inf.p_audio_attr[i-1].i_multichannel_extension,
750 vts.manager_inf.p_audio_attr[i-1].i_type,
751 vts.manager_inf.p_audio_attr[i-1].i_appl_mode,
752 vts.manager_inf.p_audio_attr[i-1].i_foo,
753 vts.manager_inf.p_audio_attr[i-1].i_test,
754 vts.manager_inf.p_audio_attr[i-1].i_bar,
755 vts.manager_inf.p_audio_attr[i-1].i_quantization,
756 vts.manager_inf.p_audio_attr[i-1].i_sample_freq,
757 vts.manager_inf.p_audio_attr[i-1].i_lang_code,
758 vts.manager_inf.p_audio_attr[i-1].i_caption );
761 switch( vts.manager_inf.p_audio_attr[i-1].i_coding_mode )
764 i_id = ( ( i_ac3 + i ) << 8 ) | 0xbd;
765 p_es = input_AddES( p_input,
766 p_input->stream.pp_programs[0], i_id, 0 );
767 p_es->i_stream_id = 0xbd;
768 p_es->i_type = AC3_AUDIO_ES;
770 p_es->i_cat = AUDIO_ES;
771 strcpy( p_es->psz_desc, Language( hton16(
772 vts.manager_inf.p_audio_attr[i-1].i_lang_code ) ) );
773 strcat( p_es->psz_desc, " (ac3)" );
775 intf_WarnMsg( 1, "dvd info: audio stream %d %s\t(0x%x)",
776 i, p_es->psz_desc, i_id );
780 case 0x03: /* MPEG audio */
782 p_es = input_AddES( p_input,
783 p_input->stream.pp_programs[0], i_id, 0 );
784 p_es->i_stream_id = i_id;
785 p_es->i_type = MPEG2_AUDIO_ES;
787 p_es->i_cat = AUDIO_ES;
788 strcpy( p_es->psz_desc, Language( hton16(
789 vts.manager_inf.p_audio_attr[i-1].i_lang_code ) ) );
790 strcat( p_es->psz_desc, " (mpeg)" );
792 intf_WarnMsg( 1, "dvd info: audio stream %d %s\t(0x%x)",
793 i, p_es->psz_desc, i_id );
796 case 0x04: /* LPCM */
798 intf_ErrMsg( "dvd warning: LPCM audio not handled yet" );
803 intf_ErrMsg( "dvd warning: DTS audio not handled yet" );
807 intf_ErrMsg( "dvd warning: unknown audio type %.2x",
808 vts.manager_inf.p_audio_attr[i-1].i_coding_mode );
817 for( i = 1 ; i <= vts.manager_inf.i_spu_nb; i++ )
821 i_id = ( i_sub_pic++ << 8 ) | 0xbd;
822 p_es = input_AddES( p_input,
823 p_input->stream.pp_programs[0], i_id, 0 );
824 p_es->i_stream_id = 0xbd;
825 p_es->i_type = DVD_SPU_ES;
826 p_es->i_cat = SPU_ES;
827 strcpy( p_es->psz_desc, Language( hton16(
828 vts.manager_inf.p_spu_attr[i-1].i_lang_code ) ) );
829 intf_WarnMsg( 1, "dvd info: spu stream %d %s\t(0x%x)",
830 i, p_es->psz_desc, i_id );
832 /* The before the last spu has a 0x0 prefix */
834 ( vts.manager_inf.p_spu_attr[i].i_prefix == 0 );
838 if( p_main->b_audio )
840 /* For audio: first one if none or a not existing one specified */
841 i_audio = main_GetIntVariable( INPUT_CHANNEL_VAR, 1 );
842 if( i_audio < 0 || i_audio > vts.manager_inf.i_audio_nb )
844 main_PutIntVariable( INPUT_CHANNEL_VAR, 1 );
847 if( i_audio > 0 && vts.manager_inf.i_audio_nb > 0 )
849 input_SelectES( p_input, p_input->stream.pp_es[i_audio] );
853 if( p_main->b_video )
855 /* for spu, default is none */
856 i_spu = main_GetIntVariable( INPUT_SUBTITLE_VAR, 0 );
857 if( i_spu < 0 || i_spu > vts.manager_inf.i_spu_nb )
859 main_PutIntVariable( INPUT_SUBTITLE_VAR, 0 );
862 if( i_spu > 0 && vts.manager_inf.i_spu_nb > 0 )
864 i_spu += vts.manager_inf.i_audio_nb;
865 input_SelectES( p_input, p_input->stream.pp_es[i_spu] );
871 p_area = p_input->stream.p_selected_area;
881 if( p_area->i_part != p_dvd->i_chapter )
883 if( ( p_area->i_part > 0 ) &&
884 ( p_area->i_part <= p_area->i_part_nb ))
886 if( DVDChapterSelect( p_dvd, p_area->i_part ) < 0 )
888 intf_ErrMsg( "dvd error: can't set chapter in area" );
889 p_input->b_error = 1;
893 p_input->stream.p_selected_area->i_tell = p_dvd->i_start -
895 p_input->stream.p_selected_area->i_part = p_dvd->i_chapter;
897 intf_WarnMsg( 2, "dvd info: chapter %d start at: %lld",
898 p_area->i_part, p_area->i_tell );
903 p_dvd->i_chapter = 1;
907 p_dvd->p_ifo->vts.title_unit.p_title[p_dvd->i_title_id-1].title
909 if( p_area->i_angle != p_dvd->i_angle )
911 if( title.p_cell_play[p_dvd->i_prg_cell].i_category & 0xf000 )
913 if( ( p_area->i_angle - p_dvd->i_angle ) < 0 )
917 p_dvd->i_prg_cell += ( p_area->i_angle - p_dvd->i_angle );
918 p_dvd->i_angle = p_area->i_angle;
920 DVDFindSector( p_dvd );
921 p_dvd->i_cell += p_dvd->i_angle_cell;
925 p_dvd->i_angle = p_area->i_angle;
928 intf_WarnMsg( 2, "dvd info: angle %d selected", p_area->i_angle );
931 vlc_mutex_unlock( &p_input->stream.stream_lock );
937 /*****************************************************************************
938 * DVDRead: reads data packets into the netlist.
939 *****************************************************************************
940 * Returns -1 in case of error, 0 if everything went well, and 1 in case of
942 *****************************************************************************/
943 static int DVDRead( input_thread_t * p_input,
944 data_packet_t ** pp_packets )
946 thread_dvd_data_t * p_dvd;
947 dvd_netlist_t * p_netlist;
948 struct iovec * p_vec;
949 struct data_packet_s * pp_data[p_input->i_read_once];
961 p_dvd = (thread_dvd_data_t *)p_input->p_plugin_data;
962 p_netlist = (dvd_netlist_t *)p_input->p_method_data;
964 /* Get an iovec pointer */
965 if( ( p_vec = DVDGetiovec( p_netlist ) ) == NULL )
967 intf_ErrMsg( "dvd error: can't get iovec" );
971 i_block_once = p_dvd->i_end_sector - p_dvd->i_sector + 1;
973 /* Get the position of the next cell if we're at cell end */
974 if( i_block_once <= 0 )
979 p_dvd->i_angle_cell++;
981 /* Find cell index in adress map */
982 if( DVDFindSector( p_dvd ) < 0 )
984 pp_packets[0] = NULL;
985 intf_ErrMsg( "dvd error: can't find next cell" );
989 /* Position the fd pointer on the right address */
990 i_off = lseek( p_dvd->i_fd,
991 p_dvd->i_title_start +
992 (off_t)( p_dvd->i_sector ) *DVD_LB_SIZE, SEEK_SET );
994 /* update chapter : it will be easier when we have navigation
996 if( title.p_cell_play[p_dvd->i_prg_cell].i_category & 0xf000 )
998 i_angle = p_dvd->i_angle - 1;
1004 if( title.chapter_map.pi_start_cell[p_dvd->i_chapter-1] <=
1005 ( p_dvd->i_prg_cell - i_angle ) )
1010 vlc_mutex_lock( &p_input->stream.stream_lock );
1012 p_input->stream.p_selected_area->i_tell = i_off -
1013 p_input->stream.p_selected_area->i_start;
1014 p_input->stream.p_selected_area->i_part = p_dvd->i_chapter;
1016 /* the synchro has to be reinitialized when we change cell */
1017 p_input->stream.pp_programs[0]->i_synchro_state = SYNCHRO_START;
1019 vlc_mutex_unlock( &p_input->stream.stream_lock );
1021 i_block_once = p_dvd->i_end_sector - p_dvd->i_sector + 1;
1024 /* the number of blocks read is the maw between the requested
1025 * value and the leaving block in the cell */
1026 if( i_block_once > p_dvd->i_block_once )
1028 i_block_once = p_dvd->i_block_once;
1030 //intf_WarnMsg( 2, "Sector: 0x%x Read: %d Chapter: %d", p_dvd->i_sector, i_block_once, p_dvd->i_chapter );
1032 p_netlist->i_read_once = i_block_once;
1034 /* Reads from DVD */
1035 i_read_bytes = readv( p_dvd->i_fd, p_vec, i_block_once );
1036 i_read_blocks = ( i_read_bytes + 0x7ff ) >> 11;
1038 /* Update netlist indexes */
1039 DVDMviovec( p_netlist, i_read_blocks, pp_data );
1041 /* Update global position */
1042 p_dvd->i_sector += i_read_blocks;
1046 /* Read headers to compute payload length */
1047 for( i_iovec = 0 ; i_iovec < i_read_blocks ; i_iovec++ )
1049 if( p_dvd->b_encrypted )
1051 CSSDescrambleSector( p_dvd->p_css->pi_title_key,
1052 p_vec[i_iovec].iov_base );
1053 ((u8*)(p_vec[i_iovec].iov_base))[0x14] &= 0x8F;
1058 while( i_pos < p_netlist->i_buffer_size )
1060 pi_cur = (u8*)(p_vec[i_iovec].iov_base + i_pos);
1063 if( U32_AT( pi_cur ) != 0x1BA )
1065 /* That's the case for all packets, except pack header. */
1066 i_packet_size = U16_AT( pi_cur + 4 );
1067 pp_packets[i_packet] = DVDNewPtr( p_netlist );
1072 if( ( pi_cur[4] & 0xC0 ) == 0x40 )
1077 else if( ( pi_cur[4] & 0xF0 ) == 0x20 )
1084 intf_ErrMsg( "Unable to determine stream type" );
1088 pp_packets[i_packet] = pp_data[i_iovec];
1092 (*pp_data[i_iovec]->pi_refcount)++;
1094 pp_packets[i_packet]->pi_refcount = pp_data[i_iovec]->pi_refcount;
1096 pp_packets[i_packet]->p_buffer = pp_data[i_iovec]->p_buffer;
1098 pp_packets[i_packet]->p_payload_start =
1099 pp_packets[i_packet]->p_buffer + i_pos;
1101 pp_packets[i_packet]->p_payload_end =
1102 pp_packets[i_packet]->p_payload_start + i_packet_size + 6;
1104 pp_packets[i_packet]->p_next = NULL;
1105 pp_packets[i_packet]->b_discard_payload = 0;
1108 i_pos += i_packet_size + 6;
1112 pp_packets[i_packet] = NULL;
1114 vlc_mutex_lock( &p_input->stream.stream_lock );
1116 p_input->stream.p_selected_area->i_tell += i_read_bytes;
1117 b_eof = !( p_input->stream.p_selected_area->i_tell < p_dvd->i_size );
1119 vlc_mutex_unlock( &p_input->stream.stream_lock );
1121 if( ( i_read_blocks == i_block_once ) && ( !b_eof ) )
1131 /*****************************************************************************
1132 * DVDRewind : reads a stream backward
1133 *****************************************************************************/
1134 static int DVDRewind( input_thread_t * p_input )
1139 /*****************************************************************************
1140 * DVDSeek : Goes to a given position on the stream ; this one is used by the
1141 * input and translate chronological position from input to logical postion
1144 * The lock should be taken before calling this function.
1145 *****************************************************************************/
1146 static void DVDSeek( input_thread_t * p_input, off_t i_off )
1148 thread_dvd_data_t * p_dvd;
1155 p_dvd = ( thread_dvd_data_t * )p_input->p_plugin_data;
1157 /* we have to take care of offset of beginning of title */
1158 i_pos = i_off + p_input->stream.p_selected_area->i_start
1159 - p_dvd->i_title_start;
1161 /* update navigation data */
1162 p_dvd->i_sector = i_pos >> 11;
1167 /* parse vobu address map to find program cell */
1168 while( title.p_cell_play[i_prg_cell].i_end_sector < p_dvd->i_sector )
1173 p_dvd->i_prg_cell = i_prg_cell;
1175 if( DVDChooseAngle( p_dvd ) < 0 )
1177 p_input->b_error = 1;
1183 /* Find first title cell which is inside program cell */
1184 if( DVDFindCell( p_dvd ) < 0 )
1186 /* no following cell : we're at eof */
1187 intf_ErrMsg( "dvd error: cell seeking failed" );
1188 p_input->b_error = 1;
1192 i_cell = p_dvd->i_cell;
1194 #define cell p_dvd->p_ifo->vts.cell_inf.p_cell_map[i_cell]
1195 /* parse cell address map to find title cell containing sector */
1196 while( cell.i_end_sector < p_dvd->i_sector )
1201 p_dvd->i_cell = i_cell;
1203 /* if we're inside a multi-angle zone, we have to choose i_sector
1204 * in the current angle ; we can't do it all the time since cells
1205 * can be very wide out of such zones */
1206 if( title.p_cell_play[p_dvd->i_prg_cell].i_category & 0xf000 )
1208 p_dvd->i_sector = MAX(
1209 cell.i_start_sector,
1210 title.p_cell_play[p_dvd->i_prg_cell].i_start_sector );
1213 p_dvd->i_end_sector = MIN(
1215 title.p_cell_play[p_dvd->i_prg_cell].i_end_sector );
1217 /* update chapter */
1218 if( title.p_cell_play[p_dvd->i_prg_cell].i_category & 0xf000 )
1220 i_angle = p_dvd->i_angle - 1;
1226 while( title.chapter_map.pi_start_cell[i_chapter-1] <=
1227 ( p_dvd->i_prg_cell - i_angle ) )
1232 p_dvd->i_chapter = i_chapter;
1233 p_input->stream.p_selected_area->i_part = p_dvd->i_chapter;
1235 p_input->stream.p_selected_area->i_tell =
1236 lseek( p_dvd->i_fd, p_dvd->i_title_start +
1237 (off_t)( p_dvd->i_sector ) *DVD_LB_SIZE, SEEK_SET ) -
1238 p_input->stream.p_selected_area->i_start;
1240 intf_WarnMsg( 1, "Program Cell: %d Cell: %d Chapter: %d",
1241 p_dvd->i_prg_cell, p_dvd->i_cell, p_dvd->i_chapter );
1247 #define cell p_dvd->p_ifo->vts.cell_inf
1249 /*****************************************************************************
1250 * DVDFindCell: adjust the title cell index with the program cell
1251 *****************************************************************************/
1252 static int DVDFindCell( thread_dvd_data_t * p_dvd )
1257 i_cell = p_dvd->i_cell;
1258 i_index = p_dvd->i_prg_cell;
1260 if( i_cell >= cell.i_cell_nb )
1265 while( ( ( title.p_cell_pos[i_index].i_vob_id !=
1266 cell.p_cell_map[i_cell].i_vob_id ) ||
1267 ( title.p_cell_pos[i_index].i_cell_id !=
1268 cell.p_cell_map[i_cell].i_cell_id ) ) &&
1269 ( i_cell < cell.i_cell_nb - 1 ) )
1275 intf_WarnMsg( 1, "FindCell: i_cell %d i_index %d found %d nb %d",
1282 p_dvd->i_cell = i_cell;
1289 /*****************************************************************************
1290 * DVDFindSector: find cell index in adress map from index in
1291 * information table program map and give corresponding sectors.
1292 *****************************************************************************/
1293 static int DVDFindSector( thread_dvd_data_t * p_dvd )
1296 if( p_dvd->i_sector > title.p_cell_play[p_dvd->i_prg_cell].i_end_sector )
1298 p_dvd->i_prg_cell++;
1300 if( DVDChooseAngle( p_dvd ) < 0 )
1306 if( DVDFindCell( p_dvd ) < 0 )
1308 intf_ErrMsg( "dvd error: can't find sector" );
1312 /* Find start and end sectors of new cell */
1313 p_dvd->i_sector = MAX(
1314 p_dvd->p_ifo->vts.cell_inf.p_cell_map[p_dvd->i_cell].i_start_sector,
1315 title.p_cell_play[p_dvd->i_prg_cell].i_start_sector );
1316 p_dvd->i_end_sector = MIN(
1317 p_dvd->p_ifo->vts.cell_inf.p_cell_map[p_dvd->i_cell].i_end_sector,
1318 title.p_cell_play[p_dvd->i_prg_cell].i_end_sector );
1321 intf_WarnMsg( 1, "cell: %d sector1: 0x%x end1: 0x%x\n"
1322 "index: %d sector2: 0x%x end2: 0x%x\n"
1323 "category: 0x%x ilvu end: 0x%x vobu start 0x%x",
1325 p_dvd->p_ifo->vts.cell_inf.p_cell_map[p_dvd->i_cell].i_start_sector,
1326 p_dvd->p_ifo->vts.cell_inf.p_cell_map[p_dvd->i_cell].i_end_sector,
1328 title.p_cell_play[p_dvd->i_prg_cell].i_start_sector,
1329 title.p_cell_play[p_dvd->i_prg_cell].i_end_sector,
1330 title.p_cell_play[p_dvd->i_prg_cell].i_category,
1331 title.p_cell_play[p_dvd->i_prg_cell].i_first_ilvu_vobu_esector,
1332 title.p_cell_play[p_dvd->i_prg_cell].i_last_vobu_start_sector );
1338 /*****************************************************************************
1339 * DVDChapterSelect: find the cell corresponding to requested chapter
1340 *****************************************************************************/
1341 static int DVDChapterSelect( thread_dvd_data_t * p_dvd, int i_chapter )
1344 /* Find cell index in Program chain for current chapter */
1345 p_dvd->i_prg_cell = title.chapter_map.pi_start_cell[i_chapter-1] - 1;
1347 p_dvd->i_sector = 0;
1349 DVDChooseAngle( p_dvd );
1351 /* Search for cell_index in cell adress_table and initialize start sector */
1352 if( DVDFindSector( p_dvd ) < 0 )
1354 intf_ErrMsg( "dvd error: can't select chapter" );
1358 /* start is : beginning of vts vobs + offset to vob x */
1359 p_dvd->i_start = p_dvd->i_title_start +
1360 DVD_LB_SIZE * (off_t)( p_dvd->i_sector );
1362 /* Position the fd pointer on the right address */
1363 p_dvd->i_start = lseek( p_dvd->i_fd, p_dvd->i_start, SEEK_SET );
1365 p_dvd->i_chapter = i_chapter;
1369 /*****************************************************************************
1370 * DVDChooseAngle: select the cell corresponding to the selected angle
1371 *****************************************************************************/
1372 static int DVDChooseAngle( thread_dvd_data_t * p_dvd )
1374 /* basic handling of angles */
1375 switch( ( ( title.p_cell_play[p_dvd->i_prg_cell].i_category & 0xf000 )
1378 /* we enter a muli-angle section */
1380 p_dvd->i_prg_cell += p_dvd->i_angle - 1;
1381 p_dvd->i_angle_cell = 0;
1382 // intf_WarnMsg( 1, "dvd info: choosing angle %d", p_dvd->i_angle );
1384 /* we exit a multi-angle section */
1387 p_dvd->i_prg_cell += p_dvd->i_angle_nb - p_dvd->i_angle;