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