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.34 2001/03/22 01:23:03 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-css
40 #define MODULE_NAME dvd-nocss
42 #include "modules_inner.h"
47 #include <netinet/in.h>
50 #include <sys/types.h>
66 #include "stream_control.h"
67 #include "input_ext-intf.h"
68 #include "input_ext-dec.h"
72 #include "dvd_netlist.h"
75 #include "input_dvd.h"
76 #include "mpeg_system.h"
82 /*****************************************************************************
84 *****************************************************************************/
93 /* The ISO 639 language codes.
94 * Language names with * prefix are not spelled in their own language
96 { " ", "Not Specified" },
98 { "ab", "*Abkhazian" },
99 { "af", "*Afrikaans" },
100 { "am", "*Amharic" },
102 { "as", "*Assamese" },
104 { "az", "*Azerbaijani" },
105 { "ba", "*Bashkir" },
106 { "be", "*Byelorussian" },
107 { "bg", "*Bulgarian" },
109 { "bi", "*Bislama" },
110 { "bn", "*Bengali; Bangla" },
111 { "bo", "*Tibetan" },
113 { "ca", "*Catalan" },
114 { "co", "*Corsican" },
115 { "cs", "*Czech(Ceske)" },
119 { "dz", "*Bhutani" },
122 { "eo", "*Esperanto" },
124 { "et", "*Estonian" },
126 { "fa", "*Persian" },
129 { "fo", "*Faroese" },
130 { "fr", "Francais" },
131 { "fy", "*Frisian" },
133 { "gd", "*Scots Gaelic" },
134 { "gl", "*Galician" },
135 { "gn", "*Guarani" },
136 { "gu", "*Gujarati" },
138 { "he", "*Hebrew" }, /* formerly iw */
140 { "hr", "Hrvatski" }, /* Croatian */
142 { "hy", "*Armenian" },
143 { "ia", "*Interlingua" },
144 { "id", "*Indonesian" }, /* formerly in */
145 { "ie", "*Interlingue" },
146 { "ik", "*Inupiak" },
147 { "in", "*Indonesian" }, /* replaced by id */
148 { "is", "Islenska" },
149 { "it", "Italiano" },
150 { "iu", "*Inuktitut" },
151 { "iw", "*Hebrew" }, /* replaced by he */
152 { "ja", "*Japanese" },
153 { "ji", "*Yiddish" }, /* replaced by yi */
154 { "jw", "*Javanese" },
155 { "ka", "*Georgian" },
157 { "kl", "*Greenlandic" },
158 { "km", "*Cambodian" },
159 { "kn", "*Kannada" },
161 { "ks", "*Kashmiri" },
162 { "ku", "*Kurdish" },
163 { "ky", "*Kirghiz" },
165 { "ln", "*Lingala" },
166 { "lo", "*Laothian" },
167 { "lt", "*Lithuanian" },
168 { "lv", "*Latvian, Lettish" },
169 { "mg", "*Malagasy" },
171 { "mk", "*Macedonian" },
172 { "ml", "*Malayalam" },
173 { "mn", "*Mongolian" },
174 { "mo", "*Moldavian" },
175 { "mr", "*Marathi" },
177 { "mt", "*Maltese" },
178 { "my", "*Burmese" },
181 { "nl", "Nederlands" },
183 { "oc", "*Occitan" },
184 { "om", "*(Afan) Oromo" },
186 { "pa", "*Punjabi" },
188 { "ps", "*Pashto, Pushto" },
189 { "pt", "Portugues" },
190 { "qu", "*Quechua" },
191 { "rm", "*Rhaeto-Romance" },
192 { "rn", "*Kirundi" },
193 { "ro", "*Romanian" },
194 { "ru", "*Russian" },
195 { "rw", "*Kinyarwanda" },
196 { "sa", "*Sanskrit" },
199 { "sh", "*Serbo-Croatian" },
200 { "si", "*Sinhalese" },
202 { "sl", "*Slovenian" },
206 { "sq", "*Albanian" },
207 { "sr", "*Serbian" },
208 { "ss", "*Siswati" },
209 { "st", "*Sesotho" },
210 { "su", "*Sundanese" },
212 { "sw", "*Swahili" },
217 { "ti", "*Tigrinya" },
218 { "tk", "*Turkmen" },
219 { "tl", "*Tagalog" },
220 { "tn", "*Setswana" },
222 { "tr", "*Turkish" },
227 { "uk", "*Ukrainian" },
230 { "vi", "*Vietnamese" },
231 { "vo", "*Volapuk" },
234 { "yi", "*Yiddish" }, /* formerly ji */
237 { "zh", "*Chinese" },
242 /*****************************************************************************
244 *****************************************************************************/
245 static int DVDProbe ( probedata_t *p_data );
246 static int DVDCheckCSS ( struct input_thread_s * );
247 static int DVDRead ( struct input_thread_s *, data_packet_t ** );
248 static void DVDInit ( struct input_thread_s * );
249 static void DVDEnd ( struct input_thread_s * );
250 static void DVDSeek ( struct input_thread_s *, off_t );
251 static int DVDSetArea ( struct input_thread_s *, struct input_area_s * );
252 static int DVDRewind ( struct input_thread_s * );
254 /*****************************************************************************
255 * Functions exported as capabilities. They are declared as static so that
256 * we don't pollute the namespace too much.
257 *****************************************************************************/
258 void _M( input_getfunctions )( function_list_t * p_function_list )
260 #define input p_function_list->functions.input
261 p_function_list->pf_probe = DVDProbe;
262 input.pf_init = DVDInit;
263 input.pf_open = input_FileOpen;
264 input.pf_close = input_FileClose;
265 input.pf_end = DVDEnd;
266 input.pf_read = DVDRead;
267 input.pf_set_area = DVDSetArea;
268 input.pf_demux = input_DemuxPS;
269 input.pf_new_packet = DVDNewPacket;
270 input.pf_new_pes = DVDNewPES;
271 input.pf_delete_packet = DVDDeletePacket;
272 input.pf_delete_pes = DVDDeletePES;
273 input.pf_rewind = DVDRewind;
274 input.pf_seek = DVDSeek;
279 * Local tools to decode some data in ifo
282 /*****************************************************************************
283 * Language: gives the long language name from the two-letters ISO-639 code
284 *****************************************************************************/
285 static char * Language( u16 i_code )
289 while( memcmp( lang_tbl[i].p_code, &i_code, 2 ) &&
290 lang_tbl[i].p_lang_long[0] )
295 return lang_tbl[i].p_lang_long;
299 * Data reading functions
302 /*****************************************************************************
303 * DVDProbe: verifies that the stream is a PS stream
304 *****************************************************************************/
305 static int DVDProbe( probedata_t *p_data )
307 input_thread_t * p_input = (input_thread_t *)p_data;
309 char * psz_name = p_input->p_source;
313 if( TestMethod( INPUT_METHOD_VAR, "dvd" ) )
319 #endif /* HAVE_CSS */
322 if( ( strlen(psz_name) > 4 ) && !strncasecmp( psz_name, "dvd:", 4 ) )
324 /* If the user specified "dvd:" then it's probably a DVD */
329 #endif /* HAVE_CSS */
333 i_handle = open( psz_name, 0 );
343 /*****************************************************************************
344 * DVDCheckCSS: check the stream
345 *****************************************************************************/
346 static int DVDCheckCSS( input_thread_t * p_input )
348 return CSSTest( p_input->i_handle );
352 /*****************************************************************************
353 * DVDFindCell: adjust the title cell index with the program cell
354 *****************************************************************************/
355 static int DVDFindCell( thread_dvd_data_t * p_dvd )
361 p_pgc = &p_dvd->ifo.vts.pgci_ti.p_srp[p_dvd->i_vts_title-1].pgc;
363 i_cell = p_dvd->i_cell;
364 i_index = p_dvd->i_prg_cell;
366 while( ( ( p_pgc->p_cell_pos_inf[i_index].i_vob_id !=
367 p_dvd->ifo.vts.c_adt.p_cell_inf[i_cell].i_vob_id ) ||
368 ( p_pgc->p_cell_pos_inf[i_index].i_cell_id !=
369 p_dvd->ifo.vts.c_adt.p_cell_inf[i_cell].i_cell_id ) ) &&
370 ( i_cell < p_dvd->ifo.vts.c_adt.i_cell_nb ) )
375 if( i_cell == p_dvd->ifo.vts.c_adt.i_cell_nb )
381 p_dvd->i_cell = i_cell;
386 /*****************************************************************************
387 * DVDFindSector: find cell index in adress map from index in
388 * information table program map and give corresponding sectors.
389 *****************************************************************************/
390 static int DVDFindSector( thread_dvd_data_t * p_dvd )
394 p_pgc = &p_dvd->ifo.vts.pgci_ti.p_srp[p_dvd->i_vts_title-1].pgc;
396 if( p_dvd->i_sector > p_pgc->p_cell_play_inf[p_dvd->i_prg_cell].i_lsector )
401 if( DVDFindCell( p_dvd ) < 0 )
406 /* Find start and end sectors of new cell */
407 p_dvd->i_sector = MAX(
408 p_dvd->ifo.vts.c_adt.p_cell_inf[p_dvd->i_cell].i_ssector,
409 p_pgc->p_cell_play_inf[p_dvd->i_prg_cell].i_entry_sector );
410 p_dvd->i_end_sector = MIN(
411 p_dvd->ifo.vts.c_adt.p_cell_inf[p_dvd->i_cell].i_esector,
412 p_pgc->p_cell_play_inf[p_dvd->i_prg_cell].i_lsector );
414 intf_WarnMsg( 3, "cell: %d sector1: 0x%x end1: 0x%x\nindex: %d sector2: 0x%x end2: 0x%x", p_dvd->i_cell, p_dvd->ifo.vts.c_adt.p_cell_inf[p_dvd->i_cell].i_ssector, p_dvd->ifo.vts.c_adt.p_cell_inf[p_dvd->i_cell].i_esector, p_dvd->i_prg_cell, p_pgc->p_cell_play_inf[p_dvd->i_prg_cell].i_entry_sector, p_pgc->p_cell_play_inf[p_dvd->i_prg_cell].i_lsector );
419 /*****************************************************************************
420 * DVDChapterSelect: find the cell corresponding to requested chapter
421 * When called to find chapter 1, also sets title size and end.
422 *****************************************************************************/
423 static int DVDChapterSelect( thread_dvd_data_t * p_dvd, int i_chapter )
427 p_pgc = &p_dvd->ifo.vts.pgci_ti.p_srp[p_dvd->i_vts_title-1].pgc;
429 /* Find cell index in Program chain for current chapter */
430 p_dvd->i_prg_cell = p_pgc->prg_map.pi_entry_cell[i_chapter-1] - 1;
433 /* Search for cell_index in cell adress_table and initialize start sector */
434 DVDFindSector( p_dvd );
436 /* start is : beginning of vts vobs + offset to vob x */
437 p_dvd->i_start = p_dvd->i_title_start +
438 DVD_LB_SIZE * (off_t)( p_dvd->i_sector );
440 /* Position the fd pointer on the right address */
441 p_dvd->i_start = lseek( p_dvd->i_fd, p_dvd->i_start, SEEK_SET );
443 p_dvd->i_chapter = i_chapter;
448 /*****************************************************************************
449 * DVDSetArea: initialize input data for title x, chapter y.
450 * It should be called for each user navigation request, and to change
451 * audio or sub-picture streams.
453 * Take care that i_title starts from 0 (vmg) and i_chapter start from 1.
454 * i_audio, i_spu start from 1 ; 0 means off.
455 * A negative value for an argument means it does not change
456 *****************************************************************************/
457 static int DVDSetArea( input_thread_t * p_input, input_area_t * p_area )
459 thread_dvd_data_t * p_dvd;
460 es_descriptor_t * p_es;
469 p_dvd = (thread_dvd_data_t*)p_input->p_plugin_data;
471 vlc_mutex_lock( &p_input->stream.stream_lock );
473 if( p_area != p_input->stream.p_selected_area )
477 * We have to load all title information
479 /* Change the default area */
480 p_input->stream.p_selected_area =
481 p_input->stream.pp_areas[p_area->i_id];
483 /* title number: it is not vts nb! */
484 p_dvd->i_title = p_area->i_id;
486 /* title position inside the selected vts */
488 p_dvd->ifo.vmg.ptt_srpt.p_tts[p_dvd->i_title-1].i_vts_ttn;
491 p_dvd->ifo.i_title = p_dvd->i_title;
494 IfoReadVTS( &(p_dvd->ifo) );
495 intf_WarnMsg( 2, "ifo info: vts initialized" );
497 /* css title key for current vts */
498 if( p_dvd->b_encrypted )
500 p_dvd->p_css->i_title =
501 p_dvd->ifo.vmg.ptt_srpt.p_tts[p_dvd->i_title-1].i_tts_nb;
502 p_dvd->p_css->i_title_pos =
503 p_dvd->ifo.vts.i_pos +
504 p_dvd->ifo.vts.mat.i_tt_vobs_ssector * DVD_LB_SIZE;
505 CSSGetKey( p_dvd->p_css );
506 intf_WarnMsg( 2, "css info: vts key initialized" );
510 * Set selected title start and size
513 /* title set offset */
514 p_dvd->i_title_start = p_dvd->ifo.vts.i_pos + DVD_LB_SIZE *
515 (off_t)( p_dvd->ifo.vts.mat.i_tt_vobs_ssector );
517 /* last video cell */
519 p_dvd->i_prg_cell = -1 +
520 p_dvd->ifo.vts.pgci_ti.p_srp[p_dvd->i_vts_title-1].pgc.i_cell_nb;
521 DVDFindCell( p_dvd );
523 /* temporary hack to fix size in some dvds */
524 if( p_dvd->i_cell >= p_dvd->ifo.vts.c_adt.i_cell_nb )
526 p_dvd->i_cell = p_dvd->ifo.vts.c_adt.i_cell_nb - 1;
530 p_dvd->i_size = DVD_LB_SIZE *
531 (off_t)( p_dvd->ifo.vts.c_adt.p_cell_inf[p_dvd->i_cell].i_esector );
533 DVDChapterSelect( p_dvd, 1 );
535 p_dvd->i_size -= (off_t)( p_dvd->i_sector + 1 ) *DVD_LB_SIZE;
537 intf_WarnMsg( 2, "dvd info: title: %d", p_dvd->i_title );
538 intf_WarnMsg( 2, "dvd info: vobstart at: %lld", p_dvd->i_start );
539 intf_WarnMsg( 2, "dvd info: stream size: %lld", p_dvd->i_size );
540 intf_WarnMsg( 2, "dvd info: number of chapters: %d",
541 p_dvd->ifo.vmg.ptt_srpt.p_tts[p_dvd->i_title-1].i_ptt_nb );
543 /* Area definition */
544 p_input->stream.p_selected_area->i_start = p_dvd->i_start;
545 p_input->stream.p_selected_area->i_size = p_dvd->i_size;
548 * Destroy obsolete ES by reinitializing program 0
549 * and find all ES in title with ifo data
551 if( p_input->stream.pp_programs != NULL )
553 /* We don't use input_EndStream here since
554 * we keep area structures */
556 for( i = 0 ; i < p_input->stream.i_selected_es_number ; i++ )
558 input_UnselectES( p_input, p_input->stream.pp_selected_es[i] );
561 input_DelProgram( p_input, p_input->stream.pp_programs[0] );
563 free( p_input->stream.pp_selected_es );
564 p_input->stream.pp_selected_es = NULL;
565 p_input->stream.i_selected_es_number = 0;
568 input_AddProgram( p_input, 0, sizeof( stream_ps_data_t ) );
572 /* ES 0 -> video MPEG2 */
573 p_es = input_AddES( p_input, p_input->stream.pp_programs[0], 0xe0, 0 );
574 p_es->i_stream_id = 0xe0;
575 p_es->i_type = MPEG2_VIDEO_ES;
576 input_SelectES( p_input, p_es );
577 intf_WarnMsg( 1, "dvd info: video MPEG2 stream" );
580 /* Audio ES, in the order they appear in .ifo */
581 i_nb = p_dvd->ifo.vts.mat.i_audio_nb;
586 for( i = 1 ; i <= i_nb ; i++ )
590 intf_DbgMsg( "Audio %d: %x %x %x %x %x %x %x %x %x %x %x %x\n", i,
591 p_dvd->ifo.vts.mat.p_audio_atrt[i-1].i_num_channels,
592 p_dvd->ifo.vts.mat.p_audio_atrt[i-1].i_coding_mode,
593 p_dvd->ifo.vts.mat.p_audio_atrt[i-1].i_multichannel_extension,
594 p_dvd->ifo.vts.mat.p_audio_atrt[i-1].i_type,
595 p_dvd->ifo.vts.mat.p_audio_atrt[i-1].i_appl_mode,
596 p_dvd->ifo.vts.mat.p_audio_atrt[i-1].i_foo,
597 p_dvd->ifo.vts.mat.p_audio_atrt[i-1].i_bar,
598 p_dvd->ifo.vts.mat.p_audio_atrt[i-1].i_appl_mode,
599 p_dvd->ifo.vts.mat.p_audio_atrt[i-1].i_quantization,
600 p_dvd->ifo.vts.mat.p_audio_atrt[i-1].i_sample_freq,
601 p_dvd->ifo.vts.mat.p_audio_atrt[i-1].i_lang_code,
602 p_dvd->ifo.vts.mat.p_audio_atrt[i-1].i_caption );
605 switch( p_dvd->ifo.vts.mat.p_audio_atrt[i-1].i_coding_mode )
608 i_id = ( ( i_ac3 + i ) << 8 ) | 0xbd;
609 p_es = input_AddES( p_input,
610 p_input->stream.pp_programs[0], i_id, 0 );
611 p_es->i_stream_id = 0xbd;
612 p_es->i_type = AC3_AUDIO_ES;
614 strcpy( p_es->psz_desc, Language( hton16(
615 p_dvd->ifo.vts.mat.p_audio_atrt[i-1].i_lang_code ) ) );
617 intf_WarnMsg( 1, "dvd info: audio stream %d %s\t(0x%x)",
618 i, p_es->psz_desc, i_id );
622 case 0x03: /* MPEG audio */
624 p_es = input_AddES( p_input,
625 p_input->stream.pp_programs[0], i_id, 0 );
626 p_es->i_stream_id = i_id;
627 p_es->i_type = MPEG2_AUDIO_ES;
629 strcpy( p_es->psz_desc, Language( hton16(
630 p_dvd->ifo.vts.mat.p_audio_atrt[i-1].i_lang_code ) ) );
632 intf_WarnMsg( 1, "dvd info: audio stream %d %s\t(0x%x)",
633 i, p_es->psz_desc, i_id );
636 case 0x04: /* LPCM */
638 intf_ErrMsg( "dvd warning: LPCM audio not handled yet" );
643 intf_ErrMsg( "dvd warning: DTS audio not handled yet" );
647 intf_ErrMsg( "dvd warning: unknown audio type %.2x",
648 p_dvd->ifo.vts.mat.p_audio_atrt[i-1].i_coding_mode );
654 i_nb = p_dvd->ifo.vts.mat.i_subpic_nb;
658 for( i = 1 ; i <= i_nb ; i++ )
662 i_id = ( i_sub_pic++ << 8 ) | 0xbd;
663 p_es = input_AddES( p_input,
664 p_input->stream.pp_programs[0], i_id, 0 );
665 p_es->i_stream_id = 0xbd;
666 p_es->i_type = DVD_SPU_ES;
668 strcpy( p_es->psz_desc, Language( hton16(
669 p_dvd->ifo.vts.mat.p_subpic_atrt[i-1].i_lang_code ) ) );
670 intf_WarnMsg( 1, "dvd info: spu stream %d %s\t(0x%x)",
671 i, p_es->psz_desc, i_id );
673 /* The before the last spu has a 0x0 prefix */
675 ( p_dvd->ifo.vts.mat.p_subpic_atrt[i].i_prefix == 0 );
682 p_area = p_input->stream.p_selected_area;
689 if( ( p_area->i_part > 0 ) &&
690 ( p_area->i_part <= p_area->i_part_nb ) )
692 DVDChapterSelect( p_dvd, p_area->i_part );
694 p_input->stream.p_selected_area->i_tell = p_dvd->i_start -
696 p_input->stream.p_selected_area->i_part = p_dvd->i_chapter;
698 intf_WarnMsg( 2, "dvd info: chapter %d start at: %lld",
699 p_area->i_part, p_area->i_tell );
702 /* By default first audio stream and no spu */
703 input_SelectES( p_input, p_input->stream.pp_es[1] );
705 /* No PSM to read in DVD mode, we already have all information */
706 p_input->stream.pp_programs[0]->b_is_ok = 1;
708 vlc_mutex_unlock( &p_input->stream.stream_lock );
713 /*****************************************************************************
714 * DVDInit: initializes DVD structures
715 *****************************************************************************/
716 static void DVDInit( input_thread_t * p_input )
718 thread_dvd_data_t * p_dvd;
725 if( (p_dvd = malloc( sizeof(thread_dvd_data_t) )) == NULL )
727 intf_ErrMsg( "Out of memory" );
728 p_input->b_error = 1;
732 p_input->p_plugin_data = (void *)p_dvd;
733 p_input->p_method_data = NULL;
735 p_dvd->i_fd = p_input->i_handle;
737 p_dvd->i_block_once = 32;
738 p_input->i_read_once = 128;
740 p_dvd->b_encrypted = DVDCheckCSS( p_input );
742 lseek( p_input->i_handle, 0, SEEK_SET );
744 /* Reading structures initialisation */
745 p_input->p_method_data =
746 DVDNetlistInit( 8192, 16384, 2048, DVD_LB_SIZE, p_dvd->i_block_once );
747 intf_WarnMsg( 2, "dvd info: netlist initialized" );
749 /* Ifo initialisation */
750 p_dvd->ifo = IfoInit( p_input->i_handle );
751 intf_WarnMsg( 2, "ifo info: vmg initialized" );
753 /* CSS initialisation */
754 if( p_dvd->b_encrypted )
756 p_dvd->p_css = CSSInit( p_input->i_handle );
758 if( p_dvd->p_css == NULL )
760 intf_ErrMsg( "css error: fatal failure" );
761 p_input->b_error = 1;
765 intf_WarnMsg( 2, "css info: initialized" );
768 /* Initialize ES structures */
769 input_InitStream( p_input, sizeof( stream_ps_data_t ) );
771 /* Set stream and area data */
772 vlc_mutex_lock( &p_input->stream.stream_lock );
774 #define srpt p_dvd->ifo.vmg.ptt_srpt
775 intf_WarnMsg( 2, "dvd info: number of titles: %d", srpt.i_ttu_nb );
777 #define area p_input->stream.pp_areas
778 /* We start from 1 here since area 0 is reserved for video_ts.vob */
779 for( i = 1 ; i <= srpt.i_ttu_nb ; i++ )
781 input_AddArea( p_input );
783 /* Titles are Program Chains */
786 /* Absolute start offset and size
787 * We can only set that with vts ifo, so we do it during the
788 * first call to DVDSetArea */
789 area[i]->i_start = 0;
792 /* Number of chapters */
793 area[i]->i_part_nb = srpt.p_tts[i-1].i_ptt_nb;
796 /* Offset to vts_i_0.ifo */
797 area[i]->i_plugin_data = p_dvd->ifo.i_off +
798 ( srpt.p_tts[i-1].i_ssector * DVD_LB_SIZE );
802 vlc_mutex_unlock( &p_input->stream.stream_lock );
804 /* Get requested title - if none try the first title */
805 i_title = main_GetIntVariable( INPUT_TITLE_VAR, 1 );
806 if( i_title <= 0 || i_title > srpt.i_ttu_nb )
811 /* Get requested chapter - if none defaults to first one */
812 i_chapter = main_GetIntVariable( INPUT_CHAPTER_VAR, 1 );
818 p_input->stream.pp_areas[i_title]->i_part = i_chapter;
819 DVDSetArea( p_input, p_input->stream.pp_areas[i_title] );
821 /* For audio: first one if none or a not existing one specified */
822 i_audio = main_GetIntVariable( INPUT_CHANNEL_VAR, 1 );
825 main_PutIntVariable( INPUT_CHANNEL_VAR, 1 );
829 input_ChangeES( p_input, p_input->stream.pp_es[i_audio], 1 );
831 i_spu = main_GetIntVariable( INPUT_SUBTITLE_VAR, 0 );
834 main_PutIntVariable( INPUT_CHANNEL_VAR, 1 );
838 /* We don't select spu here since we don't know the index :
839 * i_spu + i_audio_nb */
840 // input_ChangeES( p_input, p_input->stream.pp_es[i_spu], 1 );
845 /*****************************************************************************
846 * DVDEnd: frees unused data
847 *****************************************************************************/
848 static void DVDEnd( input_thread_t * p_input )
850 thread_dvd_data_t * p_dvd;
851 dvd_netlist_t * p_netlist;
853 p_dvd = (thread_dvd_data_t*)p_input->p_plugin_data;
854 p_netlist = (dvd_netlist_t *)p_input->p_method_data;
856 CSSEnd( p_dvd->p_css );
857 // IfoEnd( p_dvd->p_ifo ) );
859 DVDNetlistEnd( p_netlist );
862 /*****************************************************************************
863 * DVDRead: reads data packets into the netlist.
864 *****************************************************************************
865 * Returns -1 in case of error, 0 if everything went well, and 1 in case of
867 *****************************************************************************/
868 static int DVDRead( input_thread_t * p_input,
869 data_packet_t ** pp_packets )
871 thread_dvd_data_t * p_dvd;
872 dvd_netlist_t * p_netlist;
874 struct iovec * p_vec;
875 struct data_packet_s * pp_data[p_input->i_read_once];
887 p_dvd = (thread_dvd_data_t *)p_input->p_plugin_data;
888 p_netlist = (dvd_netlist_t *)p_input->p_method_data;
889 p_pgc = &p_dvd->ifo.vts.pgci_ti.p_srp[p_dvd->i_vts_title-1].pgc;
891 /* Get an iovec pointer */
892 if( ( p_vec = DVDGetiovec( p_netlist ) ) == NULL )
894 intf_ErrMsg( "DVD: read error" );
898 i_block_once = p_dvd->i_end_sector - p_dvd->i_sector + 1;
900 /* Get the position of the next cell if we're at cell end */
901 if( i_block_once <= 0 )
905 /* Find cell index in adress map */
906 if( DVDFindSector( p_dvd ) < 0 )
908 pp_packets[0] = NULL;
909 intf_ErrMsg( "dvd error: can't find next cell" );
913 /* Position the fd pointer on the right address */
914 i_off = lseek( p_dvd->i_fd,
915 p_dvd->i_title_start +
916 (off_t)( p_dvd->i_sector ) *DVD_LB_SIZE, SEEK_SET );
918 /* update chapter : it will be easier when we have navigation
920 if( p_pgc->prg_map.pi_entry_cell[p_dvd->i_chapter-1] <=
926 vlc_mutex_lock( &p_input->stream.stream_lock );
928 p_input->stream.p_selected_area->i_tell = i_off -
929 p_input->stream.p_selected_area->i_start;
930 p_input->stream.p_selected_area->i_part = p_dvd->i_chapter;
932 /* the synchro has to be reinitialized when we change cell */
933 p_input->stream.pp_programs[0]->i_synchro_state = SYNCHRO_START;
935 vlc_mutex_unlock( &p_input->stream.stream_lock );
937 i_block_once = p_dvd->i_end_sector - p_dvd->i_sector + 1;
940 /* the number of blocks read is the maw between the requested
941 * value and the leaving block in the cell */
942 if( i_block_once > p_dvd->i_block_once )
944 i_block_once = p_dvd->i_block_once;
946 //intf_WarnMsg( 2, "Sector: 0x%x Read: %d Chapter: %d", p_dvd->i_sector, i_block_once, p_dvd->i_chapter );
948 p_netlist->i_read_once = i_block_once;
951 i_read_bytes = readv( p_dvd->i_fd, p_vec, i_block_once );
952 i_read_blocks = ( i_read_bytes + 0x7ff ) >> 11;
954 /* Update netlist indexes */
955 DVDMviovec( p_netlist, i_read_blocks, pp_data );
957 /* Update global position */
958 p_dvd->i_sector += i_read_blocks;
962 /* Read headers to compute payload length */
963 for( i_iovec = 0 ; i_iovec < i_read_blocks ; i_iovec++ )
965 if( p_dvd->b_encrypted )
967 CSSDescrambleSector( p_dvd->p_css->pi_title_key,
968 p_vec[i_iovec].iov_base );
969 ((u8*)(p_vec[i_iovec].iov_base))[0x14] &= 0x8F;
974 while( i_pos < p_netlist->i_buffer_size )
976 pi_cur = (u8*)(p_vec[i_iovec].iov_base + i_pos);
979 if( U32_AT( pi_cur ) != 0x1BA )
981 /* That's the case for all packets, except pack header. */
982 i_packet_size = U16_AT( pi_cur + 4 );
983 pp_packets[i_packet] = DVDNewPtr( p_netlist );
988 if( ( pi_cur[4] & 0xC0 ) == 0x40 )
993 else if( ( pi_cur[4] & 0xF0 ) == 0x20 )
1000 intf_ErrMsg( "Unable to determine stream type" );
1004 pp_packets[i_packet] = pp_data[i_iovec];
1008 (*pp_data[i_iovec]->pi_refcount)++;
1010 pp_packets[i_packet]->pi_refcount = pp_data[i_iovec]->pi_refcount;
1012 pp_packets[i_packet]->p_buffer = pp_data[i_iovec]->p_buffer;
1014 pp_packets[i_packet]->p_payload_start =
1015 pp_packets[i_packet]->p_buffer + i_pos;
1017 pp_packets[i_packet]->p_payload_end =
1018 pp_packets[i_packet]->p_payload_start + i_packet_size + 6;
1020 pp_packets[i_packet]->p_next = NULL;
1021 pp_packets[i_packet]->b_discard_payload = 0;
1024 i_pos += i_packet_size + 6;
1028 pp_packets[i_packet] = NULL;
1030 vlc_mutex_lock( &p_input->stream.stream_lock );
1032 p_input->stream.p_selected_area->i_tell += i_read_bytes;
1033 b_eof = p_input->stream.p_selected_area->i_tell < p_dvd->i_size ? 0 : 1;
1035 vlc_mutex_unlock( &p_input->stream.stream_lock );
1037 if( ( i_read_blocks == i_block_once ) && ( !b_eof ) )
1048 /*****************************************************************************
1049 * DVDRewind : reads a stream backward
1050 *****************************************************************************/
1051 static int DVDRewind( input_thread_t * p_input )
1056 /*****************************************************************************
1057 * DVDSeek : Goes to a given position on the stream ; this one is used by the
1058 * input and translate chronological position from input to logical postion
1061 * The lock should be taken before calling this function.
1062 *****************************************************************************/
1063 static void DVDSeek( input_thread_t * p_input, off_t i_off )
1065 thread_dvd_data_t * p_dvd;
1072 p_dvd = ( thread_dvd_data_t * )p_input->p_plugin_data;
1073 p_pgc = &p_dvd->ifo.vts.pgci_ti.p_srp[p_dvd->i_vts_title-1].pgc;
1075 /* we have to take care of offset of beginning of title */
1076 i_pos = i_off + p_input->stream.p_selected_area->i_start
1077 - p_dvd->i_title_start;
1079 /* update navigation data */
1080 p_dvd->i_sector = i_pos >> 11;
1085 /* parse vobu address map to find program cell */
1086 while( p_pgc->p_cell_play_inf[i_prg_cell].i_lsector < p_dvd->i_sector )
1091 p_dvd->i_prg_cell = i_prg_cell;
1094 /* Find first title cell which is inside program cell */
1095 DVDFindCell( p_dvd );
1097 i_cell = p_dvd->i_cell;
1099 /* parse cell address map to find title cell containing sector */
1100 while( p_dvd->ifo.vts.c_adt.p_cell_inf[i_cell].i_esector < p_dvd->i_sector )
1105 p_dvd->i_cell = i_cell;
1107 p_dvd->i_end_sector = MIN(
1108 p_dvd->ifo.vts.c_adt.p_cell_inf[p_dvd->i_cell].i_esector,
1109 p_pgc->p_cell_play_inf[p_dvd->i_prg_cell].i_lsector );
1111 /* update chapter */
1112 while( p_pgc->prg_map.pi_entry_cell[i_chapter-1] <= p_dvd->i_prg_cell )
1117 p_dvd->i_chapter = i_chapter;
1118 p_input->stream.p_selected_area->i_part = p_dvd->i_chapter;
1120 p_input->stream.p_selected_area->i_tell =
1121 lseek( p_dvd->i_fd, p_dvd->i_title_start +
1122 (off_t)( p_dvd->i_sector ) *DVD_LB_SIZE, SEEK_SET ) -
1123 p_input->stream.p_selected_area->i_start;
1125 intf_WarnMsg( 1, "Program Cell: %d Cell: %d Chapter: %d",
1126 i_prg_cell, i_cell, p_dvd->i_chapter );