Mercurial > libdvdnav.hg
annotate dvdread/ifo_read.c @ 350:950749fdce4a src
read_multichannel_ext() uses getbits() instead of relying on endianness-specific bitfields
author | nicodvb |
---|---|
date | Sat, 03 May 2008 08:04:34 +0000 |
parents | 8a711d89e294 |
children | c2218d2a9387 |
rev | line source |
---|---|
225 | 1 /* |
2 * Copyright (C) 2000, 2001, 2002, 2003 | |
3 * Björn Englund <d4bjorn@dtek.chalmers.se>, | |
4 * Håkan Hjort <d95hjort@dtek.chalmers.se> | |
5 * | |
6 * This program is free software; you can redistribute it and/or modify | |
7 * it under the terms of the GNU General Public License as published by | |
8 * the Free Software Foundation; either version 2 of the License, or | |
9 * (at your option) any later version. | |
10 * | |
11 * This program is distributed in the hope that it will be useful, | |
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 * GNU General Public License for more details. | |
15 * | |
16 * You should have received a copy of the GNU General Public License | |
17 * along with this program; if not, write to the Free Software | |
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
19 */ | |
20 | |
21 #include "config.h" | |
22 | |
23 #include <stdio.h> | |
24 #include <stdlib.h> | |
25 #include <inttypes.h> | |
26 #include <string.h> | |
27 | |
28 #include "bswap.h" | |
29 #include "ifo_types.h" | |
30 #include "ifo_read.h" | |
31 #include "dvd_reader.h" | |
32 #include "dvdread_internal.h" | |
348
5300b5982b85
video_attr_t is now defined and read correctly, rather than relying on conditional bitfields
nicodvb
parents:
346
diff
changeset
|
33 #include "bitreader.h" |
225 | 34 |
35 #ifndef DVD_BLOCK_LEN | |
36 #define DVD_BLOCK_LEN 2048 | |
37 #endif | |
38 | |
39 #ifndef NDEBUG | |
40 #define CHECK_ZERO0(arg) \ | |
41 if(arg != 0) { \ | |
42 fprintf(stderr, "*** Zero check failed in %s:%i\n for %s = 0x%x\n", \ | |
43 __FILE__, __LINE__, # arg, arg); \ | |
44 } | |
45 #define CHECK_ZERO(arg) \ | |
46 if(memcmp(my_friendly_zeros, &arg, sizeof(arg))) { \ | |
47 unsigned int i_CZ; \ | |
48 fprintf(stderr, "*** Zero check failed in %s:%i\n for %s = 0x", \ | |
49 __FILE__, __LINE__, # arg ); \ | |
50 for(i_CZ = 0; i_CZ < sizeof(arg); i_CZ++) \ | |
51 fprintf(stderr, "%02x", *((uint8_t *)&arg + i_CZ)); \ | |
52 fprintf(stderr, "\n"); \ | |
53 } | |
54 static const uint8_t my_friendly_zeros[2048]; | |
55 #else | |
56 #define CHECK_ZERO0(arg) (void)(arg) | |
57 #define CHECK_ZERO(arg) (void)(arg) | |
58 #endif | |
59 | |
60 | |
61 /* Prototypes for internal functions */ | |
62 static int ifoRead_VMG(ifo_handle_t *ifofile); | |
63 static int ifoRead_VTS(ifo_handle_t *ifofile); | |
64 static int ifoRead_PGC(ifo_handle_t *ifofile, pgc_t *pgc, unsigned int offset); | |
65 static int ifoRead_PGC_COMMAND_TBL(ifo_handle_t *ifofile, | |
66 pgc_command_tbl_t *cmd_tbl, | |
67 unsigned int offset); | |
68 static int ifoRead_PGC_PROGRAM_MAP(ifo_handle_t *ifofile, | |
69 pgc_program_map_t *program_map, | |
70 unsigned int nr, unsigned int offset); | |
71 static int ifoRead_CELL_PLAYBACK_TBL(ifo_handle_t *ifofile, | |
72 cell_playback_t *cell_playback, | |
73 unsigned int nr, unsigned int offset); | |
74 static int ifoRead_CELL_POSITION_TBL(ifo_handle_t *ifofile, | |
75 cell_position_t *cell_position, | |
76 unsigned int nr, unsigned int offset); | |
77 static int ifoRead_VTS_ATTRIBUTES(ifo_handle_t *ifofile, | |
78 vts_attributes_t *vts_attributes, | |
79 unsigned int offset); | |
80 static int ifoRead_C_ADT_internal(ifo_handle_t *ifofile, c_adt_t *c_adt, | |
81 unsigned int sector); | |
82 static int ifoRead_VOBU_ADMAP_internal(ifo_handle_t *ifofile, | |
83 vobu_admap_t *vobu_admap, | |
84 unsigned int sector); | |
85 static int ifoRead_PGCIT_internal(ifo_handle_t *ifofile, pgcit_t *pgcit, | |
86 unsigned int offset); | |
87 | |
88 static void ifoFree_PGC(pgc_t *pgc); | |
89 static void ifoFree_PGC_COMMAND_TBL(pgc_command_tbl_t *cmd_tbl); | |
90 static void ifoFree_PGCIT_internal(pgcit_t *pgcit); | |
91 | |
92 | |
93 static inline int DVDFileSeek_( dvd_file_t *dvd_file, uint32_t offset ) { | |
94 return (DVDFileSeek(dvd_file, (int)offset) == (int)offset); | |
95 } | |
96 | |
348
5300b5982b85
video_attr_t is now defined and read correctly, rather than relying on conditional bitfields
nicodvb
parents:
346
diff
changeset
|
97 static void read_video_attr(video_attr_t *va) { |
5300b5982b85
video_attr_t is now defined and read correctly, rather than relying on conditional bitfields
nicodvb
parents:
346
diff
changeset
|
98 getbits_state_t state; |
5300b5982b85
video_attr_t is now defined and read correctly, rather than relying on conditional bitfields
nicodvb
parents:
346
diff
changeset
|
99 uint8_t buf[sizeof(video_attr_t)]; |
5300b5982b85
video_attr_t is now defined and read correctly, rather than relying on conditional bitfields
nicodvb
parents:
346
diff
changeset
|
100 |
5300b5982b85
video_attr_t is now defined and read correctly, rather than relying on conditional bitfields
nicodvb
parents:
346
diff
changeset
|
101 memcpy(buf, va, sizeof(video_attr_t)); |
5300b5982b85
video_attr_t is now defined and read correctly, rather than relying on conditional bitfields
nicodvb
parents:
346
diff
changeset
|
102 if (!dvdread_getbits_init(&state, buf)) abort(); |
5300b5982b85
video_attr_t is now defined and read correctly, rather than relying on conditional bitfields
nicodvb
parents:
346
diff
changeset
|
103 va->mpeg_version = dvdread_getbits(&state, 2); |
5300b5982b85
video_attr_t is now defined and read correctly, rather than relying on conditional bitfields
nicodvb
parents:
346
diff
changeset
|
104 va->video_format = dvdread_getbits(&state, 2); |
5300b5982b85
video_attr_t is now defined and read correctly, rather than relying on conditional bitfields
nicodvb
parents:
346
diff
changeset
|
105 va->display_aspect_ratio = dvdread_getbits(&state, 2); |
5300b5982b85
video_attr_t is now defined and read correctly, rather than relying on conditional bitfields
nicodvb
parents:
346
diff
changeset
|
106 va->permitted_df = dvdread_getbits(&state, 2); |
5300b5982b85
video_attr_t is now defined and read correctly, rather than relying on conditional bitfields
nicodvb
parents:
346
diff
changeset
|
107 va->line21_cc_1 = dvdread_getbits(&state, 1); |
5300b5982b85
video_attr_t is now defined and read correctly, rather than relying on conditional bitfields
nicodvb
parents:
346
diff
changeset
|
108 va->line21_cc_2 = dvdread_getbits(&state, 1); |
5300b5982b85
video_attr_t is now defined and read correctly, rather than relying on conditional bitfields
nicodvb
parents:
346
diff
changeset
|
109 va->unknown1 = dvdread_getbits(&state, 1); |
5300b5982b85
video_attr_t is now defined and read correctly, rather than relying on conditional bitfields
nicodvb
parents:
346
diff
changeset
|
110 va->bit_rate = dvdread_getbits(&state, 1); |
5300b5982b85
video_attr_t is now defined and read correctly, rather than relying on conditional bitfields
nicodvb
parents:
346
diff
changeset
|
111 va->picture_size = dvdread_getbits(&state, 2); |
5300b5982b85
video_attr_t is now defined and read correctly, rather than relying on conditional bitfields
nicodvb
parents:
346
diff
changeset
|
112 va->letterboxed = dvdread_getbits(&state, 1); |
5300b5982b85
video_attr_t is now defined and read correctly, rather than relying on conditional bitfields
nicodvb
parents:
346
diff
changeset
|
113 va->film_mode = dvdread_getbits(&state, 1); |
5300b5982b85
video_attr_t is now defined and read correctly, rather than relying on conditional bitfields
nicodvb
parents:
346
diff
changeset
|
114 } |
225 | 115 |
349
8a711d89e294
read_audio_attr() uses getbits() instead of relying on endianness-specific bitfields
nicodvb
parents:
348
diff
changeset
|
116 static void read_audio_attr(audio_attr_t *aa) { |
8a711d89e294
read_audio_attr() uses getbits() instead of relying on endianness-specific bitfields
nicodvb
parents:
348
diff
changeset
|
117 getbits_state_t state; |
8a711d89e294
read_audio_attr() uses getbits() instead of relying on endianness-specific bitfields
nicodvb
parents:
348
diff
changeset
|
118 uint8_t buf[sizeof(audio_attr_t)]; |
8a711d89e294
read_audio_attr() uses getbits() instead of relying on endianness-specific bitfields
nicodvb
parents:
348
diff
changeset
|
119 |
8a711d89e294
read_audio_attr() uses getbits() instead of relying on endianness-specific bitfields
nicodvb
parents:
348
diff
changeset
|
120 memcpy(buf, aa, sizeof(audio_attr_t)); |
8a711d89e294
read_audio_attr() uses getbits() instead of relying on endianness-specific bitfields
nicodvb
parents:
348
diff
changeset
|
121 if (!dvdread_getbits_init(&state, buf)) abort(); |
8a711d89e294
read_audio_attr() uses getbits() instead of relying on endianness-specific bitfields
nicodvb
parents:
348
diff
changeset
|
122 aa->audio_format = dvdread_getbits(&state, 3); |
8a711d89e294
read_audio_attr() uses getbits() instead of relying on endianness-specific bitfields
nicodvb
parents:
348
diff
changeset
|
123 aa->multichannel_extension = dvdread_getbits(&state, 1); |
8a711d89e294
read_audio_attr() uses getbits() instead of relying on endianness-specific bitfields
nicodvb
parents:
348
diff
changeset
|
124 aa->lang_type = dvdread_getbits(&state, 2); |
8a711d89e294
read_audio_attr() uses getbits() instead of relying on endianness-specific bitfields
nicodvb
parents:
348
diff
changeset
|
125 aa->application_mode = dvdread_getbits(&state, 2); |
8a711d89e294
read_audio_attr() uses getbits() instead of relying on endianness-specific bitfields
nicodvb
parents:
348
diff
changeset
|
126 aa->quantization = dvdread_getbits(&state, 2); |
8a711d89e294
read_audio_attr() uses getbits() instead of relying on endianness-specific bitfields
nicodvb
parents:
348
diff
changeset
|
127 aa->sample_frequency = dvdread_getbits(&state, 2); |
8a711d89e294
read_audio_attr() uses getbits() instead of relying on endianness-specific bitfields
nicodvb
parents:
348
diff
changeset
|
128 aa->unknown1 = dvdread_getbits(&state, 1); |
8a711d89e294
read_audio_attr() uses getbits() instead of relying on endianness-specific bitfields
nicodvb
parents:
348
diff
changeset
|
129 aa->channels = dvdread_getbits(&state, 3); |
8a711d89e294
read_audio_attr() uses getbits() instead of relying on endianness-specific bitfields
nicodvb
parents:
348
diff
changeset
|
130 aa->lang_code = dvdread_getbits(&state, 16); |
8a711d89e294
read_audio_attr() uses getbits() instead of relying on endianness-specific bitfields
nicodvb
parents:
348
diff
changeset
|
131 aa->lang_extension = dvdread_getbits(&state, 8); |
8a711d89e294
read_audio_attr() uses getbits() instead of relying on endianness-specific bitfields
nicodvb
parents:
348
diff
changeset
|
132 aa->code_extension = dvdread_getbits(&state, 8); |
8a711d89e294
read_audio_attr() uses getbits() instead of relying on endianness-specific bitfields
nicodvb
parents:
348
diff
changeset
|
133 aa->unknown3 = dvdread_getbits(&state, 8); |
8a711d89e294
read_audio_attr() uses getbits() instead of relying on endianness-specific bitfields
nicodvb
parents:
348
diff
changeset
|
134 aa->app_info.karaoke.unknown4 = dvdread_getbits(&state, 1); |
8a711d89e294
read_audio_attr() uses getbits() instead of relying on endianness-specific bitfields
nicodvb
parents:
348
diff
changeset
|
135 aa->app_info.karaoke.channel_assignment = dvdread_getbits(&state, 3); |
8a711d89e294
read_audio_attr() uses getbits() instead of relying on endianness-specific bitfields
nicodvb
parents:
348
diff
changeset
|
136 aa->app_info.karaoke.version = dvdread_getbits(&state, 2); |
8a711d89e294
read_audio_attr() uses getbits() instead of relying on endianness-specific bitfields
nicodvb
parents:
348
diff
changeset
|
137 aa->app_info.karaoke.mc_intro = dvdread_getbits(&state, 1); |
8a711d89e294
read_audio_attr() uses getbits() instead of relying on endianness-specific bitfields
nicodvb
parents:
348
diff
changeset
|
138 aa->app_info.karaoke.mode = dvdread_getbits(&state, 1); |
8a711d89e294
read_audio_attr() uses getbits() instead of relying on endianness-specific bitfields
nicodvb
parents:
348
diff
changeset
|
139 fprintf(stderr, "\r\nLANG: %c%c, x: %x\r\n", aa->lang_code>>8, aa->lang_code&0xFF, aa->lang_code); |
8a711d89e294
read_audio_attr() uses getbits() instead of relying on endianness-specific bitfields
nicodvb
parents:
348
diff
changeset
|
140 } |
8a711d89e294
read_audio_attr() uses getbits() instead of relying on endianness-specific bitfields
nicodvb
parents:
348
diff
changeset
|
141 |
350
950749fdce4a
read_multichannel_ext() uses getbits() instead of relying on endianness-specific bitfields
nicodvb
parents:
349
diff
changeset
|
142 static void read_multichannel_ext(multichannel_ext_t *me) { |
950749fdce4a
read_multichannel_ext() uses getbits() instead of relying on endianness-specific bitfields
nicodvb
parents:
349
diff
changeset
|
143 getbits_state_t state; |
950749fdce4a
read_multichannel_ext() uses getbits() instead of relying on endianness-specific bitfields
nicodvb
parents:
349
diff
changeset
|
144 uint8_t buf[sizeof(multichannel_ext_t)]; |
950749fdce4a
read_multichannel_ext() uses getbits() instead of relying on endianness-specific bitfields
nicodvb
parents:
349
diff
changeset
|
145 |
950749fdce4a
read_multichannel_ext() uses getbits() instead of relying on endianness-specific bitfields
nicodvb
parents:
349
diff
changeset
|
146 memcpy(buf, me, sizeof(multichannel_ext_t)); |
950749fdce4a
read_multichannel_ext() uses getbits() instead of relying on endianness-specific bitfields
nicodvb
parents:
349
diff
changeset
|
147 if (!dvdread_getbits_init(&state, buf)) abort(); |
950749fdce4a
read_multichannel_ext() uses getbits() instead of relying on endianness-specific bitfields
nicodvb
parents:
349
diff
changeset
|
148 me->zero1 = dvdread_getbits(&state, 7); |
950749fdce4a
read_multichannel_ext() uses getbits() instead of relying on endianness-specific bitfields
nicodvb
parents:
349
diff
changeset
|
149 me->ach0_gme = dvdread_getbits(&state, 1); |
950749fdce4a
read_multichannel_ext() uses getbits() instead of relying on endianness-specific bitfields
nicodvb
parents:
349
diff
changeset
|
150 me->zero2 = dvdread_getbits(&state, 7); |
950749fdce4a
read_multichannel_ext() uses getbits() instead of relying on endianness-specific bitfields
nicodvb
parents:
349
diff
changeset
|
151 me->ach1_gme = dvdread_getbits(&state, 1); |
950749fdce4a
read_multichannel_ext() uses getbits() instead of relying on endianness-specific bitfields
nicodvb
parents:
349
diff
changeset
|
152 me->zero3 = dvdread_getbits(&state, 4); |
950749fdce4a
read_multichannel_ext() uses getbits() instead of relying on endianness-specific bitfields
nicodvb
parents:
349
diff
changeset
|
153 me->ach2_gv1e = dvdread_getbits(&state, 1); |
950749fdce4a
read_multichannel_ext() uses getbits() instead of relying on endianness-specific bitfields
nicodvb
parents:
349
diff
changeset
|
154 me->ach2_gv2e = dvdread_getbits(&state, 1); |
950749fdce4a
read_multichannel_ext() uses getbits() instead of relying on endianness-specific bitfields
nicodvb
parents:
349
diff
changeset
|
155 me->ach2_gm1e = dvdread_getbits(&state, 1); |
950749fdce4a
read_multichannel_ext() uses getbits() instead of relying on endianness-specific bitfields
nicodvb
parents:
349
diff
changeset
|
156 me->ach2_gm2e = dvdread_getbits(&state, 1); |
950749fdce4a
read_multichannel_ext() uses getbits() instead of relying on endianness-specific bitfields
nicodvb
parents:
349
diff
changeset
|
157 me->zero4 = dvdread_getbits(&state, 4); |
950749fdce4a
read_multichannel_ext() uses getbits() instead of relying on endianness-specific bitfields
nicodvb
parents:
349
diff
changeset
|
158 me->ach3_gv1e = dvdread_getbits(&state, 1); |
950749fdce4a
read_multichannel_ext() uses getbits() instead of relying on endianness-specific bitfields
nicodvb
parents:
349
diff
changeset
|
159 me->ach3_gv2e = dvdread_getbits(&state, 1); |
950749fdce4a
read_multichannel_ext() uses getbits() instead of relying on endianness-specific bitfields
nicodvb
parents:
349
diff
changeset
|
160 me->ach3_gmAe = dvdread_getbits(&state, 1); |
950749fdce4a
read_multichannel_ext() uses getbits() instead of relying on endianness-specific bitfields
nicodvb
parents:
349
diff
changeset
|
161 me->ach3_se2e = dvdread_getbits(&state, 1); |
950749fdce4a
read_multichannel_ext() uses getbits() instead of relying on endianness-specific bitfields
nicodvb
parents:
349
diff
changeset
|
162 me->zero5 = dvdread_getbits(&state, 4); |
950749fdce4a
read_multichannel_ext() uses getbits() instead of relying on endianness-specific bitfields
nicodvb
parents:
349
diff
changeset
|
163 me->ach4_gv1e = dvdread_getbits(&state, 1); |
950749fdce4a
read_multichannel_ext() uses getbits() instead of relying on endianness-specific bitfields
nicodvb
parents:
349
diff
changeset
|
164 me->ach4_gv2e = dvdread_getbits(&state, 1); |
950749fdce4a
read_multichannel_ext() uses getbits() instead of relying on endianness-specific bitfields
nicodvb
parents:
349
diff
changeset
|
165 me->ach4_gmBe = dvdread_getbits(&state, 1); |
950749fdce4a
read_multichannel_ext() uses getbits() instead of relying on endianness-specific bitfields
nicodvb
parents:
349
diff
changeset
|
166 me->ach4_seBe = dvdread_getbits(&state, 1); |
950749fdce4a
read_multichannel_ext() uses getbits() instead of relying on endianness-specific bitfields
nicodvb
parents:
349
diff
changeset
|
167 } |
950749fdce4a
read_multichannel_ext() uses getbits() instead of relying on endianness-specific bitfields
nicodvb
parents:
349
diff
changeset
|
168 |
950749fdce4a
read_multichannel_ext() uses getbits() instead of relying on endianness-specific bitfields
nicodvb
parents:
349
diff
changeset
|
169 |
225 | 170 ifo_handle_t *ifoOpen(dvd_reader_t *dvd, int title) { |
171 ifo_handle_t *ifofile; | |
172 | |
173 ifofile = (ifo_handle_t *)malloc(sizeof(ifo_handle_t)); | |
174 if(!ifofile) | |
321
e9dd884a3054
functions returning pointers should return NULL, not 0. part of ogle-1764
nicodvb
parents:
255
diff
changeset
|
175 return NULL; |
225 | 176 |
177 memset(ifofile, 0, sizeof(ifo_handle_t)); | |
178 | |
179 ifofile->file = DVDOpenFile(dvd, title, DVD_READ_INFO_FILE); | |
180 if(!ifofile->file) /* Should really catch any error and try to fallback */ | |
181 ifofile->file = DVDOpenFile(dvd, title, DVD_READ_INFO_BACKUP_FILE); | |
182 if(!ifofile->file) { | |
183 if(title) { | |
184 fprintf(stderr, "libdvdread: Can't open file VTS_%02d_0.IFO.\n", title); | |
185 } else { | |
186 fprintf(stderr, "libdvdread: Can't open file VIDEO_TS.IFO.\n"); | |
187 } | |
188 free(ifofile); | |
321
e9dd884a3054
functions returning pointers should return NULL, not 0. part of ogle-1764
nicodvb
parents:
255
diff
changeset
|
189 return NULL; |
225 | 190 } |
191 | |
192 /* First check if this is a VMGI file. */ | |
193 if(ifoRead_VMG(ifofile)) { | |
194 | |
195 /* These are both mandatory. */ | |
196 if(!ifoRead_FP_PGC(ifofile) || !ifoRead_TT_SRPT(ifofile)) { | |
346
e14f453bb208
some debug message to identify the cause of 'Invalid main menu IFO'
nicodvb
parents:
322
diff
changeset
|
197 fprintf(stderr, "libdvdread: Invalid main menu IFO (VIDEO_TS.IFO), ifoRead_FP_PGC() failed.\n"); |
225 | 198 ifoClose(ifofile); |
321
e9dd884a3054
functions returning pointers should return NULL, not 0. part of ogle-1764
nicodvb
parents:
255
diff
changeset
|
199 return NULL; |
225 | 200 } |
201 | |
202 ifoRead_PGCI_UT(ifofile); | |
203 ifoRead_PTL_MAIT(ifofile); | |
204 | |
205 /* This is also mandatory. */ | |
206 if(!ifoRead_VTS_ATRT(ifofile)) { | |
346
e14f453bb208
some debug message to identify the cause of 'Invalid main menu IFO'
nicodvb
parents:
322
diff
changeset
|
207 fprintf(stderr, "libdvdread: Invalid main menu IFO (VIDEO_TS.IFO), ifoRead_VTS_ATRT() failed.\n"); |
225 | 208 ifoClose(ifofile); |
321
e9dd884a3054
functions returning pointers should return NULL, not 0. part of ogle-1764
nicodvb
parents:
255
diff
changeset
|
209 return NULL; |
225 | 210 } |
211 | |
212 ifoRead_TXTDT_MGI(ifofile); | |
213 ifoRead_C_ADT(ifofile); | |
214 ifoRead_VOBU_ADMAP(ifofile); | |
215 | |
216 return ifofile; | |
217 } | |
218 | |
219 if(ifoRead_VTS(ifofile)) { | |
220 | |
221 if(!ifoRead_VTS_PTT_SRPT(ifofile) || !ifoRead_PGCIT(ifofile)) { | |
222 fprintf(stderr, "libdvdread: Invalid title IFO (VTS_%02d_0.IFO).\n", | |
223 title); | |
224 ifoClose(ifofile); | |
321
e9dd884a3054
functions returning pointers should return NULL, not 0. part of ogle-1764
nicodvb
parents:
255
diff
changeset
|
225 return NULL; |
225 | 226 } |
227 | |
228 | |
229 ifoRead_PGCI_UT(ifofile); | |
230 ifoRead_VTS_TMAPT(ifofile); | |
231 ifoRead_C_ADT(ifofile); | |
232 ifoRead_VOBU_ADMAP(ifofile); | |
233 | |
234 if(!ifoRead_TITLE_C_ADT(ifofile) || !ifoRead_TITLE_VOBU_ADMAP(ifofile)) { | |
235 fprintf(stderr, "libdvdread: Invalid title IFO (VTS_%02d_0.IFO).\n", | |
236 title); | |
237 ifoClose(ifofile); | |
321
e9dd884a3054
functions returning pointers should return NULL, not 0. part of ogle-1764
nicodvb
parents:
255
diff
changeset
|
238 return NULL; |
225 | 239 } |
240 | |
241 return ifofile; | |
242 } | |
243 | |
244 if(title) { | |
245 fprintf(stderr, "libdvdread: Invalid IFO for title %d (VTS_%02d_0.IFO).\n", | |
246 title, title); | |
247 } else { | |
248 fprintf(stderr, "libdvdread: Invalid IFO for VMGM (VIDEO_TS.IFO).\n"); | |
249 } | |
250 ifoClose(ifofile); | |
321
e9dd884a3054
functions returning pointers should return NULL, not 0. part of ogle-1764
nicodvb
parents:
255
diff
changeset
|
251 return NULL; |
225 | 252 } |
253 | |
254 | |
255 ifo_handle_t *ifoOpenVMGI(dvd_reader_t *dvd) { | |
256 ifo_handle_t *ifofile; | |
257 | |
258 ifofile = (ifo_handle_t *)malloc(sizeof(ifo_handle_t)); | |
259 if(!ifofile) | |
321
e9dd884a3054
functions returning pointers should return NULL, not 0. part of ogle-1764
nicodvb
parents:
255
diff
changeset
|
260 return NULL; |
225 | 261 |
262 memset(ifofile, 0, sizeof(ifo_handle_t)); | |
263 | |
264 ifofile->file = DVDOpenFile(dvd, 0, DVD_READ_INFO_FILE); | |
265 if(!ifofile->file) /* Should really catch any error and try to fallback */ | |
266 ifofile->file = DVDOpenFile(dvd, 0, DVD_READ_INFO_BACKUP_FILE); | |
267 if(!ifofile->file) { | |
268 fprintf(stderr, "libdvdread: Can't open file VIDEO_TS.IFO.\n"); | |
269 free(ifofile); | |
321
e9dd884a3054
functions returning pointers should return NULL, not 0. part of ogle-1764
nicodvb
parents:
255
diff
changeset
|
270 return NULL; |
225 | 271 } |
272 | |
273 if(ifoRead_VMG(ifofile)) | |
274 return ifofile; | |
275 | |
346
e14f453bb208
some debug message to identify the cause of 'Invalid main menu IFO'
nicodvb
parents:
322
diff
changeset
|
276 fprintf(stderr, "libdvdread,ifoOpenVMGI(): Invalid main menu IFO (VIDEO_TS.IFO).\n"); |
225 | 277 ifoClose(ifofile); |
321
e9dd884a3054
functions returning pointers should return NULL, not 0. part of ogle-1764
nicodvb
parents:
255
diff
changeset
|
278 return NULL; |
225 | 279 } |
280 | |
281 | |
282 ifo_handle_t *ifoOpenVTSI(dvd_reader_t *dvd, int title) { | |
283 ifo_handle_t *ifofile; | |
284 | |
285 ifofile = (ifo_handle_t *)malloc(sizeof(ifo_handle_t)); | |
286 if(!ifofile) | |
321
e9dd884a3054
functions returning pointers should return NULL, not 0. part of ogle-1764
nicodvb
parents:
255
diff
changeset
|
287 return NULL; |
225 | 288 |
289 memset(ifofile, 0, sizeof(ifo_handle_t)); | |
290 | |
291 if(title <= 0 || title > 99) { | |
292 fprintf(stderr, "libdvdread: ifoOpenVTSI invalid title (%d).\n", title); | |
293 free(ifofile); | |
321
e9dd884a3054
functions returning pointers should return NULL, not 0. part of ogle-1764
nicodvb
parents:
255
diff
changeset
|
294 return NULL; |
225 | 295 } |
296 | |
297 ifofile->file = DVDOpenFile(dvd, title, DVD_READ_INFO_FILE); | |
298 if(!ifofile->file) /* Should really catch any error and try to fallback */ | |
299 ifofile->file = DVDOpenFile(dvd, title, DVD_READ_INFO_BACKUP_FILE); | |
300 if(!ifofile->file) { | |
301 fprintf(stderr, "libdvdread: Can't open file VTS_%02d_0.IFO.\n", title); | |
302 free(ifofile); | |
321
e9dd884a3054
functions returning pointers should return NULL, not 0. part of ogle-1764
nicodvb
parents:
255
diff
changeset
|
303 return NULL; |
225 | 304 } |
305 | |
306 ifoRead_VTS(ifofile); | |
307 if(ifofile->vtsi_mat) | |
308 return ifofile; | |
309 | |
310 fprintf(stderr, "libdvdread: Invalid IFO for title %d (VTS_%02d_0.IFO).\n", | |
311 title, title); | |
312 ifoClose(ifofile); | |
321
e9dd884a3054
functions returning pointers should return NULL, not 0. part of ogle-1764
nicodvb
parents:
255
diff
changeset
|
313 return NULL; |
225 | 314 } |
315 | |
316 | |
317 void ifoClose(ifo_handle_t *ifofile) { | |
318 if(!ifofile) | |
319 return; | |
320 | |
321 ifoFree_VOBU_ADMAP(ifofile); | |
322 ifoFree_TITLE_VOBU_ADMAP(ifofile); | |
323 ifoFree_C_ADT(ifofile); | |
324 ifoFree_TITLE_C_ADT(ifofile); | |
325 ifoFree_TXTDT_MGI(ifofile); | |
326 ifoFree_VTS_ATRT(ifofile); | |
327 ifoFree_PTL_MAIT(ifofile); | |
328 ifoFree_PGCI_UT(ifofile); | |
329 ifoFree_TT_SRPT(ifofile); | |
330 ifoFree_FP_PGC(ifofile); | |
331 ifoFree_PGCIT(ifofile); | |
332 ifoFree_VTS_PTT_SRPT(ifofile); | |
322 | 333 ifoFree_VTS_TMAPT(ifofile); |
225 | 334 |
335 if(ifofile->vmgi_mat) | |
336 free(ifofile->vmgi_mat); | |
337 | |
338 if(ifofile->vtsi_mat) | |
339 free(ifofile->vtsi_mat); | |
340 | |
341 DVDCloseFile(ifofile->file); | |
342 ifofile->file = 0; | |
343 free(ifofile); | |
344 ifofile = 0; | |
345 } | |
346 | |
347 | |
348 static int ifoRead_VMG(ifo_handle_t *ifofile) { | |
349 vmgi_mat_t *vmgi_mat; | |
350 | |
351 vmgi_mat = (vmgi_mat_t *)malloc(sizeof(vmgi_mat_t)); | |
352 if(!vmgi_mat) | |
353 return 0; | |
354 | |
355 ifofile->vmgi_mat = vmgi_mat; | |
356 | |
357 if(!DVDFileSeek_(ifofile->file, 0)) { | |
358 free(ifofile->vmgi_mat); | |
359 ifofile->vmgi_mat = 0; | |
360 return 0; | |
361 } | |
362 | |
363 if(!DVDReadBytes(ifofile->file, vmgi_mat, sizeof(vmgi_mat_t))) { | |
364 free(ifofile->vmgi_mat); | |
365 ifofile->vmgi_mat = 0; | |
366 return 0; | |
367 } | |
368 | |
369 if(strncmp("DVDVIDEO-VMG", vmgi_mat->vmg_identifier, 12) != 0) { | |
370 free(ifofile->vmgi_mat); | |
371 ifofile->vmgi_mat = 0; | |
372 return 0; | |
373 } | |
374 | |
375 B2N_32(vmgi_mat->vmg_last_sector); | |
376 B2N_32(vmgi_mat->vmgi_last_sector); | |
377 B2N_32(vmgi_mat->vmg_category); | |
378 B2N_16(vmgi_mat->vmg_nr_of_volumes); | |
379 B2N_16(vmgi_mat->vmg_this_volume_nr); | |
380 B2N_16(vmgi_mat->vmg_nr_of_title_sets); | |
381 B2N_64(vmgi_mat->vmg_pos_code); | |
382 B2N_32(vmgi_mat->vmgi_last_byte); | |
383 B2N_32(vmgi_mat->first_play_pgc); | |
384 B2N_32(vmgi_mat->vmgm_vobs); | |
385 B2N_32(vmgi_mat->tt_srpt); | |
386 B2N_32(vmgi_mat->vmgm_pgci_ut); | |
387 B2N_32(vmgi_mat->ptl_mait); | |
388 B2N_32(vmgi_mat->vts_atrt); | |
389 B2N_32(vmgi_mat->txtdt_mgi); | |
390 B2N_32(vmgi_mat->vmgm_c_adt); | |
391 B2N_32(vmgi_mat->vmgm_vobu_admap); | |
392 B2N_16(vmgi_mat->vmgm_subp_attr.lang_code); | |
348
5300b5982b85
video_attr_t is now defined and read correctly, rather than relying on conditional bitfields
nicodvb
parents:
346
diff
changeset
|
393 read_video_attr(&vmgi_mat->vmgm_video_attr); |
349
8a711d89e294
read_audio_attr() uses getbits() instead of relying on endianness-specific bitfields
nicodvb
parents:
348
diff
changeset
|
394 read_audio_attr(&vmgi_mat->vmgm_audio_attr); |
225 | 395 |
396 | |
397 CHECK_ZERO(vmgi_mat->zero_1); | |
398 CHECK_ZERO(vmgi_mat->zero_2); | |
399 CHECK_ZERO(vmgi_mat->zero_3); | |
400 CHECK_ZERO(vmgi_mat->zero_4); | |
401 CHECK_ZERO(vmgi_mat->zero_5); | |
402 CHECK_ZERO(vmgi_mat->zero_6); | |
403 CHECK_ZERO(vmgi_mat->zero_7); | |
404 CHECK_ZERO(vmgi_mat->zero_8); | |
405 CHECK_ZERO(vmgi_mat->zero_9); | |
406 CHECK_ZERO(vmgi_mat->zero_10); | |
407 CHECK_VALUE(vmgi_mat->vmg_last_sector != 0); | |
408 CHECK_VALUE(vmgi_mat->vmgi_last_sector != 0); | |
409 CHECK_VALUE(vmgi_mat->vmgi_last_sector * 2 <= vmgi_mat->vmg_last_sector); | |
410 CHECK_VALUE(vmgi_mat->vmgi_last_sector * 2 <= vmgi_mat->vmg_last_sector); | |
411 CHECK_VALUE(vmgi_mat->vmg_nr_of_volumes != 0); | |
412 CHECK_VALUE(vmgi_mat->vmg_this_volume_nr != 0); | |
413 CHECK_VALUE(vmgi_mat->vmg_this_volume_nr <= vmgi_mat->vmg_nr_of_volumes); | |
414 CHECK_VALUE(vmgi_mat->disc_side == 1 || vmgi_mat->disc_side == 2); | |
415 CHECK_VALUE(vmgi_mat->vmg_nr_of_title_sets != 0); | |
416 CHECK_VALUE(vmgi_mat->vmgi_last_byte >= 341); | |
417 CHECK_VALUE(vmgi_mat->vmgi_last_byte / DVD_BLOCK_LEN <= | |
418 vmgi_mat->vmgi_last_sector); | |
419 /* It seems that first_play_pgc is optional. */ | |
420 CHECK_VALUE(vmgi_mat->first_play_pgc < vmgi_mat->vmgi_last_byte); | |
421 CHECK_VALUE(vmgi_mat->vmgm_vobs == 0 || | |
422 (vmgi_mat->vmgm_vobs > vmgi_mat->vmgi_last_sector && | |
423 vmgi_mat->vmgm_vobs < vmgi_mat->vmg_last_sector)); | |
424 CHECK_VALUE(vmgi_mat->tt_srpt <= vmgi_mat->vmgi_last_sector); | |
425 CHECK_VALUE(vmgi_mat->vmgm_pgci_ut <= vmgi_mat->vmgi_last_sector); | |
426 CHECK_VALUE(vmgi_mat->ptl_mait <= vmgi_mat->vmgi_last_sector); | |
427 CHECK_VALUE(vmgi_mat->vts_atrt <= vmgi_mat->vmgi_last_sector); | |
428 CHECK_VALUE(vmgi_mat->txtdt_mgi <= vmgi_mat->vmgi_last_sector); | |
429 CHECK_VALUE(vmgi_mat->vmgm_c_adt <= vmgi_mat->vmgi_last_sector); | |
430 CHECK_VALUE(vmgi_mat->vmgm_vobu_admap <= vmgi_mat->vmgi_last_sector); | |
431 | |
432 CHECK_VALUE(vmgi_mat->nr_of_vmgm_audio_streams <= 1); | |
433 CHECK_VALUE(vmgi_mat->nr_of_vmgm_subp_streams <= 1); | |
434 | |
435 return 1; | |
436 } | |
437 | |
438 | |
439 static int ifoRead_VTS(ifo_handle_t *ifofile) { | |
440 vtsi_mat_t *vtsi_mat; | |
441 int i; | |
442 | |
443 vtsi_mat = (vtsi_mat_t *)malloc(sizeof(vtsi_mat_t)); | |
444 if(!vtsi_mat) | |
445 return 0; | |
446 | |
447 ifofile->vtsi_mat = vtsi_mat; | |
448 | |
449 if(!DVDFileSeek_(ifofile->file, 0)) { | |
450 free(ifofile->vtsi_mat); | |
451 ifofile->vtsi_mat = 0; | |
452 return 0; | |
453 } | |
454 | |
455 if(!(DVDReadBytes(ifofile->file, vtsi_mat, sizeof(vtsi_mat_t)))) { | |
456 free(ifofile->vtsi_mat); | |
457 ifofile->vtsi_mat = 0; | |
458 return 0; | |
459 } | |
460 | |
461 if(strncmp("DVDVIDEO-VTS", vtsi_mat->vts_identifier, 12) != 0) { | |
462 free(ifofile->vtsi_mat); | |
463 ifofile->vtsi_mat = 0; | |
464 return 0; | |
465 } | |
466 | |
348
5300b5982b85
video_attr_t is now defined and read correctly, rather than relying on conditional bitfields
nicodvb
parents:
346
diff
changeset
|
467 read_video_attr(&vtsi_mat->vtsm_video_attr); |
5300b5982b85
video_attr_t is now defined and read correctly, rather than relying on conditional bitfields
nicodvb
parents:
346
diff
changeset
|
468 read_video_attr(&vtsi_mat->vts_video_attr); |
349
8a711d89e294
read_audio_attr() uses getbits() instead of relying on endianness-specific bitfields
nicodvb
parents:
348
diff
changeset
|
469 read_audio_attr(&vtsi_mat->vtsm_audio_attr); |
8a711d89e294
read_audio_attr() uses getbits() instead of relying on endianness-specific bitfields
nicodvb
parents:
348
diff
changeset
|
470 for(i=0; i<8; i++) |
8a711d89e294
read_audio_attr() uses getbits() instead of relying on endianness-specific bitfields
nicodvb
parents:
348
diff
changeset
|
471 read_audio_attr(&vtsi_mat->vts_audio_attr[i]); |
225 | 472 B2N_32(vtsi_mat->vts_last_sector); |
473 B2N_32(vtsi_mat->vtsi_last_sector); | |
474 B2N_32(vtsi_mat->vts_category); | |
475 B2N_32(vtsi_mat->vtsi_last_byte); | |
476 B2N_32(vtsi_mat->vtsm_vobs); | |
477 B2N_32(vtsi_mat->vtstt_vobs); | |
478 B2N_32(vtsi_mat->vts_ptt_srpt); | |
479 B2N_32(vtsi_mat->vts_pgcit); | |
480 B2N_32(vtsi_mat->vtsm_pgci_ut); | |
481 B2N_32(vtsi_mat->vts_tmapt); | |
482 B2N_32(vtsi_mat->vtsm_c_adt); | |
483 B2N_32(vtsi_mat->vtsm_vobu_admap); | |
484 B2N_32(vtsi_mat->vts_c_adt); | |
485 B2N_32(vtsi_mat->vts_vobu_admap); | |
486 B2N_16(vtsi_mat->vtsm_subp_attr.lang_code); | |
487 for(i = 0; i < 32; i++) | |
488 B2N_16(vtsi_mat->vts_subp_attr[i].lang_code); | |
489 | |
490 | |
491 CHECK_ZERO(vtsi_mat->zero_1); | |
492 CHECK_ZERO(vtsi_mat->zero_2); | |
493 CHECK_ZERO(vtsi_mat->zero_3); | |
494 CHECK_ZERO(vtsi_mat->zero_4); | |
495 CHECK_ZERO(vtsi_mat->zero_5); | |
496 CHECK_ZERO(vtsi_mat->zero_6); | |
497 CHECK_ZERO(vtsi_mat->zero_7); | |
498 CHECK_ZERO(vtsi_mat->zero_8); | |
499 CHECK_ZERO(vtsi_mat->zero_9); | |
500 CHECK_ZERO(vtsi_mat->zero_10); | |
501 CHECK_ZERO(vtsi_mat->zero_11); | |
502 CHECK_ZERO(vtsi_mat->zero_12); | |
503 CHECK_ZERO(vtsi_mat->zero_13); | |
504 CHECK_ZERO(vtsi_mat->zero_14); | |
505 CHECK_ZERO(vtsi_mat->zero_15); | |
506 CHECK_ZERO(vtsi_mat->zero_16); | |
507 CHECK_ZERO(vtsi_mat->zero_17); | |
508 CHECK_ZERO(vtsi_mat->zero_18); | |
509 CHECK_ZERO(vtsi_mat->zero_19); | |
510 CHECK_ZERO(vtsi_mat->zero_20); | |
511 CHECK_ZERO(vtsi_mat->zero_21); | |
512 CHECK_VALUE(vtsi_mat->vtsi_last_sector*2 <= vtsi_mat->vts_last_sector); | |
513 CHECK_VALUE(vtsi_mat->vtsi_last_byte/DVD_BLOCK_LEN <= vtsi_mat->vtsi_last_sector); | |
514 CHECK_VALUE(vtsi_mat->vtsm_vobs == 0 || | |
515 (vtsi_mat->vtsm_vobs > vtsi_mat->vtsi_last_sector && | |
516 vtsi_mat->vtsm_vobs < vtsi_mat->vts_last_sector)); | |
517 CHECK_VALUE(vtsi_mat->vtstt_vobs == 0 || | |
518 (vtsi_mat->vtstt_vobs > vtsi_mat->vtsi_last_sector && | |
519 vtsi_mat->vtstt_vobs < vtsi_mat->vts_last_sector)); | |
520 CHECK_VALUE(vtsi_mat->vts_ptt_srpt <= vtsi_mat->vtsi_last_sector); | |
521 CHECK_VALUE(vtsi_mat->vts_pgcit <= vtsi_mat->vtsi_last_sector); | |
522 CHECK_VALUE(vtsi_mat->vtsm_pgci_ut <= vtsi_mat->vtsi_last_sector); | |
523 CHECK_VALUE(vtsi_mat->vts_tmapt <= vtsi_mat->vtsi_last_sector); | |
524 CHECK_VALUE(vtsi_mat->vtsm_c_adt <= vtsi_mat->vtsi_last_sector); | |
525 CHECK_VALUE(vtsi_mat->vtsm_vobu_admap <= vtsi_mat->vtsi_last_sector); | |
526 CHECK_VALUE(vtsi_mat->vts_c_adt <= vtsi_mat->vtsi_last_sector); | |
527 CHECK_VALUE(vtsi_mat->vts_vobu_admap <= vtsi_mat->vtsi_last_sector); | |
528 | |
529 CHECK_VALUE(vtsi_mat->nr_of_vtsm_audio_streams <= 1); | |
530 CHECK_VALUE(vtsi_mat->nr_of_vtsm_subp_streams <= 1); | |
531 | |
532 CHECK_VALUE(vtsi_mat->nr_of_vts_audio_streams <= 8); | |
533 for(i = vtsi_mat->nr_of_vts_audio_streams; i < 8; i++) | |
534 CHECK_ZERO(vtsi_mat->vts_audio_attr[i]); | |
535 | |
536 CHECK_VALUE(vtsi_mat->nr_of_vts_subp_streams <= 32); | |
537 for(i = vtsi_mat->nr_of_vts_subp_streams; i < 32; i++) | |
538 CHECK_ZERO(vtsi_mat->vts_subp_attr[i]); | |
539 | |
540 for(i = 0; i < 8; i++) { | |
350
950749fdce4a
read_multichannel_ext() uses getbits() instead of relying on endianness-specific bitfields
nicodvb
parents:
349
diff
changeset
|
541 read_multichannel_ext(&vtsi_mat->vts_mu_audio_attr[i]); |
225 | 542 CHECK_ZERO0(vtsi_mat->vts_mu_audio_attr[i].zero1); |
543 CHECK_ZERO0(vtsi_mat->vts_mu_audio_attr[i].zero2); | |
544 CHECK_ZERO0(vtsi_mat->vts_mu_audio_attr[i].zero3); | |
545 CHECK_ZERO0(vtsi_mat->vts_mu_audio_attr[i].zero4); | |
546 CHECK_ZERO0(vtsi_mat->vts_mu_audio_attr[i].zero5); | |
547 CHECK_ZERO(vtsi_mat->vts_mu_audio_attr[i].zero6); | |
548 } | |
549 | |
550 return 1; | |
551 } | |
552 | |
553 | |
554 static int ifoRead_PGC_COMMAND_TBL(ifo_handle_t *ifofile, | |
555 pgc_command_tbl_t *cmd_tbl, | |
556 unsigned int offset) { | |
557 | |
558 memset(cmd_tbl, 0, sizeof(pgc_command_tbl_t)); | |
559 | |
560 if(!DVDFileSeek_(ifofile->file, offset)) | |
561 return 0; | |
562 | |
563 if(!(DVDReadBytes(ifofile->file, cmd_tbl, PGC_COMMAND_TBL_SIZE))) | |
564 return 0; | |
565 | |
566 B2N_16(cmd_tbl->nr_of_pre); | |
567 B2N_16(cmd_tbl->nr_of_post); | |
568 B2N_16(cmd_tbl->nr_of_cell); | |
569 | |
570 CHECK_VALUE(cmd_tbl->nr_of_pre + cmd_tbl->nr_of_post + cmd_tbl->nr_of_cell<= 255); | |
571 | |
572 if(cmd_tbl->nr_of_pre != 0) { | |
573 unsigned int pre_cmds_size = cmd_tbl->nr_of_pre * COMMAND_DATA_SIZE; | |
574 cmd_tbl->pre_cmds = (vm_cmd_t *)malloc(pre_cmds_size); | |
575 if(!cmd_tbl->pre_cmds) | |
576 return 0; | |
577 | |
578 if(!(DVDReadBytes(ifofile->file, cmd_tbl->pre_cmds, pre_cmds_size))) { | |
579 free(cmd_tbl->pre_cmds); | |
580 return 0; | |
581 } | |
582 } | |
583 | |
584 if(cmd_tbl->nr_of_post != 0) { | |
585 unsigned int post_cmds_size = cmd_tbl->nr_of_post * COMMAND_DATA_SIZE; | |
586 cmd_tbl->post_cmds = (vm_cmd_t *)malloc(post_cmds_size); | |
587 if(!cmd_tbl->post_cmds) { | |
588 if(cmd_tbl->pre_cmds) | |
589 free(cmd_tbl->pre_cmds); | |
590 return 0; | |
591 } | |
592 if(!(DVDReadBytes(ifofile->file, cmd_tbl->post_cmds, post_cmds_size))) { | |
593 if(cmd_tbl->pre_cmds) | |
594 free(cmd_tbl->pre_cmds); | |
595 free(cmd_tbl->post_cmds); | |
596 return 0; | |
597 } | |
598 } | |
599 | |
600 if(cmd_tbl->nr_of_cell != 0) { | |
601 unsigned int cell_cmds_size = cmd_tbl->nr_of_cell * COMMAND_DATA_SIZE; | |
602 cmd_tbl->cell_cmds = (vm_cmd_t *)malloc(cell_cmds_size); | |
603 if(!cmd_tbl->cell_cmds) { | |
604 if(cmd_tbl->pre_cmds) | |
605 free(cmd_tbl->pre_cmds); | |
606 if(cmd_tbl->post_cmds) | |
607 free(cmd_tbl->post_cmds); | |
608 return 0; | |
609 } | |
610 if(!(DVDReadBytes(ifofile->file, cmd_tbl->cell_cmds, cell_cmds_size))) { | |
611 if(cmd_tbl->pre_cmds) | |
612 free(cmd_tbl->pre_cmds); | |
613 if(cmd_tbl->post_cmds) | |
614 free(cmd_tbl->post_cmds); | |
615 free(cmd_tbl->cell_cmds); | |
616 return 0; | |
617 } | |
618 } | |
619 | |
620 /* | |
621 * Make a run over all the commands and see that we can interpret them all? | |
622 */ | |
623 return 1; | |
624 } | |
625 | |
626 | |
627 static void ifoFree_PGC_COMMAND_TBL(pgc_command_tbl_t *cmd_tbl) { | |
628 if(cmd_tbl) { | |
629 if(cmd_tbl->nr_of_pre && cmd_tbl->pre_cmds) | |
630 free(cmd_tbl->pre_cmds); | |
631 if(cmd_tbl->nr_of_post && cmd_tbl->post_cmds) | |
632 free(cmd_tbl->post_cmds); | |
633 if(cmd_tbl->nr_of_cell && cmd_tbl->cell_cmds) | |
634 free(cmd_tbl->cell_cmds); | |
635 free(cmd_tbl); | |
636 } | |
637 } | |
638 | |
639 static int ifoRead_PGC_PROGRAM_MAP(ifo_handle_t *ifofile, | |
640 pgc_program_map_t *program_map, | |
641 unsigned int nr, unsigned int offset) { | |
642 unsigned int size = nr * sizeof(pgc_program_map_t); | |
643 | |
644 if(!DVDFileSeek_(ifofile->file, offset)) | |
645 return 0; | |
646 | |
647 if(!(DVDReadBytes(ifofile->file, program_map, size))) | |
648 return 0; | |
649 | |
650 return 1; | |
651 } | |
652 | |
653 static int ifoRead_CELL_PLAYBACK_TBL(ifo_handle_t *ifofile, | |
654 cell_playback_t *cell_playback, | |
655 unsigned int nr, unsigned int offset) { | |
656 unsigned int i; | |
657 unsigned int size = nr * sizeof(cell_playback_t); | |
658 | |
659 if(!DVDFileSeek_(ifofile->file, offset)) | |
660 return 0; | |
661 | |
662 if(!(DVDReadBytes(ifofile->file, cell_playback, size))) | |
663 return 0; | |
664 | |
665 for(i = 0; i < nr; i++) { | |
666 B2N_32(cell_playback[i].first_sector); | |
667 B2N_32(cell_playback[i].first_ilvu_end_sector); | |
668 B2N_32(cell_playback[i].last_vobu_start_sector); | |
669 B2N_32(cell_playback[i].last_sector); | |
670 | |
671 /* Changed < to <= because this was false in the movie 'Pi'. */ | |
672 CHECK_VALUE(cell_playback[i].last_vobu_start_sector <= | |
673 cell_playback[i].last_sector); | |
674 CHECK_VALUE(cell_playback[i].first_sector <= | |
675 cell_playback[i].last_vobu_start_sector); | |
676 } | |
677 | |
678 return 1; | |
679 } | |
680 | |
681 | |
682 static int ifoRead_CELL_POSITION_TBL(ifo_handle_t *ifofile, | |
683 cell_position_t *cell_position, | |
684 unsigned int nr, unsigned int offset) { | |
685 unsigned int i; | |
686 unsigned int size = nr * sizeof(cell_position_t); | |
687 | |
688 if(!DVDFileSeek_(ifofile->file, offset)) | |
689 return 0; | |
690 | |
691 if(!(DVDReadBytes(ifofile->file, cell_position, size))) | |
692 return 0; | |
693 | |
694 for(i = 0; i < nr; i++) { | |
695 B2N_16(cell_position[i].vob_id_nr); | |
696 CHECK_ZERO(cell_position[i].zero_1); | |
697 } | |
698 | |
699 return 1; | |
700 } | |
701 | |
702 static int ifoRead_PGC(ifo_handle_t *ifofile, pgc_t *pgc, unsigned int offset) { | |
703 unsigned int i; | |
704 | |
705 if(!DVDFileSeek_(ifofile->file, offset)) | |
706 return 0; | |
707 | |
708 if(!(DVDReadBytes(ifofile->file, pgc, PGC_SIZE))) | |
709 return 0; | |
710 | |
711 B2N_16(pgc->next_pgc_nr); | |
712 B2N_16(pgc->prev_pgc_nr); | |
713 B2N_16(pgc->goup_pgc_nr); | |
714 B2N_16(pgc->command_tbl_offset); | |
715 B2N_16(pgc->program_map_offset); | |
716 B2N_16(pgc->cell_playback_offset); | |
717 B2N_16(pgc->cell_position_offset); | |
718 | |
719 for(i = 0; i < 8; i++) | |
720 B2N_16(pgc->audio_control[i]); | |
721 for(i = 0; i < 32; i++) | |
722 B2N_32(pgc->subp_control[i]); | |
723 for(i = 0; i < 16; i++) | |
724 B2N_32(pgc->palette[i]); | |
725 | |
726 CHECK_ZERO(pgc->zero_1); | |
727 CHECK_VALUE(pgc->nr_of_programs <= pgc->nr_of_cells); | |
728 | |
729 /* verify time (look at print_time) */ | |
730 for(i = 0; i < 8; i++) | |
731 if(!pgc->audio_control[i] & 0x8000) /* The 'is present' bit */ | |
732 CHECK_ZERO(pgc->audio_control[i]); | |
733 for(i = 0; i < 32; i++) | |
734 if(!pgc->subp_control[i] & 0x80000000) /* The 'is present' bit */ | |
735 CHECK_ZERO(pgc->subp_control[i]); | |
736 | |
737 /* Check that time is 0:0:0:0 also if nr_of_programs == 0 */ | |
738 if(pgc->nr_of_programs == 0) { | |
739 CHECK_ZERO(pgc->still_time); | |
740 CHECK_ZERO(pgc->pg_playback_mode); /* ?? */ | |
741 CHECK_VALUE(pgc->program_map_offset == 0); | |
742 CHECK_VALUE(pgc->cell_playback_offset == 0); | |
743 CHECK_VALUE(pgc->cell_position_offset == 0); | |
744 } else { | |
745 CHECK_VALUE(pgc->program_map_offset != 0); | |
746 CHECK_VALUE(pgc->cell_playback_offset != 0); | |
747 CHECK_VALUE(pgc->cell_position_offset != 0); | |
748 } | |
749 | |
750 if(pgc->command_tbl_offset != 0) { | |
751 pgc->command_tbl = malloc(sizeof(pgc_command_tbl_t)); | |
752 if(!pgc->command_tbl) | |
753 return 0; | |
754 | |
755 if(!ifoRead_PGC_COMMAND_TBL(ifofile, pgc->command_tbl, | |
756 offset + pgc->command_tbl_offset)) { | |
757 free(pgc->command_tbl); | |
758 return 0; | |
759 } | |
760 } else { | |
761 pgc->command_tbl = NULL; | |
762 } | |
763 | |
255 | 764 if(pgc->program_map_offset != 0 && pgc->nr_of_programs>0) { |
225 | 765 pgc->program_map = malloc(pgc->nr_of_programs * sizeof(pgc_program_map_t)); |
766 if(!pgc->program_map) { | |
767 ifoFree_PGC_COMMAND_TBL(pgc->command_tbl); | |
768 return 0; | |
769 } | |
770 if(!ifoRead_PGC_PROGRAM_MAP(ifofile, pgc->program_map,pgc->nr_of_programs, | |
771 offset + pgc->program_map_offset)) { | |
772 ifoFree_PGC_COMMAND_TBL(pgc->command_tbl); | |
773 free(pgc->program_map); | |
774 return 0; | |
775 } | |
776 } else { | |
777 pgc->program_map = NULL; | |
778 } | |
779 | |
255 | 780 if(pgc->cell_playback_offset != 0 && pgc->nr_of_cells>0) { |
225 | 781 pgc->cell_playback = malloc(pgc->nr_of_cells * sizeof(cell_playback_t)); |
782 if(!pgc->cell_playback) { | |
783 ifoFree_PGC_COMMAND_TBL(pgc->command_tbl); | |
784 if(pgc->program_map) | |
785 free(pgc->program_map); | |
786 return 0; | |
787 } | |
788 if(!ifoRead_CELL_PLAYBACK_TBL(ifofile, pgc->cell_playback, | |
789 pgc->nr_of_cells, | |
790 offset + pgc->cell_playback_offset)) { | |
791 ifoFree_PGC_COMMAND_TBL(pgc->command_tbl); | |
792 if(pgc->program_map) | |
793 free(pgc->program_map); | |
794 free(pgc->cell_playback); | |
795 return 0; | |
796 } | |
797 } else { | |
798 pgc->cell_playback = NULL; | |
799 } | |
800 | |
255 | 801 if(pgc->cell_position_offset != 0 && pgc->nr_of_cells>0) { |
225 | 802 pgc->cell_position = malloc(pgc->nr_of_cells * sizeof(cell_position_t)); |
803 if(!pgc->cell_position) { | |
804 ifoFree_PGC(pgc); | |
805 return 0; | |
806 } | |
807 if(!ifoRead_CELL_POSITION_TBL(ifofile, pgc->cell_position, | |
808 pgc->nr_of_cells, | |
809 offset + pgc->cell_position_offset)) { | |
810 ifoFree_PGC(pgc); | |
811 return 0; | |
812 } | |
813 } else { | |
814 pgc->cell_position = NULL; | |
815 } | |
816 | |
817 return 1; | |
818 } | |
819 | |
820 int ifoRead_FP_PGC(ifo_handle_t *ifofile) { | |
821 | |
822 if(!ifofile) | |
823 return 0; | |
824 | |
825 if(!ifofile->vmgi_mat) | |
826 return 0; | |
827 | |
828 /* It seems that first_play_pgc is optional after all. */ | |
829 ifofile->first_play_pgc = 0; | |
830 if(ifofile->vmgi_mat->first_play_pgc == 0) | |
831 return 1; | |
832 | |
833 ifofile->first_play_pgc = (pgc_t *)malloc(sizeof(pgc_t)); | |
834 if(!ifofile->first_play_pgc) | |
835 return 0; | |
836 | |
837 if(!ifoRead_PGC(ifofile, ifofile->first_play_pgc, | |
838 ifofile->vmgi_mat->first_play_pgc)) { | |
839 free(ifofile->first_play_pgc); | |
840 ifofile->first_play_pgc = 0; | |
841 return 0; | |
842 } | |
843 | |
844 return 1; | |
845 } | |
846 | |
847 static void ifoFree_PGC(pgc_t *pgc) { | |
848 if(pgc) { | |
849 ifoFree_PGC_COMMAND_TBL(pgc->command_tbl); | |
850 if(pgc->program_map) | |
851 free(pgc->program_map); | |
852 if(pgc->cell_playback) | |
853 free(pgc->cell_playback); | |
854 if(pgc->cell_position) | |
855 free(pgc->cell_position); | |
856 } | |
857 } | |
858 | |
859 void ifoFree_FP_PGC(ifo_handle_t *ifofile) { | |
860 if(!ifofile) | |
861 return; | |
862 | |
863 if(ifofile->first_play_pgc) { | |
864 ifoFree_PGC(ifofile->first_play_pgc); | |
865 free(ifofile->first_play_pgc); | |
866 ifofile->first_play_pgc = 0; | |
867 } | |
868 } | |
869 | |
870 | |
871 int ifoRead_TT_SRPT(ifo_handle_t *ifofile) { | |
872 tt_srpt_t *tt_srpt; | |
873 int i, info_length; | |
874 | |
875 if(!ifofile) | |
876 return 0; | |
877 | |
878 if(!ifofile->vmgi_mat) | |
879 return 0; | |
880 | |
881 if(ifofile->vmgi_mat->tt_srpt == 0) /* mandatory */ | |
882 return 0; | |
883 | |
884 if(!DVDFileSeek_(ifofile->file, ifofile->vmgi_mat->tt_srpt * DVD_BLOCK_LEN)) | |
885 return 0; | |
886 | |
887 tt_srpt = (tt_srpt_t *)malloc(sizeof(tt_srpt_t)); | |
888 if(!tt_srpt) | |
889 return 0; | |
890 | |
891 ifofile->tt_srpt = tt_srpt; | |
892 | |
893 if(!(DVDReadBytes(ifofile->file, tt_srpt, TT_SRPT_SIZE))) { | |
894 fprintf(stderr, "libdvdread: Unable to read read TT_SRPT.\n"); | |
895 free(tt_srpt); | |
896 return 0; | |
897 } | |
898 | |
899 B2N_16(tt_srpt->nr_of_srpts); | |
900 B2N_32(tt_srpt->last_byte); | |
901 | |
902 info_length = tt_srpt->last_byte + 1 - TT_SRPT_SIZE; | |
903 | |
904 tt_srpt->title = (title_info_t *)malloc(info_length); | |
905 if(!tt_srpt->title) { | |
906 free(tt_srpt); | |
907 ifofile->tt_srpt = 0; | |
908 return 0; | |
909 } | |
910 if(!(DVDReadBytes(ifofile->file, tt_srpt->title, info_length))) { | |
911 fprintf(stderr, "libdvdread: Unable to read read TT_SRPT.\n"); | |
912 ifoFree_TT_SRPT(ifofile); | |
913 return 0; | |
914 } | |
915 | |
916 for(i = 0; i < tt_srpt->nr_of_srpts; i++) { | |
917 B2N_16(tt_srpt->title[i].nr_of_ptts); | |
918 B2N_16(tt_srpt->title[i].parental_id); | |
919 B2N_32(tt_srpt->title[i].title_set_sector); | |
920 } | |
921 | |
922 | |
923 CHECK_ZERO(tt_srpt->zero_1); | |
924 CHECK_VALUE(tt_srpt->nr_of_srpts != 0); | |
925 CHECK_VALUE(tt_srpt->nr_of_srpts < 100); /* ?? */ | |
926 CHECK_VALUE((int)tt_srpt->nr_of_srpts * sizeof(title_info_t) <= info_length); | |
927 | |
928 for(i = 0; i < tt_srpt->nr_of_srpts; i++) { | |
929 CHECK_VALUE(tt_srpt->title[i].pb_ty.zero_1 == 0); | |
930 CHECK_VALUE(tt_srpt->title[i].nr_of_angles != 0); | |
931 CHECK_VALUE(tt_srpt->title[i].nr_of_angles < 10); | |
932 /* CHECK_VALUE(tt_srpt->title[i].nr_of_ptts != 0); */ | |
933 /* XXX: this assertion breaks Ghostbusters: */ | |
934 CHECK_VALUE(tt_srpt->title[i].nr_of_ptts < 1000); /* ?? */ | |
935 CHECK_VALUE(tt_srpt->title[i].title_set_nr != 0); | |
936 CHECK_VALUE(tt_srpt->title[i].title_set_nr < 100); /* ?? */ | |
937 CHECK_VALUE(tt_srpt->title[i].vts_ttn != 0); | |
938 CHECK_VALUE(tt_srpt->title[i].vts_ttn < 100); /* ?? */ | |
939 /* CHECK_VALUE(tt_srpt->title[i].title_set_sector != 0); */ | |
940 } | |
941 | |
942 /* Make this a function */ | |
943 #if 0 | |
944 if(memcmp((uint8_t *)tt_srpt->title + | |
945 tt_srpt->nr_of_srpts * sizeof(title_info_t), | |
946 my_friendly_zeros, | |
947 info_length - tt_srpt->nr_of_srpts * sizeof(title_info_t))) { | |
948 fprintf(stderr, "VMG_PTT_SRPT slack is != 0, "); | |
949 hexdump((uint8_t *)tt_srpt->title + | |
950 tt_srpt->nr_of_srpts * sizeof(title_info_t), | |
951 info_length - tt_srpt->nr_of_srpts * sizeof(title_info_t)); | |
952 } | |
953 #endif | |
954 | |
955 return 1; | |
956 } | |
957 | |
958 | |
959 void ifoFree_TT_SRPT(ifo_handle_t *ifofile) { | |
960 if(!ifofile) | |
961 return; | |
962 | |
963 if(ifofile->tt_srpt) { | |
964 free(ifofile->tt_srpt->title); | |
965 free(ifofile->tt_srpt); | |
966 ifofile->tt_srpt = 0; | |
967 } | |
968 } | |
969 | |
970 | |
971 int ifoRead_VTS_PTT_SRPT(ifo_handle_t *ifofile) { | |
972 vts_ptt_srpt_t *vts_ptt_srpt; | |
973 int info_length, i, j; | |
974 uint32_t *data; | |
975 | |
976 if(!ifofile) | |
977 return 0; | |
978 | |
979 if(!ifofile->vtsi_mat) | |
980 return 0; | |
981 | |
982 if(ifofile->vtsi_mat->vts_ptt_srpt == 0) /* mandatory */ | |
983 return 0; | |
984 | |
985 if(!DVDFileSeek_(ifofile->file, | |
986 ifofile->vtsi_mat->vts_ptt_srpt * DVD_BLOCK_LEN)) | |
987 return 0; | |
988 | |
989 vts_ptt_srpt = (vts_ptt_srpt_t *)malloc(sizeof(vts_ptt_srpt_t)); | |
990 if(!vts_ptt_srpt) | |
991 return 0; | |
992 | |
993 ifofile->vts_ptt_srpt = vts_ptt_srpt; | |
994 | |
995 if(!(DVDReadBytes(ifofile->file, vts_ptt_srpt, VTS_PTT_SRPT_SIZE))) { | |
996 fprintf(stderr, "libdvdread: Unable to read PTT search table.\n"); | |
997 free(vts_ptt_srpt); | |
998 return 0; | |
999 } | |
1000 | |
1001 B2N_16(vts_ptt_srpt->nr_of_srpts); | |
1002 B2N_32(vts_ptt_srpt->last_byte); | |
1003 | |
1004 CHECK_ZERO(vts_ptt_srpt->zero_1); | |
1005 CHECK_VALUE(vts_ptt_srpt->nr_of_srpts != 0); | |
1006 CHECK_VALUE(vts_ptt_srpt->nr_of_srpts < 100); /* ?? */ | |
1007 | |
1008 info_length = vts_ptt_srpt->last_byte + 1 - VTS_PTT_SRPT_SIZE; | |
1009 | |
1010 data = (uint32_t *)malloc(info_length); | |
1011 if(!data) { | |
1012 free(vts_ptt_srpt); | |
1013 ifofile->vts_ptt_srpt = 0; | |
1014 return 0; | |
1015 } | |
1016 if(!(DVDReadBytes(ifofile->file, data, info_length))) { | |
1017 fprintf(stderr, "libdvdread: Unable to read PTT search table.\n"); | |
1018 free(vts_ptt_srpt); | |
1019 free(data); | |
1020 ifofile->vts_ptt_srpt = 0; | |
1021 return 0; | |
1022 } | |
1023 | |
1024 for(i = 0; i < vts_ptt_srpt->nr_of_srpts; i++) { | |
1025 B2N_32(data[i]); | |
1026 /* assert(data[i] + sizeof(ptt_info_t) <= vts_ptt_srpt->last_byte + 1); | |
1027 Magic Knight Rayearth Daybreak is mastered very strange and has | |
1028 Titles with 0 PTTs. They all have a data[i] offsets beyond the end of | |
1029 of the vts_ptt_srpt structure. */ | |
1030 CHECK_VALUE(data[i] + sizeof(ptt_info_t) <= vts_ptt_srpt->last_byte + 1 + 4); | |
1031 } | |
1032 | |
1033 vts_ptt_srpt->ttu_offset = data; | |
1034 | |
1035 vts_ptt_srpt->title = malloc(vts_ptt_srpt->nr_of_srpts * sizeof(ttu_t)); | |
1036 if(!vts_ptt_srpt->title) { | |
1037 free(vts_ptt_srpt); | |
1038 free(data); | |
1039 ifofile->vts_ptt_srpt = 0; | |
1040 return 0; | |
1041 } | |
1042 for(i = 0; i < vts_ptt_srpt->nr_of_srpts; i++) { | |
1043 int n; | |
1044 if(i < vts_ptt_srpt->nr_of_srpts - 1) | |
1045 n = (data[i+1] - data[i]); | |
1046 else | |
1047 n = (vts_ptt_srpt->last_byte + 1 - data[i]); | |
1048 /* assert(n > 0 && (n % 4) == 0); | |
1049 Magic Knight Rayearth Daybreak is mastered very strange and has | |
1050 Titles with 0 PTTs. */ | |
1051 if(n < 0) n = 0; | |
1052 CHECK_VALUE(n % 4 == 0); | |
1053 | |
1054 vts_ptt_srpt->title[i].nr_of_ptts = n / 4; | |
1055 vts_ptt_srpt->title[i].ptt = malloc(n * sizeof(ptt_info_t)); | |
1056 if(!vts_ptt_srpt->title[i].ptt) { | |
1057 for(n = 0; n < i; n++) | |
1058 free(vts_ptt_srpt->title[n].ptt); | |
1059 free(vts_ptt_srpt); | |
1060 free(data); | |
1061 ifofile->vts_ptt_srpt = 0; | |
1062 return 0; | |
1063 } | |
1064 for(j = 0; j < vts_ptt_srpt->title[i].nr_of_ptts; j++) { | |
1065 /* The assert placed here because of Magic Knight Rayearth Daybreak */ | |
1066 CHECK_VALUE(data[i] + sizeof(ptt_info_t) <= vts_ptt_srpt->last_byte + 1); | |
1067 vts_ptt_srpt->title[i].ptt[j].pgcn | |
1068 = *(uint16_t*)(((char *)data) + data[i] + 4*j - VTS_PTT_SRPT_SIZE); | |
1069 vts_ptt_srpt->title[i].ptt[j].pgn | |
1070 = *(uint16_t*)(((char *)data) + data[i] + 4*j + 2 - VTS_PTT_SRPT_SIZE); | |
1071 } | |
1072 } | |
1073 | |
1074 for(i = 0; i < vts_ptt_srpt->nr_of_srpts; i++) { | |
1075 for(j = 0; j < vts_ptt_srpt->title[i].nr_of_ptts; j++) { | |
1076 B2N_16(vts_ptt_srpt->title[i].ptt[j].pgcn); | |
1077 B2N_16(vts_ptt_srpt->title[i].ptt[j].pgn); | |
1078 } | |
1079 } | |
1080 | |
1081 for(i = 0; i < vts_ptt_srpt->nr_of_srpts; i++) { | |
1082 CHECK_VALUE(vts_ptt_srpt->title[i].nr_of_ptts < 1000); /* ?? */ | |
1083 for(j = 0; j < vts_ptt_srpt->title[i].nr_of_ptts; j++) { | |
1084 CHECK_VALUE(vts_ptt_srpt->title[i].ptt[j].pgcn != 0 ); | |
1085 CHECK_VALUE(vts_ptt_srpt->title[i].ptt[j].pgcn < 1000); /* ?? */ | |
1086 CHECK_VALUE(vts_ptt_srpt->title[i].ptt[j].pgn != 0); | |
1087 CHECK_VALUE(vts_ptt_srpt->title[i].ptt[j].pgn < 100); /* ?? */ | |
1088 } | |
1089 } | |
1090 | |
1091 return 1; | |
1092 } | |
1093 | |
1094 | |
1095 void ifoFree_VTS_PTT_SRPT(ifo_handle_t *ifofile) { | |
1096 if(!ifofile) | |
1097 return; | |
1098 | |
1099 if(ifofile->vts_ptt_srpt) { | |
1100 int i; | |
1101 for(i = 0; i < ifofile->vts_ptt_srpt->nr_of_srpts; i++) | |
1102 free(ifofile->vts_ptt_srpt->title[i].ptt); | |
1103 free(ifofile->vts_ptt_srpt->ttu_offset); | |
1104 free(ifofile->vts_ptt_srpt->title); | |
1105 free(ifofile->vts_ptt_srpt); | |
1106 ifofile->vts_ptt_srpt = 0; | |
1107 } | |
1108 } | |
1109 | |
1110 | |
1111 int ifoRead_PTL_MAIT(ifo_handle_t *ifofile) { | |
1112 ptl_mait_t *ptl_mait; | |
1113 int info_length; | |
1114 unsigned int i, j; | |
1115 | |
1116 if(!ifofile) | |
1117 return 0; | |
1118 | |
1119 if(!ifofile->vmgi_mat) | |
1120 return 0; | |
1121 | |
1122 if(ifofile->vmgi_mat->ptl_mait == 0) | |
1123 return 1; | |
1124 | |
1125 if(!DVDFileSeek_(ifofile->file, ifofile->vmgi_mat->ptl_mait * DVD_BLOCK_LEN)) | |
1126 return 0; | |
1127 | |
1128 ptl_mait = (ptl_mait_t *)malloc(sizeof(ptl_mait_t)); | |
1129 if(!ptl_mait) | |
1130 return 0; | |
1131 | |
1132 ifofile->ptl_mait = ptl_mait; | |
1133 | |
1134 if(!(DVDReadBytes(ifofile->file, ptl_mait, PTL_MAIT_SIZE))) { | |
1135 free(ptl_mait); | |
1136 ifofile->ptl_mait = 0; | |
1137 return 0; | |
1138 } | |
1139 | |
1140 B2N_16(ptl_mait->nr_of_countries); | |
1141 B2N_16(ptl_mait->nr_of_vtss); | |
1142 B2N_32(ptl_mait->last_byte); | |
1143 | |
1144 CHECK_VALUE(ptl_mait->nr_of_countries != 0); | |
1145 CHECK_VALUE(ptl_mait->nr_of_countries < 100); /* ?? */ | |
1146 CHECK_VALUE(ptl_mait->nr_of_vtss != 0); | |
1147 CHECK_VALUE(ptl_mait->nr_of_vtss < 100); /* ?? */ | |
1148 CHECK_VALUE(ptl_mait->nr_of_countries * PTL_MAIT_COUNTRY_SIZE | |
1149 <= ptl_mait->last_byte + 1 - PTL_MAIT_SIZE); | |
1150 | |
1151 info_length = ptl_mait->nr_of_countries * sizeof(ptl_mait_country_t); | |
1152 ptl_mait->countries = (ptl_mait_country_t *)malloc(info_length); | |
1153 if(!ptl_mait->countries) { | |
1154 free(ptl_mait); | |
1155 ifofile->ptl_mait = 0; | |
1156 return 0; | |
1157 } | |
1158 | |
1159 for(i = 0; i < ptl_mait->nr_of_countries; i++) { | |
1160 if(!(DVDReadBytes(ifofile->file, &ptl_mait->countries[i], PTL_MAIT_COUNTRY_SIZE))) { | |
1161 fprintf(stderr, "libdvdread: Unable to read PTL_MAIT.\n"); | |
1162 free(ptl_mait->countries); | |
1163 free(ptl_mait); | |
1164 ifofile->ptl_mait = 0; | |
1165 return 0; | |
1166 } | |
1167 } | |
1168 | |
1169 for(i = 0; i < ptl_mait->nr_of_countries; i++) { | |
1170 B2N_16(ptl_mait->countries[i].country_code); | |
1171 B2N_16(ptl_mait->countries[i].pf_ptl_mai_start_byte); | |
1172 } | |
1173 | |
1174 for(i = 0; i < ptl_mait->nr_of_countries; i++) { | |
1175 CHECK_ZERO(ptl_mait->countries[i].zero_1); | |
1176 CHECK_ZERO(ptl_mait->countries[i].zero_2); | |
1177 CHECK_VALUE(ptl_mait->countries[i].pf_ptl_mai_start_byte | |
1178 + 8*2 * (ptl_mait->nr_of_vtss + 1) <= ptl_mait->last_byte + 1); | |
1179 } | |
1180 | |
1181 for(i = 0; i < ptl_mait->nr_of_countries; i++) { | |
1182 uint16_t *pf_temp; | |
1183 | |
1184 if(!DVDFileSeek_(ifofile->file, | |
1185 ifofile->vmgi_mat->ptl_mait * DVD_BLOCK_LEN | |
1186 + ptl_mait->countries[i].pf_ptl_mai_start_byte)) { | |
1187 fprintf(stderr, "libdvdread: Unable to seak PTL_MAIT table.\n"); | |
1188 free(ptl_mait->countries); | |
1189 free(ptl_mait); | |
1190 return 0; | |
1191 } | |
1192 info_length = (ptl_mait->nr_of_vtss + 1) * sizeof(pf_level_t); | |
1193 pf_temp = (uint16_t *)malloc(info_length); | |
1194 if(!pf_temp) { | |
1195 for(j = 0; j < i ; j++) { | |
1196 free(ptl_mait->countries[j].pf_ptl_mai); | |
1197 } | |
1198 free(ptl_mait->countries); | |
1199 free(ptl_mait); | |
1200 return 0; | |
1201 } | |
1202 if(!(DVDReadBytes(ifofile->file, pf_temp, info_length))) { | |
1203 fprintf(stderr, "libdvdread: Unable to read PTL_MAIT table.\n"); | |
1204 free(pf_temp); | |
1205 for(j = 0; j < i ; j++) { | |
1206 free(ptl_mait->countries[j].pf_ptl_mai); | |
1207 } | |
1208 free(ptl_mait->countries); | |
1209 free(ptl_mait); | |
1210 return 0; | |
1211 } | |
1212 for (j = 0; j < ((ptl_mait->nr_of_vtss + 1) * 8); j++) { | |
1213 B2N_16(pf_temp[j]); | |
1214 } | |
1215 ptl_mait->countries[i].pf_ptl_mai = (pf_level_t *)malloc(info_length); | |
1216 if(!ptl_mait->countries[i].pf_ptl_mai) { | |
1217 free(pf_temp); | |
1218 for(j = 0; j < i ; j++) { | |
1219 free(ptl_mait->countries[j].pf_ptl_mai); | |
1220 } | |
1221 free(ptl_mait->countries); | |
1222 free(ptl_mait); | |
1223 return 0; | |
1224 } | |
1225 { /* Transpose the array so we can use C indexing. */ | |
1226 int level, vts; | |
1227 for(level = 0; level < 8; level++) { | |
1228 for(vts = 0; vts <= ptl_mait->nr_of_vtss; vts++) { | |
1229 ptl_mait->countries[i].pf_ptl_mai[vts][level] = | |
1230 pf_temp[(7-level)*(ptl_mait->nr_of_vtss+1) + vts]; | |
1231 } | |
1232 } | |
1233 free(pf_temp); | |
1234 } | |
1235 } | |
1236 return 1; | |
1237 } | |
1238 | |
1239 void ifoFree_PTL_MAIT(ifo_handle_t *ifofile) { | |
1240 unsigned int i; | |
1241 | |
1242 if(!ifofile) | |
1243 return; | |
1244 | |
1245 if(ifofile->ptl_mait) { | |
1246 for(i = 0; i < ifofile->ptl_mait->nr_of_countries; i++) { | |
1247 free(ifofile->ptl_mait->countries[i].pf_ptl_mai); | |
1248 } | |
1249 free(ifofile->ptl_mait->countries); | |
1250 free(ifofile->ptl_mait); | |
1251 ifofile->ptl_mait = 0; | |
1252 } | |
1253 } | |
1254 | |
1255 int ifoRead_VTS_TMAPT(ifo_handle_t *ifofile) { | |
1256 vts_tmapt_t *vts_tmapt; | |
1257 uint32_t *vts_tmap_srp; | |
1258 unsigned int offset; | |
1259 int info_length; | |
1260 unsigned int i, j; | |
1261 | |
1262 if(!ifofile) | |
1263 return 0; | |
1264 | |
1265 if(!ifofile->vtsi_mat) | |
1266 return 0; | |
1267 | |
1268 if(ifofile->vtsi_mat->vts_tmapt == 0) { /* optional(?) */ | |
1269 ifofile->vts_tmapt = NULL; | |
1270 fprintf(stderr,"Please send bug report - no VTS_TMAPT ?? \n"); | |
1271 return 1; | |
1272 } | |
1273 | |
1274 offset = ifofile->vtsi_mat->vts_tmapt * DVD_BLOCK_LEN; | |
1275 | |
1276 if(!DVDFileSeek_(ifofile->file, offset)) | |
1277 return 0; | |
1278 | |
1279 vts_tmapt = (vts_tmapt_t *)malloc(sizeof(vts_tmapt_t)); | |
1280 if(!vts_tmapt) | |
1281 return 0; | |
1282 | |
1283 ifofile->vts_tmapt = vts_tmapt; | |
1284 | |
1285 if(!(DVDReadBytes(ifofile->file, vts_tmapt, VTS_TMAPT_SIZE))) { | |
1286 fprintf(stderr, "libdvdread: Unable to read VTS_TMAPT.\n"); | |
1287 free(vts_tmapt); | |
1288 ifofile->vts_tmapt = NULL; | |
1289 return 0; | |
1290 } | |
1291 | |
1292 B2N_16(vts_tmapt->nr_of_tmaps); | |
1293 B2N_32(vts_tmapt->last_byte); | |
1294 | |
1295 CHECK_ZERO(vts_tmapt->zero_1); | |
1296 | |
1297 info_length = vts_tmapt->nr_of_tmaps * 4; | |
1298 | |
1299 vts_tmap_srp = (uint32_t *)malloc(info_length); | |
1300 if(!vts_tmap_srp) { | |
1301 free(vts_tmapt); | |
1302 ifofile->vts_tmapt = NULL; | |
1303 return 0; | |
1304 } | |
1305 | |
1306 vts_tmapt->tmap_offset = vts_tmap_srp; | |
1307 | |
1308 if(!(DVDReadBytes(ifofile->file, vts_tmap_srp, info_length))) { | |
1309 fprintf(stderr, "libdvdread: Unable to read VTS_TMAPT.\n"); | |
1310 free(vts_tmap_srp); | |
1311 free(vts_tmapt); | |
1312 ifofile->vts_tmapt = NULL; | |
1313 return 0; | |
1314 } | |
1315 | |
1316 for (i = 0; i < vts_tmapt->nr_of_tmaps; i++) { | |
1317 B2N_32(vts_tmap_srp[i]); | |
1318 } | |
1319 | |
1320 | |
1321 info_length = vts_tmapt->nr_of_tmaps * sizeof(vts_tmap_t); | |
1322 | |
1323 vts_tmapt->tmap = (vts_tmap_t *)malloc(info_length); | |
1324 if(!vts_tmapt->tmap) { | |
1325 free(vts_tmap_srp); | |
1326 free(vts_tmapt); | |
1327 ifofile->vts_tmapt = NULL; | |
1328 return 0; | |
1329 } | |
1330 | |
1331 memset(vts_tmapt->tmap, 0, info_length); /* So ifoFree_VTS_TMAPT works. */ | |
1332 | |
1333 for(i = 0; i < vts_tmapt->nr_of_tmaps; i++) { | |
1334 if(!DVDFileSeek_(ifofile->file, offset + vts_tmap_srp[i])) { | |
1335 ifoFree_VTS_TMAPT(ifofile); | |
1336 return 0; | |
1337 } | |
1338 | |
1339 if(!(DVDReadBytes(ifofile->file, &vts_tmapt->tmap[i], VTS_TMAP_SIZE))) { | |
1340 fprintf(stderr, "libdvdread: Unable to read VTS_TMAP.\n"); | |
1341 ifoFree_VTS_TMAPT(ifofile); | |
1342 return 0; | |
1343 } | |
1344 | |
1345 B2N_16(vts_tmapt->tmap[i].nr_of_entries); | |
1346 CHECK_ZERO(vts_tmapt->tmap[i].zero_1); | |
1347 | |
1348 if(vts_tmapt->tmap[i].nr_of_entries == 0) { /* Early out if zero entries */ | |
1349 vts_tmapt->tmap[i].map_ent = NULL; | |
1350 continue; | |
1351 } | |
1352 | |
1353 info_length = vts_tmapt->tmap[i].nr_of_entries * sizeof(map_ent_t); | |
1354 | |
1355 vts_tmapt->tmap[i].map_ent = (map_ent_t *)malloc(info_length); | |
1356 if(!vts_tmapt->tmap[i].map_ent) { | |
1357 ifoFree_VTS_TMAPT(ifofile); | |
1358 return 0; | |
1359 } | |
1360 | |
1361 if(!(DVDReadBytes(ifofile->file, vts_tmapt->tmap[i].map_ent, info_length))) { | |
1362 fprintf(stderr, "libdvdread: Unable to read VTS_TMAP_ENT.\n"); | |
1363 ifoFree_VTS_TMAPT(ifofile); | |
1364 return 0; | |
1365 } | |
1366 | |
1367 for(j = 0; j < vts_tmapt->tmap[i].nr_of_entries; j++) | |
1368 B2N_32(vts_tmapt->tmap[i].map_ent[j]); | |
1369 } | |
1370 | |
1371 return 1; | |
1372 } | |
1373 | |
1374 void ifoFree_VTS_TMAPT(ifo_handle_t *ifofile) { | |
1375 unsigned int i; | |
1376 | |
1377 if(!ifofile) | |
1378 return; | |
1379 | |
1380 if(ifofile->vts_tmapt) { | |
1381 for(i = 0; i < ifofile->vts_tmapt->nr_of_tmaps; i++) | |
1382 if(ifofile->vts_tmapt->tmap[i].map_ent) | |
1383 free(ifofile->vts_tmapt->tmap[i].map_ent); | |
1384 free(ifofile->vts_tmapt->tmap); | |
1385 free(ifofile->vts_tmapt->tmap_offset); | |
1386 free(ifofile->vts_tmapt); | |
1387 ifofile->vts_tmapt = NULL; | |
1388 } | |
1389 } | |
1390 | |
1391 | |
1392 int ifoRead_TITLE_C_ADT(ifo_handle_t *ifofile) { | |
1393 | |
1394 if(!ifofile) | |
1395 return 0; | |
1396 | |
1397 if(!ifofile->vtsi_mat) | |
1398 return 0; | |
1399 | |
1400 if(ifofile->vtsi_mat->vts_c_adt == 0) /* mandatory */ | |
1401 return 0; | |
1402 | |
1403 ifofile->vts_c_adt = (c_adt_t *)malloc(sizeof(c_adt_t)); | |
1404 if(!ifofile->vts_c_adt) | |
1405 return 0; | |
1406 | |
1407 if(!ifoRead_C_ADT_internal(ifofile, ifofile->vts_c_adt, | |
1408 ifofile->vtsi_mat->vts_c_adt)) { | |
1409 free(ifofile->vts_c_adt); | |
1410 ifofile->vts_c_adt = 0; | |
1411 return 0; | |
1412 } | |
1413 | |
1414 return 1; | |
1415 } | |
1416 | |
1417 int ifoRead_C_ADT(ifo_handle_t *ifofile) { | |
1418 unsigned int sector; | |
1419 | |
1420 if(!ifofile) | |
1421 return 0; | |
1422 | |
1423 if(ifofile->vmgi_mat) { | |
1424 if(ifofile->vmgi_mat->vmgm_c_adt == 0) | |
1425 return 1; | |
1426 sector = ifofile->vmgi_mat->vmgm_c_adt; | |
1427 } else if(ifofile->vtsi_mat) { | |
1428 if(ifofile->vtsi_mat->vtsm_c_adt == 0) | |
1429 return 1; | |
1430 sector = ifofile->vtsi_mat->vtsm_c_adt; | |
1431 } else { | |
1432 return 0; | |
1433 } | |
1434 | |
1435 ifofile->menu_c_adt = (c_adt_t *)malloc(sizeof(c_adt_t)); | |
1436 if(!ifofile->menu_c_adt) | |
1437 return 0; | |
1438 | |
1439 if(!ifoRead_C_ADT_internal(ifofile, ifofile->menu_c_adt, sector)) { | |
1440 free(ifofile->menu_c_adt); | |
1441 ifofile->menu_c_adt = 0; | |
1442 return 0; | |
1443 } | |
1444 | |
1445 return 1; | |
1446 } | |
1447 | |
1448 static int ifoRead_C_ADT_internal(ifo_handle_t *ifofile, | |
1449 c_adt_t *c_adt, unsigned int sector) { | |
1450 int i, info_length; | |
1451 | |
1452 if(!DVDFileSeek_(ifofile->file, sector * DVD_BLOCK_LEN)) | |
1453 return 0; | |
1454 | |
1455 if(!(DVDReadBytes(ifofile->file, c_adt, C_ADT_SIZE))) | |
1456 return 0; | |
1457 | |
1458 B2N_16(c_adt->nr_of_vobs); | |
1459 B2N_32(c_adt->last_byte); | |
1460 | |
1461 info_length = c_adt->last_byte + 1 - C_ADT_SIZE; | |
1462 | |
1463 CHECK_ZERO(c_adt->zero_1); | |
1464 /* assert(c_adt->nr_of_vobs > 0); | |
1465 Magic Knight Rayearth Daybreak is mastered very strange and has | |
1466 Titles with a VOBS that has no cells. */ | |
1467 CHECK_VALUE(info_length % sizeof(cell_adr_t) == 0); | |
1468 | |
1469 /* assert(info_length / sizeof(cell_adr_t) >= c_adt->nr_of_vobs); | |
1470 Enemy of the State region 2 (de) has Titles where nr_of_vobs field | |
1471 is to high, they high ones are never referenced though. */ | |
1472 if(info_length / sizeof(cell_adr_t) < c_adt->nr_of_vobs) { | |
1473 fprintf(stderr, "libdvdread: *C_ADT nr_of_vobs > avaiable info entries\n"); | |
1474 c_adt->nr_of_vobs = info_length / sizeof(cell_adr_t); | |
1475 } | |
1476 | |
1477 c_adt->cell_adr_table = (cell_adr_t *)malloc(info_length); | |
1478 if(!c_adt->cell_adr_table) | |
1479 return 0; | |
1480 | |
1481 if(info_length && | |
1482 !(DVDReadBytes(ifofile->file, c_adt->cell_adr_table, info_length))) { | |
1483 free(c_adt->cell_adr_table); | |
1484 return 0; | |
1485 } | |
1486 | |
1487 for(i = 0; i < info_length/sizeof(cell_adr_t); i++) { | |
1488 B2N_16(c_adt->cell_adr_table[i].vob_id); | |
1489 B2N_32(c_adt->cell_adr_table[i].start_sector); | |
1490 B2N_32(c_adt->cell_adr_table[i].last_sector); | |
1491 | |
1492 CHECK_ZERO(c_adt->cell_adr_table[i].zero_1); | |
1493 CHECK_VALUE(c_adt->cell_adr_table[i].vob_id > 0); | |
1494 CHECK_VALUE(c_adt->cell_adr_table[i].vob_id <= c_adt->nr_of_vobs); | |
1495 CHECK_VALUE(c_adt->cell_adr_table[i].cell_id > 0); | |
1496 CHECK_VALUE(c_adt->cell_adr_table[i].start_sector < | |
1497 c_adt->cell_adr_table[i].last_sector); | |
1498 } | |
1499 | |
1500 return 1; | |
1501 } | |
1502 | |
1503 | |
1504 static void ifoFree_C_ADT_internal(c_adt_t *c_adt) { | |
1505 if(c_adt) { | |
1506 free(c_adt->cell_adr_table); | |
1507 free(c_adt); | |
1508 } | |
1509 } | |
1510 | |
1511 void ifoFree_C_ADT(ifo_handle_t *ifofile) { | |
1512 if(!ifofile) | |
1513 return; | |
1514 | |
1515 ifoFree_C_ADT_internal(ifofile->menu_c_adt); | |
1516 ifofile->menu_c_adt = 0; | |
1517 } | |
1518 | |
1519 void ifoFree_TITLE_C_ADT(ifo_handle_t *ifofile) { | |
1520 if(!ifofile) | |
1521 return; | |
1522 | |
1523 ifoFree_C_ADT_internal(ifofile->vts_c_adt); | |
1524 ifofile->vts_c_adt = 0; | |
1525 } | |
1526 | |
1527 int ifoRead_TITLE_VOBU_ADMAP(ifo_handle_t *ifofile) { | |
1528 if(!ifofile) | |
1529 return 0; | |
1530 | |
1531 if(!ifofile->vtsi_mat) | |
1532 return 0; | |
1533 | |
1534 if(ifofile->vtsi_mat->vts_vobu_admap == 0) /* mandatory */ | |
1535 return 0; | |
1536 | |
1537 ifofile->vts_vobu_admap = (vobu_admap_t *)malloc(sizeof(vobu_admap_t)); | |
1538 if(!ifofile->vts_vobu_admap) | |
1539 return 0; | |
1540 | |
1541 if(!ifoRead_VOBU_ADMAP_internal(ifofile, ifofile->vts_vobu_admap, | |
1542 ifofile->vtsi_mat->vts_vobu_admap)) { | |
1543 free(ifofile->vts_vobu_admap); | |
1544 ifofile->vts_vobu_admap = 0; | |
1545 return 0; | |
1546 } | |
1547 | |
1548 return 1; | |
1549 } | |
1550 | |
1551 int ifoRead_VOBU_ADMAP(ifo_handle_t *ifofile) { | |
1552 unsigned int sector; | |
1553 | |
1554 if(!ifofile) | |
1555 return 0; | |
1556 | |
1557 if(ifofile->vmgi_mat) { | |
1558 if(ifofile->vmgi_mat->vmgm_vobu_admap == 0) | |
1559 return 1; | |
1560 sector = ifofile->vmgi_mat->vmgm_vobu_admap; | |
1561 } else if(ifofile->vtsi_mat) { | |
1562 if(ifofile->vtsi_mat->vtsm_vobu_admap == 0) | |
1563 return 1; | |
1564 sector = ifofile->vtsi_mat->vtsm_vobu_admap; | |
1565 } else { | |
1566 return 0; | |
1567 } | |
1568 | |
1569 ifofile->menu_vobu_admap = (vobu_admap_t *)malloc(sizeof(vobu_admap_t)); | |
1570 if(!ifofile->menu_vobu_admap) | |
1571 return 0; | |
1572 | |
1573 if(!ifoRead_VOBU_ADMAP_internal(ifofile, ifofile->menu_vobu_admap, sector)) { | |
1574 free(ifofile->menu_vobu_admap); | |
1575 ifofile->menu_vobu_admap = 0; | |
1576 return 0; | |
1577 } | |
1578 | |
1579 return 1; | |
1580 } | |
1581 | |
1582 static int ifoRead_VOBU_ADMAP_internal(ifo_handle_t *ifofile, | |
1583 vobu_admap_t *vobu_admap, | |
1584 unsigned int sector) { | |
1585 unsigned int i; | |
1586 int info_length; | |
1587 | |
1588 if(!DVDFileSeek_(ifofile->file, sector * DVD_BLOCK_LEN)) | |
1589 return 0; | |
1590 | |
1591 if(!(DVDReadBytes(ifofile->file, vobu_admap, VOBU_ADMAP_SIZE))) | |
1592 return 0; | |
1593 | |
1594 B2N_32(vobu_admap->last_byte); | |
1595 | |
1596 info_length = vobu_admap->last_byte + 1 - VOBU_ADMAP_SIZE; | |
1597 /* assert(info_length > 0); | |
1598 Magic Knight Rayearth Daybreak is mastered very strange and has | |
1599 Titles with a VOBS that has no VOBUs. */ | |
1600 CHECK_VALUE(info_length % sizeof(uint32_t) == 0); | |
1601 | |
1602 vobu_admap->vobu_start_sectors = (uint32_t *)malloc(info_length); | |
1603 if(!vobu_admap->vobu_start_sectors) { | |
1604 return 0; | |
1605 } | |
1606 if(info_length && | |
1607 !(DVDReadBytes(ifofile->file, | |
1608 vobu_admap->vobu_start_sectors, info_length))) { | |
1609 free(vobu_admap->vobu_start_sectors); | |
1610 return 0; | |
1611 } | |
1612 | |
1613 for(i = 0; i < info_length/sizeof(uint32_t); i++) | |
1614 B2N_32(vobu_admap->vobu_start_sectors[i]); | |
1615 | |
1616 return 1; | |
1617 } | |
1618 | |
1619 | |
1620 static void ifoFree_VOBU_ADMAP_internal(vobu_admap_t *vobu_admap) { | |
1621 if(vobu_admap) { | |
1622 free(vobu_admap->vobu_start_sectors); | |
1623 free(vobu_admap); | |
1624 } | |
1625 } | |
1626 | |
1627 void ifoFree_VOBU_ADMAP(ifo_handle_t *ifofile) { | |
1628 if(!ifofile) | |
1629 return; | |
1630 | |
1631 ifoFree_VOBU_ADMAP_internal(ifofile->menu_vobu_admap); | |
1632 ifofile->menu_vobu_admap = 0; | |
1633 } | |
1634 | |
1635 void ifoFree_TITLE_VOBU_ADMAP(ifo_handle_t *ifofile) { | |
1636 if(!ifofile) | |
1637 return; | |
1638 | |
1639 ifoFree_VOBU_ADMAP_internal(ifofile->vts_vobu_admap); | |
1640 ifofile->vts_vobu_admap = 0; | |
1641 } | |
1642 | |
1643 int ifoRead_PGCIT(ifo_handle_t *ifofile) { | |
1644 | |
1645 if(!ifofile) | |
1646 return 0; | |
1647 | |
1648 if(!ifofile->vtsi_mat) | |
1649 return 0; | |
1650 | |
1651 if(ifofile->vtsi_mat->vts_pgcit == 0) /* mandatory */ | |
1652 return 0; | |
1653 | |
1654 ifofile->vts_pgcit = (pgcit_t *)malloc(sizeof(pgcit_t)); | |
1655 if(!ifofile->vts_pgcit) | |
1656 return 0; | |
1657 | |
1658 if(!ifoRead_PGCIT_internal(ifofile, ifofile->vts_pgcit, | |
1659 ifofile->vtsi_mat->vts_pgcit * DVD_BLOCK_LEN)) { | |
1660 free(ifofile->vts_pgcit); | |
1661 ifofile->vts_pgcit = 0; | |
1662 return 0; | |
1663 } | |
1664 | |
1665 return 1; | |
1666 } | |
1667 | |
1668 static int ifoRead_PGCIT_internal(ifo_handle_t *ifofile, pgcit_t *pgcit, | |
1669 unsigned int offset) { | |
1670 int i, info_length; | |
1671 uint8_t *data, *ptr; | |
1672 | |
1673 if(!DVDFileSeek_(ifofile->file, offset)) | |
1674 return 0; | |
1675 | |
1676 if(!(DVDReadBytes(ifofile->file, pgcit, PGCIT_SIZE))) | |
1677 return 0; | |
1678 | |
1679 B2N_16(pgcit->nr_of_pgci_srp); | |
1680 B2N_32(pgcit->last_byte); | |
1681 | |
1682 CHECK_ZERO(pgcit->zero_1); | |
1683 /* assert(pgcit->nr_of_pgci_srp != 0); | |
1684 Magic Knight Rayearth Daybreak is mastered very strange and has | |
1685 Titles with 0 PTTs. */ | |
1686 CHECK_VALUE(pgcit->nr_of_pgci_srp < 10000); /* ?? seen max of 1338 */ | |
1687 | |
1688 info_length = pgcit->nr_of_pgci_srp * PGCI_SRP_SIZE; | |
1689 data = malloc(info_length); | |
1690 if(!data) | |
1691 return 0; | |
1692 | |
1693 if(info_length && !(DVDReadBytes(ifofile->file, data, info_length))) { | |
1694 free(data); | |
1695 return 0; | |
1696 } | |
1697 | |
1698 pgcit->pgci_srp = malloc(pgcit->nr_of_pgci_srp * sizeof(pgci_srp_t)); | |
1699 if(!pgcit->pgci_srp) { | |
1700 free(data); | |
1701 return 0; | |
1702 } | |
1703 ptr = data; | |
1704 for(i = 0; i < pgcit->nr_of_pgci_srp; i++) { | |
241
12cff2e56f55
typo fix from libdvdread, found by Yi-Shin Li, patch seen on Ogle-devel by Daniel
mroi
parents:
225
diff
changeset
|
1705 memcpy(&pgcit->pgci_srp[i], ptr, PGCI_SRP_SIZE); |
12cff2e56f55
typo fix from libdvdread, found by Yi-Shin Li, patch seen on Ogle-devel by Daniel
mroi
parents:
225
diff
changeset
|
1706 ptr += PGCI_SRP_SIZE; |
225 | 1707 B2N_16(pgcit->pgci_srp[i].ptl_id_mask); |
1708 B2N_32(pgcit->pgci_srp[i].pgc_start_byte); | |
1709 CHECK_VALUE(pgcit->pgci_srp[i].unknown1 == 0); | |
1710 } | |
1711 free(data); | |
1712 | |
1713 for(i = 0; i < pgcit->nr_of_pgci_srp; i++) | |
1714 CHECK_VALUE(pgcit->pgci_srp[i].pgc_start_byte + PGC_SIZE <= pgcit->last_byte+1); | |
1715 | |
1716 for(i = 0; i < pgcit->nr_of_pgci_srp; i++) { | |
1717 pgcit->pgci_srp[i].pgc = malloc(sizeof(pgc_t)); | |
1718 if(!pgcit->pgci_srp[i].pgc) { | |
1719 int j; | |
1720 for(j = 0; j < i; j++) { | |
1721 ifoFree_PGC(pgcit->pgci_srp[j].pgc); | |
1722 free(pgcit->pgci_srp[j].pgc); | |
1723 } | |
322 | 1724 goto fail; |
225 | 1725 } |
1726 if(!ifoRead_PGC(ifofile, pgcit->pgci_srp[i].pgc, | |
1727 offset + pgcit->pgci_srp[i].pgc_start_byte)) { | |
1728 int j; | |
1729 for(j = 0; j < i; j++) { | |
1730 ifoFree_PGC(pgcit->pgci_srp[j].pgc); | |
1731 free(pgcit->pgci_srp[j].pgc); | |
1732 } | |
322 | 1733 goto fail; |
225 | 1734 } |
1735 } | |
1736 | |
1737 return 1; | |
322 | 1738 fail: |
1739 free(pgcit->pgci_srp); | |
1740 pgcit->pgci_srp = NULL; | |
1741 return 0; | |
225 | 1742 } |
1743 | |
1744 static void ifoFree_PGCIT_internal(pgcit_t *pgcit) { | |
1745 if(pgcit) { | |
1746 int i; | |
1747 for(i = 0; i < pgcit->nr_of_pgci_srp; i++) | |
1748 ifoFree_PGC(pgcit->pgci_srp[i].pgc); | |
1749 free(pgcit->pgci_srp); | |
1750 } | |
1751 } | |
1752 | |
1753 void ifoFree_PGCIT(ifo_handle_t *ifofile) { | |
1754 if(!ifofile) | |
1755 return; | |
1756 | |
1757 if(ifofile->vts_pgcit) { | |
1758 ifoFree_PGCIT_internal(ifofile->vts_pgcit); | |
1759 free(ifofile->vts_pgcit); | |
1760 ifofile->vts_pgcit = 0; | |
1761 } | |
1762 } | |
1763 | |
1764 | |
1765 int ifoRead_PGCI_UT(ifo_handle_t *ifofile) { | |
1766 pgci_ut_t *pgci_ut; | |
1767 unsigned int sector; | |
1768 unsigned int i; | |
1769 int info_length; | |
1770 uint8_t *data, *ptr; | |
1771 | |
1772 if(!ifofile) | |
1773 return 0; | |
1774 | |
1775 if(ifofile->vmgi_mat) { | |
1776 if(ifofile->vmgi_mat->vmgm_pgci_ut == 0) | |
1777 return 1; | |
1778 sector = ifofile->vmgi_mat->vmgm_pgci_ut; | |
1779 } else if(ifofile->vtsi_mat) { | |
1780 if(ifofile->vtsi_mat->vtsm_pgci_ut == 0) | |
1781 return 1; | |
1782 sector = ifofile->vtsi_mat->vtsm_pgci_ut; | |
1783 } else { | |
1784 return 0; | |
1785 } | |
1786 | |
1787 ifofile->pgci_ut = (pgci_ut_t *)malloc(sizeof(pgci_ut_t)); | |
1788 if(!ifofile->pgci_ut) | |
1789 return 0; | |
1790 | |
1791 if(!DVDFileSeek_(ifofile->file, sector * DVD_BLOCK_LEN)) { | |
1792 free(ifofile->pgci_ut); | |
1793 ifofile->pgci_ut = 0; | |
1794 return 0; | |
1795 } | |
1796 | |
1797 if(!(DVDReadBytes(ifofile->file, ifofile->pgci_ut, PGCI_UT_SIZE))) { | |
1798 free(ifofile->pgci_ut); | |
1799 ifofile->pgci_ut = 0; | |
1800 return 0; | |
1801 } | |
1802 | |
1803 pgci_ut = ifofile->pgci_ut; | |
1804 | |
1805 B2N_16(pgci_ut->nr_of_lus); | |
1806 B2N_32(pgci_ut->last_byte); | |
1807 | |
1808 CHECK_ZERO(pgci_ut->zero_1); | |
1809 CHECK_VALUE(pgci_ut->nr_of_lus != 0); | |
1810 CHECK_VALUE(pgci_ut->nr_of_lus < 100); /* ?? 3-4 ? */ | |
1811 CHECK_VALUE((uint32_t)pgci_ut->nr_of_lus * PGCI_LU_SIZE < pgci_ut->last_byte); | |
1812 | |
1813 info_length = pgci_ut->nr_of_lus * PGCI_LU_SIZE; | |
1814 data = malloc(info_length); | |
1815 if(!data) { | |
1816 free(pgci_ut); | |
1817 ifofile->pgci_ut = 0; | |
1818 return 0; | |
1819 } | |
1820 if(!(DVDReadBytes(ifofile->file, data, info_length))) { | |
1821 free(data); | |
1822 free(pgci_ut); | |
1823 ifofile->pgci_ut = 0; | |
1824 return 0; | |
1825 } | |
1826 | |
1827 pgci_ut->lu = malloc(pgci_ut->nr_of_lus * sizeof(pgci_lu_t)); | |
1828 if(!pgci_ut->lu) { | |
1829 free(data); | |
1830 free(pgci_ut); | |
1831 ifofile->pgci_ut = 0; | |
1832 return 0; | |
1833 } | |
1834 ptr = data; | |
1835 for(i = 0; i < pgci_ut->nr_of_lus; i++) { | |
1836 memcpy(&pgci_ut->lu[i], ptr, PGCI_LU_SIZE); | |
1837 ptr += PGCI_LU_SIZE; | |
1838 B2N_16(pgci_ut->lu[i].lang_code); | |
1839 B2N_32(pgci_ut->lu[i].lang_start_byte); | |
1840 } | |
1841 free(data); | |
1842 | |
1843 for(i = 0; i < pgci_ut->nr_of_lus; i++) { | |
1844 /* Maybe this is only defined for v1.1 and later titles? */ | |
1845 /* If the bits in 'lu[i].exists' are enumerated abcd efgh then: | |
1846 VTS_x_yy.IFO VIDEO_TS.IFO | |
1847 a == 0x83 "Root" 0x82 "Title" | |
1848 b == 0x84 "Subpicture" | |
1849 c == 0x85 "Audio" | |
1850 d == 0x86 "Angle" | |
1851 e == 0x87 "PTT" | |
1852 */ | |
1853 CHECK_VALUE((pgci_ut->lu[i].exists & 0x07) == 0); | |
1854 } | |
1855 | |
1856 for(i = 0; i < pgci_ut->nr_of_lus; i++) { | |
1857 pgci_ut->lu[i].pgcit = malloc(sizeof(pgcit_t)); | |
1858 if(!pgci_ut->lu[i].pgcit) { | |
1859 unsigned int j; | |
1860 for(j = 0; j < i; j++) { | |
1861 ifoFree_PGCIT_internal(pgci_ut->lu[j].pgcit); | |
1862 free(pgci_ut->lu[j].pgcit); | |
1863 } | |
1864 free(pgci_ut->lu); | |
1865 free(pgci_ut); | |
1866 ifofile->pgci_ut = 0; | |
1867 return 0; | |
1868 } | |
1869 if(!ifoRead_PGCIT_internal(ifofile, pgci_ut->lu[i].pgcit, | |
1870 sector * DVD_BLOCK_LEN | |
1871 + pgci_ut->lu[i].lang_start_byte)) { | |
1872 unsigned int j; | |
1873 for(j = 0; j < i; j++) { | |
1874 ifoFree_PGCIT_internal(pgci_ut->lu[j].pgcit); | |
1875 free(pgci_ut->lu[j].pgcit); | |
1876 } | |
1877 free(pgci_ut->lu[i].pgcit); | |
1878 free(pgci_ut->lu); | |
1879 free(pgci_ut); | |
1880 ifofile->pgci_ut = 0; | |
1881 return 0; | |
1882 } | |
1883 /* | |
1884 * FIXME: Iterate and verify that all menus that should exists accordingly | |
1885 * to pgci_ut->lu[i].exists really do? | |
1886 */ | |
1887 } | |
1888 | |
1889 return 1; | |
1890 } | |
1891 | |
1892 | |
1893 void ifoFree_PGCI_UT(ifo_handle_t *ifofile) { | |
1894 unsigned int i; | |
1895 | |
1896 if(!ifofile) | |
1897 return; | |
1898 | |
1899 if(ifofile->pgci_ut) { | |
1900 for(i = 0; i < ifofile->pgci_ut->nr_of_lus; i++) { | |
1901 ifoFree_PGCIT_internal(ifofile->pgci_ut->lu[i].pgcit); | |
1902 free(ifofile->pgci_ut->lu[i].pgcit); | |
1903 } | |
1904 free(ifofile->pgci_ut->lu); | |
1905 free(ifofile->pgci_ut); | |
1906 ifofile->pgci_ut = 0; | |
1907 } | |
1908 } | |
1909 | |
1910 static int ifoRead_VTS_ATTRIBUTES(ifo_handle_t *ifofile, | |
1911 vts_attributes_t *vts_attributes, | |
1912 unsigned int offset) { | |
1913 unsigned int i; | |
1914 | |
1915 if(!DVDFileSeek_(ifofile->file, offset)) | |
1916 return 0; | |
1917 | |
1918 if(!(DVDReadBytes(ifofile->file, vts_attributes, sizeof(vts_attributes_t)))) | |
1919 return 0; | |
1920 | |
348
5300b5982b85
video_attr_t is now defined and read correctly, rather than relying on conditional bitfields
nicodvb
parents:
346
diff
changeset
|
1921 read_video_attr(&vts_attributes->vtsm_vobs_attr); |
5300b5982b85
video_attr_t is now defined and read correctly, rather than relying on conditional bitfields
nicodvb
parents:
346
diff
changeset
|
1922 read_video_attr(&vts_attributes->vtstt_vobs_video_attr); |
349
8a711d89e294
read_audio_attr() uses getbits() instead of relying on endianness-specific bitfields
nicodvb
parents:
348
diff
changeset
|
1923 read_audio_attr(&vts_attributes->vtsm_audio_attr); |
8a711d89e294
read_audio_attr() uses getbits() instead of relying on endianness-specific bitfields
nicodvb
parents:
348
diff
changeset
|
1924 for(i=0; i<8; i++) |
8a711d89e294
read_audio_attr() uses getbits() instead of relying on endianness-specific bitfields
nicodvb
parents:
348
diff
changeset
|
1925 read_audio_attr(&vts_attributes->vtstt_audio_attr[i]); |
225 | 1926 B2N_32(vts_attributes->last_byte); |
1927 B2N_32(vts_attributes->vts_cat); | |
1928 B2N_16(vts_attributes->vtsm_subp_attr.lang_code); | |
1929 for(i = 0; i < 32; i++) | |
1930 B2N_16(vts_attributes->vtstt_subp_attr[i].lang_code); | |
1931 | |
1932 CHECK_ZERO(vts_attributes->zero_1); | |
1933 CHECK_ZERO(vts_attributes->zero_2); | |
1934 CHECK_ZERO(vts_attributes->zero_3); | |
1935 CHECK_ZERO(vts_attributes->zero_4); | |
1936 CHECK_ZERO(vts_attributes->zero_5); | |
1937 CHECK_ZERO(vts_attributes->zero_6); | |
1938 CHECK_ZERO(vts_attributes->zero_7); | |
1939 CHECK_VALUE(vts_attributes->nr_of_vtsm_audio_streams <= 1); | |
1940 CHECK_VALUE(vts_attributes->nr_of_vtsm_subp_streams <= 1); | |
1941 CHECK_VALUE(vts_attributes->nr_of_vtstt_audio_streams <= 8); | |
1942 for(i = vts_attributes->nr_of_vtstt_audio_streams; i < 8; i++) | |
1943 CHECK_ZERO(vts_attributes->vtstt_audio_attr[i]); | |
1944 CHECK_VALUE(vts_attributes->nr_of_vtstt_subp_streams <= 32); | |
1945 { | |
1946 unsigned int nr_coded; | |
1947 CHECK_VALUE(vts_attributes->last_byte + 1 >= VTS_ATTRIBUTES_MIN_SIZE); | |
1948 nr_coded = (vts_attributes->last_byte + 1 - VTS_ATTRIBUTES_MIN_SIZE)/6; | |
1949 /* This is often nr_coded = 70, how do you know how many there really are? */ | |
1950 if(nr_coded > 32) { /* We haven't read more from disk/file anyway */ | |
1951 nr_coded = 32; | |
1952 } | |
1953 CHECK_VALUE(vts_attributes->nr_of_vtstt_subp_streams <= nr_coded); | |
1954 for(i = vts_attributes->nr_of_vtstt_subp_streams; i < nr_coded; i++) | |
1955 CHECK_ZERO(vts_attributes->vtstt_subp_attr[i]); | |
1956 } | |
1957 | |
1958 return 1; | |
1959 } | |
1960 | |
1961 | |
1962 | |
1963 int ifoRead_VTS_ATRT(ifo_handle_t *ifofile) { | |
1964 vts_atrt_t *vts_atrt; | |
1965 unsigned int i, info_length, sector; | |
1966 uint32_t *data; | |
1967 | |
1968 if(!ifofile) | |
1969 return 0; | |
1970 | |
1971 if(!ifofile->vmgi_mat) | |
1972 return 0; | |
1973 | |
1974 if(ifofile->vmgi_mat->vts_atrt == 0) /* mandatory */ | |
1975 return 0; | |
1976 | |
1977 sector = ifofile->vmgi_mat->vts_atrt; | |
1978 if(!DVDFileSeek_(ifofile->file, sector * DVD_BLOCK_LEN)) | |
1979 return 0; | |
1980 | |
1981 vts_atrt = (vts_atrt_t *)malloc(sizeof(vts_atrt_t)); | |
1982 if(!vts_atrt) | |
1983 return 0; | |
1984 | |
1985 ifofile->vts_atrt = vts_atrt; | |
1986 | |
1987 if(!(DVDReadBytes(ifofile->file, vts_atrt, VTS_ATRT_SIZE))) { | |
1988 free(vts_atrt); | |
1989 ifofile->vts_atrt = 0; | |
1990 return 0; | |
1991 } | |
1992 | |
1993 B2N_16(vts_atrt->nr_of_vtss); | |
1994 B2N_32(vts_atrt->last_byte); | |
1995 | |
1996 CHECK_ZERO(vts_atrt->zero_1); | |
1997 CHECK_VALUE(vts_atrt->nr_of_vtss != 0); | |
1998 CHECK_VALUE(vts_atrt->nr_of_vtss < 100); /* ?? */ | |
1999 CHECK_VALUE((uint32_t)vts_atrt->nr_of_vtss * (4 + VTS_ATTRIBUTES_MIN_SIZE) + | |
2000 VTS_ATRT_SIZE < vts_atrt->last_byte + 1); | |
2001 | |
2002 info_length = vts_atrt->nr_of_vtss * sizeof(uint32_t); | |
2003 data = (uint32_t *)malloc(info_length); | |
2004 if(!data) { | |
2005 free(vts_atrt); | |
2006 ifofile->vts_atrt = 0; | |
2007 return 0; | |
2008 } | |
2009 | |
2010 vts_atrt->vts_atrt_offsets = data; | |
2011 | |
2012 if(!(DVDReadBytes(ifofile->file, data, info_length))) { | |
2013 free(data); | |
2014 free(vts_atrt); | |
2015 ifofile->vts_atrt = 0; | |
2016 return 0; | |
2017 } | |
2018 | |
2019 for(i = 0; i < vts_atrt->nr_of_vtss; i++) { | |
2020 B2N_32(data[i]); | |
2021 CHECK_VALUE(data[i] + VTS_ATTRIBUTES_MIN_SIZE < vts_atrt->last_byte + 1); | |
2022 } | |
2023 | |
2024 info_length = vts_atrt->nr_of_vtss * sizeof(vts_attributes_t); | |
2025 vts_atrt->vts = (vts_attributes_t *)malloc(info_length); | |
2026 if(!vts_atrt->vts) { | |
2027 free(data); | |
2028 free(vts_atrt); | |
2029 ifofile->vts_atrt = 0; | |
2030 return 0; | |
2031 } | |
2032 for(i = 0; i < vts_atrt->nr_of_vtss; i++) { | |
2033 unsigned int offset = data[i]; | |
2034 if(!ifoRead_VTS_ATTRIBUTES(ifofile, &(vts_atrt->vts[i]), | |
2035 (sector * DVD_BLOCK_LEN) + offset)) { | |
2036 free(data); | |
2037 free(vts_atrt); | |
2038 ifofile->vts_atrt = 0; | |
2039 return 0; | |
2040 } | |
2041 | |
2042 /* This assert cant be in ifoRead_VTS_ATTRIBUTES */ | |
2043 CHECK_VALUE(offset + vts_atrt->vts[i].last_byte <= vts_atrt->last_byte + 1); | |
2044 /* Is this check correct? */ | |
2045 } | |
2046 | |
2047 return 1; | |
2048 } | |
2049 | |
2050 | |
2051 void ifoFree_VTS_ATRT(ifo_handle_t *ifofile) { | |
2052 if(!ifofile) | |
2053 return; | |
2054 | |
2055 if(ifofile->vts_atrt) { | |
2056 free(ifofile->vts_atrt->vts); | |
2057 free(ifofile->vts_atrt->vts_atrt_offsets); | |
2058 free(ifofile->vts_atrt); | |
2059 ifofile->vts_atrt = 0; | |
2060 } | |
2061 } | |
2062 | |
2063 | |
2064 int ifoRead_TXTDT_MGI(ifo_handle_t *ifofile) { | |
2065 txtdt_mgi_t *txtdt_mgi; | |
2066 | |
2067 if(!ifofile) | |
2068 return 0; | |
2069 | |
2070 if(!ifofile->vmgi_mat) | |
2071 return 0; | |
2072 | |
2073 /* Return successfully if there is nothing to read. */ | |
2074 if(ifofile->vmgi_mat->txtdt_mgi == 0) | |
2075 return 1; | |
2076 | |
2077 if(!DVDFileSeek_(ifofile->file, | |
2078 ifofile->vmgi_mat->txtdt_mgi * DVD_BLOCK_LEN)) | |
2079 return 0; | |
2080 | |
2081 txtdt_mgi = (txtdt_mgi_t *)malloc(sizeof(txtdt_mgi_t)); | |
2082 if(!txtdt_mgi) { | |
2083 return 0; | |
2084 } | |
2085 ifofile->txtdt_mgi = txtdt_mgi; | |
2086 | |
2087 if(!(DVDReadBytes(ifofile->file, txtdt_mgi, TXTDT_MGI_SIZE))) { | |
2088 fprintf(stderr, "libdvdread: Unable to read TXTDT_MGI.\n"); | |
2089 free(txtdt_mgi); | |
2090 ifofile->txtdt_mgi = 0; | |
2091 return 0; | |
2092 } | |
2093 | |
2094 /* fprintf(stderr, "-- Not done yet --\n"); */ | |
2095 return 1; | |
2096 } | |
2097 | |
2098 void ifoFree_TXTDT_MGI(ifo_handle_t *ifofile) { | |
2099 if(!ifofile) | |
2100 return; | |
2101 | |
2102 if(ifofile->txtdt_mgi) { | |
2103 free(ifofile->txtdt_mgi); | |
2104 ifofile->txtdt_mgi = 0; | |
2105 } | |
2106 } | |
2107 |