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 * -dvd_ifo for ifo parsing and analyse
9 * -dvd_css for unscrambling
10 * -dvd_udf to find files
11 *****************************************************************************
12 * Copyright (C) 1998-2001 VideoLAN
13 * $Id: input_dvd.c,v 1.61 2001/05/30 17:03:12 sam 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 /*****************************************************************************
34 *****************************************************************************/
38 #define MODULE_NAME dvd
40 #define MODULE_NAME dvdnocss
42 #include "modules_inner.h"
47 #include <netinet/in.h>
50 #include <sys/types.h>
54 #ifdef STRNCASECMP_IN_STRINGS_H
69 #include "stream_control.h"
70 #include "input_ext-intf.h"
71 #include "input_ext-dec.h"
75 #include "dvd_netlist.h"
78 #include "dvd_summary.h"
79 #include "input_dvd.h"
80 #include "mpeg_system.h"
86 /*****************************************************************************
88 *****************************************************************************/
89 /* called from outside */
90 static int DVDProbe ( probedata_t *p_data );
91 static void DVDInit ( struct input_thread_s * );
92 static void DVDEnd ( struct input_thread_s * );
93 static int DVDSetArea ( struct input_thread_s *, struct input_area_s * );
94 static int DVDRead ( struct input_thread_s *, data_packet_t ** );
95 static void DVDSeek ( struct input_thread_s *, off_t );
96 static int DVDRewind ( struct input_thread_s * );
98 /* called only inside */
99 static int DVDChooseAngle( thread_dvd_data_t * );
100 static int DVDFindCell( thread_dvd_data_t * );
101 static int DVDFindSector( thread_dvd_data_t * );
102 static int DVDChapterSelect( thread_dvd_data_t *, int );
104 /*****************************************************************************
105 * Functions exported as capabilities. They are declared as static so that
106 * we don't pollute the namespace too much.
107 *****************************************************************************/
108 void _M( input_getfunctions )( function_list_t * p_function_list )
110 #define input p_function_list->functions.input
111 p_function_list->pf_probe = DVDProbe;
112 input.pf_init = DVDInit;
113 input.pf_open = NULL; /* Set in DVDInit */
114 input.pf_close = NULL;
115 input.pf_end = DVDEnd;
116 input.pf_read = DVDRead;
117 input.pf_set_area = DVDSetArea;
118 input.pf_demux = input_DemuxPS;
119 input.pf_new_packet = DVDNewPacket;
120 input.pf_new_pes = DVDNewPES;
121 input.pf_delete_packet = DVDDeletePacket;
122 input.pf_delete_pes = DVDDeletePES;
123 input.pf_rewind = DVDRewind;
124 input.pf_seek = DVDSeek;
129 * Data reading functions
132 /*****************************************************************************
133 * DVDProbe: verifies that the stream is a PS stream
134 *****************************************************************************/
135 static int DVDProbe( probedata_t *p_data )
137 input_thread_t * p_input = (input_thread_t *)p_data;
139 char * psz_name = p_input->p_source;
143 if( TestMethod( INPUT_METHOD_VAR, "dvd" ) )
149 #endif /* HAVE_CSS */
152 if( ( strlen(psz_name) > 4 ) && !strncasecmp( psz_name, "dvd:", 4 ) )
154 /* If the user specified "dvd:" then it's probably a DVD */
159 #endif /* HAVE_CSS */
163 i_handle = open( psz_name, 0 );
173 /*****************************************************************************
174 * DVDInit: initializes DVD structures
175 *****************************************************************************/
176 static void DVDInit( input_thread_t * p_input )
178 thread_dvd_data_t * p_dvd;
179 input_area_t * p_area;
184 p_dvd = malloc( sizeof(thread_dvd_data_t) );
187 intf_ErrMsg( "dvd error: out of memory" );
188 p_input->b_error = 1;
192 p_input->p_plugin_data = (void *)p_dvd;
193 p_input->p_method_data = NULL;
196 p_input->pf_open = p_input->pf_file_open;
197 p_input->pf_close = p_input->pf_file_close;
199 p_dvd->i_fd = p_input->i_handle;
201 /* reading several block once seems to cause lock-up
202 * when using input_ToggleES
203 * who wrote thez damn buggy piece of shit ??? --stef */
204 p_dvd->i_block_once = 32;
205 p_input->i_read_once = 128;
207 i = CSSTest( p_input->i_handle );
211 intf_ErrMsg( "dvd error: error in css" );
213 p_input->b_error = 1;
217 p_dvd->b_encrypted = i;
219 lseek( p_input->i_handle, 0, SEEK_SET );
221 /* Reading structures initialisation */
222 p_input->p_method_data =
223 DVDNetlistInit( 2048, 4096, 2048, DVD_LB_SIZE, p_dvd->i_block_once );
224 intf_WarnMsg( 2, "dvd info: netlist initialized" );
226 /* Ifo allocation & initialisation */
227 if( IfoCreate( p_dvd ) < 0 )
229 intf_ErrMsg( "dvd error: allcation error in ifo" );
230 p_input->b_error = 1;
234 if( IfoInit( p_dvd->p_ifo ) < 0 )
236 intf_ErrMsg( "dvd error: fatal failure in ifo" );
238 p_input->b_error = 1;
242 /* CSS initialisation */
243 if( p_dvd->b_encrypted )
245 p_dvd->p_css = malloc( sizeof(css_t) );
246 if( p_dvd->p_css == NULL )
248 intf_ErrMsg( "dvd error: couldn't create css structure" );
250 p_input->b_error = 1;
254 p_dvd->p_css->i_agid = 0;
256 if( CSSInit( p_input->i_handle, p_dvd->p_css ) < 0 )
258 intf_ErrMsg( "dvd error: fatal failure in css" );
259 free( p_dvd->p_css );
261 p_input->b_error = 1;
265 intf_WarnMsg( 2, "dvd info: css initialized" );
268 /* Set stream and area data */
269 vlc_mutex_lock( &p_input->stream.stream_lock );
271 /* Initialize ES structures */
272 input_InitStream( p_input, sizeof( stream_ps_data_t ) );
274 /* disc input method */
275 p_input->stream.i_method = INPUT_METHOD_DVD;
277 #define title_inf p_dvd->p_ifo->vmg.title_inf
278 intf_WarnMsg( 2, "dvd info: number of titles: %d", title_inf.i_title_nb );
280 #define area p_input->stream.pp_areas
281 /* We start from 1 here since the default area 0
282 * is reserved for video_ts.vob */
283 for( i = 1 ; i <= title_inf.i_title_nb ; i++ )
285 input_AddArea( p_input );
287 /* Titles are Program Chains */
290 /* Absolute start offset and size
291 * We can only set that with vts ifo, so we do it during the
292 * first call to DVDSetArea */
293 area[i]->i_start = 0;
296 /* Number of chapters */
297 area[i]->i_part_nb = title_inf.p_attr[i-1].i_chapter_nb;
300 /* Number of angles */
301 area[i]->i_angle_nb = 0;
302 area[i]->i_angle = 1;
304 /* Offset to vts_i_0.ifo */
305 area[i]->i_plugin_data = p_dvd->p_ifo->i_off +
306 ( title_inf.p_attr[i-1].i_start_sector * DVD_LB_SIZE );
310 /* Get requested title - if none try the first title */
311 i_title = main_GetIntVariable( INPUT_TITLE_VAR, 1 );
312 if( i_title <= 0 || i_title > title_inf.i_title_nb )
319 /* Get requested chapter - if none defaults to first one */
320 i_chapter = main_GetIntVariable( INPUT_CHAPTER_VAR, 1 );
326 p_input->stream.pp_areas[i_title]->i_part = i_chapter;
328 p_area = p_input->stream.pp_areas[i_title];
330 /* set title, chapter, audio and subpic */
331 DVDSetArea( p_input, p_area );
333 vlc_mutex_unlock( &p_input->stream.stream_lock );
338 /*****************************************************************************
339 * DVDEnd: frees unused data
340 *****************************************************************************/
341 static void DVDEnd( input_thread_t * p_input )
343 thread_dvd_data_t * p_dvd;
344 dvd_netlist_t * p_netlist;
346 p_dvd = (thread_dvd_data_t*)p_input->p_plugin_data;
347 p_netlist = (dvd_netlist_t *)p_input->p_method_data;
349 if( p_dvd->b_encrypted )
351 free( p_dvd->p_css );
354 IfoDestroy( p_dvd->p_ifo );
356 DVDNetlistEnd( p_netlist );
359 /*****************************************************************************
360 * DVDSetArea: initialize input data for title x, chapter y.
361 * It should be called for each user navigation request.
362 *****************************************************************************
363 * Take care that i_title starts from 0 (vmg) and i_chapter start from 1.
364 * Note that you have to take the lock before entering here.
365 *****************************************************************************/
366 static int DVDSetArea( input_thread_t * p_input, input_area_t * p_area )
368 thread_dvd_data_t * p_dvd;
369 es_descriptor_t * p_es;
377 p_dvd = (thread_dvd_data_t*)p_input->p_plugin_data;
379 /* we can't use the interface slider until initilization is complete */
380 p_input->stream.b_seekable = 0;
382 if( p_area != p_input->stream.p_selected_area )
386 * We have to load all title information
388 /* Change the default area */
389 p_input->stream.p_selected_area =
390 p_input->stream.pp_areas[p_area->i_id];
392 /* release the lock to to let the interface go */
393 // vlc_mutex_unlock( &p_input->stream.stream_lock );
395 /* title number: it is not vts nb!,
396 * it is what appears in the interface list */
397 p_dvd->i_title = p_area->i_id;
398 p_dvd->p_ifo->i_title = p_dvd->i_title;
400 /* set number of chapters of current title */
401 p_dvd->i_chapter_nb = p_area->i_part_nb;
404 if( IfoTitleSet( p_dvd->p_ifo ) < 0 )
406 intf_ErrMsg( "dvd error: fatal error in vts ifo" );
408 p_input->b_error = 1;
412 #define vmg p_dvd->p_ifo->vmg
413 #define vts p_dvd->p_ifo->vts
414 /* title position inside the selected vts */
415 i_vts_title = vmg.title_inf.p_attr[p_dvd->i_title-1].i_title_num;
417 vts.title_inf.p_title_start[i_vts_title-1].i_title_id;
419 intf_WarnMsg( 3, "dvd: title %d vts_title %d pgc %d",
424 /* css title key for current vts */
425 if( p_dvd->b_encrypted )
427 /* this one is vts number */
428 p_dvd->p_css->i_title =
429 vmg.title_inf.p_attr[p_dvd->i_title-1].i_title_set_num;
430 p_dvd->p_css->i_title_pos =
432 vts.manager_inf.i_title_vob_start_sector * DVD_LB_SIZE;
434 j = CSSGetKey( p_input->i_handle, p_dvd->p_css );
437 intf_ErrMsg( "dvd error: fatal error in vts css key" );
439 p_input->b_error = 1;
444 intf_ErrMsg( "dvd error: css decryption unavailable" );
446 p_input->b_error = 1;
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 */
466 p_dvd->i_title_start = vts.i_pos + DVD_LB_SIZE *
467 (off_t)( 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 = DVD_LB_SIZE *
489 (off_t)( vts.cell_inf.p_cell_map[p_dvd->i_cell].i_end_sector );
490 intf_WarnMsg( 2, "dvd info: stream size 1: %lld @ %d", p_dvd->i_size,
491 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 p_dvd->i_size -= (off_t)( p_dvd->i_sector + 1 ) *DVD_LB_SIZE;
502 IfoPrintTitle( p_dvd );
504 // vlc_mutex_lock( &p_input->stream.stream_lock );
506 /* Area definition */
507 p_input->stream.p_selected_area->i_start = p_dvd->i_start;
508 p_input->stream.p_selected_area->i_size = p_dvd->i_size;
509 p_input->stream.p_selected_area->i_angle_nb = p_dvd->i_angle_nb;
510 p_input->stream.p_selected_area->i_angle = p_dvd->i_angle;
513 * Destroy obsolete ES by reinitializing program 0
514 * and find all ES in title with ifo data
516 if( p_input->stream.pp_programs != NULL )
518 /* We don't use input_EndStream here since
519 * we keep area structures */
521 for( i = 0 ; i < p_input->stream.i_selected_es_number ; i++ )
523 input_UnselectES( p_input, p_input->stream.pp_selected_es[i] );
526 input_DelProgram( p_input, p_input->stream.pp_programs[0] );
528 p_input->stream.pp_selected_es = NULL;
529 p_input->stream.i_selected_es_number = 0;
532 input_AddProgram( p_input, 0, sizeof( stream_ps_data_t ) );
534 /* No PSM to read in DVD mode, we already have all information */
535 p_input->stream.pp_programs[0]->b_is_ok = 1;
536 p_input->stream.pp_programs[0]->i_synchro_state = SYNCHRO_START;
540 /* ES 0 -> video MPEG2 */
541 IfoPrintVideo( p_dvd );
543 p_es = input_AddES( p_input, p_input->stream.pp_programs[0], 0xe0, 0 );
544 p_es->i_stream_id = 0xe0;
545 p_es->i_type = MPEG2_VIDEO_ES;
546 p_es->i_cat = VIDEO_ES;
547 intf_WarnMsg( 1, "dvd info: video mpeg2 stream" );
548 if( p_main->b_video )
550 input_SelectES( p_input, p_es );
552 intf_WarnMsg( 4, "dvd info: video selected" );
554 #define audio_status \
555 vts.title_unit.p_title[p_dvd->i_title_id-1].title.pi_audio_status[i-1]
556 /* Audio ES, in the order they appear in .ifo */
557 for( i = 1 ; i <= vts.manager_inf.i_audio_nb ; i++ )
559 IfoPrintAudio( p_dvd, i );
561 /* audio channel is active if first byte is 0x80 */
562 if( audio_status.i_available )
564 switch( vts.manager_inf.p_audio_attr[i-1].i_coding_mode )
567 i_id = ( ( 0x80 + audio_status.i_position ) << 8 ) | 0xbd;
568 p_es = input_AddES( p_input,
569 p_input->stream.pp_programs[0], i_id, 0 );
570 p_es->i_stream_id = 0xbd;
571 p_es->i_type = AC3_AUDIO_ES;
573 p_es->i_cat = AUDIO_ES;
574 strcpy( p_es->psz_desc, IfoLanguage( hton16(
575 vts.manager_inf.p_audio_attr[i-1].i_lang_code ) ) );
576 strcat( p_es->psz_desc, " (ac3)" );
578 intf_WarnMsg( 3, "dvd info: audio stream %d %s\t(0x%x)",
579 i, p_es->psz_desc, i_id );
583 case 0x03: /* MPEG audio */
584 i_id = 0xc0 + audio_status.i_position;
585 p_es = input_AddES( p_input,
586 p_input->stream.pp_programs[0], i_id, 0 );
587 p_es->i_stream_id = i_id;
588 p_es->i_type = MPEG2_AUDIO_ES;
590 p_es->i_cat = AUDIO_ES;
591 strcpy( p_es->psz_desc, IfoLanguage( hton16(
592 vts.manager_inf.p_audio_attr[i-1].i_lang_code ) ) );
593 strcat( p_es->psz_desc, " (mpeg)" );
595 intf_WarnMsg( 3, "dvd info: audio stream %d %s\t(0x%x)",
596 i, p_es->psz_desc, i_id );
599 case 0x04: /* LPCM */
601 i_id = ( ( 0xa0 + audio_status.i_position ) << 8 ) | 0xbd;
602 p_es = input_AddES( p_input,
603 p_input->stream.pp_programs[0], i_id, 0 );
604 p_es->i_stream_id = i_id;
605 p_es->i_type = LPCM_AUDIO_ES;
607 p_es->i_cat = AUDIO_ES;
608 strcpy( p_es->psz_desc, IfoLanguage( hton16(
609 vts.manager_inf.p_audio_attr[i-1].i_lang_code ) ) );
610 strcat( p_es->psz_desc, " (lpcm)" );
612 intf_WarnMsg( 3, "dvd info: audio stream %d %s\t(0x%x)",
613 i, p_es->psz_desc, i_id );
616 i_id = ( ( 0x88 + audio_status.i_position ) << 8 ) | 0xbd;
617 intf_ErrMsg( "dvd warning: DTS audio not handled yet"
622 intf_ErrMsg( "dvd warning: unknown audio type %.2x",
623 vts.manager_inf.p_audio_attr[i-1].i_coding_mode );
629 vts.title_unit.p_title[p_dvd->i_title_id-1].title.pi_spu_status[i-1]
633 for( i = 1 ; i <= vts.manager_inf.i_spu_nb; i++ )
635 IfoPrintSpu( p_dvd, i );
637 if( spu_status.i_available )
639 /* there are several streams for one spu */
640 if( vts.manager_inf.video_attr.i_ratio )
643 switch( vts.manager_inf.video_attr.i_perm_displ )
646 i_id = ( ( 0x20 + spu_status.i_position_pan ) << 8 )
650 i_id = ( ( 0x20 + spu_status.i_position_letter ) << 8 )
654 i_id = ( ( 0x20 + spu_status.i_position_wide ) << 8 )
662 i_id = ( ( 0x20 + spu_status.i_position_43 ) << 8 )
665 p_es = input_AddES( p_input,
666 p_input->stream.pp_programs[0], i_id, 0 );
667 p_es->i_stream_id = 0xbd;
668 p_es->i_type = DVD_SPU_ES;
669 p_es->i_cat = SPU_ES;
670 strcpy( p_es->psz_desc, IfoLanguage( hton16(
671 vts.manager_inf.p_spu_attr[i-1].i_lang_code ) ) );
672 intf_WarnMsg( 3, "dvd info: spu stream %d %s\t(0x%x)",
673 i, p_es->psz_desc, i_id );
677 if( p_main->b_audio )
679 /* For audio: first one if none or a not existing one specified */
680 i_audio = main_GetIntVariable( INPUT_CHANNEL_VAR, 1 );
681 if( i_audio < 0 || i_audio > vts.manager_inf.i_audio_nb )
683 main_PutIntVariable( INPUT_CHANNEL_VAR, 1 );
686 if( i_audio > 0 && vts.manager_inf.i_audio_nb > 0 )
688 input_SelectES( p_input, p_input->stream.pp_es[i_audio] );
692 if( p_main->b_video )
694 /* for spu, default is none */
695 i_spu = main_GetIntVariable( INPUT_SUBTITLE_VAR, 0 );
696 if( i_spu < 0 || i_spu > vts.manager_inf.i_spu_nb )
698 main_PutIntVariable( INPUT_SUBTITLE_VAR, 0 );
701 if( i_spu > 0 && vts.manager_inf.i_spu_nb > 0 )
703 i_spu += vts.manager_inf.i_audio_nb;
704 input_SelectES( p_input, p_input->stream.pp_es[i_spu] );
710 p_area = p_input->stream.p_selected_area;
720 if( p_area->i_part != p_dvd->i_chapter )
722 if( ( p_area->i_part > 0 ) &&
723 ( p_area->i_part <= p_area->i_part_nb ))
725 if( DVDChapterSelect( p_dvd, p_area->i_part ) < 0 )
727 intf_ErrMsg( "dvd error: can't set chapter in area" );
728 p_input->b_error = 1;
732 p_input->stream.p_selected_area->i_tell = p_dvd->i_start -
734 p_input->stream.p_selected_area->i_part = p_dvd->i_chapter;
736 intf_WarnMsg( 2, "dvd info: chapter %d start at: %lld",
737 p_area->i_part, p_area->i_tell );
742 p_dvd->i_chapter = 1;
747 p_dvd->p_ifo->vts.title_unit.p_title[p_dvd->i_title_id-1].title
748 if( p_area->i_angle != p_dvd->i_angle )
750 if( title.p_cell_play[p_dvd->i_prg_cell].i_category & 0xf000 )
752 if( ( p_area->i_angle - p_dvd->i_angle ) < 0 )
756 p_dvd->i_prg_cell += ( p_area->i_angle - p_dvd->i_angle );
757 p_dvd->i_angle = p_area->i_angle;
759 DVDFindSector( p_dvd );
760 p_dvd->i_cell += p_dvd->i_angle_cell;
764 p_dvd->i_angle = p_area->i_angle;
767 intf_WarnMsg( 2, "dvd info: angle %d selected", p_area->i_angle );
770 /* warn interface that something has changed */
771 p_input->stream.b_seekable = 1;
772 p_input->stream.b_changed = 1;
774 p_input->stream.pp_programs[0]->i_synchro_state = SYNCHRO_REINIT;
780 /*****************************************************************************
781 * DVDRead: reads data packets into the netlist.
782 *****************************************************************************
783 * Returns -1 in case of error, 0 if everything went well, and 1 in case of
785 *****************************************************************************/
786 static int DVDRead( input_thread_t * p_input,
787 data_packet_t ** pp_packets )
789 thread_dvd_data_t * p_dvd;
790 dvd_netlist_t * p_netlist;
791 struct iovec * p_vec;
792 struct data_packet_s * pp_data[p_input->i_read_once];
805 p_dvd = (thread_dvd_data_t *)p_input->p_plugin_data;
806 p_netlist = (dvd_netlist_t *)p_input->p_method_data;
808 /* Get an iovec pointer */
809 if( ( p_vec = DVDGetiovec( p_netlist ) ) == NULL )
811 intf_ErrMsg( "dvd error: can't get iovec" );
815 i_block_once = p_dvd->i_end_sector - p_dvd->i_sector + 1;
817 /* Get the position of the next cell if we're at cell end */
818 if( i_block_once <= 0 )
823 p_dvd->i_angle_cell++;
825 /* Find cell index in adress map */
826 if( DVDFindSector( p_dvd ) < 0 )
828 pp_packets[0] = NULL;
829 intf_ErrMsg( "dvd error: can't find next cell" );
833 /* Position the fd pointer on the right address */
834 i_off = lseek( p_dvd->i_fd,
835 p_dvd->i_title_start +
836 (off_t)( p_dvd->i_sector ) *DVD_LB_SIZE, SEEK_SET );
838 /* update chapter : it will be easier when we have navigation
840 if( p_dvd->i_chapter < ( p_dvd->i_chapter_nb - 1 ) )
842 if( title.p_cell_play[p_dvd->i_prg_cell].i_category & 0xf000 )
844 i_angle = p_dvd->i_angle - 1;
850 if( title.chapter_map.pi_start_cell[p_dvd->i_chapter] <=
851 ( p_dvd->i_prg_cell - i_angle + 1 ) )
857 vlc_mutex_lock( &p_input->stream.stream_lock );
859 p_input->stream.p_selected_area->i_tell = i_off -
860 p_input->stream.p_selected_area->i_start;
861 p_input->stream.p_selected_area->i_part = p_dvd->i_chapter;
863 /* the synchro has to be reinitialized when we change cell */
864 p_input->stream.pp_programs[0]->i_synchro_state = SYNCHRO_REINIT;
866 vlc_mutex_unlock( &p_input->stream.stream_lock );
868 i_block_once = p_dvd->i_end_sector - p_dvd->i_sector + 1;
871 /* the number of blocks read is the maw between the requested
872 * value and the leaving block in the cell */
873 if( i_block_once > p_dvd->i_block_once )
875 i_block_once = p_dvd->i_block_once;
877 //intf_WarnMsg( 2, "Sector: 0x%x Read: %d Chapter: %d", p_dvd->i_sector, i_block_once, p_dvd->i_chapter );
879 p_netlist->i_read_once = i_block_once;
882 i_read_bytes = readv( p_dvd->i_fd, p_vec, i_block_once );
883 i_read_blocks = ( i_read_bytes + 0x7ff ) >> 11;
885 /* Update netlist indexes */
886 DVDMviovec( p_netlist, i_read_blocks, pp_data );
888 /* Update global position */
889 p_dvd->i_sector += i_read_blocks;
893 /* Read headers to compute payload length */
894 for( i_iovec = 0 ; i_iovec < i_read_blocks ; i_iovec++ )
896 if( p_dvd->b_encrypted )
898 CSSDescrambleSector( p_dvd->p_css->pi_title_key,
899 p_vec[i_iovec].iov_base );
900 ((u8*)(p_vec[i_iovec].iov_base))[0x14] &= 0x8F;
905 while( i_pos < p_netlist->i_buffer_size )
907 pi_cur = (u8*)(p_vec[i_iovec].iov_base + i_pos);
910 if( U32_AT( pi_cur ) != 0x1BA )
912 /* That's the case for all packets, except pack header. */
913 i_packet_size = U16_AT( pi_cur + 4 );
914 pp_packets[i_packet] = DVDNewPtr( p_netlist );
919 if( ( pi_cur[4] & 0xC0 ) == 0x40 )
924 else if( ( pi_cur[4] & 0xF0 ) == 0x20 )
931 intf_ErrMsg( "Unable to determine stream type" );
935 pp_packets[i_packet] = pp_data[i_iovec];
939 (*pp_data[i_iovec]->pi_refcount)++;
941 pp_packets[i_packet]->pi_refcount = pp_data[i_iovec]->pi_refcount;
943 pp_packets[i_packet]->p_buffer = pp_data[i_iovec]->p_buffer;
945 pp_packets[i_packet]->p_payload_start =
946 pp_packets[i_packet]->p_buffer + i_pos;
948 pp_packets[i_packet]->p_payload_end =
949 pp_packets[i_packet]->p_payload_start + i_packet_size + 6;
951 pp_packets[i_packet]->p_next = NULL;
952 pp_packets[i_packet]->b_discard_payload = 0;
955 i_pos += i_packet_size + 6;
959 pp_packets[i_packet] = NULL;
961 vlc_mutex_lock( &p_input->stream.stream_lock );
963 p_input->stream.p_selected_area->i_tell += i_read_bytes;
964 b_eot = !( p_input->stream.p_selected_area->i_tell < p_dvd->i_size );
965 b_eof = b_eot && ( ( p_dvd->i_title + 1 ) >= p_input->stream.i_area_nb );
967 vlc_mutex_unlock( &p_input->stream.stream_lock );
976 intf_WarnMsg( 4, "dvd info: new title" );
978 vlc_mutex_lock( &p_input->stream.stream_lock );
979 DVDSetArea( p_input, p_input->stream.pp_areas[p_dvd->i_title] );
980 vlc_mutex_unlock( &p_input->stream.stream_lock );
984 if( i_read_blocks == i_block_once )
992 /*****************************************************************************
993 * DVDRewind : reads a stream backward
994 *****************************************************************************/
995 static int DVDRewind( input_thread_t * p_input )
1000 /*****************************************************************************
1001 * DVDSeek : Goes to a given position on the stream.
1002 *****************************************************************************
1003 * This one is used by the input and translate chronological position from
1004 * input to logical position on the device.
1005 * The lock should be taken before calling this function.
1006 *****************************************************************************/
1007 static void DVDSeek( input_thread_t * p_input, off_t i_off )
1009 thread_dvd_data_t * p_dvd;
1016 p_dvd = ( thread_dvd_data_t * )p_input->p_plugin_data;
1018 /* we have to take care of offset of beginning of title */
1019 i_pos = i_off + p_input->stream.p_selected_area->i_start
1020 - p_dvd->i_title_start;
1022 /* update navigation data */
1023 p_dvd->i_sector = i_pos >> 11;
1028 /* parse vobu address map to find program cell */
1029 while( title.p_cell_play[i_prg_cell].i_end_sector < p_dvd->i_sector )
1034 p_dvd->i_prg_cell = i_prg_cell;
1036 if( DVDChooseAngle( p_dvd ) < 0 )
1038 p_input->b_error = 1;
1044 /* Find first title cell which is inside program cell */
1045 if( DVDFindCell( p_dvd ) < 0 )
1047 /* no following cell : we're at eof */
1048 intf_ErrMsg( "dvd error: cell seeking failed" );
1049 p_input->b_error = 1;
1053 i_cell = p_dvd->i_cell;
1055 #define cell p_dvd->p_ifo->vts.cell_inf.p_cell_map[i_cell]
1056 /* parse cell address map to find title cell containing sector */
1057 while( cell.i_end_sector < p_dvd->i_sector )
1062 p_dvd->i_cell = i_cell;
1064 /* if we're inside a multi-angle zone, we have to choose i_sector
1065 * in the current angle ; we can't do it all the time since cells
1066 * can be very wide out of such zones */
1067 if( title.p_cell_play[p_dvd->i_prg_cell].i_category & 0xf000 )
1069 p_dvd->i_sector = MAX(
1070 cell.i_start_sector,
1071 title.p_cell_play[p_dvd->i_prg_cell].i_start_sector );
1074 p_dvd->i_end_sector = MIN(
1076 title.p_cell_play[p_dvd->i_prg_cell].i_end_sector );
1078 /* update chapter */
1079 if( title.p_cell_play[p_dvd->i_prg_cell].i_category & 0xf000 )
1081 i_angle = p_dvd->i_angle - 1;
1087 while( ( title.chapter_map.pi_start_cell[i_chapter] <=
1088 ( p_dvd->i_prg_cell - i_angle + 1 ) ) &&
1089 ( i_chapter < ( p_dvd->i_chapter_nb - 1 ) ) )
1094 p_dvd->i_chapter = i_chapter;
1095 p_input->stream.p_selected_area->i_part = p_dvd->i_chapter;
1097 p_input->stream.p_selected_area->i_tell =
1098 lseek( p_dvd->i_fd, p_dvd->i_title_start +
1099 (off_t)( p_dvd->i_sector ) *DVD_LB_SIZE, SEEK_SET ) -
1100 p_input->stream.p_selected_area->i_start;
1102 intf_WarnMsg( 3, "Program Cell: %d Cell: %d Chapter: %d",
1103 p_dvd->i_prg_cell, p_dvd->i_cell, p_dvd->i_chapter );
1109 #define cell p_dvd->p_ifo->vts.cell_inf
1111 /*****************************************************************************
1112 * DVDFindCell: adjust the title cell index with the program cell
1113 *****************************************************************************/
1114 static int DVDFindCell( thread_dvd_data_t * p_dvd )
1119 i_cell = p_dvd->i_cell;
1120 i_index = p_dvd->i_prg_cell;
1122 if( i_cell >= cell.i_cell_nb )
1127 while( ( ( title.p_cell_pos[i_index].i_vob_id !=
1128 cell.p_cell_map[i_cell].i_vob_id ) ||
1129 ( title.p_cell_pos[i_index].i_cell_id !=
1130 cell.p_cell_map[i_cell].i_cell_id ) ) &&
1131 ( i_cell < cell.i_cell_nb - 1 ) )
1137 intf_WarnMsg( 3, "FindCell: i_cell %d i_index %d found %d nb %d",
1144 p_dvd->i_cell = i_cell;
1151 /*****************************************************************************
1152 * DVDFindSector: find cell index in adress map from index in
1153 * information table program map and give corresponding sectors.
1154 *****************************************************************************/
1155 static int DVDFindSector( thread_dvd_data_t * p_dvd )
1158 if( p_dvd->i_sector > title.p_cell_play[p_dvd->i_prg_cell].i_end_sector )
1160 p_dvd->i_prg_cell++;
1162 if( DVDChooseAngle( p_dvd ) < 0 )
1168 if( DVDFindCell( p_dvd ) < 0 )
1170 intf_ErrMsg( "dvd error: can't find sector" );
1174 /* Find start and end sectors of new cell */
1176 p_dvd->i_sector = MAX(
1177 p_dvd->p_ifo->vts.cell_inf.p_cell_map[p_dvd->i_cell].i_start_sector,
1178 title.p_cell_play[p_dvd->i_prg_cell].i_start_sector );
1179 p_dvd->i_end_sector = MIN(
1180 p_dvd->p_ifo->vts.cell_inf.p_cell_map[p_dvd->i_cell].i_end_sector,
1181 title.p_cell_play[p_dvd->i_prg_cell].i_end_sector );
1183 p_dvd->i_sector = title.p_cell_play[p_dvd->i_prg_cell].i_start_sector;
1184 p_dvd->i_end_sector = title.p_cell_play[p_dvd->i_prg_cell].i_end_sector;
1188 intf_WarnMsg( 3, "cell: %d sector1: 0x%x end1: 0x%x\n"
1189 "index: %d sector2: 0x%x end2: 0x%x\n"
1190 "category: 0x%x ilvu end: 0x%x vobu start 0x%x",
1192 p_dvd->p_ifo->vts.cell_inf.p_cell_map[p_dvd->i_cell].i_start_sector,
1193 p_dvd->p_ifo->vts.cell_inf.p_cell_map[p_dvd->i_cell].i_end_sector,
1195 title.p_cell_play[p_dvd->i_prg_cell].i_start_sector,
1196 title.p_cell_play[p_dvd->i_prg_cell].i_end_sector,
1197 title.p_cell_play[p_dvd->i_prg_cell].i_category,
1198 title.p_cell_play[p_dvd->i_prg_cell].i_first_ilvu_vobu_esector,
1199 title.p_cell_play[p_dvd->i_prg_cell].i_last_vobu_start_sector );
1205 /*****************************************************************************
1206 * DVDChapterSelect: find the cell corresponding to requested chapter
1207 *****************************************************************************/
1208 static int DVDChapterSelect( thread_dvd_data_t * p_dvd, int i_chapter )
1211 /* Find cell index in Program chain for current chapter */
1212 p_dvd->i_prg_cell = title.chapter_map.pi_start_cell[i_chapter-1] - 1;
1214 p_dvd->i_sector = 0;
1216 DVDChooseAngle( p_dvd );
1218 /* Search for cell_index in cell adress_table and initialize
1220 if( DVDFindSector( p_dvd ) < 0 )
1222 intf_ErrMsg( "dvd error: can't select chapter" );
1226 /* start is : beginning of vts vobs + offset to vob x */
1227 p_dvd->i_start = p_dvd->i_title_start +
1228 DVD_LB_SIZE * (off_t)( p_dvd->i_sector );
1230 /* Position the fd pointer on the right address */
1231 p_dvd->i_start = lseek( p_dvd->i_fd, p_dvd->i_start, SEEK_SET );
1233 p_dvd->i_chapter = i_chapter;
1237 /*****************************************************************************
1238 * DVDChooseAngle: select the cell corresponding to the selected angle
1239 *****************************************************************************/
1240 static int DVDChooseAngle( thread_dvd_data_t * p_dvd )
1242 /* basic handling of angles */
1243 switch( ( ( title.p_cell_play[p_dvd->i_prg_cell].i_category & 0xf000 )
1246 /* we enter a muli-angle section */
1248 p_dvd->i_prg_cell += p_dvd->i_angle - 1;
1249 p_dvd->i_angle_cell = 0;
1251 /* we exit a multi-angle section */
1254 p_dvd->i_prg_cell += p_dvd->i_angle_nb - p_dvd->i_angle;