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.5 2001/12/20 23:53:49 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
88 #define DVD_DATA_READ_ONCE (4 * DVD_BLOCK_READ_ONCE)
90 /*****************************************************************************
92 *****************************************************************************/
93 /* called from outside */
94 static int DvdReadProbe ( probedata_t *p_data );
95 static void DvdReadInit ( struct input_thread_s * );
96 static void DvdReadEnd ( struct input_thread_s * );
97 static void DvdReadOpen ( struct input_thread_s * );
98 static void DvdReadClose ( struct input_thread_s * );
99 static int DvdReadSetArea ( struct input_thread_s *, struct input_area_s * );
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, 150 );
118 DECLARE_BUFFERS_NEWPES( FLAGS, NB_LIFO );
119 DECLARE_BUFFERS_DELETEPES( FLAGS, NB_LIFO, 150 );
120 DECLARE_BUFFERS_TOIO( FLAGS, DVD_LB_SIZE );
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_demux = input_DemuxPS;
139 input.pf_new_packet = input_NewPacket;
140 input.pf_new_pes = input_NewPES;
141 input.pf_delete_packet = input_DeletePacket;
142 input.pf_delete_pes = input_DeletePES;
143 input.pf_rewind = DvdReadRewind;
144 input.pf_seek = DvdReadSeek;
149 * Data reading functions
152 /*****************************************************************************
153 * DvdReadProbe: verifies that the stream is a PS stream
154 *****************************************************************************/
155 static int DvdReadProbe( probedata_t *p_data )
157 input_thread_t * p_input = (input_thread_t *)p_data;
159 char * psz_name = p_input->p_source;
162 if( TestMethod( INPUT_METHOD_VAR, "dvdread" ) )
167 if( ( strlen(psz_name) > 8 ) && !strncasecmp( psz_name, "dvdread:", 8 ) )
169 /* If the user specified "dvdread:" then he probably wants
170 * to use libdvdread */
178 /*****************************************************************************
179 * DvdReadInit: initializes DVD structures
180 *****************************************************************************/
181 static void DvdReadInit( input_thread_t * p_input )
183 thread_dvd_data_t * p_dvd;
184 input_area_t * p_area;
189 p_dvd = malloc( sizeof(thread_dvd_data_t) );
192 intf_ErrMsg( "dvdread error: out of memory" );
193 p_input->b_error = 1;
197 /* we take the pointer to dvd_reader_t back */
198 p_dvd->p_dvdread = (dvd_reader_t *)p_input->p_plugin_data;
199 p_dvd->p_title = NULL;
200 p_dvd->p_vts_file = NULL;
202 p_input->p_plugin_data = (void *)p_dvd;
204 if( (p_input->p_method_data = input_BuffersInit()) == NULL )
206 p_input->b_error = 1;
210 /* We read DVD_BLOCK_READ_ONCE in each loop, so the input will receive
211 * DVD_DATA_READ_ONCE at most */
212 p_dvd->i_block_once = DVD_BLOCK_READ_ONCE;
213 /* this value mustn't be modifed */
214 p_input->i_read_once = DVD_DATA_READ_ONCE;
216 /* Ifo allocation & initialisation */
217 if( ! ( p_dvd->p_vmg_file = ifoOpen( p_dvd->p_dvdread, 0 ) ) )
219 intf_ErrMsg( "dvdread error: can't open VMG info" );
220 DVDClose( p_dvd->p_dvdread );
222 p_input->b_error = 1;
225 intf_WarnMsg( 2, "dvdread info: VMG opened" );
227 /* Set stream and area data */
228 vlc_mutex_lock( &p_input->stream.stream_lock );
230 /* Initialize ES structures */
231 input_InitStream( p_input, sizeof( stream_ps_data_t ) );
233 /* disc input method */
234 p_input->stream.i_method = INPUT_METHOD_DVD;
236 #define tt_srpt p_dvd->p_vmg_file->tt_srpt
237 intf_WarnMsg( 2, "dvdread info: number of titles: %d", tt_srpt->nr_of_srpts );
239 #define area p_input->stream.pp_areas
240 /* We start from 1 here since the default area 0
241 * is reserved for video_ts.vob */
242 for( i = 1 ; i <= tt_srpt->nr_of_srpts ; i++ )
244 input_AddArea( p_input );
246 /* Titles are Program Chains */
249 /* Absolute start offset and size
250 * We can only set that with vts ifo, so we do it during the
251 * first call to DVDSetArea */
252 area[i]->i_start = 0;
255 /* Number of chapters */
256 area[i]->i_part_nb = tt_srpt->title[i-1].nr_of_ptts;
259 /* Number of angles */
260 area[i]->i_angle_nb = 0;
261 area[i]->i_angle = 1;
263 area[i]->i_plugin_data = tt_srpt->title[i-1].title_set_nr;
267 /* Get requested title - if none try the first title */
268 i_title = main_GetIntVariable( INPUT_TITLE_VAR, 1 );
269 if( i_title <= 0 || i_title > tt_srpt->nr_of_srpts )
276 /* Get requested chapter - if none defaults to first one */
277 i_chapter = main_GetIntVariable( INPUT_CHAPTER_VAR, 1 );
283 p_input->stream.pp_areas[i_title]->i_part = i_chapter;
285 p_area = p_input->stream.pp_areas[i_title];
287 /* set title, chapter, audio and subpic */
288 DvdReadSetArea( p_input, p_area );
290 vlc_mutex_unlock( &p_input->stream.stream_lock );
295 /*****************************************************************************
296 * DvdReadOpen: open libdvdread
297 *****************************************************************************/
298 static void DvdReadOpen( struct input_thread_s *p_input )
300 dvd_reader_t *p_dvdread;
302 vlc_mutex_lock( &p_input->stream.stream_lock );
304 /* If we are here we can control the pace... */
305 p_input->stream.b_pace_control = 1;
307 p_input->stream.b_seekable = 1;
308 p_input->stream.p_selected_area->i_size = 0;
310 p_input->stream.p_selected_area->i_tell = 0;
312 vlc_mutex_unlock( &p_input->stream.stream_lock );
314 /* XXX: put this shit in an access plugin */
315 if( strlen( p_input->p_source ) > 8
316 && !strncasecmp( p_input->p_source, "dvdread:", 8 ) )
318 p_dvdread = DVDOpen( p_input->p_source + 8 );
322 p_dvdread = DVDOpen( p_input->p_source );
327 p_input->b_error = 1;
331 /* the vlc input doesn't handle the file descriptor with libdvdread */
332 p_input->i_handle = -1;
334 /* p_dvdread is stored in p_plugin_data until we have
335 * malloc'ed a dvd_thread_data_t */
336 p_input->p_plugin_data = (void *) p_dvdread;
339 /*****************************************************************************
340 * DvdReadClose: close libdvdread
341 *****************************************************************************/
342 static void DvdReadClose( struct input_thread_s *p_input )
344 DVDClose( ((thread_dvd_data_t*)p_input->p_plugin_data)->p_dvdread );
345 free( p_input->p_plugin_data );
346 p_input->p_plugin_data = NULL;
350 /*****************************************************************************
351 * DvdReadEnd: frees unused data
352 *****************************************************************************/
353 static void DvdReadEnd( input_thread_t * p_input )
355 thread_dvd_data_t * p_dvd;
357 p_dvd = (thread_dvd_data_t *)p_input->p_plugin_data;
359 /* close libdvdread */
360 DVDCloseFile( p_dvd->p_title );
361 ifoClose( p_dvd->p_vts_file );
362 ifoClose( p_dvd->p_vmg_file );
364 input_BuffersEnd( p_input->p_method_data );
367 #define p_pgc p_dvd->p_cur_pgc
369 /*****************************************************************************
370 * DvdReadSetArea: initialize input data for title x, chapter y.
371 * It should be called for each user navigation request.
372 *****************************************************************************
373 * Take care that i_title starts from 0 (vmg) and i_chapter start from 1.
374 * Note that you have to take the lock before entering here.
375 *****************************************************************************/
376 static int DvdReadSetArea( input_thread_t * p_input, input_area_t * p_area )
378 thread_dvd_data_t * p_dvd;
382 p_dvd = (thread_dvd_data_t*)p_input->p_plugin_data;
384 /* we can't use the interface slider until initilization is complete */
385 p_input->stream.b_seekable = 0;
387 if( p_area != p_input->stream.p_selected_area )
389 es_descriptor_t * p_es;
395 #define p_vmg p_dvd->p_vmg_file
396 #define p_vts p_dvd->p_vts_file
397 if( p_dvd->p_title != NULL )
399 DVDCloseFile( p_dvd->p_title );
407 /* Reset the Chapter position of the old title */
408 p_input->stream.p_selected_area->i_part = 1;
411 * We have to load all title information
413 /* Change the default area */
414 p_input->stream.p_selected_area = p_area;
416 intf_WarnMsg( 12, "dvdread: open VTS %d, for title %d",
417 p_vmg->tt_srpt->title[ p_area->i_id - 1 ].title_set_nr,
421 if( ! ( p_vts = ifoOpen( p_dvd->p_dvdread,
422 p_vmg->tt_srpt->title[ p_area->i_id - 1 ].title_set_nr ) ) )
424 intf_ErrMsg( "dvdread error: fatal error in vts ifo" );
426 DVDClose( p_dvd->p_dvdread );
427 p_input->b_error = 1;
431 /* title position inside the selected vts */
432 p_dvd->i_ttn = p_vmg->tt_srpt->title[ p_area->i_id - 1 ].vts_ttn;
435 * Set selected title start
437 pgc_id = p_vts->vts_ptt_srpt->title[p_dvd->i_ttn-1].ptt[0].pgcn;
438 pgn = p_vts->vts_ptt_srpt->title[p_dvd->i_ttn-1].ptt[0].pgn;
439 p_pgc = p_vts->vts_pgcit->pgci_srp[ pgc_id - 1 ].pgc;
440 i_cell = p_pgc->program_map[ pgn - 1 ] - 1;
443 LB2OFF( p_dvd->p_cur_pgc->cell_playback[ i_cell ].first_sector );
445 intf_WarnMsg( 3, "dvdread: start %d vts_title %d pgc %d pgn %d",
446 p_area->i_id, p_dvd->i_ttn, pgc_id, pgn );
451 i_cell = p_dvd->p_cur_pgc->nr_of_cells - 1;
453 p_dvd->i_end_block = p_pgc->cell_playback[ i_cell ].last_sector;
454 p_area->i_size = LB2OFF( p_dvd->i_end_block )- p_area->i_start;
456 intf_WarnMsg( 12, "dvdread: start %lld size %lld end %d",
457 p_area->i_start , p_area->i_size, p_dvd->i_end_block );
460 * Set properties for current chapter
462 /* Remeber current chapter */
463 p_dvd->i_chapter = p_area->i_part;
466 pgc_id = p_vts->vts_ptt_srpt->title[
467 p_dvd->i_ttn-1].ptt[p_area->i_part-1].pgcn;
468 pgn = p_vts->vts_ptt_srpt->title[
469 p_dvd->i_ttn-1].ptt[p_area->i_part-1].pgn;
471 p_pgc = p_vts->vts_pgcit->pgci_srp[pgc_id-1].pgc;
472 p_dvd->i_pack_len = 0;
473 p_dvd->i_next_cell = p_dvd->i_cur_cell = p_pgc->program_map[pgn-1] - 1;
474 DvdReadFindCell( p_dvd );
476 p_dvd->i_next_vobu = p_dvd->i_cur_block =
477 p_pgc->cell_playback[p_dvd->i_cur_cell].first_sector;
482 p_area->i_angle_nb = p_vmg->tt_srpt->title[p_area->i_id-1].nr_of_angles;
483 p_area->i_angle = main_GetIntVariable( INPUT_ANGLE_VAR, 1 );
485 if( ( p_area->i_angle <= 0 ) || p_area->i_angle > p_area->i_angle_nb )
489 p_dvd->i_angle = p_area->i_angle;
490 p_dvd->i_angle_nb = p_area->i_angle_nb;
493 * We've got enough info, time to open the title set data.
495 if( ! ( p_dvd->p_title = DVDOpenFile( p_dvd->p_dvdread,
496 p_vmg->tt_srpt->title[ p_area->i_id - 1 ].title_set_nr,
497 DVD_READ_TITLE_VOBS ) ) )
499 intf_ErrMsg( "dvdread error: can't open title (VTS_%02d_1.VOB)",
500 p_vmg->tt_srpt->title[p_area->i_id-1].title_set_nr );
503 DVDClose( p_dvd->p_dvdread );
507 // IfoPrintTitle( p_dvd );
510 * Destroy obsolete ES by reinitializing program 0
511 * and find all ES in title with ifo data
513 if( p_input->stream.pp_programs != NULL )
515 /* We don't use input_EndStream here since
516 * we keep area structures */
518 for( i = 0 ; i < p_input->stream.i_selected_es_number ; i++ )
520 input_UnselectES( p_input, p_input->stream.pp_selected_es[i] );
523 free( p_input->stream.pp_selected_es );
524 input_DelProgram( p_input, p_input->stream.pp_programs[0] );
526 p_input->stream.pp_selected_es = NULL;
527 p_input->stream.i_selected_es_number = 0;
530 input_AddProgram( p_input, 0, sizeof( stream_ps_data_t ) );
532 /* No PSM to read in DVD mode, we already have all information */
533 p_input->stream.pp_programs[0]->b_is_ok = 1;
537 /* ES 0 -> video MPEG2 */
538 // IfoPrintVideo( p_dvd );
540 p_es = input_AddES( p_input, p_input->stream.pp_programs[0], 0xe0, 0 );
541 p_es->i_stream_id = 0xe0;
542 p_es->i_type = MPEG2_VIDEO_ES;
543 p_es->i_cat = VIDEO_ES;
544 if( p_main->b_video )
546 input_SelectES( p_input, p_es );
549 #define audio_control \
550 p_dvd->p_vts_file->vts_pgcit->pgci_srp[pgc_id-1].pgc->audio_control[i-1]
551 /* Audio ES, in the order they appear in .ifo */
552 for( i = 1 ; i <= p_vts->vtsi_mat->nr_of_vts_audio_streams ; i++ )
557 // IfoPrintAudio( p_dvd, i );
559 /* audio channel is active if first byte is 0x80 */
560 if( audio_control & 0x8000 )
563 i_position = ( audio_control & 0x7F00 ) >> 8;
565 intf_WarnMsg( 12, "dvd audio position %d", i_position );
566 switch( p_vts->vtsi_mat->vts_audio_attr[i-1].audio_format )
569 i_id = ( ( 0x80 + i_position ) << 8 ) | 0xbd;
570 p_es = input_AddES( p_input,
571 p_input->stream.pp_programs[0], i_id, 0 );
572 p_es->i_stream_id = 0xbd;
573 p_es->i_type = AC3_AUDIO_ES;
575 p_es->i_cat = AUDIO_ES;
576 strcpy( p_es->psz_desc, DecodeLanguage( hton16(
577 p_vts->vtsi_mat->vts_audio_attr[i-1].lang_code ) ) );
578 strcat( p_es->psz_desc, " (ac3)" );
582 case 0x03: /* MPEG audio */
583 i_id = 0xc0 + i_position;
584 p_es = input_AddES( p_input,
585 p_input->stream.pp_programs[0], i_id, 0 );
586 p_es->i_stream_id = i_id;
587 p_es->i_type = MPEG2_AUDIO_ES;
589 p_es->i_cat = AUDIO_ES;
590 strcpy( p_es->psz_desc, DecodeLanguage( hton16(
591 p_vts->vtsi_mat->vts_audio_attr[i-1].lang_code ) ) );
592 strcat( p_es->psz_desc, " (mpeg)" );
595 case 0x04: /* LPCM */
597 i_id = ( ( 0xa0 + i_position ) << 8 ) | 0xbd;
598 p_es = input_AddES( p_input,
599 p_input->stream.pp_programs[0], i_id, 0 );
600 p_es->i_stream_id = i_id;
601 p_es->i_type = LPCM_AUDIO_ES;
603 p_es->i_cat = AUDIO_ES;
604 strcpy( p_es->psz_desc, DecodeLanguage( hton16(
605 p_vts->vtsi_mat->vts_audio_attr[i-1].lang_code ) ) );
606 strcat( p_es->psz_desc, " (lpcm)" );
610 i_id = ( ( 0x88 + i_position ) << 8 ) | 0xbd;
611 intf_ErrMsg( "dvd warning: DTS audio not handled yet"
616 intf_ErrMsg( "dvd warning: unknown audio type %.2x",
617 p_vts->vtsi_mat->vts_audio_attr[i-1].audio_format );
622 #define spu_control \
623 p_dvd->p_vts_file->vts_pgcit->pgci_srp[pgc_id-1].pgc->subp_control[i-1]
627 for( i = 1 ; i <= p_vts->vtsi_mat->nr_of_vts_subp_streams; i++ )
632 // IfoPrintSpu( p_dvd, i );
633 intf_WarnMsg( 12, "dvd spu %d 0x%02x", i, spu_control );
635 if( spu_control & 0x80000000 )
639 /* there are several streams for one spu */
640 if( p_vts->vtsi_mat->vts_video_attr.display_aspect_ratio )
643 switch( p_vts->vtsi_mat->vts_video_attr.permitted_df )
646 i_position = spu_control & 0xff;
649 i_position = ( spu_control >> 8 ) & 0xff;
652 i_position = ( spu_control >> 16 ) & 0xff;
659 i_position = ( spu_control >> 24 ) & 0x7F;
662 i_id = ( ( 0x20 + i_position ) << 8 ) | 0xbd;
663 p_es = input_AddES( p_input,
664 p_input->stream.pp_programs[0], i_id, 0 );
665 p_es->i_stream_id = 0xbd;
666 p_es->i_type = DVD_SPU_ES;
667 p_es->i_cat = SPU_ES;
668 strcpy( p_es->psz_desc, DecodeLanguage( hton16(
669 p_vts->vtsi_mat->vts_subp_attr[i-1].lang_code ) ) );
673 if( p_main->b_audio )
675 /* For audio: first one if none or a not existing one specified */
676 int i_audio = main_GetIntVariable( INPUT_CHANNEL_VAR, 1 );
677 if( i_audio < 0 || i_audio > i_audio_nb )
679 main_PutIntVariable( INPUT_CHANNEL_VAR, 1 );
682 if( i_audio > 0 && i_audio_nb > 0 )
684 if( main_GetIntVariable( AOUT_SPDIF_VAR, 0 ) ||
685 ( main_GetIntVariable( INPUT_AUDIO_VAR, 0 ) ==
689 while( ( p_input->stream.pp_es[i_ac3]->i_type !=
690 AC3_AUDIO_ES ) && ( i_ac3 <=
691 p_vts->vtsi_mat->nr_of_vts_audio_streams ) )
695 if( p_input->stream.pp_es[i_ac3]->i_type == AC3_AUDIO_ES )
697 input_SelectES( p_input,
698 p_input->stream.pp_es[i_ac3] );
703 input_SelectES( p_input,
704 p_input->stream.pp_es[i_audio] );
709 if( p_main->b_video )
711 /* for spu, default is none */
712 int i_spu = main_GetIntVariable( INPUT_SUBTITLE_VAR, 0 );
713 if( i_spu < 0 || i_spu > i_spu_nb )
715 main_PutIntVariable( INPUT_SUBTITLE_VAR, 0 );
718 if( i_spu > 0 && i_spu_nb > 0 )
720 i_spu += p_vts->vtsi_mat->nr_of_vts_audio_streams;
721 input_SelectES( p_input, p_input->stream.pp_es[i_spu] );
727 p_area = p_input->stream.p_selected_area;
734 if( p_area->i_part != p_dvd->i_chapter )
736 if( ( p_area->i_part > 0 ) &&
737 ( p_area->i_part <= p_area->i_part_nb ))
739 p_dvd->i_ttn = p_vmg->tt_srpt->title[p_area->i_id-1].vts_ttn;
740 pgc_id = p_vts->vts_ptt_srpt->title[
741 p_dvd->i_ttn-1].ptt[p_area->i_part-1].pgcn;
742 pgn = p_vts->vts_ptt_srpt->title[
743 p_dvd->i_ttn-1].ptt[p_area->i_part-1].pgn;
745 p_pgc = p_vts->vts_pgcit->pgci_srp[ pgc_id - 1 ].pgc;
747 p_dvd->i_cur_cell = p_pgc->program_map[ pgn - 1 ] - 1;
748 p_dvd->i_chapter = p_area->i_part;
749 DvdReadFindCell( p_dvd );
751 p_dvd->i_pack_len = 0;
752 p_dvd->i_next_vobu = p_dvd->i_cur_block =
753 p_pgc->cell_playback[p_dvd->i_cur_cell].first_sector;
757 p_area->i_part = p_dvd->i_chapter;
763 if( p_area->i_angle != p_dvd->i_angle )
765 p_dvd->i_angle = p_area->i_angle;
767 intf_WarnMsg( 3, "dvd info: angle %d selected", p_area->i_angle );
769 /* warn interface that something has changed */
770 p_area->i_tell = LB2OFF( p_dvd->i_next_vobu ) - p_area->i_start;
771 p_input->stream.b_seekable = 1;
772 p_input->stream.b_changed = 1;
778 /*****************************************************************************
779 * DvdReadRead: reads data packets into the netlist.
780 *****************************************************************************
781 * Returns -1 in case of error, 0 if everything went well, and 1 in case of
783 *****************************************************************************/
784 static int DvdReadRead( input_thread_t * p_input,
785 data_packet_t ** pp_packets )
787 thread_dvd_data_t * p_dvd;
788 u8 p_data[DVD_VIDEO_LB_LEN];
789 struct iovec p_vec[DVD_DATA_READ_ONCE];
797 data_packet_t * p_data_p;
799 p_dvd = (thread_dvd_data_t *)p_input->p_plugin_data;
802 * Playback by cell in this pgc, starting at the cell for our chapter.
806 * End of pack, we select the following one
808 if( ! p_dvd->i_pack_len )
813 if( ( i_read = DVDReadBlocks( p_dvd->p_title, p_dvd->i_next_vobu,
816 intf_ErrMsg( "dvdread error: read failed for block %d",
817 p_dvd->i_next_vobu );
821 assert( p_data[41] == 0xbf && p_data[1027] == 0xbf );
824 * Parse the contained dsi packet.
827 DvdReadHandleDSI( p_dvd, p_data );
830 if( p_dvd->i_next_vobu >= p_dvd->i_end_block + 1 )
835 assert( p_dvd->i_pack_len < 1024 );
836 /* Ugly kludge: we send the pack block to the input for it
837 * sometimes has a zero scr and restart the sync */
838 // p_dvd->i_cur_block ++;
844 * Compute the number of blocks to read
846 i_blocks = p_dvd->i_pack_len >= DVD_BLOCK_READ_ONCE
847 ? DVD_BLOCK_READ_ONCE : p_dvd->i_pack_len;
848 p_dvd->i_pack_len -= i_blocks;
851 p_data_p = input_BuffersToIO( p_input->p_method_data, p_vec,
852 DVD_DATA_READ_ONCE );
854 if ( p_data_p == NULL )
860 i_read = DVDReadVBlocks( p_dvd->p_title, p_dvd->i_cur_block,
862 if( i_read != i_blocks )
864 intf_ErrMsg( "dvdread error: read failed for %d/%d blocks at 0x%02x",
865 i_read, i_blocks, p_dvd->i_cur_block );
869 p_dvd->i_cur_block += i_read;
871 intf_WarnMsg( 12, "dvdread i_blocks: %d len: %d current: 0x%02x", i_read, p_dvd->i_pack_len, p_dvd->i_cur_block );
875 /* Read headers to compute payload length */
876 for( i_iovec = 0 ; i_iovec < i_read ; i_iovec++ )
878 data_packet_t * p_current = p_data_p;
881 while( i_pos < DVD_LB_SIZE )
883 pi_cur = (u8*)p_vec[i_iovec].iov_base + i_pos;
886 if( U32_AT( pi_cur ) != 0x1BA )
888 /* That's the case for all packets, except pack header. */
889 i_packet_size = U16_AT( pi_cur + 4 );
893 /* MPEG-2 Pack header. */
898 pp_packets[i_packet] = input_ShareBuffer(
899 p_input->p_method_data, p_current );
903 pp_packets[i_packet] = p_data_p;
904 p_data_p = p_data_p->p_next;
907 pp_packets[i_packet]->p_payload_start =
908 pp_packets[i_packet]->p_demux_start =
909 pp_packets[i_packet]->p_demux_start + i_pos;
912 pp_packets[i_packet]->p_payload_end =
913 pp_packets[i_packet]->p_payload_start + i_packet_size + 6;
915 pp_packets[i_packet]->p_next = NULL;
916 pp_packets[i_packet]->b_discard_payload = 0;
919 i_pos += i_packet_size + 6;
923 pp_packets[i_packet] = NULL;
925 while( p_data_p != NULL )
927 data_packet_t * p_next = p_data_p->p_next;
928 p_input->pf_delete_packet( p_input->p_method_data, p_data_p );
932 vlc_mutex_lock( &p_input->stream.stream_lock );
934 p_input->stream.p_selected_area->i_tell =
935 LB2OFF( p_dvd->i_cur_block ) -
936 p_input->stream.p_selected_area->i_start;
940 /* We modify i_part only at end of chapter not to erase
941 * some modification from the interface */
942 p_input->stream.p_selected_area->i_part = p_dvd->i_chapter;
947 vlc_mutex_unlock( &p_input->stream.stream_lock );
953 /*****************************************************************************
954 * DVDRewind : reads a stream backward
955 *****************************************************************************/
956 static int DvdReadRewind( input_thread_t * p_input )
961 /*****************************************************************************
962 * DvdReadSeek : Goes to a given position on the stream.
963 *****************************************************************************
964 * This one is used by the input and translate chronological position from
965 * input to logical position on the device.
966 * The lock should be taken before calling this function.
967 *****************************************************************************/
968 static void DvdReadSeek( input_thread_t * p_input, off_t i_off )
970 thread_dvd_data_t * p_dvd;
978 i_off += p_input->stream.p_selected_area->i_start;
979 i_lb = OFF2LB( i_off );
980 p_dvd = ( thread_dvd_data_t * )p_input->p_plugin_data;
983 while( p_dvd->p_cur_pgc->cell_playback[i_cell].last_sector < i_lb )
995 pgc_id = p_dvd->p_vts_file->vts_ptt_srpt->title[
996 p_dvd->i_ttn-1].ptt[i_chapter].pgcn;
997 pgn = p_dvd->p_vts_file->vts_ptt_srpt->title[
998 p_dvd->i_ttn-1].ptt[i_chapter].pgn;
1000 p_pgc = p_dvd->p_vts_file->vts_pgcit->pgci_srp[pgc_id-1].pgc;
1001 i_tmp = p_pgc->program_map[pgn-1];
1003 } while( i_tmp <= i_cell );
1006 while( p_dvd->p_vts_file->vts_vobu_admap->vobu_start_sectors[i_vobu]
1013 while( p_dvd->p_vts_file->vts_c_adt->cell_adr_table[i_sub_cell].start_sector <
1014 p_dvd->p_vts_file->vts_vobu_admap->vobu_start_sectors[i_vobu-1] )
1020 intf_WarnMsg(12, "cell %d i_sub_cell %d chapter %d vobu %d cell_sector %d vobu_sector %d sub_cell_sector %d",
1021 i_cell, i_sub_cell,i_chapter, i_vobu,
1022 p_dvd->p_cur_pgc->cell_playback[i_cell].first_sector,
1023 p_dvd->p_vts_file->vts_vobu_admap->vobu_start_sectors[i_vobu],
1024 p_dvd->p_vts_file->vts_c_adt->cell_adr_table[i_sub_cell-1].start_sector);
1026 p_dvd->i_cur_block = i_lb;
1027 p_dvd->i_next_vobu =
1028 p_dvd->p_vts_file->vts_vobu_admap->vobu_start_sectors[i_vobu];
1029 p_dvd->i_pack_len = p_dvd->i_next_vobu - i_lb;
1030 p_dvd->i_cur_cell = i_cell;
1031 p_dvd->i_chapter = i_chapter;
1032 DvdReadFindCell( p_dvd );
1034 p_input->stream.p_selected_area->i_tell =
1035 LB2OFF ( p_dvd->i_cur_block )
1036 - p_input->stream.p_selected_area->i_start;
1037 p_input->stream.p_selected_area->i_part = p_dvd->i_chapter;
1042 /*****************************************************************************
1044 *****************************************************************************/
1045 static void DvdReadHandleDSI( thread_dvd_data_t * p_dvd, u8 * p_data )
1047 navRead_DSI( &(p_dvd->dsi_pack), &(p_data[ DSI_START_BYTE ]) );
1050 * Determine where we go next. These values are the ones we mostly
1053 p_dvd->i_cur_block = p_dvd->dsi_pack.dsi_gi.nv_pck_lbn;
1056 * If we're not at the end of this cell, we can determine the next
1057 * VOBU to display using the VOBU_SRI information section of the
1058 * DSI. Using this value correctly follows the current angle,
1059 * avoiding the doubled scenes in The Matrix, and makes our life
1062 if( p_dvd->dsi_pack.vobu_sri.next_vobu != SRI_END_OF_CELL )
1065 switch( ( p_dvd->dsi_pack.sml_pbi.category & 0xf000 ) >> 12 )
1068 /* interleaved unit with no angle */
1069 if( p_dvd->dsi_pack.sml_pbi.ilvu_sa != -1 )
1071 p_dvd->i_next_vobu = p_dvd->i_cur_block +
1072 p_dvd->dsi_pack.sml_pbi.ilvu_sa;
1073 p_dvd->i_pack_len = p_dvd->dsi_pack.sml_pbi.ilvu_ea;
1077 p_dvd->i_next_vobu = p_dvd->i_cur_block +
1078 p_dvd->dsi_pack.dsi_gi.vobu_ea + 1;
1079 p_dvd->i_pack_len = p_dvd->dsi_pack.dsi_gi.vobu_ea;
1083 /* vobu is end of ilvu */
1084 if( p_dvd->dsi_pack.sml_agli.data[p_dvd->i_angle-1].address )
1086 p_dvd->i_next_vobu = p_dvd->i_cur_block +
1087 p_dvd->dsi_pack.sml_agli.data[p_dvd->i_angle-1].address;
1088 p_dvd->i_pack_len = p_dvd->dsi_pack.sml_pbi.ilvu_ea;
1093 /* vobu is beginning of ilvu */
1097 /* entering interleaved section */
1099 /* non interleaved cells in interleaved section */
1101 p_dvd->i_next_vobu = p_dvd->i_cur_block +
1102 ( p_dvd->dsi_pack.vobu_sri.next_vobu & 0x7fffffff );
1103 p_dvd->i_pack_len = p_dvd->dsi_pack.dsi_gi.vobu_ea;
1107 p_dvd->i_next_vobu = p_dvd->i_cur_block +
1108 ( p_dvd->dsi_pack.vobu_sri.next_vobu & 0x7fffffff );
1109 p_dvd->i_pack_len = p_dvd->dsi_pack.dsi_gi.vobu_ea;
1114 p_dvd->i_cur_cell = p_dvd->i_next_cell;
1115 DvdReadFindCell( p_dvd );
1117 p_dvd->i_pack_len = p_dvd->dsi_pack.dsi_gi.vobu_ea;
1118 p_dvd->i_next_vobu =
1119 p_dvd->p_cur_pgc->cell_playback[p_dvd->i_cur_cell].first_sector;
1123 intf_WarnMsg( 12, "scr %d lbn 0x%02x vobu_ea %d vob_id %d c_id %d",
1124 p_dvd->dsi_pack.dsi_gi.nv_pck_scr,
1125 p_dvd->dsi_pack.dsi_gi.nv_pck_lbn,
1126 p_dvd->dsi_pack.dsi_gi.vobu_ea,
1127 p_dvd->dsi_pack.dsi_gi.vobu_vob_idn,
1128 p_dvd->dsi_pack.dsi_gi.vobu_c_idn );
1130 intf_WarnMsg( 12, "cat 0x%02x ilvu_ea %d ilvu_sa %d size %d",
1131 p_dvd->dsi_pack.sml_pbi.category,
1132 p_dvd->dsi_pack.sml_pbi.ilvu_ea,
1133 p_dvd->dsi_pack.sml_pbi.ilvu_sa,
1134 p_dvd->dsi_pack.sml_pbi.size );
1136 intf_WarnMsg( 12, "next_vobu %d next_ilvu1 %d next_ilvu2 %d",
1137 p_dvd->dsi_pack.vobu_sri.next_vobu & 0x7fffffff,
1138 p_dvd->dsi_pack.sml_agli.data[ p_dvd->i_angle - 1 ].address,
1139 p_dvd->dsi_pack.sml_agli.data[ p_dvd->i_angle ].address);
1143 /*****************************************************************************
1145 *****************************************************************************/
1146 static void DvdReadFindCell( thread_dvd_data_t * p_dvd )
1151 #define cell p_dvd->p_cur_pgc->cell_playback
1152 if( cell[p_dvd->i_cur_cell].block_type == BLOCK_TYPE_ANGLE_BLOCK )
1155 p_dvd->i_next_cell = p_dvd->i_cur_cell + p_dvd->i_angle_nb;
1156 p_dvd->i_cur_cell += p_dvd->i_angle - 1;
1158 p_dvd->i_cur_cell += p_dvd->i_angle - 1;
1160 while( cell[p_dvd->i_cur_cell+i].block_mode != BLOCK_MODE_LAST_CELL )
1164 p_dvd->i_next_cell = p_dvd->i_cur_cell + i + 1;
1169 p_dvd->i_next_cell = p_dvd->i_cur_cell + 1;
1172 pgc_id = p_dvd->p_vts_file->vts_ptt_srpt->title[
1173 p_dvd->i_ttn-1].ptt[p_dvd->i_chapter].pgcn;
1174 pgn = p_dvd->p_vts_file->vts_ptt_srpt->title[
1175 p_dvd->i_ttn-1].ptt[p_dvd->i_chapter].pgn;
1176 p_pgc = p_dvd->p_vts_file->vts_pgcit->pgci_srp[pgc_id-1].pgc;
1178 if( p_pgc->program_map[pgn-1] <= p_dvd->i_cur_cell )