1 /* 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 * -ifo.* for ifo parsing and analyse
9 *****************************************************************************
10 * Copyright (C) 1998-2001 VideoLAN
11 * $Id: access.c,v 1.9 2003/01/28 22:03:21 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 *****************************************************************************/
38 #include <vlc/input.h>
40 #include "../../demux/mpeg/system.h"
47 #include <sys/types.h>
52 #ifdef STRNCASECMP_IN_STRINGS_H
59 # include <dvdcss/dvdcss.h>
69 /*****************************************************************************
71 *****************************************************************************/
73 /* called from outside */
74 static int DVDSetArea ( input_thread_t *, input_area_t * );
75 static int DVDSetProgram ( input_thread_t *, pgrm_descriptor_t * );
76 static ssize_t DVDRead ( input_thread_t *, byte_t *, size_t );
77 static void DVDSeek ( input_thread_t *, off_t );
79 static char * DVDParse( input_thread_t * );
82 * Data access functions
85 #define DVDTell LB2OFF( p_dvd->i_vts_start + p_dvd->i_vts_lb ) \
86 - p_input->stream.p_selected_area->i_start
88 /*****************************************************************************
90 *****************************************************************************/
91 int E_(DVDOpen) ( vlc_object_t *p_this )
93 input_thread_t * p_input = (input_thread_t *)p_this;
95 thread_dvd_data_t * p_dvd;
96 input_area_t * p_area;
98 char * psz_dvdcss_env;
100 p_dvd = malloc( sizeof(thread_dvd_data_t) );
103 msg_Err( p_input, "out of memory" );
106 p_input->p_access_data = (void *)p_dvd;
108 p_input->pf_read = DVDRead;
109 p_input->pf_seek = DVDSeek;
110 p_input->pf_set_area = DVDSetArea;
111 p_input->pf_set_program = DVDSetProgram;
113 /* Parse command line */
114 if( !( psz_device = DVDParse( p_input ) ) )
125 /* override environment variable DVDCSS_METHOD with config option
126 * (FIXME: this creates a small memory leak) */
127 psz_dvdcss_env = config_GetPsz( p_input, "dvdcss-method" );
128 if( psz_dvdcss_env && *psz_dvdcss_env )
132 psz_env = malloc( strlen("DVDCSS_METHOD=") +
133 strlen( psz_dvdcss_env ) + 1 );
141 sprintf( psz_env, "%s%s", "DVDCSS_METHOD=", psz_dvdcss_env );
145 if( psz_dvdcss_env ) free( psz_dvdcss_env );
150 p_dvd->dvdhandle = dvdcss_open( psz_device );
152 /* free allocated string */
155 if( p_dvd->dvdhandle == NULL )
157 msg_Err( p_input, "dvdcss cannot open device" );
162 if( dvdcss_seek( p_dvd->dvdhandle, 0, DVDCSS_NOFLAGS ) < 0 )
164 msg_Err( p_input, "%s", dvdcss_error( p_dvd->dvdhandle ) );
165 dvdcss_close( p_dvd->dvdhandle );
170 /* Ifo allocation & initialisation */
171 if( IfoCreate( p_dvd ) < 0 )
173 msg_Err( p_input, "allcation error in ifo" );
174 dvdcss_close( p_dvd->dvdhandle );
179 if( IfoInit( p_dvd->p_ifo ) < 0 )
181 msg_Err( p_input, "fatal failure in ifo" );
182 IfoDestroy( p_dvd->p_ifo );
183 dvdcss_close( p_dvd->dvdhandle );
188 /* Set stream and area data */
189 vlc_mutex_lock( &p_input->stream.stream_lock );
191 p_input->stream.i_method = INPUT_METHOD_DVD;
192 p_input->stream.b_pace_control = 1;
193 p_input->stream.b_seekable = 1;
194 p_input->stream.b_connected = 1;
195 p_input->stream.p_selected_area->i_size = 0;
196 p_input->stream.p_selected_area->i_tell = 0;
198 /* Initialize ES structures */
199 input_InitStream( p_input, sizeof( stream_ps_data_t ) );
201 #define title_inf p_dvd->p_ifo->vmg.title_inf
202 msg_Dbg( p_input, "number of titles: %d", title_inf.i_title_nb );
204 #define area p_input->stream.pp_areas
205 /* We start from 1 here since the default area 0
206 * is reserved for video_ts.vob */
207 for( i = 1 ; i <= title_inf.i_title_nb ; i++ )
209 input_AddArea( p_input );
211 /* Titles are Program Chains */
214 /* Absolute start offset and size
215 * We can only set that with vts ifo, so we do it during the
216 * first call to DVDSetArea */
217 area[i]->i_start = 0;
220 /* Number of chapters */
221 area[i]->i_part_nb = title_inf.p_attr[i-1].i_chapter_nb;
224 /* Offset to vts_i_0.ifo */
225 area[i]->i_plugin_data = p_dvd->p_ifo->i_start +
226 title_inf.p_attr[i-1].i_start_sector;
230 p_dvd->i_title = p_dvd->i_title <= title_inf.i_title_nb ?
234 p_area = p_input->stream.pp_areas[p_dvd->i_title];
236 p_area->i_part = p_dvd->i_chapter <= p_area->i_part_nb ?
237 p_dvd->i_chapter : 1;
238 p_dvd->i_chapter = 1;
240 p_dvd->b_new_chapter = 0;
241 p_dvd->i_audio_nb = 0;
244 /* set title, chapter, audio and subpic */
245 if( DVDSetArea( p_input, p_area ) < 0 )
247 vlc_mutex_unlock( &p_input->stream.stream_lock );
248 IfoDestroy( p_dvd->p_ifo );
249 dvdcss_close( p_dvd->dvdhandle );
254 vlc_mutex_unlock( &p_input->stream.stream_lock );
256 if( !p_input->psz_demux || !*p_input->psz_demux )
258 p_input->psz_demux = "dvdold";
264 /*****************************************************************************
265 * DVDClose: close dvd
266 *****************************************************************************/
267 void E_(DVDClose) ( vlc_object_t *p_this )
269 input_thread_t * p_input = (input_thread_t *)p_this;
270 thread_dvd_data_t *p_dvd = (thread_dvd_data_t*)p_input->p_access_data;
272 /* This is a very nasty side-effect in the DVD plug-in : language
273 * selection here influences language selection of other streams. So
274 * unset those variables (may not be what the user wants).
275 * FIXME FIXME FIXME FIXME FIXME FIXME FIXME --Meuuh */
276 config_PutInt( p_input, "audio-channel", -1 );
277 config_PutInt( p_input, "spu-channel", -1 );
279 IfoDestroy( p_dvd->p_ifo );
280 dvdcss_close( p_dvd->dvdhandle );
284 /*****************************************************************************
285 * DVDSetProgram: used to change angle
286 *****************************************************************************/
287 static int DVDSetProgram( input_thread_t * p_input,
288 pgrm_descriptor_t * p_program )
290 if( p_input->stream.p_selected_program != p_program )
292 thread_dvd_data_t * p_dvd;
295 p_dvd = (thread_dvd_data_t*)(p_input->p_access_data);
296 i_angle = p_program->i_number;
298 /* DVD is actually mono-program: we only need the current angle
299 * number, so copy the data between programs */
301 p_input->stream.p_selected_program,
302 sizeof(pgrm_descriptor_t) );
303 p_program->i_number = i_angle;
304 p_input->stream.p_selected_program = p_program;
307 p_dvd->p_ifo->vts.title_unit.p_title[p_dvd->i_title_id-1].title
308 if( title.p_cell_play[p_dvd->i_prg_cell].i_category & 0xf000 )
310 if( ( p_program->i_number - p_dvd->i_angle ) < 0 )
312 /* we have to go backwards */
313 p_dvd->i_map_cell = 0;
315 p_dvd->i_prg_cell += ( p_program->i_number - p_dvd->i_angle );
316 p_dvd->i_map_cell = CellPrg2Map( p_dvd );
317 p_dvd->i_map_cell += p_dvd->i_angle_cell;
318 p_dvd->i_vts_lb = CellFirstSector( p_dvd );
319 p_dvd->i_last_lb = CellLastSector( p_dvd );
320 p_dvd->i_angle = p_program->i_number;
324 p_dvd->i_angle = p_program->i_number;
327 msg_Dbg( p_input, "angle %d selected", p_dvd->i_angle );
333 /*****************************************************************************
334 * DVDSetArea: initialize input data for title x, chapter y.
335 * It should be called for each user navigation request.
336 *****************************************************************************
337 * Take care that i_title starts from 0 (vmg) and i_chapter start from 1.
338 * Note that you have to take the lock before entering here.
339 *****************************************************************************/
340 #define vmg p_dvd->p_ifo->vmg
341 #define vts p_dvd->p_ifo->vts
343 static void DVDFlushStream( input_thread_t * p_input )
345 if( p_input->stream.pp_programs != NULL )
347 /* We don't use input_EndStream here since
348 * we keep area structures */
349 while( p_input->stream.i_es_number )
351 input_DelES( p_input, p_input->stream.pp_es[0] );
354 while( p_input->stream.i_pgrm_number )
356 input_DelProgram( p_input, p_input->stream.pp_programs[0] );
359 if( p_input->stream.pp_selected_es )
361 free( p_input->stream.pp_selected_es );
362 p_input->stream.pp_selected_es = NULL;
364 p_input->stream.i_selected_es_number = 0;
370 static int DVDReadAngle( input_thread_t * p_input )
372 thread_dvd_data_t * p_dvd;
376 p_dvd = (thread_dvd_data_t*)(p_input->p_access_data);
377 i_angle_nb = vmg.title_inf.p_attr[p_dvd->i_title-1].i_angle_nb;
379 input_AddProgram( p_input, 1, sizeof( stream_ps_data_t ) );
380 p_input->stream.p_selected_program = p_input->stream.pp_programs[0];
382 for( i = 1 ; i < i_angle_nb ; i++ )
384 input_AddProgram( p_input, i+1, 0 );
390 static int DVDSetArea( input_thread_t * p_input, input_area_t * p_area )
392 thread_dvd_data_t * p_dvd;
394 p_dvd = (thread_dvd_data_t*)(p_input->p_access_data);
396 /* we can't use the interface slider until initilization is complete */
397 p_input->stream.b_seekable = 0;
399 if( p_area != p_input->stream.p_selected_area )
405 /* Reset the Chapter position of the old title */
406 p_input->stream.p_selected_area->i_part = 1;
407 p_input->stream.p_selected_area = p_area;
410 * We have to load all title information
413 /* title number as it appears in the interface list */
414 p_dvd->i_title = p_area->i_id;
415 p_dvd->i_chapter_nb = p_area->i_part_nb;
417 if( IfoTitleSet( p_dvd->p_ifo, p_dvd->i_title ) < 0 )
419 msg_Err( p_input, "fatal error in vts ifo" );
424 /* title position inside the selected vts */
425 i_vts_title = vmg.title_inf.p_attr[p_dvd->i_title-1].i_title_num;
427 vts.title_inf.p_title_start[i_vts_title-1].i_title_id;
429 msg_Dbg( p_input, "title %d vts_title %d pgc %d",
430 p_dvd->i_title, i_vts_title, p_dvd->i_title_id );
432 /* title set offset XXX: convert to block values */
434 vts.i_pos + vts.manager_inf.i_title_vob_start_sector;
437 p_dvd->i_prg_cell = -1 +
438 vts.title_unit.p_title[p_dvd->i_title_id-1].title.i_cell_nb;
439 p_dvd->i_map_cell = 0;
440 p_dvd->i_map_cell = CellPrg2Map( p_dvd );
441 i_last = CellLastSector( p_dvd );
444 p_dvd->i_prg_cell = 0;
445 p_dvd->i_map_cell = 0;
446 p_dvd->i_angle_cell = 0;
447 p_dvd->i_map_cell = CellPrg2Map ( p_dvd );
448 p_dvd->i_vts_lb = CellFirstSector( p_dvd );
449 p_dvd->i_last_lb = CellLastSector ( p_dvd );
451 /* Force libdvdcss to check its title key.
452 * It is only useful for title cracking method. Methods using the
453 * decrypted disc key are fast enough to check the key at each seek */
454 i_first = dvdcss_seek( p_dvd->dvdhandle,
455 p_dvd->i_vts_start + p_dvd->i_vts_lb,
459 msg_Err( p_input, "%s", dvdcss_error( p_dvd->dvdhandle ) );
463 /* Area definition */
464 p_input->stream.p_selected_area->i_start = LB2OFF( i_first );
465 p_input->stream.p_selected_area->i_size =
466 LB2OFF( i_last + 1 - p_dvd->i_vts_lb );
468 /* Destroy obsolete ES by reinitializing programs */
469 DVDFlushStream( p_input );
471 /* Angle management: angles are handled through programs */
472 p_dvd->i_angle_nb = DVDReadAngle( p_input );
473 if( ( p_dvd->i_angle <= 0 ) || p_dvd->i_angle > p_dvd->i_angle_nb )
478 DVDSetProgram( p_input,
479 p_input->stream.pp_programs[p_dvd->i_angle-1] );
481 msg_Dbg( p_input, "title first %i, last %i, size %i",
482 i_first, i_last, i_last + 1 - p_dvd->i_vts_lb );
483 IfoPrintTitle( p_dvd );
485 /* No PSM to read in DVD mode, we already have all information */
486 p_input->stream.p_selected_program->b_is_ok = 1;
488 /* Find all ES in title with ifo data */
489 DVDReadVideo( p_input );
490 DVDReadAudio( p_input );
491 DVDReadSPU ( p_input );
493 if( p_input->p_demux )
495 DVDLaunchDecoders( p_input );
501 p_area = p_input->stream.p_selected_area;
504 /* Chapter selection */
505 p_dvd->i_chapter = DVDSetChapter( p_dvd, p_area->i_part );
507 p_input->stream.p_selected_area->i_tell = DVDTell;
509 /* warn interface that something has changed */
510 p_input->stream.b_seekable = 1;
511 p_input->stream.b_changed = 1;
519 p_dvd->p_ifo->vts.title_unit.p_title[p_dvd->i_title_id-1].title
521 /*****************************************************************************
522 * DVDRead: reads data packets.
523 *****************************************************************************
524 * Returns -1 in case of error, 0 in case of EOF, otherwise the number of
526 *****************************************************************************/
527 static ssize_t DVDRead( input_thread_t * p_input,
528 byte_t * p_buffer, size_t i_count )
530 thread_dvd_data_t * p_dvd;
533 int i_block_once = 0;
535 p_dvd = (thread_dvd_data_t *)(p_input->p_access_data);
538 i_blocks = OFF2LB(i_count);
542 i_block_once = LbMaxOnce( p_dvd );
543 if( i_block_once > i_blocks )
545 i_block_once = i_blocks;
547 else if( i_block_once <= 0 )
553 if( i_block_once != dvdcss_read( p_dvd->dvdhandle, p_buffer,
554 i_block_once, DVDCSS_READ_DECRYPT ) )
559 i_blocks -= i_block_once;
560 i_read += i_block_once;
561 p_buffer += LB2OFF( i_block_once );
563 /* Update global position */
564 p_dvd->i_vts_lb += i_block_once;
567 vlc_mutex_lock( &p_input->stream.stream_lock );
569 if( p_dvd->b_new_chapter )
571 p_input->stream.p_selected_area->i_part = p_dvd->i_chapter;
572 p_dvd->b_new_chapter = 0;
575 if( ( p_input->stream.p_selected_area->i_tell + LB2OFF( i_read )
576 >= p_input->stream.p_selected_area->i_size )
577 || ( i_block_once <= 0 ) )
579 if( ( p_dvd->i_title + 1 ) >= p_input->stream.i_area_nb )
582 vlc_mutex_unlock( &p_input->stream.stream_lock );
587 msg_Dbg( p_input, "new title" );
589 DVDSetArea( p_input, p_input->stream.pp_areas[p_dvd->i_title] );
592 vlc_mutex_unlock( &p_input->stream.stream_lock );
594 return LB2OFF( i_read );
597 /*****************************************************************************
598 * DVDSeek : Goes to a given position on the stream.
599 *****************************************************************************
600 * This one is used by the input and translate chronological position from
601 * input to logical position on the device.
602 * The lock should be taken before calling this function.
603 *****************************************************************************/
604 static void DVDSeek( input_thread_t * p_input, off_t i_off )
606 thread_dvd_data_t * p_dvd;
608 p_dvd = ( thread_dvd_data_t * )(p_input->p_access_data);
610 vlc_mutex_lock( &p_input->stream.stream_lock );
611 p_dvd->i_vts_lb = OFF2LB(i_off + p_input->stream.p_selected_area->i_start)
612 - p_dvd->i_vts_start;
613 vlc_mutex_unlock( &p_input->stream.stream_lock );
615 p_dvd->i_prg_cell = Lb2CellPrg( p_dvd );
616 p_dvd->i_map_cell = Lb2CellMap( p_dvd );
618 if( CellIsInterleaved( p_dvd ) )
620 /* if we're inside a multi-angle zone, we have to choose i_sector
621 * in the current angle ; we can't do it all the time since cells
622 * can be very wide out of such zones */
623 p_dvd->i_vts_lb = CellFirstSector( p_dvd );
626 p_dvd->i_last_lb = CellLastSector( p_dvd );
627 p_dvd->i_chapter = CellPrg2Chapter( p_dvd );
629 if( dvdcss_seek( p_dvd->dvdhandle, p_dvd->i_vts_start + p_dvd->i_vts_lb,
630 DVDCSS_SEEK_MPEG ) < 0 )
632 msg_Err( p_input, "%s", dvdcss_error( p_dvd->dvdhandle ) );
633 p_input->b_error = 1;
637 vlc_mutex_lock( &p_input->stream.stream_lock );
638 p_input->stream.p_selected_area->i_part = p_dvd->i_chapter;
639 p_input->stream.p_selected_area->i_tell = DVDTell;
640 vlc_mutex_unlock( &p_input->stream.stream_lock );
642 msg_Dbg( p_input, "program cell: %d cell: %d chapter: %d tell "I64Fd,
643 p_dvd->i_prg_cell, p_dvd->i_map_cell, p_dvd->i_chapter, DVDTell );
648 /*****************************************************************************
649 * DVDParse: parse command line
650 *****************************************************************************/
651 static char * DVDParse( input_thread_t * p_input )
653 thread_dvd_data_t * p_dvd;
654 struct stat stat_info;
659 vlc_bool_t b_options = 0;
665 p_dvd = (thread_dvd_data_t*)(p_input->p_access_data);
668 /* On Win32 we want the DVD access plugin to be explicitly requested,
669 * we end up with lots of problems otherwise */
670 if( !p_input->psz_access || !*p_input->psz_access ) return NULL;
673 psz_parser = psz_device = strdup( p_input->psz_name );
679 /* Parse input string :
680 * [device][@rawdevice][@[title][,[chapter][,angle]]] */
681 while( *psz_parser && *psz_parser != '@' )
686 if( *psz_parser == '@' )
688 /* Maybe found raw device or option list */
690 psz_raw = ++psz_parser;
697 if( *psz_parser && !strtol( psz_parser, NULL, 10 ) )
699 /* what we've found is either a raw device or a partial option
700 * list e.g. @,29 or both a device and a list ; search end of string */
701 while( *psz_parser && *psz_parser != '@' )
706 if( *psz_parser == '@' )
708 /* found end of raw device, and beginning of options */
715 psz_parser = psz_raw + 1;
716 for( i=0 ; i<3 ; i++ )
720 /* we have only a raw device */
723 if( strtol( psz_parser, NULL, 10 ) )
725 /* we have only a partial list of options, no device */
726 psz_parser = psz_raw;
737 /* found beginning of options ; no raw device specified */
745 i_title = (int)strtol( psz_parser, &psz_next, 10 );
748 psz_parser = psz_next + 1;
749 i_chapter = (int)strtol( psz_parser, &psz_next, 10 );
752 i_angle = (int)strtol( psz_next + 1, NULL, 10 );
756 p_dvd->i_title = i_title ? i_title : 1;
757 p_dvd->i_chapter = i_chapter ? i_chapter : 1;
758 p_dvd->i_angle = i_angle ? i_angle : 1;
765 /* check the raw device */
766 if( stat( psz_raw, &stat_info ) == -1 )
768 msg_Warn( p_input, "cannot stat() raw device `%s' (%s)",
769 psz_raw, strerror(errno));
771 *(psz_raw - 1) = '@';
779 if( !S_ISCHR(stat_info.st_mode) )
781 msg_Warn( p_input, "raw device %s is"
782 " not a valid char device", psz_raw );
784 *(psz_raw - 1) = '@';
790 psz_env = malloc( strlen("DVDCSS_RAW_DEVICE=")
791 + strlen( psz_raw ) + 1 );
792 sprintf( psz_env, "DVDCSS_RAW_DEVICE=%s", psz_raw );
807 if( !p_input->psz_access )
809 /* no device and no access specified: we probably don't want DVD */
812 psz_device = config_GetPsz( p_input, "dvd" );
816 /* check block device */
817 if( stat( psz_device, &stat_info ) == -1 )
819 msg_Err( p_input, "cannot stat() device `%s' (%s)",
820 psz_device, strerror(errno));
825 if( !S_ISBLK(stat_info.st_mode) && !S_ISCHR(stat_info.st_mode) )
828 "dvd module discarded (not a valid block device)" );
834 msg_Dbg( p_input, "dvd=%s raw=%s title=%d chapter=%d angle=%d",
835 psz_device, psz_raw, p_dvd->i_title,
836 p_dvd->i_chapter, p_dvd->i_angle );