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 * -input_netlist used to read packets
8 * -libdvdcss for access and unscrambling
9 * -dvd_ifo for ifo parsing and analyse
10 * -dvd_udf to find files
11 *****************************************************************************
12 * Copyright (C) 1998-2001 VideoLAN
13 * $Id: input_dvd.c,v 1.92 2001/11/07 17:37:16 stef Exp $
15 * Author: Stéphane Borel <stef@via.ecp.fr>
17 * This program is free software; you can redistribute it and/or modify
18 * it under the terms of the GNU General Public License as published by
19 * the Free Software Foundation; either version 2 of the License, or
20 * (at your option) any later version.
22 * This program is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
27 * You should have received a copy of the GNU General Public License
28 * along with this program; if not, write to the Free Software
29 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
30 *****************************************************************************/
32 #define MODULE_NAME dvd
33 #include "modules_inner.h"
35 /*****************************************************************************
37 *****************************************************************************/
48 #include <sys/types.h>
52 #ifdef STRNCASECMP_IN_STRINGS_H
57 # include <io.h> /* read() */
59 # include <sys/uio.h> /* struct iovec */
63 # include "dummy_dvdcss.h"
65 # include <videolan/dvdcss.h>
76 # include "input_iovec.h"
83 #include "stream_control.h"
84 #include "input_ext-intf.h"
85 #include "input_ext-dec.h"
86 #include "input_ext-plugins.h"
88 #include "input_dvd.h"
89 #include "dvd_netlist.h"
91 #include "dvd_summary.h"
96 #include "modules_export.h"
98 /* how many blocks DVDRead will read in each loop */
99 #define DVD_BLOCK_READ_ONCE 64
100 #define DVD_DATA_READ_ONCE (4 * DVD_BLOCK_READ_ONCE)
102 /* Size of netlist */
103 #define DVD_NETLIST_SIZE 256
105 /*****************************************************************************
107 *****************************************************************************/
108 /* called from outside */
109 static int DVDProbe ( probedata_t *p_data );
110 static void DVDInit ( struct input_thread_s * );
111 static void DVDEnd ( struct input_thread_s * );
112 static void DVDOpen ( struct input_thread_s * );
113 static void DVDClose ( struct input_thread_s * );
114 static int DVDSetArea ( struct input_thread_s *, struct input_area_s * );
115 static int DVDRead ( struct input_thread_s *, data_packet_t ** );
116 static void DVDSeek ( struct input_thread_s *, off_t );
117 static int DVDRewind ( struct input_thread_s * );
119 /* called only inside */
120 static int DVDChooseAngle( thread_dvd_data_t * );
121 static int DVDFindCell( thread_dvd_data_t * );
122 static int DVDFindSector( thread_dvd_data_t * );
123 static int DVDChapterSelect( thread_dvd_data_t *, int );
125 /*****************************************************************************
126 * Functions exported as capabilities. They are declared as static so that
127 * we don't pollute the namespace too much.
128 *****************************************************************************/
129 void _M( input_getfunctions )( function_list_t * p_function_list )
131 #define input p_function_list->functions.input
132 p_function_list->pf_probe = DVDProbe;
133 input.pf_init = DVDInit;
134 input.pf_open = DVDOpen;
135 input.pf_close = DVDClose;
136 input.pf_end = DVDEnd;
137 input.pf_init_bit_stream = InitBitstream;
138 input.pf_read = DVDRead;
139 input.pf_set_area = DVDSetArea;
140 input.pf_demux = input_DemuxPS;
141 input.pf_new_packet = DVDNewPacket;
142 input.pf_new_pes = DVDNewPES;
143 input.pf_delete_packet = DVDDeletePacket;
144 input.pf_delete_pes = DVDDeletePES;
145 input.pf_rewind = DVDRewind;
146 input.pf_seek = DVDSeek;
151 * Data reading functions
154 /*****************************************************************************
155 * DVDProbe: verifies that the stream is a PS stream
156 *****************************************************************************/
157 static int DVDProbe( probedata_t *p_data )
159 input_thread_t * p_input = (input_thread_t *)p_data;
161 char * psz_name = p_input->p_source;
164 if( TestMethod( INPUT_METHOD_VAR, "dvd" ) )
169 if( ( strlen(psz_name) > 4 ) && !strncasecmp( psz_name, "dvd:", 4 ) )
171 /* If the user specified "dvd:" then it's probably a DVD */
179 /*****************************************************************************
180 * DVDInit: initializes DVD structures
181 *****************************************************************************/
182 static void DVDInit( input_thread_t * p_input )
184 thread_dvd_data_t * p_dvd;
185 input_area_t * p_area;
190 p_dvd = malloc( sizeof(thread_dvd_data_t) );
193 intf_ErrMsg( "dvd error: out of memory" );
194 p_input->b_error = 1;
198 p_input->p_plugin_data = (void *)p_dvd;
199 p_input->p_method_data = NULL;
201 p_dvd->dvdhandle = (dvdcss_handle) p_input->i_handle;
203 if( dvdcss_seek( p_dvd->dvdhandle, 0, DVDCSS_NOFLAGS ) < 0 )
205 intf_ErrMsg( "dvd error: %s", dvdcss_error( p_dvd->dvdhandle ) );
206 p_input->b_error = 1;
210 /* We read DVD_BLOCK_READ_ONCE in each loop, so the input will receive
211 * DVD_DATA_READ_ONCE at most */
212 p_dvd->i_block_once = DVD_BLOCK_READ_ONCE;
213 /* this value mustn't be modifed */
214 p_input->i_read_once = DVD_DATA_READ_ONCE;
216 /* Reading structures initialisation */
217 p_input->p_method_data =
218 DVDNetlistInit( DVD_NETLIST_SIZE, 2 * DVD_NETLIST_SIZE,
219 DVD_NETLIST_SIZE, DVD_LB_SIZE, p_dvd->i_block_once );
220 intf_WarnMsg( 2, "dvd info: netlist initialized" );
222 /* Ifo allocation & initialisation */
223 if( IfoCreate( p_dvd ) < 0 )
225 intf_ErrMsg( "dvd error: allcation error in ifo" );
226 dvdcss_close( p_dvd->dvdhandle );
228 p_input->b_error = 1;
232 if( IfoInit( p_dvd->p_ifo ) < 0 )
234 intf_ErrMsg( "dvd error: fatal failure in ifo" );
235 IfoDestroy( p_dvd->p_ifo );
236 dvdcss_close( p_dvd->dvdhandle );
238 p_input->b_error = 1;
242 /* Set stream and area data */
243 vlc_mutex_lock( &p_input->stream.stream_lock );
245 /* Initialize ES structures */
246 input_InitStream( p_input, sizeof( stream_ps_data_t ) );
248 #define title_inf p_dvd->p_ifo->vmg.title_inf
249 intf_WarnMsg( 2, "dvd info: number of titles: %d", title_inf.i_title_nb );
251 #define area p_input->stream.pp_areas
252 /* We start from 1 here since the default area 0
253 * is reserved for video_ts.vob */
254 for( i = 1 ; i <= title_inf.i_title_nb ; i++ )
256 input_AddArea( p_input );
258 /* Titles are Program Chains */
261 /* Absolute start offset and size
262 * We can only set that with vts ifo, so we do it during the
263 * first call to DVDSetArea */
264 area[i]->i_start = 0;
267 /* Number of chapters */
268 area[i]->i_part_nb = title_inf.p_attr[i-1].i_chapter_nb;
271 /* Number of angles */
272 area[i]->i_angle_nb = 0;
273 area[i]->i_angle = 1;
275 /* Offset to vts_i_0.ifo */
276 area[i]->i_plugin_data = p_dvd->p_ifo->i_start +
277 title_inf.p_attr[i-1].i_start_sector;
281 /* Get requested title - if none try the first title */
282 i_title = main_GetIntVariable( INPUT_TITLE_VAR, 1 );
283 if( i_title <= 0 || i_title > title_inf.i_title_nb )
290 /* Get requested chapter - if none defaults to first one */
291 i_chapter = main_GetIntVariable( INPUT_CHAPTER_VAR, 1 );
297 p_input->stream.pp_areas[i_title]->i_part = i_chapter;
299 p_area = p_input->stream.pp_areas[i_title];
301 /* set title, chapter, audio and subpic */
302 DVDSetArea( p_input, p_area );
304 vlc_mutex_unlock( &p_input->stream.stream_lock );
309 /*****************************************************************************
311 *****************************************************************************/
312 static void DVDOpen( struct input_thread_s *p_input )
314 dvdcss_handle dvdhandle;
317 vlc_mutex_lock( &p_input->stream.stream_lock );
319 /* If we are here we can control the pace... */
320 p_input->stream.b_pace_control = 1;
322 p_input->stream.b_seekable = 1;
323 p_input->stream.p_selected_area->i_size = 0;
325 p_input->stream.p_selected_area->i_tell = 0;
327 vlc_mutex_unlock( &p_input->stream.stream_lock );
329 /* Error messages from libdvdcss only in verbose mode */
330 i_flags = p_main->i_warning_level ? DVDCSS_NOFLAGS : DVDCSS_INIT_QUIET;
332 /* Debug message from libdvdcss selected from config.h #define */
334 i_flags |= DVDCSS_INIT_DEBUG;
337 /* XXX: put this shit in an access plugin */
338 if( strlen( p_input->p_source ) > 4
339 && !strncasecmp( p_input->p_source, "dvd:", 4 ) )
341 dvdhandle = dvdcss_open( p_input->p_source + 4, i_flags );
345 dvdhandle = dvdcss_open( p_input->p_source, i_flags );
348 if( dvdhandle == NULL )
350 intf_ErrMsg( "dvd error: dvdcss can't open device" );
351 p_input->b_error = 1;
355 p_input->i_handle = (int) dvdhandle;
358 /*****************************************************************************
359 * DVDClose: close dvd
360 *****************************************************************************/
361 static void DVDClose( struct input_thread_s *p_input )
363 /* Clean up libdvdcss */
364 dvdcss_close( (dvdcss_handle) p_input->i_handle );
367 /*****************************************************************************
368 * DVDEnd: frees unused data
369 *****************************************************************************/
370 static void DVDEnd( input_thread_t * p_input )
372 thread_dvd_data_t * p_dvd;
373 dvd_netlist_t * p_netlist;
375 p_dvd = (thread_dvd_data_t*)p_input->p_plugin_data;
376 p_netlist = (dvd_netlist_t *)p_input->p_method_data;
378 IfoDestroy( p_dvd->p_ifo );
382 DVDNetlistEnd( p_netlist );
385 /*****************************************************************************
386 * DVDSetArea: initialize input data for title x, chapter y.
387 * It should be called for each user navigation request.
388 *****************************************************************************
389 * Take care that i_title starts from 0 (vmg) and i_chapter start from 1.
390 * Note that you have to take the lock before entering here.
391 *****************************************************************************/
392 static int DVDSetArea( input_thread_t * p_input, input_area_t * p_area )
394 thread_dvd_data_t * p_dvd;
395 es_descriptor_t * p_es;
404 p_dvd = (thread_dvd_data_t*)p_input->p_plugin_data;
406 /* we can't use the interface slider until initilization is complete */
407 p_input->stream.b_seekable = 0;
409 if( p_area != p_input->stream.p_selected_area )
411 /* Reset the Chapter position of the old title */
412 p_input->stream.p_selected_area->i_part = 0;
415 * We have to load all title information
417 /* Change the default area */
418 p_input->stream.p_selected_area =
419 p_input->stream.pp_areas[p_area->i_id];
421 /* title number: it is not vts nb!,
422 * it is what appears in the interface list */
423 p_dvd->i_title = p_area->i_id;
424 p_dvd->p_ifo->i_title = p_dvd->i_title;
426 /* set number of chapters of current title */
427 p_dvd->i_chapter_nb = p_area->i_part_nb;
430 if( IfoTitleSet( p_dvd->p_ifo ) < 0 )
432 intf_ErrMsg( "dvd error: fatal error in vts ifo" );
434 p_input->b_error = 1;
438 #define vmg p_dvd->p_ifo->vmg
439 #define vts p_dvd->p_ifo->vts
440 /* title position inside the selected vts */
441 i_vts_title = vmg.title_inf.p_attr[p_dvd->i_title-1].i_title_num;
443 vts.title_inf.p_title_start[i_vts_title-1].i_title_id;
445 intf_WarnMsgImm( 3, "dvd: title %d vts_title %d pgc %d",
446 p_dvd->i_title, i_vts_title, p_dvd->i_title_id );
452 p_dvd->i_angle_nb = vmg.title_inf.p_attr[p_dvd->i_title-1].i_angle_nb;
453 p_dvd->i_angle = main_GetIntVariable( INPUT_ANGLE_VAR, 1 );
454 if( ( p_dvd->i_angle <= 0 ) || p_dvd->i_angle > p_dvd->i_angle_nb )
460 * Set selected title start and size
463 /* title set offset XXX: convert to block values */
464 p_dvd->i_title_start =
465 vts.i_pos + vts.manager_inf.i_title_vob_start_sector;
467 /* last video cell */
469 p_dvd->i_prg_cell = -1 +
470 vts.title_unit.p_title[p_dvd->i_title_id-1].title.i_cell_nb;
472 if( DVDFindCell( p_dvd ) < 0 )
474 intf_ErrMsg( "dvd error: can't find title end" );
475 p_input->b_error = 1;
479 /* temporary hack to fix size in some dvds */
480 if( p_dvd->i_cell >= vts.cell_inf.i_cell_nb )
482 p_dvd->i_cell = vts.cell_inf.i_cell_nb - 1;
486 p_dvd->i_size = vts.cell_inf.p_cell_map[p_dvd->i_cell].i_end_sector;
488 if( DVDChapterSelect( p_dvd, 1 ) < 0 )
490 intf_ErrMsg( "dvd error: can't find first chapter" );
491 p_input->b_error = 1;
496 * Force libdvdcss to check its title key.
497 * It is only useful for title cracking method. Methods using the decrypted
498 * disc key are fast enough to check the key at each seek
500 if( dvdcss_seek( p_dvd->dvdhandle, p_dvd->i_start, DVDCSS_SEEK_INI ) < 0 )
502 intf_ErrMsg( "dvd error: %s", dvdcss_error( p_dvd->dvdhandle ) );
506 p_dvd->i_size -= p_dvd->i_sector + 1;
508 IfoPrintTitle( p_dvd );
510 /* Area definition */
511 p_input->stream.p_selected_area->i_start = LB2OFF( p_dvd->i_start );
512 p_input->stream.p_selected_area->i_size = LB2OFF( p_dvd->i_size );
513 p_input->stream.p_selected_area->i_angle_nb = p_dvd->i_angle_nb;
514 p_input->stream.p_selected_area->i_angle = p_dvd->i_angle;
517 /* start at the beginning of the title */
518 /* FIXME: create a conf option to select whether to restart
520 p_input->stream.p_selected_area->i_tell = 0;
521 p_input->stream.p_selected_area->i_part = 1;
525 * Destroy obsolete ES by reinitializing program 0
526 * and find all ES in title with ifo data
528 if( p_input->stream.pp_programs != NULL )
530 /* We don't use input_EndStream here since
531 * we keep area structures */
533 for( i = 0 ; i < p_input->stream.i_selected_es_number ; i++ )
535 input_UnselectES( p_input, p_input->stream.pp_selected_es[i] );
538 free( p_input->stream.pp_selected_es );
539 input_DelProgram( p_input, p_input->stream.pp_programs[0] );
541 p_input->stream.pp_selected_es = NULL;
542 p_input->stream.i_selected_es_number = 0;
545 input_AddProgram( p_input, 0, sizeof( stream_ps_data_t ) );
547 /* No PSM to read in DVD mode, we already have all information */
548 p_input->stream.pp_programs[0]->b_is_ok = 1;
552 /* ES 0 -> video MPEG2 */
553 IfoPrintVideo( p_dvd );
555 p_es = input_AddES( p_input, p_input->stream.pp_programs[0], 0xe0, 0 );
556 p_es->i_stream_id = 0xe0;
557 p_es->i_type = MPEG2_VIDEO_ES;
558 p_es->i_cat = VIDEO_ES;
559 if( p_main->b_video )
561 input_SelectES( p_input, p_es );
564 #define audio_status \
565 vts.title_unit.p_title[p_dvd->i_title_id-1].title.pi_audio_status[i-1]
566 /* Audio ES, in the order they appear in .ifo */
567 for( i = 1 ; i <= vts.manager_inf.i_audio_nb ; i++ )
569 IfoPrintAudio( p_dvd, i );
571 /* audio channel is active if first byte is 0x80 */
572 if( audio_status.i_available )
576 switch( vts.manager_inf.p_audio_attr[i-1].i_coding_mode )
579 i_id = ( ( 0x80 + audio_status.i_position ) << 8 ) | 0xbd;
580 p_es = input_AddES( p_input,
581 p_input->stream.pp_programs[0], i_id, 0 );
582 p_es->i_stream_id = 0xbd;
583 p_es->i_type = AC3_AUDIO_ES;
585 p_es->i_cat = AUDIO_ES;
586 strcpy( p_es->psz_desc, DecodeLanguage( hton16(
587 vts.manager_inf.p_audio_attr[i-1].i_lang_code ) ) );
588 strcat( p_es->psz_desc, " (ac3)" );
592 case 0x03: /* MPEG audio */
593 i_id = 0xc0 + audio_status.i_position;
594 p_es = input_AddES( p_input,
595 p_input->stream.pp_programs[0], i_id, 0 );
596 p_es->i_stream_id = i_id;
597 p_es->i_type = MPEG2_AUDIO_ES;
599 p_es->i_cat = AUDIO_ES;
600 strcpy( p_es->psz_desc, DecodeLanguage( hton16(
601 vts.manager_inf.p_audio_attr[i-1].i_lang_code ) ) );
602 strcat( p_es->psz_desc, " (mpeg)" );
605 case 0x04: /* LPCM */
607 i_id = ( ( 0xa0 + audio_status.i_position ) << 8 ) | 0xbd;
608 p_es = input_AddES( p_input,
609 p_input->stream.pp_programs[0], i_id, 0 );
610 p_es->i_stream_id = i_id;
611 p_es->i_type = LPCM_AUDIO_ES;
613 p_es->i_cat = AUDIO_ES;
614 strcpy( p_es->psz_desc, DecodeLanguage( hton16(
615 vts.manager_inf.p_audio_attr[i-1].i_lang_code ) ) );
616 strcat( p_es->psz_desc, " (lpcm)" );
620 i_id = ( ( 0x88 + audio_status.i_position ) << 8 ) | 0xbd;
621 intf_ErrMsg( "dvd warning: DTS audio not handled yet"
626 intf_ErrMsg( "dvd warning: unknown audio type %.2x",
627 vts.manager_inf.p_audio_attr[i-1].i_coding_mode );
633 vts.title_unit.p_title[p_dvd->i_title_id-1].title.pi_spu_status[i-1]
637 for( i = 1 ; i <= vts.manager_inf.i_spu_nb; i++ )
639 IfoPrintSpu( p_dvd, i );
641 if( spu_status.i_available )
645 /* there are several streams for one spu */
646 if( vts.manager_inf.video_attr.i_ratio )
649 switch( vts.manager_inf.video_attr.i_perm_displ )
652 i_id = ( ( 0x20 + spu_status.i_position_pan ) << 8 )
656 i_id = ( ( 0x20 + spu_status.i_position_letter ) << 8 )
660 i_id = ( ( 0x20 + spu_status.i_position_wide ) << 8 )
668 i_id = ( ( 0x20 + spu_status.i_position_43 ) << 8 )
671 p_es = input_AddES( p_input,
672 p_input->stream.pp_programs[0], i_id, 0 );
673 p_es->i_stream_id = 0xbd;
674 p_es->i_type = DVD_SPU_ES;
675 p_es->i_cat = SPU_ES;
676 strcpy( p_es->psz_desc, DecodeLanguage( hton16(
677 vts.manager_inf.p_spu_attr[i-1].i_lang_code ) ) );
681 if( p_main->b_audio )
683 /* For audio: first one if none or a not existing one specified */
684 i_audio = main_GetIntVariable( INPUT_CHANNEL_VAR, 1 );
685 if( i_audio < 0 || i_audio > i_audio_nb )
687 main_PutIntVariable( INPUT_CHANNEL_VAR, 1 );
690 if( i_audio > 0 && i_audio_nb > 0 )
692 if( main_GetIntVariable( AOUT_SPDIF_VAR, 0 ) ||
693 ( main_GetIntVariable( INPUT_AUDIO_VAR, 0 ) ==
697 while( ( p_input->stream.pp_es[i_ac3]->i_type !=
698 AC3_AUDIO_ES ) && ( i_ac3 <=
699 vts.manager_inf.i_audio_nb ) )
703 if( p_input->stream.pp_es[i_ac3]->i_type == AC3_AUDIO_ES )
705 input_SelectES( p_input,
706 p_input->stream.pp_es[i_ac3] );
711 input_SelectES( p_input,
712 p_input->stream.pp_es[i_audio] );
717 if( p_main->b_video )
719 /* for spu, default is none */
720 i_spu = main_GetIntVariable( INPUT_SUBTITLE_VAR, 0 );
721 if( i_spu < 0 || i_spu > i_spu_nb )
723 main_PutIntVariable( INPUT_SUBTITLE_VAR, 0 );
726 if( i_spu > 0 && i_spu_nb > 0 )
728 i_spu += vts.manager_inf.i_audio_nb;
729 input_SelectES( p_input, p_input->stream.pp_es[i_spu] );
735 p_area = p_input->stream.p_selected_area;
744 if( p_area->i_part != p_dvd->i_chapter )
746 if( ( p_area->i_part > 0 ) &&
747 ( p_area->i_part <= p_area->i_part_nb ))
749 if( DVDChapterSelect( p_dvd, p_area->i_part ) < 0 )
751 intf_ErrMsg( "dvd error: can't set chapter in area" );
752 p_input->b_error = 1;
756 p_input->stream.p_selected_area->i_tell =
757 LB2OFF( p_dvd->i_start ) - p_area->i_start;
758 p_input->stream.p_selected_area->i_part = p_dvd->i_chapter;
760 intf_WarnMsg( 4, "dvd info: chapter %d start at: %lld",
761 p_area->i_part, p_area->i_tell );
766 p_dvd->i_chapter = 1;
771 p_dvd->p_ifo->vts.title_unit.p_title[p_dvd->i_title_id-1].title
772 if( p_area->i_angle != p_dvd->i_angle )
774 if( title.p_cell_play[p_dvd->i_prg_cell].i_category & 0xf000 )
776 if( ( p_area->i_angle - p_dvd->i_angle ) < 0 )
780 p_dvd->i_prg_cell += ( p_area->i_angle - p_dvd->i_angle );
781 p_dvd->i_angle = p_area->i_angle;
783 DVDFindSector( p_dvd );
784 p_dvd->i_cell += p_dvd->i_angle_cell;
788 p_dvd->i_angle = p_area->i_angle;
791 intf_WarnMsg( 3, "dvd info: angle %d selected", p_area->i_angle );
794 /* warn interface that something has changed */
795 p_input->stream.b_seekable = 1;
796 p_input->stream.b_changed = 1;
802 /*****************************************************************************
803 * DVDRead: reads data packets into the netlist.
804 *****************************************************************************
805 * Returns -1 in case of error, 0 if everything went well, and 1 in case of
807 *****************************************************************************/
808 static int DVDRead( input_thread_t * p_input,
809 data_packet_t ** pp_packets )
811 thread_dvd_data_t * p_dvd;
812 dvd_netlist_t * p_netlist;
813 struct iovec * p_vec;
814 struct data_packet_s * pp_data[DVD_DATA_READ_ONCE];
827 p_dvd = (thread_dvd_data_t *)p_input->p_plugin_data;
828 p_netlist = (dvd_netlist_t *)p_input->p_method_data;
831 i_sector = p_dvd->i_title_start + p_dvd->i_sector;
832 i_block_once = p_dvd->i_end_sector - p_dvd->i_sector + 1;
835 /* Get the position of the next cell if we're at cell end */
836 if( i_block_once <= 0 )
841 p_dvd->i_angle_cell++;
843 /* Find cell index in adress map */
844 if( DVDFindSector( p_dvd ) < 0 )
846 pp_packets[0] = NULL;
847 intf_ErrMsg( "dvd error: can't find next cell" );
851 /* Position the fd pointer on the right address */
852 if( ( i_sector = dvdcss_seek( p_dvd->dvdhandle,
853 p_dvd->i_title_start + p_dvd->i_sector,
854 DVDCSS_SEEK_MPEG ) ) < 0 )
856 intf_ErrMsg( "dvd error: %s", dvdcss_error( p_dvd->dvdhandle ) );
860 /* update chapter : it will be easier when we have navigation
862 if( p_dvd->i_chapter < ( p_dvd->i_chapter_nb - 1 ) )
864 if( title.p_cell_play[p_dvd->i_prg_cell].i_category & 0xf000 )
866 i_angle = p_dvd->i_angle - 1;
872 if( title.chapter_map.pi_start_cell[p_dvd->i_chapter] <=
873 ( p_dvd->i_prg_cell - i_angle + 1 ) )
880 i_block_once = p_dvd->i_end_sector - p_dvd->i_sector + 1;
883 /* The number of blocks read is the max between the requested
884 * value and the leaving block in the cell */
885 if( i_block_once > p_dvd->i_block_once )
887 i_block_once = p_dvd->i_block_once;
890 intf_WarnMsg( 2, "Sector: 0x%x Read: %d Chapter: %d", p_dvd->i_sector, i_block_once, p_dvd->i_chapter );
892 p_netlist->i_read_once = i_block_once;
894 /* Get an iovec pointer */
895 if( ( p_vec = DVDGetiovec( p_netlist ) ) == NULL )
897 intf_ErrMsg( "dvd error: can't get iovec" );
902 i_read_blocks = dvdcss_readv( p_dvd->dvdhandle, p_vec,
903 i_block_once, DVDCSS_READ_DECRYPT );
905 /* Update netlist indexes: we don't do it in DVDGetiovec since we
906 * need know the real number of blocks read */
907 DVDMviovec( p_netlist, i_read_blocks, pp_data );
909 /* Update global position */
910 p_dvd->i_sector += i_read_blocks;
914 /* Read headers to compute payload length */
915 for( i_iovec = 0 ; i_iovec < i_read_blocks ; i_iovec++ )
919 while( i_pos < p_netlist->i_buffer_size )
921 pi_cur = (u8*)p_vec[i_iovec].iov_base + i_pos;
924 if( U32_AT( pi_cur ) != 0x1BA )
926 /* That's the case for all packets, except pack header. */
927 i_packet_size = U16_AT( pi_cur + 4 );
928 pp_packets[i_packet] = DVDNewPtr( p_netlist );
932 /* MPEG-2 Pack header. */
934 pp_packets[i_packet] = pp_data[i_iovec];
938 (*pp_data[i_iovec]->pi_refcount)++;
940 pp_packets[i_packet]->pi_refcount = pp_data[i_iovec]->pi_refcount;
942 pp_packets[i_packet]->p_buffer = pp_data[i_iovec]->p_buffer;
944 pp_packets[i_packet]->p_payload_start =
945 pp_packets[i_packet]->p_buffer + i_pos;
947 pp_packets[i_packet]->p_payload_end =
948 pp_packets[i_packet]->p_payload_start + i_packet_size + 6;
950 pp_packets[i_packet]->p_next = NULL;
951 pp_packets[i_packet]->b_discard_payload = 0;
954 i_pos += i_packet_size + 6;
958 pp_packets[i_packet] = NULL;
960 vlc_mutex_lock( &p_input->stream.stream_lock );
962 p_input->stream.p_selected_area->i_tell =
963 LB2OFF( i_sector + i_read_blocks ) -
964 p_input->stream.p_selected_area->i_start;
967 /* We modify i_part only at end of chapter not to erase
968 * some modification from the interface */
969 p_input->stream.p_selected_area->i_part = p_dvd->i_chapter;
972 b_eot = !( p_input->stream.p_selected_area->i_tell
973 < p_input->stream.p_selected_area->i_size );
974 b_eof = b_eot && ( ( p_dvd->i_title + 1 ) >= p_input->stream.i_area_nb );
978 vlc_mutex_unlock( &p_input->stream.stream_lock );
984 intf_WarnMsg( 4, "dvd info: new title" );
986 DVDSetArea( p_input, p_input->stream.pp_areas[p_dvd->i_title] );
987 vlc_mutex_unlock( &p_input->stream.stream_lock );
991 vlc_mutex_unlock( &p_input->stream.stream_lock );
993 if( i_read_blocks == i_block_once )
1001 /*****************************************************************************
1002 * DVDRewind : reads a stream backward
1003 *****************************************************************************/
1004 static int DVDRewind( input_thread_t * p_input )
1009 /*****************************************************************************
1010 * DVDSeek : Goes to a given position on the stream.
1011 *****************************************************************************
1012 * This one is used by the input and translate chronological position from
1013 * input to logical position on the device.
1014 * The lock should be taken before calling this function.
1015 *****************************************************************************/
1016 static void DVDSeek( input_thread_t * p_input, off_t i_off )
1018 thread_dvd_data_t * p_dvd;
1025 p_dvd = ( thread_dvd_data_t * )p_input->p_plugin_data;
1027 /* we have to take care of offset of beginning of title */
1028 p_dvd->i_sector = OFF2LB(i_off + p_input->stream.p_selected_area->i_start)
1029 - p_dvd->i_title_start;
1034 /* parse vobu address map to find program cell */
1035 while( title.p_cell_play[i_prg_cell].i_end_sector < p_dvd->i_sector )
1040 p_dvd->i_prg_cell = i_prg_cell;
1042 if( DVDChooseAngle( p_dvd ) < 0 )
1044 p_input->b_error = 1;
1050 /* Find first title cell which is inside program cell */
1051 if( DVDFindCell( p_dvd ) < 0 )
1053 /* no following cell : we're at eof */
1054 intf_ErrMsg( "dvd error: cell seeking failed" );
1055 p_input->b_error = 1;
1059 i_cell = p_dvd->i_cell;
1061 #define cell p_dvd->p_ifo->vts.cell_inf.p_cell_map[i_cell]
1062 /* parse cell address map to find title cell containing sector */
1063 while( cell.i_end_sector < p_dvd->i_sector )
1068 p_dvd->i_cell = i_cell;
1070 /* if we're inside a multi-angle zone, we have to choose i_sector
1071 * in the current angle ; we can't do it all the time since cells
1072 * can be very wide out of such zones */
1073 if( title.p_cell_play[p_dvd->i_prg_cell].i_category & 0xf000 )
1075 p_dvd->i_sector = MAX(
1076 cell.i_start_sector,
1077 title.p_cell_play[p_dvd->i_prg_cell].i_start_sector );
1080 p_dvd->i_end_sector = MIN(
1082 title.p_cell_play[p_dvd->i_prg_cell].i_end_sector );
1084 /* update chapter */
1085 if( title.p_cell_play[p_dvd->i_prg_cell].i_category & 0xf000 )
1087 i_angle = p_dvd->i_angle - 1;
1093 if( p_dvd->i_chapter_nb > 1 )
1095 while( ( title.chapter_map.pi_start_cell[i_chapter] <=
1096 ( p_dvd->i_prg_cell - i_angle + 1 ) ) &&
1097 ( i_chapter < ( p_dvd->i_chapter_nb - 1 ) ) )
1107 p_dvd->i_chapter = i_chapter;
1108 p_input->stream.p_selected_area->i_part = p_dvd->i_chapter;
1110 if( ( i_block = dvdcss_seek( p_dvd->dvdhandle,
1111 p_dvd->i_title_start + p_dvd->i_sector,
1112 DVDCSS_SEEK_MPEG ) ) < 0 )
1114 intf_ErrMsg( "dvd error: %s", dvdcss_error( p_dvd->dvdhandle ) );
1115 p_input->b_error = 1;
1119 p_input->stream.p_selected_area->i_tell =
1120 LB2OFF ( i_block ) - p_input->stream.p_selected_area->i_start;
1122 intf_WarnMsg( 7, "Program Cell: %d Cell: %d Chapter: %d",
1123 p_dvd->i_prg_cell, p_dvd->i_cell, p_dvd->i_chapter );
1129 #define cell p_dvd->p_ifo->vts.cell_inf
1131 /*****************************************************************************
1132 * DVDFindCell: adjust the title cell index with the program cell
1133 *****************************************************************************/
1134 static int DVDFindCell( thread_dvd_data_t * p_dvd )
1139 i_cell = p_dvd->i_cell;
1140 i_index = p_dvd->i_prg_cell;
1142 if( i_cell >= cell.i_cell_nb )
1147 while( ( ( title.p_cell_pos[i_index].i_vob_id !=
1148 cell.p_cell_map[i_cell].i_vob_id ) ||
1149 ( title.p_cell_pos[i_index].i_cell_id !=
1150 cell.p_cell_map[i_cell].i_cell_id ) ) &&
1151 ( i_cell < cell.i_cell_nb - 1 ) )
1157 intf_WarnMsg( 12, "FindCell: i_cell %d i_index %d found %d nb %d",
1164 p_dvd->i_cell = i_cell;
1171 /*****************************************************************************
1172 * DVDFindSector: find cell index in adress map from index in
1173 * information table program map and give corresponding sectors.
1174 *****************************************************************************/
1175 static int DVDFindSector( thread_dvd_data_t * p_dvd )
1178 if( p_dvd->i_sector > title.p_cell_play[p_dvd->i_prg_cell].i_end_sector )
1180 p_dvd->i_prg_cell++;
1182 if( DVDChooseAngle( p_dvd ) < 0 )
1188 if( DVDFindCell( p_dvd ) < 0 )
1190 intf_ErrMsg( "dvd error: can't find sector" );
1194 /* Find start and end sectors of new cell */
1196 p_dvd->i_sector = MAX(
1197 p_dvd->p_ifo->vts.cell_inf.p_cell_map[p_dvd->i_cell].i_start_sector,
1198 title.p_cell_play[p_dvd->i_prg_cell].i_start_sector );
1199 p_dvd->i_end_sector = MIN(
1200 p_dvd->p_ifo->vts.cell_inf.p_cell_map[p_dvd->i_cell].i_end_sector,
1201 title.p_cell_play[p_dvd->i_prg_cell].i_end_sector );
1203 p_dvd->i_sector = title.p_cell_play[p_dvd->i_prg_cell].i_start_sector;
1204 p_dvd->i_end_sector = title.p_cell_play[p_dvd->i_prg_cell].i_end_sector;
1208 intf_WarnMsg( 12, "cell: %d sector1: 0x%x end1: 0x%x\n"
1209 "index: %d sector2: 0x%x end2: 0x%x\n"
1210 "category: 0x%x ilvu end: 0x%x vobu start 0x%x",
1212 p_dvd->p_ifo->vts.cell_inf.p_cell_map[p_dvd->i_cell].i_start_sector,
1213 p_dvd->p_ifo->vts.cell_inf.p_cell_map[p_dvd->i_cell].i_end_sector,
1215 title.p_cell_play[p_dvd->i_prg_cell].i_start_sector,
1216 title.p_cell_play[p_dvd->i_prg_cell].i_end_sector,
1217 title.p_cell_play[p_dvd->i_prg_cell].i_category,
1218 title.p_cell_play[p_dvd->i_prg_cell].i_first_ilvu_vobu_esector,
1219 title.p_cell_play[p_dvd->i_prg_cell].i_last_vobu_start_sector );
1225 /*****************************************************************************
1226 * DVDChapterSelect: find the cell corresponding to requested chapter
1227 *****************************************************************************/
1228 static int DVDChapterSelect( thread_dvd_data_t * p_dvd, int i_chapter )
1231 /* Find cell index in Program chain for current chapter */
1232 p_dvd->i_prg_cell = title.chapter_map.pi_start_cell[i_chapter-1] - 1;
1234 p_dvd->i_sector = 0;
1236 DVDChooseAngle( p_dvd );
1238 /* Search for cell_index in cell adress_table and initialize
1240 if( DVDFindSector( p_dvd ) < 0 )
1242 intf_ErrMsg( "dvd error: can't select chapter" );
1246 /* start is : beginning of vts vobs + offset to vob x */
1247 p_dvd->i_start = p_dvd->i_title_start + p_dvd->i_sector;
1249 /* Position the fd pointer on the right address */
1250 if( ( p_dvd->i_start = dvdcss_seek( p_dvd->dvdhandle,
1252 DVDCSS_SEEK_MPEG ) ) < 0 )
1254 intf_ErrMsg( "dvd error: %s", dvdcss_error( p_dvd->dvdhandle ) );
1258 p_dvd->i_chapter = i_chapter;
1262 /*****************************************************************************
1263 * DVDChooseAngle: select the cell corresponding to the selected angle
1264 *****************************************************************************/
1265 static int DVDChooseAngle( thread_dvd_data_t * p_dvd )
1267 /* basic handling of angles */
1268 switch( ( ( title.p_cell_play[p_dvd->i_prg_cell].i_category & 0xf000 )
1271 /* we enter a muli-angle section */
1273 p_dvd->i_prg_cell += p_dvd->i_angle - 1;
1274 p_dvd->i_angle_cell = 0;
1276 /* we exit a multi-angle section */
1279 p_dvd->i_prg_cell += p_dvd->i_angle_nb - p_dvd->i_angle;