1 /*****************************************************************************
2 * dvd_ifo.c: Functions for ifo parsing
3 *****************************************************************************
4 * Copyright (C) 1999-2001 VideoLAN
5 * $Id: dvd_ifo.c,v 1.34 2001/06/20 07:43:48 sam Exp $
7 * Authors: Stéphane Borel <stef@via.ecp.fr>
8 * German Tischler <tanis@gaspode.franken.de>
11 * - libifo by Thomas Mirlacher <dent@cosy.sbg.ac.at>
12 * - IFO structure documentation by Thomas Mirlacher, Björn Englund,
15 * This program is free software; you can redistribute it and/or modify
16 * it under the terms of the GNU General Public License as published by
17 * the Free Software Foundation; either version 2 of the License, or
18 * (at your option) any later version.
20 * This program is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
25 * You should have received a copy of the GNU General Public License
26 * along with this program; if not, write to the Free Software
27 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
28 *****************************************************************************/
30 /*****************************************************************************
32 *****************************************************************************/
41 #elif defined( _MSC_VER ) && defined( _WIN32 )
48 #include <videolan/dvdcss.h>
57 #include "input_dvd.h"
62 #include "modules_export.h"
64 /*****************************************************************************
66 *****************************************************************************/
67 void CommandRead ( command_desc_t );
68 static int ReadTitle ( ifo_t * , title_t *, int, int );
69 static int FreeTitle ( title_t * );
70 static int ReadUnitInf ( ifo_t * , unit_inf_t *, int, int );
71 static int FreeUnitInf ( unit_inf_t * );
72 static int ReadTitleUnit ( ifo_t * , title_unit_t *, int );
73 static int FreeTitleUnit ( title_unit_t * );
74 static int ReadVobuMap ( ifo_t * , vobu_map_t *, int );
75 static int FreeVobuMap ( vobu_map_t * );
76 static int ReadCellInf ( ifo_t * , cell_inf_t *, int );
77 static int FreeCellInf ( cell_inf_t * );
78 static int FreeTitleSet ( vts_t * );
80 static u8* FillBuffer ( ifo_t *, u8 *, int );
81 static u8 ReadByte ( ifo_t *, u8 *, u8 ** );
82 static void ReadBytes ( ifo_t *, u8 *, u8 **, u8 *, int );
83 static void DumpBytes ( ifo_t *, u8 *, u8 **, int );
84 static u16 ReadWord ( ifo_t *, u8 *, u8 ** );
85 static u32 ReadDouble ( ifo_t *, u8 *, u8 ** );
86 static u64 ReadQuad ( ifo_t *, u8 *, u8 ** );
92 /*****************************************************************************
93 * IfoCreate : Creates an ifo structure and prepares for parsing directly
95 *****************************************************************************/
96 int IfoCreate( thread_dvd_data_t * p_dvd )
98 p_dvd->p_ifo = malloc( sizeof(ifo_t) );
99 if( p_dvd->p_ifo == NULL )
101 intf_ErrMsg( "ifo error: unable to allocate memory. aborting" );
105 /* if we are here the dvd device has already been opened */
106 p_dvd->p_ifo->dvdhandle = p_dvd->dvdhandle;
111 /*****************************************************************************
112 * IfoInit : Reads information from the management table.
113 *****************************************************************************/
114 int IfoInit( ifo_t * p_ifo )
116 u8 p_buf[DVD_LB_SIZE];
122 /* find the start sector of video information on the dvd */
123 p_ifo->i_start = UDFFindFile( p_ifo->dvdhandle, "/VIDEO_TS/VIDEO_TS.IFO" );
125 p_tmp = FillBuffer( p_ifo, p_buf, p_ifo->i_start );
126 //i_start = p_ifo->i_pos;
129 * read the video manager information table
131 #define MGINF p_ifo->vmg.manager_inf
132 //fprintf( stderr, "VMGI\n" );
134 ReadBytes( p_ifo, p_buf, &p_tmp, MGINF.psz_id, 12 );
135 MGINF.psz_id[12] = '\0';
136 MGINF.i_vmg_end_sector = ReadDouble( p_ifo, p_buf, &p_tmp );
137 DumpBytes( p_ifo, p_buf, &p_tmp, 12 );
138 MGINF.i_vmg_inf_end_sector = ReadDouble( p_ifo, p_buf, &p_tmp );
139 DumpBytes( p_ifo, p_buf, &p_tmp, 1 );
140 MGINF.i_spec_ver = ReadByte( p_ifo, p_buf, &p_tmp );
141 MGINF.i_cat = ReadDouble( p_ifo, p_buf, &p_tmp );
142 MGINF.i_volume_nb = ReadWord( p_ifo, p_buf, &p_tmp );
143 MGINF.i_volume = ReadWord( p_ifo, p_buf, &p_tmp );
144 MGINF.i_disc_side = ReadByte( p_ifo, p_buf, &p_tmp );
145 DumpBytes( p_ifo, p_buf, &p_tmp, 19 );
146 MGINF.i_title_set_nb = ReadWord( p_ifo, p_buf, &p_tmp );
147 ReadBytes( p_ifo, p_buf, &p_tmp, MGINF.ps_provider_id, 32 );
148 MGINF.i_pos_code = ReadQuad( p_ifo, p_buf, &p_tmp );
149 DumpBytes( p_ifo, p_buf, &p_tmp, 24 );
150 MGINF.i_vmg_inf_end_byte = ReadDouble( p_ifo, p_buf, &p_tmp );
151 MGINF.i_first_play_title_start_byte = ReadDouble( p_ifo, p_buf, &p_tmp );
152 DumpBytes( p_ifo, p_buf, &p_tmp, 56 );
153 MGINF.i_vob_start_sector = ReadDouble( p_ifo, p_buf, &p_tmp );
154 MGINF.i_title_inf_start_sector = ReadDouble( p_ifo, p_buf, &p_tmp );
155 MGINF.i_title_unit_start_sector = ReadDouble( p_ifo, p_buf, &p_tmp );
156 MGINF.i_parental_inf_start_sector = ReadDouble( p_ifo, p_buf, &p_tmp );
157 MGINF.i_vts_inf_start_sector = ReadDouble( p_ifo, p_buf, &p_tmp );
158 MGINF.i_text_data_start_sector = ReadDouble( p_ifo, p_buf, &p_tmp );
159 MGINF.i_cell_inf_start_sector = ReadDouble( p_ifo, p_buf, &p_tmp );
160 MGINF.i_vobu_map_start_sector = ReadDouble( p_ifo, p_buf, &p_tmp );
161 DumpBytes( p_ifo, p_buf, &p_tmp, 32 );
162 DumpBytes( p_ifo, p_buf, &p_tmp, 2 );
163 DumpBytes( p_ifo, p_buf, &p_tmp, 1 );
164 MGINF.i_audio_nb = ReadByte( p_ifo, p_buf, &p_tmp );
165 //fprintf( stderr, "vmgi audio nb : %d\n", MGINF.i_audio_nb );
167 for( i = 0 ; i < 8 ; i++ )
169 i_temp = ReadQuad( p_ifo, p_buf, &p_tmp );
172 DumpBytes( p_ifo, p_buf, &p_tmp, 17 );
173 MGINF.i_spu_nb = ReadByte( p_ifo, p_buf, &p_tmp );
174 //fprintf( stderr, "vmgi subpic nb : %d\n", MGINF.i_spu_nb );
176 for( i = 0 ; i < MGINF.i_spu_nb ; i++ )
178 ReadBytes( p_ifo, p_buf, &p_tmp, (u8*)(&i_temp), 6 );
179 /* FIXME : take care of endianness */
183 * read first play title.
185 //fprintf(stderr,"readtitle %i\n", MGINF.i_first_play_title_start_byte & 0x7ff );
186 if( ReadTitle( p_ifo, &p_ifo->vmg.title, p_ifo->i_start
187 + OFF2LB( MGINF.i_first_play_title_start_byte ),
188 MGINF.i_first_play_title_start_byte & 0x7ff ) < 0 )
194 * fills the title information structure.
196 #define TITINF p_ifo->vmg.title_inf
197 if( MGINF.i_title_inf_start_sector )
199 p_tmp = FillBuffer( p_ifo, p_buf,
200 p_ifo->i_start + MGINF.i_title_inf_start_sector );
201 //fprintf( stderr, "title inf %d\n", p_ifo->i_pos );
203 TITINF.i_title_nb = ReadWord( p_ifo, p_buf, &p_tmp );
204 //fprintf( stderr, "title_inf: TTU nb %d\n", TITINF.i_title_nb );
205 DumpBytes( p_ifo, p_buf, &p_tmp, 2 );
206 TITINF.i_end_byte = ReadDouble( p_ifo, p_buf, &p_tmp );
208 /* parsing of title attributes */
209 TITINF.p_attr = malloc( TITINF.i_title_nb *sizeof(title_attr_t) );
210 if( TITINF.p_attr == NULL )
212 intf_ErrMsg( "ifo error: out of memory in IfoInit" );
216 for( i = 0 ; i < TITINF.i_title_nb ; i++ )
218 TITINF.p_attr[i].i_play_type = ReadByte( p_ifo, p_buf, &p_tmp );
219 TITINF.p_attr[i].i_angle_nb = ReadByte( p_ifo, p_buf, &p_tmp );
220 TITINF.p_attr[i].i_chapter_nb = ReadWord( p_ifo, p_buf, &p_tmp );
221 TITINF.p_attr[i].i_parental_id = ReadWord( p_ifo, p_buf, &p_tmp );
222 TITINF.p_attr[i].i_title_set_num = ReadByte( p_ifo, p_buf, &p_tmp );
223 TITINF.p_attr[i].i_title_num = ReadByte( p_ifo, p_buf, &p_tmp );
224 TITINF.p_attr[i].i_start_sector = ReadDouble( p_ifo, p_buf, &p_tmp );
225 //fprintf( stderr, "title_inf: %d %d %d\n", TITINF.p_attr[i].i_chapter_nb, TITINF.p_attr[i].i_title_set_num, TITINF.p_attr[i].i_title_num );
230 TITINF.p_attr = NULL;
235 * fills the title unit structure.
237 if( MGINF.i_title_unit_start_sector )
239 if( ReadTitleUnit( p_ifo, &p_ifo->vmg.title_unit, p_ifo->i_start
240 + MGINF.i_title_unit_start_sector ) < 0 )
247 * fills the structure about parental information.
249 #define PARINF p_ifo->vmg.parental_inf
250 if( MGINF.i_parental_inf_start_sector )
252 p_tmp = FillBuffer( p_ifo, p_buf, p_ifo->i_start +
253 MGINF.i_parental_inf_start_sector );
254 i_start = p_ifo->i_pos;
256 //fprintf( stderr, "PTL\n" );
258 PARINF.i_country_nb = ReadWord( p_ifo, p_buf, &p_tmp );
259 PARINF.i_vts_nb = ReadWord( p_ifo, p_buf, &p_tmp );
260 PARINF.i_end_byte = ReadDouble( p_ifo, p_buf, &p_tmp );
262 PARINF.p_parental_desc = malloc( PARINF.i_country_nb
263 * sizeof(parental_desc_t) );
264 if( PARINF.p_parental_desc == NULL )
266 intf_ErrMsg( "ifo error: out of memory in IfoInit" );
270 for( i = 0 ; i < PARINF.i_country_nb ; i++ )
272 ReadBytes( p_ifo, p_buf, &p_tmp,
273 PARINF.p_parental_desc[i].ps_country_code, 2 );
274 DumpBytes( p_ifo, p_buf, &p_tmp, 2 );
275 PARINF.p_parental_desc[i].i_parental_mask_start_byte =
276 ReadWord( p_ifo, p_buf, &p_tmp );
277 DumpBytes( p_ifo, p_buf, &p_tmp, 2 );
280 PARINF.p_parental_mask = malloc( PARINF.i_country_nb
281 * sizeof(parental_mask_t) );
282 if( PARINF.p_parental_mask == NULL )
284 intf_ErrMsg( "ifo erro: out of memory in IfoInit" );
288 for( i = 0 ; i < PARINF.i_country_nb ; i++ )
290 p_tmp = FillBuffer( p_ifo, p_buf, i_start + OFF2LB(
291 PARINF.p_parental_desc[i].i_parental_mask_start_byte ) )
292 + (PARINF.p_parental_desc[i].i_parental_mask_start_byte & 0x7ff);
294 for( j = 0 ; j < 8 ; j++ )
296 PARINF.p_parental_mask[i].ppi_mask[j] =
297 malloc( ( PARINF.i_vts_nb + 1 ) * sizeof(u16) );
299 if( PARINF.p_parental_mask[i].ppi_mask[j] == NULL )
301 intf_ErrMsg( "ifo error: out of memory in IfoInit" );
305 for( k = 0 ; k < PARINF.i_vts_nb + 1 ; k++ )
307 PARINF.p_parental_mask[i].ppi_mask[j][k] =
308 ReadWord( p_ifo, p_buf, &p_tmp );
316 * information and attributes about for each vts.
318 #define VTSINF p_ifo->vmg.vts_inf
319 if( MGINF.i_vts_inf_start_sector )
323 p_tmp = FillBuffer( p_ifo, p_buf, p_ifo->i_start +
324 MGINF.i_vts_inf_start_sector );
325 i_start = p_ifo->i_pos;
327 //fprintf( stderr, "VTS ATTR\n" );
329 VTSINF.i_vts_nb = ReadWord( p_ifo, p_buf, &p_tmp );;
330 //fprintf( stderr, "VTS ATTR Nb: %d\n", VTSINF.i_vts_nb );
331 DumpBytes( p_ifo, p_buf, &p_tmp, 2 );
332 VTSINF.i_end_byte = ReadDouble( p_ifo, p_buf, &p_tmp );
333 VTSINF.pi_vts_attr_start_byte =
334 malloc( VTSINF.i_vts_nb * sizeof(u32) );
335 if( VTSINF.pi_vts_attr_start_byte == NULL )
337 intf_ErrMsg( "ifo error: out of memory in IfoInit" );
341 for( i = 0 ; i < VTSINF.i_vts_nb ; i++ )
343 VTSINF.pi_vts_attr_start_byte[i] =
344 ReadDouble( p_ifo, p_buf, &p_tmp );
347 VTSINF.p_vts_attr = malloc( VTSINF.i_vts_nb * sizeof(vts_attr_t) );
348 if( VTSINF.p_vts_attr == NULL )
350 intf_ErrMsg( "ifo erro: out of memory in IfoInit" );
354 for( i = 0 ; i < VTSINF.i_vts_nb ; i++ )
356 p_tmp = FillBuffer( p_ifo, p_buf, i_start +
357 OFF2LB( VTSINF.pi_vts_attr_start_byte[i] ) )
358 + ( VTSINF.pi_vts_attr_start_byte[i] & 0x7ff );
360 VTSINF.p_vts_attr[i].i_end_byte =
361 ReadDouble( p_ifo, p_buf, &p_tmp );
362 VTSINF.p_vts_attr[i].i_cat_app_type =
363 ReadDouble( p_ifo, p_buf, &p_tmp );
364 DumpBytes( p_ifo, p_buf, &p_tmp, 2 );
365 DumpBytes( p_ifo, p_buf, &p_tmp, 1 );
366 VTSINF.p_vts_attr[i].i_vts_menu_audio_nb =
367 ReadByte( p_ifo, p_buf, &p_tmp );
368 //fprintf( stderr, "m audio nb : %d\n", VTSINF.p_vts_attr[i].i_vts_menu_audio_nb );
370 for( j = 0 ; j < 8 ; j++ )
372 i_temp = ReadQuad( p_ifo, p_buf, &p_tmp );
375 DumpBytes( p_ifo, p_buf, &p_tmp, 17 );
376 VTSINF.p_vts_attr[i].i_vts_menu_spu_nb =
377 ReadByte( p_ifo, p_buf, &p_tmp );
378 //fprintf( stderr, "m subp nb : %d\n", VTSINF.p_vts_attr[i].i_vts_menu_spu_nb );
380 for( j = 0 ; j < 28 ; j++ )
382 /* FIXME : Fix endianness issue here */
383 ReadBytes( p_ifo, p_buf, &p_tmp, (u8*)(&i_temp), 6 );
386 DumpBytes( p_ifo, p_buf, &p_tmp, 2 );
387 DumpBytes( p_ifo, p_buf, &p_tmp, 2 );
388 DumpBytes( p_ifo, p_buf, &p_tmp, 1 );
389 VTSINF.p_vts_attr[i].i_vts_title_audio_nb =
390 ReadDouble( p_ifo, p_buf, &p_tmp );
391 //fprintf( stderr, "tt audio nb : %d\n", VTSINF.p_vts_attr[i].i_vts_title_audio_nb );
393 for( j = 0 ; j < 8 ; j++ )
395 i_temp = ReadQuad( p_ifo, p_buf, &p_tmp );;
398 DumpBytes( p_ifo, p_buf, &p_tmp, 17 );
399 VTSINF.p_vts_attr[i].i_vts_title_spu_nb =
400 ReadByte( p_ifo, p_buf, &p_tmp );
401 //fprintf( stderr, "tt subp nb : %d\n", VTSINF.p_vts_attr[i].i_vts_title_spu_nb );
403 for( j = 0 ; j < 28 /*VTSINF.p_vts_vts_inf[i].i_vtstt_subpic_nb*/ ; j++ )
405 /* FIXME : Fix endianness issue here */
406 ReadBytes( p_ifo, p_buf, &p_tmp, (u8*)(&i_temp), 6 );
415 if( MGINF.i_cell_inf_start_sector )
417 if( ReadCellInf( p_ifo, &p_ifo->vmg.cell_inf, p_ifo->i_start +
418 MGINF.i_cell_inf_start_sector ) < 0 )
425 * global vob unit map.
427 if( MGINF.i_vobu_map_start_sector )
429 if( ReadVobuMap( p_ifo, &p_ifo->vmg.vobu_map, p_ifo->i_start +
430 MGINF.i_vobu_map_start_sector ) < 0 )
437 p_ifo->vts.b_initialized = 0;
439 intf_WarnMsg( 2, "ifo info: vmg initialized" );
444 /*****************************************************************************
445 * IfoTitleSet: Parse vts*.ifo files to fill the Video Title Set structure.
446 *****************************************************************************/
447 int IfoTitleSet( ifo_t * p_ifo )
449 u8 p_buf[DVD_LB_SIZE];
457 if( p_ifo->vts.b_initialized )
459 FreeTitleSet( &p_ifo->vts );
462 i_off = p_ifo->vmg.title_inf.p_attr[p_ifo->i_title-1].i_start_sector
465 //fprintf(stderr, "offset: %d\n" , i_off );
467 p_tmp = FillBuffer( p_ifo, p_buf, i_off );
468 //i_start = p_ifo->i_pos;
469 p_ifo->vts.i_pos = p_ifo->i_pos;
471 #define MGINF p_ifo->vts.manager_inf
474 * read manager information
476 //fprintf( stderr, "VTSI\n" );
478 ReadBytes( p_ifo, p_buf, &p_tmp, MGINF.psz_id , 12 );
479 MGINF.psz_id[12] = '\0';
480 MGINF.i_end_sector = ReadDouble( p_ifo, p_buf, &p_tmp );
481 DumpBytes( p_ifo, p_buf, &p_tmp, 12 );
482 MGINF.i_inf_end_sector = ReadDouble( p_ifo, p_buf, &p_tmp );
483 DumpBytes( p_ifo, p_buf, &p_tmp, 1 );
484 MGINF.i_spec_ver = ReadByte( p_ifo, p_buf, &p_tmp );
485 MGINF.i_cat = ReadDouble( p_ifo, p_buf, &p_tmp );
486 DumpBytes( p_ifo, p_buf, &p_tmp, 90 );
487 MGINF.i_inf_end_byte = ReadDouble( p_ifo, p_buf, &p_tmp );
488 DumpBytes( p_ifo, p_buf, &p_tmp, 60 );
489 MGINF.i_menu_vob_start_sector = ReadDouble( p_ifo, p_buf, &p_tmp );
490 MGINF.i_title_vob_start_sector = ReadDouble( p_ifo, p_buf, &p_tmp );
491 MGINF.i_title_inf_start_sector = ReadDouble( p_ifo, p_buf, &p_tmp );
492 MGINF.i_title_unit_start_sector = ReadDouble( p_ifo, p_buf, &p_tmp );
493 MGINF.i_menu_unit_start_sector = ReadDouble( p_ifo, p_buf, &p_tmp );
494 MGINF.i_time_inf_start_sector = ReadDouble( p_ifo, p_buf, &p_tmp );
495 MGINF.i_menu_cell_inf_start_sector = ReadDouble( p_ifo, p_buf, &p_tmp );
496 MGINF.i_menu_vobu_map_start_sector = ReadDouble( p_ifo, p_buf, &p_tmp );
497 MGINF.i_cell_inf_start_sector = ReadDouble( p_ifo, p_buf, &p_tmp );
498 MGINF.i_vobu_map_start_sector = ReadDouble( p_ifo, p_buf, &p_tmp );
499 DumpBytes( p_ifo, p_buf, &p_tmp, 24 );
500 DumpBytes( p_ifo, p_buf, &p_tmp, 2 );
501 DumpBytes( p_ifo, p_buf, &p_tmp, 1 );
502 MGINF.i_menu_audio_nb = ReadByte( p_ifo, p_buf, &p_tmp );
504 for( i = 0 ; i < 8 ; i++ )
506 i_temp = ReadQuad( p_ifo, p_buf, &p_tmp );
509 DumpBytes( p_ifo, p_buf, &p_tmp, 17 );
510 MGINF.i_menu_spu_nb = ReadByte( p_ifo, p_buf, &p_tmp );
512 for( i = 0 ; i < 28 ; i++ )
514 /* FIXME : take care of endianness */
515 ReadBytes( p_ifo, p_buf, &p_tmp, (u8*)(&i_temp), 6 );
518 DumpBytes( p_ifo, p_buf, &p_tmp, 2 );
520 i_short = ReadWord( p_ifo, p_buf, &p_tmp );
522 MGINF.video_attr.i_mode = i_short & 0x1;
524 MGINF.video_attr.i_letterboxed = i_short & 0x1;
526 MGINF.video_attr.i_source_res = i_short & 0x3;
528 MGINF.video_attr.i_line21_2 = i_short & 0x1;
530 MGINF.video_attr.i_line21_1 = i_short & 0x1;
532 MGINF.video_attr.i_perm_displ = i_short & 0x3;
534 MGINF.video_attr.i_ratio = i_short & 0x3;
536 MGINF.video_attr.i_system = i_short & 0x3;
538 MGINF.video_attr.i_compression = i_short & 0x3;
540 DumpBytes( p_ifo, p_buf, &p_tmp, 1 );
541 MGINF.i_audio_nb = ReadByte( p_ifo, p_buf, &p_tmp );
542 //fprintf( stderr, "vtsi audio nb : %d\n", MGINF.i_audio_nb );
544 for( i = 0 ; i < 8 ; i++ )
546 i_temp = ReadQuad( p_ifo, p_buf, &p_tmp );
547 //fprintf( stderr, "Audio %d: %llx\n", i, i_temp );
549 MGINF.p_audio_attr[i].i_bar = i_temp & 0xff;
551 MGINF.p_audio_attr[i].i_caption = i_temp & 0xff;
553 MGINF.p_audio_attr[i].i_foo = i_temp & 0xff;
555 MGINF.p_audio_attr[i].i_lang_code = i_temp & 0xffff;
557 MGINF.p_audio_attr[i].i_num_channels = i_temp & 0x7;
559 MGINF.p_audio_attr[i].i_test = i_temp & 0x1;
561 MGINF.p_audio_attr[i].i_sample_freq = i_temp & 0x3;
563 MGINF.p_audio_attr[i].i_quantization = i_temp & 0x3;
565 MGINF.p_audio_attr[i].i_appl_mode = i_temp & 0x3;
567 MGINF.p_audio_attr[i].i_type = i_temp & 0x3;
569 MGINF.p_audio_attr[i].i_multichannel_extension = i_temp & 0x1;
571 MGINF.p_audio_attr[i].i_coding_mode = i_temp & 0x7;
574 DumpBytes( p_ifo, p_buf, &p_tmp, 17 );
575 MGINF.i_spu_nb = ReadByte( p_ifo, p_buf, &p_tmp );
576 //fprintf( stderr, "vtsi subpic nb : %d\n", MGINF.i_spu_nb );
578 for( i=0 ; i<MGINF.i_spu_nb ; i++ )
580 ReadBytes( p_ifo, p_buf, &p_tmp, (u8*)(&i_temp), 6 );
581 i_temp = hton64( i_temp ) >> 16;
582 //fprintf( stderr, "Subpic %d: %llx\n", i, i_temp );
583 MGINF.p_spu_attr[i].i_caption = i_temp & 0xff;
585 MGINF.p_spu_attr[i].i_foo = i_temp & 0xff;
587 MGINF.p_spu_attr[i].i_lang_code = i_temp & 0xffff;
589 MGINF.p_spu_attr[i].i_prefix = i_temp & 0xffff;
593 * read title information: set of pointers to title
595 #define TITINF p_ifo->vts.title_inf
596 if( MGINF.i_title_inf_start_sector )
598 p_tmp = FillBuffer( p_ifo, p_buf, p_ifo->vts.i_pos +
599 MGINF.i_title_inf_start_sector );
601 i_start = p_ifo->i_pos;
603 //fprintf( stderr, "VTS PTR\n" );
605 TITINF.i_title_nb = ReadWord( p_ifo, p_buf, &p_tmp );
606 //fprintf( stderr, "VTS title_inf nb: %d\n", TITINF.i_title_nb );
607 DumpBytes( p_ifo, p_buf, &p_tmp, 2 );
608 TITINF.i_end_byte = ReadDouble( p_ifo, p_buf, &p_tmp );
610 TITINF.pi_start_byte = malloc( TITINF.i_title_nb * sizeof(u32) );
611 if( TITINF.pi_start_byte == NULL )
613 intf_ErrMsg( "ifo error: out of memory in IfoTitleSet" );
617 for( i = 0 ; i < TITINF.i_title_nb ; i++ )
619 TITINF.pi_start_byte[i] = ReadDouble( p_ifo, p_buf, &p_tmp );
623 TITINF.p_title_start = malloc( TITINF.i_title_nb
624 * sizeof(title_start_t) );
625 if( TITINF.p_title_start == NULL )
627 intf_ErrMsg( "ifo error: out of memory in IfoTitleSet" );
631 for( i = 0 ; i < TITINF.i_title_nb ; i++ )
633 p_tmp = FillBuffer( p_ifo, p_buf, i_start +
634 OFF2LB( TITINF.pi_start_byte[i] ) )
635 + (TITINF.pi_start_byte[i] & 0x7ff);
637 TITINF.p_title_start[i].i_title_id =
638 ReadWord( p_ifo, p_buf, &p_tmp );
639 TITINF.p_title_start[i].i_chapter =
640 ReadWord( p_ifo, p_buf, &p_tmp );
646 * menu unit information
648 if( MGINF.i_menu_unit_start_sector )
650 if( ReadTitleUnit( p_ifo, &p_ifo->vts.menu_unit, p_ifo->vts.i_pos +
651 MGINF.i_menu_unit_start_sector ) < 0 )
658 * title unit information
660 if( MGINF.i_title_unit_start_sector )
662 if( ReadUnitInf( p_ifo, &p_ifo->vts.title_unit, p_ifo->vts.i_pos +
663 MGINF.i_title_unit_start_sector, 0 ) < 0 )
670 * time map information
672 #define TIMINF p_ifo->vts.time_inf
673 if( MGINF.i_time_inf_start_sector )
675 u8 p_buf[DVD_LB_SIZE];
677 p_tmp = FillBuffer( p_ifo, p_buf, p_ifo->vts.i_pos +
678 MGINF.i_time_inf_start_sector );
680 //fprintf( stderr, "TMAP\n" );
682 TIMINF.i_nb = ReadWord( p_ifo, p_buf, &p_tmp );;
683 DumpBytes( p_ifo, p_buf, &p_tmp, 2 );
684 TIMINF.i_end_byte = ReadDouble( p_ifo, p_buf, &p_tmp );
686 TIMINF.pi_start_byte = malloc( TIMINF.i_nb * sizeof(u32) );
687 if( TIMINF.pi_start_byte == NULL )
689 intf_ErrMsg( "ifo error: out of memory in IfoTitleSet" );
693 for( i = 0 ; i < TIMINF.i_nb ; i++ )
695 TIMINF.pi_start_byte[i] = ReadDouble( p_ifo, p_buf, &p_tmp );
698 TIMINF.p_time_map = malloc( TIMINF.i_nb * sizeof(time_map_t) );
699 if( TIMINF.p_time_map == NULL )
701 intf_ErrMsg( "ifo error: out of memory in IfoTitleSet" );
705 for( i = 0 ; i < TIMINF.i_nb ; i++ )
707 TIMINF.p_time_map[i].i_time_unit = ReadByte( p_ifo, p_buf, &p_tmp );
708 DumpBytes( p_ifo, p_buf, &p_tmp, 1 );
709 TIMINF.p_time_map[i].i_entry_nb = ReadWord( p_ifo, p_buf, &p_tmp );
711 TIMINF.p_time_map[i].pi_sector =
712 malloc( TIMINF.p_time_map[i].i_entry_nb * sizeof(u32) );
713 if( TIMINF.p_time_map[i].pi_sector == NULL )
715 intf_ErrMsg( "ifo error: out of memory in IfoTitleSet" );
719 for( j = 0 ; j < TIMINF.p_time_map[i].i_entry_nb ; j++ )
721 TIMINF.p_time_map[i].pi_sector[j] =
722 ReadDouble( p_ifo, p_buf, &p_tmp );
728 if( MGINF.i_menu_cell_inf_start_sector
729 && ReadCellInf( p_ifo, &p_ifo->vts.menu_cell_inf, p_ifo->vts.i_pos +
730 MGINF.i_menu_cell_inf_start_sector ) < 0 )
735 if( MGINF.i_menu_vobu_map_start_sector
736 && ReadVobuMap( p_ifo, &p_ifo->vts.menu_vobu_map, p_ifo->vts.i_pos +
737 MGINF.i_menu_vobu_map_start_sector ) < 0 )
742 if( MGINF.i_cell_inf_start_sector
743 && ReadCellInf( p_ifo, &p_ifo->vts.cell_inf, p_ifo->vts.i_pos +
744 MGINF.i_cell_inf_start_sector ) )
749 if( MGINF.i_vobu_map_start_sector
750 && ReadVobuMap( p_ifo, &p_ifo->vts.vobu_map, p_ifo->vts.i_pos +
751 MGINF.i_vobu_map_start_sector ) )
757 intf_WarnMsg( 2, "ifo info: vts %d initialized",
758 p_ifo->vmg.title_inf.p_attr[p_ifo->i_title-1].i_title_set_num );
760 p_ifo->vts.b_initialized = 1;
765 /*****************************************************************************
766 * FreeTitleSet : free all structures allocated by IfoTitleSet
767 *****************************************************************************/
768 static int FreeTitleSet( vts_t * p_vts )
772 if( p_vts->manager_inf.i_vobu_map_start_sector )
774 FreeVobuMap( &p_vts->vobu_map );
777 if( p_vts->manager_inf.i_cell_inf_start_sector )
779 FreeCellInf( &p_vts->cell_inf );
782 if( p_vts->manager_inf.i_menu_vobu_map_start_sector )
784 FreeVobuMap( &p_vts->menu_vobu_map );
787 if( p_vts->manager_inf.i_menu_cell_inf_start_sector )
789 FreeCellInf( &p_vts->menu_cell_inf );
792 if( p_vts->manager_inf.i_time_inf_start_sector )
794 for( i = 0 ; i < p_vts->time_inf.i_nb ; i++ )
796 free( p_vts->time_inf.p_time_map[i].pi_sector );
799 free( p_vts->time_inf.p_time_map );
800 free( p_vts->time_inf.pi_start_byte );
803 if( p_vts->manager_inf.i_title_unit_start_sector )
805 FreeUnitInf( &p_vts->title_unit );
808 if( p_vts->manager_inf.i_menu_unit_start_sector )
810 FreeTitleUnit( &p_vts->menu_unit );
813 if( p_vts->manager_inf.i_title_inf_start_sector )
815 free( p_vts->title_inf.pi_start_byte );
816 free( p_vts->title_inf.p_title_start );
819 p_vts->b_initialized = 0;
824 /*****************************************************************************
825 * IfoDestroy : Frees all the memory allocated to ifo structures
826 *****************************************************************************/
827 void IfoDestroy( ifo_t * p_ifo )
831 FreeTitleSet( &p_ifo->vts );
833 if( p_ifo->vmg.manager_inf.i_vobu_map_start_sector )
835 FreeVobuMap( &p_ifo->vmg.vobu_map );
838 if( p_ifo->vmg.manager_inf.i_cell_inf_start_sector )
840 FreeCellInf( &p_ifo->vmg.cell_inf );
843 if( p_ifo->vmg.manager_inf.i_vts_inf_start_sector )
845 free( p_ifo->vmg.vts_inf.p_vts_attr );
846 free( p_ifo->vmg.vts_inf.pi_vts_attr_start_byte );
849 /* free parental information structures */
850 if( p_ifo->vmg.manager_inf.i_parental_inf_start_sector )
852 for( i = 0 ; i < p_ifo->vmg.parental_inf.i_country_nb ; i++ )
854 for( j = 0 ; j < 8 ; j++ )
856 free( p_ifo->vmg.parental_inf.p_parental_mask[i].ppi_mask[j] );
860 free( p_ifo->vmg.parental_inf.p_parental_mask );
861 free( p_ifo->vmg.parental_inf.p_parental_desc );
864 if( p_ifo->vmg.manager_inf.i_title_unit_start_sector )
866 FreeTitleUnit( &p_ifo->vmg.title_unit );
869 if( p_ifo->vmg.manager_inf.i_title_inf_start_sector )
871 free( p_ifo->vmg.title_inf.p_attr );
874 FreeTitle( &p_ifo->vmg.title );
882 * Function common to Video Manager and Video Title set Processing
885 /*****************************************************************************
886 * ReadTitle : Fills the title structure.
887 *****************************************************************************
888 * Titles are logical stream units that correspond to a whole inside the dvd.
889 * Several title can point to the same part of the physical DVD, and give
890 * map to different anglesfor instance.
891 *****************************************************************************/
892 static int ReadTitle( ifo_t * p_ifo, title_t * p_title, int i_block, int i_bytes )
894 u8 p_buf[DVD_LB_SIZE];
901 p_tmp = FillBuffer( p_ifo, p_buf, i_block ) + i_bytes;
903 i_start = p_ifo->i_pos;
905 //fprintf( stderr, "PGC @ %d + %d\n", p_ifo->i_pos, i_bytes );
907 DumpBytes( p_ifo, p_buf, &p_tmp, 2);
908 p_title->i_chapter_nb = ReadByte( p_ifo, p_buf, &p_tmp );
909 p_title->i_cell_nb = ReadByte( p_ifo, p_buf, &p_tmp );
910 //fprintf( stderr, "title: Prg: %d Cell: %d\n",p_title->i_chapter_nb,p_title->i_cell_nb );
911 p_title->i_play_time = ReadDouble( p_ifo, p_buf, &p_tmp );
912 p_title->i_prohibited_user_op = ReadDouble( p_ifo, p_buf, &p_tmp );
914 for( i = 0 ; i < 8 ; i++ )
916 i_audio = ReadWord( p_ifo, p_buf, &p_tmp );
917 p_title->pi_audio_status[i].i_foo = i_audio & 0xff;
919 p_title->pi_audio_status[i].i_position = i_audio & 0x07;
921 p_title->pi_audio_status[i].i_available = i_audio;
924 for( i = 0 ; i < 32 ; i++ )
926 i_spu = ReadDouble( p_ifo, p_buf, &p_tmp );
927 p_title->pi_spu_status[i].i_position_pan = i_spu & 0x1f;
929 p_title->pi_spu_status[i].i_position_letter = i_spu & 0x1f;
931 p_title->pi_spu_status[i].i_position_wide = i_spu & 0x1f;
933 p_title->pi_spu_status[i].i_position_43 = i_spu & 0x1f;
935 p_title->pi_spu_status[i].i_available = i_spu;
938 p_title->i_next_title_num = ReadWord( p_ifo, p_buf, &p_tmp );
939 p_title->i_prev_title_num = ReadWord( p_ifo, p_buf, &p_tmp );
940 p_title->i_go_up_title_num = ReadWord( p_ifo, p_buf, &p_tmp );
941 p_title->i_still_time = ReadByte( p_ifo, p_buf, &p_tmp );
942 p_title->i_play_mode = ReadByte( p_ifo, p_buf, &p_tmp );
944 for( i = 0 ; i < 16 ; i++ )
946 /* FIXME : We have to erase the extra bit */
947 p_title->pi_yuv_color[i] = ReadDouble( p_ifo, p_buf, &p_tmp );
950 p_title->i_command_start_byte = ReadWord( p_ifo, p_buf, &p_tmp );
951 p_title->i_chapter_map_start_byte = ReadWord( p_ifo, p_buf, &p_tmp );
952 p_title->i_cell_play_start_byte = ReadWord( p_ifo, p_buf, &p_tmp );
953 p_title->i_cell_pos_start_byte = ReadWord( p_ifo, p_buf, &p_tmp );
955 /* parsing of command_t */
956 if( p_title->i_command_start_byte )
958 p_tmp = FillBuffer( p_ifo, p_buf, i_start +
959 OFF2LB( p_title->i_command_start_byte + i_bytes ) )
960 + ( (p_title->i_command_start_byte + i_bytes) & 0x7ff );
963 p_title->command.i_pre_command_nb = ReadWord( p_ifo, p_buf, &p_tmp );
964 p_title->command.i_post_command_nb = ReadWord( p_ifo, p_buf, &p_tmp );
965 p_title->command.i_cell_command_nb = ReadWord( p_ifo, p_buf, &p_tmp );
966 DumpBytes( p_ifo, p_buf, &p_tmp, 2 );
968 /* pre-title commands */
969 if( p_title->command.i_pre_command_nb )
971 p_title->command.p_pre_command =
972 malloc( p_title->command.i_pre_command_nb
973 * sizeof(command_desc_t) );
975 if( p_title->command.p_pre_command == NULL )
977 intf_ErrMsg( "ifo error: out of memory in ReadTitle" );
981 for( i = 0 ; i < p_title->command.i_pre_command_nb ; i++ )
983 p_title->command.p_pre_command[i] =
984 ReadQuad( p_ifo, p_buf, &p_tmp );
989 p_title->command.p_pre_command = NULL;
992 /* post-title commands */
993 if( p_title->command.i_post_command_nb )
995 p_title->command.p_post_command =
996 malloc( p_title->command.i_post_command_nb
997 * sizeof(command_desc_t) );
999 if( p_title->command.p_post_command == NULL )
1001 intf_ErrMsg( "ifo error: out of memory in ReadTitle" );
1005 for( i = 0 ; i < p_title->command.i_post_command_nb ; i++ )
1007 p_title->command.p_post_command[i] =
1008 ReadQuad( p_ifo, p_buf, &p_tmp );
1013 p_title->command.p_post_command = NULL;
1017 if( p_title->command.i_cell_command_nb )
1019 p_title->command.p_cell_command =
1020 malloc( p_title->command.i_cell_command_nb
1021 * sizeof(command_desc_t) );
1023 if( p_title->command.p_cell_command == NULL )
1025 intf_ErrMsg( "ifo error: out of memory in ReadTitle" );
1029 for( i = 0 ; i < p_title->command.i_cell_command_nb ; i++ )
1031 p_title->command.p_cell_command[i] =
1032 ReadQuad( p_ifo, p_buf, &p_tmp );
1037 p_title->command.p_cell_command = NULL;
1041 /* parsing of chapter_map_t: it gives the entry cell for each chapter */
1042 if( p_title->i_chapter_map_start_byte )
1044 p_ifo->i_pos = dvdcss_seek( p_ifo->dvdhandle,
1045 OFF2LB( i_start + p_title->i_chapter_map_start_byte ) );
1047 p_title->chapter_map.pi_start_cell =
1048 malloc( p_title->i_chapter_nb * sizeof(chapter_map_t) );
1050 if( p_title->chapter_map.pi_start_cell == NULL )
1052 intf_ErrMsg( "ifo error: out of memory in Read Title" );
1056 ReadBytes( p_ifo, p_buf, &p_tmp, p_title->chapter_map.pi_start_cell,
1057 p_title->i_chapter_nb );
1061 p_title->chapter_map.pi_start_cell = NULL;
1064 /* parsing of cell_play_t */
1065 if( p_title->i_cell_play_start_byte )
1067 p_tmp = FillBuffer( p_ifo, p_buf, i_start +
1068 OFF2LB( p_title->i_cell_play_start_byte+i_bytes ) )
1069 + ( (p_title->i_cell_play_start_byte+i_bytes) & 0x7ff );
1071 p_title->p_cell_play = malloc( p_title->i_cell_nb
1072 * sizeof(cell_play_t) );
1074 if( p_title->p_cell_play == NULL )
1076 intf_ErrMsg( "ifo error: out of memory in ReadTitle" );
1080 for( i = 0 ; i < p_title->i_cell_nb ; i++ )
1082 #define PLAY p_title->p_cell_play[i]
1083 PLAY.i_category = ReadWord( p_ifo, p_buf, &p_tmp );
1084 PLAY.i_still_time = ReadByte( p_ifo, p_buf, &p_tmp );
1085 PLAY.i_command_nb = ReadByte( p_ifo, p_buf, &p_tmp );
1086 PLAY.i_play_time = ReadDouble( p_ifo, p_buf, &p_tmp );
1087 PLAY.i_start_sector = ReadDouble( p_ifo, p_buf, &p_tmp );
1088 PLAY.i_first_ilvu_vobu_esector = ReadDouble( p_ifo, p_buf, &p_tmp );
1089 PLAY.i_last_vobu_start_sector = ReadDouble( p_ifo, p_buf, &p_tmp );
1090 PLAY.i_end_sector = ReadDouble( p_ifo, p_buf, &p_tmp );
1095 /* Parsing of cell_pos_t */
1096 if( p_title->i_cell_pos_start_byte )
1098 p_tmp = FillBuffer( p_ifo, p_buf, i_start +
1099 OFF2LB( p_title->i_cell_pos_start_byte + i_bytes ) )
1100 + ( (p_title->i_cell_pos_start_byte + i_bytes) & 0x7ff );
1102 p_title->p_cell_pos = malloc( p_title->i_cell_nb
1103 * sizeof(cell_pos_t) );
1105 if( p_title->p_cell_pos == NULL )
1107 intf_ErrMsg( "ifo error: out of memory" );
1111 for( i = 0 ; i < p_title->i_cell_nb ; i++ )
1113 p_title->p_cell_pos[i].i_vob_id = ReadWord( p_ifo, p_buf, &p_tmp );
1114 DumpBytes( p_ifo, p_buf, &p_tmp, 1 );
1115 p_title->p_cell_pos[i].i_cell_id = ReadByte( p_ifo, p_buf, &p_tmp );
1122 /*****************************************************************************
1123 * FreeTitle: frees alla structure allocated by a call to ReadTitle
1124 *****************************************************************************/
1125 static int FreeTitle( title_t * p_title )
1127 if( p_title->i_command_start_byte )
1129 if( p_title->command.i_pre_command_nb )
1131 free( p_title->command.p_pre_command );
1134 if( p_title->command.i_post_command_nb )
1136 free( p_title->command.p_post_command );
1139 if( p_title->command.i_cell_command_nb )
1141 free( p_title->command.p_cell_command );
1144 if( p_title->i_chapter_map_start_byte )
1146 free( p_title->chapter_map.pi_start_cell );
1149 if( p_title->i_cell_play_start_byte )
1151 free( p_title->p_cell_play );
1154 if( p_title->i_cell_pos_start_byte )
1156 free( p_title->p_cell_pos );
1163 /*****************************************************************************
1164 * ReadUnitInf : Fills Menu Language Unit Table/ PGC Info Table
1165 *****************************************************************************/
1166 static int ReadUnitInf( ifo_t * p_ifo, unit_inf_t * p_unit_inf,
1167 int i_block, int i_bytes )
1169 u8 p_buf[DVD_LB_SIZE];
1174 p_tmp = FillBuffer( p_ifo, p_buf, i_block ) + i_bytes;
1176 i_start = p_ifo->i_pos;
1177 //fprintf( stderr, "Unit\n" );
1179 p_unit_inf->i_title_nb = ReadWord( p_ifo, p_buf, &p_tmp );
1180 //fprintf( stderr, "Unit nb: %d\n", p_unit_inf->i_title_nb );
1181 DumpBytes( p_ifo, p_buf, &p_tmp, 2 );
1182 p_unit_inf->i_end_byte = ReadDouble( p_ifo, p_buf, &p_tmp );
1184 p_unit_inf->p_title =
1185 malloc( p_unit_inf->i_title_nb * sizeof(unit_title_t) );
1186 if( p_unit_inf->p_title == NULL )
1188 intf_ErrMsg( "ifo error: out of memory in ReadUnit" );
1192 for( i = 0 ; i < p_unit_inf->i_title_nb ; i++ )
1194 #define TITLE p_unit_inf->p_title[i]
1195 TITLE.i_category_mask = ReadByte( p_ifo, p_buf, &p_tmp );
1196 TITLE.i_category = ReadByte( p_ifo, p_buf, &p_tmp );
1197 //fprintf( stderr, "cat mask %d: %x cat %x\n", i, TITLE.i_category_mask, TITLE.i_category );
1198 TITLE.i_parental_mask = ReadWord( p_ifo, p_buf, &p_tmp );
1199 TITLE.i_title_start_byte = ReadDouble( p_ifo, p_buf, &p_tmp );
1203 for( i = 0 ; i < p_unit_inf->i_title_nb ; i++ )
1205 //fprintf( stderr, "Unit: PGC %d @ %d\n", i, p_ifo->i_pos );
1206 ReadTitle( p_ifo, &p_unit_inf->p_title[i].title, i_start +
1207 OFF2LB( p_unit_inf->p_title[i].i_title_start_byte + i_bytes ),
1208 (p_unit_inf->p_title[i].i_title_start_byte+i_bytes) & 0x7ff );
1214 /*****************************************************************************
1215 * FreeUnitInf : frees a structure allocated by ReadUnit
1216 *****************************************************************************/
1217 static int FreeUnitInf( unit_inf_t * p_unit_inf )
1219 if( p_unit_inf->p_title != NULL )
1221 free( p_unit_inf->p_title );
1228 /*****************************************************************************
1229 * ReadTitleUnit: Fills the Title Unit structure.
1230 *****************************************************************************/
1231 static int ReadTitleUnit( ifo_t * p_ifo, title_unit_t * p_title_unit,
1234 u8 p_buf[DVD_LB_SIZE];
1239 p_tmp = FillBuffer( p_ifo, p_buf, i_block );
1240 i_start = p_ifo->i_pos;
1241 //fprintf( stderr, "Unit Table\n" );
1243 p_title_unit->i_unit_nb = ReadWord( p_ifo, p_buf, &p_tmp );
1244 DumpBytes( p_ifo, p_buf, &p_tmp, 2 );
1245 p_title_unit->i_end_byte = ReadDouble( p_ifo, p_buf, &p_tmp );
1247 //fprintf(stderr, "Unit: nb %d end %d\n", p_title_unit->i_unit_nb, p_title_unit->i_end_byte );
1249 p_title_unit->p_unit = malloc( p_title_unit->i_unit_nb * sizeof(unit_t) );
1250 if( p_title_unit->p_unit == NULL )
1252 intf_ErrMsg( "ifo error: out of memory in ReadTitleUnit" );
1256 for( i = 0 ; i < p_title_unit->i_unit_nb ; i++ )
1258 //ReadBytes( p_ifo, p_buf, &p_tmp, p_title_unit->p_unit[i].ps_lang_code, 2 );
1259 p_title_unit->p_unit[i].i_lang_code = ReadWord( p_ifo, p_buf, &p_tmp );
1260 //fprintf( stderr, "lang %d %x\n", i,p_title_unit->p_unit[i].i_lang_code );
1261 DumpBytes( p_ifo, p_buf, &p_tmp, 1 );
1262 p_title_unit->p_unit[i].i_existence_mask =
1263 ReadByte( p_ifo, p_buf, &p_tmp );
1264 p_title_unit->p_unit[i].i_unit_inf_start_byte =
1265 ReadDouble( p_ifo, p_buf, &p_tmp );
1268 p_title_unit->p_unit_inf =
1269 malloc( p_title_unit->i_unit_nb * sizeof(unit_inf_t) );
1270 if( p_title_unit->p_unit_inf == NULL )
1272 intf_ErrMsg( "ifo error: out of memory in ReadTitleUnit" );
1276 for( i = 0 ; i < p_title_unit->i_unit_nb ; i++ )
1278 ReadUnitInf( p_ifo, &p_title_unit->p_unit_inf[i], i_start +
1279 OFF2LB( p_title_unit->p_unit[i].i_unit_inf_start_byte ),
1280 p_title_unit->p_unit[i].i_unit_inf_start_byte & 0x7ff );
1286 /*****************************************************************************
1287 * FreeTitleUnit: frees a structure allocateed by ReadTitleUnit
1288 *****************************************************************************/
1289 static int FreeTitleUnit( title_unit_t * p_title_unit )
1293 if( p_title_unit->p_unit_inf != NULL )
1295 for( i = 0 ; i < p_title_unit->i_unit_nb ; i++ )
1297 FreeUnitInf( &p_title_unit->p_unit_inf[i] );
1300 free( p_title_unit->p_unit_inf );
1306 /*****************************************************************************
1307 * ReadCellInf : Fills the Cell Information structure.
1308 *****************************************************************************/
1309 static int ReadCellInf( ifo_t * p_ifo, cell_inf_t * p_cell_inf, int i_block )
1311 u8 p_buf[DVD_LB_SIZE];
1316 p_tmp = FillBuffer( p_ifo, p_buf, i_block );
1317 i_start = p_ifo->i_pos;
1318 //fprintf( stderr, "CELL ADD\n" );
1320 p_cell_inf->i_vob_nb = ReadWord( p_ifo, p_buf, &p_tmp );
1321 DumpBytes( p_ifo, p_buf, &p_tmp, 2 );
1322 p_cell_inf->i_end_byte = ReadDouble( p_ifo, p_buf, &p_tmp );
1324 p_cell_inf->i_cell_nb = (p_cell_inf->i_end_byte/* - 7*/) / sizeof(cell_map_t);
1326 //fprintf( stderr, "Cell inf: vob %d end %d cell %d\n", p_cell_inf->i_vob_nb, p_cell_inf->i_end_byte, p_cell_inf->i_cell_nb );
1328 p_cell_inf->p_cell_map =
1329 malloc( p_cell_inf->i_cell_nb *sizeof(cell_map_t) );
1330 if( p_cell_inf->p_cell_map == NULL )
1332 intf_ErrMsg( "ifo error: out of memory in ReadCellInf" );
1336 for( i = 0 ; i < p_cell_inf->i_cell_nb ; i++ )
1338 #define MAP p_cell_inf->p_cell_map[i]
1339 MAP.i_vob_id = ReadWord( p_ifo, p_buf, &p_tmp );
1340 MAP.i_cell_id = ReadByte( p_ifo, p_buf, &p_tmp );
1341 DumpBytes( p_ifo, p_buf, &p_tmp, 1 );
1342 MAP.i_start_sector = ReadDouble( p_ifo, p_buf, &p_tmp );
1343 //fprintf(stderr, "sector[%d] %d (%d)\n", i,ntohl(*(u32*)(p_tmp)), p_ifo->i_pos);
1344 MAP.i_end_sector = ReadDouble( p_ifo, p_buf, &p_tmp );
1351 /*****************************************************************************
1352 * FreeCellInf : frees structures allocated by ReadCellInf
1353 *****************************************************************************/
1354 static int FreeCellInf( cell_inf_t * p_cell_inf )
1356 free( p_cell_inf->p_cell_map );
1361 /*****************************************************************************
1362 * ReadVobuMap : Fills the VOBU Map structure.
1363 *****************************************************************************/
1364 static int ReadVobuMap( ifo_t * p_ifo, vobu_map_t * p_vobu_map, int i_block )
1366 u8 p_buf[DVD_LB_SIZE];
1371 p_tmp = FillBuffer( p_ifo, p_buf, i_block );
1372 i_start = p_ifo->i_pos;
1373 //fprintf( stderr, "VOBU ADMAP\n" );
1375 p_vobu_map->i_end_byte = ReadDouble( p_ifo, p_buf, &p_tmp );
1376 i_max = ( i_start + p_vobu_map->i_end_byte + 1 - p_ifo->i_pos )
1379 p_vobu_map->pi_vobu_start_sector = malloc( i_max * sizeof(u32) );
1380 if( p_vobu_map->pi_vobu_start_sector == NULL )
1382 intf_ErrMsg( "ifo error: out of memory in ReadVobuMap" );
1386 for( i = 0 ; i < i_max ; i++ )
1388 p_vobu_map->pi_vobu_start_sector[i] = ReadDouble( p_ifo, p_buf, &p_tmp );
1394 /*****************************************************************************
1395 * FreeVobuMap: frees structures allocated by ReadVobuMap
1396 *****************************************************************************/
1397 static int FreeVobuMap( vobu_map_t * p_vobu_map )
1399 free( p_vobu_map->pi_vobu_start_sector );
1405 * IFO virtual machine : a set of commands that give the
1406 * interactive behaviour of the dvd
1410 #define OP_VAL_16(i) (ntoh16( com.data.pi_16[i]))
1411 #define OP_VAL_8(i) ((com.data.pi_8[i]))
1413 static char ifo_reg[][80]=
1415 "Menu_Language_Code",
1417 "SubPicture_Stream_#",
1423 "Highlighted_Button_#",
1426 "Karaoke_audio_mixing_mode",
1427 "Parental_mgmt_country_code",
1431 "Audio_language_code_setting",
1432 "Audio_language_extension_code",
1433 "SPU_language_code_setting",
1434 "SPU_language_extension_code",
1435 "?Player_Regional_Code",
1441 static char * IfoMath( char val )
1443 static char math_op[][10] =
1463 return (char *) math_op[val & 0x0f];
1467 char ifo_cmp[][10] =
1479 char ifo_parental[][10] =
1491 char ifo_menu_id[][80] =
1503 char * IfoMenuName( char index )
1505 return ifo_menu_id[index&0x07];
1508 static void IfoRegister( u16 i_data, u8 i_direct)
1512 if( 0/*isalpha( i_data >> 8 & 0xff )*/ )
1514 printf("'%c%c'", i_data>>8&0xff, i_data&0xff);
1518 printf("0x%02x", i_data);
1533 printf("s[%s]", ifo_reg[i_data]);
1546 printf("r[0x%02x]", i_data);
1552 static void IfoAdvanced( u8 *pi_code )
1554 u8 i_cmd = pi_code[0];
1560 printf( " Highlight button %d; ", pi_code[1]>>2 );
1565 printf( " Illegal " );
1570 printf( "ReSuME %d", pi_code[7] );
1572 else if( ( i_cmd & 0x06) == 0x02 )
1574 printf ("Link to %s cell ", ( i_cmd & 0x01 ) ? "prev" : "next");
1578 printf( "advanced (0x%02x) ", i_cmd );
1583 static void IfoJmp( ifo_command_t com )
1588 switch( com.i_sub_cmd )
1594 printf( "VTS 0x%02x", OP_VAL_8(3) );
1597 printf( "This VTS Title 0x%02x", OP_VAL_8(3) );
1600 printf( "This VTS Title 0x%02x Part 0x%04x",
1602 OP_VAL_8(0)<<8|OP_VAL_8(1));
1606 printf ("in SystemSpace ");
1607 switch (OP_VAL_8(3)>>4) {
1609 printf ("to play first PGC");
1612 printf ("to menu \"%s\"", decode_menuname (OP_VAL_8(3)));
1616 printf ("to VTS 0x%02x and TTN 0x%02x", OP_VAL_8(1), OP_VAL_8(2));
1619 printf ("to VMGM PGC number 0x%02x", OP_VAL_8(0)<<8 | OP_VAL_8(1));
1622 printf ("vts 0x%02x lu 0x%02x menu \"%s\"", OP_VAL_8(2), OP_VAL_8(1), decode_menuname (OP_VAL_8(3)));
1625 switch( OP_VAL_8(3)>>6 )
1628 printf( "to play first PGC" );
1631 printf( "to VMG title menu (?)" );
1634 printf( "vts 0x%02x lu 0x%02x menu \"%s\"",
1637 IfoMenuName( OP_VAL_8(3)&0xF ) );
1640 printf( "vmg pgc 0x%04x (?)", ( OP_VAL_8(0)<<8) | OP_VAL_8(1) );
1647 switch(OP_VAL_8(3)>>4) {
1649 printf ("system first pgc");
1652 printf ("system title menu");
1655 printf ("system menu \"%s\"", decode_menuname (OP_VAL_8(3)));
1658 printf ("system vmg pgc %02x ????", OP_VAL_8(0)<<8|OP_VAL_8(1));
1661 printf ("system lu 0x%02x menu \"%s\"", OP_VAL_8(2), decode_menuname (OP_VAL_8(3)));
1664 printf ("system vmg pgc 0x%02x", OP_VAL_8(0)<<8|OP_VAL_8(1));
1668 // OP_VAL_8(2) is number of cell
1669 // it is processed BEFORE switch
1670 // under some conditions, it is ignored
1671 // I don't understand exactly what it means
1672 printf( " ( spec cell 0x%02X ) ", OP_VAL_8(2) );
1674 switch( OP_VAL_8(3)>>6 )
1677 printf( "to FP PGC" );
1680 printf( "to VMG root menu (?)" );
1683 printf( "to VTS menu \"%s\" (?)",
1684 IfoMenuName(OP_VAL_8(3)&0xF) );
1687 printf( "vmg pgc 0x%02x (?)", (OP_VAL_8(0)<<8)|OP_VAL_8(1) );
1695 static void IfoLnk( ifo_command_t com )
1697 u16 i_button=OP_VAL_8(4)>>2;
1701 switch( com.i_sub_cmd )
1704 IfoAdvanced( &OP_VAL_8(4) );
1708 printf( "PGC 0x%02x", OP_VAL_16(2) );
1712 printf( "PTT 0x%02x", OP_VAL_16(2) );
1716 printf( "Program 0x%02x this PGC", OP_VAL_8(5) );
1720 printf( "Cell 0x%02x this PGC", OP_VAL_8(5) );
1728 printf( ", Highlight 0x%02x", OP_VAL_8(4)>>2 );
1733 void IfoSetSystem( ifo_command_t com )
1740 for( i=1; i<=3; i++ )
1742 if( OP_VAL_8(i)&0x80 )
1746 printf ("s[%s] = 0x%02x;", ifo_reg[i], OP_VAL_8(i)&0xf);
1750 printf ("s[%s] = r[0x%02x];", ifo_reg[i], OP_VAL_8(i)&0xf);
1756 if(OP_VAL_8(1]&0x80)
1757 printf ("s[%s] = 0x%02x;", reg_name[1], OP_VAL_8(1]&0xf);
1758 if(OP_VAL_8(2)&0x80)
1759 //DENT: lwhat about 0x7f here ???
1760 printf ("s[%s] = 0x%02x;", reg_name[2], OP_VAL_8(2)&0x7f);
1761 if(OP_VAL_8(3)&0x80)
1762 printf ("s[%s] = 0x%02x;", reg_name[3], OP_VAL_8(3)&0xf);
1764 if(OP_VAL_8(1)&0x80)
1765 printf ("s[%s] = r[0x%02x];", reg_name[1], OP_VAL_8(1)&0xf);
1766 if(OP_VAL_8(2)&0x80)
1767 printf ("s[%s] = r[0x%02x];", reg_name[2], OP_VAL_8(2)&0xf);
1768 if(OP_VAL_8(3)&0x80)
1769 printf ("s[%s] = r[0x%02x];", reg_name[3], OP_VAL_8(3)&0xf);
1777 printf( "s[%s] = 0x%02x", ifo_reg[9], OP_VAL_16(0) );
1781 printf( "s[%s] = r[0x%02x]", ifo_reg[9], OP_VAL_8(1)&0x0f );
1784 printf( "s[%s] = (s[%s]&0x7FFF)|0x%02x",
1785 ifo_reg[10], ifo_reg[10], OP_VAL_16(1)&0x8000);
1790 printf( "r[0x%02x] = 0x%02x", OP_VAL_8(3)&0x0f, OP_VAL_16(0) );
1794 printf ("r[r[0x%02x]] = r[0x%02x]",
1795 OP_VAL_8(3)&0x0f, OP_VAL_8(1)&0x0f);
1799 //actually only bits 00011100 00011100 are set
1802 printf ("s[%s] = 0x%02x", ifo_reg[11], OP_VAL_16(1));
1806 printf ("s[%s] = r[0x%02x]", ifo_reg[11], OP_VAL_8(3)&0x0f );
1811 //s[%s]=(r[%s]&0x3FF) | (0x%02x << 0xA);
1812 //but it is way too ugly
1815 printf( "s[%s] = 0x%02x", ifo_reg[8], OP_VAL_8(2)>>2 );
1819 printf( "s[%s] = r[0x%02x]", ifo_reg[8], OP_VAL_8(3)&0x0f );
1827 static void IfoSet( ifo_command_t com )
1829 IfoRegister( OP_VAL_16(0), 0 );
1830 printf( " %s ", IfoMath( com.i_cmd ) );
1831 IfoRegister( OP_VAL_16(1), com.i_direct );
1834 /*****************************************************************************
1835 * CommandRead : translates the command strings in ifo into command
1837 *****************************************************************************/
1838 void CommandRead( ifo_command_t com )
1840 u8* pi_code = (u8*)(&com);
1842 switch( com.i_type )
1855 printf ("if (r[0x%02x] %s ", OP_VAL_8(1)&0x0f,
1856 ifo_cmp[com.i_cmp]);
1857 IfoRegister (OP_VAL_16(1), com.i_dir_cmp);
1862 switch( com.i_sub_cmd )
1865 printf( "goto Line 0x%02x", OP_VAL_16(2) );
1869 printf( "stop VM" );
1873 printf( "Set Parental Level To %s and goto Line 0x%02x",
1874 ifo_parental[OP_VAL_8(4)&0x7],
1879 printf( "Illegal" );
1898 printf( "if (r[0x%02x] %s ", OP_VAL_8(4)&0x0f,
1899 ifo_cmp[com.i_cmp] );
1900 IfoRegister( OP_VAL_8(5), 0 );
1911 printf( "if (r[0x%02x] %s ", OP_VAL_8(1)&0x0f,
1912 ifo_cmp[com.i_cmp] );
1913 IfoRegister( OP_VAL_16(1), com.i_dir_cmp );
1927 IfoSetSystem( com );
1929 else if( com.i_cmp && !com.i_sub_cmd )
1931 printf ("if (r[0x%02x] %s ", OP_VAL_8(4)&0x0f, ifo_cmp[com.i_cmp]);
1932 IfoRegister( OP_VAL_8(5), 0 );
1934 IfoSetSystem( com );
1936 else if( !com.i_cmp && com.i_sub_cmd )
1939 IfoSetSystem( com );
1955 else if( com.i_cmp && !com.i_sub_cmd )
1957 printf ("if (r[0x%02x] %s ", OP_VAL_8(0)&0x0f, ifo_cmp[com.i_cmp]);
1958 IfoRegister( OP_VAL_16(2), com.i_dir_cmp );
1962 else if( !com.i_cmp && com.i_sub_cmd )
1976 * math command on r[opcode[1]] and
1977 * direct?be2me_16(OP_VAL_8(0)):reg[OP_VAL_8(1)] is executed
1978 * ( unless command is swap; then r[opcode[1]] and r[OP_VAL_8(1)]
1980 * boolean operation cmp on r[opcode[1]] and
1981 * dir_cmp?be2me_16(OP_VAL_8(1)[1]):reg[OP_VAL_8(3)] is executed
1982 * on true result, buttons(c[6], c[7]) is called
1983 * problem is 'what is buttons()'
1986 printf( "r[0x%X] ", pi_code[1] );
1987 printf( " %s ", IfoMath( com.i_cmd ) );
1988 if( com.i_cmd == 2 )
1990 printf( "r[0x%X] ", OP_VAL_8(1) );
1994 IfoRegister( OP_VAL_16(0), com.i_direct );
1998 printf( "if ( r[%d] %s ", pi_code[1], ifo_cmp[com.i_cmp] );
1999 IfoRegister( OP_VAL_8(1), com.i_dir_cmp );
2000 printf( " ) then {" );
2001 IfoAdvanced( &OP_VAL_8(4) );
2006 * opposite to case 4: boolean, math and buttons.
2012 if( !com.i_direct && com.i_dir_cmp )
2014 printf( "0x%X", OP_VAL_16(1) );
2018 IfoRegister( OP_VAL_8(3), 0 );
2019 if( OP_VAL_8(3)&0x80 )
2021 printf( "s[%s]", ifo_reg[OP_VAL_8(3)&0x1F] );
2025 printf( "r[0x%X]", OP_VAL_8(3)&0x1F);
2026 // 0x1F is either not a mistake,
2027 // or Microsoft programmer's mistake!!!
2031 printf( " %s r[0x%X] ", ifo_cmp[com.i_cmp],
2032 com.i_direct ? OP_VAL_8(2) : OP_VAL_8(1) );
2033 printf( " ) then {" );
2034 printf( "r[0x%X] ", pi_code[1] & 0xF );
2035 printf( " %s ", IfoMath( com.i_cmd ) );
2037 if( com.i_cmd == 0x02 ) // swap
2039 printf("r[0x%X] ", OP_VAL_8(0)&0x1F);
2045 printf( "0x%X", OP_VAL_16(0) );
2049 if( OP_VAL_8(0) & 0x80 )
2051 printf("s[%s]", ifo_reg[OP_VAL_8(0) & 0x1F] );
2055 printf("r[0x%X]", OP_VAL_8(0) & 0x1F );
2061 IfoAdvanced( &OP_VAL_8(4) );
2067 printf( "Unknown Command\n" );
2074 /*****************************************************************************
2075 * CommandPrint : print in clear text (I hope so !) what a command does
2076 *****************************************************************************/
2077 void CommandPrint( ifo_t ifo )
2084 /*****************************************************************************
2086 *****************************************************************************/
2087 static u8* FillBuffer( ifo_t* p_ifo, u8* p_buf, int i_pos )
2089 p_ifo->i_pos = dvdcss_seek( p_ifo->dvdhandle, i_pos );
2090 dvdcss_read( p_ifo->dvdhandle, p_buf, 1, DVDCSS_NOFLAGS );
2095 static void ReadBytes( ifo_t* p_ifo, u8* p_buf, u8** pp_tmp,
2096 u8* pi_dest, int i_nb )
2098 if( i_nb > DVD_LB_SIZE )
2100 intf_ErrMsg( "ifo error: excessive ReadBytes call (%i)", i_nb );
2103 if( *pp_tmp + i_nb >= p_buf + DVD_LB_SIZE )
2105 int i_spare = (int)( (p_buf + DVD_LB_SIZE) - *pp_tmp );
2107 /* Copy the bytes remaining in the current buffer */
2108 memcpy( pi_dest, *pp_tmp, i_spare );
2112 /* Load the next buffer */
2113 *pp_tmp = FillBuffer( p_ifo, p_buf, p_ifo->i_pos + 1 );
2116 memcpy( pi_dest, *pp_tmp, i_nb );
2122 static void DumpBytes( ifo_t* p_ifo, u8* p_buf, u8** pp_tmp, int i_nb )
2124 if( i_nb > DVD_LB_SIZE )
2126 intf_ErrMsg( "ifo error: excessive DumpBytes call (%i)", i_nb );
2131 if( *pp_tmp >= p_buf + DVD_LB_SIZE )
2133 /* If we went too far, load the next buffer */
2134 *pp_tmp = FillBuffer( p_ifo, p_buf, p_ifo->i_pos + 1 )
2135 + (int)( (*pp_tmp) - (p_buf - DVD_LB_SIZE) );
2142 if( *pp_tmp >= p_buf + DVD_LB_SIZE ) \
2144 *pp_tmp = FillBuffer( p_ifo, p_buf, p_ifo->i_pos + 1 ); \
2146 i_ret <<= 8; i_ret |= **pp_tmp; (*pp_tmp)++;
2148 static u8 ReadByte( ifo_t * p_ifo, u8* p_buf, u8** pp_tmp )
2155 static u16 ReadWord( ifo_t * p_ifo, u8* p_buf, u8** pp_tmp )
2162 static u32 ReadDouble( ifo_t * p_ifo, u8* p_buf, u8** pp_tmp )
2165 ADDBYTE; ADDBYTE; ADDBYTE; ADDBYTE;
2169 static u64 ReadQuad( ifo_t * p_ifo, u8* p_buf, u8** pp_tmp )
2172 ADDBYTE; ADDBYTE; ADDBYTE; ADDBYTE; ADDBYTE; ADDBYTE; ADDBYTE; ADDBYTE;