dvd_ifo.c 52.9 KB
Newer Older
Stéphane Borel's avatar
Stéphane Borel committed
1
2
3
4
/*****************************************************************************
 * dvd_ifo.c: Functions for ifo parsing
 *****************************************************************************
 * Copyright (C) 1999-2001 VideoLAN
Sam Hocevar's avatar
   
Sam Hocevar committed
5
 * $Id: dvd_ifo.c,v 1.13 2001/02/20 07:49:12 sam Exp $
Stéphane Borel's avatar
Stéphane Borel committed
6
7
8
 *
 * Author: Stphane Borel <stef@via.ecp.fr>
 *
9
10
11
12
13
 * based on:
 *  - libifo by Thomas Mirlacher <dent@cosy.sbg.ac.at>
 *  - IFO structure documentation by Thomas Mirlacher, Bjrn Englund,
 *  Hkan Hjort
 *
Stéphane Borel's avatar
Stéphane Borel committed
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
 *****************************************************************************/

/*****************************************************************************
 * Preamble
 *****************************************************************************/
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <fcntl.h>

#include "common.h"

#include "intf_msg.h"
#include "dvd_ifo.h"
41
#include "dvd_udf.h"
Sam Hocevar's avatar
   
Sam Hocevar committed
42
#include "dvd_css.h"
43
#include "input_dvd.h"
Stéphane Borel's avatar
Stéphane Borel committed
44

Stéphane Borel's avatar
   
Stéphane Borel committed
45
46
47
48
49
/*
 * Local prototypes
 */
static vmg_t ReadVMG    ( ifo_t* );
void         CommandRead( ifo_command_t );
50

Stéphane Borel's avatar
Stéphane Borel committed
51
52
53
54
55
56
57
58
59
60
61
/*
 * IFO Management.
 */

/*****************************************************************************
 * IfoInit : Creates an ifo structure and prepares for parsing directly
 * on DVD device.
 *****************************************************************************/
ifo_t IfoInit( int i_fd )
{
    ifo_t       ifo;
62
    u32         i_lba;
Stéphane Borel's avatar
Stéphane Borel committed
63
64
65
    
    /* If we are here the dvd device has already been opened */
    ifo.i_fd = i_fd;
66
67
68
69
70
71

    i_lba = UDFFindFile( i_fd, "/VIDEO_TS/VIDEO_TS.IFO");

    ifo.i_off = (off_t)(i_lba) * DVD_LB_SIZE;
    ifo.i_pos = lseek( ifo.i_fd, ifo.i_off, SEEK_SET );

Stéphane Borel's avatar
   
Stéphane Borel committed
72
73
74
75
    /* Video Manager Initialization */
    intf_WarnMsg( 2, "ifo: initializing VMG" );
    ifo.vmg = ReadVMG( &ifo );

Stéphane Borel's avatar
Stéphane Borel committed
76
77
78
79
80
81
82
83
    return ifo;
}

/*****************************************************************************
 * IfoEnd : Frees all the memory allocated to ifo structures
 *****************************************************************************/
void IfoEnd( ifo_t* p_ifo )
{
Stéphane Borel's avatar
   
Stéphane Borel committed
84
#if 0
Stéphane Borel's avatar
Stéphane Borel committed
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
    int     i,j;

    /* Free structures from video title sets */
    for( j=0 ; j<p_ifo->vmg.mat.i_tts_nb ; j++ )
    {
        free( p_ifo->p_vts[j].vobu_admap.pi_vobu_ssector );
        free( p_ifo->p_vts[j].c_adt.p_cell_inf );
        free( p_ifo->p_vts[j].m_vobu_admap.pi_vobu_ssector );
        free( p_ifo->p_vts[j].m_c_adt.p_cell_inf );
        for( i=0 ; i<p_ifo->p_vts[j].tmap_ti.i_nb ; i++ )
        {
            free( p_ifo->p_vts[j].tmap_ti.p_tmap[i].pi_sector );
        }
        free( p_ifo->p_vts[j].tmap_ti.pi_sbyte );
        free( p_ifo->p_vts[j].tmap_ti.p_tmap );
100
101
102
103
104
105
        free( p_ifo->p_vts[j].pgci_ti.p_srp );
        for( i=0 ; i<p_ifo->p_vts[j].pgci_ut.i_lu_nb ; i++ )
        {
            free( p_ifo->p_vts[j].pgci_ut.p_pgci_inf[i].p_srp );
        }
        free( p_ifo->p_vts[j].pgci_ut.p_pgci_inf );
Stéphane Borel's avatar
Stéphane Borel committed
106
107
108
109
110
111
112
113
114
115
        free( p_ifo->p_vts[j].pgci_ut.p_lu );
    }

    free( p_ifo->p_vts );

    /* Free structures from video manager */
    free( p_ifo->vmg.vobu_admap.pi_vobu_ssector );
    free( p_ifo->vmg.c_adt.p_cell_inf );
    for( i=0 ; i<p_ifo->vmg.pgci_ut.i_lu_nb ; i++ )
    {
116
        free( p_ifo->vmg.pgci_ut.p_pgci_inf[i].p_srp );
Stéphane Borel's avatar
Stéphane Borel committed
117
    }
118
    free( p_ifo->vmg.pgci_ut.p_pgci_inf );
Stéphane Borel's avatar
Stéphane Borel committed
119
120
121
122
123
124
125
126
127
128
129
130
    free( p_ifo->vmg.pgci_ut.p_lu );
    for( i=1 ; i<=8 ; i++ )
    {
        free( p_ifo->vmg.ptl_mait.p_ptl_mask->ppi_ptl_mask[i] );
    }
    free( p_ifo->vmg.ptl_mait.p_ptl_desc );
    free( p_ifo->vmg.ptl_mait.p_ptl_mask );
    free( p_ifo->vmg.vts_atrt.pi_vts_atrt_sbyte );
    free( p_ifo->vmg.vts_atrt.p_vts_atrt );
    free( p_ifo->vmg.pgc.p_cell_pos_inf );
    free( p_ifo->vmg.pgc.p_cell_play_inf );
    free( p_ifo->vmg.pgc.prg_map.pi_entry_cell );
131
132
133
    free( p_ifo->vmg.pgc.com_tab.p_cell_com );
    free( p_ifo->vmg.pgc.com_tab.p_post_com );
    free( p_ifo->vmg.pgc.com_tab.p_pre_com );
Stéphane Borel's avatar
   
Stéphane Borel committed
134
#endif
Stéphane Borel's avatar
Stéphane Borel committed
135
136
137
138
139
140
141
142
143
    return;
}

/*
 * Macros to process ifo files
 */
 
#define GET( p_field , i_len )                                              \
    {                                                                       \
144
        read( p_ifo->i_fd , (p_field) , (i_len) );                          \
145
/*fprintf(stderr, "Pos : %lld Val : %llx\n",                                  \
146
                                (long long)(p_ifo->i_pos - i_start),        \
147
                                (long long)*(p_field) );    */                \
148
        p_ifo->i_pos += i_len;                                              \
Stéphane Borel's avatar
Stéphane Borel committed
149
150
151
152
    }

#define GETC( p_field )                                                     \
    {                                                                       \
153
        read( p_ifo->i_fd , (p_field) , 1 );                                \
154
/*fprintf(stderr, "Pos : %lld Value : %d\n",                                  \
155
                                (long long)(p_ifo->i_pos - i_start),        \
156
                                          *(p_field) );*/                     \
157
        p_ifo->i_pos += 1;                                                  \
Stéphane Borel's avatar
Stéphane Borel committed
158
159
160
161
    }

#define GETS( p_field )                                                     \
    {                                                                       \
162
163
        read( p_ifo->i_fd , (p_field) , 2 );                                \
        *(p_field) = ntohs( *(p_field) );                                   \
164
/*fprintf(stderr, "Pos : %lld Value : %d\n",                                  \
165
                                (long long)(p_ifo->i_pos - i_start),        \
166
                                          *(p_field) );*/                     \
167
        p_ifo->i_pos += 2;                                                  \
Stéphane Borel's avatar
Stéphane Borel committed
168
169
170
171
    }

#define GETL( p_field )                                                     \
    {                                                                       \
172
173
        read( p_ifo->i_fd , (p_field) , 4 );                                \
        *(p_field) = ntohl( *(p_field) );                                   \
174
/*fprintf(stderr, "Pos : %lld Value : %d\n",                                  \
175
                                (long long)(p_ifo->i_pos - i_start),        \
176
                                          *(p_field) );*/                     \
177
        p_ifo->i_pos += 4;                                                  \
Stéphane Borel's avatar
Stéphane Borel committed
178
179
180
181
    }

