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 2001/12/07 18:33:07 sam 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
88 #define DVD_DATA_READ_ONCE (4 * DVD_BLOCK_READ_ONCE)
91 #define DVD_NETLIST_SIZE 512
93 /*****************************************************************************
95 *****************************************************************************/
96 /* called from outside */
97 static int DvdReadProbe ( probedata_t *p_data );
98 static void DvdReadInit ( struct input_thread_s * );
99 static void DvdReadEnd ( struct input_thread_s * );
100 static void DvdReadOpen ( struct input_thread_s * );
101 static void DvdReadClose ( struct input_thread_s * );
102 static int DvdReadSetArea ( struct input_thread_s *, struct input_area_s * );
103 static int DvdReadRead ( struct input_thread_s *, data_packet_t ** );
104 static void DvdReadSeek ( struct input_thread_s *, off_t );
105 static int DvdReadRewind ( struct input_thread_s * );
107 /* called only from here */
108 static void DvdReadHandleDSI( thread_dvd_data_t * p_dvd, u8 * p_data );
109 static void DvdReadFindCell ( thread_dvd_data_t * p_dvd );
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_demux = input_DemuxPS;
127 input.pf_new_packet = input_NetlistNewPacket;
128 input.pf_new_pes = input_NetlistNewPES;
129 input.pf_delete_packet = input_NetlistDeletePacket;
130 input.pf_delete_pes = input_NetlistDeletePES;
131 input.pf_rewind = DvdReadRewind;
132 input.pf_seek = DvdReadSeek;
137 * Data reading functions
140 /*****************************************************************************
141 * DvdReadProbe: verifies that the stream is a PS stream
142 *****************************************************************************/
143 static int DvdReadProbe( probedata_t *p_data )
145 input_thread_t * p_input = (input_thread_t *)p_data;
147 char * psz_name = p_input->p_source;
150 if( TestMethod( INPUT_METHOD_VAR, "dvdread" ) )
155 if( ( strlen(psz_name) > 8 ) && !strncasecmp( psz_name, "dvdread:", 8 ) )
157 /* If the user specified "dvdread:" then he probably wants
158 * to use libdvdread */
166 /*****************************************************************************
167 * DvdReadInit: initializes DVD structures
168 *****************************************************************************/
169 static void DvdReadInit( input_thread_t * p_input )
171 thread_dvd_data_t * p_dvd;
172 input_area_t * p_area;
177 p_dvd = malloc( sizeof(thread_dvd_data_t) );
180 intf_ErrMsg( "dvdread error: out of memory" );
181 p_input->b_error = 1;
185 /* we take the pointer to dvd_reader_t back */
186 p_dvd->p_dvdread = (dvd_reader_t *)p_input->p_plugin_data;
187 p_dvd->p_title = NULL;
188 p_dvd->p_vts_file = NULL;
190 p_input->p_plugin_data = (void *)p_dvd;
191 p_input->p_method_data = NULL;
193 /* We read DVD_BLOCK_READ_ONCE in each loop, so the input will receive
194 * DVD_DATA_READ_ONCE at most */
195 p_dvd->i_block_once = DVD_BLOCK_READ_ONCE;
196 /* this value mustn't be modifed */
197 p_input->i_read_once = DVD_DATA_READ_ONCE;
199 /* Reading structures initialisation */
200 input_NetlistInit( p_input, DVD_NETLIST_SIZE, 2 * DVD_NETLIST_SIZE,
201 DVD_NETLIST_SIZE, DVD_VIDEO_LB_LEN, p_dvd->i_block_once );
202 intf_WarnMsg( 2, "dvdread info: netlist initialized" );
204 /* Ifo allocation & initialisation */
205 if( ! ( p_dvd->p_vmg_file = ifoOpen( p_dvd->p_dvdread, 0 ) ) )
207 intf_ErrMsg( "dvdread error: can't open VMG info" );
208 DVDClose( p_dvd->p_dvdread );
210 p_input->b_error = 1;
213 intf_WarnMsg( 2, "dvdread info: VMG opened" );
215 /* Set stream and area data */
216 vlc_mutex_lock( &p_input->stream.stream_lock );
218 /* Initialize ES structures */
219 input_InitStream( p_input, sizeof( stream_ps_data_t ) );
221 /* disc input method */
222 p_input->stream.i_method = INPUT_METHOD_DVD;
224 #define tt_srpt p_dvd->p_vmg_file->tt_srpt
225 intf_WarnMsg( 2, "dvdread info: number of titles: %d", tt_srpt->nr_of_srpts );
227 #define area p_input->stream.pp_areas
228 /* We start from 1 here since the default area 0
229 * is reserved for video_ts.vob */
230 for( i = 1 ; i <= tt_srpt->nr_of_srpts ; i++ )
232 input_AddArea( p_input );
234 /* Titles are Program Chains */
237 /* Absolute start offset and size
238 * We can only set that with vts ifo, so we do it during the
239 * first call to DVDSetArea */
240 area[i]->i_start = 0;
243 /* Number of chapters */
244 area[i]->i_part_nb = tt_srpt->title[i-1].nr_of_ptts;
247 /* Number of angles */
248 area[i]->i_angle_nb = 0;
249 area[i]->i_angle = 1;
251 area[i]->i_plugin_data = tt_srpt->title[i-1].title_set_nr;
255 /* Get requested title - if none try the first title */
256 i_title = main_GetIntVariable( INPUT_TITLE_VAR, 1 );
257 if( i_title <= 0 || i_title > tt_srpt->nr_of_srpts )
264 /* Get requested chapter - if none defaults to first one */
265 i_chapter = main_GetIntVariable( INPUT_CHAPTER_VAR, 1 );
271 p_input->stream.pp_areas[i_title]->i_part = i_chapter;
273 p_area = p_input->stream.pp_areas[i_title];
275 /* set title, chapter, audio and subpic */
276 DvdReadSetArea( p_input, p_area );
278 vlc_mutex_unlock( &p_input->stream.stream_lock );
283 /*****************************************************************************
284 * DvdReadOpen: open libdvdread
285 *****************************************************************************/
286 static void DvdReadOpen( struct input_thread_s *p_input )
288 dvd_reader_t *p_dvdread;
290 vlc_mutex_lock( &p_input->stream.stream_lock );
292 /* If we are here we can control the pace... */
293 p_input->stream.b_pace_control = 1;
295 p_input->stream.b_seekable = 1;
296 p_input->stream.p_selected_area->i_size = 0;
298 p_input->stream.p_selected_area->i_tell = 0;
300 vlc_mutex_unlock( &p_input->stream.stream_lock );
302 /* XXX: put this shit in an access plugin */
303 if( strlen( p_input->p_source ) > 8
304 && !strncasecmp( p_input->p_source, "dvdread:", 8 ) )
306 p_dvdread = DVDOpen( p_input->p_source + 8 );
310 p_dvdread = DVDOpen( p_input->p_source );
315 p_input->b_error = 1;
319 /* the vlc input doesn't handle the file descriptor with libdvdread */
320 p_input->i_handle = -1;
322 /* p_dvdread is stored in p_plugin_data until we have
323 * malloc'ed a dvd_thread_data_t */
324 p_input->p_plugin_data = (void *) p_dvdread;
327 /*****************************************************************************
328 * DvdReadClose: close libdvdread
329 *****************************************************************************/
330 static void DvdReadClose( struct input_thread_s *p_input )
332 DVDClose( ((thread_dvd_data_t*)p_input->p_plugin_data)->p_dvdread );
333 free( p_input->p_plugin_data );
334 p_input->p_plugin_data = NULL;
338 /*****************************************************************************
339 * DvdReadEnd: frees unused data
340 *****************************************************************************/
341 static void DvdReadEnd( input_thread_t * p_input )
343 thread_dvd_data_t * p_dvd;
345 p_dvd = (thread_dvd_data_t *)p_input->p_plugin_data;
347 /* close libdvdread */
348 DVDCloseFile( p_dvd->p_title );
349 ifoClose( p_dvd->p_vts_file );
350 ifoClose( p_dvd->p_vmg_file );
353 input_NetlistEnd( p_input );
354 p_input->p_method_data = NULL;
357 #define p_pgc p_dvd->p_cur_pgc
359 /*****************************************************************************
360 * DvdReadSetArea: initialize input data for title x, chapter y.
361 * It should be called for each user navigation request.
362 *****************************************************************************
363 * Take care that i_title starts from 0 (vmg) and i_chapter start from 1.
364 * Note that you have to take the lock before entering here.
365 *****************************************************************************/
366 static int DvdReadSetArea( input_thread_t * p_input, input_area_t * p_area )
368 thread_dvd_data_t * p_dvd;
372 p_dvd = (thread_dvd_data_t*)p_input->p_plugin_data;
374 /* we can't use the interface slider until initilization is complete */
375 p_input->stream.b_seekable = 0;
377 if( p_area != p_input->stream.p_selected_area )
379 es_descriptor_t * p_es;
385 #define p_vmg p_dvd->p_vmg_file
386 #define p_vts p_dvd->p_vts_file
387 if( p_dvd->p_title != NULL )
389 DVDCloseFile( p_dvd->p_title );
397 /* Reset the Chapter position of the old title */
398 p_input->stream.p_selected_area->i_part = 1;
401 * We have to load all title information
403 /* Change the default area */
404 p_input->stream.p_selected_area = p_area;
406 intf_WarnMsg( 12, "dvdread: open VTS %d, for title %d",
407 p_vmg->tt_srpt->title[ p_area->i_id - 1 ].title_set_nr,
411 if( ! ( p_vts = ifoOpen( p_dvd->p_dvdread,
412 p_vmg->tt_srpt->title[ p_area->i_id - 1 ].title_set_nr ) ) )
414 intf_ErrMsg( "dvdread error: fatal error in vts ifo" );
416 DVDClose( p_dvd->p_dvdread );
417 p_input->b_error = 1;
421 /* title position inside the selected vts */
422 p_dvd->i_ttn = p_vmg->tt_srpt->title[ p_area->i_id - 1 ].vts_ttn;
425 * Set selected title start
427 pgc_id = p_vts->vts_ptt_srpt->title[p_dvd->i_ttn-1].ptt[0].pgcn;
428 pgn = p_vts->vts_ptt_srpt->title[p_dvd->i_ttn-1].ptt[0].pgn;
429 p_pgc = p_vts->vts_pgcit->pgci_srp[ pgc_id - 1 ].pgc;
430 i_cell = p_pgc->program_map[ pgn - 1 ] - 1;
433 LB2OFF( p_dvd->p_cur_pgc->cell_playback[ i_cell ].first_sector );
435 intf_WarnMsg( 3, "dvdread: start %d vts_title %d pgc %d pgn %d",
436 p_area->i_id, p_dvd->i_ttn, pgc_id, pgn );
441 i_cell = p_dvd->p_cur_pgc->nr_of_cells - 1;
443 p_dvd->i_end_block = p_pgc->cell_playback[ i_cell ].last_sector;
444 p_area->i_size = LB2OFF( p_dvd->i_end_block )- p_area->i_start;
446 intf_WarnMsg( 12, "dvdread: start %lld size %lld end %d",
447 p_area->i_start , p_area->i_size, p_dvd->i_end_block );
450 * Set properties for current chapter
452 /* Remeber current chapter */
453 p_dvd->i_chapter = p_area->i_part;
456 pgc_id = p_vts->vts_ptt_srpt->title[
457 p_dvd->i_ttn-1].ptt[p_area->i_part-1].pgcn;
458 pgn = p_vts->vts_ptt_srpt->title[
459 p_dvd->i_ttn-1].ptt[p_area->i_part-1].pgn;
461 p_pgc = p_vts->vts_pgcit->pgci_srp[pgc_id-1].pgc;
462 p_dvd->i_pack_len = 0;
463 p_dvd->i_next_cell = p_dvd->i_cur_cell = p_pgc->program_map[pgn-1] - 1;
464 DvdReadFindCell( p_dvd );
466 p_dvd->i_next_vobu = p_dvd->i_cur_block =
467 p_pgc->cell_playback[p_dvd->i_cur_cell].first_sector;
472 p_area->i_angle_nb = p_vmg->tt_srpt->title[p_area->i_id-1].nr_of_angles;
473 p_area->i_angle = main_GetIntVariable( INPUT_ANGLE_VAR, 1 );
475 if( ( p_area->i_angle <= 0 ) || p_area->i_angle > p_area->i_angle_nb )
479 p_dvd->i_angle = p_area->i_angle;
480 p_dvd->i_angle_nb = p_area->i_angle_nb;
483 * We've got enough info, time to open the title set data.
485 if( ! ( p_dvd->p_title = DVDOpenFile( p_dvd->p_dvdread,
486 p_vmg->tt_srpt->title[ p_area->i_id - 1 ].title_set_nr,
487 DVD_READ_TITLE_VOBS ) ) )
489 intf_ErrMsg( "dvdread error: can't open title (VTS_%02d_1.VOB)",
490 p_vmg->tt_srpt->title[p_area->i_id-1].title_set_nr );
493 DVDClose( p_dvd->p_dvdread );
497 // IfoPrintTitle( p_dvd );
500 * Destroy obsolete ES by reinitializing program 0
501 * and find all ES in title with ifo data
503 if( p_input->stream.pp_programs != NULL )
505 /* We don't use input_EndStream here since
506 * we keep area structures */
508 for( i = 0 ; i < p_input->stream.i_selected_es_number ; i++ )
510 input_UnselectES( p_input, p_input->stream.pp_selected_es[i] );
513 free( p_input->stream.pp_selected_es );
514 input_DelProgram( p_input, p_input->stream.pp_programs[0] );
516 p_input->stream.pp_selected_es = NULL;
517 p_input->stream.i_selected_es_number = 0;
520 input_AddProgram( p_input, 0, sizeof( stream_ps_data_t ) );
522 /* No PSM to read in DVD mode, we already have all information */
523 p_input->stream.pp_programs[0]->b_is_ok = 1;
527 /* ES 0 -> video MPEG2 */
528 // IfoPrintVideo( p_dvd );
530 p_es = input_AddES( p_input, p_input->stream.pp_programs[0], 0xe0, 0 );
531 p_es->i_stream_id = 0xe0;
532 p_es->i_type = MPEG2_VIDEO_ES;
533 p_es->i_cat = VIDEO_ES;
534 if( p_main->b_video )
536 input_SelectES( p_input, p_es );
539 #define audio_control \
540 p_dvd->p_vts_file->vts_pgcit->pgci_srp[pgc_id-1].pgc->audio_control[i-1]
541 /* Audio ES, in the order they appear in .ifo */
542 for( i = 1 ; i <= p_vts->vtsi_mat->nr_of_vts_audio_streams ; i++ )
547 // IfoPrintAudio( p_dvd, i );
549 /* audio channel is active if first byte is 0x80 */
550 if( audio_control & 0x8000 )
553 i_position = ( audio_control & 0x7F00 ) >> 8;
555 intf_WarnMsg( 12, "dvd audio position %d", i_position );
556 switch( p_vts->vtsi_mat->vts_audio_attr[i-1].audio_format )
559 i_id = ( ( 0x80 + i_position ) << 8 ) | 0xbd;
560 p_es = input_AddES( p_input,
561 p_input->stream.pp_programs[0], i_id, 0 );
562 p_es->i_stream_id = 0xbd;
563 p_es->i_type = AC3_AUDIO_ES;
565 p_es->i_cat = AUDIO_ES;
566 strcpy( p_es->psz_desc, DecodeLanguage( hton16(
567 p_vts->vtsi_mat->vts_audio_attr[i-1].lang_code ) ) );
568 strcat( p_es->psz_desc, " (ac3)" );
572 case 0x03: /* MPEG audio */
573 i_id = 0xc0 + i_position;
574 p_es = input_AddES( p_input,
575 p_input->stream.pp_programs[0], i_id, 0 );
576 p_es->i_stream_id = i_id;
577 p_es->i_type = MPEG2_AUDIO_ES;
579 p_es->i_cat = AUDIO_ES;
580 strcpy( p_es->psz_desc, DecodeLanguage( hton16(
581 p_vts->vtsi_mat->vts_audio_attr[i-1].lang_code ) ) );
582 strcat( p_es->psz_desc, " (mpeg)" );
585 case 0x04: /* LPCM */
587 i_id = ( ( 0xa0 + i_position ) << 8 ) | 0xbd;
588 p_es = input_AddES( p_input,
589 p_input->stream.pp_programs[0], i_id, 0 );
590 p_es->i_stream_id = i_id;
591 p_es->i_type = LPCM_AUDIO_ES;
593 p_es->i_cat = AUDIO_ES;
594 strcpy( p_es->psz_desc, DecodeLanguage( hton16(
595 p_vts->vtsi_mat->vts_audio_attr[i-1].lang_code ) ) );
596 strcat( p_es->psz_desc, " (lpcm)" );
600 i_id = ( ( 0x88 + i_position ) << 8 ) | 0xbd;
601 intf_ErrMsg( "dvd warning: DTS audio not handled yet"
606 intf_ErrMsg( "dvd warning: unknown audio type %.2x",
607 p_vts->vtsi_mat->vts_audio_attr[i-1].audio_format );
612 #define spu_control \
613 p_dvd->p_vts_file->vts_pgcit->pgci_srp[pgc_id-1].pgc->subp_control[i-1]
617 for( i = 1 ; i <= p_vts->vtsi_mat->nr_of_vts_subp_streams; i++ )
622 // IfoPrintSpu( p_dvd, i );
623 intf_WarnMsg( 12, "dvd spu %d 0x%02x", i, spu_control );
625 if( spu_control & 0x80000000 )
629 /* there are several streams for one spu */
630 if( p_vts->vtsi_mat->vts_video_attr.display_aspect_ratio )
633 switch( p_vts->vtsi_mat->vts_video_attr.permitted_df )
636 i_position = spu_control & 0xff;
639 i_position = ( spu_control >> 8 ) & 0xff;
642 i_position = ( spu_control >> 16 ) & 0xff;
649 i_position = ( spu_control >> 24 ) & 0x7F;
652 i_id = ( ( 0x20 + i_position ) << 8 ) | 0xbd;
653 p_es = input_AddES( p_input,
654 p_input->stream.pp_programs[0], i_id, 0 );
655 p_es->i_stream_id = 0xbd;
656 p_es->i_type = DVD_SPU_ES;
657 p_es->i_cat = SPU_ES;
658 strcpy( p_es->psz_desc, DecodeLanguage( hton16(
659 p_vts->vtsi_mat->vts_subp_attr[i-1].lang_code ) ) );
663 if( p_main->b_audio )
665 /* For audio: first one if none or a not existing one specified */
666 int i_audio = main_GetIntVariable( INPUT_CHANNEL_VAR, 1 );
667 if( i_audio < 0 || i_audio > i_audio_nb )
669 main_PutIntVariable( INPUT_CHANNEL_VAR, 1 );
672 if( i_audio > 0 && i_audio_nb > 0 )
674 if( main_GetIntVariable( AOUT_SPDIF_VAR, 0 ) ||
675 ( main_GetIntVariable( INPUT_AUDIO_VAR, 0 ) ==
679 while( ( p_input->stream.pp_es[i_ac3]->i_type !=
680 AC3_AUDIO_ES ) && ( i_ac3 <=
681 p_vts->vtsi_mat->nr_of_vts_audio_streams ) )
685 if( p_input->stream.pp_es[i_ac3]->i_type == AC3_AUDIO_ES )
687 input_SelectES( p_input,
688 p_input->stream.pp_es[i_ac3] );
693 input_SelectES( p_input,
694 p_input->stream.pp_es[i_audio] );
699 if( p_main->b_video )
701 /* for spu, default is none */
702 int i_spu = main_GetIntVariable( INPUT_SUBTITLE_VAR, 0 );
703 if( i_spu < 0 || i_spu > i_spu_nb )
705 main_PutIntVariable( INPUT_SUBTITLE_VAR, 0 );
708 if( i_spu > 0 && i_spu_nb > 0 )
710 i_spu += p_vts->vtsi_mat->nr_of_vts_audio_streams;
711 input_SelectES( p_input, p_input->stream.pp_es[i_spu] );
717 p_area = p_input->stream.p_selected_area;
724 if( p_area->i_part != p_dvd->i_chapter )
726 if( ( p_area->i_part > 0 ) &&
727 ( p_area->i_part <= p_area->i_part_nb ))
729 p_dvd->i_ttn = p_vmg->tt_srpt->title[p_area->i_id-1].vts_ttn;
730 pgc_id = p_vts->vts_ptt_srpt->title[
731 p_dvd->i_ttn-1].ptt[p_area->i_part-1].pgcn;
732 pgn = p_vts->vts_ptt_srpt->title[
733 p_dvd->i_ttn-1].ptt[p_area->i_part-1].pgn;
735 p_pgc = p_vts->vts_pgcit->pgci_srp[ pgc_id - 1 ].pgc;
737 p_dvd->i_cur_cell = p_pgc->program_map[ pgn - 1 ] - 1;
738 p_dvd->i_chapter = p_area->i_part;
739 DvdReadFindCell( p_dvd );
741 p_dvd->i_pack_len = 0;
742 p_dvd->i_next_vobu = p_dvd->i_cur_block =
743 p_pgc->cell_playback[p_dvd->i_cur_cell].first_sector;
747 p_area->i_part = p_dvd->i_chapter;
753 if( p_area->i_angle != p_dvd->i_angle )
755 p_dvd->i_angle = p_area->i_angle;
757 intf_WarnMsg( 3, "dvd info: angle %d selected", p_area->i_angle );
759 /* warn interface that something has changed */
760 p_area->i_tell = LB2OFF( p_dvd->i_next_vobu ) - p_area->i_start;
761 p_input->stream.b_seekable = 1;
762 p_input->stream.b_changed = 1;
768 /*****************************************************************************
769 * DvdReadRead: reads data packets into the netlist.
770 *****************************************************************************
771 * Returns -1 in case of error, 0 if everything went well, and 1 in case of
773 *****************************************************************************/
774 static int DvdReadRead( input_thread_t * p_input,
775 data_packet_t ** pp_packets )
777 thread_dvd_data_t * p_dvd;
778 netlist_t * p_netlist;
779 u8 p_data[DVD_VIDEO_LB_LEN];
780 struct iovec * p_vec;
781 struct data_packet_s * pp_data[DVD_DATA_READ_ONCE];
790 p_dvd = (thread_dvd_data_t *)p_input->p_plugin_data;
791 p_netlist = (netlist_t *)p_input->p_method_data;
794 * Playback by cell in this pgc, starting at the cell for our chapter.
798 * End of pack, we select the following one
800 if( ! p_dvd->i_pack_len )
805 if( ( i_read = DVDReadBlocks( p_dvd->p_title, p_dvd->i_next_vobu,
808 intf_ErrMsg( "dvdread error: read failed for block %d",
809 p_dvd->i_next_vobu );
813 assert( p_data[41] == 0xbf && p_data[1027] == 0xbf );
816 * Parse the contained dsi packet.
819 DvdReadHandleDSI( p_dvd, p_data );
822 if( p_dvd->i_next_vobu >= p_dvd->i_end_block + 1 )
827 assert( p_dvd->i_pack_len < 1024 );
828 /* Ugly kludge: we send the pack block to the input for it
829 * sometimes has a zero scr and restart the sync */
830 // p_dvd->i_cur_block ++;
836 * Compute the number of blocks to read
838 i_blocks = p_dvd->i_pack_len >= DVD_BLOCK_READ_ONCE
839 ? DVD_BLOCK_READ_ONCE : p_dvd->i_pack_len;
840 p_dvd->i_pack_len -= i_blocks;
841 p_netlist->i_read_once = i_blocks;
843 /* Get an iovec pointer */
844 if( ( p_vec = input_NetlistGetiovec( p_netlist ) ) == NULL )
846 intf_ErrMsg( "dvdread error: can't get iovec" );
851 i_read = DVDReadVBlocks( p_dvd->p_title, p_dvd->i_cur_block,
853 if( i_read != i_blocks )
855 intf_ErrMsg( "dvdread error: read failed for %d/%d blocks at 0x%02x",
856 i_read, i_blocks, p_dvd->i_cur_block );
860 p_dvd->i_cur_block += i_read;
862 intf_WarnMsg( 12, "dvdread i_blocks: %d len: %d current: 0x%02x", i_read, p_dvd->i_pack_len, p_dvd->i_cur_block );
864 /* Update netlist indexes: we don't do it in DVDGetiovec since we
865 * need know the real number of blocks read */
866 input_NetlistMviovec( p_netlist, i_read, pp_data );
870 /* Read headers to compute payload length */
871 for( i_iovec = 0 ; i_iovec < i_read ; i_iovec++ )
875 while( i_pos < p_netlist->i_buffer_size )
877 pi_cur = (u8*)p_vec[i_iovec].iov_base + i_pos;
880 if( U32_AT( pi_cur ) != 0x1BA )
882 /* That's the case for all packets, except pack header. */
883 i_packet_size = U16_AT( pi_cur + 4 );
884 pp_packets[i_packet] = input_NetlistNewPtr( p_netlist );
885 (*pp_data[i_iovec]->pi_refcount)++;
886 pp_packets[i_packet]->pi_refcount =
887 pp_data[i_iovec]->pi_refcount;
888 pp_packets[i_packet]->p_buffer = pp_data[i_iovec]->p_buffer;
892 /* MPEG-2 Pack header. */
894 pp_packets[i_packet] = pp_data[i_iovec];
898 pp_packets[i_packet]->p_payload_start =
899 pp_packets[i_packet]->p_buffer + i_pos;
901 pp_packets[i_packet]->p_payload_end =
902 pp_packets[i_packet]->p_payload_start + i_packet_size + 6;
904 pp_packets[i_packet]->p_next = NULL;
905 pp_packets[i_packet]->b_discard_payload = 0;
908 i_pos += i_packet_size + 6;
912 pp_packets[i_packet] = NULL;
914 vlc_mutex_lock( &p_input->stream.stream_lock );
916 p_input->stream.p_selected_area->i_tell =
917 LB2OFF( p_dvd->i_cur_block ) -
918 p_input->stream.p_selected_area->i_start;
922 /* We modify i_part only at end of chapter not to erase
923 * some modification from the interface */
924 p_input->stream.p_selected_area->i_part = p_dvd->i_chapter;
929 vlc_mutex_unlock( &p_input->stream.stream_lock );
935 /*****************************************************************************
936 * DVDRewind : reads a stream backward
937 *****************************************************************************/
938 static int DvdReadRewind( input_thread_t * p_input )
943 /*****************************************************************************
944 * DvdReadSeek : Goes to a given position on the stream.
945 *****************************************************************************
946 * This one is used by the input and translate chronological position from
947 * input to logical position on the device.
948 * The lock should be taken before calling this function.
949 *****************************************************************************/
950 static void DvdReadSeek( input_thread_t * p_input, off_t i_off )
952 thread_dvd_data_t * p_dvd;
960 i_off += p_input->stream.p_selected_area->i_start;
961 i_lb = OFF2LB( i_off );
962 p_dvd = ( thread_dvd_data_t * )p_input->p_plugin_data;
965 while( p_dvd->p_cur_pgc->cell_playback[i_cell].last_sector < i_lb )
977 pgc_id = p_dvd->p_vts_file->vts_ptt_srpt->title[
978 p_dvd->i_ttn-1].ptt[i_chapter].pgcn;
979 pgn = p_dvd->p_vts_file->vts_ptt_srpt->title[
980 p_dvd->i_ttn-1].ptt[i_chapter].pgn;
982 p_pgc = p_dvd->p_vts_file->vts_pgcit->pgci_srp[pgc_id-1].pgc;
983 i_tmp = p_pgc->program_map[pgn-1];
985 } while( i_tmp <= i_cell );
988 while( p_dvd->p_vts_file->vts_vobu_admap->vobu_start_sectors[i_vobu]
995 while( p_dvd->p_vts_file->vts_c_adt->cell_adr_table[i_sub_cell].start_sector <
996 p_dvd->p_vts_file->vts_vobu_admap->vobu_start_sectors[i_vobu-1] )
1002 intf_WarnMsg(12, "cell %d i_sub_cell %d chapter %d vobu %d cell_sector %d vobu_sector %d sub_cell_sector %d",
1003 i_cell, i_sub_cell,i_chapter, i_vobu,
1004 p_dvd->p_cur_pgc->cell_playback[i_cell].first_sector,
1005 p_dvd->p_vts_file->vts_vobu_admap->vobu_start_sectors[i_vobu],
1006 p_dvd->p_vts_file->vts_c_adt->cell_adr_table[i_sub_cell-1].start_sector);
1008 p_dvd->i_cur_block = i_lb;
1009 p_dvd->i_next_vobu =
1010 p_dvd->p_vts_file->vts_vobu_admap->vobu_start_sectors[i_vobu];
1011 p_dvd->i_pack_len = p_dvd->i_next_vobu - i_lb;
1012 p_dvd->i_cur_cell = i_cell;
1013 p_dvd->i_chapter = i_chapter;
1014 DvdReadFindCell( p_dvd );
1016 p_input->stream.p_selected_area->i_tell =
1017 LB2OFF ( p_dvd->i_cur_block )
1018 - p_input->stream.p_selected_area->i_start;
1019 p_input->stream.p_selected_area->i_part = p_dvd->i_chapter;
1024 /*****************************************************************************
1026 *****************************************************************************/
1027 static void DvdReadHandleDSI( thread_dvd_data_t * p_dvd, u8 * p_data )
1029 navRead_DSI( &(p_dvd->dsi_pack), &(p_data[ DSI_START_BYTE ]) );
1032 * Determine where we go next. These values are the ones we mostly
1035 p_dvd->i_cur_block = p_dvd->dsi_pack.dsi_gi.nv_pck_lbn;
1038 * If we're not at the end of this cell, we can determine the next
1039 * VOBU to display using the VOBU_SRI information section of the
1040 * DSI. Using this value correctly follows the current angle,
1041 * avoiding the doubled scenes in The Matrix, and makes our life
1044 if( p_dvd->dsi_pack.vobu_sri.next_vobu != SRI_END_OF_CELL )
1047 switch( ( p_dvd->dsi_pack.sml_pbi.category & 0xf000 ) >> 12 )
1050 /* interleaved unit with no angle */
1051 if( p_dvd->dsi_pack.sml_pbi.ilvu_sa != -1 )
1053 p_dvd->i_next_vobu = p_dvd->i_cur_block +
1054 p_dvd->dsi_pack.sml_pbi.ilvu_sa;
1055 p_dvd->i_pack_len = p_dvd->dsi_pack.sml_pbi.ilvu_ea;
1059 p_dvd->i_next_vobu = p_dvd->i_cur_block +
1060 p_dvd->dsi_pack.dsi_gi.vobu_ea + 1;
1061 p_dvd->i_pack_len = p_dvd->dsi_pack.dsi_gi.vobu_ea;
1065 /* vobu is end of ilvu */
1066 if( p_dvd->dsi_pack.sml_agli.data[p_dvd->i_angle-1].address )
1068 p_dvd->i_next_vobu = p_dvd->i_cur_block +
1069 p_dvd->dsi_pack.sml_agli.data[p_dvd->i_angle-1].address;
1070 p_dvd->i_pack_len = p_dvd->dsi_pack.sml_pbi.ilvu_ea;
1075 /* vobu is beginning of ilvu */
1079 /* entering interleaved section */
1081 /* non interleaved cells in interleaved section */
1083 p_dvd->i_next_vobu = p_dvd->i_cur_block +
1084 ( p_dvd->dsi_pack.vobu_sri.next_vobu & 0x7fffffff );
1085 p_dvd->i_pack_len = p_dvd->dsi_pack.dsi_gi.vobu_ea;
1089 p_dvd->i_next_vobu = p_dvd->i_cur_block +
1090 ( p_dvd->dsi_pack.vobu_sri.next_vobu & 0x7fffffff );
1091 p_dvd->i_pack_len = p_dvd->dsi_pack.dsi_gi.vobu_ea;
1096 p_dvd->i_cur_cell = p_dvd->i_next_cell;
1097 DvdReadFindCell( p_dvd );
1099 p_dvd->i_pack_len = p_dvd->dsi_pack.dsi_gi.vobu_ea;
1100 p_dvd->i_next_vobu =
1101 p_dvd->p_cur_pgc->cell_playback[p_dvd->i_cur_cell].first_sector;
1105 intf_WarnMsg( 12, "scr %d lbn 0x%02x vobu_ea %d vob_id %d c_id %d",
1106 p_dvd->dsi_pack.dsi_gi.nv_pck_scr,
1107 p_dvd->dsi_pack.dsi_gi.nv_pck_lbn,
1108 p_dvd->dsi_pack.dsi_gi.vobu_ea,
1109 p_dvd->dsi_pack.dsi_gi.vobu_vob_idn,
1110 p_dvd->dsi_pack.dsi_gi.vobu_c_idn );
1112 intf_WarnMsg( 12, "cat 0x%02x ilvu_ea %d ilvu_sa %d size %d",
1113 p_dvd->dsi_pack.sml_pbi.category,
1114 p_dvd->dsi_pack.sml_pbi.ilvu_ea,
1115 p_dvd->dsi_pack.sml_pbi.ilvu_sa,
1116 p_dvd->dsi_pack.sml_pbi.size );
1118 intf_WarnMsg( 12, "next_vobu %d next_ilvu1 %d next_ilvu2 %d",
1119 p_dvd->dsi_pack.vobu_sri.next_vobu & 0x7fffffff,
1120 p_dvd->dsi_pack.sml_agli.data[ p_dvd->i_angle - 1 ].address,
1121 p_dvd->dsi_pack.sml_agli.data[ p_dvd->i_angle ].address);
1125 /*****************************************************************************
1127 *****************************************************************************/
1128 static void DvdReadFindCell( thread_dvd_data_t * p_dvd )
1133 #define cell p_dvd->p_cur_pgc->cell_playback
1134 if( cell[p_dvd->i_cur_cell].block_type == BLOCK_TYPE_ANGLE_BLOCK )
1137 p_dvd->i_next_cell = p_dvd->i_cur_cell + p_dvd->i_angle_nb;
1138 p_dvd->i_cur_cell += p_dvd->i_angle - 1;
1140 p_dvd->i_cur_cell += p_dvd->i_angle - 1;
1142 while( cell[p_dvd->i_cur_cell+i].block_mode != BLOCK_MODE_LAST_CELL )
1146 p_dvd->i_next_cell = p_dvd->i_cur_cell + i + 1;
1151 p_dvd->i_next_cell = p_dvd->i_cur_cell + 1;
1154 pgc_id = p_dvd->p_vts_file->vts_ptt_srpt->title[
1155 p_dvd->i_ttn-1].ptt[p_dvd->i_chapter].pgcn;
1156 pgn = p_dvd->p_vts_file->vts_ptt_srpt->title[
1157 p_dvd->i_ttn-1].ptt[p_dvd->i_chapter].pgn;
1158 p_pgc = p_dvd->p_vts_file->vts_pgcit->pgci_srp[pgc_id-1].pgc;
1160 if( p_pgc->program_map[pgn-1] <= p_dvd->i_cur_cell )