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.3.2.1 2001/12/31 01:21:44 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 #define MODULE_NAME dvdread
32 #include "modules_inner.h"
34 /*****************************************************************************
36 *****************************************************************************/
47 #include <sys/types.h>
52 #ifdef STRNCASECMP_IN_STRINGS_H
57 # include <io.h> /* read() */
59 # include <sys/uio.h> /* struct iovec */
71 # include "input_iovec.h"
74 #include "stream_control.h"
75 #include "input_ext-intf.h"
76 #include "input_ext-dec.h"
77 #include "input_ext-plugins.h"
79 #include "input_dvdread.h"
84 #include "modules_export.h"
86 /* how many blocks DVDRead will read in each loop */
87 #define DVD_BLOCK_READ_ONCE 64
89 /*****************************************************************************
91 *****************************************************************************/
92 /* called from outside */
93 static int DvdReadProbe ( probedata_t *p_data );
94 static void DvdReadInit ( struct input_thread_s * );
95 static void DvdReadEnd ( struct input_thread_s * );
96 static void DvdReadOpen ( struct input_thread_s * );
97 static void DvdReadClose ( struct input_thread_s * );
98 static int DvdReadSetArea ( struct input_thread_s *, struct input_area_s * );
99 static int DvdReadSetProgram( struct input_thread_s *, pgrm_descriptor_t * );
100 static int DvdReadRead ( struct input_thread_s *, data_packet_t ** );
101 static void DvdReadSeek ( struct input_thread_s *, off_t );
102 static int DvdReadRewind ( struct input_thread_s * );
104 /* called only from here */
105 static void DvdReadHandleDSI( thread_dvd_data_t * p_dvd, u8 * p_data );
106 static void DvdReadFindCell ( thread_dvd_data_t * p_dvd );
108 /*****************************************************************************
109 * Declare a buffer manager
110 *****************************************************************************/
111 #define FLAGS BUFFERS_UNIQUE_SIZE
113 DECLARE_BUFFERS_SHARED( FLAGS, NB_LIFO );
114 DECLARE_BUFFERS_INIT( FLAGS, NB_LIFO );
115 DECLARE_BUFFERS_END_SHARED( FLAGS, NB_LIFO );
116 DECLARE_BUFFERS_NEWPACKET_SHARED( FLAGS, NB_LIFO );
117 DECLARE_BUFFERS_DELETEPACKET_SHARED( FLAGS, NB_LIFO, 1000 );
118 DECLARE_BUFFERS_NEWPES( FLAGS, NB_LIFO );
119 DECLARE_BUFFERS_DELETEPES( FLAGS, NB_LIFO, 1000 );
120 DECLARE_BUFFERS_TOIO( FLAGS, DVD_VIDEO_LB_LEN );
121 DECLARE_BUFFERS_SHAREBUFFER( FLAGS );
123 /*****************************************************************************
124 * Functions exported as capabilities. They are declared as static so that
125 * we don't pollute the namespace too much.
126 *****************************************************************************/
127 void _M( input_getfunctions )( function_list_t * p_function_list )
129 #define input p_function_list->functions.input
130 p_function_list->pf_probe = DvdReadProbe;
131 input.pf_init = DvdReadInit;
132 input.pf_open = DvdReadOpen;
133 input.pf_close = DvdReadClose;
134 input.pf_end = DvdReadEnd;
135 input.pf_init_bit_stream = InitBitstream;
136 input.pf_read = DvdReadRead;
137 input.pf_set_area = DvdReadSetArea;
138 input.pf_set_program = DvdReadSetProgram;
139 input.pf_demux = input_DemuxPS;
140 input.pf_new_packet = input_NewPacket;
141 input.pf_new_pes = input_NewPES;
142 input.pf_delete_packet = input_DeletePacket;
143 input.pf_delete_pes = input_DeletePES;
144 input.pf_rewind = DvdReadRewind;
145 input.pf_seek = DvdReadSeek;
150 * Data reading functions
153 /*****************************************************************************
154 * DvdReadProbe: verifies that the stream is a PS stream
155 *****************************************************************************/
156 static int DvdReadProbe( probedata_t *p_data )
158 input_thread_t * p_input = (input_thread_t *)p_data;
160 char * psz_name = p_input->p_source;
163 if( TestMethod( INPUT_METHOD_VAR, "dvdread" ) )
168 if( ( strlen(psz_name) > 8 ) && !strncasecmp( psz_name, "dvdread:", 8 ) )
170 /* If the user specified "dvdread:" then he probably wants
171 * to use libdvdread */
179 /*****************************************************************************
180 * DvdReadInit: initializes DVD structures
181 *****************************************************************************/
182 static void DvdReadInit( input_thread_t * p_input )
184 thread_dvd_data_t * p_dvd;
185 input_area_t * p_area;
190 p_dvd = malloc( sizeof(thread_dvd_data_t) );
193 intf_ErrMsg( "dvdread error: out of memory" );
194 p_input->b_error = 1;
198 /* we take the pointer to dvd_reader_t back */
199 p_dvd->p_dvdread = (dvd_reader_t *)p_input->p_plugin_data;
200 p_dvd->p_title = NULL;
201 p_dvd->p_vts_file = NULL;
203 p_input->p_plugin_data = (void *)p_dvd;
205 if( (p_input->p_method_data = input_BuffersInit()) == NULL )
208 p_input->b_error = 1;
212 /* We read DVD_BLOCK_READ_ONCE in each loop */
213 p_dvd->i_block_once = DVD_BLOCK_READ_ONCE;
215 /* Ifo allocation & initialisation */
216 if( ! ( p_dvd->p_vmg_file = ifoOpen( p_dvd->p_dvdread, 0 ) ) )
218 intf_ErrMsg( "dvdread error: can't open VMG info" );
219 input_BuffersEnd( p_input->p_method_data );
221 p_input->b_error = 1;
224 intf_WarnMsg( 2, "dvdread info: VMG opened" );
226 /* Set stream and area data */
227 vlc_mutex_lock( &p_input->stream.stream_lock );
229 /* Initialize ES structures */
230 input_InitStream( p_input, sizeof( stream_ps_data_t ) );
232 /* disc input method */
233 p_input->stream.i_method = INPUT_METHOD_DVD;
235 #define tt_srpt p_dvd->p_vmg_file->tt_srpt
236 intf_WarnMsg( 2, "dvdread info: number of titles: %d", tt_srpt->nr_of_srpts );
238 #define area p_input->stream.pp_areas
239 /* We start from 1 here since the default area 0
240 * is reserved for video_ts.vob */
241 for( i = 1 ; i <= tt_srpt->nr_of_srpts ; i++ )
243 input_AddArea( p_input );
245 /* Titles are Program Chains */
248 /* Absolute start offset and size
249 * We can only set that with vts ifo, so we do it during the
250 * first call to DVDSetArea */
251 area[i]->i_start = 0;
254 /* Number of chapters */
255 area[i]->i_part_nb = tt_srpt->title[i-1].nr_of_ptts;
258 /* Number of angles */
259 area[i]->i_angle_nb = 0;
260 area[i]->i_angle = 1;
262 area[i]->i_plugin_data = tt_srpt->title[i-1].title_set_nr;
266 /* Get requested title - if none try the first title */
267 i_title = main_GetIntVariable( INPUT_TITLE_VAR, 1 );
268 if( i_title <= 0 || i_title > tt_srpt->nr_of_srpts )
275 /* Get requested chapter - if none defaults to first one */
276 i_chapter = main_GetIntVariable( INPUT_CHAPTER_VAR, 1 );
282 p_input->stream.pp_areas[i_title]->i_part = i_chapter;
284 p_area = p_input->stream.pp_areas[i_title];
286 /* set title, chapter, audio and subpic */
287 DvdReadSetArea( p_input, p_area );
289 vlc_mutex_unlock( &p_input->stream.stream_lock );
294 /*****************************************************************************
295 * DvdReadOpen: open libdvdread
296 *****************************************************************************/
297 static void DvdReadOpen( struct input_thread_s *p_input )
299 dvd_reader_t *p_dvdread;
301 vlc_mutex_lock( &p_input->stream.stream_lock );
303 /* If we are here we can control the pace... */
304 p_input->stream.b_pace_control = 1;
306 p_input->stream.b_seekable = 1;
307 p_input->stream.p_selected_area->i_size = 0;
309 p_input->stream.p_selected_area->i_tell = 0;
311 vlc_mutex_unlock( &p_input->stream.stream_lock );
313 /* XXX: put this shit in an access plugin */
314 if( strlen( p_input->p_source ) > 8
315 && !strncasecmp( p_input->p_source, "dvdread:", 8 ) )
317 p_dvdread = DVDOpen( p_input->p_source + 8 );
321 p_dvdread = DVDOpen( p_input->p_source );
326 p_input->b_error = 1;
330 /* the vlc input doesn't handle the file descriptor with libdvdread */
331 p_input->i_handle = -1;
333 /* p_dvdread is stored in p_plugin_data until we have
334 * malloc'ed a dvd_thread_data_t */
335 p_input->p_plugin_data = (void *) p_dvdread;
338 /*****************************************************************************
339 * DvdReadClose: close libdvdread
340 *****************************************************************************/
341 static void DvdReadClose( struct input_thread_s *p_input )
343 DVDClose( ((thread_dvd_data_t*)p_input->p_plugin_data)->p_dvdread );
344 free( p_input->p_plugin_data );
345 p_input->p_plugin_data = NULL;
349 /*****************************************************************************
350 * DvdReadEnd: frees unused data
351 *****************************************************************************/
352 static void DvdReadEnd( input_thread_t * p_input )
354 thread_dvd_data_t * p_dvd;
356 p_dvd = (thread_dvd_data_t *)p_input->p_plugin_data;
358 /* close libdvdread */
359 DVDCloseFile( p_dvd->p_title );
360 ifoClose( p_dvd->p_vts_file );
361 ifoClose( p_dvd->p_vmg_file );
363 input_BuffersEnd( p_input->p_method_data );
366 /*****************************************************************************
367 * DvdReadSetProgram: Does nothing, a DVD is mono-program
368 *****************************************************************************/
369 static int DvdReadSetProgram( input_thread_t * p_input,
370 pgrm_descriptor_t * p_program )
375 #define p_pgc p_dvd->p_cur_pgc
377 /*****************************************************************************
378 * DvdReadSetArea: initialize input data for title x, chapter y.
379 * It should be called for each user navigation request.
380 *****************************************************************************
381 * Take care that i_title starts from 0 (vmg) and i_chapter start from 1.
382 * Note that you have to take the lock before entering here.
383 *****************************************************************************/
384 static int DvdReadSetArea( input_thread_t * p_input, input_area_t * p_area )
386 thread_dvd_data_t * p_dvd;
390 p_dvd = (thread_dvd_data_t*)p_input->p_plugin_data;
392 /* we can't use the interface slider until initilization is complete */
393 p_input->stream.b_seekable = 0;
395 if( p_area != p_input->stream.p_selected_area )
397 es_descriptor_t * p_es;
403 #define p_vmg p_dvd->p_vmg_file
404 #define p_vts p_dvd->p_vts_file
405 if( p_dvd->p_title != NULL )
407 DVDCloseFile( p_dvd->p_title );
415 /* Reset the Chapter position of the old title */
416 p_input->stream.p_selected_area->i_part = 1;
419 * We have to load all title information
421 /* Change the default area */
422 p_input->stream.p_selected_area = p_area;
424 intf_WarnMsg( 12, "dvdread: open VTS %d, for title %d",
425 p_vmg->tt_srpt->title[ p_area->i_id - 1 ].title_set_nr,
429 if( ! ( p_vts = ifoOpen( p_dvd->p_dvdread,
430 p_vmg->tt_srpt->title[ p_area->i_id - 1 ].title_set_nr ) ) )
432 intf_ErrMsg( "dvdread error: fatal error in vts ifo" );
434 DVDClose( p_dvd->p_dvdread );
435 p_input->b_error = 1;
439 /* title position inside the selected vts */
440 p_dvd->i_ttn = p_vmg->tt_srpt->title[ p_area->i_id - 1 ].vts_ttn;
443 * Set selected title start
445 pgc_id = p_vts->vts_ptt_srpt->title[p_dvd->i_ttn-1].ptt[0].pgcn;
446 pgn = p_vts->vts_ptt_srpt->title[p_dvd->i_ttn-1].ptt[0].pgn;
447 p_pgc = p_vts->vts_pgcit->pgci_srp[ pgc_id - 1 ].pgc;
448 i_cell = p_pgc->program_map[ pgn - 1 ] - 1;
451 LB2OFF( p_dvd->p_cur_pgc->cell_playback[ i_cell ].first_sector );
453 intf_WarnMsg( 3, "dvdread: start %d vts_title %d pgc %d pgn %d",
454 p_area->i_id, p_dvd->i_ttn, pgc_id, pgn );
459 i_cell = p_dvd->p_cur_pgc->nr_of_cells - 1;
461 p_dvd->i_end_block = p_pgc->cell_playback[ i_cell ].last_sector;
462 p_area->i_size = LB2OFF( p_dvd->i_end_block )- p_area->i_start;
464 intf_WarnMsg( 12, "dvdread: start %lld size %lld end %d",
465 p_area->i_start , p_area->i_size, p_dvd->i_end_block );
468 * Set properties for current chapter
470 /* Remeber current chapter */
471 p_dvd->i_chapter = p_area->i_part;
474 pgc_id = p_vts->vts_ptt_srpt->title[
475 p_dvd->i_ttn-1].ptt[p_area->i_part-1].pgcn;
476 pgn = p_vts->vts_ptt_srpt->title[
477 p_dvd->i_ttn-1].ptt[p_area->i_part-1].pgn;
479 p_pgc = p_vts->vts_pgcit->pgci_srp[pgc_id-1].pgc;
480 p_dvd->i_pack_len = 0;
481 p_dvd->i_next_cell = p_dvd->i_cur_cell = p_pgc->program_map[pgn-1] - 1;
482 DvdReadFindCell( p_dvd );
484 p_dvd->i_next_vobu = p_dvd->i_cur_block =
485 p_pgc->cell_playback[p_dvd->i_cur_cell].first_sector;
490 p_area->i_angle_nb = p_vmg->tt_srpt->title[p_area->i_id-1].nr_of_angles;
491 p_area->i_angle = main_GetIntVariable( INPUT_ANGLE_VAR, 1 );
493 if( ( p_area->i_angle <= 0 ) || p_area->i_angle > p_area->i_angle_nb )
497 p_dvd->i_angle = p_area->i_angle;
498 p_dvd->i_angle_nb = p_area->i_angle_nb;
501 * We've got enough info, time to open the title set data.
503 if( ! ( p_dvd->p_title = DVDOpenFile( p_dvd->p_dvdread,
504 p_vmg->tt_srpt->title[ p_area->i_id - 1 ].title_set_nr,
505 DVD_READ_TITLE_VOBS ) ) )
507 intf_ErrMsg( "dvdread error: can't open title (VTS_%02d_1.VOB)",
508 p_vmg->tt_srpt->title[p_area->i_id-1].title_set_nr );
511 DVDClose( p_dvd->p_dvdread );
515 // IfoPrintTitle( p_dvd );
518 * Destroy obsolete ES by reinitializing program 0
519 * and find all ES in title with ifo data
521 if( p_input->stream.pp_programs != NULL )
523 /* We don't use input_EndStream here since
524 * we keep area structures */
526 for( i = 0 ; i < p_input->stream.i_selected_es_number ; i++ )
528 input_UnselectES( p_input, p_input->stream.pp_selected_es[i] );
531 free( p_input->stream.pp_selected_es );
532 input_DelProgram( p_input, p_input->stream.p_selected_program );
534 p_input->stream.pp_selected_es = NULL;
535 p_input->stream.i_selected_es_number = 0;
538 input_AddProgram( p_input, 0, sizeof( stream_ps_data_t ) );
539 p_input->stream.p_selected_program = p_input->stream.pp_programs[0];
541 /* No PSM to read in DVD mode, we already have all information */
542 p_input->stream.p_selected_program->b_is_ok = 1;
546 /* ES 0 -> video MPEG2 */
547 // IfoPrintVideo( p_dvd );
549 p_es = input_AddES( p_input, p_input->stream.p_selected_program, 0xe0, 0 );
550 p_es->i_stream_id = 0xe0;
551 p_es->i_type = MPEG2_VIDEO_ES;
552 p_es->i_cat = VIDEO_ES;
553 if( p_main->b_video )
555 input_SelectES( p_input, p_es );
558 #define audio_control \
559 p_dvd->p_vts_file->vts_pgcit->pgci_srp[pgc_id-1].pgc->audio_control[i-1]
560 /* Audio ES, in the order they appear in .ifo */
561 for( i = 1 ; i <= p_vts->vtsi_mat->nr_of_vts_audio_streams ; i++ )
566 // IfoPrintAudio( p_dvd, i );
568 /* audio channel is active if first byte is 0x80 */
569 if( audio_control & 0x8000 )
572 i_position = ( audio_control & 0x7F00 ) >> 8;
574 intf_WarnMsg( 12, "dvd audio position %d", i_position );
575 switch( p_vts->vtsi_mat->vts_audio_attr[i-1].audio_format )
578 i_id = ( ( 0x80 + i_position ) << 8 ) | 0xbd;
579 p_es = input_AddES( p_input,
580 p_input->stream.p_selected_program, i_id, 0 );
581 p_es->i_stream_id = 0xbd;
582 p_es->i_type = AC3_AUDIO_ES;
584 p_es->i_cat = AUDIO_ES;
585 strcpy( p_es->psz_desc, DecodeLanguage( hton16(
586 p_vts->vtsi_mat->vts_audio_attr[i-1].lang_code ) ) );
587 strcat( p_es->psz_desc, " (ac3)" );
591 case 0x03: /* MPEG audio */
592 i_id = 0xc0 + i_position;
593 p_es = input_AddES( p_input,
594 p_input->stream.p_selected_program, i_id, 0 );
595 p_es->i_stream_id = i_id;
596 p_es->i_type = MPEG2_AUDIO_ES;
598 p_es->i_cat = AUDIO_ES;
599 strcpy( p_es->psz_desc, DecodeLanguage( hton16(
600 p_vts->vtsi_mat->vts_audio_attr[i-1].lang_code ) ) );
601 strcat( p_es->psz_desc, " (mpeg)" );
604 case 0x04: /* LPCM */
606 i_id = ( ( 0xa0 + i_position ) << 8 ) | 0xbd;
607 p_es = input_AddES( p_input,
608 p_input->stream.p_selected_program, i_id, 0 );
609 p_es->i_stream_id = i_id;
610 p_es->i_type = LPCM_AUDIO_ES;
612 p_es->i_cat = AUDIO_ES;
613 strcpy( p_es->psz_desc, DecodeLanguage( hton16(
614 p_vts->vtsi_mat->vts_audio_attr[i-1].lang_code ) ) );
615 strcat( p_es->psz_desc, " (lpcm)" );
619 i_id = ( ( 0x88 + i_position ) << 8 ) | 0xbd;
620 intf_ErrMsg( "dvd warning: DTS audio not handled yet"
625 intf_ErrMsg( "dvd warning: unknown audio type %.2x",
626 p_vts->vtsi_mat->vts_audio_attr[i-1].audio_format );
631 #define spu_control \
632 p_dvd->p_vts_file->vts_pgcit->pgci_srp[pgc_id-1].pgc->subp_control[i-1]
636 for( i = 1 ; i <= p_vts->vtsi_mat->nr_of_vts_subp_streams; i++ )
641 // IfoPrintSpu( p_dvd, i );
642 intf_WarnMsg( 12, "dvd spu %d 0x%02x", i, spu_control );
644 if( spu_control & 0x80000000 )
648 /* there are several streams for one spu */
649 if( p_vts->vtsi_mat->vts_video_attr.display_aspect_ratio )
652 switch( p_vts->vtsi_mat->vts_video_attr.permitted_df )
655 i_position = spu_control & 0xff;
658 i_position = ( spu_control >> 8 ) & 0xff;
661 i_position = ( spu_control >> 16 ) & 0xff;
668 i_position = ( spu_control >> 24 ) & 0x7F;
671 i_id = ( ( 0x20 + i_position ) << 8 ) | 0xbd;
672 p_es = input_AddES( p_input,
673 p_input->stream.p_selected_program, i_id, 0 );
674 p_es->i_stream_id = 0xbd;
675 p_es->i_type = DVD_SPU_ES;
676 p_es->i_cat = SPU_ES;
677 strcpy( p_es->psz_desc, DecodeLanguage( hton16(
678 p_vts->vtsi_mat->vts_subp_attr[i-1].lang_code ) ) );
682 if( p_main->b_audio )
684 /* For audio: first one if none or a not existing one specified */
685 int i_audio = main_GetIntVariable( INPUT_CHANNEL_VAR, 1 );
686 if( i_audio < 0 || i_audio > i_audio_nb )
688 main_PutIntVariable( INPUT_CHANNEL_VAR, 1 );
691 if( i_audio > 0 && i_audio_nb > 0 )
693 if( main_GetIntVariable( AOUT_SPDIF_VAR, 0 ) ||
694 ( main_GetIntVariable( INPUT_AUDIO_VAR, 0 ) ==
698 while( ( p_input->stream.pp_es[i_ac3]->i_type !=
699 AC3_AUDIO_ES ) && ( i_ac3 <=
700 p_vts->vtsi_mat->nr_of_vts_audio_streams ) )
704 if( p_input->stream.pp_es[i_ac3]->i_type == AC3_AUDIO_ES )
706 input_SelectES( p_input,
707 p_input->stream.pp_es[i_ac3] );
712 input_SelectES( p_input,
713 p_input->stream.pp_es[i_audio] );
718 if( p_main->b_video )
720 /* for spu, default is none */
721 int i_spu = main_GetIntVariable( INPUT_SUBTITLE_VAR, 0 );
722 if( i_spu < 0 || i_spu > i_spu_nb )
724 main_PutIntVariable( INPUT_SUBTITLE_VAR, 0 );
727 if( i_spu > 0 && i_spu_nb > 0 )
729 i_spu += p_vts->vtsi_mat->nr_of_vts_audio_streams;
730 input_SelectES( p_input, p_input->stream.pp_es[i_spu] );
736 p_area = p_input->stream.p_selected_area;
743 if( p_area->i_part != p_dvd->i_chapter )
745 if( ( p_area->i_part > 0 ) &&
746 ( p_area->i_part <= p_area->i_part_nb ))
748 p_dvd->i_ttn = p_vmg->tt_srpt->title[p_area->i_id-1].vts_ttn;
749 pgc_id = p_vts->vts_ptt_srpt->title[
750 p_dvd->i_ttn-1].ptt[p_area->i_part-1].pgcn;
751 pgn = p_vts->vts_ptt_srpt->title[
752 p_dvd->i_ttn-1].ptt[p_area->i_part-1].pgn;
754 p_pgc = p_vts->vts_pgcit->pgci_srp[ pgc_id - 1 ].pgc;
756 p_dvd->i_cur_cell = p_pgc->program_map[ pgn - 1 ] - 1;
757 p_dvd->i_chapter = p_area->i_part;
758 DvdReadFindCell( p_dvd );
760 p_dvd->i_pack_len = 0;
761 p_dvd->i_next_vobu = p_dvd->i_cur_block =
762 p_pgc->cell_playback[p_dvd->i_cur_cell].first_sector;
766 p_area->i_part = p_dvd->i_chapter;
772 if( p_area->i_angle != p_dvd->i_angle )
774 p_dvd->i_angle = p_area->i_angle;
776 intf_WarnMsg( 3, "dvd info: angle %d selected", p_area->i_angle );
778 /* warn interface that something has changed */
779 p_area->i_tell = LB2OFF( p_dvd->i_next_vobu ) - p_area->i_start;
780 p_input->stream.b_seekable = 1;
781 p_input->stream.b_changed = 1;
787 /*****************************************************************************
788 * DvdReadRead: reads data packets into the netlist.
789 *****************************************************************************
790 * Returns -1 in case of error, 0 if everything went well, and 1 in case of
792 *****************************************************************************/
793 static int DvdReadRead( input_thread_t * p_input,
794 data_packet_t ** pp_data )
796 thread_dvd_data_t * p_dvd;
797 u8 p_data[DVD_VIDEO_LB_LEN];
798 struct iovec p_vec[DVD_BLOCK_READ_ONCE];
806 data_packet_t * p_data_p;
809 p_dvd = (thread_dvd_data_t *)p_input->p_plugin_data;
814 * Playback by cell in this pgc, starting at the cell for our chapter.
818 * End of pack, we select the following one
820 if( ! p_dvd->i_pack_len )
825 if( ( i_read = DVDReadBlocks( p_dvd->p_title, p_dvd->i_next_vobu,
828 intf_ErrMsg( "dvdread error: read failed for block %d",
829 p_dvd->i_next_vobu );
833 /* basic check to be sure we don't have a empty title
834 * go to next title if so */
835 assert( p_data[41] == 0xbf && p_data[1027] == 0xbf );
836 if( p_data[41] == 0xbf && p_data[1027] == 0xbf )
839 * Parse the contained dsi packet.
842 DvdReadHandleDSI( p_dvd, p_data );
845 if( p_dvd->i_next_vobu >= p_dvd->i_end_block + 1 )
850 assert( p_dvd->i_pack_len < 1024 );
851 /* FIXME: Ugly kludge: we send the pack block to the input for it
852 * sometimes has a zero scr and restart the sync */
853 //p_dvd->i_cur_block ++;
859 p_dvd->i_pack_len = 0;
865 * Compute the number of blocks to read
867 i_blocks = p_dvd->i_pack_len >= DVD_BLOCK_READ_ONCE
868 ? DVD_BLOCK_READ_ONCE : p_dvd->i_pack_len;
869 p_dvd->i_pack_len -= i_blocks;
872 *pp_data = p_data_p = input_BuffersToIO( p_input->p_method_data, p_vec,
873 DVD_BLOCK_READ_ONCE );
875 if ( p_data_p == NULL )
881 i_read = DVDReadVBlocks( p_dvd->p_title, p_dvd->i_cur_block,
883 if( i_read != i_blocks )
885 intf_ErrMsg( "dvdread error: read failed for %d/%d blocks at 0x%02x",
886 i_read, i_blocks, p_dvd->i_cur_block );
890 p_dvd->i_cur_block += i_read;
892 intf_WarnMsg( 12, "dvdread i_blocks: %d len: %d current: 0x%02x", i_read, p_dvd->i_pack_len, p_dvd->i_cur_block );
896 /* Read headers to compute payload length */
897 for( i_iovec = 0 ; i_iovec < i_read ; i_iovec++ )
899 data_packet_t * p_current = p_data_p;
902 while( i_pos < DVD_VIDEO_LB_LEN )
904 pi_cur = (u8*)p_vec[i_iovec].iov_base + i_pos;
907 if( U32_AT( pi_cur ) != 0x1BA )
909 /* That's the case for all packets, except pack header. */
910 i_packet_size = U16_AT( pi_cur + 4 );
914 /* MPEG-2 Pack header. */
919 *pp_data = input_ShareBuffer( p_input->p_method_data,
925 p_data_p = p_data_p->p_next;
928 (*pp_data)->p_payload_start = (*pp_data)->p_demux_start =
929 (*pp_data)->p_demux_start + i_pos;
932 (*pp_data)->p_payload_end =
933 (*pp_data)->p_payload_start + i_packet_size + 6;
935 // pp_packets[i_packet]->p_next = NULL;
936 // pp_packets[i_packet]->b_discard_payload = 0;
939 i_pos += i_packet_size + 6;
940 pp_data = &(*pp_data)->p_next;
944 p_input->pf_delete_packet( p_input->p_method_data, p_data_p );
947 while( p_data_p != NULL )
949 data_packet_t * p_next = p_data_p->p_next;
950 p_input->pf_delete_packet( p_input->p_method_data, p_data_p );
954 vlc_mutex_lock( &p_input->stream.stream_lock );
956 p_input->stream.p_selected_area->i_tell =
957 LB2OFF( p_dvd->i_cur_block ) -
958 p_input->stream.p_selected_area->i_start;
962 /* We modify i_part only at end of chapter not to erase
963 * some modification from the interface */
964 p_input->stream.p_selected_area->i_part = p_dvd->i_chapter;
968 if( p_input->stream.p_selected_area->i_tell
969 >= p_input->stream.p_selected_area->i_size || b_eot )
971 if( ( p_input->stream.p_selected_area->i_id + 1 ) >=
972 p_input->stream.i_area_nb )
975 vlc_mutex_unlock( &p_input->stream.stream_lock );
980 intf_WarnMsg( 4, "dvd info: new title" );
981 DvdReadSetArea( p_input, p_input->stream.pp_areas[
982 p_input->stream.p_selected_area->i_id+1] );
983 vlc_mutex_unlock( &p_input->stream.stream_lock );
987 vlc_mutex_unlock( &p_input->stream.stream_lock );
993 /*****************************************************************************
994 * DVDRewind : reads a stream backward
995 *****************************************************************************/
996 static int DvdReadRewind( input_thread_t * p_input )
1001 /*****************************************************************************
1002 * DvdReadSeek : Goes to a given position on the stream.
1003 *****************************************************************************
1004 * This one is used by the input and translate chronological position from
1005 * input to logical position on the device.
1006 * The lock should be taken before calling this function.
1007 *****************************************************************************/
1008 static void DvdReadSeek( input_thread_t * p_input, off_t i_off )
1010 thread_dvd_data_t * p_dvd;
1018 i_off += p_input->stream.p_selected_area->i_start;
1019 i_lb = OFF2LB( i_off );
1020 p_dvd = ( thread_dvd_data_t * )p_input->p_plugin_data;
1023 while( p_dvd->p_cur_pgc->cell_playback[i_cell].last_sector < i_lb )
1035 pgc_id = p_dvd->p_vts_file->vts_ptt_srpt->title[
1036 p_dvd->i_ttn-1].ptt[i_chapter].pgcn;
1037 pgn = p_dvd->p_vts_file->vts_ptt_srpt->title[
1038 p_dvd->i_ttn-1].ptt[i_chapter].pgn;
1040 p_pgc = p_dvd->p_vts_file->vts_pgcit->pgci_srp[pgc_id-1].pgc;
1041 i_tmp = p_pgc->program_map[pgn-1];
1043 } while( i_tmp <= i_cell );
1046 while( p_dvd->p_vts_file->vts_vobu_admap->vobu_start_sectors[i_vobu]
1053 while( p_dvd->p_vts_file->vts_c_adt->cell_adr_table[i_sub_cell].start_sector <
1054 p_dvd->p_vts_file->vts_vobu_admap->vobu_start_sectors[i_vobu-1] )
1060 intf_WarnMsg(12, "cell %d i_sub_cell %d chapter %d vobu %d cell_sector %d vobu_sector %d sub_cell_sector %d",
1061 i_cell, i_sub_cell,i_chapter, i_vobu,
1062 p_dvd->p_cur_pgc->cell_playback[i_cell].first_sector,
1063 p_dvd->p_vts_file->vts_vobu_admap->vobu_start_sectors[i_vobu],
1064 p_dvd->p_vts_file->vts_c_adt->cell_adr_table[i_sub_cell-1].start_sector);
1066 p_dvd->i_cur_block = i_lb;
1067 p_dvd->i_next_vobu =
1068 p_dvd->p_vts_file->vts_vobu_admap->vobu_start_sectors[i_vobu];
1069 p_dvd->i_pack_len = p_dvd->i_next_vobu - i_lb;
1070 p_dvd->i_cur_cell = i_cell;
1071 p_dvd->i_chapter = i_chapter;
1072 DvdReadFindCell( p_dvd );
1074 p_input->stream.p_selected_area->i_tell =
1075 LB2OFF ( p_dvd->i_cur_block )
1076 - p_input->stream.p_selected_area->i_start;
1077 p_input->stream.p_selected_area->i_part = p_dvd->i_chapter;
1082 /*****************************************************************************
1084 *****************************************************************************/
1085 static void DvdReadHandleDSI( thread_dvd_data_t * p_dvd, u8 * p_data )
1087 navRead_DSI( &(p_dvd->dsi_pack), &(p_data[ DSI_START_BYTE ]) );
1090 * Determine where we go next. These values are the ones we mostly
1093 p_dvd->i_cur_block = p_dvd->dsi_pack.dsi_gi.nv_pck_lbn;
1096 * If we're not at the end of this cell, we can determine the next
1097 * VOBU to display using the VOBU_SRI information section of the
1098 * DSI. Using this value correctly follows the current angle,
1099 * avoiding the doubled scenes in The Matrix, and makes our life
1102 if( p_dvd->dsi_pack.vobu_sri.next_vobu != SRI_END_OF_CELL )
1105 switch( ( p_dvd->dsi_pack.sml_pbi.category & 0xf000 ) >> 12 )
1108 /* interleaved unit with no angle */
1109 if( p_dvd->dsi_pack.sml_pbi.ilvu_sa != -1 )
1111 p_dvd->i_next_vobu = p_dvd->i_cur_block +
1112 p_dvd->dsi_pack.sml_pbi.ilvu_sa;
1113 p_dvd->i_pack_len = p_dvd->dsi_pack.sml_pbi.ilvu_ea;
1117 p_dvd->i_next_vobu = p_dvd->i_cur_block +
1118 p_dvd->dsi_pack.dsi_gi.vobu_ea + 1;
1119 p_dvd->i_pack_len = p_dvd->dsi_pack.dsi_gi.vobu_ea;
1123 /* vobu is end of ilvu */
1124 if( p_dvd->dsi_pack.sml_agli.data[p_dvd->i_angle-1].address )
1126 p_dvd->i_next_vobu = p_dvd->i_cur_block +
1127 p_dvd->dsi_pack.sml_agli.data[p_dvd->i_angle-1].address;
1128 p_dvd->i_pack_len = p_dvd->dsi_pack.sml_pbi.ilvu_ea;
1133 /* vobu is beginning of ilvu */
1137 /* entering interleaved section */
1139 /* non interleaved cells in interleaved section */
1141 p_dvd->i_next_vobu = p_dvd->i_cur_block +
1142 ( p_dvd->dsi_pack.vobu_sri.next_vobu & 0x7fffffff );
1143 p_dvd->i_pack_len = p_dvd->dsi_pack.dsi_gi.vobu_ea;
1147 p_dvd->i_next_vobu = p_dvd->i_cur_block +
1148 ( p_dvd->dsi_pack.vobu_sri.next_vobu & 0x7fffffff );
1149 p_dvd->i_pack_len = p_dvd->dsi_pack.dsi_gi.vobu_ea;
1154 p_dvd->i_cur_cell = p_dvd->i_next_cell;
1155 DvdReadFindCell( p_dvd );
1157 p_dvd->i_pack_len = p_dvd->dsi_pack.dsi_gi.vobu_ea;
1158 p_dvd->i_next_vobu =
1159 p_dvd->p_cur_pgc->cell_playback[p_dvd->i_cur_cell].first_sector;
1163 intf_WarnMsg( 12, "scr %d lbn 0x%02x vobu_ea %d vob_id %d c_id %d",
1164 p_dvd->dsi_pack.dsi_gi.nv_pck_scr,
1165 p_dvd->dsi_pack.dsi_gi.nv_pck_lbn,
1166 p_dvd->dsi_pack.dsi_gi.vobu_ea,
1167 p_dvd->dsi_pack.dsi_gi.vobu_vob_idn,
1168 p_dvd->dsi_pack.dsi_gi.vobu_c_idn );
1170 intf_WarnMsg( 12, "cat 0x%02x ilvu_ea %d ilvu_sa %d size %d",
1171 p_dvd->dsi_pack.sml_pbi.category,
1172 p_dvd->dsi_pack.sml_pbi.ilvu_ea,
1173 p_dvd->dsi_pack.sml_pbi.ilvu_sa,
1174 p_dvd->dsi_pack.sml_pbi.size );
1176 intf_WarnMsg( 12, "next_vobu %d next_ilvu1 %d next_ilvu2 %d",
1177 p_dvd->dsi_pack.vobu_sri.next_vobu & 0x7fffffff,
1178 p_dvd->dsi_pack.sml_agli.data[ p_dvd->i_angle - 1 ].address,
1179 p_dvd->dsi_pack.sml_agli.data[ p_dvd->i_angle ].address);
1183 /*****************************************************************************
1185 *****************************************************************************/
1186 static void DvdReadFindCell( thread_dvd_data_t * p_dvd )
1191 #define cell p_dvd->p_cur_pgc->cell_playback
1192 if( cell[p_dvd->i_cur_cell].block_type == BLOCK_TYPE_ANGLE_BLOCK )
1195 p_dvd->i_next_cell = p_dvd->i_cur_cell + p_dvd->i_angle_nb;
1196 p_dvd->i_cur_cell += p_dvd->i_angle - 1;
1198 p_dvd->i_cur_cell += p_dvd->i_angle - 1;
1200 while( cell[p_dvd->i_cur_cell+i].block_mode != BLOCK_MODE_LAST_CELL )
1204 p_dvd->i_next_cell = p_dvd->i_cur_cell + i + 1;
1209 p_dvd->i_next_cell = p_dvd->i_cur_cell + 1;
1212 pgc_id = p_dvd->p_vts_file->vts_ptt_srpt->title[
1213 p_dvd->i_ttn-1].ptt[p_dvd->i_chapter].pgcn;
1214 pgn = p_dvd->p_vts_file->vts_ptt_srpt->title[
1215 p_dvd->i_ttn-1].ptt[p_dvd->i_chapter].pgn;
1216 p_pgc = p_dvd->p_vts_file->vts_pgcit->pgci_srp[pgc_id-1].pgc;
1218 if( p_pgc->program_map[pgn-1] <= p_dvd->i_cur_cell )