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.41 2002/07/23 00:39:17 sam Exp $
11 * Author: Stéphane Borel <stef@via.ecp.fr>
13 * Some code taken form the play_title.c by Billy Biggs <vektor@dumbterm.net>
16 * This program is free software; you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License as published by
18 * the Free Software Foundation; either version 2 of the License, or
19 * (at your option) any later version.
21 * This program is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU General Public License for more details.
26 * You should have received a copy of the GNU General Public License
27 * along with this program; if not, write to the Free Software
28 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
29 *****************************************************************************/
31 /*****************************************************************************
33 *****************************************************************************/
38 #include <vlc/input.h>
45 #include <sys/types.h>
51 #ifdef STRNCASECMP_IN_STRINGS_H
56 # include <io.h> /* read() */
58 # include <sys/uio.h> /* struct iovec */
62 # include "input_iovec.h"
65 #include <dvdread/dvd_reader.h>
66 #include <dvdread/ifo_types.h>
67 #include <dvdread/ifo_read.h>
68 #include <dvdread/nav_read.h>
69 #include <dvdread/nav_print.h>
71 #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 ( input_thread_t * );
83 static void DvdReadEnd ( input_thread_t * );
84 static int DvdReadDemux ( input_thread_t * );
85 static int DvdReadRewind ( input_thread_t * );
87 static int DvdReadOpen ( input_thread_t * );
88 static void DvdReadClose ( input_thread_t * );
89 static int DvdReadSetArea ( input_thread_t *, input_area_t * );
90 static int DvdReadSetProgram ( input_thread_t *, pgrm_descriptor_t * );
91 static int DvdReadRead ( input_thread_t *, byte_t *, size_t );
92 static void DvdReadSeek ( input_thread_t *, 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( input_thread_t *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_GetPsz( p_input, "dvd" );
295 if( stat( psz_source, &stat_info ) == -1 )
297 msg_Err( p_input, "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 msg_Warn( p_input, "dvdread module discarded (not a valid source)" );
309 msg_Dbg( p_input, "dvdroot=%s title=%d chapter=%d angle=%d",
310 psz_source, i_title, i_chapter, i_angle );
313 p_dvdread = DVDOpen( psz_source );
315 /* free allocated strings */
316 if( psz_source != psz_orig )
322 msg_Err( p_input, "libdvdcss cannot open source" );
329 p_dvd = malloc( sizeof(thread_dvd_data_t) );
332 msg_Err( p_input, "out of memory" );
336 p_dvd->p_dvdread = p_dvdread;
337 p_dvd->p_title = NULL;
338 p_dvd->p_vts_file = NULL;
341 p_input->p_access_data = (void *)p_dvd;
343 /* Ifo allocation & initialisation */
344 if( ! ( p_dvd->p_vmg_file = ifoOpen( p_dvd->p_dvdread, 0 ) ) )
346 msg_Err( p_input, "cannot open VMG info" );
350 msg_Dbg( p_input, "VMG opened" );
352 /* Set stream and area data */
353 vlc_mutex_lock( &p_input->stream.stream_lock );
355 p_input->stream.i_method = INPUT_METHOD_DVD;
357 /* If we are here we can control the pace... */
358 p_input->stream.b_pace_control = 1;
359 p_input->stream.b_seekable = 1;
361 p_input->stream.p_selected_area->i_size = 0;
362 p_input->stream.p_selected_area->i_tell = 0;
364 /* Initialize ES structures */
365 input_InitStream( p_input, sizeof( stream_ps_data_t ) );
367 /* disc input method */
368 p_input->stream.i_method = INPUT_METHOD_DVD;
370 #define tt_srpt p_dvd->p_vmg_file->tt_srpt
371 msg_Dbg( p_input, "number of titles: %d", tt_srpt->nr_of_srpts );
373 #define area p_input->stream.pp_areas
374 /* We start from 1 here since the default area 0
375 * is reserved for video_ts.vob */
376 for( i = 1 ; i <= tt_srpt->nr_of_srpts ; i++ )
378 input_AddArea( p_input );
380 /* Titles are Program Chains */
383 /* Absolute start offset and size
384 * We can only set that with vts ifo, so we do it during the
385 * first call to DVDSetArea */
386 area[i]->i_start = 0;
389 /* Number of chapters */
390 area[i]->i_part_nb = tt_srpt->title[i-1].nr_of_ptts;
393 area[i]->i_plugin_data = tt_srpt->title[i-1].title_set_nr;
397 p_dvd->i_title = i_title <= tt_srpt->nr_of_srpts ? i_title : 1;
400 p_area = p_input->stream.pp_areas[p_dvd->i_title];
401 p_dvd->i_chapter = i_chapter;
403 p_dvd->i_chapter = i_chapter < p_area->i_part_nb ? i_chapter : 1;
404 p_area->i_part = p_dvd->i_chapter;
406 p_dvd->i_angle = i_angle;
408 /* set title, chapter, audio and subpic */
409 if( DvdReadSetArea( p_input, p_area ) )
411 vlc_mutex_unlock( &p_input->stream.stream_lock );
415 vlc_mutex_unlock( &p_input->stream.stream_lock );
417 p_input->psz_demux = "dvdread";
422 /*****************************************************************************
423 * DvdReadClose: close libdvdread
424 *****************************************************************************/
425 static void DvdReadClose( input_thread_t *p_input )
427 thread_dvd_data_t * p_dvd;
429 p_dvd = (thread_dvd_data_t *)p_input->p_access_data;
431 /* close libdvdread */
432 DVDCloseFile( p_dvd->p_title );
433 ifoClose( p_dvd->p_vts_file );
434 ifoClose( p_dvd->p_vmg_file );
436 DVDClose( p_dvd->p_dvdread );
438 p_input->p_access_data = NULL;
442 /*****************************************************************************
443 * DvdReadSetProgram: Does nothing, a DVD is mono-program
444 *****************************************************************************/
445 static int DvdReadSetProgram( input_thread_t * p_input,
446 pgrm_descriptor_t * p_program )
448 if( p_input->stream.p_selected_program != p_program )
450 thread_dvd_data_t * p_dvd;
452 p_dvd = (thread_dvd_data_t*)(p_input->p_access_data);
453 p_dvd->i_angle = p_program->i_number;
455 memcpy( p_program, p_input->stream.p_selected_program,
456 sizeof(pgrm_descriptor_t) );
457 p_program->i_number = p_dvd->i_angle;
458 p_input->stream.p_selected_program = p_program;
460 msg_Dbg( p_input, "angle %d selected", p_dvd->i_angle );
466 #define p_pgc p_dvd->p_cur_pgc
468 /*****************************************************************************
469 * DvdReadSetArea: initialize input data for title x, chapter y.
470 * It should be called for each user navigation request.
471 *****************************************************************************
472 * Take care that i_title starts from 0 (vmg) and i_chapter start from 1.
473 * Note that you have to take the lock before entering here.
474 *****************************************************************************/
475 static int DvdReadSetArea( input_thread_t * p_input, input_area_t * p_area )
477 thread_dvd_data_t * p_dvd;
481 p_dvd = (thread_dvd_data_t*)p_input->p_access_data;
483 /* we can't use the interface slider until initilization is complete */
484 p_input->stream.b_seekable = 0;
486 if( p_area != p_input->stream.p_selected_area )
488 es_descriptor_t * p_es;
494 #define p_vmg p_dvd->p_vmg_file
495 #define p_vts p_dvd->p_vts_file
496 if( p_dvd->p_title != NULL )
498 DVDCloseFile( p_dvd->p_title );
506 /* Reset the Chapter position of the old title */
507 p_input->stream.p_selected_area->i_part = 1;
510 * We have to load all title information
512 /* Change the default area */
513 p_input->stream.p_selected_area = p_area;
515 msg_Dbg( p_input, "open VTS %d, for title %d",
516 p_vmg->tt_srpt->title[ p_area->i_id - 1 ].title_set_nr,
520 if( ! ( p_vts = ifoOpen( p_dvd->p_dvdread,
521 p_vmg->tt_srpt->title[ p_area->i_id - 1 ].title_set_nr ) ) )
523 msg_Err( p_input, "fatal error in vts ifo" );
525 DVDClose( p_dvd->p_dvdread );
529 /* title position inside the selected vts */
530 p_dvd->i_ttn = p_vmg->tt_srpt->title[ p_area->i_id - 1 ].vts_ttn;
533 * Set selected title start
535 pgc_id = p_vts->vts_ptt_srpt->title[p_dvd->i_ttn-1].ptt[0].pgcn;
536 pgn = p_vts->vts_ptt_srpt->title[p_dvd->i_ttn-1].ptt[0].pgn;
537 p_pgc = p_vts->vts_pgcit->pgci_srp[ pgc_id - 1 ].pgc;
538 i_cell = p_pgc->program_map[ pgn - 1 ] - 1;
541 LB2OFF( p_dvd->p_cur_pgc->cell_playback[ i_cell ].first_sector );
543 msg_Dbg( p_input, "start %d vts_title %d pgc %d pgn %d",
544 p_area->i_id, p_dvd->i_ttn, pgc_id, pgn );
549 i_cell = p_dvd->p_cur_pgc->nr_of_cells - 1;
551 p_dvd->i_end_block = p_pgc->cell_playback[ i_cell ].last_sector;
552 p_area->i_size = LB2OFF( p_dvd->i_end_block )- p_area->i_start;
554 msg_Dbg( p_input, "start %lld size %lld end %d",
555 p_area->i_start , p_area->i_size, p_dvd->i_end_block );
558 * Set properties for current chapter
560 /* Remeber current chapter */
561 p_dvd->i_chapter = p_area->i_part;
564 pgc_id = p_vts->vts_ptt_srpt->title[
565 p_dvd->i_ttn-1].ptt[p_area->i_part-1].pgcn;
566 pgn = p_vts->vts_ptt_srpt->title[
567 p_dvd->i_ttn-1].ptt[p_area->i_part-1].pgn;
569 p_pgc = p_vts->vts_pgcit->pgci_srp[pgc_id-1].pgc;
570 p_dvd->i_pack_len = 0;
571 p_dvd->i_next_cell = p_dvd->i_cur_cell = p_pgc->program_map[pgn-1] - 1;
572 DvdReadFindCell( p_dvd );
574 p_dvd->i_next_vobu = p_dvd->i_cur_block =
575 p_pgc->cell_playback[p_dvd->i_cur_cell].first_sector;
580 p_dvd->i_angle_nb = p_vmg->tt_srpt->title[p_area->i_id-1].nr_of_angles;
582 if( p_dvd->i_angle > p_dvd->i_angle_nb )
588 * We've got enough info, time to open the title set data.
590 if( ! ( p_dvd->p_title = DVDOpenFile( p_dvd->p_dvdread,
591 p_vmg->tt_srpt->title[ p_area->i_id - 1 ].title_set_nr,
592 DVD_READ_TITLE_VOBS ) ) )
594 msg_Err( p_input, "cannot open title (VTS_%02d_1.VOB)",
595 p_vmg->tt_srpt->title[p_area->i_id-1].title_set_nr );
598 DVDClose( p_dvd->p_dvdread );
602 // IfoPrintTitle( p_dvd );
605 * Destroy obsolete ES by reinitializing program 0
606 * and find all ES in title with ifo data
608 if( p_input->stream.pp_programs != NULL )
610 /* We don't use input_EndStream here since
611 * we keep area structures */
613 while( p_input->stream.i_es_number )
615 input_DelES( p_input, p_input->stream.pp_es[0] );
618 while( p_input->stream.i_pgrm_number )
620 input_DelProgram( p_input, p_input->stream.pp_programs[0] );
623 if( p_input->stream.pp_selected_es )
625 free( p_input->stream.pp_selected_es );
626 p_input->stream.pp_selected_es = NULL;
628 p_input->stream.i_selected_es_number = 0;
631 input_AddProgram( p_input, 1, sizeof( stream_ps_data_t ) );
632 p_input->stream.p_selected_program = p_input->stream.pp_programs[0];
634 for( i = 1 ; i < p_dvd->i_angle_nb ; i++ )
636 input_AddProgram( p_input, i+1, 0 );
639 DvdReadSetProgram( p_input,
640 p_input->stream.pp_programs[p_dvd->i_angle-1] );
642 /* No PSM to read in DVD mode, we already have all information */
643 p_input->stream.p_selected_program->b_is_ok = 1;
647 /* ES 0 -> video MPEG2 */
648 // IfoPrintVideo( p_dvd );
650 p_es = input_AddES( p_input, NULL, 0xe0, 0 );
651 p_es->i_stream_id = 0xe0;
652 p_es->i_fourcc = VLC_FOURCC('m','p','g','v');
653 p_es->i_cat = VIDEO_ES;
655 #define audio_control \
656 p_dvd->p_vts_file->vts_pgcit->pgci_srp[pgc_id-1].pgc->audio_control[i-1]
657 /* Audio ES, in the order they appear in .ifo */
658 for( i = 1 ; i <= p_vts->vtsi_mat->nr_of_vts_audio_streams ; i++ )
663 // IfoPrintAudio( p_dvd, i );
665 /* audio channel is active if first byte is 0x80 */
666 if( audio_control & 0x8000 )
669 i_position = ( audio_control & 0x7F00 ) >> 8;
671 msg_Dbg( p_input, "audio position %d", i_position );
672 switch( p_vts->vtsi_mat->vts_audio_attr[i-1].audio_format )
675 i_id = ( ( 0x80 + i_position ) << 8 ) | 0xbd;
676 p_es = input_AddES( p_input, NULL, i_id, 0 );
677 p_es->i_stream_id = 0xbd;
678 p_es->i_fourcc = VLC_FOURCC('a','5','2',' ');
679 p_es->i_cat = AUDIO_ES;
680 strcpy( p_es->psz_desc, DecodeLanguage(
681 p_vts->vtsi_mat->vts_audio_attr[i-1].lang_code ) );
682 strcat( p_es->psz_desc, " (ac3)" );
686 case 0x03: /* MPEG audio */
687 i_id = 0xc0 + i_position;
688 p_es = input_AddES( p_input, NULL, i_id, 0 );
689 p_es->i_stream_id = i_id;
690 p_es->i_fourcc = VLC_FOURCC('m','p','g','a');
691 p_es->i_cat = AUDIO_ES;
692 strcpy( p_es->psz_desc, DecodeLanguage(
693 p_vts->vtsi_mat->vts_audio_attr[i-1].lang_code ) );
694 strcat( p_es->psz_desc, " (mpeg)" );
697 case 0x04: /* LPCM */
699 i_id = ( ( 0xa0 + i_position ) << 8 ) | 0xbd;
700 p_es = input_AddES( p_input, NULL, i_id, 0 );
701 p_es->i_stream_id = i_id;
702 p_es->i_fourcc = VLC_FOURCC('l','p','c','m');
703 p_es->i_cat = AUDIO_ES;
704 strcpy( p_es->psz_desc, DecodeLanguage(
705 p_vts->vtsi_mat->vts_audio_attr[i-1].lang_code ) );
706 strcat( p_es->psz_desc, " (lpcm)" );
710 i_id = ( ( 0x88 + i_position ) << 8 ) | 0xbd;
711 msg_Err( p_input, "DTS audio not handled yet"
716 msg_Err( p_input, "unknown audio type %.2x",
717 p_vts->vtsi_mat->vts_audio_attr[i-1].audio_format );
722 #define spu_control \
723 p_dvd->p_vts_file->vts_pgcit->pgci_srp[pgc_id-1].pgc->subp_control[i-1]
727 for( i = 1 ; i <= p_vts->vtsi_mat->nr_of_vts_subp_streams; i++ )
732 // IfoPrintSpu( p_dvd, i );
733 msg_Dbg( p_input, "spu %d 0x%02x", i, spu_control );
735 if( spu_control & 0x80000000 )
739 /* there are several streams for one spu */
740 if( p_vts->vtsi_mat->vts_video_attr.display_aspect_ratio )
743 switch( p_vts->vtsi_mat->vts_video_attr.permitted_df )
746 i_position = spu_control & 0xff;
749 i_position = ( spu_control >> 8 ) & 0xff;
752 i_position = ( spu_control >> 16 ) & 0xff;
759 i_position = ( spu_control >> 24 ) & 0x7F;
762 i_id = ( ( 0x20 + i_position ) << 8 ) | 0xbd;
763 p_es = input_AddES( p_input, NULL, i_id, 0 );
764 p_es->i_stream_id = 0xbd;
765 p_es->i_fourcc = VLC_FOURCC('s','p','u',' ');
766 p_es->i_cat = SPU_ES;
767 strcpy( p_es->psz_desc, DecodeLanguage(
768 p_vts->vtsi_mat->vts_subp_attr[i-1].lang_code ) );
773 /* FIXME: hack to check that the demuxer is ready, and set
775 if( p_input->p_demux_module )
777 DvdReadLauchDecoders( p_input );
783 p_area = p_input->stream.p_selected_area;
790 if( p_area->i_part != p_dvd->i_chapter )
792 if( ( p_area->i_part > 0 ) &&
793 ( p_area->i_part <= p_area->i_part_nb ))
795 p_dvd->i_ttn = p_vmg->tt_srpt->title[p_area->i_id-1].vts_ttn;
796 pgc_id = p_vts->vts_ptt_srpt->title[
797 p_dvd->i_ttn-1].ptt[p_area->i_part-1].pgcn;
798 pgn = p_vts->vts_ptt_srpt->title[
799 p_dvd->i_ttn-1].ptt[p_area->i_part-1].pgn;
801 p_pgc = p_vts->vts_pgcit->pgci_srp[ pgc_id - 1 ].pgc;
803 p_dvd->i_cur_cell = p_pgc->program_map[ pgn - 1 ] - 1;
804 p_dvd->i_chapter = p_area->i_part;
805 DvdReadFindCell( p_dvd );
807 p_dvd->i_pack_len = 0;
808 p_dvd->i_next_vobu = p_dvd->i_cur_block =
809 p_pgc->cell_playback[p_dvd->i_cur_cell].first_sector;
813 p_area->i_part = p_dvd->i_chapter;
819 /* warn interface that something has changed */
820 p_area->i_tell = LB2OFF( p_dvd->i_next_vobu ) - p_area->i_start;
821 p_input->stream.b_seekable = 1;
822 p_input->stream.b_changed = 1;
828 /*****************************************************************************
829 * DvdReadRead: reads data packets into the netlist.
830 *****************************************************************************
831 * Returns -1 in case of error, 0 if everything went well, and 1 in case of
833 *****************************************************************************/
834 static int DvdReadRead( input_thread_t * p_input,
835 byte_t * p_buffer, size_t i_count )
837 thread_dvd_data_t * p_dvd;
843 vlc_bool_t b_eot = 0;
845 p_dvd = (thread_dvd_data_t *)p_input->p_access_data;
849 * Playback by cell in this pgc, starting at the cell for our chapter.
851 i_blocks = OFF2LB( i_count );
858 * End of pack, we select the following one
860 if( ! p_dvd->i_pack_len )
865 if( ( i_read = DVDReadBlocks( p_dvd->p_title, p_dvd->i_next_vobu,
868 msg_Err( p_input, "read failed for block %d",
869 p_dvd->i_next_vobu );
873 /* basic check to be sure we don't have a empty title
874 * go to next title if so */
875 //assert( p_buffer[41] == 0xbf && p_buffer[1027] == 0xbf );
878 * Parse the contained dsi packet.
881 DvdReadHandleDSI( p_dvd, p_buf );
884 if( p_dvd->i_next_vobu >= p_dvd->i_end_block + 1 )
889 assert( p_dvd->i_pack_len < 1024 );
890 /* FIXME: Ugly kludge: we send the pack block to the input for it
891 * sometimes has a zero scr and restart the sync */
892 p_dvd->i_cur_block ++;
893 //p_dvd->i_pack_len++;
896 p_buf += DVD_VIDEO_LB_LEN;
901 * Compute the number of blocks to read
903 i_blocks_once = p_dvd->i_pack_len >= i_blocks
904 ? i_blocks : p_dvd->i_pack_len;
905 p_dvd->i_pack_len -= i_blocks_once;
908 i_read = DVDReadBlocks( p_dvd->p_title, p_dvd->i_cur_block,
909 i_blocks_once, p_buf );
910 if( i_read != i_blocks_once )
912 msg_Err( p_input, "read failed for %d/%d blocks at 0x%02x",
913 i_read, i_blocks_once, p_dvd->i_cur_block );
918 i_read_total += i_read;
919 p_dvd->i_cur_block += i_read;
920 p_buf += LB2OFF( i_read );
924 msg_Dbg( p_input, "i_blocks: %d len: %d current: 0x%02x", i_read, p_dvd->i_pack_len, p_dvd->i_cur_block );
927 vlc_mutex_lock( &p_input->stream.stream_lock );
929 p_input->stream.p_selected_area->i_tell =
930 LB2OFF( p_dvd->i_cur_block ) -
931 p_input->stream.p_selected_area->i_start;
935 /* We modify i_part only at end of chapter not to erase
936 * some modification from the interface */
937 p_input->stream.p_selected_area->i_part = p_dvd->i_chapter;
941 if( p_input->stream.p_selected_area->i_tell
942 >= p_input->stream.p_selected_area->i_size || b_eot )
944 if( ( p_input->stream.p_selected_area->i_id + 1 ) >=
945 p_input->stream.i_area_nb )
948 vlc_mutex_unlock( &p_input->stream.stream_lock );
953 msg_Dbg( p_input, "new title" );
954 DvdReadSetArea( p_input, p_input->stream.pp_areas[
955 p_input->stream.p_selected_area->i_id+1] );
956 vlc_mutex_unlock( &p_input->stream.stream_lock );
960 vlc_mutex_unlock( &p_input->stream.stream_lock );
962 return LB2OFF( i_read_total );
966 /*****************************************************************************
967 * DvdReadSeek : Goes to a given position on the stream.
968 *****************************************************************************
969 * This one is used by the input and translate chronological position from
970 * input to logical position on the device.
971 * The lock should be taken before calling this function.
972 *****************************************************************************/
973 static void DvdReadSeek( input_thread_t * p_input, off_t i_off )
975 thread_dvd_data_t * p_dvd;
983 vlc_mutex_lock( &p_input->stream.stream_lock );
984 i_off += p_input->stream.p_selected_area->i_start;
985 vlc_mutex_unlock( &p_input->stream.stream_lock );
987 i_lb = OFF2LB( i_off );
988 p_dvd = ( thread_dvd_data_t * )p_input->p_access_data;
991 while( p_dvd->p_cur_pgc->cell_playback[i_cell].last_sector < i_lb )
1003 pgc_id = p_dvd->p_vts_file->vts_ptt_srpt->title[
1004 p_dvd->i_ttn-1].ptt[i_chapter-1].pgcn;
1005 pgn = p_dvd->p_vts_file->vts_ptt_srpt->title[
1006 p_dvd->i_ttn-1].ptt[i_chapter-1].pgn;
1008 p_pgc = p_dvd->p_vts_file->vts_pgcit->pgci_srp[pgc_id-1].pgc;
1009 i_tmp = p_pgc->program_map[pgn-1];
1011 } while( i_tmp <= i_cell );
1014 while( p_dvd->p_vts_file->vts_vobu_admap->vobu_start_sectors[i_vobu]
1021 while( p_dvd->p_vts_file->vts_c_adt->cell_adr_table[i_sub_cell].start_sector <
1022 p_dvd->p_vts_file->vts_vobu_admap->vobu_start_sectors[i_vobu-1] )
1028 msg_Dbg( p_input, "cell %d i_sub_cell %d chapter %d vobu %d cell_sector %d vobu_sector %d sub_cell_sector %d",
1029 i_cell, i_sub_cell,i_chapter, i_vobu,
1030 p_dvd->p_cur_pgc->cell_playback[i_cell].first_sector,
1031 p_dvd->p_vts_file->vts_vobu_admap->vobu_start_sectors[i_vobu],
1032 p_dvd->p_vts_file->vts_c_adt->cell_adr_table[i_sub_cell-1].start_sector);
1034 p_dvd->i_cur_block = i_lb;
1035 p_dvd->i_next_vobu =
1036 p_dvd->p_vts_file->vts_vobu_admap->vobu_start_sectors[i_vobu];
1037 p_dvd->i_pack_len = p_dvd->i_next_vobu - i_lb;
1038 p_dvd->i_cur_cell = i_cell;
1039 p_dvd->i_chapter = i_chapter;
1040 DvdReadFindCell( p_dvd );
1042 vlc_mutex_lock( &p_input->stream.stream_lock );
1043 p_input->stream.p_selected_area->i_tell =
1044 LB2OFF ( p_dvd->i_cur_block )
1045 - p_input->stream.p_selected_area->i_start;
1046 p_input->stream.p_selected_area->i_part = p_dvd->i_chapter;
1047 vlc_mutex_unlock( &p_input->stream.stream_lock );
1052 /*****************************************************************************
1054 *****************************************************************************/
1055 static void DvdReadHandleDSI( thread_dvd_data_t * p_dvd, u8 * p_data )
1057 navRead_DSI( &(p_dvd->dsi_pack), &(p_data[ DSI_START_BYTE ]) );
1060 * Determine where we go next. These values are the ones we mostly
1063 p_dvd->i_cur_block = p_dvd->dsi_pack.dsi_gi.nv_pck_lbn;
1066 * If we're not at the end of this cell, we can determine the next
1067 * VOBU to display using the VOBU_SRI information section of the
1068 * DSI. Using this value correctly follows the current angle,
1069 * avoiding the doubled scenes in The Matrix, and makes our life
1072 if( p_dvd->dsi_pack.vobu_sri.next_vobu != SRI_END_OF_CELL )
1075 switch( ( p_dvd->dsi_pack.sml_pbi.category & 0xf000 ) >> 12 )
1078 /* interleaved unit with no angle */
1079 if( p_dvd->dsi_pack.sml_pbi.ilvu_sa != -1 )
1081 p_dvd->i_next_vobu = p_dvd->i_cur_block +
1082 p_dvd->dsi_pack.sml_pbi.ilvu_sa;
1083 p_dvd->i_pack_len = p_dvd->dsi_pack.sml_pbi.ilvu_ea;
1087 p_dvd->i_next_vobu = p_dvd->i_cur_block +
1088 p_dvd->dsi_pack.dsi_gi.vobu_ea + 1;
1089 p_dvd->i_pack_len = p_dvd->dsi_pack.dsi_gi.vobu_ea;
1093 /* vobu is end of ilvu */
1094 if( p_dvd->dsi_pack.sml_agli.data[p_dvd->i_angle-1].address )
1096 p_dvd->i_next_vobu = p_dvd->i_cur_block +
1097 p_dvd->dsi_pack.sml_agli.data[p_dvd->i_angle-1].address;
1098 p_dvd->i_pack_len = p_dvd->dsi_pack.sml_pbi.ilvu_ea;
1103 /* vobu is beginning of ilvu */
1107 /* entering interleaved section */
1109 /* non interleaved cells in interleaved section */
1111 p_dvd->i_next_vobu = p_dvd->i_cur_block +
1112 ( p_dvd->dsi_pack.vobu_sri.next_vobu & 0x7fffffff );
1113 p_dvd->i_pack_len = p_dvd->dsi_pack.dsi_gi.vobu_ea;
1117 p_dvd->i_next_vobu = p_dvd->i_cur_block +
1118 ( p_dvd->dsi_pack.vobu_sri.next_vobu & 0x7fffffff );
1119 p_dvd->i_pack_len = p_dvd->dsi_pack.dsi_gi.vobu_ea;
1124 p_dvd->i_cur_cell = p_dvd->i_next_cell;
1125 DvdReadFindCell( p_dvd );
1127 p_dvd->i_pack_len = p_dvd->dsi_pack.dsi_gi.vobu_ea;
1128 p_dvd->i_next_vobu =
1129 p_dvd->p_cur_pgc->cell_playback[p_dvd->i_cur_cell].first_sector;
1133 msg_Dbg( p_input, 12, "scr %d lbn 0x%02x vobu_ea %d vob_id %d c_id %d",
1134 p_dvd->dsi_pack.dsi_gi.nv_pck_scr,
1135 p_dvd->dsi_pack.dsi_gi.nv_pck_lbn,
1136 p_dvd->dsi_pack.dsi_gi.vobu_ea,
1137 p_dvd->dsi_pack.dsi_gi.vobu_vob_idn,
1138 p_dvd->dsi_pack.dsi_gi.vobu_c_idn );
1140 msg_Dbg( p_input, 12, "cat 0x%02x ilvu_ea %d ilvu_sa %d size %d",
1141 p_dvd->dsi_pack.sml_pbi.category,
1142 p_dvd->dsi_pack.sml_pbi.ilvu_ea,
1143 p_dvd->dsi_pack.sml_pbi.ilvu_sa,
1144 p_dvd->dsi_pack.sml_pbi.size );
1146 msg_Dbg( p_input, 12, "next_vobu %d next_ilvu1 %d next_ilvu2 %d",
1147 p_dvd->dsi_pack.vobu_sri.next_vobu & 0x7fffffff,
1148 p_dvd->dsi_pack.sml_agli.data[ p_dvd->i_angle - 1 ].address,
1149 p_dvd->dsi_pack.sml_agli.data[ p_dvd->i_angle ].address);
1153 /*****************************************************************************
1155 *****************************************************************************/
1156 static void DvdReadFindCell( thread_dvd_data_t * p_dvd )
1161 #define cell p_dvd->p_cur_pgc->cell_playback
1162 if( cell[p_dvd->i_cur_cell].block_type == BLOCK_TYPE_ANGLE_BLOCK )
1165 p_dvd->i_next_cell = p_dvd->i_cur_cell + p_dvd->i_angle_nb;
1166 p_dvd->i_cur_cell += p_dvd->i_angle - 1;
1168 p_dvd->i_cur_cell += p_dvd->i_angle - 1;
1170 while( cell[p_dvd->i_cur_cell+i].block_mode != BLOCK_MODE_LAST_CELL )
1174 p_dvd->i_next_cell = p_dvd->i_cur_cell + i + 1;
1179 p_dvd->i_next_cell = p_dvd->i_cur_cell + 1;
1182 pgc_id = p_dvd->p_vts_file->vts_ptt_srpt->title[
1183 p_dvd->i_ttn-1].ptt[p_dvd->i_chapter-1].pgcn;
1184 pgn = p_dvd->p_vts_file->vts_ptt_srpt->title[
1185 p_dvd->i_ttn-1].ptt[p_dvd->i_chapter-1].pgn;
1186 p_pgc = p_dvd->p_vts_file->vts_pgcit->pgci_srp[pgc_id-1].pgc;
1188 if( p_pgc->program_map[pgn-1] <= p_dvd->i_cur_cell )
1195 /*****************************************************************************
1196 * DvdReadLaunchDecoders
1197 *****************************************************************************/
1198 static void DvdReadLauchDecoders( input_thread_t * p_input )
1200 thread_dvd_data_t * p_dvd;
1202 p_dvd = (thread_dvd_data_t*)(p_input->p_access_data);
1204 if( config_GetInt( p_input, "video" ) )
1206 input_SelectES( p_input, p_input->stream.pp_es[0] );
1209 if( config_GetInt( p_input, "audio" ) )
1211 /* For audio: first one if none or a not existing one specified */
1212 int i_audio = config_GetInt( p_input, "audio-channel" );
1213 if( i_audio < 0 /*|| i_audio > i_audio_nb*/ )
1215 config_PutInt( p_input, "audio-channel", 1 );
1218 if( i_audio > 0/* && i_audio_nb > 0*/ )
1220 if( config_GetInt( p_input, "audio-type" )
1223 int i_ac3 = i_audio;
1224 while( ( p_input->stream.pp_es[i_ac3]->i_fourcc !=
1225 VLC_FOURCC('a','5','2',' ') ) && ( i_ac3 <=
1226 p_dvd->p_vts_file->vtsi_mat->nr_of_vts_audio_streams ) )
1230 if( p_input->stream.pp_es[i_ac3]->i_fourcc
1231 == VLC_FOURCC('a','5','2',' ') )
1233 input_SelectES( p_input,
1234 p_input->stream.pp_es[i_ac3] );
1239 input_SelectES( p_input,
1240 p_input->stream.pp_es[i_audio] );
1245 if( config_GetInt( p_input, "video" ) )
1247 /* for spu, default is none */
1248 int i_spu = config_GetInt( p_input, "spu-channel" );
1249 if( i_spu < 0 /*|| i_spu > i_spu_nb*/ )
1251 config_PutInt( p_input, "spu-channel", 0 );
1254 if( i_spu > 0 /*&& i_spu_nb > 0*/ )
1256 i_spu += p_dvd->p_vts_file->vtsi_mat->nr_of_vts_audio_streams;
1257 input_SelectES( p_input, p_input->stream.pp_es[i_spu] );