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.120 2002/02/19 00:50:19 sam 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 ( struct input_thread_s * );
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 input.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( input_thread_t * p_input )
155 char * psz_name = p_input->p_source;
157 if( ( strlen(psz_name) > 4 ) && !strncasecmp( psz_name, "dvd:", 4 ) )
159 /* If the user specified "dvd:" then it's probably a DVD */
166 /*****************************************************************************
167 * DVDInit: initializes DVD structures
168 *****************************************************************************/
169 static void DVDInit( 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( "dvd error: out of memory" );
181 p_input->b_error = 1;
185 p_input->p_plugin_data = (void *)p_dvd;
187 if( (p_input->p_method_data = input_BuffersInit()) == NULL )
189 p_input->b_error = 1;
193 p_dvd->dvdhandle = (dvdcss_handle) p_input->p_handle;
195 if( dvdcss_seek( p_dvd->dvdhandle, 0, DVDCSS_NOFLAGS ) < 0 )
197 intf_ErrMsg( "dvd error: %s", dvdcss_error( p_dvd->dvdhandle ) );
198 input_BuffersEnd( p_input->p_method_data );
199 p_input->b_error = 1;
203 /* We read DVD_BLOCK_READ_ONCE in each loop */
204 p_dvd->i_block_once = DVD_BLOCK_READ_ONCE;
206 /* Ifo allocation & initialisation */
207 if( IfoCreate( p_dvd ) < 0 )
209 intf_ErrMsg( "dvd error: allcation error in ifo" );
211 input_BuffersEnd( p_input->p_method_data );
212 p_input->b_error = 1;
216 if( IfoInit( p_dvd->p_ifo ) < 0 )
218 intf_ErrMsg( "dvd error: fatal failure in ifo" );
219 IfoDestroy( p_dvd->p_ifo );
221 input_BuffersEnd( p_input->p_method_data );
222 p_input->b_error = 1;
226 /* Set stream and area data */
227 vlc_mutex_lock( &p_input->stream.stream_lock );
229 /* Initialize ES structures */
230 input_InitStream( p_input, sizeof( stream_ps_data_t ) );
232 #define title_inf p_dvd->p_ifo->vmg.title_inf
233 intf_WarnMsg( 2, "dvd info: number of titles: %d", title_inf.i_title_nb );
235 #define area p_input->stream.pp_areas
236 /* We start from 1 here since the default area 0
237 * is reserved for video_ts.vob */
238 for( i = 1 ; i <= title_inf.i_title_nb ; i++ )
240 input_AddArea( p_input );
242 /* Titles are Program Chains */
245 /* Absolute start offset and size
246 * We can only set that with vts ifo, so we do it during the
247 * first call to DVDSetArea */
248 area[i]->i_start = 0;
251 /* Number of chapters */
252 area[i]->i_part_nb = title_inf.p_attr[i-1].i_chapter_nb;
255 /* Number of angles */
256 area[i]->i_angle_nb = 0;
257 area[i]->i_angle = 1;
259 /* Offset to vts_i_0.ifo */
260 area[i]->i_plugin_data = p_dvd->p_ifo->i_start +
261 title_inf.p_attr[i-1].i_start_sector;
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 > title_inf.i_title_nb )
274 /* Get requested chapter - if none defaults to first one */
275 i_chapter = main_GetIntVariable( INPUT_CHAPTER_VAR, 1 );
281 p_area = p_input->stream.pp_areas[i_title];
282 p_area->i_part = i_chapter;
284 /* set title, chapter, audio and subpic */
285 DVDSetArea( p_input, p_area );
287 vlc_mutex_unlock( &p_input->stream.stream_lock );
292 /*****************************************************************************
294 *****************************************************************************/
295 static void DVDOpen( struct input_thread_s *p_input )
297 dvdcss_handle dvdhandle;
301 vlc_mutex_lock( &p_input->stream.stream_lock );
303 p_input->stream.i_method = INPUT_METHOD_DVD;
305 /* If we are here we can control the pace... */
306 p_input->stream.b_pace_control = 1;
308 p_input->stream.b_seekable = 1;
309 p_input->stream.p_selected_area->i_size = 0;
311 p_input->stream.p_selected_area->i_tell = 0;
313 vlc_mutex_unlock( &p_input->stream.stream_lock );
315 /* Parse input string : dvd:device[@rawdevice] */
316 if( strlen( p_input->p_source ) > 4
317 && !strncasecmp( p_input->p_source, "dvd:", 4 ) )
319 psz_parser = psz_device = p_input->p_source + 4;
323 psz_parser = psz_device = p_input->p_source;
326 while( *psz_parser && *psz_parser != '@' )
331 if( *psz_parser == '@' )
333 /* Found raw device */
337 main_PutPszVariable( "DVDCSS_RAW_DEVICE", psz_parser );
340 intf_WarnMsg( 2, "input: dvd=%s raw=%s", psz_device, psz_parser );
342 dvdhandle = dvdcss_open( psz_device );
344 if( dvdhandle == NULL )
346 intf_ErrMsg( "dvd error: dvdcss can't open device" );
347 p_input->b_error = 1;
351 p_input->p_handle = (void *) dvdhandle;
354 /*****************************************************************************
355 * DVDClose: close dvd
356 *****************************************************************************/
357 static void DVDClose( struct input_thread_s *p_input )
359 /* Clean up libdvdcss */
360 dvdcss_close( (dvdcss_handle) p_input->p_handle );
363 /*****************************************************************************
364 * DVDEnd: frees unused data
365 *****************************************************************************/
366 static void DVDEnd( input_thread_t * p_input )
368 thread_dvd_data_t * p_dvd;
370 p_dvd = (thread_dvd_data_t*)p_input->p_plugin_data;
372 IfoDestroy( p_dvd->p_ifo );
376 input_BuffersEnd( p_input->p_method_data );
379 /*****************************************************************************
380 * DVDSetProgram: Does nothing, a DVD is mono-program
381 *****************************************************************************/
382 static int DVDSetProgram( input_thread_t * p_input,
383 pgrm_descriptor_t * p_program )
388 /*****************************************************************************
389 * DVDSetArea: initialize input data for title x, chapter y.
390 * It should be called for each user navigation request.
391 *****************************************************************************
392 * Take care that i_title starts from 0 (vmg) and i_chapter start from 1.
393 * Note that you have to take the lock before entering here.
394 *****************************************************************************/
395 static int DVDSetArea( input_thread_t * p_input, input_area_t * p_area )
397 thread_dvd_data_t * p_dvd;
398 es_descriptor_t * p_es;
407 p_dvd = (thread_dvd_data_t*)p_input->p_plugin_data;
409 /* we can't use the interface slider until initilization is complete */
410 p_input->stream.b_seekable = 0;
412 if( p_area != p_input->stream.p_selected_area )
414 /* Reset the Chapter position of the old title */
415 p_input->stream.p_selected_area->i_part = 0;
418 * We have to load all title information
420 /* Change the default area */
421 p_input->stream.p_selected_area =
422 p_input->stream.pp_areas[p_area->i_id];
424 /* title number: it is not vts nb!,
425 * it is what appears in the interface list */
426 p_dvd->i_title = p_area->i_id;
427 p_dvd->p_ifo->i_title = p_dvd->i_title;
429 /* set number of chapters of current title */
430 p_dvd->i_chapter_nb = p_area->i_part_nb;
433 if( IfoTitleSet( p_dvd->p_ifo ) < 0 )
435 intf_ErrMsg( "dvd error: fatal error in vts ifo" );
437 p_input->b_error = 1;
441 #define vmg p_dvd->p_ifo->vmg
442 #define vts p_dvd->p_ifo->vts
443 /* title position inside the selected vts */
444 i_vts_title = vmg.title_inf.p_attr[p_dvd->i_title-1].i_title_num;
446 vts.title_inf.p_title_start[i_vts_title-1].i_title_id;
448 intf_WarnMsg( 3, "dvd: title %d vts_title %d pgc %d",
449 p_dvd->i_title, i_vts_title, p_dvd->i_title_id );
454 p_dvd->i_angle_nb = vmg.title_inf.p_attr[p_dvd->i_title-1].i_angle_nb;
455 p_dvd->i_angle = main_GetIntVariable( INPUT_ANGLE_VAR, 1 );
456 if( ( p_dvd->i_angle <= 0 ) || p_dvd->i_angle > p_dvd->i_angle_nb )
462 * Set selected title start and size
465 /* title set offset XXX: convert to block values */
466 p_dvd->i_title_start =
467 vts.i_pos + vts.manager_inf.i_title_vob_start_sector;
469 /* last video cell */
471 p_dvd->i_prg_cell = -1 +
472 vts.title_unit.p_title[p_dvd->i_title_id-1].title.i_cell_nb;
474 if( DVDFindCell( p_dvd ) < 0 )
476 intf_ErrMsg( "dvd error: can't find title end" );
477 p_input->b_error = 1;
481 /* temporary hack to fix size in some dvds */
482 if( p_dvd->i_cell >= vts.cell_inf.i_cell_nb )
484 p_dvd->i_cell = vts.cell_inf.i_cell_nb - 1;
488 p_dvd->i_size = vts.cell_inf.p_cell_map[p_dvd->i_cell].i_end_sector;
490 if( DVDChapterSelect( p_dvd, 1 ) < 0 )
492 intf_ErrMsg( "dvd error: can't find first chapter" );
493 p_input->b_error = 1;
497 /* Force libdvdcss to check its title key.
498 * It is only useful for title cracking method. Methods using the
499 * decrypted disc key are fast enough to check the key at each seek */
501 if( dvdcss_seek( p_dvd->dvdhandle, p_dvd->i_start,
502 DVDCSS_SEEK_KEY ) < 0 )
504 intf_ErrMsg( "dvd error: %s", dvdcss_error( p_dvd->dvdhandle ) );
508 p_dvd->i_size -= p_dvd->i_sector + 1;
510 IfoPrintTitle( p_dvd );
512 /* Area definition */
513 p_input->stream.p_selected_area->i_start = LB2OFF( p_dvd->i_start );
514 p_input->stream.p_selected_area->i_size = LB2OFF( p_dvd->i_size );
515 p_input->stream.p_selected_area->i_angle_nb = p_dvd->i_angle_nb;
516 p_input->stream.p_selected_area->i_angle = p_dvd->i_angle;
519 /* start at the beginning of the title */
520 /* FIXME: create a conf option to select whether to restart
522 p_input->stream.p_selected_area->i_tell = 0;
523 p_input->stream.p_selected_area->i_part = 1;
527 * Destroy obsolete ES by reinitializing program 0
528 * and find all ES in title with ifo data
530 if( p_input->stream.pp_programs != NULL )
532 /* We don't use input_EndStream here since
533 * we keep area structures */
535 for( i = 0 ; i < p_input->stream.i_selected_es_number ; i++ )
537 input_UnselectES( p_input, p_input->stream.pp_selected_es[i] );
540 free( p_input->stream.pp_selected_es );
541 input_DelProgram( p_input, p_input->stream.p_selected_program );
543 p_input->stream.pp_selected_es = NULL;
544 p_input->stream.i_selected_es_number = 0;
547 input_AddProgram( p_input, 0, sizeof( stream_ps_data_t ) );
548 p_input->stream.p_selected_program = p_input->stream.pp_programs[0];
550 /* No PSM to read in DVD mode, we already have all information */
551 p_input->stream.p_selected_program->b_is_ok = 1;
555 /* ES 0 -> video MPEG2 */
556 IfoPrintVideo( p_dvd );
558 p_es = input_AddES( p_input, p_input->stream.p_selected_program,
560 p_es->i_stream_id = 0xe0;
561 p_es->i_type = MPEG2_VIDEO_ES;
562 p_es->i_cat = VIDEO_ES;
563 if( p_main->b_video )
565 input_SelectES( p_input, p_es );
568 #define audio_status \
569 vts.title_unit.p_title[p_dvd->i_title_id-1].title.pi_audio_status[i-1]
570 /* Audio ES, in the order they appear in .ifo */
571 for( i = 1 ; i <= vts.manager_inf.i_audio_nb ; i++ )
573 IfoPrintAudio( p_dvd, i );
575 /* audio channel is active if first byte is 0x80 */
576 if( audio_status.i_available )
580 switch( vts.manager_inf.p_audio_attr[i-1].i_coding_mode )
583 i_id = ( ( 0x80 + audio_status.i_position ) << 8 ) | 0xbd;
584 p_es = input_AddES( p_input,
585 p_input->stream.p_selected_program, i_id, 0 );
586 p_es->i_stream_id = 0xbd;
587 p_es->i_type = AC3_AUDIO_ES;
589 p_es->i_cat = AUDIO_ES;
590 strcpy( p_es->psz_desc, DecodeLanguage( hton16(
591 vts.manager_inf.p_audio_attr[i-1].i_lang_code ) ) );
592 strcat( p_es->psz_desc, " (ac3)" );
596 case 0x03: /* MPEG audio */
597 i_id = 0xc0 + audio_status.i_position;
598 p_es = input_AddES( p_input,
599 p_input->stream.p_selected_program, i_id
601 p_es->i_stream_id = i_id;
602 p_es->i_type = MPEG2_AUDIO_ES;
604 p_es->i_cat = AUDIO_ES;
605 strcpy( p_es->psz_desc, DecodeLanguage( hton16(
606 vts.manager_inf.p_audio_attr[i-1].i_lang_code ) ) );
607 strcat( p_es->psz_desc, " (mpeg)" );
610 case 0x04: /* LPCM */
612 i_id = ( ( 0xa0 + audio_status.i_position ) << 8 ) | 0xbd;
613 p_es = input_AddES( p_input,
614 p_input->stream.p_selected_program,
616 p_es->i_stream_id = 0xbd;
617 p_es->i_type = LPCM_AUDIO_ES;
619 p_es->i_cat = AUDIO_ES;
620 strcpy( p_es->psz_desc, DecodeLanguage( hton16(
621 vts.manager_inf.p_audio_attr[i-1].i_lang_code ) ) );
622 strcat( p_es->psz_desc, " (lpcm)" );
626 i_id = ( ( 0x88 + audio_status.i_position ) << 8 ) | 0xbd;
627 intf_ErrMsg( "dvd warning: DTS audio not handled yet"
632 intf_ErrMsg( "dvd warning: unknown audio type %.2x",
633 vts.manager_inf.p_audio_attr[i-1].i_coding_mode );
639 vts.title_unit.p_title[p_dvd->i_title_id-1].title.pi_spu_status[i-1]
643 for( i = 1 ; i <= vts.manager_inf.i_spu_nb; i++ )
645 IfoPrintSpu( p_dvd, i );
647 if( spu_status.i_available )
651 /* there are several streams for one spu */
652 if( vts.manager_inf.video_attr.i_ratio )
655 switch( vts.manager_inf.video_attr.i_perm_displ )
658 i_id = ( ( 0x20 + spu_status.i_position_pan ) << 8 )
662 i_id = ( ( 0x20 + spu_status.i_position_letter ) << 8 )
666 i_id = ( ( 0x20 + spu_status.i_position_wide ) << 8 )
674 i_id = ( ( 0x20 + spu_status.i_position_43 ) << 8 )
677 p_es = input_AddES( p_input,
678 p_input->stream.p_selected_program,
680 p_es->i_stream_id = 0xbd;
681 p_es->i_type = DVD_SPU_ES;
682 p_es->i_cat = SPU_ES;
683 strcpy( p_es->psz_desc, DecodeLanguage( hton16(
684 vts.manager_inf.p_spu_attr[i-1].i_lang_code ) ) );
688 if( p_main->b_audio )
690 /* For audio: first one if none or a not existing one specified */
691 i_audio = main_GetIntVariable( INPUT_CHANNEL_VAR, 1 );
692 if( i_audio < 0 || i_audio > i_audio_nb )
694 main_PutIntVariable( INPUT_CHANNEL_VAR, 1 );
697 if( i_audio > 0 && i_audio_nb > 0 )
699 if( main_GetIntVariable( AOUT_SPDIF_VAR, 0 ) ||
700 ( main_GetIntVariable( INPUT_AUDIO_VAR, 0 ) ==
704 while( ( p_input->stream.pp_es[i_ac3]->i_type !=
705 AC3_AUDIO_ES ) && ( i_ac3 <=
706 vts.manager_inf.i_audio_nb ) )
710 if( p_input->stream.pp_es[i_ac3]->i_type == AC3_AUDIO_ES )
712 input_SelectES( p_input,
713 p_input->stream.pp_es[i_ac3] );
718 input_SelectES( p_input,
719 p_input->stream.pp_es[i_audio] );
724 if( p_main->b_video )
726 /* for spu, default is none */
727 i_spu = main_GetIntVariable( INPUT_SUBTITLE_VAR, 0 );
728 if( i_spu < 0 || i_spu > i_spu_nb )
730 main_PutIntVariable( INPUT_SUBTITLE_VAR, 0 );
733 if( i_spu > 0 && i_spu_nb > 0 )
735 i_spu += vts.manager_inf.i_audio_nb;
736 input_SelectES( p_input, p_input->stream.pp_es[i_spu] );
742 p_area = p_input->stream.p_selected_area;
751 if( p_area->i_part != p_dvd->i_chapter )
753 if( ( p_area->i_part > 0 ) &&
754 ( p_area->i_part <= p_area->i_part_nb ))
756 if( DVDChapterSelect( p_dvd, p_area->i_part ) < 0 )
758 intf_ErrMsg( "dvd error: can't set chapter in area" );
759 p_input->b_error = 1;
763 p_input->stream.p_selected_area->i_tell =
764 LB2OFF( p_dvd->i_start ) - p_area->i_start;
765 p_input->stream.p_selected_area->i_part = p_dvd->i_chapter;
767 intf_WarnMsg( 4, "dvd info: chapter %d start at: %lld",
768 p_area->i_part, p_area->i_tell );
773 p_dvd->i_chapter = 1;
778 p_dvd->p_ifo->vts.title_unit.p_title[p_dvd->i_title_id-1].title
779 if( p_area->i_angle != p_dvd->i_angle )
781 if( title.p_cell_play[p_dvd->i_prg_cell].i_category & 0xf000 )
783 if( ( p_area->i_angle - p_dvd->i_angle ) < 0 )
787 p_dvd->i_prg_cell += ( p_area->i_angle - p_dvd->i_angle );
788 p_dvd->i_angle = p_area->i_angle;
790 DVDFindSector( p_dvd );
791 p_dvd->i_cell += p_dvd->i_angle_cell;
795 p_dvd->i_angle = p_area->i_angle;
798 intf_WarnMsg( 3, "dvd info: angle %d selected", p_area->i_angle );
801 /* warn interface that something has changed */
802 p_input->stream.b_seekable = 1;
803 p_input->stream.b_changed = 1;
809 /*****************************************************************************
810 * DVDRead: reads data packets.
811 *****************************************************************************
812 * Returns -1 in case of error, 0 in case of EOF, otherwise the number of
814 *****************************************************************************/
815 static int DVDRead( input_thread_t * p_input,
816 data_packet_t ** pp_data )
818 thread_dvd_data_t * p_dvd;
819 struct iovec p_vec[DVD_BLOCK_READ_ONCE];
829 data_packet_t * p_data;
831 p_dvd = (thread_dvd_data_t *)p_input->p_plugin_data;
836 i_sector = p_dvd->i_title_start + p_dvd->i_sector;
837 i_block_once = p_dvd->i_end_sector - p_dvd->i_sector + 1;
840 /* Get the position of the next cell if we're at cell end */
841 if( i_block_once <= 0 )
846 p_dvd->i_angle_cell++;
848 /* Find cell index in adress map */
849 if( DVDFindSector( p_dvd ) < 0 )
851 intf_ErrMsg( "dvd error: can't find next cell" );
855 /* Position the fd pointer on the right address */
856 if( ( i_sector = dvdcss_seek( p_dvd->dvdhandle,
857 p_dvd->i_title_start + p_dvd->i_sector,
858 DVDCSS_SEEK_MPEG ) ) < 0 )
860 intf_ErrMsg( "dvd error: %s", dvdcss_error( p_dvd->dvdhandle ) );
864 /* update chapter : it will be easier when we have navigation
866 if( p_dvd->i_chapter < ( p_dvd->i_chapter_nb - 1 ) )
868 if( title.p_cell_play[p_dvd->i_prg_cell].i_category & 0xf000 )
870 i_angle = p_dvd->i_angle - 1;
876 if( title.chapter_map.pi_start_cell[p_dvd->i_chapter] <=
877 ( p_dvd->i_prg_cell - i_angle + 1 ) )
884 i_block_once = p_dvd->i_end_sector - p_dvd->i_sector + 1;
887 /* The number of blocks read is the max between the requested
888 * value and the leaving block in the cell */
889 if( i_block_once > p_dvd->i_block_once )
891 i_block_once = p_dvd->i_block_once;
894 intf_WarnMsg( 2, "Sector: 0x%x Read: %d Chapter: %d", p_dvd->i_sector, i_block_once, p_dvd->i_chapter );
898 *pp_data = p_data = input_BuffersToIO( p_input->p_method_data, p_vec,
899 DVD_BLOCK_READ_ONCE );
901 if ( p_data == NULL )
907 i_read_blocks = dvdcss_readv( p_dvd->dvdhandle, p_vec,
908 i_block_once, DVDCSS_READ_DECRYPT );
910 /* Update global position */
911 p_dvd->i_sector += i_read_blocks;
915 /* Read headers to compute payload length */
916 for( i_iovec = 0 ; i_iovec < i_read_blocks ; i_iovec++ )
918 data_packet_t * p_current = p_data;
921 while( i_pos < DVD_LB_SIZE )
923 pi_cur = (u8*)p_vec[i_iovec].iov_base + i_pos;
926 if( U32_AT( pi_cur ) != 0x1BA )
928 /* That's the case for all packets, except pack header. */
929 i_packet_size = U16_AT( pi_cur + 4 );
933 /* MPEG-2 Pack header. */
939 *pp_data = input_ShareBuffer( p_input->p_method_data,
945 p_data = p_data->p_next;
948 (*pp_data)->p_payload_start = (*pp_data)->p_demux_start =
949 (*pp_data)->p_demux_start + i_pos;
951 (*pp_data)->p_payload_end =
952 (*pp_data)->p_payload_start + i_packet_size + 6;
955 i_pos += i_packet_size + 6;
956 pp_data = &(*pp_data)->p_next;
960 p_input->pf_delete_packet( p_input->p_method_data, p_data );
963 vlc_mutex_lock( &p_input->stream.stream_lock );
965 p_input->stream.p_selected_area->i_tell =
966 LB2OFF( i_sector + i_read_blocks ) -
967 p_input->stream.p_selected_area->i_start;
970 /* We modify i_part only at end of chapter not to erase
971 * some modification from the interface */
972 p_input->stream.p_selected_area->i_part = p_dvd->i_chapter;
975 if( p_input->stream.p_selected_area->i_tell
976 >= p_input->stream.p_selected_area->i_size )
978 if( ( p_dvd->i_title + 1 ) >= p_input->stream.i_area_nb )
981 vlc_mutex_unlock( &p_input->stream.stream_lock );
986 intf_WarnMsg( 4, "dvd info: new title" );
988 DVDSetArea( p_input, p_input->stream.pp_areas[p_dvd->i_title] );
989 vlc_mutex_unlock( &p_input->stream.stream_lock );
993 vlc_mutex_unlock( &p_input->stream.stream_lock );
995 if( i_read_blocks != i_block_once )
1003 /*****************************************************************************
1004 * DVDRewind : reads a stream backward
1005 *****************************************************************************/
1006 static int DVDRewind( input_thread_t * p_input )
1011 /*****************************************************************************
1012 * DVDSeek : Goes to a given position on the stream.
1013 *****************************************************************************
1014 * This one is used by the input and translate chronological position from
1015 * input to logical position on the device.
1016 * The lock should be taken before calling this function.
1017 *****************************************************************************/
1018 static void DVDSeek( input_thread_t * p_input, off_t i_off )
1020 thread_dvd_data_t * p_dvd;
1027 p_dvd = ( thread_dvd_data_t * )p_input->p_plugin_data;
1029 /* we have to take care of offset of beginning of title */
1030 p_dvd->i_sector = OFF2LB(i_off + p_input->stream.p_selected_area->i_start)
1031 - p_dvd->i_title_start;
1036 /* parse vobu address map to find program cell */
1037 while( title.p_cell_play[i_prg_cell].i_end_sector < p_dvd->i_sector )
1042 p_dvd->i_prg_cell = i_prg_cell;
1044 if( DVDChooseAngle( p_dvd ) < 0 )
1046 p_input->b_error = 1;
1052 /* Find first title cell which is inside program cell */
1053 if( DVDFindCell( p_dvd ) < 0 )
1055 /* no following cell : we're at eof */
1056 intf_ErrMsg( "dvd error: cell seeking failed" );
1057 p_input->b_error = 1;
1061 i_cell = p_dvd->i_cell;
1063 #define cell p_dvd->p_ifo->vts.cell_inf.p_cell_map[i_cell]
1064 /* parse cell address map to find title cell containing sector */
1065 while( cell.i_end_sector < p_dvd->i_sector )
1070 p_dvd->i_cell = i_cell;
1072 /* if we're inside a multi-angle zone, we have to choose i_sector
1073 * in the current angle ; we can't do it all the time since cells
1074 * can be very wide out of such zones */
1075 if( title.p_cell_play[p_dvd->i_prg_cell].i_category & 0xf000 )
1077 p_dvd->i_sector = MAX(
1078 cell.i_start_sector,
1079 title.p_cell_play[p_dvd->i_prg_cell].i_start_sector );
1082 p_dvd->i_end_sector = MIN(
1084 title.p_cell_play[p_dvd->i_prg_cell].i_end_sector );
1086 /* update chapter */
1087 if( title.p_cell_play[p_dvd->i_prg_cell].i_category & 0xf000 )
1089 i_angle = p_dvd->i_angle - 1;
1095 if( p_dvd->i_chapter_nb > 1 )
1097 while( ( title.chapter_map.pi_start_cell[i_chapter] <=
1098 ( p_dvd->i_prg_cell - i_angle + 1 ) ) &&
1099 ( i_chapter < ( p_dvd->i_chapter_nb - 1 ) ) )
1109 p_dvd->i_chapter = i_chapter;
1110 p_input->stream.p_selected_area->i_part = p_dvd->i_chapter;
1112 if( ( i_block = dvdcss_seek( p_dvd->dvdhandle,
1113 p_dvd->i_title_start + p_dvd->i_sector,
1114 DVDCSS_SEEK_MPEG ) ) < 0 )
1116 intf_ErrMsg( "dvd error: %s", dvdcss_error( p_dvd->dvdhandle ) );
1117 p_input->b_error = 1;
1121 p_input->stream.p_selected_area->i_tell =
1122 LB2OFF ( i_block ) - p_input->stream.p_selected_area->i_start;
1124 intf_WarnMsg( 7, "Program Cell: %d Cell: %d Chapter: %d",
1125 p_dvd->i_prg_cell, p_dvd->i_cell, p_dvd->i_chapter );
1131 #define cell p_dvd->p_ifo->vts.cell_inf
1133 /*****************************************************************************
1134 * DVDFindCell: adjust the title cell index with the program cell
1135 *****************************************************************************/
1136 static int DVDFindCell( thread_dvd_data_t * p_dvd )
1141 i_cell = p_dvd->i_cell;
1142 i_index = p_dvd->i_prg_cell;
1144 if( i_cell >= cell.i_cell_nb )
1149 while( ( ( title.p_cell_pos[i_index].i_vob_id !=
1150 cell.p_cell_map[i_cell].i_vob_id ) ||
1151 ( title.p_cell_pos[i_index].i_cell_id !=
1152 cell.p_cell_map[i_cell].i_cell_id ) ) &&
1153 ( i_cell < cell.i_cell_nb - 1 ) )
1159 intf_WarnMsg( 12, "FindCell: i_cell %d i_index %d found %d nb %d",
1166 p_dvd->i_cell = i_cell;
1173 /*****************************************************************************
1174 * DVDFindSector: find cell index in adress map from index in
1175 * information table program map and give corresponding sectors.
1176 *****************************************************************************/
1177 static int DVDFindSector( thread_dvd_data_t * p_dvd )
1180 if( p_dvd->i_sector > title.p_cell_play[p_dvd->i_prg_cell].i_end_sector )
1182 p_dvd->i_prg_cell++;
1184 if( DVDChooseAngle( p_dvd ) < 0 )
1190 if( DVDFindCell( p_dvd ) < 0 )
1192 intf_ErrMsg( "dvd error: can't find sector" );
1196 /* Find start and end sectors of new cell */
1198 p_dvd->i_sector = MAX(
1199 p_dvd->p_ifo->vts.cell_inf.p_cell_map[p_dvd->i_cell].i_start_sector,
1200 title.p_cell_play[p_dvd->i_prg_cell].i_start_sector );
1201 p_dvd->i_end_sector = MIN(
1202 p_dvd->p_ifo->vts.cell_inf.p_cell_map[p_dvd->i_cell].i_end_sector,
1203 title.p_cell_play[p_dvd->i_prg_cell].i_end_sector );
1205 p_dvd->i_sector = title.p_cell_play[p_dvd->i_prg_cell].i_start_sector;
1206 p_dvd->i_end_sector = title.p_cell_play[p_dvd->i_prg_cell].i_end_sector;
1210 intf_WarnMsg( 12, "cell: %d sector1: 0x%x end1: 0x%x\n"
1211 "index: %d sector2: 0x%x end2: 0x%x\n"
1212 "category: 0x%x ilvu end: 0x%x vobu start 0x%x",
1214 p_dvd->p_ifo->vts.cell_inf.p_cell_map[p_dvd->i_cell].i_start_sector,
1215 p_dvd->p_ifo->vts.cell_inf.p_cell_map[p_dvd->i_cell].i_end_sector,
1217 title.p_cell_play[p_dvd->i_prg_cell].i_start_sector,
1218 title.p_cell_play[p_dvd->i_prg_cell].i_end_sector,
1219 title.p_cell_play[p_dvd->i_prg_cell].i_category,
1220 title.p_cell_play[p_dvd->i_prg_cell].i_first_ilvu_vobu_esector,
1221 title.p_cell_play[p_dvd->i_prg_cell].i_last_vobu_start_sector );
1227 /*****************************************************************************
1228 * DVDChapterSelect: find the cell corresponding to requested chapter
1229 *****************************************************************************/
1230 static int DVDChapterSelect( thread_dvd_data_t * p_dvd, int i_chapter )
1233 /* Find cell index in Program chain for current chapter */
1234 p_dvd->i_prg_cell = title.chapter_map.pi_start_cell[i_chapter-1] - 1;
1236 p_dvd->i_sector = 0;
1238 DVDChooseAngle( p_dvd );
1240 /* Search for cell_index in cell adress_table and initialize
1242 if( DVDFindSector( p_dvd ) < 0 )
1244 intf_ErrMsg( "dvd error: can't select chapter" );
1248 /* start is : beginning of vts vobs + offset to vob x */
1249 p_dvd->i_start = p_dvd->i_title_start + p_dvd->i_sector;
1251 /* Position the fd pointer on the right address */
1252 if( ( p_dvd->i_start = dvdcss_seek( p_dvd->dvdhandle,
1254 DVDCSS_SEEK_MPEG ) ) < 0 )
1256 intf_ErrMsg( "dvd error: %s", dvdcss_error( p_dvd->dvdhandle ) );
1260 p_dvd->i_chapter = i_chapter;
1264 /*****************************************************************************
1265 * DVDChooseAngle: select the cell corresponding to the selected angle
1266 *****************************************************************************/
1267 static int DVDChooseAngle( thread_dvd_data_t * p_dvd )
1269 /* basic handling of angles */
1270 switch( ( ( title.p_cell_play[p_dvd->i_prg_cell].i_category & 0xf000 )
1273 /* we enter a muli-angle section */
1275 p_dvd->i_prg_cell += p_dvd->i_angle - 1;
1276 p_dvd->i_angle_cell = 0;
1278 /* we exit a multi-angle section */
1281 p_dvd->i_prg_cell += p_dvd->i_angle_nb - p_dvd->i_angle;