]> git.sesse.net Git - vlc/blob - plugins/dvd/dvd_ifo.c
-adaptation of DVD module to navigation slider. The seek has to be
[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.6 2001/02/13 10:08:51 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     /* Video Manager Initialization */
1099     intf_WarnMsg( 2, "ifo: initializing VMG" );
1100     p_ifo->vmg = ReadVMG( p_ifo );
1101
1102     /* Video Title Sets initialization */
1103     p_ifo->p_vts = malloc( p_ifo->vmg.mat.i_tts_nb *sizeof(vts_t) );
1104     if( p_ifo->p_vts == NULL )
1105     {
1106         intf_ErrMsg( "Out of memory" );
1107         p_ifo->b_error = 1;
1108         return;
1109     }
1110     for( i=0 ; i<p_ifo->vmg.mat.i_tts_nb ; i++ )
1111     {
1112
1113         intf_WarnMsg( 2, "ifo: initializing VTS %d", i+1 );
1114
1115         i_off = (off_t)(p_ifo->vmg.ptt_srpt.p_tts[i].i_ssector) *DVD_LB_SIZE;
1116         p_ifo->i_pos = lseek( p_ifo->i_fd, i_off, SEEK_SET );
1117 //fprintf( stderr, "%lld\n" , p_ifo->i_pos );
1118
1119         /* FIXME : use udf filesystem to avoid this */
1120         IfoFindVTS( p_ifo );
1121         p_ifo->p_vts[i] = ReadVTS( p_ifo );
1122     }
1123     return; 
1124 }
1125
1126 /*
1127  * IFO virtual machine : a set of commands that give the
1128  * interactive behaviour of the dvd
1129  */
1130 #if 1
1131
1132 #define OP_VAL_16(i) (ntoh16( com.data.pi_16[i]))
1133 #define OP_VAL_8(i) ((com.data.pi_8[i]))
1134
1135 static char ifo_reg[][80]=
1136 {
1137     "Menu_Language_Code",
1138     "Audio_Stream_#",
1139     "SubPicture_Stream_#",
1140     "Angle_#",
1141     "VTS_#",
1142     "VTS_Title_#",
1143     "PGC_#",
1144     "PTT_#",
1145     "Highlighted_Button_#",
1146     "Nav_Timer",
1147     "TimedPGC",
1148     "Karaoke_audio_mixing_mode",
1149     "Parental_mgmt_country_code",
1150     "Parental_Level",
1151     "Player_Video_Cfg",
1152     "Player_Audio_Cfg",
1153     "Audio_language_code_setting",
1154     "Audio_language_extension_code",
1155     "SPU_language_code_setting",
1156     "SPU_language_extension_code",
1157     "?Player_Regional_Code",
1158     "Reserved_21",
1159     "Reserved_22",
1160     "Reserved_23"
1161 };
1162
1163 static char * IfoMath( char val )
1164 {
1165     static char math_op[][10] =
1166     {
1167         "none",
1168         "=", 
1169         "<->",    // swap
1170         "+=",
1171         "-=",
1172         "*=",
1173         "/=",
1174         "%=",
1175         "rnd",    // rnd
1176         "&=",
1177         "|=",
1178         "^=",
1179         "??",    // invalid
1180         "??",    // invalid
1181         "??",    // invalid
1182         "??"    // invalid
1183     };
1184
1185     return (char *) math_op[val & 0x0f];
1186 }
1187
1188
1189 char ifo_cmp[][10] =
1190 {
1191     "none",
1192     "&&",
1193     "==",
1194     "!=",
1195     ">=",
1196     ">",
1197     "<",
1198     "<="
1199 };
1200
1201 char ifo_parental[][10] =
1202 {
1203     "0",
1204     "G",
1205     "2",
1206     "PG",
1207     "PG-13",
1208     "5",
1209     "R",
1210     "NC-17"
1211 };
1212
1213 char ifo_menu_id[][80] =
1214 {
1215     "-0-",
1216     "-1-",
1217     "Title (VTS menu)",
1218     "Root",
1219     "Sub-Picture",
1220     "Audio",
1221     "Angle",
1222     "Part of Title",
1223 };
1224
1225 char * IfoMenuName( char index )
1226 {
1227     return ifo_menu_id[index&0x07];
1228 }
1229
1230 static void IfoRegister( u16 i_data, u8 i_direct)
1231 {
1232     if( i_direct )
1233     {
1234         if( 0/*isalpha( i_data >> 8 & 0xff )*/ )
1235         {
1236             printf("'%c%c'", i_data>>8&0xff, i_data&0xff);
1237         }
1238         else
1239         {
1240             printf("0x%02x", i_data);
1241         }
1242     }
1243     else
1244     {
1245         if( i_data & 0x80 )
1246         {
1247             i_data &= 0x1f;
1248
1249             if( i_data > 0x17 )
1250             {
1251                 printf("s[ILL]");
1252             }
1253             else
1254             {
1255                 printf("s[%s]", ifo_reg[i_data]);
1256             }
1257         }
1258         else
1259         {
1260             i_data &= 0x1f;
1261
1262             if( i_data > 0xf )
1263             {
1264                 printf("r[ILL]");
1265             }
1266             else
1267             {
1268                 printf("r[0x%02x]", i_data);
1269             }
1270         }
1271     }
1272 }
1273
1274 static void IfoAdvanced( u8 *pi_code )
1275 {
1276     u8      i_cmd = pi_code[0];
1277
1278     printf(" { ");
1279
1280     if( pi_code[1]>>2 )
1281     {
1282         printf( " Highlight button %d; ", pi_code[1]>>2 );
1283     }
1284
1285     if( i_cmd == 0xff )
1286     {
1287         printf( " Illegal " );
1288     }
1289
1290     if( i_cmd == 0x00 )
1291     {
1292         printf( "ReSuME %d", pi_code[7] );
1293     }
1294     else if( ( i_cmd & 0x06) == 0x02 )
1295     {    // XX01Y
1296         printf ("Link to %s cell ", ( i_cmd & 0x01 ) ? "prev" : "next");
1297     }
1298     else
1299     {
1300         printf( "advanced (0x%02x) ", i_cmd );
1301     }
1302     printf(" } ");
1303 }
1304
1305 static void IfoJmp( ifo_command_t com )
1306 {
1307
1308     printf ("jmp ");
1309
1310     switch( com.i_sub_cmd )
1311     {
1312     case 0x01:
1313         printf( "Exit" );
1314         break;
1315     case 0x02:
1316         printf( "VTS 0x%02x", OP_VAL_8(3) );
1317         break;
1318     case 0x03:
1319         printf( "This VTS Title 0x%02x", OP_VAL_8(3) );
1320         break;
1321     case 0x05:
1322         printf( "This VTS Title 0x%02x Part 0x%04x",
1323                             OP_VAL_8(3),
1324                             OP_VAL_8(0)<<8|OP_VAL_8(1));
1325         break;
1326     case 0x06:
1327 #if 0
1328             printf ("in SystemSpace ");
1329             switch (OP_VAL_8(3)>>4) {
1330                 case 0x00:
1331                     printf ("to play first PGC");
1332                     break;
1333                 case 0x01: {
1334                     printf ("to menu \"%s\"", decode_menuname (OP_VAL_8(3)));
1335                 }
1336                     break;
1337                 case 0x02:
1338                     printf ("to VTS 0x%02x and TTN 0x%02x", OP_VAL_8(1), OP_VAL_8(2));
1339                     break;
1340                 case 0x03:
1341                     printf ("to VMGM PGC number 0x%02x", OP_VAL_8(0)<<8 | OP_VAL_8(1));
1342                     break;
1343                 case 0x08:
1344                     printf ("vts 0x%02x lu 0x%02x menu \"%s\"", OP_VAL_8(2), OP_VAL_8(1), decode_menuname (OP_VAL_8(3)));
1345                     break;
1346 #else
1347         switch( OP_VAL_8(3)>>6 )
1348         {
1349         case 0x00:
1350             printf( "to play first PGC" );
1351             break;                
1352         case 0x01:
1353             printf( "to VMG title menu (?)" );
1354             break;
1355         case 0x02:
1356             printf( "vts 0x%02x lu 0x%02x menu \"%s\"",
1357                             OP_VAL_8(2),
1358                             OP_VAL_8(1),
1359                             IfoMenuName( OP_VAL_8(3)&0xF ) );
1360             break;                
1361         case 0x03:
1362             printf( "vmg pgc 0x%04x (?)", ( OP_VAL_8(0)<<8) | OP_VAL_8(1) );
1363             break;
1364 #endif
1365         }
1366         break;
1367     case 0x08:
1368 #if 0
1369             switch(OP_VAL_8(3)>>4) {
1370                 case 0x00:
1371                     printf ("system first pgc");
1372                     break;
1373                 case 0x01:
1374                     printf ("system title menu");
1375                     break;
1376                 case 0x02:
1377                     printf ("system menu \"%s\"", decode_menuname (OP_VAL_8(3)));
1378                     break;
1379                 case 0x03:
1380                     printf ("system vmg pgc %02x ????", OP_VAL_8(0)<<8|OP_VAL_8(1));
1381                     break;
1382                 case 0x08:
1383                     printf ("system lu 0x%02x menu \"%s\"", OP_VAL_8(2), decode_menuname (OP_VAL_8(3)));
1384                     break;
1385                 case 0x0c:
1386                     printf ("system vmg pgc 0x%02x", OP_VAL_8(0)<<8|OP_VAL_8(1));
1387                     break;
1388             }
1389 #else
1390         // OP_VAL_8(2) is number of cell
1391         // it is processed BEFORE switch
1392         // under some conditions, it is ignored
1393         // I don't understand exactly what it means
1394         printf( " ( spec cell 0x%02X ) ", OP_VAL_8(2) ); 
1395
1396         switch( OP_VAL_8(3)>>6 )
1397         {
1398         case 0:
1399             printf( "to FP PGC" );
1400             break;
1401         case 1:
1402             printf( "to VMG root menu (?)" );
1403             break;
1404         case 2:
1405             printf( "to VTS menu \"%s\" (?)",
1406                     IfoMenuName(OP_VAL_8(3)&0xF) );
1407             break; 
1408         case 3:
1409             printf( "vmg pgc 0x%02x (?)", (OP_VAL_8(0)<<8)|OP_VAL_8(1) );
1410             break;
1411         }    
1412 #endif
1413         break;
1414     }
1415 }
1416
1417 static void IfoLnk( ifo_command_t com )
1418 {
1419     u16     i_button=OP_VAL_8(4)>>2;
1420
1421     printf ("lnk to ");
1422
1423     switch( com.i_sub_cmd )
1424     {
1425     case 0x01:
1426         IfoAdvanced( &OP_VAL_8(4) );
1427         break;
1428
1429     case 0x04:
1430         printf( "PGC 0x%02x", OP_VAL_16(2) );
1431         break; 
1432
1433     case 0x05:
1434         printf( "PTT 0x%02x", OP_VAL_16(2) );
1435         break; 
1436
1437     case 0x06:
1438         printf( "Program 0x%02x this PGC", OP_VAL_8(5) );
1439         break;
1440
1441     case 0x07:
1442         printf( "Cell 0x%02x this PGC", OP_VAL_8(5) );
1443         break;
1444     default:
1445         return;
1446     }
1447
1448     if( i_button )
1449     {
1450         printf( ", Highlight 0x%02x", OP_VAL_8(4)>>2 );
1451     }
1452             
1453 }
1454
1455 void IfoSetSystem( ifo_command_t com )
1456 {
1457     switch( com.i_cmd )
1458     {
1459     case 1: {
1460         int i;
1461
1462         for( i=1; i<=3; i++ )
1463         {
1464             if( OP_VAL_8(i)&0x80 )
1465             {
1466                 if( com.i_direct )
1467                 {
1468                     printf ("s[%s] = 0x%02x;", ifo_reg[i], OP_VAL_8(i)&0xf);
1469                 }
1470                 else
1471                 {
1472                     printf ("s[%s] = r[0x%02x];", ifo_reg[i], OP_VAL_8(i)&0xf);
1473                 }
1474             }
1475         }
1476 #if 0
1477                 if(op->direct) {
1478                         if(OP_VAL_8(1]&0x80)
1479                                 printf ("s[%s] = 0x%02x;", reg_name[1], OP_VAL_8(1]&0xf);
1480                         if(OP_VAL_8(2)&0x80)
1481 //DENT: lwhat about 0x7f here ???
1482                                 printf ("s[%s] = 0x%02x;", reg_name[2], OP_VAL_8(2)&0x7f);
1483                         if(OP_VAL_8(3)&0x80)
1484                                 printf ("s[%s] = 0x%02x;", reg_name[3], OP_VAL_8(3)&0xf);
1485                 } else {
1486                         if(OP_VAL_8(1)&0x80)
1487                                 printf ("s[%s] = r[0x%02x];", reg_name[1], OP_VAL_8(1)&0xf);
1488                         if(OP_VAL_8(2)&0x80)
1489                                 printf ("s[%s] = r[0x%02x];", reg_name[2], OP_VAL_8(2)&0xf);
1490                         if(OP_VAL_8(3)&0x80)
1491                                 printf ("s[%s] = r[0x%02x];", reg_name[3], OP_VAL_8(3)&0xf);
1492                 }
1493 #endif
1494         }
1495         break;
1496     case 2:
1497         if( com.i_direct )
1498         {
1499             printf( "s[%s] = 0x%02x", ifo_reg[9], OP_VAL_16(0) );
1500         }
1501         else
1502         {
1503             printf( "s[%s] = r[0x%02x]", ifo_reg[9], OP_VAL_8(1)&0x0f );
1504         }
1505
1506         printf( "s[%s] = (s[%s]&0x7FFF)|0x%02x", 
1507                         ifo_reg[10], ifo_reg[10], OP_VAL_16(1)&0x8000);
1508         break;
1509     case 3:
1510         if( com.i_direct )
1511         {
1512             printf( "r[0x%02x] = 0x%02x", OP_VAL_8(3)&0x0f, OP_VAL_16(0) );
1513         }
1514         else
1515         {
1516             printf ("r[r[0x%02x]] = r[0x%02x]",
1517                                     OP_VAL_8(3)&0x0f, OP_VAL_8(1)&0x0f);
1518         }
1519         break;
1520     case 4:
1521         //actually only bits 00011100 00011100 are set
1522         if( com.i_direct )
1523         {
1524             printf ("s[%s] = 0x%02x", ifo_reg[11], OP_VAL_16(1));
1525         }
1526         else
1527         {
1528             printf ("s[%s] = r[0x%02x]", ifo_reg[11], OP_VAL_8(3)&0x0f );
1529         }
1530         break;
1531     case 6:
1532         //actually,
1533         //s[%s]=(r[%s]&0x3FF) | (0x%02x << 0xA);
1534         //but it is way too ugly
1535         if( com.i_direct )
1536         {
1537             printf( "s[%s] = 0x%02x", ifo_reg[8], OP_VAL_8(2)>>2 );
1538         }
1539         else
1540         {
1541             printf( "s[%s] = r[0x%02x]", ifo_reg[8], OP_VAL_8(3)&0x0f );
1542         }
1543         break;
1544     default:
1545         printf ("unknown");
1546     }
1547 }
1548
1549 static void IfoSet( ifo_command_t com )
1550 {
1551     IfoRegister( OP_VAL_16(0), 0 );
1552     printf( " %s ", IfoMath( com.i_cmd ) );
1553     IfoRegister( OP_VAL_16(1), com.i_direct );
1554 }
1555
1556 /*****************************************************************************
1557  * CommandRead : translates the command strings in ifo into command
1558  * structures.
1559  *****************************************************************************/
1560 void CommandRead( ifo_command_t com )
1561 {
1562     u8*     pi_code = (u8*)(&com);
1563
1564     switch( com.i_type )
1565     {
1566     /* Goto */
1567     case 0:
1568         /* Main command */
1569         if( !pi_code[1] )
1570         {
1571             printf( "NOP\n" );
1572         }
1573         else
1574         {
1575             if( com.i_cmp )
1576             {
1577                 printf ("if (r[0x%02x] %s ", OP_VAL_8(1)&0x0f,
1578                                              ifo_cmp[com.i_cmp]);
1579                 IfoRegister (OP_VAL_16(1), com.i_dir_cmp);
1580                 printf (") ");
1581             }
1582         
1583             /* Sub command */
1584             switch( com.i_sub_cmd )
1585             {
1586             case 1:
1587                 printf( "goto Line 0x%02x", OP_VAL_16(2) );
1588                 break;
1589         
1590             case 2:
1591                 printf( "stop VM" );
1592                 break;
1593         
1594             case 3:
1595                 printf( "Set Parental Level To %s and goto Line 0x%02x",
1596                                      ifo_parental[OP_VAL_8(4)&0x7],
1597                                      OP_VAL_8(5) );
1598                 break;
1599         
1600             default:
1601                 printf( "Illegal" );
1602                 break;
1603             }
1604         }
1605         break;
1606
1607     /* Lnk */
1608     case 1:
1609         /* Main command */
1610         if( !pi_code[1] )
1611         {
1612             printf( "NOP\n" );
1613         }
1614         else
1615         {
1616             if( com.i_direct )
1617             {
1618                 if( com.i_cmp )
1619                 {
1620                     printf( "if (r[0x%02x] %s ", OP_VAL_8(4)&0x0f,
1621                                                  ifo_cmp[com.i_cmp] );
1622                     IfoRegister( OP_VAL_8(5), 0 );
1623                     printf( ") " );
1624                 }
1625
1626                 /* Sub command */
1627                 IfoJmp( com );
1628             }
1629             else
1630             {    
1631                 if( com.i_cmp )
1632                 {
1633                     printf( "if (r[0x%02x] %s ", OP_VAL_8(1)&0x0f,
1634                                                  ifo_cmp[com.i_cmp] );
1635                     IfoRegister( OP_VAL_16(1), com.i_dir_cmp );
1636                     printf( ") " );
1637                 }
1638
1639                 /* Sub command */
1640                 IfoLnk( com );
1641             }
1642         }
1643         break;
1644
1645     /* SetSystem */
1646     case 2:
1647         if( !pi_code[1] )
1648         {
1649             IfoSetSystem( com );
1650         }
1651         else if( com.i_cmp && !com.i_sub_cmd )
1652         {
1653             printf ("if (r[0x%02x] %s ", OP_VAL_8(4)&0x0f, ifo_cmp[com.i_cmp]);
1654             IfoRegister( OP_VAL_8(5), 0 );
1655             printf (") ");
1656             IfoSetSystem( com );
1657         }
1658         else if( !com.i_cmp && com.i_sub_cmd )
1659         {
1660             printf( "if (" );
1661             IfoSetSystem( com );
1662             printf( ") " );
1663             IfoLnk( com );
1664         }
1665         else
1666         {
1667             printf("nop");
1668         }
1669         break;
1670
1671     /* Set */
1672     case 3:
1673           if( ! pi_code[1] )
1674         {
1675             IfoSet( com );
1676         }
1677         else if( com.i_cmp && !com.i_sub_cmd )
1678         {
1679             printf ("if (r[0x%02x] %s ", OP_VAL_8(0)&0x0f, ifo_cmp[com.i_cmp]);
1680             IfoRegister( OP_VAL_16(2), com.i_dir_cmp );
1681             printf (") ");
1682             IfoSet( com );
1683         }
1684         else if( !com.i_cmp && com.i_sub_cmd )
1685         {
1686             printf ("if (");
1687             IfoSet( com );
1688             printf (") ");
1689             IfoLnk( com );
1690         }
1691         else
1692         {
1693             printf( "nop" );
1694         }
1695         break;
1696
1697     /* 
1698      * math command on r[opcode[1]] and
1699      * direct?be2me_16(OP_VAL_8(0)):reg[OP_VAL_8(1)] is executed
1700      * ( unless command is swap; then r[opcode[1]] and r[OP_VAL_8(1)]
1701      * are swapped )
1702      * boolean operation cmp on r[opcode[1]] and
1703      * dir_cmp?be2me_16(OP_VAL_8(1)[1]):reg[OP_VAL_8(3)] is executed
1704      * on true result, buttons(c[6], c[7]) is called
1705      * problem is 'what is buttons()'
1706      */
1707     case 4:
1708         printf( "r[0x%X] ", pi_code[1] );
1709         printf( " %s ", IfoMath( com.i_cmd ) );
1710         if( com.i_cmd == 2 )
1711         {
1712             printf( "r[0x%X] ", OP_VAL_8(1) );
1713         }
1714         else
1715         {
1716             IfoRegister( OP_VAL_16(0), com.i_direct );
1717         }
1718         printf("; ");
1719
1720         printf( "if ( r[%d] %s ", pi_code[1], ifo_cmp[com.i_cmp] );
1721         IfoRegister( OP_VAL_8(1), com.i_dir_cmp );
1722         printf( " )  then {" );
1723         IfoAdvanced( &OP_VAL_8(4) );
1724         printf( "}" );
1725         break;
1726
1727     /*
1728      * opposite to case 4: boolean, math and buttons.
1729      */
1730     case 5:
1731     case 6:
1732         printf("if (");
1733
1734         if( !com.i_direct && com.i_dir_cmp )
1735         {
1736             printf( "0x%X", OP_VAL_16(1) );
1737         }
1738         else
1739         {
1740             IfoRegister( OP_VAL_8(3), 0 );
1741             if( OP_VAL_8(3)&0x80 )
1742             {
1743                 printf( "s[%s]", ifo_reg[OP_VAL_8(3)&0x1F] );
1744             }
1745             else
1746             {
1747                 printf( "r[0x%X]", OP_VAL_8(3)&0x1F);
1748                     // 0x1F is either not a mistake,
1749                     // or Microsoft programmer's mistake!!!
1750             }
1751         }
1752
1753         printf( " %s r[0x%X] ", ifo_cmp[com.i_cmp],
1754                                 com.i_direct ? OP_VAL_8(2) : OP_VAL_8(1) );
1755            printf( " )  then {" );
1756         printf( "r[0x%X] ", pi_code[1] & 0xF );
1757         printf( " %s ", IfoMath( com.i_cmd ) );
1758
1759         if( com.i_cmd == 0x02 )    // swap
1760         {
1761             printf("r[0x%X] ", OP_VAL_8(0)&0x1F);
1762         }
1763         else
1764         {
1765             if( com.i_direct )
1766             {
1767                 printf( "0x%X", OP_VAL_16(0) );
1768             }
1769             else
1770             {
1771                 if( OP_VAL_8(0) & 0x80 )
1772                 {
1773                     printf("s[%s]", ifo_reg[OP_VAL_8(0) & 0x1F] );
1774                 }
1775                 else
1776                 {
1777                     printf("r[0x%X]", OP_VAL_8(0) & 0x1F );
1778                 }
1779             }
1780         }
1781
1782         printf("; ");
1783         IfoAdvanced( &OP_VAL_8(4) );
1784         printf("}");
1785
1786         break;
1787
1788     default:
1789         printf( "Unknown Command\n" );
1790         break;
1791     }
1792
1793     return;
1794 }
1795
1796 /*****************************************************************************
1797  * CommandPrint : print in clear text (I hope so !) what a command does
1798  *****************************************************************************/
1799 void CommandPrint( ifo_t ifo )
1800 {
1801     return;
1802 }
1803
1804 #endif