#define GETLL( p_field )                                                    \
    {                                                                       \
182
183
        read( p_ifo->i_fd , (p_field) , 8 );                                \
        *(p_field) = ntoh64( *(p_field) );                                  \
184
/*fprintf(stderr, "Pos : %lld Value : %lld\n",                                \
185
                                (long long)(p_ifo->i_pos - i_start),        \
186
                                            *(p_field) );*/                   \
187
        p_ifo->i_pos += 8;                                                  \
Stéphane Borel's avatar
Stéphane Borel committed
188
189
190
191
    }

#define FLUSH( i_len )                                                      \
    {                                                                       \
192
/*fprintf(stderr, "Pos : %lld\n", (long long)(p_ifo->i_pos - i_start));*/       \
193
        p_ifo->i_pos = lseek( p_ifo->i_fd ,                               \
194
                              p_ifo->i_pos + (i_len), SEEK_SET );           \
Stéphane Borel's avatar
Stéphane Borel committed
195
196
197
198
199
200
201
202
203
    }

/*
 * Function common to Video Manager and Video Title set Processing
 */

/*****************************************************************************
 * ReadPGC : Fills the Program Chain structure.
 *****************************************************************************/
204
205
206
207
208
209
210
211
212
213
214
#define GETCOMMAND( p_com )                                                 \
    {                                                                       \
        read( p_ifo->i_fd , (p_com) , 8 );                                  \
/*fprintf(stderr, "Pos : %lld Type : %d direct : %d cmd : %d dircmp : %d cmp : %d subcmd : %d v0 : %d v2 : %d v4 : %d\n",                                  \
                                (long long)(p_ifo->i_pos - i_start),        \
                                (int)((p_com)->i_type),                     \
                                (int)((p_com)->i_direct),                   \
                                (int)((p_com)->i_cmd),                      \
                                (int)((p_com)->i_dir_cmp),                  \
                                (int)((p_com)->i_cmp),                      \
                                (int)((p_com)->i_sub_cmd),                  \
215
216
217
218
                                (int)((p_com)->data.pi_16[0]),              \
                                (int)((p_com)->data.pi_16[1]),              \
                                (int)((p_com)->data.pi_16[2]));*/             \
/*        CommandRead( *(p_com) );*/                                            \
219
        p_ifo->i_pos += 8;                                                  \
220
221
    }

Stéphane Borel's avatar
Stéphane Borel committed
222
223
224
225
static pgc_t ReadPGC( ifo_t* p_ifo )
{
    pgc_t   pgc;
    int     i;
226
    off_t   i_start = p_ifo->i_pos;
Stéphane Borel's avatar
Stéphane Borel committed
227

228
//fprintf( stderr, "PGC\n" );
Stéphane Borel's avatar
Stéphane Borel committed
229
230
231
232

    FLUSH(2);
    GETC( &pgc.i_prg_nb );
    GETC( &pgc.i_cell_nb );
233
//fprintf( stderr, "PGC: Prg: %d Cell: %d\n", pgc.i_prg_nb, pgc.i_cell_nb );
Stéphane Borel's avatar
Stéphane Borel committed
234
235
236
237
238
239
240
241
242
243
244
245
246
    GETL( &pgc.i_play_time );
    GETL( &pgc.i_prohibited_user_op );
    for( i=0 ; i<8 ; i++ )
    {
        GETS( &pgc.pi_audio_status[i] );
    }
    for( i=0 ; i<32 ; i++ )
    {
        GETL( &pgc.pi_subpic_status[i] );
    }
    GETS( &pgc.i_next_pgc_nb );
    GETS( &pgc.i_prev_pgc_nb );
    GETS( &pgc.i_goup_pgc_nb );
247
//fprintf( stderr, "PGC: Prev: %d Next: %d Up: %d\n",pgc.i_prev_pgc_nb ,pgc.i_next_pgc_nb, pgc.i_goup_pgc_nb );
Stéphane Borel's avatar
Stéphane Borel committed
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
    GETC( &pgc.i_still_time );
    GETC( &pgc.i_play_mode );
    for( i=0 ; i<16 ; i++ )
    {
        GETL( &pgc.pi_yuv_color[i] );
        /* FIXME : We have to erase the extra bit */
    }
    GETS( &pgc.i_com_tab_sbyte );
    GETS( &pgc.i_prg_map_sbyte );
    GETS( &pgc.i_cell_play_inf_sbyte );
    GETS( &pgc.i_cell_pos_inf_sbyte );

    /* Parsing of pgc_com_tab_t */
    if( pgc.i_com_tab_sbyte )
    {
263
        p_ifo->i_pos = lseek( p_ifo->i_fd, i_start
Stéphane Borel's avatar
Stéphane Borel committed
264
265
266
267
                            + pgc.i_com_tab_sbyte, SEEK_SET );
        GETS( &pgc.com_tab.i_pre_com_nb );
        GETS( &pgc.com_tab.i_post_com_nb );
        GETS( &pgc.com_tab.i_cell_com_nb );
268
269
        FLUSH( 2 );
        if( pgc.com_tab.i_pre_com_nb )
Stéphane Borel's avatar
Stéphane Borel committed
270
        {
271
272
273
            pgc.com_tab.p_pre_com =
                      malloc(pgc.com_tab.i_pre_com_nb *sizeof(ifo_command_t));
            if( pgc.com_tab.p_pre_com == NULL )
274
275
276
277
278
            {
                intf_ErrMsg( "Out of memory" );
                p_ifo->b_error = 1;
                return pgc;
            }
279
280
281
282
            for( i=0 ; i<pgc.com_tab.i_pre_com_nb ; i++ )
            {
                GETCOMMAND( &pgc.com_tab.p_pre_com[i] );
            }
Stéphane Borel's avatar
Stéphane Borel committed
283
        }
284
        if( pgc.com_tab.i_post_com_nb )
Stéphane Borel's avatar
Stéphane Borel committed
285
        {
286
287
288
            pgc.com_tab.p_post_com =
                      malloc(pgc.com_tab.i_post_com_nb *sizeof(ifo_command_t));
            if( pgc.com_tab.p_post_com == NULL )
289
290
291
292
293
            {
                intf_ErrMsg( "Out of memory" );
                p_ifo->b_error = 1;
                return pgc;
            }
294
295
296
297
            for( i=0 ; i<pgc.com_tab.i_post_com_nb ; i++ )
            {
                GETCOMMAND( &pgc.com_tab.p_post_com[i] );
            }
Stéphane Borel's avatar
Stéphane Borel committed
298
        }
299
        if( pgc.com_tab.i_cell_com_nb )
Stéphane Borel's avatar
Stéphane Borel committed
300
        {
301
302
303
            pgc.com_tab.p_cell_com =
                      malloc(pgc.com_tab.i_cell_com_nb *sizeof(ifo_command_t));
            if( pgc.com_tab.p_cell_com == NULL )
304
305
306
307
308
            {
                intf_ErrMsg( "Out of memory" );
                p_ifo->b_error = 1;
                return pgc;
            }
309
310
311
312
            for( i=0 ; i<pgc.com_tab.i_cell_com_nb ; i++ )
            {
                GETCOMMAND( &pgc.com_tab.p_cell_com[i] );
            }
Stéphane Borel's avatar
Stéphane Borel committed
313
314
315
316
317
        }
    }
    /* Parsing of pgc_prg_map_t */
    if( pgc.i_prg_map_sbyte )
    {
318
        p_ifo->i_pos = lseek( p_ifo->i_fd, i_start
Stéphane Borel's avatar
Stéphane Borel committed
319
                            + pgc.i_prg_map_sbyte, SEEK_SET );
320
        pgc.prg_map.pi_entry_cell = malloc( pgc.i_prg_nb *sizeof(u8) );
Stéphane Borel's avatar
Stéphane Borel committed
321
322
323
324
325
326
327
328
329
330
331
332
        if( pgc.prg_map.pi_entry_cell == NULL )
        {
            intf_ErrMsg( "Out of memory" );
            p_ifo->b_error = 1;
            return pgc;
        }
        GET( pgc.prg_map.pi_entry_cell, pgc.i_prg_nb );
        /* FIXME : check endianness here */
    }
    /* Parsing of cell_play_inf_t */
    if( pgc.i_cell_play_inf_sbyte )
    {
333
        p_ifo->i_pos = lseek( p_ifo->i_fd, i_start
Stéphane Borel's avatar
Stéphane Borel committed
334
335
336
337
338
339
340
341
342
343
344
345
                            + pgc.i_cell_play_inf_sbyte, SEEK_SET );
        pgc.p_cell_play_inf = malloc( pgc.i_cell_nb *sizeof(cell_play_inf_t) );
        if( pgc.p_cell_play_inf == NULL )
        {
            intf_ErrMsg( "Out of memory" );
            p_ifo->b_error = 1;
            return pgc;
        }
        for( i=0 ; i<pgc.i_cell_nb ; i++ )
        {
            GETS( &pgc.p_cell_play_inf[i].i_cat );
            GETC( &pgc.p_cell_play_inf[i].i_still_time );
346
            GETC( &pgc.p_cell_play_inf[i].i_com_nb );
Stéphane Borel's avatar
Stéphane Borel committed
347
348
349
350
351
352
353
354
355
356
            GETL( &pgc.p_cell_play_inf[i].i_play_time );
            GETL( &pgc.p_cell_play_inf[i].i_entry_sector );
            GETL( &pgc.p_cell_play_inf[i].i_first_ilvu_vobu_esector );
            GETL( &pgc.p_cell_play_inf[i].i_lvobu_ssector );
            GETL( &pgc.p_cell_play_inf[i].i_lsector );
        }
    }
    /* Parsing of cell_pos_inf_map */
    if( pgc.i_cell_pos_inf_sbyte )
    {
357
        p_ifo->i_pos = lseek( p_ifo->i_fd, i_start
Stéphane Borel's avatar
Stéphane Borel committed
358
359
                            + pgc.i_cell_pos_inf_sbyte, SEEK_SET );
        pgc.p_cell_pos_inf = malloc( pgc.i_cell_nb *sizeof(cell_pos_inf_t) );
360
        if( pgc.p_cell_pos_inf == NULL )
Stéphane Borel's avatar
Stéphane Borel committed
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
        {
            intf_ErrMsg( "Out of memory" );
            p_ifo->b_error = 1;
            return pgc;
        }
        for( i=0 ; i<pgc.i_cell_nb ; i++ )
        {
            GETS( &pgc.p_cell_pos_inf[i].i_vob_id );
            FLUSH( 1 );
            GETC( &pgc.p_cell_pos_inf[i].i_cell_id );
        }
    } 

    return pgc;
}

