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.22 2002/03/01 01:12:28 stef Exp $
11 * Author: Stéphane Borel <stef@via.ecp.fr>
13 * Some code taken form the play_title.c by Billy Biggs <vektor@dumbterm.net>
16 * This program is free software; you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License as published by
18 * the Free Software Foundation; either version 2 of the License, or
19 * (at your option) any later version.
21 * This program is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU General Public License for more details.
26 * You should have received a copy of the GNU General Public License
27 * along with this program; if not, write to the Free Software
28 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
29 *****************************************************************************/
31 /*****************************************************************************
33 *****************************************************************************/
37 #include <videolan/vlc.h>
44 #include <sys/types.h>
49 #ifdef STRNCASECMP_IN_STRINGS_H
54 # include <io.h> /* read() */
56 # include <sys/uio.h> /* struct iovec */
60 # include "input_iovec.h"
63 #include "stream_control.h"
64 #include "input_ext-intf.h"
65 #include "input_ext-dec.h"
66 #include "input_ext-plugins.h"
68 #include "input_dvdread.h"
74 /* how many blocks DVDRead will read in each loop */
75 #define DVD_BLOCK_READ_ONCE 64
77 /*****************************************************************************
79 *****************************************************************************/
80 /* called from outside */
81 static int DvdReadInit ( struct input_thread_s * );
82 static void DvdReadEnd ( struct input_thread_s * );
83 static int DvdReadDemux ( struct input_thread_s * );
84 static int DvdReadRewind ( struct input_thread_s * );
86 static int DvdReadOpen ( struct input_thread_s * );
87 static void DvdReadClose ( struct input_thread_s * );
88 static int DvdReadSetArea ( struct input_thread_s *, struct input_area_s * );
89 static int DvdReadSetProgram( struct input_thread_s *, pgrm_descriptor_t * );
90 static int DvdReadRead ( struct input_thread_s *, byte_t *, size_t );
91 static void DvdReadSeek ( struct input_thread_s *, off_t );
93 /* called only from here */
94 static void DvdReadHandleDSI( thread_dvd_data_t * p_dvd, u8 * p_data );
95 static void DvdReadFindCell ( thread_dvd_data_t * p_dvd );
97 /*****************************************************************************
98 * Functions exported as capabilities. They are declared as static so that
99 * we don't pollute the namespace too much.
100 *****************************************************************************/
101 void _M( access_getfunctions )( function_list_t * p_function_list )
103 #define access p_function_list->functions.access
104 access.pf_open = DvdReadOpen;
105 access.pf_close = DvdReadClose;
106 access.pf_read = DvdReadRead;
107 access.pf_set_area = DvdReadSetArea;
108 access.pf_set_program = DvdReadSetProgram;
109 access.pf_seek = DvdReadSeek;
113 void _M( demux_getfunctions )( function_list_t * p_function_list )
115 #define demux p_function_list->functions.demux
116 demux.pf_init = DvdReadInit;
117 demux.pf_end = DvdReadEnd;
118 demux.pf_demux = DvdReadDemux;
119 demux.pf_rewind = DvdReadRewind;
124 * Data demux functions
127 /*****************************************************************************
128 * DvdReadInit: initializes DVD structures
129 *****************************************************************************/
130 static int DvdReadInit( input_thread_t * p_input )
132 thread_dvd_data_t * p_dvd;
134 if( strncmp( p_input->p_access_module->psz_name, "dvdread", 7 ) )
139 p_dvd = (thread_dvd_data_t*)(p_input->p_access_data);
141 if( p_main->b_video )
143 input_SelectES( p_input, p_input->stream.pp_es[0] );
146 if( p_main->b_audio )
148 /* For audio: first one if none or a not existing one specified */
149 int i_audio = config_GetIntVariable( INPUT_CHANNEL_VAR );
150 if( i_audio < 0 /*|| i_audio > i_audio_nb*/ )
152 config_PutIntVariable( INPUT_CHANNEL_VAR, 1 );
155 if( i_audio > 0/* && i_audio_nb > 0*/ )
157 if( config_GetIntVariable( AOUT_SPDIF_VAR ) ||
158 ( config_GetIntVariable( INPUT_AUDIO_VAR ) ==
162 while( ( p_input->stream.pp_es[i_ac3]->i_type !=
163 AC3_AUDIO_ES ) && ( i_ac3 <=
164 p_dvd->p_vts_file->vtsi_mat->nr_of_vts_audio_streams ) )
168 if( p_input->stream.pp_es[i_ac3]->i_type == AC3_AUDIO_ES )
170 input_SelectES( p_input,
171 p_input->stream.pp_es[i_ac3] );
176 input_SelectES( p_input,
177 p_input->stream.pp_es[i_audio] );
182 if( p_main->b_video )
184 /* for spu, default is none */
185 int i_spu = config_GetIntVariable( INPUT_SUBTITLE_VAR );
186 if( i_spu < 0 /*|| i_spu > i_spu_nb*/ )
188 config_PutIntVariable( INPUT_SUBTITLE_VAR, 0 );
191 if( i_spu > 0 /*&& i_spu_nb > 0*/ )
193 i_spu += p_dvd->p_vts_file->vtsi_mat->nr_of_vts_audio_streams;
194 input_SelectES( p_input, p_input->stream.pp_es[i_spu] );
201 /*****************************************************************************
202 * DvdReadEnd: frees unused data
203 *****************************************************************************/
204 static void DvdReadEnd( input_thread_t * p_input )
208 /*****************************************************************************
210 *****************************************************************************/
211 #define PEEK( SIZE ) \
212 i_result = input_Peek( p_input, &p_peek, SIZE ); \
213 if( i_result == -1 ) \
217 else if( i_result < SIZE ) \
223 static int DvdReadDemux( input_thread_t * p_input )
227 data_packet_t * p_data;
232 /* Read headers to compute payload length */
233 for( i = 0 ; i < DVD_BLOCK_READ_ONCE ; i++ )
236 /* Read what we believe to be a packet header. */
240 if( U32_AT( p_peek ) != 0x1BA )
242 /* That's the case for all packets, except pack header. */
243 i_packet_size = U16_AT( p_peek + 4 );
247 /* MPEG-2 Pack header. */
251 /* Fetch a packet of the appropriate size. */
252 i_result = input_SplitBuffer( p_input, &p_data, i_packet_size + 6 );
258 /* In MPEG-2 pack headers we still have to read stuffing bytes. */
259 if( (p_data->p_demux_start[3] == 0xBA) && (i_packet_size == 8) )
261 size_t i_stuffing = (p_data->p_demux_start[13] & 0x7);
262 /* Force refill of the input buffer - though we don't care
263 * about p_peek. Please note that this is unoptimized. */
265 p_input->p_current_data += i_stuffing;
268 input_DemuxPS( p_input, p_data );
275 /*****************************************************************************
276 * DVDRewind : reads a stream backward
277 *****************************************************************************/
278 static int DvdReadRewind( input_thread_t * p_input )
284 * Data access functions
287 /*****************************************************************************
288 * DvdReadOpen: open libdvdread
289 *****************************************************************************/
290 static int DvdReadOpen( struct input_thread_s *p_input )
292 struct stat stat_info;
293 thread_dvd_data_t * p_dvd;
294 dvd_reader_t * p_dvdread;
295 input_area_t * p_area;
300 if( stat( p_input->psz_name, &stat_info ) == -1 )
302 intf_ErrMsg( "input error: cannot stat() device `%s' (%s)",
303 p_input->psz_name, strerror(errno));
306 if( !S_ISBLK(stat_info.st_mode) &&
307 !S_ISCHR(stat_info.st_mode) &&
308 !S_ISDIR(stat_info.st_mode) )
310 intf_WarnMsg( 3, "input : DvdRead plugin discarded"
311 " (not a valid source)" );
315 p_dvdread = DVDOpen( p_input->psz_name );
318 intf_ErrMsg( "dvdread error: libdvdcss can't open source" );
325 p_dvd = malloc( sizeof(thread_dvd_data_t) );
328 intf_ErrMsg( "dvdread error: out of memory" );
332 p_dvd->p_dvdread = p_dvdread;
333 p_dvd->p_title = NULL;
334 p_dvd->p_vts_file = NULL;
336 p_input->p_access_data = (void *)p_dvd;
338 /* Ifo allocation & initialisation */
339 if( ! ( p_dvd->p_vmg_file = ifoOpen( p_dvd->p_dvdread, 0 ) ) )
341 intf_ErrMsg( "dvdread error: can't open VMG info" );
345 intf_WarnMsg( 2, "dvdread info: VMG opened" );
347 /* Set stream and area data */
348 vlc_mutex_lock( &p_input->stream.stream_lock );
350 p_input->stream.i_method = INPUT_METHOD_DVD;
352 /* If we are here we can control the pace... */
353 p_input->stream.b_pace_control = 1;
354 p_input->stream.b_seekable = 1;
356 p_input->stream.p_selected_area->i_size = 0;
357 p_input->stream.p_selected_area->i_tell = 0;
359 /* Initialize ES structures */
360 input_InitStream( p_input, sizeof( stream_ps_data_t ) );
362 /* disc input method */
363 p_input->stream.i_method = INPUT_METHOD_DVD;
365 #define tt_srpt p_dvd->p_vmg_file->tt_srpt
366 intf_WarnMsg( 2, "dvdread info: number of titles: %d", tt_srpt->nr_of_srpts );
368 #define area p_input->stream.pp_areas
369 /* We start from 1 here since the default area 0
370 * is reserved for video_ts.vob */
371 for( i = 1 ; i <= tt_srpt->nr_of_srpts ; i++ )
373 input_AddArea( p_input );
375 /* Titles are Program Chains */
378 /* Absolute start offset and size
379 * We can only set that with vts ifo, so we do it during the
380 * first call to DVDSetArea */
381 area[i]->i_start = 0;
384 /* Number of chapters */
385 area[i]->i_part_nb = tt_srpt->title[i-1].nr_of_ptts;
388 /* Number of angles */
389 area[i]->i_angle_nb = 0;
390 area[i]->i_angle = 1;
392 area[i]->i_plugin_data = tt_srpt->title[i-1].title_set_nr;
396 /* Get requested title - if none try the first title */
397 i_title = config_GetIntVariable( INPUT_TITLE_VAR );
398 if( i_title <= 0 || i_title > tt_srpt->nr_of_srpts )
405 /* Get requested chapter - if none defaults to first one */
406 i_chapter = config_GetIntVariable( INPUT_CHAPTER_VAR );
412 p_input->stream.pp_areas[i_title]->i_part = i_chapter;
414 p_area = p_input->stream.pp_areas[i_title];
416 /* set title, chapter, audio and subpic */
417 if( DvdReadSetArea( p_input, p_area ) )
419 vlc_mutex_unlock( &p_input->stream.stream_lock );
423 vlc_mutex_unlock( &p_input->stream.stream_lock );
428 /*****************************************************************************
429 * DvdReadClose: close libdvdread
430 *****************************************************************************/
431 static void DvdReadClose( struct input_thread_s *p_input )
433 thread_dvd_data_t * p_dvd;
435 p_dvd = (thread_dvd_data_t *)p_input->p_access_data;
437 /* close libdvdread */
438 DVDCloseFile( p_dvd->p_title );
439 ifoClose( p_dvd->p_vts_file );
440 ifoClose( p_dvd->p_vmg_file );
442 DVDClose( p_dvd->p_dvdread );
444 p_input->p_access_data = NULL;
448 /*****************************************************************************
449 * DvdReadSetProgram: Does nothing, a DVD is mono-program
450 *****************************************************************************/
451 static int DvdReadSetProgram( input_thread_t * p_input,
452 pgrm_descriptor_t * p_program )
457 #define p_pgc p_dvd->p_cur_pgc
459 /*****************************************************************************
460 * DvdReadSetArea: initialize input data for title x, chapter y.
461 * It should be called for each user navigation request.
462 *****************************************************************************
463 * Take care that i_title starts from 0 (vmg) and i_chapter start from 1.
464 * Note that you have to take the lock before entering here.
465 *****************************************************************************/
466 static int DvdReadSetArea( input_thread_t * p_input, input_area_t * p_area )
468 thread_dvd_data_t * p_dvd;
472 p_dvd = (thread_dvd_data_t*)p_input->p_access_data;
474 /* we can't use the interface slider until initilization is complete */
475 p_input->stream.b_seekable = 0;
477 if( p_area != p_input->stream.p_selected_area )
479 es_descriptor_t * p_es;
485 #define p_vmg p_dvd->p_vmg_file
486 #define p_vts p_dvd->p_vts_file
487 if( p_dvd->p_title != NULL )
489 DVDCloseFile( p_dvd->p_title );
497 /* Reset the Chapter position of the old title */
498 p_input->stream.p_selected_area->i_part = 1;
501 * We have to load all title information
503 /* Change the default area */
504 p_input->stream.p_selected_area = p_area;
506 intf_WarnMsg( 12, "dvdread: open VTS %d, for title %d",
507 p_vmg->tt_srpt->title[ p_area->i_id - 1 ].title_set_nr,
511 if( ! ( p_vts = ifoOpen( p_dvd->p_dvdread,
512 p_vmg->tt_srpt->title[ p_area->i_id - 1 ].title_set_nr ) ) )
514 intf_ErrMsg( "dvdread error: fatal error in vts ifo" );
516 DVDClose( p_dvd->p_dvdread );
520 /* title position inside the selected vts */
521 p_dvd->i_ttn = p_vmg->tt_srpt->title[ p_area->i_id - 1 ].vts_ttn;
524 * Set selected title start
526 pgc_id = p_vts->vts_ptt_srpt->title[p_dvd->i_ttn-1].ptt[0].pgcn;
527 pgn = p_vts->vts_ptt_srpt->title[p_dvd->i_ttn-1].ptt[0].pgn;
528 p_pgc = p_vts->vts_pgcit->pgci_srp[ pgc_id - 1 ].pgc;
529 i_cell = p_pgc->program_map[ pgn - 1 ] - 1;
532 LB2OFF( p_dvd->p_cur_pgc->cell_playback[ i_cell ].first_sector );
534 intf_WarnMsg( 3, "dvdread: start %d vts_title %d pgc %d pgn %d",
535 p_area->i_id, p_dvd->i_ttn, pgc_id, pgn );
540 i_cell = p_dvd->p_cur_pgc->nr_of_cells - 1;
542 p_dvd->i_end_block = p_pgc->cell_playback[ i_cell ].last_sector;
543 p_area->i_size = LB2OFF( p_dvd->i_end_block )- p_area->i_start;
545 intf_WarnMsg( 12, "dvdread: start %lld size %lld end %d",
546 p_area->i_start , p_area->i_size, p_dvd->i_end_block );
549 * Set properties for current chapter
551 /* Remeber current chapter */
552 p_dvd->i_chapter = p_area->i_part;
555 pgc_id = p_vts->vts_ptt_srpt->title[
556 p_dvd->i_ttn-1].ptt[p_area->i_part-1].pgcn;
557 pgn = p_vts->vts_ptt_srpt->title[
558 p_dvd->i_ttn-1].ptt[p_area->i_part-1].pgn;
560 p_pgc = p_vts->vts_pgcit->pgci_srp[pgc_id-1].pgc;
561 p_dvd->i_pack_len = 0;
562 p_dvd->i_next_cell = p_dvd->i_cur_cell = p_pgc->program_map[pgn-1] - 1;
563 DvdReadFindCell( p_dvd );
565 p_dvd->i_next_vobu = p_dvd->i_cur_block =
566 p_pgc->cell_playback[p_dvd->i_cur_cell].first_sector;
571 p_area->i_angle_nb = p_vmg->tt_srpt->title[p_area->i_id-1].nr_of_angles;
572 p_area->i_angle = config_GetIntVariable( INPUT_ANGLE_VAR );
574 if( ( p_area->i_angle <= 0 ) || p_area->i_angle > p_area->i_angle_nb )
578 p_dvd->i_angle = p_area->i_angle;
579 p_dvd->i_angle_nb = p_area->i_angle_nb;
582 * We've got enough info, time to open the title set data.
584 if( ! ( p_dvd->p_title = DVDOpenFile( p_dvd->p_dvdread,
585 p_vmg->tt_srpt->title[ p_area->i_id - 1 ].title_set_nr,
586 DVD_READ_TITLE_VOBS ) ) )
588 intf_ErrMsg( "dvdread error: can't open title (VTS_%02d_1.VOB)",
589 p_vmg->tt_srpt->title[p_area->i_id-1].title_set_nr );
592 DVDClose( p_dvd->p_dvdread );
596 // IfoPrintTitle( p_dvd );
599 * Destroy obsolete ES by reinitializing program 0
600 * and find all ES in title with ifo data
602 if( p_input->stream.pp_programs != NULL )
604 /* We don't use input_EndStream here since
605 * we keep area structures */
607 for( i = 0 ; i < p_input->stream.i_selected_es_number ; i++ )
609 input_UnselectES( p_input, p_input->stream.pp_selected_es[i] );
612 free( p_input->stream.pp_selected_es );
613 input_DelProgram( p_input, p_input->stream.p_selected_program );
615 p_input->stream.pp_selected_es = NULL;
616 p_input->stream.i_selected_es_number = 0;
619 input_AddProgram( p_input, 0, sizeof( stream_ps_data_t ) );
620 p_input->stream.p_selected_program = p_input->stream.pp_programs[0];
622 /* No PSM to read in DVD mode, we already have all information */
623 p_input->stream.p_selected_program->b_is_ok = 1;
627 /* ES 0 -> video MPEG2 */
628 // IfoPrintVideo( p_dvd );
630 p_es = input_AddES( p_input, p_input->stream.p_selected_program, 0xe0, 0 );
631 p_es->i_stream_id = 0xe0;
632 p_es->i_type = MPEG2_VIDEO_ES;
633 p_es->i_cat = VIDEO_ES;
635 #define audio_control \
636 p_dvd->p_vts_file->vts_pgcit->pgci_srp[pgc_id-1].pgc->audio_control[i-1]
637 /* Audio ES, in the order they appear in .ifo */
638 for( i = 1 ; i <= p_vts->vtsi_mat->nr_of_vts_audio_streams ; i++ )
643 // IfoPrintAudio( p_dvd, i );
645 /* audio channel is active if first byte is 0x80 */
646 if( audio_control & 0x8000 )
649 i_position = ( audio_control & 0x7F00 ) >> 8;
651 intf_WarnMsg( 12, "dvd audio position %d", i_position );
652 switch( p_vts->vtsi_mat->vts_audio_attr[i-1].audio_format )
655 i_id = ( ( 0x80 + i_position ) << 8 ) | 0xbd;
656 p_es = input_AddES( p_input,
657 p_input->stream.p_selected_program, i_id, 0 );
658 p_es->i_stream_id = 0xbd;
659 p_es->i_type = AC3_AUDIO_ES;
661 p_es->i_cat = AUDIO_ES;
662 strcpy( p_es->psz_desc, DecodeLanguage( hton16(
663 p_vts->vtsi_mat->vts_audio_attr[i-1].lang_code ) ) );
664 strcat( p_es->psz_desc, " (ac3)" );
668 case 0x03: /* MPEG audio */
669 i_id = 0xc0 + i_position;
670 p_es = input_AddES( p_input,
671 p_input->stream.p_selected_program, i_id, 0 );
672 p_es->i_stream_id = i_id;
673 p_es->i_type = MPEG2_AUDIO_ES;
675 p_es->i_cat = AUDIO_ES;
676 strcpy( p_es->psz_desc, DecodeLanguage( hton16(
677 p_vts->vtsi_mat->vts_audio_attr[i-1].lang_code ) ) );
678 strcat( p_es->psz_desc, " (mpeg)" );
681 case 0x04: /* LPCM */
683 i_id = ( ( 0xa0 + i_position ) << 8 ) | 0xbd;
684 p_es = input_AddES( p_input,
685 p_input->stream.p_selected_program, i_id, 0 );
686 p_es->i_stream_id = i_id;
687 p_es->i_type = LPCM_AUDIO_ES;
689 p_es->i_cat = AUDIO_ES;
690 strcpy( p_es->psz_desc, DecodeLanguage( hton16(
691 p_vts->vtsi_mat->vts_audio_attr[i-1].lang_code ) ) );
692 strcat( p_es->psz_desc, " (lpcm)" );
696 i_id = ( ( 0x88 + i_position ) << 8 ) | 0xbd;
697 intf_ErrMsg( "dvd warning: DTS audio not handled yet"
702 intf_ErrMsg( "dvd warning: unknown audio type %.2x",
703 p_vts->vtsi_mat->vts_audio_attr[i-1].audio_format );
708 #define spu_control \
709 p_dvd->p_vts_file->vts_pgcit->pgci_srp[pgc_id-1].pgc->subp_control[i-1]
713 for( i = 1 ; i <= p_vts->vtsi_mat->nr_of_vts_subp_streams; i++ )
718 // IfoPrintSpu( p_dvd, i );
719 intf_WarnMsg( 12, "dvd spu %d 0x%02x", i, spu_control );
721 if( spu_control & 0x80000000 )
725 /* there are several streams for one spu */
726 if( p_vts->vtsi_mat->vts_video_attr.display_aspect_ratio )
729 switch( p_vts->vtsi_mat->vts_video_attr.permitted_df )
732 i_position = spu_control & 0xff;
735 i_position = ( spu_control >> 8 ) & 0xff;
738 i_position = ( spu_control >> 16 ) & 0xff;
745 i_position = ( spu_control >> 24 ) & 0x7F;
748 i_id = ( ( 0x20 + i_position ) << 8 ) | 0xbd;
749 p_es = input_AddES( p_input,
750 p_input->stream.p_selected_program, i_id, 0 );
751 p_es->i_stream_id = 0xbd;
752 p_es->i_type = DVD_SPU_ES;
753 p_es->i_cat = SPU_ES;
754 strcpy( p_es->psz_desc, DecodeLanguage( hton16(
755 p_vts->vtsi_mat->vts_subp_attr[i-1].lang_code ) ) );
760 /* FIXME: hack to check that the demuxer is ready, and set
762 if( p_input->pf_init )
764 p_input->pf_init( p_input );
770 p_area = p_input->stream.p_selected_area;
777 if( p_area->i_part != p_dvd->i_chapter )
779 if( ( p_area->i_part > 0 ) &&
780 ( p_area->i_part <= p_area->i_part_nb ))
782 p_dvd->i_ttn = p_vmg->tt_srpt->title[p_area->i_id-1].vts_ttn;
783 pgc_id = p_vts->vts_ptt_srpt->title[
784 p_dvd->i_ttn-1].ptt[p_area->i_part-1].pgcn;
785 pgn = p_vts->vts_ptt_srpt->title[
786 p_dvd->i_ttn-1].ptt[p_area->i_part-1].pgn;
788 p_pgc = p_vts->vts_pgcit->pgci_srp[ pgc_id - 1 ].pgc;
790 p_dvd->i_cur_cell = p_pgc->program_map[ pgn - 1 ] - 1;
791 p_dvd->i_chapter = p_area->i_part;
792 DvdReadFindCell( p_dvd );
794 p_dvd->i_pack_len = 0;
795 p_dvd->i_next_vobu = p_dvd->i_cur_block =
796 p_pgc->cell_playback[p_dvd->i_cur_cell].first_sector;
800 p_area->i_part = p_dvd->i_chapter;
806 if( p_area->i_angle != p_dvd->i_angle )
808 p_dvd->i_angle = p_area->i_angle;
810 intf_WarnMsg( 3, "dvd info: angle %d selected", p_area->i_angle );
812 /* warn interface that something has changed */
813 p_area->i_tell = LB2OFF( p_dvd->i_next_vobu ) - p_area->i_start;
814 p_input->stream.b_seekable = 1;
815 p_input->stream.b_changed = 1;
821 /*****************************************************************************
822 * DvdReadRead: reads data packets into the netlist.
823 *****************************************************************************
824 * Returns -1 in case of error, 0 if everything went well, and 1 in case of
826 *****************************************************************************/
827 static int DvdReadRead( input_thread_t * p_input,
828 byte_t * p_buffer, size_t i_count )
830 thread_dvd_data_t * p_dvd;
838 p_dvd = (thread_dvd_data_t *)p_input->p_access_data;
842 * Playback by cell in this pgc, starting at the cell for our chapter.
844 i_blocks = OFF2LB( i_count );
851 * End of pack, we select the following one
853 if( ! p_dvd->i_pack_len )
858 if( ( i_read = DVDReadBlocks( p_dvd->p_title, p_dvd->i_next_vobu,
861 intf_ErrMsg( "dvdread error: read failed for block %d",
862 p_dvd->i_next_vobu );
866 /* basic check to be sure we don't have a empty title
867 * go to next title if so */
868 //assert( p_buffer[41] == 0xbf && p_buffer[1027] == 0xbf );
871 * Parse the contained dsi packet.
874 DvdReadHandleDSI( p_dvd, p_buf );
877 if( p_dvd->i_next_vobu >= p_dvd->i_end_block + 1 )
882 assert( p_dvd->i_pack_len < 1024 );
883 /* FIXME: Ugly kludge: we send the pack block to the input for it
884 * sometimes has a zero scr and restart the sync */
885 p_dvd->i_cur_block ++;
886 //p_dvd->i_pack_len++;
889 p_buf += DVD_VIDEO_LB_LEN;
894 * Compute the number of blocks to read
896 i_blocks_once = p_dvd->i_pack_len >= i_blocks
897 ? i_blocks : p_dvd->i_pack_len;
898 p_dvd->i_pack_len -= i_blocks_once;
901 i_read = DVDReadBlocks( p_dvd->p_title, p_dvd->i_cur_block,
902 i_blocks_once, p_buf );
903 if( i_read != i_blocks_once )
905 intf_ErrMsg( "dvdread error: read failed for %d/%d blocks at 0x%02x",
906 i_read, i_blocks_once, p_dvd->i_cur_block );
911 i_read_total += i_read;
912 p_dvd->i_cur_block += i_read;
913 p_buf += LB2OFF( i_read );
917 intf_WarnMsg( 12, "dvdread i_blocks: %d len: %d current: 0x%02x", i_read, p_dvd->i_pack_len, p_dvd->i_cur_block );
920 vlc_mutex_lock( &p_input->stream.stream_lock );
922 p_input->stream.p_selected_area->i_tell =
923 LB2OFF( p_dvd->i_cur_block ) -
924 p_input->stream.p_selected_area->i_start;
928 /* We modify i_part only at end of chapter not to erase
929 * some modification from the interface */
930 p_input->stream.p_selected_area->i_part = p_dvd->i_chapter;
934 if( p_input->stream.p_selected_area->i_tell
935 >= p_input->stream.p_selected_area->i_size || b_eot )
937 if( ( p_input->stream.p_selected_area->i_id + 1 ) >=
938 p_input->stream.i_area_nb )
941 vlc_mutex_unlock( &p_input->stream.stream_lock );
946 intf_WarnMsg( 4, "dvd info: new title" );
947 DvdReadSetArea( p_input, p_input->stream.pp_areas[
948 p_input->stream.p_selected_area->i_id+1] );
949 vlc_mutex_unlock( &p_input->stream.stream_lock );
953 vlc_mutex_unlock( &p_input->stream.stream_lock );
955 return LB2OFF( i_read_total );
959 /*****************************************************************************
960 * DvdReadSeek : Goes to a given position on the stream.
961 *****************************************************************************
962 * This one is used by the input and translate chronological position from
963 * input to logical position on the device.
964 * The lock should be taken before calling this function.
965 *****************************************************************************/
966 static void DvdReadSeek( input_thread_t * p_input, off_t i_off )
968 thread_dvd_data_t * p_dvd;
976 i_off += p_input->stream.p_selected_area->i_start;
977 i_lb = OFF2LB( i_off );
978 p_dvd = ( thread_dvd_data_t * )p_input->p_access_data;
981 while( p_dvd->p_cur_pgc->cell_playback[i_cell].last_sector < i_lb )
993 pgc_id = p_dvd->p_vts_file->vts_ptt_srpt->title[
994 p_dvd->i_ttn-1].ptt[i_chapter-1].pgcn;
995 pgn = p_dvd->p_vts_file->vts_ptt_srpt->title[
996 p_dvd->i_ttn-1].ptt[i_chapter-1].pgn;
998 p_pgc = p_dvd->p_vts_file->vts_pgcit->pgci_srp[pgc_id-1].pgc;
999 i_tmp = p_pgc->program_map[pgn-1];
1001 } while( i_tmp <= i_cell );
1004 while( p_dvd->p_vts_file->vts_vobu_admap->vobu_start_sectors[i_vobu]
1011 while( p_dvd->p_vts_file->vts_c_adt->cell_adr_table[i_sub_cell].start_sector <
1012 p_dvd->p_vts_file->vts_vobu_admap->vobu_start_sectors[i_vobu-1] )
1018 intf_WarnMsg(12, "cell %d i_sub_cell %d chapter %d vobu %d cell_sector %d vobu_sector %d sub_cell_sector %d",
1019 i_cell, i_sub_cell,i_chapter, i_vobu,
1020 p_dvd->p_cur_pgc->cell_playback[i_cell].first_sector,
1021 p_dvd->p_vts_file->vts_vobu_admap->vobu_start_sectors[i_vobu],
1022 p_dvd->p_vts_file->vts_c_adt->cell_adr_table[i_sub_cell-1].start_sector);
1024 p_dvd->i_cur_block = i_lb;
1025 p_dvd->i_next_vobu =
1026 p_dvd->p_vts_file->vts_vobu_admap->vobu_start_sectors[i_vobu];
1027 p_dvd->i_pack_len = p_dvd->i_next_vobu - i_lb;
1028 p_dvd->i_cur_cell = i_cell;
1029 p_dvd->i_chapter = i_chapter;
1030 DvdReadFindCell( p_dvd );
1032 p_input->stream.p_selected_area->i_tell =
1033 LB2OFF ( p_dvd->i_cur_block )
1034 - p_input->stream.p_selected_area->i_start;
1035 p_input->stream.p_selected_area->i_part = p_dvd->i_chapter;
1040 /*****************************************************************************
1042 *****************************************************************************/
1043 static void DvdReadHandleDSI( thread_dvd_data_t * p_dvd, u8 * p_data )
1045 navRead_DSI( &(p_dvd->dsi_pack), &(p_data[ DSI_START_BYTE ]) );
1048 * Determine where we go next. These values are the ones we mostly
1051 p_dvd->i_cur_block = p_dvd->dsi_pack.dsi_gi.nv_pck_lbn;
1054 * If we're not at the end of this cell, we can determine the next
1055 * VOBU to display using the VOBU_SRI information section of the
1056 * DSI. Using this value correctly follows the current angle,
1057 * avoiding the doubled scenes in The Matrix, and makes our life
1060 if( p_dvd->dsi_pack.vobu_sri.next_vobu != SRI_END_OF_CELL )
1063 switch( ( p_dvd->dsi_pack.sml_pbi.category & 0xf000 ) >> 12 )
1066 /* interleaved unit with no angle */
1067 if( p_dvd->dsi_pack.sml_pbi.ilvu_sa != -1 )
1069 p_dvd->i_next_vobu = p_dvd->i_cur_block +
1070 p_dvd->dsi_pack.sml_pbi.ilvu_sa;
1071 p_dvd->i_pack_len = p_dvd->dsi_pack.sml_pbi.ilvu_ea;
1075 p_dvd->i_next_vobu = p_dvd->i_cur_block +
1076 p_dvd->dsi_pack.dsi_gi.vobu_ea + 1;
1077 p_dvd->i_pack_len = p_dvd->dsi_pack.dsi_gi.vobu_ea;
1081 /* vobu is end of ilvu */
1082 if( p_dvd->dsi_pack.sml_agli.data[p_dvd->i_angle-1].address )
1084 p_dvd->i_next_vobu = p_dvd->i_cur_block +
1085 p_dvd->dsi_pack.sml_agli.data[p_dvd->i_angle-1].address;
1086 p_dvd->i_pack_len = p_dvd->dsi_pack.sml_pbi.ilvu_ea;
1091 /* vobu is beginning of ilvu */
1095 /* entering interleaved section */
1097 /* non interleaved cells in interleaved section */
1099 p_dvd->i_next_vobu = p_dvd->i_cur_block +
1100 ( p_dvd->dsi_pack.vobu_sri.next_vobu & 0x7fffffff );
1101 p_dvd->i_pack_len = p_dvd->dsi_pack.dsi_gi.vobu_ea;
1105 p_dvd->i_next_vobu = p_dvd->i_cur_block +
1106 ( p_dvd->dsi_pack.vobu_sri.next_vobu & 0x7fffffff );
1107 p_dvd->i_pack_len = p_dvd->dsi_pack.dsi_gi.vobu_ea;
1112 p_dvd->i_cur_cell = p_dvd->i_next_cell;
1113 DvdReadFindCell( p_dvd );
1115 p_dvd->i_pack_len = p_dvd->dsi_pack.dsi_gi.vobu_ea;
1116 p_dvd->i_next_vobu =
1117 p_dvd->p_cur_pgc->cell_playback[p_dvd->i_cur_cell].first_sector;
1121 intf_WarnMsg( 12, "scr %d lbn 0x%02x vobu_ea %d vob_id %d c_id %d",
1122 p_dvd->dsi_pack.dsi_gi.nv_pck_scr,
1123 p_dvd->dsi_pack.dsi_gi.nv_pck_lbn,
1124 p_dvd->dsi_pack.dsi_gi.vobu_ea,
1125 p_dvd->dsi_pack.dsi_gi.vobu_vob_idn,
1126 p_dvd->dsi_pack.dsi_gi.vobu_c_idn );
1128 intf_WarnMsg( 12, "cat 0x%02x ilvu_ea %d ilvu_sa %d size %d",
1129 p_dvd->dsi_pack.sml_pbi.category,
1130 p_dvd->dsi_pack.sml_pbi.ilvu_ea,
1131 p_dvd->dsi_pack.sml_pbi.ilvu_sa,
1132 p_dvd->dsi_pack.sml_pbi.size );
1134 intf_WarnMsg( 12, "next_vobu %d next_ilvu1 %d next_ilvu2 %d",
1135 p_dvd->dsi_pack.vobu_sri.next_vobu & 0x7fffffff,
1136 p_dvd->dsi_pack.sml_agli.data[ p_dvd->i_angle - 1 ].address,
1137 p_dvd->dsi_pack.sml_agli.data[ p_dvd->i_angle ].address);
1141 /*****************************************************************************
1143 *****************************************************************************/
1144 static void DvdReadFindCell( thread_dvd_data_t * p_dvd )
1149 #define cell p_dvd->p_cur_pgc->cell_playback
1150 if( cell[p_dvd->i_cur_cell].block_type == BLOCK_TYPE_ANGLE_BLOCK )
1153 p_dvd->i_next_cell = p_dvd->i_cur_cell + p_dvd->i_angle_nb;
1154 p_dvd->i_cur_cell += p_dvd->i_angle - 1;
1156 p_dvd->i_cur_cell += p_dvd->i_angle - 1;
1158 while( cell[p_dvd->i_cur_cell+i].block_mode != BLOCK_MODE_LAST_CELL )
1162 p_dvd->i_next_cell = p_dvd->i_cur_cell + i + 1;
1167 p_dvd->i_next_cell = p_dvd->i_cur_cell + 1;
1170 pgc_id = p_dvd->p_vts_file->vts_ptt_srpt->title[
1171 p_dvd->i_ttn-1].ptt[p_dvd->i_chapter-1].pgcn;
1172 pgn = p_dvd->p_vts_file->vts_ptt_srpt->title[
1173 p_dvd->i_ttn-1].ptt[p_dvd->i_chapter-1].pgn;
1174 p_pgc = p_dvd->p_vts_file->vts_pgcit->pgci_srp[pgc_id-1].pgc;
1176 if( p_pgc->program_map[pgn-1] <= p_dvd->i_cur_cell )