comparison libdvdread/ifo_read.c @ 20615:1a4fcea7ab53

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