/*****************************************************************************
378
379
380
381
382
383
 * ReadUnit : Fills Menu Language Unit Table/ PGC Info Table
 *****************************************************************************/
static pgci_inf_t ReadUnit( ifo_t* p_ifo )
{
    pgci_inf_t      inf;
    int             i;
384
    off_t           i_start = p_ifo->i_pos;
385

386
//fprintf( stderr, "Unit\n" );
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406

    GETS( &inf.i_srp_nb );
    FLUSH( 2 );
    GETL( &inf.i_lu_ebyte );
    inf.p_srp = malloc( inf.i_srp_nb *sizeof(pgci_srp_t) );
    if( inf.p_srp == NULL )
    {
        intf_ErrMsg( "Out of memory" );
        p_ifo->b_error = 1;
        return inf;
    }
    for( i=0 ; i<inf.i_srp_nb ; i++ )
    {
        GETC( &inf.p_srp[i].i_pgc_cat_mask );
        GETC( &inf.p_srp[i].i_pgc_cat );
        GETS( &inf.p_srp[i].i_par_mask );
        GETL( &inf.p_srp[i].i_pgci_sbyte );
    }
    for( i=0 ; i<inf.i_srp_nb ; i++ )
    {
407
        p_ifo->i_pos = lseek( p_ifo->i_fd,
408
409
                         i_start + inf.p_srp[i].i_pgci_sbyte,
                         SEEK_SET );
410
//fprintf( stderr, "Unit: PGC %d\n", i );
411
412
413
414
415
416
417
418
        inf.p_srp[i].pgc = ReadPGC( p_ifo );
    }

    return inf;
}

/*****************************************************************************
 * ReadUnitTable : Fills the PGCI Unit structure.
Stéphane Borel's avatar
Stéphane Borel committed
419
420
421
 *****************************************************************************/
static pgci_ut_t ReadUnitTable( ifo_t* p_ifo )
{
422
423
    pgci_ut_t       pgci;
    int             i;
424
    off_t           i_start = p_ifo->i_pos;
Stéphane Borel's avatar
Stéphane Borel committed
425

426
//fprintf( stderr, "Unit Table\n" );
Stéphane Borel's avatar
Stéphane Borel committed
427

428
    GETS( &pgci.i_lu_nb );
Stéphane Borel's avatar
Stéphane Borel committed
429
    FLUSH( 2 );
430
431
432
    GETL( &pgci.i_ebyte );
    pgci.p_lu = malloc( pgci.i_lu_nb *sizeof(pgci_lu_t) );
    if( pgci.p_lu == NULL )
Stéphane Borel's avatar
Stéphane Borel committed
433
434
435
    {
        intf_ErrMsg( "Out of memory" );
        p_ifo->b_error = 1;
436
        return pgci;
Stéphane Borel's avatar
Stéphane Borel committed
437
    }
438
    for( i=0 ; i<pgci.i_lu_nb ; i++ )
Stéphane Borel's avatar
Stéphane Borel committed
439
    {
Stéphane Borel's avatar
Stéphane Borel committed
440
        GET( pgci.p_lu[i].ps_lang_code, 2 );
Stéphane Borel's avatar
Stéphane Borel committed
441
        FLUSH( 1 );
442
443
        GETC( &pgci.p_lu[i].i_existence_mask );
        GETL( &pgci.p_lu[i].i_lu_sbyte );
Stéphane Borel's avatar
Stéphane Borel committed
444
    }
445
446
    pgci.p_pgci_inf = malloc( pgci.i_lu_nb *sizeof(pgci_inf_t) );
    if( pgci.p_pgci_inf == NULL )
Stéphane Borel's avatar
Stéphane Borel committed
447
448
449
    {
        intf_ErrMsg( "Out of memory" );
        p_ifo->b_error = 1;
450
        return pgci;
Stéphane Borel's avatar
Stéphane Borel committed
451
    }
452
    for( i=0 ; i<pgci.i_lu_nb ; i++ )
Stéphane Borel's avatar
Stéphane Borel committed
453
    {
454
        p_ifo->i_pos = lseek( p_ifo->i_fd, i_start +
455
                                pgci.p_lu[i].i_lu_sbyte,
Stéphane Borel's avatar
Stéphane Borel committed
456
                                SEEK_SET );
457
        pgci.p_pgci_inf[i] = ReadUnit( p_ifo );
Stéphane Borel's avatar
Stéphane Borel committed
458
459
    }

460
    return pgci;
Stéphane Borel's avatar
Stéphane Borel committed
461
462
463
464
465
466
467
468
}

/*****************************************************************************
 * ReadCellInf : Fills the Cell Information structure.
 *****************************************************************************/
static c_adt_t ReadCellInf( ifo_t* p_ifo )
{
    c_adt_t         c_adt;
469
    off_t           i_start = p_ifo->i_pos;
470
    int             i;
Stéphane Borel's avatar
Stéphane Borel committed
471

472
//fprintf( stderr, "CELL ADD\n" );
Stéphane Borel's avatar
Stéphane Borel committed
473
474
475
476

    GETS( &c_adt.i_vob_nb );
    FLUSH( 2 );
    GETL( &c_adt.i_ebyte );
477
478
479
    c_adt.i_cell_nb =
        ( i_start + c_adt.i_ebyte + 1 - p_ifo->i_pos ) / sizeof(cell_inf_t);
    c_adt.p_cell_inf = malloc( c_adt.i_cell_nb *sizeof(cell_inf_t) );
Stéphane Borel's avatar
Stéphane Borel committed
480
481
482
483
484
485
    if( c_adt.p_cell_inf == NULL )
    {
        intf_ErrMsg( "Out of memory" );
        p_ifo->b_error = 1;
        return c_adt;
    }
486
    for( i = 0 ; i < c_adt.i_cell_nb ; i++ )
Stéphane Borel's avatar
Stéphane Borel committed
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
    {
        GETS( &c_adt.p_cell_inf[i].i_vob_id );
        GETC( &c_adt.p_cell_inf[i].i_cell_id );
        FLUSH( 1 );
        GETL( &c_adt.p_cell_inf[i].i_ssector );
        GETL( &c_adt.p_cell_inf[i].i_esector );
    }
    
    return c_adt;
}

/*****************************************************************************
 * ReadMap : Fills the VOBU Map structure.
 *****************************************************************************/
static vobu_admap_t ReadMap( ifo_t* p_ifo )
{
    vobu_admap_t        map;
    int                 i, i_max;
505
    off_t               i_start = p_ifo->i_pos;
Stéphane Borel's avatar
Stéphane Borel committed
506
    
507
//fprintf( stderr, "VOBU ADMAP\n" );
Stéphane Borel's avatar
Stéphane Borel committed
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531

    GETL( &map.i_ebyte );
    i_max = ( i_start + map.i_ebyte + 1 - p_ifo->i_pos ) / sizeof(u32);
    map.pi_vobu_ssector = malloc( i_max *sizeof(u32) );
    for( i=0 ; i<i_max ; i++ )
    {
        GETL( &map.pi_vobu_ssector[i] );
    }

    return map;
}
 
