1 /* dvd_access.c: DVD access plugin.
2 *****************************************************************************
3 * This plugins should handle all the known specificities of the DVD format,
4 * especially the 2048 bytes logical block size.
6 * -libdvdcss for access and unscrambling
7 * -dvd_ifo for ifo parsing and analyse
8 * -dvd_udf to find files
9 *****************************************************************************
10 * Copyright (C) 1998-2001 VideoLAN
11 * $Id: dvd_access.c,v 1.8 2002/03/15 04:41:54 sam Exp $
13 * Author: Stéphane Borel <stef@via.ecp.fr>
15 * This program is free software; you can redistribute it and/or modify
16 * it under the terms of the GNU General Public License as published by
17 * the Free Software Foundation; either version 2 of the License, or
18 * (at your option) any later version.
20 * This program is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
25 * You should have received a copy of the GNU General Public License
26 * along with this program; if not, write to the Free Software
27 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
28 *****************************************************************************/
30 /*****************************************************************************
32 *****************************************************************************/
37 #include <videolan/vlc.h>
44 #include <sys/types.h>
49 #ifdef STRNCASECMP_IN_STRINGS_H
54 # include "dummy_dvdcss.h"
56 # include <videolan/dvdcss.h>
59 #include "stream_control.h"
60 #include "input_ext-intf.h"
61 #include "input_ext-dec.h"
62 #include "input_ext-plugins.h"
68 #include "dvd_summary.h"
73 /*****************************************************************************
75 *****************************************************************************/
77 /* called from outside */
78 static int DVDOpen ( struct input_thread_s * );
79 static void DVDClose ( struct input_thread_s * );
80 static int DVDSetArea ( struct input_thread_s *, struct input_area_s * );
81 static int DVDSetProgram ( struct input_thread_s *, pgrm_descriptor_t * );
82 static int DVDRead ( struct input_thread_s *, byte_t *, size_t );
83 static void DVDSeek ( struct input_thread_s *, off_t );
85 static char * DVDParse( input_thread_t * );
87 /*****************************************************************************
88 * Functions exported as capabilities. They are declared as static so that
89 * we don't pollute the namespace too much.
90 *****************************************************************************/
91 void _M( access_getfunctions)( function_list_t * p_function_list )
93 #define input p_function_list->functions.access
94 input.pf_open = DVDOpen;
95 input.pf_close = DVDClose;
96 input.pf_read = DVDRead;
97 input.pf_set_area = DVDSetArea;
98 input.pf_set_program = DVDSetProgram;
99 input.pf_seek = DVDSeek;
104 * Data access functions
107 #define DVDLB p_dvd->i_vts_start + p_dvd->i_vts_lb
108 #define DVDTell LB2OFF( p_dvd->i_vts_start + p_dvd->i_vts_lb ) \
109 - p_input->stream.p_selected_area->i_start
111 /*****************************************************************************
113 *****************************************************************************/
114 static int DVDOpen( struct input_thread_s *p_input )
117 thread_dvd_data_t * p_dvd;
118 input_area_t * p_area;
121 p_dvd = malloc( sizeof(thread_dvd_data_t) );
124 intf_ErrMsg( "dvd error: out of memory" );
127 p_input->p_access_data = (void *)p_dvd;
129 /* Parse command line */
130 if( !( psz_device = DVDParse( p_input ) ) )
144 p_dvd->dvdhandle = dvdcss_open( psz_device );
146 /* free allocated string */
149 if( p_dvd->dvdhandle == NULL )
151 intf_ErrMsg( "dvd error: dvdcss can't open device" );
156 if( dvdcss_seek( p_dvd->dvdhandle, 0, DVDCSS_NOFLAGS ) < 0 )
158 intf_ErrMsg( "dvd error: %s", dvdcss_error( p_dvd->dvdhandle ) );
159 dvdcss_close( p_dvd->dvdhandle );
164 /* Ifo allocation & initialisation */
165 if( IfoCreate( p_dvd ) < 0 )
167 intf_ErrMsg( "dvd error: allcation error in ifo" );
168 dvdcss_close( p_dvd->dvdhandle );
173 if( IfoInit( p_dvd->p_ifo ) < 0 )
175 intf_ErrMsg( "dvd error: fatal failure in ifo" );
176 IfoDestroy( p_dvd->p_ifo );
177 dvdcss_close( p_dvd->dvdhandle );
182 /* Set stream and area data */
183 vlc_mutex_lock( &p_input->stream.stream_lock );
185 p_input->stream.i_method = INPUT_METHOD_DVD;
186 p_input->stream.b_pace_control = 1;
187 p_input->stream.b_seekable = 1;
188 p_input->stream.p_selected_area->i_size = 0;
189 p_input->stream.p_selected_area->i_tell = 0;
191 /* Initialize ES structures */
192 input_InitStream( p_input, sizeof( stream_ps_data_t ) );
194 #define title_inf p_dvd->p_ifo->vmg.title_inf
195 intf_WarnMsg( 3, "dvd info: number of titles: %d", title_inf.i_title_nb );
197 #define area p_input->stream.pp_areas
198 /* We start from 1 here since the default area 0
199 * is reserved for video_ts.vob */
200 for( i = 1 ; i <= title_inf.i_title_nb ; i++ )
202 input_AddArea( p_input );
204 /* Titles are Program Chains */
207 /* Absolute start offset and size
208 * We can only set that with vts ifo, so we do it during the
209 * first call to DVDSetArea */
210 area[i]->i_start = 0;
213 /* Number of chapters */
214 area[i]->i_part_nb = title_inf.p_attr[i-1].i_chapter_nb;
217 /* Offset to vts_i_0.ifo */
218 area[i]->i_plugin_data = p_dvd->p_ifo->i_start +
219 title_inf.p_attr[i-1].i_start_sector;
223 p_dvd->i_title = p_dvd->i_title <= title_inf.i_title_nb ?
227 p_area = p_input->stream.pp_areas[p_dvd->i_title];
229 p_area->i_part = p_dvd->i_chapter <= p_area->i_part_nb ?
230 p_dvd->i_chapter : 1;
231 p_dvd->i_chapter = 1;
233 p_dvd->b_new_chapter = 0;
234 p_dvd->i_audio_nb = 0;
237 /* set title, chapter, audio and subpic */
238 if( DVDSetArea( p_input, p_area ) < 0 )
240 vlc_mutex_unlock( &p_input->stream.stream_lock );
241 IfoDestroy( p_dvd->p_ifo );
242 dvdcss_close( p_dvd->dvdhandle );
247 vlc_mutex_unlock( &p_input->stream.stream_lock );
249 p_input->psz_demux = "dvd";
254 /*****************************************************************************
255 * DVDClose: close dvd
256 *****************************************************************************/
257 static void DVDClose( struct input_thread_s *p_input )
259 thread_dvd_data_t *p_dvd = (thread_dvd_data_t*)p_input->p_access_data;
261 IfoDestroy( p_dvd->p_ifo );
262 dvdcss_close( p_dvd->dvdhandle );
266 /*****************************************************************************
267 * DVDSetProgram: used to change angle
268 *****************************************************************************/
269 static int DVDSetProgram( input_thread_t * p_input,
270 pgrm_descriptor_t * p_program )
272 if( p_input->stream.p_selected_program != p_program )
274 thread_dvd_data_t * p_dvd;
277 p_dvd = (thread_dvd_data_t*)(p_input->p_access_data);
278 i_angle = p_program->i_number;
280 /* DVD is actually mono-program: we only need the current angle
281 * number, so copy the data between programs */
283 p_input->stream.p_selected_program,
284 sizeof(pgrm_descriptor_t) );
285 p_program->i_number = i_angle;
286 p_input->stream.p_selected_program = p_program;
289 p_dvd->p_ifo->vts.title_unit.p_title[p_dvd->i_title_id-1].title
290 if( title.p_cell_play[p_dvd->i_prg_cell].i_category & 0xf000 )
292 if( ( p_program->i_number - p_dvd->i_angle ) < 0 )
294 /* we have to go backwards */
295 p_dvd->i_map_cell = 0;
297 p_dvd->i_prg_cell += ( p_program->i_number - p_dvd->i_angle );
298 p_dvd->i_map_cell = CellPrg2Map( p_dvd );
299 p_dvd->i_map_cell += p_dvd->i_angle_cell;
300 p_dvd->i_vts_lb = CellStartSector( p_dvd );
301 p_dvd->i_end_lb = CellEndSector( p_dvd );
302 p_dvd->i_angle = p_program->i_number;
306 p_dvd->i_angle = p_program->i_number;
309 intf_WarnMsg( 3, "dvd info: angle %d selected", p_dvd->i_angle );
315 /*****************************************************************************
316 * DVDSetArea: initialize input data for title x, chapter y.
317 * It should be called for each user navigation request.
318 *****************************************************************************
319 * Take care that i_title starts from 0 (vmg) and i_chapter start from 1.
320 * Note that you have to take the lock before entering here.
321 *****************************************************************************/
322 #define vmg p_dvd->p_ifo->vmg
323 #define vts p_dvd->p_ifo->vts
325 static void DVDFlushStream( input_thread_t * p_input )
327 if( p_input->stream.pp_programs != NULL )
329 /* We don't use input_EndStream here since
330 * we keep area structures */
331 while( p_input->stream.i_es_number )
333 input_DelES( p_input, p_input->stream.pp_es[0] );
336 while( p_input->stream.i_pgrm_number )
338 input_DelProgram( p_input, p_input->stream.pp_programs[0] );
341 if( p_input->stream.pp_selected_es )
343 free( p_input->stream.pp_selected_es );
344 p_input->stream.pp_selected_es = NULL;
346 p_input->stream.i_selected_es_number = 0;
352 static int DVDReadAngle( input_thread_t * p_input )
354 thread_dvd_data_t * p_dvd;
358 p_dvd = (thread_dvd_data_t*)(p_input->p_access_data);
359 i_angle_nb = vmg.title_inf.p_attr[p_dvd->i_title-1].i_angle_nb;
361 input_AddProgram( p_input, 1, sizeof( stream_ps_data_t ) );
362 p_input->stream.p_selected_program = p_input->stream.pp_programs[0];
364 for( i = 1 ; i < i_angle_nb ; i++ )
366 input_AddProgram( p_input, i+1, 0 );
372 static int DVDSetArea( input_thread_t * p_input, input_area_t * p_area )
374 thread_dvd_data_t * p_dvd;
376 p_dvd = (thread_dvd_data_t*)(p_input->p_access_data);
378 /* we can't use the interface slider until initilization is complete */
379 p_input->stream.b_seekable = 0;
381 if( p_area != p_input->stream.p_selected_area )
387 /* Reset the Chapter position of the old title */
388 p_input->stream.p_selected_area->i_part = 1;
389 p_input->stream.p_selected_area = p_area;
392 * We have to load all title information
395 /* title number as it appears in the interface list */
396 p_dvd->i_title = p_area->i_id;
397 p_dvd->i_chapter_nb = p_area->i_part_nb;
399 if( IfoTitleSet( p_dvd->p_ifo, p_dvd->i_title ) < 0 )
401 intf_ErrMsg( "dvd error: fatal error in vts ifo" );
406 /* title position inside the selected vts */
407 i_vts_title = vmg.title_inf.p_attr[p_dvd->i_title-1].i_title_num;
409 vts.title_inf.p_title_start[i_vts_title-1].i_title_id;
411 intf_WarnMsg( 3, "dvd: title %d vts_title %d pgc %d",
412 p_dvd->i_title, i_vts_title, p_dvd->i_title_id );
414 /* title set offset XXX: convert to block values */
416 vts.i_pos + vts.manager_inf.i_title_vob_start_sector;
419 p_dvd->i_prg_cell = -1 +
420 vts.title_unit.p_title[p_dvd->i_title_id-1].title.i_cell_nb;
421 p_dvd->i_map_cell = 0;
422 p_dvd->i_map_cell = CellPrg2Map( p_dvd );
423 i_size = CellEndSector( p_dvd );
426 p_dvd->i_prg_cell = 0;
427 p_dvd->i_map_cell = 0;
428 p_dvd->i_angle_cell = 0;
429 p_dvd->i_map_cell = CellPrg2Map ( p_dvd );
430 p_dvd->i_vts_lb = CellStartSector( p_dvd );
431 p_dvd->i_end_lb = CellEndSector ( p_dvd );
433 /* Force libdvdcss to check its title key.
434 * It is only useful for title cracking method. Methods using the
435 * decrypted disc key are fast enough to check the key at each seek */
436 if( ( i_start = dvdcss_seek( p_dvd->dvdhandle, DVDLB,
437 DVDCSS_SEEK_KEY ) ) < 0 )
439 intf_ErrMsg( "dvd error: %s", dvdcss_error( p_dvd->dvdhandle ) );
443 i_size -= p_dvd->i_vts_lb + 1;
445 /* Area definition */
446 p_input->stream.p_selected_area->i_start = LB2OFF( i_start );
447 p_input->stream.p_selected_area->i_size = LB2OFF( i_size );
449 /* Destroy obsolete ES by reinitializing programs */
450 DVDFlushStream( p_input );
452 /* Angle management: angles are handled through programs */
453 p_dvd->i_angle_nb = DVDReadAngle( p_input );
454 if( ( p_dvd->i_angle <= 0 ) || p_dvd->i_angle > p_dvd->i_angle_nb )
459 DVDSetProgram( p_input,
460 p_input->stream.pp_programs[p_dvd->i_angle-1] );
462 intf_WarnMsg( 3, "dvd info: title start: %d size: %d",
464 IfoPrintTitle( p_dvd );
466 /* No PSM to read in DVD mode, we already have all information */
467 p_input->stream.p_selected_program->b_is_ok = 1;
469 /* Find all ES in title with ifo data */
470 DVDReadVideo( p_input );
471 DVDReadAudio( p_input );
472 DVDReadSPU ( p_input );
474 if( p_input->p_demux_module )
476 DVDLaunchDecoders( p_input );
482 p_area = p_input->stream.p_selected_area;
485 /* Chapter selection */
486 p_dvd->i_chapter = DVDSetChapter( p_dvd, p_area->i_part );
488 p_input->stream.p_selected_area->i_tell = DVDTell;
490 /* warn interface that something has changed */
491 p_input->stream.b_seekable = 1;
492 p_input->stream.b_changed = 1;
500 p_dvd->p_ifo->vts.title_unit.p_title[p_dvd->i_title_id-1].title
502 /*****************************************************************************
503 * DVDRead: reads data packets.
504 *****************************************************************************
505 * Returns -1 in case of error, 0 in case of EOF, otherwise the number of
507 *****************************************************************************/
508 static int DVDRead( input_thread_t * p_input,
509 byte_t * p_buffer, size_t i_count )
511 thread_dvd_data_t * p_dvd;
514 int i_block_once = 0;
516 p_dvd = (thread_dvd_data_t *)(p_input->p_access_data);
519 i_blocks = OFF2LB(i_count);
523 if( ( i_block_once = __MIN( LbMaxOnce( p_dvd ), i_blocks ) ) <= 0 )
529 if( i_block_once != dvdcss_read( p_dvd->dvdhandle, p_buffer,
530 i_block_once, DVDCSS_READ_DECRYPT ) )
535 i_blocks -= i_block_once;
536 i_read += i_block_once;
537 p_buffer += LB2OFF( i_block_once );
539 /* Update global position */
540 p_dvd->i_vts_lb += i_block_once;
543 vlc_mutex_lock( &p_input->stream.stream_lock );
545 p_input->stream.p_selected_area->i_tell += LB2OFF( i_read );
546 if( p_dvd->b_new_chapter )
548 p_input->stream.p_selected_area->i_part = p_dvd->i_chapter;
549 p_dvd->b_new_chapter = 0;
552 if( ( p_input->stream.p_selected_area->i_tell
553 >= p_input->stream.p_selected_area->i_size )
554 || ( i_block_once <= 0 ) )
556 if( ( p_dvd->i_title + 1 ) >= p_input->stream.i_area_nb )
559 vlc_mutex_unlock( &p_input->stream.stream_lock );
564 intf_WarnMsg( 4, "dvd info: new title" );
566 DVDSetArea( p_input, p_input->stream.pp_areas[p_dvd->i_title] );
569 vlc_mutex_unlock( &p_input->stream.stream_lock );
571 return LB2OFF( i_read );
574 /*****************************************************************************
575 * DVDSeek : Goes to a given position on the stream.
576 *****************************************************************************
577 * This one is used by the input and translate chronological position from
578 * input to logical position on the device.
579 * The lock should be taken before calling this function.
580 *****************************************************************************/
581 static void DVDSeek( input_thread_t * p_input, off_t i_off )
583 thread_dvd_data_t * p_dvd;
585 p_dvd = ( thread_dvd_data_t * )(p_input->p_access_data);
587 vlc_mutex_lock( &p_input->stream.stream_lock );
588 p_dvd->i_vts_lb = OFF2LB(i_off + p_input->stream.p_selected_area->i_start)
589 - p_dvd->i_vts_start;
590 vlc_mutex_unlock( &p_input->stream.stream_lock );
592 p_dvd->i_prg_cell = Lb2CellPrg( p_dvd );
593 p_dvd->i_map_cell = Lb2CellMap( p_dvd );
595 if( CellIsInterleaved( p_dvd ) )
597 /* if we're inside a multi-angle zone, we have to choose i_sector
598 * in the current angle ; we can't do it all the time since cells
599 * can be very wide out of such zones */
600 p_dvd->i_vts_lb = CellStartSector( p_dvd );
603 p_dvd->i_end_lb = CellEndSector ( p_dvd );
604 p_dvd->i_chapter = CellPrg2Chapter( p_dvd );
606 if( dvdcss_seek( p_dvd->dvdhandle, DVDLB,
607 DVDCSS_SEEK_MPEG ) < 0 )
609 intf_ErrMsg( "dvd error: %s", dvdcss_error( p_dvd->dvdhandle ) );
610 p_input->b_error = 1;
614 vlc_mutex_lock( &p_input->stream.stream_lock );
615 p_input->stream.p_selected_area->i_part = p_dvd->i_chapter;
616 p_input->stream.p_selected_area->i_tell = DVDTell;
617 vlc_mutex_unlock( &p_input->stream.stream_lock );
619 intf_WarnMsg( 4, "Program Cell: %d Cell: %d Chapter: %d tell %lld",
620 p_dvd->i_prg_cell, p_dvd->i_map_cell, p_dvd->i_chapter, DVDTell );
625 /*****************************************************************************
626 * DVDParse: parse command line
627 *****************************************************************************/
628 static char * DVDParse( input_thread_t * p_input )
630 thread_dvd_data_t * p_dvd;
631 struct stat stat_info;
636 boolean_t b_options = 0;
642 p_dvd = (thread_dvd_data_t*)(p_input->p_access_data);
644 psz_parser = psz_device = strdup( p_input->psz_name );
650 /* Parse input string :
651 * [device][@rawdevice][@[title][,[chapter][,angle]]] */
652 while( *psz_parser && *psz_parser != '@' )
657 if( *psz_parser == '@' )
659 /* Maybe found raw device or option list */
661 psz_raw = ++psz_parser;
668 if( *psz_parser && !strtol( psz_parser, NULL, 10 ) )
670 /* what we've found is either a raw device or a partial option
671 * list e.g. @,29 or both a device and a list ; search end of string */
672 while( *psz_parser && *psz_parser != '@' )
677 if( *psz_parser == '@' )
679 /* found end of raw device, and beginning of options */
686 psz_parser = psz_raw + 1;
687 for( i=0 ; i<3 ; i++ )
691 /* we have only a raw device */
694 if( strtol( psz_parser, NULL, 10 ) )
696 /* we have only a partial list of options, no device */
697 psz_parser = psz_raw;
708 /* found beginning of options ; no raw device specified */
716 i_title = (int)strtol( psz_parser, &psz_next, 10 );
719 psz_parser = psz_next + 1;
720 i_chapter = (int)strtol( psz_parser, &psz_next, 10 );
723 i_angle = (int)strtol( psz_next + 1, NULL, 10 );
727 p_dvd->i_title = i_title ? i_title : 1;
728 p_dvd->i_chapter = i_chapter ? i_chapter : 1;
729 p_dvd->i_angle = i_angle ? i_angle : 1;
736 /* check the raw device */
737 if( stat( psz_raw, &stat_info ) == -1 )
739 intf_WarnMsg( 3, "dvd warning: cannot stat() raw"
741 psz_raw, strerror(errno));
743 *(psz_raw - 1) = '@';
751 if( !S_ISCHR(stat_info.st_mode) )
753 intf_WarnMsg( 3, "dvd warning: raw device %s is"
754 " not a valid char device", psz_raw );
756 *(psz_raw - 1) = '@';
762 psz_env = malloc( strlen("DVDCSS_RAW_DEVICE=")
763 + strlen( psz_raw ) + 1 );
764 sprintf( psz_env, "DVDCSS_RAW_DEVICE=%s", psz_raw );
779 if( !p_input->psz_access )
781 /* no device and no access specified: we probably don't want DVD */
784 psz_device = config_GetPszVariable( "dvd_device" );
787 /* check block device */
788 if( stat( psz_device, &stat_info ) == -1 )
790 intf_ErrMsg( "input error: cannot stat() device `%s' (%s)",
791 psz_device, strerror(errno));
796 if( !S_ISBLK(stat_info.st_mode) && !S_ISCHR(stat_info.st_mode) )
798 intf_WarnMsg( 3, "input: DVD plugin discarded"
799 " (not a valid block device)" );
804 intf_WarnMsg( 2, "input: dvd=%s raw=%s title=%d chapter=%d angle=%d",
805 psz_device, psz_raw, p_dvd->i_title,
806 p_dvd->i_chapter, p_dvd->i_angle );