]> git.sesse.net Git - vlc/blob - plugins/dvd/dvd_ifo.c
-Some functions to prepare ifo commands utilisation to move in the
[vlc] / plugins / dvd / dvd_ifo.c
1 /*****************************************************************************
2  * dvd_ifo.c: Functions for ifo parsing
3  *****************************************************************************
4  * Copyright (C) 1999-2001 VideoLAN
5  * $Id: dvd_ifo.c,v 1.4 2001/02/09 03:51:42 stef Exp $
6  *
7  * Author: Stéphane Borel <stef@via.ecp.fr>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #include <stdio.h>
28 #include <unistd.h>
29 #include <string.h>
30 #include <fcntl.h>
31
32 #include "common.h"
33
34 #include "intf_msg.h"
35 #include "dvd_ifo.h"
36 #include "input_dvd.h"
37
38 void CommandRead( ifo_command_t );
39
40 /*
41  * IFO Management.
42  */
43
44 /*****************************************************************************
45  * IfoFindVMG : When reading directly on a device, finds the offset to the
46  * beginning of video_ts.ifo.
47  *****************************************************************************/
48 static int IfoFindVMG( ifo_t* p_ifo )
49 {
50     char    psz_ifo_start[12] = "DVDVIDEO-VMG";
51     char    psz_test[12];
52
53     read( p_ifo->i_fd, psz_test, 12 );
54
55     while( strncmp( psz_test, psz_ifo_start, 12 ) != 0 )
56     {
57         /* The start of ifo file is on a sector boundary */
58         p_ifo->i_pos = lseek( p_ifo->i_fd,
59                               p_ifo->i_pos + DVD_LB_SIZE,
60                               SEEK_SET );
61         read( p_ifo->i_fd, psz_test, 12 );
62     }
63     p_ifo->i_off = p_ifo->i_pos;
64
65 //fprintf( stderr, "VMG Off : %lld\n", (long long)(p_ifo->i_off) );
66
67     return 0;
68 }
69
70 /*****************************************************************************
71  * IfoFindVTS : beginning of vts_*.ifo.
72  *****************************************************************************/
73 static int IfoFindVTS( ifo_t* p_ifo )
74 {
75     char    psz_ifo_start[12] = "DVDVIDEO-VTS";
76     char    psz_test[12];
77
78     read( p_ifo->i_fd, psz_test, 12 );
79
80     while( strncmp( psz_test, psz_ifo_start, 12 ) != 0 )
81     {
82         /* The start of ifo file is on a sector boundary */
83         p_ifo->i_pos = lseek( p_ifo->i_fd,
84                               p_ifo->i_pos + DVD_LB_SIZE,
85                               SEEK_SET );
86         read( p_ifo->i_fd, psz_test, 12 );
87     }
88     p_ifo->i_off = p_ifo->i_pos;
89
90 //fprintf( stderr, "VTS Off : %lld\n", (long long)(p_ifo->i_off) );
91
92     return 0;
93 }
94
95 /*****************************************************************************
96  * IfoInit : Creates an ifo structure and prepares for parsing directly
97  * on DVD device.
98  *****************************************************************************/
99 ifo_t IfoInit( int i_fd )
100 {
101     ifo_t       ifo;
102     
103     /* If we are here the dvd device has already been opened */
104     ifo.i_fd = i_fd;
105     /* No data at the beginning of the disk
106      * 512000 bytes is just another value :) */
107     ifo.i_pos = lseek( ifo.i_fd, 250 *DVD_LB_SIZE, SEEK_SET );
108     /* FIXME : use udf filesystem to find the beginning of the file */
109     IfoFindVMG( &ifo );
110     
111     return ifo;
112 }
113
114 /*****************************************************************************
115  * IfoEnd : Frees all the memory allocated to ifo structures
116  *****************************************************************************/
117 void IfoEnd( ifo_t* p_ifo )
118 {
119     int     i,j;
120
121     /* Free structures from video title sets */
122     for( j=0 ; j<p_ifo->vmg.mat.i_tts_nb ; j++ )
123     {
124         free( p_ifo->p_vts[j].vobu_admap.pi_vobu_ssector );
125         free( p_ifo->p_vts[j].c_adt.p_cell_inf );
126         free( p_ifo->p_vts[j].m_vobu_admap.pi_vobu_ssector );
127         free( p_ifo->p_vts[j].m_c_adt.p_cell_inf );
128         for( i=0 ; i<p_ifo->p_vts[j].tmap_ti.i_nb ; i++ )
129         {
130             free( p_ifo->p_vts[j].tmap_ti.p_tmap[i].pi_sector );
131         }
132         free( p_ifo->p_vts[j].tmap_ti.pi_sbyte );
133         free( p_ifo->p_vts[j].tmap_ti.p_tmap );
134         free( p_ifo->p_vts[j].pgci_ti.p_srp );
135         for( i=0 ; i<p_ifo->p_vts[j].pgci_ut.i_lu_nb ; i++ )
136         {
137             free( p_ifo->p_vts[j].pgci_ut.p_pgci_inf[i].p_srp );
138         }
139         free( p_ifo->p_vts[j].pgci_ut.p_pgci_inf );
140         free( p_ifo->p_vts[j].pgci_ut.p_lu );
141     }
142
143     free( p_ifo->p_vts );
144
145     /* Free structures from video manager */
146     free( p_ifo->vmg.vobu_admap.pi_vobu_ssector );
147     free( p_ifo->vmg.c_adt.p_cell_inf );
148     for( i=0 ; i<p_ifo->vmg.pgci_ut.i_lu_nb ; i++ )
149     {
150         free( p_ifo->vmg.pgci_ut.p_pgci_inf[i].p_srp );
151     }
152     free( p_ifo->vmg.pgci_ut.p_pgci_inf );
153     free( p_ifo->vmg.pgci_ut.p_lu );
154     for( i=1 ; i<=8 ; i++ )
155     {
156         free( p_ifo->vmg.ptl_mait.p_ptl_mask->ppi_ptl_mask[i] );
157     }
158     free( p_ifo->vmg.ptl_mait.p_ptl_desc );
159     free( p_ifo->vmg.ptl_mait.p_ptl_mask );
160     free( p_ifo->vmg.vts_atrt.pi_vts_atrt_sbyte );
161     free( p_ifo->vmg.vts_atrt.p_vts_atrt );
162     free( p_ifo->vmg.pgc.p_cell_pos_inf );
163     free( p_ifo->vmg.pgc.p_cell_play_inf );
164     free( p_ifo->vmg.pgc.prg_map.pi_entry_cell );
165     free( p_ifo->vmg.pgc.com_tab.p_cell_com );
166     free( p_ifo->vmg.pgc.com_tab.p_post_com );
167     free( p_ifo->vmg.pgc.com_tab.p_pre_com );
168
169     return;
170 }
171
172 /*
173  * Macros to process ifo files
174  */
175  
176 #define GET( p_field , i_len )                                              \
177     {                                                                       \
178         read( p_ifo->i_fd , (p_field) , (i_len) );                          \
179 /*fprintf(stderr, "Pos : %lld Val : %llx\n",                                  \
180                                 (long long)(p_ifo->i_pos - i_start),        \
181                                 (long long)*(p_field) );    */                \
182         p_ifo->i_pos += i_len;                                              \
183     }
184
185 #define GETC( p_field )                                                     \
186     {                                                                       \
187         read( p_ifo->i_fd , (p_field) , 1 );                                \
188 /*fprintf(stderr, "Pos : %lld Value : %d\n",                                  \
189                                 (long long)(p_ifo->i_pos - i_start),        \
190                                           *(p_field) );*/                     \
191         p_ifo->i_pos += 1;                                                  \
192     }
193
194 #define GETS( p_field )                                                     \
195     {                                                                       \
196         read( p_ifo->i_fd , (p_field) , 2 );                                \
197         *(p_field) = ntohs( *(p_field) );                                   \
198 /*fprintf(stderr, "Pos : %lld Value : %d\n",                                  \
199                                 (long long)(p_ifo->i_pos - i_start),        \
200                                           *(p_field) );*/                     \
201         p_ifo->i_pos += 2;                                                  \
202     }
203
204 #define GETL( p_field )                                                     \
205     {                                                                       \
206         read( p_ifo->i_fd , (p_field) , 4 );                                \
207         *(p_field) = ntohl( *(p_field) );                                   \
208 /*fprintf(stderr, "Pos : %lld Value : %d\n",                                  \
209                                 (long long)(p_ifo->i_pos - i_start),        \
210                                           *(p_field) );*/                     \
211         p_ifo->i_pos += 4;                                                  \
212     }
213
214 #define GETLL( p_field )                                                    \
215     {                                                                       \
216         read( p_ifo->i_fd , (p_field) , 8 );                                \
217         *(p_field) = ntoh64( *(p_field) );                                  \
218 /*fprintf(stderr, "Pos : %lld Value : %lld\n",                                \
219                                 (long long)(p_ifo->i_pos - i_start),        \
220                                             *(p_field) );*/                   \
221         p_ifo->i_pos += 8;                                                  \
222     }
223
224 #define FLUSH( i_len )                                                      \
225     {                                                                       \
226 /*fprintf(stderr, "Pos : %lld\n", (long long)(p_ifo->i_pos - i_start));*/       \
227         p_ifo->i_pos = lseek( p_ifo->i_fd ,                               \
228                               p_ifo->i_pos + (i_len), SEEK_SET );           \
229     }
230
231 /*
232  * Function common to Video Manager and Video Title set Processing
233  */
234
235 /*****************************************************************************
236  * ReadPGC : Fills the Program Chain structure.
237  *****************************************************************************/
238 #define GETCOMMAND( p_com )                                                 \
239     {                                                                       \
240         read( p_ifo->i_fd , (p_com) , 8 );                                  \
241 /*fprintf(stderr, "Pos : %lld Type : %d direct : %d cmd : %d dircmp : %d cmp : %d subcmd : %d v0 : %d v2 : %d v4 : %d\n",                                  \
242                                 (long long)(p_ifo->i_pos - i_start),        \
243                                 (int)((p_com)->i_type),                     \
244                                 (int)((p_com)->i_direct),                   \
245                                 (int)((p_com)->i_cmd),                      \
246                                 (int)((p_com)->i_dir_cmp),                  \
247                                 (int)((p_com)->i_cmp),                      \
248                                 (int)((p_com)->i_sub_cmd),                  \
249                                 (int)((p_com)->data.pi_16[0]),              \
250                                 (int)((p_com)->data.pi_16[1]),              \
251                                 (int)((p_com)->data.pi_16[2]));*/             \
252 /*        CommandRead( *(p_com) );*/                                            \
253         p_ifo->i_pos += 8;                                                  \
254     }
255
256 static pgc_t ReadPGC( ifo_t* p_ifo )
257 {
258     pgc_t   pgc;
259     int     i;
260     off_t   i_start = p_ifo->i_pos;
261
262 //fprintf( stderr, "PGC\n" );
263
264     FLUSH(2);
265     GETC( &pgc.i_prg_nb );
266     GETC( &pgc.i_cell_nb );
267     GETL( &pgc.i_play_time );
268     GETL( &pgc.i_prohibited_user_op );
269     for( i=0 ; i<8 ; i++ )
270     {
271         GETS( &pgc.pi_audio_status[i] );
272     }
273     for( i=0 ; i<32 ; i++ )
274     {
275         GETL( &pgc.pi_subpic_status[i] );
276     }
277     GETS( &pgc.i_next_pgc_nb );
278     GETS( &pgc.i_prev_pgc_nb );
279     GETS( &pgc.i_goup_pgc_nb );
280     GETC( &pgc.i_still_time );
281     GETC( &pgc.i_play_mode );
282     for( i=0 ; i<16 ; i++ )
283     {
284         GETL( &pgc.pi_yuv_color[i] );
285         /* FIXME : We have to erase the extra bit */
286     }
287     GETS( &pgc.i_com_tab_sbyte );
288     GETS( &pgc.i_prg_map_sbyte );
289     GETS( &pgc.i_cell_play_inf_sbyte );
290     GETS( &pgc.i_cell_pos_inf_sbyte );
291
292     /* Parsing of pgc_com_tab_t */
293     if( pgc.i_com_tab_sbyte )
294     {
295         p_ifo->i_pos = lseek( p_ifo->i_fd, i_start
296                             + pgc.i_com_tab_sbyte, SEEK_SET );
297         GETS( &pgc.com_tab.i_pre_com_nb );
298         GETS( &pgc.com_tab.i_post_com_nb );
299         GETS( &pgc.com_tab.i_cell_com_nb );
300         FLUSH( 2 );
301         if( pgc.com_tab.i_pre_com_nb )
302         {
303             pgc.com_tab.p_pre_com =
304                       malloc(pgc.com_tab.i_pre_com_nb *sizeof(ifo_command_t));
305             if( pgc.com_tab.p_pre_com == NULL )
306             {
307                 intf_ErrMsg( "Out of memory" );
308                 p_ifo->b_error = 1;
309                 return pgc;
310             }
311             for( i=0 ; i<pgc.com_tab.i_pre_com_nb ; i++ )
312             {
313                 GETCOMMAND( &pgc.com_tab.p_pre_com[i] );
314             }
315         }
316         if( pgc.com_tab.i_post_com_nb )
317         {
318             pgc.com_tab.p_post_com =
319                       malloc(pgc.com_tab.i_post_com_nb *sizeof(ifo_command_t));
320             if( pgc.com_tab.p_post_com == NULL )
321             {
322                 intf_ErrMsg( "Out of memory" );
323                 p_ifo->b_error = 1;
324                 return pgc;
325             }
326             for( i=0 ; i<pgc.com_tab.i_post_com_nb ; i++ )
327             {
328                 GETCOMMAND( &pgc.com_tab.p_post_com[i] );
329             }
330         }
331         if( pgc.com_tab.i_cell_com_nb )
332         {
333             pgc.com_tab.p_cell_com =
334                       malloc(pgc.com_tab.i_cell_com_nb *sizeof(ifo_command_t));
335             if( pgc.com_tab.p_cell_com == NULL )
336             {
337                 intf_ErrMsg( "Out of memory" );
338                 p_ifo->b_error = 1;
339                 return pgc;
340             }
341             for( i=0 ; i<pgc.com_tab.i_cell_com_nb ; i++ )
342             {
343                 GETCOMMAND( &pgc.com_tab.p_cell_com[i] );
344             }
345         }
346     }
347     /* Parsing of pgc_prg_map_t */
348     if( pgc.i_prg_map_sbyte )
349     {
350         p_ifo->i_pos = lseek( p_ifo->i_fd, i_start
351                             + pgc.i_prg_map_sbyte, SEEK_SET );
352         pgc.prg_map.pi_entry_cell = malloc( pgc.i_prg_nb *sizeof(u8) );
353         if( pgc.prg_map.pi_entry_cell == NULL )
354         {
355             intf_ErrMsg( "Out of memory" );
356             p_ifo->b_error = 1;
357             return pgc;
358         }
359         GET( pgc.prg_map.pi_entry_cell, pgc.i_prg_nb );
360         /* FIXME : check endianness here */
361     }
362     /* Parsing of cell_play_inf_t */
363     if( pgc.i_cell_play_inf_sbyte )
364     {
365         p_ifo->i_pos = lseek( p_ifo->i_fd, i_start
366                             + pgc.i_cell_play_inf_sbyte, SEEK_SET );
367         pgc.p_cell_play_inf = malloc( pgc.i_cell_nb *sizeof(cell_play_inf_t) );
368         if( pgc.p_cell_play_inf == NULL )
369         {
370             intf_ErrMsg( "Out of memory" );
371             p_ifo->b_error = 1;
372             return pgc;
373         }
374         for( i=0 ; i<pgc.i_cell_nb ; i++ )
375         {
376             GETS( &pgc.p_cell_play_inf[i].i_cat );
377             GETC( &pgc.p_cell_play_inf[i].i_still_time );
378             GETC( &pgc.p_cell_play_inf[i].i_com_nb );
379             GETL( &pgc.p_cell_play_inf[i].i_play_time );
380             GETL( &pgc.p_cell_play_inf[i].i_entry_sector );
381             GETL( &pgc.p_cell_play_inf[i].i_first_ilvu_vobu_esector );
382             GETL( &pgc.p_cell_play_inf[i].i_lvobu_ssector );
383             GETL( &pgc.p_cell_play_inf[i].i_lsector );
384         }
385     }
386     /* Parsing of cell_pos_inf_map */
387     if( pgc.i_cell_pos_inf_sbyte )
388     {
389         p_ifo->i_pos = lseek( p_ifo->i_fd, i_start
390                             + pgc.i_cell_pos_inf_sbyte, SEEK_SET );
391         pgc.p_cell_pos_inf = malloc( pgc.i_cell_nb *sizeof(cell_pos_inf_t) );
392         if( pgc.p_cell_play_inf == NULL )
393         {
394             intf_ErrMsg( "Out of memory" );
395             p_ifo->b_error = 1;
396             return pgc;
397         }
398         for( i=0 ; i<pgc.i_cell_nb ; i++ )
399         {
400             GETS( &pgc.p_cell_pos_inf[i].i_vob_id );
401             FLUSH( 1 );
402             GETC( &pgc.p_cell_pos_inf[i].i_cell_id );
403         }
404     } 
405
406     return pgc;
407 }
408
409 /*****************************************************************************
410  * ReadUnit : Fills Menu Language Unit Table/ PGC Info Table
411  *****************************************************************************/
412 static pgci_inf_t ReadUnit( ifo_t* p_ifo )
413 {
414     pgci_inf_t      inf;
415     int             i;
416     off_t           i_start = p_ifo->i_pos;
417
418 //fprintf( stderr, "Unit\n" );
419
420     GETS( &inf.i_srp_nb );
421     FLUSH( 2 );
422     GETL( &inf.i_lu_ebyte );
423     inf.p_srp = malloc( inf.i_srp_nb *sizeof(pgci_srp_t) );
424     if( inf.p_srp == NULL )
425     {
426         intf_ErrMsg( "Out of memory" );
427         p_ifo->b_error = 1;
428         return inf;
429     }
430     for( i=0 ; i<inf.i_srp_nb ; i++ )
431     {
432         GETC( &inf.p_srp[i].i_pgc_cat_mask );
433         GETC( &inf.p_srp[i].i_pgc_cat );
434         GETS( &inf.p_srp[i].i_par_mask );
435         GETL( &inf.p_srp[i].i_pgci_sbyte );
436     }
437     for( i=0 ; i<inf.i_srp_nb ; i++ )
438     {
439         p_ifo->i_pos = lseek( p_ifo->i_fd,
440                          i_start + inf.p_srp[i].i_pgci_sbyte,
441                          SEEK_SET );
442         inf.p_srp[i].pgc = ReadPGC( p_ifo );
443     }
444
445     return inf;
446 }
447
448 /*****************************************************************************
449  * ReadUnitTable : Fills the PGCI Unit structure.
450  *****************************************************************************/
451 static pgci_ut_t ReadUnitTable( ifo_t* p_ifo )
452 {
453     pgci_ut_t       pgci;
454     int             i;
455     off_t           i_start = p_ifo->i_pos;
456
457 //fprintf( stderr, "Unit Table\n" );
458
459     GETS( &pgci.i_lu_nb );
460     FLUSH( 2 );
461     GETL( &pgci.i_ebyte );
462     pgci.p_lu = malloc( pgci.i_lu_nb *sizeof(pgci_lu_t) );
463     if( pgci.p_lu == NULL )
464     {
465         intf_ErrMsg( "Out of memory" );
466         p_ifo->b_error = 1;
467         return pgci;
468     }
469     for( i=0 ; i<pgci.i_lu_nb ; i++ )
470     {
471         GET( pgci.p_lu[i].ps_lang_code, 2 );
472         FLUSH( 1 );
473         GETC( &pgci.p_lu[i].i_existence_mask );
474         GETL( &pgci.p_lu[i].i_lu_sbyte );
475     }
476     pgci.p_pgci_inf = malloc( pgci.i_lu_nb *sizeof(pgci_inf_t) );
477     if( pgci.p_pgci_inf == NULL )
478     {
479         intf_ErrMsg( "Out of memory" );
480         p_ifo->b_error = 1;
481         return pgci;
482     }
483     for( i=0 ; i<pgci.i_lu_nb ; i++ )
484     {
485         p_ifo->i_pos = lseek( p_ifo->i_fd, i_start +
486                                 pgci.p_lu[i].i_lu_sbyte,
487                                 SEEK_SET );
488         pgci.p_pgci_inf[i] = ReadUnit( p_ifo );
489     }
490
491     return pgci;
492 }
493
494 /*****************************************************************************
495  * ReadCellInf : Fills the Cell Information structure.
496  *****************************************************************************/
497 static c_adt_t ReadCellInf( ifo_t* p_ifo )
498 {
499     c_adt_t         c_adt;
500     int             i, i_max;
501     off_t           i_start = p_ifo->i_pos;
502
503 //fprintf( stderr, "CELL ADD\n" );
504
505     GETS( &c_adt.i_vob_nb );
506     FLUSH( 2 );
507     GETL( &c_adt.i_ebyte );
508     i_max = ( i_start + c_adt.i_ebyte + 1 - p_ifo->i_pos ) / sizeof(cell_inf_t);
509     c_adt.p_cell_inf = malloc( i_max *sizeof(cell_inf_t) );
510     if( c_adt.p_cell_inf == NULL )
511     {
512         intf_ErrMsg( "Out of memory" );
513         p_ifo->b_error = 1;
514         return c_adt;
515     }
516     for( i=0 ; i<i_max ; i++ )
517     {
518         GETS( &c_adt.p_cell_inf[i].i_vob_id );
519         GETC( &c_adt.p_cell_inf[i].i_cell_id );
520         FLUSH( 1 );
521         GETL( &c_adt.p_cell_inf[i].i_ssector );
522         GETL( &c_adt.p_cell_inf[i].i_esector );
523     }
524     
525     return c_adt;
526 }
527
528 /*****************************************************************************
529  * ReadMap : Fills the VOBU Map structure.
530  *****************************************************************************/
531 static vobu_admap_t ReadMap( ifo_t* p_ifo )
532 {
533     vobu_admap_t        map;
534     int                 i, i_max;
535     off_t               i_start = p_ifo->i_pos;
536     
537 //fprintf( stderr, "VOBU ADMAP\n" );
538
539     GETL( &map.i_ebyte );
540     i_max = ( i_start + map.i_ebyte + 1 - p_ifo->i_pos ) / sizeof(u32);
541     map.pi_vobu_ssector = malloc( i_max *sizeof(u32) );
542     for( i=0 ; i<i_max ; i++ )
543     {
544         GETL( &map.pi_vobu_ssector[i] );
545     }
546
547     return map;
548 }
549  
550 /*
551  * Video Manager Information Processing.
552  * This is what is contained in video_ts.ifo.
553  */
554
555 /*****************************************************************************
556  * ReadVMGInfMat : Fills the Management Information structure.
557  *****************************************************************************/
558 static vmgi_mat_t ReadVMGInfMat( ifo_t* p_ifo )
559 {
560     vmgi_mat_t  mat;
561     int         i;
562 //    off_t     i_start = p_ifo->i_pos;
563
564 //fprintf( stderr, "VMGI\n" );
565
566     GET( mat.psz_id , 12 );
567     mat.psz_id[12] = '\0';
568     GETL( &mat.i_lsector );
569     FLUSH( 12 );
570     GETL( &mat.i_i_lsector );
571     FLUSH( 1 );
572     GETC( &mat.i_spec_ver );
573     GETL( &mat.i_cat );
574     GETS( &mat.i_vol_nb );
575     GETS( &mat.i_vol );
576     GETC( &mat.i_disc_side );
577     FLUSH( 19 );
578     GETS( &mat.i_tts_nb );
579     GET( mat.ps_provider_id, 32 );
580     GETLL( &mat.i_pos_code );
581     FLUSH( 24 );
582     GETL( &mat.i_i_mat_ebyte );
583     GETL( &mat.i_fp_pgc_sbyte );
584     FLUSH( 56 );
585     GETL( &mat.i_vobs_ssector );
586     GETL( &mat.i_ptt_srpt_ssector );
587     GETL( &mat.i_pgci_ut_ssector );
588     GETL( &mat.i_ptl_mait_ssector );
589     GETL( &mat.i_vts_atrt_ssector );
590     GETL( &mat.i_txtdt_mg_ssector );
591     GETL( &mat.i_c_adt_ssector );
592     GETL( &mat.i_vobu_admap_ssector );
593     FLUSH( 32 );
594     GETS( &mat.i_video_atrt );
595     FLUSH( 1 );
596     GETC( &mat.i_audio_nb );
597 //fprintf( stderr, "vmgi audio nb : %d\n", mat.i_audio_nb );
598     for( i=0 ; i < 8 ; i++ )
599     {
600         GETLL( &mat.pi_audio_atrt[i] );
601     }
602     FLUSH( 17 );
603     GETC( &mat.i_subpic_nb );
604 //fprintf( stderr, "vmgi subpic nb : %d\n", mat.i_subpic_nb );
605     for( i=0 ; i < mat.i_subpic_nb ; i++ )
606     {
607         GET( &mat.pi_subpic_atrt[i], 6 );
608         /* FIXME : take care of endianness */
609     }
610
611     return mat;
612 }
613
614 /*****************************************************************************
615  * ReadVMGTitlePointer : Fills the Part Of Title Search Pointer structure.
616  *****************************************************************************/
617 static vmg_ptt_srpt_t ReadVMGTitlePointer( ifo_t* p_ifo )
618 {
619     vmg_ptt_srpt_t  ptr;
620     int             i;
621 //    off_t           i_start = p_ifo->i_pos;
622
623 //fprintf( stderr, "PTR\n" );
624
625     GETS( &ptr.i_ttu_nb );
626     FLUSH( 2 );
627     GETL( &ptr.i_ebyte );
628     /* Parsing of tts */
629     ptr.p_tts = malloc( ptr.i_ttu_nb *sizeof(tts_t) );
630     if( ptr.p_tts == NULL )
631     {
632         intf_ErrMsg( "Out of memory" );
633         p_ifo->b_error = 1;
634         return ptr;
635     }
636     for( i=0 ; i<ptr.i_ttu_nb ; i++ )
637     {
638         GETC( &ptr.p_tts[i].i_play_type );
639         GETC( &ptr.p_tts[i].i_angle_nb );
640         GETS( &ptr.p_tts[i].i_ptt_nb );
641         GETS( &ptr.p_tts[i].i_parental_id );
642         GETC( &ptr.p_tts[i].i_tts_nb );
643         GETC( &ptr.p_tts[i].i_vts_ttn );
644         GETL( &ptr.p_tts[i].i_ssector );
645     }
646
647     return ptr;
648 }
649
650 /*****************************************************************************
651  * ReadParentalInf : Fills the Parental Management structure.
652  *****************************************************************************/
653 static vmg_ptl_mait_t ReadParentalInf( ifo_t* p_ifo )
654 {
655     vmg_ptl_mait_t  par;
656     int             i, j, k;
657     off_t           i_start = p_ifo->i_pos;
658
659 //fprintf( stderr, "PTL\n" );
660
661     GETS( &par.i_country_nb );
662     GETS( &par.i_vts_nb );
663     GETL( &par.i_ebyte );
664     par.p_ptl_desc = malloc( par.i_country_nb *sizeof(vmg_ptl_mai_desc_t) );
665     if( par.p_ptl_desc == NULL )
666     {
667         intf_ErrMsg( "Out of memory" );
668         p_ifo->b_error = 1;
669         return par;
670     }
671     for( i=0 ; i<par.i_country_nb ; i++ )
672     {
673         GET( par.p_ptl_desc[i].ps_country_code, 2 );
674         FLUSH( 2 );
675         GETS( &par.p_ptl_desc[i].i_ptl_mai_sbyte );
676         FLUSH( 2 );
677     }
678     par.p_ptl_mask = malloc( par.i_country_nb *sizeof(vmg_ptl_mask_t) );
679     if( par.p_ptl_mask == NULL )
680     {
681         intf_ErrMsg( "Out of memory" );
682         p_ifo->b_error = 1;
683         return par;
684     }
685     for( i=0 ; i<par.i_country_nb ; i++ )
686     {
687         p_ifo->i_pos = lseek( p_ifo->i_fd, i_start +
688                          par.p_ptl_desc[i].i_ptl_mai_sbyte, SEEK_SET );
689         for( j=1 ; j<=8 ; j++ )
690         {
691             par.p_ptl_mask[i].ppi_ptl_mask[j] =
692                                     malloc( par.i_vts_nb *sizeof(u16) );
693             if( par.p_ptl_mask[i].ppi_ptl_mask[j] == NULL )
694             {
695                 intf_ErrMsg( "Out of memory" );
696                 p_ifo->b_error = 1;
697                 return par;
698             }        
699             for( k=0 ; k<par.i_vts_nb ; k++ )
700             {
701                 GETS( &par.p_ptl_mask[i].ppi_ptl_mask[j][k] );
702             }
703         }
704     }
705
706     return par;
707 }
708
709 /*****************************************************************************
710  * ReadVTSAttr : Fills the structure about VTS attributes.
711  *****************************************************************************/
712 static vmg_vts_atrt_t ReadVTSAttr( ifo_t* p_ifo )
713 {
714     vmg_vts_atrt_t  atrt;
715     int             i, j;
716     off_t           i_start = p_ifo->i_pos;
717
718 //fprintf( stderr, "VTS ATTR\n" );
719
720     GETS( &atrt.i_vts_nb );
721     FLUSH( 2 );
722     GETL( &atrt.i_ebyte );
723     atrt.pi_vts_atrt_sbyte = malloc( atrt.i_vts_nb *sizeof(u32) );
724     if( atrt.pi_vts_atrt_sbyte == NULL )
725     {
726         intf_ErrMsg( "Out of memory" );
727         p_ifo->b_error = 1;
728         return atrt;
729     }
730     for( i=0 ; i<atrt.i_vts_nb ; i++ )
731     {
732         GETL( &atrt.pi_vts_atrt_sbyte[i] );
733     }
734     atrt.p_vts_atrt = malloc( atrt.i_vts_nb *sizeof(vts_atrt_t) );
735     if( atrt.p_vts_atrt == NULL )
736     {
737         intf_ErrMsg( "Out of memory" );
738         p_ifo->b_error = 1;
739         return atrt;
740     }
741     for( i=0 ; i<atrt.i_vts_nb ; i++ )
742     {
743         p_ifo->i_pos = lseek( p_ifo->i_fd, i_start +
744                                 atrt.pi_vts_atrt_sbyte[i],
745                                 SEEK_SET );
746         GETL( &atrt.p_vts_atrt[i].i_ebyte );
747         GETL( &atrt.p_vts_atrt[i].i_cat_app_type );
748         GETS( &atrt.p_vts_atrt[i].i_vtsm_video_atrt );
749         FLUSH( 1 );
750         GETC( &atrt.p_vts_atrt[i].i_vtsm_audio_nb );
751 //fprintf( stderr, "m audio nb : %d\n", atrt.p_vts_atrt[i].i_vtsm_audio_nb );
752         for( j=0 ; j<8 ; j++ )
753         {
754             GETLL( &atrt.p_vts_atrt[i].pi_vtsm_audio_atrt[j] );
755         }
756         FLUSH( 17 );
757         GETC( &atrt.p_vts_atrt[i].i_vtsm_subpic_nb );
758 //fprintf( stderr, "m subp nb : %d\n", atrt.p_vts_atrt[i].i_vtsm_subpic_nb );
759         for( j=0 ; j<28 ; j++ )
760         {
761             GET( &atrt.p_vts_atrt[i].pi_vtsm_subpic_atrt[j], 6 );
762             /* FIXME : Fix endianness issue here */
763         }
764         FLUSH( 2 );
765         GETS( &atrt.p_vts_atrt[i].i_vtstt_video_atrt );
766         FLUSH( 1 );
767         GETL( &atrt.p_vts_atrt[i].i_vtstt_audio_nb );
768 //fprintf( stderr, "tt audio nb : %d\n", atrt.p_vts_atrt[i].i_vtstt_audio_nb );
769         for( j=0 ; j<8 ; j++ )
770         {
771             GETLL( &atrt.p_vts_atrt[i].pi_vtstt_audio_atrt[j] );
772         }
773         FLUSH( 17 );
774         GETC( &atrt.p_vts_atrt[i].i_vtstt_subpic_nb );
775 //fprintf( stderr, "tt subp nb : %d\n", atrt.p_vts_atrt[i].i_vtstt_subpic_nb );
776         for( j=0 ; j<28/*atrt.p_vts_atrt[i].i_vtstt_subpic_nb*/ ; j++ )
777         {
778             GET( &atrt.p_vts_atrt[i].pi_vtstt_subpic_atrt[j], 6 );
779             /* FIXME : Fix endianness issue here */
780         }
781     }
782
783     return atrt;
784 }
785                            
786 /*****************************************************************************
787  * ReadVMG : Parse video_ts.ifo file to fill the Video Manager structure.
788  *****************************************************************************/
789 static vmg_t ReadVMG( ifo_t* p_ifo )
790 {
791     vmg_t       vmg;
792
793     p_ifo->i_pos = lseek( p_ifo->i_fd, p_ifo->i_off, SEEK_SET);
794     vmg.mat = ReadVMGInfMat( p_ifo );
795     p_ifo->i_pos = lseek( p_ifo->i_fd, p_ifo->i_off + 
796                               vmg.mat.i_fp_pgc_sbyte, SEEK_SET );
797     vmg.pgc = ReadPGC( p_ifo );
798     if( vmg.mat.i_ptt_srpt_ssector )
799     {
800         p_ifo->i_pos = lseek( p_ifo->i_fd, p_ifo->i_off +
801                         vmg.mat.i_ptt_srpt_ssector *DVD_LB_SIZE,
802                         SEEK_SET );
803         vmg.ptt_srpt = ReadVMGTitlePointer( p_ifo );
804     }
805     if( vmg.mat.i_pgci_ut_ssector )
806     {
807         p_ifo->i_pos = lseek( p_ifo->i_fd, p_ifo->i_off +
808                         vmg.mat.i_pgci_ut_ssector *DVD_LB_SIZE,
809                         SEEK_SET );
810         vmg.pgci_ut = ReadUnitTable( p_ifo );
811     }
812     if( vmg.mat.i_ptl_mait_ssector )
813     {
814         p_ifo->i_pos = lseek( p_ifo->i_fd, p_ifo->i_off +
815                         vmg.mat.i_ptl_mait_ssector *DVD_LB_SIZE,
816                         SEEK_SET );
817         vmg.ptl_mait = ReadParentalInf( p_ifo );
818     }
819     if( vmg.mat.i_vts_atrt_ssector )
820     {
821         p_ifo->i_pos = lseek( p_ifo->i_fd, p_ifo->i_off +
822                         vmg.mat.i_vts_atrt_ssector *DVD_LB_SIZE,
823                         SEEK_SET );
824         vmg.vts_atrt = ReadVTSAttr( p_ifo );
825     }
826     if( vmg.mat.i_c_adt_ssector )
827     {
828         p_ifo->i_pos = lseek( p_ifo->i_fd, p_ifo->i_off +
829                         vmg.mat.i_c_adt_ssector *DVD_LB_SIZE,
830                         SEEK_SET );
831         vmg.c_adt = ReadCellInf( p_ifo );
832     }
833     if( vmg.mat.i_vobu_admap_ssector )
834     {
835         p_ifo->i_pos = lseek( p_ifo->i_fd, p_ifo->i_off +
836                         vmg.mat.i_vobu_admap_ssector *DVD_LB_SIZE,
837                         SEEK_SET );
838         vmg.vobu_admap = ReadMap( p_ifo );
839     }
840     return vmg;
841 }
842
843 /*
844  * Video Title Set Information Processing.
845  * This is what is contained in vts_*.ifo.
846  */
847
848 /*****************************************************************************
849  * ReadVTSInfMat : Fills the Title Set Information structure.
850  *****************************************************************************/
851 static vtsi_mat_t ReadVTSInfMat( ifo_t* p_ifo )
852 {
853     vtsi_mat_t  mat;
854     int         i;
855 //    off_t       i_start = p_ifo->i_pos;
856
857 //fprintf( stderr, "VTSI\n" );
858
859     GET( mat.psz_id , 12 );
860     mat.psz_id[12] = '\0';
861     GETL( &mat.i_lsector );
862     FLUSH( 12 );
863     GETL( &mat.i_i_lsector );
864     FLUSH( 1 );
865     GETC( &mat.i_spec_ver );
866     GETL( &mat.i_cat );
867     FLUSH( 90 );
868     GETL( &mat.i_mat_ebyte );
869     FLUSH( 60 );
870     GETL( &mat.i_m_vobs_ssector );
871     GETL( &mat.i_tt_vobs_ssector );
872     GETL( &mat.i_ptt_srpt_ssector );
873     GETL( &mat.i_pgcit_ssector );
874     GETL( &mat.i_m_pgci_ut_ssector );
875     GETL( &mat.i_tmap_ti_ssector );
876     GETL( &mat.i_m_c_adt_ssector );
877     GETL( &mat.i_m_vobu_admap_ssector );
878     GETL( &mat.i_c_adt_ssector );
879     GETL( &mat.i_vobu_admap_ssector );
880     FLUSH( 24 );
881     GETS( &mat.i_m_video_atrt );
882     FLUSH( 1 );
883     GETC( &mat.i_m_audio_nb );
884     for( i=0 ; i<8 ; i++ )
885     {
886         GETLL( &mat.pi_m_audio_atrt[i] );
887     }
888     FLUSH( 17 );
889     GETC( &mat.i_m_subpic_nb );
890     for( i=0 ; i<28 ; i++ )
891     {
892         GET( &mat.pi_m_subpic_atrt[i], 6 );
893         /* FIXME : take care of endianness */
894     }
895     FLUSH( 2 );
896     GETS( &mat.i_video_atrt );
897     FLUSH( 1 );
898     GETC( &mat.i_audio_nb );
899 //fprintf( stderr, "vtsi audio nb : %d\n", mat.i_audio_nb );
900     for( i=0 ; i<8 ; i++ )
901     {
902         GETLL( &mat.pi_audio_atrt[i] );
903     }
904     FLUSH( 17 );
905     GETC( &mat.i_subpic_nb );
906 //fprintf( stderr, "vtsi subpic nb : %d\n", mat.i_subpic_nb );
907     for( i=0 ; i<mat.i_subpic_nb ; i++ )
908     {
909         GET( &mat.pi_subpic_atrt[i], 6 );
910         /* FIXME : take care of endianness */
911     }
912
913     return mat;
914 }
915
916 /*****************************************************************************
917  * ReadVTSTitlePointer : Fills the Part Of Title Search Pointer structure.
918  *****************************************************************************/
919 static vts_ptt_srpt_t ReadVTSTitlePointer( ifo_t* p_ifo )
920 {
921     vts_ptt_srpt_t  ptr;
922     int             i;
923     off_t           i_start = p_ifo->i_pos;
924
925 //fprintf( stderr, "PTR\n" );
926
927     GETS( &ptr.i_ttu_nb );
928     FLUSH( 2 );
929     GETL( &ptr.i_ebyte );
930     ptr.pi_ttu_sbyte = malloc( ptr.i_ttu_nb *sizeof(u32) );
931     if( ptr.pi_ttu_sbyte == NULL )
932     {
933         intf_ErrMsg( "Out of memory" );
934         p_ifo->b_error = 1;
935         return ptr;
936     }
937     for( i=0 ; i<ptr.i_ttu_nb ; i++ )
938     {
939         GETL( &ptr.pi_ttu_sbyte[i] );
940     }
941     /* Parsing of tts */
942     ptr.p_ttu = malloc( ptr.i_ttu_nb *sizeof(ttu_t) );
943     if( ptr.p_ttu == NULL )
944     {
945         intf_ErrMsg( "Out of memory" );
946         p_ifo->b_error = 1;
947         return ptr;
948     }
949     for( i=0 ; i<ptr.i_ttu_nb ; i++ )
950     {
951         p_ifo->i_pos = lseek( p_ifo->i_fd, i_start +
952                         ptr.pi_ttu_sbyte[i], SEEK_SET );
953         GETS( &ptr.p_ttu[i].i_pgc_nb );
954         GETS( &ptr.p_ttu[i].i_prg_nb );
955     }
956
957     return ptr;
958 }
959
960 /*****************************************************************************
961  * ReadVTSTimeMap : Fills the time map table
962  *****************************************************************************/
963 static vts_tmap_ti_t ReadVTSTimeMap( ifo_t* p_ifo )
964 {
965     vts_tmap_ti_t   tmap;
966     int             i,j;
967 //    off_t           i_start = p_ifo->i_pos;
968
969 //fprintf( stderr, "TMAP\n" );
970
971     GETS( &tmap.i_nb );
972     FLUSH( 2 );
973     GETL( &tmap.i_ebyte );
974     tmap.pi_sbyte = malloc( tmap.i_nb *sizeof(u32) );
975     if( tmap.pi_sbyte == NULL )
976     {
977         intf_ErrMsg( "Out of memory" );
978         p_ifo->b_error = 1;
979         return tmap;
980     }
981     for( i=0 ; i<tmap.i_nb ; i++ )
982     {    
983         GETL( &tmap.pi_sbyte[i] );
984     }
985     tmap.p_tmap = malloc( tmap.i_nb *sizeof(tmap_t) );
986     if( tmap.p_tmap == NULL )
987     {
988         intf_ErrMsg( "Out of memory" );
989         p_ifo->b_error = 1;
990         return tmap;
991     }
992     for( i=0 ; i<tmap.i_nb ; i++ )
993     {    
994         GETC( &tmap.p_tmap[i].i_time_unit );
995         FLUSH( 1 );
996         GETS( &tmap.p_tmap[i].i_entry_nb );
997         tmap.p_tmap[i].pi_sector =
998                     malloc( tmap.p_tmap[i].i_entry_nb *sizeof(u32) );
999         if( tmap.p_tmap[i].pi_sector == NULL )
1000         {
1001             intf_ErrMsg( "Out of memory" );
1002             p_ifo->b_error = 1;
1003             return tmap;
1004         }
1005         for( j=0 ; j<tmap.p_tmap[i].i_entry_nb ; j++ )
1006         {
1007             GETL( &tmap.p_tmap[i].pi_sector[j] );
1008         }
1009     }
1010
1011     return tmap;
1012 }
1013     
1014
1015 /*****************************************************************************
1016  * ReadVTS : Parse vts*.ifo files to fill the Video Title Set structure.
1017  *****************************************************************************/
1018 static vts_t ReadVTS( ifo_t* p_ifo )
1019 {
1020     vts_t       vts;
1021
1022     vts.i_pos = p_ifo->i_pos;
1023
1024     vts.mat = ReadVTSInfMat( p_ifo );
1025     if( vts.mat.i_ptt_srpt_ssector )
1026     {
1027         p_ifo->i_pos = lseek( p_ifo->i_fd, p_ifo->i_off +
1028                         vts.mat.i_ptt_srpt_ssector *DVD_LB_SIZE,
1029                         SEEK_SET );
1030         vts.ptt_srpt = ReadVTSTitlePointer( p_ifo );
1031     }
1032     if( vts.mat.i_m_pgci_ut_ssector )
1033     {
1034         p_ifo->i_pos = lseek( p_ifo->i_fd, p_ifo->i_off +
1035                         vts.mat.i_m_pgci_ut_ssector *DVD_LB_SIZE,
1036                         SEEK_SET );
1037         vts.pgci_ut = ReadUnitTable( p_ifo );
1038     }
1039     if( vts.mat.i_pgcit_ssector )
1040     {
1041         p_ifo->i_pos = lseek( p_ifo->i_fd, p_ifo->i_off +
1042                         vts.mat.i_pgcit_ssector *DVD_LB_SIZE,
1043                         SEEK_SET );
1044         vts.pgci_ti = ReadUnit( p_ifo );
1045     }
1046     if( vts.mat.i_tmap_ti_ssector )
1047     {
1048         p_ifo->i_pos = lseek( p_ifo->i_fd, p_ifo->i_off +
1049                         vts.mat.i_tmap_ti_ssector *DVD_LB_SIZE,
1050                         SEEK_SET );
1051         vts.tmap_ti = ReadVTSTimeMap( p_ifo );
1052     }
1053     if( vts.mat.i_m_c_adt_ssector )
1054     {
1055         p_ifo->i_pos = lseek( p_ifo->i_fd, p_ifo->i_off +
1056                         vts.mat.i_m_c_adt_ssector *DVD_LB_SIZE,
1057                         SEEK_SET );
1058         vts.m_c_adt = ReadCellInf( p_ifo );
1059     }
1060     if( vts.mat.i_m_vobu_admap_ssector )
1061     {
1062         p_ifo->i_pos = lseek( p_ifo->i_fd, p_ifo->i_off +
1063                         vts.mat.i_m_vobu_admap_ssector *DVD_LB_SIZE,
1064                         SEEK_SET );
1065         vts.m_vobu_admap = ReadMap( p_ifo );
1066     }
1067     if( vts.mat.i_c_adt_ssector )
1068     {
1069         p_ifo->i_pos = lseek( p_ifo->i_fd, p_ifo->i_off +
1070                         vts.mat.i_c_adt_ssector *DVD_LB_SIZE,
1071                         SEEK_SET );
1072         vts.c_adt = ReadCellInf( p_ifo );
1073     }
1074     if( vts.mat.i_vobu_admap_ssector )
1075     {
1076         p_ifo->i_pos = lseek( p_ifo->i_fd, p_ifo->i_off +
1077                         vts.mat.i_vobu_admap_ssector *DVD_LB_SIZE,
1078                         SEEK_SET );
1079         vts.vobu_admap = ReadMap( p_ifo );
1080     }
1081
1082     return vts;
1083 }
1084
1085 /*
1086  * DVD Information Management
1087  */
1088
1089 /*****************************************************************************
1090  * IfoRead : Function that fills structure and calls specified functions
1091  * to do it.
1092  *****************************************************************************/
1093 void IfoRead( ifo_t* p_ifo )
1094 {
1095     int     i;
1096     off_t   i_off;
1097
1098     p_ifo->vmg = ReadVMG( p_ifo );
1099     p_ifo->p_vts = malloc( p_ifo->vmg.mat.i_tts_nb *sizeof(vts_t) );
1100     if( p_ifo->p_vts == NULL )
1101     {
1102         intf_ErrMsg( "Out of memory" );
1103         p_ifo->b_error = 1;
1104         return;
1105     }
1106     for( i=0 ; i<1/*p_ifo->vmg.mat.i_tts_nb*/ ; i++ )
1107     {
1108
1109         intf_WarnMsg( 3, "######### VTS %d #############\n", i+1 );
1110
1111         i_off = p_ifo->vmg.ptt_srpt.p_tts[i].i_ssector *DVD_LB_SIZE;
1112         p_ifo->i_pos = lseek( p_ifo->i_fd, i_off, SEEK_SET );
1113         /* FIXME : use udf filesystem to avoid this */
1114         IfoFindVTS( p_ifo );
1115         p_ifo->p_vts[i] = ReadVTS( p_ifo );
1116     }
1117     return; 
1118 }
1119
1120 /*
1121  * IFO virtual machine : a set of commands that give the
1122  * interactive behaviour of the dvd
1123  */
1124 #if 1
1125
1126 #define OP_VAL_16(i) (ntoh16( com.data.pi_16[i]))
1127 #define OP_VAL_8(i) ((com.data.pi_8[i]))
1128
1129 static char ifo_reg[][80]=
1130 {
1131     "Menu_Language_Code",
1132     "Audio_Stream_#",
1133     "SubPicture_Stream_#",
1134     "Angle_#",
1135     "VTS_#",
1136     "VTS_Title_#",
1137     "PGC_#",
1138     "PTT_#",
1139     "Highlighted_Button_#",
1140     "Nav_Timer",
1141     "TimedPGC",
1142     "Karaoke_audio_mixing_mode",
1143     "Parental_mgmt_country_code",
1144     "Parental_Level",
1145     "Player_Video_Cfg",
1146     "Player_Audio_Cfg",
1147     "Audio_language_code_setting",
1148     "Audio_language_extension_code",
1149     "SPU_language_code_setting",
1150     "SPU_language_extension_code",
1151     "?Player_Regional_Code",
1152     "Reserved_21",
1153     "Reserved_22",
1154     "Reserved_23"
1155 };
1156
1157 static char * IfoMath( char val )
1158 {
1159     static char math_op[][10] =
1160     {
1161         "none",
1162         "=", 
1163         "<->",    // swap
1164         "+=",
1165         "-=",
1166         "*=",
1167         "/=",
1168         "%=",
1169         "rnd",    // rnd
1170         "&=",
1171         "|=",
1172         "^=",
1173         "??",    // invalid
1174         "??",    // invalid
1175         "??",    // invalid
1176         "??"    // invalid
1177     };
1178
1179     return (char *) math_op[val & 0x0f];
1180 }
1181
1182
1183 char ifo_cmp[][10] =
1184 {
1185     "none",
1186     "&&",
1187     "==",
1188     "!=",
1189     ">=",
1190     ">",
1191     "<",
1192     "<="
1193 };
1194
1195 char ifo_parental[][10] =
1196 {
1197     "0",
1198     "G",
1199     "2",
1200     "PG",
1201     "PG-13",
1202     "5",
1203     "R",
1204     "NC-17"
1205 };
1206
1207 char ifo_menu_id[][80] =
1208 {
1209     "-0-",
1210     "-1-",
1211     "Title (VTS menu)",
1212     "Root",
1213     "Sub-Picture",
1214     "Audio",
1215     "Angle",
1216     "Part of Title",
1217 };
1218
1219 char * IfoMenuName( char index )
1220 {
1221     return ifo_menu_id[index&0x07];
1222 }
1223
1224 static void IfoRegister( u16 i_data, u8 i_direct)
1225 {
1226     if( i_direct )
1227     {
1228         if( 0/*isalpha( i_data >> 8 & 0xff )*/ )
1229         {
1230             printf("'%c%c'", i_data>>8&0xff, i_data&0xff);
1231         }
1232         else
1233         {
1234             printf("0x%02x", i_data);
1235         }
1236     }
1237     else
1238     {
1239         if( i_data & 0x80 )
1240         {
1241             i_data &= 0x1f;
1242
1243             if( i_data > 0x17 )
1244             {
1245                 printf("s[ILL]");
1246             }
1247             else
1248             {
1249                 printf("s[%s]", ifo_reg[i_data]);
1250             }
1251         }
1252         else
1253         {
1254             i_data &= 0x1f;
1255
1256             if( i_data > 0xf )
1257             {
1258                 printf("r[ILL]");
1259             }
1260             else
1261             {
1262                 printf("r[0x%02x]", i_data);
1263             }
1264         }
1265     }
1266 }
1267
1268 static void IfoAdvanced( u8 *pi_code )
1269 {
1270     u8      i_cmd = pi_code[0];
1271
1272     printf(" { ");
1273
1274     if( pi_code[1]>>2 )
1275     {
1276         printf( " Highlight button %d; ", pi_code[1]>>2 );
1277     }
1278
1279     if( i_cmd == 0xff )
1280     {
1281         printf( " Illegal " );
1282     }
1283
1284     if( i_cmd == 0x00 )
1285     {
1286         printf( "ReSuME %d", pi_code[7] );
1287     }
1288     else if( ( i_cmd & 0x06) == 0x02 )
1289     {    // XX01Y
1290         printf ("Link to %s cell ", ( i_cmd & 0x01 ) ? "prev" : "next");
1291     }
1292     else
1293     {
1294         printf( "advanced (0x%02x) ", i_cmd );
1295     }
1296     printf(" } ");
1297 }
1298
1299 static void IfoJmp( ifo_command_t com )
1300 {
1301
1302     printf ("jmp ");
1303
1304     switch( com.i_sub_cmd )
1305     {
1306     case 0x01:
1307         printf( "Exit" );
1308         break;
1309     case 0x02:
1310         printf( "VTS 0x%02x", OP_VAL_8(3) );
1311         break;
1312     case 0x03:
1313         printf( "This VTS Title 0x%02x", OP_VAL_8(3) );
1314         break;
1315     case 0x05:
1316         printf( "This VTS Title 0x%02x Part 0x%04x",
1317                             OP_VAL_8(3),
1318                             OP_VAL_8(0)<<8|OP_VAL_8(1));
1319         break;
1320     case 0x06:
1321 #if 0
1322             printf ("in SystemSpace ");
1323             switch (OP_VAL_8(3)>>4) {
1324                 case 0x00:
1325                     printf ("to play first PGC");
1326                     break;
1327                 case 0x01: {
1328                     printf ("to menu \"%s\"", decode_menuname (OP_VAL_8(3)));
1329                 }
1330                     break;
1331                 case 0x02:
1332                     printf ("to VTS 0x%02x and TTN 0x%02x", OP_VAL_8(1), OP_VAL_8(2));
1333                     break;
1334                 case 0x03:
1335                     printf ("to VMGM PGC number 0x%02x", OP_VAL_8(0)<<8 | OP_VAL_8(1));
1336                     break;
1337                 case 0x08:
1338                     printf ("vts 0x%02x lu 0x%02x menu \"%s\"", OP_VAL_8(2), OP_VAL_8(1), decode_menuname (OP_VAL_8(3)));
1339                     break;
1340 #else
1341         switch( OP_VAL_8(3)>>6 )
1342         {
1343         case 0x00:
1344             printf( "to play first PGC" );
1345             break;                
1346         case 0x01:
1347             printf( "to VMG title menu (?)" );
1348             break;
1349         case 0x02:
1350             printf( "vts 0x%02x lu 0x%02x menu \"%s\"",
1351                             OP_VAL_8(2),
1352                             OP_VAL_8(1),
1353                             IfoMenuName( OP_VAL_8(3)&0xF ) );
1354             break;                
1355         case 0x03:
1356             printf( "vmg pgc 0x%04x (?)", ( OP_VAL_8(0)<<8) | OP_VAL_8(1) );
1357             break;
1358 #endif
1359         }
1360         break;
1361     case 0x08:
1362 #if 0
1363             switch(OP_VAL_8(3)>>4) {
1364                 case 0x00:
1365                     printf ("system first pgc");
1366                     break;
1367                 case 0x01:
1368                     printf ("system title menu");
1369                     break;
1370                 case 0x02:
1371                     printf ("system menu \"%s\"", decode_menuname (OP_VAL_8(3)));
1372                     break;
1373                 case 0x03:
1374                     printf ("system vmg pgc %02x ????", OP_VAL_8(0)<<8|OP_VAL_8(1));
1375                     break;
1376                 case 0x08:
1377                     printf ("system lu 0x%02x menu \"%s\"", OP_VAL_8(2), decode_menuname (OP_VAL_8(3)));
1378                     break;
1379                 case 0x0c:
1380                     printf ("system vmg pgc 0x%02x", OP_VAL_8(0)<<8|OP_VAL_8(1));
1381                     break;
1382             }
1383 #else
1384         // OP_VAL_8(2) is number of cell
1385         // it is processed BEFORE switch
1386         // under some conditions, it is ignored
1387         // I don't understand exactly what it means
1388         printf( " ( spec cell 0x%02X ) ", OP_VAL_8(2) ); 
1389
1390         switch( OP_VAL_8(3)>>6 )
1391         {
1392         case 0:
1393             printf( "to FP PGC" );
1394             break;
1395         case 1:
1396             printf( "to VMG root menu (?)" );
1397             break;
1398         case 2:
1399             printf( "to VTS menu \"%s\" (?)",
1400                     IfoMenuName(OP_VAL_8(3)&0xF) );
1401             break; 
1402         case 3:
1403             printf( "vmg pgc 0x%02x (?)", (OP_VAL_8(0)<<8)|OP_VAL_8(1) );
1404             break;
1405         }    
1406 #endif
1407         break;
1408     }
1409 }
1410
1411 static void IfoLnk( ifo_command_t com )
1412 {
1413     u16     i_button=OP_VAL_8(4)>>2;
1414
1415     printf ("lnk to ");
1416
1417     switch( com.i_sub_cmd )
1418     {
1419     case 0x01:
1420         IfoAdvanced( &OP_VAL_8(4) );
1421         break;
1422
1423     case 0x04:
1424         printf( "PGC 0x%02x", OP_VAL_16(2) );
1425         break; 
1426
1427     case 0x05:
1428         printf( "PTT 0x%02x", OP_VAL_16(2) );
1429         break; 
1430
1431     case 0x06:
1432         printf( "Program 0x%02x this PGC", OP_VAL_8(5) );
1433         break;
1434
1435     case 0x07:
1436         printf( "Cell 0x%02x this PGC", OP_VAL_8(5) );
1437         break;
1438     default:
1439         return;
1440     }
1441
1442     if( i_button )
1443     {
1444         printf( ", Highlight 0x%02x", OP_VAL_8(4)>>2 );
1445     }
1446             
1447 }
1448
1449 void IfoSetSystem( ifo_command_t com )
1450 {
1451     switch( com.i_cmd )
1452     {
1453     case 1: {
1454         int i;
1455
1456         for( i=1; i<=3; i++ )
1457         {
1458             if( OP_VAL_8(i)&0x80 )
1459             {
1460                 if( com.i_direct )
1461                 {
1462                     printf ("s[%s] = 0x%02x;", ifo_reg[i], OP_VAL_8(i)&0xf);
1463                 }
1464                 else
1465                 {
1466                     printf ("s[%s] = r[0x%02x];", ifo_reg[i], OP_VAL_8(i)&0xf);
1467                 }
1468             }
1469         }
1470 #if 0
1471                 if(op->direct) {
1472                         if(OP_VAL_8(1]&0x80)
1473                                 printf ("s[%s] = 0x%02x;", reg_name[1], OP_VAL_8(1]&0xf);
1474                         if(OP_VAL_8(2)&0x80)
1475 //DENT: lwhat about 0x7f here ???
1476                                 printf ("s[%s] = 0x%02x;", reg_name[2], OP_VAL_8(2)&0x7f);
1477                         if(OP_VAL_8(3)&0x80)
1478                                 printf ("s[%s] = 0x%02x;", reg_name[3], OP_VAL_8(3)&0xf);
1479                 } else {
1480                         if(OP_VAL_8(1)&0x80)
1481                                 printf ("s[%s] = r[0x%02x];", reg_name[1], OP_VAL_8(1)&0xf);
1482                         if(OP_VAL_8(2)&0x80)
1483                                 printf ("s[%s] = r[0x%02x];", reg_name[2], OP_VAL_8(2)&0xf);
1484                         if(OP_VAL_8(3)&0x80)
1485                                 printf ("s[%s] = r[0x%02x];", reg_name[3], OP_VAL_8(3)&0xf);
1486                 }
1487 #endif
1488         }
1489         break;
1490     case 2:
1491         if( com.i_direct )
1492         {
1493             printf( "s[%s] = 0x%02x", ifo_reg[9], OP_VAL_16(0) );
1494         }
1495         else
1496         {
1497             printf( "s[%s] = r[0x%02x]", ifo_reg[9], OP_VAL_8(1)&0x0f );
1498         }
1499
1500         printf( "s[%s] = (s[%s]&0x7FFF)|0x%02x", 
1501                         ifo_reg[10], ifo_reg[10], OP_VAL_16(1)&0x8000);
1502         break;
1503     case 3:
1504         if( com.i_direct )
1505         {
1506             printf( "r[0x%02x] = 0x%02x", OP_VAL_8(3)&0x0f, OP_VAL_16(0) );
1507         }
1508         else
1509         {
1510             printf ("r[r[0x%02x]] = r[0x%02x]",
1511                                     OP_VAL_8(3)&0x0f, OP_VAL_8(1)&0x0f);
1512         }
1513         break;
1514     case 4:
1515         //actually only bits 00011100 00011100 are set
1516         if( com.i_direct )
1517         {
1518             printf ("s[%s] = 0x%02x", ifo_reg[11], OP_VAL_16(1));
1519         }
1520         else
1521         {
1522             printf ("s[%s] = r[0x%02x]", ifo_reg[11], OP_VAL_8(3)&0x0f );
1523         }
1524         break;
1525     case 6:
1526         //actually,
1527         //s[%s]=(r[%s]&0x3FF) | (0x%02x << 0xA);
1528         //but it is way too ugly
1529         if( com.i_direct )
1530         {
1531             printf( "s[%s] = 0x%02x", ifo_reg[8], OP_VAL_8(2)>>2 );
1532         }
1533         else
1534         {
1535             printf( "s[%s] = r[0x%02x]", ifo_reg[8], OP_VAL_8(3)&0x0f );
1536         }
1537         break;
1538     default:
1539         printf ("unknown");
1540     }
1541 }
1542
1543 static void IfoSet( ifo_command_t com )
1544 {
1545     IfoRegister( OP_VAL_16(0), 0 );
1546     printf( " %s ", IfoMath( com.i_cmd ) );
1547     IfoRegister( OP_VAL_16(1), com.i_direct );
1548 }
1549
1550 /*****************************************************************************
1551  * CommandRead : translates the command strings in ifo into command
1552  * structures.
1553  *****************************************************************************/
1554 void CommandRead( ifo_command_t com )
1555 {
1556     u8*     pi_code = (u8*)(&com);
1557
1558     switch( com.i_type )
1559     {
1560     /* Goto */
1561     case 0:
1562         /* Main command */
1563         if( !pi_code[1] )
1564         {
1565             printf( "NOP\n" );
1566         }
1567         else
1568         {
1569             if( com.i_cmp )
1570             {
1571                 printf ("if (r[0x%02x] %s ", OP_VAL_8(1)&0x0f,
1572                                              ifo_cmp[com.i_cmp]);
1573                 IfoRegister (OP_VAL_16(1), com.i_dir_cmp);
1574                 printf (") ");
1575             }
1576         
1577             /* Sub command */
1578             switch( com.i_sub_cmd )
1579             {
1580             case 1:
1581                 printf( "goto Line 0x%02x", OP_VAL_16(2) );
1582                 break;
1583         
1584             case 2:
1585                 printf( "stop VM" );
1586                 break;
1587         
1588             case 3:
1589                 printf( "Set Parental Level To %s and goto Line 0x%02x",
1590                                      ifo_parental[OP_VAL_8(4)&0x7],
1591                                      OP_VAL_8(5) );
1592                 break;
1593         
1594             default:
1595                 printf( "Illegal" );
1596                 break;
1597             }
1598         }
1599         break;
1600
1601     /* Lnk */
1602     case 1:
1603         /* Main command */
1604         if( !pi_code[1] )
1605         {
1606             printf( "NOP\n" );
1607         }
1608         else
1609         {
1610             if( com.i_direct )
1611             {
1612                 if( com.i_cmp )
1613                 {
1614                     printf( "if (r[0x%02x] %s ", OP_VAL_8(4)&0x0f,
1615                                                  ifo_cmp[com.i_cmp] );
1616                     IfoRegister( OP_VAL_8(5), 0 );
1617                     printf( ") " );
1618                 }
1619
1620                 /* Sub command */
1621                 IfoJmp( com );
1622             }
1623             else
1624             {    
1625                 if( com.i_cmp )
1626                 {
1627                     printf( "if (r[0x%02x] %s ", OP_VAL_8(1)&0x0f,
1628                                                  ifo_cmp[com.i_cmp] );
1629                     IfoRegister( OP_VAL_16(1), com.i_dir_cmp );
1630                     printf( ") " );
1631                 }
1632
1633                 /* Sub command */
1634                 IfoLnk( com );
1635             }
1636         }
1637         break;
1638
1639     /* SetSystem */
1640     case 2:
1641         if( !pi_code[1] )
1642         {
1643             IfoSetSystem( com );
1644         }
1645         else if( com.i_cmp && !com.i_sub_cmd )
1646         {
1647             printf ("if (r[0x%02x] %s ", OP_VAL_8(4)&0x0f, ifo_cmp[com.i_cmp]);
1648             IfoRegister( OP_VAL_8(5), 0 );
1649             printf (") ");
1650             IfoSetSystem( com );
1651         }
1652         else if( !com.i_cmp && com.i_sub_cmd )
1653         {
1654             printf( "if (" );
1655             IfoSetSystem( com );
1656             printf( ") " );
1657             IfoLnk( com );
1658         }
1659         else
1660         {
1661             printf("nop");
1662         }
1663         break;
1664
1665     /* Set */
1666     case 3:
1667           if( ! pi_code[1] )
1668         {
1669             IfoSet( com );
1670         }
1671         else if( com.i_cmp && !com.i_sub_cmd )
1672         {
1673             printf ("if (r[0x%02x] %s ", OP_VAL_8(0)&0x0f, ifo_cmp[com.i_cmp]);
1674             IfoRegister( OP_VAL_16(2), com.i_dir_cmp );
1675             printf (") ");
1676             IfoSet( com );
1677         }
1678         else if( !com.i_cmp && com.i_sub_cmd )
1679         {
1680             printf ("if (");
1681             IfoSet( com );
1682             printf (") ");
1683             IfoLnk( com );
1684         }
1685         else
1686         {
1687             printf( "nop" );
1688         }
1689         break;
1690
1691     /* 
1692      * math command on r[opcode[1]] and
1693      * direct?be2me_16(OP_VAL_8(0)):reg[OP_VAL_8(1)] is executed
1694      * ( unless command is swap; then r[opcode[1]] and r[OP_VAL_8(1)]
1695      * are swapped )
1696      * boolean operation cmp on r[opcode[1]] and
1697      * dir_cmp?be2me_16(OP_VAL_8(1)[1]):reg[OP_VAL_8(3)] is executed
1698      * on true result, buttons(c[6], c[7]) is called
1699      * problem is 'what is buttons()'
1700      */
1701     case 4:
1702         printf( "r[0x%X] ", pi_code[1] );
1703         printf( " %s ", IfoMath( com.i_cmd ) );
1704         if( com.i_cmd == 2 )
1705         {
1706             printf( "r[0x%X] ", OP_VAL_8(1) );
1707         }
1708         else
1709         {
1710             IfoRegister( OP_VAL_16(0), com.i_direct );
1711         }
1712         printf("; ");
1713
1714         printf( "if ( r[%d] %s ", pi_code[1], ifo_cmp[com.i_cmp] );
1715         IfoRegister( OP_VAL_8(1), com.i_dir_cmp );
1716         printf( " )  then {" );
1717         IfoAdvanced( &OP_VAL_8(4) );
1718         printf( "}" );
1719         break;
1720
1721     /*
1722      * opposite to case 4: boolean, math and buttons.
1723      */
1724     case 5:
1725     case 6:
1726         printf("if (");
1727
1728         if( !com.i_direct && com.i_dir_cmp )
1729         {
1730             printf( "0x%X", OP_VAL_16(1) );
1731         }
1732         else
1733         {
1734             IfoRegister( OP_VAL_8(3), 0 );
1735             if( OP_VAL_8(3)&0x80 )
1736             {
1737                 printf( "s[%s]", ifo_reg[OP_VAL_8(3)&0x1F] );
1738             }
1739             else
1740             {
1741                 printf( "r[0x%X]", OP_VAL_8(3)&0x1F);
1742                     // 0x1F is either not a mistake,
1743                     // or Microsoft programmer's mistake!!!
1744             }
1745         }
1746
1747         printf( " %s r[0x%X] ", ifo_cmp[com.i_cmp],
1748                                 com.i_direct ? OP_VAL_8(2) : OP_VAL_8(1) );
1749            printf( " )  then {" );
1750         printf( "r[0x%X] ", pi_code[1] & 0xF );
1751         printf( " %s ", IfoMath( com.i_cmd ) );
1752
1753         if( com.i_cmd == 0x02 )    // swap
1754         {
1755             printf("r[0x%X] ", OP_VAL_8(0)&0x1F);
1756         }
1757         else
1758         {
1759             if( com.i_direct )
1760             {
1761                 printf( "0x%X", OP_VAL_16(0) );
1762             }
1763             else
1764             {
1765                 if( OP_VAL_8(0) & 0x80 )
1766                 {
1767                     printf("s[%s]", ifo_reg[OP_VAL_8(0) & 0x1F] );
1768                 }
1769                 else
1770                 {
1771                     printf("r[0x%X]", OP_VAL_8(0) & 0x1F );
1772                 }
1773             }
1774         }
1775
1776         printf("; ");
1777         IfoAdvanced( &OP_VAL_8(4) );
1778         printf("}");
1779
1780         break;
1781
1782     default:
1783         printf( "Unknown Command\n" );
1784         break;
1785     }
1786
1787     return;
1788 }
1789
1790 /*****************************************************************************
1791  * CommandPrint : print in clear text (I hope so !) what a command does
1792  *****************************************************************************/
1793 void CommandPrint( ifo_t ifo )
1794 {
1795     return;
1796 }
1797
1798 #endif