/*
 * Video Manager Information Processing.
 * This is what is contained in video_ts.ifo.
 */

/*****************************************************************************
 * ReadVMGInfMat : Fills the Management Information structure.
 *****************************************************************************/
static vmgi_mat_t ReadVMGInfMat( ifo_t* p_ifo )
{
    vmgi_mat_t  mat;
    int         i;
532
//    off_t     i_start = p_ifo->i_pos;
Stéphane Borel's avatar
Stéphane Borel committed
533

534
//fprintf( stderr, "VMGI\n" );
Stéphane Borel's avatar
Stéphane Borel committed
535
536
537
538
539
540
541
542
543
544
545
546
547
548

    GET( mat.psz_id , 12 );
    mat.psz_id[12] = '\0';
    GETL( &mat.i_lsector );
    FLUSH( 12 );
    GETL( &mat.i_i_lsector );
    FLUSH( 1 );
    GETC( &mat.i_spec_ver );
    GETL( &mat.i_cat );
    GETS( &mat.i_vol_nb );
    GETS( &mat.i_vol );
    GETC( &mat.i_disc_side );
    FLUSH( 19 );
    GETS( &mat.i_tts_nb );
Stéphane Borel's avatar
Stéphane Borel committed
549
    GET( mat.ps_provider_id, 32 );
Stéphane Borel's avatar
Stéphane Borel committed
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
    GETLL( &mat.i_pos_code );
    FLUSH( 24 );
    GETL( &mat.i_i_mat_ebyte );
    GETL( &mat.i_fp_pgc_sbyte );
    FLUSH( 56 );
    GETL( &mat.i_vobs_ssector );
    GETL( &mat.i_ptt_srpt_ssector );
    GETL( &mat.i_pgci_ut_ssector );
    GETL( &mat.i_ptl_mait_ssector );
    GETL( &mat.i_vts_atrt_ssector );
    GETL( &mat.i_txtdt_mg_ssector );
    GETL( &mat.i_c_adt_ssector );
    GETL( &mat.i_vobu_admap_ssector );
    FLUSH( 32 );
    GETS( &mat.i_video_atrt );
    FLUSH( 1 );
    GETC( &mat.i_audio_nb );
Stéphane Borel's avatar
Stéphane Borel committed
567
//fprintf( stderr, "vmgi audio nb : %d\n", mat.i_audio_nb );
Stéphane Borel's avatar
Stéphane Borel committed
568
569
570
571
572
573
    for( i=0 ; i < 8 ; i++ )
    {
        GETLL( &mat.pi_audio_atrt[i] );
    }
    FLUSH( 17 );
    GETC( &mat.i_subpic_nb );
Stéphane Borel's avatar
Stéphane Borel committed
574
//fprintf( stderr, "vmgi subpic nb : %d\n", mat.i_subpic_nb );
Stéphane Borel's avatar
Stéphane Borel committed
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
    for( i=0 ; i < mat.i_subpic_nb ; i++ )
    {
        GET( &mat.pi_subpic_atrt[i], 6 );
        /* FIXME : take care of endianness */
    }

    return mat;
}

/*****************************************************************************
 * ReadVMGTitlePointer : Fills the Part Of Title Search Pointer structure.
 *****************************************************************************/
static vmg_ptt_srpt_t ReadVMGTitlePointer( ifo_t* p_ifo )
{
    vmg_ptt_srpt_t  ptr;
    int             i;
591
//    off_t           i_start = p_ifo->i_pos;
Stéphane Borel's avatar
Stéphane Borel committed
592

593
//fprintf( stderr, "PTR\n" );
Stéphane Borel's avatar
Stéphane Borel committed
594
595

    GETS( &ptr.i_ttu_nb );
596
//fprintf( stderr, "PTR: TTU nb %d\n", ptr.i_ttu_nb );
Stéphane Borel's avatar
Stéphane Borel committed
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
    FLUSH( 2 );
    GETL( &ptr.i_ebyte );
    /* Parsing of tts */
    ptr.p_tts = malloc( ptr.i_ttu_nb *sizeof(tts_t) );
    if( ptr.p_tts == NULL )
    {
        intf_ErrMsg( "Out of memory" );
        p_ifo->b_error = 1;
        return ptr;
    }
    for( i=0 ; i<ptr.i_ttu_nb ; i++ )
    {
        GETC( &ptr.p_tts[i].i_play_type );
        GETC( &ptr.p_tts[i].i_angle_nb );
        GETS( &ptr.p_tts[i].i_ptt_nb );
        GETS( &ptr.p_tts[i].i_parental_id );
        GETC( &ptr.p_tts[i].i_tts_nb );
        GETC( &ptr.p_tts[i].i_vts_ttn );
        GETL( &ptr.p_tts[i].i_ssector );
616
//fprintf( stderr, "PTR: %d %d %d\n", ptr.p_tts[i].i_ptt_nb, ptr.p_tts[i].i_tts_nb,ptr.p_tts[i].i_vts_ttn );
Stéphane Borel's avatar
Stéphane Borel committed
617
618
619
620
621
622
623
624
625
626
627
628
    }

    return ptr;
}

/*****************************************************************************
 * ReadParentalInf : Fills the Parental Management structure.
 *****************************************************************************/
static vmg_ptl_mait_t ReadParentalInf( ifo_t* p_ifo )
{
    vmg_ptl_mait_t  par;
    int             i, j, k;
629
    off_t           i_start = p_ifo->i_pos;
Stéphane Borel's avatar
Stéphane Borel committed
630

631
//fprintf( stderr, "PTL\n" );
Stéphane Borel's avatar
Stéphane Borel committed
632
633
634
635
636
637
638
639
640
641
642
643
644

    GETS( &par.i_country_nb );
    GETS( &par.i_vts_nb );
    GETL( &par.i_ebyte );
    par.p_ptl_desc = malloc( par.i_country_nb *sizeof(vmg_ptl_mai_desc_t) );
    if( par.p_ptl_desc == NULL )
    {
        intf_ErrMsg( "Out of memory" );
        p_ifo->b_error = 1;
        return par;
    }
    for( i=0 ; i<par.i_country_nb ; i++ )
    {
Stéphane Borel's avatar
Stéphane Borel committed
645
        GET( par.p_ptl_desc[i].ps_country_code, 2 );
Stéphane Borel's avatar
Stéphane Borel committed
646
647
648
649
650
651
652
653
654
655
656
657
658
        FLUSH( 2 );
        GETS( &par.p_ptl_desc[i].i_ptl_mai_sbyte );
        FLUSH( 2 );
    }
    par.p_ptl_mask = malloc( par.i_country_nb *sizeof(vmg_ptl_mask_t) );
    if( par.p_ptl_mask == NULL )
    {
        intf_ErrMsg( "Out of memory" );
        p_ifo->b_error = 1;
        return par;
    }
    for( i=0 ; i<par.i_country_nb ; i++ )
    {
659
        p_ifo->i_pos = lseek( p_ifo->i_fd, i_start +
Stéphane Borel's avatar
Stéphane Borel committed
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
                         par.p_ptl_desc[i].i_ptl_mai_sbyte, SEEK_SET );
        for( j=1 ; j<=8 ; j++ )
        {
            par.p_ptl_mask[i].ppi_ptl_mask[j] =
                                    malloc( par.i_vts_nb *sizeof(u16) );
            if( par.p_ptl_mask[i].ppi_ptl_mask[j] == NULL )
            {
                intf_ErrMsg( "Out of memory" );
                p_ifo->b_error = 1;
                return par;
            }        
            for( k=0 ; k<par.i_vts_nb ; k++ )
            {
                GETS( &par.p_ptl_mask[i].ppi_ptl_mask[j][k] );
            }
        }
    }

    return par;
}

/*****************************************************************************
 * ReadVTSAttr : Fills the structure about VTS attributes.
 *****************************************************************************/
