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.64 2001/05/31 03:12:49 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
43 #include "modules_inner.h"
53 # include <netinet/in.h>
57 #include <sys/types.h>
61 #ifdef STRNCASECMP_IN_STRINGS_H
69 # include <sys/uio.h> /* struct iovec */
82 #include "stream_control.h"
83 #include "input_ext-intf.h"
84 #include "input_ext-dec.h"
88 #include "dvd_netlist.h"
91 #include "dvd_summary.h"
92 #include "input_dvd.h"
93 #include "mpeg_system.h"
99 /*****************************************************************************
101 *****************************************************************************/
102 /* called from outside */
103 static int DVDProbe ( probedata_t *p_data );
104 static void DVDInit ( struct input_thread_s * );
105 static void DVDEnd ( struct input_thread_s * );
106 static int DVDSetArea ( struct input_thread_s *, struct input_area_s * );
107 static int DVDRead ( struct input_thread_s *, data_packet_t ** );
108 static void DVDSeek ( struct input_thread_s *, off_t );
109 static int DVDRewind ( struct input_thread_s * );
111 /* called only inside */
112 static int DVDChooseAngle( thread_dvd_data_t * );
113 static int DVDFindCell( thread_dvd_data_t * );
114 static int DVDFindSector( thread_dvd_data_t * );
115 static int DVDChapterSelect( thread_dvd_data_t *, int );
117 /*****************************************************************************
118 * Functions exported as capabilities. They are declared as static so that
119 * we don't pollute the namespace too much.
120 *****************************************************************************/
121 void _M( input_getfunctions )( function_list_t * p_function_list )
123 #define input p_function_list->functions.input
124 p_function_list->pf_probe = DVDProbe;
125 input.pf_init = DVDInit;
126 input.pf_open = NULL; /* Set in DVDInit */
127 input.pf_close = NULL;
128 input.pf_end = DVDEnd;
129 input.pf_read = DVDRead;
130 input.pf_set_area = DVDSetArea;
131 input.pf_demux = input_DemuxPS;
132 input.pf_new_packet = DVDNewPacket;
133 input.pf_new_pes = DVDNewPES;
134 input.pf_delete_packet = DVDDeletePacket;
135 input.pf_delete_pes = DVDDeletePES;
136 input.pf_rewind = DVDRewind;
137 input.pf_seek = DVDSeek;
142 * Data reading functions
145 /*****************************************************************************
146 * DVDProbe: verifies that the stream is a PS stream
147 *****************************************************************************/
148 static int DVDProbe( probedata_t *p_data )
150 input_thread_t * p_input = (input_thread_t *)p_data;
152 char * psz_name = p_input->p_source;
159 if( TestMethod( INPUT_METHOD_VAR, "dvd" ) )
165 #endif /* HAVE_CSS */
168 if( ( strlen(psz_name) > 4 ) && !strncasecmp( psz_name, "dvd:", 4 ) )
170 /* If the user specified "dvd:" then it's probably a DVD */
175 #endif /* HAVE_CSS */
179 #if !defined( WIN32 )
180 i_handle = open( psz_name, 0 );
187 snprintf( buf, 7, "\\\\.\\%c:", psz_name[0] );
188 (HANDLE) i_handle = CreateFile( i_score < 90 ? psz_name : buf,
189 GENERIC_READ | GENERIC_WRITE,
190 FILE_SHARE_READ | FILE_SHARE_WRITE,
191 NULL, OPEN_EXISTING, 0, NULL );
192 if( (HANDLE) i_handle == INVALID_HANDLE_VALUE )
196 CloseHandle( (HANDLE) i_handle );
202 /*****************************************************************************
203 * DVDInit: initializes DVD structures
204 *****************************************************************************/
205 static void DVDInit( input_thread_t * p_input )
207 thread_dvd_data_t * p_dvd;
208 input_area_t * p_area;
213 p_dvd = malloc( sizeof(thread_dvd_data_t) );
216 intf_ErrMsg( "dvd error: out of memory" );
217 p_input->b_error = 1;
221 p_input->p_plugin_data = (void *)p_dvd;
222 p_input->p_method_data = NULL;
224 p_dvd->i_fd = p_input->i_handle;
226 /* reading several block once seems to cause lock-up
227 * when using input_ToggleES
228 * who wrote thez damn buggy piece of shit ??? --stef */
229 p_dvd->i_block_once = 32;
230 p_input->i_read_once = 128;
232 i = CSSTest( p_input->i_handle );
236 intf_ErrMsg( "dvd error: error in css" );
238 p_input->b_error = 1;
242 p_dvd->b_encrypted = i;
244 #if !defined( WIN32 )
245 lseek( p_input->i_handle, 0, SEEK_SET );
247 SetFilePointer( (HANDLE) p_input->i_handle, 0, 0, FILE_BEGIN );
250 /* Reading structures initialisation */
251 p_input->p_method_data =
252 DVDNetlistInit( 2048, 4096, 2048, DVD_LB_SIZE, p_dvd->i_block_once );
253 intf_WarnMsg( 2, "dvd info: netlist initialized" );
255 /* Ifo allocation & initialisation */
256 if( IfoCreate( p_dvd ) < 0 )
258 intf_ErrMsg( "dvd error: allcation error in ifo" );
259 p_input->b_error = 1;
263 if( IfoInit( p_dvd->p_ifo ) < 0 )
265 intf_ErrMsg( "dvd error: fatal failure in ifo" );
267 p_input->b_error = 1;
271 /* CSS initialisation */
272 if( p_dvd->b_encrypted )
274 p_dvd->p_css = malloc( sizeof(css_t) );
275 if( p_dvd->p_css == NULL )
277 intf_ErrMsg( "dvd error: couldn't create css structure" );
279 p_input->b_error = 1;
283 p_dvd->p_css->i_agid = 0;
285 if( CSSInit( p_input->i_handle, p_dvd->p_css ) < 0 )
287 intf_ErrMsg( "dvd error: fatal failure in css" );
288 free( p_dvd->p_css );
290 p_input->b_error = 1;
294 intf_WarnMsg( 2, "dvd info: css initialized" );
297 /* Set stream and area data */
298 vlc_mutex_lock( &p_input->stream.stream_lock );
300 /* Initialize ES structures */
301 input_InitStream( p_input, sizeof( stream_ps_data_t ) );
303 /* disc input method */
304 p_input->stream.i_method = INPUT_METHOD_DVD;
306 #define title_inf p_dvd->p_ifo->vmg.title_inf
307 intf_WarnMsg( 2, "dvd info: number of titles: %d", title_inf.i_title_nb );
309 #define area p_input->stream.pp_areas
310 /* We start from 1 here since the default area 0
311 * is reserved for video_ts.vob */
312 for( i = 1 ; i <= title_inf.i_title_nb ; i++ )
314 input_AddArea( p_input );
316 /* Titles are Program Chains */
319 /* Absolute start offset and size
320 * We can only set that with vts ifo, so we do it during the
321 * first call to DVDSetArea */
322 area[i]->i_start = 0;
325 /* Number of chapters */
326 area[i]->i_part_nb = title_inf.p_attr[i-1].i_chapter_nb;
329 /* Number of angles */
330 area[i]->i_angle_nb = 0;
331 area[i]->i_angle = 1;
333 /* Offset to vts_i_0.ifo */
334 area[i]->i_plugin_data = p_dvd->p_ifo->i_off +
335 ( title_inf.p_attr[i-1].i_start_sector * DVD_LB_SIZE );
339 /* Get requested title - if none try the first title */
340 i_title = main_GetIntVariable( INPUT_TITLE_VAR, 1 );
341 if( i_title <= 0 || i_title > title_inf.i_title_nb )
348 /* Get requested chapter - if none defaults to first one */
349 i_chapter = main_GetIntVariable( INPUT_CHAPTER_VAR, 1 );
355 p_input->stream.pp_areas[i_title]->i_part = i_chapter;
357 p_area = p_input->stream.pp_areas[i_title];
359 /* set title, chapter, audio and subpic */
360 DVDSetArea( p_input, p_area );
362 vlc_mutex_unlock( &p_input->stream.stream_lock );
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 if( p_dvd->b_encrypted )
380 free( p_dvd->p_css );
383 IfoDestroy( p_dvd->p_ifo );
385 DVDNetlistEnd( p_netlist );
388 /*****************************************************************************
389 * DVDSetArea: initialize input data for title x, chapter y.
390 * It should be called for each user navigation request.
391 *****************************************************************************
392 * Take care that i_title starts from 0 (vmg) and i_chapter start from 1.
393 * Note that you have to take the lock before entering here.
394 *****************************************************************************/
395 static int DVDSetArea( input_thread_t * p_input, input_area_t * p_area )
397 thread_dvd_data_t * p_dvd;
398 es_descriptor_t * p_es;
406 p_dvd = (thread_dvd_data_t*)p_input->p_plugin_data;
408 /* we can't use the interface slider until initilization is complete */
409 p_input->stream.b_seekable = 0;
411 if( p_area != p_input->stream.p_selected_area )
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 /* release the lock to to let the interface go */
422 // vlc_mutex_unlock( &p_input->stream.stream_lock );
424 /* title number: it is not vts nb!,
425 * it is what appears in the interface list */
426 p_dvd->i_title = p_area->i_id;
427 p_dvd->p_ifo->i_title = p_dvd->i_title;
429 /* set number of chapters of current title */
430 p_dvd->i_chapter_nb = p_area->i_part_nb;
433 if( IfoTitleSet( p_dvd->p_ifo ) < 0 )
435 intf_ErrMsg( "dvd error: fatal error in vts ifo" );
437 p_input->b_error = 1;
441 #define vmg p_dvd->p_ifo->vmg
442 #define vts p_dvd->p_ifo->vts
443 /* title position inside the selected vts */
444 i_vts_title = vmg.title_inf.p_attr[p_dvd->i_title-1].i_title_num;
446 vts.title_inf.p_title_start[i_vts_title-1].i_title_id;
448 intf_WarnMsg( 3, "dvd: title %d vts_title %d pgc %d",
453 /* css title key for current vts */
454 if( p_dvd->b_encrypted )
456 /* this one is vts number */
457 p_dvd->p_css->i_title =
458 vmg.title_inf.p_attr[p_dvd->i_title-1].i_title_set_num;
459 p_dvd->p_css->i_title_pos =
461 vts.manager_inf.i_title_vob_start_sector * DVD_LB_SIZE;
463 j = CSSGetKey( p_input->i_handle, p_dvd->p_css );
466 intf_ErrMsg( "dvd error: fatal error in vts css key" );
468 p_input->b_error = 1;
473 intf_ErrMsg( "dvd error: css decryption unavailable" );
475 p_input->b_error = 1;
483 p_dvd->i_angle_nb = vmg.title_inf.p_attr[p_dvd->i_title-1].i_angle_nb;
484 p_dvd->i_angle = main_GetIntVariable( INPUT_ANGLE_VAR, 1 );
485 if( ( p_dvd->i_angle <= 0 ) || p_dvd->i_angle > p_dvd->i_angle_nb )
491 * Set selected title start and size
494 /* title set offset */
495 p_dvd->i_title_start = vts.i_pos + DVD_LB_SIZE *
496 (off_t)( vts.manager_inf.i_title_vob_start_sector );
498 /* last video cell */
500 p_dvd->i_prg_cell = -1 +
501 vts.title_unit.p_title[p_dvd->i_title_id-1].title.i_cell_nb;
503 if( DVDFindCell( p_dvd ) < 0 )
505 intf_ErrMsg( "dvd error: can't find title end" );
506 p_input->b_error = 1;
510 /* temporary hack to fix size in some dvds */
511 if( p_dvd->i_cell >= vts.cell_inf.i_cell_nb )
513 p_dvd->i_cell = vts.cell_inf.i_cell_nb - 1;
517 p_dvd->i_size = DVD_LB_SIZE *
518 (off_t)( vts.cell_inf.p_cell_map[p_dvd->i_cell].i_end_sector );
519 intf_WarnMsg( 2, "dvd info: stream size 1: %lld @ %d", p_dvd->i_size,
520 vts.cell_inf.p_cell_map[p_dvd->i_cell].i_end_sector );
522 if( DVDChapterSelect( p_dvd, 1 ) < 0 )
524 intf_ErrMsg( "dvd error: can't find first chapter" );
525 p_input->b_error = 1;
529 p_dvd->i_size -= (off_t)( p_dvd->i_sector + 1 ) *DVD_LB_SIZE;
531 IfoPrintTitle( p_dvd );
533 // vlc_mutex_lock( &p_input->stream.stream_lock );
535 /* Area definition */
536 p_input->stream.p_selected_area->i_start = p_dvd->i_start;
537 p_input->stream.p_selected_area->i_size = p_dvd->i_size;
538 p_input->stream.p_selected_area->i_angle_nb = p_dvd->i_angle_nb;
539 p_input->stream.p_selected_area->i_angle = p_dvd->i_angle;
542 * Destroy obsolete ES by reinitializing program 0
543 * and find all ES in title with ifo data
545 if( p_input->stream.pp_programs != NULL )
547 /* We don't use input_EndStream here since
548 * we keep area structures */
550 for( i = 0 ; i < p_input->stream.i_selected_es_number ; i++ )
552 input_UnselectES( p_input, p_input->stream.pp_selected_es[i] );
555 input_DelProgram( p_input, p_input->stream.pp_programs[0] );
557 p_input->stream.pp_selected_es = NULL;
558 p_input->stream.i_selected_es_number = 0;
561 input_AddProgram( p_input, 0, sizeof( stream_ps_data_t ) );
563 /* No PSM to read in DVD mode, we already have all information */
564 p_input->stream.pp_programs[0]->b_is_ok = 1;
565 p_input->stream.pp_programs[0]->i_synchro_state = SYNCHRO_START;
569 /* ES 0 -> video MPEG2 */
570 IfoPrintVideo( p_dvd );
572 p_es = input_AddES( p_input, p_input->stream.pp_programs[0], 0xe0, 0 );
573 p_es->i_stream_id = 0xe0;
574 p_es->i_type = MPEG2_VIDEO_ES;
575 p_es->i_cat = VIDEO_ES;
576 intf_WarnMsg( 1, "dvd info: video mpeg2 stream" );
577 if( p_main->b_video )
579 input_SelectES( p_input, p_es );
581 intf_WarnMsg( 4, "dvd info: video selected" );
583 #define audio_status \
584 vts.title_unit.p_title[p_dvd->i_title_id-1].title.pi_audio_status[i-1]
585 /* Audio ES, in the order they appear in .ifo */
586 for( i = 1 ; i <= vts.manager_inf.i_audio_nb ; i++ )
588 IfoPrintAudio( p_dvd, i );
590 /* audio channel is active if first byte is 0x80 */
591 if( audio_status.i_available )
593 switch( vts.manager_inf.p_audio_attr[i-1].i_coding_mode )
596 i_id = ( ( 0x80 + audio_status.i_position ) << 8 ) | 0xbd;
597 p_es = input_AddES( p_input,
598 p_input->stream.pp_programs[0], i_id, 0 );
599 p_es->i_stream_id = 0xbd;
600 p_es->i_type = AC3_AUDIO_ES;
602 p_es->i_cat = AUDIO_ES;
603 strcpy( p_es->psz_desc, IfoLanguage( hton16(
604 vts.manager_inf.p_audio_attr[i-1].i_lang_code ) ) );
605 strcat( p_es->psz_desc, " (ac3)" );
607 intf_WarnMsg( 3, "dvd info: audio stream %d %s\t(0x%x)",
608 i, p_es->psz_desc, i_id );
612 case 0x03: /* MPEG audio */
613 i_id = 0xc0 + audio_status.i_position;
614 p_es = input_AddES( p_input,
615 p_input->stream.pp_programs[0], i_id, 0 );
616 p_es->i_stream_id = i_id;
617 p_es->i_type = MPEG2_AUDIO_ES;
619 p_es->i_cat = AUDIO_ES;
620 strcpy( p_es->psz_desc, IfoLanguage( hton16(
621 vts.manager_inf.p_audio_attr[i-1].i_lang_code ) ) );
622 strcat( p_es->psz_desc, " (mpeg)" );
624 intf_WarnMsg( 3, "dvd info: audio stream %d %s\t(0x%x)",
625 i, p_es->psz_desc, i_id );
628 case 0x04: /* LPCM */
630 i_id = ( ( 0xa0 + audio_status.i_position ) << 8 ) | 0xbd;
631 p_es = input_AddES( p_input,
632 p_input->stream.pp_programs[0], i_id, 0 );
633 p_es->i_stream_id = i_id;
634 p_es->i_type = LPCM_AUDIO_ES;
636 p_es->i_cat = AUDIO_ES;
637 strcpy( p_es->psz_desc, IfoLanguage( hton16(
638 vts.manager_inf.p_audio_attr[i-1].i_lang_code ) ) );
639 strcat( p_es->psz_desc, " (lpcm)" );
641 intf_WarnMsg( 3, "dvd info: audio stream %d %s\t(0x%x)",
642 i, p_es->psz_desc, i_id );
645 i_id = ( ( 0x88 + audio_status.i_position ) << 8 ) | 0xbd;
646 intf_ErrMsg( "dvd warning: DTS audio not handled yet"
651 intf_ErrMsg( "dvd warning: unknown audio type %.2x",
652 vts.manager_inf.p_audio_attr[i-1].i_coding_mode );
658 vts.title_unit.p_title[p_dvd->i_title_id-1].title.pi_spu_status[i-1]
662 for( i = 1 ; i <= vts.manager_inf.i_spu_nb; i++ )
664 IfoPrintSpu( p_dvd, i );
666 if( spu_status.i_available )
668 /* there are several streams for one spu */
669 if( vts.manager_inf.video_attr.i_ratio )
672 switch( vts.manager_inf.video_attr.i_perm_displ )
675 i_id = ( ( 0x20 + spu_status.i_position_pan ) << 8 )
679 i_id = ( ( 0x20 + spu_status.i_position_letter ) << 8 )
683 i_id = ( ( 0x20 + spu_status.i_position_wide ) << 8 )
691 i_id = ( ( 0x20 + spu_status.i_position_43 ) << 8 )
694 p_es = input_AddES( p_input,
695 p_input->stream.pp_programs[0], i_id, 0 );
696 p_es->i_stream_id = 0xbd;
697 p_es->i_type = DVD_SPU_ES;
698 p_es->i_cat = SPU_ES;
699 strcpy( p_es->psz_desc, IfoLanguage( hton16(
700 vts.manager_inf.p_spu_attr[i-1].i_lang_code ) ) );
701 intf_WarnMsg( 3, "dvd info: spu stream %d %s\t(0x%x)",
702 i, p_es->psz_desc, i_id );
706 if( p_main->b_audio )
708 /* For audio: first one if none or a not existing one specified */
709 i_audio = main_GetIntVariable( INPUT_CHANNEL_VAR, 1 );
710 if( i_audio < 0 || i_audio > vts.manager_inf.i_audio_nb )
712 main_PutIntVariable( INPUT_CHANNEL_VAR, 1 );
715 if( i_audio > 0 && vts.manager_inf.i_audio_nb > 0 )
717 input_SelectES( p_input, p_input->stream.pp_es[i_audio] );
721 if( p_main->b_video )
723 /* for spu, default is none */
724 i_spu = main_GetIntVariable( INPUT_SUBTITLE_VAR, 0 );
725 if( i_spu < 0 || i_spu > vts.manager_inf.i_spu_nb )
727 main_PutIntVariable( INPUT_SUBTITLE_VAR, 0 );
730 if( i_spu > 0 && vts.manager_inf.i_spu_nb > 0 )
732 i_spu += vts.manager_inf.i_audio_nb;
733 input_SelectES( p_input, p_input->stream.pp_es[i_spu] );
739 p_area = p_input->stream.p_selected_area;
749 if( p_area->i_part != p_dvd->i_chapter )
751 if( ( p_area->i_part > 0 ) &&
752 ( p_area->i_part <= p_area->i_part_nb ))
754 if( DVDChapterSelect( p_dvd, p_area->i_part ) < 0 )
756 intf_ErrMsg( "dvd error: can't set chapter in area" );
757 p_input->b_error = 1;
761 p_input->stream.p_selected_area->i_tell = p_dvd->i_start -
763 p_input->stream.p_selected_area->i_part = p_dvd->i_chapter;
765 intf_WarnMsg( 2, "dvd info: chapter %d start at: %lld",
766 p_area->i_part, p_area->i_tell );
771 p_dvd->i_chapter = 1;
776 p_dvd->p_ifo->vts.title_unit.p_title[p_dvd->i_title_id-1].title
777 if( p_area->i_angle != p_dvd->i_angle )
779 if( title.p_cell_play[p_dvd->i_prg_cell].i_category & 0xf000 )
781 if( ( p_area->i_angle - p_dvd->i_angle ) < 0 )
785 p_dvd->i_prg_cell += ( p_area->i_angle - p_dvd->i_angle );
786 p_dvd->i_angle = p_area->i_angle;
788 DVDFindSector( p_dvd );
789 p_dvd->i_cell += p_dvd->i_angle_cell;
793 p_dvd->i_angle = p_area->i_angle;
796 intf_WarnMsg( 2, "dvd info: angle %d selected", p_area->i_angle );
799 /* warn interface that something has changed */
800 p_input->stream.b_seekable = 1;
801 p_input->stream.b_changed = 1;
803 p_input->stream.pp_programs[0]->i_synchro_state = SYNCHRO_REINIT;
809 /*****************************************************************************
810 * DVDRead: reads data packets into the netlist.
811 *****************************************************************************
812 * Returns -1 in case of error, 0 if everything went well, and 1 in case of
814 *****************************************************************************/
815 static int DVDRead( input_thread_t * p_input,
816 data_packet_t ** pp_packets )
818 thread_dvd_data_t * p_dvd;
819 dvd_netlist_t * p_netlist;
820 struct iovec * p_vec;
821 struct data_packet_s ** pp_data;
834 pp_data = (struct data_packet_s **) malloc( p_input->i_read_once *
835 sizeof( struct data_packet_s * ) );
836 if( pp_data == NULL )
838 intf_ErrMsg( "dvd error: out of memory" );
842 pp_data = (struct data_packet_s **) malloc( p_input->i_read_once *
843 sizeof( struct data_packet_s * ) );
844 if( pp_data == NULL )
846 intf_ErrMsg( "dvd error: out of memory" );
850 p_dvd = (thread_dvd_data_t *)p_input->p_plugin_data;
851 p_netlist = (dvd_netlist_t *)p_input->p_method_data;
853 /* Get an iovec pointer */
854 if( ( p_vec = DVDGetiovec( p_netlist ) ) == NULL )
856 intf_ErrMsg( "dvd error: can't get iovec" );
861 i_block_once = p_dvd->i_end_sector - p_dvd->i_sector + 1;
863 /* Get the position of the next cell if we're at cell end */
864 if( i_block_once <= 0 )
869 p_dvd->i_angle_cell++;
871 /* Find cell index in adress map */
872 if( DVDFindSector( p_dvd ) < 0 )
874 pp_packets[0] = NULL;
875 intf_ErrMsg( "dvd error: can't find next cell" );
880 /* Position the fd pointer on the right address */
881 i_off = lseek( p_dvd->i_fd,
882 p_dvd->i_title_start +
883 (off_t)( p_dvd->i_sector ) *DVD_LB_SIZE, SEEK_SET );
885 /* update chapter : it will be easier when we have navigation
887 if( p_dvd->i_chapter < ( p_dvd->i_chapter_nb - 1 ) )
889 if( title.p_cell_play[p_dvd->i_prg_cell].i_category & 0xf000 )
891 i_angle = p_dvd->i_angle - 1;
897 if( title.chapter_map.pi_start_cell[p_dvd->i_chapter] <=
898 ( p_dvd->i_prg_cell - i_angle + 1 ) )
904 vlc_mutex_lock( &p_input->stream.stream_lock );
906 p_input->stream.p_selected_area->i_tell = i_off -
907 p_input->stream.p_selected_area->i_start;
908 p_input->stream.p_selected_area->i_part = p_dvd->i_chapter;
910 /* the synchro has to be reinitialized when we change cell */
911 p_input->stream.pp_programs[0]->i_synchro_state = SYNCHRO_REINIT;
913 vlc_mutex_unlock( &p_input->stream.stream_lock );
915 i_block_once = p_dvd->i_end_sector - p_dvd->i_sector + 1;
918 /* the number of blocks read is the maw between the requested
919 * value and the leaving block in the cell */
920 if( i_block_once > p_dvd->i_block_once )
922 i_block_once = p_dvd->i_block_once;
924 //intf_WarnMsg( 2, "Sector: 0x%x Read: %d Chapter: %d", p_dvd->i_sector, i_block_once, p_dvd->i_chapter );
926 p_netlist->i_read_once = i_block_once;
929 #if !defined( WIN32 )
930 i_read_bytes = readv( p_dvd->i_fd, p_vec, i_block_once );
932 i_read_bytes = ReadFileV( p_dvd->i_fd, p_vec, i_block_once );
934 i_read_blocks = ( i_read_bytes + 0x7ff ) >> 11;
936 /* Update netlist indexes */
937 DVDMviovec( p_netlist, i_read_blocks, pp_data );
939 /* Update global position */
940 p_dvd->i_sector += i_read_blocks;
944 /* Read headers to compute payload length */
945 for( i_iovec = 0 ; i_iovec < i_read_blocks ; i_iovec++ )
947 if( p_dvd->b_encrypted )
949 CSSDescrambleSector( p_dvd->p_css->pi_title_key,
950 p_vec[i_iovec].iov_base );
951 ((u8*)(p_vec[i_iovec].iov_base))[0x14] &= 0x8F;
956 while( i_pos < p_netlist->i_buffer_size )
958 pi_cur = (u8*)p_vec[i_iovec].iov_base + i_pos;
961 if( U32_AT( pi_cur ) != 0x1BA )
963 /* That's the case for all packets, except pack header. */
964 i_packet_size = U16_AT( pi_cur + 4 );
965 pp_packets[i_packet] = DVDNewPtr( p_netlist );
970 if( ( pi_cur[4] & 0xC0 ) == 0x40 )
975 else if( ( pi_cur[4] & 0xF0 ) == 0x20 )
982 intf_ErrMsg( "Unable to determine stream type" );
987 pp_packets[i_packet] = pp_data[i_iovec];
991 (*pp_data[i_iovec]->pi_refcount)++;
993 pp_packets[i_packet]->pi_refcount = pp_data[i_iovec]->pi_refcount;
995 pp_packets[i_packet]->p_buffer = pp_data[i_iovec]->p_buffer;
997 pp_packets[i_packet]->p_payload_start =
998 pp_packets[i_packet]->p_buffer + i_pos;
1000 pp_packets[i_packet]->p_payload_end =
1001 pp_packets[i_packet]->p_payload_start + i_packet_size + 6;
1003 pp_packets[i_packet]->p_next = NULL;
1004 pp_packets[i_packet]->b_discard_payload = 0;
1007 i_pos += i_packet_size + 6;
1011 pp_packets[i_packet] = NULL;
1013 vlc_mutex_lock( &p_input->stream.stream_lock );
1015 p_input->stream.p_selected_area->i_tell += i_read_bytes;
1016 b_eot = !( p_input->stream.p_selected_area->i_tell < p_dvd->i_size );
1017 b_eof = b_eot && ( ( p_dvd->i_title + 1 ) >= p_input->stream.i_area_nb );
1019 vlc_mutex_unlock( &p_input->stream.stream_lock );
1032 intf_WarnMsg( 4, "dvd info: new title" );
1034 vlc_mutex_lock( &p_input->stream.stream_lock );
1035 DVDSetArea( p_input, p_input->stream.pp_areas[p_dvd->i_title] );
1036 vlc_mutex_unlock( &p_input->stream.stream_lock );
1040 if( i_read_blocks == i_block_once )
1048 /*****************************************************************************
1049 * DVDRewind : reads a stream backward
1050 *****************************************************************************/
1051 static int DVDRewind( input_thread_t * p_input )
1056 /*****************************************************************************
1057 * DVDSeek : Goes to a given position on the stream.
1058 *****************************************************************************
1059 * This one is used by the input and translate chronological position from
1060 * input to logical position on the device.
1061 * The lock should be taken before calling this function.
1062 *****************************************************************************/
1063 static void DVDSeek( input_thread_t * p_input, off_t i_off )
1065 thread_dvd_data_t * p_dvd;
1072 p_dvd = ( thread_dvd_data_t * )p_input->p_plugin_data;
1074 /* we have to take care of offset of beginning of title */
1075 i_pos = i_off + p_input->stream.p_selected_area->i_start
1076 - p_dvd->i_title_start;
1078 /* update navigation data */
1079 p_dvd->i_sector = i_pos >> 11;
1084 /* parse vobu address map to find program cell */
1085 while( title.p_cell_play[i_prg_cell].i_end_sector < p_dvd->i_sector )
1090 p_dvd->i_prg_cell = i_prg_cell;
1092 if( DVDChooseAngle( p_dvd ) < 0 )
1094 p_input->b_error = 1;
1100 /* Find first title cell which is inside program cell */
1101 if( DVDFindCell( p_dvd ) < 0 )
1103 /* no following cell : we're at eof */
1104 intf_ErrMsg( "dvd error: cell seeking failed" );
1105 p_input->b_error = 1;
1109 i_cell = p_dvd->i_cell;
1111 #define cell p_dvd->p_ifo->vts.cell_inf.p_cell_map[i_cell]
1112 /* parse cell address map to find title cell containing sector */
1113 while( cell.i_end_sector < p_dvd->i_sector )
1118 p_dvd->i_cell = i_cell;
1120 /* if we're inside a multi-angle zone, we have to choose i_sector
1121 * in the current angle ; we can't do it all the time since cells
1122 * can be very wide out of such zones */
1123 if( title.p_cell_play[p_dvd->i_prg_cell].i_category & 0xf000 )
1125 p_dvd->i_sector = MAX(
1126 cell.i_start_sector,
1127 title.p_cell_play[p_dvd->i_prg_cell].i_start_sector );
1130 p_dvd->i_end_sector = MIN(
1132 title.p_cell_play[p_dvd->i_prg_cell].i_end_sector );
1134 /* update chapter */
1135 if( title.p_cell_play[p_dvd->i_prg_cell].i_category & 0xf000 )
1137 i_angle = p_dvd->i_angle - 1;
1143 while( ( title.chapter_map.pi_start_cell[i_chapter] <=
1144 ( p_dvd->i_prg_cell - i_angle + 1 ) ) &&
1145 ( i_chapter < ( p_dvd->i_chapter_nb - 1 ) ) )
1150 p_dvd->i_chapter = i_chapter;
1151 p_input->stream.p_selected_area->i_part = p_dvd->i_chapter;
1153 #if !defined( WIN32 )
1154 p_input->stream.p_selected_area->i_tell =
1155 lseek( p_dvd->i_fd, p_dvd->i_title_start +
1156 (off_t)( p_dvd->i_sector ) *DVD_LB_SIZE, SEEK_SET ) -
1157 p_input->stream.p_selected_area->i_start;
1159 p_input->stream.p_selected_area->i_tell =
1160 SetFilePointer( (HANDLE) p_dvd->i_fd, p_dvd->i_title_start +
1161 (off_t)( p_dvd->i_sector ) *DVD_LB_SIZE, NULL, FILE_BEGIN) -
1162 p_input->stream.p_selected_area->i_start;
1166 intf_WarnMsg( 3, "Program Cell: %d Cell: %d Chapter: %d",
1167 p_dvd->i_prg_cell, p_dvd->i_cell, p_dvd->i_chapter );
1173 #define cell p_dvd->p_ifo->vts.cell_inf
1175 /*****************************************************************************
1176 * DVDFindCell: adjust the title cell index with the program cell
1177 *****************************************************************************/
1178 static int DVDFindCell( thread_dvd_data_t * p_dvd )
1183 i_cell = p_dvd->i_cell;
1184 i_index = p_dvd->i_prg_cell;
1186 if( i_cell >= cell.i_cell_nb )
1191 while( ( ( title.p_cell_pos[i_index].i_vob_id !=
1192 cell.p_cell_map[i_cell].i_vob_id ) ||
1193 ( title.p_cell_pos[i_index].i_cell_id !=
1194 cell.p_cell_map[i_cell].i_cell_id ) ) &&
1195 ( i_cell < cell.i_cell_nb - 1 ) )
1201 intf_WarnMsg( 3, "FindCell: i_cell %d i_index %d found %d nb %d",
1208 p_dvd->i_cell = i_cell;
1215 /*****************************************************************************
1216 * DVDFindSector: find cell index in adress map from index in
1217 * information table program map and give corresponding sectors.
1218 *****************************************************************************/
1219 static int DVDFindSector( thread_dvd_data_t * p_dvd )
1222 if( p_dvd->i_sector > title.p_cell_play[p_dvd->i_prg_cell].i_end_sector )
1224 p_dvd->i_prg_cell++;
1226 if( DVDChooseAngle( p_dvd ) < 0 )
1232 if( DVDFindCell( p_dvd ) < 0 )
1234 intf_ErrMsg( "dvd error: can't find sector" );
1238 /* Find start and end sectors of new cell */
1240 p_dvd->i_sector = MAX(
1241 p_dvd->p_ifo->vts.cell_inf.p_cell_map[p_dvd->i_cell].i_start_sector,
1242 title.p_cell_play[p_dvd->i_prg_cell].i_start_sector );
1243 p_dvd->i_end_sector = MIN(
1244 p_dvd->p_ifo->vts.cell_inf.p_cell_map[p_dvd->i_cell].i_end_sector,
1245 title.p_cell_play[p_dvd->i_prg_cell].i_end_sector );
1247 p_dvd->i_sector = title.p_cell_play[p_dvd->i_prg_cell].i_start_sector;
1248 p_dvd->i_end_sector = title.p_cell_play[p_dvd->i_prg_cell].i_end_sector;
1252 intf_WarnMsg( 3, "cell: %d sector1: 0x%x end1: 0x%x\n"
1253 "index: %d sector2: 0x%x end2: 0x%x\n"
1254 "category: 0x%x ilvu end: 0x%x vobu start 0x%x",
1256 p_dvd->p_ifo->vts.cell_inf.p_cell_map[p_dvd->i_cell].i_start_sector,
1257 p_dvd->p_ifo->vts.cell_inf.p_cell_map[p_dvd->i_cell].i_end_sector,
1259 title.p_cell_play[p_dvd->i_prg_cell].i_start_sector,
1260 title.p_cell_play[p_dvd->i_prg_cell].i_end_sector,
1261 title.p_cell_play[p_dvd->i_prg_cell].i_category,
1262 title.p_cell_play[p_dvd->i_prg_cell].i_first_ilvu_vobu_esector,
1263 title.p_cell_play[p_dvd->i_prg_cell].i_last_vobu_start_sector );
1269 /*****************************************************************************
1270 * DVDChapterSelect: find the cell corresponding to requested chapter
1271 *****************************************************************************/
1272 static int DVDChapterSelect( thread_dvd_data_t * p_dvd, int i_chapter )
1275 /* Find cell index in Program chain for current chapter */
1276 p_dvd->i_prg_cell = title.chapter_map.pi_start_cell[i_chapter-1] - 1;
1278 p_dvd->i_sector = 0;
1280 DVDChooseAngle( p_dvd );
1282 /* Search for cell_index in cell adress_table and initialize
1284 if( DVDFindSector( p_dvd ) < 0 )
1286 intf_ErrMsg( "dvd error: can't select chapter" );
1290 /* start is : beginning of vts vobs + offset to vob x */
1291 p_dvd->i_start = p_dvd->i_title_start +
1292 DVD_LB_SIZE * (off_t)( p_dvd->i_sector );
1294 /* Position the fd pointer on the right address */
1295 #if !defined( WIN32 )
1296 p_dvd->i_start = lseek( p_dvd->i_fd, p_dvd->i_start, SEEK_SET );
1298 p_dvd->i_start = SetFilePointer( (HANDLE) p_dvd->i_fd,
1299 p_dvd->i_start, NULL, FILE_BEGIN );
1302 p_dvd->i_chapter = i_chapter;
1306 /*****************************************************************************
1307 * DVDChooseAngle: select the cell corresponding to the selected angle
1308 *****************************************************************************/
1309 static int DVDChooseAngle( thread_dvd_data_t * p_dvd )
1311 /* basic handling of angles */
1312 switch( ( ( title.p_cell_play[p_dvd->i_prg_cell].i_category & 0xf000 )
1315 /* we enter a muli-angle section */
1317 p_dvd->i_prg_cell += p_dvd->i_angle - 1;
1318 p_dvd->i_angle_cell = 0;
1320 /* we exit a multi-angle section */
1323 p_dvd->i_prg_cell += p_dvd->i_angle_nb - p_dvd->i_angle;