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.84 2001/08/06 13:28:00 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 #define MODULE_NAME dvd
33 #include "modules_inner.h"
35 /*****************************************************************************
37 *****************************************************************************/
48 #include <sys/types.h>
52 #ifdef STRNCASECMP_IN_STRINGS_H
57 # include <io.h> /* read() */
59 # include <sys/uio.h> /* struct iovec */
63 # include "dummy_dvdcss.h"
65 # include <videolan/dvdcss.h>
75 # include "input_iovec.h"
82 #include "stream_control.h"
83 #include "input_ext-intf.h"
84 #include "input_ext-dec.h"
85 #include "input_ext-plugins.h"
87 #include "input_dvd.h"
88 #include "dvd_netlist.h"
90 #include "dvd_summary.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" ) )
168 if( ( strlen(psz_name) > 4 ) && !strncasecmp( psz_name, "dvd:", 4 ) )
170 /* If the user specified "dvd:" then it's probably a DVD */
178 /*****************************************************************************
179 * DVDInit: initializes DVD structures
180 *****************************************************************************/
181 static void DVDInit( input_thread_t * p_input )
183 thread_dvd_data_t * p_dvd;
184 input_area_t * p_area;
189 p_dvd = malloc( sizeof(thread_dvd_data_t) );
192 intf_ErrMsg( "dvd error: out of memory" );
193 p_input->b_error = 1;
197 p_input->p_plugin_data = (void *)p_dvd;
198 p_input->p_method_data = NULL;
200 p_dvd->dvdhandle = (dvdcss_handle) p_input->i_handle;
202 dvdcss_seek( p_dvd->dvdhandle, 0 );
204 /* We read DVD_BLOCK_READ_ONCE in each loop, so the input will receive
205 * DVD_DATA_READ_ONCE at most */
206 p_dvd->i_block_once = DVD_BLOCK_READ_ONCE;
207 /* this value mustn't be modifed */
208 p_input->i_read_once = DVD_DATA_READ_ONCE;
210 /* Reading structures initialisation */
211 p_input->p_method_data =
212 DVDNetlistInit( DVD_NETLIST_SIZE, 2 * DVD_NETLIST_SIZE,
213 DVD_NETLIST_SIZE, DVD_LB_SIZE, p_dvd->i_block_once );
214 intf_WarnMsg( 2, "dvd info: netlist initialized" );
216 /* Ifo allocation & initialisation */
217 if( IfoCreate( p_dvd ) < 0 )
219 intf_ErrMsg( "dvd error: allcation error in ifo" );
220 dvdcss_close( p_dvd->dvdhandle );
222 p_input->b_error = 1;
226 if( IfoInit( p_dvd->p_ifo ) < 0 )
228 intf_ErrMsg( "dvd error: fatal failure in ifo" );
229 IfoDestroy( p_dvd->p_ifo );
230 dvdcss_close( p_dvd->dvdhandle );
232 p_input->b_error = 1;
236 /* Set stream and area data */
237 vlc_mutex_lock( &p_input->stream.stream_lock );
239 /* Initialize ES structures */
240 input_InitStream( p_input, sizeof( stream_ps_data_t ) );
242 /* disc input method */
243 p_input->stream.i_method = INPUT_METHOD_DVD;
245 #define title_inf p_dvd->p_ifo->vmg.title_inf
246 intf_WarnMsg( 2, "dvd info: number of titles: %d", title_inf.i_title_nb );
248 #define area p_input->stream.pp_areas
249 /* We start from 1 here since the default area 0
250 * is reserved for video_ts.vob */
251 for( i = 1 ; i <= title_inf.i_title_nb ; i++ )
253 input_AddArea( p_input );
255 /* Titles are Program Chains */
258 /* Absolute start offset and size
259 * We can only set that with vts ifo, so we do it during the
260 * first call to DVDSetArea */
261 area[i]->i_start = 0;
264 /* Number of chapters */
265 area[i]->i_part_nb = title_inf.p_attr[i-1].i_chapter_nb;
268 /* Number of angles */
269 area[i]->i_angle_nb = 0;
270 area[i]->i_angle = 1;
272 /* Offset to vts_i_0.ifo */
273 area[i]->i_plugin_data = p_dvd->p_ifo->i_start +
274 title_inf.p_attr[i-1].i_start_sector;
278 /* Get requested title - if none try the first title */
279 i_title = main_GetIntVariable( INPUT_TITLE_VAR, 1 );
280 if( i_title <= 0 || i_title > title_inf.i_title_nb )
287 /* Get requested chapter - if none defaults to first one */
288 i_chapter = main_GetIntVariable( INPUT_CHAPTER_VAR, 1 );
294 p_input->stream.pp_areas[i_title]->i_part = i_chapter;
296 p_area = p_input->stream.pp_areas[i_title];
298 /* set title, chapter, audio and subpic */
299 DVDSetArea( p_input, p_area );
301 vlc_mutex_unlock( &p_input->stream.stream_lock );
306 /*****************************************************************************
308 *****************************************************************************/
309 static void DVDOpen( struct input_thread_s *p_input )
311 dvdcss_handle dvdhandle;
313 vlc_mutex_lock( &p_input->stream.stream_lock );
315 /* If we are here we can control the pace... */
316 p_input->stream.b_pace_control = 1;
318 p_input->stream.b_seekable = 1;
319 p_input->stream.p_selected_area->i_size = 0;
321 p_input->stream.p_selected_area->i_tell = 0;
323 vlc_mutex_unlock( &p_input->stream.stream_lock );
325 /* XXX: put this shit in an access plugin */
326 if( strlen( p_input->p_source ) > 4
327 && !strncasecmp( p_input->p_source, "dvd:", 4 ) )
329 dvdhandle = dvdcss_open( p_input->p_source + 4, DVDCSS_INIT_QUIET );
333 dvdhandle = dvdcss_open( p_input->p_source, DVDCSS_INIT_QUIET );
336 if( dvdhandle == NULL )
338 p_input->b_error = 1;
342 p_input->i_handle = (int) dvdhandle;
345 /*****************************************************************************
346 * DVDClose: close dvd
347 *****************************************************************************/
348 static void DVDClose( struct input_thread_s *p_input )
350 /* Clean up libdvdcss */
351 dvdcss_close( (dvdcss_handle) p_input->i_handle );
354 /*****************************************************************************
355 * DVDEnd: frees unused data
356 *****************************************************************************/
357 static void DVDEnd( input_thread_t * p_input )
359 thread_dvd_data_t * p_dvd;
360 dvd_netlist_t * p_netlist;
362 p_dvd = (thread_dvd_data_t*)p_input->p_plugin_data;
363 p_netlist = (dvd_netlist_t *)p_input->p_method_data;
365 IfoDestroy( p_dvd->p_ifo );
369 DVDNetlistEnd( p_netlist );
372 /*****************************************************************************
373 * DVDSetArea: initialize input data for title x, chapter y.
374 * It should be called for each user navigation request.
375 *****************************************************************************
376 * Take care that i_title starts from 0 (vmg) and i_chapter start from 1.
377 * Note that you have to take the lock before entering here.
378 *****************************************************************************/
379 static int DVDSetArea( input_thread_t * p_input, input_area_t * p_area )
381 thread_dvd_data_t * p_dvd;
382 es_descriptor_t * p_es;
389 p_dvd = (thread_dvd_data_t*)p_input->p_plugin_data;
391 /* we can't use the interface slider until initilization is complete */
392 p_input->stream.b_seekable = 0;
394 if( p_area != p_input->stream.p_selected_area )
396 /* Reset the Chapter position of the old title */
397 p_input->stream.p_selected_area->i_part = 0;
400 * We have to load all title information
402 /* Change the default area */
403 p_input->stream.p_selected_area =
404 p_input->stream.pp_areas[p_area->i_id];
406 /* title number: it is not vts nb!,
407 * it is what appears in the interface list */
408 p_dvd->i_title = p_area->i_id;
409 p_dvd->p_ifo->i_title = p_dvd->i_title;
411 /* set number of chapters of current title */
412 p_dvd->i_chapter_nb = p_area->i_part_nb;
415 if( IfoTitleSet( p_dvd->p_ifo ) < 0 )
417 intf_ErrMsg( "dvd error: fatal error in vts ifo" );
419 p_input->b_error = 1;
423 #define vmg p_dvd->p_ifo->vmg
424 #define vts p_dvd->p_ifo->vts
425 /* title position inside the selected vts */
426 i_vts_title = vmg.title_inf.p_attr[p_dvd->i_title-1].i_title_num;
428 vts.title_inf.p_title_start[i_vts_title-1].i_title_id;
430 intf_WarnMsgImm( 3, "dvd: title %d vts_title %d pgc %d",
431 p_dvd->i_title, i_vts_title, p_dvd->i_title_id );
434 * Tell libdvdcss we changed title
436 dvdcss_title( p_dvd->dvdhandle,
437 vts.i_pos + vts.manager_inf.i_title_vob_start_sector );
442 p_dvd->i_angle_nb = vmg.title_inf.p_attr[p_dvd->i_title-1].i_angle_nb;
443 p_dvd->i_angle = main_GetIntVariable( INPUT_ANGLE_VAR, 1 );
444 if( ( p_dvd->i_angle <= 0 ) || p_dvd->i_angle > p_dvd->i_angle_nb )
450 * Set selected title start and size
453 /* title set offset XXX: convert to block values */
454 p_dvd->i_title_start =
455 vts.i_pos + vts.manager_inf.i_title_vob_start_sector;
457 /* last video cell */
459 p_dvd->i_prg_cell = -1 +
460 vts.title_unit.p_title[p_dvd->i_title_id-1].title.i_cell_nb;
462 if( DVDFindCell( p_dvd ) < 0 )
464 intf_ErrMsg( "dvd error: can't find title end" );
465 p_input->b_error = 1;
469 /* temporary hack to fix size in some dvds */
470 if( p_dvd->i_cell >= vts.cell_inf.i_cell_nb )
472 p_dvd->i_cell = vts.cell_inf.i_cell_nb - 1;
476 p_dvd->i_size = vts.cell_inf.p_cell_map[p_dvd->i_cell].i_end_sector;
478 if( DVDChapterSelect( p_dvd, 1 ) < 0 )
480 intf_ErrMsg( "dvd error: can't find first chapter" );
481 p_input->b_error = 1;
485 p_dvd->i_size -= p_dvd->i_sector + 1;
487 IfoPrintTitle( p_dvd );
489 /* Area definition */
490 p_input->stream.p_selected_area->i_start = LB2OFF( p_dvd->i_start );
491 p_input->stream.p_selected_area->i_size = LB2OFF( p_dvd->i_size );
492 p_input->stream.p_selected_area->i_angle_nb = p_dvd->i_angle_nb;
493 p_input->stream.p_selected_area->i_angle = p_dvd->i_angle;
496 /* start at the beginning of the title */
497 /* FIXME: create a conf option to select whether to restart
499 p_input->stream.p_selected_area->i_tell = 0;
500 p_input->stream.p_selected_area->i_part = 1;
504 * Destroy obsolete ES by reinitializing program 0
505 * and find all ES in title with ifo data
507 if( p_input->stream.pp_programs != NULL )
509 /* We don't use input_EndStream here since
510 * we keep area structures */
512 for( i = 0 ; i < p_input->stream.i_selected_es_number ; i++ )
514 input_UnselectES( p_input, p_input->stream.pp_selected_es[i] );
517 input_DelProgram( p_input, p_input->stream.pp_programs[0] );
519 p_input->stream.pp_selected_es = NULL;
520 p_input->stream.i_selected_es_number = 0;
523 input_AddProgram( p_input, 0, sizeof( stream_ps_data_t ) );
525 /* No PSM to read in DVD mode, we already have all information */
526 p_input->stream.pp_programs[0]->b_is_ok = 1;
530 /* ES 0 -> video MPEG2 */
531 IfoPrintVideo( p_dvd );
533 p_es = input_AddES( p_input, p_input->stream.pp_programs[0], 0xe0, 0 );
534 p_es->i_stream_id = 0xe0;
535 p_es->i_type = MPEG2_VIDEO_ES;
536 p_es->i_cat = VIDEO_ES;
537 if( p_main->b_video )
539 input_SelectES( p_input, p_es );
542 #define audio_status \
543 vts.title_unit.p_title[p_dvd->i_title_id-1].title.pi_audio_status[i-1]
544 /* Audio ES, in the order they appear in .ifo */
545 for( i = 1 ; i <= vts.manager_inf.i_audio_nb ; i++ )
547 IfoPrintAudio( p_dvd, i );
549 /* audio channel is active if first byte is 0x80 */
550 if( audio_status.i_available )
552 switch( vts.manager_inf.p_audio_attr[i-1].i_coding_mode )
555 i_id = ( ( 0x80 + audio_status.i_position ) << 8 ) | 0xbd;
556 p_es = input_AddES( p_input,
557 p_input->stream.pp_programs[0], i_id, 0 );
558 p_es->i_stream_id = 0xbd;
559 p_es->i_type = AC3_AUDIO_ES;
561 p_es->i_cat = AUDIO_ES;
562 strcpy( p_es->psz_desc, IfoLanguage( hton16(
563 vts.manager_inf.p_audio_attr[i-1].i_lang_code ) ) );
564 strcat( p_es->psz_desc, " (ac3)" );
568 case 0x03: /* MPEG audio */
569 i_id = 0xc0 + audio_status.i_position;
570 p_es = input_AddES( p_input,
571 p_input->stream.pp_programs[0], i_id, 0 );
572 p_es->i_stream_id = i_id;
573 p_es->i_type = MPEG2_AUDIO_ES;
575 p_es->i_cat = AUDIO_ES;
576 strcpy( p_es->psz_desc, IfoLanguage( hton16(
577 vts.manager_inf.p_audio_attr[i-1].i_lang_code ) ) );
578 strcat( p_es->psz_desc, " (mpeg)" );
581 case 0x04: /* LPCM */
583 i_id = ( ( 0xa0 + audio_status.i_position ) << 8 ) | 0xbd;
584 p_es = input_AddES( p_input,
585 p_input->stream.pp_programs[0], i_id, 0 );
586 p_es->i_stream_id = i_id;
587 p_es->i_type = LPCM_AUDIO_ES;
589 p_es->i_cat = AUDIO_ES;
590 strcpy( p_es->psz_desc, IfoLanguage( hton16(
591 vts.manager_inf.p_audio_attr[i-1].i_lang_code ) ) );
592 strcat( p_es->psz_desc, " (lpcm)" );
596 i_id = ( ( 0x88 + audio_status.i_position ) << 8 ) | 0xbd;
597 intf_ErrMsg( "dvd warning: DTS audio not handled yet"
602 intf_ErrMsg( "dvd warning: unknown audio type %.2x",
603 vts.manager_inf.p_audio_attr[i-1].i_coding_mode );
609 vts.title_unit.p_title[p_dvd->i_title_id-1].title.pi_spu_status[i-1]
613 for( i = 1 ; i <= vts.manager_inf.i_spu_nb; i++ )
615 IfoPrintSpu( p_dvd, i );
617 if( spu_status.i_available )
619 /* there are several streams for one spu */
620 if( vts.manager_inf.video_attr.i_ratio )
623 switch( vts.manager_inf.video_attr.i_perm_displ )
626 i_id = ( ( 0x20 + spu_status.i_position_pan ) << 8 )
630 i_id = ( ( 0x20 + spu_status.i_position_letter ) << 8 )
634 i_id = ( ( 0x20 + spu_status.i_position_wide ) << 8 )
642 i_id = ( ( 0x20 + spu_status.i_position_43 ) << 8 )
645 p_es = input_AddES( p_input,
646 p_input->stream.pp_programs[0], i_id, 0 );
647 p_es->i_stream_id = 0xbd;
648 p_es->i_type = DVD_SPU_ES;
649 p_es->i_cat = SPU_ES;
650 strcpy( p_es->psz_desc, IfoLanguage( hton16(
651 vts.manager_inf.p_spu_attr[i-1].i_lang_code ) ) );
655 if( p_main->b_audio )
657 /* For audio: first one if none or a not existing one specified */
658 i_audio = main_GetIntVariable( INPUT_CHANNEL_VAR, 1 );
659 if( i_audio < 0 || i_audio > vts.manager_inf.i_audio_nb )
661 main_PutIntVariable( INPUT_CHANNEL_VAR, 1 );
664 if( i_audio > 0 && vts.manager_inf.i_audio_nb > 0 )
666 input_SelectES( p_input, p_input->stream.pp_es[i_audio] );
670 if( p_main->b_video )
672 /* for spu, default is none */
673 i_spu = main_GetIntVariable( INPUT_SUBTITLE_VAR, 0 );
674 if( i_spu < 0 || i_spu > vts.manager_inf.i_spu_nb )
676 main_PutIntVariable( INPUT_SUBTITLE_VAR, 0 );
679 if( i_spu > 0 && vts.manager_inf.i_spu_nb > 0 )
681 i_spu += vts.manager_inf.i_audio_nb;
682 input_SelectES( p_input, p_input->stream.pp_es[i_spu] );
688 p_area = p_input->stream.p_selected_area;
697 if( p_area->i_part != p_dvd->i_chapter )
699 if( ( p_area->i_part > 0 ) &&
700 ( p_area->i_part <= p_area->i_part_nb ))
702 if( DVDChapterSelect( p_dvd, p_area->i_part ) < 0 )
704 intf_ErrMsg( "dvd error: can't set chapter in area" );
705 p_input->b_error = 1;
709 p_input->stream.p_selected_area->i_tell =
710 LB2OFF( p_dvd->i_start ) - p_area->i_start;
711 p_input->stream.p_selected_area->i_part = p_dvd->i_chapter;
713 intf_WarnMsg( 4, "dvd info: chapter %d start at: %lld",
714 p_area->i_part, p_area->i_tell );
719 p_dvd->i_chapter = 1;
724 p_dvd->p_ifo->vts.title_unit.p_title[p_dvd->i_title_id-1].title
725 if( p_area->i_angle != p_dvd->i_angle )
727 if( title.p_cell_play[p_dvd->i_prg_cell].i_category & 0xf000 )
729 if( ( p_area->i_angle - p_dvd->i_angle ) < 0 )
733 p_dvd->i_prg_cell += ( p_area->i_angle - p_dvd->i_angle );
734 p_dvd->i_angle = p_area->i_angle;
736 DVDFindSector( p_dvd );
737 p_dvd->i_cell += p_dvd->i_angle_cell;
741 p_dvd->i_angle = p_area->i_angle;
744 intf_WarnMsg( 3, "dvd info: angle %d selected", p_area->i_angle );
747 /* warn interface that something has changed */
748 p_input->stream.b_seekable = 1;
749 p_input->stream.b_changed = 1;
755 /*****************************************************************************
756 * DVDRead: reads data packets into the netlist.
757 *****************************************************************************
758 * Returns -1 in case of error, 0 if everything went well, and 1 in case of
760 *****************************************************************************/
761 static int DVDRead( input_thread_t * p_input,
762 data_packet_t ** pp_packets )
764 thread_dvd_data_t * p_dvd;
765 dvd_netlist_t * p_netlist;
766 struct iovec * p_vec;
767 struct data_packet_s * pp_data[DVD_DATA_READ_ONCE];
780 p_dvd = (thread_dvd_data_t *)p_input->p_plugin_data;
781 p_netlist = (dvd_netlist_t *)p_input->p_method_data;
784 i_sector = p_dvd->i_title_start + p_dvd->i_sector;
785 i_block_once = p_dvd->i_end_sector - p_dvd->i_sector + 1;
787 /* Get the position of the next cell if we're at cell end */
788 if( i_block_once <= 0 )
793 p_dvd->i_angle_cell++;
795 /* Find cell index in adress map */
796 if( DVDFindSector( p_dvd ) < 0 )
798 pp_packets[0] = NULL;
799 intf_ErrMsg( "dvd error: can't find next cell" );
803 /* Position the fd pointer on the right address */
804 i_sector = dvdcss_seek( p_dvd->dvdhandle,
805 p_dvd->i_title_start + p_dvd->i_sector );
807 /* update chapter : it will be easier when we have navigation
809 if( p_dvd->i_chapter < ( p_dvd->i_chapter_nb - 1 ) )
811 if( title.p_cell_play[p_dvd->i_prg_cell].i_category & 0xf000 )
813 i_angle = p_dvd->i_angle - 1;
819 if( title.chapter_map.pi_start_cell[p_dvd->i_chapter] <=
820 ( p_dvd->i_prg_cell - i_angle + 1 ) )
827 i_block_once = p_dvd->i_end_sector - p_dvd->i_sector + 1;
830 /* The number of blocks read is the max between the requested
831 * value and the leaving block in the cell */
832 if( i_block_once > p_dvd->i_block_once )
834 i_block_once = p_dvd->i_block_once;
837 intf_WarnMsg( 2, "Sector: 0x%x Read: %d Chapter: %d", p_dvd->i_sector, i_block_once, p_dvd->i_chapter );
839 p_netlist->i_read_once = i_block_once;
841 /* Get an iovec pointer */
842 if( ( p_vec = DVDGetiovec( p_netlist ) ) == NULL )
844 intf_ErrMsg( "dvd error: can't get iovec" );
849 i_read_blocks = dvdcss_readv( p_dvd->dvdhandle, p_vec,
850 i_block_once, DVDCSS_READ_DECRYPT );
852 /* Update netlist indexes: we don't do it in DVDGetiovec since we
853 * need know the real number of blocks read */
854 DVDMviovec( p_netlist, i_read_blocks, pp_data );
856 /* Update global position */
857 p_dvd->i_sector += i_read_blocks;
861 /* Read headers to compute payload length */
862 for( i_iovec = 0 ; i_iovec < i_read_blocks ; i_iovec++ )
866 while( i_pos < p_netlist->i_buffer_size )
868 pi_cur = (u8*)p_vec[i_iovec].iov_base + i_pos;
871 if( U32_AT( pi_cur ) != 0x1BA )
873 /* That's the case for all packets, except pack header. */
874 i_packet_size = U16_AT( pi_cur + 4 );
875 pp_packets[i_packet] = DVDNewPtr( p_netlist );
879 /* MPEG-2 Pack header. */
881 pp_packets[i_packet] = pp_data[i_iovec];
885 (*pp_data[i_iovec]->pi_refcount)++;
887 pp_packets[i_packet]->pi_refcount = pp_data[i_iovec]->pi_refcount;
889 pp_packets[i_packet]->p_buffer = pp_data[i_iovec]->p_buffer;
891 pp_packets[i_packet]->p_payload_start =
892 pp_packets[i_packet]->p_buffer + i_pos;
894 pp_packets[i_packet]->p_payload_end =
895 pp_packets[i_packet]->p_payload_start + i_packet_size + 6;
897 pp_packets[i_packet]->p_next = NULL;
898 pp_packets[i_packet]->b_discard_payload = 0;
901 i_pos += i_packet_size + 6;
905 pp_packets[i_packet] = NULL;
907 vlc_mutex_lock( &p_input->stream.stream_lock );
909 p_input->stream.p_selected_area->i_tell =
910 LB2OFF( i_sector + i_read_blocks ) -
911 p_input->stream.p_selected_area->i_start;
914 /* We modify i_part only at end of chapter not to erase
915 * some modification from the interface */
916 p_input->stream.p_selected_area->i_part = p_dvd->i_chapter;
919 b_eot = !( p_input->stream.p_selected_area->i_tell
920 < p_input->stream.p_selected_area->i_size );
921 b_eof = b_eot && ( ( p_dvd->i_title + 1 ) >= p_input->stream.i_area_nb );
925 vlc_mutex_unlock( &p_input->stream.stream_lock );
931 intf_WarnMsg( 4, "dvd info: new title" );
933 DVDSetArea( p_input, p_input->stream.pp_areas[p_dvd->i_title] );
934 vlc_mutex_unlock( &p_input->stream.stream_lock );
938 vlc_mutex_unlock( &p_input->stream.stream_lock );
940 if( i_read_blocks == i_block_once )
948 /*****************************************************************************
949 * DVDRewind : reads a stream backward
950 *****************************************************************************/
951 static int DVDRewind( input_thread_t * p_input )
956 /*****************************************************************************
957 * DVDSeek : Goes to a given position on the stream.
958 *****************************************************************************
959 * This one is used by the input and translate chronological position from
960 * input to logical position on the device.
961 * The lock should be taken before calling this function.
962 *****************************************************************************/
963 static void DVDSeek( input_thread_t * p_input, off_t i_off )
965 thread_dvd_data_t * p_dvd;
971 p_dvd = ( thread_dvd_data_t * )p_input->p_plugin_data;
973 /* we have to take care of offset of beginning of title */
974 p_dvd->i_sector = OFF2LB(i_off + p_input->stream.p_selected_area->i_start)
975 - p_dvd->i_title_start;
980 /* parse vobu address map to find program cell */
981 while( title.p_cell_play[i_prg_cell].i_end_sector < p_dvd->i_sector )
986 p_dvd->i_prg_cell = i_prg_cell;
988 if( DVDChooseAngle( p_dvd ) < 0 )
990 p_input->b_error = 1;
996 /* Find first title cell which is inside program cell */
997 if( DVDFindCell( p_dvd ) < 0 )
999 /* no following cell : we're at eof */
1000 intf_ErrMsg( "dvd error: cell seeking failed" );
1001 p_input->b_error = 1;
1005 i_cell = p_dvd->i_cell;
1007 #define cell p_dvd->p_ifo->vts.cell_inf.p_cell_map[i_cell]
1008 /* parse cell address map to find title cell containing sector */
1009 while( cell.i_end_sector < p_dvd->i_sector )
1014 p_dvd->i_cell = i_cell;
1016 /* if we're inside a multi-angle zone, we have to choose i_sector
1017 * in the current angle ; we can't do it all the time since cells
1018 * can be very wide out of such zones */
1019 if( title.p_cell_play[p_dvd->i_prg_cell].i_category & 0xf000 )
1021 p_dvd->i_sector = MAX(
1022 cell.i_start_sector,
1023 title.p_cell_play[p_dvd->i_prg_cell].i_start_sector );
1026 p_dvd->i_end_sector = MIN(
1028 title.p_cell_play[p_dvd->i_prg_cell].i_end_sector );
1030 /* update chapter */
1031 if( title.p_cell_play[p_dvd->i_prg_cell].i_category & 0xf000 )
1033 i_angle = p_dvd->i_angle - 1;
1039 if( p_dvd->i_chapter_nb > 1 )
1041 while( ( title.chapter_map.pi_start_cell[i_chapter] <=
1042 ( p_dvd->i_prg_cell - i_angle + 1 ) ) &&
1043 ( i_chapter < ( p_dvd->i_chapter_nb - 1 ) ) )
1053 p_dvd->i_chapter = i_chapter;
1054 p_input->stream.p_selected_area->i_part = p_dvd->i_chapter;
1056 p_input->stream.p_selected_area->i_tell =
1057 LB2OFF ( dvdcss_seek( p_dvd->dvdhandle, p_dvd->i_title_start
1058 + p_dvd->i_sector ) )
1059 - p_input->stream.p_selected_area->i_start;
1061 intf_WarnMsg( 7, "Program Cell: %d Cell: %d Chapter: %d",
1062 p_dvd->i_prg_cell, p_dvd->i_cell, p_dvd->i_chapter );
1068 #define cell p_dvd->p_ifo->vts.cell_inf
1070 /*****************************************************************************
1071 * DVDFindCell: adjust the title cell index with the program cell
1072 *****************************************************************************/
1073 static int DVDFindCell( thread_dvd_data_t * p_dvd )
1078 i_cell = p_dvd->i_cell;
1079 i_index = p_dvd->i_prg_cell;
1081 if( i_cell >= cell.i_cell_nb )
1086 while( ( ( title.p_cell_pos[i_index].i_vob_id !=
1087 cell.p_cell_map[i_cell].i_vob_id ) ||
1088 ( title.p_cell_pos[i_index].i_cell_id !=
1089 cell.p_cell_map[i_cell].i_cell_id ) ) &&
1090 ( i_cell < cell.i_cell_nb - 1 ) )
1096 intf_WarnMsg( 7, "FindCell: i_cell %d i_index %d found %d nb %d",
1103 p_dvd->i_cell = i_cell;
1110 /*****************************************************************************
1111 * DVDFindSector: find cell index in adress map from index in
1112 * information table program map and give corresponding sectors.
1113 *****************************************************************************/
1114 static int DVDFindSector( thread_dvd_data_t * p_dvd )
1117 if( p_dvd->i_sector > title.p_cell_play[p_dvd->i_prg_cell].i_end_sector )
1119 p_dvd->i_prg_cell++;
1121 if( DVDChooseAngle( p_dvd ) < 0 )
1127 if( DVDFindCell( p_dvd ) < 0 )
1129 intf_ErrMsg( "dvd error: can't find sector" );
1133 /* Find start and end sectors of new cell */
1135 p_dvd->i_sector = MAX(
1136 p_dvd->p_ifo->vts.cell_inf.p_cell_map[p_dvd->i_cell].i_start_sector,
1137 title.p_cell_play[p_dvd->i_prg_cell].i_start_sector );
1138 p_dvd->i_end_sector = MIN(
1139 p_dvd->p_ifo->vts.cell_inf.p_cell_map[p_dvd->i_cell].i_end_sector,
1140 title.p_cell_play[p_dvd->i_prg_cell].i_end_sector );
1142 p_dvd->i_sector = title.p_cell_play[p_dvd->i_prg_cell].i_start_sector;
1143 p_dvd->i_end_sector = title.p_cell_play[p_dvd->i_prg_cell].i_end_sector;
1147 intf_WarnMsg( 7, "cell: %d sector1: 0x%x end1: 0x%x\n"
1148 "index: %d sector2: 0x%x end2: 0x%x\n"
1149 "category: 0x%x ilvu end: 0x%x vobu start 0x%x",
1151 p_dvd->p_ifo->vts.cell_inf.p_cell_map[p_dvd->i_cell].i_start_sector,
1152 p_dvd->p_ifo->vts.cell_inf.p_cell_map[p_dvd->i_cell].i_end_sector,
1154 title.p_cell_play[p_dvd->i_prg_cell].i_start_sector,
1155 title.p_cell_play[p_dvd->i_prg_cell].i_end_sector,
1156 title.p_cell_play[p_dvd->i_prg_cell].i_category,
1157 title.p_cell_play[p_dvd->i_prg_cell].i_first_ilvu_vobu_esector,
1158 title.p_cell_play[p_dvd->i_prg_cell].i_last_vobu_start_sector );
1164 /*****************************************************************************
1165 * DVDChapterSelect: find the cell corresponding to requested chapter
1166 *****************************************************************************/
1167 static int DVDChapterSelect( thread_dvd_data_t * p_dvd, int i_chapter )
1170 /* Find cell index in Program chain for current chapter */
1171 p_dvd->i_prg_cell = title.chapter_map.pi_start_cell[i_chapter-1] - 1;
1173 p_dvd->i_sector = 0;
1175 DVDChooseAngle( p_dvd );
1177 /* Search for cell_index in cell adress_table and initialize
1179 if( DVDFindSector( p_dvd ) < 0 )
1181 intf_ErrMsg( "dvd error: can't select chapter" );
1185 /* start is : beginning of vts vobs + offset to vob x */
1186 p_dvd->i_start = p_dvd->i_title_start + p_dvd->i_sector;
1188 /* Position the fd pointer on the right address */
1189 p_dvd->i_start = dvdcss_seek( p_dvd->dvdhandle, p_dvd->i_start );
1191 p_dvd->i_chapter = i_chapter;
1195 /*****************************************************************************
1196 * DVDChooseAngle: select the cell corresponding to the selected angle
1197 *****************************************************************************/
1198 static int DVDChooseAngle( thread_dvd_data_t * p_dvd )
1200 /* basic handling of angles */
1201 switch( ( ( title.p_cell_play[p_dvd->i_prg_cell].i_category & 0xf000 )
1204 /* we enter a muli-angle section */
1206 p_dvd->i_prg_cell += p_dvd->i_angle - 1;
1207 p_dvd->i_angle_cell = 0;
1209 /* we exit a multi-angle section */
1212 p_dvd->i_prg_cell += p_dvd->i_angle_nb - p_dvd->i_angle;