1 /*****************************************************************************
2 * input_dvd.c: DVD raw reading plugin.
3 *****************************************************************************
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.58 2001/05/15 01:01:44 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>
54 #ifdef STRNCASECMP_IN_STRINGS_H
69 #include "stream_control.h"
70 #include "input_ext-intf.h"
71 #include "input_ext-dec.h"
75 #include "dvd_netlist.h"
78 #include "input_dvd.h"
79 #include "mpeg_system.h"
85 /*****************************************************************************
87 *****************************************************************************/
96 /* The ISO 639 language codes.
97 * Language names with * prefix are not spelled in their own language
99 { " ", "Not Specified" },
101 { "ab", "*Abkhazian" },
102 { "af", "*Afrikaans" },
103 { "am", "*Amharic" },
105 { "as", "*Assamese" },
107 { "az", "*Azerbaijani" },
108 { "ba", "*Bashkir" },
109 { "be", "*Byelorussian" },
110 { "bg", "*Bulgarian" },
112 { "bi", "*Bislama" },
113 { "bn", "*Bengali; Bangla" },
114 { "bo", "*Tibetan" },
116 { "ca", "*Catalan" },
117 { "co", "*Corsican" },
118 { "cs", "*Czech(Ceske)" },
122 { "dz", "*Bhutani" },
125 { "eo", "*Esperanto" },
127 { "et", "*Estonian" },
129 { "fa", "*Persian" },
132 { "fo", "*Faroese" },
133 { "fr", "Francais" },
134 { "fy", "*Frisian" },
136 { "gd", "*Scots Gaelic" },
137 { "gl", "*Galician" },
138 { "gn", "*Guarani" },
139 { "gu", "*Gujarati" },
141 { "he", "*Hebrew" }, /* formerly iw */
143 { "hr", "Hrvatski" }, /* Croatian */
145 { "hy", "*Armenian" },
146 { "ia", "*Interlingua" },
147 { "id", "*Indonesian" }, /* formerly in */
148 { "ie", "*Interlingue" },
149 { "ik", "*Inupiak" },
150 { "in", "*Indonesian" }, /* replaced by id */
151 { "is", "Islenska" },
152 { "it", "Italiano" },
153 { "iu", "*Inuktitut" },
154 { "iw", "*Hebrew" }, /* replaced by he */
155 { "ja", "*Japanese" },
156 { "ji", "*Yiddish" }, /* replaced by yi */
157 { "jw", "*Javanese" },
158 { "ka", "*Georgian" },
160 { "kl", "*Greenlandic" },
161 { "km", "*Cambodian" },
162 { "kn", "*Kannada" },
164 { "ks", "*Kashmiri" },
165 { "ku", "*Kurdish" },
166 { "ky", "*Kirghiz" },
168 { "ln", "*Lingala" },
169 { "lo", "*Laothian" },
170 { "lt", "*Lithuanian" },
171 { "lv", "*Latvian, Lettish" },
172 { "mg", "*Malagasy" },
174 { "mk", "*Macedonian" },
175 { "ml", "*Malayalam" },
176 { "mn", "*Mongolian" },
177 { "mo", "*Moldavian" },
178 { "mr", "*Marathi" },
180 { "mt", "*Maltese" },
181 { "my", "*Burmese" },
184 { "nl", "Nederlands" },
186 { "oc", "*Occitan" },
187 { "om", "*(Afan) Oromo" },
189 { "pa", "*Punjabi" },
191 { "ps", "*Pashto, Pushto" },
192 { "pt", "Portugues" },
193 { "qu", "*Quechua" },
194 { "rm", "*Rhaeto-Romance" },
195 { "rn", "*Kirundi" },
196 { "ro", "*Romanian" },
197 { "ru", "*Russian" },
198 { "rw", "*Kinyarwanda" },
199 { "sa", "*Sanskrit" },
202 { "sh", "*Serbo-Croatian" },
203 { "si", "*Sinhalese" },
205 { "sl", "*Slovenian" },
209 { "sq", "*Albanian" },
210 { "sr", "*Serbian" },
211 { "ss", "*Siswati" },
212 { "st", "*Sesotho" },
213 { "su", "*Sundanese" },
215 { "sw", "*Swahili" },
220 { "ti", "*Tigrinya" },
221 { "tk", "*Turkmen" },
222 { "tl", "*Tagalog" },
223 { "tn", "*Setswana" },
225 { "tr", "*Turkish" },
230 { "uk", "*Ukrainian" },
233 { "vi", "*Vietnamese" },
234 { "vo", "*Volapuk" },
237 { "yi", "*Yiddish" }, /* formerly ji */
240 { "zh", "*Chinese" },
245 /*****************************************************************************
247 *****************************************************************************/
248 /* called from outside */
249 static int DVDProbe ( probedata_t *p_data );
250 static void DVDInit ( struct input_thread_s * );
251 static void DVDEnd ( struct input_thread_s * );
252 static int DVDSetArea ( struct input_thread_s *, struct input_area_s * );
253 static int DVDRead ( struct input_thread_s *, data_packet_t ** );
254 static void DVDSeek ( struct input_thread_s *, off_t );
255 static int DVDRewind ( struct input_thread_s * );
257 /* called only inside */
258 static char * Language( u16 );
259 static int DVDChooseAngle( thread_dvd_data_t * );
260 static int DVDFindCell( thread_dvd_data_t * );
261 static int DVDFindSector( thread_dvd_data_t * );
262 static int DVDChapterSelect( thread_dvd_data_t *, int );
264 /*****************************************************************************
265 * Functions exported as capabilities. They are declared as static so that
266 * we don't pollute the namespace too much.
267 *****************************************************************************/
268 void _M( input_getfunctions )( function_list_t * p_function_list )
270 #define input p_function_list->functions.input
271 p_function_list->pf_probe = DVDProbe;
272 input.pf_init = DVDInit;
273 input.pf_open = input_FileOpen;
274 input.pf_close = input_FileClose;
275 input.pf_end = DVDEnd;
276 input.pf_read = DVDRead;
277 input.pf_set_area = DVDSetArea;
278 input.pf_demux = input_DemuxPS;
279 input.pf_new_packet = DVDNewPacket;
280 input.pf_new_pes = DVDNewPES;
281 input.pf_delete_packet = DVDDeletePacket;
282 input.pf_delete_pes = DVDDeletePES;
283 input.pf_rewind = DVDRewind;
284 input.pf_seek = DVDSeek;
289 * Local tools to decode some data in ifo
292 /*****************************************************************************
293 * Language: gives the long language name from the two-letters ISO-639 code
294 *****************************************************************************/
295 static char * Language( u16 i_code )
299 while( memcmp( lang_tbl[i].p_code, &i_code, 2 ) &&
300 lang_tbl[i].p_lang_long[0] )
305 return lang_tbl[i].p_lang_long;
309 * Data reading functions
312 /*****************************************************************************
313 * DVDProbe: verifies that the stream is a PS stream
314 *****************************************************************************/
315 static int DVDProbe( probedata_t *p_data )
317 input_thread_t * p_input = (input_thread_t *)p_data;
319 char * psz_name = p_input->p_source;
323 if( TestMethod( INPUT_METHOD_VAR, "dvd" ) )
329 #endif /* HAVE_CSS */
332 if( ( strlen(psz_name) > 4 ) && !strncasecmp( psz_name, "dvd:", 4 ) )
334 /* If the user specified "dvd:" then it's probably a DVD */
339 #endif /* HAVE_CSS */
343 i_handle = open( psz_name, 0 );
353 /*****************************************************************************
354 * DVDInit: initializes DVD structures
355 *****************************************************************************/
356 static void DVDInit( input_thread_t * p_input )
358 thread_dvd_data_t * p_dvd;
359 input_area_t * p_area;
364 /* I don't want DVDs to start playing immediately */
365 // p_input->stream.i_new_status = PAUSE_S;
367 p_dvd = malloc( sizeof(thread_dvd_data_t) );
370 intf_ErrMsg( "dvd error: out of memory" );
371 p_input->b_error = 1;
375 p_input->p_plugin_data = (void *)p_dvd;
376 p_input->p_method_data = NULL;
378 p_dvd->i_fd = p_input->i_handle;
380 /* reading several block once seems to cause lock-up
381 * when using input_ToggleES
382 * who wrote thez damn buggy piece of shit ??? --stef */
383 p_dvd->i_block_once = 1;//32;
384 p_input->i_read_once = 8;//128;
386 i = CSSTest( p_input->i_handle );
391 p_input->b_error = 1;
395 p_dvd->b_encrypted = i;
397 lseek( p_input->i_handle, 0, SEEK_SET );
399 /* Reading structures initialisation */
400 p_input->p_method_data =
401 DVDNetlistInit( 2048, 4096, 2048, DVD_LB_SIZE, p_dvd->i_block_once );
402 intf_WarnMsg( 2, "dvd info: netlist initialized" );
404 /* Ifo allocation & initialisation */
405 if( IfoCreate( p_dvd ) < 0 )
407 intf_ErrMsg( "dvd error: allcation error in ifo" );
408 p_input->b_error = 1;
412 if( IfoInit( p_dvd->p_ifo ) < 0 )
414 intf_ErrMsg( "dvd error: fatal failure in ifo" );
416 p_input->b_error = 1;
420 /* CSS initialisation */
421 if( p_dvd->b_encrypted )
423 p_dvd->p_css = malloc( sizeof(css_t) );
424 if( p_dvd->p_css == NULL )
426 intf_ErrMsg( "dvd error: couldn't create css structure" );
428 p_input->b_error = 1;
432 p_dvd->p_css->i_agid = 0;
434 if( CSSInit( p_input->i_handle, p_dvd->p_css ) < 0 )
436 intf_ErrMsg( "dvd error: fatal failure in css" );
437 free( p_dvd->p_css );
439 p_input->b_error = 1;
443 intf_WarnMsg( 2, "dvd info: css initialized" );
446 /* Set stream and area data */
447 vlc_mutex_lock( &p_input->stream.stream_lock );
449 /* Initialize ES structures */
450 input_InitStream( p_input, sizeof( stream_ps_data_t ) );
452 /* disc input method */
453 p_input->stream.i_method = INPUT_METHOD_DVD;
455 #define title_inf p_dvd->p_ifo->vmg.title_inf
456 intf_WarnMsg( 2, "dvd info: number of titles: %d", title_inf.i_title_nb );
458 #define area p_input->stream.pp_areas
459 /* We start from 1 here since the default area 0
460 * is reserved for video_ts.vob */
461 for( i = 1 ; i <= title_inf.i_title_nb ; i++ )
463 input_AddArea( p_input );
465 /* Titles are Program Chains */
468 /* Absolute start offset and size
469 * We can only set that with vts ifo, so we do it during the
470 * first call to DVDSetArea */
471 area[i]->i_start = 0;
474 /* Number of chapters */
475 area[i]->i_part_nb = title_inf.p_attr[i-1].i_chapter_nb;
478 /* Number of angles */
479 area[i]->i_angle_nb = 0;
480 area[i]->i_angle = 1;
482 /* Offset to vts_i_0.ifo */
483 area[i]->i_plugin_data = p_dvd->p_ifo->i_off +
484 ( title_inf.p_attr[i-1].i_start_sector * DVD_LB_SIZE );
488 /* Get requested title - if none try the first title */
489 i_title = main_GetIntVariable( INPUT_TITLE_VAR, 1 );
490 if( i_title <= 0 || i_title > title_inf.i_title_nb )
497 /* Get requested chapter - if none defaults to first one */
498 i_chapter = main_GetIntVariable( INPUT_CHAPTER_VAR, 1 );
504 p_input->stream.pp_areas[i_title]->i_part = i_chapter;
506 p_area = p_input->stream.pp_areas[i_title];
508 vlc_mutex_unlock( &p_input->stream.stream_lock );
510 /* set title, chapter, audio and subpic */
511 DVDSetArea( p_input, p_area );
516 /*****************************************************************************
517 * DVDEnd: frees unused data
518 *****************************************************************************/
519 static void DVDEnd( input_thread_t * p_input )
521 thread_dvd_data_t * p_dvd;
522 dvd_netlist_t * p_netlist;
524 p_dvd = (thread_dvd_data_t*)p_input->p_plugin_data;
525 p_netlist = (dvd_netlist_t *)p_input->p_method_data;
527 if( p_dvd->b_encrypted )
529 free( p_dvd->p_css );
532 IfoDestroy( p_dvd->p_ifo );
534 DVDNetlistEnd( p_netlist );
537 /*****************************************************************************
538 * DVDSetArea: initialize input data for title x, chapter y.
539 * It should be called for each user navigation request, and to change
540 * audio or sub-picture streams.
542 * Take care that i_title starts from 0 (vmg) and i_chapter start from 1.
543 * i_audio, i_spu start from 1 ; 0 means off.
544 * A negative value for an argument means it does not change
545 *****************************************************************************/
546 static int DVDSetArea( input_thread_t * p_input, input_area_t * p_area )
548 thread_dvd_data_t * p_dvd;
549 es_descriptor_t * p_es;
561 p_dvd = (thread_dvd_data_t*)p_input->p_plugin_data;
563 vlc_mutex_lock( &p_input->stream.stream_lock );
565 /* we can't use the interface slider until initilization is complete */
566 p_input->stream.b_seekable = 0;
568 if( p_area != p_input->stream.p_selected_area )
572 * We have to load all title information
574 /* Change the default area */
575 p_input->stream.p_selected_area =
576 p_input->stream.pp_areas[p_area->i_id];
578 // vlc_mutex_unlock( &p_input->stream.stream_lock );
580 /* title number: it is not vts nb!,
581 * it is what appears in the interface list */
582 p_dvd->i_title = p_area->i_id;
583 p_dvd->p_ifo->i_title = p_dvd->i_title;
585 /* uodate title environnement variable so that we don't
586 * loop on the same title forever */
587 // main_PutIntVariable( INPUT_TITLE_VAR, p_dvd->i_title + 1 );
590 if( IfoTitleSet( p_dvd->p_ifo ) < 0 )
592 intf_ErrMsg( "dvd error: fatal error in vts ifo" );
594 p_input->b_error = 1;
598 #define vmg p_dvd->p_ifo->vmg
599 #define vts p_dvd->p_ifo->vts
600 /* title position inside the selected vts */
602 vmg.title_inf.p_attr[p_dvd->i_title-1].i_title_num;
604 vts.title_inf.p_title_start[p_dvd->i_vts_title-1].i_title_id;
606 intf_WarnMsg( 3, "dvd: title %d vts_title %d pgc %d",
611 /* css title key for current vts */
612 if( p_dvd->b_encrypted )
614 /* this one is vts number */
615 p_dvd->p_css->i_title =
616 vmg.title_inf.p_attr[p_dvd->i_title-1].i_title_set_num;
617 p_dvd->p_css->i_title_pos =
619 vts.manager_inf.i_title_vob_start_sector * DVD_LB_SIZE;
621 j = CSSGetKey( p_input->i_handle, p_dvd->p_css );
624 intf_ErrMsg( "dvd error: fatal error in vts css key" );
626 p_input->b_error = 1;
631 intf_ErrMsg( "dvd error: css decryption unavailable" );
633 p_input->b_error = 1;
641 p_dvd->i_angle_nb = vmg.title_inf.p_attr[p_dvd->i_title-1].i_angle_nb;
642 p_dvd->i_angle = main_GetIntVariable( INPUT_ANGLE_VAR, 1 );
643 if( ( p_dvd->i_angle <= 0 ) || p_dvd->i_angle > p_dvd->i_angle_nb )
649 * Set selected title start and size
652 /* title set offset */
653 p_dvd->i_title_start = vts.i_pos + DVD_LB_SIZE *
654 (off_t)( vts.manager_inf.i_title_vob_start_sector );
656 /* last video cell */
658 p_dvd->i_prg_cell = -1 +
659 vts.title_unit.p_title[p_dvd->i_title_id-1].title.i_cell_nb;
661 if( DVDFindCell( p_dvd ) < 0 )
663 intf_ErrMsg( "dvd error: can't find title end" );
664 p_input->b_error = 1;
668 /* temporary hack to fix size in some dvds */
669 if( p_dvd->i_cell >= vts.cell_inf.i_cell_nb )
671 p_dvd->i_cell = vts.cell_inf.i_cell_nb - 1;
675 p_dvd->i_size = DVD_LB_SIZE *
676 (off_t)( vts.cell_inf.p_cell_map[p_dvd->i_cell].i_end_sector );
677 intf_WarnMsg( 2, "dvd info: stream size 1: %lld @ %d", p_dvd->i_size,
678 vts.cell_inf.p_cell_map[p_dvd->i_cell].i_end_sector );
680 if( DVDChapterSelect( p_dvd, 1 ) < 0 )
682 intf_ErrMsg( "dvd error: can't find first chapter" );
683 p_input->b_error = 1;
687 p_dvd->i_size -= (off_t)( p_dvd->i_sector + 1 ) *DVD_LB_SIZE;
689 intf_WarnMsg( 2, "dvd info: title: %d", p_dvd->i_title );
690 intf_WarnMsg( 2, "dvd info: vobstart at: %lld", p_dvd->i_start );
691 intf_WarnMsg( 2, "dvd info: stream size: %lld", p_dvd->i_size );
692 intf_WarnMsg( 2, "dvd info: number of chapters: %d",
693 vmg.title_inf.p_attr[p_dvd->i_title-1].i_chapter_nb );
694 intf_WarnMsg( 2, "dvd info: number of angles: %d", p_dvd->i_angle_nb );
696 // vlc_mutex_lock( &p_input->stream.stream_lock );
698 /* Area definition */
699 p_input->stream.p_selected_area->i_start = p_dvd->i_start;
700 p_input->stream.p_selected_area->i_size = p_dvd->i_size;
701 p_input->stream.p_selected_area->i_angle_nb = p_dvd->i_angle_nb;
702 p_input->stream.p_selected_area->i_angle = p_dvd->i_angle;
705 * Destroy obsolete ES by reinitializing program 0
706 * and find all ES in title with ifo data
708 if( p_input->stream.pp_programs != NULL )
710 /* We don't use input_EndStream here since
711 * we keep area structures */
713 for( i = 0 ; i < p_input->stream.i_selected_es_number ; i++ )
715 input_UnselectES( p_input, p_input->stream.pp_selected_es[i] );
718 input_DelProgram( p_input, p_input->stream.pp_programs[0] );
720 p_input->stream.pp_selected_es = NULL;
721 p_input->stream.i_selected_es_number = 0;
724 input_AddProgram( p_input, 0, sizeof( stream_ps_data_t ) );
726 /* No PSM to read in DVD mode, we already have all information */
727 p_input->stream.pp_programs[0]->b_is_ok = 1;
728 p_input->stream.pp_programs[0]->i_synchro_state = SYNCHRO_START;
732 /* ES 0 -> video MPEG2 */
733 p_es = input_AddES( p_input, p_input->stream.pp_programs[0], 0xe0, 0 );
734 p_es->i_stream_id = 0xe0;
735 p_es->i_type = MPEG2_VIDEO_ES;
736 p_es->i_cat = VIDEO_ES;
737 intf_WarnMsg( 1, "dvd info: video mpeg2 stream" );
738 if( p_main->b_video )
740 input_SelectES( p_input, p_es );
743 /* Audio ES, in the order they appear in .ifo */
749 for( i = 1 ; i <= vts.manager_inf.i_audio_nb ; i++ )
752 intf_WarnMsg( 5, "dvd info: audio %d: %x %x %x %x %x %x %x %x %x %x %x %x", i,
753 vts.manager_inf.p_audio_attr[i-1].i_num_channels,
754 vts.manager_inf.p_audio_attr[i-1].i_coding_mode,
755 vts.manager_inf.p_audio_attr[i-1].i_multichannel_extension,
756 vts.manager_inf.p_audio_attr[i-1].i_type,
757 vts.manager_inf.p_audio_attr[i-1].i_appl_mode,
758 vts.manager_inf.p_audio_attr[i-1].i_foo,
759 vts.manager_inf.p_audio_attr[i-1].i_test,
760 vts.manager_inf.p_audio_attr[i-1].i_bar,
761 vts.manager_inf.p_audio_attr[i-1].i_quantization,
762 vts.manager_inf.p_audio_attr[i-1].i_sample_freq,
763 vts.manager_inf.p_audio_attr[i-1].i_lang_code,
764 vts.manager_inf.p_audio_attr[i-1].i_caption );
766 switch( vts.manager_inf.p_audio_attr[i-1].i_coding_mode )
769 i_id = ( ( i_ac3 + i ) << 8 ) | 0xbd;
770 p_es = input_AddES( p_input,
771 p_input->stream.pp_programs[0], i_id, 0 );
772 p_es->i_stream_id = 0xbd;
773 p_es->i_type = AC3_AUDIO_ES;
775 p_es->i_cat = AUDIO_ES;
776 strcpy( p_es->psz_desc, Language( hton16(
777 vts.manager_inf.p_audio_attr[i-1].i_lang_code ) ) );
778 strcat( p_es->psz_desc, " (ac3)" );
780 intf_WarnMsg( 3, "dvd info: audio stream %d %s\t(0x%x)",
781 i, p_es->psz_desc, i_id );
785 case 0x03: /* MPEG audio */
787 p_es = input_AddES( p_input,
788 p_input->stream.pp_programs[0], i_id, 0 );
789 p_es->i_stream_id = i_id;
790 p_es->i_type = MPEG2_AUDIO_ES;
792 p_es->i_cat = AUDIO_ES;
793 strcpy( p_es->psz_desc, Language( hton16(
794 vts.manager_inf.p_audio_attr[i-1].i_lang_code ) ) );
795 strcat( p_es->psz_desc, " (mpeg)" );
797 intf_WarnMsg( 3, "dvd info: audio stream %d %s\t(0x%x)",
798 i, p_es->psz_desc, i_id );
801 case 0x04: /* LPCM */
803 i_id = ( ( i_lpcm + i ) << 8 ) | 0xbd;
804 p_es = input_AddES( p_input,
805 p_input->stream.pp_programs[0], i_id, 0 );
806 p_es->i_stream_id = i_id;
807 p_es->i_type = LPCM_AUDIO_ES;
809 p_es->i_cat = AUDIO_ES;
810 strcpy( p_es->psz_desc, Language( hton16(
811 vts.manager_inf.p_audio_attr[i-1].i_lang_code ) ) );
812 strcat( p_es->psz_desc, " (lpcm)" );
814 intf_WarnMsg( 3, "dvd info: audio stream %d %s\t(0x%x)",
815 i, p_es->psz_desc, i_id );
818 intf_ErrMsg( "dvd warning: LPCM audio not handled yet" );
824 intf_ErrMsg( "dvd warning: DTS audio not handled yet" );
828 intf_ErrMsg( "dvd warning: unknown audio type %.2x",
829 vts.manager_inf.p_audio_attr[i-1].i_coding_mode );
838 for( i = 1 ; i <= vts.manager_inf.i_spu_nb; i++ )
842 i_id = ( i_sub_pic++ << 8 ) | 0xbd;
843 p_es = input_AddES( p_input,
844 p_input->stream.pp_programs[0], i_id, 0 );
845 p_es->i_stream_id = 0xbd;
846 p_es->i_type = DVD_SPU_ES;
847 p_es->i_cat = SPU_ES;
848 strcpy( p_es->psz_desc, Language( hton16(
849 vts.manager_inf.p_spu_attr[i-1].i_lang_code ) ) );
850 intf_WarnMsg( 3, "dvd info: spu stream %d %s\t(0x%x)",
851 i, p_es->psz_desc, i_id );
853 /* The before the last spu has a 0x0 prefix */
855 ( vts.manager_inf.p_spu_attr[i].i_prefix == 0 );
859 if( p_main->b_audio )
861 /* For audio: first one if none or a not existing one specified */
862 i_audio = main_GetIntVariable( INPUT_CHANNEL_VAR, 1 );
863 if( i_audio < 0 || i_audio > vts.manager_inf.i_audio_nb )
865 main_PutIntVariable( INPUT_CHANNEL_VAR, 1 );
868 if( i_audio > 0 && vts.manager_inf.i_audio_nb > 0 )
870 input_SelectES( p_input, p_input->stream.pp_es[i_audio] );
874 if( p_main->b_video )
876 /* for spu, default is none */
877 i_spu = main_GetIntVariable( INPUT_SUBTITLE_VAR, 0 );
878 if( i_spu < 0 || i_spu > vts.manager_inf.i_spu_nb )
880 main_PutIntVariable( INPUT_SUBTITLE_VAR, 0 );
883 if( i_spu > 0 && vts.manager_inf.i_spu_nb > 0 )
885 i_spu += vts.manager_inf.i_audio_nb;
886 input_SelectES( p_input, p_input->stream.pp_es[i_spu] );
892 p_area = p_input->stream.p_selected_area;
902 if( p_area->i_part != p_dvd->i_chapter )
904 if( ( p_area->i_part > 0 ) &&
905 ( p_area->i_part <= p_area->i_part_nb ))
907 if( DVDChapterSelect( p_dvd, p_area->i_part ) < 0 )
909 intf_ErrMsg( "dvd error: can't set chapter in area" );
910 p_input->b_error = 1;
914 p_input->stream.p_selected_area->i_tell = p_dvd->i_start -
916 p_input->stream.p_selected_area->i_part = p_dvd->i_chapter;
918 intf_WarnMsg( 2, "dvd info: chapter %d start at: %lld",
919 p_area->i_part, p_area->i_tell );
924 p_dvd->i_chapter = 1;
928 p_dvd->p_ifo->vts.title_unit.p_title[p_dvd->i_title_id-1].title
930 if( p_area->i_angle != p_dvd->i_angle )
932 if( title.p_cell_play[p_dvd->i_prg_cell].i_category & 0xf000 )
934 if( ( p_area->i_angle - p_dvd->i_angle ) < 0 )
938 p_dvd->i_prg_cell += ( p_area->i_angle - p_dvd->i_angle );
939 p_dvd->i_angle = p_area->i_angle;
941 DVDFindSector( p_dvd );
942 p_dvd->i_cell += p_dvd->i_angle_cell;
946 p_dvd->i_angle = p_area->i_angle;
949 intf_WarnMsg( 2, "dvd info: angle %d selected", p_area->i_angle );
952 /* warn interface that something has changed */
953 p_input->stream.b_seekable = 1;
954 p_input->stream.b_changed = 1;
955 intf_WarnMsg( 3, "Pos: %lld Size: %lld", p_input->stream.p_selected_area->i_tell,p_input->stream.p_selected_area->i_size );
957 vlc_mutex_unlock( &p_input->stream.stream_lock );
963 /*****************************************************************************
964 * DVDRead: reads data packets into the netlist.
965 *****************************************************************************
966 * Returns -1 in case of error, 0 if everything went well, and 1 in case of
968 *****************************************************************************/
969 static int DVDRead( input_thread_t * p_input,
970 data_packet_t ** pp_packets )
972 thread_dvd_data_t * p_dvd;
973 dvd_netlist_t * p_netlist;
974 input_area_t * p_area;
975 struct iovec * p_vec;
976 struct data_packet_s * pp_data[p_input->i_read_once];
989 p_dvd = (thread_dvd_data_t *)p_input->p_plugin_data;
990 p_netlist = (dvd_netlist_t *)p_input->p_method_data;
992 /* Get an iovec pointer */
993 if( ( p_vec = DVDGetiovec( p_netlist ) ) == NULL )
995 intf_ErrMsg( "dvd error: can't get iovec" );
999 i_block_once = p_dvd->i_end_sector - p_dvd->i_sector + 1;
1001 /* Get the position of the next cell if we're at cell end */
1002 if( i_block_once <= 0 )
1007 p_dvd->i_angle_cell++;
1009 /* Find cell index in adress map */
1010 if( DVDFindSector( p_dvd ) < 0 )
1012 pp_packets[0] = NULL;
1013 intf_ErrMsg( "dvd error: can't find next cell" );
1017 /* Position the fd pointer on the right address */
1018 i_off = lseek( p_dvd->i_fd,
1019 p_dvd->i_title_start +
1020 (off_t)( p_dvd->i_sector ) *DVD_LB_SIZE, SEEK_SET );
1022 /* update chapter : it will be easier when we have navigation
1024 if( p_dvd->i_chapter < ( p_dvd->i_chapter_nb - 1 ) )
1026 if( title.p_cell_play[p_dvd->i_prg_cell].i_category & 0xf000 )
1028 i_angle = p_dvd->i_angle - 1;
1034 if( title.chapter_map.pi_start_cell[p_dvd->i_chapter] <=
1035 ( p_dvd->i_prg_cell - i_angle + 1 ) )
1041 vlc_mutex_lock( &p_input->stream.stream_lock );
1043 p_input->stream.p_selected_area->i_tell = i_off -
1044 p_input->stream.p_selected_area->i_start;
1045 p_input->stream.p_selected_area->i_part = p_dvd->i_chapter;
1047 /* the synchro has to be reinitialized when we change cell */
1048 p_input->stream.pp_programs[0]->i_synchro_state = SYNCHRO_REINIT;
1050 vlc_mutex_unlock( &p_input->stream.stream_lock );
1052 i_block_once = p_dvd->i_end_sector - p_dvd->i_sector + 1;
1055 /* the number of blocks read is the maw between the requested
1056 * value and the leaving block in the cell */
1057 if( i_block_once > p_dvd->i_block_once )
1059 i_block_once = p_dvd->i_block_once;
1061 //intf_WarnMsg( 2, "Sector: 0x%x Read: %d Chapter: %d", p_dvd->i_sector, i_block_once, p_dvd->i_chapter );
1063 p_netlist->i_read_once = i_block_once;
1065 /* Reads from DVD */
1066 i_read_bytes = readv( p_dvd->i_fd, p_vec, i_block_once );
1067 i_read_blocks = ( i_read_bytes + 0x7ff ) >> 11;
1069 /* Update netlist indexes */
1070 DVDMviovec( p_netlist, i_read_blocks, pp_data );
1072 /* Update global position */
1073 p_dvd->i_sector += i_read_blocks;
1077 /* Read headers to compute payload length */
1078 for( i_iovec = 0 ; i_iovec < i_read_blocks ; i_iovec++ )
1080 if( p_dvd->b_encrypted )
1082 CSSDescrambleSector( p_dvd->p_css->pi_title_key,
1083 p_vec[i_iovec].iov_base );
1084 ((u8*)(p_vec[i_iovec].iov_base))[0x14] &= 0x8F;
1089 while( i_pos < p_netlist->i_buffer_size )
1091 pi_cur = (u8*)(p_vec[i_iovec].iov_base + i_pos);
1094 if( U32_AT( pi_cur ) != 0x1BA )
1096 /* That's the case for all packets, except pack header. */
1097 i_packet_size = U16_AT( pi_cur + 4 );
1098 pp_packets[i_packet] = DVDNewPtr( p_netlist );
1103 if( ( pi_cur[4] & 0xC0 ) == 0x40 )
1108 else if( ( pi_cur[4] & 0xF0 ) == 0x20 )
1115 intf_ErrMsg( "Unable to determine stream type" );
1119 pp_packets[i_packet] = pp_data[i_iovec];
1123 (*pp_data[i_iovec]->pi_refcount)++;
1125 pp_packets[i_packet]->pi_refcount = pp_data[i_iovec]->pi_refcount;
1127 pp_packets[i_packet]->p_buffer = pp_data[i_iovec]->p_buffer;
1129 pp_packets[i_packet]->p_payload_start =
1130 pp_packets[i_packet]->p_buffer + i_pos;
1132 pp_packets[i_packet]->p_payload_end =
1133 pp_packets[i_packet]->p_payload_start + i_packet_size + 6;
1135 pp_packets[i_packet]->p_next = NULL;
1136 pp_packets[i_packet]->b_discard_payload = 0;
1139 i_pos += i_packet_size + 6;
1143 pp_packets[i_packet] = NULL;
1145 vlc_mutex_lock( &p_input->stream.stream_lock );
1147 p_input->stream.p_selected_area->i_tell += i_read_bytes;
1148 b_eot = !( p_input->stream.p_selected_area->i_tell < p_dvd->i_size );
1149 b_eof = b_eot && ( ( p_dvd->i_title + 1 ) >= p_input->stream.i_area_nb );
1150 p_area = p_input->stream.pp_areas[p_dvd->i_title + 1];
1152 vlc_mutex_unlock( &p_input->stream.stream_lock );
1161 intf_WarnMsg( 3, "dvd info: new title" );
1163 DVDSetArea( p_input, p_area );
1167 if( i_read_blocks == i_block_once )
1175 /*****************************************************************************
1176 * DVDRewind : reads a stream backward
1177 *****************************************************************************/
1178 static int DVDRewind( input_thread_t * p_input )
1183 /*****************************************************************************
1184 * DVDSeek : Goes to a given position on the stream ; this one is used by the
1185 * input and translate chronological position from input to logical postion
1188 * The lock should be taken before calling this function.
1189 *****************************************************************************/
1190 static void DVDSeek( input_thread_t * p_input, off_t i_off )
1192 thread_dvd_data_t * p_dvd;
1199 p_dvd = ( thread_dvd_data_t * )p_input->p_plugin_data;
1201 /* we have to take care of offset of beginning of title */
1202 i_pos = i_off + p_input->stream.p_selected_area->i_start
1203 - p_dvd->i_title_start;
1205 /* update navigation data */
1206 p_dvd->i_sector = i_pos >> 11;
1211 /* parse vobu address map to find program cell */
1212 while( title.p_cell_play[i_prg_cell].i_end_sector < p_dvd->i_sector )
1217 p_dvd->i_prg_cell = i_prg_cell;
1219 if( DVDChooseAngle( p_dvd ) < 0 )
1221 p_input->b_error = 1;
1227 /* Find first title cell which is inside program cell */
1228 if( DVDFindCell( p_dvd ) < 0 )
1230 /* no following cell : we're at eof */
1231 intf_ErrMsg( "dvd error: cell seeking failed" );
1232 p_input->b_error = 1;
1236 i_cell = p_dvd->i_cell;
1238 #define cell p_dvd->p_ifo->vts.cell_inf.p_cell_map[i_cell]
1239 /* parse cell address map to find title cell containing sector */
1240 while( cell.i_end_sector < p_dvd->i_sector )
1245 p_dvd->i_cell = i_cell;
1247 /* if we're inside a multi-angle zone, we have to choose i_sector
1248 * in the current angle ; we can't do it all the time since cells
1249 * can be very wide out of such zones */
1250 if( title.p_cell_play[p_dvd->i_prg_cell].i_category & 0xf000 )
1252 p_dvd->i_sector = MAX(
1253 cell.i_start_sector,
1254 title.p_cell_play[p_dvd->i_prg_cell].i_start_sector );
1257 p_dvd->i_end_sector = MIN(
1259 title.p_cell_play[p_dvd->i_prg_cell].i_end_sector );
1261 /* update chapter */
1262 if( title.p_cell_play[p_dvd->i_prg_cell].i_category & 0xf000 )
1264 i_angle = p_dvd->i_angle - 1;
1270 while( ( title.chapter_map.pi_start_cell[i_chapter] <=
1271 ( p_dvd->i_prg_cell - i_angle + 1 ) ) &&
1272 ( i_chapter < ( p_dvd->i_chapter_nb - 1 ) ) )
1277 p_dvd->i_chapter = i_chapter;
1278 p_input->stream.p_selected_area->i_part = p_dvd->i_chapter;
1280 p_input->stream.p_selected_area->i_tell =
1281 lseek( p_dvd->i_fd, p_dvd->i_title_start +
1282 (off_t)( p_dvd->i_sector ) *DVD_LB_SIZE, SEEK_SET ) -
1283 p_input->stream.p_selected_area->i_start;
1285 intf_WarnMsg( 3, "Program Cell: %d Cell: %d Chapter: %d",
1286 p_dvd->i_prg_cell, p_dvd->i_cell, p_dvd->i_chapter );
1292 #define cell p_dvd->p_ifo->vts.cell_inf
1294 /*****************************************************************************
1295 * DVDFindCell: adjust the title cell index with the program cell
1296 *****************************************************************************/
1297 static int DVDFindCell( thread_dvd_data_t * p_dvd )
1302 i_cell = p_dvd->i_cell;
1303 i_index = p_dvd->i_prg_cell;
1305 if( i_cell >= cell.i_cell_nb )
1310 while( ( ( title.p_cell_pos[i_index].i_vob_id !=
1311 cell.p_cell_map[i_cell].i_vob_id ) ||
1312 ( title.p_cell_pos[i_index].i_cell_id !=
1313 cell.p_cell_map[i_cell].i_cell_id ) ) &&
1314 ( i_cell < cell.i_cell_nb - 1 ) )
1320 intf_WarnMsg( 3, "FindCell: i_cell %d i_index %d found %d nb %d",
1327 p_dvd->i_cell = i_cell;
1334 /*****************************************************************************
1335 * DVDFindSector: find cell index in adress map from index in
1336 * information table program map and give corresponding sectors.
1337 *****************************************************************************/
1338 static int DVDFindSector( thread_dvd_data_t * p_dvd )
1341 if( p_dvd->i_sector > title.p_cell_play[p_dvd->i_prg_cell].i_end_sector )
1343 p_dvd->i_prg_cell++;
1345 if( DVDChooseAngle( p_dvd ) < 0 )
1351 if( DVDFindCell( p_dvd ) < 0 )
1353 intf_ErrMsg( "dvd error: can't find sector" );
1357 /* Find start and end sectors of new cell */
1359 p_dvd->i_sector = MAX(
1360 p_dvd->p_ifo->vts.cell_inf.p_cell_map[p_dvd->i_cell].i_start_sector,
1361 title.p_cell_play[p_dvd->i_prg_cell].i_start_sector );
1362 p_dvd->i_end_sector = MIN(
1363 p_dvd->p_ifo->vts.cell_inf.p_cell_map[p_dvd->i_cell].i_end_sector,
1364 title.p_cell_play[p_dvd->i_prg_cell].i_end_sector );
1366 p_dvd->i_sector = title.p_cell_play[p_dvd->i_prg_cell].i_start_sector;
1367 p_dvd->i_end_sector = title.p_cell_play[p_dvd->i_prg_cell].i_end_sector;
1371 intf_WarnMsg( 3, "cell: %d sector1: 0x%x end1: 0x%x\n"
1372 "index: %d sector2: 0x%x end2: 0x%x\n"
1373 "category: 0x%x ilvu end: 0x%x vobu start 0x%x",
1375 p_dvd->p_ifo->vts.cell_inf.p_cell_map[p_dvd->i_cell].i_start_sector,
1376 p_dvd->p_ifo->vts.cell_inf.p_cell_map[p_dvd->i_cell].i_end_sector,
1378 title.p_cell_play[p_dvd->i_prg_cell].i_start_sector,
1379 title.p_cell_play[p_dvd->i_prg_cell].i_end_sector,
1380 title.p_cell_play[p_dvd->i_prg_cell].i_category,
1381 title.p_cell_play[p_dvd->i_prg_cell].i_first_ilvu_vobu_esector,
1382 title.p_cell_play[p_dvd->i_prg_cell].i_last_vobu_start_sector );
1388 /*****************************************************************************
1389 * DVDChapterSelect: find the cell corresponding to requested chapter
1390 *****************************************************************************/
1391 static int DVDChapterSelect( thread_dvd_data_t * p_dvd, int i_chapter )
1394 /* Find cell index in Program chain for current chapter */
1395 p_dvd->i_prg_cell = title.chapter_map.pi_start_cell[i_chapter-1] - 1;
1397 p_dvd->i_sector = 0;
1399 DVDChooseAngle( p_dvd );
1401 /* Search for cell_index in cell adress_table and initialize
1403 if( DVDFindSector( p_dvd ) < 0 )
1405 intf_ErrMsg( "dvd error: can't select chapter" );
1409 /* start is : beginning of vts vobs + offset to vob x */
1410 p_dvd->i_start = p_dvd->i_title_start +
1411 DVD_LB_SIZE * (off_t)( p_dvd->i_sector );
1413 /* Position the fd pointer on the right address */
1414 p_dvd->i_start = lseek( p_dvd->i_fd, p_dvd->i_start, SEEK_SET );
1416 p_dvd->i_chapter = i_chapter;
1420 /*****************************************************************************
1421 * DVDChooseAngle: select the cell corresponding to the selected angle
1422 *****************************************************************************/
1423 static int DVDChooseAngle( thread_dvd_data_t * p_dvd )
1425 /* basic handling of angles */
1426 switch( ( ( title.p_cell_play[p_dvd->i_prg_cell].i_category & 0xf000 )
1429 /* we enter a muli-angle section */
1431 p_dvd->i_prg_cell += p_dvd->i_angle - 1;
1432 p_dvd->i_angle_cell = 0;
1433 // intf_WarnMsg( 3, "dvd info: choosing angle %d", p_dvd->i_angle );
1435 /* we exit a multi-angle section */
1438 p_dvd->i_prg_cell += p_dvd->i_angle_nb - p_dvd->i_angle;