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.33 2002/03/12 18:37:46 stef 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 vlc_mutex_lock( &p_input->stream.stream_lock );
136 DvdReadLauchDecoders( p_input );
138 vlc_mutex_unlock( &p_input->stream.stream_lock );
143 /*****************************************************************************
144 * DvdReadEnd: frees unused data
145 *****************************************************************************/
146 static void DvdReadEnd( input_thread_t * p_input )
150 /*****************************************************************************
152 *****************************************************************************/
153 #define PEEK( SIZE ) \
154 i_result = input_Peek( p_input, &p_peek, SIZE ); \
155 if( i_result == -1 ) \
159 else if( i_result < SIZE ) \
165 static int DvdReadDemux( input_thread_t * p_input )
169 data_packet_t * p_data;
174 /* Read headers to compute payload length */
175 for( i = 0 ; i < DVD_BLOCK_READ_ONCE ; i++ )
178 /* Read what we believe to be a packet header. */
182 if( U32_AT( p_peek ) != 0x1BA )
184 /* That's the case for all packets, except pack header. */
185 i_packet_size = U16_AT( p_peek + 4 );
189 /* MPEG-2 Pack header. */
193 /* Fetch a packet of the appropriate size. */
194 i_result = input_SplitBuffer( p_input, &p_data, i_packet_size + 6 );
200 /* In MPEG-2 pack headers we still have to read stuffing bytes. */
201 if( (p_data->p_demux_start[3] == 0xBA) && (i_packet_size == 8) )
203 size_t i_stuffing = (p_data->p_demux_start[13] & 0x7);
204 /* Force refill of the input buffer - though we don't care
205 * about p_peek. Please note that this is unoptimized. */
207 p_input->p_current_data += i_stuffing;
210 input_DemuxPS( p_input, p_data );
217 /*****************************************************************************
218 * DVDRewind : reads a stream backward
219 *****************************************************************************/
220 static int DvdReadRewind( input_thread_t * p_input )
226 * Data access functions
229 /*****************************************************************************
230 * DvdReadOpen: open libdvdread
231 *****************************************************************************/
232 static int DvdReadOpen( struct input_thread_s *p_input )
238 struct stat stat_info;
239 thread_dvd_data_t * p_dvd;
240 dvd_reader_t * p_dvdread;
241 input_area_t * p_area;
247 psz_orig = psz_parser = psz_source = strdup( p_input->psz_name );
253 while( *psz_parser && *psz_parser != '@' )
258 if( *psz_parser == '@' )
264 i_title = (int)strtol( psz_parser, &psz_next, 10 );
267 psz_parser = psz_next + 1;
268 i_chapter = (int)strtol( psz_parser, &psz_next, 10 );
271 i_angle = (int)strtol( psz_next + 1, NULL, 10 );
275 i_title = i_title ? i_title : 1;
276 i_chapter = i_chapter ? i_chapter : 1;
277 i_angle = i_angle ? i_angle : 1;
282 if( !p_input->psz_access )
287 psz_source = config_GetPszVariable( "dvd_device" );
290 if( stat( psz_source, &stat_info ) == -1 )
292 intf_ErrMsg( "input error: cannot stat() source `%s' (%s)",
293 psz_source, strerror(errno));
296 if( !S_ISBLK(stat_info.st_mode) &&
297 !S_ISCHR(stat_info.st_mode) &&
298 !S_ISDIR(stat_info.st_mode) )
300 intf_WarnMsg( 3, "input : DvdRead plugin discarded"
301 " (not a valid source)" );
305 intf_WarnMsg( 2, "input: dvdroot=%s title=%d chapter=%d angle=%d",
306 psz_source, i_title, i_chapter, i_angle );
309 p_dvdread = DVDOpen( psz_source );
311 /* free allocated strings */
312 if( psz_source != psz_orig )
318 intf_ErrMsg( "dvdread error: libdvdcss can't open source" );
325 p_dvd = malloc( sizeof(thread_dvd_data_t) );
328 intf_ErrMsg( "dvdread error: out of memory" );
332 p_dvd->p_dvdread = p_dvdread;
333 p_dvd->p_title = NULL;
334 p_dvd->p_vts_file = NULL;
337 p_input->p_access_data = (void *)p_dvd;
339 /* Ifo allocation & initialisation */
340 if( ! ( p_dvd->p_vmg_file = ifoOpen( p_dvd->p_dvdread, 0 ) ) )
342 intf_ErrMsg( "dvdread error: can't open VMG info" );
346 intf_WarnMsg( 2, "dvdread info: VMG opened" );
348 /* Set stream and area data */
349 vlc_mutex_lock( &p_input->stream.stream_lock );
351 p_input->stream.i_method = INPUT_METHOD_DVD;
353 /* If we are here we can control the pace... */
354 p_input->stream.b_pace_control = 1;
355 p_input->stream.b_seekable = 1;
357 p_input->stream.p_selected_area->i_size = 0;
358 p_input->stream.p_selected_area->i_tell = 0;
360 /* Initialize ES structures */
361 input_InitStream( p_input, sizeof( stream_ps_data_t ) );
363 /* disc input method */
364 p_input->stream.i_method = INPUT_METHOD_DVD;
366 #define tt_srpt p_dvd->p_vmg_file->tt_srpt
367 intf_WarnMsg( 2, "dvdread info: number of titles: %d", tt_srpt->nr_of_srpts );
369 #define area p_input->stream.pp_areas
370 /* We start from 1 here since the default area 0
371 * is reserved for video_ts.vob */
372 for( i = 1 ; i <= tt_srpt->nr_of_srpts ; i++ )
374 input_AddArea( p_input );
376 /* Titles are Program Chains */
379 /* Absolute start offset and size
380 * We can only set that with vts ifo, so we do it during the
381 * first call to DVDSetArea */
382 area[i]->i_start = 0;
385 /* Number of chapters */
386 area[i]->i_part_nb = tt_srpt->title[i-1].nr_of_ptts;
389 area[i]->i_plugin_data = tt_srpt->title[i-1].title_set_nr;
393 p_dvd->i_title = i_title <= tt_srpt->nr_of_srpts ? i_title : 1;
396 p_area = p_input->stream.pp_areas[p_dvd->i_title];
397 p_dvd->i_chapter = i_chapter;
399 p_dvd->i_chapter = i_chapter < p_area->i_part_nb ? i_chapter : 1;
400 p_area->i_part = p_dvd->i_chapter;
402 p_dvd->i_angle = i_angle;
404 /* set title, chapter, audio and subpic */
405 if( DvdReadSetArea( p_input, p_area ) )
407 vlc_mutex_unlock( &p_input->stream.stream_lock );
411 vlc_mutex_unlock( &p_input->stream.stream_lock );
413 p_input->psz_demux = "dvdread";
418 /*****************************************************************************
419 * DvdReadClose: close libdvdread
420 *****************************************************************************/
421 static void DvdReadClose( struct input_thread_s *p_input )
423 thread_dvd_data_t * p_dvd;
425 p_dvd = (thread_dvd_data_t *)p_input->p_access_data;
427 /* close libdvdread */
428 DVDCloseFile( p_dvd->p_title );
429 ifoClose( p_dvd->p_vts_file );
430 ifoClose( p_dvd->p_vmg_file );
432 DVDClose( p_dvd->p_dvdread );
434 p_input->p_access_data = NULL;
438 /*****************************************************************************
439 * DvdReadSetProgram: Does nothing, a DVD is mono-program
440 *****************************************************************************/
441 static int DvdReadSetProgram( input_thread_t * p_input,
442 pgrm_descriptor_t * p_program )
444 if( p_input->stream.p_selected_program != p_program )
446 thread_dvd_data_t * p_dvd;
448 p_dvd = (thread_dvd_data_t*)(p_input->p_access_data);
449 p_dvd->i_angle = p_program->i_number;
451 memcpy( p_program, p_input->stream.p_selected_program,
452 sizeof(pgrm_descriptor_t) );
453 p_program->i_number = p_dvd->i_angle;
454 p_input->stream.p_selected_program = p_program;
456 intf_WarnMsg( 3, "dvd info: angle %d selected", p_dvd->i_angle );
462 #define p_pgc p_dvd->p_cur_pgc
464 /*****************************************************************************
465 * DvdReadSetArea: initialize input data for title x, chapter y.
466 * It should be called for each user navigation request.
467 *****************************************************************************
468 * Take care that i_title starts from 0 (vmg) and i_chapter start from 1.
469 * Note that you have to take the lock before entering here.
470 *****************************************************************************/
471 static int DvdReadSetArea( input_thread_t * p_input, input_area_t * p_area )
473 thread_dvd_data_t * p_dvd;
477 p_dvd = (thread_dvd_data_t*)p_input->p_access_data;
479 /* we can't use the interface slider until initilization is complete */
480 p_input->stream.b_seekable = 0;
482 if( p_area != p_input->stream.p_selected_area )
484 es_descriptor_t * p_es;
490 #define p_vmg p_dvd->p_vmg_file
491 #define p_vts p_dvd->p_vts_file
492 if( p_dvd->p_title != NULL )
494 DVDCloseFile( p_dvd->p_title );
502 /* Reset the Chapter position of the old title */
503 p_input->stream.p_selected_area->i_part = 1;
506 * We have to load all title information
508 /* Change the default area */
509 p_input->stream.p_selected_area = p_area;
511 intf_WarnMsg( 12, "dvdread: open VTS %d, for title %d",
512 p_vmg->tt_srpt->title[ p_area->i_id - 1 ].title_set_nr,
516 if( ! ( p_vts = ifoOpen( p_dvd->p_dvdread,
517 p_vmg->tt_srpt->title[ p_area->i_id - 1 ].title_set_nr ) ) )
519 intf_ErrMsg( "dvdread error: fatal error in vts ifo" );
521 DVDClose( p_dvd->p_dvdread );
525 /* title position inside the selected vts */
526 p_dvd->i_ttn = p_vmg->tt_srpt->title[ p_area->i_id - 1 ].vts_ttn;
529 * Set selected title start
531 pgc_id = p_vts->vts_ptt_srpt->title[p_dvd->i_ttn-1].ptt[0].pgcn;
532 pgn = p_vts->vts_ptt_srpt->title[p_dvd->i_ttn-1].ptt[0].pgn;
533 p_pgc = p_vts->vts_pgcit->pgci_srp[ pgc_id - 1 ].pgc;
534 i_cell = p_pgc->program_map[ pgn - 1 ] - 1;
537 LB2OFF( p_dvd->p_cur_pgc->cell_playback[ i_cell ].first_sector );
539 intf_WarnMsg( 3, "dvdread: start %d vts_title %d pgc %d pgn %d",
540 p_area->i_id, p_dvd->i_ttn, pgc_id, pgn );
545 i_cell = p_dvd->p_cur_pgc->nr_of_cells - 1;
547 p_dvd->i_end_block = p_pgc->cell_playback[ i_cell ].last_sector;
548 p_area->i_size = LB2OFF( p_dvd->i_end_block )- p_area->i_start;
550 intf_WarnMsg( 12, "dvdread: start %lld size %lld end %d",
551 p_area->i_start , p_area->i_size, p_dvd->i_end_block );
554 * Set properties for current chapter
556 /* Remeber current chapter */
557 p_dvd->i_chapter = p_area->i_part;
560 pgc_id = p_vts->vts_ptt_srpt->title[
561 p_dvd->i_ttn-1].ptt[p_area->i_part-1].pgcn;
562 pgn = p_vts->vts_ptt_srpt->title[
563 p_dvd->i_ttn-1].ptt[p_area->i_part-1].pgn;
565 p_pgc = p_vts->vts_pgcit->pgci_srp[pgc_id-1].pgc;
566 p_dvd->i_pack_len = 0;
567 p_dvd->i_next_cell = p_dvd->i_cur_cell = p_pgc->program_map[pgn-1] - 1;
568 DvdReadFindCell( p_dvd );
570 p_dvd->i_next_vobu = p_dvd->i_cur_block =
571 p_pgc->cell_playback[p_dvd->i_cur_cell].first_sector;
576 p_dvd->i_angle_nb = p_vmg->tt_srpt->title[p_area->i_id-1].nr_of_angles;
578 if( p_dvd->i_angle > p_dvd->i_angle_nb )
584 * We've got enough info, time to open the title set data.
586 if( ! ( p_dvd->p_title = DVDOpenFile( p_dvd->p_dvdread,
587 p_vmg->tt_srpt->title[ p_area->i_id - 1 ].title_set_nr,
588 DVD_READ_TITLE_VOBS ) ) )
590 intf_ErrMsg( "dvdread error: can't open title (VTS_%02d_1.VOB)",
591 p_vmg->tt_srpt->title[p_area->i_id-1].title_set_nr );
594 DVDClose( p_dvd->p_dvdread );
598 // IfoPrintTitle( p_dvd );
601 * Destroy obsolete ES by reinitializing program 0
602 * and find all ES in title with ifo data
604 if( p_input->stream.pp_programs != NULL )
606 /* We don't use input_EndStream here since
607 * we keep area structures */
609 while( p_input->stream.i_es_number )
611 input_DelES( p_input, p_input->stream.pp_es[0] );
614 while( p_input->stream.i_pgrm_number )
616 input_DelProgram( p_input, p_input->stream.pp_programs[0] );
619 if( p_input->stream.pp_selected_es )
621 free( p_input->stream.pp_selected_es );
622 p_input->stream.pp_selected_es = NULL;
624 p_input->stream.i_selected_es_number = 0;
627 input_AddProgram( p_input, 1, sizeof( stream_ps_data_t ) );
628 p_input->stream.p_selected_program = p_input->stream.pp_programs[0];
630 for( i = 1 ; i < p_dvd->i_angle_nb ; i++ )
632 input_AddProgram( p_input, i+1, 0 );
635 DvdReadSetProgram( p_input,
636 p_input->stream.pp_programs[p_dvd->i_angle-1] );
638 /* No PSM to read in DVD mode, we already have all information */
639 p_input->stream.p_selected_program->b_is_ok = 1;
643 /* ES 0 -> video MPEG2 */
644 // IfoPrintVideo( p_dvd );
646 p_es = input_AddES( p_input, NULL, 0xe0, 0 );
647 p_es->i_stream_id = 0xe0;
648 p_es->i_type = MPEG2_VIDEO_ES;
649 p_es->i_cat = VIDEO_ES;
651 #define audio_control \
652 p_dvd->p_vts_file->vts_pgcit->pgci_srp[pgc_id-1].pgc->audio_control[i-1]
653 /* Audio ES, in the order they appear in .ifo */
654 for( i = 1 ; i <= p_vts->vtsi_mat->nr_of_vts_audio_streams ; i++ )
659 // IfoPrintAudio( p_dvd, i );
661 /* audio channel is active if first byte is 0x80 */
662 if( audio_control & 0x8000 )
665 i_position = ( audio_control & 0x7F00 ) >> 8;
667 intf_WarnMsg( 12, "dvd audio position %d", i_position );
668 switch( p_vts->vtsi_mat->vts_audio_attr[i-1].audio_format )
671 i_id = ( ( 0x80 + i_position ) << 8 ) | 0xbd;
672 p_es = input_AddES( p_input, NULL, i_id, 0 );
673 p_es->i_stream_id = 0xbd;
674 p_es->i_type = AC3_AUDIO_ES;
676 p_es->i_cat = AUDIO_ES;
677 strcpy( p_es->psz_desc, DecodeLanguage( hton16(
678 p_vts->vtsi_mat->vts_audio_attr[i-1].lang_code ) ) );
679 strcat( p_es->psz_desc, " (ac3)" );
683 case 0x03: /* MPEG audio */
684 i_id = 0xc0 + i_position;
685 p_es = input_AddES( p_input, NULL, i_id, 0 );
686 p_es->i_stream_id = i_id;
687 p_es->i_type = MPEG2_AUDIO_ES;
689 p_es->i_cat = AUDIO_ES;
690 strcpy( p_es->psz_desc, DecodeLanguage( hton16(
691 p_vts->vtsi_mat->vts_audio_attr[i-1].lang_code ) ) );
692 strcat( p_es->psz_desc, " (mpeg)" );
695 case 0x04: /* LPCM */
697 i_id = ( ( 0xa0 + i_position ) << 8 ) | 0xbd;
698 p_es = input_AddES( p_input, NULL, i_id, 0 );
699 p_es->i_stream_id = i_id;
700 p_es->i_type = LPCM_AUDIO_ES;
702 p_es->i_cat = AUDIO_ES;
703 strcpy( p_es->psz_desc, DecodeLanguage( hton16(
704 p_vts->vtsi_mat->vts_audio_attr[i-1].lang_code ) ) );
705 strcat( p_es->psz_desc, " (lpcm)" );
709 i_id = ( ( 0x88 + i_position ) << 8 ) | 0xbd;
710 intf_ErrMsg( "dvd warning: DTS audio not handled yet"
715 intf_ErrMsg( "dvd warning: unknown audio type %.2x",
716 p_vts->vtsi_mat->vts_audio_attr[i-1].audio_format );
721 #define spu_control \
722 p_dvd->p_vts_file->vts_pgcit->pgci_srp[pgc_id-1].pgc->subp_control[i-1]
726 for( i = 1 ; i <= p_vts->vtsi_mat->nr_of_vts_subp_streams; i++ )
731 // IfoPrintSpu( p_dvd, i );
732 intf_WarnMsg( 12, "dvd spu %d 0x%02x", i, spu_control );
734 if( spu_control & 0x80000000 )
738 /* there are several streams for one spu */
739 if( p_vts->vtsi_mat->vts_video_attr.display_aspect_ratio )
742 switch( p_vts->vtsi_mat->vts_video_attr.permitted_df )
745 i_position = spu_control & 0xff;
748 i_position = ( spu_control >> 8 ) & 0xff;
751 i_position = ( spu_control >> 16 ) & 0xff;
758 i_position = ( spu_control >> 24 ) & 0x7F;
761 i_id = ( ( 0x20 + i_position ) << 8 ) | 0xbd;
762 p_es = input_AddES( p_input, NULL, i_id, 0 );
763 p_es->i_stream_id = 0xbd;
764 p_es->i_type = DVD_SPU_ES;
765 p_es->i_cat = SPU_ES;
766 strcpy( p_es->psz_desc, DecodeLanguage( hton16(
767 p_vts->vtsi_mat->vts_subp_attr[i-1].lang_code ) ) );
772 /* FIXME: hack to check that the demuxer is ready, and set
774 if( p_input->p_demux_module )
776 DvdReadLauchDecoders( p_input );
782 p_area = p_input->stream.p_selected_area;
789 if( p_area->i_part != p_dvd->i_chapter )
791 if( ( p_area->i_part > 0 ) &&
792 ( p_area->i_part <= p_area->i_part_nb ))
794 p_dvd->i_ttn = p_vmg->tt_srpt->title[p_area->i_id-1].vts_ttn;
795 pgc_id = p_vts->vts_ptt_srpt->title[
796 p_dvd->i_ttn-1].ptt[p_area->i_part-1].pgcn;
797 pgn = p_vts->vts_ptt_srpt->title[
798 p_dvd->i_ttn-1].ptt[p_area->i_part-1].pgn;
800 p_pgc = p_vts->vts_pgcit->pgci_srp[ pgc_id - 1 ].pgc;
802 p_dvd->i_cur_cell = p_pgc->program_map[ pgn - 1 ] - 1;
803 p_dvd->i_chapter = p_area->i_part;
804 DvdReadFindCell( p_dvd );
806 p_dvd->i_pack_len = 0;
807 p_dvd->i_next_vobu = p_dvd->i_cur_block =
808 p_pgc->cell_playback[p_dvd->i_cur_cell].first_sector;
812 p_area->i_part = p_dvd->i_chapter;
818 /* warn interface that something has changed */
819 p_area->i_tell = LB2OFF( p_dvd->i_next_vobu ) - p_area->i_start;
820 p_input->stream.b_seekable = 1;
821 p_input->stream.b_changed = 1;
827 /*****************************************************************************
828 * DvdReadRead: reads data packets into the netlist.
829 *****************************************************************************
830 * Returns -1 in case of error, 0 if everything went well, and 1 in case of
832 *****************************************************************************/
833 static int DvdReadRead( input_thread_t * p_input,
834 byte_t * p_buffer, size_t i_count )
836 thread_dvd_data_t * p_dvd;
844 p_dvd = (thread_dvd_data_t *)p_input->p_access_data;
848 * Playback by cell in this pgc, starting at the cell for our chapter.
850 i_blocks = OFF2LB( i_count );
857 * End of pack, we select the following one
859 if( ! p_dvd->i_pack_len )
864 if( ( i_read = DVDReadBlocks( p_dvd->p_title, p_dvd->i_next_vobu,
867 intf_ErrMsg( "dvdread error: read failed for block %d",
868 p_dvd->i_next_vobu );
872 /* basic check to be sure we don't have a empty title
873 * go to next title if so */
874 //assert( p_buffer[41] == 0xbf && p_buffer[1027] == 0xbf );
877 * Parse the contained dsi packet.
880 DvdReadHandleDSI( p_dvd, p_buf );
883 if( p_dvd->i_next_vobu >= p_dvd->i_end_block + 1 )
888 assert( p_dvd->i_pack_len < 1024 );
889 /* FIXME: Ugly kludge: we send the pack block to the input for it
890 * sometimes has a zero scr and restart the sync */
891 p_dvd->i_cur_block ++;
892 //p_dvd->i_pack_len++;
895 p_buf += DVD_VIDEO_LB_LEN;
900 * Compute the number of blocks to read
902 i_blocks_once = p_dvd->i_pack_len >= i_blocks
903 ? i_blocks : p_dvd->i_pack_len;
904 p_dvd->i_pack_len -= i_blocks_once;
907 i_read = DVDReadBlocks( p_dvd->p_title, p_dvd->i_cur_block,
908 i_blocks_once, p_buf );
909 if( i_read != i_blocks_once )
911 intf_ErrMsg( "dvdread error: read failed for %d/%d blocks at 0x%02x",
912 i_read, i_blocks_once, p_dvd->i_cur_block );
917 i_read_total += i_read;
918 p_dvd->i_cur_block += i_read;
919 p_buf += LB2OFF( i_read );
923 intf_WarnMsg( 12, "dvdread i_blocks: %d len: %d current: 0x%02x", i_read, p_dvd->i_pack_len, p_dvd->i_cur_block );
926 vlc_mutex_lock( &p_input->stream.stream_lock );
928 p_input->stream.p_selected_area->i_tell =
929 LB2OFF( p_dvd->i_cur_block ) -
930 p_input->stream.p_selected_area->i_start;
934 /* We modify i_part only at end of chapter not to erase
935 * some modification from the interface */
936 p_input->stream.p_selected_area->i_part = p_dvd->i_chapter;
940 if( p_input->stream.p_selected_area->i_tell
941 >= p_input->stream.p_selected_area->i_size || b_eot )
943 if( ( p_input->stream.p_selected_area->i_id + 1 ) >=
944 p_input->stream.i_area_nb )
947 vlc_mutex_unlock( &p_input->stream.stream_lock );
952 intf_WarnMsg( 4, "dvd info: new title" );
953 DvdReadSetArea( p_input, p_input->stream.pp_areas[
954 p_input->stream.p_selected_area->i_id+1] );
955 vlc_mutex_unlock( &p_input->stream.stream_lock );
959 vlc_mutex_unlock( &p_input->stream.stream_lock );
961 return LB2OFF( i_read_total );
965 /*****************************************************************************
966 * DvdReadSeek : Goes to a given position on the stream.
967 *****************************************************************************
968 * This one is used by the input and translate chronological position from
969 * input to logical position on the device.
970 * The lock should be taken before calling this function.
971 *****************************************************************************/
972 static void DvdReadSeek( input_thread_t * p_input, off_t i_off )
974 thread_dvd_data_t * p_dvd;
982 vlc_mutex_lock( &p_input->stream.stream_lock );
983 i_off += p_input->stream.p_selected_area->i_start;
984 vlc_mutex_unlock( &p_input->stream.stream_lock );
986 i_lb = OFF2LB( i_off );
987 p_dvd = ( thread_dvd_data_t * )p_input->p_access_data;
990 while( p_dvd->p_cur_pgc->cell_playback[i_cell].last_sector < i_lb )
1002 pgc_id = p_dvd->p_vts_file->vts_ptt_srpt->title[
1003 p_dvd->i_ttn-1].ptt[i_chapter-1].pgcn;
1004 pgn = p_dvd->p_vts_file->vts_ptt_srpt->title[
1005 p_dvd->i_ttn-1].ptt[i_chapter-1].pgn;
1007 p_pgc = p_dvd->p_vts_file->vts_pgcit->pgci_srp[pgc_id-1].pgc;
1008 i_tmp = p_pgc->program_map[pgn-1];
1010 } while( i_tmp <= i_cell );
1013 while( p_dvd->p_vts_file->vts_vobu_admap->vobu_start_sectors[i_vobu]
1020 while( p_dvd->p_vts_file->vts_c_adt->cell_adr_table[i_sub_cell].start_sector <
1021 p_dvd->p_vts_file->vts_vobu_admap->vobu_start_sectors[i_vobu-1] )
1027 intf_WarnMsg(12, "cell %d i_sub_cell %d chapter %d vobu %d cell_sector %d vobu_sector %d sub_cell_sector %d",
1028 i_cell, i_sub_cell,i_chapter, i_vobu,
1029 p_dvd->p_cur_pgc->cell_playback[i_cell].first_sector,
1030 p_dvd->p_vts_file->vts_vobu_admap->vobu_start_sectors[i_vobu],
1031 p_dvd->p_vts_file->vts_c_adt->cell_adr_table[i_sub_cell-1].start_sector);
1033 p_dvd->i_cur_block = i_lb;
1034 p_dvd->i_next_vobu =
1035 p_dvd->p_vts_file->vts_vobu_admap->vobu_start_sectors[i_vobu];
1036 p_dvd->i_pack_len = p_dvd->i_next_vobu - i_lb;
1037 p_dvd->i_cur_cell = i_cell;
1038 p_dvd->i_chapter = i_chapter;
1039 DvdReadFindCell( p_dvd );
1041 vlc_mutex_lock( &p_input->stream.stream_lock );
1042 p_input->stream.p_selected_area->i_tell =
1043 LB2OFF ( p_dvd->i_cur_block )
1044 - p_input->stream.p_selected_area->i_start;
1045 p_input->stream.p_selected_area->i_part = p_dvd->i_chapter;
1046 vlc_mutex_unlock( &p_input->stream.stream_lock );
1051 /*****************************************************************************
1053 *****************************************************************************/
1054 static void DvdReadHandleDSI( thread_dvd_data_t * p_dvd, u8 * p_data )
1056 navRead_DSI( &(p_dvd->dsi_pack), &(p_data[ DSI_START_BYTE ]) );
1059 * Determine where we go next. These values are the ones we mostly
1062 p_dvd->i_cur_block = p_dvd->dsi_pack.dsi_gi.nv_pck_lbn;
1065 * If we're not at the end of this cell, we can determine the next
1066 * VOBU to display using the VOBU_SRI information section of the
1067 * DSI. Using this value correctly follows the current angle,
1068 * avoiding the doubled scenes in The Matrix, and makes our life
1071 if( p_dvd->dsi_pack.vobu_sri.next_vobu != SRI_END_OF_CELL )
1074 switch( ( p_dvd->dsi_pack.sml_pbi.category & 0xf000 ) >> 12 )
1077 /* interleaved unit with no angle */
1078 if( p_dvd->dsi_pack.sml_pbi.ilvu_sa != -1 )
1080 p_dvd->i_next_vobu = p_dvd->i_cur_block +
1081 p_dvd->dsi_pack.sml_pbi.ilvu_sa;
1082 p_dvd->i_pack_len = p_dvd->dsi_pack.sml_pbi.ilvu_ea;
1086 p_dvd->i_next_vobu = p_dvd->i_cur_block +
1087 p_dvd->dsi_pack.dsi_gi.vobu_ea + 1;
1088 p_dvd->i_pack_len = p_dvd->dsi_pack.dsi_gi.vobu_ea;
1092 /* vobu is end of ilvu */
1093 if( p_dvd->dsi_pack.sml_agli.data[p_dvd->i_angle-1].address )
1095 p_dvd->i_next_vobu = p_dvd->i_cur_block +
1096 p_dvd->dsi_pack.sml_agli.data[p_dvd->i_angle-1].address;
1097 p_dvd->i_pack_len = p_dvd->dsi_pack.sml_pbi.ilvu_ea;
1102 /* vobu is beginning of ilvu */
1106 /* entering interleaved section */
1108 /* non interleaved cells in interleaved section */
1110 p_dvd->i_next_vobu = p_dvd->i_cur_block +
1111 ( p_dvd->dsi_pack.vobu_sri.next_vobu & 0x7fffffff );
1112 p_dvd->i_pack_len = p_dvd->dsi_pack.dsi_gi.vobu_ea;
1116 p_dvd->i_next_vobu = p_dvd->i_cur_block +
1117 ( p_dvd->dsi_pack.vobu_sri.next_vobu & 0x7fffffff );
1118 p_dvd->i_pack_len = p_dvd->dsi_pack.dsi_gi.vobu_ea;
1123 p_dvd->i_cur_cell = p_dvd->i_next_cell;
1124 DvdReadFindCell( p_dvd );
1126 p_dvd->i_pack_len = p_dvd->dsi_pack.dsi_gi.vobu_ea;
1127 p_dvd->i_next_vobu =
1128 p_dvd->p_cur_pgc->cell_playback[p_dvd->i_cur_cell].first_sector;
1132 intf_WarnMsg( 12, "scr %d lbn 0x%02x vobu_ea %d vob_id %d c_id %d",
1133 p_dvd->dsi_pack.dsi_gi.nv_pck_scr,
1134 p_dvd->dsi_pack.dsi_gi.nv_pck_lbn,
1135 p_dvd->dsi_pack.dsi_gi.vobu_ea,
1136 p_dvd->dsi_pack.dsi_gi.vobu_vob_idn,
1137 p_dvd->dsi_pack.dsi_gi.vobu_c_idn );
1139 intf_WarnMsg( 12, "cat 0x%02x ilvu_ea %d ilvu_sa %d size %d",
1140 p_dvd->dsi_pack.sml_pbi.category,
1141 p_dvd->dsi_pack.sml_pbi.ilvu_ea,
1142 p_dvd->dsi_pack.sml_pbi.ilvu_sa,
1143 p_dvd->dsi_pack.sml_pbi.size );
1145 intf_WarnMsg( 12, "next_vobu %d next_ilvu1 %d next_ilvu2 %d",
1146 p_dvd->dsi_pack.vobu_sri.next_vobu & 0x7fffffff,
1147 p_dvd->dsi_pack.sml_agli.data[ p_dvd->i_angle - 1 ].address,
1148 p_dvd->dsi_pack.sml_agli.data[ p_dvd->i_angle ].address);
1152 /*****************************************************************************
1154 *****************************************************************************/
1155 static void DvdReadFindCell( thread_dvd_data_t * p_dvd )
1160 #define cell p_dvd->p_cur_pgc->cell_playback
1161 if( cell[p_dvd->i_cur_cell].block_type == BLOCK_TYPE_ANGLE_BLOCK )
1164 p_dvd->i_next_cell = p_dvd->i_cur_cell + p_dvd->i_angle_nb;
1165 p_dvd->i_cur_cell += p_dvd->i_angle - 1;
1167 p_dvd->i_cur_cell += p_dvd->i_angle - 1;
1169 while( cell[p_dvd->i_cur_cell+i].block_mode != BLOCK_MODE_LAST_CELL )
1173 p_dvd->i_next_cell = p_dvd->i_cur_cell + i + 1;
1178 p_dvd->i_next_cell = p_dvd->i_cur_cell + 1;
1181 pgc_id = p_dvd->p_vts_file->vts_ptt_srpt->title[
1182 p_dvd->i_ttn-1].ptt[p_dvd->i_chapter-1].pgcn;
1183 pgn = p_dvd->p_vts_file->vts_ptt_srpt->title[
1184 p_dvd->i_ttn-1].ptt[p_dvd->i_chapter-1].pgn;
1185 p_pgc = p_dvd->p_vts_file->vts_pgcit->pgci_srp[pgc_id-1].pgc;
1187 if( p_pgc->program_map[pgn-1] <= p_dvd->i_cur_cell )
1194 /*****************************************************************************
1195 * DvdReadLaunchDecoders
1196 *****************************************************************************/
1197 static void DvdReadLauchDecoders( input_thread_t * p_input )
1199 thread_dvd_data_t * p_dvd;
1201 p_dvd = (thread_dvd_data_t*)(p_input->p_access_data);
1203 if( p_main->b_video )
1205 input_SelectES( p_input, p_input->stream.pp_es[0] );
1208 if( p_main->b_audio )
1210 /* For audio: first one if none or a not existing one specified */
1211 int i_audio = config_GetIntVariable( "input_channel" );
1212 if( i_audio < 0 /*|| i_audio > i_audio_nb*/ )
1214 config_PutIntVariable( "input_channel", 1 );
1217 if( i_audio > 0/* && i_audio_nb > 0*/ )
1219 if( config_GetIntVariable( "input_audio" ) == REQUESTED_AC3 )
1221 int i_ac3 = i_audio;
1222 while( ( p_input->stream.pp_es[i_ac3]->i_type !=
1223 AC3_AUDIO_ES ) && ( i_ac3 <=
1224 p_dvd->p_vts_file->vtsi_mat->nr_of_vts_audio_streams ) )
1228 if( p_input->stream.pp_es[i_ac3]->i_type == AC3_AUDIO_ES )
1230 input_SelectES( p_input,
1231 p_input->stream.pp_es[i_ac3] );
1236 input_SelectES( p_input,
1237 p_input->stream.pp_es[i_audio] );
1242 if( p_main->b_video )
1244 /* for spu, default is none */
1245 int i_spu = config_GetIntVariable( "input_subtitle" );
1246 if( i_spu < 0 /*|| i_spu > i_spu_nb*/ )
1248 config_PutIntVariable( "input_subtitle", 0 );
1251 if( i_spu > 0 /*&& i_spu_nb > 0*/ )
1253 i_spu += p_dvd->p_vts_file->vtsi_mat->nr_of_vts_audio_streams;
1254 input_SelectES( p_input, p_input->stream.pp_es[i_spu] );