1 /*****************************************************************************
2 * input_dvd.c: DVD raw reading plugin.
3 *****************************************************************************
4 * This plugins should handle all the known specificities of the DVD format,
5 * especially the 2048 bytes logical block size.
7 * -libdvdcss for access and unscrambling
8 * -dvd_ifo for ifo parsing and analyse
9 * -dvd_udf to find files
10 *****************************************************************************
11 * Copyright (C) 1998-2001 VideoLAN
12 * $Id: input_dvd.c,v 1.117 2001/12/31 01:13:12 massiot Exp $
14 * Author: Stéphane Borel <stef@via.ecp.fr>
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 *****************************************************************************/
38 #include <videolan/vlc.h>
45 #include <sys/types.h>
49 #ifdef STRNCASECMP_IN_STRINGS_H
54 # include <io.h> /* read() */
56 # include <sys/uio.h> /* struct iovec */
60 # include "dummy_dvdcss.h"
62 # include <videolan/dvdcss.h>
66 # include "input_iovec.h"
69 #include "stream_control.h"
70 #include "input_ext-intf.h"
71 #include "input_ext-dec.h"
72 #include "input_ext-plugins.h"
74 #include "input_dvd.h"
76 #include "dvd_summary.h"
81 /* how many blocks DVDRead will read in each loop */
82 #define DVD_BLOCK_READ_ONCE 64
84 /*****************************************************************************
86 *****************************************************************************/
87 /* called from outside */
88 static int DVDProbe ( probedata_t *p_data );
89 static void DVDInit ( struct input_thread_s * );
90 static void DVDEnd ( struct input_thread_s * );
91 static void DVDOpen ( struct input_thread_s * );
92 static void DVDClose ( struct input_thread_s * );
93 static int DVDSetArea ( struct input_thread_s *, struct input_area_s * );
94 static int DVDSetProgram ( struct input_thread_s *, pgrm_descriptor_t * );
95 static int DVDRead ( struct input_thread_s *, data_packet_t ** );
96 static void DVDSeek ( struct input_thread_s *, off_t );
97 static int DVDRewind ( struct input_thread_s * );
99 /* called only inside */
100 static int DVDChooseAngle( thread_dvd_data_t * );
101 static int DVDFindCell( thread_dvd_data_t * );
102 static int DVDFindSector( thread_dvd_data_t * );
103 static int DVDChapterSelect( thread_dvd_data_t *, int );
105 /*****************************************************************************
106 * Declare a buffer manager
107 *****************************************************************************/
108 #define FLAGS BUFFERS_UNIQUE_SIZE
110 DECLARE_BUFFERS_SHARED( FLAGS, NB_LIFO );
111 DECLARE_BUFFERS_INIT( FLAGS, NB_LIFO );
112 DECLARE_BUFFERS_END_SHARED( FLAGS, NB_LIFO );
113 DECLARE_BUFFERS_NEWPACKET_SHARED( FLAGS, NB_LIFO );
114 DECLARE_BUFFERS_DELETEPACKET_SHARED( FLAGS, NB_LIFO, 1000 );
115 DECLARE_BUFFERS_NEWPES( FLAGS, NB_LIFO );
116 DECLARE_BUFFERS_DELETEPES( FLAGS, NB_LIFO, 1000 );
117 DECLARE_BUFFERS_TOIO( FLAGS, DVD_LB_SIZE );
118 DECLARE_BUFFERS_SHAREBUFFER( FLAGS );
120 /*****************************************************************************
121 * Functions exported as capabilities. They are declared as static so that
122 * we don't pollute the namespace too much.
123 *****************************************************************************/
124 void _M( input_getfunctions )( function_list_t * p_function_list )
126 #define input p_function_list->functions.input
127 p_function_list->pf_probe = DVDProbe;
128 input.pf_init = DVDInit;
129 input.pf_open = DVDOpen;
130 input.pf_close = DVDClose;
131 input.pf_end = DVDEnd;
132 input.pf_init_bit_stream = InitBitstream;
133 input.pf_read = DVDRead;
134 input.pf_set_area = DVDSetArea;
135 input.pf_set_program = DVDSetProgram;
136 input.pf_demux = input_DemuxPS;
137 input.pf_new_packet = input_NewPacket;
138 input.pf_new_pes = input_NewPES;
139 input.pf_delete_packet = input_DeletePacket;
140 input.pf_delete_pes = input_DeletePES;
141 input.pf_rewind = DVDRewind;
142 input.pf_seek = DVDSeek;
147 * Data reading functions
150 /*****************************************************************************
151 * DVDProbe: verifies that the stream is a PS stream
152 *****************************************************************************/
153 static int DVDProbe( probedata_t *p_data )
155 input_thread_t * p_input = (input_thread_t *)p_data;
157 char * psz_name = p_input->p_source;
160 if( ( strlen(psz_name) > 4 ) && !strncasecmp( psz_name, "dvd:", 4 ) )
162 /* If the user specified "dvd:" then it's probably a DVD */
170 /*****************************************************************************
171 * DVDInit: initializes DVD structures
172 *****************************************************************************/
173 static void DVDInit( input_thread_t * p_input )
175 thread_dvd_data_t * p_dvd;
176 input_area_t * p_area;
181 p_dvd = malloc( sizeof(thread_dvd_data_t) );
184 intf_ErrMsg( "dvd error: out of memory" );
185 p_input->b_error = 1;
189 p_input->p_plugin_data = (void *)p_dvd;
191 if( (p_input->p_method_data = input_BuffersInit()) == NULL )
193 p_input->b_error = 1;
197 p_dvd->dvdhandle = (dvdcss_handle) p_input->p_handle;
199 if( dvdcss_seek( p_dvd->dvdhandle, 0, DVDCSS_NOFLAGS ) < 0 )
201 intf_ErrMsg( "dvd error: %s", dvdcss_error( p_dvd->dvdhandle ) );
202 input_BuffersEnd( p_input->p_method_data );
203 p_input->b_error = 1;
207 /* We read DVD_BLOCK_READ_ONCE in each loop */
208 p_dvd->i_block_once = DVD_BLOCK_READ_ONCE;
210 /* Ifo allocation & initialisation */
211 if( IfoCreate( p_dvd ) < 0 )
213 intf_ErrMsg( "dvd error: allcation error in ifo" );
215 input_BuffersEnd( p_input->p_method_data );
216 p_input->b_error = 1;
220 if( IfoInit( p_dvd->p_ifo ) < 0 )
222 intf_ErrMsg( "dvd error: fatal failure in ifo" );
223 IfoDestroy( p_dvd->p_ifo );
225 input_BuffersEnd( p_input->p_method_data );
226 p_input->b_error = 1;
230 /* Set stream and area data */
231 vlc_mutex_lock( &p_input->stream.stream_lock );
233 /* Initialize ES structures */
234 input_InitStream( p_input, sizeof( stream_ps_data_t ) );
236 #define title_inf p_dvd->p_ifo->vmg.title_inf
237 intf_WarnMsg( 2, "dvd info: number of titles: %d", title_inf.i_title_nb );
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 <= title_inf.i_title_nb ; 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 = title_inf.p_attr[i-1].i_chapter_nb;
259 /* Number of angles */
260 area[i]->i_angle_nb = 0;
261 area[i]->i_angle = 1;
263 /* Offset to vts_i_0.ifo */
264 area[i]->i_plugin_data = p_dvd->p_ifo->i_start +
265 title_inf.p_attr[i-1].i_start_sector;
269 /* Get requested title - if none try the first title */
270 i_title = main_GetIntVariable( INPUT_TITLE_VAR, 1 );
271 if( i_title <= 0 || i_title > title_inf.i_title_nb )
278 /* Get requested chapter - if none defaults to first one */
279 i_chapter = main_GetIntVariable( INPUT_CHAPTER_VAR, 1 );
285 p_area = p_input->stream.pp_areas[i_title];
286 p_area->i_part = i_chapter;
288 /* set title, chapter, audio and subpic */
289 DVDSetArea( p_input, p_area );
291 vlc_mutex_unlock( &p_input->stream.stream_lock );
296 /*****************************************************************************
298 *****************************************************************************/
299 static void DVDOpen( struct input_thread_s *p_input )
301 dvdcss_handle dvdhandle;
305 vlc_mutex_lock( &p_input->stream.stream_lock );
307 /* If we are here we can control the pace... */
308 p_input->stream.b_pace_control = 1;
310 p_input->stream.b_seekable = 1;
311 p_input->stream.p_selected_area->i_size = 0;
313 p_input->stream.p_selected_area->i_tell = 0;
315 vlc_mutex_unlock( &p_input->stream.stream_lock );
317 /* Parse input string : dvd:device[@rawdevice] */
318 if( strlen( p_input->p_source ) > 4
319 && !strncasecmp( p_input->p_source, "dvd:", 4 ) )
321 psz_parser = psz_device = p_input->p_source + 4;
325 psz_parser = psz_device = p_input->p_source;
328 while( *psz_parser && *psz_parser != '@' )
333 if( *psz_parser == '@' )
335 /* Found raw device */
339 main_PutPszVariable( "DVDCSS_RAW_DEVICE", psz_parser );
342 intf_WarnMsg( 2, "input: dvd=%s raw=%s", psz_device, psz_parser );
344 dvdhandle = dvdcss_open( psz_device );
346 if( dvdhandle == NULL )
348 intf_ErrMsg( "dvd error: dvdcss can't open device" );
349 p_input->b_error = 1;
353 p_input->p_handle = (void *) dvdhandle;
356 /*****************************************************************************
357 * DVDClose: close dvd
358 *****************************************************************************/
359 static void DVDClose( struct input_thread_s *p_input )
361 /* Clean up libdvdcss */
362 dvdcss_close( (dvdcss_handle) p_input->p_handle );
365 /*****************************************************************************
366 * DVDEnd: frees unused data
367 *****************************************************************************/
368 static void DVDEnd( input_thread_t * p_input )
370 thread_dvd_data_t * p_dvd;
372 p_dvd = (thread_dvd_data_t*)p_input->p_plugin_data;
374 IfoDestroy( p_dvd->p_ifo );
378 input_BuffersEnd( p_input->p_method_data );
381 /*****************************************************************************
382 * DVDSetProgram: Does nothing, a DVD is mono-program
383 *****************************************************************************/
384 static int DVDSetProgram( input_thread_t * p_input,
385 pgrm_descriptor_t * p_program )
390 /*****************************************************************************
391 * DVDSetArea: initialize input data for title x, chapter y.
392 * It should be called for each user navigation request.
393 *****************************************************************************
394 * Take care that i_title starts from 0 (vmg) and i_chapter start from 1.
395 * Note that you have to take the lock before entering here.
396 *****************************************************************************/
397 static int DVDSetArea( input_thread_t * p_input, input_area_t * p_area )
399 thread_dvd_data_t * p_dvd;
400 es_descriptor_t * p_es;
409 p_dvd = (thread_dvd_data_t*)p_input->p_plugin_data;
411 /* we can't use the interface slider until initilization is complete */
412 p_input->stream.b_seekable = 0;
414 if( p_area != p_input->stream.p_selected_area )
416 /* Reset the Chapter position of the old title */
417 p_input->stream.p_selected_area->i_part = 0;
420 * We have to load all title information
422 /* Change the default area */
423 p_input->stream.p_selected_area =
424 p_input->stream.pp_areas[p_area->i_id];
426 /* title number: it is not vts nb!,
427 * it is what appears in the interface list */
428 p_dvd->i_title = p_area->i_id;
429 p_dvd->p_ifo->i_title = p_dvd->i_title;
431 /* set number of chapters of current title */
432 p_dvd->i_chapter_nb = p_area->i_part_nb;
435 if( IfoTitleSet( p_dvd->p_ifo ) < 0 )
437 intf_ErrMsg( "dvd error: fatal error in vts ifo" );
439 p_input->b_error = 1;
443 #define vmg p_dvd->p_ifo->vmg
444 #define vts p_dvd->p_ifo->vts
445 /* title position inside the selected vts */
446 i_vts_title = vmg.title_inf.p_attr[p_dvd->i_title-1].i_title_num;
448 vts.title_inf.p_title_start[i_vts_title-1].i_title_id;
450 intf_WarnMsgImm( 3, "dvd: title %d vts_title %d pgc %d",
451 p_dvd->i_title, i_vts_title, p_dvd->i_title_id );
457 p_dvd->i_angle_nb = vmg.title_inf.p_attr[p_dvd->i_title-1].i_angle_nb;
458 p_dvd->i_angle = main_GetIntVariable( INPUT_ANGLE_VAR, 1 );
459 if( ( p_dvd->i_angle <= 0 ) || p_dvd->i_angle > p_dvd->i_angle_nb )
465 * Set selected title start and size
468 /* title set offset XXX: convert to block values */
469 p_dvd->i_title_start =
470 vts.i_pos + vts.manager_inf.i_title_vob_start_sector;
472 /* last video cell */
474 p_dvd->i_prg_cell = -1 +
475 vts.title_unit.p_title[p_dvd->i_title_id-1].title.i_cell_nb;
477 if( DVDFindCell( p_dvd ) < 0 )
479 intf_ErrMsg( "dvd error: can't find title end" );
480 p_input->b_error = 1;
484 /* temporary hack to fix size in some dvds */
485 if( p_dvd->i_cell >= vts.cell_inf.i_cell_nb )
487 p_dvd->i_cell = vts.cell_inf.i_cell_nb - 1;
491 p_dvd->i_size = vts.cell_inf.p_cell_map[p_dvd->i_cell].i_end_sector;
493 if( DVDChapterSelect( p_dvd, 1 ) < 0 )
495 intf_ErrMsg( "dvd error: can't find first chapter" );
496 p_input->b_error = 1;
500 /* Force libdvdcss to check its title key.
501 * It is only useful for title cracking method. Methods using the
502 * decrypted disc key are fast enough to check the key at each seek */
504 if( dvdcss_seek( p_dvd->dvdhandle, p_dvd->i_start,
505 DVDCSS_SEEK_KEY ) < 0 )
507 intf_ErrMsg( "dvd error: %s", dvdcss_error( p_dvd->dvdhandle ) );
511 p_dvd->i_size -= p_dvd->i_sector + 1;
513 IfoPrintTitle( p_dvd );
515 /* Area definition */
516 p_input->stream.p_selected_area->i_start = LB2OFF( p_dvd->i_start );
517 p_input->stream.p_selected_area->i_size = LB2OFF( p_dvd->i_size );
518 p_input->stream.p_selected_area->i_angle_nb = p_dvd->i_angle_nb;
519 p_input->stream.p_selected_area->i_angle = p_dvd->i_angle;
522 /* start at the beginning of the title */
523 /* FIXME: create a conf option to select whether to restart
525 p_input->stream.p_selected_area->i_tell = 0;
526 p_input->stream.p_selected_area->i_part = 1;
530 * Destroy obsolete ES by reinitializing program 0
531 * and find all ES in title with ifo data
533 if( p_input->stream.pp_programs != NULL )
535 /* We don't use input_EndStream here since
536 * we keep area structures */
538 for( i = 0 ; i < p_input->stream.i_selected_es_number ; i++ )
540 input_UnselectES( p_input, p_input->stream.pp_selected_es[i] );
543 free( p_input->stream.pp_selected_es );
544 input_DelProgram( p_input, p_input->stream.p_selected_program );
546 p_input->stream.pp_selected_es = NULL;
547 p_input->stream.i_selected_es_number = 0;
550 input_AddProgram( p_input, 0, sizeof( stream_ps_data_t ) );
551 p_input->stream.p_selected_program = p_input->stream.pp_programs[0];
553 /* No PSM to read in DVD mode, we already have all information */
554 p_input->stream.p_selected_program->b_is_ok = 1;
558 /* ES 0 -> video MPEG2 */
559 IfoPrintVideo( p_dvd );
561 p_es = input_AddES( p_input, p_input->stream.p_selected_program,
563 p_es->i_stream_id = 0xe0;
564 p_es->i_type = MPEG2_VIDEO_ES;
565 p_es->i_cat = VIDEO_ES;
566 if( p_main->b_video )
568 input_SelectES( p_input, p_es );
571 #define audio_status \
572 vts.title_unit.p_title[p_dvd->i_title_id-1].title.pi_audio_status[i-1]
573 /* Audio ES, in the order they appear in .ifo */
574 for( i = 1 ; i <= vts.manager_inf.i_audio_nb ; i++ )
576 IfoPrintAudio( p_dvd, i );
578 /* audio channel is active if first byte is 0x80 */
579 if( audio_status.i_available )
583 switch( vts.manager_inf.p_audio_attr[i-1].i_coding_mode )
586 i_id = ( ( 0x80 + audio_status.i_position ) << 8 ) | 0xbd;
587 p_es = input_AddES( p_input,
588 p_input->stream.p_selected_program, i_id, 0 );
589 p_es->i_stream_id = 0xbd;
590 p_es->i_type = AC3_AUDIO_ES;
592 p_es->i_cat = AUDIO_ES;
593 strcpy( p_es->psz_desc, DecodeLanguage( hton16(
594 vts.manager_inf.p_audio_attr[i-1].i_lang_code ) ) );
595 strcat( p_es->psz_desc, " (ac3)" );
599 case 0x03: /* MPEG audio */
600 i_id = 0xc0 + audio_status.i_position;
601 p_es = input_AddES( p_input,
602 p_input->stream.p_selected_program, i_id
604 p_es->i_stream_id = i_id;
605 p_es->i_type = MPEG2_AUDIO_ES;
607 p_es->i_cat = AUDIO_ES;
608 strcpy( p_es->psz_desc, DecodeLanguage( hton16(
609 vts.manager_inf.p_audio_attr[i-1].i_lang_code ) ) );
610 strcat( p_es->psz_desc, " (mpeg)" );
613 case 0x04: /* LPCM */
615 i_id = ( ( 0xa0 + audio_status.i_position ) << 8 ) | 0xbd;
616 p_es = input_AddES( p_input,
617 p_input->stream.p_selected_program,
619 p_es->i_stream_id = 0xbd;
620 p_es->i_type = LPCM_AUDIO_ES;
622 p_es->i_cat = AUDIO_ES;
623 strcpy( p_es->psz_desc, DecodeLanguage( hton16(
624 vts.manager_inf.p_audio_attr[i-1].i_lang_code ) ) );
625 strcat( p_es->psz_desc, " (lpcm)" );
629 i_id = ( ( 0x88 + audio_status.i_position ) << 8 ) | 0xbd;
630 intf_ErrMsg( "dvd warning: DTS audio not handled yet"
635 intf_ErrMsg( "dvd warning: unknown audio type %.2x",
636 vts.manager_inf.p_audio_attr[i-1].i_coding_mode );
642 vts.title_unit.p_title[p_dvd->i_title_id-1].title.pi_spu_status[i-1]
646 for( i = 1 ; i <= vts.manager_inf.i_spu_nb; i++ )
648 IfoPrintSpu( p_dvd, i );
650 if( spu_status.i_available )
654 /* there are several streams for one spu */
655 if( vts.manager_inf.video_attr.i_ratio )
658 switch( vts.manager_inf.video_attr.i_perm_displ )
661 i_id = ( ( 0x20 + spu_status.i_position_pan ) << 8 )
665 i_id = ( ( 0x20 + spu_status.i_position_letter ) << 8 )
669 i_id = ( ( 0x20 + spu_status.i_position_wide ) << 8 )
677 i_id = ( ( 0x20 + spu_status.i_position_43 ) << 8 )
680 p_es = input_AddES( p_input,
681 p_input->stream.p_selected_program,
683 p_es->i_stream_id = 0xbd;
684 p_es->i_type = DVD_SPU_ES;
685 p_es->i_cat = SPU_ES;
686 strcpy( p_es->psz_desc, DecodeLanguage( hton16(
687 vts.manager_inf.p_spu_attr[i-1].i_lang_code ) ) );
691 if( p_main->b_audio )
693 /* For audio: first one if none or a not existing one specified */
694 i_audio = main_GetIntVariable( INPUT_CHANNEL_VAR, 1 );
695 if( i_audio < 0 || i_audio > i_audio_nb )
697 main_PutIntVariable( INPUT_CHANNEL_VAR, 1 );
700 if( i_audio > 0 && i_audio_nb > 0 )
702 if( main_GetIntVariable( AOUT_SPDIF_VAR, 0 ) ||
703 ( main_GetIntVariable( INPUT_AUDIO_VAR, 0 ) ==
707 while( ( p_input->stream.pp_es[i_ac3]->i_type !=
708 AC3_AUDIO_ES ) && ( i_ac3 <=
709 vts.manager_inf.i_audio_nb ) )
713 if( p_input->stream.pp_es[i_ac3]->i_type == AC3_AUDIO_ES )
715 input_SelectES( p_input,
716 p_input->stream.pp_es[i_ac3] );
721 input_SelectES( p_input,
722 p_input->stream.pp_es[i_audio] );
727 if( p_main->b_video )
729 /* for spu, default is none */
730 i_spu = main_GetIntVariable( INPUT_SUBTITLE_VAR, 0 );
731 if( i_spu < 0 || i_spu > i_spu_nb )
733 main_PutIntVariable( INPUT_SUBTITLE_VAR, 0 );
736 if( i_spu > 0 && i_spu_nb > 0 )
738 i_spu += vts.manager_inf.i_audio_nb;
739 input_SelectES( p_input, p_input->stream.pp_es[i_spu] );
745 p_area = p_input->stream.p_selected_area;
754 if( p_area->i_part != p_dvd->i_chapter )
756 if( ( p_area->i_part > 0 ) &&
757 ( p_area->i_part <= p_area->i_part_nb ))
759 if( DVDChapterSelect( p_dvd, p_area->i_part ) < 0 )
761 intf_ErrMsg( "dvd error: can't set chapter in area" );
762 p_input->b_error = 1;
766 p_input->stream.p_selected_area->i_tell =
767 LB2OFF( p_dvd->i_start ) - p_area->i_start;
768 p_input->stream.p_selected_area->i_part = p_dvd->i_chapter;
770 intf_WarnMsg( 4, "dvd info: chapter %d start at: %lld",
771 p_area->i_part, p_area->i_tell );
776 p_dvd->i_chapter = 1;
781 p_dvd->p_ifo->vts.title_unit.p_title[p_dvd->i_title_id-1].title
782 if( p_area->i_angle != p_dvd->i_angle )
784 if( title.p_cell_play[p_dvd->i_prg_cell].i_category & 0xf000 )
786 if( ( p_area->i_angle - p_dvd->i_angle ) < 0 )
790 p_dvd->i_prg_cell += ( p_area->i_angle - p_dvd->i_angle );
791 p_dvd->i_angle = p_area->i_angle;
793 DVDFindSector( p_dvd );
794 p_dvd->i_cell += p_dvd->i_angle_cell;
798 p_dvd->i_angle = p_area->i_angle;
801 intf_WarnMsg( 3, "dvd info: angle %d selected", p_area->i_angle );
804 /* warn interface that something has changed */
805 p_input->stream.b_seekable = 1;
806 p_input->stream.b_changed = 1;
812 /*****************************************************************************
813 * DVDRead: reads data packets.
814 *****************************************************************************
815 * Returns -1 in case of error, 0 in case of EOF, otherwise the number of
817 *****************************************************************************/
818 static int DVDRead( input_thread_t * p_input,
819 data_packet_t ** pp_data )
821 thread_dvd_data_t * p_dvd;
822 struct iovec p_vec[DVD_BLOCK_READ_ONCE];
832 data_packet_t * p_data;
834 p_dvd = (thread_dvd_data_t *)p_input->p_plugin_data;
839 i_sector = p_dvd->i_title_start + p_dvd->i_sector;
840 i_block_once = p_dvd->i_end_sector - p_dvd->i_sector + 1;
843 /* Get the position of the next cell if we're at cell end */
844 if( i_block_once <= 0 )
849 p_dvd->i_angle_cell++;
851 /* Find cell index in adress map */
852 if( DVDFindSector( p_dvd ) < 0 )
854 intf_ErrMsg( "dvd error: can't find next cell" );
858 /* Position the fd pointer on the right address */
859 if( ( i_sector = dvdcss_seek( p_dvd->dvdhandle,
860 p_dvd->i_title_start + p_dvd->i_sector,
861 DVDCSS_SEEK_MPEG ) ) < 0 )
863 intf_ErrMsg( "dvd error: %s", dvdcss_error( p_dvd->dvdhandle ) );
867 /* update chapter : it will be easier when we have navigation
869 if( p_dvd->i_chapter < ( p_dvd->i_chapter_nb - 1 ) )
871 if( title.p_cell_play[p_dvd->i_prg_cell].i_category & 0xf000 )
873 i_angle = p_dvd->i_angle - 1;
879 if( title.chapter_map.pi_start_cell[p_dvd->i_chapter] <=
880 ( p_dvd->i_prg_cell - i_angle + 1 ) )
887 i_block_once = p_dvd->i_end_sector - p_dvd->i_sector + 1;
890 /* The number of blocks read is the max between the requested
891 * value and the leaving block in the cell */
892 if( i_block_once > p_dvd->i_block_once )
894 i_block_once = p_dvd->i_block_once;
897 intf_WarnMsg( 2, "Sector: 0x%x Read: %d Chapter: %d", p_dvd->i_sector, i_block_once, p_dvd->i_chapter );
901 *pp_data = p_data = input_BuffersToIO( p_input->p_method_data, p_vec,
902 DVD_BLOCK_READ_ONCE );
904 if ( p_data == NULL )
910 i_read_blocks = dvdcss_readv( p_dvd->dvdhandle, p_vec,
911 i_block_once, DVDCSS_READ_DECRYPT );
913 /* Update global position */
914 p_dvd->i_sector += i_read_blocks;
918 /* Read headers to compute payload length */
919 for( i_iovec = 0 ; i_iovec < i_read_blocks ; i_iovec++ )
921 data_packet_t * p_current = p_data;
924 while( i_pos < DVD_LB_SIZE )
926 pi_cur = (u8*)p_vec[i_iovec].iov_base + i_pos;
929 if( U32_AT( pi_cur ) != 0x1BA )
931 /* That's the case for all packets, except pack header. */
932 i_packet_size = U16_AT( pi_cur + 4 );
936 /* MPEG-2 Pack header. */
942 *pp_data = input_ShareBuffer( p_input->p_method_data,
948 p_data = p_data->p_next;
951 (*pp_data)->p_payload_start = (*pp_data)->p_demux_start =
952 (*pp_data)->p_demux_start + i_pos;
954 (*pp_data)->p_payload_end =
955 (*pp_data)->p_payload_start + i_packet_size + 6;
958 i_pos += i_packet_size + 6;
959 pp_data = &(*pp_data)->p_next;
963 p_input->pf_delete_packet( p_input->p_method_data, p_data );
966 vlc_mutex_lock( &p_input->stream.stream_lock );
968 p_input->stream.p_selected_area->i_tell =
969 LB2OFF( i_sector + i_read_blocks ) -
970 p_input->stream.p_selected_area->i_start;
973 /* We modify i_part only at end of chapter not to erase
974 * some modification from the interface */
975 p_input->stream.p_selected_area->i_part = p_dvd->i_chapter;
978 if( p_input->stream.p_selected_area->i_tell
979 >= p_input->stream.p_selected_area->i_size )
981 if( ( p_dvd->i_title + 1 ) >= p_input->stream.i_area_nb )
984 vlc_mutex_unlock( &p_input->stream.stream_lock );
989 intf_WarnMsg( 4, "dvd info: new title" );
991 DVDSetArea( p_input, p_input->stream.pp_areas[p_dvd->i_title] );
992 vlc_mutex_unlock( &p_input->stream.stream_lock );
996 vlc_mutex_unlock( &p_input->stream.stream_lock );
998 if( i_read_blocks != i_block_once )
1006 /*****************************************************************************
1007 * DVDRewind : reads a stream backward
1008 *****************************************************************************/
1009 static int DVDRewind( input_thread_t * p_input )
1014 /*****************************************************************************
1015 * DVDSeek : Goes to a given position on the stream.
1016 *****************************************************************************
1017 * This one is used by the input and translate chronological position from
1018 * input to logical position on the device.
1019 * The lock should be taken before calling this function.
1020 *****************************************************************************/
1021 static void DVDSeek( input_thread_t * p_input, off_t i_off )
1023 thread_dvd_data_t * p_dvd;
1030 p_dvd = ( thread_dvd_data_t * )p_input->p_plugin_data;
1032 /* we have to take care of offset of beginning of title */
1033 p_dvd->i_sector = OFF2LB(i_off + p_input->stream.p_selected_area->i_start)
1034 - p_dvd->i_title_start;
1039 /* parse vobu address map to find program cell */
1040 while( title.p_cell_play[i_prg_cell].i_end_sector < p_dvd->i_sector )
1045 p_dvd->i_prg_cell = i_prg_cell;
1047 if( DVDChooseAngle( p_dvd ) < 0 )
1049 p_input->b_error = 1;
1055 /* Find first title cell which is inside program cell */
1056 if( DVDFindCell( p_dvd ) < 0 )
1058 /* no following cell : we're at eof */
1059 intf_ErrMsg( "dvd error: cell seeking failed" );
1060 p_input->b_error = 1;
1064 i_cell = p_dvd->i_cell;
1066 #define cell p_dvd->p_ifo->vts.cell_inf.p_cell_map[i_cell]
1067 /* parse cell address map to find title cell containing sector */
1068 while( cell.i_end_sector < p_dvd->i_sector )
1073 p_dvd->i_cell = i_cell;
1075 /* if we're inside a multi-angle zone, we have to choose i_sector
1076 * in the current angle ; we can't do it all the time since cells
1077 * can be very wide out of such zones */
1078 if( title.p_cell_play[p_dvd->i_prg_cell].i_category & 0xf000 )
1080 p_dvd->i_sector = MAX(
1081 cell.i_start_sector,
1082 title.p_cell_play[p_dvd->i_prg_cell].i_start_sector );
1085 p_dvd->i_end_sector = MIN(
1087 title.p_cell_play[p_dvd->i_prg_cell].i_end_sector );
1089 /* update chapter */
1090 if( title.p_cell_play[p_dvd->i_prg_cell].i_category & 0xf000 )
1092 i_angle = p_dvd->i_angle - 1;
1098 if( p_dvd->i_chapter_nb > 1 )
1100 while( ( title.chapter_map.pi_start_cell[i_chapter] <=
1101 ( p_dvd->i_prg_cell - i_angle + 1 ) ) &&
1102 ( i_chapter < ( p_dvd->i_chapter_nb - 1 ) ) )
1112 p_dvd->i_chapter = i_chapter;
1113 p_input->stream.p_selected_area->i_part = p_dvd->i_chapter;
1115 if( ( i_block = dvdcss_seek( p_dvd->dvdhandle,
1116 p_dvd->i_title_start + p_dvd->i_sector,
1117 DVDCSS_SEEK_MPEG ) ) < 0 )
1119 intf_ErrMsg( "dvd error: %s", dvdcss_error( p_dvd->dvdhandle ) );
1120 p_input->b_error = 1;
1124 p_input->stream.p_selected_area->i_tell =
1125 LB2OFF ( i_block ) - p_input->stream.p_selected_area->i_start;
1127 intf_WarnMsg( 7, "Program Cell: %d Cell: %d Chapter: %d",
1128 p_dvd->i_prg_cell, p_dvd->i_cell, p_dvd->i_chapter );
1134 #define cell p_dvd->p_ifo->vts.cell_inf
1136 /*****************************************************************************
1137 * DVDFindCell: adjust the title cell index with the program cell
1138 *****************************************************************************/
1139 static int DVDFindCell( thread_dvd_data_t * p_dvd )
1144 i_cell = p_dvd->i_cell;
1145 i_index = p_dvd->i_prg_cell;
1147 if( i_cell >= cell.i_cell_nb )
1152 while( ( ( title.p_cell_pos[i_index].i_vob_id !=
1153 cell.p_cell_map[i_cell].i_vob_id ) ||
1154 ( title.p_cell_pos[i_index].i_cell_id !=
1155 cell.p_cell_map[i_cell].i_cell_id ) ) &&
1156 ( i_cell < cell.i_cell_nb - 1 ) )
1162 intf_WarnMsg( 12, "FindCell: i_cell %d i_index %d found %d nb %d",
1169 p_dvd->i_cell = i_cell;
1176 /*****************************************************************************
1177 * DVDFindSector: find cell index in adress map from index in
1178 * information table program map and give corresponding sectors.
1179 *****************************************************************************/
1180 static int DVDFindSector( thread_dvd_data_t * p_dvd )
1183 if( p_dvd->i_sector > title.p_cell_play[p_dvd->i_prg_cell].i_end_sector )
1185 p_dvd->i_prg_cell++;
1187 if( DVDChooseAngle( p_dvd ) < 0 )
1193 if( DVDFindCell( p_dvd ) < 0 )
1195 intf_ErrMsg( "dvd error: can't find sector" );
1199 /* Find start and end sectors of new cell */
1201 p_dvd->i_sector = MAX(
1202 p_dvd->p_ifo->vts.cell_inf.p_cell_map[p_dvd->i_cell].i_start_sector,
1203 title.p_cell_play[p_dvd->i_prg_cell].i_start_sector );
1204 p_dvd->i_end_sector = MIN(
1205 p_dvd->p_ifo->vts.cell_inf.p_cell_map[p_dvd->i_cell].i_end_sector,
1206 title.p_cell_play[p_dvd->i_prg_cell].i_end_sector );
1208 p_dvd->i_sector = title.p_cell_play[p_dvd->i_prg_cell].i_start_sector;
1209 p_dvd->i_end_sector = title.p_cell_play[p_dvd->i_prg_cell].i_end_sector;
1213 intf_WarnMsg( 12, "cell: %d sector1: 0x%x end1: 0x%x\n"
1214 "index: %d sector2: 0x%x end2: 0x%x\n"
1215 "category: 0x%x ilvu end: 0x%x vobu start 0x%x",
1217 p_dvd->p_ifo->vts.cell_inf.p_cell_map[p_dvd->i_cell].i_start_sector,
1218 p_dvd->p_ifo->vts.cell_inf.p_cell_map[p_dvd->i_cell].i_end_sector,
1220 title.p_cell_play[p_dvd->i_prg_cell].i_start_sector,
1221 title.p_cell_play[p_dvd->i_prg_cell].i_end_sector,
1222 title.p_cell_play[p_dvd->i_prg_cell].i_category,
1223 title.p_cell_play[p_dvd->i_prg_cell].i_first_ilvu_vobu_esector,
1224 title.p_cell_play[p_dvd->i_prg_cell].i_last_vobu_start_sector );
1230 /*****************************************************************************
1231 * DVDChapterSelect: find the cell corresponding to requested chapter
1232 *****************************************************************************/
1233 static int DVDChapterSelect( thread_dvd_data_t * p_dvd, int i_chapter )
1236 /* Find cell index in Program chain for current chapter */
1237 p_dvd->i_prg_cell = title.chapter_map.pi_start_cell[i_chapter-1] - 1;
1239 p_dvd->i_sector = 0;
1241 DVDChooseAngle( p_dvd );
1243 /* Search for cell_index in cell adress_table and initialize
1245 if( DVDFindSector( p_dvd ) < 0 )
1247 intf_ErrMsg( "dvd error: can't select chapter" );
1251 /* start is : beginning of vts vobs + offset to vob x */
1252 p_dvd->i_start = p_dvd->i_title_start + p_dvd->i_sector;
1254 /* Position the fd pointer on the right address */
1255 if( ( p_dvd->i_start = dvdcss_seek( p_dvd->dvdhandle,
1257 DVDCSS_SEEK_MPEG ) ) < 0 )
1259 intf_ErrMsg( "dvd error: %s", dvdcss_error( p_dvd->dvdhandle ) );
1263 p_dvd->i_chapter = i_chapter;
1267 /*****************************************************************************
1268 * DVDChooseAngle: select the cell corresponding to the selected angle
1269 *****************************************************************************/
1270 static int DVDChooseAngle( thread_dvd_data_t * p_dvd )
1272 /* basic handling of angles */
1273 switch( ( ( title.p_cell_play[p_dvd->i_prg_cell].i_category & 0xf000 )
1276 /* we enter a muli-angle section */
1278 p_dvd->i_prg_cell += p_dvd->i_angle - 1;
1279 p_dvd->i_angle_cell = 0;
1281 /* we exit a multi-angle section */
1284 p_dvd->i_prg_cell += p_dvd->i_angle_nb - p_dvd->i_angle;