static vmg_vts_atrt_t ReadVTSAttr( ifo_t* p_ifo )
{
    vmg_vts_atrt_t  atrt;
    int             i, j;
688
    off_t           i_start = p_ifo->i_pos;
Stéphane Borel's avatar
Stéphane Borel committed
689

690
//fprintf( stderr, "VTS ATTR\n" );
Stéphane Borel's avatar
Stéphane Borel committed
691
692

    GETS( &atrt.i_vts_nb );
693
//fprintf( stderr, "VTS ATTR Nb: %d\n", atrt.i_vts_nb );
Stéphane Borel's avatar
Stéphane Borel committed
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
    FLUSH( 2 );
    GETL( &atrt.i_ebyte );
    atrt.pi_vts_atrt_sbyte = malloc( atrt.i_vts_nb *sizeof(u32) );
    if( atrt.pi_vts_atrt_sbyte == NULL )
    {
        intf_ErrMsg( "Out of memory" );
        p_ifo->b_error = 1;
        return atrt;
    }
    for( i=0 ; i<atrt.i_vts_nb ; i++ )
    {
        GETL( &atrt.pi_vts_atrt_sbyte[i] );
    }
    atrt.p_vts_atrt = malloc( atrt.i_vts_nb *sizeof(vts_atrt_t) );
    if( atrt.p_vts_atrt == NULL )
    {
        intf_ErrMsg( "Out of memory" );
        p_ifo->b_error = 1;
        return atrt;
    }
    for( i=0 ; i<atrt.i_vts_nb ; i++ )
    {
716
        p_ifo->i_pos = lseek( p_ifo->i_fd, i_start +
717
                                atrt.pi_vts_atrt_sbyte[i],
Stéphane Borel's avatar
Stéphane Borel committed
718
719
720
721
722
723
                                SEEK_SET );
        GETL( &atrt.p_vts_atrt[i].i_ebyte );
        GETL( &atrt.p_vts_atrt[i].i_cat_app_type );
        GETS( &atrt.p_vts_atrt[i].i_vtsm_video_atrt );
        FLUSH( 1 );
        GETC( &atrt.p_vts_atrt[i].i_vtsm_audio_nb );
Stéphane Borel's avatar
Stéphane Borel committed
724
//fprintf( stderr, "m audio nb : %d\n", atrt.p_vts_atrt[i].i_vtsm_audio_nb );
725
        for( j=0 ; j<8 ; j++ )
Stéphane Borel's avatar
Stéphane Borel committed
726
727
728
        {
            GETLL( &atrt.p_vts_atrt[i].pi_vtsm_audio_atrt[j] );
        }
729
        FLUSH( 17 );
Stéphane Borel's avatar
Stéphane Borel committed
730
        GETC( &atrt.p_vts_atrt[i].i_vtsm_subpic_nb );
Stéphane Borel's avatar
Stéphane Borel committed
731
//fprintf( stderr, "m subp nb : %d\n", atrt.p_vts_atrt[i].i_vtsm_subpic_nb );
732
        for( j=0 ; j<28 ; j++ )
Stéphane Borel's avatar
Stéphane Borel committed
733
734
735
736
        {
            GET( &atrt.p_vts_atrt[i].pi_vtsm_subpic_atrt[j], 6 );
            /* FIXME : Fix endianness issue here */
        }
737
        FLUSH( 2 );
Stéphane Borel's avatar
Stéphane Borel committed
738
739
740
        GETS( &atrt.p_vts_atrt[i].i_vtstt_video_atrt );
        FLUSH( 1 );
        GETL( &atrt.p_vts_atrt[i].i_vtstt_audio_nb );
Stéphane Borel's avatar
Stéphane Borel committed
741
//fprintf( stderr, "tt audio nb : %d\n", atrt.p_vts_atrt[i].i_vtstt_audio_nb );
742
        for( j=0 ; j<8 ; j++ )
Stéphane Borel's avatar
Stéphane Borel committed
743
744
745
        {
            GETLL( &atrt.p_vts_atrt[i].pi_vtstt_audio_atrt[j] );
        }
746
        FLUSH( 17 );
Stéphane Borel's avatar
Stéphane Borel committed
747
        GETC( &atrt.p_vts_atrt[i].i_vtstt_subpic_nb );
Stéphane Borel's avatar
Stéphane Borel committed
748
749
//fprintf( stderr, "tt subp nb : %d\n", atrt.p_vts_atrt[i].i_vtstt_subpic_nb );
        for( j=0 ; j<28/*atrt.p_vts_atrt[i].i_vtstt_subpic_nb*/ ; j++ )
Stéphane Borel's avatar
Stéphane Borel committed
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
        {
            GET( &atrt.p_vts_atrt[i].pi_vtstt_subpic_atrt[j], 6 );
            /* FIXME : Fix endianness issue here */
        }
    }

    return atrt;
}
                           
/*****************************************************************************
 * ReadVMG : Parse video_ts.ifo file to fill the Video Manager structure.
 *****************************************************************************/
static vmg_t ReadVMG( ifo_t* p_ifo )
{
    vmg_t       vmg;

766
    p_ifo->i_pos = lseek( p_ifo->i_fd, p_ifo->i_off, SEEK_SET);
Stéphane Borel's avatar
Stéphane Borel committed
767
    vmg.mat = ReadVMGInfMat( p_ifo );
768
    p_ifo->i_pos = lseek( p_ifo->i_fd, p_ifo->i_off + 
Stéphane Borel's avatar
Stéphane Borel committed
769
770
771
772
                              vmg.mat.i_fp_pgc_sbyte, SEEK_SET );
    vmg.pgc = ReadPGC( p_ifo );
    if( vmg.mat.i_ptt_srpt_ssector )
    {
773
        p_ifo->i_pos = lseek( p_ifo->i_fd, p_ifo->i_off +
Stéphane Borel's avatar
Stéphane Borel committed
774
775
776
777
778
779
                        vmg.mat.i_ptt_srpt_ssector *DVD_LB_SIZE,
                        SEEK_SET );
        vmg.ptt_srpt = ReadVMGTitlePointer( p_ifo );
    }
    if( vmg.mat.i_pgci_ut_ssector )
    {
780
        p_ifo->i_pos = lseek( p_ifo->i_fd, p_ifo->i_off +
Stéphane Borel's avatar
Stéphane Borel committed
781
782
783
784
785
786
                        vmg.mat.i_pgci_ut_ssector *DVD_LB_SIZE,
                        SEEK_SET );
        vmg.pgci_ut = ReadUnitTable( p_ifo );
    }
    if( vmg.mat.i_ptl_mait_ssector )
    {
787
        p_ifo->i_pos = lseek( p_ifo->i_fd, p_ifo->i_off +
Stéphane Borel's avatar
Stéphane Borel committed
788
789
790
791
792
793
                        vmg.mat.i_ptl_mait_ssector *DVD_LB_SIZE,
                        SEEK_SET );
        vmg.ptl_mait = ReadParentalInf( p_ifo );
    }
    if( vmg.mat.i_vts_atrt_ssector )
    {
794
        p_ifo->i_pos = lseek( p_ifo->i_fd, p_ifo->i_off +
Stéphane Borel's avatar
Stéphane Borel committed
795
796
797
798
799
800
                        vmg.mat.i_vts_atrt_ssector *DVD_LB_SIZE,
                        SEEK_SET );
        vmg.vts_atrt = ReadVTSAttr( p_ifo );
    }
    if( vmg.mat.i_c_adt_ssector )
    {
801
        p_ifo->i_pos = lseek( p_ifo->i_fd, p_ifo->i_off +
Stéphane Borel's avatar
Stéphane Borel committed
802
803
804
805
806
807
                        vmg.mat.i_c_adt_ssector *DVD_LB_SIZE,
                        SEEK_SET );
        vmg.c_adt = ReadCellInf( p_ifo );
    }
    if( vmg.mat.i_vobu_admap_ssector )
    {
808
        p_ifo->i_pos = lseek( p_ifo->i_fd, p_ifo->i_off +
Stéphane Borel's avatar
Stéphane Borel committed
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
                        vmg.mat.i_vobu_admap_ssector *DVD_LB_SIZE,
                        SEEK_SET );
        vmg.vobu_admap = ReadMap( p_ifo );
    }
    return vmg;
}

/*
 * Video Title Set Information Processing.
 * This is what is contained in vts_*.ifo.
 */

/*****************************************************************************
 * ReadVTSInfMat : Fills the Title Set Information structure.
 *****************************************************************************/
