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.51 2001/04/28 03:36:25 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
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, 8192, 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;
560 p_dvd = (thread_dvd_data_t*)p_input->p_plugin_data;
562 vlc_mutex_lock( &p_input->stream.stream_lock );
564 if( p_area != p_input->stream.p_selected_area )
568 * We have to load all title information
570 /* Change the default area */
571 p_input->stream.p_selected_area =
572 p_input->stream.pp_areas[p_area->i_id];
574 // vlc_mutex_unlock( &p_input->stream.stream_lock );
576 /* title number: it is not vts nb!,
577 * it is what appears in the interface list */
578 p_dvd->i_title = p_area->i_id;
579 p_dvd->p_ifo->i_title = p_dvd->i_title;
581 /* uodate title environnement variable so that we don't
582 * loop on the same title forever */
583 main_PutIntVariable( INPUT_TITLE_VAR, p_dvd->i_title + 1 );
586 if( IfoTitleSet( p_dvd->p_ifo ) < 0 )
588 intf_ErrMsg( "dvd error: fatal error in vts ifo" );
590 p_input->b_error = 1;
594 #define vmg p_dvd->p_ifo->vmg
595 #define vts p_dvd->p_ifo->vts
596 /* title position inside the selected vts */
598 vmg.title_inf.p_attr[p_dvd->i_title-1].i_title_num;
600 vts.title_inf.p_title_start[p_dvd->i_vts_title-1].i_title_id;
602 intf_WarnMsg( 1, "dvd: title %d vts_title %d pgc %d",
607 /* css title key for current vts */
608 if( p_dvd->b_encrypted )
610 /* this one is vts number */
611 p_dvd->p_css->i_title =
612 vmg.title_inf.p_attr[p_dvd->i_title-1].i_title_set_num;
613 p_dvd->p_css->i_title_pos =
615 vts.manager_inf.i_title_vob_start_sector * DVD_LB_SIZE;
617 j = CSSGetKey( p_input->i_handle, p_dvd->p_css );
620 intf_ErrMsg( "dvd error: fatal error in vts css key" );
622 p_input->b_error = 1;
627 intf_ErrMsg( "dvd error: css decryption unavailable" );
629 p_input->b_error = 1;
637 p_dvd->i_angle_nb = vmg.title_inf.p_attr[p_dvd->i_title-1].i_angle_nb;
638 p_dvd->i_angle = main_GetIntVariable( INPUT_ANGLE_VAR, 1 );
639 if( ( p_dvd->i_angle <= 0 ) || p_dvd->i_angle > p_dvd->i_angle_nb )
645 * Set selected title start and size
648 /* title set offset */
649 p_dvd->i_title_start = vts.i_pos + DVD_LB_SIZE *
650 (off_t)( vts.manager_inf.i_title_vob_start_sector );
652 /* last video cell */
654 p_dvd->i_prg_cell = -1 +
655 vts.title_unit.p_title[p_dvd->i_title_id-1].title.i_cell_nb;
657 if( DVDFindCell( p_dvd ) < 0 )
659 intf_ErrMsg( "dvd error: can't find title end" );
660 p_input->b_error = 1;
664 /* temporary hack to fix size in some dvds */
665 if( p_dvd->i_cell >= vts.cell_inf.i_cell_nb )
667 p_dvd->i_cell = vts.cell_inf.i_cell_nb - 1;
672 p_dvd->i_size = DVD_LB_SIZE *
673 (off_t)( vts.cell_inf.p_cell_map[p_dvd->i_cell].i_end_sector );
674 intf_WarnMsg( 2, "dvd info: stream size 1: %lld @ %d", p_dvd->i_size,
675 vts.cell_inf.p_cell_map[p_dvd->i_cell].i_end_sector );
677 if( DVDChapterSelect( p_dvd, 1 ) < 0 )
679 intf_ErrMsg( "dvd error: can't find first chapter" );
680 p_input->b_error = 1;
684 p_dvd->i_size -= (off_t)( p_dvd->i_sector + 1 ) *DVD_LB_SIZE;
686 intf_WarnMsg( 2, "dvd info: title: %d", p_dvd->i_title );
687 intf_WarnMsg( 2, "dvd info: vobstart at: %lld", p_dvd->i_start );
688 intf_WarnMsg( 2, "dvd info: stream size: %lld", p_dvd->i_size );
689 intf_WarnMsg( 2, "dvd info: number of chapters: %d",
690 vmg.title_inf.p_attr[p_dvd->i_title-1].i_chapter_nb );
691 intf_WarnMsg( 2, "dvd info: number of angles: %d", p_dvd->i_angle_nb );
693 // vlc_mutex_lock( &p_input->stream.stream_lock );
695 /* Area definition */
696 p_input->stream.p_selected_area->i_start = p_dvd->i_start;
697 p_input->stream.p_selected_area->i_size = p_dvd->i_size;
698 p_input->stream.p_selected_area->i_angle_nb = p_dvd->i_angle_nb;
699 p_input->stream.p_selected_area->i_angle = p_dvd->i_angle;
702 * Destroy obsolete ES by reinitializing program 0
703 * and find all ES in title with ifo data
705 if( p_input->stream.pp_programs != NULL )
707 /* We don't use input_EndStream here since
708 * we keep area structures */
710 for( i = 0 ; i < p_input->stream.i_selected_es_number ; i++ )
712 input_UnselectES( p_input, p_input->stream.pp_selected_es[i] );
715 input_DelProgram( p_input, p_input->stream.pp_programs[0] );
717 p_input->stream.pp_selected_es = NULL;
718 p_input->stream.i_selected_es_number = 0;
721 input_AddProgram( p_input, 0, sizeof( stream_ps_data_t ) );
723 /* No PSM to read in DVD mode, we already have all information */
724 p_input->stream.pp_programs[0]->b_is_ok = 1;
725 p_input->stream.pp_programs[0]->i_synchro_state = SYNCHRO_START;
729 /* ES 0 -> video MPEG2 */
730 p_es = input_AddES( p_input, p_input->stream.pp_programs[0], 0xe0, 0 );
731 p_es->i_stream_id = 0xe0;
732 p_es->i_type = MPEG2_VIDEO_ES;
733 p_es->i_cat = VIDEO_ES;
734 intf_WarnMsg( 1, "dvd info: video mpeg2 stream" );
735 if( p_main->b_video )
737 input_SelectES( p_input, p_es );
740 /* Audio ES, in the order they appear in .ifo */
745 for( i = 1 ; i <= vts.manager_inf.i_audio_nb ; i++ )
748 intf_WarnMsg( 1, "Audio %d: %x %x %x %x %x %x %x %x %x %x %x %x", i,
749 vts.manager_inf.p_audio_attr[i-1].i_num_channels,
750 vts.manager_inf.p_audio_attr[i-1].i_coding_mode,
751 vts.manager_inf.p_audio_attr[i-1].i_multichannel_extension,
752 vts.manager_inf.p_audio_attr[i-1].i_type,
753 vts.manager_inf.p_audio_attr[i-1].i_appl_mode,
754 vts.manager_inf.p_audio_attr[i-1].i_foo,
755 vts.manager_inf.p_audio_attr[i-1].i_test,
756 vts.manager_inf.p_audio_attr[i-1].i_bar,
757 vts.manager_inf.p_audio_attr[i-1].i_quantization,
758 vts.manager_inf.p_audio_attr[i-1].i_sample_freq,
759 vts.manager_inf.p_audio_attr[i-1].i_lang_code,
760 vts.manager_inf.p_audio_attr[i-1].i_caption );
762 switch( vts.manager_inf.p_audio_attr[i-1].i_coding_mode )
765 i_id = ( ( i_ac3 + i ) << 8 ) | 0xbd;
766 p_es = input_AddES( p_input,
767 p_input->stream.pp_programs[0], i_id, 0 );
768 p_es->i_stream_id = 0xbd;
769 p_es->i_type = AC3_AUDIO_ES;
771 p_es->i_cat = AUDIO_ES;
772 strcpy( p_es->psz_desc, Language( hton16(
773 vts.manager_inf.p_audio_attr[i-1].i_lang_code ) ) );
774 strcat( p_es->psz_desc, " (ac3)" );
776 intf_WarnMsg( 1, "dvd info: audio stream %d %s\t(0x%x)",
777 i, p_es->psz_desc, i_id );
781 case 0x03: /* MPEG audio */
783 p_es = input_AddES( p_input,
784 p_input->stream.pp_programs[0], i_id, 0 );
785 p_es->i_stream_id = i_id;
786 p_es->i_type = MPEG2_AUDIO_ES;
788 p_es->i_cat = AUDIO_ES;
789 strcpy( p_es->psz_desc, Language( hton16(
790 vts.manager_inf.p_audio_attr[i-1].i_lang_code ) ) );
791 strcat( p_es->psz_desc, " (mpeg)" );
793 intf_WarnMsg( 1, "dvd info: audio stream %d %s\t(0x%x)",
794 i, p_es->psz_desc, i_id );
797 case 0x04: /* LPCM */
799 intf_ErrMsg( "dvd warning: LPCM audio not handled yet" );
804 intf_ErrMsg( "dvd warning: DTS audio not handled yet" );
808 intf_ErrMsg( "dvd warning: unknown audio type %.2x",
809 vts.manager_inf.p_audio_attr[i-1].i_coding_mode );
818 for( i = 1 ; i <= vts.manager_inf.i_spu_nb; i++ )
822 i_id = ( i_sub_pic++ << 8 ) | 0xbd;
823 p_es = input_AddES( p_input,
824 p_input->stream.pp_programs[0], i_id, 0 );
825 p_es->i_stream_id = 0xbd;
826 p_es->i_type = DVD_SPU_ES;
827 p_es->i_cat = SPU_ES;
828 strcpy( p_es->psz_desc, Language( hton16(
829 vts.manager_inf.p_spu_attr[i-1].i_lang_code ) ) );
830 intf_WarnMsg( 1, "dvd info: spu stream %d %s\t(0x%x)",
831 i, p_es->psz_desc, i_id );
833 /* The before the last spu has a 0x0 prefix */
835 ( vts.manager_inf.p_spu_attr[i].i_prefix == 0 );
839 if( p_main->b_audio )
841 /* For audio: first one if none or a not existing one specified */
842 i_audio = main_GetIntVariable( INPUT_CHANNEL_VAR, 1 );
843 if( i_audio < 0 || i_audio > vts.manager_inf.i_audio_nb )
845 main_PutIntVariable( INPUT_CHANNEL_VAR, 1 );
848 if( i_audio > 0 && vts.manager_inf.i_audio_nb > 0 )
850 input_SelectES( p_input, p_input->stream.pp_es[i_audio] );
854 if( p_main->b_video )
856 /* for spu, default is none */
857 i_spu = main_GetIntVariable( INPUT_SUBTITLE_VAR, 0 );
858 if( i_spu < 0 || i_spu > vts.manager_inf.i_spu_nb )
860 main_PutIntVariable( INPUT_SUBTITLE_VAR, 0 );
863 if( i_spu > 0 && vts.manager_inf.i_spu_nb > 0 )
865 i_spu += vts.manager_inf.i_audio_nb;
866 input_SelectES( p_input, p_input->stream.pp_es[i_spu] );
872 p_area = p_input->stream.p_selected_area;
882 if( p_area->i_part != p_dvd->i_chapter )
884 if( ( p_area->i_part > 0 ) &&
885 ( p_area->i_part <= p_area->i_part_nb ))
887 if( DVDChapterSelect( p_dvd, p_area->i_part ) < 0 )
889 intf_ErrMsg( "dvd error: can't set chapter in area" );
890 p_input->b_error = 1;
894 p_input->stream.p_selected_area->i_tell = p_dvd->i_start -
896 p_input->stream.p_selected_area->i_part = p_dvd->i_chapter;
898 intf_WarnMsg( 2, "dvd info: chapter %d start at: %lld",
899 p_area->i_part, p_area->i_tell );
904 p_dvd->i_chapter = 1;
908 p_dvd->p_ifo->vts.title_unit.p_title[p_dvd->i_title_id-1].title
910 if( p_area->i_angle != p_dvd->i_angle )
912 if( title.p_cell_play[p_dvd->i_prg_cell].i_category & 0xf000 )
914 if( ( p_area->i_angle - p_dvd->i_angle ) < 0 )
918 p_dvd->i_prg_cell += ( p_area->i_angle - p_dvd->i_angle );
919 p_dvd->i_angle = p_area->i_angle;
921 DVDFindSector( p_dvd );
922 p_dvd->i_cell += p_dvd->i_angle_cell;
926 p_dvd->i_angle = p_area->i_angle;
929 intf_WarnMsg( 2, "dvd info: angle %d selected", p_area->i_angle );
932 vlc_mutex_unlock( &p_input->stream.stream_lock );
938 /*****************************************************************************
939 * DVDRead: reads data packets into the netlist.
940 *****************************************************************************
941 * Returns -1 in case of error, 0 if everything went well, and 1 in case of
943 *****************************************************************************/
944 static int DVDRead( input_thread_t * p_input,
945 data_packet_t ** pp_packets )
947 thread_dvd_data_t * p_dvd;
948 dvd_netlist_t * p_netlist;
949 struct iovec * p_vec;
950 struct data_packet_s * pp_data[p_input->i_read_once];
962 p_dvd = (thread_dvd_data_t *)p_input->p_plugin_data;
963 p_netlist = (dvd_netlist_t *)p_input->p_method_data;
965 /* Get an iovec pointer */
966 if( ( p_vec = DVDGetiovec( p_netlist ) ) == NULL )
968 intf_ErrMsg( "dvd error: can't get iovec" );
972 i_block_once = p_dvd->i_end_sector - p_dvd->i_sector + 1;
974 /* Get the position of the next cell if we're at cell end */
975 if( i_block_once <= 0 )
980 p_dvd->i_angle_cell++;
982 /* Find cell index in adress map */
983 if( DVDFindSector( p_dvd ) < 0 )
985 pp_packets[0] = NULL;
986 intf_ErrMsg( "dvd error: can't find next cell" );
990 /* Position the fd pointer on the right address */
991 i_off = lseek( p_dvd->i_fd,
992 p_dvd->i_title_start +
993 (off_t)( p_dvd->i_sector ) *DVD_LB_SIZE, SEEK_SET );
995 /* update chapter : it will be easier when we have navigation
997 if( title.p_cell_play[p_dvd->i_prg_cell].i_category & 0xf000 )
999 i_angle = p_dvd->i_angle - 1;
1005 if( title.chapter_map.pi_start_cell[p_dvd->i_chapter-1] <=
1006 ( p_dvd->i_prg_cell - i_angle ) )
1011 vlc_mutex_lock( &p_input->stream.stream_lock );
1013 p_input->stream.p_selected_area->i_tell = i_off -
1014 p_input->stream.p_selected_area->i_start;
1015 p_input->stream.p_selected_area->i_part = p_dvd->i_chapter;
1017 /* the synchro has to be reinitialized when we change cell */
1018 p_input->stream.pp_programs[0]->i_synchro_state = SYNCHRO_START;
1020 vlc_mutex_unlock( &p_input->stream.stream_lock );
1022 i_block_once = p_dvd->i_end_sector - p_dvd->i_sector + 1;
1025 /* the number of blocks read is the maw between the requested
1026 * value and the leaving block in the cell */
1027 if( i_block_once > p_dvd->i_block_once )
1029 i_block_once = p_dvd->i_block_once;
1031 //intf_WarnMsg( 2, "Sector: 0x%x Read: %d Chapter: %d", p_dvd->i_sector, i_block_once, p_dvd->i_chapter );
1033 p_netlist->i_read_once = i_block_once;
1035 /* Reads from DVD */
1036 i_read_bytes = readv( p_dvd->i_fd, p_vec, i_block_once );
1037 i_read_blocks = ( i_read_bytes + 0x7ff ) >> 11;
1039 /* Update netlist indexes */
1040 DVDMviovec( p_netlist, i_read_blocks, pp_data );
1042 /* Update global position */
1043 p_dvd->i_sector += i_read_blocks;
1047 /* Read headers to compute payload length */
1048 for( i_iovec = 0 ; i_iovec < i_read_blocks ; i_iovec++ )
1050 if( p_dvd->b_encrypted )
1052 CSSDescrambleSector( p_dvd->p_css->pi_title_key,
1053 p_vec[i_iovec].iov_base );
1054 ((u8*)(p_vec[i_iovec].iov_base))[0x14] &= 0x8F;
1059 while( i_pos < p_netlist->i_buffer_size )
1061 pi_cur = (u8*)(p_vec[i_iovec].iov_base + i_pos);
1064 if( U32_AT( pi_cur ) != 0x1BA )
1066 /* That's the case for all packets, except pack header. */
1067 i_packet_size = U16_AT( pi_cur + 4 );
1068 pp_packets[i_packet] = DVDNewPtr( p_netlist );
1073 if( ( pi_cur[4] & 0xC0 ) == 0x40 )
1078 else if( ( pi_cur[4] & 0xF0 ) == 0x20 )
1085 intf_ErrMsg( "Unable to determine stream type" );
1089 pp_packets[i_packet] = pp_data[i_iovec];
1093 (*pp_data[i_iovec]->pi_refcount)++;
1095 pp_packets[i_packet]->pi_refcount = pp_data[i_iovec]->pi_refcount;
1097 pp_packets[i_packet]->p_buffer = pp_data[i_iovec]->p_buffer;
1099 pp_packets[i_packet]->p_payload_start =
1100 pp_packets[i_packet]->p_buffer + i_pos;
1102 pp_packets[i_packet]->p_payload_end =
1103 pp_packets[i_packet]->p_payload_start + i_packet_size + 6;
1105 pp_packets[i_packet]->p_next = NULL;
1106 pp_packets[i_packet]->b_discard_payload = 0;
1109 i_pos += i_packet_size + 6;
1113 pp_packets[i_packet] = NULL;
1115 vlc_mutex_lock( &p_input->stream.stream_lock );
1117 p_input->stream.p_selected_area->i_tell += i_read_bytes;
1118 b_eof = !( p_input->stream.p_selected_area->i_tell < p_dvd->i_size );
1120 vlc_mutex_unlock( &p_input->stream.stream_lock );
1122 if( ( i_read_blocks == i_block_once ) && ( !b_eof ) )
1132 /*****************************************************************************
1133 * DVDRewind : reads a stream backward
1134 *****************************************************************************/
1135 static int DVDRewind( input_thread_t * p_input )
1140 /*****************************************************************************
1141 * DVDSeek : Goes to a given position on the stream ; this one is used by the
1142 * input and translate chronological position from input to logical postion
1145 * The lock should be taken before calling this function.
1146 *****************************************************************************/
1147 static void DVDSeek( input_thread_t * p_input, off_t i_off )
1149 thread_dvd_data_t * p_dvd;
1156 p_dvd = ( thread_dvd_data_t * )p_input->p_plugin_data;
1158 /* we have to take care of offset of beginning of title */
1159 i_pos = i_off + p_input->stream.p_selected_area->i_start
1160 - p_dvd->i_title_start;
1162 /* update navigation data */
1163 p_dvd->i_sector = i_pos >> 11;
1168 /* parse vobu address map to find program cell */
1169 while( title.p_cell_play[i_prg_cell].i_end_sector < p_dvd->i_sector )
1174 p_dvd->i_prg_cell = i_prg_cell;
1176 if( DVDChooseAngle( p_dvd ) < 0 )
1178 p_input->b_error = 1;
1184 /* Find first title cell which is inside program cell */
1185 if( DVDFindCell( p_dvd ) < 0 )
1187 /* no following cell : we're at eof */
1188 intf_ErrMsg( "dvd error: cell seeking failed" );
1189 p_input->b_error = 1;
1193 i_cell = p_dvd->i_cell;
1195 #define cell p_dvd->p_ifo->vts.cell_inf.p_cell_map[i_cell]
1196 /* parse cell address map to find title cell containing sector */
1197 while( cell.i_end_sector < p_dvd->i_sector )
1202 p_dvd->i_cell = i_cell;
1204 /* if we're inside a multi-angle zone, we have to choose i_sector
1205 * in the current angle ; we can't do it all the time since cells
1206 * can be very wide out of such zones */
1207 if( title.p_cell_play[p_dvd->i_prg_cell].i_category & 0xf000 )
1209 p_dvd->i_sector = MAX(
1210 cell.i_start_sector,
1211 title.p_cell_play[p_dvd->i_prg_cell].i_start_sector );
1214 p_dvd->i_end_sector = MIN(
1216 title.p_cell_play[p_dvd->i_prg_cell].i_end_sector );
1218 /* update chapter */
1219 if( title.p_cell_play[p_dvd->i_prg_cell].i_category & 0xf000 )
1221 i_angle = p_dvd->i_angle - 1;
1227 while( title.chapter_map.pi_start_cell[i_chapter-1] <=
1228 ( p_dvd->i_prg_cell - i_angle ) )
1233 p_dvd->i_chapter = i_chapter;
1234 p_input->stream.p_selected_area->i_part = p_dvd->i_chapter;
1236 p_input->stream.p_selected_area->i_tell =
1237 lseek( p_dvd->i_fd, p_dvd->i_title_start +
1238 (off_t)( p_dvd->i_sector ) *DVD_LB_SIZE, SEEK_SET ) -
1239 p_input->stream.p_selected_area->i_start;
1241 intf_WarnMsg( 1, "Program Cell: %d Cell: %d Chapter: %d",
1242 p_dvd->i_prg_cell, p_dvd->i_cell, p_dvd->i_chapter );
1248 #define cell p_dvd->p_ifo->vts.cell_inf
1250 /*****************************************************************************
1251 * DVDFindCell: adjust the title cell index with the program cell
1252 *****************************************************************************/
1253 static int DVDFindCell( thread_dvd_data_t * p_dvd )
1258 i_cell = p_dvd->i_cell;
1259 i_index = p_dvd->i_prg_cell;
1261 if( i_cell >= cell.i_cell_nb )
1266 while( ( ( title.p_cell_pos[i_index].i_vob_id !=
1267 cell.p_cell_map[i_cell].i_vob_id ) ||
1268 ( title.p_cell_pos[i_index].i_cell_id !=
1269 cell.p_cell_map[i_cell].i_cell_id ) ) &&
1270 ( i_cell < cell.i_cell_nb - 1 ) )
1276 intf_WarnMsg( 1, "FindCell: i_cell %d i_index %d found %d nb %d",
1283 p_dvd->i_cell = i_cell;
1290 /*****************************************************************************
1291 * DVDFindSector: find cell index in adress map from index in
1292 * information table program map and give corresponding sectors.
1293 *****************************************************************************/
1294 static int DVDFindSector( thread_dvd_data_t * p_dvd )
1297 if( p_dvd->i_sector > title.p_cell_play[p_dvd->i_prg_cell].i_end_sector )
1299 p_dvd->i_prg_cell++;
1301 if( DVDChooseAngle( p_dvd ) < 0 )
1307 if( DVDFindCell( p_dvd ) < 0 )
1309 intf_ErrMsg( "dvd error: can't find sector" );
1313 /* Find start and end sectors of new cell */
1314 p_dvd->i_sector = MAX(
1315 p_dvd->p_ifo->vts.cell_inf.p_cell_map[p_dvd->i_cell].i_start_sector,
1316 title.p_cell_play[p_dvd->i_prg_cell].i_start_sector );
1317 p_dvd->i_end_sector = MIN(
1318 p_dvd->p_ifo->vts.cell_inf.p_cell_map[p_dvd->i_cell].i_end_sector,
1319 title.p_cell_play[p_dvd->i_prg_cell].i_end_sector );
1322 intf_WarnMsg( 1, "cell: %d sector1: 0x%x end1: 0x%x\n"
1323 "index: %d sector2: 0x%x end2: 0x%x\n"
1324 "category: 0x%x ilvu end: 0x%x vobu start 0x%x",
1326 p_dvd->p_ifo->vts.cell_inf.p_cell_map[p_dvd->i_cell].i_start_sector,
1327 p_dvd->p_ifo->vts.cell_inf.p_cell_map[p_dvd->i_cell].i_end_sector,
1329 title.p_cell_play[p_dvd->i_prg_cell].i_start_sector,
1330 title.p_cell_play[p_dvd->i_prg_cell].i_end_sector,
1331 title.p_cell_play[p_dvd->i_prg_cell].i_category,
1332 title.p_cell_play[p_dvd->i_prg_cell].i_first_ilvu_vobu_esector,
1333 title.p_cell_play[p_dvd->i_prg_cell].i_last_vobu_start_sector );
1339 /*****************************************************************************
1340 * DVDChapterSelect: find the cell corresponding to requested chapter
1341 *****************************************************************************/
1342 static int DVDChapterSelect( thread_dvd_data_t * p_dvd, int i_chapter )
1345 /* Find cell index in Program chain for current chapter */
1346 p_dvd->i_prg_cell = title.chapter_map.pi_start_cell[i_chapter-1] - 1;
1348 p_dvd->i_sector = 0;
1350 DVDChooseAngle( p_dvd );
1352 /* Search for cell_index in cell adress_table and initialize
1354 if( DVDFindSector( p_dvd ) < 0 )
1356 intf_ErrMsg( "dvd error: can't select chapter" );
1360 /* start is : beginning of vts vobs + offset to vob x */
1361 p_dvd->i_start = p_dvd->i_title_start +
1362 DVD_LB_SIZE * (off_t)( p_dvd->i_sector );
1364 /* Position the fd pointer on the right address */
1365 p_dvd->i_start = lseek( p_dvd->i_fd, p_dvd->i_start, SEEK_SET );
1367 p_dvd->i_chapter = i_chapter;
1371 /*****************************************************************************
1372 * DVDChooseAngle: select the cell corresponding to the selected angle
1373 *****************************************************************************/
1374 static int DVDChooseAngle( thread_dvd_data_t * p_dvd )
1376 /* basic handling of angles */
1377 switch( ( ( title.p_cell_play[p_dvd->i_prg_cell].i_category & 0xf000 )
1380 /* we enter a muli-angle section */
1382 p_dvd->i_prg_cell += p_dvd->i_angle - 1;
1383 p_dvd->i_angle_cell = 0;
1384 // intf_WarnMsg( 1, "dvd info: choosing angle %d", p_dvd->i_angle );
1386 /* we exit a multi-angle section */
1389 p_dvd->i_prg_cell += p_dvd->i_angle_nb - p_dvd->i_angle;