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.98 2001/11/19 15:13:11 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 #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>
76 # include "input_iovec.h"
83 #include "stream_control.h"
84 #include "input_ext-intf.h"
85 #include "input_ext-dec.h"
86 #include "input_ext-plugins.h"
88 #include "input_dvd.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 = input_NetlistNewPacket;
141 input.pf_new_pes = input_NetlistNewPES;
142 input.pf_delete_packet = input_NetlistDeletePacket;
143 input.pf_delete_pes = input_NetlistDeletePES;
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->p_handle;
202 if( dvdcss_seek( p_dvd->dvdhandle, 0, DVDCSS_NOFLAGS ) < 0 )
204 intf_ErrMsg( "dvd error: %s", dvdcss_error( p_dvd->dvdhandle ) );
205 p_input->b_error = 1;
209 /* We read DVD_BLOCK_READ_ONCE in each loop, so the input will receive
210 * DVD_DATA_READ_ONCE at most */
211 p_dvd->i_block_once = DVD_BLOCK_READ_ONCE;
212 /* this value mustn't be modifed */
213 p_input->i_read_once = DVD_DATA_READ_ONCE;
215 /* Reading structures initialisation */
216 input_NetlistInit( p_input, DVD_NETLIST_SIZE, 2 * DVD_NETLIST_SIZE,
217 DVD_NETLIST_SIZE, DVD_LB_SIZE, p_dvd->i_block_once );
218 intf_WarnMsg( 2, "dvd info: netlist initialized" );
220 /* Ifo allocation & initialisation */
221 if( IfoCreate( p_dvd ) < 0 )
223 intf_ErrMsg( "dvd error: allcation error in ifo" );
224 dvdcss_close( p_dvd->dvdhandle );
226 p_input->b_error = 1;
230 if( IfoInit( p_dvd->p_ifo ) < 0 )
232 intf_ErrMsg( "dvd error: fatal failure in ifo" );
233 IfoDestroy( p_dvd->p_ifo );
234 dvdcss_close( p_dvd->dvdhandle );
236 p_input->b_error = 1;
240 /* Set stream and area data */
241 vlc_mutex_lock( &p_input->stream.stream_lock );
243 /* Initialize ES structures */
244 input_InitStream( p_input, sizeof( stream_ps_data_t ) );
246 #define title_inf p_dvd->p_ifo->vmg.title_inf
247 intf_WarnMsg( 2, "dvd info: number of titles: %d", title_inf.i_title_nb );
249 #define area p_input->stream.pp_areas
250 /* We start from 1 here since the default area 0
251 * is reserved for video_ts.vob */
252 for( i = 1 ; i <= title_inf.i_title_nb ; i++ )
254 input_AddArea( p_input );
256 /* Titles are Program Chains */
259 /* Absolute start offset and size
260 * We can only set that with vts ifo, so we do it during the
261 * first call to DVDSetArea */
262 area[i]->i_start = 0;
265 /* Number of chapters */
266 area[i]->i_part_nb = title_inf.p_attr[i-1].i_chapter_nb;
269 /* Number of angles */
270 area[i]->i_angle_nb = 0;
271 area[i]->i_angle = 1;
273 /* Offset to vts_i_0.ifo */
274 area[i]->i_plugin_data = p_dvd->p_ifo->i_start +
275 title_inf.p_attr[i-1].i_start_sector;
279 /* Get requested title - if none try the first title */
280 i_title = main_GetIntVariable( INPUT_TITLE_VAR, 1 );
281 if( i_title <= 0 || i_title > title_inf.i_title_nb )
288 /* Get requested chapter - if none defaults to first one */
289 i_chapter = main_GetIntVariable( INPUT_CHAPTER_VAR, 1 );
295 p_input->stream.pp_areas[i_title]->i_part = i_chapter;
297 p_area = p_input->stream.pp_areas[i_title];
299 /* set title, chapter, audio and subpic */
300 DVDSetArea( p_input, p_area );
302 vlc_mutex_unlock( &p_input->stream.stream_lock );
307 /*****************************************************************************
309 *****************************************************************************/
310 static void DVDOpen( struct input_thread_s *p_input )
312 dvdcss_handle dvdhandle;
314 vlc_mutex_lock( &p_input->stream.stream_lock );
316 /* If we are here we can control the pace... */
317 p_input->stream.b_pace_control = 1;
319 p_input->stream.b_seekable = 1;
320 p_input->stream.p_selected_area->i_size = 0;
322 p_input->stream.p_selected_area->i_tell = 0;
324 vlc_mutex_unlock( &p_input->stream.stream_lock );
326 /* XXX: put this shit in an access plugin */
327 if( strlen( p_input->p_source ) > 4
328 && !strncasecmp( p_input->p_source, "dvd:", 4 ) )
330 dvdhandle = dvdcss_open( p_input->p_source + 4 );
334 dvdhandle = dvdcss_open( p_input->p_source );
337 if( dvdhandle == NULL )
339 intf_ErrMsg( "dvd error: dvdcss can't open device" );
340 p_input->b_error = 1;
344 p_input->p_handle = (void *) dvdhandle;
347 /*****************************************************************************
348 * DVDClose: close dvd
349 *****************************************************************************/
350 static void DVDClose( struct input_thread_s *p_input )
352 /* Clean up libdvdcss */
353 dvdcss_close( (dvdcss_handle) p_input->p_handle );
356 /*****************************************************************************
357 * DVDEnd: frees unused data
358 *****************************************************************************/
359 static void DVDEnd( input_thread_t * p_input )
361 thread_dvd_data_t * p_dvd;
363 p_dvd = (thread_dvd_data_t*)p_input->p_plugin_data;
365 IfoDestroy( p_dvd->p_ifo );
369 input_NetlistEnd( p_input );
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;
391 p_dvd = (thread_dvd_data_t*)p_input->p_plugin_data;
393 /* we can't use the interface slider until initilization is complete */
394 p_input->stream.b_seekable = 0;
396 if( p_area != p_input->stream.p_selected_area )
398 /* Reset the Chapter position of the old title */
399 p_input->stream.p_selected_area->i_part = 0;
402 * We have to load all title information
404 /* Change the default area */
405 p_input->stream.p_selected_area =
406 p_input->stream.pp_areas[p_area->i_id];
408 /* title number: it is not vts nb!,
409 * it is what appears in the interface list */
410 p_dvd->i_title = p_area->i_id;
411 p_dvd->p_ifo->i_title = p_dvd->i_title;
413 /* set number of chapters of current title */
414 p_dvd->i_chapter_nb = p_area->i_part_nb;
417 if( IfoTitleSet( p_dvd->p_ifo ) < 0 )
419 intf_ErrMsg( "dvd error: fatal error in vts ifo" );
421 p_input->b_error = 1;
425 #define vmg p_dvd->p_ifo->vmg
426 #define vts p_dvd->p_ifo->vts
427 /* title position inside the selected vts */
428 i_vts_title = vmg.title_inf.p_attr[p_dvd->i_title-1].i_title_num;
430 vts.title_inf.p_title_start[i_vts_title-1].i_title_id;
432 intf_WarnMsgImm( 3, "dvd: title %d vts_title %d pgc %d",
433 p_dvd->i_title, i_vts_title, p_dvd->i_title_id );
439 p_dvd->i_angle_nb = vmg.title_inf.p_attr[p_dvd->i_title-1].i_angle_nb;
440 p_dvd->i_angle = main_GetIntVariable( INPUT_ANGLE_VAR, 1 );
441 if( ( p_dvd->i_angle <= 0 ) || p_dvd->i_angle > p_dvd->i_angle_nb )
447 * Set selected title start and size
450 /* title set offset XXX: convert to block values */
451 p_dvd->i_title_start =
452 vts.i_pos + vts.manager_inf.i_title_vob_start_sector;
454 /* last video cell */
456 p_dvd->i_prg_cell = -1 +
457 vts.title_unit.p_title[p_dvd->i_title_id-1].title.i_cell_nb;
459 if( DVDFindCell( p_dvd ) < 0 )
461 intf_ErrMsg( "dvd error: can't find title end" );
462 p_input->b_error = 1;
466 /* temporary hack to fix size in some dvds */
467 if( p_dvd->i_cell >= vts.cell_inf.i_cell_nb )
469 p_dvd->i_cell = vts.cell_inf.i_cell_nb - 1;
473 p_dvd->i_size = vts.cell_inf.p_cell_map[p_dvd->i_cell].i_end_sector;
475 if( DVDChapterSelect( p_dvd, 1 ) < 0 )
477 intf_ErrMsg( "dvd error: can't find first chapter" );
478 p_input->b_error = 1;
482 /* Force libdvdcss to check its title key.
483 * It is only useful for title cracking method. Methods using the
484 * decrypted disc key are fast enough to check the key at each seek */
486 if( dvdcss_seek( p_dvd->dvdhandle, p_dvd->i_start,
487 DVDCSS_SEEK_KEY ) < 0 )
489 intf_ErrMsg( "dvd error: %s", dvdcss_error( p_dvd->dvdhandle ) );
493 p_dvd->i_size -= p_dvd->i_sector + 1;
495 IfoPrintTitle( p_dvd );
497 /* Area definition */
498 p_input->stream.p_selected_area->i_start = LB2OFF( p_dvd->i_start );
499 p_input->stream.p_selected_area->i_size = LB2OFF( p_dvd->i_size );
500 p_input->stream.p_selected_area->i_angle_nb = p_dvd->i_angle_nb;
501 p_input->stream.p_selected_area->i_angle = p_dvd->i_angle;
504 /* start at the beginning of the title */
505 /* FIXME: create a conf option to select whether to restart
507 p_input->stream.p_selected_area->i_tell = 0;
508 p_input->stream.p_selected_area->i_part = 1;
512 * Destroy obsolete ES by reinitializing program 0
513 * and find all ES in title with ifo data
515 if( p_input->stream.pp_programs != NULL )
517 /* We don't use input_EndStream here since
518 * we keep area structures */
520 for( i = 0 ; i < p_input->stream.i_selected_es_number ; i++ )
522 input_UnselectES( p_input, p_input->stream.pp_selected_es[i] );
525 free( p_input->stream.pp_selected_es );
526 input_DelProgram( p_input, p_input->stream.pp_programs[0] );
528 p_input->stream.pp_selected_es = NULL;
529 p_input->stream.i_selected_es_number = 0;
532 input_AddProgram( p_input, 0, sizeof( stream_ps_data_t ) );
534 /* No PSM to read in DVD mode, we already have all information */
535 p_input->stream.pp_programs[0]->b_is_ok = 1;
539 /* ES 0 -> video MPEG2 */
540 IfoPrintVideo( p_dvd );
542 p_es = input_AddES( p_input, p_input->stream.pp_programs[0], 0xe0, 0 );
543 p_es->i_stream_id = 0xe0;
544 p_es->i_type = MPEG2_VIDEO_ES;
545 p_es->i_cat = VIDEO_ES;
546 if( p_main->b_video )
548 input_SelectES( p_input, p_es );
551 #define audio_status \
552 vts.title_unit.p_title[p_dvd->i_title_id-1].title.pi_audio_status[i-1]
553 /* Audio ES, in the order they appear in .ifo */
554 for( i = 1 ; i <= vts.manager_inf.i_audio_nb ; i++ )
556 IfoPrintAudio( p_dvd, i );
558 /* audio channel is active if first byte is 0x80 */
559 if( audio_status.i_available )
563 switch( vts.manager_inf.p_audio_attr[i-1].i_coding_mode )
566 i_id = ( ( 0x80 + audio_status.i_position ) << 8 ) | 0xbd;
567 p_es = input_AddES( p_input,
568 p_input->stream.pp_programs[0], i_id, 0 );
569 p_es->i_stream_id = 0xbd;
570 p_es->i_type = AC3_AUDIO_ES;
572 p_es->i_cat = AUDIO_ES;
573 strcpy( p_es->psz_desc, DecodeLanguage( hton16(
574 vts.manager_inf.p_audio_attr[i-1].i_lang_code ) ) );
575 strcat( p_es->psz_desc, " (ac3)" );
579 case 0x03: /* MPEG audio */
580 i_id = 0xc0 + audio_status.i_position;
581 p_es = input_AddES( p_input,
582 p_input->stream.pp_programs[0], i_id, 0 );
583 p_es->i_stream_id = i_id;
584 p_es->i_type = MPEG2_AUDIO_ES;
586 p_es->i_cat = AUDIO_ES;
587 strcpy( p_es->psz_desc, DecodeLanguage( hton16(
588 vts.manager_inf.p_audio_attr[i-1].i_lang_code ) ) );
589 strcat( p_es->psz_desc, " (mpeg)" );
592 case 0x04: /* LPCM */
594 i_id = ( ( 0xa0 + audio_status.i_position ) << 8 ) | 0xbd;
595 p_es = input_AddES( p_input,
596 p_input->stream.pp_programs[0], i_id, 0 );
597 p_es->i_stream_id = 0xbd;
598 p_es->i_type = LPCM_AUDIO_ES;
600 p_es->i_cat = AUDIO_ES;
601 strcpy( p_es->psz_desc, DecodeLanguage( hton16(
602 vts.manager_inf.p_audio_attr[i-1].i_lang_code ) ) );
603 strcat( p_es->psz_desc, " (lpcm)" );
607 i_id = ( ( 0x88 + audio_status.i_position ) << 8 ) | 0xbd;
608 intf_ErrMsg( "dvd warning: DTS audio not handled yet"
613 intf_ErrMsg( "dvd warning: unknown audio type %.2x",
614 vts.manager_inf.p_audio_attr[i-1].i_coding_mode );
620 vts.title_unit.p_title[p_dvd->i_title_id-1].title.pi_spu_status[i-1]
624 for( i = 1 ; i <= vts.manager_inf.i_spu_nb; i++ )
626 IfoPrintSpu( p_dvd, i );
628 if( spu_status.i_available )
632 /* there are several streams for one spu */
633 if( vts.manager_inf.video_attr.i_ratio )
636 switch( vts.manager_inf.video_attr.i_perm_displ )
639 i_id = ( ( 0x20 + spu_status.i_position_pan ) << 8 )
643 i_id = ( ( 0x20 + spu_status.i_position_letter ) << 8 )
647 i_id = ( ( 0x20 + spu_status.i_position_wide ) << 8 )
655 i_id = ( ( 0x20 + spu_status.i_position_43 ) << 8 )
658 p_es = input_AddES( p_input,
659 p_input->stream.pp_programs[0], i_id, 0 );
660 p_es->i_stream_id = 0xbd;
661 p_es->i_type = DVD_SPU_ES;
662 p_es->i_cat = SPU_ES;
663 strcpy( p_es->psz_desc, DecodeLanguage( hton16(
664 vts.manager_inf.p_spu_attr[i-1].i_lang_code ) ) );
668 if( p_main->b_audio )
670 /* For audio: first one if none or a not existing one specified */
671 i_audio = main_GetIntVariable( INPUT_CHANNEL_VAR, 1 );
672 if( i_audio < 0 || i_audio > i_audio_nb )
674 main_PutIntVariable( INPUT_CHANNEL_VAR, 1 );
677 if( i_audio > 0 && i_audio_nb > 0 )
679 if( main_GetIntVariable( AOUT_SPDIF_VAR, 0 ) ||
680 ( main_GetIntVariable( INPUT_AUDIO_VAR, 0 ) ==
684 while( ( p_input->stream.pp_es[i_ac3]->i_type !=
685 AC3_AUDIO_ES ) && ( i_ac3 <=
686 vts.manager_inf.i_audio_nb ) )
690 if( p_input->stream.pp_es[i_ac3]->i_type == AC3_AUDIO_ES )
692 input_SelectES( p_input,
693 p_input->stream.pp_es[i_ac3] );
698 input_SelectES( p_input,
699 p_input->stream.pp_es[i_audio] );
704 if( p_main->b_video )
706 /* for spu, default is none */
707 i_spu = main_GetIntVariable( INPUT_SUBTITLE_VAR, 0 );
708 if( i_spu < 0 || i_spu > i_spu_nb )
710 main_PutIntVariable( INPUT_SUBTITLE_VAR, 0 );
713 if( i_spu > 0 && i_spu_nb > 0 )
715 i_spu += vts.manager_inf.i_audio_nb;
716 input_SelectES( p_input, p_input->stream.pp_es[i_spu] );
722 p_area = p_input->stream.p_selected_area;
731 if( p_area->i_part != p_dvd->i_chapter )
733 if( ( p_area->i_part > 0 ) &&
734 ( p_area->i_part <= p_area->i_part_nb ))
736 if( DVDChapterSelect( p_dvd, p_area->i_part ) < 0 )
738 intf_ErrMsg( "dvd error: can't set chapter in area" );
739 p_input->b_error = 1;
743 p_input->stream.p_selected_area->i_tell =
744 LB2OFF( p_dvd->i_start ) - p_area->i_start;
745 p_input->stream.p_selected_area->i_part = p_dvd->i_chapter;
747 intf_WarnMsg( 4, "dvd info: chapter %d start at: %lld",
748 p_area->i_part, p_area->i_tell );
753 p_dvd->i_chapter = 1;
758 p_dvd->p_ifo->vts.title_unit.p_title[p_dvd->i_title_id-1].title
759 if( p_area->i_angle != p_dvd->i_angle )
761 if( title.p_cell_play[p_dvd->i_prg_cell].i_category & 0xf000 )
763 if( ( p_area->i_angle - p_dvd->i_angle ) < 0 )
767 p_dvd->i_prg_cell += ( p_area->i_angle - p_dvd->i_angle );
768 p_dvd->i_angle = p_area->i_angle;
770 DVDFindSector( p_dvd );
771 p_dvd->i_cell += p_dvd->i_angle_cell;
775 p_dvd->i_angle = p_area->i_angle;
778 intf_WarnMsg( 3, "dvd info: angle %d selected", p_area->i_angle );
781 /* warn interface that something has changed */
782 p_input->stream.b_seekable = 1;
783 p_input->stream.b_changed = 1;
789 /*****************************************************************************
790 * DVDRead: reads data packets into the netlist.
791 *****************************************************************************
792 * Returns -1 in case of error, 0 if everything went well, and 1 in case of
794 *****************************************************************************/
795 static int DVDRead( input_thread_t * p_input,
796 data_packet_t ** pp_packets )
798 thread_dvd_data_t * p_dvd;
799 netlist_t * p_netlist;
800 struct iovec * p_vec;
801 struct data_packet_s * pp_data[DVD_DATA_READ_ONCE];
814 p_dvd = (thread_dvd_data_t *)p_input->p_plugin_data;
815 p_netlist = (netlist_t *)p_input->p_method_data;
818 i_sector = p_dvd->i_title_start + p_dvd->i_sector;
819 i_block_once = p_dvd->i_end_sector - p_dvd->i_sector + 1;
822 /* Get the position of the next cell if we're at cell end */
823 if( i_block_once <= 0 )
828 p_dvd->i_angle_cell++;
830 /* Find cell index in adress map */
831 if( DVDFindSector( p_dvd ) < 0 )
833 pp_packets[0] = NULL;
834 intf_ErrMsg( "dvd error: can't find next cell" );
838 /* Position the fd pointer on the right address */
839 if( ( i_sector = dvdcss_seek( p_dvd->dvdhandle,
840 p_dvd->i_title_start + p_dvd->i_sector,
841 DVDCSS_SEEK_MPEG ) ) < 0 )
843 intf_ErrMsg( "dvd error: %s", dvdcss_error( p_dvd->dvdhandle ) );
847 /* update chapter : it will be easier when we have navigation
849 if( p_dvd->i_chapter < ( p_dvd->i_chapter_nb - 1 ) )
851 if( title.p_cell_play[p_dvd->i_prg_cell].i_category & 0xf000 )
853 i_angle = p_dvd->i_angle - 1;
859 if( title.chapter_map.pi_start_cell[p_dvd->i_chapter] <=
860 ( p_dvd->i_prg_cell - i_angle + 1 ) )
867 i_block_once = p_dvd->i_end_sector - p_dvd->i_sector + 1;
870 /* The number of blocks read is the max between the requested
871 * value and the leaving block in the cell */
872 if( i_block_once > p_dvd->i_block_once )
874 i_block_once = p_dvd->i_block_once;
877 intf_WarnMsg( 2, "Sector: 0x%x Read: %d Chapter: %d", p_dvd->i_sector, i_block_once, p_dvd->i_chapter );
879 p_netlist->i_read_once = i_block_once;
881 /* Get an iovec pointer */
882 if( ( p_vec = input_NetlistGetiovec( p_netlist ) ) == NULL )
884 intf_ErrMsg( "dvd error: can't get iovec" );
889 i_read_blocks = dvdcss_readv( p_dvd->dvdhandle, p_vec,
890 i_block_once, DVDCSS_READ_DECRYPT );
892 /* Update netlist indexes: we don't do it in DVDGetiovec since we
893 * need know the real number of blocks read */
894 input_NetlistMviovec( p_netlist, i_read_blocks, pp_data );
896 /* Update global position */
897 p_dvd->i_sector += i_read_blocks;
901 /* Read headers to compute payload length */
902 for( i_iovec = 0 ; i_iovec < i_read_blocks ; i_iovec++ )
906 while( i_pos < p_netlist->i_buffer_size )
908 pi_cur = (u8*)p_vec[i_iovec].iov_base + i_pos;
911 if( U32_AT( pi_cur ) != 0x1BA )
913 /* That's the case for all packets, except pack header. */
914 i_packet_size = U16_AT( pi_cur + 4 );
915 pp_packets[i_packet] = input_NetlistNewPtr( p_netlist );
916 (*pp_data[i_iovec]->pi_refcount)++;
917 pp_packets[i_packet]->pi_refcount =
918 pp_data[i_iovec]->pi_refcount;
919 pp_packets[i_packet]->p_buffer = pp_data[i_iovec]->p_buffer;
923 /* MPEG-2 Pack header. */
925 pp_packets[i_packet] = pp_data[i_iovec];
928 pp_packets[i_packet]->p_payload_start =
929 pp_packets[i_packet]->p_buffer + i_pos;
931 pp_packets[i_packet]->p_payload_end =
932 pp_packets[i_packet]->p_payload_start + i_packet_size + 6;
935 i_pos += i_packet_size + 6;
939 pp_packets[i_packet] = NULL;
941 vlc_mutex_lock( &p_input->stream.stream_lock );
943 p_input->stream.p_selected_area->i_tell =
944 LB2OFF( i_sector + i_read_blocks ) -
945 p_input->stream.p_selected_area->i_start;
948 /* We modify i_part only at end of chapter not to erase
949 * some modification from the interface */
950 p_input->stream.p_selected_area->i_part = p_dvd->i_chapter;
953 b_eot = !( p_input->stream.p_selected_area->i_tell
954 < p_input->stream.p_selected_area->i_size );
955 b_eof = b_eot && ( ( p_dvd->i_title + 1 ) >= p_input->stream.i_area_nb );
959 vlc_mutex_unlock( &p_input->stream.stream_lock );
965 intf_WarnMsg( 4, "dvd info: new title" );
967 DVDSetArea( p_input, p_input->stream.pp_areas[p_dvd->i_title] );
968 vlc_mutex_unlock( &p_input->stream.stream_lock );
972 vlc_mutex_unlock( &p_input->stream.stream_lock );
974 if( i_read_blocks == i_block_once )
982 /*****************************************************************************
983 * DVDRewind : reads a stream backward
984 *****************************************************************************/
985 static int DVDRewind( input_thread_t * p_input )
990 /*****************************************************************************
991 * DVDSeek : Goes to a given position on the stream.
992 *****************************************************************************
993 * This one is used by the input and translate chronological position from
994 * input to logical position on the device.
995 * The lock should be taken before calling this function.
996 *****************************************************************************/
997 static void DVDSeek( input_thread_t * p_input, off_t i_off )
999 thread_dvd_data_t * p_dvd;
1006 p_dvd = ( thread_dvd_data_t * )p_input->p_plugin_data;
1008 /* we have to take care of offset of beginning of title */
1009 p_dvd->i_sector = OFF2LB(i_off + p_input->stream.p_selected_area->i_start)
1010 - p_dvd->i_title_start;
1015 /* parse vobu address map to find program cell */
1016 while( title.p_cell_play[i_prg_cell].i_end_sector < p_dvd->i_sector )
1021 p_dvd->i_prg_cell = i_prg_cell;
1023 if( DVDChooseAngle( p_dvd ) < 0 )
1025 p_input->b_error = 1;
1031 /* Find first title cell which is inside program cell */
1032 if( DVDFindCell( p_dvd ) < 0 )
1034 /* no following cell : we're at eof */
1035 intf_ErrMsg( "dvd error: cell seeking failed" );
1036 p_input->b_error = 1;
1040 i_cell = p_dvd->i_cell;
1042 #define cell p_dvd->p_ifo->vts.cell_inf.p_cell_map[i_cell]
1043 /* parse cell address map to find title cell containing sector */
1044 while( cell.i_end_sector < p_dvd->i_sector )
1049 p_dvd->i_cell = i_cell;
1051 /* if we're inside a multi-angle zone, we have to choose i_sector
1052 * in the current angle ; we can't do it all the time since cells
1053 * can be very wide out of such zones */
1054 if( title.p_cell_play[p_dvd->i_prg_cell].i_category & 0xf000 )
1056 p_dvd->i_sector = MAX(
1057 cell.i_start_sector,
1058 title.p_cell_play[p_dvd->i_prg_cell].i_start_sector );
1061 p_dvd->i_end_sector = MIN(
1063 title.p_cell_play[p_dvd->i_prg_cell].i_end_sector );
1065 /* update chapter */
1066 if( title.p_cell_play[p_dvd->i_prg_cell].i_category & 0xf000 )
1068 i_angle = p_dvd->i_angle - 1;
1074 if( p_dvd->i_chapter_nb > 1 )
1076 while( ( title.chapter_map.pi_start_cell[i_chapter] <=
1077 ( p_dvd->i_prg_cell - i_angle + 1 ) ) &&
1078 ( i_chapter < ( p_dvd->i_chapter_nb - 1 ) ) )
1088 p_dvd->i_chapter = i_chapter;
1089 p_input->stream.p_selected_area->i_part = p_dvd->i_chapter;
1091 if( ( i_block = dvdcss_seek( p_dvd->dvdhandle,
1092 p_dvd->i_title_start + p_dvd->i_sector,
1093 DVDCSS_SEEK_MPEG ) ) < 0 )
1095 intf_ErrMsg( "dvd error: %s", dvdcss_error( p_dvd->dvdhandle ) );
1096 p_input->b_error = 1;
1100 p_input->stream.p_selected_area->i_tell =
1101 LB2OFF ( i_block ) - p_input->stream.p_selected_area->i_start;
1103 intf_WarnMsg( 7, "Program Cell: %d Cell: %d Chapter: %d",
1104 p_dvd->i_prg_cell, p_dvd->i_cell, p_dvd->i_chapter );
1110 #define cell p_dvd->p_ifo->vts.cell_inf
1112 /*****************************************************************************
1113 * DVDFindCell: adjust the title cell index with the program cell
1114 *****************************************************************************/
1115 static int DVDFindCell( thread_dvd_data_t * p_dvd )
1120 i_cell = p_dvd->i_cell;
1121 i_index = p_dvd->i_prg_cell;
1123 if( i_cell >= cell.i_cell_nb )
1128 while( ( ( title.p_cell_pos[i_index].i_vob_id !=
1129 cell.p_cell_map[i_cell].i_vob_id ) ||
1130 ( title.p_cell_pos[i_index].i_cell_id !=
1131 cell.p_cell_map[i_cell].i_cell_id ) ) &&
1132 ( i_cell < cell.i_cell_nb - 1 ) )
1138 intf_WarnMsg( 12, "FindCell: i_cell %d i_index %d found %d nb %d",
1145 p_dvd->i_cell = i_cell;
1152 /*****************************************************************************
1153 * DVDFindSector: find cell index in adress map from index in
1154 * information table program map and give corresponding sectors.
1155 *****************************************************************************/
1156 static int DVDFindSector( thread_dvd_data_t * p_dvd )
1159 if( p_dvd->i_sector > title.p_cell_play[p_dvd->i_prg_cell].i_end_sector )
1161 p_dvd->i_prg_cell++;
1163 if( DVDChooseAngle( p_dvd ) < 0 )
1169 if( DVDFindCell( p_dvd ) < 0 )
1171 intf_ErrMsg( "dvd error: can't find sector" );
1175 /* Find start and end sectors of new cell */
1177 p_dvd->i_sector = MAX(
1178 p_dvd->p_ifo->vts.cell_inf.p_cell_map[p_dvd->i_cell].i_start_sector,
1179 title.p_cell_play[p_dvd->i_prg_cell].i_start_sector );
1180 p_dvd->i_end_sector = MIN(
1181 p_dvd->p_ifo->vts.cell_inf.p_cell_map[p_dvd->i_cell].i_end_sector,
1182 title.p_cell_play[p_dvd->i_prg_cell].i_end_sector );
1184 p_dvd->i_sector = title.p_cell_play[p_dvd->i_prg_cell].i_start_sector;
1185 p_dvd->i_end_sector = title.p_cell_play[p_dvd->i_prg_cell].i_end_sector;
1189 intf_WarnMsg( 12, "cell: %d sector1: 0x%x end1: 0x%x\n"
1190 "index: %d sector2: 0x%x end2: 0x%x\n"
1191 "category: 0x%x ilvu end: 0x%x vobu start 0x%x",
1193 p_dvd->p_ifo->vts.cell_inf.p_cell_map[p_dvd->i_cell].i_start_sector,
1194 p_dvd->p_ifo->vts.cell_inf.p_cell_map[p_dvd->i_cell].i_end_sector,
1196 title.p_cell_play[p_dvd->i_prg_cell].i_start_sector,
1197 title.p_cell_play[p_dvd->i_prg_cell].i_end_sector,
1198 title.p_cell_play[p_dvd->i_prg_cell].i_category,
1199 title.p_cell_play[p_dvd->i_prg_cell].i_first_ilvu_vobu_esector,
1200 title.p_cell_play[p_dvd->i_prg_cell].i_last_vobu_start_sector );
1206 /*****************************************************************************
1207 * DVDChapterSelect: find the cell corresponding to requested chapter
1208 *****************************************************************************/
1209 static int DVDChapterSelect( thread_dvd_data_t * p_dvd, int i_chapter )
1212 /* Find cell index in Program chain for current chapter */
1213 p_dvd->i_prg_cell = title.chapter_map.pi_start_cell[i_chapter-1] - 1;
1215 p_dvd->i_sector = 0;
1217 DVDChooseAngle( p_dvd );
1219 /* Search for cell_index in cell adress_table and initialize
1221 if( DVDFindSector( p_dvd ) < 0 )
1223 intf_ErrMsg( "dvd error: can't select chapter" );
1227 /* start is : beginning of vts vobs + offset to vob x */
1228 p_dvd->i_start = p_dvd->i_title_start + p_dvd->i_sector;
1230 /* Position the fd pointer on the right address */
1231 if( ( p_dvd->i_start = dvdcss_seek( p_dvd->dvdhandle,
1233 DVDCSS_SEEK_MPEG ) ) < 0 )
1235 intf_ErrMsg( "dvd error: %s", dvdcss_error( p_dvd->dvdhandle ) );
1239 p_dvd->i_chapter = i_chapter;
1243 /*****************************************************************************
1244 * DVDChooseAngle: select the cell corresponding to the selected angle
1245 *****************************************************************************/
1246 static int DVDChooseAngle( thread_dvd_data_t * p_dvd )
1248 /* basic handling of angles */
1249 switch( ( ( title.p_cell_play[p_dvd->i_prg_cell].i_category & 0xf000 )
1252 /* we enter a muli-angle section */
1254 p_dvd->i_prg_cell += p_dvd->i_angle - 1;
1255 p_dvd->i_angle_cell = 0;
1257 /* we exit a multi-angle section */
1260 p_dvd->i_prg_cell += p_dvd->i_angle_nb - p_dvd->i_angle;