chromecast: lower vorbis encoding quality
[vlc.git] / modules / access / dvdread.c
blobbd708d99affdadd48a46c02c143b5873a595c9d0
1 /*****************************************************************************
2 * dvdread.c : DvdRead input module for vlc
3 *****************************************************************************
4 * Copyright (C) 2001-2006 VLC authors and VideoLAN
5 * $Id$
7 * Authors: Stéphane Borel <stef@via.ecp.fr>
8 * Gildas Bazin <gbazin@videolan.org>
10 * This program is free software; you can redistribute it and/or modify it
11 * under the terms of the GNU Lesser General Public License as published by
12 * the Free Software Foundation; either version 2.1 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public License
21 * along with this program; if not, write to the Free Software Foundation,
22 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 *****************************************************************************/
25 /*****************************************************************************
26 * NOTA BENE: this module requires the linking against a library which is
27 * known to require licensing under the GNU General Public License version 2
28 * (or later). Therefore, the result of compiling this module will normally
29 * be subject to the terms of that later license.
30 *****************************************************************************/
33 /*****************************************************************************
34 * Preamble
35 *****************************************************************************/
37 #ifdef HAVE_CONFIG_H
38 # include "config.h"
39 #endif
41 #include <vlc_common.h>
42 #include <vlc_plugin.h>
43 #include <vlc_input.h>
44 #include <vlc_access.h>
45 #include <vlc_charset.h>
46 #include <vlc_interface.h>
47 #include <vlc_dialog.h>
49 #include <vlc_iso_lang.h>
51 #include "../demux/mpeg/pes.h"
52 #include "../demux/mpeg/ps.h"
54 #include <sys/types.h>
55 #include <unistd.h>
57 #include <dvdread/dvd_reader.h>
58 #include <dvdread/ifo_types.h>
59 #include <dvdread/ifo_read.h>
60 #include <dvdread/nav_read.h>
61 #include <dvdread/nav_print.h>
63 #include <assert.h>
65 /*****************************************************************************
66 * Module descriptor
67 *****************************************************************************/
68 #define ANGLE_TEXT N_("DVD angle")
69 #define ANGLE_LONGTEXT N_( \
70 "Default DVD angle." )
72 static int Open ( vlc_object_t * );
73 static void Close( vlc_object_t * );
75 vlc_module_begin ()
76 set_shortname( N_("DVD without menus") )
77 set_description( N_("DVDRead Input (no menu support)") )
78 set_category( CAT_INPUT )
79 set_subcategory( SUBCAT_INPUT_ACCESS )
80 add_integer( "dvdread-angle", 1, ANGLE_TEXT,
81 ANGLE_LONGTEXT, false )
82 add_obsolete_string( "dvdread-css-method" ) /* obsolete since 1.1.0 */
83 set_capability( "access_demux", 0 )
84 add_shortcut( "dvd", "dvdread", "dvdsimple" )
85 set_callbacks( Open, Close )
86 vlc_module_end ()
88 /* how many blocks DVDRead will read in each loop */
89 #define DVD_BLOCK_READ_ONCE 4
91 /*****************************************************************************
92 * Local prototypes
93 *****************************************************************************/
95 struct demux_sys_t
97 /* DVDRead state */
98 dvd_reader_t *p_dvdread;
99 dvd_file_t *p_title;
101 ifo_handle_t *p_vmg_file;
102 ifo_handle_t *p_vts_file;
104 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 mtime_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 mtime_t i_cell_cur_time;
134 mtime_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];
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->psz_filepath || !*p_demux->psz_filepath )
173 psz_file = var_InheritString( p_this, "dvd" );
174 else
175 psz_file = strdup( p_demux->psz_filepath );
177 #if defined( _WIN32 ) || defined( __OS2__ )
178 if( psz_file != NULL )
180 size_t flen = strlen( psz_file );
181 if( flen > 0 && psz_file[flen - 1] == '\\' )
182 psz_file[flen - 1] = '\0';
184 else
185 psz_file = strdup("");
186 #endif
187 if( unlikely(psz_file == NULL) )
188 return VLC_EGENERIC;
190 /* Open dvdread */
191 const char *psz_path = ToLocale( psz_file );
192 dvd_reader_t *p_dvdread = DVDOpen( psz_path );
194 LocaleFree( psz_path );
195 if( p_dvdread == NULL )
197 msg_Err( p_demux, "DVDRead cannot open source: %s", psz_file );
198 vlc_dialog_display_error( p_demux, _("Playback failure"),
199 _("DVDRead could not open the disc \"%s\"."), psz_file );
200 free( psz_file );
201 return VLC_EGENERIC;
203 free( psz_file );
205 /* Ifo allocation & initialisation */
206 if( !( p_vmg_file = ifoOpen( p_dvdread, 0 ) ) )
208 char rgsz_volid[32];
209 if( DVDUDFVolumeInfo( p_dvdread, rgsz_volid, 32, NULL, 0 ) )
211 if( DVDISOVolumeInfo( p_dvdread, rgsz_volid, 32, NULL, 0 ) == 0 )
213 vlc_dialog_display_error( p_demux, _("Playback failure"),
214 _("Cannot play a non-UDF mastered DVD." ) );
215 msg_Err( p_demux, "Invalid UDF DVD. (Found ISO9660 '%s')", rgsz_volid );
218 msg_Warn( p_demux, "cannot open VMG info" );
219 return VLC_EGENERIC;
221 msg_Dbg( p_demux, "VMG opened" );
223 /* Fill p_demux field */
224 DEMUX_INIT_COMMON(); p_sys = p_demux->p_sys;
226 ps_track_init( p_sys->tk );
227 p_sys->i_sar_num = 0;
228 p_sys->i_sar_den = 0;
229 p_sys->i_title_cur_time = (mtime_t) 0;
230 p_sys->i_cell_cur_time = (mtime_t) 0;
231 p_sys->i_cell_duration = (mtime_t) 0;
233 p_sys->p_dvdread = p_dvdread;
234 p_sys->p_vmg_file = p_vmg_file;
235 p_sys->p_title = NULL;
236 p_sys->p_vts_file = NULL;
238 p_sys->updates = 0;
239 p_sys->i_title = p_sys->i_chapter = -1;
240 p_sys->cur_title = p_sys->cur_chapter = 0;
241 p_sys->i_mux_rate = 0;
243 p_sys->i_angle = var_CreateGetInteger( p_demux, "dvdread-angle" );
244 if( p_sys->i_angle <= 0 ) p_sys->i_angle = 1;
246 DemuxTitles( p_demux, &p_sys->i_angle );
247 if( DvdReadSetArea( p_demux, 0, 0, p_sys->i_angle ) != VLC_SUCCESS )
249 msg_Err( p_demux, "DvdReadSetArea(0,0,%i) failed (can't decrypt DVD?)",
250 p_sys->i_angle );
251 Close( p_this );
252 return VLC_EGENERIC;
255 return VLC_SUCCESS;
258 /*****************************************************************************
259 * Close:
260 *****************************************************************************/
261 static void Close( vlc_object_t *p_this )
263 demux_t *p_demux = (demux_t*)p_this;
264 demux_sys_t *p_sys = p_demux->p_sys;
266 for( int i = 0; i < PS_TK_COUNT; i++ )
268 ps_track_t *tk = &p_sys->tk[i];
269 if( tk->b_configured )
271 es_format_Clean( &tk->fmt );
272 if( tk->es ) es_out_Del( p_demux->out, tk->es );
276 /* Free the array of titles */
277 for( int i = 0; i < p_sys->i_titles; i++ )
278 vlc_input_title_Delete( p_sys->titles[i] );
279 TAB_CLEAN( p_sys->i_titles, p_sys->titles );
281 /* Close libdvdread */
282 if( p_sys->p_title ) DVDCloseFile( p_sys->p_title );
283 if( p_sys->p_vts_file ) ifoClose( p_sys->p_vts_file );
284 if( p_sys->p_vmg_file ) ifoClose( p_sys->p_vmg_file );
285 DVDClose( p_sys->p_dvdread );
287 free( p_sys );
290 static int64_t dvdtime_to_time( dvd_time_t *dtime, uint8_t still_time )
292 /* Macro to convert Binary Coded Decimal to Decimal */
293 #define BCD2D(__x__) (((__x__ & 0xf0) >> 4) * 10 + (__x__ & 0x0f))
295 double f_fps, f_ms;
296 int64_t i_micro_second = 0;
298 if (still_time == 0 || still_time == 0xFF)
300 i_micro_second += (int64_t)(BCD2D(dtime->hour)) * 60 * 60 * 1000000;
301 i_micro_second += (int64_t)(BCD2D(dtime->minute)) * 60 * 1000000;
302 i_micro_second += (int64_t)(BCD2D(dtime->second)) * 1000000;
304 switch((dtime->frame_u & 0xc0) >> 6)
306 case 1:
307 f_fps = 25.0;
308 break;
309 case 3:
310 f_fps = 29.97;
311 break;
312 default:
313 f_fps = 2500.0;
314 break;
316 f_ms = BCD2D(dtime->frame_u&0x3f) * 1000.0 / f_fps;
317 i_micro_second += (int64_t)(f_ms * 1000.0);
319 else
321 i_micro_second = still_time;
322 i_micro_second = (int64_t)((double)i_micro_second * 1000000.0);
325 return i_micro_second;
328 /*****************************************************************************
329 * Control:
330 *****************************************************************************/
331 static int Control( demux_t *p_demux, int i_query, va_list args )
333 demux_sys_t *p_sys = p_demux->p_sys;
334 double f, *pf;
335 bool *pb;
336 int64_t *pi64;
337 input_title_t ***ppp_title;
338 int *pi_int;
339 int i;
341 switch( i_query )
343 case DEMUX_GET_POSITION:
345 pf = va_arg( args, double * );
347 if( p_sys->i_title_blocks > 0 )
348 *pf = (double)p_sys->i_title_offset / p_sys->i_title_blocks;
349 else
350 *pf = 0.0;
352 return VLC_SUCCESS;
354 case DEMUX_SET_POSITION:
356 f = va_arg( args, double );
358 DvdReadSeek( p_demux, f * p_sys->i_title_blocks );
360 return VLC_SUCCESS;
362 case DEMUX_GET_TIME:
363 pi64 = va_arg( args, int64_t * );
364 if( p_sys->cur_title >= 0 && p_sys->cur_title < p_sys->i_titles )
366 *pi64 = (int64_t) dvdtime_to_time( &p_sys->p_cur_pgc->playback_time, 0 ) /
367 p_sys->i_title_blocks * p_sys->i_title_offset;
368 return VLC_SUCCESS;
370 *pi64 = 0;
371 return VLC_EGENERIC;
373 case DEMUX_GET_LENGTH:
374 pi64 = va_arg( args, int64_t * );
375 if( p_sys->cur_title >= 0 && p_sys->cur_title < p_sys->i_titles )
377 *pi64 = (int64_t)dvdtime_to_time( &p_sys->p_cur_pgc->playback_time, 0 );
378 return VLC_SUCCESS;
380 *pi64 = 0;
381 return VLC_EGENERIC;
383 /* Special for access_demux */
384 case DEMUX_CAN_PAUSE:
385 case DEMUX_CAN_SEEK:
386 case DEMUX_CAN_CONTROL_PACE:
387 /* TODO */
388 pb = va_arg( args, bool * );
389 *pb = true;
390 return VLC_SUCCESS;
392 case DEMUX_SET_PAUSE_STATE:
393 return VLC_SUCCESS;
395 case DEMUX_GET_TITLE_INFO:
396 ppp_title = va_arg( args, input_title_t *** );
397 pi_int = va_arg( args, int * );
398 *va_arg( args, int * ) = 1; /* Title offset */
399 *va_arg( args, int * ) = 1; /* Chapter offset */
401 /* Duplicate title infos */
402 *pi_int = p_sys->i_titles;
403 *ppp_title = vlc_alloc( p_sys->i_titles, sizeof(input_title_t *) );
404 for( i = 0; i < p_sys->i_titles; i++ )
406 (*ppp_title)[i] = vlc_input_title_Duplicate(p_sys->titles[i]);
408 return VLC_SUCCESS;
410 case DEMUX_SET_TITLE:
411 i = va_arg( args, int );
412 if( DvdReadSetArea( p_demux, i, 0, -1 ) != VLC_SUCCESS )
414 msg_Warn( p_demux, "cannot set title/chapter" );
415 return VLC_EGENERIC;
417 p_sys->updates |= INPUT_UPDATE_TITLE | INPUT_UPDATE_SEEKPOINT;
418 p_sys->cur_title = i;
419 p_sys->cur_chapter = 0;
420 return VLC_SUCCESS;
422 case DEMUX_SET_SEEKPOINT:
423 i = va_arg( args, int );
424 if( DvdReadSetArea( p_demux, -1, i, -1 ) != VLC_SUCCESS )
426 msg_Warn( p_demux, "cannot set title/chapter" );
427 return VLC_EGENERIC;
429 p_sys->updates |= INPUT_UPDATE_SEEKPOINT;
430 p_sys->cur_chapter = i;
431 return VLC_SUCCESS;
433 case DEMUX_TEST_AND_CLEAR_FLAGS:
435 unsigned *restrict flags = va_arg(args, unsigned *);
436 *flags &= p_sys->updates;
437 p_sys->updates &= ~*flags;
438 return VLC_SUCCESS;
441 case DEMUX_GET_TITLE:
442 *va_arg( args, int * ) = p_sys->cur_title;
443 return VLC_SUCCESS;
445 case DEMUX_GET_SEEKPOINT:
446 *va_arg( args, int * ) = p_sys->cur_chapter;
447 return VLC_SUCCESS;
449 case DEMUX_GET_PTS_DELAY:
450 pi64 = va_arg( args, int64_t * );
451 *pi64 =
452 INT64_C(1000) * var_InheritInteger( p_demux, "disc-caching" );
453 return VLC_SUCCESS;
455 /* TODO implement others */
456 default:
457 return VLC_EGENERIC;
461 /*****************************************************************************
462 * Demux:
463 *****************************************************************************/
464 static int Demux( demux_t *p_demux )
466 demux_sys_t *p_sys = p_demux->p_sys;
468 uint8_t p_buffer[DVD_VIDEO_LB_LEN * DVD_BLOCK_READ_ONCE];
469 int i_blocks_once, i_read;
472 * Playback by cell in this pgc, starting at the cell for our chapter.
476 * Check end of pack, and select the following one
478 if( !p_sys->i_pack_len )
480 /* Read NAV packet */
481 if( DVDReadBlocks( p_sys->p_title, p_sys->i_next_vobu,
482 1, p_buffer ) != 1 )
484 msg_Err( p_demux, "read failed for block %d", p_sys->i_next_vobu );
485 vlc_dialog_display_error( p_demux, _("Playback failure"),
486 _("DVDRead could not read block %d."),
487 p_sys->i_next_vobu );
488 return -1;
491 /* Basic check to be sure we don't have a empty title
492 * go to next title if so */
493 //assert( p_buffer[41] == 0xbf && p_buffer[1027] == 0xbf );
495 /* Parse the contained dsi packet */
496 DvdReadHandleDSI( p_demux, p_buffer );
498 /* End of title */
499 if( p_sys->i_cur_cell >= p_sys->p_cur_pgc->nr_of_cells )
501 int k = p_sys->i_title;
503 /* Looking for a not broken title */
504 while( k < p_sys->i_titles && DvdReadSetArea( p_demux, ++k, 0, -1 ) != VLC_SUCCESS )
506 msg_Err(p_demux, "Failed next title, trying another: %i", k );
507 if( k >= p_sys->i_titles )
508 return 0; // EOF
512 if( p_sys->i_pack_len >= 1024 )
514 msg_Err( p_demux, "i_pack_len >= 1024 (%i). "
515 "This shouldn't happen!", p_sys->i_pack_len );
516 return 0; /* EOF */
519 /* FIXME: Ugly kludge: we send the pack block to the input for it
520 * sometimes has a zero scr and restart the sync */
521 p_sys->i_cur_block++;
522 p_sys->i_title_offset++;
524 DemuxBlock( p_demux, p_buffer, DVD_VIDEO_LB_LEN );
527 if( p_sys->i_cur_cell >= p_sys->p_cur_pgc->nr_of_cells )
529 int k = p_sys->i_title;
531 /* Looking for a not broken title */
532 while( k < p_sys->i_titles && DvdReadSetArea( p_demux, ++k, 0, -1 ) != VLC_SUCCESS )
534 msg_Err(p_demux, "Failed next title, trying another: %i", k );
535 if( k >= p_sys->i_titles )
536 return 0; // EOF
541 * Read actual data
543 i_blocks_once = __MIN( p_sys->i_pack_len, DVD_BLOCK_READ_ONCE );
544 p_sys->i_pack_len -= i_blocks_once;
546 /* Reads from DVD */
547 i_read = DVDReadBlocks( p_sys->p_title, p_sys->i_cur_block,
548 i_blocks_once, p_buffer );
549 if( i_read != i_blocks_once )
551 msg_Err( p_demux, "read failed for %d/%d blocks at 0x%02x",
552 i_read, i_blocks_once, p_sys->i_cur_block );
553 vlc_dialog_display_error( p_demux, _("Playback failure"),
554 _("DVDRead could not read %d/%d blocks at 0x%02x."),
555 i_read, i_blocks_once, p_sys->i_cur_block );
556 return -1;
559 p_sys->i_cur_block += i_read;
560 p_sys->i_title_offset += i_read;
562 #if 0
563 msg_Dbg( p_demux, "i_blocks: %d len: %d current: 0x%02x",
564 i_read, p_sys->i_pack_len, p_sys->i_cur_block );
565 #endif
567 for( int i = 0; i < i_read; i++ )
569 DemuxBlock( p_demux, p_buffer + i * DVD_VIDEO_LB_LEN,
570 DVD_VIDEO_LB_LEN );
573 #undef p_pgc
575 return 1;
578 /*****************************************************************************
579 * DemuxBlock: demux a given block
580 *****************************************************************************/
581 static int DemuxBlock( demux_t *p_demux, const uint8_t *p, int len )
583 demux_sys_t *p_sys = p_demux->p_sys;
585 while( len > 0 )
587 int i_size = ps_pkt_size( p, len );
588 if( i_size <= 0 || i_size > len )
590 break;
593 /* Create a block */
594 block_t *p_pkt = block_Alloc( i_size );
595 memcpy( p_pkt->p_buffer, p, i_size);
597 /* Parse it and send it */
598 switch( 0x100 | p[3] )
600 case 0x1b9:
601 case 0x1bb:
602 case 0x1bc:
604 #ifdef DVDREAD_DEBUG
605 if( p[3] == 0xbc )
607 msg_Warn( p_demux, "received a PSM packet" );
609 else if( p[3] == 0xbb )
611 msg_Warn( p_demux, "received a SYSTEM packet" );
613 #endif
614 block_Release( p_pkt );
615 break;
617 case 0x1ba:
619 int64_t i_scr;
620 int i_mux_rate;
621 if( !ps_pkt_parse_pack( p_pkt, &i_scr, &i_mux_rate ) )
623 es_out_SetPCR( p_demux->out, i_scr );
624 if( i_mux_rate > 0 ) p_sys->i_mux_rate = i_mux_rate;
626 block_Release( p_pkt );
627 break;
629 default:
631 int i_id = ps_pkt_id( p_pkt );
632 if( i_id >= 0xc0 )
634 ps_track_t *tk = &p_sys->tk[ps_id_to_tk(i_id)];
636 if( !tk->b_configured )
638 ESNew( p_demux, i_id, 0 );
640 if( tk->es &&
641 !ps_pkt_parse_pes( VLC_OBJECT(p_demux), p_pkt, tk->i_skip ) )
643 es_out_Send( p_demux->out, tk->es, p_pkt );
645 else
647 block_Release( p_pkt );
650 else
652 block_Release( p_pkt );
654 break;
658 p += i_size;
659 len -= i_size;
662 return VLC_SUCCESS;
665 /*****************************************************************************
666 * ESNew: register a new elementary stream
667 *****************************************************************************/
668 static void ESNew( demux_t *p_demux, int i_id, int i_lang )
670 demux_sys_t *p_sys = p_demux->p_sys;
671 ps_track_t *tk = &p_sys->tk[ps_id_to_tk(i_id)];
672 char psz_language[3];
674 if( tk->b_configured ) return;
676 if( ps_track_fill( tk, 0, i_id, NULL, true ) )
678 msg_Warn( p_demux, "unknown codec for id=0x%x", i_id );
679 return;
682 psz_language[0] = psz_language[1] = psz_language[2] = 0;
683 if( i_lang && i_lang != 0xffff )
685 psz_language[0] = (i_lang >> 8)&0xff;
686 psz_language[1] = (i_lang )&0xff;
689 /* Add a new ES */
690 if( tk->fmt.i_cat == VIDEO_ES )
692 tk->fmt.video.i_sar_num = p_sys->i_sar_num;
693 tk->fmt.video.i_sar_den = p_sys->i_sar_den;
695 else if( tk->fmt.i_cat == AUDIO_ES )
697 #if 0
698 int i_audio = -1;
699 /* find the audio number PLEASE find another way */
700 if( (i_id&0xbdf8) == 0xbd88 ) /* dts */
702 i_audio = i_id&0x07;
704 else if( (i_id&0xbdf0) == 0xbd80 ) /* a52 */
706 i_audio = i_id&0xf;
708 else if( (i_id&0xbdf0) == 0xbda0 ) /* lpcm */
710 i_audio = i_id&0x1f;
712 else if( ( i_id&0xe0 ) == 0xc0 ) /* mpga */
714 i_audio = i_id&0x1f;
716 #endif
718 if( psz_language[0] ) tk->fmt.psz_language = strdup( psz_language );
720 else if( tk->fmt.i_cat == SPU_ES )
722 /* Palette */
723 tk->fmt.subs.spu.palette[0] = SPU_PALETTE_DEFINED;
724 memcpy( &tk->fmt.subs.spu.palette[1], p_sys->clut,
725 16 * sizeof( uint32_t ) );
727 if( psz_language[0] ) tk->fmt.psz_language = strdup( psz_language );
730 tk->es = es_out_Add( p_demux->out, &tk->fmt );
731 tk->b_configured = true;
734 /*****************************************************************************
735 * DvdReadSetArea: initialize input data for title x, chapter y.
736 * It should be called for each user navigation request.
737 *****************************************************************************
738 * Take care that i_title and i_chapter start from 0.
739 *****************************************************************************/
740 static int DvdReadSetArea( demux_t *p_demux, int i_title, int i_chapter,
741 int i_angle )
743 VLC_UNUSED( i_angle );
745 demux_sys_t *p_sys = p_demux->p_sys;
746 int pgc_id = 0, pgn = 0;
748 #define p_pgc p_sys->p_cur_pgc
749 #define p_vmg p_sys->p_vmg_file
750 #define p_vts p_sys->p_vts_file
752 if( i_title >= 0 && i_title < p_sys->i_titles &&
753 i_title != p_sys->i_title )
755 int i_start_cell, i_end_cell;
757 if( p_sys->p_title != NULL )
759 DVDCloseFile( p_sys->p_title );
760 p_sys->p_title = NULL;
762 if( p_vts != NULL ) ifoClose( p_vts );
763 p_sys->i_title = i_title;
766 * We have to load all title information
768 msg_Dbg( p_demux, "open VTS %d, for title %d",
769 p_vmg->tt_srpt->title[i_title].title_set_nr, i_title + 1 );
771 /* Ifo vts */
772 if( !( p_vts = ifoOpen( p_sys->p_dvdread,
773 p_vmg->tt_srpt->title[i_title].title_set_nr ) ) )
775 msg_Err( p_demux, "fatal error in vts ifo" );
776 return VLC_EGENERIC;
779 /* Title position inside the selected vts */
780 p_sys->i_ttn = p_vmg->tt_srpt->title[i_title].vts_ttn;
782 /* Find title start/end */
783 pgc_id = p_vts->vts_ptt_srpt->title[p_sys->i_ttn - 1].ptt[0].pgcn;
784 pgn = p_vts->vts_ptt_srpt->title[p_sys->i_ttn - 1].ptt[0].pgn;
785 p_pgc = p_vts->vts_pgcit->pgci_srp[pgc_id - 1].pgc;
787 if( p_pgc->cell_playback == NULL )
789 msg_Err( p_demux, "Invalid PGC (cell_playback_offset)" );
790 return VLC_EGENERIC;
793 p_sys->i_title_start_cell =
794 i_start_cell = p_pgc->program_map[pgn - 1] - 1;
795 p_sys->i_title_start_block =
796 p_pgc->cell_playback[i_start_cell].first_sector;
798 p_sys->i_title_end_cell =
799 i_end_cell = p_pgc->nr_of_cells - 1;
800 p_sys->i_title_end_block =
801 p_pgc->cell_playback[i_end_cell].last_sector;
803 p_sys->i_title_offset = 0;
805 p_sys->i_title_blocks = 0;
806 for( int i = i_start_cell; i <= i_end_cell; i++ )
808 p_sys->i_title_blocks += p_pgc->cell_playback[i].last_sector -
809 p_pgc->cell_playback[i].first_sector + 1;
812 msg_Dbg( p_demux, "title %d vts_title %d pgc %d pgn %d "
813 "start %d end %d blocks: %d",
814 i_title + 1, p_sys->i_ttn, pgc_id, pgn,
815 p_sys->i_title_start_block, p_sys->i_title_end_block,
816 p_sys->i_title_blocks );
819 * Set properties for current chapter
821 p_sys->i_chapter = 0;
822 p_sys->i_chapters =
823 p_vts->vts_ptt_srpt->title[p_sys->i_ttn - 1].nr_of_ptts;
825 pgc_id = p_vts->vts_ptt_srpt->title[
826 p_sys->i_ttn - 1].ptt[p_sys->i_chapter].pgcn;
827 pgn = p_vts->vts_ptt_srpt->title[
828 p_sys->i_ttn - 1].ptt[p_sys->i_chapter].pgn;
830 p_pgc = p_vts->vts_pgcit->pgci_srp[pgc_id - 1].pgc;
831 p_sys->i_pack_len = 0;
832 p_sys->i_next_cell =
833 p_sys->i_cur_cell = p_pgc->program_map[pgn - 1] - 1;
834 DvdReadFindCell( p_demux );
836 p_sys->i_next_vobu = p_sys->i_cur_block =
837 p_pgc->cell_playback[p_sys->i_cur_cell].first_sector;
840 * Angle management
842 p_sys->i_angles = p_vmg->tt_srpt->title[i_title].nr_of_angles;
843 if( p_sys->i_angle > p_sys->i_angles ) p_sys->i_angle = 1;
846 * We've got enough info, time to open the title set data.
848 if( !( p_sys->p_title = DVDOpenFile( p_sys->p_dvdread,
849 p_vmg->tt_srpt->title[i_title].title_set_nr,
850 DVD_READ_TITLE_VOBS ) ) )
852 msg_Err( p_demux, "cannot open title (VTS_%02d_1.VOB)",
853 p_vmg->tt_srpt->title[i_title].title_set_nr );
854 return VLC_EGENERIC;
857 //IfoPrintTitle( p_demux );
860 * Destroy obsolete ES by reinitializing program 0
861 * and find all ES in title with ifo data
863 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
865 for( int i = 0; i < PS_TK_COUNT; i++ )
867 ps_track_t *tk = &p_sys->tk[i];
868 if( tk->b_configured )
870 es_format_Clean( &tk->fmt );
871 if( tk->es ) es_out_Del( p_demux->out, tk->es );
873 tk->b_configured = false;
876 if( p_sys->cur_title != i_title )
878 p_sys->updates |= INPUT_UPDATE_TITLE | INPUT_UPDATE_SEEKPOINT;
879 p_sys->cur_title = i_title;
880 p_sys->cur_chapter = 0;
883 /* TODO: re-add angles */
886 ESNew( p_demux, 0xe0, 0 ); /* Video, FIXME ? */
887 const video_attr_t *p_attr = &p_vts->vtsi_mat->vts_video_attr;
888 int i_video_height = p_attr->video_format != 0 ? 576 : 480;
889 int i_video_width;
890 switch( p_attr->picture_size )
892 case 0:
893 i_video_width = 720;
894 break;
895 case 1:
896 i_video_width = 704;
897 break;
898 case 2:
899 i_video_width = 352;
900 break;
901 default:
902 case 3:
903 i_video_width = 352;
904 i_video_height /= 2;
905 break;
907 switch( p_attr->display_aspect_ratio )
909 case 0:
910 p_sys->i_sar_num = 4 * i_video_height;
911 p_sys->i_sar_den = 3 * i_video_width;
912 break;
913 case 3:
914 p_sys->i_sar_num = 16 * i_video_height;
915 p_sys->i_sar_den = 9 * i_video_width;
916 break;
917 default:
918 p_sys->i_sar_num = 0;
919 p_sys->i_sar_den = 0;
920 break;
923 #define audio_control \
924 p_sys->p_vts_file->vts_pgcit->pgci_srp[pgc_id-1].pgc->audio_control[i-1]
926 /* Audio ES, in the order they appear in the .ifo */
927 for( int i = 1; i <= p_vts->vtsi_mat->nr_of_vts_audio_streams; i++ )
929 int i_position = 0;
930 uint16_t i_id;
932 //IfoPrintAudio( p_demux, i );
934 /* Audio channel is active if first byte is 0x80 */
935 if( audio_control & 0x8000 )
937 i_position = ( audio_control & 0x7F00 ) >> 8;
939 msg_Dbg( p_demux, "audio position %d", i_position );
940 switch( p_vts->vtsi_mat->vts_audio_attr[i - 1].audio_format )
942 case 0x00: /* A52 */
943 i_id = (0x80 + i_position) | 0xbd00;
944 break;
945 case 0x02:
946 case 0x03: /* MPEG audio */
947 i_id = 0xc000 + i_position;
948 break;
949 case 0x04: /* LPCM */
950 i_id = (0xa0 + i_position) | 0xbd00;
951 break;
952 case 0x06: /* DTS */
953 i_id = (0x88 + i_position) | 0xbd00;
954 break;
955 default:
956 i_id = 0;
957 msg_Err( p_demux, "unknown audio type %.2x",
958 p_vts->vtsi_mat->vts_audio_attr[i - 1].audio_format );
961 ESNew( p_demux, i_id, p_sys->p_vts_file->vtsi_mat->
962 vts_audio_attr[i - 1].lang_code );
965 #undef audio_control
967 #define spu_palette \
968 p_sys->p_vts_file->vts_pgcit->pgci_srp[pgc_id-1].pgc->palette
970 memcpy( p_sys->clut, spu_palette, 16 * sizeof( uint32_t ) );
972 #define spu_control \
973 p_sys->p_vts_file->vts_pgcit->pgci_srp[pgc_id-1].pgc->subp_control[i-1]
975 /* Sub Picture ES */
976 for( int i = 1; i <= p_vts->vtsi_mat->nr_of_vts_subp_streams; i++ )
978 int i_position = 0;
979 uint16_t i_id;
981 //IfoPrintSpu( p_sys, i );
982 msg_Dbg( p_demux, "spu %d 0x%02x", i, spu_control );
984 if( spu_control & 0x80000000 )
986 /* there are several streams for one spu */
987 if( p_vts->vtsi_mat->vts_video_attr.display_aspect_ratio )
989 /* 16:9 */
990 switch( p_vts->vtsi_mat->vts_video_attr.permitted_df )
992 case 1: /* letterbox */
993 i_position = spu_control & 0xff;
994 break;
995 case 2: /* pan&scan */
996 i_position = ( spu_control >> 8 ) & 0xff;
997 break;
998 default: /* widescreen */
999 i_position = ( spu_control >> 16 ) & 0xff;
1000 break;
1003 else
1005 /* 4:3 */
1006 i_position = ( spu_control >> 24 ) & 0x7F;
1009 i_id = (0x20 + i_position) | 0xbd00;
1011 ESNew( p_demux, i_id, p_sys->p_vts_file->vtsi_mat->
1012 vts_subp_attr[i - 1].lang_code );
1015 #undef spu_control
1018 else if( i_title != -1 && i_title != p_sys->i_title )
1021 return VLC_EGENERIC; /* Couldn't set title */
1025 * Chapter selection
1028 if( i_chapter >= 0 && i_chapter < p_sys->i_chapters )
1030 pgc_id = p_vts->vts_ptt_srpt->title[
1031 p_sys->i_ttn - 1].ptt[i_chapter].pgcn;
1032 pgn = p_vts->vts_ptt_srpt->title[
1033 p_sys->i_ttn - 1].ptt[i_chapter].pgn;
1035 p_pgc = p_vts->vts_pgcit->pgci_srp[pgc_id - 1].pgc;
1036 if( p_pgc->cell_playback == NULL )
1037 return VLC_EGENERIC; /* Couldn't set chapter */
1039 p_sys->i_cur_cell = p_pgc->program_map[pgn - 1] - 1;
1040 p_sys->i_chapter = i_chapter;
1041 DvdReadFindCell( p_demux );
1043 p_sys->i_title_offset = 0;
1044 for( int i = p_sys->i_title_start_cell; i < p_sys->i_cur_cell; i++ )
1046 p_sys->i_title_offset += p_pgc->cell_playback[i].last_sector -
1047 p_pgc->cell_playback[i].first_sector + 1;
1050 p_sys->i_pack_len = 0;
1051 p_sys->i_next_vobu = p_sys->i_cur_block =
1052 p_pgc->cell_playback[p_sys->i_cur_cell].first_sector;
1054 if( p_sys->cur_chapter != i_chapter )
1056 p_sys->updates |= INPUT_UPDATE_SEEKPOINT;
1057 p_sys->cur_chapter = i_chapter;
1060 else if( i_chapter != -1 )
1063 return VLC_EGENERIC; /* Couldn't set chapter */
1066 #undef p_pgc
1067 #undef p_vts
1068 #undef p_vmg
1070 return VLC_SUCCESS;
1073 /*****************************************************************************
1074 * DvdReadSeek : Goes to a given position on the stream.
1075 *****************************************************************************
1076 * This one is used by the input and translate chronological position from
1077 * input to logical position on the device.
1078 *****************************************************************************/
1079 static void DvdReadSeek( demux_t *p_demux, int i_block_offset )
1081 demux_sys_t *p_sys = p_demux->p_sys;
1082 int i_chapter = 0;
1083 int i_cell = 0;
1084 int i_vobu = 0;
1085 int i_sub_cell = 0;
1086 int i_block;
1088 #define p_pgc p_sys->p_cur_pgc
1089 #define p_vts p_sys->p_vts_file
1091 /* Find cell */
1092 i_block = i_block_offset;
1093 for( i_cell = p_sys->i_title_start_cell;
1094 i_cell <= p_sys->i_title_end_cell; i_cell++ )
1096 if( i_block < (int)p_pgc->cell_playback[i_cell].last_sector -
1097 (int)p_pgc->cell_playback[i_cell].first_sector + 1 ) break;
1099 i_block -= (p_pgc->cell_playback[i_cell].last_sector -
1100 p_pgc->cell_playback[i_cell].first_sector + 1);
1102 if( i_cell > p_sys->i_title_end_cell )
1104 msg_Err( p_demux, "couldn't find cell for block %i", i_block_offset );
1105 return;
1107 i_block += p_pgc->cell_playback[i_cell].first_sector;
1108 p_sys->i_title_offset = i_block_offset;
1110 /* Find chapter */
1111 for( i_chapter = 0; i_chapter < p_sys->i_chapters; i_chapter++ )
1113 int pgc_id, pgn, i_tmp;
1115 pgc_id = p_vts->vts_ptt_srpt->title[
1116 p_sys->i_ttn - 1].ptt[i_chapter].pgcn;
1117 pgn = p_vts->vts_ptt_srpt->title[
1118 p_sys->i_ttn - 1].ptt[i_chapter].pgn;
1120 i_tmp = p_vts->vts_pgcit->pgci_srp[pgc_id - 1].pgc->program_map[pgn-1];
1122 if( i_tmp > i_cell ) break;
1125 if( i_chapter < p_sys->i_chapters &&
1126 p_sys->cur_chapter != i_chapter )
1128 p_sys->updates |= INPUT_UPDATE_SEEKPOINT;
1129 p_sys->cur_chapter = i_chapter;
1132 /* Find vobu */
1133 while( (int)p_vts->vts_vobu_admap->vobu_start_sectors[i_vobu] <= i_block )
1135 i_vobu++;
1138 /* Find sub_cell */
1139 while( p_vts->vts_c_adt->cell_adr_table[i_sub_cell].start_sector <
1140 p_vts->vts_vobu_admap->vobu_start_sectors[i_vobu-1] )
1142 i_sub_cell++;
1145 #if 1
1146 msg_Dbg( p_demux, "cell %d i_sub_cell %d chapter %d vobu %d "
1147 "cell_sector %d vobu_sector %d sub_cell_sector %d",
1148 i_cell, i_sub_cell, i_chapter, i_vobu,
1149 p_sys->p_cur_pgc->cell_playback[i_cell].first_sector,
1150 p_vts->vts_vobu_admap->vobu_start_sectors[i_vobu],
1151 p_vts->vts_c_adt->cell_adr_table[i_sub_cell - 1].start_sector);
1152 #endif
1154 p_sys->i_cur_block = i_block;
1155 p_sys->i_next_vobu = p_vts->vts_vobu_admap->vobu_start_sectors[i_vobu];
1156 p_sys->i_pack_len = p_sys->i_next_vobu - i_block;
1157 p_sys->i_cur_cell = i_cell;
1158 p_sys->i_chapter = i_chapter;
1159 DvdReadFindCell( p_demux );
1161 #undef p_vts
1162 #undef p_pgc
1164 return;
1167 /*****************************************************************************
1168 * DvdReadHandleDSI
1169 *****************************************************************************/
1170 static void DvdReadHandleDSI( demux_t *p_demux, uint8_t *p_data )
1172 demux_sys_t *p_sys = p_demux->p_sys;
1174 navRead_DSI( &p_sys->dsi_pack, &p_data[DSI_START_BYTE] );
1177 * Determine where we go next. These values are the ones we mostly
1178 * care about.
1180 p_sys->i_cur_block = p_sys->dsi_pack.dsi_gi.nv_pck_lbn;
1181 p_sys->i_pack_len = p_sys->dsi_pack.dsi_gi.vobu_ea;
1184 * Store the timecodes so we can get the current time
1186 p_sys->i_title_cur_time = (mtime_t) p_sys->dsi_pack.dsi_gi.nv_pck_scr / 90 * 1000;
1187 p_sys->i_cell_cur_time = (mtime_t) dvdtime_to_time( &p_sys->dsi_pack.dsi_gi.c_eltm, 0 );
1190 * If we're not at the end of this cell, we can determine the next
1191 * VOBU to display using the VOBU_SRI information section of the
1192 * DSI. Using this value correctly follows the current angle,
1193 * avoiding the doubled scenes in The Matrix, and makes our life
1194 * really happy.
1197 p_sys->i_next_vobu = p_sys->i_cur_block +
1198 ( p_sys->dsi_pack.vobu_sri.next_vobu & 0x7fffffff );
1200 if( p_sys->dsi_pack.vobu_sri.next_vobu != SRI_END_OF_CELL
1201 && p_sys->i_angle > 1 )
1203 switch( ( p_sys->dsi_pack.sml_pbi.category & 0xf000 ) >> 12 )
1205 case 0x4:
1206 /* Interleaved unit with no angle */
1207 if( p_sys->dsi_pack.sml_pbi.ilvu_sa != 0 )
1209 p_sys->i_next_vobu = p_sys->i_cur_block +
1210 p_sys->dsi_pack.sml_pbi.ilvu_sa;
1211 p_sys->i_pack_len = p_sys->dsi_pack.sml_pbi.ilvu_ea;
1213 else
1215 p_sys->i_next_vobu = p_sys->i_cur_block +
1216 p_sys->dsi_pack.dsi_gi.vobu_ea + 1;
1218 break;
1219 case 0x5:
1220 /* vobu is end of ilvu */
1221 if( p_sys->dsi_pack.sml_agli.data[p_sys->i_angle-1].address )
1223 p_sys->i_next_vobu = p_sys->i_cur_block +
1224 p_sys->dsi_pack.sml_agli.data[p_sys->i_angle-1].address;
1225 p_sys->i_pack_len = p_sys->dsi_pack.sml_pbi.ilvu_ea;
1227 break;
1229 /* fall through */
1230 case 0x6:
1231 /* vobu is beginning of ilvu */
1232 case 0x9:
1233 /* next scr is 0 */
1234 case 0xa:
1235 /* entering interleaved section */
1236 case 0x8:
1237 /* non interleaved cells in interleaved section */
1238 default:
1239 p_sys->i_next_vobu = p_sys->i_cur_block +
1240 ( p_sys->dsi_pack.vobu_sri.next_vobu & 0x7fffffff );
1241 break;
1244 else if( p_sys->dsi_pack.vobu_sri.next_vobu == SRI_END_OF_CELL )
1246 p_sys->i_cur_cell = p_sys->i_next_cell;
1248 /* End of title */
1249 if( p_sys->i_cur_cell >= p_sys->p_cur_pgc->nr_of_cells ) return;
1251 DvdReadFindCell( p_demux );
1253 p_sys->i_next_vobu =
1254 p_sys->p_cur_pgc->cell_playback[p_sys->i_cur_cell].first_sector;
1256 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 );
1260 #if 0
1261 msg_Dbg( p_demux, "scr %d lbn 0x%02x vobu_ea %d vob_id %d c_id %d c_time %lld",
1262 p_sys->dsi_pack.dsi_gi.nv_pck_scr,
1263 p_sys->dsi_pack.dsi_gi.nv_pck_lbn,
1264 p_sys->dsi_pack.dsi_gi.vobu_ea,
1265 p_sys->dsi_pack.dsi_gi.vobu_vob_idn,
1266 p_sys->dsi_pack.dsi_gi.vobu_c_idn,
1267 dvdtime_to_time( &p_sys->dsi_pack.dsi_gi.c_eltm, 0 ) );
1269 msg_Dbg( p_demux, "cell duration: %lld",
1270 (mtime_t)dvdtime_to_time( &p_sys->p_cur_pgc->cell_playback[p_sys->i_cur_cell].playback_time, 0 ) );
1272 msg_Dbg( p_demux, "cat 0x%02x ilvu_ea %d ilvu_sa %d size %d",
1273 p_sys->dsi_pack.sml_pbi.category,
1274 p_sys->dsi_pack.sml_pbi.ilvu_ea,
1275 p_sys->dsi_pack.sml_pbi.ilvu_sa,
1276 p_sys->dsi_pack.sml_pbi.size );
1278 msg_Dbg( p_demux, "next_vobu %d next_ilvu1 %d next_ilvu2 %d",
1279 p_sys->dsi_pack.vobu_sri.next_vobu & 0x7fffffff,
1280 p_sys->dsi_pack.sml_agli.data[ p_sys->i_angle - 1 ].address,
1281 p_sys->dsi_pack.sml_agli.data[ p_sys->i_angle ].address);
1282 #endif
1285 /*****************************************************************************
1286 * DvdReadFindCell
1287 *****************************************************************************/
1288 static void DvdReadFindCell( demux_t *p_demux )
1290 demux_sys_t *p_sys = p_demux->p_sys;
1292 pgc_t *p_pgc;
1293 int pgc_id, pgn;
1294 int i = 0;
1296 #define cell p_sys->p_cur_pgc->cell_playback
1298 if( cell[p_sys->i_cur_cell].block_type == BLOCK_TYPE_ANGLE_BLOCK )
1300 p_sys->i_cur_cell += p_sys->i_angle - 1;
1302 while( cell[p_sys->i_cur_cell+i].block_mode != BLOCK_MODE_LAST_CELL )
1304 i++;
1306 p_sys->i_next_cell = p_sys->i_cur_cell + i + 1;
1308 else
1310 p_sys->i_next_cell = p_sys->i_cur_cell + 1;
1313 #undef cell
1315 if( p_sys->i_chapter + 1 >= p_sys->i_chapters ) return;
1317 pgc_id = p_sys->p_vts_file->vts_ptt_srpt->title[
1318 p_sys->i_ttn - 1].ptt[p_sys->i_chapter + 1].pgcn;
1319 pgn = p_sys->p_vts_file->vts_ptt_srpt->title[
1320 p_sys->i_ttn - 1].ptt[p_sys->i_chapter + 1].pgn;
1321 p_pgc = p_sys->p_vts_file->vts_pgcit->pgci_srp[pgc_id - 1].pgc;
1323 if( p_sys->i_cur_cell >= p_pgc->program_map[pgn - 1] - 1 )
1325 p_sys->i_chapter++;
1327 if( p_sys->i_chapter < p_sys->i_chapters &&
1328 p_sys->cur_chapter != p_sys->i_chapter )
1330 p_sys->updates |= INPUT_UPDATE_SEEKPOINT;
1331 p_sys->cur_chapter = p_sys->i_chapter;
1336 /*****************************************************************************
1337 * DemuxTitles: get the titles/chapters structure
1338 *****************************************************************************/
1339 static void DemuxTitles( demux_t *p_demux, int *pi_angle )
1341 VLC_UNUSED( pi_angle );
1343 demux_sys_t *p_sys = p_demux->p_sys;
1344 input_title_t *t;
1345 seekpoint_t *s;
1347 /* Find out number of titles/chapters */
1348 #define tt_srpt p_sys->p_vmg_file->tt_srpt
1350 int32_t i_titles = tt_srpt->nr_of_srpts;
1351 msg_Dbg( p_demux, "number of titles: %d", i_titles );
1353 for( int i = 0; i < i_titles; i++ )
1355 int32_t i_chapters = 0;
1356 int j;
1358 i_chapters = tt_srpt->title[i].nr_of_ptts;
1359 msg_Dbg( p_demux, "title %d has %d chapters", i, i_chapters );
1361 t = vlc_input_title_New();
1363 for( j = 0; j < __MAX( i_chapters, 1 ); j++ )
1365 s = vlc_seekpoint_New();
1366 TAB_APPEND( t->i_seekpoint, t->seekpoint, s );
1369 TAB_APPEND( p_sys->i_titles, p_sys->titles, t );
1372 #undef tt_srpt