comparison libmpdvdkit2/ifo_read.c @ 7029:9db58ffbd73c

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