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.19 2002/01/15 19:01: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 DvdReadProbe ( probedata_t *p_data );
82 static void DvdReadInit ( struct input_thread_s * );
83 static void DvdReadEnd ( struct input_thread_s * );
84 static void DvdReadOpen ( struct input_thread_s * );
85 static void DvdReadClose ( struct input_thread_s * );
86 static int DvdReadSetArea ( struct input_thread_s *, struct input_area_s * );
87 static int DvdReadSetProgram( struct input_thread_s *, pgrm_descriptor_t * );
88 static int DvdReadRead ( struct input_thread_s *, data_packet_t ** );
89 static void DvdReadSeek ( struct input_thread_s *, off_t );
90 static int DvdReadRewind ( struct input_thread_s * );
92 /* called only from here */
93 static void DvdReadHandleDSI( thread_dvd_data_t * p_dvd, u8 * p_data );
94 static void DvdReadFindCell ( thread_dvd_data_t * p_dvd );
96 /*****************************************************************************
97 * Declare a buffer manager
98 *****************************************************************************/
99 #define FLAGS BUFFERS_UNIQUE_SIZE
101 DECLARE_BUFFERS_SHARED( FLAGS, NB_LIFO );
102 DECLARE_BUFFERS_INIT( FLAGS, NB_LIFO );
103 DECLARE_BUFFERS_END_SHARED( FLAGS, NB_LIFO );
104 DECLARE_BUFFERS_NEWPACKET_SHARED( FLAGS, NB_LIFO );
105 DECLARE_BUFFERS_DELETEPACKET_SHARED( FLAGS, NB_LIFO, 1000 );
106 DECLARE_BUFFERS_NEWPES( FLAGS, NB_LIFO );
107 DECLARE_BUFFERS_DELETEPES( FLAGS, NB_LIFO, 1000 );
108 DECLARE_BUFFERS_TOIO( FLAGS, DVD_VIDEO_LB_LEN );
109 DECLARE_BUFFERS_SHAREBUFFER( FLAGS );
111 /*****************************************************************************
112 * Functions exported as capabilities. They are declared as static so that
113 * we don't pollute the namespace too much.
114 *****************************************************************************/
115 void _M( input_getfunctions )( function_list_t * p_function_list )
117 #define input p_function_list->functions.input
118 p_function_list->pf_probe = DvdReadProbe;
119 input.pf_init = DvdReadInit;
120 input.pf_open = DvdReadOpen;
121 input.pf_close = DvdReadClose;
122 input.pf_end = DvdReadEnd;
123 input.pf_init_bit_stream = InitBitstream;
124 input.pf_read = DvdReadRead;
125 input.pf_set_area = DvdReadSetArea;
126 input.pf_set_program = DvdReadSetProgram;
127 input.pf_demux = input_DemuxPS;
128 input.pf_new_packet = input_NewPacket;
129 input.pf_new_pes = input_NewPES;
130 input.pf_delete_packet = input_DeletePacket;
131 input.pf_delete_pes = input_DeletePES;
132 input.pf_rewind = DvdReadRewind;
133 input.pf_seek = DvdReadSeek;
139 * Data reading functions
142 /*****************************************************************************
143 * DvdReadProbe: verifies that the stream is a PS stream
144 *****************************************************************************/
145 static int DvdReadProbe( probedata_t *p_data )
147 input_thread_t * p_input = (input_thread_t *)p_data;
149 char * psz_name = p_input->p_source;
151 if((( strlen(psz_name) > 8 ) && !strncasecmp( psz_name, "dvdread:", 8 ))
152 ||(( strlen(psz_name) > 4 ) && !strncasecmp( psz_name, "dvd:", 4 )))
160 /*****************************************************************************
161 * DvdReadInit: initializes DVD structures
162 *****************************************************************************/
163 static void DvdReadInit( input_thread_t * p_input )
165 thread_dvd_data_t * p_dvd;
166 input_area_t * p_area;
171 p_dvd = malloc( sizeof(thread_dvd_data_t) );
174 intf_ErrMsg( "dvdread error: out of memory" );
175 p_input->b_error = 1;
179 /* we take the pointer to dvd_reader_t back */
180 p_dvd->p_dvdread = (dvd_reader_t *)p_input->p_plugin_data;
181 p_dvd->p_title = NULL;
182 p_dvd->p_vts_file = NULL;
184 p_input->p_plugin_data = (void *)p_dvd;
186 if( (p_input->p_method_data = input_BuffersInit()) == NULL )
189 p_input->b_error = 1;
193 /* We read DVD_BLOCK_READ_ONCE in each loop */
194 p_dvd->i_block_once = DVD_BLOCK_READ_ONCE;
196 /* Ifo allocation & initialisation */
197 if( ! ( p_dvd->p_vmg_file = ifoOpen( p_dvd->p_dvdread, 0 ) ) )
199 intf_ErrMsg( "dvdread error: can't open VMG info" );
200 input_BuffersEnd( p_input->p_method_data );
202 p_input->b_error = 1;
205 intf_WarnMsg( 2, "dvdread info: VMG opened" );
207 /* Set stream and area data */
208 vlc_mutex_lock( &p_input->stream.stream_lock );
210 /* Initialize ES structures */
211 input_InitStream( p_input, sizeof( stream_ps_data_t ) );
213 /* disc input method */
214 p_input->stream.i_method = INPUT_METHOD_DVD;
216 #define tt_srpt p_dvd->p_vmg_file->tt_srpt
217 intf_WarnMsg( 2, "dvdread info: number of titles: %d", tt_srpt->nr_of_srpts );
219 #define area p_input->stream.pp_areas
220 /* We start from 1 here since the default area 0
221 * is reserved for video_ts.vob */
222 for( i = 1 ; i <= tt_srpt->nr_of_srpts ; i++ )
224 input_AddArea( p_input );
226 /* Titles are Program Chains */
229 /* Absolute start offset and size
230 * We can only set that with vts ifo, so we do it during the
231 * first call to DVDSetArea */
232 area[i]->i_start = 0;
235 /* Number of chapters */
236 area[i]->i_part_nb = tt_srpt->title[i-1].nr_of_ptts;
239 /* Number of angles */
240 area[i]->i_angle_nb = 0;
241 area[i]->i_angle = 1;
243 area[i]->i_plugin_data = tt_srpt->title[i-1].title_set_nr;
247 /* Get requested title - if none try the first title */
248 i_title = main_GetIntVariable( INPUT_TITLE_VAR, 1 );
249 if( i_title <= 0 || i_title > tt_srpt->nr_of_srpts )
256 /* Get requested chapter - if none defaults to first one */
257 i_chapter = main_GetIntVariable( INPUT_CHAPTER_VAR, 1 );
263 p_input->stream.pp_areas[i_title]->i_part = i_chapter;
265 p_area = p_input->stream.pp_areas[i_title];
267 /* set title, chapter, audio and subpic */
268 DvdReadSetArea( p_input, p_area );
270 vlc_mutex_unlock( &p_input->stream.stream_lock );
275 /*****************************************************************************
276 * DvdReadOpen: open libdvdread
277 *****************************************************************************/
278 static void DvdReadOpen( struct input_thread_s *p_input )
280 dvd_reader_t *p_dvdread;
282 vlc_mutex_lock( &p_input->stream.stream_lock );
284 p_input->stream.i_method = INPUT_METHOD_DVD;
286 /* If we are here we can control the pace... */
287 p_input->stream.b_pace_control = 1;
288 p_input->stream.b_seekable = 1;
290 p_input->stream.p_selected_area->i_size = 0;
291 p_input->stream.p_selected_area->i_tell = 0;
293 vlc_mutex_unlock( &p_input->stream.stream_lock );
295 /* XXX: put this shit in an access plugin */
296 if( strlen( p_input->p_source ) > 8
297 && !strncasecmp( p_input->p_source, "dvdread:", 8 ) )
299 p_dvdread = DVDOpen( p_input->p_source + 8 );
301 else if( strlen( p_input->p_source ) > 4
302 && !strncasecmp( p_input->p_source, "dvd:", 4 ) )
304 p_dvdread = DVDOpen( p_input->p_source + 4 );
308 p_dvdread = DVDOpen( p_input->p_source );
313 p_input->b_error = 1;
317 /* the vlc input doesn't handle the file descriptor with libdvdread */
318 p_input->i_handle = -1;
320 /* p_dvdread is stored in p_plugin_data until we have
321 * malloc'ed a dvd_thread_data_t */
322 p_input->p_plugin_data = (void *) p_dvdread;
325 /*****************************************************************************
326 * DvdReadClose: close libdvdread
327 *****************************************************************************/
328 static void DvdReadClose( struct input_thread_s *p_input )
330 DVDClose( ((thread_dvd_data_t*)p_input->p_plugin_data)->p_dvdread );
331 free( p_input->p_plugin_data );
332 p_input->p_plugin_data = NULL;
336 /*****************************************************************************
337 * DvdReadEnd: frees unused data
338 *****************************************************************************/
339 static void DvdReadEnd( input_thread_t * p_input )
341 thread_dvd_data_t * p_dvd;
343 p_dvd = (thread_dvd_data_t *)p_input->p_plugin_data;
345 /* close libdvdread */
346 DVDCloseFile( p_dvd->p_title );
347 ifoClose( p_dvd->p_vts_file );
348 ifoClose( p_dvd->p_vmg_file );
350 input_BuffersEnd( p_input->p_method_data );
353 /*****************************************************************************
354 * DvdReadSetProgram: Does nothing, a DVD is mono-program
355 *****************************************************************************/
356 static int DvdReadSetProgram( input_thread_t * p_input,
357 pgrm_descriptor_t * p_program )
362 #define p_pgc p_dvd->p_cur_pgc
364 /*****************************************************************************
365 * DvdReadSetArea: initialize input data for title x, chapter y.
366 * It should be called for each user navigation request.
367 *****************************************************************************
368 * Take care that i_title starts from 0 (vmg) and i_chapter start from 1.
369 * Note that you have to take the lock before entering here.
370 *****************************************************************************/
371 static int DvdReadSetArea( input_thread_t * p_input, input_area_t * p_area )
373 thread_dvd_data_t * p_dvd;
377 p_dvd = (thread_dvd_data_t*)p_input->p_plugin_data;
379 /* we can't use the interface slider until initilization is complete */
380 p_input->stream.b_seekable = 0;
382 if( p_area != p_input->stream.p_selected_area )
384 es_descriptor_t * p_es;
390 #define p_vmg p_dvd->p_vmg_file
391 #define p_vts p_dvd->p_vts_file
392 if( p_dvd->p_title != NULL )
394 DVDCloseFile( p_dvd->p_title );
402 /* Reset the Chapter position of the old title */
403 p_input->stream.p_selected_area->i_part = 1;
406 * We have to load all title information
408 /* Change the default area */
409 p_input->stream.p_selected_area = p_area;
411 intf_WarnMsg( 12, "dvdread: open VTS %d, for title %d",
412 p_vmg->tt_srpt->title[ p_area->i_id - 1 ].title_set_nr,
416 if( ! ( p_vts = ifoOpen( p_dvd->p_dvdread,
417 p_vmg->tt_srpt->title[ p_area->i_id - 1 ].title_set_nr ) ) )
419 intf_ErrMsg( "dvdread error: fatal error in vts ifo" );
421 DVDClose( p_dvd->p_dvdread );
422 p_input->b_error = 1;
426 /* title position inside the selected vts */
427 p_dvd->i_ttn = p_vmg->tt_srpt->title[ p_area->i_id - 1 ].vts_ttn;
430 * Set selected title start
432 pgc_id = p_vts->vts_ptt_srpt->title[p_dvd->i_ttn-1].ptt[0].pgcn;
433 pgn = p_vts->vts_ptt_srpt->title[p_dvd->i_ttn-1].ptt[0].pgn;
434 p_pgc = p_vts->vts_pgcit->pgci_srp[ pgc_id - 1 ].pgc;
435 i_cell = p_pgc->program_map[ pgn - 1 ] - 1;
438 LB2OFF( p_dvd->p_cur_pgc->cell_playback[ i_cell ].first_sector );
440 intf_WarnMsg( 3, "dvdread: start %d vts_title %d pgc %d pgn %d",
441 p_area->i_id, p_dvd->i_ttn, pgc_id, pgn );
446 i_cell = p_dvd->p_cur_pgc->nr_of_cells - 1;
448 p_dvd->i_end_block = p_pgc->cell_playback[ i_cell ].last_sector;
449 p_area->i_size = LB2OFF( p_dvd->i_end_block )- p_area->i_start;
451 intf_WarnMsg( 12, "dvdread: start %lld size %lld end %d",
452 p_area->i_start , p_area->i_size, p_dvd->i_end_block );
455 * Set properties for current chapter
457 /* Remeber current chapter */
458 p_dvd->i_chapter = p_area->i_part;
461 pgc_id = p_vts->vts_ptt_srpt->title[
462 p_dvd->i_ttn-1].ptt[p_area->i_part-1].pgcn;
463 pgn = p_vts->vts_ptt_srpt->title[
464 p_dvd->i_ttn-1].ptt[p_area->i_part-1].pgn;
466 p_pgc = p_vts->vts_pgcit->pgci_srp[pgc_id-1].pgc;
467 p_dvd->i_pack_len = 0;
468 p_dvd->i_next_cell = p_dvd->i_cur_cell = p_pgc->program_map[pgn-1] - 1;
469 DvdReadFindCell( p_dvd );
471 p_dvd->i_next_vobu = p_dvd->i_cur_block =
472 p_pgc->cell_playback[p_dvd->i_cur_cell].first_sector;
477 p_area->i_angle_nb = p_vmg->tt_srpt->title[p_area->i_id-1].nr_of_angles;
478 p_area->i_angle = main_GetIntVariable( INPUT_ANGLE_VAR, 1 );
480 if( ( p_area->i_angle <= 0 ) || p_area->i_angle > p_area->i_angle_nb )
484 p_dvd->i_angle = p_area->i_angle;
485 p_dvd->i_angle_nb = p_area->i_angle_nb;
488 * We've got enough info, time to open the title set data.
490 if( ! ( p_dvd->p_title = DVDOpenFile( p_dvd->p_dvdread,
491 p_vmg->tt_srpt->title[ p_area->i_id - 1 ].title_set_nr,
492 DVD_READ_TITLE_VOBS ) ) )
494 intf_ErrMsg( "dvdread error: can't open title (VTS_%02d_1.VOB)",
495 p_vmg->tt_srpt->title[p_area->i_id-1].title_set_nr );
498 DVDClose( p_dvd->p_dvdread );
502 // IfoPrintTitle( p_dvd );
505 * Destroy obsolete ES by reinitializing program 0
506 * and find all ES in title with ifo data
508 if( p_input->stream.pp_programs != NULL )
510 /* We don't use input_EndStream here since
511 * we keep area structures */
513 for( i = 0 ; i < p_input->stream.i_selected_es_number ; i++ )
515 input_UnselectES( p_input, p_input->stream.pp_selected_es[i] );
518 free( p_input->stream.pp_selected_es );
519 input_DelProgram( p_input, p_input->stream.p_selected_program );
521 p_input->stream.pp_selected_es = NULL;
522 p_input->stream.i_selected_es_number = 0;
525 input_AddProgram( p_input, 0, sizeof( stream_ps_data_t ) );
526 p_input->stream.p_selected_program = p_input->stream.pp_programs[0];
528 /* No PSM to read in DVD mode, we already have all information */
529 p_input->stream.p_selected_program->b_is_ok = 1;
533 /* ES 0 -> video MPEG2 */
534 // IfoPrintVideo( p_dvd );
536 p_es = input_AddES( p_input, p_input->stream.p_selected_program, 0xe0, 0 );
537 p_es->i_stream_id = 0xe0;
538 p_es->i_type = MPEG2_VIDEO_ES;
539 p_es->i_cat = VIDEO_ES;
540 if( p_main->b_video )
542 input_SelectES( p_input, p_es );
545 #define audio_control \
546 p_dvd->p_vts_file->vts_pgcit->pgci_srp[pgc_id-1].pgc->audio_control[i-1]
547 /* Audio ES, in the order they appear in .ifo */
548 for( i = 1 ; i <= p_vts->vtsi_mat->nr_of_vts_audio_streams ; i++ )
553 // IfoPrintAudio( p_dvd, i );
555 /* audio channel is active if first byte is 0x80 */
556 if( audio_control & 0x8000 )
559 i_position = ( audio_control & 0x7F00 ) >> 8;
561 intf_WarnMsg( 12, "dvd audio position %d", i_position );
562 switch( p_vts->vtsi_mat->vts_audio_attr[i-1].audio_format )
565 i_id = ( ( 0x80 + i_position ) << 8 ) | 0xbd;
566 p_es = input_AddES( p_input,
567 p_input->stream.p_selected_program, i_id, 0 );
568 p_es->i_stream_id = 0xbd;
569 p_es->i_type = AC3_AUDIO_ES;
571 p_es->i_cat = AUDIO_ES;
572 strcpy( p_es->psz_desc, DecodeLanguage( hton16(
573 p_vts->vtsi_mat->vts_audio_attr[i-1].lang_code ) ) );
574 strcat( p_es->psz_desc, " (ac3)" );
578 case 0x03: /* MPEG audio */
579 i_id = 0xc0 + i_position;
580 p_es = input_AddES( p_input,
581 p_input->stream.p_selected_program, i_id, 0 );
582 p_es->i_stream_id = i_id;
583 p_es->i_type = MPEG2_AUDIO_ES;
585 p_es->i_cat = AUDIO_ES;
586 strcpy( p_es->psz_desc, DecodeLanguage( hton16(
587 p_vts->vtsi_mat->vts_audio_attr[i-1].lang_code ) ) );
588 strcat( p_es->psz_desc, " (mpeg)" );
591 case 0x04: /* LPCM */
593 i_id = ( ( 0xa0 + i_position ) << 8 ) | 0xbd;
594 p_es = input_AddES( p_input,
595 p_input->stream.p_selected_program, i_id, 0 );
596 p_es->i_stream_id = i_id;
597 p_es->i_type = LPCM_AUDIO_ES;
599 p_es->i_cat = AUDIO_ES;
600 strcpy( p_es->psz_desc, DecodeLanguage( hton16(
601 p_vts->vtsi_mat->vts_audio_attr[i-1].lang_code ) ) );
602 strcat( p_es->psz_desc, " (lpcm)" );
606 i_id = ( ( 0x88 + i_position ) << 8 ) | 0xbd;
607 intf_ErrMsg( "dvd warning: DTS audio not handled yet"
612 intf_ErrMsg( "dvd warning: unknown audio type %.2x",
613 p_vts->vtsi_mat->vts_audio_attr[i-1].audio_format );
618 #define spu_control \
619 p_dvd->p_vts_file->vts_pgcit->pgci_srp[pgc_id-1].pgc->subp_control[i-1]
623 for( i = 1 ; i <= p_vts->vtsi_mat->nr_of_vts_subp_streams; i++ )
628 // IfoPrintSpu( p_dvd, i );
629 intf_WarnMsg( 12, "dvd spu %d 0x%02x", i, spu_control );
631 if( spu_control & 0x80000000 )
635 /* there are several streams for one spu */
636 if( p_vts->vtsi_mat->vts_video_attr.display_aspect_ratio )
639 switch( p_vts->vtsi_mat->vts_video_attr.permitted_df )
642 i_position = spu_control & 0xff;
645 i_position = ( spu_control >> 8 ) & 0xff;
648 i_position = ( spu_control >> 16 ) & 0xff;
655 i_position = ( spu_control >> 24 ) & 0x7F;
658 i_id = ( ( 0x20 + i_position ) << 8 ) | 0xbd;
659 p_es = input_AddES( p_input,
660 p_input->stream.p_selected_program, i_id, 0 );
661 p_es->i_stream_id = 0xbd;
662 p_es->i_type = DVD_SPU_ES;
663 p_es->i_cat = SPU_ES;
664 strcpy( p_es->psz_desc, DecodeLanguage( hton16(
665 p_vts->vtsi_mat->vts_subp_attr[i-1].lang_code ) ) );
669 if( p_main->b_audio )
671 /* For audio: first one if none or a not existing one specified */
672 int i_audio = main_GetIntVariable( INPUT_CHANNEL_VAR, 1 );
673 if( i_audio < 0 || i_audio > i_audio_nb )
675 main_PutIntVariable( INPUT_CHANNEL_VAR, 1 );
678 if( i_audio > 0 && i_audio_nb > 0 )
680 if( main_GetIntVariable( AOUT_SPDIF_VAR, 0 ) ||
681 ( main_GetIntVariable( INPUT_AUDIO_VAR, 0 ) ==
685 while( ( p_input->stream.pp_es[i_ac3]->i_type !=
686 AC3_AUDIO_ES ) && ( i_ac3 <=
687 p_vts->vtsi_mat->nr_of_vts_audio_streams ) )
691 if( p_input->stream.pp_es[i_ac3]->i_type == AC3_AUDIO_ES )
693 input_SelectES( p_input,
694 p_input->stream.pp_es[i_ac3] );
699 input_SelectES( p_input,
700 p_input->stream.pp_es[i_audio] );
705 if( p_main->b_video )
707 /* for spu, default is none */
708 int i_spu = main_GetIntVariable( INPUT_SUBTITLE_VAR, 0 );
709 if( i_spu < 0 || i_spu > i_spu_nb )
711 main_PutIntVariable( INPUT_SUBTITLE_VAR, 0 );
714 if( i_spu > 0 && i_spu_nb > 0 )
716 i_spu += p_vts->vtsi_mat->nr_of_vts_audio_streams;
717 input_SelectES( p_input, p_input->stream.pp_es[i_spu] );
723 p_area = p_input->stream.p_selected_area;
730 if( p_area->i_part != p_dvd->i_chapter )
732 if( ( p_area->i_part > 0 ) &&
733 ( p_area->i_part <= p_area->i_part_nb ))
735 p_dvd->i_ttn = p_vmg->tt_srpt->title[p_area->i_id-1].vts_ttn;
736 pgc_id = p_vts->vts_ptt_srpt->title[
737 p_dvd->i_ttn-1].ptt[p_area->i_part-1].pgcn;
738 pgn = p_vts->vts_ptt_srpt->title[
739 p_dvd->i_ttn-1].ptt[p_area->i_part-1].pgn;
741 p_pgc = p_vts->vts_pgcit->pgci_srp[ pgc_id - 1 ].pgc;
743 p_dvd->i_cur_cell = p_pgc->program_map[ pgn - 1 ] - 1;
744 p_dvd->i_chapter = p_area->i_part;
745 DvdReadFindCell( p_dvd );
747 p_dvd->i_pack_len = 0;
748 p_dvd->i_next_vobu = p_dvd->i_cur_block =
749 p_pgc->cell_playback[p_dvd->i_cur_cell].first_sector;
753 p_area->i_part = p_dvd->i_chapter;
759 if( p_area->i_angle != p_dvd->i_angle )
761 p_dvd->i_angle = p_area->i_angle;
763 intf_WarnMsg( 3, "dvd info: angle %d selected", p_area->i_angle );
765 /* warn interface that something has changed */
766 p_area->i_tell = LB2OFF( p_dvd->i_next_vobu ) - p_area->i_start;
767 p_input->stream.b_seekable = 1;
768 p_input->stream.b_changed = 1;
774 /*****************************************************************************
775 * DvdReadRead: reads data packets into the netlist.
776 *****************************************************************************
777 * Returns -1 in case of error, 0 if everything went well, and 1 in case of
779 *****************************************************************************/
780 static int DvdReadRead( input_thread_t * p_input,
781 data_packet_t ** pp_data )
783 thread_dvd_data_t * p_dvd;
784 u8 p_data[DVD_VIDEO_LB_LEN];
785 struct iovec p_vec[DVD_BLOCK_READ_ONCE];
793 data_packet_t * p_data_p;
796 p_dvd = (thread_dvd_data_t *)p_input->p_plugin_data;
801 * Playback by cell in this pgc, starting at the cell for our chapter.
805 * End of pack, we select the following one
807 if( ! p_dvd->i_pack_len )
812 if( ( i_read = DVDReadBlocks( p_dvd->p_title, p_dvd->i_next_vobu,
815 intf_ErrMsg( "dvdread error: read failed for block %d",
816 p_dvd->i_next_vobu );
820 /* basic check to be sure we don't have a empty title
821 * go to next title if so */
822 assert( p_data[41] == 0xbf && p_data[1027] == 0xbf );
823 if( p_data[41] == 0xbf && p_data[1027] == 0xbf )
826 * Parse the contained dsi packet.
829 DvdReadHandleDSI( p_dvd, p_data );
832 if( p_dvd->i_next_vobu >= p_dvd->i_end_block + 1 )
837 assert( p_dvd->i_pack_len < 1024 );
838 /* FIXME: Ugly kludge: we send the pack block to the input for it
839 * sometimes has a zero scr and restart the sync */
840 //p_dvd->i_cur_block ++;
846 p_dvd->i_pack_len = 0;
852 * Compute the number of blocks to read
854 i_blocks = p_dvd->i_pack_len >= DVD_BLOCK_READ_ONCE
855 ? DVD_BLOCK_READ_ONCE : p_dvd->i_pack_len;
856 p_dvd->i_pack_len -= i_blocks;
859 *pp_data = p_data_p = input_BuffersToIO( p_input->p_method_data, p_vec,
860 DVD_BLOCK_READ_ONCE );
862 if ( p_data_p == NULL )
868 i_read = DVDReadVBlocks( p_dvd->p_title, p_dvd->i_cur_block,
870 if( i_read != i_blocks )
872 intf_ErrMsg( "dvdread error: read failed for %d/%d blocks at 0x%02x",
873 i_read, i_blocks, p_dvd->i_cur_block );
877 p_dvd->i_cur_block += i_read;
879 intf_WarnMsg( 12, "dvdread i_blocks: %d len: %d current: 0x%02x", i_read, p_dvd->i_pack_len, p_dvd->i_cur_block );
883 /* Read headers to compute payload length */
884 for( i_iovec = 0 ; i_iovec < i_read ; i_iovec++ )
886 data_packet_t * p_current = p_data_p;
889 while( i_pos < DVD_VIDEO_LB_LEN )
891 pi_cur = (u8*)p_vec[i_iovec].iov_base + i_pos;
894 if( U32_AT( pi_cur ) != 0x1BA )
896 /* That's the case for all packets, except pack header. */
897 i_packet_size = U16_AT( pi_cur + 4 );
901 /* MPEG-2 Pack header. */
906 *pp_data = input_ShareBuffer( p_input->p_method_data,
912 p_data_p = p_data_p->p_next;
915 (*pp_data)->p_payload_start = (*pp_data)->p_demux_start =
916 (*pp_data)->p_demux_start + i_pos;
919 (*pp_data)->p_payload_end =
920 (*pp_data)->p_payload_start + i_packet_size + 6;
922 // pp_packets[i_packet]->p_next = NULL;
923 // pp_packets[i_packet]->b_discard_payload = 0;
926 i_pos += i_packet_size + 6;
927 pp_data = &(*pp_data)->p_next;
931 p_input->pf_delete_packet( p_input->p_method_data, p_data_p );
934 vlc_mutex_lock( &p_input->stream.stream_lock );
936 p_input->stream.p_selected_area->i_tell =
937 LB2OFF( p_dvd->i_cur_block ) -
938 p_input->stream.p_selected_area->i_start;
942 /* We modify i_part only at end of chapter not to erase
943 * some modification from the interface */
944 p_input->stream.p_selected_area->i_part = p_dvd->i_chapter;
948 if( p_input->stream.p_selected_area->i_tell
949 >= p_input->stream.p_selected_area->i_size || b_eot )
951 if( ( p_input->stream.p_selected_area->i_id + 1 ) >=
952 p_input->stream.i_area_nb )
955 vlc_mutex_unlock( &p_input->stream.stream_lock );
960 intf_WarnMsg( 4, "dvd info: new title" );
961 DvdReadSetArea( p_input, p_input->stream.pp_areas[
962 p_input->stream.p_selected_area->i_id+1] );
963 vlc_mutex_unlock( &p_input->stream.stream_lock );
967 vlc_mutex_unlock( &p_input->stream.stream_lock );
973 /*****************************************************************************
974 * DVDRewind : reads a stream backward
975 *****************************************************************************/
976 static int DvdReadRewind( input_thread_t * p_input )
981 /*****************************************************************************
982 * DvdReadSeek : Goes to a given position on the stream.
983 *****************************************************************************
984 * This one is used by the input and translate chronological position from
985 * input to logical position on the device.
986 * The lock should be taken before calling this function.
987 *****************************************************************************/
988 static void DvdReadSeek( input_thread_t * p_input, off_t i_off )
990 thread_dvd_data_t * p_dvd;
998 i_off += p_input->stream.p_selected_area->i_start;
999 i_lb = OFF2LB( i_off );
1000 p_dvd = ( thread_dvd_data_t * )p_input->p_plugin_data;
1003 while( p_dvd->p_cur_pgc->cell_playback[i_cell].last_sector < i_lb )
1015 pgc_id = p_dvd->p_vts_file->vts_ptt_srpt->title[
1016 p_dvd->i_ttn-1].ptt[i_chapter-1].pgcn;
1017 pgn = p_dvd->p_vts_file->vts_ptt_srpt->title[
1018 p_dvd->i_ttn-1].ptt[i_chapter-1].pgn;
1020 p_pgc = p_dvd->p_vts_file->vts_pgcit->pgci_srp[pgc_id-1].pgc;
1021 i_tmp = p_pgc->program_map[pgn-1];
1023 } while( i_tmp <= i_cell );
1026 while( p_dvd->p_vts_file->vts_vobu_admap->vobu_start_sectors[i_vobu]
1033 while( p_dvd->p_vts_file->vts_c_adt->cell_adr_table[i_sub_cell].start_sector <
1034 p_dvd->p_vts_file->vts_vobu_admap->vobu_start_sectors[i_vobu-1] )
1040 intf_WarnMsg(12, "cell %d i_sub_cell %d chapter %d vobu %d cell_sector %d vobu_sector %d sub_cell_sector %d",
1041 i_cell, i_sub_cell,i_chapter, i_vobu,
1042 p_dvd->p_cur_pgc->cell_playback[i_cell].first_sector,
1043 p_dvd->p_vts_file->vts_vobu_admap->vobu_start_sectors[i_vobu],
1044 p_dvd->p_vts_file->vts_c_adt->cell_adr_table[i_sub_cell-1].start_sector);
1046 p_dvd->i_cur_block = i_lb;
1047 p_dvd->i_next_vobu =
1048 p_dvd->p_vts_file->vts_vobu_admap->vobu_start_sectors[i_vobu];
1049 p_dvd->i_pack_len = p_dvd->i_next_vobu - i_lb;
1050 p_dvd->i_cur_cell = i_cell;
1051 p_dvd->i_chapter = i_chapter;
1052 DvdReadFindCell( p_dvd );
1054 p_input->stream.p_selected_area->i_tell =
1055 LB2OFF ( p_dvd->i_cur_block )
1056 - p_input->stream.p_selected_area->i_start;
1057 p_input->stream.p_selected_area->i_part = p_dvd->i_chapter;
1062 /*****************************************************************************
1064 *****************************************************************************/
1065 static void DvdReadHandleDSI( thread_dvd_data_t * p_dvd, u8 * p_data )
1067 navRead_DSI( &(p_dvd->dsi_pack), &(p_data[ DSI_START_BYTE ]) );
1070 * Determine where we go next. These values are the ones we mostly
1073 p_dvd->i_cur_block = p_dvd->dsi_pack.dsi_gi.nv_pck_lbn;
1076 * If we're not at the end of this cell, we can determine the next
1077 * VOBU to display using the VOBU_SRI information section of the
1078 * DSI. Using this value correctly follows the current angle,
1079 * avoiding the doubled scenes in The Matrix, and makes our life
1082 if( p_dvd->dsi_pack.vobu_sri.next_vobu != SRI_END_OF_CELL )
1085 switch( ( p_dvd->dsi_pack.sml_pbi.category & 0xf000 ) >> 12 )
1088 /* interleaved unit with no angle */
1089 if( p_dvd->dsi_pack.sml_pbi.ilvu_sa != -1 )
1091 p_dvd->i_next_vobu = p_dvd->i_cur_block +
1092 p_dvd->dsi_pack.sml_pbi.ilvu_sa;
1093 p_dvd->i_pack_len = p_dvd->dsi_pack.sml_pbi.ilvu_ea;
1097 p_dvd->i_next_vobu = p_dvd->i_cur_block +
1098 p_dvd->dsi_pack.dsi_gi.vobu_ea + 1;
1099 p_dvd->i_pack_len = p_dvd->dsi_pack.dsi_gi.vobu_ea;
1103 /* vobu is end of ilvu */
1104 if( p_dvd->dsi_pack.sml_agli.data[p_dvd->i_angle-1].address )
1106 p_dvd->i_next_vobu = p_dvd->i_cur_block +
1107 p_dvd->dsi_pack.sml_agli.data[p_dvd->i_angle-1].address;
1108 p_dvd->i_pack_len = p_dvd->dsi_pack.sml_pbi.ilvu_ea;
1113 /* vobu is beginning of ilvu */
1117 /* entering interleaved section */
1119 /* non interleaved cells in interleaved section */
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;
1127 p_dvd->i_next_vobu = p_dvd->i_cur_block +
1128 ( p_dvd->dsi_pack.vobu_sri.next_vobu & 0x7fffffff );
1129 p_dvd->i_pack_len = p_dvd->dsi_pack.dsi_gi.vobu_ea;
1134 p_dvd->i_cur_cell = p_dvd->i_next_cell;
1135 DvdReadFindCell( p_dvd );
1137 p_dvd->i_pack_len = p_dvd->dsi_pack.dsi_gi.vobu_ea;
1138 p_dvd->i_next_vobu =
1139 p_dvd->p_cur_pgc->cell_playback[p_dvd->i_cur_cell].first_sector;
1143 intf_WarnMsg( 12, "scr %d lbn 0x%02x vobu_ea %d vob_id %d c_id %d",
1144 p_dvd->dsi_pack.dsi_gi.nv_pck_scr,
1145 p_dvd->dsi_pack.dsi_gi.nv_pck_lbn,
1146 p_dvd->dsi_pack.dsi_gi.vobu_ea,
1147 p_dvd->dsi_pack.dsi_gi.vobu_vob_idn,
1148 p_dvd->dsi_pack.dsi_gi.vobu_c_idn );
1150 intf_WarnMsg( 12, "cat 0x%02x ilvu_ea %d ilvu_sa %d size %d",
1151 p_dvd->dsi_pack.sml_pbi.category,
1152 p_dvd->dsi_pack.sml_pbi.ilvu_ea,
1153 p_dvd->dsi_pack.sml_pbi.ilvu_sa,
1154 p_dvd->dsi_pack.sml_pbi.size );
1156 intf_WarnMsg( 12, "next_vobu %d next_ilvu1 %d next_ilvu2 %d",
1157 p_dvd->dsi_pack.vobu_sri.next_vobu & 0x7fffffff,
1158 p_dvd->dsi_pack.sml_agli.data[ p_dvd->i_angle - 1 ].address,
1159 p_dvd->dsi_pack.sml_agli.data[ p_dvd->i_angle ].address);
1163 /*****************************************************************************
1165 *****************************************************************************/
1166 static void DvdReadFindCell( thread_dvd_data_t * p_dvd )
1171 #define cell p_dvd->p_cur_pgc->cell_playback
1172 if( cell[p_dvd->i_cur_cell].block_type == BLOCK_TYPE_ANGLE_BLOCK )
1175 p_dvd->i_next_cell = p_dvd->i_cur_cell + p_dvd->i_angle_nb;
1176 p_dvd->i_cur_cell += p_dvd->i_angle - 1;
1178 p_dvd->i_cur_cell += p_dvd->i_angle - 1;
1180 while( cell[p_dvd->i_cur_cell+i].block_mode != BLOCK_MODE_LAST_CELL )
1184 p_dvd->i_next_cell = p_dvd->i_cur_cell + i + 1;
1189 p_dvd->i_next_cell = p_dvd->i_cur_cell + 1;
1192 pgc_id = p_dvd->p_vts_file->vts_ptt_srpt->title[
1193 p_dvd->i_ttn-1].ptt[p_dvd->i_chapter-1].pgcn;
1194 pgn = p_dvd->p_vts_file->vts_ptt_srpt->title[
1195 p_dvd->i_ttn-1].ptt[p_dvd->i_chapter-1].pgn;
1196 p_pgc = p_dvd->p_vts_file->vts_pgcit->pgci_srp[pgc_id-1].pgc;
1198 if( p_pgc->program_map[pgn-1] <= p_dvd->i_cur_cell )