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.66 2001/05/31 16:10:05 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
43 #include "modules_inner.h"
53 # include <netinet/in.h>
57 #include <sys/types.h>
61 #ifdef STRNCASECMP_IN_STRINGS_H
67 # include "input_iovec.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 /* how many blocks DVDRead will read in each loop */
100 #define DVD_BLOCK_READ_ONCE 32
101 #define DVD_DATA_READ_ONCE 4*DVD_BLOCK_READ_ONCE
103 /* Size of netlist */
104 #define DVD_NETLIST_SIZE 2048
106 /*****************************************************************************
108 *****************************************************************************/
109 /* called from outside */
110 static int DVDProbe ( probedata_t *p_data );
111 static void DVDInit ( struct input_thread_s * );
112 static void DVDEnd ( struct input_thread_s * );
113 static int DVDSetArea ( struct input_thread_s *, struct input_area_s * );
114 static int DVDRead ( struct input_thread_s *, data_packet_t ** );
115 static void DVDSeek ( struct input_thread_s *, off_t );
116 static int DVDRewind ( struct input_thread_s * );
118 /* called only inside */
119 static int DVDChooseAngle( thread_dvd_data_t * );
120 static int DVDFindCell( thread_dvd_data_t * );
121 static int DVDFindSector( thread_dvd_data_t * );
122 static int DVDChapterSelect( thread_dvd_data_t *, int );
124 /*****************************************************************************
125 * Functions exported as capabilities. They are declared as static so that
126 * we don't pollute the namespace too much.
127 *****************************************************************************/
128 void _M( input_getfunctions )( function_list_t * p_function_list )
130 #define input p_function_list->functions.input
131 p_function_list->pf_probe = DVDProbe;
132 input.pf_init = DVDInit;
133 input.pf_open = NULL; /* Set in DVDInit */
134 input.pf_close = NULL;
135 input.pf_end = DVDEnd;
136 input.pf_read = DVDRead;
137 input.pf_set_area = DVDSetArea;
138 input.pf_demux = input_DemuxPS;
139 input.pf_new_packet = DVDNewPacket;
140 input.pf_new_pes = DVDNewPES;
141 input.pf_delete_packet = DVDDeletePacket;
142 input.pf_delete_pes = DVDDeletePES;
143 input.pf_rewind = DVDRewind;
144 input.pf_seek = DVDSeek;
149 * Data reading functions
152 /*****************************************************************************
153 * DVDProbe: verifies that the stream is a PS stream
154 *****************************************************************************/
155 static int DVDProbe( probedata_t *p_data )
157 input_thread_t * p_input = (input_thread_t *)p_data;
159 char * psz_name = p_input->p_source;
166 if( TestMethod( INPUT_METHOD_VAR, "dvd" ) )
172 #endif /* HAVE_CSS */
175 if( ( strlen(psz_name) > 4 ) && !strncasecmp( psz_name, "dvd:", 4 ) )
177 /* If the user specified "dvd:" then it's probably a DVD */
182 #endif /* HAVE_CSS */
186 #if !defined( WIN32 )
187 i_handle = open( psz_name, 0 );
194 snprintf( buf, 7, "\\\\.\\%c:", psz_name[0] );
195 (HANDLE) i_handle = CreateFile( i_score < 90 ? psz_name : buf,
196 GENERIC_READ | GENERIC_WRITE,
197 FILE_SHARE_READ | FILE_SHARE_WRITE,
198 NULL, OPEN_EXISTING, 0, NULL );
199 if( (HANDLE) i_handle == INVALID_HANDLE_VALUE )
203 CloseHandle( (HANDLE) i_handle );
209 /*****************************************************************************
210 * DVDInit: initializes DVD structures
211 *****************************************************************************/
212 static void DVDInit( input_thread_t * p_input )
214 thread_dvd_data_t * p_dvd;
215 input_area_t * p_area;
220 p_dvd = malloc( sizeof(thread_dvd_data_t) );
223 intf_ErrMsg( "dvd error: out of memory" );
224 p_input->b_error = 1;
228 p_input->p_plugin_data = (void *)p_dvd;
229 p_input->p_method_data = NULL;
231 p_dvd->i_fd = p_input->i_handle;
233 /* We read DVD_BLOCK_READ_ONCE in each loop, so the input will receive
234 * DVD_DATA_READ_ONCE at most */
235 p_dvd->i_block_once = DVD_BLOCK_READ_ONCE;
236 /* this value mustn't be modifed */
237 p_input->i_read_once = DVD_DATA_READ_ONCE;
239 i = CSSTest( p_input->i_handle );
243 intf_ErrMsg( "dvd error: error in css" );
245 p_input->b_error = 1;
249 p_dvd->b_encrypted = i;
251 #if !defined( WIN32 )
252 lseek( p_input->i_handle, 0, SEEK_SET );
254 SetFilePointer( (HANDLE) p_input->i_handle, 0, 0, FILE_BEGIN );
257 /* Reading structures initialisation */
258 p_input->p_method_data =
259 DVDNetlistInit( DVD_NETLIST_SIZE, 2*DVD_NETLIST_SIZE,
260 DVD_NETLIST_SIZE, DVD_LB_SIZE, p_dvd->i_block_once );
261 intf_WarnMsg( 2, "dvd info: netlist initialized" );
263 /* Ifo allocation & initialisation */
264 if( IfoCreate( p_dvd ) < 0 )
266 intf_ErrMsg( "dvd error: allcation error in ifo" );
267 p_input->b_error = 1;
271 if( IfoInit( p_dvd->p_ifo ) < 0 )
273 intf_ErrMsg( "dvd error: fatal failure in ifo" );
275 p_input->b_error = 1;
279 /* CSS initialisation */
280 if( p_dvd->b_encrypted )
282 p_dvd->p_css = malloc( sizeof(css_t) );
283 if( p_dvd->p_css == NULL )
285 intf_ErrMsg( "dvd error: couldn't create css structure" );
287 p_input->b_error = 1;
291 p_dvd->p_css->i_agid = 0;
293 if( CSSInit( p_input->i_handle, p_dvd->p_css ) < 0 )
295 intf_ErrMsg( "dvd error: fatal failure in css" );
296 free( p_dvd->p_css );
298 p_input->b_error = 1;
302 intf_WarnMsg( 2, "dvd info: css initialized" );
305 /* Set stream and area data */
306 vlc_mutex_lock( &p_input->stream.stream_lock );
308 /* Initialize ES structures */
309 input_InitStream( p_input, sizeof( stream_ps_data_t ) );
311 /* disc input method */
312 p_input->stream.i_method = INPUT_METHOD_DVD;
314 #define title_inf p_dvd->p_ifo->vmg.title_inf
315 intf_WarnMsg( 2, "dvd info: number of titles: %d", title_inf.i_title_nb );
317 #define area p_input->stream.pp_areas
318 /* We start from 1 here since the default area 0
319 * is reserved for video_ts.vob */
320 for( i = 1 ; i <= title_inf.i_title_nb ; i++ )
322 input_AddArea( p_input );
324 /* Titles are Program Chains */
327 /* Absolute start offset and size
328 * We can only set that with vts ifo, so we do it during the
329 * first call to DVDSetArea */
330 area[i]->i_start = 0;
333 /* Number of chapters */
334 area[i]->i_part_nb = title_inf.p_attr[i-1].i_chapter_nb;
337 /* Number of angles */
338 area[i]->i_angle_nb = 0;
339 area[i]->i_angle = 1;
341 /* Offset to vts_i_0.ifo */
342 area[i]->i_plugin_data = p_dvd->p_ifo->i_off +
343 ( title_inf.p_attr[i-1].i_start_sector * DVD_LB_SIZE );
347 /* Get requested title - if none try the first title */
348 i_title = main_GetIntVariable( INPUT_TITLE_VAR, 1 );
349 if( i_title <= 0 || i_title > title_inf.i_title_nb )
356 /* Get requested chapter - if none defaults to first one */
357 i_chapter = main_GetIntVariable( INPUT_CHAPTER_VAR, 1 );
363 p_input->stream.pp_areas[i_title]->i_part = i_chapter;
365 p_area = p_input->stream.pp_areas[i_title];
367 /* set title, chapter, audio and subpic */
368 DVDSetArea( p_input, p_area );
370 vlc_mutex_unlock( &p_input->stream.stream_lock );
375 /*****************************************************************************
376 * DVDEnd: frees unused data
377 *****************************************************************************/
378 static void DVDEnd( input_thread_t * p_input )
380 thread_dvd_data_t * p_dvd;
381 dvd_netlist_t * p_netlist;
383 p_dvd = (thread_dvd_data_t*)p_input->p_plugin_data;
384 p_netlist = (dvd_netlist_t *)p_input->p_method_data;
386 if( p_dvd->b_encrypted )
388 free( p_dvd->p_css );
391 IfoDestroy( p_dvd->p_ifo );
393 DVDNetlistEnd( p_netlist );
396 /*****************************************************************************
397 * DVDSetArea: initialize input data for title x, chapter y.
398 * It should be called for each user navigation request.
399 *****************************************************************************
400 * Take care that i_title starts from 0 (vmg) and i_chapter start from 1.
401 * Note that you have to take the lock before entering here.
402 *****************************************************************************/
403 static int DVDSetArea( input_thread_t * p_input, input_area_t * p_area )
405 thread_dvd_data_t * p_dvd;
406 es_descriptor_t * p_es;
414 p_dvd = (thread_dvd_data_t*)p_input->p_plugin_data;
416 /* we can't use the interface slider until initilization is complete */
417 p_input->stream.b_seekable = 0;
419 if( p_area != p_input->stream.p_selected_area )
423 * We have to load all title information
425 /* Change the default area */
426 p_input->stream.p_selected_area =
427 p_input->stream.pp_areas[p_area->i_id];
429 /* release the lock to to let the interface go */
430 // vlc_mutex_unlock( &p_input->stream.stream_lock );
432 /* title number: it is not vts nb!,
433 * it is what appears in the interface list */
434 p_dvd->i_title = p_area->i_id;
435 p_dvd->p_ifo->i_title = p_dvd->i_title;
437 /* set number of chapters of current title */
438 p_dvd->i_chapter_nb = p_area->i_part_nb;
441 if( IfoTitleSet( p_dvd->p_ifo ) < 0 )
443 intf_ErrMsg( "dvd error: fatal error in vts ifo" );
445 p_input->b_error = 1;
449 #define vmg p_dvd->p_ifo->vmg
450 #define vts p_dvd->p_ifo->vts
451 /* title position inside the selected vts */
452 i_vts_title = vmg.title_inf.p_attr[p_dvd->i_title-1].i_title_num;
454 vts.title_inf.p_title_start[i_vts_title-1].i_title_id;
456 intf_WarnMsg( 3, "dvd: title %d vts_title %d pgc %d",
461 /* css title key for current vts */
462 if( p_dvd->b_encrypted )
464 /* this one is vts number */
465 p_dvd->p_css->i_title =
466 vmg.title_inf.p_attr[p_dvd->i_title-1].i_title_set_num;
467 p_dvd->p_css->i_title_pos =
469 vts.manager_inf.i_title_vob_start_sector * DVD_LB_SIZE;
471 j = CSSGetKey( p_input->i_handle, p_dvd->p_css );
474 intf_ErrMsg( "dvd error: fatal error in vts css key" );
476 p_input->b_error = 1;
481 intf_ErrMsg( "dvd error: css decryption unavailable" );
483 p_input->b_error = 1;
491 p_dvd->i_angle_nb = vmg.title_inf.p_attr[p_dvd->i_title-1].i_angle_nb;
492 p_dvd->i_angle = main_GetIntVariable( INPUT_ANGLE_VAR, 1 );
493 if( ( p_dvd->i_angle <= 0 ) || p_dvd->i_angle > p_dvd->i_angle_nb )
499 * Set selected title start and size
502 /* title set offset */
503 p_dvd->i_title_start = vts.i_pos + DVD_LB_SIZE *
504 (off_t)( vts.manager_inf.i_title_vob_start_sector );
506 /* last video cell */
508 p_dvd->i_prg_cell = -1 +
509 vts.title_unit.p_title[p_dvd->i_title_id-1].title.i_cell_nb;
511 if( DVDFindCell( p_dvd ) < 0 )
513 intf_ErrMsg( "dvd error: can't find title end" );
514 p_input->b_error = 1;
518 /* temporary hack to fix size in some dvds */
519 if( p_dvd->i_cell >= vts.cell_inf.i_cell_nb )
521 p_dvd->i_cell = vts.cell_inf.i_cell_nb - 1;
525 p_dvd->i_size = DVD_LB_SIZE *
526 (off_t)( vts.cell_inf.p_cell_map[p_dvd->i_cell].i_end_sector );
527 intf_WarnMsg( 2, "dvd info: stream size 1: %lld @ %d", p_dvd->i_size,
528 vts.cell_inf.p_cell_map[p_dvd->i_cell].i_end_sector );
530 if( DVDChapterSelect( p_dvd, 1 ) < 0 )
532 intf_ErrMsg( "dvd error: can't find first chapter" );
533 p_input->b_error = 1;
537 p_dvd->i_size -= (off_t)( p_dvd->i_sector + 1 ) *DVD_LB_SIZE;
539 IfoPrintTitle( p_dvd );
541 // vlc_mutex_lock( &p_input->stream.stream_lock );
543 /* Area definition */
544 p_input->stream.p_selected_area->i_start = p_dvd->i_start;
545 p_input->stream.p_selected_area->i_size = p_dvd->i_size;
546 p_input->stream.p_selected_area->i_angle_nb = p_dvd->i_angle_nb;
547 p_input->stream.p_selected_area->i_angle = p_dvd->i_angle;
550 * Destroy obsolete ES by reinitializing program 0
551 * and find all ES in title with ifo data
553 if( p_input->stream.pp_programs != NULL )
555 /* We don't use input_EndStream here since
556 * we keep area structures */
558 for( i = 0 ; i < p_input->stream.i_selected_es_number ; i++ )
560 input_UnselectES( p_input, p_input->stream.pp_selected_es[i] );
563 input_DelProgram( p_input, p_input->stream.pp_programs[0] );
565 p_input->stream.pp_selected_es = NULL;
566 p_input->stream.i_selected_es_number = 0;
569 input_AddProgram( p_input, 0, sizeof( stream_ps_data_t ) );
571 /* No PSM to read in DVD mode, we already have all information */
572 p_input->stream.pp_programs[0]->b_is_ok = 1;
573 p_input->stream.pp_programs[0]->i_synchro_state = SYNCHRO_START;
577 /* ES 0 -> video MPEG2 */
578 IfoPrintVideo( p_dvd );
580 p_es = input_AddES( p_input, p_input->stream.pp_programs[0], 0xe0, 0 );
581 p_es->i_stream_id = 0xe0;
582 p_es->i_type = MPEG2_VIDEO_ES;
583 p_es->i_cat = VIDEO_ES;
584 intf_WarnMsg( 1, "dvd info: video mpeg2 stream" );
585 if( p_main->b_video )
587 input_SelectES( p_input, p_es );
589 intf_WarnMsg( 4, "dvd info: video selected" );
591 #define audio_status \
592 vts.title_unit.p_title[p_dvd->i_title_id-1].title.pi_audio_status[i-1]
593 /* Audio ES, in the order they appear in .ifo */
594 for( i = 1 ; i <= vts.manager_inf.i_audio_nb ; i++ )
596 IfoPrintAudio( p_dvd, i );
598 /* audio channel is active if first byte is 0x80 */
599 if( audio_status.i_available )
601 switch( vts.manager_inf.p_audio_attr[i-1].i_coding_mode )
604 i_id = ( ( 0x80 + audio_status.i_position ) << 8 ) | 0xbd;
605 p_es = input_AddES( p_input,
606 p_input->stream.pp_programs[0], i_id, 0 );
607 p_es->i_stream_id = 0xbd;
608 p_es->i_type = AC3_AUDIO_ES;
610 p_es->i_cat = AUDIO_ES;
611 strcpy( p_es->psz_desc, IfoLanguage( hton16(
612 vts.manager_inf.p_audio_attr[i-1].i_lang_code ) ) );
613 strcat( p_es->psz_desc, " (ac3)" );
615 intf_WarnMsg( 3, "dvd info: audio stream %d %s\t(0x%x)",
616 i, p_es->psz_desc, i_id );
620 case 0x03: /* MPEG audio */
621 i_id = 0xc0 + audio_status.i_position;
622 p_es = input_AddES( p_input,
623 p_input->stream.pp_programs[0], i_id, 0 );
624 p_es->i_stream_id = i_id;
625 p_es->i_type = MPEG2_AUDIO_ES;
627 p_es->i_cat = AUDIO_ES;
628 strcpy( p_es->psz_desc, IfoLanguage( hton16(
629 vts.manager_inf.p_audio_attr[i-1].i_lang_code ) ) );
630 strcat( p_es->psz_desc, " (mpeg)" );
632 intf_WarnMsg( 3, "dvd info: audio stream %d %s\t(0x%x)",
633 i, p_es->psz_desc, i_id );
636 case 0x04: /* LPCM */
638 i_id = ( ( 0xa0 + audio_status.i_position ) << 8 ) | 0xbd;
639 p_es = input_AddES( p_input,
640 p_input->stream.pp_programs[0], i_id, 0 );
641 p_es->i_stream_id = i_id;
642 p_es->i_type = LPCM_AUDIO_ES;
644 p_es->i_cat = AUDIO_ES;
645 strcpy( p_es->psz_desc, IfoLanguage( hton16(
646 vts.manager_inf.p_audio_attr[i-1].i_lang_code ) ) );
647 strcat( p_es->psz_desc, " (lpcm)" );
649 intf_WarnMsg( 3, "dvd info: audio stream %d %s\t(0x%x)",
650 i, p_es->psz_desc, i_id );
653 i_id = ( ( 0x88 + audio_status.i_position ) << 8 ) | 0xbd;
654 intf_ErrMsg( "dvd warning: DTS audio not handled yet"
659 intf_ErrMsg( "dvd warning: unknown audio type %.2x",
660 vts.manager_inf.p_audio_attr[i-1].i_coding_mode );
666 vts.title_unit.p_title[p_dvd->i_title_id-1].title.pi_spu_status[i-1]
670 for( i = 1 ; i <= vts.manager_inf.i_spu_nb; i++ )
672 IfoPrintSpu( p_dvd, i );
674 if( spu_status.i_available )
676 /* there are several streams for one spu */
677 if( vts.manager_inf.video_attr.i_ratio )
680 switch( vts.manager_inf.video_attr.i_perm_displ )
683 i_id = ( ( 0x20 + spu_status.i_position_pan ) << 8 )
687 i_id = ( ( 0x20 + spu_status.i_position_letter ) << 8 )
691 i_id = ( ( 0x20 + spu_status.i_position_wide ) << 8 )
699 i_id = ( ( 0x20 + spu_status.i_position_43 ) << 8 )
702 p_es = input_AddES( p_input,
703 p_input->stream.pp_programs[0], i_id, 0 );
704 p_es->i_stream_id = 0xbd;
705 p_es->i_type = DVD_SPU_ES;
706 p_es->i_cat = SPU_ES;
707 strcpy( p_es->psz_desc, IfoLanguage( hton16(
708 vts.manager_inf.p_spu_attr[i-1].i_lang_code ) ) );
709 intf_WarnMsg( 3, "dvd info: spu stream %d %s\t(0x%x)",
710 i, p_es->psz_desc, i_id );
714 if( p_main->b_audio )
716 /* For audio: first one if none or a not existing one specified */
717 i_audio = main_GetIntVariable( INPUT_CHANNEL_VAR, 1 );
718 if( i_audio < 0 || i_audio > vts.manager_inf.i_audio_nb )
720 main_PutIntVariable( INPUT_CHANNEL_VAR, 1 );
723 if( i_audio > 0 && vts.manager_inf.i_audio_nb > 0 )
725 input_SelectES( p_input, p_input->stream.pp_es[i_audio] );
729 if( p_main->b_video )
731 /* for spu, default is none */
732 i_spu = main_GetIntVariable( INPUT_SUBTITLE_VAR, 0 );
733 if( i_spu < 0 || i_spu > vts.manager_inf.i_spu_nb )
735 main_PutIntVariable( INPUT_SUBTITLE_VAR, 0 );
738 if( i_spu > 0 && vts.manager_inf.i_spu_nb > 0 )
740 i_spu += vts.manager_inf.i_audio_nb;
741 input_SelectES( p_input, p_input->stream.pp_es[i_spu] );
747 p_area = p_input->stream.p_selected_area;
757 if( p_area->i_part != p_dvd->i_chapter )
759 if( ( p_area->i_part > 0 ) &&
760 ( p_area->i_part <= p_area->i_part_nb ))
762 if( DVDChapterSelect( p_dvd, p_area->i_part ) < 0 )
764 intf_ErrMsg( "dvd error: can't set chapter in area" );
765 p_input->b_error = 1;
769 p_input->stream.p_selected_area->i_tell = p_dvd->i_start -
771 p_input->stream.p_selected_area->i_part = p_dvd->i_chapter;
773 intf_WarnMsg( 2, "dvd info: chapter %d start at: %lld",
774 p_area->i_part, p_area->i_tell );
779 p_dvd->i_chapter = 1;
784 p_dvd->p_ifo->vts.title_unit.p_title[p_dvd->i_title_id-1].title
785 if( p_area->i_angle != p_dvd->i_angle )
787 if( title.p_cell_play[p_dvd->i_prg_cell].i_category & 0xf000 )
789 if( ( p_area->i_angle - p_dvd->i_angle ) < 0 )
793 p_dvd->i_prg_cell += ( p_area->i_angle - p_dvd->i_angle );
794 p_dvd->i_angle = p_area->i_angle;
796 DVDFindSector( p_dvd );
797 p_dvd->i_cell += p_dvd->i_angle_cell;
801 p_dvd->i_angle = p_area->i_angle;
804 intf_WarnMsg( 2, "dvd info: angle %d selected", p_area->i_angle );
807 /* warn interface that something has changed */
808 p_input->stream.b_seekable = 1;
809 p_input->stream.b_changed = 1;
811 p_input->stream.pp_programs[0]->i_synchro_state = SYNCHRO_REINIT;
817 /*****************************************************************************
818 * DVDRead: reads data packets into the netlist.
819 *****************************************************************************
820 * Returns -1 in case of error, 0 if everything went well, and 1 in case of
822 *****************************************************************************/
823 static int DVDRead( input_thread_t * p_input,
824 data_packet_t ** pp_packets )
826 thread_dvd_data_t * p_dvd;
827 dvd_netlist_t * p_netlist;
828 struct iovec * p_vec;
829 struct data_packet_s * pp_data[DVD_DATA_READ_ONCE];
842 p_dvd = (thread_dvd_data_t *)p_input->p_plugin_data;
843 p_netlist = (dvd_netlist_t *)p_input->p_method_data;
845 /* Get an iovec pointer */
846 if( ( p_vec = DVDGetiovec( p_netlist ) ) == NULL )
848 intf_ErrMsg( "dvd error: can't get iovec" );
852 i_block_once = p_dvd->i_end_sector - p_dvd->i_sector + 1;
854 /* Get the position of the next cell if we're at cell end */
855 if( i_block_once <= 0 )
860 p_dvd->i_angle_cell++;
862 /* Find cell index in adress map */
863 if( DVDFindSector( p_dvd ) < 0 )
865 pp_packets[0] = NULL;
866 intf_ErrMsg( "dvd error: can't find next cell" );
870 /* Position the fd pointer on the right address */
871 i_off = lseek( p_dvd->i_fd,
872 p_dvd->i_title_start +
873 (off_t)( p_dvd->i_sector ) *DVD_LB_SIZE, SEEK_SET );
875 /* update chapter : it will be easier when we have navigation
877 if( p_dvd->i_chapter < ( p_dvd->i_chapter_nb - 1 ) )
879 if( title.p_cell_play[p_dvd->i_prg_cell].i_category & 0xf000 )
881 i_angle = p_dvd->i_angle - 1;
887 if( title.chapter_map.pi_start_cell[p_dvd->i_chapter] <=
888 ( p_dvd->i_prg_cell - i_angle + 1 ) )
894 vlc_mutex_lock( &p_input->stream.stream_lock );
896 p_input->stream.p_selected_area->i_tell = i_off -
897 p_input->stream.p_selected_area->i_start;
898 p_input->stream.p_selected_area->i_part = p_dvd->i_chapter;
900 /* the synchro has to be reinitialized when we change cell */
901 p_input->stream.pp_programs[0]->i_synchro_state = SYNCHRO_REINIT;
903 vlc_mutex_unlock( &p_input->stream.stream_lock );
905 i_block_once = p_dvd->i_end_sector - p_dvd->i_sector + 1;
908 /* the number of blocks read is the maw between the requested
909 * value and the leaving block in the cell */
910 if( i_block_once > p_dvd->i_block_once )
912 i_block_once = p_dvd->i_block_once;
914 //intf_WarnMsg( 2, "Sector: 0x%x Read: %d Chapter: %d", p_dvd->i_sector, i_block_once, p_dvd->i_chapter );
916 p_netlist->i_read_once = i_block_once;
919 #if !defined( WIN32 )
920 i_read_bytes = readv( p_dvd->i_fd, p_vec, i_block_once );
922 i_read_bytes = ReadFileV( p_dvd->i_fd, p_vec, i_block_once );
924 i_read_blocks = ( i_read_bytes + 0x7ff ) >> 11;
926 /* Update netlist indexes */
927 DVDMviovec( p_netlist, i_read_blocks, pp_data );
929 /* Update global position */
930 p_dvd->i_sector += i_read_blocks;
934 /* Read headers to compute payload length */
935 for( i_iovec = 0 ; i_iovec < i_read_blocks ; i_iovec++ )
937 if( p_dvd->b_encrypted )
939 CSSDescrambleSector( p_dvd->p_css->pi_title_key,
940 p_vec[i_iovec].iov_base );
941 ((u8*)(p_vec[i_iovec].iov_base))[0x14] &= 0x8F;
946 while( i_pos < p_netlist->i_buffer_size )
948 pi_cur = (u8*)p_vec[i_iovec].iov_base + i_pos;
951 if( U32_AT( pi_cur ) != 0x1BA )
953 /* That's the case for all packets, except pack header. */
954 i_packet_size = U16_AT( pi_cur + 4 );
955 pp_packets[i_packet] = DVDNewPtr( p_netlist );
960 if( ( pi_cur[4] & 0xC0 ) == 0x40 )
965 else if( ( pi_cur[4] & 0xF0 ) == 0x20 )
972 intf_ErrMsg( "Unable to determine stream type" );
976 pp_packets[i_packet] = pp_data[i_iovec];
980 (*pp_data[i_iovec]->pi_refcount)++;
982 pp_packets[i_packet]->pi_refcount = pp_data[i_iovec]->pi_refcount;
984 pp_packets[i_packet]->p_buffer = pp_data[i_iovec]->p_buffer;
986 pp_packets[i_packet]->p_payload_start =
987 pp_packets[i_packet]->p_buffer + i_pos;
989 pp_packets[i_packet]->p_payload_end =
990 pp_packets[i_packet]->p_payload_start + i_packet_size + 6;
992 pp_packets[i_packet]->p_next = NULL;
993 pp_packets[i_packet]->b_discard_payload = 0;
996 i_pos += i_packet_size + 6;
1000 pp_packets[i_packet] = NULL;
1002 vlc_mutex_lock( &p_input->stream.stream_lock );
1004 p_input->stream.p_selected_area->i_tell += i_read_bytes;
1005 b_eot = !( p_input->stream.p_selected_area->i_tell < p_dvd->i_size );
1006 b_eof = b_eot && ( ( p_dvd->i_title + 1 ) >= p_input->stream.i_area_nb );
1008 vlc_mutex_unlock( &p_input->stream.stream_lock );
1017 intf_WarnMsg( 4, "dvd info: new title" );
1019 vlc_mutex_lock( &p_input->stream.stream_lock );
1020 DVDSetArea( p_input, p_input->stream.pp_areas[p_dvd->i_title] );
1021 vlc_mutex_unlock( &p_input->stream.stream_lock );
1025 if( i_read_blocks == i_block_once )
1033 /*****************************************************************************
1034 * DVDRewind : reads a stream backward
1035 *****************************************************************************/
1036 static int DVDRewind( input_thread_t * p_input )
1041 /*****************************************************************************
1042 * DVDSeek : Goes to a given position on the stream.
1043 *****************************************************************************
1044 * This one is used by the input and translate chronological position from
1045 * input to logical position on the device.
1046 * The lock should be taken before calling this function.
1047 *****************************************************************************/
1048 static void DVDSeek( input_thread_t * p_input, off_t i_off )
1050 thread_dvd_data_t * p_dvd;
1057 p_dvd = ( thread_dvd_data_t * )p_input->p_plugin_data;
1059 /* we have to take care of offset of beginning of title */
1060 i_pos = i_off + p_input->stream.p_selected_area->i_start
1061 - p_dvd->i_title_start;
1063 /* update navigation data */
1064 p_dvd->i_sector = i_pos >> 11;
1069 /* parse vobu address map to find program cell */
1070 while( title.p_cell_play[i_prg_cell].i_end_sector < p_dvd->i_sector )
1075 p_dvd->i_prg_cell = i_prg_cell;
1077 if( DVDChooseAngle( p_dvd ) < 0 )
1079 p_input->b_error = 1;
1085 /* Find first title cell which is inside program cell */
1086 if( DVDFindCell( p_dvd ) < 0 )
1088 /* no following cell : we're at eof */
1089 intf_ErrMsg( "dvd error: cell seeking failed" );
1090 p_input->b_error = 1;
1094 i_cell = p_dvd->i_cell;
1096 #define cell p_dvd->p_ifo->vts.cell_inf.p_cell_map[i_cell]
1097 /* parse cell address map to find title cell containing sector */
1098 while( cell.i_end_sector < p_dvd->i_sector )
1103 p_dvd->i_cell = i_cell;
1105 /* if we're inside a multi-angle zone, we have to choose i_sector
1106 * in the current angle ; we can't do it all the time since cells
1107 * can be very wide out of such zones */
1108 if( title.p_cell_play[p_dvd->i_prg_cell].i_category & 0xf000 )
1110 p_dvd->i_sector = MAX(
1111 cell.i_start_sector,
1112 title.p_cell_play[p_dvd->i_prg_cell].i_start_sector );
1115 p_dvd->i_end_sector = MIN(
1117 title.p_cell_play[p_dvd->i_prg_cell].i_end_sector );
1119 /* update chapter */
1120 if( title.p_cell_play[p_dvd->i_prg_cell].i_category & 0xf000 )
1122 i_angle = p_dvd->i_angle - 1;
1128 while( ( title.chapter_map.pi_start_cell[i_chapter] <=
1129 ( p_dvd->i_prg_cell - i_angle + 1 ) ) &&
1130 ( i_chapter < ( p_dvd->i_chapter_nb - 1 ) ) )
1135 p_dvd->i_chapter = i_chapter;
1136 p_input->stream.p_selected_area->i_part = p_dvd->i_chapter;
1138 #if !defined( WIN32 )
1139 p_input->stream.p_selected_area->i_tell =
1140 lseek( p_dvd->i_fd, p_dvd->i_title_start +
1141 (off_t)( p_dvd->i_sector ) *DVD_LB_SIZE, SEEK_SET ) -
1142 p_input->stream.p_selected_area->i_start;
1144 p_input->stream.p_selected_area->i_tell =
1145 SetFilePointer( (HANDLE) p_dvd->i_fd, p_dvd->i_title_start +
1146 (off_t)( p_dvd->i_sector ) *DVD_LB_SIZE, NULL, FILE_BEGIN) -
1147 p_input->stream.p_selected_area->i_start;
1151 intf_WarnMsg( 3, "Program Cell: %d Cell: %d Chapter: %d",
1152 p_dvd->i_prg_cell, p_dvd->i_cell, p_dvd->i_chapter );
1158 #define cell p_dvd->p_ifo->vts.cell_inf
1160 /*****************************************************************************
1161 * DVDFindCell: adjust the title cell index with the program cell
1162 *****************************************************************************/
1163 static int DVDFindCell( thread_dvd_data_t * p_dvd )
1168 i_cell = p_dvd->i_cell;
1169 i_index = p_dvd->i_prg_cell;
1171 if( i_cell >= cell.i_cell_nb )
1176 while( ( ( title.p_cell_pos[i_index].i_vob_id !=
1177 cell.p_cell_map[i_cell].i_vob_id ) ||
1178 ( title.p_cell_pos[i_index].i_cell_id !=
1179 cell.p_cell_map[i_cell].i_cell_id ) ) &&
1180 ( i_cell < cell.i_cell_nb - 1 ) )
1186 intf_WarnMsg( 3, "FindCell: i_cell %d i_index %d found %d nb %d",
1193 p_dvd->i_cell = i_cell;
1200 /*****************************************************************************
1201 * DVDFindSector: find cell index in adress map from index in
1202 * information table program map and give corresponding sectors.
1203 *****************************************************************************/
1204 static int DVDFindSector( thread_dvd_data_t * p_dvd )
1207 if( p_dvd->i_sector > title.p_cell_play[p_dvd->i_prg_cell].i_end_sector )
1209 p_dvd->i_prg_cell++;
1211 if( DVDChooseAngle( p_dvd ) < 0 )
1217 if( DVDFindCell( p_dvd ) < 0 )
1219 intf_ErrMsg( "dvd error: can't find sector" );
1223 /* Find start and end sectors of new cell */
1225 p_dvd->i_sector = MAX(
1226 p_dvd->p_ifo->vts.cell_inf.p_cell_map[p_dvd->i_cell].i_start_sector,
1227 title.p_cell_play[p_dvd->i_prg_cell].i_start_sector );
1228 p_dvd->i_end_sector = MIN(
1229 p_dvd->p_ifo->vts.cell_inf.p_cell_map[p_dvd->i_cell].i_end_sector,
1230 title.p_cell_play[p_dvd->i_prg_cell].i_end_sector );
1232 p_dvd->i_sector = title.p_cell_play[p_dvd->i_prg_cell].i_start_sector;
1233 p_dvd->i_end_sector = title.p_cell_play[p_dvd->i_prg_cell].i_end_sector;
1237 intf_WarnMsg( 3, "cell: %d sector1: 0x%x end1: 0x%x\n"
1238 "index: %d sector2: 0x%x end2: 0x%x\n"
1239 "category: 0x%x ilvu end: 0x%x vobu start 0x%x",
1241 p_dvd->p_ifo->vts.cell_inf.p_cell_map[p_dvd->i_cell].i_start_sector,
1242 p_dvd->p_ifo->vts.cell_inf.p_cell_map[p_dvd->i_cell].i_end_sector,
1244 title.p_cell_play[p_dvd->i_prg_cell].i_start_sector,
1245 title.p_cell_play[p_dvd->i_prg_cell].i_end_sector,
1246 title.p_cell_play[p_dvd->i_prg_cell].i_category,
1247 title.p_cell_play[p_dvd->i_prg_cell].i_first_ilvu_vobu_esector,
1248 title.p_cell_play[p_dvd->i_prg_cell].i_last_vobu_start_sector );
1254 /*****************************************************************************
1255 * DVDChapterSelect: find the cell corresponding to requested chapter
1256 *****************************************************************************/
1257 static int DVDChapterSelect( thread_dvd_data_t * p_dvd, int i_chapter )
1260 /* Find cell index in Program chain for current chapter */
1261 p_dvd->i_prg_cell = title.chapter_map.pi_start_cell[i_chapter-1] - 1;
1263 p_dvd->i_sector = 0;
1265 DVDChooseAngle( p_dvd );
1267 /* Search for cell_index in cell adress_table and initialize
1269 if( DVDFindSector( p_dvd ) < 0 )
1271 intf_ErrMsg( "dvd error: can't select chapter" );
1275 /* start is : beginning of vts vobs + offset to vob x */
1276 p_dvd->i_start = p_dvd->i_title_start +
1277 DVD_LB_SIZE * (off_t)( p_dvd->i_sector );
1279 /* Position the fd pointer on the right address */
1280 #if !defined( WIN32 )
1281 p_dvd->i_start = lseek( p_dvd->i_fd, p_dvd->i_start, SEEK_SET );
1283 p_dvd->i_start = SetFilePointer( (HANDLE) p_dvd->i_fd,
1284 p_dvd->i_start, NULL, FILE_BEGIN );
1287 p_dvd->i_chapter = i_chapter;
1291 /*****************************************************************************
1292 * DVDChooseAngle: select the cell corresponding to the selected angle
1293 *****************************************************************************/
1294 static int DVDChooseAngle( thread_dvd_data_t * p_dvd )
1296 /* basic handling of angles */
1297 switch( ( ( title.p_cell_play[p_dvd->i_prg_cell].i_category & 0xf000 )
1300 /* we enter a muli-angle section */
1302 p_dvd->i_prg_cell += p_dvd->i_angle - 1;
1303 p_dvd->i_angle_cell = 0;
1305 /* we exit a multi-angle section */
1308 p_dvd->i_prg_cell += p_dvd->i_angle_nb - p_dvd->i_angle;