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.60 2001/05/19 00:39:30 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 /*****************************************************************************
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 = input_FileOpen;
114 input.pf_close = input_FileClose;
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;
195 p_dvd->i_fd = p_input->i_handle;
197 /* reading several block once seems to cause lock-up
198 * when using input_ToggleES
199 * who wrote thez damn buggy piece of shit ??? --stef */
200 p_dvd->i_block_once = 32;
201 p_input->i_read_once = 128;
203 i = CSSTest( p_input->i_handle );
207 intf_ErrMsg( "dvd error: error in css" );
209 p_input->b_error = 1;
213 p_dvd->b_encrypted = i;
215 lseek( p_input->i_handle, 0, SEEK_SET );
217 /* Reading structures initialisation */
218 p_input->p_method_data =
219 DVDNetlistInit( 2048, 4096, 2048, 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 p_input->b_error = 1;
230 if( IfoInit( p_dvd->p_ifo ) < 0 )
232 intf_ErrMsg( "dvd error: fatal failure in ifo" );
234 p_input->b_error = 1;
238 /* CSS initialisation */
239 if( p_dvd->b_encrypted )
241 p_dvd->p_css = malloc( sizeof(css_t) );
242 if( p_dvd->p_css == NULL )
244 intf_ErrMsg( "dvd error: couldn't create css structure" );
246 p_input->b_error = 1;
250 p_dvd->p_css->i_agid = 0;
252 if( CSSInit( p_input->i_handle, p_dvd->p_css ) < 0 )
254 intf_ErrMsg( "dvd error: fatal failure in css" );
255 free( p_dvd->p_css );
257 p_input->b_error = 1;
261 intf_WarnMsg( 2, "dvd info: css initialized" );
264 /* Set stream and area data */
265 vlc_mutex_lock( &p_input->stream.stream_lock );
267 /* Initialize ES structures */
268 input_InitStream( p_input, sizeof( stream_ps_data_t ) );
270 /* disc input method */
271 p_input->stream.i_method = INPUT_METHOD_DVD;
273 #define title_inf p_dvd->p_ifo->vmg.title_inf
274 intf_WarnMsg( 2, "dvd info: number of titles: %d", title_inf.i_title_nb );
276 #define area p_input->stream.pp_areas
277 /* We start from 1 here since the default area 0
278 * is reserved for video_ts.vob */
279 for( i = 1 ; i <= title_inf.i_title_nb ; i++ )
281 input_AddArea( p_input );
283 /* Titles are Program Chains */
286 /* Absolute start offset and size
287 * We can only set that with vts ifo, so we do it during the
288 * first call to DVDSetArea */
289 area[i]->i_start = 0;
292 /* Number of chapters */
293 area[i]->i_part_nb = title_inf.p_attr[i-1].i_chapter_nb;
296 /* Number of angles */
297 area[i]->i_angle_nb = 0;
298 area[i]->i_angle = 1;
300 /* Offset to vts_i_0.ifo */
301 area[i]->i_plugin_data = p_dvd->p_ifo->i_off +
302 ( title_inf.p_attr[i-1].i_start_sector * DVD_LB_SIZE );
306 /* Get requested title - if none try the first title */
307 i_title = main_GetIntVariable( INPUT_TITLE_VAR, 1 );
308 if( i_title <= 0 || i_title > title_inf.i_title_nb )
315 /* Get requested chapter - if none defaults to first one */
316 i_chapter = main_GetIntVariable( INPUT_CHAPTER_VAR, 1 );
322 p_input->stream.pp_areas[i_title]->i_part = i_chapter;
324 p_area = p_input->stream.pp_areas[i_title];
326 /* set title, chapter, audio and subpic */
327 DVDSetArea( p_input, p_area );
329 vlc_mutex_unlock( &p_input->stream.stream_lock );
334 /*****************************************************************************
335 * DVDEnd: frees unused data
336 *****************************************************************************/
337 static void DVDEnd( input_thread_t * p_input )
339 thread_dvd_data_t * p_dvd;
340 dvd_netlist_t * p_netlist;
342 p_dvd = (thread_dvd_data_t*)p_input->p_plugin_data;
343 p_netlist = (dvd_netlist_t *)p_input->p_method_data;
345 if( p_dvd->b_encrypted )
347 free( p_dvd->p_css );
350 IfoDestroy( p_dvd->p_ifo );
352 DVDNetlistEnd( p_netlist );
355 /*****************************************************************************
356 * DVDSetArea: initialize input data for title x, chapter y.
357 * It should be called for each user navigation request.
358 *****************************************************************************
359 * Take care that i_title starts from 0 (vmg) and i_chapter start from 1.
360 * Note that you have to take the lock before entering here.
361 *****************************************************************************/
362 static int DVDSetArea( input_thread_t * p_input, input_area_t * p_area )
364 thread_dvd_data_t * p_dvd;
365 es_descriptor_t * p_es;
373 p_dvd = (thread_dvd_data_t*)p_input->p_plugin_data;
375 /* we can't use the interface slider until initilization is complete */
376 p_input->stream.b_seekable = 0;
378 if( p_area != p_input->stream.p_selected_area )
382 * We have to load all title information
384 /* Change the default area */
385 p_input->stream.p_selected_area =
386 p_input->stream.pp_areas[p_area->i_id];
388 /* release the lock to to let the interface go */
389 // vlc_mutex_unlock( &p_input->stream.stream_lock );
391 /* title number: it is not vts nb!,
392 * it is what appears in the interface list */
393 p_dvd->i_title = p_area->i_id;
394 p_dvd->p_ifo->i_title = p_dvd->i_title;
396 /* set number of chapters of current title */
397 p_dvd->i_chapter_nb = p_area->i_part_nb;
400 if( IfoTitleSet( p_dvd->p_ifo ) < 0 )
402 intf_ErrMsg( "dvd error: fatal error in vts ifo" );
404 p_input->b_error = 1;
408 #define vmg p_dvd->p_ifo->vmg
409 #define vts p_dvd->p_ifo->vts
410 /* title position inside the selected vts */
411 i_vts_title = vmg.title_inf.p_attr[p_dvd->i_title-1].i_title_num;
413 vts.title_inf.p_title_start[i_vts_title-1].i_title_id;
415 intf_WarnMsg( 3, "dvd: title %d vts_title %d pgc %d",
420 /* css title key for current vts */
421 if( p_dvd->b_encrypted )
423 /* this one is vts number */
424 p_dvd->p_css->i_title =
425 vmg.title_inf.p_attr[p_dvd->i_title-1].i_title_set_num;
426 p_dvd->p_css->i_title_pos =
428 vts.manager_inf.i_title_vob_start_sector * DVD_LB_SIZE;
430 j = CSSGetKey( p_input->i_handle, p_dvd->p_css );
433 intf_ErrMsg( "dvd error: fatal error in vts css key" );
435 p_input->b_error = 1;
440 intf_ErrMsg( "dvd error: css decryption unavailable" );
442 p_input->b_error = 1;
450 p_dvd->i_angle_nb = vmg.title_inf.p_attr[p_dvd->i_title-1].i_angle_nb;
451 p_dvd->i_angle = main_GetIntVariable( INPUT_ANGLE_VAR, 1 );
452 if( ( p_dvd->i_angle <= 0 ) || p_dvd->i_angle > p_dvd->i_angle_nb )
458 * Set selected title start and size
461 /* title set offset */
462 p_dvd->i_title_start = vts.i_pos + DVD_LB_SIZE *
463 (off_t)( vts.manager_inf.i_title_vob_start_sector );
465 /* last video cell */
467 p_dvd->i_prg_cell = -1 +
468 vts.title_unit.p_title[p_dvd->i_title_id-1].title.i_cell_nb;
470 if( DVDFindCell( p_dvd ) < 0 )
472 intf_ErrMsg( "dvd error: can't find title end" );
473 p_input->b_error = 1;
477 /* temporary hack to fix size in some dvds */
478 if( p_dvd->i_cell >= vts.cell_inf.i_cell_nb )
480 p_dvd->i_cell = vts.cell_inf.i_cell_nb - 1;
484 p_dvd->i_size = DVD_LB_SIZE *
485 (off_t)( vts.cell_inf.p_cell_map[p_dvd->i_cell].i_end_sector );
486 intf_WarnMsg( 2, "dvd info: stream size 1: %lld @ %d", p_dvd->i_size,
487 vts.cell_inf.p_cell_map[p_dvd->i_cell].i_end_sector );
489 if( DVDChapterSelect( p_dvd, 1 ) < 0 )
491 intf_ErrMsg( "dvd error: can't find first chapter" );
492 p_input->b_error = 1;
496 p_dvd->i_size -= (off_t)( p_dvd->i_sector + 1 ) *DVD_LB_SIZE;
498 IfoPrintTitle( p_dvd );
500 // vlc_mutex_lock( &p_input->stream.stream_lock );
502 /* Area definition */
503 p_input->stream.p_selected_area->i_start = p_dvd->i_start;
504 p_input->stream.p_selected_area->i_size = p_dvd->i_size;
505 p_input->stream.p_selected_area->i_angle_nb = p_dvd->i_angle_nb;
506 p_input->stream.p_selected_area->i_angle = p_dvd->i_angle;
509 * Destroy obsolete ES by reinitializing program 0
510 * and find all ES in title with ifo data
512 if( p_input->stream.pp_programs != NULL )
514 /* We don't use input_EndStream here since
515 * we keep area structures */
517 for( i = 0 ; i < p_input->stream.i_selected_es_number ; i++ )
519 input_UnselectES( p_input, p_input->stream.pp_selected_es[i] );
522 input_DelProgram( p_input, p_input->stream.pp_programs[0] );
524 p_input->stream.pp_selected_es = NULL;
525 p_input->stream.i_selected_es_number = 0;
528 input_AddProgram( p_input, 0, sizeof( stream_ps_data_t ) );
530 /* No PSM to read in DVD mode, we already have all information */
531 p_input->stream.pp_programs[0]->b_is_ok = 1;
532 p_input->stream.pp_programs[0]->i_synchro_state = SYNCHRO_START;
536 /* ES 0 -> video MPEG2 */
537 IfoPrintVideo( p_dvd );
539 p_es = input_AddES( p_input, p_input->stream.pp_programs[0], 0xe0, 0 );
540 p_es->i_stream_id = 0xe0;
541 p_es->i_type = MPEG2_VIDEO_ES;
542 p_es->i_cat = VIDEO_ES;
543 intf_WarnMsg( 1, "dvd info: video mpeg2 stream" );
544 if( p_main->b_video )
546 input_SelectES( p_input, p_es );
548 intf_WarnMsg( 4, "dvd info: video selected" );
550 #define audio_status \
551 vts.title_unit.p_title[p_dvd->i_title_id-1].title.pi_audio_status[i-1]
552 /* Audio ES, in the order they appear in .ifo */
553 for( i = 1 ; i <= vts.manager_inf.i_audio_nb ; i++ )
555 IfoPrintAudio( p_dvd, i );
557 /* audio channel is active if first byte is 0x80 */
558 if( audio_status.i_available )
560 switch( vts.manager_inf.p_audio_attr[i-1].i_coding_mode )
563 i_id = ( ( 0x80 + audio_status.i_position ) << 8 ) | 0xbd;
564 p_es = input_AddES( p_input,
565 p_input->stream.pp_programs[0], i_id, 0 );
566 p_es->i_stream_id = 0xbd;
567 p_es->i_type = AC3_AUDIO_ES;
569 p_es->i_cat = AUDIO_ES;
570 strcpy( p_es->psz_desc, IfoLanguage( hton16(
571 vts.manager_inf.p_audio_attr[i-1].i_lang_code ) ) );
572 strcat( p_es->psz_desc, " (ac3)" );
574 intf_WarnMsg( 3, "dvd info: audio stream %d %s\t(0x%x)",
575 i, p_es->psz_desc, i_id );
579 case 0x03: /* MPEG audio */
580 i_id = 0xc0 + audio_status.i_position;
581 p_es = input_AddES( p_input,
582 p_input->stream.pp_programs[0], i_id, 0 );
583 p_es->i_stream_id = i_id;
584 p_es->i_type = MPEG2_AUDIO_ES;
586 p_es->i_cat = AUDIO_ES;
587 strcpy( p_es->psz_desc, IfoLanguage( hton16(
588 vts.manager_inf.p_audio_attr[i-1].i_lang_code ) ) );
589 strcat( p_es->psz_desc, " (mpeg)" );
591 intf_WarnMsg( 3, "dvd info: audio stream %d %s\t(0x%x)",
592 i, p_es->psz_desc, i_id );
595 case 0x04: /* LPCM */
597 i_id = ( ( 0xa0 + audio_status.i_position ) << 8 ) | 0xbd;
598 p_es = input_AddES( p_input,
599 p_input->stream.pp_programs[0], i_id, 0 );
600 p_es->i_stream_id = i_id;
601 p_es->i_type = LPCM_AUDIO_ES;
603 p_es->i_cat = AUDIO_ES;
604 strcpy( p_es->psz_desc, IfoLanguage( hton16(
605 vts.manager_inf.p_audio_attr[i-1].i_lang_code ) ) );
606 strcat( p_es->psz_desc, " (lpcm)" );
608 intf_WarnMsg( 3, "dvd info: audio stream %d %s\t(0x%x)",
609 i, p_es->psz_desc, i_id );
612 i_id = ( ( 0x88 + audio_status.i_position ) << 8 ) | 0xbd;
613 intf_ErrMsg( "dvd warning: DTS audio not handled yet"
618 intf_ErrMsg( "dvd warning: unknown audio type %.2x",
619 vts.manager_inf.p_audio_attr[i-1].i_coding_mode );
625 vts.title_unit.p_title[p_dvd->i_title_id-1].title.pi_spu_status[i-1]
629 for( i = 1 ; i <= vts.manager_inf.i_spu_nb; i++ )
631 IfoPrintSpu( p_dvd, i );
633 if( spu_status.i_available )
635 /* there are several streams for one spu */
636 if( vts.manager_inf.video_attr.i_ratio )
639 switch( vts.manager_inf.video_attr.i_perm_displ )
642 i_id = ( ( 0x20 + spu_status.i_position_pan ) << 8 )
646 i_id = ( ( 0x20 + spu_status.i_position_letter ) << 8 )
650 i_id = ( ( 0x20 + spu_status.i_position_wide ) << 8 )
658 i_id = ( ( 0x20 + spu_status.i_position_43 ) << 8 )
661 p_es = input_AddES( p_input,
662 p_input->stream.pp_programs[0], i_id, 0 );
663 p_es->i_stream_id = 0xbd;
664 p_es->i_type = DVD_SPU_ES;
665 p_es->i_cat = SPU_ES;
666 strcpy( p_es->psz_desc, IfoLanguage( hton16(
667 vts.manager_inf.p_spu_attr[i-1].i_lang_code ) ) );
668 intf_WarnMsg( 3, "dvd info: spu stream %d %s\t(0x%x)",
669 i, p_es->psz_desc, i_id );
673 if( p_main->b_audio )
675 /* For audio: first one if none or a not existing one specified */
676 i_audio = main_GetIntVariable( INPUT_CHANNEL_VAR, 1 );
677 if( i_audio < 0 || i_audio > vts.manager_inf.i_audio_nb )
679 main_PutIntVariable( INPUT_CHANNEL_VAR, 1 );
682 if( i_audio > 0 && vts.manager_inf.i_audio_nb > 0 )
684 input_SelectES( p_input, p_input->stream.pp_es[i_audio] );
688 if( p_main->b_video )
690 /* for spu, default is none */
691 i_spu = main_GetIntVariable( INPUT_SUBTITLE_VAR, 0 );
692 if( i_spu < 0 || i_spu > vts.manager_inf.i_spu_nb )
694 main_PutIntVariable( INPUT_SUBTITLE_VAR, 0 );
697 if( i_spu > 0 && vts.manager_inf.i_spu_nb > 0 )
699 i_spu += vts.manager_inf.i_audio_nb;
700 input_SelectES( p_input, p_input->stream.pp_es[i_spu] );
706 p_area = p_input->stream.p_selected_area;
716 if( p_area->i_part != p_dvd->i_chapter )
718 if( ( p_area->i_part > 0 ) &&
719 ( p_area->i_part <= p_area->i_part_nb ))
721 if( DVDChapterSelect( p_dvd, p_area->i_part ) < 0 )
723 intf_ErrMsg( "dvd error: can't set chapter in area" );
724 p_input->b_error = 1;
728 p_input->stream.p_selected_area->i_tell = p_dvd->i_start -
730 p_input->stream.p_selected_area->i_part = p_dvd->i_chapter;
732 intf_WarnMsg( 2, "dvd info: chapter %d start at: %lld",
733 p_area->i_part, p_area->i_tell );
738 p_dvd->i_chapter = 1;
743 p_dvd->p_ifo->vts.title_unit.p_title[p_dvd->i_title_id-1].title
744 if( p_area->i_angle != p_dvd->i_angle )
746 if( title.p_cell_play[p_dvd->i_prg_cell].i_category & 0xf000 )
748 if( ( p_area->i_angle - p_dvd->i_angle ) < 0 )
752 p_dvd->i_prg_cell += ( p_area->i_angle - p_dvd->i_angle );
753 p_dvd->i_angle = p_area->i_angle;
755 DVDFindSector( p_dvd );
756 p_dvd->i_cell += p_dvd->i_angle_cell;
760 p_dvd->i_angle = p_area->i_angle;
763 intf_WarnMsg( 2, "dvd info: angle %d selected", p_area->i_angle );
766 /* warn interface that something has changed */
767 p_input->stream.b_seekable = 1;
768 p_input->stream.b_changed = 1;
770 p_input->stream.pp_programs[0]->i_synchro_state = SYNCHRO_REINIT;
776 /*****************************************************************************
777 * DVDRead: reads data packets into the netlist.
778 *****************************************************************************
779 * Returns -1 in case of error, 0 if everything went well, and 1 in case of
781 *****************************************************************************/
782 static int DVDRead( input_thread_t * p_input,
783 data_packet_t ** pp_packets )
785 thread_dvd_data_t * p_dvd;
786 dvd_netlist_t * p_netlist;
787 struct iovec * p_vec;
788 struct data_packet_s * pp_data[p_input->i_read_once];
801 p_dvd = (thread_dvd_data_t *)p_input->p_plugin_data;
802 p_netlist = (dvd_netlist_t *)p_input->p_method_data;
804 /* Get an iovec pointer */
805 if( ( p_vec = DVDGetiovec( p_netlist ) ) == NULL )
807 intf_ErrMsg( "dvd error: can't get iovec" );
811 i_block_once = p_dvd->i_end_sector - p_dvd->i_sector + 1;
813 /* Get the position of the next cell if we're at cell end */
814 if( i_block_once <= 0 )
819 p_dvd->i_angle_cell++;
821 /* Find cell index in adress map */
822 if( DVDFindSector( p_dvd ) < 0 )
824 pp_packets[0] = NULL;
825 intf_ErrMsg( "dvd error: can't find next cell" );
829 /* Position the fd pointer on the right address */
830 i_off = lseek( p_dvd->i_fd,
831 p_dvd->i_title_start +
832 (off_t)( p_dvd->i_sector ) *DVD_LB_SIZE, SEEK_SET );
834 /* update chapter : it will be easier when we have navigation
836 if( p_dvd->i_chapter < ( p_dvd->i_chapter_nb - 1 ) )
838 if( title.p_cell_play[p_dvd->i_prg_cell].i_category & 0xf000 )
840 i_angle = p_dvd->i_angle - 1;
846 if( title.chapter_map.pi_start_cell[p_dvd->i_chapter] <=
847 ( p_dvd->i_prg_cell - i_angle + 1 ) )
853 vlc_mutex_lock( &p_input->stream.stream_lock );
855 p_input->stream.p_selected_area->i_tell = i_off -
856 p_input->stream.p_selected_area->i_start;
857 p_input->stream.p_selected_area->i_part = p_dvd->i_chapter;
859 /* the synchro has to be reinitialized when we change cell */
860 p_input->stream.pp_programs[0]->i_synchro_state = SYNCHRO_REINIT;
862 vlc_mutex_unlock( &p_input->stream.stream_lock );
864 i_block_once = p_dvd->i_end_sector - p_dvd->i_sector + 1;
867 /* the number of blocks read is the maw between the requested
868 * value and the leaving block in the cell */
869 if( i_block_once > p_dvd->i_block_once )
871 i_block_once = p_dvd->i_block_once;
873 //intf_WarnMsg( 2, "Sector: 0x%x Read: %d Chapter: %d", p_dvd->i_sector, i_block_once, p_dvd->i_chapter );
875 p_netlist->i_read_once = i_block_once;
878 i_read_bytes = readv( p_dvd->i_fd, p_vec, i_block_once );
879 i_read_blocks = ( i_read_bytes + 0x7ff ) >> 11;
881 /* Update netlist indexes */
882 DVDMviovec( p_netlist, i_read_blocks, pp_data );
884 /* Update global position */
885 p_dvd->i_sector += i_read_blocks;
889 /* Read headers to compute payload length */
890 for( i_iovec = 0 ; i_iovec < i_read_blocks ; i_iovec++ )
892 if( p_dvd->b_encrypted )
894 CSSDescrambleSector( p_dvd->p_css->pi_title_key,
895 p_vec[i_iovec].iov_base );
896 ((u8*)(p_vec[i_iovec].iov_base))[0x14] &= 0x8F;
901 while( i_pos < p_netlist->i_buffer_size )
903 pi_cur = (u8*)(p_vec[i_iovec].iov_base + i_pos);
906 if( U32_AT( pi_cur ) != 0x1BA )
908 /* That's the case for all packets, except pack header. */
909 i_packet_size = U16_AT( pi_cur + 4 );
910 pp_packets[i_packet] = DVDNewPtr( p_netlist );
915 if( ( pi_cur[4] & 0xC0 ) == 0x40 )
920 else if( ( pi_cur[4] & 0xF0 ) == 0x20 )
927 intf_ErrMsg( "Unable to determine stream type" );
931 pp_packets[i_packet] = pp_data[i_iovec];
935 (*pp_data[i_iovec]->pi_refcount)++;
937 pp_packets[i_packet]->pi_refcount = pp_data[i_iovec]->pi_refcount;
939 pp_packets[i_packet]->p_buffer = pp_data[i_iovec]->p_buffer;
941 pp_packets[i_packet]->p_payload_start =
942 pp_packets[i_packet]->p_buffer + i_pos;
944 pp_packets[i_packet]->p_payload_end =
945 pp_packets[i_packet]->p_payload_start + i_packet_size + 6;
947 pp_packets[i_packet]->p_next = NULL;
948 pp_packets[i_packet]->b_discard_payload = 0;
951 i_pos += i_packet_size + 6;
955 pp_packets[i_packet] = NULL;
957 vlc_mutex_lock( &p_input->stream.stream_lock );
959 p_input->stream.p_selected_area->i_tell += i_read_bytes;
960 b_eot = !( p_input->stream.p_selected_area->i_tell < p_dvd->i_size );
961 b_eof = b_eot && ( ( p_dvd->i_title + 1 ) >= p_input->stream.i_area_nb );
963 vlc_mutex_unlock( &p_input->stream.stream_lock );
972 intf_WarnMsg( 4, "dvd info: new title" );
974 vlc_mutex_lock( &p_input->stream.stream_lock );
975 DVDSetArea( p_input, p_input->stream.pp_areas[p_dvd->i_title] );
976 vlc_mutex_unlock( &p_input->stream.stream_lock );
980 if( i_read_blocks == i_block_once )
988 /*****************************************************************************
989 * DVDRewind : reads a stream backward
990 *****************************************************************************/
991 static int DVDRewind( input_thread_t * p_input )
996 /*****************************************************************************
997 * DVDSeek : Goes to a given position on the stream.
998 *****************************************************************************
999 * This one is used by the input and translate chronological position from
1000 * input to logical position on the device.
1001 * The lock should be taken before calling this function.
1002 *****************************************************************************/
1003 static void DVDSeek( input_thread_t * p_input, off_t i_off )
1005 thread_dvd_data_t * p_dvd;
1012 p_dvd = ( thread_dvd_data_t * )p_input->p_plugin_data;
1014 /* we have to take care of offset of beginning of title */
1015 i_pos = i_off + p_input->stream.p_selected_area->i_start
1016 - p_dvd->i_title_start;
1018 /* update navigation data */
1019 p_dvd->i_sector = i_pos >> 11;
1024 /* parse vobu address map to find program cell */
1025 while( title.p_cell_play[i_prg_cell].i_end_sector < p_dvd->i_sector )
1030 p_dvd->i_prg_cell = i_prg_cell;
1032 if( DVDChooseAngle( p_dvd ) < 0 )
1034 p_input->b_error = 1;
1040 /* Find first title cell which is inside program cell */
1041 if( DVDFindCell( p_dvd ) < 0 )
1043 /* no following cell : we're at eof */
1044 intf_ErrMsg( "dvd error: cell seeking failed" );
1045 p_input->b_error = 1;
1049 i_cell = p_dvd->i_cell;
1051 #define cell p_dvd->p_ifo->vts.cell_inf.p_cell_map[i_cell]
1052 /* parse cell address map to find title cell containing sector */
1053 while( cell.i_end_sector < p_dvd->i_sector )
1058 p_dvd->i_cell = i_cell;
1060 /* if we're inside a multi-angle zone, we have to choose i_sector
1061 * in the current angle ; we can't do it all the time since cells
1062 * can be very wide out of such zones */
1063 if( title.p_cell_play[p_dvd->i_prg_cell].i_category & 0xf000 )
1065 p_dvd->i_sector = MAX(
1066 cell.i_start_sector,
1067 title.p_cell_play[p_dvd->i_prg_cell].i_start_sector );
1070 p_dvd->i_end_sector = MIN(
1072 title.p_cell_play[p_dvd->i_prg_cell].i_end_sector );
1074 /* update chapter */
1075 if( title.p_cell_play[p_dvd->i_prg_cell].i_category & 0xf000 )
1077 i_angle = p_dvd->i_angle - 1;
1083 while( ( title.chapter_map.pi_start_cell[i_chapter] <=
1084 ( p_dvd->i_prg_cell - i_angle + 1 ) ) &&
1085 ( i_chapter < ( p_dvd->i_chapter_nb - 1 ) ) )
1090 p_dvd->i_chapter = i_chapter;
1091 p_input->stream.p_selected_area->i_part = p_dvd->i_chapter;
1093 p_input->stream.p_selected_area->i_tell =
1094 lseek( p_dvd->i_fd, p_dvd->i_title_start +
1095 (off_t)( p_dvd->i_sector ) *DVD_LB_SIZE, SEEK_SET ) -
1096 p_input->stream.p_selected_area->i_start;
1098 intf_WarnMsg( 3, "Program Cell: %d Cell: %d Chapter: %d",
1099 p_dvd->i_prg_cell, p_dvd->i_cell, p_dvd->i_chapter );
1105 #define cell p_dvd->p_ifo->vts.cell_inf
1107 /*****************************************************************************
1108 * DVDFindCell: adjust the title cell index with the program cell
1109 *****************************************************************************/
1110 static int DVDFindCell( thread_dvd_data_t * p_dvd )
1115 i_cell = p_dvd->i_cell;
1116 i_index = p_dvd->i_prg_cell;
1118 if( i_cell >= cell.i_cell_nb )
1123 while( ( ( title.p_cell_pos[i_index].i_vob_id !=
1124 cell.p_cell_map[i_cell].i_vob_id ) ||
1125 ( title.p_cell_pos[i_index].i_cell_id !=
1126 cell.p_cell_map[i_cell].i_cell_id ) ) &&
1127 ( i_cell < cell.i_cell_nb - 1 ) )
1133 intf_WarnMsg( 3, "FindCell: i_cell %d i_index %d found %d nb %d",
1140 p_dvd->i_cell = i_cell;
1147 /*****************************************************************************
1148 * DVDFindSector: find cell index in adress map from index in
1149 * information table program map and give corresponding sectors.
1150 *****************************************************************************/
1151 static int DVDFindSector( thread_dvd_data_t * p_dvd )
1154 if( p_dvd->i_sector > title.p_cell_play[p_dvd->i_prg_cell].i_end_sector )
1156 p_dvd->i_prg_cell++;
1158 if( DVDChooseAngle( p_dvd ) < 0 )
1164 if( DVDFindCell( p_dvd ) < 0 )
1166 intf_ErrMsg( "dvd error: can't find sector" );
1170 /* Find start and end sectors of new cell */
1172 p_dvd->i_sector = MAX(
1173 p_dvd->p_ifo->vts.cell_inf.p_cell_map[p_dvd->i_cell].i_start_sector,
1174 title.p_cell_play[p_dvd->i_prg_cell].i_start_sector );
1175 p_dvd->i_end_sector = MIN(
1176 p_dvd->p_ifo->vts.cell_inf.p_cell_map[p_dvd->i_cell].i_end_sector,
1177 title.p_cell_play[p_dvd->i_prg_cell].i_end_sector );
1179 p_dvd->i_sector = title.p_cell_play[p_dvd->i_prg_cell].i_start_sector;
1180 p_dvd->i_end_sector = title.p_cell_play[p_dvd->i_prg_cell].i_end_sector;
1184 intf_WarnMsg( 3, "cell: %d sector1: 0x%x end1: 0x%x\n"
1185 "index: %d sector2: 0x%x end2: 0x%x\n"
1186 "category: 0x%x ilvu end: 0x%x vobu start 0x%x",
1188 p_dvd->p_ifo->vts.cell_inf.p_cell_map[p_dvd->i_cell].i_start_sector,
1189 p_dvd->p_ifo->vts.cell_inf.p_cell_map[p_dvd->i_cell].i_end_sector,
1191 title.p_cell_play[p_dvd->i_prg_cell].i_start_sector,
1192 title.p_cell_play[p_dvd->i_prg_cell].i_end_sector,
1193 title.p_cell_play[p_dvd->i_prg_cell].i_category,
1194 title.p_cell_play[p_dvd->i_prg_cell].i_first_ilvu_vobu_esector,
1195 title.p_cell_play[p_dvd->i_prg_cell].i_last_vobu_start_sector );
1201 /*****************************************************************************
1202 * DVDChapterSelect: find the cell corresponding to requested chapter
1203 *****************************************************************************/
1204 static int DVDChapterSelect( thread_dvd_data_t * p_dvd, int i_chapter )
1207 /* Find cell index in Program chain for current chapter */
1208 p_dvd->i_prg_cell = title.chapter_map.pi_start_cell[i_chapter-1] - 1;
1210 p_dvd->i_sector = 0;
1212 DVDChooseAngle( p_dvd );
1214 /* Search for cell_index in cell adress_table and initialize
1216 if( DVDFindSector( p_dvd ) < 0 )
1218 intf_ErrMsg( "dvd error: can't select chapter" );
1222 /* start is : beginning of vts vobs + offset to vob x */
1223 p_dvd->i_start = p_dvd->i_title_start +
1224 DVD_LB_SIZE * (off_t)( p_dvd->i_sector );
1226 /* Position the fd pointer on the right address */
1227 p_dvd->i_start = lseek( p_dvd->i_fd, p_dvd->i_start, SEEK_SET );
1229 p_dvd->i_chapter = i_chapter;
1233 /*****************************************************************************
1234 * DVDChooseAngle: select the cell corresponding to the selected angle
1235 *****************************************************************************/
1236 static int DVDChooseAngle( thread_dvd_data_t * p_dvd )
1238 /* basic handling of angles */
1239 switch( ( ( title.p_cell_play[p_dvd->i_prg_cell].i_category & 0xf000 )
1242 /* we enter a muli-angle section */
1244 p_dvd->i_prg_cell += p_dvd->i_angle - 1;
1245 p_dvd->i_angle_cell = 0;
1247 /* we exit a multi-angle section */
1250 p_dvd->i_prg_cell += p_dvd->i_angle_nb - p_dvd->i_angle;