static vtsi_mat_t ReadVTSInfMat( ifo_t* p_ifo )
{
    vtsi_mat_t  mat;
    int         i;
828
//    off_t       i_start = p_ifo->i_pos;
Stéphane Borel's avatar
Stéphane Borel committed
829

830
//fprintf( stderr, "VTSI\n" );
Stéphane Borel's avatar
Stéphane Borel committed
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871

    GET( mat.psz_id , 12 );
    mat.psz_id[12] = '\0';
    GETL( &mat.i_lsector );
    FLUSH( 12 );
    GETL( &mat.i_i_lsector );
    FLUSH( 1 );
    GETC( &mat.i_spec_ver );
    GETL( &mat.i_cat );
    FLUSH( 90 );
    GETL( &mat.i_mat_ebyte );
    FLUSH( 60 );
    GETL( &mat.i_m_vobs_ssector );
    GETL( &mat.i_tt_vobs_ssector );
    GETL( &mat.i_ptt_srpt_ssector );
    GETL( &mat.i_pgcit_ssector );
    GETL( &mat.i_m_pgci_ut_ssector );
    GETL( &mat.i_tmap_ti_ssector );
    GETL( &mat.i_m_c_adt_ssector );
    GETL( &mat.i_m_vobu_admap_ssector );
    GETL( &mat.i_c_adt_ssector );
    GETL( &mat.i_vobu_admap_ssector );
    FLUSH( 24 );
    GETS( &mat.i_m_video_atrt );
    FLUSH( 1 );
    GETC( &mat.i_m_audio_nb );
    for( i=0 ; i<8 ; i++ )
    {
        GETLL( &mat.pi_m_audio_atrt[i] );
    }
    FLUSH( 17 );
    GETC( &mat.i_m_subpic_nb );
    for( i=0 ; i<28 ; i++ )
    {
        GET( &mat.pi_m_subpic_atrt[i], 6 );
        /* FIXME : take care of endianness */
    }
    FLUSH( 2 );
    GETS( &mat.i_video_atrt );
    FLUSH( 1 );
    GETC( &mat.i_audio_nb );
Stéphane Borel's avatar
Stéphane Borel committed
872
//fprintf( stderr, "vtsi audio nb : %d\n", mat.i_audio_nb );
Stéphane Borel's avatar
Stéphane Borel committed
873
874
875
876
877
878
    for( i=0 ; i<8 ; i++ )
    {
        GETLL( &mat.pi_audio_atrt[i] );
    }
    FLUSH( 17 );
    GETC( &mat.i_subpic_nb );
Stéphane Borel's avatar
Stéphane Borel committed
879
//fprintf( stderr, "vtsi subpic nb : %d\n", mat.i_subpic_nb );
Stéphane Borel's avatar
Stéphane Borel committed
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
    for( i=0 ; i<mat.i_subpic_nb ; i++ )
    {
        GET( &mat.pi_subpic_atrt[i], 6 );
        /* FIXME : take care of endianness */
    }

    return mat;
}

/*****************************************************************************
 * ReadVTSTitlePointer : Fills the Part Of Title Search Pointer structure.
 *****************************************************************************/
static vts_ptt_srpt_t ReadVTSTitlePointer( ifo_t* p_ifo )
{
    vts_ptt_srpt_t  ptr;
    int             i;
896
    off_t           i_start = p_ifo->i_pos;
Stéphane Borel's avatar
Stéphane Borel committed
897

898
//fprintf( stderr, "VTS PTR\n" );
Stéphane Borel's avatar
Stéphane Borel committed
899
900

    GETS( &ptr.i_ttu_nb );
901
//fprintf( stderr, "VTS PTR nb: %d\n", ptr.i_ttu_nb );
Stéphane Borel's avatar
Stéphane Borel committed
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
    FLUSH( 2 );
    GETL( &ptr.i_ebyte );
    ptr.pi_ttu_sbyte = malloc( ptr.i_ttu_nb *sizeof(u32) );
    if( ptr.pi_ttu_sbyte == NULL )
    {
        intf_ErrMsg( "Out of memory" );
        p_ifo->b_error = 1;
        return ptr;
    }
    for( i=0 ; i<ptr.i_ttu_nb ; i++ )
    {
        GETL( &ptr.pi_ttu_sbyte[i] );
    }
    /* Parsing of tts */
    ptr.p_ttu = malloc( ptr.i_ttu_nb *sizeof(ttu_t) );
    if( ptr.p_ttu == NULL )
    {
        intf_ErrMsg( "Out of memory" );
        p_ifo->b_error = 1;
        return ptr;
    }
    for( i=0 ; i<ptr.i_ttu_nb ; i++ )
    {
925
        p_ifo->i_pos = lseek( p_ifo->i_fd, i_start +
Stéphane Borel's avatar
Stéphane Borel committed
926
927
928
                        ptr.pi_ttu_sbyte[i], SEEK_SET );
        GETS( &ptr.p_ttu[i].i_pgc_nb );
        GETS( &ptr.p_ttu[i].i_prg_nb );
929
//fprintf( stderr, "VTS %d PTR Pgc: %d Prg: %d\n", i,ptr.p_ttu[i].i_pgc_nb, ptr.p_ttu[i].i_prg_nb );
Stéphane Borel's avatar
Stéphane Borel committed
930
931
932
933
934
935
936
937
938
939
940
941
    }

    return ptr;
}

/*****************************************************************************
 * ReadVTSTimeMap : Fills the time map table
 *****************************************************************************/
static vts_tmap_ti_t ReadVTSTimeMap( ifo_t* p_ifo )
{
    vts_tmap_ti_t   tmap;
    int             i,j;
942
//    off_t           i_start = p_ifo->i_pos;
Stéphane Borel's avatar
Stéphane Borel committed
943

944
//fprintf( stderr, "TMAP\n" );
Stéphane Borel's avatar
Stéphane Borel committed
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990

    GETS( &tmap.i_nb );
    FLUSH( 2 );
    GETL( &tmap.i_ebyte );
    tmap.pi_sbyte = malloc( tmap.i_nb *sizeof(u32) );
    if( tmap.pi_sbyte == NULL )
    {
        intf_ErrMsg( "Out of memory" );
        p_ifo->b_error = 1;
        return tmap;
    }
    for( i=0 ; i<tmap.i_nb ; i++ )
    {    
        GETL( &tmap.pi_sbyte[i] );
    }
    tmap.p_tmap = malloc( tmap.i_nb *sizeof(tmap_t) );
    if( tmap.p_tmap == NULL )
    {
        intf_ErrMsg( "Out of memory" );
        p_ifo->b_error = 1;
        return tmap;
    }
    for( i=0 ; i<tmap.i_nb ; i++ )
    {    
        GETC( &tmap.p_tmap[i].i_time_unit );
        FLUSH( 1 );
        GETS( &tmap.p_tmap[i].i_entry_nb );
        tmap.p_tmap[i].pi_sector =
                    malloc( tmap.p_tmap[i].i_entry_nb *sizeof(u32) );
        if( tmap.p_tmap[i].pi_sector == NULL )
        {
            intf_ErrMsg( "Out of memory" );
            p_ifo->b_error = 1;
            return tmap;
        }
        for( j=0 ; j<tmap.p_tmap[i].i_entry_nb ; j++ )
        {
            GETL( &tmap.p_tmap[i].pi_sector[j] );
        }
    }

    return tmap;
}
    

/*****************************************************************************
Stéphane Borel's avatar
   
Stéphane Borel committed
991
 * IfoReadVTS : Parse vts*.ifo files to fill the Video Title Set structure.
Stéphane Borel's avatar
Stéphane Borel committed
992
 *****************************************************************************/
Stéphane Borel's avatar
   
