A little more detail regarding using my github copies of the code with where it's...
[vlc/adversarial.git] / modules / access / dvdread.c
blob9dd4c5bcce406c83ebbffde885171be83446624f
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/ps.h"
53 #include <sys/types.h>
54 #include <unistd.h>
56 #include <dvdread/dvd_reader.h>
57 #include <dvdread/ifo_types.h>
58 #include <dvdread/ifo_read.h>
59 #include <dvdread/nav_read.h>
60 #include <dvdread/nav_print.h>
62 #include <assert.h>
64 /*****************************************************************************
65 * Module descriptor
66 *****************************************************************************/
67 #define ANGLE_TEXT N_("DVD angle")
68 #define ANGLE_LONGTEXT N_( \
69 "Default DVD angle." )
71 static int Open ( vlc_object_t * );
72 static void Close( vlc_object_t * );
74 vlc_module_begin ()
75 set_shortname( N_("DVD without menus") )
76 set_description( N_("DVDRead Input (no menu support)") )
77 set_category( CAT_INPUT )
78 set_subcategory( SUBCAT_INPUT_ACCESS )
79 add_integer( "dvdread-angle", 1, ANGLE_TEXT,
80 ANGLE_LONGTEXT, false )
81 add_obsolete_string( "dvdread-css-method" ) /* obsolete since 1.1.0 */
82 set_capability( "access_demux", 0 )
83 add_shortcut( "dvd", "dvdread", "dvdsimple" )
84 set_callbacks( Open, Close )
85 vlc_module_end ()
87 /* how many blocks DVDRead will read in each loop */
88 #define DVD_BLOCK_READ_ONCE 4
90 /*****************************************************************************
91 * Local prototypes
92 *****************************************************************************/
94 struct demux_sys_t
96 /* DVDRead state */
97 dvd_reader_t *p_dvdread;
98 dvd_file_t *p_title;
100 ifo_handle_t *p_vmg_file;
101 ifo_handle_t *p_vts_file;
103 int i_title;
104 int i_chapter, i_chapters;
105 int i_angle, i_angles;
107 tt_srpt_t *p_tt_srpt;
108 pgc_t *p_cur_pgc;
109 dsi_t dsi_pack;
110 int i_ttn;
112 int i_pack_len;
113 int i_cur_block;
114 int i_next_vobu;
116 int i_mux_rate;
118 /* Current title start/end blocks */
119 int i_title_start_block;
120 int i_title_end_block;
121 int i_title_blocks;
122 int i_title_offset;
123 mtime_t i_title_cur_time;
125 int i_title_start_cell;
126 int i_title_end_cell;
127 int i_cur_cell;
128 int i_next_cell;
129 mtime_t i_cell_cur_time;
130 mtime_t i_cell_duration;
132 /* Track */
133 ps_track_t tk[PS_TK_COUNT];
135 int i_titles;
136 input_title_t **titles;
138 /* Video */
139 int i_sar_num;
140 int i_sar_den;
142 /* SPU */
143 uint32_t clut[16];
146 static int Control ( demux_t *, int, va_list );
147 static int Demux ( demux_t * );
148 static int DemuxBlock( demux_t *, const uint8_t *, int );
150 static void DemuxTitles( demux_t *, int * );
151 static void ESNew( demux_t *, int, int );
153 static int DvdReadSetArea ( demux_t *, int, int, int );
154 static void DvdReadSeek ( demux_t *, int );
155 static void DvdReadHandleDSI( demux_t *, uint8_t * );
156 static void DvdReadFindCell ( demux_t * );
158 /*****************************************************************************
159 * Open:
160 *****************************************************************************/
161 static int Open( vlc_object_t *p_this )
163 demux_t *p_demux = (demux_t*)p_this;
164 demux_sys_t *p_sys;
165 char *psz_file;
166 ifo_handle_t *p_vmg_file;
168 if( !p_demux->psz_file || !*p_demux->psz_file )
170 /* Only when selected */
171 if( !p_demux->psz_access || !*p_demux->psz_access )
172 return VLC_EGENERIC;
174 psz_file = var_InheritString( p_this, "dvd" );
176 else
177 psz_file = strdup( p_demux->psz_file );
179 #if defined( _WIN32 ) || defined( __OS2__ )
180 if( psz_file != NULL )
182 size_t flen = strlen( psz_file );
183 if( flen > 0 && psz_file[flen - 1] == '\\' )
184 psz_file[flen - 1] = '\0';
186 else
187 psz_file = strdup("");
188 #endif
189 if( unlikely(psz_file == NULL) )
190 return VLC_EGENERIC;
192 /* Open dvdread */
193 const char *psz_path = ToLocale( psz_file );
194 dvd_reader_t *p_dvdread = DVDOpen( psz_path );
196 LocaleFree( psz_path );
197 if( p_dvdread == NULL )
199 msg_Err( p_demux, "DVDRead cannot open source: %s", psz_file );
200 dialog_Fatal( p_demux, _("Playback failure"),
201 _("DVDRead could not open the disc \"%s\"."), psz_file );
202 free( psz_file );
203 return VLC_EGENERIC;
205 free( psz_file );
207 /* Ifo allocation & initialisation */
208 if( !( p_vmg_file = ifoOpen( p_dvdread, 0 ) ) )
210 msg_Warn( p_demux, "cannot open VMG info" );
211 return VLC_EGENERIC;
213 msg_Dbg( p_demux, "VMG opened" );
215 /* Fill p_demux field */
216 DEMUX_INIT_COMMON(); p_sys = p_demux->p_sys;
218 ps_track_init( p_sys->tk );
219 p_sys->i_sar_num = 0;
220 p_sys->i_sar_den = 0;
221 p_sys->i_title_cur_time = (mtime_t) 0;
222 p_sys->i_cell_cur_time = (mtime_t) 0;
223 p_sys->i_cell_duration = (mtime_t) 0;
225 p_sys->p_dvdread = p_dvdread;
226 p_sys->p_vmg_file = p_vmg_file;
227 p_sys->p_title = NULL;
228 p_sys->p_vts_file = NULL;
230 p_sys->i_title = p_sys->i_chapter = -1;
231 p_sys->i_mux_rate = 0;
233 p_sys->i_angle = var_CreateGetInteger( p_demux, "dvdread-angle" );
234 if( p_sys->i_angle <= 0 ) p_sys->i_angle = 1;
236 DemuxTitles( p_demux, &p_sys->i_angle );
237 if( DvdReadSetArea( p_demux, 0, 0, p_sys->i_angle ) != VLC_SUCCESS )
239 msg_Err( p_demux, "DvdReadSetArea(0,0,%i) failed (can't decrypt DVD?)",
240 p_sys->i_angle );
241 Close( p_this );
242 return VLC_EGENERIC;
245 return VLC_SUCCESS;
248 /*****************************************************************************
249 * Close:
250 *****************************************************************************/
251 static void Close( vlc_object_t *p_this )
253 demux_t *p_demux = (demux_t*)p_this;
254 demux_sys_t *p_sys = p_demux->p_sys;
255 int i;
257 for( i = 0; i < PS_TK_COUNT; i++ )
259 ps_track_t *tk = &p_sys->tk[i];
260 if( tk->b_seen )
262 es_format_Clean( &tk->fmt );
263 if( tk->es ) es_out_Del( p_demux->out, tk->es );
267 /* Free the array of titles */
268 for( int i = 0; i < p_sys->i_titles; i++ )
269 vlc_input_title_Delete( p_sys->titles[i] );
270 TAB_CLEAN( p_sys->i_titles, p_sys->titles );
272 /* Close libdvdread */
273 if( p_sys->p_title ) DVDCloseFile( p_sys->p_title );
274 if( p_sys->p_vts_file ) ifoClose( p_sys->p_vts_file );
275 if( p_sys->p_vmg_file ) ifoClose( p_sys->p_vmg_file );
276 DVDClose( p_sys->p_dvdread );
278 free( p_sys );
281 static int64_t dvdtime_to_time( dvd_time_t *dtime, uint8_t still_time )
283 /* Macro to convert Binary Coded Decimal to Decimal */
284 #define BCD2D(__x__) (((__x__ & 0xf0) >> 4) * 10 + (__x__ & 0x0f))
286 double f_fps, f_ms;
287 int64_t i_micro_second = 0;
289 if (still_time == 0 || still_time == 0xFF)
291 i_micro_second += (int64_t)(BCD2D(dtime->hour)) * 60 * 60 * 1000000;
292 i_micro_second += (int64_t)(BCD2D(dtime->minute)) * 60 * 1000000;
293 i_micro_second += (int64_t)(BCD2D(dtime->second)) * 1000000;
295 switch((dtime->frame_u & 0xc0) >> 6)
297 case 1:
298 f_fps = 25.0;
299 break;
300 case 3:
301 f_fps = 29.97;
302 break;
303 default:
304 f_fps = 2500.0;
305 break;
307 f_ms = BCD2D(dtime->frame_u&0x3f) * 1000.0 / f_fps;
308 i_micro_second += (int64_t)(f_ms * 1000.0);
310 else
312 i_micro_second = still_time;
313 i_micro_second = (int64_t)((double)i_micro_second * 1000000.0);
316 return i_micro_second;
319 /*****************************************************************************
320 * Control:
321 *****************************************************************************/
322 static int Control( demux_t *p_demux, int i_query, va_list args )
324 demux_sys_t *p_sys = p_demux->p_sys;
325 double f, *pf;
326 bool *pb;
327 int64_t *pi64;
328 input_title_t ***ppp_title;
329 int *pi_int;
330 int i;
332 switch( i_query )
334 case DEMUX_GET_POSITION:
336 pf = (double*) va_arg( args, double* );
338 if( p_sys->i_title_blocks > 0 )
339 *pf = (double)p_sys->i_title_offset / p_sys->i_title_blocks;
340 else
341 *pf = 0.0;
343 return VLC_SUCCESS;
345 case DEMUX_SET_POSITION:
347 f = (double)va_arg( args, double );
349 DvdReadSeek( p_demux, f * p_sys->i_title_blocks );
351 return VLC_SUCCESS;
353 case DEMUX_GET_TIME:
354 pi64 = (int64_t*)va_arg( args, int64_t * );
355 if( p_demux->info.i_title >= 0 && p_demux->info.i_title < p_sys->i_titles )
357 *pi64 = (int64_t) dvdtime_to_time( &p_sys->p_cur_pgc->playback_time, 0 ) /
358 p_sys->i_title_blocks * p_sys->i_title_offset;
359 return VLC_SUCCESS;
361 *pi64 = 0;
362 return VLC_EGENERIC;
364 case DEMUX_GET_LENGTH:
365 pi64 = (int64_t*)va_arg( args, int64_t * );
366 if( p_demux->info.i_title >= 0 && p_demux->info.i_title < p_sys->i_titles )
368 *pi64 = (int64_t)dvdtime_to_time( &p_sys->p_cur_pgc->playback_time, 0 );
369 return VLC_SUCCESS;
371 *pi64 = 0;
372 return VLC_EGENERIC;
374 /* Special for access_demux */
375 case DEMUX_CAN_PAUSE:
376 case DEMUX_CAN_SEEK:
377 case DEMUX_CAN_CONTROL_PACE:
378 /* TODO */
379 pb = (bool*)va_arg( args, bool * );
380 *pb = true;
381 return VLC_SUCCESS;
383 case DEMUX_SET_PAUSE_STATE:
384 return VLC_SUCCESS;
386 case DEMUX_GET_TITLE_INFO:
387 ppp_title = (input_title_t***)va_arg( args, input_title_t*** );
388 pi_int = (int*)va_arg( args, int* );
389 *((int*)va_arg( args, int* )) = 1; /* Title offset */
390 *((int*)va_arg( args, int* )) = 1; /* Chapter offset */
392 /* Duplicate title infos */
393 *pi_int = p_sys->i_titles;
394 *ppp_title = malloc( p_sys->i_titles * sizeof(input_title_t *) );
395 for( i = 0; i < p_sys->i_titles; i++ )
397 (*ppp_title)[i] = vlc_input_title_Duplicate(p_sys->titles[i]);
399 return VLC_SUCCESS;
401 case DEMUX_SET_TITLE:
402 i = (int)va_arg( args, int );
403 if( DvdReadSetArea( p_demux, i, 0, -1 ) != VLC_SUCCESS )
405 msg_Warn( p_demux, "cannot set title/chapter" );
406 return VLC_EGENERIC;
408 p_demux->info.i_update |=
409 INPUT_UPDATE_TITLE | INPUT_UPDATE_SEEKPOINT;
410 p_demux->info.i_title = i;
411 p_demux->info.i_seekpoint = 0;
412 return VLC_SUCCESS;
414 case DEMUX_SET_SEEKPOINT:
415 i = (int)va_arg( args, int );
416 if( DvdReadSetArea( p_demux, -1, i, -1 ) != VLC_SUCCESS )
418 msg_Warn( p_demux, "cannot set title/chapter" );
419 return VLC_EGENERIC;
421 p_demux->info.i_update |= INPUT_UPDATE_SEEKPOINT;
422 p_demux->info.i_seekpoint = i;
423 return VLC_SUCCESS;
425 case DEMUX_GET_PTS_DELAY:
426 pi64 = (int64_t*)va_arg( args, int64_t * );
427 *pi64 =
428 INT64_C(1000) * var_InheritInteger( p_demux, "disc-caching" );
429 return VLC_SUCCESS;
431 /* TODO implement others */
432 default:
433 return VLC_EGENERIC;
437 /*****************************************************************************
438 * Demux:
439 *****************************************************************************/
440 static int Demux( demux_t *p_demux )
442 demux_sys_t *p_sys = p_demux->p_sys;
444 uint8_t p_buffer[DVD_VIDEO_LB_LEN * DVD_BLOCK_READ_ONCE];
445 int i_blocks_once, i_read;
446 int i;
449 * Playback by cell in this pgc, starting at the cell for our chapter.
453 * Check end of pack, and select the following one
455 if( !p_sys->i_pack_len )
457 /* Read NAV packet */
458 if( DVDReadBlocks( p_sys->p_title, p_sys->i_next_vobu,
459 1, p_buffer ) != 1 )
461 msg_Err( p_demux, "read failed for block %d", p_sys->i_next_vobu );
462 dialog_Fatal( p_demux, _("Playback failure"),
463 _("DVDRead could not read block %d."),
464 p_sys->i_next_vobu );
465 return -1;
468 /* Basic check to be sure we don't have a empty title
469 * go to next title if so */
470 //assert( p_buffer[41] == 0xbf && p_buffer[1027] == 0xbf );
472 /* Parse the contained dsi packet */
473 DvdReadHandleDSI( p_demux, p_buffer );
475 /* End of title */
476 if( p_sys->i_cur_cell >= p_sys->p_cur_pgc->nr_of_cells )
478 int k = p_sys->i_title;
480 /* Looking for a not broken title */
481 while( k < p_sys->i_titles && DvdReadSetArea( p_demux, ++k, 0, -1 ) != VLC_SUCCESS )
483 msg_Err(p_demux, "Failed next title, trying another: %i", k );
484 if( k >= p_sys->i_titles )
485 return 0; // EOF
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 int k = p_sys->i_title;
508 /* Looking for a not broken title */
509 while( k < p_sys->i_titles && DvdReadSetArea( p_demux, ++k, 0, -1 ) != VLC_SUCCESS )
511 msg_Err(p_demux, "Failed next title, trying another: %i", k );
512 if( k >= p_sys->i_titles )
513 return 0; // EOF
518 * Read actual data
520 i_blocks_once = __MIN( p_sys->i_pack_len, DVD_BLOCK_READ_ONCE );
521 p_sys->i_pack_len -= i_blocks_once;
523 /* Reads from DVD */
524 i_read = DVDReadBlocks( p_sys->p_title, p_sys->i_cur_block,
525 i_blocks_once, p_buffer );
526 if( i_read != i_blocks_once )
528 msg_Err( p_demux, "read failed for %d/%d blocks at 0x%02x",
529 i_read, i_blocks_once, p_sys->i_cur_block );
530 dialog_Fatal( p_demux, _("Playback failure"),
531 _("DVDRead could not read %d/%d blocks at 0x%02x."),
532 i_read, i_blocks_once, p_sys->i_cur_block );
533 return -1;
536 p_sys->i_cur_block += i_read;
537 p_sys->i_title_offset += i_read;
539 #if 0
540 msg_Dbg( p_demux, "i_blocks: %d len: %d current: 0x%02x",
541 i_read, p_sys->i_pack_len, p_sys->i_cur_block );
542 #endif
544 for( i = 0; i < i_read; i++ )
546 DemuxBlock( p_demux, p_buffer + i * DVD_VIDEO_LB_LEN,
547 DVD_VIDEO_LB_LEN );
550 #undef p_pgc
552 return 1;
555 /*****************************************************************************
556 * DemuxBlock: demux a given block
557 *****************************************************************************/
558 static int DemuxBlock( demux_t *p_demux, const uint8_t *p, int len )
560 demux_sys_t *p_sys = p_demux->p_sys;
562 while( len > 0 )
564 int i_size = ps_pkt_size( p, len );
565 if( i_size <= 0 || i_size > len )
567 break;
570 /* Create a block */
571 block_t *p_pkt = block_Alloc( i_size );
572 memcpy( p_pkt->p_buffer, p, i_size);
574 /* Parse it and send it */
575 switch( 0x100 | p[3] )
577 case 0x1b9:
578 case 0x1bb:
579 case 0x1bc:
581 #ifdef DVDREAD_DEBUG
582 if( p[3] == 0xbc )
584 msg_Warn( p_demux, "received a PSM packet" );
586 else if( p[3] == 0xbb )
588 msg_Warn( p_demux, "received a SYSTEM packet" );
590 #endif
591 block_Release( p_pkt );
592 break;
594 case 0x1ba:
596 int64_t i_scr;
597 int i_mux_rate;
598 if( !ps_pkt_parse_pack( p_pkt, &i_scr, &i_mux_rate ) )
600 es_out_Control( p_demux->out, ES_OUT_SET_PCR, i_scr );
601 if( i_mux_rate > 0 ) p_sys->i_mux_rate = i_mux_rate;
603 block_Release( p_pkt );
604 break;
606 default:
608 int i_id = ps_pkt_id( p_pkt );
609 if( i_id >= 0xc0 )
611 ps_track_t *tk = &p_sys->tk[PS_ID_TO_TK(i_id)];
613 if( !tk->b_seen )
615 ESNew( p_demux, i_id, 0 );
617 if( tk->b_seen && tk->es &&
618 !ps_pkt_parse_pes( p_pkt, tk->i_skip ) )
620 es_out_Send( p_demux->out, tk->es, p_pkt );
622 else
624 block_Release( p_pkt );
627 else
629 block_Release( p_pkt );
631 break;
635 p += i_size;
636 len -= i_size;
639 return VLC_SUCCESS;
642 /*****************************************************************************
643 * ESNew: register a new elementary stream
644 *****************************************************************************/
645 static void ESNew( demux_t *p_demux, int i_id, int i_lang )
647 demux_sys_t *p_sys = p_demux->p_sys;
648 ps_track_t *tk = &p_sys->tk[PS_ID_TO_TK(i_id)];
649 char psz_language[3];
651 if( tk->b_seen ) return;
653 if( ps_track_fill( tk, 0, i_id, NULL ) )
655 msg_Warn( p_demux, "unknown codec for id=0x%x", i_id );
656 return;
659 psz_language[0] = psz_language[1] = psz_language[2] = 0;
660 if( i_lang && i_lang != 0xffff )
662 psz_language[0] = (i_lang >> 8)&0xff;
663 psz_language[1] = (i_lang )&0xff;
666 /* Add a new ES */
667 if( tk->fmt.i_cat == VIDEO_ES )
669 tk->fmt.video.i_sar_num = p_sys->i_sar_num;
670 tk->fmt.video.i_sar_den = p_sys->i_sar_den;
672 else if( tk->fmt.i_cat == AUDIO_ES )
674 #if 0
675 int i_audio = -1;
676 /* find the audio number PLEASE find another way */
677 if( (i_id&0xbdf8) == 0xbd88 ) /* dts */
679 i_audio = i_id&0x07;
681 else if( (i_id&0xbdf0) == 0xbd80 ) /* a52 */
683 i_audio = i_id&0xf;
685 else if( (i_id&0xbdf0) == 0xbda0 ) /* lpcm */
687 i_audio = i_id&0x1f;
689 else if( ( i_id&0xe0 ) == 0xc0 ) /* mpga */
691 i_audio = i_id&0x1f;
693 #endif
695 if( psz_language[0] ) tk->fmt.psz_language = strdup( psz_language );
697 else if( tk->fmt.i_cat == SPU_ES )
699 /* Palette */
700 tk->fmt.subs.spu.palette[0] = 0xBeef;
701 memcpy( &tk->fmt.subs.spu.palette[1], p_sys->clut,
702 16 * sizeof( uint32_t ) );
704 if( psz_language[0] ) tk->fmt.psz_language = strdup( psz_language );
707 tk->es = es_out_Add( p_demux->out, &tk->fmt );
708 tk->b_seen = true;
711 /*****************************************************************************
712 * DvdReadSetArea: initialize input data for title x, chapter y.
713 * It should be called for each user navigation request.
714 *****************************************************************************
715 * Take care that i_title and i_chapter start from 0.
716 *****************************************************************************/
717 static int DvdReadSetArea( demux_t *p_demux, int i_title, int i_chapter,
718 int i_angle )
720 VLC_UNUSED( i_angle );
722 demux_sys_t *p_sys = p_demux->p_sys;
723 int pgc_id = 0, pgn = 0;
724 int i;
726 #define p_pgc p_sys->p_cur_pgc
727 #define p_vmg p_sys->p_vmg_file
728 #define p_vts p_sys->p_vts_file
730 if( i_title >= 0 && i_title < p_sys->i_titles &&
731 i_title != p_sys->i_title )
733 int i_start_cell, i_end_cell;
735 if( p_sys->p_title != NULL )
737 DVDCloseFile( p_sys->p_title );
738 p_sys->p_title = NULL;
740 if( p_vts != NULL ) ifoClose( p_vts );
741 p_sys->i_title = i_title;
744 * We have to load all title information
746 msg_Dbg( p_demux, "open VTS %d, for title %d",
747 p_vmg->tt_srpt->title[i_title].title_set_nr, i_title + 1 );
749 /* Ifo vts */
750 if( !( p_vts = ifoOpen( p_sys->p_dvdread,
751 p_vmg->tt_srpt->title[i_title].title_set_nr ) ) )
753 msg_Err( p_demux, "fatal error in vts ifo" );
754 return VLC_EGENERIC;
757 /* Title position inside the selected vts */
758 p_sys->i_ttn = p_vmg->tt_srpt->title[i_title].vts_ttn;
760 /* Find title start/end */
761 pgc_id = p_vts->vts_ptt_srpt->title[p_sys->i_ttn - 1].ptt[0].pgcn;
762 pgn = p_vts->vts_ptt_srpt->title[p_sys->i_ttn - 1].ptt[0].pgn;
763 p_pgc = p_vts->vts_pgcit->pgci_srp[pgc_id - 1].pgc;
765 if( p_pgc->cell_playback == NULL )
767 msg_Err( p_demux, "Invalid PGC (cell_playback_offset)" );
768 return VLC_EGENERIC;
771 p_sys->i_title_start_cell =
772 i_start_cell = p_pgc->program_map[pgn - 1] - 1;
773 p_sys->i_title_start_block =
774 p_pgc->cell_playback[i_start_cell].first_sector;
776 p_sys->i_title_end_cell =
777 i_end_cell = p_pgc->nr_of_cells - 1;
778 p_sys->i_title_end_block =
779 p_pgc->cell_playback[i_end_cell].last_sector;
781 p_sys->i_title_offset = 0;
783 p_sys->i_title_blocks = 0;
784 for( i = i_start_cell; i <= i_end_cell; i++ )
786 p_sys->i_title_blocks += p_pgc->cell_playback[i].last_sector -
787 p_pgc->cell_playback[i].first_sector + 1;
790 msg_Dbg( p_demux, "title %d vts_title %d pgc %d pgn %d "
791 "start %d end %d blocks: %d",
792 i_title + 1, p_sys->i_ttn, pgc_id, pgn,
793 p_sys->i_title_start_block, p_sys->i_title_end_block,
794 p_sys->i_title_blocks );
797 * Set properties for current chapter
799 p_sys->i_chapter = 0;
800 p_sys->i_chapters =
801 p_vts->vts_ptt_srpt->title[p_sys->i_ttn - 1].nr_of_ptts;
803 pgc_id = p_vts->vts_ptt_srpt->title[
804 p_sys->i_ttn - 1].ptt[p_sys->i_chapter].pgcn;
805 pgn = p_vts->vts_ptt_srpt->title[
806 p_sys->i_ttn - 1].ptt[p_sys->i_chapter].pgn;
808 p_pgc = p_vts->vts_pgcit->pgci_srp[pgc_id - 1].pgc;
809 p_sys->i_pack_len = 0;
810 p_sys->i_next_cell =
811 p_sys->i_cur_cell = p_pgc->program_map[pgn - 1] - 1;
812 DvdReadFindCell( p_demux );
814 p_sys->i_next_vobu = p_sys->i_cur_block =
815 p_pgc->cell_playback[p_sys->i_cur_cell].first_sector;
818 * Angle management
820 p_sys->i_angles = p_vmg->tt_srpt->title[i_title].nr_of_angles;
821 if( p_sys->i_angle > p_sys->i_angles ) p_sys->i_angle = 1;
824 * We've got enough info, time to open the title set data.
826 if( !( p_sys->p_title = DVDOpenFile( p_sys->p_dvdread,
827 p_vmg->tt_srpt->title[i_title].title_set_nr,
828 DVD_READ_TITLE_VOBS ) ) )
830 msg_Err( p_demux, "cannot open title (VTS_%02d_1.VOB)",
831 p_vmg->tt_srpt->title[i_title].title_set_nr );
832 return VLC_EGENERIC;
835 //IfoPrintTitle( p_demux );
838 * Destroy obsolete ES by reinitializing program 0
839 * and find all ES in title with ifo data
841 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
843 for( i = 0; i < PS_TK_COUNT; i++ )
845 ps_track_t *tk = &p_sys->tk[i];
846 if( tk->b_seen )
848 es_format_Clean( &tk->fmt );
849 if( tk->es ) es_out_Del( p_demux->out, tk->es );
851 tk->b_seen = false;
854 if( p_demux->info.i_title != i_title )
856 p_demux->info.i_update |=
857 INPUT_UPDATE_TITLE | INPUT_UPDATE_SEEKPOINT;
858 p_demux->info.i_title = i_title;
859 p_demux->info.i_seekpoint = 0;
862 /* TODO: re-add angles */
865 ESNew( p_demux, 0xe0, 0 ); /* Video, FIXME ? */
866 const video_attr_t *p_attr = &p_vts->vtsi_mat->vts_video_attr;
867 int i_video_height = p_attr->video_format != 0 ? 576 : 480;
868 int i_video_width;
869 switch( p_attr->picture_size )
871 case 0:
872 i_video_width = 720;
873 break;
874 case 1:
875 i_video_width = 704;
876 break;
877 case 2:
878 i_video_width = 352;
879 break;
880 default:
881 case 3:
882 i_video_width = 352;
883 i_video_height /= 2;
884 break;
886 switch( p_attr->display_aspect_ratio )
888 case 0:
889 p_sys->i_sar_num = 4 * i_video_height;
890 p_sys->i_sar_den = 3 * i_video_width;
891 break;
892 case 3:
893 p_sys->i_sar_num = 16 * i_video_height;
894 p_sys->i_sar_den = 9 * i_video_width;
895 break;
896 default:
897 p_sys->i_sar_num = 0;
898 p_sys->i_sar_den = 0;
899 break;
902 #define audio_control \
903 p_sys->p_vts_file->vts_pgcit->pgci_srp[pgc_id-1].pgc->audio_control[i-1]
905 /* Audio ES, in the order they appear in the .ifo */
906 for( i = 1; i <= p_vts->vtsi_mat->nr_of_vts_audio_streams; i++ )
908 int i_position = 0;
909 uint16_t i_id;
911 //IfoPrintAudio( p_demux, i );
913 /* Audio channel is active if first byte is 0x80 */
914 if( audio_control & 0x8000 )
916 i_position = ( audio_control & 0x7F00 ) >> 8;
918 msg_Dbg( p_demux, "audio position %d", i_position );
919 switch( p_vts->vtsi_mat->vts_audio_attr[i - 1].audio_format )
921 case 0x00: /* A52 */
922 i_id = (0x80 + i_position) | 0xbd00;
923 break;
924 case 0x02:
925 case 0x03: /* MPEG audio */
926 i_id = 0xc000 + i_position;
927 break;
928 case 0x04: /* LPCM */
929 i_id = (0xa0 + i_position) | 0xbd00;
930 break;
931 case 0x06: /* DTS */
932 i_id = (0x88 + i_position) | 0xbd00;
933 break;
934 default:
935 i_id = 0;
936 msg_Err( p_demux, "unknown audio type %.2x",
937 p_vts->vtsi_mat->vts_audio_attr[i - 1].audio_format );
940 ESNew( p_demux, i_id, p_sys->p_vts_file->vtsi_mat->
941 vts_audio_attr[i - 1].lang_code );
944 #undef audio_control
946 #define spu_palette \
947 p_sys->p_vts_file->vts_pgcit->pgci_srp[pgc_id-1].pgc->palette
949 memcpy( p_sys->clut, spu_palette, 16 * sizeof( uint32_t ) );
951 #define spu_control \
952 p_sys->p_vts_file->vts_pgcit->pgci_srp[pgc_id-1].pgc->subp_control[i-1]
954 /* Sub Picture ES */
955 for( i = 1; i <= p_vts->vtsi_mat->nr_of_vts_subp_streams; i++ )
957 int i_position = 0;
958 uint16_t i_id;
960 //IfoPrintSpu( p_sys, i );
961 msg_Dbg( p_demux, "spu %d 0x%02x", i, spu_control );
963 if( spu_control & 0x80000000 )
965 /* there are several streams for one spu */
966 if( p_vts->vtsi_mat->vts_video_attr.display_aspect_ratio )
968 /* 16:9 */
969 switch( p_vts->vtsi_mat->vts_video_attr.permitted_df )
971 case 1: /* letterbox */
972 i_position = spu_control & 0xff;
973 break;
974 case 2: /* pan&scan */
975 i_position = ( spu_control >> 8 ) & 0xff;
976 break;
977 default: /* widescreen */
978 i_position = ( spu_control >> 16 ) & 0xff;
979 break;
982 else
984 /* 4:3 */
985 i_position = ( spu_control >> 24 ) & 0x7F;
988 i_id = (0x20 + i_position) | 0xbd00;
990 ESNew( p_demux, i_id, p_sys->p_vts_file->vtsi_mat->
991 vts_subp_attr[i - 1].lang_code );
994 #undef spu_control
997 else if( i_title != -1 && i_title != p_sys->i_title )
1000 return VLC_EGENERIC; /* Couldn't set title */
1004 * Chapter selection
1007 if( i_chapter >= 0 && i_chapter < p_sys->i_chapters )
1009 pgc_id = p_vts->vts_ptt_srpt->title[
1010 p_sys->i_ttn - 1].ptt[i_chapter].pgcn;
1011 pgn = p_vts->vts_ptt_srpt->title[
1012 p_sys->i_ttn - 1].ptt[i_chapter].pgn;
1014 p_pgc = p_vts->vts_pgcit->pgci_srp[pgc_id - 1].pgc;
1015 if( p_pgc->cell_playback == NULL )
1016 return VLC_EGENERIC; /* Couldn't set chapter */
1018 p_sys->i_cur_cell = p_pgc->program_map[pgn - 1] - 1;
1019 p_sys->i_chapter = i_chapter;
1020 DvdReadFindCell( p_demux );
1022 p_sys->i_title_offset = 0;
1023 for( i = p_sys->i_title_start_cell; i < p_sys->i_cur_cell; i++ )
1025 p_sys->i_title_offset += p_pgc->cell_playback[i].last_sector -
1026 p_pgc->cell_playback[i].first_sector + 1;
1029 p_sys->i_pack_len = 0;
1030 p_sys->i_next_vobu = p_sys->i_cur_block =
1031 p_pgc->cell_playback[p_sys->i_cur_cell].first_sector;
1033 if( p_demux->info.i_seekpoint != i_chapter )
1035 p_demux->info.i_update |= INPUT_UPDATE_SEEKPOINT;
1036 p_demux->info.i_seekpoint = i_chapter;
1039 else if( i_chapter != -1 )
1042 return VLC_EGENERIC; /* Couldn't set chapter */
1045 #undef p_pgc
1046 #undef p_vts
1047 #undef p_vmg
1049 return VLC_SUCCESS;
1052 /*****************************************************************************
1053 * DvdReadSeek : Goes to a given position on the stream.
1054 *****************************************************************************
1055 * This one is used by the input and translate chronological position from
1056 * input to logical position on the device.
1057 *****************************************************************************/
1058 static void DvdReadSeek( demux_t *p_demux, int i_block_offset )
1060 demux_sys_t *p_sys = p_demux->p_sys;
1061 int i_chapter = 0;
1062 int i_cell = 0;
1063 int i_vobu = 0;
1064 int i_sub_cell = 0;
1065 int i_block;
1067 #define p_pgc p_sys->p_cur_pgc
1068 #define p_vts p_sys->p_vts_file
1070 /* Find cell */
1071 i_block = i_block_offset;
1072 for( i_cell = p_sys->i_title_start_cell;
1073 i_cell <= p_sys->i_title_end_cell; i_cell++ )
1075 if( i_block < (int)p_pgc->cell_playback[i_cell].last_sector -
1076 (int)p_pgc->cell_playback[i_cell].first_sector + 1 ) break;
1078 i_block -= (p_pgc->cell_playback[i_cell].last_sector -
1079 p_pgc->cell_playback[i_cell].first_sector + 1);
1081 if( i_cell > p_sys->i_title_end_cell )
1083 msg_Err( p_demux, "couldn't find cell for block %i", i_block_offset );
1084 return;
1086 i_block += p_pgc->cell_playback[i_cell].first_sector;
1087 p_sys->i_title_offset = i_block_offset;
1089 /* Find chapter */
1090 for( i_chapter = 0; i_chapter < p_sys->i_chapters; i_chapter++ )
1092 int pgc_id, pgn, i_tmp;
1094 pgc_id = p_vts->vts_ptt_srpt->title[
1095 p_sys->i_ttn - 1].ptt[i_chapter].pgcn;
1096 pgn = p_vts->vts_ptt_srpt->title[
1097 p_sys->i_ttn - 1].ptt[i_chapter].pgn;
1099 i_tmp = p_vts->vts_pgcit->pgci_srp[pgc_id - 1].pgc->program_map[pgn-1];
1101 if( i_tmp > i_cell ) break;
1104 if( i_chapter < p_sys->i_chapters &&
1105 p_demux->info.i_seekpoint != i_chapter )
1107 p_demux->info.i_update |= INPUT_UPDATE_SEEKPOINT;
1108 p_demux->info.i_seekpoint = i_chapter;
1111 /* Find vobu */
1112 while( (int)p_vts->vts_vobu_admap->vobu_start_sectors[i_vobu] <= i_block )
1114 i_vobu++;
1117 /* Find sub_cell */
1118 while( p_vts->vts_c_adt->cell_adr_table[i_sub_cell].start_sector <
1119 p_vts->vts_vobu_admap->vobu_start_sectors[i_vobu-1] )
1121 i_sub_cell++;
1124 #if 1
1125 msg_Dbg( p_demux, "cell %d i_sub_cell %d chapter %d vobu %d "
1126 "cell_sector %d vobu_sector %d sub_cell_sector %d",
1127 i_cell, i_sub_cell, i_chapter, i_vobu,
1128 p_sys->p_cur_pgc->cell_playback[i_cell].first_sector,
1129 p_vts->vts_vobu_admap->vobu_start_sectors[i_vobu],
1130 p_vts->vts_c_adt->cell_adr_table[i_sub_cell - 1].start_sector);
1131 #endif
1133 p_sys->i_cur_block = i_block;
1134 p_sys->i_next_vobu = p_vts->vts_vobu_admap->vobu_start_sectors[i_vobu];
1135 p_sys->i_pack_len = p_sys->i_next_vobu - i_block;
1136 p_sys->i_cur_cell = i_cell;
1137 p_sys->i_chapter = i_chapter;
1138 DvdReadFindCell( p_demux );
1140 #undef p_vts
1141 #undef p_pgc
1143 return;
1146 /*****************************************************************************
1147 * DvdReadHandleDSI
1148 *****************************************************************************/
1149 static void DvdReadHandleDSI( demux_t *p_demux, uint8_t *p_data )
1151 demux_sys_t *p_sys = p_demux->p_sys;
1153 navRead_DSI( &p_sys->dsi_pack, &p_data[DSI_START_BYTE] );
1156 * Determine where we go next. These values are the ones we mostly
1157 * care about.
1159 p_sys->i_cur_block = p_sys->dsi_pack.dsi_gi.nv_pck_lbn;
1160 p_sys->i_pack_len = p_sys->dsi_pack.dsi_gi.vobu_ea;
1163 * Store the timecodes so we can get the current time
1165 p_sys->i_title_cur_time = (mtime_t) p_sys->dsi_pack.dsi_gi.nv_pck_scr / 90 * 1000;
1166 p_sys->i_cell_cur_time = (mtime_t) dvdtime_to_time( &p_sys->dsi_pack.dsi_gi.c_eltm, 0 );
1169 * If we're not at the end of this cell, we can determine the next
1170 * VOBU to display using the VOBU_SRI information section of the
1171 * DSI. Using this value correctly follows the current angle,
1172 * avoiding the doubled scenes in The Matrix, and makes our life
1173 * really happy.
1176 p_sys->i_next_vobu = p_sys->i_cur_block +
1177 ( p_sys->dsi_pack.vobu_sri.next_vobu & 0x7fffffff );
1179 if( p_sys->dsi_pack.vobu_sri.next_vobu != SRI_END_OF_CELL
1180 && p_sys->i_angle > 1 )
1182 switch( ( p_sys->dsi_pack.sml_pbi.category & 0xf000 ) >> 12 )
1184 case 0x4:
1185 /* Interleaved unit with no angle */
1186 if( p_sys->dsi_pack.sml_pbi.ilvu_sa != 0 )
1188 p_sys->i_next_vobu = p_sys->i_cur_block +
1189 p_sys->dsi_pack.sml_pbi.ilvu_sa;
1190 p_sys->i_pack_len = p_sys->dsi_pack.sml_pbi.ilvu_ea;
1192 else
1194 p_sys->i_next_vobu = p_sys->i_cur_block +
1195 p_sys->dsi_pack.dsi_gi.vobu_ea + 1;
1197 break;
1198 case 0x5:
1199 /* vobu is end of ilvu */
1200 if( p_sys->dsi_pack.sml_agli.data[p_sys->i_angle-1].address )
1202 p_sys->i_next_vobu = p_sys->i_cur_block +
1203 p_sys->dsi_pack.sml_agli.data[p_sys->i_angle-1].address;
1204 p_sys->i_pack_len = p_sys->dsi_pack.sml_pbi.ilvu_ea;
1206 break;
1208 case 0x6:
1209 /* vobu is beginning of ilvu */
1210 case 0x9:
1211 /* next scr is 0 */
1212 case 0xa:
1213 /* entering interleaved section */
1214 case 0x8:
1215 /* non interleaved cells in interleaved section */
1216 default:
1217 p_sys->i_next_vobu = p_sys->i_cur_block +
1218 ( p_sys->dsi_pack.vobu_sri.next_vobu & 0x7fffffff );
1219 break;
1222 else if( p_sys->dsi_pack.vobu_sri.next_vobu == SRI_END_OF_CELL )
1224 p_sys->i_cur_cell = p_sys->i_next_cell;
1226 /* End of title */
1227 if( p_sys->i_cur_cell >= p_sys->p_cur_pgc->nr_of_cells ) return;
1229 DvdReadFindCell( p_demux );
1231 p_sys->i_next_vobu =
1232 p_sys->p_cur_pgc->cell_playback[p_sys->i_cur_cell].first_sector;
1234 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 );
1238 #if 0
1239 msg_Dbg( p_demux, "scr %d lbn 0x%02x vobu_ea %d vob_id %d c_id %d c_time %lld",
1240 p_sys->dsi_pack.dsi_gi.nv_pck_scr,
1241 p_sys->dsi_pack.dsi_gi.nv_pck_lbn,
1242 p_sys->dsi_pack.dsi_gi.vobu_ea,
1243 p_sys->dsi_pack.dsi_gi.vobu_vob_idn,
1244 p_sys->dsi_pack.dsi_gi.vobu_c_idn,
1245 dvdtime_to_time( &p_sys->dsi_pack.dsi_gi.c_eltm, 0 ) );
1247 msg_Dbg( p_demux, "cell duration: %lld",
1248 (mtime_t)dvdtime_to_time( &p_sys->p_cur_pgc->cell_playback[p_sys->i_cur_cell].playback_time, 0 ) );
1250 msg_Dbg( p_demux, "cat 0x%02x ilvu_ea %d ilvu_sa %d size %d",
1251 p_sys->dsi_pack.sml_pbi.category,
1252 p_sys->dsi_pack.sml_pbi.ilvu_ea,
1253 p_sys->dsi_pack.sml_pbi.ilvu_sa,
1254 p_sys->dsi_pack.sml_pbi.size );
1256 msg_Dbg( p_demux, "next_vobu %d next_ilvu1 %d next_ilvu2 %d",
1257 p_sys->dsi_pack.vobu_sri.next_vobu & 0x7fffffff,
1258 p_sys->dsi_pack.sml_agli.data[ p_sys->i_angle - 1 ].address,
1259 p_sys->dsi_pack.sml_agli.data[ p_sys->i_angle ].address);
1260 #endif
1263 /*****************************************************************************
1264 * DvdReadFindCell
1265 *****************************************************************************/
1266 static void DvdReadFindCell( demux_t *p_demux )
1268 demux_sys_t *p_sys = p_demux->p_sys;
1270 pgc_t *p_pgc;
1271 int pgc_id, pgn;
1272 int i = 0;
1274 #define cell p_sys->p_cur_pgc->cell_playback
1276 if( cell[p_sys->i_cur_cell].block_type == BLOCK_TYPE_ANGLE_BLOCK )
1278 p_sys->i_cur_cell += p_sys->i_angle - 1;
1280 while( cell[p_sys->i_cur_cell+i].block_mode != BLOCK_MODE_LAST_CELL )
1282 i++;
1284 p_sys->i_next_cell = p_sys->i_cur_cell + i + 1;
1286 else
1288 p_sys->i_next_cell = p_sys->i_cur_cell + 1;
1291 #undef cell
1293 if( p_sys->i_chapter + 1 >= p_sys->i_chapters ) return;
1295 pgc_id = p_sys->p_vts_file->vts_ptt_srpt->title[
1296 p_sys->i_ttn - 1].ptt[p_sys->i_chapter + 1].pgcn;
1297 pgn = p_sys->p_vts_file->vts_ptt_srpt->title[
1298 p_sys->i_ttn - 1].ptt[p_sys->i_chapter + 1].pgn;
1299 p_pgc = p_sys->p_vts_file->vts_pgcit->pgci_srp[pgc_id - 1].pgc;
1301 if( p_sys->i_cur_cell >= p_pgc->program_map[pgn - 1] - 1 )
1303 p_sys->i_chapter++;
1305 if( p_sys->i_chapter < p_sys->i_chapters &&
1306 p_demux->info.i_seekpoint != p_sys->i_chapter )
1308 p_demux->info.i_update |= INPUT_UPDATE_SEEKPOINT;
1309 p_demux->info.i_seekpoint = p_sys->i_chapter;
1314 /*****************************************************************************
1315 * DemuxTitles: get the titles/chapters structure
1316 *****************************************************************************/
1317 static void DemuxTitles( demux_t *p_demux, int *pi_angle )
1319 VLC_UNUSED( pi_angle );
1321 demux_sys_t *p_sys = p_demux->p_sys;
1322 input_title_t *t;
1323 seekpoint_t *s;
1325 /* Find out number of titles/chapters */
1326 #define tt_srpt p_sys->p_vmg_file->tt_srpt
1328 int32_t i_titles = tt_srpt->nr_of_srpts;
1329 msg_Dbg( p_demux, "number of titles: %d", i_titles );
1331 for( int i = 0; i < i_titles; i++ )
1333 int32_t i_chapters = 0;
1334 int j;
1336 i_chapters = tt_srpt->title[i].nr_of_ptts;
1337 msg_Dbg( p_demux, "title %d has %d chapters", i, i_chapters );
1339 t = vlc_input_title_New();
1341 for( j = 0; j < __MAX( i_chapters, 1 ); j++ )
1343 s = vlc_seekpoint_New();
1344 TAB_APPEND( t->i_seekpoint, t->seekpoint, s );
1347 TAB_APPEND( p_sys->i_titles, p_sys->titles, t );
1350 #undef tt_srpt