1 /*****************************************************************************
2 * input_dvdread.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_dvdread.c,v 1.34 2002/03/18 19:14:52 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 *****************************************************************************/
37 #include <videolan/vlc.h>
44 #include <sys/types.h>
50 #ifdef STRNCASECMP_IN_STRINGS_H
55 # include <io.h> /* read() */
57 # include <sys/uio.h> /* struct iovec */
61 # include "input_iovec.h"
64 #include "stream_control.h"
65 #include "input_ext-intf.h"
66 #include "input_ext-dec.h"
67 #include "input_ext-plugins.h"
69 #include "input_dvdread.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 DvdReadInit ( struct input_thread_s * );
83 static void DvdReadEnd ( struct input_thread_s * );
84 static int DvdReadDemux ( struct input_thread_s * );
85 static int DvdReadRewind ( struct input_thread_s * );
87 static int DvdReadOpen ( struct input_thread_s * );
88 static void DvdReadClose ( struct input_thread_s * );
89 static int DvdReadSetArea ( struct input_thread_s *, struct input_area_s * );
90 static int DvdReadSetProgram( struct input_thread_s *, pgrm_descriptor_t * );
91 static int DvdReadRead ( struct input_thread_s *, byte_t *, size_t );
92 static void DvdReadSeek ( struct input_thread_s *, off_t );
94 /* called only from here */
95 static void DvdReadLauchDecoders( input_thread_t * p_input );
96 static void DvdReadHandleDSI( thread_dvd_data_t * p_dvd, u8 * p_data );
97 static void DvdReadFindCell ( thread_dvd_data_t * p_dvd );
99 /*****************************************************************************
100 * Functions exported as capabilities. They are declared as static so that
101 * we don't pollute the namespace too much.
102 *****************************************************************************/
103 void _M( access_getfunctions )( function_list_t * p_function_list )
105 #define access p_function_list->functions.access
106 access.pf_open = DvdReadOpen;
107 access.pf_close = DvdReadClose;
108 access.pf_read = DvdReadRead;
109 access.pf_set_area = DvdReadSetArea;
110 access.pf_set_program = DvdReadSetProgram;
111 access.pf_seek = DvdReadSeek;
115 void _M( demux_getfunctions )( function_list_t * p_function_list )
117 #define demux p_function_list->functions.demux
118 demux.pf_init = DvdReadInit;
119 demux.pf_end = DvdReadEnd;
120 demux.pf_demux = DvdReadDemux;
121 demux.pf_rewind = DvdReadRewind;
126 * Data demux functions
129 /*****************************************************************************
130 * DvdReadInit: initializes DVD structures
131 *****************************************************************************/
132 static int DvdReadInit( input_thread_t * p_input )
134 if( p_input->stream.i_method != INPUT_METHOD_DVD )
139 vlc_mutex_lock( &p_input->stream.stream_lock );
141 DvdReadLauchDecoders( p_input );
143 vlc_mutex_unlock( &p_input->stream.stream_lock );
148 /*****************************************************************************
149 * DvdReadEnd: frees unused data
150 *****************************************************************************/
151 static void DvdReadEnd( input_thread_t * p_input )
155 /*****************************************************************************
157 *****************************************************************************/
158 #define PEEK( SIZE ) \
159 i_result = input_Peek( p_input, &p_peek, SIZE ); \
160 if( i_result == -1 ) \
164 else if( i_result < SIZE ) \
170 static int DvdReadDemux( input_thread_t * p_input )
174 data_packet_t * p_data;
179 /* Read headers to compute payload length */
180 for( i = 0 ; i < DVD_BLOCK_READ_ONCE ; i++ )
183 /* Read what we believe to be a packet header. */
187 if( U32_AT( p_peek ) != 0x1BA )
189 /* That's the case for all packets, except pack header. */
190 i_packet_size = U16_AT( p_peek + 4 );
194 /* MPEG-2 Pack header. */
198 /* Fetch a packet of the appropriate size. */
199 i_result = input_SplitBuffer( p_input, &p_data, i_packet_size + 6 );
205 /* In MPEG-2 pack headers we still have to read stuffing bytes. */
206 if( (p_data->p_demux_start[3] == 0xBA) && (i_packet_size == 8) )
208 size_t i_stuffing = (p_data->p_demux_start[13] & 0x7);
209 /* Force refill of the input buffer - though we don't care
210 * about p_peek. Please note that this is unoptimized. */
212 p_input->p_current_data += i_stuffing;
215 input_DemuxPS( p_input, p_data );
222 /*****************************************************************************
223 * DVDRewind : reads a stream backward
224 *****************************************************************************/
225 static int DvdReadRewind( input_thread_t * p_input )
231 * Data access functions
234 /*****************************************************************************
235 * DvdReadOpen: open libdvdread
236 *****************************************************************************/
237 static int DvdReadOpen( struct input_thread_s *p_input )
243 struct stat stat_info;
244 thread_dvd_data_t * p_dvd;
245 dvd_reader_t * p_dvdread;
246 input_area_t * p_area;
252 psz_orig = psz_parser = psz_source = strdup( p_input->psz_name );
258 while( *psz_parser && *psz_parser != '@' )
263 if( *psz_parser == '@' )
269 i_title = (int)strtol( psz_parser, &psz_next, 10 );
272 psz_parser = psz_next + 1;
273 i_chapter = (int)strtol( psz_parser, &psz_next, 10 );
276 i_angle = (int)strtol( psz_next + 1, NULL, 10 );
280 i_title = i_title ? i_title : 1;
281 i_chapter = i_chapter ? i_chapter : 1;
282 i_angle = i_angle ? i_angle : 1;
287 if( !p_input->psz_access )
292 psz_source = config_GetPszVariable( "dvd_device" );
295 if( stat( psz_source, &stat_info ) == -1 )
297 intf_ErrMsg( "input error: cannot stat() source `%s' (%s)",
298 psz_source, strerror(errno));
301 if( !S_ISBLK(stat_info.st_mode) &&
302 !S_ISCHR(stat_info.st_mode) &&
303 !S_ISDIR(stat_info.st_mode) )
305 intf_WarnMsg( 3, "input : DvdRead plugin discarded"
306 " (not a valid source)" );
310 intf_WarnMsg( 2, "input: dvdroot=%s title=%d chapter=%d angle=%d",
311 psz_source, i_title, i_chapter, i_angle );
314 p_dvdread = DVDOpen( psz_source );
316 /* free allocated strings */
317 if( psz_source != psz_orig )
323 intf_ErrMsg( "dvdread error: libdvdcss can't open source" );
330 p_dvd = malloc( sizeof(thread_dvd_data_t) );
333 intf_ErrMsg( "dvdread error: out of memory" );
337 p_dvd->p_dvdread = p_dvdread;
338 p_dvd->p_title = NULL;
339 p_dvd->p_vts_file = NULL;
342 p_input->p_access_data = (void *)p_dvd;
344 /* Ifo allocation & initialisation */
345 if( ! ( p_dvd->p_vmg_file = ifoOpen( p_dvd->p_dvdread, 0 ) ) )
347 intf_ErrMsg( "dvdread error: can't open VMG info" );
351 intf_WarnMsg( 2, "dvdread info: VMG opened" );
353 /* Set stream and area data */
354 vlc_mutex_lock( &p_input->stream.stream_lock );
356 p_input->stream.i_method = INPUT_METHOD_DVD;
358 /* If we are here we can control the pace... */
359 p_input->stream.b_pace_control = 1;
360 p_input->stream.b_seekable = 1;
362 p_input->stream.p_selected_area->i_size = 0;
363 p_input->stream.p_selected_area->i_tell = 0;
365 /* Initialize ES structures */
366 input_InitStream( p_input, sizeof( stream_ps_data_t ) );
368 /* disc input method */
369 p_input->stream.i_method = INPUT_METHOD_DVD;
371 #define tt_srpt p_dvd->p_vmg_file->tt_srpt
372 intf_WarnMsg( 2, "dvdread info: number of titles: %d", tt_srpt->nr_of_srpts );
374 #define area p_input->stream.pp_areas
375 /* We start from 1 here since the default area 0
376 * is reserved for video_ts.vob */
377 for( i = 1 ; i <= tt_srpt->nr_of_srpts ; i++ )
379 input_AddArea( p_input );
381 /* Titles are Program Chains */
384 /* Absolute start offset and size
385 * We can only set that with vts ifo, so we do it during the
386 * first call to DVDSetArea */
387 area[i]->i_start = 0;
390 /* Number of chapters */
391 area[i]->i_part_nb = tt_srpt->title[i-1].nr_of_ptts;
394 area[i]->i_plugin_data = tt_srpt->title[i-1].title_set_nr;
398 p_dvd->i_title = i_title <= tt_srpt->nr_of_srpts ? i_title : 1;
401 p_area = p_input->stream.pp_areas[p_dvd->i_title];
402 p_dvd->i_chapter = i_chapter;
404 p_dvd->i_chapter = i_chapter < p_area->i_part_nb ? i_chapter : 1;
405 p_area->i_part = p_dvd->i_chapter;
407 p_dvd->i_angle = i_angle;
409 /* set title, chapter, audio and subpic */
410 if( DvdReadSetArea( p_input, p_area ) )
412 vlc_mutex_unlock( &p_input->stream.stream_lock );
416 vlc_mutex_unlock( &p_input->stream.stream_lock );
418 p_input->psz_demux = "dvdread";
423 /*****************************************************************************
424 * DvdReadClose: close libdvdread
425 *****************************************************************************/
426 static void DvdReadClose( struct input_thread_s *p_input )
428 thread_dvd_data_t * p_dvd;
430 p_dvd = (thread_dvd_data_t *)p_input->p_access_data;
432 /* close libdvdread */
433 DVDCloseFile( p_dvd->p_title );
434 ifoClose( p_dvd->p_vts_file );
435 ifoClose( p_dvd->p_vmg_file );
437 DVDClose( p_dvd->p_dvdread );
439 p_input->p_access_data = NULL;
443 /*****************************************************************************
444 * DvdReadSetProgram: Does nothing, a DVD is mono-program
445 *****************************************************************************/
446 static int DvdReadSetProgram( input_thread_t * p_input,
447 pgrm_descriptor_t * p_program )
449 if( p_input->stream.p_selected_program != p_program )
451 thread_dvd_data_t * p_dvd;
453 p_dvd = (thread_dvd_data_t*)(p_input->p_access_data);
454 p_dvd->i_angle = p_program->i_number;
456 memcpy( p_program, p_input->stream.p_selected_program,
457 sizeof(pgrm_descriptor_t) );
458 p_program->i_number = p_dvd->i_angle;
459 p_input->stream.p_selected_program = p_program;
461 intf_WarnMsg( 3, "dvd info: angle %d selected", p_dvd->i_angle );
467 #define p_pgc p_dvd->p_cur_pgc
469 /*****************************************************************************
470 * DvdReadSetArea: initialize input data for title x, chapter y.
471 * It should be called for each user navigation request.
472 *****************************************************************************
473 * Take care that i_title starts from 0 (vmg) and i_chapter start from 1.
474 * Note that you have to take the lock before entering here.
475 *****************************************************************************/
476 static int DvdReadSetArea( input_thread_t * p_input, input_area_t * p_area )
478 thread_dvd_data_t * p_dvd;
482 p_dvd = (thread_dvd_data_t*)p_input->p_access_data;
484 /* we can't use the interface slider until initilization is complete */
485 p_input->stream.b_seekable = 0;
487 if( p_area != p_input->stream.p_selected_area )
489 es_descriptor_t * p_es;
495 #define p_vmg p_dvd->p_vmg_file
496 #define p_vts p_dvd->p_vts_file
497 if( p_dvd->p_title != NULL )
499 DVDCloseFile( p_dvd->p_title );
507 /* Reset the Chapter position of the old title */
508 p_input->stream.p_selected_area->i_part = 1;
511 * We have to load all title information
513 /* Change the default area */
514 p_input->stream.p_selected_area = p_area;
516 intf_WarnMsg( 12, "dvdread: open VTS %d, for title %d",
517 p_vmg->tt_srpt->title[ p_area->i_id - 1 ].title_set_nr,
521 if( ! ( p_vts = ifoOpen( p_dvd->p_dvdread,
522 p_vmg->tt_srpt->title[ p_area->i_id - 1 ].title_set_nr ) ) )
524 intf_ErrMsg( "dvdread error: fatal error in vts ifo" );
526 DVDClose( p_dvd->p_dvdread );
530 /* title position inside the selected vts */
531 p_dvd->i_ttn = p_vmg->tt_srpt->title[ p_area->i_id - 1 ].vts_ttn;
534 * Set selected title start
536 pgc_id = p_vts->vts_ptt_srpt->title[p_dvd->i_ttn-1].ptt[0].pgcn;
537 pgn = p_vts->vts_ptt_srpt->title[p_dvd->i_ttn-1].ptt[0].pgn;
538 p_pgc = p_vts->vts_pgcit->pgci_srp[ pgc_id - 1 ].pgc;
539 i_cell = p_pgc->program_map[ pgn - 1 ] - 1;
542 LB2OFF( p_dvd->p_cur_pgc->cell_playback[ i_cell ].first_sector );
544 intf_WarnMsg( 3, "dvdread: start %d vts_title %d pgc %d pgn %d",
545 p_area->i_id, p_dvd->i_ttn, pgc_id, pgn );
550 i_cell = p_dvd->p_cur_pgc->nr_of_cells - 1;
552 p_dvd->i_end_block = p_pgc->cell_playback[ i_cell ].last_sector;
553 p_area->i_size = LB2OFF( p_dvd->i_end_block )- p_area->i_start;
555 intf_WarnMsg( 12, "dvdread: start %lld size %lld end %d",
556 p_area->i_start , p_area->i_size, p_dvd->i_end_block );
559 * Set properties for current chapter
561 /* Remeber current chapter */
562 p_dvd->i_chapter = p_area->i_part;
565 pgc_id = p_vts->vts_ptt_srpt->title[
566 p_dvd->i_ttn-1].ptt[p_area->i_part-1].pgcn;
567 pgn = p_vts->vts_ptt_srpt->title[
568 p_dvd->i_ttn-1].ptt[p_area->i_part-1].pgn;
570 p_pgc = p_vts->vts_pgcit->pgci_srp[pgc_id-1].pgc;
571 p_dvd->i_pack_len = 0;
572 p_dvd->i_next_cell = p_dvd->i_cur_cell = p_pgc->program_map[pgn-1] - 1;
573 DvdReadFindCell( p_dvd );
575 p_dvd->i_next_vobu = p_dvd->i_cur_block =
576 p_pgc->cell_playback[p_dvd->i_cur_cell].first_sector;
581 p_dvd->i_angle_nb = p_vmg->tt_srpt->title[p_area->i_id-1].nr_of_angles;
583 if( p_dvd->i_angle > p_dvd->i_angle_nb )
589 * We've got enough info, time to open the title set data.
591 if( ! ( p_dvd->p_title = DVDOpenFile( p_dvd->p_dvdread,
592 p_vmg->tt_srpt->title[ p_area->i_id - 1 ].title_set_nr,
593 DVD_READ_TITLE_VOBS ) ) )
595 intf_ErrMsg( "dvdread error: can't open title (VTS_%02d_1.VOB)",
596 p_vmg->tt_srpt->title[p_area->i_id-1].title_set_nr );
599 DVDClose( p_dvd->p_dvdread );
603 // IfoPrintTitle( p_dvd );
606 * Destroy obsolete ES by reinitializing program 0
607 * and find all ES in title with ifo data
609 if( p_input->stream.pp_programs != NULL )
611 /* We don't use input_EndStream here since
612 * we keep area structures */
614 while( p_input->stream.i_es_number )
616 input_DelES( p_input, p_input->stream.pp_es[0] );
619 while( p_input->stream.i_pgrm_number )
621 input_DelProgram( p_input, p_input->stream.pp_programs[0] );
624 if( p_input->stream.pp_selected_es )
626 free( p_input->stream.pp_selected_es );
627 p_input->stream.pp_selected_es = NULL;
629 p_input->stream.i_selected_es_number = 0;
632 input_AddProgram( p_input, 1, sizeof( stream_ps_data_t ) );
633 p_input->stream.p_selected_program = p_input->stream.pp_programs[0];
635 for( i = 1 ; i < p_dvd->i_angle_nb ; i++ )
637 input_AddProgram( p_input, i+1, 0 );
640 DvdReadSetProgram( p_input,
641 p_input->stream.pp_programs[p_dvd->i_angle-1] );
643 /* No PSM to read in DVD mode, we already have all information */
644 p_input->stream.p_selected_program->b_is_ok = 1;
648 /* ES 0 -> video MPEG2 */
649 // IfoPrintVideo( p_dvd );
651 p_es = input_AddES( p_input, NULL, 0xe0, 0 );
652 p_es->i_stream_id = 0xe0;
653 p_es->i_type = MPEG2_VIDEO_ES;
654 p_es->i_cat = VIDEO_ES;
656 #define audio_control \
657 p_dvd->p_vts_file->vts_pgcit->pgci_srp[pgc_id-1].pgc->audio_control[i-1]
658 /* Audio ES, in the order they appear in .ifo */
659 for( i = 1 ; i <= p_vts->vtsi_mat->nr_of_vts_audio_streams ; i++ )
664 // IfoPrintAudio( p_dvd, i );
666 /* audio channel is active if first byte is 0x80 */
667 if( audio_control & 0x8000 )
670 i_position = ( audio_control & 0x7F00 ) >> 8;
672 intf_WarnMsg( 12, "dvd audio position %d", i_position );
673 switch( p_vts->vtsi_mat->vts_audio_attr[i-1].audio_format )
676 i_id = ( ( 0x80 + i_position ) << 8 ) | 0xbd;
677 p_es = input_AddES( p_input, NULL, i_id, 0 );
678 p_es->i_stream_id = 0xbd;
679 p_es->i_type = AC3_AUDIO_ES;
681 p_es->i_cat = AUDIO_ES;
682 strcpy( p_es->psz_desc, DecodeLanguage( hton16(
683 p_vts->vtsi_mat->vts_audio_attr[i-1].lang_code ) ) );
684 strcat( p_es->psz_desc, " (ac3)" );
688 case 0x03: /* MPEG audio */
689 i_id = 0xc0 + i_position;
690 p_es = input_AddES( p_input, NULL, i_id, 0 );
691 p_es->i_stream_id = i_id;
692 p_es->i_type = MPEG2_AUDIO_ES;
694 p_es->i_cat = AUDIO_ES;
695 strcpy( p_es->psz_desc, DecodeLanguage( hton16(
696 p_vts->vtsi_mat->vts_audio_attr[i-1].lang_code ) ) );
697 strcat( p_es->psz_desc, " (mpeg)" );
700 case 0x04: /* LPCM */
702 i_id = ( ( 0xa0 + i_position ) << 8 ) | 0xbd;
703 p_es = input_AddES( p_input, NULL, i_id, 0 );
704 p_es->i_stream_id = i_id;
705 p_es->i_type = LPCM_AUDIO_ES;
707 p_es->i_cat = AUDIO_ES;
708 strcpy( p_es->psz_desc, DecodeLanguage( hton16(
709 p_vts->vtsi_mat->vts_audio_attr[i-1].lang_code ) ) );
710 strcat( p_es->psz_desc, " (lpcm)" );
714 i_id = ( ( 0x88 + i_position ) << 8 ) | 0xbd;
715 intf_ErrMsg( "dvd warning: DTS audio not handled yet"
720 intf_ErrMsg( "dvd warning: unknown audio type %.2x",
721 p_vts->vtsi_mat->vts_audio_attr[i-1].audio_format );
726 #define spu_control \
727 p_dvd->p_vts_file->vts_pgcit->pgci_srp[pgc_id-1].pgc->subp_control[i-1]
731 for( i = 1 ; i <= p_vts->vtsi_mat->nr_of_vts_subp_streams; i++ )
736 // IfoPrintSpu( p_dvd, i );
737 intf_WarnMsg( 12, "dvd spu %d 0x%02x", i, spu_control );
739 if( spu_control & 0x80000000 )
743 /* there are several streams for one spu */
744 if( p_vts->vtsi_mat->vts_video_attr.display_aspect_ratio )
747 switch( p_vts->vtsi_mat->vts_video_attr.permitted_df )
750 i_position = spu_control & 0xff;
753 i_position = ( spu_control >> 8 ) & 0xff;
756 i_position = ( spu_control >> 16 ) & 0xff;
763 i_position = ( spu_control >> 24 ) & 0x7F;
766 i_id = ( ( 0x20 + i_position ) << 8 ) | 0xbd;
767 p_es = input_AddES( p_input, NULL, i_id, 0 );
768 p_es->i_stream_id = 0xbd;
769 p_es->i_type = DVD_SPU_ES;
770 p_es->i_cat = SPU_ES;
771 strcpy( p_es->psz_desc, DecodeLanguage( hton16(
772 p_vts->vtsi_mat->vts_subp_attr[i-1].lang_code ) ) );
777 /* FIXME: hack to check that the demuxer is ready, and set
779 if( p_input->p_demux_module )
781 DvdReadLauchDecoders( p_input );
787 p_area = p_input->stream.p_selected_area;
794 if( p_area->i_part != p_dvd->i_chapter )
796 if( ( p_area->i_part > 0 ) &&
797 ( p_area->i_part <= p_area->i_part_nb ))
799 p_dvd->i_ttn = p_vmg->tt_srpt->title[p_area->i_id-1].vts_ttn;
800 pgc_id = p_vts->vts_ptt_srpt->title[
801 p_dvd->i_ttn-1].ptt[p_area->i_part-1].pgcn;
802 pgn = p_vts->vts_ptt_srpt->title[
803 p_dvd->i_ttn-1].ptt[p_area->i_part-1].pgn;
805 p_pgc = p_vts->vts_pgcit->pgci_srp[ pgc_id - 1 ].pgc;
807 p_dvd->i_cur_cell = p_pgc->program_map[ pgn - 1 ] - 1;
808 p_dvd->i_chapter = p_area->i_part;
809 DvdReadFindCell( p_dvd );
811 p_dvd->i_pack_len = 0;
812 p_dvd->i_next_vobu = p_dvd->i_cur_block =
813 p_pgc->cell_playback[p_dvd->i_cur_cell].first_sector;
817 p_area->i_part = p_dvd->i_chapter;
823 /* warn interface that something has changed */
824 p_area->i_tell = LB2OFF( p_dvd->i_next_vobu ) - p_area->i_start;
825 p_input->stream.b_seekable = 1;
826 p_input->stream.b_changed = 1;
832 /*****************************************************************************
833 * DvdReadRead: reads data packets into the netlist.
834 *****************************************************************************
835 * Returns -1 in case of error, 0 if everything went well, and 1 in case of
837 *****************************************************************************/
838 static int DvdReadRead( input_thread_t * p_input,
839 byte_t * p_buffer, size_t i_count )
841 thread_dvd_data_t * p_dvd;
849 p_dvd = (thread_dvd_data_t *)p_input->p_access_data;
853 * Playback by cell in this pgc, starting at the cell for our chapter.
855 i_blocks = OFF2LB( i_count );
862 * End of pack, we select the following one
864 if( ! p_dvd->i_pack_len )
869 if( ( i_read = DVDReadBlocks( p_dvd->p_title, p_dvd->i_next_vobu,
872 intf_ErrMsg( "dvdread error: read failed for block %d",
873 p_dvd->i_next_vobu );
877 /* basic check to be sure we don't have a empty title
878 * go to next title if so */
879 //assert( p_buffer[41] == 0xbf && p_buffer[1027] == 0xbf );
882 * Parse the contained dsi packet.
885 DvdReadHandleDSI( p_dvd, p_buf );
888 if( p_dvd->i_next_vobu >= p_dvd->i_end_block + 1 )
893 assert( p_dvd->i_pack_len < 1024 );
894 /* FIXME: Ugly kludge: we send the pack block to the input for it
895 * sometimes has a zero scr and restart the sync */
896 p_dvd->i_cur_block ++;
897 //p_dvd->i_pack_len++;
900 p_buf += DVD_VIDEO_LB_LEN;
905 * Compute the number of blocks to read
907 i_blocks_once = p_dvd->i_pack_len >= i_blocks
908 ? i_blocks : p_dvd->i_pack_len;
909 p_dvd->i_pack_len -= i_blocks_once;
912 i_read = DVDReadBlocks( p_dvd->p_title, p_dvd->i_cur_block,
913 i_blocks_once, p_buf );
914 if( i_read != i_blocks_once )
916 intf_ErrMsg( "dvdread error: read failed for %d/%d blocks at 0x%02x",
917 i_read, i_blocks_once, p_dvd->i_cur_block );
922 i_read_total += i_read;
923 p_dvd->i_cur_block += i_read;
924 p_buf += LB2OFF( i_read );
928 intf_WarnMsg( 12, "dvdread i_blocks: %d len: %d current: 0x%02x", i_read, p_dvd->i_pack_len, p_dvd->i_cur_block );
931 vlc_mutex_lock( &p_input->stream.stream_lock );
933 p_input->stream.p_selected_area->i_tell =
934 LB2OFF( p_dvd->i_cur_block ) -
935 p_input->stream.p_selected_area->i_start;
939 /* We modify i_part only at end of chapter not to erase
940 * some modification from the interface */
941 p_input->stream.p_selected_area->i_part = p_dvd->i_chapter;
945 if( p_input->stream.p_selected_area->i_tell
946 >= p_input->stream.p_selected_area->i_size || b_eot )
948 if( ( p_input->stream.p_selected_area->i_id + 1 ) >=
949 p_input->stream.i_area_nb )
952 vlc_mutex_unlock( &p_input->stream.stream_lock );
957 intf_WarnMsg( 4, "dvd info: new title" );
958 DvdReadSetArea( p_input, p_input->stream.pp_areas[
959 p_input->stream.p_selected_area->i_id+1] );
960 vlc_mutex_unlock( &p_input->stream.stream_lock );
964 vlc_mutex_unlock( &p_input->stream.stream_lock );
966 return LB2OFF( i_read_total );
970 /*****************************************************************************
971 * DvdReadSeek : Goes to a given position on the stream.
972 *****************************************************************************
973 * This one is used by the input and translate chronological position from
974 * input to logical position on the device.
975 * The lock should be taken before calling this function.
976 *****************************************************************************/
977 static void DvdReadSeek( input_thread_t * p_input, off_t i_off )
979 thread_dvd_data_t * p_dvd;
987 vlc_mutex_lock( &p_input->stream.stream_lock );
988 i_off += p_input->stream.p_selected_area->i_start;
989 vlc_mutex_unlock( &p_input->stream.stream_lock );
991 i_lb = OFF2LB( i_off );
992 p_dvd = ( thread_dvd_data_t * )p_input->p_access_data;
995 while( p_dvd->p_cur_pgc->cell_playback[i_cell].last_sector < i_lb )
1007 pgc_id = p_dvd->p_vts_file->vts_ptt_srpt->title[
1008 p_dvd->i_ttn-1].ptt[i_chapter-1].pgcn;
1009 pgn = p_dvd->p_vts_file->vts_ptt_srpt->title[
1010 p_dvd->i_ttn-1].ptt[i_chapter-1].pgn;
1012 p_pgc = p_dvd->p_vts_file->vts_pgcit->pgci_srp[pgc_id-1].pgc;
1013 i_tmp = p_pgc->program_map[pgn-1];
1015 } while( i_tmp <= i_cell );
1018 while( p_dvd->p_vts_file->vts_vobu_admap->vobu_start_sectors[i_vobu]
1025 while( p_dvd->p_vts_file->vts_c_adt->cell_adr_table[i_sub_cell].start_sector <
1026 p_dvd->p_vts_file->vts_vobu_admap->vobu_start_sectors[i_vobu-1] )
1032 intf_WarnMsg(12, "cell %d i_sub_cell %d chapter %d vobu %d cell_sector %d vobu_sector %d sub_cell_sector %d",
1033 i_cell, i_sub_cell,i_chapter, i_vobu,
1034 p_dvd->p_cur_pgc->cell_playback[i_cell].first_sector,
1035 p_dvd->p_vts_file->vts_vobu_admap->vobu_start_sectors[i_vobu],
1036 p_dvd->p_vts_file->vts_c_adt->cell_adr_table[i_sub_cell-1].start_sector);
1038 p_dvd->i_cur_block = i_lb;
1039 p_dvd->i_next_vobu =
1040 p_dvd->p_vts_file->vts_vobu_admap->vobu_start_sectors[i_vobu];
1041 p_dvd->i_pack_len = p_dvd->i_next_vobu - i_lb;
1042 p_dvd->i_cur_cell = i_cell;
1043 p_dvd->i_chapter = i_chapter;
1044 DvdReadFindCell( p_dvd );
1046 vlc_mutex_lock( &p_input->stream.stream_lock );
1047 p_input->stream.p_selected_area->i_tell =
1048 LB2OFF ( p_dvd->i_cur_block )
1049 - p_input->stream.p_selected_area->i_start;
1050 p_input->stream.p_selected_area->i_part = p_dvd->i_chapter;
1051 vlc_mutex_unlock( &p_input->stream.stream_lock );
1056 /*****************************************************************************
1058 *****************************************************************************/
1059 static void DvdReadHandleDSI( thread_dvd_data_t * p_dvd, u8 * p_data )
1061 navRead_DSI( &(p_dvd->dsi_pack), &(p_data[ DSI_START_BYTE ]) );
1064 * Determine where we go next. These values are the ones we mostly
1067 p_dvd->i_cur_block = p_dvd->dsi_pack.dsi_gi.nv_pck_lbn;
1070 * If we're not at the end of this cell, we can determine the next
1071 * VOBU to display using the VOBU_SRI information section of the
1072 * DSI. Using this value correctly follows the current angle,
1073 * avoiding the doubled scenes in The Matrix, and makes our life
1076 if( p_dvd->dsi_pack.vobu_sri.next_vobu != SRI_END_OF_CELL )
1079 switch( ( p_dvd->dsi_pack.sml_pbi.category & 0xf000 ) >> 12 )
1082 /* interleaved unit with no angle */
1083 if( p_dvd->dsi_pack.sml_pbi.ilvu_sa != -1 )
1085 p_dvd->i_next_vobu = p_dvd->i_cur_block +
1086 p_dvd->dsi_pack.sml_pbi.ilvu_sa;
1087 p_dvd->i_pack_len = p_dvd->dsi_pack.sml_pbi.ilvu_ea;
1091 p_dvd->i_next_vobu = p_dvd->i_cur_block +
1092 p_dvd->dsi_pack.dsi_gi.vobu_ea + 1;
1093 p_dvd->i_pack_len = p_dvd->dsi_pack.dsi_gi.vobu_ea;
1097 /* vobu is end of ilvu */
1098 if( p_dvd->dsi_pack.sml_agli.data[p_dvd->i_angle-1].address )
1100 p_dvd->i_next_vobu = p_dvd->i_cur_block +
1101 p_dvd->dsi_pack.sml_agli.data[p_dvd->i_angle-1].address;
1102 p_dvd->i_pack_len = p_dvd->dsi_pack.sml_pbi.ilvu_ea;
1107 /* vobu is beginning of ilvu */
1111 /* entering interleaved section */
1113 /* non interleaved cells in interleaved section */
1115 p_dvd->i_next_vobu = p_dvd->i_cur_block +
1116 ( p_dvd->dsi_pack.vobu_sri.next_vobu & 0x7fffffff );
1117 p_dvd->i_pack_len = p_dvd->dsi_pack.dsi_gi.vobu_ea;
1121 p_dvd->i_next_vobu = p_dvd->i_cur_block +
1122 ( p_dvd->dsi_pack.vobu_sri.next_vobu & 0x7fffffff );
1123 p_dvd->i_pack_len = p_dvd->dsi_pack.dsi_gi.vobu_ea;
1128 p_dvd->i_cur_cell = p_dvd->i_next_cell;
1129 DvdReadFindCell( p_dvd );
1131 p_dvd->i_pack_len = p_dvd->dsi_pack.dsi_gi.vobu_ea;
1132 p_dvd->i_next_vobu =
1133 p_dvd->p_cur_pgc->cell_playback[p_dvd->i_cur_cell].first_sector;
1137 intf_WarnMsg( 12, "scr %d lbn 0x%02x vobu_ea %d vob_id %d c_id %d",
1138 p_dvd->dsi_pack.dsi_gi.nv_pck_scr,
1139 p_dvd->dsi_pack.dsi_gi.nv_pck_lbn,
1140 p_dvd->dsi_pack.dsi_gi.vobu_ea,
1141 p_dvd->dsi_pack.dsi_gi.vobu_vob_idn,
1142 p_dvd->dsi_pack.dsi_gi.vobu_c_idn );
1144 intf_WarnMsg( 12, "cat 0x%02x ilvu_ea %d ilvu_sa %d size %d",
1145 p_dvd->dsi_pack.sml_pbi.category,
1146 p_dvd->dsi_pack.sml_pbi.ilvu_ea,
1147 p_dvd->dsi_pack.sml_pbi.ilvu_sa,
1148 p_dvd->dsi_pack.sml_pbi.size );
1150 intf_WarnMsg( 12, "next_vobu %d next_ilvu1 %d next_ilvu2 %d",
1151 p_dvd->dsi_pack.vobu_sri.next_vobu & 0x7fffffff,
1152 p_dvd->dsi_pack.sml_agli.data[ p_dvd->i_angle - 1 ].address,
1153 p_dvd->dsi_pack.sml_agli.data[ p_dvd->i_angle ].address);
1157 /*****************************************************************************
1159 *****************************************************************************/
1160 static void DvdReadFindCell( thread_dvd_data_t * p_dvd )
1165 #define cell p_dvd->p_cur_pgc->cell_playback
1166 if( cell[p_dvd->i_cur_cell].block_type == BLOCK_TYPE_ANGLE_BLOCK )
1169 p_dvd->i_next_cell = p_dvd->i_cur_cell + p_dvd->i_angle_nb;
1170 p_dvd->i_cur_cell += p_dvd->i_angle - 1;
1172 p_dvd->i_cur_cell += p_dvd->i_angle - 1;
1174 while( cell[p_dvd->i_cur_cell+i].block_mode != BLOCK_MODE_LAST_CELL )
1178 p_dvd->i_next_cell = p_dvd->i_cur_cell + i + 1;
1183 p_dvd->i_next_cell = p_dvd->i_cur_cell + 1;
1186 pgc_id = p_dvd->p_vts_file->vts_ptt_srpt->title[
1187 p_dvd->i_ttn-1].ptt[p_dvd->i_chapter-1].pgcn;
1188 pgn = p_dvd->p_vts_file->vts_ptt_srpt->title[
1189 p_dvd->i_ttn-1].ptt[p_dvd->i_chapter-1].pgn;
1190 p_pgc = p_dvd->p_vts_file->vts_pgcit->pgci_srp[pgc_id-1].pgc;
1192 if( p_pgc->program_map[pgn-1] <= p_dvd->i_cur_cell )
1199 /*****************************************************************************
1200 * DvdReadLaunchDecoders
1201 *****************************************************************************/
1202 static void DvdReadLauchDecoders( input_thread_t * p_input )
1204 thread_dvd_data_t * p_dvd;
1206 p_dvd = (thread_dvd_data_t*)(p_input->p_access_data);
1208 if( p_main->b_video )
1210 input_SelectES( p_input, p_input->stream.pp_es[0] );
1213 if( p_main->b_audio )
1215 /* For audio: first one if none or a not existing one specified */
1216 int i_audio = config_GetIntVariable( "input_channel" );
1217 if( i_audio < 0 /*|| i_audio > i_audio_nb*/ )
1219 config_PutIntVariable( "input_channel", 1 );
1222 if( i_audio > 0/* && i_audio_nb > 0*/ )
1224 if( config_GetIntVariable( "input_audio" ) == REQUESTED_AC3 )
1226 int i_ac3 = i_audio;
1227 while( ( p_input->stream.pp_es[i_ac3]->i_type !=
1228 AC3_AUDIO_ES ) && ( i_ac3 <=
1229 p_dvd->p_vts_file->vtsi_mat->nr_of_vts_audio_streams ) )
1233 if( p_input->stream.pp_es[i_ac3]->i_type == AC3_AUDIO_ES )
1235 input_SelectES( p_input,
1236 p_input->stream.pp_es[i_ac3] );
1241 input_SelectES( p_input,
1242 p_input->stream.pp_es[i_audio] );
1247 if( p_main->b_video )
1249 /* for spu, default is none */
1250 int i_spu = config_GetIntVariable( "input_subtitle" );
1251 if( i_spu < 0 /*|| i_spu > i_spu_nb*/ )
1253 config_PutIntVariable( "input_subtitle", 0 );
1256 if( i_spu > 0 /*&& i_spu_nb > 0*/ )
1258 i_spu += p_dvd->p_vts_file->vtsi_mat->nr_of_vts_audio_streams;
1259 input_SelectES( p_input, p_input->stream.pp_es[i_spu] );