Stéphane Borel committed
993
int IfoReadVTS( ifo_t* p_ifo )
Stéphane Borel's avatar
Stéphane Borel committed
994
995
{
    vts_t       vts;
Stéphane Borel's avatar
   
Stéphane Borel committed
996
997
998
999
1000
1001
1002
1003
1004
1005
    off_t       i_off;
    int         i_title;

    intf_WarnMsg( 2, "ifo: initializing VTS %d", p_ifo->i_title );

    i_title = p_ifo->i_title;
    i_off = (off_t)( p_ifo->vmg.ptt_srpt.p_tts[i_title].i_ssector ) *DVD_LB_SIZE
                   + p_ifo->i_off;

    p_ifo->i_pos = lseek( p_ifo->i_fd, i_off, SEEK_SET );
Stéphane Borel's avatar
Stéphane Borel committed
1006
1007
1008
1009
1010
1011

    vts.i_pos = p_ifo->i_pos;

    vts.mat = ReadVTSInfMat( p_ifo );
    if( vts.mat.i_ptt_srpt_ssector )
    {
1012
        p_ifo->i_pos = lseek( p_ifo->i_fd, vts.i_pos +
Stéphane Borel's avatar
Stéphane Borel committed
1013
1014
1015
1016
1017
1018
                        vts.mat.i_ptt_srpt_ssector *DVD_LB_SIZE,
                        SEEK_SET );
        vts.ptt_srpt = ReadVTSTitlePointer( p_ifo );
    }
    if( vts.mat.i_m_pgci_ut_ssector )
    {
1019
        p_ifo->i_pos = lseek( p_ifo->i_fd, vts.i_pos +
Stéphane Borel's avatar
Stéphane Borel committed
1020
1021
1022
1023
1024
1025
                        vts.mat.i_m_pgci_ut_ssector *DVD_LB_SIZE,
                        SEEK_SET );
        vts.pgci_ut = ReadUnitTable( p_ifo );
    }
    if( vts.mat.i_pgcit_ssector )
    {
1026
        p_ifo->i_pos = lseek( p_ifo->i_fd, vts.i_pos +
Stéphane Borel's avatar
Stéphane Borel committed
1027
1028
                        vts.mat.i_pgcit_ssector *DVD_LB_SIZE,
                        SEEK_SET );
1029
        vts.pgci_ti = ReadUnit( p_ifo );
Stéphane Borel's avatar
Stéphane Borel committed
1030
1031
1032
    }
    if( vts.mat.i_tmap_ti_ssector )
    {
1033
        p_ifo->i_pos = lseek( p_ifo->i_fd, vts.i_pos +
Stéphane Borel's avatar
Stéphane Borel committed
1034
1035
1036
1037
1038
1039
                        vts.mat.i_tmap_ti_ssector *DVD_LB_SIZE,
                        SEEK_SET );
        vts.tmap_ti = ReadVTSTimeMap( p_ifo );
    }
    if( vts.mat.i_m_c_adt_ssector )
    {
1040
        p_ifo->i_pos = lseek( p_ifo->i_fd, vts.i_pos +
Stéphane Borel's avatar
Stéphane Borel committed
1041
1042
1043
1044
1045
1046
                        vts.mat.i_m_c_adt_ssector *DVD_LB_SIZE,
                        SEEK_SET );
        vts.m_c_adt = ReadCellInf( p_ifo );
    }
    if( vts.mat.i_m_vobu_admap_ssector )
    {
1047
        p_ifo->i_pos = lseek( p_ifo->i_fd, vts.i_pos +
Stéphane Borel's avatar
Stéphane Borel committed
1048
1049
1050
1051
1052
1053
                        vts.mat.i_m_vobu_admap_ssector *DVD_LB_SIZE,
                        SEEK_SET );
        vts.m_vobu_admap = ReadMap( p_ifo );
    }
    if( vts.mat.i_c_adt_ssector )
    {
1054
        p_ifo->i_pos = lseek( p_ifo->i_fd, vts.i_pos +
Stéphane Borel's avatar
Stéphane Borel committed
1055
1056
1057
1058
1059
1060
                        vts.mat.i_c_adt_ssector *DVD_LB_SIZE,
                        SEEK_SET );
        vts.c_adt = ReadCellInf( p_ifo );
    }
    if( vts.mat.i_vobu_admap_ssector )
    {
1061
        p_ifo->i_pos = lseek( p_ifo->i_fd, vts.i_pos +
Stéphane Borel's avatar
Stéphane Borel committed
1062
1063
1064
1065
1066
                        vts.mat.i_vobu_admap_ssector *DVD_LB_SIZE,
                        SEEK_SET );
        vts.vobu_admap = ReadMap( p_ifo );
    }

Stéphane Borel's avatar
   
Stéphane Borel committed
1067
1068
1069
    p_ifo->vts = vts;

    return 0;
Stéphane Borel's avatar
Stéphane Borel committed
1070
1071
1072
1073
1074
}

/*
 * DVD Information Management
 */
Stéphane Borel's avatar
   
Stéphane Borel committed
1075
#if 0
Stéphane Borel's avatar
Stéphane Borel committed
1076
1077
1078
1079
1080
1081
1082
/*****************************************************************************
 * IfoRead : Function that fills structure and calls specified functions
 * to do it.
 *****************************************************************************/
void IfoRead( ifo_t* p_ifo )
{
    int     i;
1083
    off_t   i_off;
1084

1085
    /* Video Title Sets initialization */
Stéphane Borel's avatar
Stéphane Borel committed
1086
1087
1088
1089
1090
1091
1092
    p_ifo->p_vts = malloc( p_ifo->vmg.mat.i_tts_nb *sizeof(vts_t) );
    if( p_ifo->p_vts == NULL )
    {
        intf_ErrMsg( "Out of memory" );
        p_ifo->b_error = 1;
        return;
    }
1093

1094
    for( i=0 ; i<p_ifo->vmg.mat.i_tts_nb ; i++ )
Stéphane Borel's avatar
Stéphane Borel committed
1095
    {
1096

1097
        intf_WarnMsg( 2, "ifo: initializing VTS %d", i+1 );
1098

1099
1100
1101
        i_off = (off_t)( p_ifo->vmg.ptt_srpt.p_tts[i].i_ssector ) *DVD_LB_SIZE
                       + p_ifo->i_off;

1102
        p_ifo->i_pos = lseek( p_ifo->i_fd, i_off, SEEK_SET );
1103

1104
1105
1106
        /* FIXME : I really don't know why udf find file
         * does not give the exact beginning of file */

Stéphane Borel's avatar
Stéphane Borel committed
1107
        p_ifo->p_vts[i] = ReadVTS( p_ifo );
1108

Stéphane Borel's avatar
Stéphane Borel committed
1109
    }
1110

Stéphane Borel's avatar
Stéphane Borel committed
1111
1112
    return; 
}
Stéphane Borel's avatar
   
Stéphane Borel committed
1113
#endif
1114
/*
1115
1116
 * IFO virtual machine : a set of commands that give the
 * interactive behaviour of the dvd
1117
 */
1118
1119
1120
1121
1122
1123
#if 1

#define OP_VAL_16(i) (ntoh16( com.data.pi_16[i]))
#define OP_VAL_8(i) ((com.data.pi_8[i]))

static char ifo_reg[][80]=
1124
{
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
    "Menu_Language_Code",
    "Audio_Stream_#",
    "SubPicture_Stream_#",
    "Angle_#",
    "VTS_#",
    "VTS_Title_#",
    "PGC_#",
    "PTT_#",
    "Highlighted_Button_#",
    "Nav_Timer",
    "TimedPGC",
    "Karaoke_audio_mixing_mode",
    "Parental_mgmt_country_code",
    "Parental_Level",
    "Player_Video_Cfg",
    "Player_Audio_Cfg",
    "Audio_language_code_setting",
    "Audio_language_extension_code",
    "SPU_language_code_setting",
    "SPU_language_extension_code",
    "?Player_Regional_Code",
    "Reserved_21",
    "Reserved_22",
    "Reserved_23"
};

static char * IfoMath( char val )
{
    static char math_op[][10] =
    {
        "none",
        "=", 
        "<->",    // swap
        "+=",
        "-=",
        "*=",
        "/=",
        "%=",
        "rnd",    // rnd
        "&=",
        "|=",
        "^=",
        "??",    // invalid
        "??",    // invalid
        "??",    // invalid
        "??"    // invalid
    };

    return (char *) math_op[val & 0x0f];
}
1175

1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220

char ifo_cmp[][10] =
{
    "none",
    "&&",
    "==",
    "!=",
    ">=",
    ">",
    "<",
    "<="
};

char ifo_parental[][10] =
{
    "0",
    "G",
    "2",
    "PG",
    "PG-13",
    "5",
    "R",
    "NC-17"
};

char ifo_menu_id[][80] =
{
    "-0-",
    "-1-",
    "Title (VTS menu)",
    "Root",
    "Sub-Picture",
    "Audio",
    "Angle",
    "Part of Title",
};

char * IfoMenuName( char index )
{
    return ifo_menu_id[index&0x07];
}

static void IfoRegister( u16 i_data, u8 i_direct)
{
    if( i_direct )
1221
    {
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
        if( 0/*isalpha( i_data >> 8 & 0xff )*/ )
        {
            printf("'%c%c'", i_data>>8&0xff, i_data&0xff);
        }
        else
        {
            printf("0x%02x", i_data);
        }
    }
    else
    {
        if( i_data & 0x80 )
        {
            i_data &= 0x1f;

            if( i_data > 0x17 )
1238
            {
1239
                printf("s[ILL]");
1240
            }
1241
            else
1242
            {
1243
                printf("s[%s]", ifo_reg[i_data]);
1244
            }
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
        }
        else
        {
            i_data &= 0x1f;

            if( i_data > 0xf )
            {
                printf("r[ILL]");
            }
            else
            {
                printf("r[0x%02x]", i_data);
            }
        }
    }
}

static void IfoAdvanced( u8 *pi_code )
{
    u8      i_cmd = pi_code[0];

    printf(" { ");

    if( pi_code[1]>>2 )
    {
        printf( " Highlight button %d; ", pi_code[1]>>2 );
    }

    if( i_cmd == 0xff )
    {
        printf( " Illegal " );
    }

    if( i_cmd == 0x00 )
    {
        printf( "ReSuME %d", pi_code[7] );
    }
    else if( ( i_cmd & 0x06) == 0x02 )
    {    // XX01Y
        printf ("Link to %s cell ", ( i_cmd & 0x01 ) ? "prev" : "next");
    }
    else
    {
        printf( "advanced (0x%02x) ", i_cmd );
    }
    printf(" } ");
}

