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.10 2001/12/29 22:22:01 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, 1000 );
118 DECLARE_BUFFERS_NEWPES( FLAGS, NB_LIFO );
119 DECLARE_BUFFERS_DELETEPES( FLAGS, NB_LIFO, 1000 );
120 DECLARE_BUFFERS_TOIO( FLAGS, DVD_VIDEO_LB_LEN );
121 DECLARE_BUFFERS_SHAREBUFFER( FLAGS );
123 /*****************************************************************************
124 * Functions exported as capabilities. They are declared as static so that
125 * we don't pollute the namespace too much.
126 *****************************************************************************/
127 void _M( input_getfunctions )( function_list_t * p_function_list )
129 #define input p_function_list->functions.input
130 p_function_list->pf_probe = DvdReadProbe;
131 input.pf_init = DvdReadInit;
132 input.pf_open = DvdReadOpen;
133 input.pf_close = DvdReadClose;
134 input.pf_end = DvdReadEnd;
135 input.pf_init_bit_stream = InitBitstream;
136 input.pf_read = DvdReadRead;
137 input.pf_set_area = DvdReadSetArea;
138 input.pf_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;
214 /* Ifo allocation & initialisation */
215 if( ! ( p_dvd->p_vmg_file = ifoOpen( p_dvd->p_dvdread, 0 ) ) )
217 intf_ErrMsg( "dvdread error: can't open VMG info" );
218 DVDClose( p_dvd->p_dvdread );
220 p_input->b_error = 1;
223 intf_WarnMsg( 2, "dvdread info: VMG opened" );
225 /* Set stream and area data */
226 vlc_mutex_lock( &p_input->stream.stream_lock );
228 /* Initialize ES structures */
229 input_InitStream( p_input, sizeof( stream_ps_data_t ) );
231 /* disc input method */
232 p_input->stream.i_method = INPUT_METHOD_DVD;
234 #define tt_srpt p_dvd->p_vmg_file->tt_srpt
235 intf_WarnMsg( 2, "dvdread info: number of titles: %d", tt_srpt->nr_of_srpts );
237 #define area p_input->stream.pp_areas
238 /* We start from 1 here since the default area 0
239 * is reserved for video_ts.vob */
240 for( i = 1 ; i <= tt_srpt->nr_of_srpts ; i++ )
242 input_AddArea( p_input );
244 /* Titles are Program Chains */
247 /* Absolute start offset and size
248 * We can only set that with vts ifo, so we do it during the
249 * first call to DVDSetArea */
250 area[i]->i_start = 0;
253 /* Number of chapters */
254 area[i]->i_part_nb = tt_srpt->title[i-1].nr_of_ptts;
257 /* Number of angles */
258 area[i]->i_angle_nb = 0;
259 area[i]->i_angle = 1;
261 area[i]->i_plugin_data = tt_srpt->title[i-1].title_set_nr;
265 /* Get requested title - if none try the first title */
266 i_title = main_GetIntVariable( INPUT_TITLE_VAR, 1 );
267 if( i_title <= 0 || i_title > tt_srpt->nr_of_srpts )
274 /* Get requested chapter - if none defaults to first one */
275 i_chapter = main_GetIntVariable( INPUT_CHAPTER_VAR, 1 );
281 p_input->stream.pp_areas[i_title]->i_part = i_chapter;
283 p_area = p_input->stream.pp_areas[i_title];
285 /* set title, chapter, audio and subpic */
286 DvdReadSetArea( p_input, p_area );
288 vlc_mutex_unlock( &p_input->stream.stream_lock );
293 /*****************************************************************************
294 * DvdReadOpen: open libdvdread
295 *****************************************************************************/
296 static void DvdReadOpen( struct input_thread_s *p_input )
298 dvd_reader_t *p_dvdread;
300 vlc_mutex_lock( &p_input->stream.stream_lock );
302 /* If we are here we can control the pace... */
303 p_input->stream.b_pace_control = 1;
305 p_input->stream.b_seekable = 1;
306 p_input->stream.p_selected_area->i_size = 0;
308 p_input->stream.p_selected_area->i_tell = 0;
310 vlc_mutex_unlock( &p_input->stream.stream_lock );
312 /* XXX: put this shit in an access plugin */
313 if( strlen( p_input->p_source ) > 8
314 && !strncasecmp( p_input->p_source, "dvdread:", 8 ) )
316 p_dvdread = DVDOpen( p_input->p_source + 8 );
320 p_dvdread = DVDOpen( p_input->p_source );
325 p_input->b_error = 1;
329 /* the vlc input doesn't handle the file descriptor with libdvdread */
330 p_input->i_handle = -1;
332 /* p_dvdread is stored in p_plugin_data until we have
333 * malloc'ed a dvd_thread_data_t */
334 p_input->p_plugin_data = (void *) p_dvdread;
337 /*****************************************************************************
338 * DvdReadClose: close libdvdread
339 *****************************************************************************/
340 static void DvdReadClose( struct input_thread_s *p_input )
342 DVDClose( ((thread_dvd_data_t*)p_input->p_plugin_data)->p_dvdread );
343 free( p_input->p_plugin_data );
344 p_input->p_plugin_data = NULL;
348 /*****************************************************************************
349 * DvdReadEnd: frees unused data
350 *****************************************************************************/
351 static void DvdReadEnd( input_thread_t * p_input )
353 thread_dvd_data_t * p_dvd;
355 p_dvd = (thread_dvd_data_t *)p_input->p_plugin_data;
357 /* close libdvdread */
358 DVDCloseFile( p_dvd->p_title );
359 ifoClose( p_dvd->p_vts_file );
360 ifoClose( p_dvd->p_vmg_file );
362 input_BuffersEnd( p_input->p_method_data );
365 #define p_pgc p_dvd->p_cur_pgc
367 /*****************************************************************************
368 * DvdReadSetArea: initialize input data for title x, chapter y.
369 * It should be called for each user navigation request.
370 *****************************************************************************
371 * Take care that i_title starts from 0 (vmg) and i_chapter start from 1.
372 * Note that you have to take the lock before entering here.
373 *****************************************************************************/
374 static int DvdReadSetArea( input_thread_t * p_input, input_area_t * p_area )
376 thread_dvd_data_t * p_dvd;
380 p_dvd = (thread_dvd_data_t*)p_input->p_plugin_data;
382 /* we can't use the interface slider until initilization is complete */
383 p_input->stream.b_seekable = 0;
385 if( p_area != p_input->stream.p_selected_area )
387 es_descriptor_t * p_es;
393 #define p_vmg p_dvd->p_vmg_file
394 #define p_vts p_dvd->p_vts_file
395 if( p_dvd->p_title != NULL )
397 DVDCloseFile( p_dvd->p_title );
405 /* Reset the Chapter position of the old title */
406 p_input->stream.p_selected_area->i_part = 1;
409 * We have to load all title information
411 /* Change the default area */
412 p_input->stream.p_selected_area = p_area;
414 intf_WarnMsg( 12, "dvdread: open VTS %d, for title %d",
415 p_vmg->tt_srpt->title[ p_area->i_id - 1 ].title_set_nr,
419 if( ! ( p_vts = ifoOpen( p_dvd->p_dvdread,
420 p_vmg->tt_srpt->title[ p_area->i_id - 1 ].title_set_nr ) ) )
422 intf_ErrMsg( "dvdread error: fatal error in vts ifo" );
424 DVDClose( p_dvd->p_dvdread );
425 p_input->b_error = 1;
429 /* title position inside the selected vts */
430 p_dvd->i_ttn = p_vmg->tt_srpt->title[ p_area->i_id - 1 ].vts_ttn;
433 * Set selected title start
435 pgc_id = p_vts->vts_ptt_srpt->title[p_dvd->i_ttn-1].ptt[0].pgcn;
436 pgn = p_vts->vts_ptt_srpt->title[p_dvd->i_ttn-1].ptt[0].pgn;
437 p_pgc = p_vts->vts_pgcit->pgci_srp[ pgc_id - 1 ].pgc;
438 i_cell = p_pgc->program_map[ pgn - 1 ] - 1;
441 LB2OFF( p_dvd->p_cur_pgc->cell_playback[ i_cell ].first_sector );
443 intf_WarnMsg( 3, "dvdread: start %d vts_title %d pgc %d pgn %d",
444 p_area->i_id, p_dvd->i_ttn, pgc_id, pgn );
449 i_cell = p_dvd->p_cur_pgc->nr_of_cells - 1;
451 p_dvd->i_end_block = p_pgc->cell_playback[ i_cell ].last_sector;
452 p_area->i_size = LB2OFF( p_dvd->i_end_block )- p_area->i_start;
454 intf_WarnMsg( 12, "dvdread: start %lld size %lld end %d",
455 p_area->i_start , p_area->i_size, p_dvd->i_end_block );
458 * Set properties for current chapter
460 /* Remeber current chapter */
461 p_dvd->i_chapter = p_area->i_part;
464 pgc_id = p_vts->vts_ptt_srpt->title[
465 p_dvd->i_ttn-1].ptt[p_area->i_part-1].pgcn;
466 pgn = p_vts->vts_ptt_srpt->title[
467 p_dvd->i_ttn-1].ptt[p_area->i_part-1].pgn;
469 p_pgc = p_vts->vts_pgcit->pgci_srp[pgc_id-1].pgc;
470 p_dvd->i_pack_len = 0;
471 p_dvd->i_next_cell = p_dvd->i_cur_cell = p_pgc->program_map[pgn-1] - 1;
472 DvdReadFindCell( p_dvd );
474 p_dvd->i_next_vobu = p_dvd->i_cur_block =
475 p_pgc->cell_playback[p_dvd->i_cur_cell].first_sector;
480 p_area->i_angle_nb = p_vmg->tt_srpt->title[p_area->i_id-1].nr_of_angles;
481 p_area->i_angle = main_GetIntVariable( INPUT_ANGLE_VAR, 1 );
483 if( ( p_area->i_angle <= 0 ) || p_area->i_angle > p_area->i_angle_nb )
487 p_dvd->i_angle = p_area->i_angle;
488 p_dvd->i_angle_nb = p_area->i_angle_nb;
491 * We've got enough info, time to open the title set data.
493 if( ! ( p_dvd->p_title = DVDOpenFile( p_dvd->p_dvdread,
494 p_vmg->tt_srpt->title[ p_area->i_id - 1 ].title_set_nr,
495 DVD_READ_TITLE_VOBS ) ) )
497 intf_ErrMsg( "dvdread error: can't open title (VTS_%02d_1.VOB)",
498 p_vmg->tt_srpt->title[p_area->i_id-1].title_set_nr );
501 DVDClose( p_dvd->p_dvdread );
505 // IfoPrintTitle( p_dvd );
508 * Destroy obsolete ES by reinitializing program 0
509 * and find all ES in title with ifo data
511 if( p_input->stream.pp_programs != NULL )
513 /* We don't use input_EndStream here since
514 * we keep area structures */
516 for( i = 0 ; i < p_input->stream.i_selected_es_number ; i++ )
518 input_UnselectES( p_input, p_input->stream.pp_selected_es[i] );
521 free( p_input->stream.pp_selected_es );
522 input_DelProgram( p_input, p_input->stream.pp_programs[0] );
524 p_input->stream.pp_selected_es = NULL;
525 p_input->stream.i_selected_es_number = 0;
528 input_AddProgram( p_input, 0, sizeof( stream_ps_data_t ) );
530 /* No PSM to read in DVD mode, we already have all information */
531 p_input->stream.pp_programs[0]->b_is_ok = 1;
535 /* ES 0 -> video MPEG2 */
536 // IfoPrintVideo( p_dvd );
538 p_es = input_AddES( p_input, p_input->stream.pp_programs[0], 0xe0, 0 );
539 p_es->i_stream_id = 0xe0;
540 p_es->i_type = MPEG2_VIDEO_ES;
541 p_es->i_cat = VIDEO_ES;
542 if( p_main->b_video )
544 input_SelectES( p_input, p_es );
547 #define audio_control \
548 p_dvd->p_vts_file->vts_pgcit->pgci_srp[pgc_id-1].pgc->audio_control[i-1]
549 /* Audio ES, in the order they appear in .ifo */
550 for( i = 1 ; i <= p_vts->vtsi_mat->nr_of_vts_audio_streams ; i++ )
555 // IfoPrintAudio( p_dvd, i );
557 /* audio channel is active if first byte is 0x80 */
558 if( audio_control & 0x8000 )
561 i_position = ( audio_control & 0x7F00 ) >> 8;
563 intf_WarnMsg( 12, "dvd audio position %d", i_position );
564 switch( p_vts->vtsi_mat->vts_audio_attr[i-1].audio_format )
567 i_id = ( ( 0x80 + i_position ) << 8 ) | 0xbd;
568 p_es = input_AddES( p_input,
569 p_input->stream.pp_programs[0], i_id, 0 );
570 p_es->i_stream_id = 0xbd;
571 p_es->i_type = AC3_AUDIO_ES;
573 p_es->i_cat = AUDIO_ES;
574 strcpy( p_es->psz_desc, DecodeLanguage( hton16(
575 p_vts->vtsi_mat->vts_audio_attr[i-1].lang_code ) ) );
576 strcat( p_es->psz_desc, " (ac3)" );
580 case 0x03: /* MPEG audio */
581 i_id = 0xc0 + i_position;
582 p_es = input_AddES( p_input,
583 p_input->stream.pp_programs[0], i_id, 0 );
584 p_es->i_stream_id = i_id;
585 p_es->i_type = MPEG2_AUDIO_ES;
587 p_es->i_cat = AUDIO_ES;
588 strcpy( p_es->psz_desc, DecodeLanguage( hton16(
589 p_vts->vtsi_mat->vts_audio_attr[i-1].lang_code ) ) );
590 strcat( p_es->psz_desc, " (mpeg)" );
593 case 0x04: /* LPCM */
595 i_id = ( ( 0xa0 + i_position ) << 8 ) | 0xbd;
596 p_es = input_AddES( p_input,
597 p_input->stream.pp_programs[0], i_id, 0 );
598 p_es->i_stream_id = i_id;
599 p_es->i_type = LPCM_AUDIO_ES;
601 p_es->i_cat = AUDIO_ES;
602 strcpy( p_es->psz_desc, DecodeLanguage( hton16(
603 p_vts->vtsi_mat->vts_audio_attr[i-1].lang_code ) ) );
604 strcat( p_es->psz_desc, " (lpcm)" );
608 i_id = ( ( 0x88 + i_position ) << 8 ) | 0xbd;
609 intf_ErrMsg( "dvd warning: DTS audio not handled yet"
614 intf_ErrMsg( "dvd warning: unknown audio type %.2x",
615 p_vts->vtsi_mat->vts_audio_attr[i-1].audio_format );
620 #define spu_control \
621 p_dvd->p_vts_file->vts_pgcit->pgci_srp[pgc_id-1].pgc->subp_control[i-1]
625 for( i = 1 ; i <= p_vts->vtsi_mat->nr_of_vts_subp_streams; i++ )
630 // IfoPrintSpu( p_dvd, i );
631 intf_WarnMsg( 12, "dvd spu %d 0x%02x", i, spu_control );
633 if( spu_control & 0x80000000 )
637 /* there are several streams for one spu */
638 if( p_vts->vtsi_mat->vts_video_attr.display_aspect_ratio )
641 switch( p_vts->vtsi_mat->vts_video_attr.permitted_df )
644 i_position = spu_control & 0xff;
647 i_position = ( spu_control >> 8 ) & 0xff;
650 i_position = ( spu_control >> 16 ) & 0xff;
657 i_position = ( spu_control >> 24 ) & 0x7F;
660 i_id = ( ( 0x20 + i_position ) << 8 ) | 0xbd;
661 p_es = input_AddES( p_input,
662 p_input->stream.pp_programs[0], i_id, 0 );
663 p_es->i_stream_id = 0xbd;
664 p_es->i_type = DVD_SPU_ES;
665 p_es->i_cat = SPU_ES;
666 strcpy( p_es->psz_desc, DecodeLanguage( hton16(
667 p_vts->vtsi_mat->vts_subp_attr[i-1].lang_code ) ) );
671 if( p_main->b_audio )
673 /* For audio: first one if none or a not existing one specified */
674 int i_audio = main_GetIntVariable( INPUT_CHANNEL_VAR, 1 );
675 if( i_audio < 0 || i_audio > i_audio_nb )
677 main_PutIntVariable( INPUT_CHANNEL_VAR, 1 );
680 if( i_audio > 0 && i_audio_nb > 0 )
682 if( main_GetIntVariable( AOUT_SPDIF_VAR, 0 ) ||
683 ( main_GetIntVariable( INPUT_AUDIO_VAR, 0 ) ==
687 while( ( p_input->stream.pp_es[i_ac3]->i_type !=
688 AC3_AUDIO_ES ) && ( i_ac3 <=
689 p_vts->vtsi_mat->nr_of_vts_audio_streams ) )
693 if( p_input->stream.pp_es[i_ac3]->i_type == AC3_AUDIO_ES )
695 input_SelectES( p_input,
696 p_input->stream.pp_es[i_ac3] );
701 input_SelectES( p_input,
702 p_input->stream.pp_es[i_audio] );
707 if( p_main->b_video )
709 /* for spu, default is none */
710 int i_spu = main_GetIntVariable( INPUT_SUBTITLE_VAR, 0 );
711 if( i_spu < 0 || i_spu > i_spu_nb )
713 main_PutIntVariable( INPUT_SUBTITLE_VAR, 0 );
716 if( i_spu > 0 && i_spu_nb > 0 )
718 i_spu += p_vts->vtsi_mat->nr_of_vts_audio_streams;
719 input_SelectES( p_input, p_input->stream.pp_es[i_spu] );
725 p_area = p_input->stream.p_selected_area;
732 if( p_area->i_part != p_dvd->i_chapter )
734 if( ( p_area->i_part > 0 ) &&
735 ( p_area->i_part <= p_area->i_part_nb ))
737 p_dvd->i_ttn = p_vmg->tt_srpt->title[p_area->i_id-1].vts_ttn;
738 pgc_id = p_vts->vts_ptt_srpt->title[
739 p_dvd->i_ttn-1].ptt[p_area->i_part-1].pgcn;
740 pgn = p_vts->vts_ptt_srpt->title[
741 p_dvd->i_ttn-1].ptt[p_area->i_part-1].pgn;
743 p_pgc = p_vts->vts_pgcit->pgci_srp[ pgc_id - 1 ].pgc;
745 p_dvd->i_cur_cell = p_pgc->program_map[ pgn - 1 ] - 1;
746 p_dvd->i_chapter = p_area->i_part;
747 DvdReadFindCell( p_dvd );
749 p_dvd->i_pack_len = 0;
750 p_dvd->i_next_vobu = p_dvd->i_cur_block =
751 p_pgc->cell_playback[p_dvd->i_cur_cell].first_sector;
755 p_area->i_part = p_dvd->i_chapter;
761 if( p_area->i_angle != p_dvd->i_angle )
763 p_dvd->i_angle = p_area->i_angle;
765 intf_WarnMsg( 3, "dvd info: angle %d selected", p_area->i_angle );
767 /* warn interface that something has changed */
768 p_area->i_tell = LB2OFF( p_dvd->i_next_vobu ) - p_area->i_start;
769 p_input->stream.b_seekable = 1;
770 p_input->stream.b_changed = 1;
776 /*****************************************************************************
777 * DvdReadRead: reads data packets into the netlist.
778 *****************************************************************************
779 * Returns -1 in case of error, 0 if everything went well, and 1 in case of
781 *****************************************************************************/
782 static int DvdReadRead( input_thread_t * p_input,
783 data_packet_t ** pp_data )
785 thread_dvd_data_t * p_dvd;
786 u8 p_data[DVD_VIDEO_LB_LEN];
787 struct iovec p_vec[DVD_DATA_READ_ONCE];
795 data_packet_t * p_data_p;
797 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 *pp_data = 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_VIDEO_LB_LEN )
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_data = input_ShareBuffer( p_input->p_method_data,
904 p_data_p = p_data_p->p_next;
907 (*pp_data)->p_payload_start = (*pp_data)->p_demux_start =
908 (*pp_data)->p_demux_start + i_pos;
911 (*pp_data)->p_payload_end =
912 (*pp_data)->p_payload_start + i_packet_size + 6;
915 i_pos += i_packet_size + 6;
916 pp_data = &(*pp_data)->p_next;
920 p_input->pf_delete_packet( p_input->p_method_data, p_data_p );
923 vlc_mutex_lock( &p_input->stream.stream_lock );
925 p_input->stream.p_selected_area->i_tell =
926 LB2OFF( p_dvd->i_cur_block ) -
927 p_input->stream.p_selected_area->i_start;
931 /* We modify i_part only at end of chapter not to erase
932 * some modification from the interface */
933 p_input->stream.p_selected_area->i_part = p_dvd->i_chapter;
938 vlc_mutex_unlock( &p_input->stream.stream_lock );
944 /*****************************************************************************
945 * DVDRewind : reads a stream backward
946 *****************************************************************************/
947 static int DvdReadRewind( input_thread_t * p_input )
952 /*****************************************************************************
953 * DvdReadSeek : Goes to a given position on the stream.
954 *****************************************************************************
955 * This one is used by the input and translate chronological position from
956 * input to logical position on the device.
957 * The lock should be taken before calling this function.
958 *****************************************************************************/
959 static void DvdReadSeek( input_thread_t * p_input, off_t i_off )
961 thread_dvd_data_t * p_dvd;
969 i_off += p_input->stream.p_selected_area->i_start;
970 i_lb = OFF2LB( i_off );
971 p_dvd = ( thread_dvd_data_t * )p_input->p_plugin_data;
974 while( p_dvd->p_cur_pgc->cell_playback[i_cell].last_sector < i_lb )
986 pgc_id = p_dvd->p_vts_file->vts_ptt_srpt->title[
987 p_dvd->i_ttn-1].ptt[i_chapter].pgcn;
988 pgn = p_dvd->p_vts_file->vts_ptt_srpt->title[
989 p_dvd->i_ttn-1].ptt[i_chapter].pgn;
991 p_pgc = p_dvd->p_vts_file->vts_pgcit->pgci_srp[pgc_id-1].pgc;
992 i_tmp = p_pgc->program_map[pgn-1];
994 } while( i_tmp <= i_cell );
997 while( p_dvd->p_vts_file->vts_vobu_admap->vobu_start_sectors[i_vobu]
1004 while( p_dvd->p_vts_file->vts_c_adt->cell_adr_table[i_sub_cell].start_sector <
1005 p_dvd->p_vts_file->vts_vobu_admap->vobu_start_sectors[i_vobu-1] )
1011 intf_WarnMsg(12, "cell %d i_sub_cell %d chapter %d vobu %d cell_sector %d vobu_sector %d sub_cell_sector %d",
1012 i_cell, i_sub_cell,i_chapter, i_vobu,
1013 p_dvd->p_cur_pgc->cell_playback[i_cell].first_sector,
1014 p_dvd->p_vts_file->vts_vobu_admap->vobu_start_sectors[i_vobu],
1015 p_dvd->p_vts_file->vts_c_adt->cell_adr_table[i_sub_cell-1].start_sector);
1017 p_dvd->i_cur_block = i_lb;
1018 p_dvd->i_next_vobu =
1019 p_dvd->p_vts_file->vts_vobu_admap->vobu_start_sectors[i_vobu];
1020 p_dvd->i_pack_len = p_dvd->i_next_vobu - i_lb;
1021 p_dvd->i_cur_cell = i_cell;
1022 p_dvd->i_chapter = i_chapter;
1023 DvdReadFindCell( p_dvd );
1025 p_input->stream.p_selected_area->i_tell =
1026 LB2OFF ( p_dvd->i_cur_block )
1027 - p_input->stream.p_selected_area->i_start;
1028 p_input->stream.p_selected_area->i_part = p_dvd->i_chapter;
1033 /*****************************************************************************
1035 *****************************************************************************/
1036 static void DvdReadHandleDSI( thread_dvd_data_t * p_dvd, u8 * p_data )
1038 navRead_DSI( &(p_dvd->dsi_pack), &(p_data[ DSI_START_BYTE ]) );
1041 * Determine where we go next. These values are the ones we mostly
1044 p_dvd->i_cur_block = p_dvd->dsi_pack.dsi_gi.nv_pck_lbn;
1047 * If we're not at the end of this cell, we can determine the next
1048 * VOBU to display using the VOBU_SRI information section of the
1049 * DSI. Using this value correctly follows the current angle,
1050 * avoiding the doubled scenes in The Matrix, and makes our life
1053 if( p_dvd->dsi_pack.vobu_sri.next_vobu != SRI_END_OF_CELL )
1056 switch( ( p_dvd->dsi_pack.sml_pbi.category & 0xf000 ) >> 12 )
1059 /* interleaved unit with no angle */
1060 if( p_dvd->dsi_pack.sml_pbi.ilvu_sa != -1 )
1062 p_dvd->i_next_vobu = p_dvd->i_cur_block +
1063 p_dvd->dsi_pack.sml_pbi.ilvu_sa;
1064 p_dvd->i_pack_len = p_dvd->dsi_pack.sml_pbi.ilvu_ea;
1068 p_dvd->i_next_vobu = p_dvd->i_cur_block +
1069 p_dvd->dsi_pack.dsi_gi.vobu_ea + 1;
1070 p_dvd->i_pack_len = p_dvd->dsi_pack.dsi_gi.vobu_ea;
1074 /* vobu is end of ilvu */
1075 if( p_dvd->dsi_pack.sml_agli.data[p_dvd->i_angle-1].address )
1077 p_dvd->i_next_vobu = p_dvd->i_cur_block +
1078 p_dvd->dsi_pack.sml_agli.data[p_dvd->i_angle-1].address;
1079 p_dvd->i_pack_len = p_dvd->dsi_pack.sml_pbi.ilvu_ea;
1084 /* vobu is beginning of ilvu */
1088 /* entering interleaved section */
1090 /* non interleaved cells in interleaved section */
1092 p_dvd->i_next_vobu = p_dvd->i_cur_block +
1093 ( p_dvd->dsi_pack.vobu_sri.next_vobu & 0x7fffffff );
1094 p_dvd->i_pack_len = p_dvd->dsi_pack.dsi_gi.vobu_ea;
1098 p_dvd->i_next_vobu = p_dvd->i_cur_block +
1099 ( p_dvd->dsi_pack.vobu_sri.next_vobu & 0x7fffffff );
1100 p_dvd->i_pack_len = p_dvd->dsi_pack.dsi_gi.vobu_ea;
1105 p_dvd->i_cur_cell = p_dvd->i_next_cell;
1106 DvdReadFindCell( p_dvd );
1108 p_dvd->i_pack_len = p_dvd->dsi_pack.dsi_gi.vobu_ea;
1109 p_dvd->i_next_vobu =
1110 p_dvd->p_cur_pgc->cell_playback[p_dvd->i_cur_cell].first_sector;
1114 intf_WarnMsg( 12, "scr %d lbn 0x%02x vobu_ea %d vob_id %d c_id %d",
1115 p_dvd->dsi_pack.dsi_gi.nv_pck_scr,
1116 p_dvd->dsi_pack.dsi_gi.nv_pck_lbn,
1117 p_dvd->dsi_pack.dsi_gi.vobu_ea,
1118 p_dvd->dsi_pack.dsi_gi.vobu_vob_idn,
1119 p_dvd->dsi_pack.dsi_gi.vobu_c_idn );
1121 intf_WarnMsg( 12, "cat 0x%02x ilvu_ea %d ilvu_sa %d size %d",
1122 p_dvd->dsi_pack.sml_pbi.category,
1123 p_dvd->dsi_pack.sml_pbi.ilvu_ea,
1124 p_dvd->dsi_pack.sml_pbi.ilvu_sa,
1125 p_dvd->dsi_pack.sml_pbi.size );
1127 intf_WarnMsg( 12, "next_vobu %d next_ilvu1 %d next_ilvu2 %d",
1128 p_dvd->dsi_pack.vobu_sri.next_vobu & 0x7fffffff,
1129 p_dvd->dsi_pack.sml_agli.data[ p_dvd->i_angle - 1 ].address,
1130 p_dvd->dsi_pack.sml_agli.data[ p_dvd->i_angle ].address);
1134 /*****************************************************************************
1136 *****************************************************************************/
1137 static void DvdReadFindCell( thread_dvd_data_t * p_dvd )
1142 #define cell p_dvd->p_cur_pgc->cell_playback
1143 if( cell[p_dvd->i_cur_cell].block_type == BLOCK_TYPE_ANGLE_BLOCK )
1146 p_dvd->i_next_cell = p_dvd->i_cur_cell + p_dvd->i_angle_nb;
1147 p_dvd->i_cur_cell += p_dvd->i_angle - 1;
1149 p_dvd->i_cur_cell += p_dvd->i_angle - 1;
1151 while( cell[p_dvd->i_cur_cell+i].block_mode != BLOCK_MODE_LAST_CELL )
1155 p_dvd->i_next_cell = p_dvd->i_cur_cell + i + 1;
1160 p_dvd->i_next_cell = p_dvd->i_cur_cell + 1;
1163 pgc_id = p_dvd->p_vts_file->vts_ptt_srpt->title[
1164 p_dvd->i_ttn-1].ptt[p_dvd->i_chapter].pgcn;
1165 pgn = p_dvd->p_vts_file->vts_ptt_srpt->title[
1166 p_dvd->i_ttn-1].ptt[p_dvd->i_chapter].pgn;
1167 p_pgc = p_dvd->p_vts_file->vts_pgcit->pgci_srp[pgc_id-1].pgc;
1169 if( p_pgc->program_map[pgn-1] <= p_dvd->i_cur_cell )