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.15 2003/01/28 15:05:52 massiot 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, uint8_t * 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 ssize_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;
246 unsigned int i_title = 1;
247 unsigned int i_chapter = 1;
248 unsigned int i_angle = 1;
251 psz_source = strdup( p_input->psz_name );
252 if( psz_source == NULL )
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 /* Start with the end, because you could have :
263 * dvdread:/Volumes/my@toto/VIDEO_TS@1,1
264 * (yes, this is kludgy). */
265 for ( psz_parser = psz_source + strlen(psz_source) - 1;
266 psz_parser >= psz_source && *psz_parser != '@';
269 if( psz_parser >= psz_source && *psz_parser == '@' )
275 i_title = (int)strtol( psz_parser, &psz_next, 10 );
278 psz_parser = psz_next + 1;
279 i_chapter = (int)strtol( psz_parser, &psz_next, 10 );
282 i_angle = (int)strtol( psz_next + 1, NULL, 10 );
286 i_title = i_title ? i_title : 1;
287 i_chapter = i_chapter ? i_chapter : 1;
288 i_angle = i_angle ? i_angle : 1;
294 if( !p_input->psz_access )
298 psz_source = config_GetPsz( p_input, "dvd" );
299 if( !psz_source ) return VLC_EGENERIC;
302 if( stat( psz_source, &stat_info ) == -1 )
304 msg_Warn( p_input, "cannot stat() source `%s' (%s)",
305 psz_source, strerror(errno) );
309 if( !S_ISBLK(stat_info.st_mode) &&
310 !S_ISCHR(stat_info.st_mode) &&
311 !S_ISDIR(stat_info.st_mode) )
313 msg_Warn( p_input, "dvdread module discarded (not a valid source)" );
318 msg_Dbg( p_input, "dvdroot=%s title=%d chapter=%d angle=%d",
319 psz_source, i_title, i_chapter, i_angle );
322 p_dvdread = DVDOpen( psz_source );
324 /* free allocated strings */
329 msg_Err( p_input, "libdvdcss cannot open source" );
336 p_dvd = malloc( sizeof(thread_dvd_data_t) );
339 msg_Err( p_input, "out of memory" );
343 p_dvd->p_dvdread = p_dvdread;
344 p_dvd->p_title = NULL;
345 p_dvd->p_vts_file = NULL;
348 p_input->p_access_data = (void *)p_dvd;
350 /* Ifo allocation & initialisation */
351 if( ! ( p_dvd->p_vmg_file = ifoOpen( p_dvd->p_dvdread, 0 ) ) )
353 msg_Err( p_input, "cannot open VMG info" );
357 msg_Dbg( p_input, "VMG opened" );
359 /* Set stream and area data */
360 vlc_mutex_lock( &p_input->stream.stream_lock );
362 p_input->stream.i_method = INPUT_METHOD_DVD;
364 /* If we are here we can control the pace... */
365 p_input->stream.b_pace_control = VLC_TRUE;
366 p_input->stream.b_seekable = VLC_TRUE;
367 p_input->stream.b_connected = VLC_TRUE;
369 p_input->stream.p_selected_area->i_size = 0;
370 p_input->stream.p_selected_area->i_tell = 0;
372 /* Initialize ES structures */
373 input_InitStream( p_input, sizeof( stream_ps_data_t ) );
375 /* disc input method */
376 p_input->stream.i_method = INPUT_METHOD_DVD;
378 #define tt_srpt p_dvd->p_vmg_file->tt_srpt
379 msg_Dbg( p_input, "number of titles: %d", tt_srpt->nr_of_srpts );
381 #define area p_input->stream.pp_areas
382 /* We start from 1 here since the default area 0
383 * is reserved for video_ts.vob */
384 for( i = 1 ; i <= tt_srpt->nr_of_srpts ; i++ )
386 input_AddArea( p_input );
388 /* Titles are Program Chains */
391 /* Absolute start offset and size
392 * We can only set that with vts ifo, so we do it during the
393 * first call to DVDSetArea */
394 area[i]->i_start = 0;
397 /* Number of chapters */
398 area[i]->i_part_nb = tt_srpt->title[i-1].nr_of_ptts;
401 area[i]->i_plugin_data = tt_srpt->title[i-1].title_set_nr;
405 p_dvd->i_title = i_title <= tt_srpt->nr_of_srpts ? i_title : 1;
408 p_area = p_input->stream.pp_areas[p_dvd->i_title];
409 p_dvd->i_chapter = i_chapter;
411 p_dvd->i_chapter = i_chapter < p_area->i_part_nb ? i_chapter : 1;
412 p_area->i_part = p_dvd->i_chapter;
414 p_dvd->i_angle = i_angle;
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 );
425 if( !p_input->psz_demux || !*p_input->psz_demux )
427 p_input->psz_demux = "dvdread";
433 /*****************************************************************************
434 * CloseDVD: close libdvdread
435 *****************************************************************************/
436 void E_(CloseDVD) ( vlc_object_t *p_this )
438 input_thread_t * p_input = (input_thread_t *)p_this;
439 thread_dvd_data_t * p_dvd = (thread_dvd_data_t *)p_input->p_access_data;
441 /* This is a very nasty side-effect in the DVD plug-in : language
442 * selection here influences language selection of other streams. So
443 * unset those variables (may not be what the user wants).
444 * FIXME FIXME FIXME FIXME FIXME FIXME FIXME --Meuuh */
445 config_PutInt( p_input, "audio-channel", -1 );
446 config_PutInt( p_input, "spu-channel", -1 );
448 /* close libdvdread */
449 DVDCloseFile( p_dvd->p_title );
450 ifoClose( p_dvd->p_vts_file );
451 ifoClose( p_dvd->p_vmg_file );
453 DVDClose( p_dvd->p_dvdread );
455 p_input->p_access_data = NULL;
459 /*****************************************************************************
460 * DvdReadSetProgram: Does nothing, a DVD is mono-program
461 *****************************************************************************/
462 static int DvdReadSetProgram( input_thread_t * p_input,
463 pgrm_descriptor_t * p_program )
465 if( p_input->stream.p_selected_program != p_program )
467 thread_dvd_data_t * p_dvd;
469 p_dvd = (thread_dvd_data_t*)(p_input->p_access_data);
470 p_dvd->i_angle = p_program->i_number;
472 memcpy( p_program, p_input->stream.p_selected_program,
473 sizeof(pgrm_descriptor_t) );
474 p_program->i_number = p_dvd->i_angle;
475 p_input->stream.p_selected_program = p_program;
477 msg_Dbg( p_input, "angle %d selected", p_dvd->i_angle );
483 #define p_pgc p_dvd->p_cur_pgc
485 /*****************************************************************************
486 * DvdReadSetArea: initialize input data for title x, chapter y.
487 * It should be called for each user navigation request.
488 *****************************************************************************
489 * Take care that i_title starts from 0 (vmg) and i_chapter start from 1.
490 * Note that you have to take the lock before entering here.
491 *****************************************************************************/
492 static int DvdReadSetArea( input_thread_t * p_input, input_area_t * p_area )
494 thread_dvd_data_t * p_dvd;
498 p_dvd = (thread_dvd_data_t*)p_input->p_access_data;
500 /* we can't use the interface slider until initilization is complete */
501 p_input->stream.b_seekable = VLC_FALSE;
503 if( p_area != p_input->stream.p_selected_area )
505 es_descriptor_t * p_es;
506 unsigned int i_cell = 0;
507 unsigned int i_audio_nb = 0;
508 unsigned int i_spu_nb = 0;
511 #define p_vmg p_dvd->p_vmg_file
512 #define p_vts p_dvd->p_vts_file
513 if( p_dvd->p_title != NULL )
515 DVDCloseFile( p_dvd->p_title );
523 /* Reset the Chapter position of the old title */
524 p_input->stream.p_selected_area->i_part = 1;
527 * We have to load all title information
529 /* Change the default area */
530 p_input->stream.p_selected_area = p_area;
532 msg_Dbg( p_input, "open VTS %d, for title %d",
533 p_vmg->tt_srpt->title[ p_area->i_id - 1 ].title_set_nr,
537 if( ! ( p_vts = ifoOpen( p_dvd->p_dvdread,
538 p_vmg->tt_srpt->title[ p_area->i_id - 1 ].title_set_nr ) ) )
540 msg_Err( p_input, "fatal error in vts ifo" );
542 DVDClose( p_dvd->p_dvdread );
546 /* title position inside the selected vts */
547 p_dvd->i_ttn = p_vmg->tt_srpt->title[ p_area->i_id - 1 ].vts_ttn;
550 * Set selected title start
552 pgc_id = p_vts->vts_ptt_srpt->title[p_dvd->i_ttn-1].ptt[0].pgcn;
553 pgn = p_vts->vts_ptt_srpt->title[p_dvd->i_ttn-1].ptt[0].pgn;
554 p_pgc = p_vts->vts_pgcit->pgci_srp[ pgc_id - 1 ].pgc;
555 i_cell = p_pgc->program_map[ pgn - 1 ] - 1;
558 LB2OFF( p_dvd->p_cur_pgc->cell_playback[ i_cell ].first_sector );
560 msg_Dbg( p_input, "start %d vts_title %d pgc %d pgn %d",
561 p_area->i_id, p_dvd->i_ttn, pgc_id, pgn );
566 i_cell = p_dvd->p_cur_pgc->nr_of_cells - 1;
568 p_dvd->i_end_block = p_pgc->cell_playback[ i_cell ].last_sector;
569 p_area->i_size = LB2OFF( p_dvd->i_end_block )- p_area->i_start;
571 msg_Dbg( p_input, "start "I64Fd" size "I64Fd" end %d",
572 p_area->i_start , p_area->i_size, p_dvd->i_end_block );
575 * Set properties for current chapter
577 /* Remeber current chapter */
578 p_dvd->i_chapter = p_area->i_part;
579 p_dvd->b_eoc = VLC_FALSE;
581 pgc_id = p_vts->vts_ptt_srpt->title[
582 p_dvd->i_ttn-1].ptt[p_area->i_part-1].pgcn;
583 pgn = p_vts->vts_ptt_srpt->title[
584 p_dvd->i_ttn-1].ptt[p_area->i_part-1].pgn;
586 p_pgc = p_vts->vts_pgcit->pgci_srp[pgc_id-1].pgc;
587 p_dvd->i_pack_len = 0;
588 p_dvd->i_next_cell = p_dvd->i_cur_cell = p_pgc->program_map[pgn-1] - 1;
589 DvdReadFindCell( p_dvd );
591 p_dvd->i_next_vobu = p_dvd->i_cur_block =
592 p_pgc->cell_playback[p_dvd->i_cur_cell].first_sector;
597 p_dvd->i_angle_nb = p_vmg->tt_srpt->title[p_area->i_id-1].nr_of_angles;
599 if( p_dvd->i_angle > p_dvd->i_angle_nb )
605 * We've got enough info, time to open the title set data.
607 if( ! ( p_dvd->p_title = DVDOpenFile( p_dvd->p_dvdread,
608 p_vmg->tt_srpt->title[ p_area->i_id - 1 ].title_set_nr,
609 DVD_READ_TITLE_VOBS ) ) )
611 msg_Err( p_input, "cannot open title (VTS_%02d_1.VOB)",
612 p_vmg->tt_srpt->title[p_area->i_id-1].title_set_nr );
615 DVDClose( p_dvd->p_dvdread );
619 // IfoPrintTitle( p_dvd );
622 * Destroy obsolete ES by reinitializing program 0
623 * and find all ES in title with ifo data
625 if( p_input->stream.pp_programs != NULL )
627 /* We don't use input_EndStream here since
628 * we keep area structures */
630 while( p_input->stream.i_es_number )
632 input_DelES( p_input, p_input->stream.pp_es[0] );
635 while( p_input->stream.i_pgrm_number )
637 input_DelProgram( p_input, p_input->stream.pp_programs[0] );
640 if( p_input->stream.pp_selected_es )
642 free( p_input->stream.pp_selected_es );
643 p_input->stream.pp_selected_es = NULL;
645 p_input->stream.i_selected_es_number = 0;
648 input_AddProgram( p_input, 1, sizeof( stream_ps_data_t ) );
649 p_input->stream.p_selected_program = p_input->stream.pp_programs[0];
651 for( i = 1 ; i < p_dvd->i_angle_nb ; i++ )
653 input_AddProgram( p_input, i+1, 0 );
656 DvdReadSetProgram( p_input,
657 p_input->stream.pp_programs[p_dvd->i_angle-1] );
659 /* No PSM to read in DVD mode, we already have all information */
660 p_input->stream.p_selected_program->b_is_ok = VLC_TRUE;
664 /* ES 0 -> video MPEG2 */
665 // IfoPrintVideo( p_dvd );
667 p_es = input_AddES( p_input, NULL, 0xe0, 0 );
668 p_es->i_stream_id = 0xe0;
669 p_es->i_fourcc = VLC_FOURCC('m','p','g','v');
670 p_es->i_cat = VIDEO_ES;
672 #define audio_control \
673 p_dvd->p_vts_file->vts_pgcit->pgci_srp[pgc_id-1].pgc->audio_control[i-1]
674 /* Audio ES, in the order they appear in .ifo */
675 for( i = 1 ; i <= p_vts->vtsi_mat->nr_of_vts_audio_streams ; i++ )
680 // IfoPrintAudio( p_dvd, i );
682 /* audio channel is active if first byte is 0x80 */
683 if( audio_control & 0x8000 )
686 i_position = ( audio_control & 0x7F00 ) >> 8;
688 msg_Dbg( p_input, "audio position %d", i_position );
689 switch( p_vts->vtsi_mat->vts_audio_attr[i-1].audio_format )
692 i_id = ( ( 0x80 + i_position ) << 8 ) | 0xbd;
693 p_es = input_AddES( p_input, NULL, i_id, 0 );
694 p_es->i_stream_id = 0xbd;
695 p_es->i_fourcc = VLC_FOURCC('a','5','2','b');
696 p_es->i_cat = AUDIO_ES;
697 strcpy( p_es->psz_desc, DecodeLanguage(
698 p_vts->vtsi_mat->vts_audio_attr[i-1].lang_code ) );
699 strcat( p_es->psz_desc, " (A52)" );
703 case 0x03: /* MPEG audio */
704 i_id = 0xc0 + i_position;
705 p_es = input_AddES( p_input, NULL, i_id, 0 );
706 p_es->i_stream_id = i_id;
707 p_es->i_fourcc = VLC_FOURCC('m','p','g','a');
708 p_es->i_cat = AUDIO_ES;
709 strcpy( p_es->psz_desc, DecodeLanguage(
710 p_vts->vtsi_mat->vts_audio_attr[i-1].lang_code ) );
711 strcat( p_es->psz_desc, " (mpeg)" );
714 case 0x04: /* LPCM */
716 i_id = ( ( 0xa0 + i_position ) << 8 ) | 0xbd;
717 p_es = input_AddES( p_input, NULL, i_id, 0 );
718 p_es->i_stream_id = i_id;
719 p_es->i_fourcc = VLC_FOURCC('l','p','c','b');
720 p_es->i_cat = AUDIO_ES;
721 strcpy( p_es->psz_desc, DecodeLanguage(
722 p_vts->vtsi_mat->vts_audio_attr[i-1].lang_code ) );
723 strcat( p_es->psz_desc, " (lpcm)" );
727 i_id = ( ( 0x88 + i_position ) << 8 ) | 0xbd;
728 msg_Err( p_input, "DTS audio not handled yet"
733 msg_Err( p_input, "unknown audio type %.2x",
734 p_vts->vtsi_mat->vts_audio_attr[i-1].audio_format );
739 #define spu_control \
740 p_dvd->p_vts_file->vts_pgcit->pgci_srp[pgc_id-1].pgc->subp_control[i-1]
744 for( i = 1 ; i <= p_vts->vtsi_mat->nr_of_vts_subp_streams; i++ )
749 // IfoPrintSpu( p_dvd, i );
750 msg_Dbg( p_input, "spu %d 0x%02x", i, spu_control );
752 if( spu_control & 0x80000000 )
756 /* there are several streams for one spu */
757 if( p_vts->vtsi_mat->vts_video_attr.display_aspect_ratio )
760 switch( p_vts->vtsi_mat->vts_video_attr.permitted_df )
763 i_position = spu_control & 0xff;
766 i_position = ( spu_control >> 8 ) & 0xff;
769 i_position = ( spu_control >> 16 ) & 0xff;
776 i_position = ( spu_control >> 24 ) & 0x7F;
779 i_id = ( ( 0x20 + i_position ) << 8 ) | 0xbd;
780 p_es = input_AddES( p_input, NULL, i_id, 0 );
781 p_es->i_stream_id = 0xbd;
782 p_es->i_fourcc = VLC_FOURCC('s','p','u','b');
783 p_es->i_cat = SPU_ES;
784 strcpy( p_es->psz_desc, DecodeLanguage(
785 p_vts->vtsi_mat->vts_subp_attr[i-1].lang_code ) );
790 /* FIXME: hack to check that the demuxer is ready, and set
792 if( p_input->p_demux )
794 DvdReadLauchDecoders( p_input );
800 p_area = p_input->stream.p_selected_area;
807 if( p_area->i_part != p_dvd->i_chapter )
809 if( ( p_area->i_part > 0 ) &&
810 ( p_area->i_part <= p_area->i_part_nb ))
812 p_dvd->i_ttn = p_vmg->tt_srpt->title[p_area->i_id-1].vts_ttn;
813 pgc_id = p_vts->vts_ptt_srpt->title[
814 p_dvd->i_ttn-1].ptt[p_area->i_part-1].pgcn;
815 pgn = p_vts->vts_ptt_srpt->title[
816 p_dvd->i_ttn-1].ptt[p_area->i_part-1].pgn;
818 p_pgc = p_vts->vts_pgcit->pgci_srp[ pgc_id - 1 ].pgc;
820 p_dvd->i_cur_cell = p_pgc->program_map[ pgn - 1 ] - 1;
821 p_dvd->i_chapter = p_area->i_part;
822 DvdReadFindCell( p_dvd );
824 p_dvd->i_pack_len = 0;
825 p_dvd->i_next_vobu = p_dvd->i_cur_block =
826 p_pgc->cell_playback[p_dvd->i_cur_cell].first_sector;
830 p_area->i_part = p_dvd->i_chapter;
836 /* warn interface that something has changed */
837 p_area->i_tell = LB2OFF( p_dvd->i_next_vobu ) - p_area->i_start;
838 p_input->stream.b_seekable = VLC_TRUE;
839 p_input->stream.b_changed = VLC_TRUE;
845 /*****************************************************************************
846 * DvdReadRead: reads data packets into the netlist.
847 *****************************************************************************
848 * Returns -1 in case of error, 0 if everything went well, and 1 in case of
850 *****************************************************************************/
851 static int DvdReadRead( input_thread_t * p_input,
852 byte_t * p_buffer, size_t i_count )
854 thread_dvd_data_t * p_dvd;
856 unsigned int i_blocks_once;
857 unsigned int i_blocks;
860 vlc_bool_t b_eot = VLC_FALSE;
862 p_dvd = (thread_dvd_data_t *)p_input->p_access_data;
866 * Playback by cell in this pgc, starting at the cell for our chapter.
868 i_blocks = OFF2LB( i_count );
875 * End of pack, we select the following one
877 if( ! p_dvd->i_pack_len )
882 if( ( i_read = DVDReadBlocks( p_dvd->p_title, p_dvd->i_next_vobu,
885 msg_Err( p_input, "read failed for block %d",
886 p_dvd->i_next_vobu );
890 /* basic check to be sure we don't have a empty title
891 * go to next title if so */
892 //assert( p_buffer[41] == 0xbf && p_buffer[1027] == 0xbf );
895 * Parse the contained dsi packet.
898 DvdReadHandleDSI( p_dvd, p_buf );
901 if( p_dvd->i_next_vobu >= p_dvd->i_end_block + 1 )
906 assert( p_dvd->i_pack_len < 1024 );
907 /* FIXME: Ugly kludge: we send the pack block to the input for it
908 * sometimes has a zero scr and restart the sync */
909 p_dvd->i_cur_block ++;
910 //p_dvd->i_pack_len++;
913 p_buf += DVD_VIDEO_LB_LEN;
918 * Compute the number of blocks to read
920 i_blocks_once = __MIN( p_dvd->i_pack_len, i_blocks );
921 p_dvd->i_pack_len -= i_blocks_once;
924 i_read = DVDReadBlocks( p_dvd->p_title, p_dvd->i_cur_block,
925 i_blocks_once, p_buf );
926 if( (unsigned int)i_read != i_blocks_once )
928 msg_Err( p_input, "read failed for %d/%d blocks at 0x%02x",
929 i_read, i_blocks_once, p_dvd->i_cur_block );
934 i_read_total += i_read;
935 p_dvd->i_cur_block += i_read;
936 p_buf += LB2OFF( i_read );
940 msg_Dbg( p_input, "i_blocks: %d len: %d current: 0x%02x", i_read, p_dvd->i_pack_len, p_dvd->i_cur_block );
943 vlc_mutex_lock( &p_input->stream.stream_lock );
947 /* We modify i_part only at end of chapter not to erase
948 * some modification from the interface */
949 p_input->stream.p_selected_area->i_part = p_dvd->i_chapter;
950 p_dvd->b_eoc = VLC_FALSE;
953 if( ( LB2OFF( p_dvd->i_cur_block )
954 - p_input->stream.p_selected_area->i_start )
955 >= p_input->stream.p_selected_area->i_size || b_eot )
957 if( ( p_input->stream.p_selected_area->i_id + 1 ) >=
958 p_input->stream.i_area_nb )
961 vlc_mutex_unlock( &p_input->stream.stream_lock );
966 msg_Dbg( p_input, "new title" );
967 DvdReadSetArea( p_input, p_input->stream.pp_areas[
968 p_input->stream.p_selected_area->i_id+1] );
969 vlc_mutex_unlock( &p_input->stream.stream_lock );
973 vlc_mutex_unlock( &p_input->stream.stream_lock );
975 return LB2OFF( i_read_total );
979 /*****************************************************************************
980 * DvdReadSeek : Goes to a given position on the stream.
981 *****************************************************************************
982 * This one is used by the input and translate chronological position from
983 * input to logical position on the device.
984 * The lock should be taken before calling this function.
985 *****************************************************************************/
986 static void DvdReadSeek( input_thread_t * p_input, off_t i_off )
988 thread_dvd_data_t * p_dvd;
991 unsigned int i_chapter = 0;
992 unsigned int i_cell = 0;
993 unsigned int i_vobu = 0;
994 unsigned int i_sub_cell = 0;
996 vlc_mutex_lock( &p_input->stream.stream_lock );
997 i_off += p_input->stream.p_selected_area->i_start;
998 vlc_mutex_unlock( &p_input->stream.stream_lock );
1000 i_lb = OFF2LB( i_off );
1001 p_dvd = ( thread_dvd_data_t * )p_input->p_access_data;
1004 while( p_dvd->p_cur_pgc->cell_playback[i_cell].last_sector < i_lb )
1016 pgc_id = p_dvd->p_vts_file->vts_ptt_srpt->title[
1017 p_dvd->i_ttn-1].ptt[i_chapter-1].pgcn;
1018 pgn = p_dvd->p_vts_file->vts_ptt_srpt->title[
1019 p_dvd->i_ttn-1].ptt[i_chapter-1].pgn;
1021 p_pgc = p_dvd->p_vts_file->vts_pgcit->pgci_srp[pgc_id-1].pgc;
1022 i_tmp = p_pgc->program_map[pgn-1];
1024 } while( i_tmp <= i_cell );
1027 while( p_dvd->p_vts_file->vts_vobu_admap->vobu_start_sectors[i_vobu]
1034 while( p_dvd->p_vts_file->vts_c_adt->cell_adr_table[i_sub_cell].start_sector <
1035 p_dvd->p_vts_file->vts_vobu_admap->vobu_start_sectors[i_vobu-1] )
1041 msg_Dbg( p_input, "cell %d i_sub_cell %d chapter %d vobu %d cell_sector %d vobu_sector %d sub_cell_sector %d",
1042 i_cell, i_sub_cell,i_chapter, i_vobu,
1043 p_dvd->p_cur_pgc->cell_playback[i_cell].first_sector,
1044 p_dvd->p_vts_file->vts_vobu_admap->vobu_start_sectors[i_vobu],
1045 p_dvd->p_vts_file->vts_c_adt->cell_adr_table[i_sub_cell-1].start_sector);
1047 p_dvd->i_cur_block = i_lb;
1048 p_dvd->i_next_vobu =
1049 p_dvd->p_vts_file->vts_vobu_admap->vobu_start_sectors[i_vobu];
1050 p_dvd->i_pack_len = p_dvd->i_next_vobu - i_lb;
1051 p_dvd->i_cur_cell = i_cell;
1052 p_dvd->i_chapter = i_chapter;
1053 DvdReadFindCell( p_dvd );
1055 vlc_mutex_lock( &p_input->stream.stream_lock );
1056 p_input->stream.p_selected_area->i_tell =
1057 LB2OFF ( p_dvd->i_cur_block )
1058 - p_input->stream.p_selected_area->i_start;
1059 p_input->stream.p_selected_area->i_part = p_dvd->i_chapter;
1060 vlc_mutex_unlock( &p_input->stream.stream_lock );
1065 /*****************************************************************************
1067 *****************************************************************************/
1068 static void DvdReadHandleDSI( thread_dvd_data_t * p_dvd, uint8_t * p_data )
1070 navRead_DSI( &(p_dvd->dsi_pack), &(p_data[ DSI_START_BYTE ]) );
1073 * Determine where we go next. These values are the ones we mostly
1076 p_dvd->i_cur_block = p_dvd->dsi_pack.dsi_gi.nv_pck_lbn;
1079 * If we're not at the end of this cell, we can determine the next
1080 * VOBU to display using the VOBU_SRI information section of the
1081 * DSI. Using this value correctly follows the current angle,
1082 * avoiding the doubled scenes in The Matrix, and makes our life
1085 if( p_dvd->dsi_pack.vobu_sri.next_vobu != SRI_END_OF_CELL )
1088 switch( ( p_dvd->dsi_pack.sml_pbi.category & 0xf000 ) >> 12 )
1091 /* interleaved unit with no angle */
1092 if( p_dvd->dsi_pack.sml_pbi.ilvu_sa != 0 )
1094 p_dvd->i_next_vobu = p_dvd->i_cur_block +
1095 p_dvd->dsi_pack.sml_pbi.ilvu_sa;
1096 p_dvd->i_pack_len = p_dvd->dsi_pack.sml_pbi.ilvu_ea;
1100 p_dvd->i_next_vobu = p_dvd->i_cur_block +
1101 p_dvd->dsi_pack.dsi_gi.vobu_ea + 1;
1102 p_dvd->i_pack_len = p_dvd->dsi_pack.dsi_gi.vobu_ea;
1106 /* vobu is end of ilvu */
1107 if( p_dvd->dsi_pack.sml_agli.data[p_dvd->i_angle-1].address )
1109 p_dvd->i_next_vobu = p_dvd->i_cur_block +
1110 p_dvd->dsi_pack.sml_agli.data[p_dvd->i_angle-1].address;
1111 p_dvd->i_pack_len = p_dvd->dsi_pack.sml_pbi.ilvu_ea;
1116 /* vobu is beginning of ilvu */
1120 /* entering interleaved section */
1122 /* non interleaved cells in interleaved section */
1124 p_dvd->i_next_vobu = p_dvd->i_cur_block +
1125 ( p_dvd->dsi_pack.vobu_sri.next_vobu & 0x7fffffff );
1126 p_dvd->i_pack_len = p_dvd->dsi_pack.dsi_gi.vobu_ea;
1130 p_dvd->i_next_vobu = p_dvd->i_cur_block +
1131 ( p_dvd->dsi_pack.vobu_sri.next_vobu & 0x7fffffff );
1132 p_dvd->i_pack_len = p_dvd->dsi_pack.dsi_gi.vobu_ea;
1137 p_dvd->i_cur_cell = p_dvd->i_next_cell;
1138 DvdReadFindCell( p_dvd );
1140 p_dvd->i_pack_len = p_dvd->dsi_pack.dsi_gi.vobu_ea;
1141 p_dvd->i_next_vobu =
1142 p_dvd->p_cur_pgc->cell_playback[p_dvd->i_cur_cell].first_sector;
1146 msg_Dbg( p_input, 12, "scr %d lbn 0x%02x vobu_ea %d vob_id %d c_id %d",
1147 p_dvd->dsi_pack.dsi_gi.nv_pck_scr,
1148 p_dvd->dsi_pack.dsi_gi.nv_pck_lbn,
1149 p_dvd->dsi_pack.dsi_gi.vobu_ea,
1150 p_dvd->dsi_pack.dsi_gi.vobu_vob_idn,
1151 p_dvd->dsi_pack.dsi_gi.vobu_c_idn );
1153 msg_Dbg( p_input, 12, "cat 0x%02x ilvu_ea %d ilvu_sa %d size %d",
1154 p_dvd->dsi_pack.sml_pbi.category,
1155 p_dvd->dsi_pack.sml_pbi.ilvu_ea,
1156 p_dvd->dsi_pack.sml_pbi.ilvu_sa,
1157 p_dvd->dsi_pack.sml_pbi.size );
1159 msg_Dbg( p_input, 12, "next_vobu %d next_ilvu1 %d next_ilvu2 %d",
1160 p_dvd->dsi_pack.vobu_sri.next_vobu & 0x7fffffff,
1161 p_dvd->dsi_pack.sml_agli.data[ p_dvd->i_angle - 1 ].address,
1162 p_dvd->dsi_pack.sml_agli.data[ p_dvd->i_angle ].address);
1166 /*****************************************************************************
1168 *****************************************************************************/
1169 static void DvdReadFindCell( thread_dvd_data_t * p_dvd )
1174 #define cell p_dvd->p_cur_pgc->cell_playback
1175 if( cell[p_dvd->i_cur_cell].block_type == BLOCK_TYPE_ANGLE_BLOCK )
1178 p_dvd->i_next_cell = p_dvd->i_cur_cell + p_dvd->i_angle_nb;
1179 p_dvd->i_cur_cell += p_dvd->i_angle - 1;
1181 p_dvd->i_cur_cell += p_dvd->i_angle - 1;
1183 while( cell[p_dvd->i_cur_cell+i].block_mode != BLOCK_MODE_LAST_CELL )
1187 p_dvd->i_next_cell = p_dvd->i_cur_cell + i + 1;
1192 p_dvd->i_next_cell = p_dvd->i_cur_cell + 1;
1195 pgc_id = p_dvd->p_vts_file->vts_ptt_srpt->title[
1196 p_dvd->i_ttn-1].ptt[p_dvd->i_chapter-1].pgcn;
1197 pgn = p_dvd->p_vts_file->vts_ptt_srpt->title[
1198 p_dvd->i_ttn-1].ptt[p_dvd->i_chapter-1].pgn;
1199 p_pgc = p_dvd->p_vts_file->vts_pgcit->pgci_srp[pgc_id-1].pgc;
1201 if( p_pgc->program_map[pgn-1] <= p_dvd->i_cur_cell )
1204 p_dvd->b_eoc = VLC_TRUE;
1208 /*****************************************************************************
1209 * DvdReadLaunchDecoders
1210 *****************************************************************************/
1211 static void DvdReadLauchDecoders( input_thread_t * p_input )
1213 thread_dvd_data_t * p_dvd = (thread_dvd_data_t*)(p_input->p_access_data);
1216 input_SelectES( p_input, p_input->stream.pp_es[0] );
1218 /* For audio: first one if none or a not existing one specified */
1219 i_audio = config_GetInt( p_input, "audio-channel" );
1220 if( i_audio < 0 /*|| i_audio > i_audio_nb*/ )
1222 config_PutInt( p_input, "audio-channel", 1 );
1225 if( i_audio > 0/* && i_audio_nb > 0*/ )
1227 if( config_GetInt( p_input, "audio-type" ) == REQUESTED_A52 )
1229 int i_a52 = i_audio;
1230 while( ( p_input->stream.pp_es[i_a52]->i_fourcc !=
1231 VLC_FOURCC('a','5','2','b') ) && ( i_a52 <=
1232 p_dvd->p_vts_file->vtsi_mat->nr_of_vts_audio_streams ) )
1236 if( p_input->stream.pp_es[i_a52]->i_fourcc
1237 == VLC_FOURCC('a','5','2','b') )
1239 input_SelectES( p_input, p_input->stream.pp_es[i_a52] );
1244 input_SelectES( p_input, p_input->stream.pp_es[i_audio] );
1248 /* for spu, default is none */
1249 i_spu = config_GetInt( p_input, "spu-channel" );
1250 if( i_spu < 0 /*|| i_spu > i_spu_nb*/ )
1252 config_PutInt( p_input, "spu-channel", 0 );
1255 if( i_spu > 0 /*&& i_spu_nb > 0*/ )
1257 i_spu += p_dvd->p_vts_file->vtsi_mat->nr_of_vts_audio_streams;
1258 input_SelectES( p_input, p_input->stream.pp_es[i_spu] );