1 /*****************************************************************************
2 * input.c: DvdRead plugin.
3 *****************************************************************************
4 * This plugins should handle all the known specificities of the DVD format,
5 * especially the 2048 bytes logical block size.
6 * It depends on: libdvdread for ifo files and block reading.
7 *****************************************************************************
8 * Copyright (C) 2001 VideoLAN
9 * $Id: input.c,v 1.1 2002/08/04 17:23:42 sam Exp $
11 * Author: Stéphane Borel <stef@via.ecp.fr>
13 * Some code taken form the play_title.c by Billy Biggs <vektor@dumbterm.net>
16 * This program is free software; you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License as published by
18 * the Free Software Foundation; either version 2 of the License, or
19 * (at your option) any later version.
21 * This program is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU General Public License for more details.
26 * You should have received a copy of the GNU General Public License
27 * along with this program; if not, write to the Free Software
28 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
29 *****************************************************************************/
31 /*****************************************************************************
33 *****************************************************************************/
38 #include <vlc/input.h>
45 #include <sys/types.h>
51 #ifdef STRNCASECMP_IN_STRINGS_H
56 # include <io.h> /* read() */
58 # include <sys/uio.h> /* struct iovec */
62 # include "input_iovec.h"
65 #include <dvdread/dvd_reader.h>
66 #include <dvdread/ifo_types.h>
67 #include <dvdread/ifo_read.h>
68 #include <dvdread/nav_read.h>
69 #include <dvdread/nav_print.h>
75 /* how many blocks DVDRead will read in each loop */
76 #define DVD_BLOCK_READ_ONCE 64
78 /*****************************************************************************
80 *****************************************************************************/
81 /* called from outside */
82 static int DvdReadDemux ( input_thread_t * );
83 static int DvdReadRewind ( input_thread_t * );
85 static int DvdReadSetArea ( input_thread_t *, input_area_t * );
86 static int DvdReadSetProgram ( input_thread_t *, pgrm_descriptor_t * );
87 static int DvdReadRead ( input_thread_t *, byte_t *, size_t );
88 static void DvdReadSeek ( input_thread_t *, off_t );
90 /* called only from here */
91 static void DvdReadLauchDecoders( input_thread_t * p_input );
92 static void DvdReadHandleDSI( thread_dvd_data_t * p_dvd, u8 * p_data );
93 static void DvdReadFindCell ( thread_dvd_data_t * p_dvd );
96 * Data demux functions
99 /*****************************************************************************
100 * InitDVD: initializes DVD structures
101 *****************************************************************************/
102 int E_(InitDVD) ( vlc_object_t *p_this )
104 input_thread_t *p_input = (input_thread_t *)p_this;
106 if( p_input->stream.i_method != INPUT_METHOD_DVD )
111 p_input->pf_demux = DvdReadDemux;
112 p_input->pf_rewind = NULL;
114 vlc_mutex_lock( &p_input->stream.stream_lock );
116 DvdReadLauchDecoders( p_input );
118 vlc_mutex_unlock( &p_input->stream.stream_lock );
123 /*****************************************************************************
125 *****************************************************************************/
126 #define PEEK( SIZE ) \
127 i_result = input_Peek( p_input, &p_peek, SIZE ); \
128 if( i_result == -1 ) \
132 else if( i_result < SIZE ) \
138 static int DvdReadDemux( input_thread_t * p_input )
142 data_packet_t * p_data;
147 /* Read headers to compute payload length */
148 for( i = 0 ; i < DVD_BLOCK_READ_ONCE ; i++ )
151 /* Read what we believe to be a packet header. */
155 if( U32_AT( p_peek ) != 0x1BA )
157 /* That's the case for all packets, except pack header. */
158 i_packet_size = U16_AT( p_peek + 4 );
162 /* MPEG-2 Pack header. */
166 /* Fetch a packet of the appropriate size. */
167 i_result = input_SplitBuffer( p_input, &p_data, i_packet_size + 6 );
173 /* In MPEG-2 pack headers we still have to read stuffing bytes. */
174 if( (p_data->p_demux_start[3] == 0xBA) && (i_packet_size == 8) )
176 size_t i_stuffing = (p_data->p_demux_start[13] & 0x7);
177 /* Force refill of the input buffer - though we don't care
178 * about p_peek. Please note that this is unoptimized. */
180 p_input->p_current_data += i_stuffing;
183 input_DemuxPS( p_input, p_data );
190 /*****************************************************************************
191 * DVDRewind : reads a stream backward
192 *****************************************************************************/
193 static int DvdReadRewind( input_thread_t * p_input )
199 * Data access functions
202 /*****************************************************************************
203 * OpenDVD: open libdvdread
204 *****************************************************************************/
205 int E_(OpenDVD) ( vlc_object_t *p_this )
207 input_thread_t * p_input = (input_thread_t *)p_this;
212 struct stat stat_info;
213 thread_dvd_data_t * p_dvd;
214 dvd_reader_t * p_dvdread;
215 input_area_t * p_area;
221 psz_orig = psz_parser = psz_source = strdup( p_input->psz_name );
227 p_input->pf_read = DvdReadRead;
228 p_input->pf_seek = DvdReadSeek;
229 p_input->pf_set_area = DvdReadSetArea;
230 p_input->pf_set_program = DvdReadSetProgram;
232 while( *psz_parser && *psz_parser != '@' )
237 if( *psz_parser == '@' )
243 i_title = (int)strtol( psz_parser, &psz_next, 10 );
246 psz_parser = psz_next + 1;
247 i_chapter = (int)strtol( psz_parser, &psz_next, 10 );
250 i_angle = (int)strtol( psz_next + 1, NULL, 10 );
254 i_title = i_title ? i_title : 1;
255 i_chapter = i_chapter ? i_chapter : 1;
256 i_angle = i_angle ? i_angle : 1;
261 if( !p_input->psz_access )
266 psz_source = config_GetPsz( p_input, "dvd" );
269 if( stat( psz_source, &stat_info ) == -1 )
271 msg_Err( p_input, "cannot stat() source `%s' (%s)",
272 psz_source, strerror(errno));
275 if( !S_ISBLK(stat_info.st_mode) &&
276 !S_ISCHR(stat_info.st_mode) &&
277 !S_ISDIR(stat_info.st_mode) )
279 msg_Warn( p_input, "dvdread module discarded (not a valid source)" );
283 msg_Dbg( p_input, "dvdroot=%s title=%d chapter=%d angle=%d",
284 psz_source, i_title, i_chapter, i_angle );
287 p_dvdread = DVDOpen( psz_source );
289 /* free allocated strings */
290 if( psz_source != psz_orig )
296 msg_Err( p_input, "libdvdcss cannot open source" );
303 p_dvd = malloc( sizeof(thread_dvd_data_t) );
306 msg_Err( p_input, "out of memory" );
310 p_dvd->p_dvdread = p_dvdread;
311 p_dvd->p_title = NULL;
312 p_dvd->p_vts_file = NULL;
315 p_input->p_access_data = (void *)p_dvd;
317 /* Ifo allocation & initialisation */
318 if( ! ( p_dvd->p_vmg_file = ifoOpen( p_dvd->p_dvdread, 0 ) ) )
320 msg_Err( p_input, "cannot open VMG info" );
324 msg_Dbg( p_input, "VMG opened" );
326 /* Set stream and area data */
327 vlc_mutex_lock( &p_input->stream.stream_lock );
329 p_input->stream.i_method = INPUT_METHOD_DVD;
331 /* If we are here we can control the pace... */
332 p_input->stream.b_pace_control = 1;
333 p_input->stream.b_seekable = 1;
335 p_input->stream.p_selected_area->i_size = 0;
336 p_input->stream.p_selected_area->i_tell = 0;
338 /* Initialize ES structures */
339 input_InitStream( p_input, sizeof( stream_ps_data_t ) );
341 /* disc input method */
342 p_input->stream.i_method = INPUT_METHOD_DVD;
344 #define tt_srpt p_dvd->p_vmg_file->tt_srpt
345 msg_Dbg( p_input, "number of titles: %d", tt_srpt->nr_of_srpts );
347 #define area p_input->stream.pp_areas
348 /* We start from 1 here since the default area 0
349 * is reserved for video_ts.vob */
350 for( i = 1 ; i <= tt_srpt->nr_of_srpts ; i++ )
352 input_AddArea( p_input );
354 /* Titles are Program Chains */
357 /* Absolute start offset and size
358 * We can only set that with vts ifo, so we do it during the
359 * first call to DVDSetArea */
360 area[i]->i_start = 0;
363 /* Number of chapters */
364 area[i]->i_part_nb = tt_srpt->title[i-1].nr_of_ptts;
367 area[i]->i_plugin_data = tt_srpt->title[i-1].title_set_nr;
371 p_dvd->i_title = i_title <= tt_srpt->nr_of_srpts ? i_title : 1;
374 p_area = p_input->stream.pp_areas[p_dvd->i_title];
375 p_dvd->i_chapter = i_chapter;
377 p_dvd->i_chapter = i_chapter < p_area->i_part_nb ? i_chapter : 1;
378 p_area->i_part = p_dvd->i_chapter;
380 p_dvd->i_angle = i_angle;
382 /* set title, chapter, audio and subpic */
383 if( DvdReadSetArea( p_input, p_area ) )
385 vlc_mutex_unlock( &p_input->stream.stream_lock );
389 vlc_mutex_unlock( &p_input->stream.stream_lock );
391 p_input->psz_demux = "dvdread";
396 /*****************************************************************************
397 * CloseDVD: close libdvdread
398 *****************************************************************************/
399 void E_(CloseDVD) ( vlc_object_t *p_this )
401 input_thread_t * p_input = (input_thread_t *)p_this;
402 thread_dvd_data_t * p_dvd = (thread_dvd_data_t *)p_input->p_access_data;
404 /* close libdvdread */
405 DVDCloseFile( p_dvd->p_title );
406 ifoClose( p_dvd->p_vts_file );
407 ifoClose( p_dvd->p_vmg_file );
409 DVDClose( p_dvd->p_dvdread );
411 p_input->p_access_data = NULL;
415 /*****************************************************************************
416 * DvdReadSetProgram: Does nothing, a DVD is mono-program
417 *****************************************************************************/
418 static int DvdReadSetProgram( input_thread_t * p_input,
419 pgrm_descriptor_t * p_program )
421 if( p_input->stream.p_selected_program != p_program )
423 thread_dvd_data_t * p_dvd;
425 p_dvd = (thread_dvd_data_t*)(p_input->p_access_data);
426 p_dvd->i_angle = p_program->i_number;
428 memcpy( p_program, p_input->stream.p_selected_program,
429 sizeof(pgrm_descriptor_t) );
430 p_program->i_number = p_dvd->i_angle;
431 p_input->stream.p_selected_program = p_program;
433 msg_Dbg( p_input, "angle %d selected", p_dvd->i_angle );
439 #define p_pgc p_dvd->p_cur_pgc
441 /*****************************************************************************
442 * DvdReadSetArea: initialize input data for title x, chapter y.
443 * It should be called for each user navigation request.
444 *****************************************************************************
445 * Take care that i_title starts from 0 (vmg) and i_chapter start from 1.
446 * Note that you have to take the lock before entering here.
447 *****************************************************************************/
448 static int DvdReadSetArea( input_thread_t * p_input, input_area_t * p_area )
450 thread_dvd_data_t * p_dvd;
454 p_dvd = (thread_dvd_data_t*)p_input->p_access_data;
456 /* we can't use the interface slider until initilization is complete */
457 p_input->stream.b_seekable = 0;
459 if( p_area != p_input->stream.p_selected_area )
461 es_descriptor_t * p_es;
467 #define p_vmg p_dvd->p_vmg_file
468 #define p_vts p_dvd->p_vts_file
469 if( p_dvd->p_title != NULL )
471 DVDCloseFile( p_dvd->p_title );
479 /* Reset the Chapter position of the old title */
480 p_input->stream.p_selected_area->i_part = 1;
483 * We have to load all title information
485 /* Change the default area */
486 p_input->stream.p_selected_area = p_area;
488 msg_Dbg( p_input, "open VTS %d, for title %d",
489 p_vmg->tt_srpt->title[ p_area->i_id - 1 ].title_set_nr,
493 if( ! ( p_vts = ifoOpen( p_dvd->p_dvdread,
494 p_vmg->tt_srpt->title[ p_area->i_id - 1 ].title_set_nr ) ) )
496 msg_Err( p_input, "fatal error in vts ifo" );
498 DVDClose( p_dvd->p_dvdread );
502 /* title position inside the selected vts */
503 p_dvd->i_ttn = p_vmg->tt_srpt->title[ p_area->i_id - 1 ].vts_ttn;
506 * Set selected title start
508 pgc_id = p_vts->vts_ptt_srpt->title[p_dvd->i_ttn-1].ptt[0].pgcn;
509 pgn = p_vts->vts_ptt_srpt->title[p_dvd->i_ttn-1].ptt[0].pgn;
510 p_pgc = p_vts->vts_pgcit->pgci_srp[ pgc_id - 1 ].pgc;
511 i_cell = p_pgc->program_map[ pgn - 1 ] - 1;
514 LB2OFF( p_dvd->p_cur_pgc->cell_playback[ i_cell ].first_sector );
516 msg_Dbg( p_input, "start %d vts_title %d pgc %d pgn %d",
517 p_area->i_id, p_dvd->i_ttn, pgc_id, pgn );
522 i_cell = p_dvd->p_cur_pgc->nr_of_cells - 1;
524 p_dvd->i_end_block = p_pgc->cell_playback[ i_cell ].last_sector;
525 p_area->i_size = LB2OFF( p_dvd->i_end_block )- p_area->i_start;
527 msg_Dbg( p_input, "start %lld size %lld end %d",
528 p_area->i_start , p_area->i_size, p_dvd->i_end_block );
531 * Set properties for current chapter
533 /* Remeber current chapter */
534 p_dvd->i_chapter = p_area->i_part;
537 pgc_id = p_vts->vts_ptt_srpt->title[
538 p_dvd->i_ttn-1].ptt[p_area->i_part-1].pgcn;
539 pgn = p_vts->vts_ptt_srpt->title[
540 p_dvd->i_ttn-1].ptt[p_area->i_part-1].pgn;
542 p_pgc = p_vts->vts_pgcit->pgci_srp[pgc_id-1].pgc;
543 p_dvd->i_pack_len = 0;
544 p_dvd->i_next_cell = p_dvd->i_cur_cell = p_pgc->program_map[pgn-1] - 1;
545 DvdReadFindCell( p_dvd );
547 p_dvd->i_next_vobu = p_dvd->i_cur_block =
548 p_pgc->cell_playback[p_dvd->i_cur_cell].first_sector;
553 p_dvd->i_angle_nb = p_vmg->tt_srpt->title[p_area->i_id-1].nr_of_angles;
555 if( p_dvd->i_angle > p_dvd->i_angle_nb )
561 * We've got enough info, time to open the title set data.
563 if( ! ( p_dvd->p_title = DVDOpenFile( p_dvd->p_dvdread,
564 p_vmg->tt_srpt->title[ p_area->i_id - 1 ].title_set_nr,
565 DVD_READ_TITLE_VOBS ) ) )
567 msg_Err( p_input, "cannot open title (VTS_%02d_1.VOB)",
568 p_vmg->tt_srpt->title[p_area->i_id-1].title_set_nr );
571 DVDClose( p_dvd->p_dvdread );
575 // IfoPrintTitle( p_dvd );
578 * Destroy obsolete ES by reinitializing program 0
579 * and find all ES in title with ifo data
581 if( p_input->stream.pp_programs != NULL )
583 /* We don't use input_EndStream here since
584 * we keep area structures */
586 while( p_input->stream.i_es_number )
588 input_DelES( p_input, p_input->stream.pp_es[0] );
591 while( p_input->stream.i_pgrm_number )
593 input_DelProgram( p_input, p_input->stream.pp_programs[0] );
596 if( p_input->stream.pp_selected_es )
598 free( p_input->stream.pp_selected_es );
599 p_input->stream.pp_selected_es = NULL;
601 p_input->stream.i_selected_es_number = 0;
604 input_AddProgram( p_input, 1, sizeof( stream_ps_data_t ) );
605 p_input->stream.p_selected_program = p_input->stream.pp_programs[0];
607 for( i = 1 ; i < p_dvd->i_angle_nb ; i++ )
609 input_AddProgram( p_input, i+1, 0 );
612 DvdReadSetProgram( p_input,
613 p_input->stream.pp_programs[p_dvd->i_angle-1] );
615 /* No PSM to read in DVD mode, we already have all information */
616 p_input->stream.p_selected_program->b_is_ok = 1;
620 /* ES 0 -> video MPEG2 */
621 // IfoPrintVideo( p_dvd );
623 p_es = input_AddES( p_input, NULL, 0xe0, 0 );
624 p_es->i_stream_id = 0xe0;
625 p_es->i_fourcc = VLC_FOURCC('m','p','g','v');
626 p_es->i_cat = VIDEO_ES;
628 #define audio_control \
629 p_dvd->p_vts_file->vts_pgcit->pgci_srp[pgc_id-1].pgc->audio_control[i-1]
630 /* Audio ES, in the order they appear in .ifo */
631 for( i = 1 ; i <= p_vts->vtsi_mat->nr_of_vts_audio_streams ; i++ )
636 // IfoPrintAudio( p_dvd, i );
638 /* audio channel is active if first byte is 0x80 */
639 if( audio_control & 0x8000 )
642 i_position = ( audio_control & 0x7F00 ) >> 8;
644 msg_Dbg( p_input, "audio position %d", i_position );
645 switch( p_vts->vtsi_mat->vts_audio_attr[i-1].audio_format )
648 i_id = ( ( 0x80 + i_position ) << 8 ) | 0xbd;
649 p_es = input_AddES( p_input, NULL, i_id, 0 );
650 p_es->i_stream_id = 0xbd;
651 p_es->i_fourcc = VLC_FOURCC('a','5','2',' ');
652 p_es->i_cat = AUDIO_ES;
653 strcpy( p_es->psz_desc, DecodeLanguage(
654 p_vts->vtsi_mat->vts_audio_attr[i-1].lang_code ) );
655 strcat( p_es->psz_desc, " (A52)" );
659 case 0x03: /* MPEG audio */
660 i_id = 0xc0 + i_position;
661 p_es = input_AddES( p_input, NULL, i_id, 0 );
662 p_es->i_stream_id = i_id;
663 p_es->i_fourcc = VLC_FOURCC('m','p','g','a');
664 p_es->i_cat = AUDIO_ES;
665 strcpy( p_es->psz_desc, DecodeLanguage(
666 p_vts->vtsi_mat->vts_audio_attr[i-1].lang_code ) );
667 strcat( p_es->psz_desc, " (mpeg)" );
670 case 0x04: /* LPCM */
672 i_id = ( ( 0xa0 + i_position ) << 8 ) | 0xbd;
673 p_es = input_AddES( p_input, NULL, i_id, 0 );
674 p_es->i_stream_id = i_id;
675 p_es->i_fourcc = VLC_FOURCC('l','p','c','m');
676 p_es->i_cat = AUDIO_ES;
677 strcpy( p_es->psz_desc, DecodeLanguage(
678 p_vts->vtsi_mat->vts_audio_attr[i-1].lang_code ) );
679 strcat( p_es->psz_desc, " (lpcm)" );
683 i_id = ( ( 0x88 + i_position ) << 8 ) | 0xbd;
684 msg_Err( p_input, "DTS audio not handled yet"
689 msg_Err( p_input, "unknown audio type %.2x",
690 p_vts->vtsi_mat->vts_audio_attr[i-1].audio_format );
695 #define spu_control \
696 p_dvd->p_vts_file->vts_pgcit->pgci_srp[pgc_id-1].pgc->subp_control[i-1]
700 for( i = 1 ; i <= p_vts->vtsi_mat->nr_of_vts_subp_streams; i++ )
705 // IfoPrintSpu( p_dvd, i );
706 msg_Dbg( p_input, "spu %d 0x%02x", i, spu_control );
708 if( spu_control & 0x80000000 )
712 /* there are several streams for one spu */
713 if( p_vts->vtsi_mat->vts_video_attr.display_aspect_ratio )
716 switch( p_vts->vtsi_mat->vts_video_attr.permitted_df )
719 i_position = spu_control & 0xff;
722 i_position = ( spu_control >> 8 ) & 0xff;
725 i_position = ( spu_control >> 16 ) & 0xff;
732 i_position = ( spu_control >> 24 ) & 0x7F;
735 i_id = ( ( 0x20 + i_position ) << 8 ) | 0xbd;
736 p_es = input_AddES( p_input, NULL, i_id, 0 );
737 p_es->i_stream_id = 0xbd;
738 p_es->i_fourcc = VLC_FOURCC('s','p','u',' ');
739 p_es->i_cat = SPU_ES;
740 strcpy( p_es->psz_desc, DecodeLanguage(
741 p_vts->vtsi_mat->vts_subp_attr[i-1].lang_code ) );
746 /* FIXME: hack to check that the demuxer is ready, and set
748 if( p_input->p_demux )
750 DvdReadLauchDecoders( p_input );
756 p_area = p_input->stream.p_selected_area;
763 if( p_area->i_part != p_dvd->i_chapter )
765 if( ( p_area->i_part > 0 ) &&
766 ( p_area->i_part <= p_area->i_part_nb ))
768 p_dvd->i_ttn = p_vmg->tt_srpt->title[p_area->i_id-1].vts_ttn;
769 pgc_id = p_vts->vts_ptt_srpt->title[
770 p_dvd->i_ttn-1].ptt[p_area->i_part-1].pgcn;
771 pgn = p_vts->vts_ptt_srpt->title[
772 p_dvd->i_ttn-1].ptt[p_area->i_part-1].pgn;
774 p_pgc = p_vts->vts_pgcit->pgci_srp[ pgc_id - 1 ].pgc;
776 p_dvd->i_cur_cell = p_pgc->program_map[ pgn - 1 ] - 1;
777 p_dvd->i_chapter = p_area->i_part;
778 DvdReadFindCell( p_dvd );
780 p_dvd->i_pack_len = 0;
781 p_dvd->i_next_vobu = p_dvd->i_cur_block =
782 p_pgc->cell_playback[p_dvd->i_cur_cell].first_sector;
786 p_area->i_part = p_dvd->i_chapter;
792 /* warn interface that something has changed */
793 p_area->i_tell = LB2OFF( p_dvd->i_next_vobu ) - p_area->i_start;
794 p_input->stream.b_seekable = 1;
795 p_input->stream.b_changed = 1;
801 /*****************************************************************************
802 * DvdReadRead: reads data packets into the netlist.
803 *****************************************************************************
804 * Returns -1 in case of error, 0 if everything went well, and 1 in case of
806 *****************************************************************************/
807 static int DvdReadRead( input_thread_t * p_input,
808 byte_t * p_buffer, size_t i_count )
810 thread_dvd_data_t * p_dvd;
816 vlc_bool_t b_eot = 0;
818 p_dvd = (thread_dvd_data_t *)p_input->p_access_data;
822 * Playback by cell in this pgc, starting at the cell for our chapter.
824 i_blocks = OFF2LB( i_count );
831 * End of pack, we select the following one
833 if( ! p_dvd->i_pack_len )
838 if( ( i_read = DVDReadBlocks( p_dvd->p_title, p_dvd->i_next_vobu,
841 msg_Err( p_input, "read failed for block %d",
842 p_dvd->i_next_vobu );
846 /* basic check to be sure we don't have a empty title
847 * go to next title if so */
848 //assert( p_buffer[41] == 0xbf && p_buffer[1027] == 0xbf );
851 * Parse the contained dsi packet.
854 DvdReadHandleDSI( p_dvd, p_buf );
857 if( p_dvd->i_next_vobu >= p_dvd->i_end_block + 1 )
862 assert( p_dvd->i_pack_len < 1024 );
863 /* FIXME: Ugly kludge: we send the pack block to the input for it
864 * sometimes has a zero scr and restart the sync */
865 p_dvd->i_cur_block ++;
866 //p_dvd->i_pack_len++;
869 p_buf += DVD_VIDEO_LB_LEN;
874 * Compute the number of blocks to read
876 i_blocks_once = p_dvd->i_pack_len >= i_blocks
877 ? i_blocks : p_dvd->i_pack_len;
878 p_dvd->i_pack_len -= i_blocks_once;
881 i_read = DVDReadBlocks( p_dvd->p_title, p_dvd->i_cur_block,
882 i_blocks_once, p_buf );
883 if( i_read != i_blocks_once )
885 msg_Err( p_input, "read failed for %d/%d blocks at 0x%02x",
886 i_read, i_blocks_once, p_dvd->i_cur_block );
891 i_read_total += i_read;
892 p_dvd->i_cur_block += i_read;
893 p_buf += LB2OFF( i_read );
897 msg_Dbg( p_input, "i_blocks: %d len: %d current: 0x%02x", i_read, p_dvd->i_pack_len, p_dvd->i_cur_block );
900 vlc_mutex_lock( &p_input->stream.stream_lock );
902 p_input->stream.p_selected_area->i_tell =
903 LB2OFF( p_dvd->i_cur_block ) -
904 p_input->stream.p_selected_area->i_start;
908 /* We modify i_part only at end of chapter not to erase
909 * some modification from the interface */
910 p_input->stream.p_selected_area->i_part = p_dvd->i_chapter;
914 if( p_input->stream.p_selected_area->i_tell
915 >= p_input->stream.p_selected_area->i_size || b_eot )
917 if( ( p_input->stream.p_selected_area->i_id + 1 ) >=
918 p_input->stream.i_area_nb )
921 vlc_mutex_unlock( &p_input->stream.stream_lock );
926 msg_Dbg( p_input, "new title" );
927 DvdReadSetArea( p_input, p_input->stream.pp_areas[
928 p_input->stream.p_selected_area->i_id+1] );
929 vlc_mutex_unlock( &p_input->stream.stream_lock );
933 vlc_mutex_unlock( &p_input->stream.stream_lock );
935 return LB2OFF( i_read_total );
939 /*****************************************************************************
940 * DvdReadSeek : Goes to a given position on the stream.
941 *****************************************************************************
942 * This one is used by the input and translate chronological position from
943 * input to logical position on the device.
944 * The lock should be taken before calling this function.
945 *****************************************************************************/
946 static void DvdReadSeek( input_thread_t * p_input, off_t i_off )
948 thread_dvd_data_t * p_dvd;
956 vlc_mutex_lock( &p_input->stream.stream_lock );
957 i_off += p_input->stream.p_selected_area->i_start;
958 vlc_mutex_unlock( &p_input->stream.stream_lock );
960 i_lb = OFF2LB( i_off );
961 p_dvd = ( thread_dvd_data_t * )p_input->p_access_data;
964 while( p_dvd->p_cur_pgc->cell_playback[i_cell].last_sector < i_lb )
976 pgc_id = p_dvd->p_vts_file->vts_ptt_srpt->title[
977 p_dvd->i_ttn-1].ptt[i_chapter-1].pgcn;
978 pgn = p_dvd->p_vts_file->vts_ptt_srpt->title[
979 p_dvd->i_ttn-1].ptt[i_chapter-1].pgn;
981 p_pgc = p_dvd->p_vts_file->vts_pgcit->pgci_srp[pgc_id-1].pgc;
982 i_tmp = p_pgc->program_map[pgn-1];
984 } while( i_tmp <= i_cell );
987 while( p_dvd->p_vts_file->vts_vobu_admap->vobu_start_sectors[i_vobu]
994 while( p_dvd->p_vts_file->vts_c_adt->cell_adr_table[i_sub_cell].start_sector <
995 p_dvd->p_vts_file->vts_vobu_admap->vobu_start_sectors[i_vobu-1] )
1001 msg_Dbg( p_input, "cell %d i_sub_cell %d chapter %d vobu %d cell_sector %d vobu_sector %d sub_cell_sector %d",
1002 i_cell, i_sub_cell,i_chapter, i_vobu,
1003 p_dvd->p_cur_pgc->cell_playback[i_cell].first_sector,
1004 p_dvd->p_vts_file->vts_vobu_admap->vobu_start_sectors[i_vobu],
1005 p_dvd->p_vts_file->vts_c_adt->cell_adr_table[i_sub_cell-1].start_sector);
1007 p_dvd->i_cur_block = i_lb;
1008 p_dvd->i_next_vobu =
1009 p_dvd->p_vts_file->vts_vobu_admap->vobu_start_sectors[i_vobu];
1010 p_dvd->i_pack_len = p_dvd->i_next_vobu - i_lb;
1011 p_dvd->i_cur_cell = i_cell;
1012 p_dvd->i_chapter = i_chapter;
1013 DvdReadFindCell( p_dvd );
1015 vlc_mutex_lock( &p_input->stream.stream_lock );
1016 p_input->stream.p_selected_area->i_tell =
1017 LB2OFF ( p_dvd->i_cur_block )
1018 - p_input->stream.p_selected_area->i_start;
1019 p_input->stream.p_selected_area->i_part = p_dvd->i_chapter;
1020 vlc_mutex_unlock( &p_input->stream.stream_lock );
1025 /*****************************************************************************
1027 *****************************************************************************/
1028 static void DvdReadHandleDSI( thread_dvd_data_t * p_dvd, u8 * p_data )
1030 navRead_DSI( &(p_dvd->dsi_pack), &(p_data[ DSI_START_BYTE ]) );
1033 * Determine where we go next. These values are the ones we mostly
1036 p_dvd->i_cur_block = p_dvd->dsi_pack.dsi_gi.nv_pck_lbn;
1039 * If we're not at the end of this cell, we can determine the next
1040 * VOBU to display using the VOBU_SRI information section of the
1041 * DSI. Using this value correctly follows the current angle,
1042 * avoiding the doubled scenes in The Matrix, and makes our life
1045 if( p_dvd->dsi_pack.vobu_sri.next_vobu != SRI_END_OF_CELL )
1048 switch( ( p_dvd->dsi_pack.sml_pbi.category & 0xf000 ) >> 12 )
1051 /* interleaved unit with no angle */
1052 if( p_dvd->dsi_pack.sml_pbi.ilvu_sa != -1 )
1054 p_dvd->i_next_vobu = p_dvd->i_cur_block +
1055 p_dvd->dsi_pack.sml_pbi.ilvu_sa;
1056 p_dvd->i_pack_len = p_dvd->dsi_pack.sml_pbi.ilvu_ea;
1060 p_dvd->i_next_vobu = p_dvd->i_cur_block +
1061 p_dvd->dsi_pack.dsi_gi.vobu_ea + 1;
1062 p_dvd->i_pack_len = p_dvd->dsi_pack.dsi_gi.vobu_ea;
1066 /* vobu is end of ilvu */
1067 if( p_dvd->dsi_pack.sml_agli.data[p_dvd->i_angle-1].address )
1069 p_dvd->i_next_vobu = p_dvd->i_cur_block +
1070 p_dvd->dsi_pack.sml_agli.data[p_dvd->i_angle-1].address;
1071 p_dvd->i_pack_len = p_dvd->dsi_pack.sml_pbi.ilvu_ea;
1076 /* vobu is beginning of ilvu */
1080 /* entering interleaved section */
1082 /* non interleaved cells in interleaved section */
1084 p_dvd->i_next_vobu = p_dvd->i_cur_block +
1085 ( p_dvd->dsi_pack.vobu_sri.next_vobu & 0x7fffffff );
1086 p_dvd->i_pack_len = p_dvd->dsi_pack.dsi_gi.vobu_ea;
1090 p_dvd->i_next_vobu = p_dvd->i_cur_block +
1091 ( p_dvd->dsi_pack.vobu_sri.next_vobu & 0x7fffffff );
1092 p_dvd->i_pack_len = p_dvd->dsi_pack.dsi_gi.vobu_ea;
1097 p_dvd->i_cur_cell = p_dvd->i_next_cell;
1098 DvdReadFindCell( p_dvd );
1100 p_dvd->i_pack_len = p_dvd->dsi_pack.dsi_gi.vobu_ea;
1101 p_dvd->i_next_vobu =
1102 p_dvd->p_cur_pgc->cell_playback[p_dvd->i_cur_cell].first_sector;
1106 msg_Dbg( p_input, 12, "scr %d lbn 0x%02x vobu_ea %d vob_id %d c_id %d",
1107 p_dvd->dsi_pack.dsi_gi.nv_pck_scr,
1108 p_dvd->dsi_pack.dsi_gi.nv_pck_lbn,
1109 p_dvd->dsi_pack.dsi_gi.vobu_ea,
1110 p_dvd->dsi_pack.dsi_gi.vobu_vob_idn,
1111 p_dvd->dsi_pack.dsi_gi.vobu_c_idn );
1113 msg_Dbg( p_input, 12, "cat 0x%02x ilvu_ea %d ilvu_sa %d size %d",
1114 p_dvd->dsi_pack.sml_pbi.category,
1115 p_dvd->dsi_pack.sml_pbi.ilvu_ea,
1116 p_dvd->dsi_pack.sml_pbi.ilvu_sa,
1117 p_dvd->dsi_pack.sml_pbi.size );
1119 msg_Dbg( p_input, 12, "next_vobu %d next_ilvu1 %d next_ilvu2 %d",
1120 p_dvd->dsi_pack.vobu_sri.next_vobu & 0x7fffffff,
1121 p_dvd->dsi_pack.sml_agli.data[ p_dvd->i_angle - 1 ].address,
1122 p_dvd->dsi_pack.sml_agli.data[ p_dvd->i_angle ].address);
1126 /*****************************************************************************
1128 *****************************************************************************/
1129 static void DvdReadFindCell( thread_dvd_data_t * p_dvd )
1134 #define cell p_dvd->p_cur_pgc->cell_playback
1135 if( cell[p_dvd->i_cur_cell].block_type == BLOCK_TYPE_ANGLE_BLOCK )
1138 p_dvd->i_next_cell = p_dvd->i_cur_cell + p_dvd->i_angle_nb;
1139 p_dvd->i_cur_cell += p_dvd->i_angle - 1;
1141 p_dvd->i_cur_cell += p_dvd->i_angle - 1;
1143 while( cell[p_dvd->i_cur_cell+i].block_mode != BLOCK_MODE_LAST_CELL )
1147 p_dvd->i_next_cell = p_dvd->i_cur_cell + i + 1;
1152 p_dvd->i_next_cell = p_dvd->i_cur_cell + 1;
1155 pgc_id = p_dvd->p_vts_file->vts_ptt_srpt->title[
1156 p_dvd->i_ttn-1].ptt[p_dvd->i_chapter-1].pgcn;
1157 pgn = p_dvd->p_vts_file->vts_ptt_srpt->title[
1158 p_dvd->i_ttn-1].ptt[p_dvd->i_chapter-1].pgn;
1159 p_pgc = p_dvd->p_vts_file->vts_pgcit->pgci_srp[pgc_id-1].pgc;
1161 if( p_pgc->program_map[pgn-1] <= p_dvd->i_cur_cell )
1168 /*****************************************************************************
1169 * DvdReadLaunchDecoders
1170 *****************************************************************************/
1171 static void DvdReadLauchDecoders( input_thread_t * p_input )
1173 thread_dvd_data_t * p_dvd;
1175 p_dvd = (thread_dvd_data_t*)(p_input->p_access_data);
1177 if( config_GetInt( p_input, "video" ) )
1179 input_SelectES( p_input, p_input->stream.pp_es[0] );
1182 if( config_GetInt( p_input, "audio" ) )
1184 /* For audio: first one if none or a not existing one specified */
1185 int i_audio = config_GetInt( p_input, "audio-channel" );
1186 if( i_audio < 0 /*|| i_audio > i_audio_nb*/ )
1188 config_PutInt( p_input, "audio-channel", 1 );
1191 if( i_audio > 0/* && i_audio_nb > 0*/ )
1193 if( config_GetInt( p_input, "audio-type" )
1196 int i_a52 = i_audio;
1197 while( ( p_input->stream.pp_es[i_a52]->i_fourcc !=
1198 VLC_FOURCC('a','5','2',' ') ) && ( i_a52 <=
1199 p_dvd->p_vts_file->vtsi_mat->nr_of_vts_audio_streams ) )
1203 if( p_input->stream.pp_es[i_a52]->i_fourcc
1204 == VLC_FOURCC('a','5','2',' ') )
1206 input_SelectES( p_input,
1207 p_input->stream.pp_es[i_a52] );
1212 input_SelectES( p_input,
1213 p_input->stream.pp_es[i_audio] );
1218 if( config_GetInt( p_input, "video" ) )
1220 /* for spu, default is none */
1221 int i_spu = config_GetInt( p_input, "spu-channel" );
1222 if( i_spu < 0 /*|| i_spu > i_spu_nb*/ )
1224 config_PutInt( p_input, "spu-channel", 0 );
1227 if( i_spu > 0 /*&& i_spu_nb > 0*/ )
1229 i_spu += p_dvd->p_vts_file->vtsi_mat->nr_of_vts_audio_streams;
1230 input_SelectES( p_input, p_input->stream.pp_es[i_spu] );