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.38 2002/05/14 19:33:54 bozo 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 <dvdread/dvd_reader.h>
65 #include <dvdread/ifo_types.h>
66 #include <dvdread/ifo_read.h>
67 #include <dvdread/nav_read.h>
68 #include <dvdread/nav_print.h>
70 #include "stream_control.h"
71 #include "input_ext-intf.h"
72 #include "input_ext-dec.h"
73 #include "input_ext-plugins.h"
75 #include "input_dvdread.h"
79 /* how many blocks DVDRead will read in each loop */
80 #define DVD_BLOCK_READ_ONCE 64
82 /*****************************************************************************
84 *****************************************************************************/
85 /* called from outside */
86 static int DvdReadInit ( struct input_thread_s * );
87 static void DvdReadEnd ( struct input_thread_s * );
88 static int DvdReadDemux ( struct input_thread_s * );
89 static int DvdReadRewind ( struct input_thread_s * );
91 static int DvdReadOpen ( struct input_thread_s * );
92 static void DvdReadClose ( struct input_thread_s * );
93 static int DvdReadSetArea ( struct input_thread_s *, struct input_area_s * );
94 static int DvdReadSetProgram( struct input_thread_s *, pgrm_descriptor_t * );
95 static int DvdReadRead ( struct input_thread_s *, byte_t *, size_t );
96 static void DvdReadSeek ( struct input_thread_s *, off_t );
98 /* called only from here */
99 static void DvdReadLauchDecoders( input_thread_t * p_input );
100 static void DvdReadHandleDSI( thread_dvd_data_t * p_dvd, u8 * p_data );
101 static void DvdReadFindCell ( thread_dvd_data_t * p_dvd );
103 /*****************************************************************************
104 * Functions exported as capabilities. They are declared as static so that
105 * we don't pollute the namespace too much.
106 *****************************************************************************/
107 void _M( access_getfunctions )( function_list_t * p_function_list )
109 #define access p_function_list->functions.access
110 access.pf_open = DvdReadOpen;
111 access.pf_close = DvdReadClose;
112 access.pf_read = DvdReadRead;
113 access.pf_set_area = DvdReadSetArea;
114 access.pf_set_program = DvdReadSetProgram;
115 access.pf_seek = DvdReadSeek;
119 void _M( demux_getfunctions )( function_list_t * p_function_list )
121 #define demux p_function_list->functions.demux
122 demux.pf_init = DvdReadInit;
123 demux.pf_end = DvdReadEnd;
124 demux.pf_demux = DvdReadDemux;
125 demux.pf_rewind = DvdReadRewind;
130 * Data demux functions
133 /*****************************************************************************
134 * DvdReadInit: initializes DVD structures
135 *****************************************************************************/
136 static int DvdReadInit( input_thread_t * p_input )
138 if( p_input->stream.i_method != INPUT_METHOD_DVD )
143 vlc_mutex_lock( &p_input->stream.stream_lock );
145 DvdReadLauchDecoders( p_input );
147 vlc_mutex_unlock( &p_input->stream.stream_lock );
152 /*****************************************************************************
153 * DvdReadEnd: frees unused data
154 *****************************************************************************/
155 static void DvdReadEnd( input_thread_t * p_input )
159 /*****************************************************************************
161 *****************************************************************************/
162 #define PEEK( SIZE ) \
163 i_result = input_Peek( p_input, &p_peek, SIZE ); \
164 if( i_result == -1 ) \
168 else if( i_result < SIZE ) \
174 static int DvdReadDemux( input_thread_t * p_input )
178 data_packet_t * p_data;
183 /* Read headers to compute payload length */
184 for( i = 0 ; i < DVD_BLOCK_READ_ONCE ; i++ )
187 /* Read what we believe to be a packet header. */
191 if( U32_AT( p_peek ) != 0x1BA )
193 /* That's the case for all packets, except pack header. */
194 i_packet_size = U16_AT( p_peek + 4 );
198 /* MPEG-2 Pack header. */
202 /* Fetch a packet of the appropriate size. */
203 i_result = input_SplitBuffer( p_input, &p_data, i_packet_size + 6 );
209 /* In MPEG-2 pack headers we still have to read stuffing bytes. */
210 if( (p_data->p_demux_start[3] == 0xBA) && (i_packet_size == 8) )
212 size_t i_stuffing = (p_data->p_demux_start[13] & 0x7);
213 /* Force refill of the input buffer - though we don't care
214 * about p_peek. Please note that this is unoptimized. */
216 p_input->p_current_data += i_stuffing;
219 input_DemuxPS( p_input, p_data );
226 /*****************************************************************************
227 * DVDRewind : reads a stream backward
228 *****************************************************************************/
229 static int DvdReadRewind( input_thread_t * p_input )
235 * Data access functions
238 /*****************************************************************************
239 * DvdReadOpen: open libdvdread
240 *****************************************************************************/
241 static int DvdReadOpen( struct input_thread_s *p_input )
247 struct stat stat_info;
248 thread_dvd_data_t * p_dvd;
249 dvd_reader_t * p_dvdread;
250 input_area_t * p_area;
256 psz_orig = psz_parser = psz_source = strdup( p_input->psz_name );
262 while( *psz_parser && *psz_parser != '@' )
267 if( *psz_parser == '@' )
273 i_title = (int)strtol( psz_parser, &psz_next, 10 );
276 psz_parser = psz_next + 1;
277 i_chapter = (int)strtol( psz_parser, &psz_next, 10 );
280 i_angle = (int)strtol( psz_next + 1, NULL, 10 );
284 i_title = i_title ? i_title : 1;
285 i_chapter = i_chapter ? i_chapter : 1;
286 i_angle = i_angle ? i_angle : 1;
291 if( !p_input->psz_access )
296 psz_source = config_GetPszVariable( "dvd" );
299 if( stat( psz_source, &stat_info ) == -1 )
301 intf_ErrMsg( "input error: cannot stat() source `%s' (%s)",
302 psz_source, strerror(errno));
305 if( !S_ISBLK(stat_info.st_mode) &&
306 !S_ISCHR(stat_info.st_mode) &&
307 !S_ISDIR(stat_info.st_mode) )
309 intf_WarnMsg( 3, "input : DvdRead plugin discarded"
310 " (not a valid source)" );
314 intf_WarnMsg( 2, "input: dvdroot=%s title=%d chapter=%d angle=%d",
315 psz_source, i_title, i_chapter, i_angle );
318 p_dvdread = DVDOpen( psz_source );
320 /* free allocated strings */
321 if( psz_source != psz_orig )
327 intf_ErrMsg( "dvdread error: libdvdcss can't open source" );
334 p_dvd = malloc( sizeof(thread_dvd_data_t) );
337 intf_ErrMsg( "dvdread error: out of memory" );
341 p_dvd->p_dvdread = p_dvdread;
342 p_dvd->p_title = NULL;
343 p_dvd->p_vts_file = NULL;
346 p_input->p_access_data = (void *)p_dvd;
348 /* Ifo allocation & initialisation */
349 if( ! ( p_dvd->p_vmg_file = ifoOpen( p_dvd->p_dvdread, 0 ) ) )
351 intf_ErrMsg( "dvdread error: can't open VMG info" );
355 intf_WarnMsg( 2, "dvdread info: VMG opened" );
357 /* Set stream and area data */
358 vlc_mutex_lock( &p_input->stream.stream_lock );
360 p_input->stream.i_method = INPUT_METHOD_DVD;
362 /* If we are here we can control the pace... */
363 p_input->stream.b_pace_control = 1;
364 p_input->stream.b_seekable = 1;
366 p_input->stream.p_selected_area->i_size = 0;
367 p_input->stream.p_selected_area->i_tell = 0;
369 /* Initialize ES structures */
370 input_InitStream( p_input, sizeof( stream_ps_data_t ) );
372 /* disc input method */
373 p_input->stream.i_method = INPUT_METHOD_DVD;
375 #define tt_srpt p_dvd->p_vmg_file->tt_srpt
376 intf_WarnMsg( 2, "dvdread info: number of titles: %d", tt_srpt->nr_of_srpts );
378 #define area p_input->stream.pp_areas
379 /* We start from 1 here since the default area 0
380 * is reserved for video_ts.vob */
381 for( i = 1 ; i <= tt_srpt->nr_of_srpts ; i++ )
383 input_AddArea( p_input );
385 /* Titles are Program Chains */
388 /* Absolute start offset and size
389 * We can only set that with vts ifo, so we do it during the
390 * first call to DVDSetArea */
391 area[i]->i_start = 0;
394 /* Number of chapters */
395 area[i]->i_part_nb = tt_srpt->title[i-1].nr_of_ptts;
398 area[i]->i_plugin_data = tt_srpt->title[i-1].title_set_nr;
402 p_dvd->i_title = i_title <= tt_srpt->nr_of_srpts ? i_title : 1;
405 p_area = p_input->stream.pp_areas[p_dvd->i_title];
406 p_dvd->i_chapter = i_chapter;
408 p_dvd->i_chapter = i_chapter < p_area->i_part_nb ? i_chapter : 1;
409 p_area->i_part = p_dvd->i_chapter;
411 p_dvd->i_angle = i_angle;
413 /* set title, chapter, audio and subpic */
414 if( DvdReadSetArea( p_input, p_area ) )
416 vlc_mutex_unlock( &p_input->stream.stream_lock );
420 vlc_mutex_unlock( &p_input->stream.stream_lock );
422 p_input->psz_demux = "dvdread";
427 /*****************************************************************************
428 * DvdReadClose: close libdvdread
429 *****************************************************************************/
430 static void DvdReadClose( struct input_thread_s *p_input )
432 thread_dvd_data_t * p_dvd;
434 p_dvd = (thread_dvd_data_t *)p_input->p_access_data;
436 /* close libdvdread */
437 DVDCloseFile( p_dvd->p_title );
438 ifoClose( p_dvd->p_vts_file );
439 ifoClose( p_dvd->p_vmg_file );
441 DVDClose( p_dvd->p_dvdread );
443 p_input->p_access_data = NULL;
447 /*****************************************************************************
448 * DvdReadSetProgram: Does nothing, a DVD is mono-program
449 *****************************************************************************/
450 static int DvdReadSetProgram( input_thread_t * p_input,
451 pgrm_descriptor_t * p_program )
453 if( p_input->stream.p_selected_program != p_program )
455 thread_dvd_data_t * p_dvd;
457 p_dvd = (thread_dvd_data_t*)(p_input->p_access_data);
458 p_dvd->i_angle = p_program->i_number;
460 memcpy( p_program, p_input->stream.p_selected_program,
461 sizeof(pgrm_descriptor_t) );
462 p_program->i_number = p_dvd->i_angle;
463 p_input->stream.p_selected_program = p_program;
465 intf_WarnMsg( 3, "dvd info: angle %d selected", p_dvd->i_angle );
471 #define p_pgc p_dvd->p_cur_pgc
473 /*****************************************************************************
474 * DvdReadSetArea: initialize input data for title x, chapter y.
475 * It should be called for each user navigation request.
476 *****************************************************************************
477 * Take care that i_title starts from 0 (vmg) and i_chapter start from 1.
478 * Note that you have to take the lock before entering here.
479 *****************************************************************************/
480 static int DvdReadSetArea( input_thread_t * p_input, input_area_t * p_area )
482 thread_dvd_data_t * p_dvd;
486 p_dvd = (thread_dvd_data_t*)p_input->p_access_data;
488 /* we can't use the interface slider until initilization is complete */
489 p_input->stream.b_seekable = 0;
491 if( p_area != p_input->stream.p_selected_area )
493 es_descriptor_t * p_es;
499 #define p_vmg p_dvd->p_vmg_file
500 #define p_vts p_dvd->p_vts_file
501 if( p_dvd->p_title != NULL )
503 DVDCloseFile( p_dvd->p_title );
511 /* Reset the Chapter position of the old title */
512 p_input->stream.p_selected_area->i_part = 1;
515 * We have to load all title information
517 /* Change the default area */
518 p_input->stream.p_selected_area = p_area;
520 intf_WarnMsg( 12, "dvdread: open VTS %d, for title %d",
521 p_vmg->tt_srpt->title[ p_area->i_id - 1 ].title_set_nr,
525 if( ! ( p_vts = ifoOpen( p_dvd->p_dvdread,
526 p_vmg->tt_srpt->title[ p_area->i_id - 1 ].title_set_nr ) ) )
528 intf_ErrMsg( "dvdread error: fatal error in vts ifo" );
530 DVDClose( p_dvd->p_dvdread );
534 /* title position inside the selected vts */
535 p_dvd->i_ttn = p_vmg->tt_srpt->title[ p_area->i_id - 1 ].vts_ttn;
538 * Set selected title start
540 pgc_id = p_vts->vts_ptt_srpt->title[p_dvd->i_ttn-1].ptt[0].pgcn;
541 pgn = p_vts->vts_ptt_srpt->title[p_dvd->i_ttn-1].ptt[0].pgn;
542 p_pgc = p_vts->vts_pgcit->pgci_srp[ pgc_id - 1 ].pgc;
543 i_cell = p_pgc->program_map[ pgn - 1 ] - 1;
546 LB2OFF( p_dvd->p_cur_pgc->cell_playback[ i_cell ].first_sector );
548 intf_WarnMsg( 3, "dvdread: start %d vts_title %d pgc %d pgn %d",
549 p_area->i_id, p_dvd->i_ttn, pgc_id, pgn );
554 i_cell = p_dvd->p_cur_pgc->nr_of_cells - 1;
556 p_dvd->i_end_block = p_pgc->cell_playback[ i_cell ].last_sector;
557 p_area->i_size = LB2OFF( p_dvd->i_end_block )- p_area->i_start;
559 intf_WarnMsg( 12, "dvdread: start %lld size %lld end %d",
560 p_area->i_start , p_area->i_size, p_dvd->i_end_block );
563 * Set properties for current chapter
565 /* Remeber current chapter */
566 p_dvd->i_chapter = p_area->i_part;
569 pgc_id = p_vts->vts_ptt_srpt->title[
570 p_dvd->i_ttn-1].ptt[p_area->i_part-1].pgcn;
571 pgn = p_vts->vts_ptt_srpt->title[
572 p_dvd->i_ttn-1].ptt[p_area->i_part-1].pgn;
574 p_pgc = p_vts->vts_pgcit->pgci_srp[pgc_id-1].pgc;
575 p_dvd->i_pack_len = 0;
576 p_dvd->i_next_cell = p_dvd->i_cur_cell = p_pgc->program_map[pgn-1] - 1;
577 DvdReadFindCell( p_dvd );
579 p_dvd->i_next_vobu = p_dvd->i_cur_block =
580 p_pgc->cell_playback[p_dvd->i_cur_cell].first_sector;
585 p_dvd->i_angle_nb = p_vmg->tt_srpt->title[p_area->i_id-1].nr_of_angles;
587 if( p_dvd->i_angle > p_dvd->i_angle_nb )
593 * We've got enough info, time to open the title set data.
595 if( ! ( p_dvd->p_title = DVDOpenFile( p_dvd->p_dvdread,
596 p_vmg->tt_srpt->title[ p_area->i_id - 1 ].title_set_nr,
597 DVD_READ_TITLE_VOBS ) ) )
599 intf_ErrMsg( "dvdread error: can't open title (VTS_%02d_1.VOB)",
600 p_vmg->tt_srpt->title[p_area->i_id-1].title_set_nr );
603 DVDClose( p_dvd->p_dvdread );
607 // IfoPrintTitle( p_dvd );
610 * Destroy obsolete ES by reinitializing program 0
611 * and find all ES in title with ifo data
613 if( p_input->stream.pp_programs != NULL )
615 /* We don't use input_EndStream here since
616 * we keep area structures */
618 while( p_input->stream.i_es_number )
620 input_DelES( p_input, p_input->stream.pp_es[0] );
623 while( p_input->stream.i_pgrm_number )
625 input_DelProgram( p_input, p_input->stream.pp_programs[0] );
628 if( p_input->stream.pp_selected_es )
630 free( p_input->stream.pp_selected_es );
631 p_input->stream.pp_selected_es = NULL;
633 p_input->stream.i_selected_es_number = 0;
636 input_AddProgram( p_input, 1, sizeof( stream_ps_data_t ) );
637 p_input->stream.p_selected_program = p_input->stream.pp_programs[0];
639 for( i = 1 ; i < p_dvd->i_angle_nb ; i++ )
641 input_AddProgram( p_input, i+1, 0 );
644 DvdReadSetProgram( p_input,
645 p_input->stream.pp_programs[p_dvd->i_angle-1] );
647 /* No PSM to read in DVD mode, we already have all information */
648 p_input->stream.p_selected_program->b_is_ok = 1;
652 /* ES 0 -> video MPEG2 */
653 // IfoPrintVideo( p_dvd );
655 p_es = input_AddES( p_input, NULL, 0xe0, 0 );
656 p_es->i_stream_id = 0xe0;
657 p_es->i_type = MPEG2_VIDEO_ES;
658 p_es->i_cat = VIDEO_ES;
660 #define audio_control \
661 p_dvd->p_vts_file->vts_pgcit->pgci_srp[pgc_id-1].pgc->audio_control[i-1]
662 /* Audio ES, in the order they appear in .ifo */
663 for( i = 1 ; i <= p_vts->vtsi_mat->nr_of_vts_audio_streams ; i++ )
668 // IfoPrintAudio( p_dvd, i );
670 /* audio channel is active if first byte is 0x80 */
671 if( audio_control & 0x8000 )
674 i_position = ( audio_control & 0x7F00 ) >> 8;
676 intf_WarnMsg( 12, "dvd audio position %d", i_position );
677 switch( p_vts->vtsi_mat->vts_audio_attr[i-1].audio_format )
680 i_id = ( ( 0x80 + i_position ) << 8 ) | 0xbd;
681 p_es = input_AddES( p_input, NULL, i_id, 0 );
682 p_es->i_stream_id = 0xbd;
683 p_es->i_type = AC3_AUDIO_ES;
685 p_es->i_cat = AUDIO_ES;
686 strcpy( p_es->psz_desc, DecodeLanguage(
687 p_vts->vtsi_mat->vts_audio_attr[i-1].lang_code ) );
688 strcat( p_es->psz_desc, " (ac3)" );
692 case 0x03: /* MPEG audio */
693 i_id = 0xc0 + i_position;
694 p_es = input_AddES( p_input, NULL, i_id, 0 );
695 p_es->i_stream_id = i_id;
696 p_es->i_type = MPEG2_AUDIO_ES;
698 p_es->i_cat = AUDIO_ES;
699 strcpy( p_es->psz_desc, DecodeLanguage(
700 p_vts->vtsi_mat->vts_audio_attr[i-1].lang_code ) );
701 strcat( p_es->psz_desc, " (mpeg)" );
704 case 0x04: /* LPCM */
706 i_id = ( ( 0xa0 + i_position ) << 8 ) | 0xbd;
707 p_es = input_AddES( p_input, NULL, i_id, 0 );
708 p_es->i_stream_id = i_id;
709 p_es->i_type = LPCM_AUDIO_ES;
711 p_es->i_cat = AUDIO_ES;
712 strcpy( p_es->psz_desc, DecodeLanguage(
713 p_vts->vtsi_mat->vts_audio_attr[i-1].lang_code ) );
714 strcat( p_es->psz_desc, " (lpcm)" );
718 i_id = ( ( 0x88 + i_position ) << 8 ) | 0xbd;
719 intf_ErrMsg( "dvd warning: DTS audio not handled yet"
724 intf_ErrMsg( "dvd warning: unknown audio type %.2x",
725 p_vts->vtsi_mat->vts_audio_attr[i-1].audio_format );
730 #define spu_control \
731 p_dvd->p_vts_file->vts_pgcit->pgci_srp[pgc_id-1].pgc->subp_control[i-1]
735 for( i = 1 ; i <= p_vts->vtsi_mat->nr_of_vts_subp_streams; i++ )
740 // IfoPrintSpu( p_dvd, i );
741 intf_WarnMsg( 12, "dvd spu %d 0x%02x", i, spu_control );
743 if( spu_control & 0x80000000 )
747 /* there are several streams for one spu */
748 if( p_vts->vtsi_mat->vts_video_attr.display_aspect_ratio )
751 switch( p_vts->vtsi_mat->vts_video_attr.permitted_df )
754 i_position = spu_control & 0xff;
757 i_position = ( spu_control >> 8 ) & 0xff;
760 i_position = ( spu_control >> 16 ) & 0xff;
767 i_position = ( spu_control >> 24 ) & 0x7F;
770 i_id = ( ( 0x20 + i_position ) << 8 ) | 0xbd;
771 p_es = input_AddES( p_input, NULL, i_id, 0 );
772 p_es->i_stream_id = 0xbd;
773 p_es->i_type = DVD_SPU_ES;
774 p_es->i_cat = SPU_ES;
775 strcpy( p_es->psz_desc, DecodeLanguage(
776 p_vts->vtsi_mat->vts_subp_attr[i-1].lang_code ) );
781 /* FIXME: hack to check that the demuxer is ready, and set
783 if( p_input->p_demux_module )
785 DvdReadLauchDecoders( p_input );
791 p_area = p_input->stream.p_selected_area;
798 if( p_area->i_part != p_dvd->i_chapter )
800 if( ( p_area->i_part > 0 ) &&
801 ( p_area->i_part <= p_area->i_part_nb ))
803 p_dvd->i_ttn = p_vmg->tt_srpt->title[p_area->i_id-1].vts_ttn;
804 pgc_id = p_vts->vts_ptt_srpt->title[
805 p_dvd->i_ttn-1].ptt[p_area->i_part-1].pgcn;
806 pgn = p_vts->vts_ptt_srpt->title[
807 p_dvd->i_ttn-1].ptt[p_area->i_part-1].pgn;
809 p_pgc = p_vts->vts_pgcit->pgci_srp[ pgc_id - 1 ].pgc;
811 p_dvd->i_cur_cell = p_pgc->program_map[ pgn - 1 ] - 1;
812 p_dvd->i_chapter = p_area->i_part;
813 DvdReadFindCell( p_dvd );
815 p_dvd->i_pack_len = 0;
816 p_dvd->i_next_vobu = p_dvd->i_cur_block =
817 p_pgc->cell_playback[p_dvd->i_cur_cell].first_sector;
821 p_area->i_part = p_dvd->i_chapter;
827 /* warn interface that something has changed */
828 p_area->i_tell = LB2OFF( p_dvd->i_next_vobu ) - p_area->i_start;
829 p_input->stream.b_seekable = 1;
830 p_input->stream.b_changed = 1;
836 /*****************************************************************************
837 * DvdReadRead: reads data packets into the netlist.
838 *****************************************************************************
839 * Returns -1 in case of error, 0 if everything went well, and 1 in case of
841 *****************************************************************************/
842 static int DvdReadRead( input_thread_t * p_input,
843 byte_t * p_buffer, size_t i_count )
845 thread_dvd_data_t * p_dvd;
853 p_dvd = (thread_dvd_data_t *)p_input->p_access_data;
857 * Playback by cell in this pgc, starting at the cell for our chapter.
859 i_blocks = OFF2LB( i_count );
866 * End of pack, we select the following one
868 if( ! p_dvd->i_pack_len )
873 if( ( i_read = DVDReadBlocks( p_dvd->p_title, p_dvd->i_next_vobu,
876 intf_ErrMsg( "dvdread error: read failed for block %d",
877 p_dvd->i_next_vobu );
881 /* basic check to be sure we don't have a empty title
882 * go to next title if so */
883 //assert( p_buffer[41] == 0xbf && p_buffer[1027] == 0xbf );
886 * Parse the contained dsi packet.
889 DvdReadHandleDSI( p_dvd, p_buf );
892 if( p_dvd->i_next_vobu >= p_dvd->i_end_block + 1 )
897 assert( p_dvd->i_pack_len < 1024 );
898 /* FIXME: Ugly kludge: we send the pack block to the input for it
899 * sometimes has a zero scr and restart the sync */
900 p_dvd->i_cur_block ++;
901 //p_dvd->i_pack_len++;
904 p_buf += DVD_VIDEO_LB_LEN;
909 * Compute the number of blocks to read
911 i_blocks_once = p_dvd->i_pack_len >= i_blocks
912 ? i_blocks : p_dvd->i_pack_len;
913 p_dvd->i_pack_len -= i_blocks_once;
916 i_read = DVDReadBlocks( p_dvd->p_title, p_dvd->i_cur_block,
917 i_blocks_once, p_buf );
918 if( i_read != i_blocks_once )
920 intf_ErrMsg( "dvdread error: read failed for %d/%d blocks at 0x%02x",
921 i_read, i_blocks_once, p_dvd->i_cur_block );
926 i_read_total += i_read;
927 p_dvd->i_cur_block += i_read;
928 p_buf += LB2OFF( i_read );
932 intf_WarnMsg( 12, "dvdread i_blocks: %d len: %d current: 0x%02x", i_read, p_dvd->i_pack_len, p_dvd->i_cur_block );
935 vlc_mutex_lock( &p_input->stream.stream_lock );
937 p_input->stream.p_selected_area->i_tell =
938 LB2OFF( p_dvd->i_cur_block ) -
939 p_input->stream.p_selected_area->i_start;
943 /* We modify i_part only at end of chapter not to erase
944 * some modification from the interface */
945 p_input->stream.p_selected_area->i_part = p_dvd->i_chapter;
949 if( p_input->stream.p_selected_area->i_tell
950 >= p_input->stream.p_selected_area->i_size || b_eot )
952 if( ( p_input->stream.p_selected_area->i_id + 1 ) >=
953 p_input->stream.i_area_nb )
956 vlc_mutex_unlock( &p_input->stream.stream_lock );
961 intf_WarnMsg( 4, "dvd info: new title" );
962 DvdReadSetArea( p_input, p_input->stream.pp_areas[
963 p_input->stream.p_selected_area->i_id+1] );
964 vlc_mutex_unlock( &p_input->stream.stream_lock );
968 vlc_mutex_unlock( &p_input->stream.stream_lock );
970 return LB2OFF( i_read_total );
974 /*****************************************************************************
975 * DvdReadSeek : Goes to a given position on the stream.
976 *****************************************************************************
977 * This one is used by the input and translate chronological position from
978 * input to logical position on the device.
979 * The lock should be taken before calling this function.
980 *****************************************************************************/
981 static void DvdReadSeek( input_thread_t * p_input, off_t i_off )
983 thread_dvd_data_t * p_dvd;
991 vlc_mutex_lock( &p_input->stream.stream_lock );
992 i_off += p_input->stream.p_selected_area->i_start;
993 vlc_mutex_unlock( &p_input->stream.stream_lock );
995 i_lb = OFF2LB( i_off );
996 p_dvd = ( thread_dvd_data_t * )p_input->p_access_data;
999 while( p_dvd->p_cur_pgc->cell_playback[i_cell].last_sector < i_lb )
1011 pgc_id = p_dvd->p_vts_file->vts_ptt_srpt->title[
1012 p_dvd->i_ttn-1].ptt[i_chapter-1].pgcn;
1013 pgn = p_dvd->p_vts_file->vts_ptt_srpt->title[
1014 p_dvd->i_ttn-1].ptt[i_chapter-1].pgn;
1016 p_pgc = p_dvd->p_vts_file->vts_pgcit->pgci_srp[pgc_id-1].pgc;
1017 i_tmp = p_pgc->program_map[pgn-1];
1019 } while( i_tmp <= i_cell );
1022 while( p_dvd->p_vts_file->vts_vobu_admap->vobu_start_sectors[i_vobu]
1029 while( p_dvd->p_vts_file->vts_c_adt->cell_adr_table[i_sub_cell].start_sector <
1030 p_dvd->p_vts_file->vts_vobu_admap->vobu_start_sectors[i_vobu-1] )
1036 intf_WarnMsg(12, "cell %d i_sub_cell %d chapter %d vobu %d cell_sector %d vobu_sector %d sub_cell_sector %d",
1037 i_cell, i_sub_cell,i_chapter, i_vobu,
1038 p_dvd->p_cur_pgc->cell_playback[i_cell].first_sector,
1039 p_dvd->p_vts_file->vts_vobu_admap->vobu_start_sectors[i_vobu],
1040 p_dvd->p_vts_file->vts_c_adt->cell_adr_table[i_sub_cell-1].start_sector);
1042 p_dvd->i_cur_block = i_lb;
1043 p_dvd->i_next_vobu =
1044 p_dvd->p_vts_file->vts_vobu_admap->vobu_start_sectors[i_vobu];
1045 p_dvd->i_pack_len = p_dvd->i_next_vobu - i_lb;
1046 p_dvd->i_cur_cell = i_cell;
1047 p_dvd->i_chapter = i_chapter;
1048 DvdReadFindCell( p_dvd );
1050 vlc_mutex_lock( &p_input->stream.stream_lock );
1051 p_input->stream.p_selected_area->i_tell =
1052 LB2OFF ( p_dvd->i_cur_block )
1053 - p_input->stream.p_selected_area->i_start;
1054 p_input->stream.p_selected_area->i_part = p_dvd->i_chapter;
1055 vlc_mutex_unlock( &p_input->stream.stream_lock );
1060 /*****************************************************************************
1062 *****************************************************************************/
1063 static void DvdReadHandleDSI( thread_dvd_data_t * p_dvd, u8 * p_data )
1065 navRead_DSI( &(p_dvd->dsi_pack), &(p_data[ DSI_START_BYTE ]) );
1068 * Determine where we go next. These values are the ones we mostly
1071 p_dvd->i_cur_block = p_dvd->dsi_pack.dsi_gi.nv_pck_lbn;
1074 * If we're not at the end of this cell, we can determine the next
1075 * VOBU to display using the VOBU_SRI information section of the
1076 * DSI. Using this value correctly follows the current angle,
1077 * avoiding the doubled scenes in The Matrix, and makes our life
1080 if( p_dvd->dsi_pack.vobu_sri.next_vobu != SRI_END_OF_CELL )
1083 switch( ( p_dvd->dsi_pack.sml_pbi.category & 0xf000 ) >> 12 )
1086 /* interleaved unit with no angle */
1087 if( p_dvd->dsi_pack.sml_pbi.ilvu_sa != -1 )
1089 p_dvd->i_next_vobu = p_dvd->i_cur_block +
1090 p_dvd->dsi_pack.sml_pbi.ilvu_sa;
1091 p_dvd->i_pack_len = p_dvd->dsi_pack.sml_pbi.ilvu_ea;
1095 p_dvd->i_next_vobu = p_dvd->i_cur_block +
1096 p_dvd->dsi_pack.dsi_gi.vobu_ea + 1;
1097 p_dvd->i_pack_len = p_dvd->dsi_pack.dsi_gi.vobu_ea;
1101 /* vobu is end of ilvu */
1102 if( p_dvd->dsi_pack.sml_agli.data[p_dvd->i_angle-1].address )
1104 p_dvd->i_next_vobu = p_dvd->i_cur_block +
1105 p_dvd->dsi_pack.sml_agli.data[p_dvd->i_angle-1].address;
1106 p_dvd->i_pack_len = p_dvd->dsi_pack.sml_pbi.ilvu_ea;
1111 /* vobu is beginning of ilvu */
1115 /* entering interleaved section */
1117 /* non interleaved cells in interleaved section */
1119 p_dvd->i_next_vobu = p_dvd->i_cur_block +
1120 ( p_dvd->dsi_pack.vobu_sri.next_vobu & 0x7fffffff );
1121 p_dvd->i_pack_len = p_dvd->dsi_pack.dsi_gi.vobu_ea;
1125 p_dvd->i_next_vobu = p_dvd->i_cur_block +
1126 ( p_dvd->dsi_pack.vobu_sri.next_vobu & 0x7fffffff );
1127 p_dvd->i_pack_len = p_dvd->dsi_pack.dsi_gi.vobu_ea;
1132 p_dvd->i_cur_cell = p_dvd->i_next_cell;
1133 DvdReadFindCell( p_dvd );
1135 p_dvd->i_pack_len = p_dvd->dsi_pack.dsi_gi.vobu_ea;
1136 p_dvd->i_next_vobu =
1137 p_dvd->p_cur_pgc->cell_playback[p_dvd->i_cur_cell].first_sector;
1141 intf_WarnMsg( 12, "scr %d lbn 0x%02x vobu_ea %d vob_id %d c_id %d",
1142 p_dvd->dsi_pack.dsi_gi.nv_pck_scr,
1143 p_dvd->dsi_pack.dsi_gi.nv_pck_lbn,
1144 p_dvd->dsi_pack.dsi_gi.vobu_ea,
1145 p_dvd->dsi_pack.dsi_gi.vobu_vob_idn,
1146 p_dvd->dsi_pack.dsi_gi.vobu_c_idn );
1148 intf_WarnMsg( 12, "cat 0x%02x ilvu_ea %d ilvu_sa %d size %d",
1149 p_dvd->dsi_pack.sml_pbi.category,
1150 p_dvd->dsi_pack.sml_pbi.ilvu_ea,
1151 p_dvd->dsi_pack.sml_pbi.ilvu_sa,
1152 p_dvd->dsi_pack.sml_pbi.size );
1154 intf_WarnMsg( 12, "next_vobu %d next_ilvu1 %d next_ilvu2 %d",
1155 p_dvd->dsi_pack.vobu_sri.next_vobu & 0x7fffffff,
1156 p_dvd->dsi_pack.sml_agli.data[ p_dvd->i_angle - 1 ].address,
1157 p_dvd->dsi_pack.sml_agli.data[ p_dvd->i_angle ].address);
1161 /*****************************************************************************
1163 *****************************************************************************/
1164 static void DvdReadFindCell( thread_dvd_data_t * p_dvd )
1169 #define cell p_dvd->p_cur_pgc->cell_playback
1170 if( cell[p_dvd->i_cur_cell].block_type == BLOCK_TYPE_ANGLE_BLOCK )
1173 p_dvd->i_next_cell = p_dvd->i_cur_cell + p_dvd->i_angle_nb;
1174 p_dvd->i_cur_cell += p_dvd->i_angle - 1;
1176 p_dvd->i_cur_cell += p_dvd->i_angle - 1;
1178 while( cell[p_dvd->i_cur_cell+i].block_mode != BLOCK_MODE_LAST_CELL )
1182 p_dvd->i_next_cell = p_dvd->i_cur_cell + i + 1;
1187 p_dvd->i_next_cell = p_dvd->i_cur_cell + 1;
1190 pgc_id = p_dvd->p_vts_file->vts_ptt_srpt->title[
1191 p_dvd->i_ttn-1].ptt[p_dvd->i_chapter-1].pgcn;
1192 pgn = p_dvd->p_vts_file->vts_ptt_srpt->title[
1193 p_dvd->i_ttn-1].ptt[p_dvd->i_chapter-1].pgn;
1194 p_pgc = p_dvd->p_vts_file->vts_pgcit->pgci_srp[pgc_id-1].pgc;
1196 if( p_pgc->program_map[pgn-1] <= p_dvd->i_cur_cell )
1203 /*****************************************************************************
1204 * DvdReadLaunchDecoders
1205 *****************************************************************************/
1206 static void DvdReadLauchDecoders( input_thread_t * p_input )
1208 thread_dvd_data_t * p_dvd;
1210 p_dvd = (thread_dvd_data_t*)(p_input->p_access_data);
1212 if( p_main->b_video )
1214 input_SelectES( p_input, p_input->stream.pp_es[0] );
1217 if( p_main->b_audio )
1219 /* For audio: first one if none or a not existing one specified */
1220 int i_audio = config_GetIntVariable( "audio-channel" );
1221 if( i_audio < 0 /*|| i_audio > i_audio_nb*/ )
1223 config_PutIntVariable( "audio-channel", 1 );
1226 if( i_audio > 0/* && i_audio_nb > 0*/ )
1228 if( config_GetIntVariable( "audio-type" ) == REQUESTED_AC3 )
1230 int i_ac3 = i_audio;
1231 while( ( p_input->stream.pp_es[i_ac3]->i_type !=
1232 AC3_AUDIO_ES ) && ( i_ac3 <=
1233 p_dvd->p_vts_file->vtsi_mat->nr_of_vts_audio_streams ) )
1237 if( p_input->stream.pp_es[i_ac3]->i_type == AC3_AUDIO_ES )
1239 input_SelectES( p_input,
1240 p_input->stream.pp_es[i_ac3] );
1245 input_SelectES( p_input,
1246 p_input->stream.pp_es[i_audio] );
1251 if( p_main->b_video )
1253 /* for spu, default is none */
1254 int i_spu = config_GetIntVariable( "spu-channel" );
1255 if( i_spu < 0 /*|| i_spu > i_spu_nb*/ )
1257 config_PutIntVariable( "spu-channel", 0 );
1260 if( i_spu > 0 /*&& i_spu_nb > 0*/ )
1262 i_spu += p_dvd->p_vts_file->vtsi_mat->nr_of_vts_audio_streams;
1263 input_SelectES( p_input, p_input->stream.pp_es[i_spu] );