access: srt: add support stream encryption
[vlc.git] / modules / access / dvdread.c
blob641ad96122f96702fc9a0fe7acd470025a3fe03a
1 /*****************************************************************************
2 * dvdread.c : DvdRead input module for vlc
3 *****************************************************************************
4 * Copyright (C) 2001-2006 VLC authors and VideoLAN
5 * $Id$
7 * Authors: Stéphane Borel <stef@via.ecp.fr>
8 * Gildas Bazin <gbazin@videolan.org>
10 * This program is free software; you can redistribute it and/or modify it
11 * under the terms of the GNU Lesser General Public License as published by
12 * the Free Software Foundation; either version 2.1 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public License
21 * along with this program; if not, write to the Free Software Foundation,
22 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 *****************************************************************************/
25 /*****************************************************************************
26 * NOTA BENE: this module requires the linking against a library which is
27 * known to require licensing under the GNU General Public License version 2
28 * (or later). Therefore, the result of compiling this module will normally
29 * be subject to the terms of that later license.
30 *****************************************************************************/
33 /*****************************************************************************
34 * Preamble
35 *****************************************************************************/
37 #ifdef HAVE_CONFIG_H
38 # include "config.h"
39 #endif
41 #include <vlc_common.h>
42 #include <vlc_plugin.h>
43 #include <vlc_input.h>
44 #include <vlc_access.h>
45 #include <vlc_charset.h>
46 #include <vlc_interface.h>
47 #include <vlc_dialog.h>
49 #include <vlc_iso_lang.h>
51 #include "../demux/mpeg/pes.h"
52 #include "../demux/mpeg/ps.h"
54 #include <sys/types.h>
55 #include <unistd.h>
57 #include <dvdread/dvd_reader.h>
58 #include <dvdread/ifo_types.h>
59 #include <dvdread/ifo_read.h>
60 #include <dvdread/nav_read.h>
61 #include <dvdread/nav_print.h>
63 #include <assert.h>
65 /*****************************************************************************
66 * Module descriptor
67 *****************************************************************************/
68 #define ANGLE_TEXT N_("DVD angle")
69 #define ANGLE_LONGTEXT N_( \
70 "Default DVD angle." )
72 static int Open ( vlc_object_t * );
73 static void Close( vlc_object_t * );
75 vlc_module_begin ()
76 set_shortname( N_("DVD without menus") )
77 set_description( N_("DVDRead Input (no menu support)") )
78 set_category( CAT_INPUT )
79 set_subcategory( SUBCAT_INPUT_ACCESS )
80 add_integer( "dvdread-angle", 1, ANGLE_TEXT,
81 ANGLE_LONGTEXT, false )
82 add_obsolete_string( "dvdread-css-method" ) /* obsolete since 1.1.0 */
83 set_capability( "access_demux", 0 )
84 add_shortcut( "dvd", "dvdread", "dvdsimple" )
85 set_callbacks( Open, Close )
86 vlc_module_end ()
88 /* how many blocks DVDRead will read in each loop */
89 #define DVD_BLOCK_READ_ONCE 4
91 /*****************************************************************************
92 * Local prototypes
93 *****************************************************************************/
95 struct demux_sys_t
97 /* DVDRead state */
98 dvd_reader_t *p_dvdread;
99 dvd_file_t *p_title;
101 ifo_handle_t *p_vmg_file;
102 ifo_handle_t *p_vts_file;
104 int i_title;
105 int cur_title;
106 int i_chapter, i_chapters;
107 int cur_chapter;
108 int i_angle, i_angles;
110 tt_srpt_t *p_tt_srpt;
111 pgc_t *p_cur_pgc;
112 dsi_t dsi_pack;
113 int i_ttn;
115 int i_pack_len;
116 int i_cur_block;
117 int i_next_vobu;
119 int i_mux_rate;
121 /* Current title start/end blocks */
122 int i_title_start_block;
123 int i_title_end_block;
124 int i_title_blocks;
125 int i_title_offset;
126 mtime_t i_title_cur_time;
128 int i_title_start_cell;
129 int i_title_end_cell;
130 int i_cur_cell;
131 int i_next_cell;
132 mtime_t i_cell_cur_time;
133 mtime_t i_cell_duration;
135 /* Track */
136 ps_track_t tk[PS_TK_COUNT];
138 int i_titles;
139 input_title_t **titles;
141 /* Video */
142 int i_sar_num;
143 int i_sar_den;
145 /* SPU */
146 uint32_t clut[16];
149 static int Control ( demux_t *, int, va_list );
150 static int Demux ( demux_t * );
151 static int DemuxBlock( demux_t *, const uint8_t *, int );
153 static void DemuxTitles( demux_t *, int * );
154 static void ESNew( demux_t *, int, int );
156 static int DvdReadSetArea ( demux_t *, int, int, int );
157 static void DvdReadSeek ( demux_t *, int );
158 static void DvdReadHandleDSI( demux_t *, uint8_t * );
159 static void DvdReadFindCell ( demux_t * );
161 /*****************************************************************************
162 * Open:
163 *****************************************************************************/
164 static int Open( vlc_object_t *p_this )
166 demux_t *p_demux = (demux_t*)p_this;
167 demux_sys_t *p_sys;
168 char *psz_file;
169 ifo_handle_t *p_vmg_file;
171 if( !p_demux->psz_filepath || !*p_demux->psz_filepath )
172 psz_file = var_InheritString( p_this, "dvd" );
173 else
174 psz_file = strdup( p_demux->psz_filepath );
176 #if defined( _WIN32 ) || defined( __OS2__ )
177 if( psz_file != NULL )
179 size_t flen = strlen( psz_file );
180 if( flen > 0 && psz_file[flen - 1] == '\\' )
181 psz_file[flen - 1] = '\0';
183 else
184 psz_file = strdup("");
185 #endif
186 if( unlikely(psz_file == NULL) )
187 return VLC_EGENERIC;
189 /* Open dvdread */
190 const char *psz_path = ToLocale( psz_file );
191 dvd_reader_t *p_dvdread = DVDOpen( psz_path );
193 LocaleFree( psz_path );
194 if( p_dvdread == NULL )
196 msg_Err( p_demux, "DVDRead cannot open source: %s", psz_file );
197 vlc_dialog_display_error( p_demux, _("Playback failure"),
198 _("DVDRead could not open the disc \"%s\"."), psz_file );
199 free( psz_file );
200 return VLC_EGENERIC;
202 free( psz_file );
204 /* Ifo allocation & initialisation */
205 if( !( p_vmg_file = ifoOpen( p_dvdread, 0 ) ) )
207 char rgsz_volid[32];
208 if( DVDUDFVolumeInfo( p_dvdread, rgsz_volid, 32, NULL, 0 ) )
210 if( DVDISOVolumeInfo( p_dvdread, rgsz_volid, 32, NULL, 0 ) == 0 )
212 vlc_dialog_display_error( p_demux, _("Playback failure"),
213 _("Cannot play a non-UDF mastered DVD." ) );
214 msg_Err( p_demux, "Invalid UDF DVD. (Found ISO9660 '%s')", rgsz_volid );
217 msg_Warn( p_demux, "cannot open VMG info" );
218 return VLC_EGENERIC;
220 msg_Dbg( p_demux, "VMG opened" );
222 /* Fill p_demux field */
223 DEMUX_INIT_COMMON(); p_sys = p_demux->p_sys;
225 ps_track_init( p_sys->tk );
226 p_sys->i_sar_num = 0;
227 p_sys->i_sar_den = 0;
228 p_sys->i_title_cur_time = (mtime_t) 0;
229 p_sys->i_cell_cur_time = (mtime_t) 0;
230 p_sys->i_cell_duration = (mtime_t) 0;
232 p_sys->p_dvdread = p_dvdread;
233 p_sys->p_vmg_file = p_vmg_file;
234 p_sys->p_title = NULL;
235 p_sys->p_vts_file = NULL;
237 p_sys->i_title = p_sys->i_chapter = -1;
238 p_sys->cur_title = p_sys->cur_chapter = 0;
239 p_sys->i_mux_rate = 0;
241 p_sys->i_angle = var_CreateGetInteger( p_demux, "dvdread-angle" );
242 if( p_sys->i_angle <= 0 ) p_sys->i_angle = 1;
244 DemuxTitles( p_demux, &p_sys->i_angle );
245 if( DvdReadSetArea( p_demux, 0, 0, p_sys->i_angle ) != VLC_SUCCESS )
247 msg_Err( p_demux, "DvdReadSetArea(0,0,%i) failed (can't decrypt DVD?)",
248 p_sys->i_angle );
249 Close( p_this );
250 return VLC_EGENERIC;
253 return VLC_SUCCESS;
256 /*****************************************************************************
257 * Close:
258 *****************************************************************************/
259 static void Close( vlc_object_t *p_this )
261 demux_t *p_demux = (demux_t*)p_this;
262 demux_sys_t *p_sys = p_demux->p_sys;
264 for( int i = 0; i < PS_TK_COUNT; i++ )
266 ps_track_t *tk = &p_sys->tk[i];
267 if( tk->b_configured )
269 es_format_Clean( &tk->fmt );
270 if( tk->es ) es_out_Del( p_demux->out, tk->es );
274 /* Free the array of titles */
275 for( int i = 0; i < p_sys->i_titles; i++ )
276 vlc_input_title_Delete( p_sys->titles[i] );
277 TAB_CLEAN( p_sys->i_titles, p_sys->titles );
279 /* Close libdvdread */
280 if( p_sys->p_title ) DVDCloseFile( p_sys->p_title );
281 if( p_sys->p_vts_file ) ifoClose( p_sys->p_vts_file );
282 if( p_sys->p_vmg_file ) ifoClose( p_sys->p_vmg_file );
283 DVDClose( p_sys->p_dvdread );
285 free( p_sys );
288 static int64_t dvdtime_to_time( dvd_time_t *dtime, uint8_t still_time )
290 /* Macro to convert Binary Coded Decimal to Decimal */
291 #define BCD2D(__x__) (((__x__ & 0xf0) >> 4) * 10 + (__x__ & 0x0f))
293 double f_fps, f_ms;
294 int64_t i_micro_second = 0;
296 if (still_time == 0 || still_time == 0xFF)
298 i_micro_second += (int64_t)(BCD2D(dtime->hour)) * 60 * 60 * 1000000;
299 i_micro_second += (int64_t)(BCD2D(dtime->minute)) * 60 * 1000000;
300 i_micro_second += (int64_t)(BCD2D(dtime->second)) * 1000000;
302 switch((dtime->frame_u & 0xc0) >> 6)
304 case 1:
305 f_fps = 25.0;
306 break;
307 case 3:
308 f_fps = 29.97;
309 break;
310 default:
311 f_fps = 2500.0;
312 break;
314 f_ms = BCD2D(dtime->frame_u&0x3f) * 1000.0 / f_fps;
315 i_micro_second += (int64_t)(f_ms * 1000.0);
317 else
319 i_micro_second = still_time;
320 i_micro_second = (int64_t)((double)i_micro_second * 1000000.0);
323 return i_micro_second;
326 /*****************************************************************************
327 * Control:
328 *****************************************************************************/
329 static int Control( demux_t *p_demux, int i_query, va_list args )
331 demux_sys_t *p_sys = p_demux->p_sys;
332 double f, *pf;
333 bool *pb;
334 int64_t *pi64;
335 input_title_t ***ppp_title;
336 int *pi_int;
337 int i;
339 switch( i_query )
341 case DEMUX_GET_POSITION:
343 pf = va_arg( args, double * );
345 if( p_sys->i_title_blocks > 0 )
346 *pf = (double)p_sys->i_title_offset / p_sys->i_title_blocks;
347 else
348 *pf = 0.0;
350 return VLC_SUCCESS;
352 case DEMUX_SET_POSITION:
354 f = va_arg( args, double );
356 DvdReadSeek( p_demux, f * p_sys->i_title_blocks );
358 return VLC_SUCCESS;
360 case DEMUX_GET_TIME:
361 pi64 = va_arg( args, int64_t * );
362 if( p_sys->cur_title >= 0 && p_sys->cur_title < p_sys->i_titles )
364 *pi64 = (int64_t) dvdtime_to_time( &p_sys->p_cur_pgc->playback_time, 0 ) /
365 p_sys->i_title_blocks * p_sys->i_title_offset;
366 return VLC_SUCCESS;
368 *pi64 = 0;
369 return VLC_EGENERIC;
371 case DEMUX_GET_LENGTH:
372 pi64 = va_arg( args, int64_t * );
373 if( p_sys->cur_title >= 0 && p_sys->cur_title < p_sys->i_titles )
375 *pi64 = (int64_t)dvdtime_to_time( &p_sys->p_cur_pgc->playback_time, 0 );
376 return VLC_SUCCESS;
378 *pi64 = 0;
379 return VLC_EGENERIC;
381 /* Special for access_demux */
382 case DEMUX_CAN_PAUSE:
383 case DEMUX_CAN_SEEK:
384 case DEMUX_CAN_CONTROL_PACE:
385 /* TODO */
386 pb = va_arg( args, bool * );
387 *pb = true;
388 return VLC_SUCCESS;
390 case DEMUX_SET_PAUSE_STATE:
391 return VLC_SUCCESS;
393 case DEMUX_GET_TITLE_INFO:
394 ppp_title = va_arg( args, input_title_t *** );
395 pi_int = va_arg( args, int * );
396 *va_arg( args, int * ) = 1; /* Title offset */
397 *va_arg( args, int * ) = 1; /* Chapter offset */
399 /* Duplicate title infos */
400 *pi_int = p_sys->i_titles;
401 *ppp_title = vlc_alloc( p_sys->i_titles, sizeof(input_title_t *) );
402 for( i = 0; i < p_sys->i_titles; i++ )
404 (*ppp_title)[i] = vlc_input_title_Duplicate(p_sys->titles[i]);
406 return VLC_SUCCESS;
408 case DEMUX_SET_TITLE:
409 i = va_arg( args, int );
410 if( DvdReadSetArea( p_demux, i, 0, -1 ) != VLC_SUCCESS )
412 msg_Warn( p_demux, "cannot set title/chapter" );
413 return VLC_EGENERIC;
415 p_demux->info.i_update |=
416 INPUT_UPDATE_TITLE | INPUT_UPDATE_SEEKPOINT;
417 p_sys->cur_title = i;
418 p_sys->cur_chapter = 0;
419 return VLC_SUCCESS;
421 case DEMUX_SET_SEEKPOINT:
422 i = va_arg( args, int );
423 if( DvdReadSetArea( p_demux, -1, i, -1 ) != VLC_SUCCESS )
425 msg_Warn( p_demux, "cannot set title/chapter" );
426 return VLC_EGENERIC;
428 p_demux->info.i_update |= INPUT_UPDATE_SEEKPOINT;
429 p_sys->cur_chapter = i;
430 return VLC_SUCCESS;
432 case DEMUX_GET_TITLE:
433 *va_arg( args, int * ) = p_sys->cur_title;
434 return VLC_SUCCESS;
436 case DEMUX_GET_SEEKPOINT:
437 *va_arg( args, int * ) = p_sys->cur_chapter;
438 return VLC_SUCCESS;
440 case DEMUX_GET_PTS_DELAY:
441 pi64 = va_arg( args, int64_t * );
442 *pi64 =
443 INT64_C(1000) * var_InheritInteger( p_demux, "disc-caching" );
444 return VLC_SUCCESS;
446 /* TODO implement others */
447 default:
448 return VLC_EGENERIC;
452 /*****************************************************************************
453 * Demux:
454 *****************************************************************************/
455 static int Demux( demux_t *p_demux )
457 demux_sys_t *p_sys = p_demux->p_sys;
459 uint8_t p_buffer[DVD_VIDEO_LB_LEN * DVD_BLOCK_READ_ONCE];
460 int i_blocks_once, i_read;
463 * Playback by cell in this pgc, starting at the cell for our chapter.
467 * Check end of pack, and select the following one
469 if( !p_sys->i_pack_len )
471 /* Read NAV packet */
472 if( DVDReadBlocks( p_sys->p_title, p_sys->i_next_vobu,
473 1, p_buffer ) != 1 )
475 msg_Err( p_demux, "read failed for block %d", p_sys->i_next_vobu );
476 vlc_dialog_display_error( p_demux, _("Playback failure"),
477 _("DVDRead could not read block %d."),
478 p_sys->i_next_vobu );
479 return -1;
482 /* Basic check to be sure we don't have a empty title
483 * go to next title if so */
484 //assert( p_buffer[41] == 0xbf && p_buffer[1027] == 0xbf );
486 /* Parse the contained dsi packet */
487 DvdReadHandleDSI( p_demux, p_buffer );
489 /* End of title */
490 if( p_sys->i_cur_cell >= p_sys->p_cur_pgc->nr_of_cells )
492 int k = p_sys->i_title;
494 /* Looking for a not broken title */
495 while( k < p_sys->i_titles && DvdReadSetArea( p_demux, ++k, 0, -1 ) != VLC_SUCCESS )
497 msg_Err(p_demux, "Failed next title, trying another: %i", k );
498 if( k >= p_sys->i_titles )
499 return 0; // EOF
503 if( p_sys->i_pack_len >= 1024 )
505 msg_Err( p_demux, "i_pack_len >= 1024 (%i). "
506 "This shouldn't happen!", p_sys->i_pack_len );
507 return 0; /* EOF */
510 /* FIXME: Ugly kludge: we send the pack block to the input for it
511 * sometimes has a zero scr and restart the sync */
512 p_sys->i_cur_block++;
513 p_sys->i_title_offset++;
515 DemuxBlock( p_demux, p_buffer, DVD_VIDEO_LB_LEN );
518 if( p_sys->i_cur_cell >= p_sys->p_cur_pgc->nr_of_cells )
520 int k = p_sys->i_title;
522 /* Looking for a not broken title */
523 while( k < p_sys->i_titles && DvdReadSetArea( p_demux, ++k, 0, -1 ) != VLC_SUCCESS )
525 msg_Err(p_demux, "Failed next title, trying another: %i", k );
526 if( k >= p_sys->i_titles )
527 return 0; // EOF
532 * Read actual data
534 i_blocks_once = __MIN( p_sys->i_pack_len, DVD_BLOCK_READ_ONCE );
535 p_sys->i_pack_len -= i_blocks_once;
537 /* Reads from DVD */
538 i_read = DVDReadBlocks( p_sys->p_title, p_sys->i_cur_block,
539 i_blocks_once, p_buffer );
540 if( i_read != i_blocks_once )
542 msg_Err( p_demux, "read failed for %d/%d blocks at 0x%02x",
543 i_read, i_blocks_once, p_sys->i_cur_block );
544 vlc_dialog_display_error( p_demux, _("Playback failure"),
545 _("DVDRead could not read %d/%d blocks at 0x%02x."),
546 i_read, i_blocks_once, p_sys->i_cur_block );
547 return -1;
550 p_sys->i_cur_block += i_read;
551 p_sys->i_title_offset += i_read;
553 #if 0
554 msg_Dbg( p_demux, "i_blocks: %d len: %d current: 0x%02x",
555 i_read, p_sys->i_pack_len, p_sys->i_cur_block );
556 #endif
558 for( int i = 0; i < i_read; i++ )
560 DemuxBlock( p_demux, p_buffer + i * DVD_VIDEO_LB_LEN,
561 DVD_VIDEO_LB_LEN );
564 #undef p_pgc
566 return 1;
569 /*****************************************************************************
570 * DemuxBlock: demux a given block
571 *****************************************************************************/
572 static int DemuxBlock( demux_t *p_demux, const uint8_t *p, int len )
574 demux_sys_t *p_sys = p_demux->p_sys;
576 while( len > 0 )
578 int i_size = ps_pkt_size( p, len );
579 if( i_size <= 0 || i_size > len )
581 break;
584 /* Create a block */
585 block_t *p_pkt = block_Alloc( i_size );
586 memcpy( p_pkt->p_buffer, p, i_size);
588 /* Parse it and send it */
589 switch( 0x100 | p[3] )
591 case 0x1b9:
592 case 0x1bb:
593 case 0x1bc:
595 #ifdef DVDREAD_DEBUG
596 if( p[3] == 0xbc )
598 msg_Warn( p_demux, "received a PSM packet" );
600 else if( p[3] == 0xbb )
602 msg_Warn( p_demux, "received a SYSTEM packet" );
604 #endif
605 block_Release( p_pkt );
606 break;
608 case 0x1ba:
610 int64_t i_scr;
611 int i_mux_rate;
612 if( !ps_pkt_parse_pack( p_pkt, &i_scr, &i_mux_rate ) )
614 es_out_SetPCR( p_demux->out, i_scr );
615 if( i_mux_rate > 0 ) p_sys->i_mux_rate = i_mux_rate;
617 block_Release( p_pkt );
618 break;
620 default:
622 int i_id = ps_pkt_id( p_pkt );
623 if( i_id >= 0xc0 )
625 ps_track_t *tk = &p_sys->tk[ps_id_to_tk(i_id)];
627 if( !tk->b_configured )
629 ESNew( p_demux, i_id, 0 );
631 if( tk->es &&
632 !ps_pkt_parse_pes( VLC_OBJECT(p_demux), p_pkt, tk->i_skip ) )
634 es_out_Send( p_demux->out, tk->es, p_pkt );
636 else
638 block_Release( p_pkt );
641 else
643 block_Release( p_pkt );
645 break;
649 p += i_size;
650 len -= i_size;
653 return VLC_SUCCESS;
656 /*****************************************************************************
657 * ESNew: register a new elementary stream
658 *****************************************************************************/
659 static void ESNew( demux_t *p_demux, int i_id, int i_lang )
661 demux_sys_t *p_sys = p_demux->p_sys;
662 ps_track_t *tk = &p_sys->tk[ps_id_to_tk(i_id)];
663 char psz_language[3];
665 if( tk->b_configured ) return;
667 if( ps_track_fill( tk, 0, i_id, NULL, true ) )
669 msg_Warn( p_demux, "unknown codec for id=0x%x", i_id );
670 return;
673 psz_language[0] = psz_language[1] = psz_language[2] = 0;
674 if( i_lang && i_lang != 0xffff )
676 psz_language[0] = (i_lang >> 8)&0xff;
677 psz_language[1] = (i_lang )&0xff;
680 /* Add a new ES */
681 if( tk->fmt.i_cat == VIDEO_ES )
683 tk->fmt.video.i_sar_num = p_sys->i_sar_num;
684 tk->fmt.video.i_sar_den = p_sys->i_sar_den;
686 else if( tk->fmt.i_cat == AUDIO_ES )
688 #if 0
689 int i_audio = -1;
690 /* find the audio number PLEASE find another way */
691 if( (i_id&0xbdf8) == 0xbd88 ) /* dts */
693 i_audio = i_id&0x07;
695 else if( (i_id&0xbdf0) == 0xbd80 ) /* a52 */
697 i_audio = i_id&0xf;
699 else if( (i_id&0xbdf0) == 0xbda0 ) /* lpcm */
701 i_audio = i_id&0x1f;
703 else if( ( i_id&0xe0 ) == 0xc0 ) /* mpga */
705 i_audio = i_id&0x1f;
707 #endif
709 if( psz_language[0] ) tk->fmt.psz_language = strdup( psz_language );
711 else if( tk->fmt.i_cat == SPU_ES )
713 /* Palette */
714 tk->fmt.subs.spu.palette[0] = SPU_PALETTE_DEFINED;
715 memcpy( &tk->fmt.subs.spu.palette[1], p_sys->clut,
716 16 * sizeof( uint32_t ) );
718 if( psz_language[0] ) tk->fmt.psz_language = strdup( psz_language );
721 tk->es = es_out_Add( p_demux->out, &tk->fmt );
722 tk->b_configured = true;
725 /*****************************************************************************
726 * DvdReadSetArea: initialize input data for title x, chapter y.
727 * It should be called for each user navigation request.
728 *****************************************************************************
729 * Take care that i_title and i_chapter start from 0.
730 *****************************************************************************/
731 static int DvdReadSetArea( demux_t *p_demux, int i_title, int i_chapter,
732 int i_angle )
734 VLC_UNUSED( i_angle );
736 demux_sys_t *p_sys = p_demux->p_sys;
737 int pgc_id = 0, pgn = 0;
739 #define p_pgc p_sys->p_cur_pgc
740 #define p_vmg p_sys->p_vmg_file
741 #define p_vts p_sys->p_vts_file
743 if( i_title >= 0 && i_title < p_sys->i_titles &&
744 i_title != p_sys->i_title )
746 int i_start_cell, i_end_cell;
748 if( p_sys->p_title != NULL )
750 DVDCloseFile( p_sys->p_title );
751 p_sys->p_title = NULL;
753 if( p_vts != NULL ) ifoClose( p_vts );
754 p_sys->i_title = i_title;
757 * We have to load all title information
759 msg_Dbg( p_demux, "open VTS %d, for title %d",
760 p_vmg->tt_srpt->title[i_title].title_set_nr, i_title + 1 );
762 /* Ifo vts */
763 if( !( p_vts = ifoOpen( p_sys->p_dvdread,
764 p_vmg->tt_srpt->title[i_title].title_set_nr ) ) )
766 msg_Err( p_demux, "fatal error in vts ifo" );
767 return VLC_EGENERIC;
770 /* Title position inside the selected vts */
771 p_sys->i_ttn = p_vmg->tt_srpt->title[i_title].vts_ttn;
773 /* Find title start/end */
774 pgc_id = p_vts->vts_ptt_srpt->title[p_sys->i_ttn - 1].ptt[0].pgcn;
775 pgn = p_vts->vts_ptt_srpt->title[p_sys->i_ttn - 1].ptt[0].pgn;
776 p_pgc = p_vts->vts_pgcit->pgci_srp[pgc_id - 1].pgc;
778 if( p_pgc->cell_playback == NULL )
780 msg_Err( p_demux, "Invalid PGC (cell_playback_offset)" );
781 return VLC_EGENERIC;
784 p_sys->i_title_start_cell =
785 i_start_cell = p_pgc->program_map[pgn - 1] - 1;
786 p_sys->i_title_start_block =
787 p_pgc->cell_playback[i_start_cell].first_sector;
789 p_sys->i_title_end_cell =
790 i_end_cell = p_pgc->nr_of_cells - 1;
791 p_sys->i_title_end_block =
792 p_pgc->cell_playback[i_end_cell].last_sector;
794 p_sys->i_title_offset = 0;
796 p_sys->i_title_blocks = 0;
797 for( int i = i_start_cell; i <= i_end_cell; i++ )
799 p_sys->i_title_blocks += p_pgc->cell_playback[i].last_sector -
800 p_pgc->cell_playback[i].first_sector + 1;
803 msg_Dbg( p_demux, "title %d vts_title %d pgc %d pgn %d "
804 "start %d end %d blocks: %d",
805 i_title + 1, p_sys->i_ttn, pgc_id, pgn,
806 p_sys->i_title_start_block, p_sys->i_title_end_block,
807 p_sys->i_title_blocks );
810 * Set properties for current chapter
812 p_sys->i_chapter = 0;
813 p_sys->i_chapters =
814 p_vts->vts_ptt_srpt->title[p_sys->i_ttn - 1].nr_of_ptts;
816 pgc_id = p_vts->vts_ptt_srpt->title[
817 p_sys->i_ttn - 1].ptt[p_sys->i_chapter].pgcn;
818 pgn = p_vts->vts_ptt_srpt->title[
819 p_sys->i_ttn - 1].ptt[p_sys->i_chapter].pgn;
821 p_pgc = p_vts->vts_pgcit->pgci_srp[pgc_id - 1].pgc;
822 p_sys->i_pack_len = 0;
823 p_sys->i_next_cell =
824 p_sys->i_cur_cell = p_pgc->program_map[pgn - 1] - 1;
825 DvdReadFindCell( p_demux );
827 p_sys->i_next_vobu = p_sys->i_cur_block =
828 p_pgc->cell_playback[p_sys->i_cur_cell].first_sector;
831 * Angle management
833 p_sys->i_angles = p_vmg->tt_srpt->title[i_title].nr_of_angles;
834 if( p_sys->i_angle > p_sys->i_angles ) p_sys->i_angle = 1;
837 * We've got enough info, time to open the title set data.
839 if( !( p_sys->p_title = DVDOpenFile( p_sys->p_dvdread,
840 p_vmg->tt_srpt->title[i_title].title_set_nr,
841 DVD_READ_TITLE_VOBS ) ) )
843 msg_Err( p_demux, "cannot open title (VTS_%02d_1.VOB)",
844 p_vmg->tt_srpt->title[i_title].title_set_nr );
845 return VLC_EGENERIC;
848 //IfoPrintTitle( p_demux );
851 * Destroy obsolete ES by reinitializing program 0
852 * and find all ES in title with ifo data
854 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
856 for( int i = 0; i < PS_TK_COUNT; i++ )
858 ps_track_t *tk = &p_sys->tk[i];
859 if( tk->b_configured )
861 es_format_Clean( &tk->fmt );
862 if( tk->es ) es_out_Del( p_demux->out, tk->es );
864 tk->b_configured = false;
867 if( p_sys->cur_title != i_title )
869 p_demux->info.i_update |=
870 INPUT_UPDATE_TITLE | INPUT_UPDATE_SEEKPOINT;
871 p_sys->cur_title = i_title;
872 p_sys->cur_chapter = 0;
875 /* TODO: re-add angles */
878 ESNew( p_demux, 0xe0, 0 ); /* Video, FIXME ? */
879 const video_attr_t *p_attr = &p_vts->vtsi_mat->vts_video_attr;
880 int i_video_height = p_attr->video_format != 0 ? 576 : 480;
881 int i_video_width;
882 switch( p_attr->picture_size )
884 case 0:
885 i_video_width = 720;
886 break;
887 case 1:
888 i_video_width = 704;
889 break;
890 case 2:
891 i_video_width = 352;
892 break;
893 default:
894 case 3:
895 i_video_width = 352;
896 i_video_height /= 2;
897 break;
899 switch( p_attr->display_aspect_ratio )
901 case 0:
902 p_sys->i_sar_num = 4 * i_video_height;
903 p_sys->i_sar_den = 3 * i_video_width;
904 break;
905 case 3:
906 p_sys->i_sar_num = 16 * i_video_height;
907 p_sys->i_sar_den = 9 * i_video_width;
908 break;
909 default:
910 p_sys->i_sar_num = 0;
911 p_sys->i_sar_den = 0;
912 break;
915 #define audio_control \
916 p_sys->p_vts_file->vts_pgcit->pgci_srp[pgc_id-1].pgc->audio_control[i-1]
918 /* Audio ES, in the order they appear in the .ifo */
919 for( int i = 1; i <= p_vts->vtsi_mat->nr_of_vts_audio_streams; i++ )
921 int i_position = 0;
922 uint16_t i_id;
924 //IfoPrintAudio( p_demux, i );
926 /* Audio channel is active if first byte is 0x80 */
927 if( audio_control & 0x8000 )
929 i_position = ( audio_control & 0x7F00 ) >> 8;
931 msg_Dbg( p_demux, "audio position %d", i_position );
932 switch( p_vts->vtsi_mat->vts_audio_attr[i - 1].audio_format )
934 case 0x00: /* A52 */
935 i_id = (0x80 + i_position) | 0xbd00;
936 break;
937 case 0x02:
938 case 0x03: /* MPEG audio */
939 i_id = 0xc000 + i_position;
940 break;
941 case 0x04: /* LPCM */
942 i_id = (0xa0 + i_position) | 0xbd00;
943 break;
944 case 0x06: /* DTS */
945 i_id = (0x88 + i_position) | 0xbd00;
946 break;
947 default:
948 i_id = 0;
949 msg_Err( p_demux, "unknown audio type %.2x",
950 p_vts->vtsi_mat->vts_audio_attr[i - 1].audio_format );
953 ESNew( p_demux, i_id, p_sys->p_vts_file->vtsi_mat->
954 vts_audio_attr[i - 1].lang_code );
957 #undef audio_control
959 #define spu_palette \
960 p_sys->p_vts_file->vts_pgcit->pgci_srp[pgc_id-1].pgc->palette
962 memcpy( p_sys->clut, spu_palette, 16 * sizeof( uint32_t ) );
964 #define spu_control \
965 p_sys->p_vts_file->vts_pgcit->pgci_srp[pgc_id-1].pgc->subp_control[i-1]
967 /* Sub Picture ES */
968 for( int i = 1; i <= p_vts->vtsi_mat->nr_of_vts_subp_streams; i++ )
970 int i_position = 0;
971 uint16_t i_id;
973 //IfoPrintSpu( p_sys, i );
974 msg_Dbg( p_demux, "spu %d 0x%02x", i, spu_control );
976 if( spu_control & 0x80000000 )
978 /* there are several streams for one spu */
979 if( p_vts->vtsi_mat->vts_video_attr.display_aspect_ratio )
981 /* 16:9 */
982 switch( p_vts->vtsi_mat->vts_video_attr.permitted_df )
984 case 1: /* letterbox */
985 i_position = spu_control & 0xff;
986 break;
987 case 2: /* pan&scan */
988 i_position = ( spu_control >> 8 ) & 0xff;
989 break;
990 default: /* widescreen */
991 i_position = ( spu_control >> 16 ) & 0xff;
992 break;
995 else
997 /* 4:3 */
998 i_position = ( spu_control >> 24 ) & 0x7F;
1001 i_id = (0x20 + i_position) | 0xbd00;
1003 ESNew( p_demux, i_id, p_sys->p_vts_file->vtsi_mat->
1004 vts_subp_attr[i - 1].lang_code );
1007 #undef spu_control
1010 else if( i_title != -1 && i_title != p_sys->i_title )
1013 return VLC_EGENERIC; /* Couldn't set title */
1017 * Chapter selection
1020 if( i_chapter >= 0 && i_chapter < p_sys->i_chapters )
1022 pgc_id = p_vts->vts_ptt_srpt->title[
1023 p_sys->i_ttn - 1].ptt[i_chapter].pgcn;
1024 pgn = p_vts->vts_ptt_srpt->title[
1025 p_sys->i_ttn - 1].ptt[i_chapter].pgn;
1027 p_pgc = p_vts->vts_pgcit->pgci_srp[pgc_id - 1].pgc;
1028 if( p_pgc->cell_playback == NULL )
1029 return VLC_EGENERIC; /* Couldn't set chapter */
1031 p_sys->i_cur_cell = p_pgc->program_map[pgn - 1] - 1;
1032 p_sys->i_chapter = i_chapter;
1033 DvdReadFindCell( p_demux );
1035 p_sys->i_title_offset = 0;
1036 for( int i = p_sys->i_title_start_cell; i < p_sys->i_cur_cell; i++ )
1038 p_sys->i_title_offset += p_pgc->cell_playback[i].last_sector -
1039 p_pgc->cell_playback[i].first_sector + 1;
1042 p_sys->i_pack_len = 0;
1043 p_sys->i_next_vobu = p_sys->i_cur_block =
1044 p_pgc->cell_playback[p_sys->i_cur_cell].first_sector;
1046 if( p_sys->cur_chapter != i_chapter )
1048 p_demux->info.i_update |= INPUT_UPDATE_SEEKPOINT;
1049 p_sys->cur_chapter = i_chapter;
1052 else if( i_chapter != -1 )
1055 return VLC_EGENERIC; /* Couldn't set chapter */
1058 #undef p_pgc
1059 #undef p_vts
1060 #undef p_vmg
1062 return VLC_SUCCESS;
1065 /*****************************************************************************
1066 * DvdReadSeek : Goes to a given position on the stream.
1067 *****************************************************************************
1068 * This one is used by the input and translate chronological position from
1069 * input to logical position on the device.
1070 *****************************************************************************/
1071 static void DvdReadSeek( demux_t *p_demux, int i_block_offset )
1073 demux_sys_t *p_sys = p_demux->p_sys;
1074 int i_chapter = 0;
1075 int i_cell = 0;
1076 int i_vobu = 0;
1077 int i_sub_cell = 0;
1078 int i_block;
1080 #define p_pgc p_sys->p_cur_pgc
1081 #define p_vts p_sys->p_vts_file
1083 /* Find cell */
1084 i_block = i_block_offset;
1085 for( i_cell = p_sys->i_title_start_cell;
1086 i_cell <= p_sys->i_title_end_cell; i_cell++ )
1088 if( i_block < (int)p_pgc->cell_playback[i_cell].last_sector -
1089 (int)p_pgc->cell_playback[i_cell].first_sector + 1 ) break;
1091 i_block -= (p_pgc->cell_playback[i_cell].last_sector -
1092 p_pgc->cell_playback[i_cell].first_sector + 1);
1094 if( i_cell > p_sys->i_title_end_cell )
1096 msg_Err( p_demux, "couldn't find cell for block %i", i_block_offset );
1097 return;
1099 i_block += p_pgc->cell_playback[i_cell].first_sector;
1100 p_sys->i_title_offset = i_block_offset;
1102 /* Find chapter */
1103 for( i_chapter = 0; i_chapter < p_sys->i_chapters; i_chapter++ )
1105 int pgc_id, pgn, i_tmp;
1107 pgc_id = p_vts->vts_ptt_srpt->title[
1108 p_sys->i_ttn - 1].ptt[i_chapter].pgcn;
1109 pgn = p_vts->vts_ptt_srpt->title[
1110 p_sys->i_ttn - 1].ptt[i_chapter].pgn;
1112 i_tmp = p_vts->vts_pgcit->pgci_srp[pgc_id - 1].pgc->program_map[pgn-1];
1114 if( i_tmp > i_cell ) break;
1117 if( i_chapter < p_sys->i_chapters &&
1118 p_sys->cur_chapter != i_chapter )
1120 p_demux->info.i_update |= INPUT_UPDATE_SEEKPOINT;
1121 p_sys->cur_chapter = i_chapter;
1124 /* Find vobu */
1125 while( (int)p_vts->vts_vobu_admap->vobu_start_sectors[i_vobu] <= i_block )
1127 i_vobu++;
1130 /* Find sub_cell */
1131 while( p_vts->vts_c_adt->cell_adr_table[i_sub_cell].start_sector <
1132 p_vts->vts_vobu_admap->vobu_start_sectors[i_vobu-1] )
1134 i_sub_cell++;
1137 #if 1
1138 msg_Dbg( p_demux, "cell %d i_sub_cell %d chapter %d vobu %d "
1139 "cell_sector %d vobu_sector %d sub_cell_sector %d",
1140 i_cell, i_sub_cell, i_chapter, i_vobu,
1141 p_sys->p_cur_pgc->cell_playback[i_cell].first_sector,
1142 p_vts->vts_vobu_admap->vobu_start_sectors[i_vobu],
1143 p_vts->vts_c_adt->cell_adr_table[i_sub_cell - 1].start_sector);
1144 #endif
1146 p_sys->i_cur_block = i_block;
1147 p_sys->i_next_vobu = p_vts->vts_vobu_admap->vobu_start_sectors[i_vobu];
1148 p_sys->i_pack_len = p_sys->i_next_vobu - i_block;
1149 p_sys->i_cur_cell = i_cell;
1150 p_sys->i_chapter = i_chapter;
1151 DvdReadFindCell( p_demux );
1153 #undef p_vts
1154 #undef p_pgc
1156 return;
1159 /*****************************************************************************
1160 * DvdReadHandleDSI
1161 *****************************************************************************/
1162 static void DvdReadHandleDSI( demux_t *p_demux, uint8_t *p_data )
1164 demux_sys_t *p_sys = p_demux->p_sys;
1166 navRead_DSI( &p_sys->dsi_pack, &p_data[DSI_START_BYTE] );
1169 * Determine where we go next. These values are the ones we mostly
1170 * care about.
1172 p_sys->i_cur_block = p_sys->dsi_pack.dsi_gi.nv_pck_lbn;
1173 p_sys->i_pack_len = p_sys->dsi_pack.dsi_gi.vobu_ea;
1176 * Store the timecodes so we can get the current time
1178 p_sys->i_title_cur_time = (mtime_t) p_sys->dsi_pack.dsi_gi.nv_pck_scr / 90 * 1000;
1179 p_sys->i_cell_cur_time = (mtime_t) dvdtime_to_time( &p_sys->dsi_pack.dsi_gi.c_eltm, 0 );
1182 * If we're not at the end of this cell, we can determine the next
1183 * VOBU to display using the VOBU_SRI information section of the
1184 * DSI. Using this value correctly follows the current angle,
1185 * avoiding the doubled scenes in The Matrix, and makes our life
1186 * really happy.
1189 p_sys->i_next_vobu = p_sys->i_cur_block +
1190 ( p_sys->dsi_pack.vobu_sri.next_vobu & 0x7fffffff );
1192 if( p_sys->dsi_pack.vobu_sri.next_vobu != SRI_END_OF_CELL
1193 && p_sys->i_angle > 1 )
1195 switch( ( p_sys->dsi_pack.sml_pbi.category & 0xf000 ) >> 12 )
1197 case 0x4:
1198 /* Interleaved unit with no angle */
1199 if( p_sys->dsi_pack.sml_pbi.ilvu_sa != 0 )
1201 p_sys->i_next_vobu = p_sys->i_cur_block +
1202 p_sys->dsi_pack.sml_pbi.ilvu_sa;
1203 p_sys->i_pack_len = p_sys->dsi_pack.sml_pbi.ilvu_ea;
1205 else
1207 p_sys->i_next_vobu = p_sys->i_cur_block +
1208 p_sys->dsi_pack.dsi_gi.vobu_ea + 1;
1210 break;
1211 case 0x5:
1212 /* vobu is end of ilvu */
1213 if( p_sys->dsi_pack.sml_agli.data[p_sys->i_angle-1].address )
1215 p_sys->i_next_vobu = p_sys->i_cur_block +
1216 p_sys->dsi_pack.sml_agli.data[p_sys->i_angle-1].address;
1217 p_sys->i_pack_len = p_sys->dsi_pack.sml_pbi.ilvu_ea;
1219 break;
1221 /* fall through */
1222 case 0x6:
1223 /* vobu is beginning of ilvu */
1224 case 0x9:
1225 /* next scr is 0 */
1226 case 0xa:
1227 /* entering interleaved section */
1228 case 0x8:
1229 /* non interleaved cells in interleaved section */
1230 default:
1231 p_sys->i_next_vobu = p_sys->i_cur_block +
1232 ( p_sys->dsi_pack.vobu_sri.next_vobu & 0x7fffffff );
1233 break;
1236 else if( p_sys->dsi_pack.vobu_sri.next_vobu == SRI_END_OF_CELL )
1238 p_sys->i_cur_cell = p_sys->i_next_cell;
1240 /* End of title */
1241 if( p_sys->i_cur_cell >= p_sys->p_cur_pgc->nr_of_cells ) return;
1243 DvdReadFindCell( p_demux );
1245 p_sys->i_next_vobu =
1246 p_sys->p_cur_pgc->cell_playback[p_sys->i_cur_cell].first_sector;
1248 p_sys->i_cell_duration = (mtime_t)dvdtime_to_time( &p_sys->p_cur_pgc->cell_playback[p_sys->i_cur_cell].playback_time, 0 );
1252 #if 0
1253 msg_Dbg( p_demux, "scr %d lbn 0x%02x vobu_ea %d vob_id %d c_id %d c_time %lld",
1254 p_sys->dsi_pack.dsi_gi.nv_pck_scr,
1255 p_sys->dsi_pack.dsi_gi.nv_pck_lbn,
1256 p_sys->dsi_pack.dsi_gi.vobu_ea,
1257 p_sys->dsi_pack.dsi_gi.vobu_vob_idn,
1258 p_sys->dsi_pack.dsi_gi.vobu_c_idn,
1259 dvdtime_to_time( &p_sys->dsi_pack.dsi_gi.c_eltm, 0 ) );
1261 msg_Dbg( p_demux, "cell duration: %lld",
1262 (mtime_t)dvdtime_to_time( &p_sys->p_cur_pgc->cell_playback[p_sys->i_cur_cell].playback_time, 0 ) );
1264 msg_Dbg( p_demux, "cat 0x%02x ilvu_ea %d ilvu_sa %d size %d",
1265 p_sys->dsi_pack.sml_pbi.category,
1266 p_sys->dsi_pack.sml_pbi.ilvu_ea,
1267 p_sys->dsi_pack.sml_pbi.ilvu_sa,
1268 p_sys->dsi_pack.sml_pbi.size );
1270 msg_Dbg( p_demux, "next_vobu %d next_ilvu1 %d next_ilvu2 %d",
1271 p_sys->dsi_pack.vobu_sri.next_vobu & 0x7fffffff,
1272 p_sys->dsi_pack.sml_agli.data[ p_sys->i_angle - 1 ].address,
1273 p_sys->dsi_pack.sml_agli.data[ p_sys->i_angle ].address);
1274 #endif
1277 /*****************************************************************************
1278 * DvdReadFindCell
1279 *****************************************************************************/
1280 static void DvdReadFindCell( demux_t *p_demux )
1282 demux_sys_t *p_sys = p_demux->p_sys;
1284 pgc_t *p_pgc;
1285 int pgc_id, pgn;
1286 int i = 0;
1288 #define cell p_sys->p_cur_pgc->cell_playback
1290 if( cell[p_sys->i_cur_cell].block_type == BLOCK_TYPE_ANGLE_BLOCK )
1292 p_sys->i_cur_cell += p_sys->i_angle - 1;
1294 while( cell[p_sys->i_cur_cell+i].block_mode != BLOCK_MODE_LAST_CELL )
1296 i++;
1298 p_sys->i_next_cell = p_sys->i_cur_cell + i + 1;
1300 else
1302 p_sys->i_next_cell = p_sys->i_cur_cell + 1;
1305 #undef cell
1307 if( p_sys->i_chapter + 1 >= p_sys->i_chapters ) return;
1309 pgc_id = p_sys->p_vts_file->vts_ptt_srpt->title[
1310 p_sys->i_ttn - 1].ptt[p_sys->i_chapter + 1].pgcn;
1311 pgn = p_sys->p_vts_file->vts_ptt_srpt->title[
1312 p_sys->i_ttn - 1].ptt[p_sys->i_chapter + 1].pgn;
1313 p_pgc = p_sys->p_vts_file->vts_pgcit->pgci_srp[pgc_id - 1].pgc;
1315 if( p_sys->i_cur_cell >= p_pgc->program_map[pgn - 1] - 1 )
1317 p_sys->i_chapter++;
1319 if( p_sys->i_chapter < p_sys->i_chapters &&
1320 p_sys->cur_chapter != p_sys->i_chapter )
1322 p_demux->info.i_update |= INPUT_UPDATE_SEEKPOINT;
1323 p_sys->cur_chapter = p_sys->i_chapter;
1328 /*****************************************************************************
1329 * DemuxTitles: get the titles/chapters structure
1330 *****************************************************************************/
1331 static void DemuxTitles( demux_t *p_demux, int *pi_angle )
1333 VLC_UNUSED( pi_angle );
1335 demux_sys_t *p_sys = p_demux->p_sys;
1336 input_title_t *t;
1337 seekpoint_t *s;
1339 /* Find out number of titles/chapters */
1340 #define tt_srpt p_sys->p_vmg_file->tt_srpt
1342 int32_t i_titles = tt_srpt->nr_of_srpts;
1343 msg_Dbg( p_demux, "number of titles: %d", i_titles );
1345 for( int i = 0; i < i_titles; i++ )
1347 int32_t i_chapters = 0;
1348 int j;
1350 i_chapters = tt_srpt->title[i].nr_of_ptts;
1351 msg_Dbg( p_demux, "title %d has %d chapters", i, i_chapters );
1353 t = vlc_input_title_New();
1355 for( j = 0; j < __MAX( i_chapters, 1 ); j++ )
1357 s = vlc_seekpoint_New();
1358 TAB_APPEND( t->i_seekpoint, t->seekpoint, s );
1361 TAB_APPEND( p_sys->i_titles, p_sys->titles, t );
1364 #undef tt_srpt