static void IfoJmp( ifo_command_t com )
{

    printf ("jmp ");

    switch( com.i_sub_cmd )
    {
    case 0x01:
        printf( "Exit" );
        break;
    case 0x02:
        printf( "VTS 0x%02x", OP_VAL_8(3) );
        break;
    case 0x03:
        printf( "This VTS Title 0x%02x", OP_VAL_8(3) );
        break;
    case 0x05:
        printf( "This VTS Title 0x%02x Part 0x%04x",
                            OP_VAL_8(3),
                            OP_VAL_8(0)<<8|OP_VAL_8(1));
        break;
    case 0x06:
#if 0
            printf ("in SystemSpace ");
            switch (OP_VAL_8(3)>>4) {
                case 0x00:
                    printf ("to play first PGC");
                    break;
                case 0x01: {
                    printf ("to menu \"%s\"", decode_menuname (OP_VAL_8(3)));
                }
                    break;
                case 0x02:
                    printf ("to VTS 0x%02x and TTN 0x%02x", OP_VAL_8(1), OP_VAL_8(2));
                    break;
                case 0x03:
                    printf ("to VMGM PGC number 0x%02x", OP_VAL_8(0)<<8 | OP_VAL_8(1));
                    break;
                case 0x08:
                    printf ("vts 0x%02x lu 0x%02x menu \"%s\"", OP_VAL_8(2), OP_VAL_8(1), decode_menuname (OP_VAL_8(3)));
                    break;
#else
        switch( OP_VAL_8(3)>>6 )
        {
        case 0x00:
            printf( "to play first PGC" );
            break;                
        case 0x01:
            printf( "to VMG title menu (?)" );
1342
            break;
1343
1344
1345
1346
1347
1348
1349
1350
        case 0x02:
            printf( "vts 0x%02x lu 0x%02x menu \"%s\"",
                            OP_VAL_8(2),
                            OP_VAL_8(1),
                            IfoMenuName( OP_VAL_8(3)&0xF ) );
            break;                
        case 0x03:
            printf( "vmg pgc 0x%04x (?)", ( OP_VAL_8(0)<<8) | OP_VAL_8(1) );
1351
            break;
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
#endif
        }
        break;
    case 0x08:
#if 0
            switch(OP_VAL_8(3)>>4) {
                case 0x00:
                    printf ("system first pgc");
                    break;
                case 0x01:
                    printf ("system title menu");
                    break;
                case 0x02:
                    printf ("system menu \"%s\"", decode_menuname (OP_VAL_8(3)));
                    break;
                case 0x03:
                    printf ("system vmg pgc %02x ????", OP_VAL_8(0)<<8|OP_VAL_8(1));
                    break;
                case 0x08:
                    printf ("system lu 0x%02x menu \"%s\"", OP_VAL_8(2), decode_menuname (OP_VAL_8(3)));
                    break;
                case 0x0c:
                    printf ("system vmg pgc 0x%02x", OP_VAL_8(0)<<8|OP_VAL_8(1));
                    break;
            }
#else
        // OP_VAL_8(2) is number of cell
        // it is processed BEFORE switch
        // under some conditions, it is ignored
        // I don't understand exactly what it means
        printf( " ( spec cell 0x%02X ) ", OP_VAL_8(2) ); 

        switch( OP_VAL_8(3)>>6 )
        {
        case 0:
            printf( "to FP PGC" );
1388
            break;
1389
1390
        case 1:
            printf( "to VMG root menu (?)" );
1391
            break;
1392
1393
1394
1395
1396
1397
        case 2:
            printf( "to VTS menu \"%s\" (?)",
                    IfoMenuName(OP_VAL_8(3)&0xF) );
            break; 
        case 3:
            printf( "vmg pgc 0x%02x (?)", (OP_VAL_8(0)<<8)|OP_VAL_8(1) );
1398
            break;
1399
1400
1401
        }    
#endif
        break;
1402
    }
1403
}
1404

1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
static void IfoLnk( ifo_command_t com )
{
    u16     i_button=OP_VAL_8(4)>>2;

    printf ("lnk to ");

    switch( com.i_sub_cmd )
    {
    case 0x01:
        IfoAdvanced( &OP_VAL_8(4) );
        break;

    case 0x04:
        printf( "PGC 0x%02x", OP_VAL_16(2) );
        break; 

    case 0x05:
        printf( "PTT 0x%02x", OP_VAL_16(2) );
        break; 

    case 0x06:
        printf( "Program 0x%02x this PGC", OP_VAL_8(5) );
        break;

    case 0x07:
        printf( "Cell 0x%02x this PGC", OP_VAL_8(5) );
        break;
    default:
        return;
    }

    if( i_button )
    {
        printf( ", Highlight 0x%02x", OP_VAL_8(4)>>2 );
    }
            
1441
1442
}

1443
void IfoSetSystem( ifo_command_t com )
1444
{
1445
1446
1447
1448
    switch( com.i_cmd )
    {
    case 1: {
        int i;
1449

1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
        for( i=1; i<=3; i++ )
        {
            if( OP_VAL_8(i)&0x80 )
            {
                if( com.i_direct )
                {
                    printf ("s[%s] = 0x%02x;", ifo_reg[i], OP_VAL_8(i)&0xf);
                }
                else
                {
                    printf ("s[%s] = r[0x%02x];", ifo_reg[i], OP_VAL_8(i)&0xf);
                }
            }
        }
#if 0
                if(op->direct) {
                        if(OP_VAL_8(1]&0x80)
                                printf ("s[%s] = 0x%02x;", reg_name[1], OP_VAL_8(1]&0xf);
                        if(OP_VAL_8(2)&0x80)
//DENT: lwhat about 0x7f here ???
                                printf ("s[%s] = 0x%02x;", reg_name[2], OP_VAL_8(2)&0x7f);
                        if(OP_VAL_8(3)&0x80)
                                printf ("s[%s] = 0x%02x;", reg_name[3], OP_VAL_8(3)&0xf);
                } else {
                        if(OP_VAL_8(1)&0x80)
                                printf ("s[%s] = r[0x%02x];", reg_name[1], OP_VAL_8(1)&0xf);
                        if(OP_VAL_8(2)&0x80)
                                printf ("s[%s] = r[0x%02x];", reg_name[2], OP_VAL_8(2)&0xf);
                        if(OP_VAL_8(3)&0x80)
                                printf ("s[%s] = r[0x%02x];", reg_name[3], OP_VAL_8(3)&0xf);
                }
#endif
        }
        break;
    case 2:
        if( com.i_direct )
        {
            printf( "s[%s] = 0x%02x", ifo_reg[9], OP_VAL_16(0) );
        }
        else
        {
            printf( "s[%s] = r[0x%02x]", ifo_reg[9], OP_VAL_8(1)&0x0f );
        }

        printf( "s[%s] = (s[%s]&0x7FFF)|0x%02x", 
                        ifo_reg[10], ifo_reg[10], OP_VAL_16(1)&0x8000);
        break;
    case 3:
        if( com.i_direct )
        {
            printf( "r[0x%02x] = 0x%02x", OP_VAL_8(3)&0x0f, OP_VAL_16(0) );
        }
        else
        {
            printf ("r[r[0x%02x]] = r[0x%02x]",
                                    OP_VAL_8(3)&0x0f, OP_VAL_8(1)&0x0f);
        }
        break;
    case 4:
        //actually only bits 00011100 00011100 are set
        if( com.i_direct )
        {
            printf ("s[%s] = 0x%02x", ifo_reg[11], OP_VAL_16(1));
        }
        else
        {
            printf ("s[%s] = r[0x%02x]", ifo_reg[11], OP_VAL_8(3)&0x0f );
        }
        break;
    case 6:
        //actually,
        //s[%s]=(r[%s]&0x3FF) | (0x%02x << 0xA);
        //but it is way too ugly
        if( com.i_direct )
        {
            printf( "s[%s] = 0x%02x", ifo_reg[8], OP_VAL_8(2)>>2 );
        }
        else
        {
            printf( "s[%s] = r[0x%02x]", ifo_reg[8], OP_VAL_8(3)&0x0f );
        }
        break;
    default:
        printf ("unknown");
    }
}

static void IfoSet( ifo_command_t com )
{
    IfoRegister( OP_VAL_16(0), 0 );
    printf( " %s ", IfoMath( com.i_cmd ) );
    IfoRegister( OP_VAL_16(1), com.i_direct );
Stéphane Borel's avatar