NEWS updates
[vlc/solaris.git] / modules / access / dvdread.c
blob4eef5d82ed913b2695b34d740cebcaf75c3fabdc
1 /*****************************************************************************
2 * dvdread.c : DvdRead input module for vlc
3 *****************************************************************************
4 * Copyright (C) 2001-2006 the VideoLAN team
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
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 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 General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 *****************************************************************************/
25 /*****************************************************************************
26 * Preamble
27 *****************************************************************************/
29 #ifdef HAVE_CONFIG_H
30 # include "config.h"
31 #endif
33 #include <vlc_common.h>
34 #include <vlc_plugin.h>
35 #include <vlc_input.h>
36 #include <vlc_access.h>
37 #include <vlc_charset.h>
38 #include <vlc_interface.h>
39 #include <vlc_dialog.h>
41 #include <vlc_iso_lang.h>
43 #include "../demux/ps.h"
45 #ifdef HAVE_UNISTD_H
46 # include <unistd.h>
47 #endif
49 #include <sys/types.h>
51 #ifdef HAVE_DVDREAD_DVD_READER_H
52 #include <dvdread/dvd_reader.h>
53 #include <dvdread/ifo_types.h>
54 #include <dvdread/ifo_read.h>
55 #include <dvdread/nav_read.h>
56 #include <dvdread/nav_print.h>
57 #else
58 #include <libdvdread/dvd_reader.h>
59 #include <libdvdread/ifo_types.h>
60 #include <libdvdread/ifo_read.h>
61 #include <libdvdread/nav_read.h>
62 #include <libdvdread/nav_print.h>
63 #endif
65 #include <assert.h>
67 /*****************************************************************************
68 * Module descriptor
69 *****************************************************************************/
70 #define ANGLE_TEXT N_("DVD angle")
71 #define ANGLE_LONGTEXT N_( \
72 "Default DVD angle." )
74 #define CACHING_TEXT N_("Caching value in ms")
75 #define CACHING_LONGTEXT N_( \
76 "Caching value for DVDs. " \
77 "This value should be set in milliseconds." )
79 static int Open ( vlc_object_t * );
80 static void Close( vlc_object_t * );
82 vlc_module_begin ()
83 set_shortname( N_("DVD without menus") )
84 set_description( N_("DVDRead Input (no menu support)") )
85 set_category( CAT_INPUT )
86 set_subcategory( SUBCAT_INPUT_ACCESS )
87 add_integer( "dvdread-angle", 1, NULL, ANGLE_TEXT,
88 ANGLE_LONGTEXT, false )
89 add_integer( "dvdread-caching", DEFAULT_PTS_DELAY / 1000, NULL,
90 CACHING_TEXT, CACHING_LONGTEXT, true )
91 add_obsolete_string( "dvdread-css-method" ) /* obsolete since 1.1.0 */
92 set_capability( "access_demux", 0 )
93 add_shortcut( "dvd" )
94 add_shortcut( "dvdread" )
95 add_shortcut( "dvdsimple" )
96 set_callbacks( Open, Close )
97 vlc_module_end ()
99 /* how many blocks DVDRead will read in each loop */
100 #define DVD_BLOCK_READ_ONCE 4
102 /*****************************************************************************
103 * Local prototypes
104 *****************************************************************************/
106 struct demux_sys_t
108 /* DVDRead state */
109 dvd_reader_t *p_dvdread;
110 dvd_file_t *p_title;
112 ifo_handle_t *p_vmg_file;
113 ifo_handle_t *p_vts_file;
115 int i_title;
116 int i_chapter, i_chapters;
117 int i_angle, i_angles;
119 tt_srpt_t *p_tt_srpt;
120 pgc_t *p_cur_pgc;
121 dsi_t dsi_pack;
122 int i_ttn;
124 int i_pack_len;
125 int i_cur_block;
126 int i_next_vobu;
128 int i_mux_rate;
130 /* Current title start/end blocks */
131 int i_title_start_block;
132 int i_title_end_block;
133 int i_title_blocks;
134 int i_title_offset;
135 mtime_t i_title_cur_time;
137 int i_title_start_cell;
138 int i_title_end_cell;
139 int i_cur_cell;
140 int i_next_cell;
141 mtime_t i_cell_cur_time;
142 mtime_t i_cell_duration;
144 /* Track */
145 ps_track_t tk[PS_TK_COUNT];
147 int i_titles;
148 input_title_t **titles;
150 /* Video */
151 int i_sar_num;
152 int i_sar_den;
154 /* SPU */
155 uint32_t clut[16];
158 static int Control ( demux_t *, int, va_list );
159 static int Demux ( demux_t * );
160 static int DemuxBlock( demux_t *, uint8_t *, int );
162 static void DemuxTitles( demux_t *, int * );
163 static void ESNew( demux_t *, int, int );
165 static int DvdReadSetArea ( demux_t *, int, int, int );
166 static void DvdReadSeek ( demux_t *, int );
167 static void DvdReadHandleDSI( demux_t *, uint8_t * );
168 static void DvdReadFindCell ( demux_t * );
170 /*****************************************************************************
171 * Open:
172 *****************************************************************************/
173 static int Open( vlc_object_t *p_this )
175 demux_t *p_demux = (demux_t*)p_this;
176 demux_sys_t *p_sys;
177 char *psz_name;
178 dvd_reader_t *p_dvdread;
179 ifo_handle_t *p_vmg_file;
181 if( !p_demux->psz_path || !*p_demux->psz_path )
183 /* Only when selected */
184 if( !p_demux->psz_access || !*p_demux->psz_access )
185 return VLC_EGENERIC;
187 psz_name = var_CreateGetString( p_this, "dvd" );
188 if( !psz_name )
190 psz_name = strdup("");
193 else
194 psz_name = ToLocaleDup( p_demux->psz_path );
196 #ifdef WIN32
197 if( psz_name[0] && psz_name[1] == ':' &&
198 psz_name[2] == '\\' && psz_name[3] == '\0' ) psz_name[2] = '\0';
199 #endif
201 /* Open dvdread */
202 if( !(p_dvdread = DVDOpen( psz_name )) )
204 msg_Err( p_demux, "DVDRead cannot open source: %s", psz_name );
205 dialog_Fatal( p_demux, _("Playback failure"),
206 _("DVDRead could not open the disc \"%s\"."), psz_name );
207 free( psz_name );
208 return VLC_EGENERIC;
210 free( psz_name );
212 /* Ifo allocation & initialisation */
213 if( !( p_vmg_file = ifoOpen( p_dvdread, 0 ) ) )
215 msg_Warn( p_demux, "cannot open VMG info" );
216 return VLC_EGENERIC;
218 msg_Dbg( p_demux, "VMG opened" );
220 /* Fill p_demux field */
221 DEMUX_INIT_COMMON(); p_sys = p_demux->p_sys;
223 ps_track_init( p_sys->tk );
224 p_sys->i_sar_num = 0;
225 p_sys->i_sar_den = 0;
226 p_sys->i_title_cur_time = (mtime_t) 0;
227 p_sys->i_cell_cur_time = (mtime_t) 0;
228 p_sys->i_cell_duration = (mtime_t) 0;
230 p_sys->p_dvdread = p_dvdread;
231 p_sys->p_vmg_file = p_vmg_file;
232 p_sys->p_title = NULL;
233 p_sys->p_vts_file = NULL;
235 p_sys->i_title = p_sys->i_chapter = -1;
236 p_sys->i_mux_rate = 0;
238 p_sys->i_angle = var_CreateGetInteger( p_demux, "dvdread-angle" );
239 if( p_sys->i_angle <= 0 ) p_sys->i_angle = 1;
241 DemuxTitles( p_demux, &p_sys->i_angle );
242 if( DvdReadSetArea( p_demux, 0, 0, p_sys->i_angle ) != VLC_SUCCESS )
244 Close( p_this );
245 msg_Err( p_demux, "DvdReadSetArea(0,0,%i) failed (can't decrypt DVD?)",
246 p_sys->i_angle );
247 return VLC_EGENERIC;
250 /* Update default_pts to a suitable value for dvdread access */
251 var_Create( p_demux, "dvdread-caching",
252 VLC_VAR_INTEGER|VLC_VAR_DOINHERIT );
254 return VLC_SUCCESS;
257 /*****************************************************************************
258 * Close:
259 *****************************************************************************/
260 static void Close( vlc_object_t *p_this )
262 demux_t *p_demux = (demux_t*)p_this;
263 demux_sys_t *p_sys = p_demux->p_sys;
264 int i;
266 for( i = 0; i < PS_TK_COUNT; i++ )
268 ps_track_t *tk = &p_sys->tk[i];
269 if( tk->b_seen )
271 es_format_Clean( &tk->fmt );
272 if( tk->es ) es_out_Del( p_demux->out, tk->es );
276 /* Close libdvdread */
277 if( p_sys->p_title ) DVDCloseFile( p_sys->p_title );
278 if( p_sys->p_vts_file ) ifoClose( p_sys->p_vts_file );
279 if( p_sys->p_vmg_file ) ifoClose( p_sys->p_vmg_file );
280 DVDClose( p_sys->p_dvdread );
282 free( p_sys );
285 static int64_t dvdtime_to_time( dvd_time_t *dtime, uint8_t still_time )
287 /* Macro to convert Binary Coded Decimal to Decimal */
288 #define BCD2D(__x__) (((__x__ & 0xf0) >> 4) * 10 + (__x__ & 0x0f))
290 double f_fps, f_ms;
291 int64_t i_micro_second = 0;
293 if (still_time == 0 || still_time == 0xFF)
295 i_micro_second += (int64_t)(BCD2D(dtime->hour)) * 60 * 60 * 1000000;
296 i_micro_second += (int64_t)(BCD2D(dtime->minute)) * 60 * 1000000;
297 i_micro_second += (int64_t)(BCD2D(dtime->second)) * 1000000;
299 switch((dtime->frame_u & 0xc0) >> 6)
301 case 1:
302 f_fps = 25.0;
303 break;
304 case 3:
305 f_fps = 29.97;
306 break;
307 default:
308 f_fps = 2500.0;
309 break;
311 f_ms = BCD2D(dtime->frame_u&0x3f) * 1000.0 / f_fps;
312 i_micro_second += (int64_t)(f_ms * 1000.0);
314 else
316 i_micro_second = still_time;
317 i_micro_second = (int64_t)((double)i_micro_second * 1000000.0);
320 return i_micro_second;
323 /*****************************************************************************
324 * Control:
325 *****************************************************************************/
326 static int Control( demux_t *p_demux, int i_query, va_list args )
328 demux_sys_t *p_sys = p_demux->p_sys;
329 double f, *pf;
330 bool *pb;
331 int64_t *pi64;
332 input_title_t ***ppp_title;
333 int *pi_int;
334 int i;
336 switch( i_query )
338 case DEMUX_GET_POSITION:
340 pf = (double*) va_arg( args, double* );
342 if( p_sys->i_title_blocks > 0 )
343 *pf = (double)p_sys->i_title_offset / p_sys->i_title_blocks;
344 else
345 *pf = 0.0;
347 return VLC_SUCCESS;
349 case DEMUX_SET_POSITION:
351 f = (double)va_arg( args, double );
353 DvdReadSeek( p_demux, f * p_sys->i_title_blocks );
355 return VLC_SUCCESS;
357 case DEMUX_GET_TIME:
358 pi64 = (int64_t*)va_arg( args, int64_t * );
359 if( p_demux->info.i_title >= 0 && p_demux->info.i_title < p_sys->i_titles )
361 *pi64 = (int64_t) dvdtime_to_time( &p_sys->p_cur_pgc->playback_time, 0 ) /
362 p_sys->i_title_blocks * p_sys->i_title_offset;
363 return VLC_SUCCESS;
365 *pi64 = 0;
366 return VLC_EGENERIC;
368 case DEMUX_GET_LENGTH:
369 pi64 = (int64_t*)va_arg( args, int64_t * );
370 if( p_demux->info.i_title >= 0 && p_demux->info.i_title < p_sys->i_titles )
372 *pi64 = (int64_t)dvdtime_to_time( &p_sys->p_cur_pgc->playback_time, 0 );
373 return VLC_SUCCESS;
375 *pi64 = 0;
376 return VLC_EGENERIC;
378 /* Special for access_demux */
379 case DEMUX_CAN_PAUSE:
380 case DEMUX_CAN_SEEK:
381 case DEMUX_CAN_CONTROL_PACE:
382 /* TODO */
383 pb = (bool*)va_arg( args, bool * );
384 *pb = true;
385 return VLC_SUCCESS;
387 case DEMUX_SET_PAUSE_STATE:
388 return VLC_SUCCESS;
390 case DEMUX_GET_TITLE_INFO:
391 ppp_title = (input_title_t***)va_arg( args, input_title_t*** );
392 pi_int = (int*)va_arg( args, int* );
393 *((int*)va_arg( args, int* )) = 1; /* Title offset */
394 *((int*)va_arg( args, int* )) = 1; /* Chapter offset */
396 /* Duplicate title infos */
397 *pi_int = p_sys->i_titles;
398 *ppp_title = malloc( sizeof(input_title_t **) * p_sys->i_titles );
399 for( i = 0; i < p_sys->i_titles; i++ )
401 (*ppp_title)[i] = vlc_input_title_Duplicate(p_sys->titles[i]);
403 return VLC_SUCCESS;
405 case DEMUX_SET_TITLE:
406 i = (int)va_arg( args, int );
407 if( DvdReadSetArea( p_demux, i, 0, -1 ) != VLC_SUCCESS )
409 msg_Warn( p_demux, "cannot set title/chapter" );
410 return VLC_EGENERIC;
412 p_demux->info.i_update |=
413 INPUT_UPDATE_TITLE | INPUT_UPDATE_SEEKPOINT;
414 p_demux->info.i_title = i;
415 p_demux->info.i_seekpoint = 0;
416 return VLC_SUCCESS;
418 case DEMUX_SET_SEEKPOINT:
419 i = (int)va_arg( args, int );
420 if( DvdReadSetArea( p_demux, -1, i, -1 ) != VLC_SUCCESS )
422 msg_Warn( p_demux, "cannot set title/chapter" );
423 return VLC_EGENERIC;
425 p_demux->info.i_update |= INPUT_UPDATE_SEEKPOINT;
426 p_demux->info.i_seekpoint = i;
427 return VLC_SUCCESS;
429 case DEMUX_GET_PTS_DELAY:
430 pi64 = (int64_t*)va_arg( args, int64_t * );
431 *pi64 = (int64_t)var_GetInteger( p_demux, "dvdread-caching" )*1000;
432 return VLC_SUCCESS;
434 /* TODO implement others */
435 default:
436 return VLC_EGENERIC;
440 /*****************************************************************************
441 * Demux:
442 *****************************************************************************/
443 static int Demux( demux_t *p_demux )
445 demux_sys_t *p_sys = p_demux->p_sys;
447 uint8_t p_buffer[DVD_VIDEO_LB_LEN * DVD_BLOCK_READ_ONCE];
448 int i_blocks_once, i_read;
449 int i;
452 * Playback by cell in this pgc, starting at the cell for our chapter.
456 * Check end of pack, and select the following one
458 if( !p_sys->i_pack_len )
460 /* Read NAV packet */
461 if( DVDReadBlocks( p_sys->p_title, p_sys->i_next_vobu,
462 1, p_buffer ) != 1 )
464 msg_Err( p_demux, "read failed for block %d", p_sys->i_next_vobu );
465 dialog_Fatal( p_demux, _("Playback failure"),
466 _("DVDRead could not read block %d."),
467 p_sys->i_next_vobu );
468 return -1;
471 /* Basic check to be sure we don't have a empty title
472 * go to next title if so */
473 //assert( p_buffer[41] == 0xbf && p_buffer[1027] == 0xbf );
475 /* Parse the contained dsi packet */
476 DvdReadHandleDSI( p_demux, p_buffer );
478 /* End of title */
479 if( p_sys->i_cur_cell >= p_sys->p_cur_pgc->nr_of_cells )
481 if( p_sys->i_title + 1 >= p_sys->i_titles )
483 return 0; /* EOF */
486 DvdReadSetArea( p_demux, p_sys->i_title + 1, 0, -1 );
489 if( p_sys->i_pack_len >= 1024 )
491 msg_Err( p_demux, "i_pack_len >= 1024 (%i). "
492 "This shouldn't happen!", p_sys->i_pack_len );
493 return 0; /* EOF */
496 /* FIXME: Ugly kludge: we send the pack block to the input for it
497 * sometimes has a zero scr and restart the sync */
498 p_sys->i_cur_block++;
499 p_sys->i_title_offset++;
501 DemuxBlock( p_demux, p_buffer, DVD_VIDEO_LB_LEN );
504 if( p_sys->i_cur_cell >= p_sys->p_cur_pgc->nr_of_cells )
506 if( p_sys->i_title + 1 >= p_sys->i_titles )
508 return 0; /* EOF */
511 DvdReadSetArea( p_demux, p_sys->i_title + 1, 0, -1 );
515 * Read actual data
517 i_blocks_once = __MIN( p_sys->i_pack_len, DVD_BLOCK_READ_ONCE );
518 p_sys->i_pack_len -= i_blocks_once;
520 /* Reads from DVD */
521 i_read = DVDReadBlocks( p_sys->p_title, p_sys->i_cur_block,
522 i_blocks_once, p_buffer );
523 if( i_read != i_blocks_once )
525 msg_Err( p_demux, "read failed for %d/%d blocks at 0x%02x",
526 i_read, i_blocks_once, p_sys->i_cur_block );
527 dialog_Fatal( p_demux, _("Playback failure"),
528 _("DVDRead could not read %d/%d blocks at 0x%02x."),
529 i_read, i_blocks_once, p_sys->i_cur_block );
530 return -1;
533 p_sys->i_cur_block += i_read;
534 p_sys->i_title_offset += i_read;
536 #if 0
537 msg_Dbg( p_demux, "i_blocks: %d len: %d current: 0x%02x",
538 i_read, p_sys->i_pack_len, p_sys->i_cur_block );
539 #endif
541 for( i = 0; i < i_read; i++ )
543 DemuxBlock( p_demux, p_buffer + i * DVD_VIDEO_LB_LEN,
544 DVD_VIDEO_LB_LEN );
547 #undef p_pgc
549 return 1;
552 /*****************************************************************************
553 * DemuxBlock: demux a given block
554 *****************************************************************************/
555 static int DemuxBlock( demux_t *p_demux, uint8_t *pkt, int i_pkt )
557 demux_sys_t *p_sys = p_demux->p_sys;
558 uint8_t *p = pkt;
560 while( p && p < &pkt[i_pkt] )
562 block_t *p_pkt;
563 int i_size = &pkt[i_pkt] - p;
565 if( i_size < 6 )
566 break;
568 i_size = ps_pkt_size( p, i_size );
569 if( i_size <= 0 )
571 break;
574 /* Create a block */
575 p_pkt = block_New( p_demux, i_size );
576 memcpy( p_pkt->p_buffer, p, i_size);
578 /* Parse it and send it */
579 switch( 0x100 | p[3] )
581 case 0x1b9:
582 case 0x1bb:
583 case 0x1bc:
585 #ifdef DVDREAD_DEBUG
586 if( p[3] == 0xbc )
588 msg_Warn( p_demux, "received a PSM packet" );
590 else if( p[3] == 0xbb )
592 msg_Warn( p_demux, "received a SYSTEM packet" );
594 #endif
595 block_Release( p_pkt );
596 break;
598 case 0x1ba:
600 int64_t i_scr;
601 int i_mux_rate;
602 if( !ps_pkt_parse_pack( p_pkt, &i_scr, &i_mux_rate ) )
604 es_out_Control( p_demux->out, ES_OUT_SET_PCR, i_scr );
605 if( i_mux_rate > 0 ) p_sys->i_mux_rate = i_mux_rate;
607 block_Release( p_pkt );
608 break;
610 default:
612 int i_id = ps_pkt_id( p_pkt );
613 if( i_id >= 0xc0 )
615 ps_track_t *tk = &p_sys->tk[PS_ID_TO_TK(i_id)];
617 if( !tk->b_seen )
619 ESNew( p_demux, i_id, 0 );
621 if( tk->b_seen && tk->es &&
622 !ps_pkt_parse_pes( p_pkt, tk->i_skip ) )
624 es_out_Send( p_demux->out, tk->es, p_pkt );
626 else
628 block_Release( p_pkt );
631 else
633 block_Release( p_pkt );
635 break;
639 p += i_size;
642 return VLC_SUCCESS;
645 /*****************************************************************************
646 * ESNew: register a new elementary stream
647 *****************************************************************************/
648 static void ESNew( demux_t *p_demux, int i_id, int i_lang )
650 demux_sys_t *p_sys = p_demux->p_sys;
651 ps_track_t *tk = &p_sys->tk[PS_ID_TO_TK(i_id)];
652 char psz_language[3];
654 if( tk->b_seen ) return;
656 if( ps_track_fill( tk, 0, i_id ) )
658 msg_Warn( p_demux, "unknown codec for id=0x%x", i_id );
659 return;
662 psz_language[0] = psz_language[1] = psz_language[2] = 0;
663 if( i_lang && i_lang != 0xffff )
665 psz_language[0] = (i_lang >> 8)&0xff;
666 psz_language[1] = (i_lang )&0xff;
669 /* Add a new ES */
670 if( tk->fmt.i_cat == VIDEO_ES )
672 tk->fmt.video.i_sar_num = p_sys->i_sar_num;
673 tk->fmt.video.i_sar_den = p_sys->i_sar_den;
675 else if( tk->fmt.i_cat == AUDIO_ES )
677 int i_audio = -1;
678 /* find the audio number PLEASE find another way */
679 if( (i_id&0xbdf8) == 0xbd88 ) /* dts */
681 i_audio = i_id&0x07;
683 else if( (i_id&0xbdf0) == 0xbd80 ) /* a52 */
685 i_audio = i_id&0xf;
687 else if( (i_id&0xbdf0) == 0xbda0 ) /* lpcm */
689 i_audio = i_id&0x1f;
691 else if( ( i_id&0xe0 ) == 0xc0 ) /* mpga */
693 i_audio = i_id&0x1f;
696 if( psz_language[0] ) tk->fmt.psz_language = strdup( psz_language );
698 else if( tk->fmt.i_cat == SPU_ES )
700 /* Palette */
701 tk->fmt.subs.spu.palette[0] = 0xBeef;
702 memcpy( &tk->fmt.subs.spu.palette[1], p_sys->clut,
703 16 * sizeof( uint32_t ) );
705 if( psz_language[0] ) tk->fmt.psz_language = strdup( psz_language );
708 tk->es = es_out_Add( p_demux->out, &tk->fmt );
709 tk->b_seen = true;
712 /*****************************************************************************
713 * DvdReadSetArea: initialize input data for title x, chapter y.
714 * It should be called for each user navigation request.
715 *****************************************************************************
716 * Take care that i_title and i_chapter start from 0.
717 *****************************************************************************/
718 static int DvdReadSetArea( demux_t *p_demux, int i_title, int i_chapter,
719 int i_angle )
721 VLC_UNUSED( i_angle );
723 demux_sys_t *p_sys = p_demux->p_sys;
724 int pgc_id = 0, pgn = 0;
725 int i;
727 #define p_pgc p_sys->p_cur_pgc
728 #define p_vmg p_sys->p_vmg_file
729 #define p_vts p_sys->p_vts_file
731 if( i_title >= 0 && i_title < p_sys->i_titles &&
732 i_title != p_sys->i_title )
734 int i_start_cell, i_end_cell;
736 if( p_sys->p_title != NULL ) DVDCloseFile( p_sys->p_title );
737 if( p_vts != NULL ) ifoClose( p_vts );
738 p_sys->i_title = i_title;
741 * We have to load all title information
743 msg_Dbg( p_demux, "open VTS %d, for title %d",
744 p_vmg->tt_srpt->title[i_title].title_set_nr, i_title + 1 );
746 /* Ifo vts */
747 if( !( p_vts = ifoOpen( p_sys->p_dvdread,
748 p_vmg->tt_srpt->title[i_title].title_set_nr ) ) )
750 msg_Err( p_demux, "fatal error in vts ifo" );
751 return VLC_EGENERIC;
754 /* Title position inside the selected vts */
755 p_sys->i_ttn = p_vmg->tt_srpt->title[i_title].vts_ttn;
757 /* Find title start/end */
758 pgc_id = p_vts->vts_ptt_srpt->title[p_sys->i_ttn - 1].ptt[0].pgcn;
759 pgn = p_vts->vts_ptt_srpt->title[p_sys->i_ttn - 1].ptt[0].pgn;
760 p_pgc = p_vts->vts_pgcit->pgci_srp[pgc_id - 1].pgc;
762 p_sys->i_title_start_cell =
763 i_start_cell = p_pgc->program_map[pgn - 1] - 1;
764 p_sys->i_title_start_block =
765 p_pgc->cell_playback[i_start_cell].first_sector;
767 p_sys->i_title_end_cell =
768 i_end_cell = p_pgc->nr_of_cells - 1;
769 p_sys->i_title_end_block =
770 p_pgc->cell_playback[i_end_cell].last_sector;
772 p_sys->i_title_offset = 0;
774 p_sys->i_title_blocks = 0;
775 for( i = i_start_cell; i <= i_end_cell; i++ )
777 p_sys->i_title_blocks += p_pgc->cell_playback[i].last_sector -
778 p_pgc->cell_playback[i].first_sector + 1;
781 msg_Dbg( p_demux, "title %d vts_title %d pgc %d pgn %d "
782 "start %d end %d blocks: %d",
783 i_title + 1, p_sys->i_ttn, pgc_id, pgn,
784 p_sys->i_title_start_block, p_sys->i_title_end_block,
785 p_sys->i_title_blocks );
788 * Set properties for current chapter
790 p_sys->i_chapter = 0;
791 p_sys->i_chapters =
792 p_vts->vts_ptt_srpt->title[p_sys->i_ttn - 1].nr_of_ptts;
794 pgc_id = p_vts->vts_ptt_srpt->title[
795 p_sys->i_ttn - 1].ptt[p_sys->i_chapter].pgcn;
796 pgn = p_vts->vts_ptt_srpt->title[
797 p_sys->i_ttn - 1].ptt[p_sys->i_chapter].pgn;
799 p_pgc = p_vts->vts_pgcit->pgci_srp[pgc_id - 1].pgc;
800 p_sys->i_pack_len = 0;
801 p_sys->i_next_cell =
802 p_sys->i_cur_cell = p_pgc->program_map[pgn - 1] - 1;
803 DvdReadFindCell( p_demux );
805 p_sys->i_next_vobu = p_sys->i_cur_block =
806 p_pgc->cell_playback[p_sys->i_cur_cell].first_sector;
809 * Angle management
811 p_sys->i_angles = p_vmg->tt_srpt->title[i_title].nr_of_angles;
812 if( p_sys->i_angle > p_sys->i_angles ) p_sys->i_angle = 1;
815 * We've got enough info, time to open the title set data.
817 if( !( p_sys->p_title = DVDOpenFile( p_sys->p_dvdread,
818 p_vmg->tt_srpt->title[i_title].title_set_nr,
819 DVD_READ_TITLE_VOBS ) ) )
821 msg_Err( p_demux, "cannot open title (VTS_%02d_1.VOB)",
822 p_vmg->tt_srpt->title[i_title].title_set_nr );
823 return VLC_EGENERIC;
826 //IfoPrintTitle( p_demux );
829 * Destroy obsolete ES by reinitializing program 0
830 * and find all ES in title with ifo data
832 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
834 for( i = 0; i < PS_TK_COUNT; i++ )
836 ps_track_t *tk = &p_sys->tk[i];
837 if( tk->b_seen )
839 es_format_Clean( &tk->fmt );
840 if( tk->es ) es_out_Del( p_demux->out, tk->es );
842 tk->b_seen = false;
845 if( p_demux->info.i_title != i_title )
847 p_demux->info.i_update |=
848 INPUT_UPDATE_TITLE | INPUT_UPDATE_SEEKPOINT;
849 p_demux->info.i_title = i_title;
850 p_demux->info.i_seekpoint = 0;
853 /* TODO: re-add angles */
856 ESNew( p_demux, 0xe0, 0 ); /* Video, FIXME ? */
857 const video_attr_t *p_attr = &p_vts->vtsi_mat->vts_video_attr;
858 int i_video_height = p_attr->video_format != 0 ? 576 : 480;
859 int i_video_width;
860 switch( p_attr->picture_size )
862 case 0:
863 i_video_width = 720;
864 break;
865 case 1:
866 i_video_width = 704;
867 break;
868 case 2:
869 i_video_width = 352;
870 break;
871 default:
872 case 3:
873 i_video_width = 352;
874 i_video_height /= 2;
875 break;
877 switch( p_attr->display_aspect_ratio )
879 case 0:
880 p_sys->i_sar_num = 4 * i_video_height;
881 p_sys->i_sar_den = 3 * i_video_width;
882 break;
883 case 3:
884 p_sys->i_sar_num = 16 * i_video_height;
885 p_sys->i_sar_den = 9 * i_video_width;
886 break;
887 default:
888 p_sys->i_sar_num = 0;
889 p_sys->i_sar_den = 0;
890 break;
893 #define audio_control \
894 p_sys->p_vts_file->vts_pgcit->pgci_srp[pgc_id-1].pgc->audio_control[i-1]
896 /* Audio ES, in the order they appear in the .ifo */
897 for( i = 1; i <= p_vts->vtsi_mat->nr_of_vts_audio_streams; i++ )
899 int i_position = 0;
900 uint16_t i_id;
902 //IfoPrintAudio( p_demux, i );
904 /* Audio channel is active if first byte is 0x80 */
905 if( audio_control & 0x8000 )
907 i_position = ( audio_control & 0x7F00 ) >> 8;
909 msg_Dbg( p_demux, "audio position %d", i_position );
910 switch( p_vts->vtsi_mat->vts_audio_attr[i - 1].audio_format )
912 case 0x00: /* A52 */
913 i_id = (0x80 + i_position) | 0xbd00;
914 break;
915 case 0x02:
916 case 0x03: /* MPEG audio */
917 i_id = 0xc000 + i_position;
918 break;
919 case 0x04: /* LPCM */
920 i_id = (0xa0 + i_position) | 0xbd00;
921 break;
922 case 0x06: /* DTS */
923 i_id = (0x88 + i_position) | 0xbd00;
924 break;
925 default:
926 i_id = 0;
927 msg_Err( p_demux, "unknown audio type %.2x",
928 p_vts->vtsi_mat->vts_audio_attr[i - 1].audio_format );
931 ESNew( p_demux, i_id, p_sys->p_vts_file->vtsi_mat->
932 vts_audio_attr[i - 1].lang_code );
935 #undef audio_control
937 #define spu_palette \
938 p_sys->p_vts_file->vts_pgcit->pgci_srp[pgc_id-1].pgc->palette
940 memcpy( p_sys->clut, spu_palette, 16 * sizeof( uint32_t ) );
942 #define spu_control \
943 p_sys->p_vts_file->vts_pgcit->pgci_srp[pgc_id-1].pgc->subp_control[i-1]
945 /* Sub Picture ES */
946 for( i = 1; i <= p_vts->vtsi_mat->nr_of_vts_subp_streams; i++ )
948 int i_position = 0;
949 uint16_t i_id;
951 //IfoPrintSpu( p_sys, i );
952 msg_Dbg( p_demux, "spu %d 0x%02x", i, spu_control );
954 if( spu_control & 0x80000000 )
956 /* there are several streams for one spu */
957 if( p_vts->vtsi_mat->vts_video_attr.display_aspect_ratio )
959 /* 16:9 */
960 switch( p_vts->vtsi_mat->vts_video_attr.permitted_df )
962 case 1: /* letterbox */
963 i_position = spu_control & 0xff;
964 break;
965 case 2: /* pan&scan */
966 i_position = ( spu_control >> 8 ) & 0xff;
967 break;
968 default: /* widescreen */
969 i_position = ( spu_control >> 16 ) & 0xff;
970 break;
973 else
975 /* 4:3 */
976 i_position = ( spu_control >> 24 ) & 0x7F;
979 i_id = (0x20 + i_position) | 0xbd00;
981 ESNew( p_demux, i_id, p_sys->p_vts_file->vtsi_mat->
982 vts_subp_attr[i - 1].lang_code );
985 #undef spu_control
988 else if( i_title != -1 && i_title != p_sys->i_title )
991 return VLC_EGENERIC; /* Couldn't set title */
995 * Chapter selection
998 if( i_chapter >= 0 && i_chapter < p_sys->i_chapters )
1000 pgc_id = p_vts->vts_ptt_srpt->title[
1001 p_sys->i_ttn - 1].ptt[i_chapter].pgcn;
1002 pgn = p_vts->vts_ptt_srpt->title[
1003 p_sys->i_ttn - 1].ptt[i_chapter].pgn;
1005 p_pgc = p_vts->vts_pgcit->pgci_srp[pgc_id - 1].pgc;
1007 p_sys->i_cur_cell = p_pgc->program_map[pgn - 1] - 1;
1008 p_sys->i_chapter = i_chapter;
1009 DvdReadFindCell( p_demux );
1011 p_sys->i_title_offset = 0;
1012 for( i = p_sys->i_title_start_cell; i < p_sys->i_cur_cell; i++ )
1014 p_sys->i_title_offset += p_pgc->cell_playback[i].last_sector -
1015 p_pgc->cell_playback[i].first_sector + 1;
1018 p_sys->i_pack_len = 0;
1019 p_sys->i_next_vobu = p_sys->i_cur_block =
1020 p_pgc->cell_playback[p_sys->i_cur_cell].first_sector;
1022 if( p_demux->info.i_seekpoint != i_chapter )
1024 p_demux->info.i_update |= INPUT_UPDATE_SEEKPOINT;
1025 p_demux->info.i_seekpoint = i_chapter;
1028 else if( i_chapter != -1 )
1031 return VLC_EGENERIC; /* Couldn't set chapter */
1034 #undef p_pgc
1035 #undef p_vts
1036 #undef p_vmg
1038 return VLC_SUCCESS;
1041 /*****************************************************************************
1042 * DvdReadSeek : Goes to a given position on the stream.
1043 *****************************************************************************
1044 * This one is used by the input and translate chronological position from
1045 * input to logical position on the device.
1046 *****************************************************************************/
1047 static void DvdReadSeek( demux_t *p_demux, int i_block_offset )
1049 demux_sys_t *p_sys = p_demux->p_sys;
1050 int i_chapter = 0;
1051 int i_cell = 0;
1052 int i_vobu = 0;
1053 int i_sub_cell = 0;
1054 int i_block;
1056 #define p_pgc p_sys->p_cur_pgc
1057 #define p_vts p_sys->p_vts_file
1059 /* Find cell */
1060 i_block = i_block_offset;
1061 for( i_cell = p_sys->i_title_start_cell;
1062 i_cell <= p_sys->i_title_end_cell; i_cell++ )
1064 if( i_block < (int)p_pgc->cell_playback[i_cell].last_sector -
1065 (int)p_pgc->cell_playback[i_cell].first_sector + 1 ) break;
1067 i_block -= (p_pgc->cell_playback[i_cell].last_sector -
1068 p_pgc->cell_playback[i_cell].first_sector + 1);
1070 if( i_cell > p_sys->i_title_end_cell )
1072 msg_Err( p_demux, "couldn't find cell for block %i", i_block_offset );
1073 return;
1075 i_block += p_pgc->cell_playback[i_cell].first_sector;
1076 p_sys->i_title_offset = i_block_offset;
1078 /* Find chapter */
1079 for( i_chapter = 0; i_chapter < p_sys->i_chapters; i_chapter++ )
1081 int pgc_id, pgn, i_tmp;
1083 pgc_id = p_vts->vts_ptt_srpt->title[
1084 p_sys->i_ttn - 1].ptt[i_chapter].pgcn;
1085 pgn = p_vts->vts_ptt_srpt->title[
1086 p_sys->i_ttn - 1].ptt[i_chapter].pgn;
1088 i_tmp = p_vts->vts_pgcit->pgci_srp[pgc_id - 1].pgc->program_map[pgn-1];
1090 if( i_tmp > i_cell ) break;
1093 if( i_chapter < p_sys->i_chapters &&
1094 p_demux->info.i_seekpoint != i_chapter )
1096 p_demux->info.i_update |= INPUT_UPDATE_SEEKPOINT;
1097 p_demux->info.i_seekpoint = i_chapter;
1100 /* Find vobu */
1101 while( (int)p_vts->vts_vobu_admap->vobu_start_sectors[i_vobu] <= i_block )
1103 i_vobu++;
1106 /* Find sub_cell */
1107 while( p_vts->vts_c_adt->cell_adr_table[i_sub_cell].start_sector <
1108 p_vts->vts_vobu_admap->vobu_start_sectors[i_vobu-1] )
1110 i_sub_cell++;
1113 #if 1
1114 msg_Dbg( p_demux, "cell %d i_sub_cell %d chapter %d vobu %d "
1115 "cell_sector %d vobu_sector %d sub_cell_sector %d",
1116 i_cell, i_sub_cell, i_chapter, i_vobu,
1117 p_sys->p_cur_pgc->cell_playback[i_cell].first_sector,
1118 p_vts->vts_vobu_admap->vobu_start_sectors[i_vobu],
1119 p_vts->vts_c_adt->cell_adr_table[i_sub_cell - 1].start_sector);
1120 #endif
1122 p_sys->i_cur_block = i_block;
1123 p_sys->i_next_vobu = p_vts->vts_vobu_admap->vobu_start_sectors[i_vobu];
1124 p_sys->i_pack_len = p_sys->i_next_vobu - i_block;
1125 p_sys->i_cur_cell = i_cell;
1126 p_sys->i_chapter = i_chapter;
1127 DvdReadFindCell( p_demux );
1129 #undef p_vts
1130 #undef p_pgc
1132 return;
1135 /*****************************************************************************
1136 * DvdReadHandleDSI
1137 *****************************************************************************/
1138 static void DvdReadHandleDSI( demux_t *p_demux, uint8_t *p_data )
1140 demux_sys_t *p_sys = p_demux->p_sys;
1142 navRead_DSI( &p_sys->dsi_pack, &p_data[DSI_START_BYTE] );
1145 * Determine where we go next. These values are the ones we mostly
1146 * care about.
1148 p_sys->i_cur_block = p_sys->dsi_pack.dsi_gi.nv_pck_lbn;
1149 p_sys->i_pack_len = p_sys->dsi_pack.dsi_gi.vobu_ea;
1152 * Store the timecodes so we can get the current time
1154 p_sys->i_title_cur_time = (mtime_t) (p_sys->dsi_pack.dsi_gi.nv_pck_scr / 90 * 1000);
1155 p_sys->i_cell_cur_time = (mtime_t) dvdtime_to_time( &p_sys->dsi_pack.dsi_gi.c_eltm, 0 );
1158 * If we're not at the end of this cell, we can determine the next
1159 * VOBU to display using the VOBU_SRI information section of the
1160 * DSI. Using this value correctly follows the current angle,
1161 * avoiding the doubled scenes in The Matrix, and makes our life
1162 * really happy.
1165 p_sys->i_next_vobu = p_sys->i_cur_block +
1166 ( p_sys->dsi_pack.vobu_sri.next_vobu & 0x7fffffff );
1168 if( p_sys->dsi_pack.vobu_sri.next_vobu != SRI_END_OF_CELL
1169 && p_sys->i_angle > 1 )
1171 switch( ( p_sys->dsi_pack.sml_pbi.category & 0xf000 ) >> 12 )
1173 case 0x4:
1174 /* Interleaved unit with no angle */
1175 if( p_sys->dsi_pack.sml_pbi.ilvu_sa != 0 )
1177 p_sys->i_next_vobu = p_sys->i_cur_block +
1178 p_sys->dsi_pack.sml_pbi.ilvu_sa;
1179 p_sys->i_pack_len = p_sys->dsi_pack.sml_pbi.ilvu_ea;
1181 else
1183 p_sys->i_next_vobu = p_sys->i_cur_block +
1184 p_sys->dsi_pack.dsi_gi.vobu_ea + 1;
1186 break;
1187 case 0x5:
1188 /* vobu is end of ilvu */
1189 if( p_sys->dsi_pack.sml_agli.data[p_sys->i_angle-1].address )
1191 p_sys->i_next_vobu = p_sys->i_cur_block +
1192 p_sys->dsi_pack.sml_agli.data[p_sys->i_angle-1].address;
1193 p_sys->i_pack_len = p_sys->dsi_pack.sml_pbi.ilvu_ea;
1195 break;
1197 case 0x6:
1198 /* vobu is beginning of ilvu */
1199 case 0x9:
1200 /* next scr is 0 */
1201 case 0xa:
1202 /* entering interleaved section */
1203 case 0x8:
1204 /* non interleaved cells in interleaved section */
1205 default:
1206 p_sys->i_next_vobu = p_sys->i_cur_block +
1207 ( p_sys->dsi_pack.vobu_sri.next_vobu & 0x7fffffff );
1208 break;
1211 else if( p_sys->dsi_pack.vobu_sri.next_vobu == SRI_END_OF_CELL )
1213 p_sys->i_cur_cell = p_sys->i_next_cell;
1215 /* End of title */
1216 if( p_sys->i_cur_cell >= p_sys->p_cur_pgc->nr_of_cells ) return;
1218 DvdReadFindCell( p_demux );
1220 p_sys->i_next_vobu =
1221 p_sys->p_cur_pgc->cell_playback[p_sys->i_cur_cell].first_sector;
1223 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 );
1227 #if 0
1228 msg_Dbg( p_demux, "scr %d lbn 0x%02x vobu_ea %d vob_id %d c_id %d c_time %lld",
1229 p_sys->dsi_pack.dsi_gi.nv_pck_scr,
1230 p_sys->dsi_pack.dsi_gi.nv_pck_lbn,
1231 p_sys->dsi_pack.dsi_gi.vobu_ea,
1232 p_sys->dsi_pack.dsi_gi.vobu_vob_idn,
1233 p_sys->dsi_pack.dsi_gi.vobu_c_idn,
1234 dvdtime_to_time( &p_sys->dsi_pack.dsi_gi.c_eltm, 0 ) );
1236 msg_Dbg( p_demux, "cell duration: %lld",
1237 (mtime_t)dvdtime_to_time( &p_sys->p_cur_pgc->cell_playback[p_sys->i_cur_cell].playback_time, 0 ) );
1239 msg_Dbg( p_demux, "cat 0x%02x ilvu_ea %d ilvu_sa %d size %d",
1240 p_sys->dsi_pack.sml_pbi.category,
1241 p_sys->dsi_pack.sml_pbi.ilvu_ea,
1242 p_sys->dsi_pack.sml_pbi.ilvu_sa,
1243 p_sys->dsi_pack.sml_pbi.size );
1245 msg_Dbg( p_demux, "next_vobu %d next_ilvu1 %d next_ilvu2 %d",
1246 p_sys->dsi_pack.vobu_sri.next_vobu & 0x7fffffff,
1247 p_sys->dsi_pack.sml_agli.data[ p_sys->i_angle - 1 ].address,
1248 p_sys->dsi_pack.sml_agli.data[ p_sys->i_angle ].address);
1249 #endif
1252 /*****************************************************************************
1253 * DvdReadFindCell
1254 *****************************************************************************/
1255 static void DvdReadFindCell( demux_t *p_demux )
1257 demux_sys_t *p_sys = p_demux->p_sys;
1259 pgc_t *p_pgc;
1260 int pgc_id, pgn;
1261 int i = 0;
1263 #define cell p_sys->p_cur_pgc->cell_playback
1265 if( cell[p_sys->i_cur_cell].block_type == BLOCK_TYPE_ANGLE_BLOCK )
1267 p_sys->i_cur_cell += p_sys->i_angle - 1;
1269 while( cell[p_sys->i_cur_cell+i].block_mode != BLOCK_MODE_LAST_CELL )
1271 i++;
1273 p_sys->i_next_cell = p_sys->i_cur_cell + i + 1;
1275 else
1277 p_sys->i_next_cell = p_sys->i_cur_cell + 1;
1280 #undef cell
1282 if( p_sys->i_chapter + 1 >= p_sys->i_chapters ) return;
1284 pgc_id = p_sys->p_vts_file->vts_ptt_srpt->title[
1285 p_sys->i_ttn - 1].ptt[p_sys->i_chapter + 1].pgcn;
1286 pgn = p_sys->p_vts_file->vts_ptt_srpt->title[
1287 p_sys->i_ttn - 1].ptt[p_sys->i_chapter + 1].pgn;
1288 p_pgc = p_sys->p_vts_file->vts_pgcit->pgci_srp[pgc_id - 1].pgc;
1290 if( p_sys->i_cur_cell >= p_pgc->program_map[pgn - 1] - 1 )
1292 p_sys->i_chapter++;
1294 if( p_sys->i_chapter < p_sys->i_chapters &&
1295 p_demux->info.i_seekpoint != p_sys->i_chapter )
1297 p_demux->info.i_update |= INPUT_UPDATE_SEEKPOINT;
1298 p_demux->info.i_seekpoint = p_sys->i_chapter;
1303 /*****************************************************************************
1304 * DemuxTitles: get the titles/chapters structure
1305 *****************************************************************************/
1306 static void DemuxTitles( demux_t *p_demux, int *pi_angle )
1308 VLC_UNUSED( pi_angle );
1310 demux_sys_t *p_sys = p_demux->p_sys;
1311 input_title_t *t;
1312 seekpoint_t *s;
1314 /* Find out number of titles/chapters */
1315 #define tt_srpt p_sys->p_vmg_file->tt_srpt
1317 int32_t i_titles = tt_srpt->nr_of_srpts;
1318 msg_Dbg( p_demux, "number of titles: %d", i_titles );
1320 for( int i = 0; i < i_titles; i++ )
1322 int32_t i_chapters = 0;
1323 int j;
1325 i_chapters = tt_srpt->title[i].nr_of_ptts;
1326 msg_Dbg( p_demux, "title %d has %d chapters", i, i_chapters );
1328 t = vlc_input_title_New();
1330 for( j = 0; j < __MAX( i_chapters, 1 ); j++ )
1332 s = vlc_seekpoint_New();
1333 TAB_APPEND( t->i_seekpoint, t->seekpoint, s );
1336 TAB_APPEND( p_sys->i_titles, p_sys->titles, t );
1339 #undef tt_srpt