demux: mkv: ProcessNavAction: negate if to prevent nesting
[vlc.git] / modules / access / dvdread.c
blobbeddd331e195fab2dba713806aa90bc39f0316d1
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", 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 typedef struct
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 unsigned updates;
105 int i_title;
106 int cur_title;
107 int i_chapter, i_chapters;
108 int cur_chapter;
109 int i_angle, i_angles;
111 tt_srpt_t *p_tt_srpt;
112 pgc_t *p_cur_pgc;
113 dsi_t dsi_pack;
114 int i_ttn;
116 int i_pack_len;
117 int i_cur_block;
118 int i_next_vobu;
120 int i_mux_rate;
122 /* Current title start/end blocks */
123 int i_title_start_block;
124 int i_title_end_block;
125 int i_title_blocks;
126 int i_title_offset;
127 vlc_tick_t i_title_cur_time;
129 int i_title_start_cell;
130 int i_title_end_cell;
131 int i_cur_cell;
132 int i_next_cell;
133 vlc_tick_t i_cell_cur_time;
134 vlc_tick_t i_cell_duration;
136 /* Track */
137 ps_track_t tk[PS_TK_COUNT];
139 int i_titles;
140 input_title_t **titles;
142 /* Video */
143 int i_sar_num;
144 int i_sar_den;
146 /* SPU */
147 uint32_t clut[16];
148 } demux_sys_t;
150 static int Control ( demux_t *, int, va_list );
151 static int Demux ( demux_t * );
152 static int DemuxBlock( demux_t *, const uint8_t *, int );
154 static void DemuxTitles( demux_t *, int * );
155 static void ESNew( demux_t *, int, int );
157 static int DvdReadSetArea ( demux_t *, int, int, int );
158 static void DvdReadSeek ( demux_t *, int );
159 static void DvdReadHandleDSI( demux_t *, uint8_t * );
160 static void DvdReadFindCell ( demux_t * );
162 /*****************************************************************************
163 * Open:
164 *****************************************************************************/
165 static int Open( vlc_object_t *p_this )
167 demux_t *p_demux = (demux_t*)p_this;
168 demux_sys_t *p_sys;
169 char *psz_file;
170 ifo_handle_t *p_vmg_file;
172 if (p_demux->out == NULL)
173 return VLC_EGENERIC;
175 if( !p_demux->psz_filepath || !*p_demux->psz_filepath )
176 psz_file = var_InheritString( p_this, "dvd" );
177 else
178 psz_file = strdup( p_demux->psz_filepath );
180 #if defined( _WIN32 ) || defined( __OS2__ )
181 if( psz_file != NULL )
183 size_t flen = strlen( psz_file );
184 if( flen > 0 && psz_file[flen - 1] == '\\' )
185 psz_file[flen - 1] = '\0';
187 else
188 psz_file = strdup("");
189 #endif
190 if( unlikely(psz_file == NULL) )
191 return VLC_EGENERIC;
193 /* Open dvdread */
194 const char *psz_path = ToLocale( psz_file );
195 dvd_reader_t *p_dvdread = DVDOpen( psz_path );
197 LocaleFree( psz_path );
198 if( p_dvdread == NULL )
200 msg_Err( p_demux, "DVDRead cannot open source: %s", psz_file );
201 vlc_dialog_display_error( p_demux, _("Playback failure"),
202 _("DVDRead could not open the disc \"%s\"."), psz_file );
203 free( psz_file );
204 return VLC_EGENERIC;
206 free( psz_file );
208 /* Ifo allocation & initialisation */
209 if( !( p_vmg_file = ifoOpen( p_dvdread, 0 ) ) )
211 char rgsz_volid[32];
212 if( DVDUDFVolumeInfo( p_dvdread, rgsz_volid, 32, NULL, 0 ) )
214 if( DVDISOVolumeInfo( p_dvdread, rgsz_volid, 32, NULL, 0 ) == 0 )
216 vlc_dialog_display_error( p_demux, _("Playback failure"),
217 _("Cannot play a non-UDF mastered DVD." ) );
218 msg_Err( p_demux, "Invalid UDF DVD. (Found ISO9660 '%s')", rgsz_volid );
221 msg_Warn( p_demux, "cannot open VMG info" );
222 return VLC_EGENERIC;
224 msg_Dbg( p_demux, "VMG opened" );
226 /* Fill p_demux field */
227 DEMUX_INIT_COMMON(); p_sys = p_demux->p_sys;
229 ps_track_init( p_sys->tk );
230 p_sys->i_sar_num = 0;
231 p_sys->i_sar_den = 0;
232 p_sys->i_title_cur_time = (vlc_tick_t) 0;
233 p_sys->i_cell_cur_time = (vlc_tick_t) 0;
234 p_sys->i_cell_duration = (vlc_tick_t) 0;
236 p_sys->p_dvdread = p_dvdread;
237 p_sys->p_vmg_file = p_vmg_file;
238 p_sys->p_title = NULL;
239 p_sys->p_vts_file = NULL;
241 p_sys->updates = 0;
242 p_sys->i_title = p_sys->i_chapter = -1;
243 p_sys->cur_title = p_sys->cur_chapter = 0;
244 p_sys->i_mux_rate = 0;
246 p_sys->i_angle = var_CreateGetInteger( p_demux, "dvdread-angle" );
247 if( p_sys->i_angle <= 0 ) p_sys->i_angle = 1;
249 DemuxTitles( p_demux, &p_sys->i_angle );
250 if( DvdReadSetArea( p_demux, 0, 0, p_sys->i_angle ) != VLC_SUCCESS )
252 msg_Err( p_demux, "DvdReadSetArea(0,0,%i) failed (can't decrypt DVD?)",
253 p_sys->i_angle );
254 Close( p_this );
255 return VLC_EGENERIC;
258 return VLC_SUCCESS;
261 /*****************************************************************************
262 * Close:
263 *****************************************************************************/
264 static void Close( vlc_object_t *p_this )
266 demux_t *p_demux = (demux_t*)p_this;
267 demux_sys_t *p_sys = p_demux->p_sys;
269 for( int i = 0; i < PS_TK_COUNT; i++ )
271 ps_track_t *tk = &p_sys->tk[i];
272 if( tk->b_configured )
274 es_format_Clean( &tk->fmt );
275 if( tk->es ) es_out_Del( p_demux->out, tk->es );
279 /* Free the array of titles */
280 for( int i = 0; i < p_sys->i_titles; i++ )
281 vlc_input_title_Delete( p_sys->titles[i] );
282 TAB_CLEAN( p_sys->i_titles, p_sys->titles );
284 /* Close libdvdread */
285 if( p_sys->p_title ) DVDCloseFile( p_sys->p_title );
286 if( p_sys->p_vts_file ) ifoClose( p_sys->p_vts_file );
287 if( p_sys->p_vmg_file ) ifoClose( p_sys->p_vmg_file );
288 DVDClose( p_sys->p_dvdread );
290 free( p_sys );
293 static int64_t dvdtime_to_time( dvd_time_t *dtime, uint8_t still_time )
295 /* Macro to convert Binary Coded Decimal to Decimal */
296 #define BCD2D(__x__) (((__x__ & 0xf0) >> 4) * 10 + (__x__ & 0x0f))
298 double f_fps, f_ms;
299 int64_t i_micro_second = 0;
301 if (still_time == 0 || still_time == 0xFF)
303 i_micro_second += (int64_t)(BCD2D(dtime->hour)) * 60 * 60 * 1000000;
304 i_micro_second += (int64_t)(BCD2D(dtime->minute)) * 60 * 1000000;
305 i_micro_second += (int64_t)(BCD2D(dtime->second)) * 1000000;
307 switch((dtime->frame_u & 0xc0) >> 6)
309 case 1:
310 f_fps = 25.0;
311 break;
312 case 3:
313 f_fps = 29.97;
314 break;
315 default:
316 f_fps = 2500.0;
317 break;
319 f_ms = BCD2D(dtime->frame_u&0x3f) * 1000.0 / f_fps;
320 i_micro_second += (int64_t)(f_ms * 1000.0);
322 else
324 i_micro_second = still_time;
325 i_micro_second = (int64_t)((double)i_micro_second * 1000000.0);
328 return i_micro_second;
331 /*****************************************************************************
332 * Control:
333 *****************************************************************************/
334 static int Control( demux_t *p_demux, int i_query, va_list args )
336 demux_sys_t *p_sys = p_demux->p_sys;
337 double f, *pf;
338 bool *pb;
339 int64_t *pi64;
340 input_title_t ***ppp_title;
341 int *pi_int;
342 int i;
344 switch( i_query )
346 case DEMUX_GET_POSITION:
348 pf = va_arg( args, double * );
350 if( p_sys->i_title_blocks > 0 )
351 *pf = (double)p_sys->i_title_offset / p_sys->i_title_blocks;
352 else
353 *pf = 0.0;
355 return VLC_SUCCESS;
357 case DEMUX_SET_POSITION:
359 f = va_arg( args, double );
361 DvdReadSeek( p_demux, f * p_sys->i_title_blocks );
363 return VLC_SUCCESS;
365 case DEMUX_GET_TIME:
366 pi64 = va_arg( args, int64_t * );
367 if( p_sys->cur_title >= 0 && p_sys->cur_title < p_sys->i_titles )
369 *pi64 = (int64_t) dvdtime_to_time( &p_sys->p_cur_pgc->playback_time, 0 ) /
370 p_sys->i_title_blocks * p_sys->i_title_offset;
371 return VLC_SUCCESS;
373 *pi64 = 0;
374 return VLC_EGENERIC;
376 case DEMUX_GET_LENGTH:
377 pi64 = va_arg( args, int64_t * );
378 if( p_sys->cur_title >= 0 && p_sys->cur_title < p_sys->i_titles )
380 *pi64 = (int64_t)dvdtime_to_time( &p_sys->p_cur_pgc->playback_time, 0 );
381 return VLC_SUCCESS;
383 *pi64 = 0;
384 return VLC_EGENERIC;
386 /* Special for access_demux */
387 case DEMUX_CAN_PAUSE:
388 case DEMUX_CAN_SEEK:
389 case DEMUX_CAN_CONTROL_PACE:
390 /* TODO */
391 pb = va_arg( args, bool * );
392 *pb = true;
393 return VLC_SUCCESS;
395 case DEMUX_SET_PAUSE_STATE:
396 return VLC_SUCCESS;
398 case DEMUX_GET_TITLE_INFO:
399 ppp_title = va_arg( args, input_title_t *** );
400 pi_int = va_arg( args, int * );
401 *va_arg( args, int * ) = 1; /* Title offset */
402 *va_arg( args, int * ) = 1; /* Chapter offset */
404 /* Duplicate title infos */
405 *pi_int = p_sys->i_titles;
406 *ppp_title = vlc_alloc( p_sys->i_titles, sizeof(input_title_t *) );
407 for( i = 0; i < p_sys->i_titles; i++ )
409 (*ppp_title)[i] = vlc_input_title_Duplicate(p_sys->titles[i]);
411 return VLC_SUCCESS;
413 case DEMUX_SET_TITLE:
414 i = va_arg( args, int );
415 if( DvdReadSetArea( p_demux, i, 0, -1 ) != VLC_SUCCESS )
417 msg_Warn( p_demux, "cannot set title/chapter" );
418 return VLC_EGENERIC;
420 p_sys->updates |= INPUT_UPDATE_TITLE | INPUT_UPDATE_SEEKPOINT;
421 p_sys->cur_title = i;
422 p_sys->cur_chapter = 0;
423 return VLC_SUCCESS;
425 case DEMUX_SET_SEEKPOINT:
426 i = va_arg( args, int );
427 if( DvdReadSetArea( p_demux, -1, i, -1 ) != VLC_SUCCESS )
429 msg_Warn( p_demux, "cannot set title/chapter" );
430 return VLC_EGENERIC;
432 p_sys->updates |= INPUT_UPDATE_SEEKPOINT;
433 p_sys->cur_chapter = i;
434 return VLC_SUCCESS;
436 case DEMUX_TEST_AND_CLEAR_FLAGS:
438 unsigned *restrict flags = va_arg(args, unsigned *);
439 *flags &= p_sys->updates;
440 p_sys->updates &= ~*flags;
441 return VLC_SUCCESS;
444 case DEMUX_GET_TITLE:
445 *va_arg( args, int * ) = p_sys->cur_title;
446 return VLC_SUCCESS;
448 case DEMUX_GET_SEEKPOINT:
449 *va_arg( args, int * ) = p_sys->cur_chapter;
450 return VLC_SUCCESS;
452 case DEMUX_GET_PTS_DELAY:
453 *va_arg( args, vlc_tick_t * ) =
454 VLC_TICK_FROM_MS(var_InheritInteger( p_demux, "disc-caching" ));
455 return VLC_SUCCESS;
457 /* TODO implement others */
458 default:
459 return VLC_EGENERIC;
463 /*****************************************************************************
464 * Demux:
465 *****************************************************************************/
466 static int Demux( demux_t *p_demux )
468 demux_sys_t *p_sys = p_demux->p_sys;
470 uint8_t p_buffer[DVD_VIDEO_LB_LEN * DVD_BLOCK_READ_ONCE];
471 int i_blocks_once, i_read;
474 * Playback by cell in this pgc, starting at the cell for our chapter.
478 * Check end of pack, and select the following one
480 if( !p_sys->i_pack_len )
482 /* Read NAV packet */
483 if( DVDReadBlocks( p_sys->p_title, p_sys->i_next_vobu,
484 1, p_buffer ) != 1 )
486 msg_Err( p_demux, "read failed for block %d", p_sys->i_next_vobu );
487 vlc_dialog_display_error( p_demux, _("Playback failure"),
488 _("DVDRead could not read block %d."),
489 p_sys->i_next_vobu );
490 return -1;
493 /* Basic check to be sure we don't have a empty title
494 * go to next title if so */
495 //assert( p_buffer[41] == 0xbf && p_buffer[1027] == 0xbf );
497 /* Parse the contained dsi packet */
498 DvdReadHandleDSI( p_demux, p_buffer );
500 /* End of title */
501 if( p_sys->i_cur_cell >= p_sys->p_cur_pgc->nr_of_cells )
503 int k = p_sys->i_title;
505 /* Looking for a not broken title */
506 while( k < p_sys->i_titles && DvdReadSetArea( p_demux, ++k, 0, -1 ) != VLC_SUCCESS )
508 msg_Err(p_demux, "Failed next title, trying another: %i", k );
509 if( k >= p_sys->i_titles )
510 return 0; // EOF
514 if( p_sys->i_pack_len >= 1024 )
516 msg_Err( p_demux, "i_pack_len >= 1024 (%i). "
517 "This shouldn't happen!", p_sys->i_pack_len );
518 return 0; /* EOF */
521 /* FIXME: Ugly kludge: we send the pack block to the input for it
522 * sometimes has a zero scr and restart the sync */
523 p_sys->i_cur_block++;
524 p_sys->i_title_offset++;
526 DemuxBlock( p_demux, p_buffer, DVD_VIDEO_LB_LEN );
529 if( p_sys->i_cur_cell >= p_sys->p_cur_pgc->nr_of_cells )
531 int k = p_sys->i_title;
533 /* Looking for a not broken title */
534 while( k < p_sys->i_titles && DvdReadSetArea( p_demux, ++k, 0, -1 ) != VLC_SUCCESS )
536 msg_Err(p_demux, "Failed next title, trying another: %i", k );
537 if( k >= p_sys->i_titles )
538 return 0; // EOF
543 * Read actual data
545 i_blocks_once = __MIN( p_sys->i_pack_len, DVD_BLOCK_READ_ONCE );
546 p_sys->i_pack_len -= i_blocks_once;
548 /* Reads from DVD */
549 i_read = DVDReadBlocks( p_sys->p_title, p_sys->i_cur_block,
550 i_blocks_once, p_buffer );
551 if( i_read != i_blocks_once )
553 msg_Err( p_demux, "read failed for %d/%d blocks at 0x%02x",
554 i_read, i_blocks_once, p_sys->i_cur_block );
555 vlc_dialog_display_error( p_demux, _("Playback failure"),
556 _("DVDRead could not read %d/%d blocks at 0x%02x."),
557 i_read, i_blocks_once, p_sys->i_cur_block );
558 return -1;
561 p_sys->i_cur_block += i_read;
562 p_sys->i_title_offset += i_read;
564 #if 0
565 msg_Dbg( p_demux, "i_blocks: %d len: %d current: 0x%02x",
566 i_read, p_sys->i_pack_len, p_sys->i_cur_block );
567 #endif
569 for( int i = 0; i < i_read; i++ )
571 DemuxBlock( p_demux, p_buffer + i * DVD_VIDEO_LB_LEN,
572 DVD_VIDEO_LB_LEN );
575 #undef p_pgc
577 return 1;
580 /*****************************************************************************
581 * DemuxBlock: demux a given block
582 *****************************************************************************/
583 static int DemuxBlock( demux_t *p_demux, const uint8_t *p, int len )
585 demux_sys_t *p_sys = p_demux->p_sys;
587 while( len > 0 )
589 int i_size = ps_pkt_size( p, len );
590 if( i_size <= 0 || i_size > len )
592 break;
595 /* Create a block */
596 block_t *p_pkt = block_Alloc( i_size );
597 memcpy( p_pkt->p_buffer, p, i_size);
599 /* Parse it and send it */
600 switch( 0x100 | p[3] )
602 case 0x1b9:
603 case 0x1bb:
604 case 0x1bc:
606 #ifdef DVDREAD_DEBUG
607 if( p[3] == 0xbc )
609 msg_Warn( p_demux, "received a PSM packet" );
611 else if( p[3] == 0xbb )
613 msg_Warn( p_demux, "received a SYSTEM packet" );
615 #endif
616 block_Release( p_pkt );
617 break;
619 case 0x1ba:
621 int64_t i_scr;
622 int i_mux_rate;
623 if( !ps_pkt_parse_pack( p_pkt, &i_scr, &i_mux_rate ) )
625 es_out_SetPCR( p_demux->out, i_scr );
626 if( i_mux_rate > 0 ) p_sys->i_mux_rate = i_mux_rate;
628 block_Release( p_pkt );
629 break;
631 default:
633 int i_id = ps_pkt_id( p_pkt );
634 if( i_id >= 0xc0 )
636 ps_track_t *tk = &p_sys->tk[ps_id_to_tk(i_id)];
638 if( !tk->b_configured )
640 ESNew( p_demux, i_id, 0 );
642 if( tk->es &&
643 !ps_pkt_parse_pes( VLC_OBJECT(p_demux), p_pkt, tk->i_skip ) )
645 es_out_Send( p_demux->out, tk->es, p_pkt );
647 else
649 block_Release( p_pkt );
652 else
654 block_Release( p_pkt );
656 break;
660 p += i_size;
661 len -= i_size;
664 return VLC_SUCCESS;
667 /*****************************************************************************
668 * ESNew: register a new elementary stream
669 *****************************************************************************/
670 static void ESNew( demux_t *p_demux, int i_id, int i_lang )
672 demux_sys_t *p_sys = p_demux->p_sys;
673 ps_track_t *tk = &p_sys->tk[ps_id_to_tk(i_id)];
674 char psz_language[3];
676 if( tk->b_configured ) return;
678 if( ps_track_fill( tk, 0, i_id, NULL, true ) )
680 msg_Warn( p_demux, "unknown codec for id=0x%x", i_id );
681 return;
684 psz_language[0] = psz_language[1] = psz_language[2] = 0;
685 if( i_lang && i_lang != 0xffff )
687 psz_language[0] = (i_lang >> 8)&0xff;
688 psz_language[1] = (i_lang )&0xff;
691 /* Add a new ES */
692 if( tk->fmt.i_cat == VIDEO_ES )
694 tk->fmt.video.i_sar_num = p_sys->i_sar_num;
695 tk->fmt.video.i_sar_den = p_sys->i_sar_den;
697 else if( tk->fmt.i_cat == AUDIO_ES )
699 #if 0
700 int i_audio = -1;
701 /* find the audio number PLEASE find another way */
702 if( (i_id&0xbdf8) == 0xbd88 ) /* dts */
704 i_audio = i_id&0x07;
706 else if( (i_id&0xbdf0) == 0xbd80 ) /* a52 */
708 i_audio = i_id&0xf;
710 else if( (i_id&0xbdf0) == 0xbda0 ) /* lpcm */
712 i_audio = i_id&0x1f;
714 else if( ( i_id&0xe0 ) == 0xc0 ) /* mpga */
716 i_audio = i_id&0x1f;
718 #endif
720 if( psz_language[0] ) tk->fmt.psz_language = strdup( psz_language );
722 else if( tk->fmt.i_cat == SPU_ES )
724 /* Palette */
725 tk->fmt.subs.spu.palette[0] = SPU_PALETTE_DEFINED;
726 memcpy( &tk->fmt.subs.spu.palette[1], p_sys->clut,
727 16 * sizeof( uint32_t ) );
729 if( psz_language[0] ) tk->fmt.psz_language = strdup( psz_language );
732 tk->es = es_out_Add( p_demux->out, &tk->fmt );
733 tk->b_configured = true;
736 /*****************************************************************************
737 * DvdReadSetArea: initialize input data for title x, chapter y.
738 * It should be called for each user navigation request.
739 *****************************************************************************
740 * Take care that i_title and i_chapter start from 0.
741 *****************************************************************************/
742 static int DvdReadSetArea( demux_t *p_demux, int i_title, int i_chapter,
743 int i_angle )
745 VLC_UNUSED( i_angle );
747 demux_sys_t *p_sys = p_demux->p_sys;
748 int pgc_id = 0, pgn = 0;
750 #define p_pgc p_sys->p_cur_pgc
751 #define p_vmg p_sys->p_vmg_file
752 #define p_vts p_sys->p_vts_file
754 if( i_title >= 0 && i_title < p_sys->i_titles &&
755 i_title != p_sys->i_title )
757 int i_start_cell, i_end_cell;
759 if( p_sys->p_title != NULL )
761 DVDCloseFile( p_sys->p_title );
762 p_sys->p_title = NULL;
764 if( p_vts != NULL ) ifoClose( p_vts );
765 p_sys->i_title = i_title;
768 * We have to load all title information
770 msg_Dbg( p_demux, "open VTS %d, for title %d",
771 p_vmg->tt_srpt->title[i_title].title_set_nr, i_title + 1 );
773 /* Ifo vts */
774 if( !( p_vts = ifoOpen( p_sys->p_dvdread,
775 p_vmg->tt_srpt->title[i_title].title_set_nr ) ) )
777 msg_Err( p_demux, "fatal error in vts ifo" );
778 return VLC_EGENERIC;
781 /* Title position inside the selected vts */
782 p_sys->i_ttn = p_vmg->tt_srpt->title[i_title].vts_ttn;
784 /* Find title start/end */
785 pgc_id = p_vts->vts_ptt_srpt->title[p_sys->i_ttn - 1].ptt[0].pgcn;
786 pgn = p_vts->vts_ptt_srpt->title[p_sys->i_ttn - 1].ptt[0].pgn;
787 p_pgc = p_vts->vts_pgcit->pgci_srp[pgc_id - 1].pgc;
789 if( p_pgc->cell_playback == NULL )
791 msg_Err( p_demux, "Invalid PGC (cell_playback_offset)" );
792 return VLC_EGENERIC;
795 p_sys->i_title_start_cell =
796 i_start_cell = p_pgc->program_map[pgn - 1] - 1;
797 p_sys->i_title_start_block =
798 p_pgc->cell_playback[i_start_cell].first_sector;
800 p_sys->i_title_end_cell =
801 i_end_cell = p_pgc->nr_of_cells - 1;
802 p_sys->i_title_end_block =
803 p_pgc->cell_playback[i_end_cell].last_sector;
805 p_sys->i_title_offset = 0;
807 p_sys->i_title_blocks = 0;
808 for( int i = i_start_cell; i <= i_end_cell; i++ )
810 p_sys->i_title_blocks += p_pgc->cell_playback[i].last_sector -
811 p_pgc->cell_playback[i].first_sector + 1;
814 msg_Dbg( p_demux, "title %d vts_title %d pgc %d pgn %d "
815 "start %d end %d blocks: %d",
816 i_title + 1, p_sys->i_ttn, pgc_id, pgn,
817 p_sys->i_title_start_block, p_sys->i_title_end_block,
818 p_sys->i_title_blocks );
821 * Set properties for current chapter
823 p_sys->i_chapter = 0;
824 p_sys->i_chapters =
825 p_vts->vts_ptt_srpt->title[p_sys->i_ttn - 1].nr_of_ptts;
827 pgc_id = p_vts->vts_ptt_srpt->title[
828 p_sys->i_ttn - 1].ptt[p_sys->i_chapter].pgcn;
829 pgn = p_vts->vts_ptt_srpt->title[
830 p_sys->i_ttn - 1].ptt[p_sys->i_chapter].pgn;
832 p_pgc = p_vts->vts_pgcit->pgci_srp[pgc_id - 1].pgc;
833 p_sys->i_pack_len = 0;
834 p_sys->i_next_cell =
835 p_sys->i_cur_cell = p_pgc->program_map[pgn - 1] - 1;
836 DvdReadFindCell( p_demux );
838 p_sys->i_next_vobu = p_sys->i_cur_block =
839 p_pgc->cell_playback[p_sys->i_cur_cell].first_sector;
842 * Angle management
844 p_sys->i_angles = p_vmg->tt_srpt->title[i_title].nr_of_angles;
845 if( p_sys->i_angle > p_sys->i_angles ) p_sys->i_angle = 1;
848 * We've got enough info, time to open the title set data.
850 if( !( p_sys->p_title = DVDOpenFile( p_sys->p_dvdread,
851 p_vmg->tt_srpt->title[i_title].title_set_nr,
852 DVD_READ_TITLE_VOBS ) ) )
854 msg_Err( p_demux, "cannot open title (VTS_%02d_1.VOB)",
855 p_vmg->tt_srpt->title[i_title].title_set_nr );
856 return VLC_EGENERIC;
859 //IfoPrintTitle( p_demux );
862 * Destroy obsolete ES by reinitializing program 0
863 * and find all ES in title with ifo data
865 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
867 for( int i = 0; i < PS_TK_COUNT; i++ )
869 ps_track_t *tk = &p_sys->tk[i];
870 if( tk->b_configured )
872 es_format_Clean( &tk->fmt );
873 if( tk->es ) es_out_Del( p_demux->out, tk->es );
875 tk->b_configured = false;
878 if( p_sys->cur_title != i_title )
880 p_sys->updates |= INPUT_UPDATE_TITLE | INPUT_UPDATE_SEEKPOINT;
881 p_sys->cur_title = i_title;
882 p_sys->cur_chapter = 0;
885 /* TODO: re-add angles */
888 ESNew( p_demux, 0xe0, 0 ); /* Video, FIXME ? */
889 const video_attr_t *p_attr = &p_vts->vtsi_mat->vts_video_attr;
890 int i_video_height = p_attr->video_format != 0 ? 576 : 480;
891 int i_video_width;
892 switch( p_attr->picture_size )
894 case 0:
895 i_video_width = 720;
896 break;
897 case 1:
898 i_video_width = 704;
899 break;
900 case 2:
901 i_video_width = 352;
902 break;
903 default:
904 case 3:
905 i_video_width = 352;
906 i_video_height /= 2;
907 break;
909 switch( p_attr->display_aspect_ratio )
911 case 0:
912 p_sys->i_sar_num = 4 * i_video_height;
913 p_sys->i_sar_den = 3 * i_video_width;
914 break;
915 case 3:
916 p_sys->i_sar_num = 16 * i_video_height;
917 p_sys->i_sar_den = 9 * i_video_width;
918 break;
919 default:
920 p_sys->i_sar_num = 0;
921 p_sys->i_sar_den = 0;
922 break;
925 #define audio_control \
926 p_sys->p_vts_file->vts_pgcit->pgci_srp[pgc_id-1].pgc->audio_control[i-1]
928 /* Audio ES, in the order they appear in the .ifo */
929 for( int i = 1; i <= p_vts->vtsi_mat->nr_of_vts_audio_streams; i++ )
931 int i_position = 0;
932 uint16_t i_id;
934 //IfoPrintAudio( p_demux, i );
936 /* Audio channel is active if first byte is 0x80 */
937 if( audio_control & 0x8000 )
939 i_position = ( audio_control & 0x7F00 ) >> 8;
941 msg_Dbg( p_demux, "audio position %d", i_position );
942 switch( p_vts->vtsi_mat->vts_audio_attr[i - 1].audio_format )
944 case 0x00: /* A52 */
945 i_id = (0x80 + i_position) | 0xbd00;
946 break;
947 case 0x02:
948 case 0x03: /* MPEG audio */
949 i_id = 0xc000 + i_position;
950 break;
951 case 0x04: /* LPCM */
952 i_id = (0xa0 + i_position) | 0xbd00;
953 break;
954 case 0x06: /* DTS */
955 i_id = (0x88 + i_position) | 0xbd00;
956 break;
957 default:
958 i_id = 0;
959 msg_Err( p_demux, "unknown audio type %.2x",
960 p_vts->vtsi_mat->vts_audio_attr[i - 1].audio_format );
963 ESNew( p_demux, i_id, p_sys->p_vts_file->vtsi_mat->
964 vts_audio_attr[i - 1].lang_code );
967 #undef audio_control
969 #define spu_palette \
970 p_sys->p_vts_file->vts_pgcit->pgci_srp[pgc_id-1].pgc->palette
972 memcpy( p_sys->clut, spu_palette, 16 * sizeof( uint32_t ) );
974 #define spu_control \
975 p_sys->p_vts_file->vts_pgcit->pgci_srp[pgc_id-1].pgc->subp_control[i-1]
977 /* Sub Picture ES */
978 for( int i = 1; i <= p_vts->vtsi_mat->nr_of_vts_subp_streams; i++ )
980 int i_position = 0;
981 uint16_t i_id;
983 //IfoPrintSpu( p_sys, i );
984 msg_Dbg( p_demux, "spu %d 0x%02x", i, spu_control );
986 if( spu_control & 0x80000000 )
988 /* there are several streams for one spu */
989 if( p_vts->vtsi_mat->vts_video_attr.display_aspect_ratio )
991 /* 16:9 */
992 switch( p_vts->vtsi_mat->vts_video_attr.permitted_df )
994 case 1: /* letterbox */
995 i_position = spu_control & 0xff;
996 break;
997 case 2: /* pan&scan */
998 i_position = ( spu_control >> 8 ) & 0xff;
999 break;
1000 default: /* widescreen */
1001 i_position = ( spu_control >> 16 ) & 0xff;
1002 break;
1005 else
1007 /* 4:3 */
1008 i_position = ( spu_control >> 24 ) & 0x7F;
1011 i_id = (0x20 + i_position) | 0xbd00;
1013 ESNew( p_demux, i_id, p_sys->p_vts_file->vtsi_mat->
1014 vts_subp_attr[i - 1].lang_code );
1017 #undef spu_control
1020 else if( i_title != -1 && i_title != p_sys->i_title )
1023 return VLC_EGENERIC; /* Couldn't set title */
1027 * Chapter selection
1030 if( i_chapter >= 0 && i_chapter < p_sys->i_chapters )
1032 pgc_id = p_vts->vts_ptt_srpt->title[
1033 p_sys->i_ttn - 1].ptt[i_chapter].pgcn;
1034 pgn = p_vts->vts_ptt_srpt->title[
1035 p_sys->i_ttn - 1].ptt[i_chapter].pgn;
1037 p_pgc = p_vts->vts_pgcit->pgci_srp[pgc_id - 1].pgc;
1038 if( p_pgc->cell_playback == NULL )
1039 return VLC_EGENERIC; /* Couldn't set chapter */
1041 p_sys->i_cur_cell = p_pgc->program_map[pgn - 1] - 1;
1042 p_sys->i_chapter = i_chapter;
1043 DvdReadFindCell( p_demux );
1045 p_sys->i_title_offset = 0;
1046 for( int i = p_sys->i_title_start_cell; i < p_sys->i_cur_cell; i++ )
1048 p_sys->i_title_offset += p_pgc->cell_playback[i].last_sector -
1049 p_pgc->cell_playback[i].first_sector + 1;
1052 p_sys->i_pack_len = 0;
1053 p_sys->i_next_vobu = p_sys->i_cur_block =
1054 p_pgc->cell_playback[p_sys->i_cur_cell].first_sector;
1056 if( p_sys->cur_chapter != i_chapter )
1058 p_sys->updates |= INPUT_UPDATE_SEEKPOINT;
1059 p_sys->cur_chapter = i_chapter;
1062 else if( i_chapter != -1 )
1065 return VLC_EGENERIC; /* Couldn't set chapter */
1068 #undef p_pgc
1069 #undef p_vts
1070 #undef p_vmg
1072 return VLC_SUCCESS;
1075 /*****************************************************************************
1076 * DvdReadSeek : Goes to a given position on the stream.
1077 *****************************************************************************
1078 * This one is used by the input and translate chronological position from
1079 * input to logical position on the device.
1080 *****************************************************************************/
1081 static void DvdReadSeek( demux_t *p_demux, int i_block_offset )
1083 demux_sys_t *p_sys = p_demux->p_sys;
1084 int i_chapter = 0;
1085 int i_cell = 0;
1086 int i_vobu = 0;
1087 int i_sub_cell = 0;
1088 int i_block;
1090 #define p_pgc p_sys->p_cur_pgc
1091 #define p_vts p_sys->p_vts_file
1093 /* Find cell */
1094 i_block = i_block_offset;
1095 for( i_cell = p_sys->i_title_start_cell;
1096 i_cell <= p_sys->i_title_end_cell; i_cell++ )
1098 if( i_block < (int)p_pgc->cell_playback[i_cell].last_sector -
1099 (int)p_pgc->cell_playback[i_cell].first_sector + 1 ) break;
1101 i_block -= (p_pgc->cell_playback[i_cell].last_sector -
1102 p_pgc->cell_playback[i_cell].first_sector + 1);
1104 if( i_cell > p_sys->i_title_end_cell )
1106 msg_Err( p_demux, "couldn't find cell for block %i", i_block_offset );
1107 return;
1109 i_block += p_pgc->cell_playback[i_cell].first_sector;
1110 p_sys->i_title_offset = i_block_offset;
1112 /* Find chapter */
1113 for( i_chapter = 0; i_chapter < p_sys->i_chapters; i_chapter++ )
1115 int pgc_id, pgn, i_tmp;
1117 pgc_id = p_vts->vts_ptt_srpt->title[
1118 p_sys->i_ttn - 1].ptt[i_chapter].pgcn;
1119 pgn = p_vts->vts_ptt_srpt->title[
1120 p_sys->i_ttn - 1].ptt[i_chapter].pgn;
1122 i_tmp = p_vts->vts_pgcit->pgci_srp[pgc_id - 1].pgc->program_map[pgn-1];
1124 if( i_tmp > i_cell ) break;
1127 if( i_chapter < p_sys->i_chapters &&
1128 p_sys->cur_chapter != i_chapter )
1130 p_sys->updates |= INPUT_UPDATE_SEEKPOINT;
1131 p_sys->cur_chapter = i_chapter;
1134 /* Find vobu */
1135 while( (int)p_vts->vts_vobu_admap->vobu_start_sectors[i_vobu] <= i_block )
1137 i_vobu++;
1140 /* Find sub_cell */
1141 while( p_vts->vts_c_adt->cell_adr_table[i_sub_cell].start_sector <
1142 p_vts->vts_vobu_admap->vobu_start_sectors[i_vobu-1] )
1144 i_sub_cell++;
1147 #if 1
1148 msg_Dbg( p_demux, "cell %d i_sub_cell %d chapter %d vobu %d "
1149 "cell_sector %d vobu_sector %d sub_cell_sector %d",
1150 i_cell, i_sub_cell, i_chapter, i_vobu,
1151 p_sys->p_cur_pgc->cell_playback[i_cell].first_sector,
1152 p_vts->vts_vobu_admap->vobu_start_sectors[i_vobu],
1153 p_vts->vts_c_adt->cell_adr_table[i_sub_cell - 1].start_sector);
1154 #endif
1156 p_sys->i_cur_block = i_block;
1157 p_sys->i_next_vobu = p_vts->vts_vobu_admap->vobu_start_sectors[i_vobu];
1158 p_sys->i_pack_len = p_sys->i_next_vobu - i_block;
1159 p_sys->i_cur_cell = i_cell;
1160 p_sys->i_chapter = i_chapter;
1161 DvdReadFindCell( p_demux );
1163 #undef p_vts
1164 #undef p_pgc
1166 return;
1169 /*****************************************************************************
1170 * DvdReadHandleDSI
1171 *****************************************************************************/
1172 static void DvdReadHandleDSI( demux_t *p_demux, uint8_t *p_data )
1174 demux_sys_t *p_sys = p_demux->p_sys;
1176 navRead_DSI( &p_sys->dsi_pack, &p_data[DSI_START_BYTE] );
1179 * Determine where we go next. These values are the ones we mostly
1180 * care about.
1182 p_sys->i_cur_block = p_sys->dsi_pack.dsi_gi.nv_pck_lbn;
1183 p_sys->i_pack_len = p_sys->dsi_pack.dsi_gi.vobu_ea;
1186 * Store the timecodes so we can get the current time
1188 p_sys->i_title_cur_time = (vlc_tick_t) p_sys->dsi_pack.dsi_gi.nv_pck_scr / 90 * 1000;
1189 p_sys->i_cell_cur_time = (vlc_tick_t) dvdtime_to_time( &p_sys->dsi_pack.dsi_gi.c_eltm, 0 );
1192 * If we're not at the end of this cell, we can determine the next
1193 * VOBU to display using the VOBU_SRI information section of the
1194 * DSI. Using this value correctly follows the current angle,
1195 * avoiding the doubled scenes in The Matrix, and makes our life
1196 * really happy.
1199 p_sys->i_next_vobu = p_sys->i_cur_block +
1200 ( p_sys->dsi_pack.vobu_sri.next_vobu & 0x7fffffff );
1202 if( p_sys->dsi_pack.vobu_sri.next_vobu != SRI_END_OF_CELL
1203 && p_sys->i_angle > 1 )
1205 switch( ( p_sys->dsi_pack.sml_pbi.category & 0xf000 ) >> 12 )
1207 case 0x4:
1208 /* Interleaved unit with no angle */
1209 if( p_sys->dsi_pack.sml_pbi.ilvu_sa != 0 )
1211 p_sys->i_next_vobu = p_sys->i_cur_block +
1212 p_sys->dsi_pack.sml_pbi.ilvu_sa;
1213 p_sys->i_pack_len = p_sys->dsi_pack.sml_pbi.ilvu_ea;
1215 else
1217 p_sys->i_next_vobu = p_sys->i_cur_block +
1218 p_sys->dsi_pack.dsi_gi.vobu_ea + 1;
1220 break;
1221 case 0x5:
1222 /* vobu is end of ilvu */
1223 if( p_sys->dsi_pack.sml_agli.data[p_sys->i_angle-1].address )
1225 p_sys->i_next_vobu = p_sys->i_cur_block +
1226 p_sys->dsi_pack.sml_agli.data[p_sys->i_angle-1].address;
1227 p_sys->i_pack_len = p_sys->dsi_pack.sml_pbi.ilvu_ea;
1229 break;
1231 /* fall through */
1232 case 0x6:
1233 /* vobu is beginning of ilvu */
1234 case 0x9:
1235 /* next scr is 0 */
1236 case 0xa:
1237 /* entering interleaved section */
1238 case 0x8:
1239 /* non interleaved cells in interleaved section */
1240 default:
1241 p_sys->i_next_vobu = p_sys->i_cur_block +
1242 ( p_sys->dsi_pack.vobu_sri.next_vobu & 0x7fffffff );
1243 break;
1246 else if( p_sys->dsi_pack.vobu_sri.next_vobu == SRI_END_OF_CELL )
1248 p_sys->i_cur_cell = p_sys->i_next_cell;
1250 /* End of title */
1251 if( p_sys->i_cur_cell >= p_sys->p_cur_pgc->nr_of_cells ) return;
1253 DvdReadFindCell( p_demux );
1255 p_sys->i_next_vobu =
1256 p_sys->p_cur_pgc->cell_playback[p_sys->i_cur_cell].first_sector;
1258 p_sys->i_cell_duration = (vlc_tick_t)dvdtime_to_time( &p_sys->p_cur_pgc->cell_playback[p_sys->i_cur_cell].playback_time, 0 );
1262 #if 0
1263 msg_Dbg( p_demux, "scr %d lbn 0x%02x vobu_ea %d vob_id %d c_id %d c_time %lld",
1264 p_sys->dsi_pack.dsi_gi.nv_pck_scr,
1265 p_sys->dsi_pack.dsi_gi.nv_pck_lbn,
1266 p_sys->dsi_pack.dsi_gi.vobu_ea,
1267 p_sys->dsi_pack.dsi_gi.vobu_vob_idn,
1268 p_sys->dsi_pack.dsi_gi.vobu_c_idn,
1269 dvdtime_to_time( &p_sys->dsi_pack.dsi_gi.c_eltm, 0 ) );
1271 msg_Dbg( p_demux, "cell duration: %lld",
1272 (vlc_tick_t)dvdtime_to_time( &p_sys->p_cur_pgc->cell_playback[p_sys->i_cur_cell].playback_time, 0 ) );
1274 msg_Dbg( p_demux, "cat 0x%02x ilvu_ea %d ilvu_sa %d size %d",
1275 p_sys->dsi_pack.sml_pbi.category,
1276 p_sys->dsi_pack.sml_pbi.ilvu_ea,
1277 p_sys->dsi_pack.sml_pbi.ilvu_sa,
1278 p_sys->dsi_pack.sml_pbi.size );
1280 msg_Dbg( p_demux, "next_vobu %d next_ilvu1 %d next_ilvu2 %d",
1281 p_sys->dsi_pack.vobu_sri.next_vobu & 0x7fffffff,
1282 p_sys->dsi_pack.sml_agli.data[ p_sys->i_angle - 1 ].address,
1283 p_sys->dsi_pack.sml_agli.data[ p_sys->i_angle ].address);
1284 #endif
1287 /*****************************************************************************
1288 * DvdReadFindCell
1289 *****************************************************************************/
1290 static void DvdReadFindCell( demux_t *p_demux )
1292 demux_sys_t *p_sys = p_demux->p_sys;
1294 pgc_t *p_pgc;
1295 int pgc_id, pgn;
1296 int i = 0;
1298 #define cell p_sys->p_cur_pgc->cell_playback
1300 if( cell[p_sys->i_cur_cell].block_type == BLOCK_TYPE_ANGLE_BLOCK )
1302 p_sys->i_cur_cell += p_sys->i_angle - 1;
1304 while( cell[p_sys->i_cur_cell+i].block_mode != BLOCK_MODE_LAST_CELL )
1306 i++;
1308 p_sys->i_next_cell = p_sys->i_cur_cell + i + 1;
1310 else
1312 p_sys->i_next_cell = p_sys->i_cur_cell + 1;
1315 #undef cell
1317 if( p_sys->i_chapter + 1 >= p_sys->i_chapters ) return;
1319 pgc_id = p_sys->p_vts_file->vts_ptt_srpt->title[
1320 p_sys->i_ttn - 1].ptt[p_sys->i_chapter + 1].pgcn;
1321 pgn = p_sys->p_vts_file->vts_ptt_srpt->title[
1322 p_sys->i_ttn - 1].ptt[p_sys->i_chapter + 1].pgn;
1323 p_pgc = p_sys->p_vts_file->vts_pgcit->pgci_srp[pgc_id - 1].pgc;
1325 if( p_sys->i_cur_cell >= p_pgc->program_map[pgn - 1] - 1 )
1327 p_sys->i_chapter++;
1329 if( p_sys->i_chapter < p_sys->i_chapters &&
1330 p_sys->cur_chapter != p_sys->i_chapter )
1332 p_sys->updates |= INPUT_UPDATE_SEEKPOINT;
1333 p_sys->cur_chapter = p_sys->i_chapter;
1338 /*****************************************************************************
1339 * DemuxTitles: get the titles/chapters structure
1340 *****************************************************************************/
1341 static void DemuxTitles( demux_t *p_demux, int *pi_angle )
1343 VLC_UNUSED( pi_angle );
1345 demux_sys_t *p_sys = p_demux->p_sys;
1346 input_title_t *t;
1347 seekpoint_t *s;
1349 /* Find out number of titles/chapters */
1350 #define tt_srpt p_sys->p_vmg_file->tt_srpt
1352 int32_t i_titles = tt_srpt->nr_of_srpts;
1353 msg_Dbg( p_demux, "number of titles: %d", i_titles );
1355 for( int i = 0; i < i_titles; i++ )
1357 int32_t i_chapters = 0;
1358 int j;
1360 i_chapters = tt_srpt->title[i].nr_of_ptts;
1361 msg_Dbg( p_demux, "title %d has %d chapters", i, i_chapters );
1363 t = vlc_input_title_New();
1365 for( j = 0; j < __MAX( i_chapters, 1 ); j++ )
1367 s = vlc_seekpoint_New();
1368 TAB_APPEND( t->i_seekpoint, t->seekpoint, s );
1371 TAB_APPEND( p_sys->i_titles, p_sys->titles, t );
1374 #undef tt_srpt