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.12 2001/12/30 07:09:55 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 /*****************************************************************************
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
76 #define DVD_DATA_READ_ONCE (4 * DVD_BLOCK_READ_ONCE)
78 /*****************************************************************************
80 *****************************************************************************/
81 /* called from outside */
82 static int DvdReadProbe ( probedata_t *p_data );
83 static void DvdReadInit ( struct input_thread_s * );
84 static void DvdReadEnd ( struct input_thread_s * );
85 static void DvdReadOpen ( struct input_thread_s * );
86 static void DvdReadClose ( struct input_thread_s * );
87 static int DvdReadSetArea ( struct input_thread_s *, struct input_area_s * );
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_demux = input_DemuxPS;
127 input.pf_new_packet = input_NewPacket;
128 input.pf_new_pes = input_NewPES;
129 input.pf_delete_packet = input_DeletePacket;
130 input.pf_delete_pes = input_DeletePES;
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;
192 if( (p_input->p_method_data = input_BuffersInit()) == NULL )
195 p_input->b_error = 1;
199 /* We read DVD_BLOCK_READ_ONCE in each loop, so the input will receive
200 * DVD_DATA_READ_ONCE at most */
201 p_dvd->i_block_once = DVD_BLOCK_READ_ONCE;
203 /* Ifo allocation & initialisation */
204 if( ! ( p_dvd->p_vmg_file = ifoOpen( p_dvd->p_dvdread, 0 ) ) )
206 intf_ErrMsg( "dvdread error: can't open VMG info" );
207 input_BuffersEnd( p_input->p_method_data );
209 p_input->b_error = 1;
212 intf_WarnMsg( 2, "dvdread info: VMG opened" );
214 /* Set stream and area data */
215 vlc_mutex_lock( &p_input->stream.stream_lock );
217 /* Initialize ES structures */
218 input_InitStream( p_input, sizeof( stream_ps_data_t ) );
220 /* disc input method */
221 p_input->stream.i_method = INPUT_METHOD_DVD;
223 #define tt_srpt p_dvd->p_vmg_file->tt_srpt
224 intf_WarnMsg( 2, "dvdread info: number of titles: %d", tt_srpt->nr_of_srpts );
226 #define area p_input->stream.pp_areas
227 /* We start from 1 here since the default area 0
228 * is reserved for video_ts.vob */
229 for( i = 1 ; i <= tt_srpt->nr_of_srpts ; i++ )
231 input_AddArea( p_input );
233 /* Titles are Program Chains */
236 /* Absolute start offset and size
237 * We can only set that with vts ifo, so we do it during the
238 * first call to DVDSetArea */
239 area[i]->i_start = 0;
242 /* Number of chapters */
243 area[i]->i_part_nb = tt_srpt->title[i-1].nr_of_ptts;
246 /* Number of angles */
247 area[i]->i_angle_nb = 0;
248 area[i]->i_angle = 1;
250 area[i]->i_plugin_data = tt_srpt->title[i-1].title_set_nr;
254 /* Get requested title - if none try the first title */
255 i_title = main_GetIntVariable( INPUT_TITLE_VAR, 1 );
256 if( i_title <= 0 || i_title > tt_srpt->nr_of_srpts )
263 /* Get requested chapter - if none defaults to first one */
264 i_chapter = main_GetIntVariable( INPUT_CHAPTER_VAR, 1 );
270 p_input->stream.pp_areas[i_title]->i_part = i_chapter;
272 p_area = p_input->stream.pp_areas[i_title];
274 /* set title, chapter, audio and subpic */
275 DvdReadSetArea( p_input, p_area );
277 vlc_mutex_unlock( &p_input->stream.stream_lock );
282 /*****************************************************************************
283 * DvdReadOpen: open libdvdread
284 *****************************************************************************/
285 static void DvdReadOpen( struct input_thread_s *p_input )
287 dvd_reader_t *p_dvdread;
289 vlc_mutex_lock( &p_input->stream.stream_lock );
291 /* If we are here we can control the pace... */
292 p_input->stream.b_pace_control = 1;
294 p_input->stream.b_seekable = 1;
295 p_input->stream.p_selected_area->i_size = 0;
297 p_input->stream.p_selected_area->i_tell = 0;
299 vlc_mutex_unlock( &p_input->stream.stream_lock );
301 /* XXX: put this shit in an access plugin */
302 if( strlen( p_input->p_source ) > 8
303 && !strncasecmp( p_input->p_source, "dvdread:", 8 ) )
305 p_dvdread = DVDOpen( p_input->p_source + 8 );
309 p_dvdread = DVDOpen( p_input->p_source );
314 p_input->b_error = 1;
318 /* the vlc input doesn't handle the file descriptor with libdvdread */
319 p_input->i_handle = -1;
321 /* p_dvdread is stored in p_plugin_data until we have
322 * malloc'ed a dvd_thread_data_t */
323 p_input->p_plugin_data = (void *) p_dvdread;
326 /*****************************************************************************
327 * DvdReadClose: close libdvdread
328 *****************************************************************************/
329 static void DvdReadClose( struct input_thread_s *p_input )
331 DVDClose( ((thread_dvd_data_t*)p_input->p_plugin_data)->p_dvdread );
332 free( p_input->p_plugin_data );
333 p_input->p_plugin_data = NULL;
337 /*****************************************************************************
338 * DvdReadEnd: frees unused data
339 *****************************************************************************/
340 static void DvdReadEnd( input_thread_t * p_input )
342 thread_dvd_data_t * p_dvd;
344 p_dvd = (thread_dvd_data_t *)p_input->p_plugin_data;
346 /* close libdvdread */
347 DVDCloseFile( p_dvd->p_title );
348 ifoClose( p_dvd->p_vts_file );
349 ifoClose( p_dvd->p_vmg_file );
351 input_BuffersEnd( p_input->p_method_data );
354 #define p_pgc p_dvd->p_cur_pgc
356 /*****************************************************************************
357 * DvdReadSetArea: initialize input data for title x, chapter y.
358 * It should be called for each user navigation request.
359 *****************************************************************************
360 * Take care that i_title starts from 0 (vmg) and i_chapter start from 1.
361 * Note that you have to take the lock before entering here.
362 *****************************************************************************/
363 static int DvdReadSetArea( input_thread_t * p_input, input_area_t * p_area )
365 thread_dvd_data_t * p_dvd;
369 p_dvd = (thread_dvd_data_t*)p_input->p_plugin_data;
371 /* we can't use the interface slider until initilization is complete */
372 p_input->stream.b_seekable = 0;
374 if( p_area != p_input->stream.p_selected_area )
376 es_descriptor_t * p_es;
382 #define p_vmg p_dvd->p_vmg_file
383 #define p_vts p_dvd->p_vts_file
384 if( p_dvd->p_title != NULL )
386 DVDCloseFile( p_dvd->p_title );
394 /* Reset the Chapter position of the old title */
395 p_input->stream.p_selected_area->i_part = 1;
398 * We have to load all title information
400 /* Change the default area */
401 p_input->stream.p_selected_area = p_area;
403 intf_WarnMsg( 12, "dvdread: open VTS %d, for title %d",
404 p_vmg->tt_srpt->title[ p_area->i_id - 1 ].title_set_nr,
408 if( ! ( p_vts = ifoOpen( p_dvd->p_dvdread,
409 p_vmg->tt_srpt->title[ p_area->i_id - 1 ].title_set_nr ) ) )
411 intf_ErrMsg( "dvdread error: fatal error in vts ifo" );
413 DVDClose( p_dvd->p_dvdread );
414 p_input->b_error = 1;
418 /* title position inside the selected vts */
419 p_dvd->i_ttn = p_vmg->tt_srpt->title[ p_area->i_id - 1 ].vts_ttn;
422 * Set selected title start
424 pgc_id = p_vts->vts_ptt_srpt->title[p_dvd->i_ttn-1].ptt[0].pgcn;
425 pgn = p_vts->vts_ptt_srpt->title[p_dvd->i_ttn-1].ptt[0].pgn;
426 p_pgc = p_vts->vts_pgcit->pgci_srp[ pgc_id - 1 ].pgc;
427 i_cell = p_pgc->program_map[ pgn - 1 ] - 1;
430 LB2OFF( p_dvd->p_cur_pgc->cell_playback[ i_cell ].first_sector );
432 intf_WarnMsg( 3, "dvdread: start %d vts_title %d pgc %d pgn %d",
433 p_area->i_id, p_dvd->i_ttn, pgc_id, pgn );
438 i_cell = p_dvd->p_cur_pgc->nr_of_cells - 1;
440 p_dvd->i_end_block = p_pgc->cell_playback[ i_cell ].last_sector;
441 p_area->i_size = LB2OFF( p_dvd->i_end_block )- p_area->i_start;
443 intf_WarnMsg( 12, "dvdread: start %lld size %lld end %d",
444 p_area->i_start , p_area->i_size, p_dvd->i_end_block );
447 * Set properties for current chapter
449 /* Remeber current chapter */
450 p_dvd->i_chapter = p_area->i_part;
453 pgc_id = p_vts->vts_ptt_srpt->title[
454 p_dvd->i_ttn-1].ptt[p_area->i_part-1].pgcn;
455 pgn = p_vts->vts_ptt_srpt->title[
456 p_dvd->i_ttn-1].ptt[p_area->i_part-1].pgn;
458 p_pgc = p_vts->vts_pgcit->pgci_srp[pgc_id-1].pgc;
459 p_dvd->i_pack_len = 0;
460 p_dvd->i_next_cell = p_dvd->i_cur_cell = p_pgc->program_map[pgn-1] - 1;
461 DvdReadFindCell( p_dvd );
463 p_dvd->i_next_vobu = p_dvd->i_cur_block =
464 p_pgc->cell_playback[p_dvd->i_cur_cell].first_sector;
469 p_area->i_angle_nb = p_vmg->tt_srpt->title[p_area->i_id-1].nr_of_angles;
470 p_area->i_angle = main_GetIntVariable( INPUT_ANGLE_VAR, 1 );
472 if( ( p_area->i_angle <= 0 ) || p_area->i_angle > p_area->i_angle_nb )
476 p_dvd->i_angle = p_area->i_angle;
477 p_dvd->i_angle_nb = p_area->i_angle_nb;
480 * We've got enough info, time to open the title set data.
482 if( ! ( p_dvd->p_title = DVDOpenFile( p_dvd->p_dvdread,
483 p_vmg->tt_srpt->title[ p_area->i_id - 1 ].title_set_nr,
484 DVD_READ_TITLE_VOBS ) ) )
486 intf_ErrMsg( "dvdread error: can't open title (VTS_%02d_1.VOB)",
487 p_vmg->tt_srpt->title[p_area->i_id-1].title_set_nr );
490 DVDClose( p_dvd->p_dvdread );
494 // IfoPrintTitle( p_dvd );
497 * Destroy obsolete ES by reinitializing program 0
498 * and find all ES in title with ifo data
500 if( p_input->stream.pp_programs != NULL )
502 /* We don't use input_EndStream here since
503 * we keep area structures */
505 for( i = 0 ; i < p_input->stream.i_selected_es_number ; i++ )
507 input_UnselectES( p_input, p_input->stream.pp_selected_es[i] );
510 free( p_input->stream.pp_selected_es );
511 input_DelProgram( p_input, p_input->stream.pp_programs[0] );
513 p_input->stream.pp_selected_es = NULL;
514 p_input->stream.i_selected_es_number = 0;
517 input_AddProgram( p_input, 0, sizeof( stream_ps_data_t ) );
519 /* No PSM to read in DVD mode, we already have all information */
520 p_input->stream.pp_programs[0]->b_is_ok = 1;
524 /* ES 0 -> video MPEG2 */
525 // IfoPrintVideo( p_dvd );
527 p_es = input_AddES( p_input, p_input->stream.pp_programs[0], 0xe0, 0 );
528 p_es->i_stream_id = 0xe0;
529 p_es->i_type = MPEG2_VIDEO_ES;
530 p_es->i_cat = VIDEO_ES;
531 if( p_main->b_video )
533 input_SelectES( p_input, p_es );
536 #define audio_control \
537 p_dvd->p_vts_file->vts_pgcit->pgci_srp[pgc_id-1].pgc->audio_control[i-1]
538 /* Audio ES, in the order they appear in .ifo */
539 for( i = 1 ; i <= p_vts->vtsi_mat->nr_of_vts_audio_streams ; i++ )
544 // IfoPrintAudio( p_dvd, i );
546 /* audio channel is active if first byte is 0x80 */
547 if( audio_control & 0x8000 )
550 i_position = ( audio_control & 0x7F00 ) >> 8;
552 intf_WarnMsg( 12, "dvd audio position %d", i_position );
553 switch( p_vts->vtsi_mat->vts_audio_attr[i-1].audio_format )
556 i_id = ( ( 0x80 + i_position ) << 8 ) | 0xbd;
557 p_es = input_AddES( p_input,
558 p_input->stream.pp_programs[0], i_id, 0 );
559 p_es->i_stream_id = 0xbd;
560 p_es->i_type = AC3_AUDIO_ES;
562 p_es->i_cat = AUDIO_ES;
563 strcpy( p_es->psz_desc, DecodeLanguage( hton16(
564 p_vts->vtsi_mat->vts_audio_attr[i-1].lang_code ) ) );
565 strcat( p_es->psz_desc, " (ac3)" );
569 case 0x03: /* MPEG audio */
570 i_id = 0xc0 + i_position;
571 p_es = input_AddES( p_input,
572 p_input->stream.pp_programs[0], i_id, 0 );
573 p_es->i_stream_id = i_id;
574 p_es->i_type = MPEG2_AUDIO_ES;
576 p_es->i_cat = AUDIO_ES;
577 strcpy( p_es->psz_desc, DecodeLanguage( hton16(
578 p_vts->vtsi_mat->vts_audio_attr[i-1].lang_code ) ) );
579 strcat( p_es->psz_desc, " (mpeg)" );
582 case 0x04: /* LPCM */
584 i_id = ( ( 0xa0 + i_position ) << 8 ) | 0xbd;
585 p_es = input_AddES( p_input,
586 p_input->stream.pp_programs[0], i_id, 0 );
587 p_es->i_stream_id = i_id;
588 p_es->i_type = LPCM_AUDIO_ES;
590 p_es->i_cat = AUDIO_ES;
591 strcpy( p_es->psz_desc, DecodeLanguage( hton16(
592 p_vts->vtsi_mat->vts_audio_attr[i-1].lang_code ) ) );
593 strcat( p_es->psz_desc, " (lpcm)" );
597 i_id = ( ( 0x88 + i_position ) << 8 ) | 0xbd;
598 intf_ErrMsg( "dvd warning: DTS audio not handled yet"
603 intf_ErrMsg( "dvd warning: unknown audio type %.2x",
604 p_vts->vtsi_mat->vts_audio_attr[i-1].audio_format );
609 #define spu_control \
610 p_dvd->p_vts_file->vts_pgcit->pgci_srp[pgc_id-1].pgc->subp_control[i-1]
614 for( i = 1 ; i <= p_vts->vtsi_mat->nr_of_vts_subp_streams; i++ )
619 // IfoPrintSpu( p_dvd, i );
620 intf_WarnMsg( 12, "dvd spu %d 0x%02x", i, spu_control );
622 if( spu_control & 0x80000000 )
626 /* there are several streams for one spu */
627 if( p_vts->vtsi_mat->vts_video_attr.display_aspect_ratio )
630 switch( p_vts->vtsi_mat->vts_video_attr.permitted_df )
633 i_position = spu_control & 0xff;
636 i_position = ( spu_control >> 8 ) & 0xff;
639 i_position = ( spu_control >> 16 ) & 0xff;
646 i_position = ( spu_control >> 24 ) & 0x7F;
649 i_id = ( ( 0x20 + i_position ) << 8 ) | 0xbd;
650 p_es = input_AddES( p_input,
651 p_input->stream.pp_programs[0], i_id, 0 );
652 p_es->i_stream_id = 0xbd;
653 p_es->i_type = DVD_SPU_ES;
654 p_es->i_cat = SPU_ES;
655 strcpy( p_es->psz_desc, DecodeLanguage( hton16(
656 p_vts->vtsi_mat->vts_subp_attr[i-1].lang_code ) ) );
660 if( p_main->b_audio )
662 /* For audio: first one if none or a not existing one specified */
663 int i_audio = main_GetIntVariable( INPUT_CHANNEL_VAR, 1 );
664 if( i_audio < 0 || i_audio > i_audio_nb )
666 main_PutIntVariable( INPUT_CHANNEL_VAR, 1 );
669 if( i_audio > 0 && i_audio_nb > 0 )
671 if( main_GetIntVariable( AOUT_SPDIF_VAR, 0 ) ||
672 ( main_GetIntVariable( INPUT_AUDIO_VAR, 0 ) ==
676 while( ( p_input->stream.pp_es[i_ac3]->i_type !=
677 AC3_AUDIO_ES ) && ( i_ac3 <=
678 p_vts->vtsi_mat->nr_of_vts_audio_streams ) )
682 if( p_input->stream.pp_es[i_ac3]->i_type == AC3_AUDIO_ES )
684 input_SelectES( p_input,
685 p_input->stream.pp_es[i_ac3] );
690 input_SelectES( p_input,
691 p_input->stream.pp_es[i_audio] );
696 if( p_main->b_video )
698 /* for spu, default is none */
699 int i_spu = main_GetIntVariable( INPUT_SUBTITLE_VAR, 0 );
700 if( i_spu < 0 || i_spu > i_spu_nb )
702 main_PutIntVariable( INPUT_SUBTITLE_VAR, 0 );
705 if( i_spu > 0 && i_spu_nb > 0 )
707 i_spu += p_vts->vtsi_mat->nr_of_vts_audio_streams;
708 input_SelectES( p_input, p_input->stream.pp_es[i_spu] );
714 p_area = p_input->stream.p_selected_area;
721 if( p_area->i_part != p_dvd->i_chapter )
723 if( ( p_area->i_part > 0 ) &&
724 ( p_area->i_part <= p_area->i_part_nb ))
726 p_dvd->i_ttn = p_vmg->tt_srpt->title[p_area->i_id-1].vts_ttn;
727 pgc_id = p_vts->vts_ptt_srpt->title[
728 p_dvd->i_ttn-1].ptt[p_area->i_part-1].pgcn;
729 pgn = p_vts->vts_ptt_srpt->title[
730 p_dvd->i_ttn-1].ptt[p_area->i_part-1].pgn;
732 p_pgc = p_vts->vts_pgcit->pgci_srp[ pgc_id - 1 ].pgc;
734 p_dvd->i_cur_cell = p_pgc->program_map[ pgn - 1 ] - 1;
735 p_dvd->i_chapter = p_area->i_part;
736 DvdReadFindCell( p_dvd );
738 p_dvd->i_pack_len = 0;
739 p_dvd->i_next_vobu = p_dvd->i_cur_block =
740 p_pgc->cell_playback[p_dvd->i_cur_cell].first_sector;
744 p_area->i_part = p_dvd->i_chapter;
750 if( p_area->i_angle != p_dvd->i_angle )
752 p_dvd->i_angle = p_area->i_angle;
754 intf_WarnMsg( 3, "dvd info: angle %d selected", p_area->i_angle );
756 /* warn interface that something has changed */
757 p_area->i_tell = LB2OFF( p_dvd->i_next_vobu ) - p_area->i_start;
758 p_input->stream.b_seekable = 1;
759 p_input->stream.b_changed = 1;
765 /*****************************************************************************
766 * DvdReadRead: reads data packets into the netlist.
767 *****************************************************************************
768 * Returns -1 in case of error, 0 if everything went well, and 1 in case of
770 *****************************************************************************/
771 static int DvdReadRead( input_thread_t * p_input,
772 data_packet_t ** pp_data )
774 thread_dvd_data_t * p_dvd;
775 u8 p_data[DVD_VIDEO_LB_LEN];
776 struct iovec p_vec[DVD_DATA_READ_ONCE];
784 data_packet_t * p_data_p;
786 p_dvd = (thread_dvd_data_t *)p_input->p_plugin_data;
791 * Playback by cell in this pgc, starting at the cell for our chapter.
795 * End of pack, we select the following one
797 if( ! p_dvd->i_pack_len )
802 if( ( i_read = DVDReadBlocks( p_dvd->p_title, p_dvd->i_next_vobu,
805 intf_ErrMsg( "dvdread error: read failed for block %d",
806 p_dvd->i_next_vobu );
810 assert( p_data[41] == 0xbf && p_data[1027] == 0xbf );
813 * Parse the contained dsi packet.
816 DvdReadHandleDSI( p_dvd, p_data );
819 if( p_dvd->i_next_vobu >= p_dvd->i_end_block + 1 )
824 assert( p_dvd->i_pack_len < 1024 );
825 /* Ugly kludge: we send the pack block to the input for it
826 * sometimes has a zero scr and restart the sync */
827 // p_dvd->i_cur_block ++;
833 * Compute the number of blocks to read
835 i_blocks = p_dvd->i_pack_len >= DVD_BLOCK_READ_ONCE
836 ? DVD_BLOCK_READ_ONCE : p_dvd->i_pack_len;
837 p_dvd->i_pack_len -= i_blocks;
840 *pp_data = p_data_p = input_BuffersToIO( p_input->p_method_data, p_vec,
841 DVD_DATA_READ_ONCE );
843 if ( p_data_p == NULL )
849 i_read = DVDReadVBlocks( p_dvd->p_title, p_dvd->i_cur_block,
851 if( i_read != i_blocks )
853 intf_ErrMsg( "dvdread error: read failed for %d/%d blocks at 0x%02x",
854 i_read, i_blocks, p_dvd->i_cur_block );
858 p_dvd->i_cur_block += i_read;
860 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 /* Read headers to compute payload length */
865 for( i_iovec = 0 ; i_iovec < i_read ; i_iovec++ )
867 data_packet_t * p_current = p_data_p;
870 while( i_pos < DVD_VIDEO_LB_LEN )
872 pi_cur = (u8*)p_vec[i_iovec].iov_base + i_pos;
875 if( U32_AT( pi_cur ) != 0x1BA )
877 /* That's the case for all packets, except pack header. */
878 i_packet_size = U16_AT( pi_cur + 4 );
882 /* MPEG-2 Pack header. */
887 *pp_data = input_ShareBuffer( p_input->p_method_data,
893 p_data_p = p_data_p->p_next;
896 (*pp_data)->p_payload_start = (*pp_data)->p_demux_start =
897 (*pp_data)->p_demux_start + i_pos;
900 (*pp_data)->p_payload_end =
901 (*pp_data)->p_payload_start + i_packet_size + 6;
904 i_pos += i_packet_size + 6;
905 pp_data = &(*pp_data)->p_next;
909 p_input->pf_delete_packet( p_input->p_method_data, p_data_p );
912 vlc_mutex_lock( &p_input->stream.stream_lock );
914 p_input->stream.p_selected_area->i_tell =
915 LB2OFF( p_dvd->i_cur_block ) -
916 p_input->stream.p_selected_area->i_start;
920 /* We modify i_part only at end of chapter not to erase
921 * some modification from the interface */
922 p_input->stream.p_selected_area->i_part = p_dvd->i_chapter;
927 vlc_mutex_unlock( &p_input->stream.stream_lock );
933 /*****************************************************************************
934 * DVDRewind : reads a stream backward
935 *****************************************************************************/
936 static int DvdReadRewind( input_thread_t * p_input )
941 /*****************************************************************************
942 * DvdReadSeek : Goes to a given position on the stream.
943 *****************************************************************************
944 * This one is used by the input and translate chronological position from
945 * input to logical position on the device.
946 * The lock should be taken before calling this function.
947 *****************************************************************************/
948 static void DvdReadSeek( input_thread_t * p_input, off_t i_off )
950 thread_dvd_data_t * p_dvd;
958 i_off += p_input->stream.p_selected_area->i_start;
959 i_lb = OFF2LB( i_off );
960 p_dvd = ( thread_dvd_data_t * )p_input->p_plugin_data;
963 while( p_dvd->p_cur_pgc->cell_playback[i_cell].last_sector < i_lb )
975 pgc_id = p_dvd->p_vts_file->vts_ptt_srpt->title[
976 p_dvd->i_ttn-1].ptt[i_chapter].pgcn;
977 pgn = p_dvd->p_vts_file->vts_ptt_srpt->title[
978 p_dvd->i_ttn-1].ptt[i_chapter].pgn;
980 p_pgc = p_dvd->p_vts_file->vts_pgcit->pgci_srp[pgc_id-1].pgc;
981 i_tmp = p_pgc->program_map[pgn-1];
983 } while( i_tmp <= i_cell );
986 while( p_dvd->p_vts_file->vts_vobu_admap->vobu_start_sectors[i_vobu]
993 while( p_dvd->p_vts_file->vts_c_adt->cell_adr_table[i_sub_cell].start_sector <
994 p_dvd->p_vts_file->vts_vobu_admap->vobu_start_sectors[i_vobu-1] )
1000 intf_WarnMsg(12, "cell %d i_sub_cell %d chapter %d vobu %d cell_sector %d vobu_sector %d sub_cell_sector %d",
1001 i_cell, i_sub_cell,i_chapter, i_vobu,
1002 p_dvd->p_cur_pgc->cell_playback[i_cell].first_sector,
1003 p_dvd->p_vts_file->vts_vobu_admap->vobu_start_sectors[i_vobu],
1004 p_dvd->p_vts_file->vts_c_adt->cell_adr_table[i_sub_cell-1].start_sector);
1006 p_dvd->i_cur_block = i_lb;
1007 p_dvd->i_next_vobu =
1008 p_dvd->p_vts_file->vts_vobu_admap->vobu_start_sectors[i_vobu];
1009 p_dvd->i_pack_len = p_dvd->i_next_vobu - i_lb;
1010 p_dvd->i_cur_cell = i_cell;
1011 p_dvd->i_chapter = i_chapter;
1012 DvdReadFindCell( p_dvd );
1014 p_input->stream.p_selected_area->i_tell =
1015 LB2OFF ( p_dvd->i_cur_block )
1016 - p_input->stream.p_selected_area->i_start;
1017 p_input->stream.p_selected_area->i_part = p_dvd->i_chapter;
1022 /*****************************************************************************
1024 *****************************************************************************/
1025 static void DvdReadHandleDSI( thread_dvd_data_t * p_dvd, u8 * p_data )
1027 navRead_DSI( &(p_dvd->dsi_pack), &(p_data[ DSI_START_BYTE ]) );
1030 * Determine where we go next. These values are the ones we mostly
1033 p_dvd->i_cur_block = p_dvd->dsi_pack.dsi_gi.nv_pck_lbn;
1036 * If we're not at the end of this cell, we can determine the next
1037 * VOBU to display using the VOBU_SRI information section of the
1038 * DSI. Using this value correctly follows the current angle,
1039 * avoiding the doubled scenes in The Matrix, and makes our life
1042 if( p_dvd->dsi_pack.vobu_sri.next_vobu != SRI_END_OF_CELL )
1045 switch( ( p_dvd->dsi_pack.sml_pbi.category & 0xf000 ) >> 12 )
1048 /* interleaved unit with no angle */
1049 if( p_dvd->dsi_pack.sml_pbi.ilvu_sa != -1 )
1051 p_dvd->i_next_vobu = p_dvd->i_cur_block +
1052 p_dvd->dsi_pack.sml_pbi.ilvu_sa;
1053 p_dvd->i_pack_len = p_dvd->dsi_pack.sml_pbi.ilvu_ea;
1057 p_dvd->i_next_vobu = p_dvd->i_cur_block +
1058 p_dvd->dsi_pack.dsi_gi.vobu_ea + 1;
1059 p_dvd->i_pack_len = p_dvd->dsi_pack.dsi_gi.vobu_ea;
1063 /* vobu is end of ilvu */
1064 if( p_dvd->dsi_pack.sml_agli.data[p_dvd->i_angle-1].address )
1066 p_dvd->i_next_vobu = p_dvd->i_cur_block +
1067 p_dvd->dsi_pack.sml_agli.data[p_dvd->i_angle-1].address;
1068 p_dvd->i_pack_len = p_dvd->dsi_pack.sml_pbi.ilvu_ea;
1073 /* vobu is beginning of ilvu */
1077 /* entering interleaved section */
1079 /* non interleaved cells in interleaved section */
1081 p_dvd->i_next_vobu = p_dvd->i_cur_block +
1082 ( p_dvd->dsi_pack.vobu_sri.next_vobu & 0x7fffffff );
1083 p_dvd->i_pack_len = p_dvd->dsi_pack.dsi_gi.vobu_ea;
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;
1094 p_dvd->i_cur_cell = p_dvd->i_next_cell;
1095 DvdReadFindCell( p_dvd );
1097 p_dvd->i_pack_len = p_dvd->dsi_pack.dsi_gi.vobu_ea;
1098 p_dvd->i_next_vobu =
1099 p_dvd->p_cur_pgc->cell_playback[p_dvd->i_cur_cell].first_sector;
1103 intf_WarnMsg( 12, "scr %d lbn 0x%02x vobu_ea %d vob_id %d c_id %d",
1104 p_dvd->dsi_pack.dsi_gi.nv_pck_scr,
1105 p_dvd->dsi_pack.dsi_gi.nv_pck_lbn,
1106 p_dvd->dsi_pack.dsi_gi.vobu_ea,
1107 p_dvd->dsi_pack.dsi_gi.vobu_vob_idn,
1108 p_dvd->dsi_pack.dsi_gi.vobu_c_idn );
1110 intf_WarnMsg( 12, "cat 0x%02x ilvu_ea %d ilvu_sa %d size %d",
1111 p_dvd->dsi_pack.sml_pbi.category,
1112 p_dvd->dsi_pack.sml_pbi.ilvu_ea,
1113 p_dvd->dsi_pack.sml_pbi.ilvu_sa,
1114 p_dvd->dsi_pack.sml_pbi.size );
1116 intf_WarnMsg( 12, "next_vobu %d next_ilvu1 %d next_ilvu2 %d",
1117 p_dvd->dsi_pack.vobu_sri.next_vobu & 0x7fffffff,
1118 p_dvd->dsi_pack.sml_agli.data[ p_dvd->i_angle - 1 ].address,
1119 p_dvd->dsi_pack.sml_agli.data[ p_dvd->i_angle ].address);
1123 /*****************************************************************************
1125 *****************************************************************************/
1126 static void DvdReadFindCell( thread_dvd_data_t * p_dvd )
1131 #define cell p_dvd->p_cur_pgc->cell_playback
1132 if( cell[p_dvd->i_cur_cell].block_type == BLOCK_TYPE_ANGLE_BLOCK )
1135 p_dvd->i_next_cell = p_dvd->i_cur_cell + p_dvd->i_angle_nb;
1136 p_dvd->i_cur_cell += p_dvd->i_angle - 1;
1138 p_dvd->i_cur_cell += p_dvd->i_angle - 1;
1140 while( cell[p_dvd->i_cur_cell+i].block_mode != BLOCK_MODE_LAST_CELL )
1144 p_dvd->i_next_cell = p_dvd->i_cur_cell + i + 1;
1149 p_dvd->i_next_cell = p_dvd->i_cur_cell + 1;
1152 pgc_id = p_dvd->p_vts_file->vts_ptt_srpt->title[
1153 p_dvd->i_ttn-1].ptt[p_dvd->i_chapter].pgcn;
1154 pgn = p_dvd->p_vts_file->vts_ptt_srpt->title[
1155 p_dvd->i_ttn-1].ptt[p_dvd->i_chapter].pgn;
1156 p_pgc = p_dvd->p_vts_file->vts_pgcit->pgci_srp[pgc_id-1].pgc;
1158 if( p_pgc->program_map[pgn-1] <= p_dvd->i_cur_cell )