1 /*****************************************************************************
2 * input_dvd.c: DVD raw reading plugin.
3 *****************************************************************************
4 * This plugins should handle all the known specificities of the DVD format,
5 * especially the 2048 bytes logical block size.
7 * -input_netlist used to read packets
8 * -libdvdcss for access and unscrambling
9 * -dvd_ifo for ifo parsing and analyse
10 * -dvd_udf to find files
11 *****************************************************************************
12 * Copyright (C) 1998-2001 VideoLAN
13 * $Id: input_dvd.c,v 1.79 2001/07/11 02:01:04 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 <sys/types.h>
57 #ifdef STRNCASECMP_IN_STRINGS_H
63 # include "input_iovec.h"
65 # include <sys/uio.h> /* struct iovec */
68 #include <videolan/dvdcss.h>
80 #include "stream_control.h"
81 #include "input_ext-intf.h"
82 #include "input_ext-dec.h"
86 #include "input_dvd.h"
87 #include "dvd_netlist.h"
89 #include "dvd_summary.h"
90 #include "mpeg_system.h"
95 #include "modules_export.h"
97 /* how many blocks DVDRead will read in each loop */
98 #define DVD_BLOCK_READ_ONCE 64
99 #define DVD_DATA_READ_ONCE (4 * DVD_BLOCK_READ_ONCE)
101 /* Size of netlist */
102 #define DVD_NETLIST_SIZE 256
104 /*****************************************************************************
106 *****************************************************************************/
107 /* called from outside */
108 static int DVDProbe ( probedata_t *p_data );
109 static void DVDInit ( struct input_thread_s * );
110 static void DVDEnd ( struct input_thread_s * );
111 static void DVDOpen ( struct input_thread_s * );
112 static void DVDClose ( 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 = DVDOpen;
134 input.pf_close = DVDClose;
135 input.pf_end = DVDEnd;
136 input.pf_init_bit_stream = InitBitstream;
137 input.pf_read = DVDRead;
138 input.pf_set_area = DVDSetArea;
139 input.pf_demux = input_DemuxPS;
140 input.pf_new_packet = DVDNewPacket;
141 input.pf_new_pes = DVDNewPES;
142 input.pf_delete_packet = DVDDeletePacket;
143 input.pf_delete_pes = DVDDeletePES;
144 input.pf_rewind = DVDRewind;
145 input.pf_seek = DVDSeek;
150 * Data reading functions
153 /*****************************************************************************
154 * DVDProbe: verifies that the stream is a PS stream
155 *****************************************************************************/
156 static int DVDProbe( probedata_t *p_data )
158 input_thread_t * p_input = (input_thread_t *)p_data;
160 char * psz_name = p_input->p_source;
163 if( TestMethod( INPUT_METHOD_VAR, "dvd" ) )
169 #endif /* HAVE_CSS */
172 if( ( strlen(psz_name) > 4 ) && !strncasecmp( psz_name, "dvd:", 4 ) )
174 /* If the user specified "dvd:" then it's probably a DVD */
179 #endif /* HAVE_CSS */
186 /*****************************************************************************
187 * DVDInit: initializes DVD structures
188 *****************************************************************************/
189 static void DVDInit( input_thread_t * p_input )
191 thread_dvd_data_t * p_dvd;
192 input_area_t * p_area;
197 p_dvd = malloc( sizeof(thread_dvd_data_t) );
200 intf_ErrMsg( "dvd error: out of memory" );
201 p_input->b_error = 1;
205 p_input->p_plugin_data = (void *)p_dvd;
206 p_input->p_method_data = NULL;
208 p_dvd->dvdhandle = (dvdcss_handle) p_input->i_handle;
210 dvdcss_seek( p_dvd->dvdhandle, 0 );
212 /* We read DVD_BLOCK_READ_ONCE in each loop, so the input will receive
213 * DVD_DATA_READ_ONCE at most */
214 p_dvd->i_block_once = DVD_BLOCK_READ_ONCE;
215 /* this value mustn't be modifed */
216 p_input->i_read_once = DVD_DATA_READ_ONCE;
218 /* Reading structures initialisation */
219 p_input->p_method_data =
220 DVDNetlistInit( DVD_NETLIST_SIZE, 2 * DVD_NETLIST_SIZE,
221 DVD_NETLIST_SIZE, DVD_LB_SIZE, p_dvd->i_block_once );
222 intf_WarnMsg( 2, "dvd info: netlist initialized" );
224 /* Ifo allocation & initialisation */
225 if( IfoCreate( p_dvd ) < 0 )
227 intf_ErrMsg( "dvd error: allcation error in ifo" );
228 dvdcss_close( p_dvd->dvdhandle );
230 p_input->b_error = 1;
234 if( IfoInit( p_dvd->p_ifo ) < 0 )
236 intf_ErrMsg( "dvd error: fatal failure in ifo" );
237 IfoDestroy( p_dvd->p_ifo );
238 dvdcss_close( p_dvd->dvdhandle );
240 p_input->b_error = 1;
244 /* Set stream and area data */
245 vlc_mutex_lock( &p_input->stream.stream_lock );
247 /* Initialize ES structures */
248 input_InitStream( p_input, sizeof( stream_ps_data_t ) );
250 /* disc input method */
251 p_input->stream.i_method = INPUT_METHOD_DVD;
253 #define title_inf p_dvd->p_ifo->vmg.title_inf
254 intf_WarnMsg( 2, "dvd info: number of titles: %d", title_inf.i_title_nb );
256 #define area p_input->stream.pp_areas
257 /* We start from 1 here since the default area 0
258 * is reserved for video_ts.vob */
259 for( i = 1 ; i <= title_inf.i_title_nb ; i++ )
261 input_AddArea( p_input );
263 /* Titles are Program Chains */
266 /* Absolute start offset and size
267 * We can only set that with vts ifo, so we do it during the
268 * first call to DVDSetArea */
269 area[i]->i_start = 0;
272 /* Number of chapters */
273 area[i]->i_part_nb = title_inf.p_attr[i-1].i_chapter_nb;
276 /* Number of angles */
277 area[i]->i_angle_nb = 0;
278 area[i]->i_angle = 1;
280 /* Offset to vts_i_0.ifo */
281 area[i]->i_plugin_data = p_dvd->p_ifo->i_start +
282 title_inf.p_attr[i-1].i_start_sector;
286 /* Get requested title - if none try the first title */
287 i_title = main_GetIntVariable( INPUT_TITLE_VAR, 1 );
288 if( i_title <= 0 || i_title > title_inf.i_title_nb )
295 /* Get requested chapter - if none defaults to first one */
296 i_chapter = main_GetIntVariable( INPUT_CHAPTER_VAR, 1 );
302 p_input->stream.pp_areas[i_title]->i_part = i_chapter;
304 p_area = p_input->stream.pp_areas[i_title];
306 /* set title, chapter, audio and subpic */
307 DVDSetArea( p_input, p_area );
309 vlc_mutex_unlock( &p_input->stream.stream_lock );
314 /*****************************************************************************
316 *****************************************************************************/
317 static void DVDOpen( struct input_thread_s *p_input )
319 dvdcss_handle dvdhandle;
321 vlc_mutex_lock( &p_input->stream.stream_lock );
323 /* If we are here we can control the pace... */
324 p_input->stream.b_pace_control = 1;
326 p_input->stream.b_seekable = 1;
327 p_input->stream.p_selected_area->i_size = 0;
329 p_input->stream.p_selected_area->i_tell = 0;
331 vlc_mutex_unlock( &p_input->stream.stream_lock );
333 /* XXX: put this shit in an access plugin */
334 if( strlen( p_input->p_source ) > 4
335 && !strncasecmp( p_input->p_source, "dvd:", 4 ) )
337 dvdhandle = dvdcss_open( p_input->p_source + 4, DVDCSS_INIT_QUIET );
341 dvdhandle = dvdcss_open( p_input->p_source, DVDCSS_INIT_QUIET );
344 if( dvdhandle == NULL )
346 p_input->b_error = 1;
350 p_input->i_handle = (int) dvdhandle;
353 /*****************************************************************************
354 * DVDClose: close dvd
355 *****************************************************************************/
356 static void DVDClose( struct input_thread_s *p_input )
358 /* Clean up libdvdcss */
359 dvdcss_close( (dvdcss_handle) p_input->i_handle );
362 /*****************************************************************************
363 * DVDEnd: frees unused data
364 *****************************************************************************/
365 static void DVDEnd( input_thread_t * p_input )
367 thread_dvd_data_t * p_dvd;
368 dvd_netlist_t * p_netlist;
370 p_dvd = (thread_dvd_data_t*)p_input->p_plugin_data;
371 p_netlist = (dvd_netlist_t *)p_input->p_method_data;
373 IfoDestroy( p_dvd->p_ifo );
377 DVDNetlistEnd( p_netlist );
380 /*****************************************************************************
381 * DVDSetArea: initialize input data for title x, chapter y.
382 * It should be called for each user navigation request.
383 *****************************************************************************
384 * Take care that i_title starts from 0 (vmg) and i_chapter start from 1.
385 * Note that you have to take the lock before entering here.
386 *****************************************************************************/
387 static int DVDSetArea( input_thread_t * p_input, input_area_t * p_area )
389 thread_dvd_data_t * p_dvd;
390 es_descriptor_t * p_es;
397 p_dvd = (thread_dvd_data_t*)p_input->p_plugin_data;
399 /* we can't use the interface slider until initilization is complete */
400 p_input->stream.b_seekable = 0;
402 if( p_area != p_input->stream.p_selected_area )
406 * We have to load all title information
408 /* Change the default area */
409 p_input->stream.p_selected_area =
410 p_input->stream.pp_areas[p_area->i_id];
412 /* title number: it is not vts nb!,
413 * it is what appears in the interface list */
414 p_dvd->i_title = p_area->i_id;
415 p_dvd->p_ifo->i_title = p_dvd->i_title;
417 /* set number of chapters of current title */
418 p_dvd->i_chapter_nb = p_area->i_part_nb;
421 if( IfoTitleSet( p_dvd->p_ifo ) < 0 )
423 intf_ErrMsg( "dvd error: fatal error in vts ifo" );
425 p_input->b_error = 1;
429 #define vmg p_dvd->p_ifo->vmg
430 #define vts p_dvd->p_ifo->vts
431 /* title position inside the selected vts */
432 i_vts_title = vmg.title_inf.p_attr[p_dvd->i_title-1].i_title_num;
434 vts.title_inf.p_title_start[i_vts_title-1].i_title_id;
436 intf_WarnMsgImm( 3, "dvd: title %d vts_title %d pgc %d",
437 p_dvd->i_title, i_vts_title, p_dvd->i_title_id );
440 * CSS cracking has to be done again
442 dvdcss_crack( p_dvd->dvdhandle,
443 vts.i_pos + vts.manager_inf.i_title_vob_start_sector );
448 p_dvd->i_angle_nb = vmg.title_inf.p_attr[p_dvd->i_title-1].i_angle_nb;
449 p_dvd->i_angle = main_GetIntVariable( INPUT_ANGLE_VAR, 1 );
450 if( ( p_dvd->i_angle <= 0 ) || p_dvd->i_angle > p_dvd->i_angle_nb )
456 * Set selected title start and size
459 /* title set offset XXX: convert to block values */
460 p_dvd->i_title_start =
461 vts.i_pos + vts.manager_inf.i_title_vob_start_sector;
463 /* last video cell */
465 p_dvd->i_prg_cell = -1 +
466 vts.title_unit.p_title[p_dvd->i_title_id-1].title.i_cell_nb;
468 if( DVDFindCell( p_dvd ) < 0 )
470 intf_ErrMsg( "dvd error: can't find title end" );
471 p_input->b_error = 1;
475 /* temporary hack to fix size in some dvds */
476 if( p_dvd->i_cell >= vts.cell_inf.i_cell_nb )
478 p_dvd->i_cell = vts.cell_inf.i_cell_nb - 1;
482 p_dvd->i_size = vts.cell_inf.p_cell_map[p_dvd->i_cell].i_end_sector;
484 if( DVDChapterSelect( p_dvd, 1 ) < 0 )
486 intf_ErrMsg( "dvd error: can't find first chapter" );
487 p_input->b_error = 1;
491 p_dvd->i_size -= p_dvd->i_sector + 1;
493 IfoPrintTitle( p_dvd );
495 /* Area definition */
496 p_input->stream.p_selected_area->i_start = LB2OFF( p_dvd->i_start );
497 p_input->stream.p_selected_area->i_size = LB2OFF( p_dvd->i_size );
498 p_input->stream.p_selected_area->i_angle_nb = p_dvd->i_angle_nb;
499 p_input->stream.p_selected_area->i_angle = p_dvd->i_angle;
501 /* start at the beginning of the title */
502 /* FIXME: create a conf option to select whether to restart
504 p_input->stream.p_selected_area->i_tell = 0;
505 p_input->stream.p_selected_area->i_part = 1;
508 * Destroy obsolete ES by reinitializing program 0
509 * and find all ES in title with ifo data
511 if( p_input->stream.pp_programs != NULL )
513 /* We don't use input_EndStream here since
514 * we keep area structures */
516 for( i = 0 ; i < p_input->stream.i_selected_es_number ; i++ )
518 input_UnselectES( p_input, p_input->stream.pp_selected_es[i] );
521 input_DelProgram( p_input, p_input->stream.pp_programs[0] );
523 p_input->stream.pp_selected_es = NULL;
524 p_input->stream.i_selected_es_number = 0;
527 input_AddProgram( p_input, 0, sizeof( stream_ps_data_t ) );
529 /* No PSM to read in DVD mode, we already have all information */
530 p_input->stream.pp_programs[0]->b_is_ok = 1;
534 /* ES 0 -> video MPEG2 */
535 IfoPrintVideo( p_dvd );
537 p_es = input_AddES( p_input, p_input->stream.pp_programs[0], 0xe0, 0 );
538 p_es->i_stream_id = 0xe0;
539 p_es->i_type = MPEG2_VIDEO_ES;
540 p_es->i_cat = VIDEO_ES;
541 if( p_main->b_video )
543 input_SelectES( p_input, p_es );
546 #define audio_status \
547 vts.title_unit.p_title[p_dvd->i_title_id-1].title.pi_audio_status[i-1]
548 /* Audio ES, in the order they appear in .ifo */
549 for( i = 1 ; i <= vts.manager_inf.i_audio_nb ; i++ )
551 IfoPrintAudio( p_dvd, i );
553 /* audio channel is active if first byte is 0x80 */
554 if( audio_status.i_available )
556 switch( vts.manager_inf.p_audio_attr[i-1].i_coding_mode )
559 i_id = ( ( 0x80 + audio_status.i_position ) << 8 ) | 0xbd;
560 p_es = input_AddES( p_input,
561 p_input->stream.pp_programs[0], i_id, 0 );
562 p_es->i_stream_id = 0xbd;
563 p_es->i_type = AC3_AUDIO_ES;
565 p_es->i_cat = AUDIO_ES;
566 strcpy( p_es->psz_desc, IfoLanguage( hton16(
567 vts.manager_inf.p_audio_attr[i-1].i_lang_code ) ) );
568 strcat( p_es->psz_desc, " (ac3)" );
572 case 0x03: /* MPEG audio */
573 i_id = 0xc0 + audio_status.i_position;
574 p_es = input_AddES( p_input,
575 p_input->stream.pp_programs[0], i_id, 0 );
576 p_es->i_stream_id = i_id;
577 p_es->i_type = MPEG2_AUDIO_ES;
579 p_es->i_cat = AUDIO_ES;
580 strcpy( p_es->psz_desc, IfoLanguage( hton16(
581 vts.manager_inf.p_audio_attr[i-1].i_lang_code ) ) );
582 strcat( p_es->psz_desc, " (mpeg)" );
585 case 0x04: /* LPCM */
587 i_id = ( ( 0xa0 + audio_status.i_position ) << 8 ) | 0xbd;
588 p_es = input_AddES( p_input,
589 p_input->stream.pp_programs[0], i_id, 0 );
590 p_es->i_stream_id = i_id;
591 p_es->i_type = LPCM_AUDIO_ES;
593 p_es->i_cat = AUDIO_ES;
594 strcpy( p_es->psz_desc, IfoLanguage( hton16(
595 vts.manager_inf.p_audio_attr[i-1].i_lang_code ) ) );
596 strcat( p_es->psz_desc, " (lpcm)" );
600 i_id = ( ( 0x88 + audio_status.i_position ) << 8 ) | 0xbd;
601 intf_ErrMsg( "dvd warning: DTS audio not handled yet"
606 intf_ErrMsg( "dvd warning: unknown audio type %.2x",
607 vts.manager_inf.p_audio_attr[i-1].i_coding_mode );
613 vts.title_unit.p_title[p_dvd->i_title_id-1].title.pi_spu_status[i-1]
617 for( i = 1 ; i <= vts.manager_inf.i_spu_nb; i++ )
619 IfoPrintSpu( p_dvd, i );
621 if( spu_status.i_available )
623 /* there are several streams for one spu */
624 if( vts.manager_inf.video_attr.i_ratio )
627 switch( vts.manager_inf.video_attr.i_perm_displ )
630 i_id = ( ( 0x20 + spu_status.i_position_pan ) << 8 )
634 i_id = ( ( 0x20 + spu_status.i_position_letter ) << 8 )
638 i_id = ( ( 0x20 + spu_status.i_position_wide ) << 8 )
646 i_id = ( ( 0x20 + spu_status.i_position_43 ) << 8 )
649 p_es = input_AddES( p_input,
650 p_input->stream.pp_programs[0], i_id, 0 );
651 p_es->i_stream_id = 0xbd;
652 p_es->i_type = DVD_SPU_ES;
653 p_es->i_cat = SPU_ES;
654 strcpy( p_es->psz_desc, IfoLanguage( hton16(
655 vts.manager_inf.p_spu_attr[i-1].i_lang_code ) ) );
659 if( p_main->b_audio )
661 /* For audio: first one if none or a not existing one specified */
662 i_audio = main_GetIntVariable( INPUT_CHANNEL_VAR, 1 );
663 if( i_audio < 0 || i_audio > vts.manager_inf.i_audio_nb )
665 main_PutIntVariable( INPUT_CHANNEL_VAR, 1 );
668 if( i_audio > 0 && vts.manager_inf.i_audio_nb > 0 )
670 input_SelectES( p_input, p_input->stream.pp_es[i_audio] );
674 if( p_main->b_video )
676 /* for spu, default is none */
677 i_spu = main_GetIntVariable( INPUT_SUBTITLE_VAR, 0 );
678 if( i_spu < 0 || i_spu > vts.manager_inf.i_spu_nb )
680 main_PutIntVariable( INPUT_SUBTITLE_VAR, 0 );
683 if( i_spu > 0 && vts.manager_inf.i_spu_nb > 0 )
685 i_spu += vts.manager_inf.i_audio_nb;
686 input_SelectES( p_input, p_input->stream.pp_es[i_spu] );
692 p_area = p_input->stream.p_selected_area;
701 if( p_area->i_part != p_dvd->i_chapter )
703 if( ( p_area->i_part > 0 ) &&
704 ( p_area->i_part <= p_area->i_part_nb ))
706 if( DVDChapterSelect( p_dvd, p_area->i_part ) < 0 )
708 intf_ErrMsg( "dvd error: can't set chapter in area" );
709 p_input->b_error = 1;
713 p_input->stream.p_selected_area->i_tell =
714 LB2OFF( p_dvd->i_start ) - p_area->i_start;
715 p_input->stream.p_selected_area->i_part = p_dvd->i_chapter;
717 intf_WarnMsg( 4, "dvd info: chapter %d start at: %lld",
718 p_area->i_part, p_area->i_tell );
723 p_dvd->i_chapter = 1;
728 p_dvd->p_ifo->vts.title_unit.p_title[p_dvd->i_title_id-1].title
729 if( p_area->i_angle != p_dvd->i_angle )
731 if( title.p_cell_play[p_dvd->i_prg_cell].i_category & 0xf000 )
733 if( ( p_area->i_angle - p_dvd->i_angle ) < 0 )
737 p_dvd->i_prg_cell += ( p_area->i_angle - p_dvd->i_angle );
738 p_dvd->i_angle = p_area->i_angle;
740 DVDFindSector( p_dvd );
741 p_dvd->i_cell += p_dvd->i_angle_cell;
745 p_dvd->i_angle = p_area->i_angle;
748 intf_WarnMsg( 3, "dvd info: angle %d selected", p_area->i_angle );
751 /* warn interface that something has changed */
752 p_input->stream.b_seekable = 1;
753 p_input->stream.b_changed = 1;
759 /*****************************************************************************
760 * DVDRead: reads data packets into the netlist.
761 *****************************************************************************
762 * Returns -1 in case of error, 0 if everything went well, and 1 in case of
764 *****************************************************************************/
765 static int DVDRead( input_thread_t * p_input,
766 data_packet_t ** pp_packets )
768 thread_dvd_data_t * p_dvd;
769 dvd_netlist_t * p_netlist;
770 struct iovec * p_vec;
771 struct data_packet_s * pp_data[DVD_DATA_READ_ONCE];
784 p_dvd = (thread_dvd_data_t *)p_input->p_plugin_data;
785 p_netlist = (dvd_netlist_t *)p_input->p_method_data;
788 i_sector = p_dvd->i_title_start + p_dvd->i_sector;
789 i_block_once = p_dvd->i_end_sector - p_dvd->i_sector + 1;
791 /* Get the position of the next cell if we're at cell end */
792 if( i_block_once <= 0 )
797 p_dvd->i_angle_cell++;
799 /* Find cell index in adress map */
800 if( DVDFindSector( p_dvd ) < 0 )
802 pp_packets[0] = NULL;
803 intf_ErrMsg( "dvd error: can't find next cell" );
807 /* Position the fd pointer on the right address */
808 i_sector = dvdcss_seek( p_dvd->dvdhandle,
809 p_dvd->i_title_start + p_dvd->i_sector );
811 /* update chapter : it will be easier when we have navigation
813 if( p_dvd->i_chapter < ( p_dvd->i_chapter_nb - 1 ) )
815 if( title.p_cell_play[p_dvd->i_prg_cell].i_category & 0xf000 )
817 i_angle = p_dvd->i_angle - 1;
823 if( title.chapter_map.pi_start_cell[p_dvd->i_chapter] <=
824 ( p_dvd->i_prg_cell - i_angle + 1 ) )
831 i_block_once = p_dvd->i_end_sector - p_dvd->i_sector + 1;
834 /* The number of blocks read is the max between the requested
835 * value and the leaving block in the cell */
836 if( i_block_once > p_dvd->i_block_once )
838 i_block_once = p_dvd->i_block_once;
841 intf_WarnMsg( 2, "Sector: 0x%x Read: %d Chapter: %d", p_dvd->i_sector, i_block_once, p_dvd->i_chapter );
843 p_netlist->i_read_once = i_block_once;
845 /* Get an iovec pointer */
846 if( ( p_vec = DVDGetiovec( p_netlist ) ) == NULL )
848 intf_ErrMsg( "dvd error: can't get iovec" );
853 i_read_blocks = dvdcss_readv( p_dvd->dvdhandle, p_vec,
854 i_block_once, DVDCSS_READ_DECRYPT );
856 /* Update netlist indexes: we don't do it in DVDGetiovec since we
857 * need know the real number of blocks read */
858 DVDMviovec( p_netlist, i_read_blocks, pp_data );
860 /* Update global position */
861 p_dvd->i_sector += i_read_blocks;
865 /* Read headers to compute payload length */
866 for( i_iovec = 0 ; i_iovec < i_read_blocks ; i_iovec++ )
870 while( i_pos < p_netlist->i_buffer_size )
872 pi_cur = (u8*)p_vec[i_iovec].iov_base + i_pos;
875 if( U32_AT( pi_cur ) != 0x1BA )
877 /* That's the case for all packets, except pack header. */
878 i_packet_size = U16_AT( pi_cur + 4 );
879 pp_packets[i_packet] = DVDNewPtr( p_netlist );
883 /* MPEG-2 Pack header. */
885 pp_packets[i_packet] = pp_data[i_iovec];
889 (*pp_data[i_iovec]->pi_refcount)++;
891 pp_packets[i_packet]->pi_refcount = pp_data[i_iovec]->pi_refcount;
893 pp_packets[i_packet]->p_buffer = pp_data[i_iovec]->p_buffer;
895 pp_packets[i_packet]->p_payload_start =
896 pp_packets[i_packet]->p_buffer + i_pos;
898 pp_packets[i_packet]->p_payload_end =
899 pp_packets[i_packet]->p_payload_start + i_packet_size + 6;
901 pp_packets[i_packet]->p_next = NULL;
902 pp_packets[i_packet]->b_discard_payload = 0;
905 i_pos += i_packet_size + 6;
909 pp_packets[i_packet] = NULL;
911 vlc_mutex_lock( &p_input->stream.stream_lock );
913 p_input->stream.p_selected_area->i_tell =
914 LB2OFF( i_sector + i_read_blocks ) -
915 p_input->stream.p_selected_area->i_start;
918 /* We modify i_part only at end of chapter not to erase
919 * some modification from the interface */
920 p_input->stream.p_selected_area->i_part = p_dvd->i_chapter;
923 b_eot = !( p_input->stream.p_selected_area->i_tell
924 < p_input->stream.p_selected_area->i_size );
925 b_eof = b_eot && ( ( p_dvd->i_title + 1 ) >= p_input->stream.i_area_nb );
929 vlc_mutex_unlock( &p_input->stream.stream_lock );
935 intf_WarnMsg( 4, "dvd info: new title" );
937 DVDSetArea( p_input, p_input->stream.pp_areas[p_dvd->i_title] );
938 vlc_mutex_unlock( &p_input->stream.stream_lock );
942 vlc_mutex_unlock( &p_input->stream.stream_lock );
944 if( i_read_blocks == i_block_once )
952 /*****************************************************************************
953 * DVDRewind : reads a stream backward
954 *****************************************************************************/
955 static int DVDRewind( input_thread_t * p_input )
960 /*****************************************************************************
961 * DVDSeek : Goes to a given position on the stream.
962 *****************************************************************************
963 * This one is used by the input and translate chronological position from
964 * input to logical position on the device.
965 * The lock should be taken before calling this function.
966 *****************************************************************************/
967 static void DVDSeek( input_thread_t * p_input, off_t i_off )
969 thread_dvd_data_t * p_dvd;
975 p_dvd = ( thread_dvd_data_t * )p_input->p_plugin_data;
977 /* we have to take care of offset of beginning of title */
978 p_dvd->i_sector = OFF2LB(i_off + p_input->stream.p_selected_area->i_start)
979 - p_dvd->i_title_start;
984 /* parse vobu address map to find program cell */
985 while( title.p_cell_play[i_prg_cell].i_end_sector < p_dvd->i_sector )
990 p_dvd->i_prg_cell = i_prg_cell;
992 if( DVDChooseAngle( p_dvd ) < 0 )
994 p_input->b_error = 1;
1000 /* Find first title cell which is inside program cell */
1001 if( DVDFindCell( p_dvd ) < 0 )
1003 /* no following cell : we're at eof */
1004 intf_ErrMsg( "dvd error: cell seeking failed" );
1005 p_input->b_error = 1;
1009 i_cell = p_dvd->i_cell;
1011 #define cell p_dvd->p_ifo->vts.cell_inf.p_cell_map[i_cell]
1012 /* parse cell address map to find title cell containing sector */
1013 while( cell.i_end_sector < p_dvd->i_sector )
1018 p_dvd->i_cell = i_cell;
1020 /* if we're inside a multi-angle zone, we have to choose i_sector
1021 * in the current angle ; we can't do it all the time since cells
1022 * can be very wide out of such zones */
1023 if( title.p_cell_play[p_dvd->i_prg_cell].i_category & 0xf000 )
1025 p_dvd->i_sector = MAX(
1026 cell.i_start_sector,
1027 title.p_cell_play[p_dvd->i_prg_cell].i_start_sector );
1030 p_dvd->i_end_sector = MIN(
1032 title.p_cell_play[p_dvd->i_prg_cell].i_end_sector );
1034 /* update chapter */
1035 if( title.p_cell_play[p_dvd->i_prg_cell].i_category & 0xf000 )
1037 i_angle = p_dvd->i_angle - 1;
1043 if( p_dvd->i_chapter_nb > 1 )
1045 while( ( title.chapter_map.pi_start_cell[i_chapter] <=
1046 ( p_dvd->i_prg_cell - i_angle + 1 ) ) &&
1047 ( i_chapter < ( p_dvd->i_chapter_nb - 1 ) ) )
1057 p_dvd->i_chapter = i_chapter;
1058 p_input->stream.p_selected_area->i_part = p_dvd->i_chapter;
1060 p_input->stream.p_selected_area->i_tell =
1061 LB2OFF ( dvdcss_seek( p_dvd->dvdhandle, p_dvd->i_title_start
1062 + p_dvd->i_sector ) )
1063 - p_input->stream.p_selected_area->i_start;
1065 intf_WarnMsg( 7, "Program Cell: %d Cell: %d Chapter: %d",
1066 p_dvd->i_prg_cell, p_dvd->i_cell, p_dvd->i_chapter );
1072 #define cell p_dvd->p_ifo->vts.cell_inf
1074 /*****************************************************************************
1075 * DVDFindCell: adjust the title cell index with the program cell
1076 *****************************************************************************/
1077 static int DVDFindCell( thread_dvd_data_t * p_dvd )
1082 i_cell = p_dvd->i_cell;
1083 i_index = p_dvd->i_prg_cell;
1085 if( i_cell >= cell.i_cell_nb )
1090 while( ( ( title.p_cell_pos[i_index].i_vob_id !=
1091 cell.p_cell_map[i_cell].i_vob_id ) ||
1092 ( title.p_cell_pos[i_index].i_cell_id !=
1093 cell.p_cell_map[i_cell].i_cell_id ) ) &&
1094 ( i_cell < cell.i_cell_nb - 1 ) )
1100 intf_WarnMsg( 7, "FindCell: i_cell %d i_index %d found %d nb %d",
1107 p_dvd->i_cell = i_cell;
1114 /*****************************************************************************
1115 * DVDFindSector: find cell index in adress map from index in
1116 * information table program map and give corresponding sectors.
1117 *****************************************************************************/
1118 static int DVDFindSector( thread_dvd_data_t * p_dvd )
1121 if( p_dvd->i_sector > title.p_cell_play[p_dvd->i_prg_cell].i_end_sector )
1123 p_dvd->i_prg_cell++;
1125 if( DVDChooseAngle( p_dvd ) < 0 )
1131 if( DVDFindCell( p_dvd ) < 0 )
1133 intf_ErrMsg( "dvd error: can't find sector" );
1137 /* Find start and end sectors of new cell */
1139 p_dvd->i_sector = MAX(
1140 p_dvd->p_ifo->vts.cell_inf.p_cell_map[p_dvd->i_cell].i_start_sector,
1141 title.p_cell_play[p_dvd->i_prg_cell].i_start_sector );
1142 p_dvd->i_end_sector = MIN(
1143 p_dvd->p_ifo->vts.cell_inf.p_cell_map[p_dvd->i_cell].i_end_sector,
1144 title.p_cell_play[p_dvd->i_prg_cell].i_end_sector );
1146 p_dvd->i_sector = title.p_cell_play[p_dvd->i_prg_cell].i_start_sector;
1147 p_dvd->i_end_sector = title.p_cell_play[p_dvd->i_prg_cell].i_end_sector;
1151 intf_WarnMsg( 7, "cell: %d sector1: 0x%x end1: 0x%x\n"
1152 "index: %d sector2: 0x%x end2: 0x%x\n"
1153 "category: 0x%x ilvu end: 0x%x vobu start 0x%x",
1155 p_dvd->p_ifo->vts.cell_inf.p_cell_map[p_dvd->i_cell].i_start_sector,
1156 p_dvd->p_ifo->vts.cell_inf.p_cell_map[p_dvd->i_cell].i_end_sector,
1158 title.p_cell_play[p_dvd->i_prg_cell].i_start_sector,
1159 title.p_cell_play[p_dvd->i_prg_cell].i_end_sector,
1160 title.p_cell_play[p_dvd->i_prg_cell].i_category,
1161 title.p_cell_play[p_dvd->i_prg_cell].i_first_ilvu_vobu_esector,
1162 title.p_cell_play[p_dvd->i_prg_cell].i_last_vobu_start_sector );
1168 /*****************************************************************************
1169 * DVDChapterSelect: find the cell corresponding to requested chapter
1170 *****************************************************************************/
1171 static int DVDChapterSelect( thread_dvd_data_t * p_dvd, int i_chapter )
1174 /* Find cell index in Program chain for current chapter */
1175 p_dvd->i_prg_cell = title.chapter_map.pi_start_cell[i_chapter-1] - 1;
1177 p_dvd->i_sector = 0;
1179 DVDChooseAngle( p_dvd );
1181 /* Search for cell_index in cell adress_table and initialize
1183 if( DVDFindSector( p_dvd ) < 0 )
1185 intf_ErrMsg( "dvd error: can't select chapter" );
1189 /* start is : beginning of vts vobs + offset to vob x */
1190 p_dvd->i_start = p_dvd->i_title_start + p_dvd->i_sector;
1192 /* Position the fd pointer on the right address */
1193 p_dvd->i_start = dvdcss_seek( p_dvd->dvdhandle, p_dvd->i_start );
1195 p_dvd->i_chapter = i_chapter;
1199 /*****************************************************************************
1200 * DVDChooseAngle: select the cell corresponding to the selected angle
1201 *****************************************************************************/
1202 static int DVDChooseAngle( thread_dvd_data_t * p_dvd )
1204 /* basic handling of angles */
1205 switch( ( ( title.p_cell_play[p_dvd->i_prg_cell].i_category & 0xf000 )
1208 /* we enter a muli-angle section */
1210 p_dvd->i_prg_cell += p_dvd->i_angle - 1;
1211 p_dvd->i_angle_cell = 0;
1213 /* we exit a multi-angle section */
1216 p_dvd->i_prg_cell += p_dvd->i_angle_nb - p_dvd->i_angle;