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