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.1 2001/11/25 05:04:38 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 #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"
78 #include "stream_control.h"
79 #include "input_ext-intf.h"
80 #include "input_ext-dec.h"
81 #include "input_ext-plugins.h"
83 #include "input_dvdread.h"
88 #include "modules_export.h"
90 /* how many blocks DVDRead will read in each loop */
91 #define DVD_BLOCK_READ_ONCE 64
92 #define DVD_DATA_READ_ONCE (4 * DVD_BLOCK_READ_ONCE)
95 #define DVD_NETLIST_SIZE 512
97 /*****************************************************************************
99 *****************************************************************************/
100 /* called from outside */
101 static int DvdReadProbe ( probedata_t *p_data );
102 static void DvdReadInit ( struct input_thread_s * );
103 static void DvdReadEnd ( struct input_thread_s * );
104 static void DvdReadOpen ( struct input_thread_s * );
105 static void DvdReadClose ( struct input_thread_s * );
106 static int DvdReadSetArea ( struct input_thread_s *, struct input_area_s * );
107 static int DvdReadRead ( struct input_thread_s *, data_packet_t ** );
108 static void DvdReadSeek ( struct input_thread_s *, off_t );
109 static int DvdReadRewind ( struct input_thread_s * );
111 /* called only from here */
112 static void DvdReadHandleDSI( thread_dvd_data_t * p_dvd, u8 * p_data );
113 static void DvdReadFindCell ( thread_dvd_data_t * p_dvd );
115 /*****************************************************************************
116 * Functions exported as capabilities. They are declared as static so that
117 * we don't pollute the namespace too much.
118 *****************************************************************************/
119 void _M( input_getfunctions )( function_list_t * p_function_list )
121 #define input p_function_list->functions.input
122 p_function_list->pf_probe = DvdReadProbe;
123 input.pf_init = DvdReadInit;
124 input.pf_open = DvdReadOpen;
125 input.pf_close = DvdReadClose;
126 input.pf_end = DvdReadEnd;
127 input.pf_init_bit_stream = InitBitstream;
128 input.pf_read = DvdReadRead;
129 input.pf_set_area = DvdReadSetArea;
130 input.pf_demux = input_DemuxPS;
131 input.pf_new_packet = input_NetlistNewPacket;
132 input.pf_new_pes = input_NetlistNewPES;
133 input.pf_delete_packet = input_NetlistDeletePacket;
134 input.pf_delete_pes = input_NetlistDeletePES;
135 input.pf_rewind = DvdReadRewind;
136 input.pf_seek = DvdReadSeek;
141 * Data reading functions
144 /*****************************************************************************
145 * DvdReadProbe: verifies that the stream is a PS stream
146 *****************************************************************************/
147 static int DvdReadProbe( probedata_t *p_data )
149 input_thread_t * p_input = (input_thread_t *)p_data;
151 char * psz_name = p_input->p_source;
154 if( TestMethod( INPUT_METHOD_VAR, "dvdread" ) )
159 if( ( strlen(psz_name) > 8 ) && !strncasecmp( psz_name, "dvdread:", 8 ) )
161 /* If the user specified "dvdread:" then he probably wants
162 * to use libdvdread */
170 /*****************************************************************************
171 * DvdReadInit: initializes DVD structures
172 *****************************************************************************/
173 static void DvdReadInit( input_thread_t * p_input )
175 thread_dvd_data_t * p_dvd;
176 input_area_t * p_area;
181 p_dvd = malloc( sizeof(thread_dvd_data_t) );
184 intf_ErrMsg( "dvdread error: out of memory" );
185 p_input->b_error = 1;
189 /* we take the pointer to dvd_reader_t back */
190 p_dvd->p_dvdread = (dvd_reader_t *)p_input->p_plugin_data;
191 p_dvd->p_title = NULL;
192 p_dvd->p_vts_file = NULL;
194 p_input->p_plugin_data = (void *)p_dvd;
195 p_input->p_method_data = NULL;
197 /* We read DVD_BLOCK_READ_ONCE in each loop, so the input will receive
198 * DVD_DATA_READ_ONCE at most */
199 p_dvd->i_block_once = DVD_BLOCK_READ_ONCE;
200 /* this value mustn't be modifed */
201 p_input->i_read_once = DVD_DATA_READ_ONCE;
203 /* Reading structures initialisation */
204 input_NetlistInit( p_input, DVD_NETLIST_SIZE, 2 * DVD_NETLIST_SIZE,
205 DVD_NETLIST_SIZE, DVD_VIDEO_LB_LEN, p_dvd->i_block_once );
206 intf_WarnMsg( 2, "dvdread info: netlist initialized" );
208 /* Ifo allocation & initialisation */
209 if( ! ( p_dvd->p_vmg_file = ifoOpen( p_dvd->p_dvdread, 0 ) ) )
211 intf_ErrMsg( "dvdread error: can't open VMG info" );
212 DVDClose( p_dvd->p_dvdread );
214 p_input->b_error = 1;
217 intf_WarnMsg( 2, "dvdread info: VMG opened" );
219 /* Set stream and area data */
220 vlc_mutex_lock( &p_input->stream.stream_lock );
222 /* Initialize ES structures */
223 input_InitStream( p_input, sizeof( stream_ps_data_t ) );
225 /* disc input method */
226 p_input->stream.i_method = INPUT_METHOD_DVD;
228 #define tt_srpt p_dvd->p_vmg_file->tt_srpt
229 intf_WarnMsg( 2, "dvdread info: number of titles: %d", tt_srpt->nr_of_srpts );
231 #define area p_input->stream.pp_areas
232 /* We start from 1 here since the default area 0
233 * is reserved for video_ts.vob */
234 for( i = 1 ; i <= tt_srpt->nr_of_srpts ; i++ )
236 input_AddArea( p_input );
238 /* Titles are Program Chains */
241 /* Absolute start offset and size
242 * We can only set that with vts ifo, so we do it during the
243 * first call to DVDSetArea */
244 area[i]->i_start = 0;
247 /* Number of chapters */
248 area[i]->i_part_nb = tt_srpt->title[i-1].nr_of_ptts;
251 /* Number of angles */
252 area[i]->i_angle_nb = 0;
253 area[i]->i_angle = 1;
255 area[i]->i_plugin_data = tt_srpt->title[i-1].title_set_nr;
259 /* Get requested title - if none try the first title */
260 i_title = main_GetIntVariable( INPUT_TITLE_VAR, 1 );
261 if( i_title <= 0 || i_title > tt_srpt->nr_of_srpts )
268 /* Get requested chapter - if none defaults to first one */
269 i_chapter = main_GetIntVariable( INPUT_CHAPTER_VAR, 1 );
275 p_input->stream.pp_areas[i_title]->i_part = i_chapter;
277 p_area = p_input->stream.pp_areas[i_title];
279 /* set title, chapter, audio and subpic */
280 DvdReadSetArea( p_input, p_area );
282 vlc_mutex_unlock( &p_input->stream.stream_lock );
287 /*****************************************************************************
288 * DvdReadOpen: open libdvdread
289 *****************************************************************************/
290 static void DvdReadOpen( struct input_thread_s *p_input )
292 dvd_reader_t *p_dvdread;
294 vlc_mutex_lock( &p_input->stream.stream_lock );
296 /* If we are here we can control the pace... */
297 p_input->stream.b_pace_control = 1;
299 p_input->stream.b_seekable = 1;
300 p_input->stream.p_selected_area->i_size = 0;
302 p_input->stream.p_selected_area->i_tell = 0;
304 vlc_mutex_unlock( &p_input->stream.stream_lock );
306 /* XXX: put this shit in an access plugin */
307 if( strlen( p_input->p_source ) > 8
308 && !strncasecmp( p_input->p_source, "dvdread:", 8 ) )
310 p_dvdread = DVDOpen( p_input->p_source + 8 );
314 p_dvdread = DVDOpen( p_input->p_source );
319 p_input->b_error = 1;
323 /* the vlc input doesn't handle the file descriptor with libdvdread */
324 p_input->i_handle = -1;
326 /* p_dvdread is stored in p_plugin_data until we have
327 * malloc'ed a dvd_thread_data_t */
328 p_input->p_plugin_data = (void *) p_dvdread;
331 /*****************************************************************************
332 * DvdReadClose: close libdvdread
333 *****************************************************************************/
334 static void DvdReadClose( struct input_thread_s *p_input )
336 DVDClose( ((thread_dvd_data_t*)p_input->p_plugin_data)->p_dvdread );
337 free( p_input->p_plugin_data );
338 p_input->p_plugin_data = NULL;
342 /*****************************************************************************
343 * DvdReadEnd: frees unused data
344 *****************************************************************************/
345 static void DvdReadEnd( input_thread_t * p_input )
347 thread_dvd_data_t * p_dvd;
349 p_dvd = (thread_dvd_data_t *)p_input->p_plugin_data;
351 /* close libdvdread */
352 DVDCloseFile( p_dvd->p_title );
353 ifoClose( p_dvd->p_vts_file );
354 ifoClose( p_dvd->p_vmg_file );
357 input_NetlistEnd( p_input );
358 p_input->p_method_data = NULL;
361 #define p_pgc p_dvd->p_cur_pgc
363 /*****************************************************************************
364 * DvdReadSetArea: initialize input data for title x, chapter y.
365 * It should be called for each user navigation request.
366 *****************************************************************************
367 * Take care that i_title starts from 0 (vmg) and i_chapter start from 1.
368 * Note that you have to take the lock before entering here.
369 *****************************************************************************/
370 static int DvdReadSetArea( input_thread_t * p_input, input_area_t * p_area )
372 thread_dvd_data_t * p_dvd;
376 p_dvd = (thread_dvd_data_t*)p_input->p_plugin_data;
378 /* we can't use the interface slider until initilization is complete */
379 p_input->stream.b_seekable = 0;
381 if( p_area != p_input->stream.p_selected_area )
383 es_descriptor_t * p_es;
389 #define p_vmg p_dvd->p_vmg_file
390 #define p_vts p_dvd->p_vts_file
391 if( p_dvd->p_title != NULL )
393 DVDCloseFile( p_dvd->p_title );
401 /* Reset the Chapter position of the old title */
402 p_input->stream.p_selected_area->i_part = 1;
405 * We have to load all title information
407 /* Change the default area */
408 p_input->stream.p_selected_area = p_area;
410 intf_WarnMsg( 12, "dvdread: open VTS %d, for title %d",
411 p_vmg->tt_srpt->title[ p_area->i_id - 1 ].title_set_nr,
415 if( ! ( p_vts = ifoOpen( p_dvd->p_dvdread,
416 p_vmg->tt_srpt->title[ p_area->i_id - 1 ].title_set_nr ) ) )
418 intf_ErrMsg( "dvdread error: fatal error in vts ifo" );
420 DVDClose( p_dvd->p_dvdread );
421 p_input->b_error = 1;
425 /* title position inside the selected vts */
426 p_dvd->i_ttn = p_vmg->tt_srpt->title[ p_area->i_id - 1 ].vts_ttn;
429 * Set selected title start
431 pgc_id = p_vts->vts_ptt_srpt->title[p_dvd->i_ttn-1].ptt[0].pgcn;
432 pgn = p_vts->vts_ptt_srpt->title[p_dvd->i_ttn-1].ptt[0].pgn;
433 p_pgc = p_vts->vts_pgcit->pgci_srp[ pgc_id - 1 ].pgc;
434 i_cell = p_pgc->program_map[ pgn - 1 ] - 1;
437 LB2OFF( p_dvd->p_cur_pgc->cell_playback[ i_cell ].first_sector );
439 intf_WarnMsg( 3, "dvdread: start %d vts_title %d pgc %d pgn %d",
440 p_area->i_id, p_dvd->i_ttn, pgc_id, pgn );
445 i_cell = p_dvd->p_cur_pgc->nr_of_cells - 1;
447 p_dvd->i_end_block = p_pgc->cell_playback[ i_cell ].last_sector;
448 p_area->i_size = LB2OFF( p_dvd->i_end_block )- p_area->i_start;
450 intf_WarnMsg( 12, "dvdread: start %lld size %lld end %d",
451 p_area->i_start , p_area->i_size, p_dvd->i_end_block );
454 * Set properties for current chapter
456 /* Remeber current chapter */
457 p_dvd->i_chapter = p_area->i_part;
460 pgc_id = p_vts->vts_ptt_srpt->title[
461 p_dvd->i_ttn-1].ptt[p_area->i_part-1].pgcn;
462 pgn = p_vts->vts_ptt_srpt->title[
463 p_dvd->i_ttn-1].ptt[p_area->i_part-1].pgn;
465 p_pgc = p_vts->vts_pgcit->pgci_srp[pgc_id-1].pgc;
466 p_dvd->i_pack_len = 0;
467 p_dvd->i_next_cell = p_dvd->i_cur_cell = p_pgc->program_map[pgn-1] - 1;
468 DvdReadFindCell( p_dvd );
470 p_dvd->i_next_vobu = p_dvd->i_cur_block =
471 p_pgc->cell_playback[p_dvd->i_cur_cell].first_sector;
476 p_area->i_angle_nb = p_vmg->tt_srpt->title[p_area->i_id-1].nr_of_angles;
477 p_area->i_angle = main_GetIntVariable( INPUT_ANGLE_VAR, 1 );
479 if( ( p_area->i_angle <= 0 ) || p_area->i_angle > p_area->i_angle_nb )
483 p_dvd->i_angle = p_area->i_angle;
484 p_dvd->i_angle_nb = p_area->i_angle_nb;
487 * We've got enough info, time to open the title set data.
489 if( ! ( p_dvd->p_title = DVDOpenFile( p_dvd->p_dvdread,
490 p_vmg->tt_srpt->title[ p_area->i_id - 1 ].title_set_nr,
491 DVD_READ_TITLE_VOBS ) ) )
493 intf_ErrMsg( "dvdread error: can't open title (VTS_%02d_1.VOB)",
494 p_vmg->tt_srpt->title[p_area->i_id-1].title_set_nr );
497 DVDClose( p_dvd->p_dvdread );
501 // IfoPrintTitle( p_dvd );
504 * Destroy obsolete ES by reinitializing program 0
505 * and find all ES in title with ifo data
507 if( p_input->stream.pp_programs != NULL )
509 /* We don't use input_EndStream here since
510 * we keep area structures */
512 for( i = 0 ; i < p_input->stream.i_selected_es_number ; i++ )
514 input_UnselectES( p_input, p_input->stream.pp_selected_es[i] );
517 free( p_input->stream.pp_selected_es );
518 input_DelProgram( p_input, p_input->stream.pp_programs[0] );
520 p_input->stream.pp_selected_es = NULL;
521 p_input->stream.i_selected_es_number = 0;
524 input_AddProgram( p_input, 0, sizeof( stream_ps_data_t ) );
526 /* No PSM to read in DVD mode, we already have all information */
527 p_input->stream.pp_programs[0]->b_is_ok = 1;
531 /* ES 0 -> video MPEG2 */
532 // IfoPrintVideo( p_dvd );
534 p_es = input_AddES( p_input, p_input->stream.pp_programs[0], 0xe0, 0 );
535 p_es->i_stream_id = 0xe0;
536 p_es->i_type = MPEG2_VIDEO_ES;
537 p_es->i_cat = VIDEO_ES;
538 if( p_main->b_video )
540 input_SelectES( p_input, p_es );
543 #define audio_control \
544 p_dvd->p_vts_file->vts_pgcit->pgci_srp[pgc_id-1].pgc->audio_control[i-1]
545 /* Audio ES, in the order they appear in .ifo */
546 for( i = 1 ; i <= p_vts->vtsi_mat->nr_of_vts_audio_streams ; i++ )
551 // IfoPrintAudio( p_dvd, i );
553 /* audio channel is active if first byte is 0x80 */
554 if( audio_control & 0x8000 )
557 i_position = ( audio_control & 0x7F00 ) >> 8;
559 intf_WarnMsg( 12, "dvd audio position %d", i_position );
560 switch( p_vts->vtsi_mat->vts_audio_attr[i-1].audio_format )
563 i_id = ( ( 0x80 + i_position ) << 8 ) | 0xbd;
564 p_es = input_AddES( p_input,
565 p_input->stream.pp_programs[0], i_id, 0 );
566 p_es->i_stream_id = 0xbd;
567 p_es->i_type = AC3_AUDIO_ES;
569 p_es->i_cat = AUDIO_ES;
570 strcpy( p_es->psz_desc, DecodeLanguage( hton16(
571 p_vts->vtsi_mat->vts_audio_attr[i-1].lang_code ) ) );
572 strcat( p_es->psz_desc, " (ac3)" );
576 case 0x03: /* MPEG audio */
577 i_id = 0xc0 + i_position;
578 p_es = input_AddES( p_input,
579 p_input->stream.pp_programs[0], i_id, 0 );
580 p_es->i_stream_id = i_id;
581 p_es->i_type = MPEG2_AUDIO_ES;
583 p_es->i_cat = AUDIO_ES;
584 strcpy( p_es->psz_desc, DecodeLanguage( hton16(
585 p_vts->vtsi_mat->vts_audio_attr[i-1].lang_code ) ) );
586 strcat( p_es->psz_desc, " (mpeg)" );
589 case 0x04: /* LPCM */
591 i_id = ( ( 0xa0 + i_position ) << 8 ) | 0xbd;
592 p_es = input_AddES( p_input,
593 p_input->stream.pp_programs[0], i_id, 0 );
594 p_es->i_stream_id = i_id;
595 p_es->i_type = LPCM_AUDIO_ES;
597 p_es->i_cat = AUDIO_ES;
598 strcpy( p_es->psz_desc, DecodeLanguage( hton16(
599 p_vts->vtsi_mat->vts_audio_attr[i-1].lang_code ) ) );
600 strcat( p_es->psz_desc, " (lpcm)" );
604 i_id = ( ( 0x88 + i_position ) << 8 ) | 0xbd;
605 intf_ErrMsg( "dvd warning: DTS audio not handled yet"
610 intf_ErrMsg( "dvd warning: unknown audio type %.2x",
611 p_vts->vtsi_mat->vts_audio_attr[i-1].audio_format );
616 #define spu_control \
617 p_dvd->p_vts_file->vts_pgcit->pgci_srp[pgc_id-1].pgc->subp_control[i-1]
621 for( i = 1 ; i <= p_vts->vtsi_mat->nr_of_vts_subp_streams; i++ )
626 // IfoPrintSpu( p_dvd, i );
627 intf_WarnMsg( 12, "dvd spu %d 0x%02x", i, spu_control );
629 if( spu_control & 0x80000000 )
633 /* there are several streams for one spu */
634 if( p_vts->vtsi_mat->vts_video_attr.display_aspect_ratio )
637 switch( p_vts->vtsi_mat->vts_video_attr.permitted_df )
640 i_position = spu_control & 0xff;
643 i_position = ( spu_control >> 8 ) & 0xff;
646 i_position = ( spu_control >> 16 ) & 0xff;
653 i_position = ( spu_control >> 24 ) & 0x7F;
656 i_id = ( ( 0x20 + i_position ) << 8 ) | 0xbd;
657 p_es = input_AddES( p_input,
658 p_input->stream.pp_programs[0], i_id, 0 );
659 p_es->i_stream_id = 0xbd;
660 p_es->i_type = DVD_SPU_ES;
661 p_es->i_cat = SPU_ES;
662 strcpy( p_es->psz_desc, DecodeLanguage( hton16(
663 p_vts->vtsi_mat->vts_subp_attr[i-1].lang_code ) ) );
667 if( p_main->b_audio )
669 /* For audio: first one if none or a not existing one specified */
670 int i_audio = main_GetIntVariable( INPUT_CHANNEL_VAR, 1 );
671 if( i_audio < 0 || i_audio > i_audio_nb )
673 main_PutIntVariable( INPUT_CHANNEL_VAR, 1 );
676 if( i_audio > 0 && i_audio_nb > 0 )
678 if( main_GetIntVariable( AOUT_SPDIF_VAR, 0 ) ||
679 ( main_GetIntVariable( INPUT_AUDIO_VAR, 0 ) ==
683 while( ( p_input->stream.pp_es[i_ac3]->i_type !=
684 AC3_AUDIO_ES ) && ( i_ac3 <=
685 p_vts->vtsi_mat->nr_of_vts_audio_streams ) )
689 if( p_input->stream.pp_es[i_ac3]->i_type == AC3_AUDIO_ES )
691 input_SelectES( p_input,
692 p_input->stream.pp_es[i_ac3] );
697 input_SelectES( p_input,
698 p_input->stream.pp_es[i_audio] );
703 if( p_main->b_video )
705 /* for spu, default is none */
706 int i_spu = main_GetIntVariable( INPUT_SUBTITLE_VAR, 0 );
707 if( i_spu < 0 || i_spu > i_spu_nb )
709 main_PutIntVariable( INPUT_SUBTITLE_VAR, 0 );
712 if( i_spu > 0 && i_spu_nb > 0 )
714 i_spu += p_vts->vtsi_mat->nr_of_vts_audio_streams;
715 input_SelectES( p_input, p_input->stream.pp_es[i_spu] );
721 p_area = p_input->stream.p_selected_area;
728 if( p_area->i_part != p_dvd->i_chapter )
730 if( ( p_area->i_part > 0 ) &&
731 ( p_area->i_part <= p_area->i_part_nb ))
733 p_dvd->i_ttn = p_vmg->tt_srpt->title[p_area->i_id-1].vts_ttn;
734 pgc_id = p_vts->vts_ptt_srpt->title[
735 p_dvd->i_ttn-1].ptt[p_area->i_part-1].pgcn;
736 pgn = p_vts->vts_ptt_srpt->title[
737 p_dvd->i_ttn-1].ptt[p_area->i_part-1].pgn;
739 p_pgc = p_vts->vts_pgcit->pgci_srp[ pgc_id - 1 ].pgc;
741 p_dvd->i_cur_cell = p_pgc->program_map[ pgn - 1 ] - 1;
742 p_dvd->i_chapter = p_area->i_part;
743 DvdReadFindCell( p_dvd );
745 p_dvd->i_pack_len = 0;
746 p_dvd->i_next_vobu = p_dvd->i_cur_block =
747 p_pgc->cell_playback[p_dvd->i_cur_cell].first_sector;
751 p_area->i_part = p_dvd->i_chapter;
757 if( p_area->i_angle != p_dvd->i_angle )
759 p_dvd->i_angle = p_area->i_angle;
761 intf_WarnMsg( 3, "dvd info: angle %d selected", p_area->i_angle );
763 /* warn interface that something has changed */
764 p_area->i_tell = LB2OFF( p_dvd->i_next_vobu ) - p_area->i_start;
765 p_input->stream.b_seekable = 1;
766 p_input->stream.b_changed = 1;
772 /*****************************************************************************
773 * DvdReadRead: reads data packets into the netlist.
774 *****************************************************************************
775 * Returns -1 in case of error, 0 if everything went well, and 1 in case of
777 *****************************************************************************/
778 static int DvdReadRead( input_thread_t * p_input,
779 data_packet_t ** pp_packets )
781 thread_dvd_data_t * p_dvd;
782 netlist_t * p_netlist;
783 u8 p_data[DVD_VIDEO_LB_LEN];
784 struct iovec * p_vec;
785 struct data_packet_s * pp_data[DVD_DATA_READ_ONCE];
794 p_dvd = (thread_dvd_data_t *)p_input->p_plugin_data;
795 p_netlist = (netlist_t *)p_input->p_method_data;
798 * Playback by cell in this pgc, starting at the cell for our chapter.
802 * End of pack, we select the following one
804 if( ! p_dvd->i_pack_len )
809 if( ( i_read = DVDReadBlocks( p_dvd->p_title, p_dvd->i_next_vobu,
812 intf_ErrMsg( "dvdread error: read failed for block %d",
813 p_dvd->i_next_vobu );
817 assert( p_data[41] == 0xbf && p_data[1027] == 0xbf );
820 * Parse the contained dsi packet.
823 DvdReadHandleDSI( p_dvd, p_data );
826 if( p_dvd->i_next_vobu >= p_dvd->i_end_block + 1 )
831 assert( p_dvd->i_pack_len < 1024 );
832 /* Ugly kludge: we send the pack block to the input for it
833 * sometimes has a zero scr and restart the sync */
834 // p_dvd->i_cur_block ++;
840 * Compute the number of blocks to read
842 i_blocks = p_dvd->i_pack_len >= DVD_BLOCK_READ_ONCE
843 ? DVD_BLOCK_READ_ONCE : p_dvd->i_pack_len;
844 p_dvd->i_pack_len -= i_blocks;
845 p_netlist->i_read_once = i_blocks;
847 /* Get an iovec pointer */
848 if( ( p_vec = input_NetlistGetiovec( p_netlist ) ) == NULL )
850 intf_ErrMsg( "dvdread error: can't get iovec" );
855 i_read = DVDReadVBlocks( p_dvd->p_title, p_dvd->i_cur_block,
857 if( i_read != i_blocks )
859 intf_ErrMsg( "dvdread error: read failed for %d/%d blocks at 0x%02x",
860 i_read, i_blocks, p_dvd->i_cur_block );
864 p_dvd->i_cur_block += i_read;
866 intf_WarnMsg( 12, "dvdread i_blocks: %d len: %d current: 0x%02x", i_read, p_dvd->i_pack_len, p_dvd->i_cur_block );
868 /* Update netlist indexes: we don't do it in DVDGetiovec since we
869 * need know the real number of blocks read */
870 input_NetlistMviovec( p_netlist, i_read, pp_data );
874 /* Read headers to compute payload length */
875 for( i_iovec = 0 ; i_iovec < i_read ; i_iovec++ )
879 while( i_pos < p_netlist->i_buffer_size )
881 pi_cur = (u8*)p_vec[i_iovec].iov_base + i_pos;
884 if( U32_AT( pi_cur ) != 0x1BA )
886 /* That's the case for all packets, except pack header. */
887 i_packet_size = U16_AT( pi_cur + 4 );
888 pp_packets[i_packet] = input_NetlistNewPtr( p_netlist );
889 (*pp_data[i_iovec]->pi_refcount)++;
890 pp_packets[i_packet]->pi_refcount =
891 pp_data[i_iovec]->pi_refcount;
892 pp_packets[i_packet]->p_buffer = pp_data[i_iovec]->p_buffer;
896 /* MPEG-2 Pack header. */
898 pp_packets[i_packet] = pp_data[i_iovec];
902 pp_packets[i_packet]->p_payload_start =
903 pp_packets[i_packet]->p_buffer + i_pos;
905 pp_packets[i_packet]->p_payload_end =
906 pp_packets[i_packet]->p_payload_start + i_packet_size + 6;
908 pp_packets[i_packet]->p_next = NULL;
909 pp_packets[i_packet]->b_discard_payload = 0;
912 i_pos += i_packet_size + 6;
916 pp_packets[i_packet] = NULL;
918 vlc_mutex_lock( &p_input->stream.stream_lock );
920 p_input->stream.p_selected_area->i_tell =
921 LB2OFF( p_dvd->i_cur_block ) -
922 p_input->stream.p_selected_area->i_start;
926 /* We modify i_part only at end of chapter not to erase
927 * some modification from the interface */
928 p_input->stream.p_selected_area->i_part = p_dvd->i_chapter;
933 vlc_mutex_unlock( &p_input->stream.stream_lock );
939 /*****************************************************************************
940 * DVDRewind : reads a stream backward
941 *****************************************************************************/
942 static int DvdReadRewind( input_thread_t * p_input )
947 /*****************************************************************************
948 * DvdReadSeek : Goes to a given position on the stream.
949 *****************************************************************************
950 * This one is used by the input and translate chronological position from
951 * input to logical position on the device.
952 * The lock should be taken before calling this function.
953 *****************************************************************************/
954 static void DvdReadSeek( input_thread_t * p_input, off_t i_off )
956 thread_dvd_data_t * p_dvd;
964 i_off += p_input->stream.p_selected_area->i_start;
965 i_lb = OFF2LB( i_off );
966 p_dvd = ( thread_dvd_data_t * )p_input->p_plugin_data;
969 while( p_dvd->p_cur_pgc->cell_playback[i_cell].last_sector < i_lb )
981 pgc_id = p_dvd->p_vts_file->vts_ptt_srpt->title[
982 p_dvd->i_ttn-1].ptt[i_chapter].pgcn;
983 pgn = p_dvd->p_vts_file->vts_ptt_srpt->title[
984 p_dvd->i_ttn-1].ptt[i_chapter].pgn;
986 p_pgc = p_dvd->p_vts_file->vts_pgcit->pgci_srp[pgc_id-1].pgc;
987 i_tmp = p_pgc->program_map[pgn-1];
989 } while( i_tmp <= i_cell );
992 while( p_dvd->p_vts_file->vts_vobu_admap->vobu_start_sectors[i_vobu]
999 while( p_dvd->p_vts_file->vts_c_adt->cell_adr_table[i_sub_cell].start_sector <
1000 p_dvd->p_vts_file->vts_vobu_admap->vobu_start_sectors[i_vobu-1] )
1006 intf_WarnMsg(12, "cell %d i_sub_cell %d chapter %d vobu %d cell_sector %d vobu_sector %d sub_cell_sector %d",
1007 i_cell, i_sub_cell,i_chapter, i_vobu,
1008 p_dvd->p_cur_pgc->cell_playback[i_cell].first_sector,
1009 p_dvd->p_vts_file->vts_vobu_admap->vobu_start_sectors[i_vobu],
1010 p_dvd->p_vts_file->vts_c_adt->cell_adr_table[i_sub_cell-1].start_sector);
1012 p_dvd->i_cur_block = i_lb;
1013 p_dvd->i_next_vobu =
1014 p_dvd->p_vts_file->vts_vobu_admap->vobu_start_sectors[i_vobu];
1015 p_dvd->i_pack_len = p_dvd->i_next_vobu - i_lb;
1016 p_dvd->i_cur_cell = i_cell;
1017 p_dvd->i_chapter = i_chapter;
1018 DvdReadFindCell( p_dvd );
1020 p_input->stream.p_selected_area->i_tell =
1021 LB2OFF ( p_dvd->i_cur_block )
1022 - p_input->stream.p_selected_area->i_start;
1023 p_input->stream.p_selected_area->i_part = p_dvd->i_chapter;
1028 /*****************************************************************************
1030 *****************************************************************************/
1031 static void DvdReadHandleDSI( thread_dvd_data_t * p_dvd, u8 * p_data )
1033 navRead_DSI( &(p_dvd->dsi_pack), &(p_data[ DSI_START_BYTE ]) );
1036 * Determine where we go next. These values are the ones we mostly
1039 p_dvd->i_cur_block = p_dvd->dsi_pack.dsi_gi.nv_pck_lbn;
1042 * If we're not at the end of this cell, we can determine the next
1043 * VOBU to display using the VOBU_SRI information section of the
1044 * DSI. Using this value correctly follows the current angle,
1045 * avoiding the doubled scenes in The Matrix, and makes our life
1048 if( p_dvd->dsi_pack.vobu_sri.next_vobu != SRI_END_OF_CELL )
1051 switch( ( p_dvd->dsi_pack.sml_pbi.category & 0xf000 ) >> 12 )
1054 /* interleaved unit with no angle */
1055 if( p_dvd->dsi_pack.sml_pbi.ilvu_sa != -1 )
1057 p_dvd->i_next_vobu = p_dvd->i_cur_block +
1058 p_dvd->dsi_pack.sml_pbi.ilvu_sa;
1059 p_dvd->i_pack_len = p_dvd->dsi_pack.sml_pbi.ilvu_ea;
1063 p_dvd->i_next_vobu = p_dvd->i_cur_block +
1064 p_dvd->dsi_pack.dsi_gi.vobu_ea + 1;
1065 p_dvd->i_pack_len = p_dvd->dsi_pack.dsi_gi.vobu_ea;
1069 /* vobu is end of ilvu */
1070 if( p_dvd->dsi_pack.sml_agli.data[p_dvd->i_angle-1].address )
1072 p_dvd->i_next_vobu = p_dvd->i_cur_block +
1073 p_dvd->dsi_pack.sml_agli.data[p_dvd->i_angle-1].address;
1074 p_dvd->i_pack_len = p_dvd->dsi_pack.sml_pbi.ilvu_ea;
1079 /* vobu is beginning of ilvu */
1083 /* entering interleaved section */
1085 /* non interleaved cells in interleaved section */
1087 p_dvd->i_next_vobu = p_dvd->i_cur_block +
1088 ( p_dvd->dsi_pack.vobu_sri.next_vobu & 0x7fffffff );
1089 p_dvd->i_pack_len = p_dvd->dsi_pack.dsi_gi.vobu_ea;
1093 p_dvd->i_next_vobu = p_dvd->i_cur_block +
1094 ( p_dvd->dsi_pack.vobu_sri.next_vobu & 0x7fffffff );
1095 p_dvd->i_pack_len = p_dvd->dsi_pack.dsi_gi.vobu_ea;
1100 p_dvd->i_cur_cell = p_dvd->i_next_cell;
1101 DvdReadFindCell( p_dvd );
1103 p_dvd->i_pack_len = p_dvd->dsi_pack.dsi_gi.vobu_ea;
1104 p_dvd->i_next_vobu =
1105 p_dvd->p_cur_pgc->cell_playback[p_dvd->i_cur_cell].first_sector;
1109 intf_WarnMsg( 12, "scr %d lbn 0x%02x vobu_ea %d vob_id %d c_id %d",
1110 p_dvd->dsi_pack.dsi_gi.nv_pck_scr,
1111 p_dvd->dsi_pack.dsi_gi.nv_pck_lbn,
1112 p_dvd->dsi_pack.dsi_gi.vobu_ea,
1113 p_dvd->dsi_pack.dsi_gi.vobu_vob_idn,
1114 p_dvd->dsi_pack.dsi_gi.vobu_c_idn );
1116 intf_WarnMsg( 12, "cat 0x%02x ilvu_ea %d ilvu_sa %d size %d",
1117 p_dvd->dsi_pack.sml_pbi.category,
1118 p_dvd->dsi_pack.sml_pbi.ilvu_ea,
1119 p_dvd->dsi_pack.sml_pbi.ilvu_sa,
1120 p_dvd->dsi_pack.sml_pbi.size );
1122 intf_WarnMsg( 12, "next_vobu %d next_ilvu1 %d next_ilvu2 %d",
1123 p_dvd->dsi_pack.vobu_sri.next_vobu & 0x7fffffff,
1124 p_dvd->dsi_pack.sml_agli.data[ p_dvd->i_angle - 1 ].address,
1125 p_dvd->dsi_pack.sml_agli.data[ p_dvd->i_angle ].address);
1129 /*****************************************************************************
1131 *****************************************************************************/
1132 static void DvdReadFindCell( thread_dvd_data_t * p_dvd )
1137 #define cell p_dvd->p_cur_pgc->cell_playback
1138 if( cell[p_dvd->i_cur_cell].block_type == BLOCK_TYPE_ANGLE_BLOCK )
1141 p_dvd->i_next_cell = p_dvd->i_cur_cell + p_dvd->i_angle_nb;
1142 p_dvd->i_cur_cell += p_dvd->i_angle - 1;
1144 p_dvd->i_cur_cell += p_dvd->i_angle - 1;
1146 while( cell[p_dvd->i_cur_cell+i].block_mode != BLOCK_MODE_LAST_CELL )
1150 p_dvd->i_next_cell = p_dvd->i_cur_cell + i + 1;
1155 p_dvd->i_next_cell = p_dvd->i_cur_cell + 1;
1158 pgc_id = p_dvd->p_vts_file->vts_ptt_srpt->title[
1159 p_dvd->i_ttn-1].ptt[p_dvd->i_chapter].pgcn;
1160 pgn = p_dvd->p_vts_file->vts_ptt_srpt->title[
1161 p_dvd->i_ttn-1].ptt[p_dvd->i_chapter].pgn;
1162 p_pgc = p_dvd->p_vts_file->vts_pgcit->pgci_srp[pgc_id-1].pgc;
1164 if( p_pgc->program_map[pgn-1] <= p_dvd->i_cur_cell )