1 /*****************************************************************************
2 * input.c: DvdRead plugin.
3 *****************************************************************************
4 * This plugins should handle all the known specificities of the DVD format,
5 * especially the 2048 bytes logical block size.
6 * It depends on: libdvdread for ifo files and block reading.
7 *****************************************************************************
8 * Copyright (C) 2001 VideoLAN
9 * $Id: input.c,v 1.9 2002/11/13 20:23:21 fenrir 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>
40 #include "../../demux/mpeg/system.h"
47 #include <sys/types.h>
53 #ifdef STRNCASECMP_IN_STRINGS_H
58 # include <io.h> /* read() */
61 #include <dvdread/dvd_reader.h>
62 #include <dvdread/ifo_types.h>
63 #include <dvdread/ifo_read.h>
64 #include <dvdread/nav_read.h>
65 #include <dvdread/nav_print.h>
71 /* how many blocks DVDRead will read in each loop */
72 #define DVD_BLOCK_READ_ONCE 64
74 /*****************************************************************************
76 *****************************************************************************/
83 /*****************************************************************************
85 *****************************************************************************/
86 /* called from outside */
87 static int DvdReadDemux ( input_thread_t * );
88 static int DvdReadRewind ( input_thread_t * );
90 static int DvdReadSetArea ( input_thread_t *, input_area_t * );
91 static int DvdReadSetProgram ( input_thread_t *, pgrm_descriptor_t * );
92 static int DvdReadRead ( input_thread_t *, byte_t *, size_t );
93 static void DvdReadSeek ( input_thread_t *, off_t );
95 /* called only from here */
96 static void DvdReadLauchDecoders( input_thread_t * p_input );
97 static void DvdReadHandleDSI( thread_dvd_data_t * p_dvd, u8 * p_data );
98 static void DvdReadFindCell ( thread_dvd_data_t * p_dvd );
101 * Data demux functions
104 /*****************************************************************************
105 * InitDVD: initialize DVD structures
106 *****************************************************************************/
107 int E_(InitDVD) ( vlc_object_t *p_this )
109 input_thread_t *p_input = (input_thread_t *)p_this;
110 demux_sys_t * p_demux;
112 if( p_input->stream.i_method != INPUT_METHOD_DVD )
117 p_demux = p_input->p_demux_data = malloc( sizeof(demux_sys_t ) );
118 if( p_demux == NULL )
123 p_input->p_private = (void*)&p_demux->mpeg;
124 p_demux->p_module = module_Need( p_input, "mpeg-system", NULL );
125 if( p_demux->p_module == NULL )
127 free( p_input->p_demux_data );
131 p_input->pf_demux = DvdReadDemux;
132 p_input->pf_rewind = NULL;
134 vlc_mutex_lock( &p_input->stream.stream_lock );
136 DvdReadLauchDecoders( p_input );
138 vlc_mutex_unlock( &p_input->stream.stream_lock );
143 /*****************************************************************************
144 * EndDVD: end DVD structures
145 *****************************************************************************/
146 void E_(EndDVD) ( vlc_object_t *p_this )
148 input_thread_t *p_input = (input_thread_t *)p_this;
150 module_Unneed( p_input, p_input->p_demux_data->p_module );
151 free( p_input->p_demux_data );
154 /*****************************************************************************
156 *****************************************************************************/
157 #define PEEK( SIZE ) \
158 i_result = input_Peek( p_input, &p_peek, SIZE ); \
159 if( i_result == -1 ) \
163 else if( i_result < SIZE ) \
169 static int DvdReadDemux( input_thread_t * p_input )
173 data_packet_t * p_data;
178 /* Read headers to compute payload length */
179 for( i = 0 ; i < DVD_BLOCK_READ_ONCE ; i++ )
182 /* Read what we believe to be a packet header. */
186 if( U32_AT( p_peek ) != 0x1BA )
188 /* That's the case for all packets, except pack header. */
189 i_packet_size = U16_AT( p_peek + 4 );
193 /* MPEG-2 Pack header. */
197 /* Fetch a packet of the appropriate size. */
198 i_result = input_SplitBuffer( p_input, &p_data, i_packet_size + 6 );
204 /* In MPEG-2 pack headers we still have to read stuffing bytes. */
205 if( (p_data->p_demux_start[3] == 0xBA) && (i_packet_size == 8) )
207 size_t i_stuffing = (p_data->p_demux_start[13] & 0x7);
208 /* Force refill of the input buffer - though we don't care
209 * about p_peek. Please note that this is unoptimized. */
211 p_input->p_current_data += i_stuffing;
214 p_input->p_demux_data->mpeg.pf_demux_ps( p_input, p_data );
221 /*****************************************************************************
222 * DVDRewind : reads a stream backward
223 *****************************************************************************/
224 static int DvdReadRewind( input_thread_t * p_input )
230 * Data access functions
233 /*****************************************************************************
234 * OpenDVD: open libdvdread
235 *****************************************************************************/
236 int E_(OpenDVD) ( vlc_object_t *p_this )
238 input_thread_t * p_input = (input_thread_t *)p_this;
242 struct stat stat_info;
243 thread_dvd_data_t * p_dvd;
244 dvd_reader_t * p_dvdread;
245 input_area_t * p_area;
251 psz_parser = psz_source = strdup( p_input->psz_name );
257 p_input->pf_read = DvdReadRead;
258 p_input->pf_seek = DvdReadSeek;
259 p_input->pf_set_area = DvdReadSetArea;
260 p_input->pf_set_program = DvdReadSetProgram;
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;
292 if( !p_input->psz_access )
296 psz_source = config_GetPsz( p_input, "dvd" );
297 if( !psz_source ) return -1;
300 if( stat( psz_source, &stat_info ) == -1 )
302 msg_Err( p_input, "cannot stat() source `%s' (%s)",
303 psz_source, strerror(errno));
307 if( !S_ISBLK(stat_info.st_mode) &&
308 !S_ISCHR(stat_info.st_mode) &&
309 !S_ISDIR(stat_info.st_mode) )
311 msg_Warn( p_input, "dvdread module discarded (not a valid source)" );
316 msg_Dbg( p_input, "dvdroot=%s title=%d chapter=%d angle=%d",
317 psz_source, i_title, i_chapter, i_angle );
320 p_dvdread = DVDOpen( psz_source );
322 /* free allocated strings */
327 msg_Err( p_input, "libdvdcss cannot open source" );
334 p_dvd = malloc( sizeof(thread_dvd_data_t) );
337 msg_Err( p_input, "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 msg_Err( p_input, "cannot open VMG info" );
355 msg_Dbg( p_input, "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 msg_Dbg( p_input, "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 if( !p_input->psz_demux || !*p_input->psz_demux )
424 p_input->psz_demux = "dvdread";
430 /*****************************************************************************
431 * CloseDVD: close libdvdread
432 *****************************************************************************/
433 void E_(CloseDVD) ( vlc_object_t *p_this )
435 input_thread_t * p_input = (input_thread_t *)p_this;
436 thread_dvd_data_t * p_dvd = (thread_dvd_data_t *)p_input->p_access_data;
438 /* close libdvdread */
439 DVDCloseFile( p_dvd->p_title );
440 ifoClose( p_dvd->p_vts_file );
441 ifoClose( p_dvd->p_vmg_file );
443 DVDClose( p_dvd->p_dvdread );
445 p_input->p_access_data = NULL;
449 /*****************************************************************************
450 * DvdReadSetProgram: Does nothing, a DVD is mono-program
451 *****************************************************************************/
452 static int DvdReadSetProgram( input_thread_t * p_input,
453 pgrm_descriptor_t * p_program )
455 if( p_input->stream.p_selected_program != p_program )
457 thread_dvd_data_t * p_dvd;
459 p_dvd = (thread_dvd_data_t*)(p_input->p_access_data);
460 p_dvd->i_angle = p_program->i_number;
462 memcpy( p_program, p_input->stream.p_selected_program,
463 sizeof(pgrm_descriptor_t) );
464 p_program->i_number = p_dvd->i_angle;
465 p_input->stream.p_selected_program = p_program;
467 msg_Dbg( p_input, "angle %d selected", p_dvd->i_angle );
473 #define p_pgc p_dvd->p_cur_pgc
475 /*****************************************************************************
476 * DvdReadSetArea: initialize input data for title x, chapter y.
477 * It should be called for each user navigation request.
478 *****************************************************************************
479 * Take care that i_title starts from 0 (vmg) and i_chapter start from 1.
480 * Note that you have to take the lock before entering here.
481 *****************************************************************************/
482 static int DvdReadSetArea( input_thread_t * p_input, input_area_t * p_area )
484 thread_dvd_data_t * p_dvd;
488 p_dvd = (thread_dvd_data_t*)p_input->p_access_data;
490 /* we can't use the interface slider until initilization is complete */
491 p_input->stream.b_seekable = 0;
493 if( p_area != p_input->stream.p_selected_area )
495 es_descriptor_t * p_es;
501 #define p_vmg p_dvd->p_vmg_file
502 #define p_vts p_dvd->p_vts_file
503 if( p_dvd->p_title != NULL )
505 DVDCloseFile( p_dvd->p_title );
513 /* Reset the Chapter position of the old title */
514 p_input->stream.p_selected_area->i_part = 1;
517 * We have to load all title information
519 /* Change the default area */
520 p_input->stream.p_selected_area = p_area;
522 msg_Dbg( p_input, "open VTS %d, for title %d",
523 p_vmg->tt_srpt->title[ p_area->i_id - 1 ].title_set_nr,
527 if( ! ( p_vts = ifoOpen( p_dvd->p_dvdread,
528 p_vmg->tt_srpt->title[ p_area->i_id - 1 ].title_set_nr ) ) )
530 msg_Err( p_input, "fatal error in vts ifo" );
532 DVDClose( p_dvd->p_dvdread );
536 /* title position inside the selected vts */
537 p_dvd->i_ttn = p_vmg->tt_srpt->title[ p_area->i_id - 1 ].vts_ttn;
540 * Set selected title start
542 pgc_id = p_vts->vts_ptt_srpt->title[p_dvd->i_ttn-1].ptt[0].pgcn;
543 pgn = p_vts->vts_ptt_srpt->title[p_dvd->i_ttn-1].ptt[0].pgn;
544 p_pgc = p_vts->vts_pgcit->pgci_srp[ pgc_id - 1 ].pgc;
545 i_cell = p_pgc->program_map[ pgn - 1 ] - 1;
548 LB2OFF( p_dvd->p_cur_pgc->cell_playback[ i_cell ].first_sector );
550 msg_Dbg( p_input, "start %d vts_title %d pgc %d pgn %d",
551 p_area->i_id, p_dvd->i_ttn, pgc_id, pgn );
556 i_cell = p_dvd->p_cur_pgc->nr_of_cells - 1;
558 p_dvd->i_end_block = p_pgc->cell_playback[ i_cell ].last_sector;
559 p_area->i_size = LB2OFF( p_dvd->i_end_block )- p_area->i_start;
561 msg_Dbg( p_input, "start "I64Fd" size "I64Fd" end %d",
562 p_area->i_start , p_area->i_size, p_dvd->i_end_block );
565 * Set properties for current chapter
567 /* Remeber current chapter */
568 p_dvd->i_chapter = p_area->i_part;
571 pgc_id = p_vts->vts_ptt_srpt->title[
572 p_dvd->i_ttn-1].ptt[p_area->i_part-1].pgcn;
573 pgn = p_vts->vts_ptt_srpt->title[
574 p_dvd->i_ttn-1].ptt[p_area->i_part-1].pgn;
576 p_pgc = p_vts->vts_pgcit->pgci_srp[pgc_id-1].pgc;
577 p_dvd->i_pack_len = 0;
578 p_dvd->i_next_cell = p_dvd->i_cur_cell = p_pgc->program_map[pgn-1] - 1;
579 DvdReadFindCell( p_dvd );
581 p_dvd->i_next_vobu = p_dvd->i_cur_block =
582 p_pgc->cell_playback[p_dvd->i_cur_cell].first_sector;
587 p_dvd->i_angle_nb = p_vmg->tt_srpt->title[p_area->i_id-1].nr_of_angles;
589 if( p_dvd->i_angle > p_dvd->i_angle_nb )
595 * We've got enough info, time to open the title set data.
597 if( ! ( p_dvd->p_title = DVDOpenFile( p_dvd->p_dvdread,
598 p_vmg->tt_srpt->title[ p_area->i_id - 1 ].title_set_nr,
599 DVD_READ_TITLE_VOBS ) ) )
601 msg_Err( p_input, "cannot open title (VTS_%02d_1.VOB)",
602 p_vmg->tt_srpt->title[p_area->i_id-1].title_set_nr );
605 DVDClose( p_dvd->p_dvdread );
609 // IfoPrintTitle( p_dvd );
612 * Destroy obsolete ES by reinitializing program 0
613 * and find all ES in title with ifo data
615 if( p_input->stream.pp_programs != NULL )
617 /* We don't use input_EndStream here since
618 * we keep area structures */
620 while( p_input->stream.i_es_number )
622 input_DelES( p_input, p_input->stream.pp_es[0] );
625 while( p_input->stream.i_pgrm_number )
627 input_DelProgram( p_input, p_input->stream.pp_programs[0] );
630 if( p_input->stream.pp_selected_es )
632 free( p_input->stream.pp_selected_es );
633 p_input->stream.pp_selected_es = NULL;
635 p_input->stream.i_selected_es_number = 0;
638 input_AddProgram( p_input, 1, sizeof( stream_ps_data_t ) );
639 p_input->stream.p_selected_program = p_input->stream.pp_programs[0];
641 for( i = 1 ; i < p_dvd->i_angle_nb ; i++ )
643 input_AddProgram( p_input, i+1, 0 );
646 DvdReadSetProgram( p_input,
647 p_input->stream.pp_programs[p_dvd->i_angle-1] );
649 /* No PSM to read in DVD mode, we already have all information */
650 p_input->stream.p_selected_program->b_is_ok = 1;
654 /* ES 0 -> video MPEG2 */
655 // IfoPrintVideo( p_dvd );
657 p_es = input_AddES( p_input, NULL, 0xe0, 0 );
658 p_es->i_stream_id = 0xe0;
659 p_es->i_fourcc = VLC_FOURCC('m','p','g','v');
660 p_es->i_cat = VIDEO_ES;
662 #define audio_control \
663 p_dvd->p_vts_file->vts_pgcit->pgci_srp[pgc_id-1].pgc->audio_control[i-1]
664 /* Audio ES, in the order they appear in .ifo */
665 for( i = 1 ; i <= p_vts->vtsi_mat->nr_of_vts_audio_streams ; i++ )
670 // IfoPrintAudio( p_dvd, i );
672 /* audio channel is active if first byte is 0x80 */
673 if( audio_control & 0x8000 )
676 i_position = ( audio_control & 0x7F00 ) >> 8;
678 msg_Dbg( p_input, "audio position %d", i_position );
679 switch( p_vts->vtsi_mat->vts_audio_attr[i-1].audio_format )
682 i_id = ( ( 0x80 + i_position ) << 8 ) | 0xbd;
683 p_es = input_AddES( p_input, NULL, i_id, 0 );
684 p_es->i_stream_id = 0xbd;
685 p_es->i_fourcc = VLC_FOURCC('a','5','2','b');
686 p_es->i_cat = AUDIO_ES;
687 strcpy( p_es->psz_desc, DecodeLanguage(
688 p_vts->vtsi_mat->vts_audio_attr[i-1].lang_code ) );
689 strcat( p_es->psz_desc, " (A52)" );
693 case 0x03: /* MPEG audio */
694 i_id = 0xc0 + i_position;
695 p_es = input_AddES( p_input, NULL, i_id, 0 );
696 p_es->i_stream_id = i_id;
697 p_es->i_fourcc = VLC_FOURCC('m','p','g','a');
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_fourcc = VLC_FOURCC('l','p','c','b');
710 p_es->i_cat = AUDIO_ES;
711 strcpy( p_es->psz_desc, DecodeLanguage(
712 p_vts->vtsi_mat->vts_audio_attr[i-1].lang_code ) );
713 strcat( p_es->psz_desc, " (lpcm)" );
717 i_id = ( ( 0x88 + i_position ) << 8 ) | 0xbd;
718 msg_Err( p_input, "DTS audio not handled yet"
723 msg_Err( p_input, "unknown audio type %.2x",
724 p_vts->vtsi_mat->vts_audio_attr[i-1].audio_format );
729 #define spu_control \
730 p_dvd->p_vts_file->vts_pgcit->pgci_srp[pgc_id-1].pgc->subp_control[i-1]
734 for( i = 1 ; i <= p_vts->vtsi_mat->nr_of_vts_subp_streams; i++ )
739 // IfoPrintSpu( p_dvd, i );
740 msg_Dbg( p_input, "spu %d 0x%02x", i, spu_control );
742 if( spu_control & 0x80000000 )
746 /* there are several streams for one spu */
747 if( p_vts->vtsi_mat->vts_video_attr.display_aspect_ratio )
750 switch( p_vts->vtsi_mat->vts_video_attr.permitted_df )
753 i_position = spu_control & 0xff;
756 i_position = ( spu_control >> 8 ) & 0xff;
759 i_position = ( spu_control >> 16 ) & 0xff;
766 i_position = ( spu_control >> 24 ) & 0x7F;
769 i_id = ( ( 0x20 + i_position ) << 8 ) | 0xbd;
770 p_es = input_AddES( p_input, NULL, i_id, 0 );
771 p_es->i_stream_id = 0xbd;
772 p_es->i_fourcc = VLC_FOURCC('s','p','u','b');
773 p_es->i_cat = SPU_ES;
774 strcpy( p_es->psz_desc, DecodeLanguage(
775 p_vts->vtsi_mat->vts_subp_attr[i-1].lang_code ) );
780 /* FIXME: hack to check that the demuxer is ready, and set
782 if( p_input->p_demux )
784 DvdReadLauchDecoders( p_input );
790 p_area = p_input->stream.p_selected_area;
797 if( p_area->i_part != p_dvd->i_chapter )
799 if( ( p_area->i_part > 0 ) &&
800 ( p_area->i_part <= p_area->i_part_nb ))
802 p_dvd->i_ttn = p_vmg->tt_srpt->title[p_area->i_id-1].vts_ttn;
803 pgc_id = p_vts->vts_ptt_srpt->title[
804 p_dvd->i_ttn-1].ptt[p_area->i_part-1].pgcn;
805 pgn = p_vts->vts_ptt_srpt->title[
806 p_dvd->i_ttn-1].ptt[p_area->i_part-1].pgn;
808 p_pgc = p_vts->vts_pgcit->pgci_srp[ pgc_id - 1 ].pgc;
810 p_dvd->i_cur_cell = p_pgc->program_map[ pgn - 1 ] - 1;
811 p_dvd->i_chapter = p_area->i_part;
812 DvdReadFindCell( p_dvd );
814 p_dvd->i_pack_len = 0;
815 p_dvd->i_next_vobu = p_dvd->i_cur_block =
816 p_pgc->cell_playback[p_dvd->i_cur_cell].first_sector;
820 p_area->i_part = p_dvd->i_chapter;
826 /* warn interface that something has changed */
827 p_area->i_tell = LB2OFF( p_dvd->i_next_vobu ) - p_area->i_start;
828 p_input->stream.b_seekable = 1;
829 p_input->stream.b_changed = 1;
835 /*****************************************************************************
836 * DvdReadRead: reads data packets into the netlist.
837 *****************************************************************************
838 * Returns -1 in case of error, 0 if everything went well, and 1 in case of
840 *****************************************************************************/
841 static int DvdReadRead( input_thread_t * p_input,
842 byte_t * p_buffer, size_t i_count )
844 thread_dvd_data_t * p_dvd;
850 vlc_bool_t b_eot = 0;
852 p_dvd = (thread_dvd_data_t *)p_input->p_access_data;
856 * Playback by cell in this pgc, starting at the cell for our chapter.
858 i_blocks = OFF2LB( i_count );
865 * End of pack, we select the following one
867 if( ! p_dvd->i_pack_len )
872 if( ( i_read = DVDReadBlocks( p_dvd->p_title, p_dvd->i_next_vobu,
875 msg_Err( p_input, "read failed for block %d",
876 p_dvd->i_next_vobu );
880 /* basic check to be sure we don't have a empty title
881 * go to next title if so */
882 //assert( p_buffer[41] == 0xbf && p_buffer[1027] == 0xbf );
885 * Parse the contained dsi packet.
888 DvdReadHandleDSI( p_dvd, p_buf );
891 if( p_dvd->i_next_vobu >= p_dvd->i_end_block + 1 )
896 assert( p_dvd->i_pack_len < 1024 );
897 /* FIXME: Ugly kludge: we send the pack block to the input for it
898 * sometimes has a zero scr and restart the sync */
899 p_dvd->i_cur_block ++;
900 //p_dvd->i_pack_len++;
903 p_buf += DVD_VIDEO_LB_LEN;
908 * Compute the number of blocks to read
910 i_blocks_once = p_dvd->i_pack_len >= i_blocks
911 ? i_blocks : p_dvd->i_pack_len;
912 p_dvd->i_pack_len -= i_blocks_once;
915 i_read = DVDReadBlocks( p_dvd->p_title, p_dvd->i_cur_block,
916 i_blocks_once, p_buf );
917 if( i_read != i_blocks_once )
919 msg_Err( p_input, "read failed for %d/%d blocks at 0x%02x",
920 i_read, i_blocks_once, p_dvd->i_cur_block );
925 i_read_total += i_read;
926 p_dvd->i_cur_block += i_read;
927 p_buf += LB2OFF( i_read );
931 msg_Dbg( p_input, "i_blocks: %d len: %d current: 0x%02x", i_read, p_dvd->i_pack_len, p_dvd->i_cur_block );
934 vlc_mutex_lock( &p_input->stream.stream_lock );
938 /* We modify i_part only at end of chapter not to erase
939 * some modification from the interface */
940 p_input->stream.p_selected_area->i_part = p_dvd->i_chapter;
944 if( ( LB2OFF( p_dvd->i_cur_block )
945 - p_input->stream.p_selected_area->i_start )
946 >= p_input->stream.p_selected_area->i_size || b_eot )
948 if( ( p_input->stream.p_selected_area->i_id + 1 ) >=
949 p_input->stream.i_area_nb )
952 vlc_mutex_unlock( &p_input->stream.stream_lock );
957 msg_Dbg( p_input, "new title" );
958 DvdReadSetArea( p_input, p_input->stream.pp_areas[
959 p_input->stream.p_selected_area->i_id+1] );
960 vlc_mutex_unlock( &p_input->stream.stream_lock );
964 vlc_mutex_unlock( &p_input->stream.stream_lock );
966 return LB2OFF( i_read_total );
970 /*****************************************************************************
971 * DvdReadSeek : Goes to a given position on the stream.
972 *****************************************************************************
973 * This one is used by the input and translate chronological position from
974 * input to logical position on the device.
975 * The lock should be taken before calling this function.
976 *****************************************************************************/
977 static void DvdReadSeek( input_thread_t * p_input, off_t i_off )
979 thread_dvd_data_t * p_dvd;
987 vlc_mutex_lock( &p_input->stream.stream_lock );
988 i_off += p_input->stream.p_selected_area->i_start;
989 vlc_mutex_unlock( &p_input->stream.stream_lock );
991 i_lb = OFF2LB( i_off );
992 p_dvd = ( thread_dvd_data_t * )p_input->p_access_data;
995 while( p_dvd->p_cur_pgc->cell_playback[i_cell].last_sector < i_lb )
1007 pgc_id = p_dvd->p_vts_file->vts_ptt_srpt->title[
1008 p_dvd->i_ttn-1].ptt[i_chapter-1].pgcn;
1009 pgn = p_dvd->p_vts_file->vts_ptt_srpt->title[
1010 p_dvd->i_ttn-1].ptt[i_chapter-1].pgn;
1012 p_pgc = p_dvd->p_vts_file->vts_pgcit->pgci_srp[pgc_id-1].pgc;
1013 i_tmp = p_pgc->program_map[pgn-1];
1015 } while( i_tmp <= i_cell );
1018 while( p_dvd->p_vts_file->vts_vobu_admap->vobu_start_sectors[i_vobu]
1025 while( p_dvd->p_vts_file->vts_c_adt->cell_adr_table[i_sub_cell].start_sector <
1026 p_dvd->p_vts_file->vts_vobu_admap->vobu_start_sectors[i_vobu-1] )
1032 msg_Dbg( p_input, "cell %d i_sub_cell %d chapter %d vobu %d cell_sector %d vobu_sector %d sub_cell_sector %d",
1033 i_cell, i_sub_cell,i_chapter, i_vobu,
1034 p_dvd->p_cur_pgc->cell_playback[i_cell].first_sector,
1035 p_dvd->p_vts_file->vts_vobu_admap->vobu_start_sectors[i_vobu],
1036 p_dvd->p_vts_file->vts_c_adt->cell_adr_table[i_sub_cell-1].start_sector);
1038 p_dvd->i_cur_block = i_lb;
1039 p_dvd->i_next_vobu =
1040 p_dvd->p_vts_file->vts_vobu_admap->vobu_start_sectors[i_vobu];
1041 p_dvd->i_pack_len = p_dvd->i_next_vobu - i_lb;
1042 p_dvd->i_cur_cell = i_cell;
1043 p_dvd->i_chapter = i_chapter;
1044 DvdReadFindCell( p_dvd );
1046 vlc_mutex_lock( &p_input->stream.stream_lock );
1047 p_input->stream.p_selected_area->i_tell =
1048 LB2OFF ( p_dvd->i_cur_block )
1049 - p_input->stream.p_selected_area->i_start;
1050 p_input->stream.p_selected_area->i_part = p_dvd->i_chapter;
1051 vlc_mutex_unlock( &p_input->stream.stream_lock );
1056 /*****************************************************************************
1058 *****************************************************************************/
1059 static void DvdReadHandleDSI( thread_dvd_data_t * p_dvd, u8 * p_data )
1061 navRead_DSI( &(p_dvd->dsi_pack), &(p_data[ DSI_START_BYTE ]) );
1064 * Determine where we go next. These values are the ones we mostly
1067 p_dvd->i_cur_block = p_dvd->dsi_pack.dsi_gi.nv_pck_lbn;
1070 * If we're not at the end of this cell, we can determine the next
1071 * VOBU to display using the VOBU_SRI information section of the
1072 * DSI. Using this value correctly follows the current angle,
1073 * avoiding the doubled scenes in The Matrix, and makes our life
1076 if( p_dvd->dsi_pack.vobu_sri.next_vobu != SRI_END_OF_CELL )
1079 switch( ( p_dvd->dsi_pack.sml_pbi.category & 0xf000 ) >> 12 )
1082 /* interleaved unit with no angle */
1083 if( p_dvd->dsi_pack.sml_pbi.ilvu_sa != -1 )
1085 p_dvd->i_next_vobu = p_dvd->i_cur_block +
1086 p_dvd->dsi_pack.sml_pbi.ilvu_sa;
1087 p_dvd->i_pack_len = p_dvd->dsi_pack.sml_pbi.ilvu_ea;
1091 p_dvd->i_next_vobu = p_dvd->i_cur_block +
1092 p_dvd->dsi_pack.dsi_gi.vobu_ea + 1;
1093 p_dvd->i_pack_len = p_dvd->dsi_pack.dsi_gi.vobu_ea;
1097 /* vobu is end of ilvu */
1098 if( p_dvd->dsi_pack.sml_agli.data[p_dvd->i_angle-1].address )
1100 p_dvd->i_next_vobu = p_dvd->i_cur_block +
1101 p_dvd->dsi_pack.sml_agli.data[p_dvd->i_angle-1].address;
1102 p_dvd->i_pack_len = p_dvd->dsi_pack.sml_pbi.ilvu_ea;
1107 /* vobu is beginning of ilvu */
1111 /* entering interleaved section */
1113 /* non interleaved cells in interleaved section */
1115 p_dvd->i_next_vobu = p_dvd->i_cur_block +
1116 ( p_dvd->dsi_pack.vobu_sri.next_vobu & 0x7fffffff );
1117 p_dvd->i_pack_len = p_dvd->dsi_pack.dsi_gi.vobu_ea;
1121 p_dvd->i_next_vobu = p_dvd->i_cur_block +
1122 ( p_dvd->dsi_pack.vobu_sri.next_vobu & 0x7fffffff );
1123 p_dvd->i_pack_len = p_dvd->dsi_pack.dsi_gi.vobu_ea;
1128 p_dvd->i_cur_cell = p_dvd->i_next_cell;
1129 DvdReadFindCell( p_dvd );
1131 p_dvd->i_pack_len = p_dvd->dsi_pack.dsi_gi.vobu_ea;
1132 p_dvd->i_next_vobu =
1133 p_dvd->p_cur_pgc->cell_playback[p_dvd->i_cur_cell].first_sector;
1137 msg_Dbg( p_input, 12, "scr %d lbn 0x%02x vobu_ea %d vob_id %d c_id %d",
1138 p_dvd->dsi_pack.dsi_gi.nv_pck_scr,
1139 p_dvd->dsi_pack.dsi_gi.nv_pck_lbn,
1140 p_dvd->dsi_pack.dsi_gi.vobu_ea,
1141 p_dvd->dsi_pack.dsi_gi.vobu_vob_idn,
1142 p_dvd->dsi_pack.dsi_gi.vobu_c_idn );
1144 msg_Dbg( p_input, 12, "cat 0x%02x ilvu_ea %d ilvu_sa %d size %d",
1145 p_dvd->dsi_pack.sml_pbi.category,
1146 p_dvd->dsi_pack.sml_pbi.ilvu_ea,
1147 p_dvd->dsi_pack.sml_pbi.ilvu_sa,
1148 p_dvd->dsi_pack.sml_pbi.size );
1150 msg_Dbg( p_input, 12, "next_vobu %d next_ilvu1 %d next_ilvu2 %d",
1151 p_dvd->dsi_pack.vobu_sri.next_vobu & 0x7fffffff,
1152 p_dvd->dsi_pack.sml_agli.data[ p_dvd->i_angle - 1 ].address,
1153 p_dvd->dsi_pack.sml_agli.data[ p_dvd->i_angle ].address);
1157 /*****************************************************************************
1159 *****************************************************************************/
1160 static void DvdReadFindCell( thread_dvd_data_t * p_dvd )
1165 #define cell p_dvd->p_cur_pgc->cell_playback
1166 if( cell[p_dvd->i_cur_cell].block_type == BLOCK_TYPE_ANGLE_BLOCK )
1169 p_dvd->i_next_cell = p_dvd->i_cur_cell + p_dvd->i_angle_nb;
1170 p_dvd->i_cur_cell += p_dvd->i_angle - 1;
1172 p_dvd->i_cur_cell += p_dvd->i_angle - 1;
1174 while( cell[p_dvd->i_cur_cell+i].block_mode != BLOCK_MODE_LAST_CELL )
1178 p_dvd->i_next_cell = p_dvd->i_cur_cell + i + 1;
1183 p_dvd->i_next_cell = p_dvd->i_cur_cell + 1;
1186 pgc_id = p_dvd->p_vts_file->vts_ptt_srpt->title[
1187 p_dvd->i_ttn-1].ptt[p_dvd->i_chapter-1].pgcn;
1188 pgn = p_dvd->p_vts_file->vts_ptt_srpt->title[
1189 p_dvd->i_ttn-1].ptt[p_dvd->i_chapter-1].pgn;
1190 p_pgc = p_dvd->p_vts_file->vts_pgcit->pgci_srp[pgc_id-1].pgc;
1192 if( p_pgc->program_map[pgn-1] <= p_dvd->i_cur_cell )
1199 /*****************************************************************************
1200 * DvdReadLaunchDecoders
1201 *****************************************************************************/
1202 static void DvdReadLauchDecoders( input_thread_t * p_input )
1204 thread_dvd_data_t * p_dvd = (thread_dvd_data_t*)(p_input->p_access_data);
1207 input_SelectES( p_input, p_input->stream.pp_es[0] );
1209 /* For audio: first one if none or a not existing one specified */
1210 i_audio = config_GetInt( p_input, "audio-channel" );
1211 if( i_audio < 0 /*|| i_audio > i_audio_nb*/ )
1213 config_PutInt( p_input, "audio-channel", 1 );
1216 if( i_audio > 0/* && i_audio_nb > 0*/ )
1218 if( config_GetInt( p_input, "audio-type" ) == REQUESTED_A52 )
1220 int i_a52 = i_audio;
1221 while( ( p_input->stream.pp_es[i_a52]->i_fourcc !=
1222 VLC_FOURCC('a','5','2','b') ) && ( i_a52 <=
1223 p_dvd->p_vts_file->vtsi_mat->nr_of_vts_audio_streams ) )
1227 if( p_input->stream.pp_es[i_a52]->i_fourcc
1228 == VLC_FOURCC('a','5','2','b') )
1230 input_SelectES( p_input, p_input->stream.pp_es[i_a52] );
1235 input_SelectES( p_input, p_input->stream.pp_es[i_audio] );
1239 /* for spu, default is none */
1240 i_spu = config_GetInt( p_input, "spu-channel" );
1241 if( i_spu < 0 /*|| i_spu > i_spu_nb*/ )
1243 config_PutInt( p_input, "spu-channel", 0 );
1246 if( i_spu > 0 /*&& i_spu_nb > 0*/ )
1248 i_spu += p_dvd->p_vts_file->vtsi_mat->nr_of_vts_audio_streams;
1249 input_SelectES( p_input, p_input->stream.pp_es[i_spu] );