Fix email address
[mplayer/greg.git] / libmpdvdkit2 / ifo_read.c
bloba01ba7e04f26dd962bd562a30896ca3f152833ca
1 /*
2 * Copyright (C) 2000, 2001, 2002 Björn Englund <d4bjorn@dtek.chalmers.se>,
3 * Håkan Hjort <d95hjort@dtek.chalmers.se>
5 * Modified for use with MPlayer, changes contained in libdvdread_changes.diff.
6 * detailed CVS changelog at http://www.mplayerhq.hu/cgi-bin/cvsweb.cgi/main/
7 * $Id$
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <unistd.h>
27 #include <inttypes.h>
28 #include <string.h>
29 //#include <assert.h>
31 #include "dvd_reader.h"
33 #include "config.h" // Needed for WORDS_BIGENDIAN
34 #include "bswap.h"
35 #include "ifo_types.h"
36 #include "ifo_read.h"
38 #ifndef DVD_BLOCK_LEN
39 #define DVD_BLOCK_LEN 2048
40 #endif
42 #ifndef NDEBUG
43 #define CHECK_ZERO(arg) \
44 if(memcmp(my_friendly_zeros, &arg, sizeof(arg))) { \
45 unsigned int i_CZ; \
46 fprintf(stderr, "*** Zero check failed in %s:%i\n for %s = 0x", \
47 __FILE__, __LINE__, # arg ); \
48 for(i_CZ = 0; i_CZ < sizeof(arg); i_CZ++) \
49 fprintf(stderr, "%02x", *((uint8_t *)&arg + i_CZ)); \
50 fprintf(stderr, "\n"); \
52 static const uint8_t my_friendly_zeros[2048];
53 #else
54 #define CHECK_ZERO(arg) (void)(arg)
55 #endif
58 /* Prototypes for internal functions */
59 static int ifoRead_VMG(ifo_handle_t *ifofile);
60 static int ifoRead_VTS(ifo_handle_t *ifofile);
61 static int ifoRead_PGC(ifo_handle_t *ifofile, pgc_t *pgc, unsigned int offset);
62 static int ifoRead_PGC_COMMAND_TBL(ifo_handle_t *ifofile,
63 pgc_command_tbl_t *cmd_tbl,
64 unsigned int offset);
65 static int ifoRead_PGC_PROGRAM_MAP(ifo_handle_t *ifofile,
66 pgc_program_map_t *program_map,
67 unsigned int nr, unsigned int offset);
68 static int ifoRead_CELL_PLAYBACK_TBL(ifo_handle_t *ifofile,
69 cell_playback_t *cell_playback,
70 unsigned int nr, unsigned int offset);
71 static int ifoRead_CELL_POSITION_TBL(ifo_handle_t *ifofile,
72 cell_position_t *cell_position,
73 unsigned int nr, unsigned int offset);
74 static int ifoRead_VTS_ATTRIBUTES(ifo_handle_t *ifofile,
75 vts_attributes_t *vts_attributes,
76 unsigned int offset);
77 static int ifoRead_C_ADT_internal(ifo_handle_t *ifofile, c_adt_t *c_adt,
78 unsigned int sector);
79 static int ifoRead_VOBU_ADMAP_internal(ifo_handle_t *ifofile,
80 vobu_admap_t *vobu_admap,
81 unsigned int sector);
82 static int ifoRead_PGCIT_internal(ifo_handle_t *ifofile, pgcit_t *pgcit,
83 unsigned int offset);
85 static void ifoFree_PGC(pgc_t *pgc);
86 static void ifoFree_PGC_COMMAND_TBL(pgc_command_tbl_t *cmd_tbl);
87 static void ifoFree_PGCIT_internal(pgcit_t *pgcit);
90 static int DVDFileSeek_( dvd_file_t *dvd_file, uint32_t offset ) {
91 return (DVDFileSeek(dvd_file, (int)offset) == (int)offset);
95 ifo_handle_t *ifoOpen(dvd_reader_t *dvd, int title) {
96 ifo_handle_t *ifofile;
98 ifofile = (ifo_handle_t *)malloc(sizeof(ifo_handle_t));
99 if(!ifofile)
100 return 0;
102 memset(ifofile, 0, sizeof(ifo_handle_t));
104 ifofile->file = DVDOpenFile(dvd, title, DVD_READ_INFO_FILE);
105 if(!ifofile->file) {
106 if(title) {
107 fprintf(stderr, "libdvdread: Can't open file VTS_%02d_0.IFO.\n", title);
108 } else {
109 fprintf(stderr, "libdvdread: Can't open file VIDEO_TS.IFO.\n");
111 free(ifofile);
112 return 0;
115 /* First check if this is a VMGI file. */
116 if(ifoRead_VMG(ifofile)) {
118 /* These are both mandatory. */
119 if(!ifoRead_FP_PGC(ifofile) || !ifoRead_TT_SRPT(ifofile)) {
120 fprintf(stderr, "libdvdread: Invalid main menu IFO (VIDEO_TS.IFO).\n");
121 ifoClose(ifofile);
122 return 0;
125 ifoRead_PGCI_UT(ifofile);
126 ifoRead_PTL_MAIT(ifofile);
128 /* This is also mandatory. */
129 if(!ifoRead_VTS_ATRT(ifofile)) {
130 fprintf(stderr, "libdvdread: Invalid main menu IFO (VIDEO_TS.IFO).\n");
131 ifoClose(ifofile);
132 return 0;
135 ifoRead_TXTDT_MGI(ifofile);
136 ifoRead_C_ADT(ifofile);
137 ifoRead_VOBU_ADMAP(ifofile);
139 return ifofile;
142 if(ifoRead_VTS(ifofile)) {
144 if(!ifoRead_VTS_PTT_SRPT(ifofile) || !ifoRead_PGCIT(ifofile)) {
145 fprintf(stderr, "libdvdread: Invalid title IFO (VTS_%02d_0.IFO).\n",
146 title);
147 ifoClose(ifofile);
148 return 0;
152 ifoRead_PGCI_UT(ifofile);
153 ifoRead_C_ADT(ifofile);
154 ifoRead_VOBU_ADMAP(ifofile);
156 if(!ifoRead_TITLE_C_ADT(ifofile) || !ifoRead_TITLE_VOBU_ADMAP(ifofile)) {
157 fprintf(stderr, "libdvdread: Invalid title IFO (VTS_%02d_0.IFO).\n",
158 title);
159 ifoClose(ifofile);
160 return 0;
163 return ifofile;
166 if(title) {
167 fprintf(stderr, "libdvdread: Invalid IFO for title %d (VTS_%02d_0.IFO).\n",
168 title, title);
169 } else {
170 fprintf(stderr, "libdvdread: Invalid IFO for VMGM (VIDEO_TS.IFO).\n");
172 ifoClose(ifofile);
173 return 0;
177 ifo_handle_t *ifoOpenVMGI(dvd_reader_t *dvd) {
178 ifo_handle_t *ifofile;
180 ifofile = (ifo_handle_t *)malloc(sizeof(ifo_handle_t));
181 if(!ifofile)
182 return 0;
184 memset(ifofile, 0, sizeof(ifo_handle_t));
186 ifofile->file = DVDOpenFile(dvd, 0, DVD_READ_INFO_FILE);
187 if(!ifofile->file) {
188 fprintf(stderr, "libdvdread: Can't open file VIDEO_TS.IFO.\n");
189 free(ifofile);
190 return 0;
193 if(ifoRead_VMG(ifofile))
194 return ifofile;
196 fprintf(stderr, "libdvdread: Invalid main menu IFO (VIDEO_TS.IFO).\n");
197 ifoClose(ifofile);
198 return 0;
202 ifo_handle_t *ifoOpenVTSI(dvd_reader_t *dvd, int title) {
203 ifo_handle_t *ifofile;
205 ifofile = (ifo_handle_t *)malloc(sizeof(ifo_handle_t));
206 if(!ifofile)
207 return 0;
209 memset(ifofile, 0, sizeof(ifo_handle_t));
211 if(title <= 0 || title > 99) {
212 fprintf(stderr, "libdvdread: ifoOpenVTSI invalid title (%d).\n", title);
213 free(ifofile);
214 return 0;
217 ifofile->file = DVDOpenFile(dvd, title, DVD_READ_INFO_FILE);
218 if(!ifofile->file) {
219 fprintf(stderr, "libdvdread: Can't open file VTS_%02d_0.IFO.\n", title);
220 free(ifofile);
221 return 0;
224 ifoRead_VTS(ifofile);
225 if(ifofile->vtsi_mat)
226 return ifofile;
228 fprintf(stderr, "libdvdread: Invalid IFO for title %d (VTS_%02d_0.IFO).\n",
229 title, title);
230 ifoClose(ifofile);
231 return 0;
235 void ifoClose(ifo_handle_t *ifofile) {
236 if(!ifofile)
237 return;
239 ifoFree_VOBU_ADMAP(ifofile);
240 ifoFree_TITLE_VOBU_ADMAP(ifofile);
241 ifoFree_C_ADT(ifofile);
242 ifoFree_TITLE_C_ADT(ifofile);
243 ifoFree_TXTDT_MGI(ifofile);
244 ifoFree_VTS_ATRT(ifofile);
245 ifoFree_PTL_MAIT(ifofile);
246 ifoFree_PGCI_UT(ifofile);
247 ifoFree_TT_SRPT(ifofile);
248 ifoFree_FP_PGC(ifofile);
249 ifoFree_PGCIT(ifofile);
250 ifoFree_VTS_PTT_SRPT(ifofile);
252 if(ifofile->vmgi_mat)
253 free(ifofile->vmgi_mat);
255 if(ifofile->vtsi_mat)
256 free(ifofile->vtsi_mat);
258 DVDCloseFile(ifofile->file);
259 ifofile->file = 0;
260 free(ifofile);
261 ifofile = 0;
265 static int ifoRead_VMG(ifo_handle_t *ifofile) {
266 vmgi_mat_t *vmgi_mat;
268 vmgi_mat = (vmgi_mat_t *)malloc(sizeof(vmgi_mat_t));
269 if(!vmgi_mat)
270 return 0;
272 ifofile->vmgi_mat = vmgi_mat;
274 if(!DVDFileSeek_(ifofile->file, 0)) {
275 free(ifofile->vmgi_mat);
276 ifofile->vmgi_mat = 0;
277 return 0;
280 if(!DVDReadBytes(ifofile->file, vmgi_mat, sizeof(vmgi_mat_t))) {
281 free(ifofile->vmgi_mat);
282 ifofile->vmgi_mat = 0;
283 return 0;
286 if(strncmp("DVDVIDEO-VMG", vmgi_mat->vmg_identifier, 12) != 0) {
287 free(ifofile->vmgi_mat);
288 ifofile->vmgi_mat = 0;
289 return 0;
292 B2N_32(vmgi_mat->vmg_last_sector);
293 B2N_32(vmgi_mat->vmgi_last_sector);
294 B2N_32(vmgi_mat->vmg_category);
295 B2N_16(vmgi_mat->vmg_nr_of_volumes);
296 B2N_16(vmgi_mat->vmg_this_volume_nr);
297 B2N_16(vmgi_mat->vmg_nr_of_title_sets);
298 B2N_64(vmgi_mat->vmg_pos_code);
299 B2N_32(vmgi_mat->vmgi_last_byte);
300 B2N_32(vmgi_mat->first_play_pgc);
301 B2N_32(vmgi_mat->vmgm_vobs);
302 B2N_32(vmgi_mat->tt_srpt);
303 B2N_32(vmgi_mat->vmgm_pgci_ut);
304 B2N_32(vmgi_mat->ptl_mait);
305 B2N_32(vmgi_mat->vts_atrt);
306 B2N_32(vmgi_mat->txtdt_mgi);
307 B2N_32(vmgi_mat->vmgm_c_adt);
308 B2N_32(vmgi_mat->vmgm_vobu_admap);
309 B2N_16(vmgi_mat->vmgm_audio_attr.lang_code);
310 B2N_16(vmgi_mat->vmgm_subp_attr.lang_code);
313 CHECK_ZERO(vmgi_mat->zero_1);
314 CHECK_ZERO(vmgi_mat->zero_2);
315 CHECK_ZERO(vmgi_mat->zero_3);
316 CHECK_ZERO(vmgi_mat->zero_4);
317 CHECK_ZERO(vmgi_mat->zero_5);
318 CHECK_ZERO(vmgi_mat->zero_6);
319 CHECK_ZERO(vmgi_mat->zero_7);
320 CHECK_ZERO(vmgi_mat->zero_8);
321 CHECK_ZERO(vmgi_mat->zero_9);
322 CHECK_ZERO(vmgi_mat->zero_10);
323 assert(vmgi_mat->vmg_last_sector != 0);
324 assert(vmgi_mat->vmgi_last_sector != 0);
325 assert(vmgi_mat->vmgi_last_sector * 2 <= vmgi_mat->vmg_last_sector);
326 assert(vmgi_mat->vmg_nr_of_volumes != 0);
327 assert(vmgi_mat->vmg_this_volume_nr != 0);
328 assert(vmgi_mat->vmg_this_volume_nr <= vmgi_mat->vmg_nr_of_volumes);
329 assert(vmgi_mat->disc_side == 1 || vmgi_mat->disc_side == 2);
330 assert(vmgi_mat->vmg_nr_of_title_sets != 0);
331 assert(vmgi_mat->vmgi_last_byte >= 341);
332 assert(vmgi_mat->vmgi_last_byte / DVD_BLOCK_LEN <=
333 vmgi_mat->vmgi_last_sector);
334 /* It seems that first_play_pgc might be optional. */
335 assert(vmgi_mat->first_play_pgc != 0 &&
336 vmgi_mat->first_play_pgc < vmgi_mat->vmgi_last_byte);
337 assert(vmgi_mat->vmgm_vobs == 0 ||
338 (vmgi_mat->vmgm_vobs > vmgi_mat->vmgi_last_sector &&
339 vmgi_mat->vmgm_vobs < vmgi_mat->vmg_last_sector));
340 assert(vmgi_mat->tt_srpt <= vmgi_mat->vmgi_last_sector);
341 assert(vmgi_mat->vmgm_pgci_ut <= vmgi_mat->vmgi_last_sector);
342 assert(vmgi_mat->ptl_mait <= vmgi_mat->vmgi_last_sector);
343 assert(vmgi_mat->vts_atrt <= vmgi_mat->vmgi_last_sector);
344 assert(vmgi_mat->txtdt_mgi <= vmgi_mat->vmgi_last_sector);
345 assert(vmgi_mat->vmgm_c_adt <= vmgi_mat->vmgi_last_sector);
346 assert(vmgi_mat->vmgm_vobu_admap <= vmgi_mat->vmgi_last_sector);
348 assert(vmgi_mat->nr_of_vmgm_audio_streams <= 1);
349 assert(vmgi_mat->nr_of_vmgm_subp_streams <= 1);
351 return 1;
355 static int ifoRead_VTS(ifo_handle_t *ifofile) {
356 vtsi_mat_t *vtsi_mat;
357 int i;
359 vtsi_mat = (vtsi_mat_t *)malloc(sizeof(vtsi_mat_t));
360 if(!vtsi_mat)
361 return 0;
363 ifofile->vtsi_mat = vtsi_mat;
365 if(!DVDFileSeek_(ifofile->file, 0)) {
366 free(ifofile->vtsi_mat);
367 ifofile->vtsi_mat = 0;
368 return 0;
371 if(!(DVDReadBytes(ifofile->file, vtsi_mat, sizeof(vtsi_mat_t)))) {
372 free(ifofile->vtsi_mat);
373 ifofile->vtsi_mat = 0;
374 return 0;
377 if(strncmp("DVDVIDEO-VTS", vtsi_mat->vts_identifier, 12) != 0) {
378 free(ifofile->vtsi_mat);
379 ifofile->vtsi_mat = 0;
380 return 0;
383 B2N_32(vtsi_mat->vts_last_sector);
384 B2N_32(vtsi_mat->vtsi_last_sector);
385 B2N_32(vtsi_mat->vts_category);
386 B2N_32(vtsi_mat->vtsi_last_byte);
387 B2N_32(vtsi_mat->vtsm_vobs);
388 B2N_32(vtsi_mat->vtstt_vobs);
389 B2N_32(vtsi_mat->vts_ptt_srpt);
390 B2N_32(vtsi_mat->vts_pgcit);
391 B2N_32(vtsi_mat->vtsm_pgci_ut);
392 B2N_32(vtsi_mat->vts_tmapt);
393 B2N_32(vtsi_mat->vtsm_c_adt);
394 B2N_32(vtsi_mat->vtsm_vobu_admap);
395 B2N_32(vtsi_mat->vts_c_adt);
396 B2N_32(vtsi_mat->vts_vobu_admap);
397 B2N_16(vtsi_mat->vtsm_audio_attr.lang_code);
398 B2N_16(vtsi_mat->vtsm_subp_attr.lang_code);
399 for(i = 0; i < 8; i++)
400 B2N_16(vtsi_mat->vts_audio_attr[i].lang_code);
401 for(i = 0; i < 32; i++)
402 B2N_16(vtsi_mat->vts_subp_attr[i].lang_code);
405 CHECK_ZERO(vtsi_mat->zero_1);
406 CHECK_ZERO(vtsi_mat->zero_2);
407 CHECK_ZERO(vtsi_mat->zero_3);
408 CHECK_ZERO(vtsi_mat->zero_4);
409 CHECK_ZERO(vtsi_mat->zero_5);
410 CHECK_ZERO(vtsi_mat->zero_6);
411 CHECK_ZERO(vtsi_mat->zero_7);
412 CHECK_ZERO(vtsi_mat->zero_8);
413 CHECK_ZERO(vtsi_mat->zero_9);
414 CHECK_ZERO(vtsi_mat->zero_10);
415 CHECK_ZERO(vtsi_mat->zero_11);
416 CHECK_ZERO(vtsi_mat->zero_12);
417 CHECK_ZERO(vtsi_mat->zero_13);
418 CHECK_ZERO(vtsi_mat->zero_14);
419 CHECK_ZERO(vtsi_mat->zero_15);
420 CHECK_ZERO(vtsi_mat->zero_16);
421 CHECK_ZERO(vtsi_mat->zero_17);
422 CHECK_ZERO(vtsi_mat->zero_18);
423 CHECK_ZERO(vtsi_mat->zero_19);
424 CHECK_ZERO(vtsi_mat->zero_20);
425 assert(vtsi_mat->vtsi_last_sector*2 <= vtsi_mat->vts_last_sector);
426 assert(vtsi_mat->vtsi_last_byte/DVD_BLOCK_LEN <= vtsi_mat->vtsi_last_sector);
427 assert(vtsi_mat->vtsm_vobs == 0 ||
428 (vtsi_mat->vtsm_vobs > vtsi_mat->vtsi_last_sector &&
429 vtsi_mat->vtsm_vobs < vtsi_mat->vts_last_sector));
430 assert(vtsi_mat->vtstt_vobs == 0 ||
431 (vtsi_mat->vtstt_vobs > vtsi_mat->vtsi_last_sector &&
432 vtsi_mat->vtstt_vobs < vtsi_mat->vts_last_sector));
433 assert(vtsi_mat->vts_ptt_srpt <= vtsi_mat->vtsi_last_sector);
434 assert(vtsi_mat->vts_pgcit <= vtsi_mat->vtsi_last_sector);
435 assert(vtsi_mat->vtsm_pgci_ut <= vtsi_mat->vtsi_last_sector);
436 assert(vtsi_mat->vts_tmapt <= vtsi_mat->vtsi_last_sector);
437 assert(vtsi_mat->vtsm_c_adt <= vtsi_mat->vtsi_last_sector);
438 assert(vtsi_mat->vtsm_vobu_admap <= vtsi_mat->vtsi_last_sector);
439 assert(vtsi_mat->vts_c_adt <= vtsi_mat->vtsi_last_sector);
440 assert(vtsi_mat->vts_vobu_admap <= vtsi_mat->vtsi_last_sector);
442 assert(vtsi_mat->nr_of_vtsm_audio_streams <= 1);
443 assert(vtsi_mat->nr_of_vtsm_subp_streams <= 1);
445 assert(vtsi_mat->nr_of_vts_audio_streams <= 8);
446 for(i = vtsi_mat->nr_of_vts_audio_streams; i < 8; i++)
447 CHECK_ZERO(vtsi_mat->vts_audio_attr[i]);
449 assert(vtsi_mat->nr_of_vts_subp_streams <= 32);
450 for(i = vtsi_mat->nr_of_vts_subp_streams; i < 32; i++)
451 CHECK_ZERO(vtsi_mat->vts_subp_attr[i]);
453 return 1;
457 static int ifoRead_PGC_COMMAND_TBL(ifo_handle_t *ifofile,
458 pgc_command_tbl_t *cmd_tbl,
459 unsigned int offset) {
461 memset(cmd_tbl, 0, sizeof(pgc_command_tbl_t));
463 if(!DVDFileSeek_(ifofile->file, offset))
464 return 0;
466 if(!(DVDReadBytes(ifofile->file, cmd_tbl, PGC_COMMAND_TBL_SIZE)))
467 return 0;
469 B2N_16(cmd_tbl->nr_of_pre);
470 B2N_16(cmd_tbl->nr_of_post);
471 B2N_16(cmd_tbl->nr_of_cell);
473 assert(cmd_tbl->nr_of_pre + cmd_tbl->nr_of_post + cmd_tbl->nr_of_cell<= 255);
475 if(cmd_tbl->nr_of_pre != 0) {
476 unsigned int pre_cmds_size = cmd_tbl->nr_of_pre * COMMAND_DATA_SIZE;
477 cmd_tbl->pre_cmds = (vm_cmd_t *)malloc(pre_cmds_size);
478 if(!cmd_tbl->pre_cmds)
479 return 0;
481 if(!(DVDReadBytes(ifofile->file, cmd_tbl->pre_cmds, pre_cmds_size))) {
482 free(cmd_tbl->pre_cmds);
483 return 0;
487 if(cmd_tbl->nr_of_post != 0) {
488 unsigned int post_cmds_size = cmd_tbl->nr_of_post * COMMAND_DATA_SIZE;
489 cmd_tbl->post_cmds = (vm_cmd_t *)malloc(post_cmds_size);
490 if(!cmd_tbl->post_cmds) {
491 if(cmd_tbl->pre_cmds)
492 free(cmd_tbl->pre_cmds);
493 return 0;
495 if(!(DVDReadBytes(ifofile->file, cmd_tbl->post_cmds, post_cmds_size))) {
496 if(cmd_tbl->pre_cmds)
497 free(cmd_tbl->pre_cmds);
498 free(cmd_tbl->post_cmds);
499 return 0;
503 if(cmd_tbl->nr_of_cell != 0) {
504 unsigned int cell_cmds_size = cmd_tbl->nr_of_cell * COMMAND_DATA_SIZE;
505 cmd_tbl->cell_cmds = (vm_cmd_t *)malloc(cell_cmds_size);
506 if(!cmd_tbl->cell_cmds) {
507 if(cmd_tbl->pre_cmds)
508 free(cmd_tbl->pre_cmds);
509 if(cmd_tbl->post_cmds)
510 free(cmd_tbl->post_cmds);
511 return 0;
513 if(!(DVDReadBytes(ifofile->file, cmd_tbl->cell_cmds, cell_cmds_size))) {
514 if(cmd_tbl->pre_cmds)
515 free(cmd_tbl->pre_cmds);
516 if(cmd_tbl->post_cmds)
517 free(cmd_tbl->post_cmds);
518 free(cmd_tbl->cell_cmds);
519 return 0;
524 * Make a run over all the commands and see that we can interpret them all?
526 return 1;
530 static void ifoFree_PGC_COMMAND_TBL(pgc_command_tbl_t *cmd_tbl) {
531 if(cmd_tbl) {
532 if(cmd_tbl->nr_of_pre && cmd_tbl->pre_cmds)
533 free(cmd_tbl->pre_cmds);
534 if(cmd_tbl->nr_of_post && cmd_tbl->post_cmds)
535 free(cmd_tbl->post_cmds);
536 if(cmd_tbl->nr_of_cell && cmd_tbl->cell_cmds)
537 free(cmd_tbl->cell_cmds);
538 free(cmd_tbl);
542 static int ifoRead_PGC_PROGRAM_MAP(ifo_handle_t *ifofile,
543 pgc_program_map_t *program_map,
544 unsigned int nr, unsigned int offset) {
545 unsigned int size = nr * sizeof(pgc_program_map_t);
547 if(!DVDFileSeek_(ifofile->file, offset))
548 return 0;
550 if(!(DVDReadBytes(ifofile->file, program_map, size)))
551 return 0;
553 return 1;
556 static int ifoRead_CELL_PLAYBACK_TBL(ifo_handle_t *ifofile,
557 cell_playback_t *cell_playback,
558 unsigned int nr, unsigned int offset) {
559 unsigned int i;
560 unsigned int size = nr * sizeof(cell_playback_t);
562 if(!DVDFileSeek_(ifofile->file, offset))
563 return 0;
565 if(!(DVDReadBytes(ifofile->file, cell_playback, size)))
566 return 0;
568 for(i = 0; i < nr; i++) {
569 B2N_32(cell_playback[i].first_sector);
570 B2N_32(cell_playback[i].first_ilvu_end_sector);
571 B2N_32(cell_playback[i].last_vobu_start_sector);
572 B2N_32(cell_playback[i].last_sector);
574 /* Changed < to <= because this was false in the movie 'Pi'. */
575 assert(cell_playback[i].last_vobu_start_sector <=
576 cell_playback[i].last_sector);
577 assert(cell_playback[i].first_sector <=
578 cell_playback[i].last_vobu_start_sector);
581 return 1;
585 static int ifoRead_CELL_POSITION_TBL(ifo_handle_t *ifofile,
586 cell_position_t *cell_position,
587 unsigned int nr, unsigned int offset) {
588 unsigned int i;
589 unsigned int size = nr * sizeof(cell_position_t);
591 if(!DVDFileSeek_(ifofile->file, offset))
592 return 0;
594 if(!(DVDReadBytes(ifofile->file, cell_position, size)))
595 return 0;
597 for(i = 0; i < nr; i++) {
598 B2N_16(cell_position[i].vob_id_nr);
599 CHECK_ZERO(cell_position[i].zero_1);
602 return 1;
605 static int ifoRead_PGC(ifo_handle_t *ifofile, pgc_t *pgc, unsigned int offset) {
606 unsigned int i;
608 if(!DVDFileSeek_(ifofile->file, offset))
609 return 0;
611 if(!(DVDReadBytes(ifofile->file, pgc, PGC_SIZE)))
612 return 0;
614 B2N_16(pgc->next_pgc_nr);
615 B2N_16(pgc->prev_pgc_nr);
616 B2N_16(pgc->goup_pgc_nr);
617 B2N_16(pgc->command_tbl_offset);
618 B2N_16(pgc->program_map_offset);
619 B2N_16(pgc->cell_playback_offset);
620 B2N_16(pgc->cell_position_offset);
622 for(i = 0; i < 8; i++)
623 B2N_16(pgc->audio_control[i]);
624 for(i = 0; i < 32; i++)
625 B2N_32(pgc->subp_control[i]);
626 for(i = 0; i < 16; i++)
627 B2N_32(pgc->palette[i]);
629 CHECK_ZERO(pgc->zero_1);
630 assert(pgc->nr_of_programs <= pgc->nr_of_cells);
632 /* verify time (look at print_time) */
633 for(i = 0; i < 8; i++)
634 if(!pgc->audio_control[i] & 0x8000) /* The 'is present' bit */
635 CHECK_ZERO(pgc->audio_control[i]);
636 for(i = 0; i < 32; i++)
637 if(!pgc->subp_control[i] & 0x80000000) /* The 'is present' bit */
638 CHECK_ZERO(pgc->subp_control[i]);
640 /* Check that time is 0:0:0:0 also if nr_of_programs == 0 */
641 if(pgc->nr_of_programs == 0) {
642 CHECK_ZERO(pgc->still_time);
643 CHECK_ZERO(pgc->pg_playback_mode); // ??
644 assert(pgc->program_map_offset == 0);
645 assert(pgc->cell_playback_offset == 0);
646 assert(pgc->cell_position_offset == 0);
647 } else {
648 assert(pgc->program_map_offset != 0);
649 assert(pgc->cell_playback_offset != 0);
650 assert(pgc->cell_position_offset != 0);
653 if(pgc->command_tbl_offset != 0) {
654 pgc->command_tbl = malloc(sizeof(pgc_command_tbl_t));
655 if(!pgc->command_tbl)
656 return 0;
658 if(!ifoRead_PGC_COMMAND_TBL(ifofile, pgc->command_tbl,
659 offset + pgc->command_tbl_offset)) {
660 free(pgc->command_tbl);
661 return 0;
663 } else {
664 pgc->command_tbl = NULL;
667 if(pgc->program_map_offset != 0) {
668 pgc->program_map = malloc(pgc->nr_of_programs * sizeof(pgc_program_map_t));
669 if(!pgc->program_map) {
670 ifoFree_PGC_COMMAND_TBL(pgc->command_tbl);
671 return 0;
673 if(!ifoRead_PGC_PROGRAM_MAP(ifofile, pgc->program_map,pgc->nr_of_programs,
674 offset + pgc->program_map_offset)) {
675 ifoFree_PGC_COMMAND_TBL(pgc->command_tbl);
676 free(pgc->program_map);
677 return 0;
679 } else {
680 pgc->program_map = NULL;
683 if(pgc->cell_playback_offset != 0) {
684 pgc->cell_playback = malloc(pgc->nr_of_cells * sizeof(cell_playback_t));
685 if(!pgc->cell_playback) {
686 ifoFree_PGC_COMMAND_TBL(pgc->command_tbl);
687 if(pgc->program_map)
688 free(pgc->program_map);
689 return 0;
691 if(!ifoRead_CELL_PLAYBACK_TBL(ifofile, pgc->cell_playback,
692 pgc->nr_of_cells,
693 offset + pgc->cell_playback_offset)) {
694 ifoFree_PGC_COMMAND_TBL(pgc->command_tbl);
695 if(pgc->program_map)
696 free(pgc->program_map);
697 free(pgc->cell_playback);
698 return 0;
700 } else {
701 pgc->cell_playback = NULL;
704 if(pgc->cell_position_offset != 0) {
705 pgc->cell_position = malloc(pgc->nr_of_cells * sizeof(cell_position_t));
706 if(!pgc->cell_position) {
707 ifoFree_PGC(pgc);
708 return 0;
710 if(!ifoRead_CELL_POSITION_TBL(ifofile, pgc->cell_position,
711 pgc->nr_of_cells,
712 offset + pgc->cell_position_offset)) {
713 ifoFree_PGC(pgc);
714 return 0;
716 } else {
717 pgc->cell_position = NULL;
720 return 1;
723 int ifoRead_FP_PGC(ifo_handle_t *ifofile) {
725 if(!ifofile)
726 return 0;
728 if(!ifofile->vmgi_mat)
729 return 0;
731 /* It seems that first_play_pgc might be optional after all. */
732 if(ifofile->vmgi_mat->first_play_pgc == 0) { /* mandatory */
733 ifofile->first_play_pgc = 0;
734 return 0; /* change this to a 1 if it's optional. */
737 ifofile->first_play_pgc = (pgc_t *)malloc(sizeof(pgc_t));
738 if(!ifofile->first_play_pgc)
739 return 0;
741 if(!ifoRead_PGC(ifofile, ifofile->first_play_pgc,
742 ifofile->vmgi_mat->first_play_pgc)) {
743 free(ifofile->first_play_pgc);
744 ifofile->first_play_pgc = 0;
745 return 0;
748 return 1;
751 static void ifoFree_PGC(pgc_t *pgc) {
752 if(pgc) {
753 ifoFree_PGC_COMMAND_TBL(pgc->command_tbl);
754 if(pgc->program_map)
755 free(pgc->program_map);
756 if(pgc->cell_playback)
757 free(pgc->cell_playback);
758 if(pgc->cell_position)
759 free(pgc->cell_position);
763 void ifoFree_FP_PGC(ifo_handle_t *ifofile) {
764 if(!ifofile)
765 return;
767 if(ifofile->first_play_pgc) {
768 ifoFree_PGC(ifofile->first_play_pgc);
769 free(ifofile->first_play_pgc);
770 ifofile->first_play_pgc = 0;
775 int ifoRead_TT_SRPT(ifo_handle_t *ifofile) {
776 tt_srpt_t *tt_srpt;
777 int i, info_length;
779 if(!ifofile)
780 return 0;
782 if(!ifofile->vmgi_mat)
783 return 0;
785 if(ifofile->vmgi_mat->tt_srpt == 0) /* mandatory */
786 return 0;
788 if(!DVDFileSeek_(ifofile->file, ifofile->vmgi_mat->tt_srpt * DVD_BLOCK_LEN))
789 return 0;
791 tt_srpt = (tt_srpt_t *)malloc(sizeof(tt_srpt_t));
792 if(!tt_srpt)
793 return 0;
795 ifofile->tt_srpt = tt_srpt;
797 if(!(DVDReadBytes(ifofile->file, tt_srpt, TT_SRPT_SIZE))) {
798 fprintf(stderr, "libdvdread: Unable to read read TT_SRPT.\n");
799 free(tt_srpt);
800 return 0;
803 B2N_16(tt_srpt->nr_of_srpts);
804 B2N_32(tt_srpt->last_byte);
806 info_length = tt_srpt->last_byte + 1 - TT_SRPT_SIZE;
808 tt_srpt->title = (title_info_t *)malloc(info_length);
809 if(!tt_srpt->title) {
810 free(tt_srpt);
811 ifofile->tt_srpt = 0;
812 return 0;
814 if(!(DVDReadBytes(ifofile->file, tt_srpt->title, info_length))) {
815 fprintf(stderr, "libdvdread: Unable to read read TT_SRPT.\n");
816 ifoFree_TT_SRPT(ifofile);
817 return 0;
820 for(i = 0; i < tt_srpt->nr_of_srpts; i++) {
821 B2N_16(tt_srpt->title[i].nr_of_ptts);
822 B2N_16(tt_srpt->title[i].parental_id);
823 B2N_32(tt_srpt->title[i].title_set_sector);
827 CHECK_ZERO(tt_srpt->zero_1);
828 assert(tt_srpt->nr_of_srpts != 0);
829 assert(tt_srpt->nr_of_srpts < 100); // ??
830 assert((int)tt_srpt->nr_of_srpts * sizeof(title_info_t) <= info_length);
832 for(i = 0; i < tt_srpt->nr_of_srpts; i++) {
833 assert(tt_srpt->title[i].pb_ty.zero_1 == 0);
834 assert(tt_srpt->title[i].nr_of_angles != 0);
835 assert(tt_srpt->title[i].nr_of_angles < 10);
836 //assert(tt_srpt->title[i].nr_of_ptts != 0);
837 // XXX: this assertion breaks Ghostbusters:
838 assert(tt_srpt->title[i].nr_of_ptts < 1000); // ??
839 assert(tt_srpt->title[i].title_set_nr != 0);
840 assert(tt_srpt->title[i].title_set_nr < 100); // ??
841 assert(tt_srpt->title[i].vts_ttn != 0);
842 assert(tt_srpt->title[i].vts_ttn < 100); // ??
843 //assert(tt_srpt->title[i].title_set_sector != 0);
846 // Make this a function
847 #if 0
848 if(memcmp((uint8_t *)tt_srpt->title +
849 tt_srpt->nr_of_srpts * sizeof(title_info_t),
850 my_friendly_zeros,
851 info_length - tt_srpt->nr_of_srpts * sizeof(title_info_t))) {
852 fprintf(stderr, "VMG_PTT_SRPT slack is != 0, ");
853 hexdump((uint8_t *)tt_srpt->title +
854 tt_srpt->nr_of_srpts * sizeof(title_info_t),
855 info_length - tt_srpt->nr_of_srpts * sizeof(title_info_t));
857 #endif
859 return 1;
863 void ifoFree_TT_SRPT(ifo_handle_t *ifofile) {
864 if(!ifofile)
865 return;
867 if(ifofile->tt_srpt) {
868 free(ifofile->tt_srpt->title);
869 free(ifofile->tt_srpt);
870 ifofile->tt_srpt = 0;
875 int ifoRead_VTS_PTT_SRPT(ifo_handle_t *ifofile) {
876 vts_ptt_srpt_t *vts_ptt_srpt;
877 int info_length, i, j;
878 uint32_t *data;
880 if(!ifofile)
881 return 0;
883 if(!ifofile->vtsi_mat)
884 return 0;
886 if(ifofile->vtsi_mat->vts_ptt_srpt == 0) /* mandatory */
887 return 0;
889 if(!DVDFileSeek_(ifofile->file,
890 ifofile->vtsi_mat->vts_ptt_srpt * DVD_BLOCK_LEN))
891 return 0;
893 vts_ptt_srpt = (vts_ptt_srpt_t *)malloc(sizeof(vts_ptt_srpt_t));
894 if(!vts_ptt_srpt)
895 return 0;
897 ifofile->vts_ptt_srpt = vts_ptt_srpt;
899 if(!(DVDReadBytes(ifofile->file, vts_ptt_srpt, VTS_PTT_SRPT_SIZE))) {
900 fprintf(stderr, "libdvdread: Unable to read PTT search table.\n");
901 free(vts_ptt_srpt);
902 return 0;
905 B2N_16(vts_ptt_srpt->nr_of_srpts);
906 B2N_32(vts_ptt_srpt->last_byte);
908 CHECK_ZERO(vts_ptt_srpt->zero_1);
909 assert(vts_ptt_srpt->nr_of_srpts != 0);
910 assert(vts_ptt_srpt->nr_of_srpts < 100); // ??
912 info_length = vts_ptt_srpt->last_byte + 1 - VTS_PTT_SRPT_SIZE;
914 data = (uint32_t *)malloc(info_length);
915 if(!data) {
916 free(vts_ptt_srpt);
917 ifofile->vts_ptt_srpt = 0;
918 return 0;
920 if(!(DVDReadBytes(ifofile->file, data, info_length))) {
921 fprintf(stderr, "libdvdread: Unable to read PTT search table.\n");
922 free(vts_ptt_srpt);
923 free(data);
924 ifofile->vts_ptt_srpt = 0;
925 return 0;
928 for(i = 0; i < vts_ptt_srpt->nr_of_srpts; i++) {
929 B2N_32(data[i]);
930 /* assert(data[i] + sizeof(ptt_info_t) <= vts_ptt_srpt->last_byte + 1);
931 Magic Knight Rayearth Daybreak is mastered very strange and has
932 Titles with 0 PTTs. They all have a data[i] offsets beyond the end of
933 of the vts_ptt_srpt structure. */
934 assert(data[i] + sizeof(ptt_info_t) <= vts_ptt_srpt->last_byte + 1 + 4);
937 vts_ptt_srpt->title = malloc(vts_ptt_srpt->nr_of_srpts * sizeof(ttu_t));
938 if(!vts_ptt_srpt->title) {
939 free(vts_ptt_srpt);
940 free(data);
941 ifofile->vts_ptt_srpt = 0;
942 return 0;
944 for(i = 0; i < vts_ptt_srpt->nr_of_srpts; i++) {
945 int n;
946 if(i < vts_ptt_srpt->nr_of_srpts - 1)
947 n = (data[i+1] - data[i]);
948 else
949 n = (vts_ptt_srpt->last_byte + 1 - data[i]);
950 /* assert(n > 0 && (n % 4) == 0);
951 Magic Knight Rayearth Daybreak is mastered very strange and has
952 Titles with 0 PTTs. */
953 if(n < 0) n = 0;
954 assert(n % 4 == 0);
956 vts_ptt_srpt->title[i].nr_of_ptts = n / 4;
957 vts_ptt_srpt->title[i].ptt = malloc(n * sizeof(ptt_info_t));
958 if(!vts_ptt_srpt->title[i].ptt) {
959 for(n = 0; n < i; n++)
960 free(vts_ptt_srpt->title[n].ptt);
961 free(vts_ptt_srpt);
962 free(data);
963 ifofile->vts_ptt_srpt = 0;
964 return 0;
966 for(j = 0; j < vts_ptt_srpt->title[i].nr_of_ptts; j++) {
967 /* The assert placed here because of Magic Knight Rayearth Daybreak */
968 assert(data[i] + sizeof(ptt_info_t) <= vts_ptt_srpt->last_byte + 1);
969 vts_ptt_srpt->title[i].ptt[j].pgcn
970 = *(uint16_t*)(((char *)data) + data[i] + 4*j - VTS_PTT_SRPT_SIZE);
971 vts_ptt_srpt->title[i].ptt[j].pgn
972 = *(uint16_t*)(((char *)data) + data[i] + 4*j + 2 - VTS_PTT_SRPT_SIZE);
975 free(data);
977 for(i = 0; i < vts_ptt_srpt->nr_of_srpts; i++) {
978 for(j = 0; j < vts_ptt_srpt->title[i].nr_of_ptts; j++) {
979 B2N_16(vts_ptt_srpt->title[i].ptt[j].pgcn);
980 B2N_16(vts_ptt_srpt->title[i].ptt[j].pgn);
984 for(i = 0; i < vts_ptt_srpt->nr_of_srpts; i++) {
985 assert(vts_ptt_srpt->title[i].nr_of_ptts < 1000); // ??
986 for(j = 0; j < vts_ptt_srpt->title[i].nr_of_ptts; j++) {
987 assert(vts_ptt_srpt->title[i].ptt[j].pgcn != 0 );
988 assert(vts_ptt_srpt->title[i].ptt[j].pgcn < 1000); // ??
989 assert(vts_ptt_srpt->title[i].ptt[j].pgn != 0);
990 assert(vts_ptt_srpt->title[i].ptt[j].pgn < 100); // ??
994 return 1;
998 void ifoFree_VTS_PTT_SRPT(ifo_handle_t *ifofile) {
999 if(!ifofile)
1000 return;
1002 if(ifofile->vts_ptt_srpt) {
1003 int i;
1004 for(i = 0; i < ifofile->vts_ptt_srpt->nr_of_srpts; i++)
1005 free(ifofile->vts_ptt_srpt->title[i].ptt);
1006 free(ifofile->vts_ptt_srpt->title);
1007 free(ifofile->vts_ptt_srpt);
1008 ifofile->vts_ptt_srpt = 0;
1013 int ifoRead_PTL_MAIT(ifo_handle_t *ifofile) {
1014 ptl_mait_t *ptl_mait;
1015 int info_length;
1016 unsigned int i;
1018 if(!ifofile)
1019 return 0;
1021 if(!ifofile->vmgi_mat)
1022 return 0;
1024 if(ifofile->vmgi_mat->ptl_mait == 0)
1025 return 1;
1027 if(!DVDFileSeek_(ifofile->file,
1028 ifofile->vmgi_mat->ptl_mait * DVD_BLOCK_LEN))
1029 return 0;
1031 ptl_mait = (ptl_mait_t *)malloc(sizeof(ptl_mait_t));
1032 if(!ptl_mait)
1033 return 0;
1035 ifofile->ptl_mait = ptl_mait;
1037 if(!(DVDReadBytes(ifofile->file, ptl_mait, PTL_MAIT_SIZE))) {
1038 free(ptl_mait);
1039 ifofile->ptl_mait = 0;
1040 return 0;
1043 B2N_16(ptl_mait->nr_of_countries);
1044 B2N_16(ptl_mait->nr_of_vtss);
1045 B2N_32(ptl_mait->last_byte);
1047 info_length = ptl_mait->last_byte + 1 - PTL_MAIT_SIZE;
1049 assert(ptl_mait->nr_of_countries != 0);
1050 assert(ptl_mait->nr_of_countries < 100); // ??
1051 assert(ptl_mait->nr_of_vtss != 0);
1052 assert(ptl_mait->nr_of_vtss < 100); // ??
1053 assert(ptl_mait->nr_of_countries * PTL_MAIT_COUNTRY_SIZE <= info_length);
1055 /* Change this to read and 'translate' the tables too.
1056 I.e don't read so much here */
1057 ptl_mait->countries = (ptl_mait_country_t *)malloc(info_length);
1058 if(!ptl_mait->countries) {
1059 free(ptl_mait);
1060 ifofile->ptl_mait = 0;
1061 return 0;
1063 if(!(DVDReadBytes(ifofile->file, ptl_mait->countries, info_length))) {
1064 fprintf(stderr, "libdvdread: Unable to read PTL_MAIT.\n");
1065 ifoFree_PTL_MAIT(ifofile);
1066 return 0;
1069 for(i = 0; i < ptl_mait->nr_of_countries; i++) {
1070 B2N_16(ptl_mait->countries[i].country_code);
1071 B2N_16(ptl_mait->countries[i].pf_ptl_mai_start_byte);
1074 for(i = 0; i < ptl_mait->nr_of_countries; i++) {
1075 CHECK_ZERO(ptl_mait->countries[i].zero_1);
1076 CHECK_ZERO(ptl_mait->countries[i].zero_2);
1077 assert(ptl_mait->countries[i].pf_ptl_mai_start_byte +
1078 8 * (ptl_mait->nr_of_vtss + 1) * 2 <= ptl_mait->last_byte + 1);
1081 return 1;
1085 void ifoFree_PTL_MAIT(ifo_handle_t *ifofile) {
1086 if(!ifofile)
1087 return;
1089 if(ifofile->ptl_mait) {
1090 free(ifofile->ptl_mait->countries);
1091 free(ifofile->ptl_mait);
1092 ifofile->ptl_mait = 0;
1096 int ifoRead_TITLE_C_ADT(ifo_handle_t *ifofile) {
1098 if(!ifofile)
1099 return 0;
1101 if(!ifofile->vtsi_mat)
1102 return 0;
1104 if(ifofile->vtsi_mat->vts_c_adt == 0) /* mandatory */
1105 return 0;
1107 ifofile->vts_c_adt = (c_adt_t *)malloc(sizeof(c_adt_t));
1108 if(!ifofile->vts_c_adt)
1109 return 0;
1111 if(!ifoRead_C_ADT_internal(ifofile, ifofile->vts_c_adt,
1112 ifofile->vtsi_mat->vts_c_adt)) {
1113 free(ifofile->vts_c_adt);
1114 ifofile->vts_c_adt = 0;
1115 return 0;
1118 return 1;
1121 int ifoRead_C_ADT(ifo_handle_t *ifofile) {
1122 unsigned int sector;
1124 if(!ifofile)
1125 return 0;
1127 if(ifofile->vmgi_mat) {
1128 if(ifofile->vmgi_mat->vmgm_c_adt == 0)
1129 return 1;
1130 sector = ifofile->vmgi_mat->vmgm_c_adt;
1131 } else if(ifofile->vtsi_mat) {
1132 if(ifofile->vtsi_mat->vtsm_c_adt == 0)
1133 return 1;
1134 sector = ifofile->vtsi_mat->vtsm_c_adt;
1135 } else {
1136 return 0;
1139 ifofile->menu_c_adt = (c_adt_t *)malloc(sizeof(c_adt_t));
1140 if(!ifofile->menu_c_adt)
1141 return 0;
1143 if(!ifoRead_C_ADT_internal(ifofile, ifofile->menu_c_adt, sector)) {
1144 free(ifofile->menu_c_adt);
1145 ifofile->menu_c_adt = 0;
1146 return 0;
1149 return 1;
1152 static int ifoRead_C_ADT_internal(ifo_handle_t *ifofile,
1153 c_adt_t *c_adt, unsigned int sector) {
1154 int i, info_length;
1156 if(!DVDFileSeek_(ifofile->file, sector * DVD_BLOCK_LEN))
1157 return 0;
1159 if(!(DVDReadBytes(ifofile->file, c_adt, C_ADT_SIZE)))
1160 return 0;
1162 B2N_16(c_adt->nr_of_vobs);
1163 B2N_32(c_adt->last_byte);
1165 info_length = c_adt->last_byte + 1 - C_ADT_SIZE;
1167 CHECK_ZERO(c_adt->zero_1);
1168 /* assert(c_adt->nr_of_vobs > 0);
1169 Magic Knight Rayearth Daybreak is mastered very strange and has
1170 Titles with a VOBS that has no cells. */
1171 assert(info_length % sizeof(cell_adr_t) == 0);
1173 /* assert(info_length / sizeof(cell_adr_t) >= c_adt->nr_of_vobs);
1174 Enemy of the State region 2 (de) has Titles where nr_of_vobs field
1175 is to high, they high ones are never referenced though. */
1176 if(info_length / sizeof(cell_adr_t) < c_adt->nr_of_vobs) {
1177 fprintf(stderr, "libdvdread: *C_ADT nr_of_vobs > avaiable info entries\n");
1178 c_adt->nr_of_vobs = info_length / sizeof(cell_adr_t);
1181 c_adt->cell_adr_table = (cell_adr_t *)malloc(info_length);
1182 if(!c_adt->cell_adr_table)
1183 return 0;
1185 if(info_length &&
1186 !(DVDReadBytes(ifofile->file, c_adt->cell_adr_table, info_length))) {
1187 free(c_adt->cell_adr_table);
1188 return 0;
1191 for(i = 0; i < info_length/sizeof(cell_adr_t); i++) {
1192 B2N_16(c_adt->cell_adr_table[i].vob_id);
1193 B2N_32(c_adt->cell_adr_table[i].start_sector);
1194 B2N_32(c_adt->cell_adr_table[i].last_sector);
1196 CHECK_ZERO(c_adt->cell_adr_table[i].zero_1);
1197 assert(c_adt->cell_adr_table[i].vob_id > 0);
1198 assert(c_adt->cell_adr_table[i].vob_id <= c_adt->nr_of_vobs);
1199 assert(c_adt->cell_adr_table[i].cell_id > 0);
1200 assert(c_adt->cell_adr_table[i].start_sector <
1201 c_adt->cell_adr_table[i].last_sector);
1204 return 1;
1208 static void ifoFree_C_ADT_internal(c_adt_t *c_adt) {
1209 if(c_adt) {
1210 free(c_adt->cell_adr_table);
1211 free(c_adt);
1215 void ifoFree_C_ADT(ifo_handle_t *ifofile) {
1216 if(!ifofile)
1217 return;
1219 ifoFree_C_ADT_internal(ifofile->menu_c_adt);
1220 ifofile->menu_c_adt = 0;
1223 void ifoFree_TITLE_C_ADT(ifo_handle_t *ifofile) {
1224 if(!ifofile)
1225 return;
1227 ifoFree_C_ADT_internal(ifofile->vts_c_adt);
1228 ifofile->vts_c_adt = 0;
1231 int ifoRead_TITLE_VOBU_ADMAP(ifo_handle_t *ifofile) {
1232 if(!ifofile)
1233 return 0;
1235 if(!ifofile->vtsi_mat)
1236 return 0;
1238 if(ifofile->vtsi_mat->vts_vobu_admap == 0) /* mandatory */
1239 return 0;
1241 ifofile->vts_vobu_admap = (vobu_admap_t *)malloc(sizeof(vobu_admap_t));
1242 if(!ifofile->vts_vobu_admap)
1243 return 0;
1245 if(!ifoRead_VOBU_ADMAP_internal(ifofile, ifofile->vts_vobu_admap,
1246 ifofile->vtsi_mat->vts_vobu_admap)) {
1247 free(ifofile->vts_vobu_admap);
1248 ifofile->vts_vobu_admap = 0;
1249 return 0;
1252 return 1;
1255 int ifoRead_VOBU_ADMAP(ifo_handle_t *ifofile) {
1256 unsigned int sector;
1258 if(!ifofile)
1259 return 0;
1261 if(ifofile->vmgi_mat) {
1262 if(ifofile->vmgi_mat->vmgm_vobu_admap == 0)
1263 return 1;
1264 sector = ifofile->vmgi_mat->vmgm_vobu_admap;
1265 } else if(ifofile->vtsi_mat) {
1266 if(ifofile->vtsi_mat->vtsm_vobu_admap == 0)
1267 return 1;
1268 sector = ifofile->vtsi_mat->vtsm_vobu_admap;
1269 } else {
1270 return 0;
1273 ifofile->menu_vobu_admap = (vobu_admap_t *)malloc(sizeof(vobu_admap_t));
1274 if(!ifofile->menu_vobu_admap)
1275 return 0;
1277 if(!ifoRead_VOBU_ADMAP_internal(ifofile, ifofile->menu_vobu_admap, sector)) {
1278 free(ifofile->menu_vobu_admap);
1279 ifofile->menu_vobu_admap = 0;
1280 return 0;
1283 return 1;
1286 static int ifoRead_VOBU_ADMAP_internal(ifo_handle_t *ifofile,
1287 vobu_admap_t *vobu_admap,
1288 unsigned int sector) {
1289 unsigned int i;
1290 int info_length;
1292 if(!DVDFileSeek_(ifofile->file, sector * DVD_BLOCK_LEN))
1293 return 0;
1295 if(!(DVDReadBytes(ifofile->file, vobu_admap, VOBU_ADMAP_SIZE)))
1296 return 0;
1298 B2N_32(vobu_admap->last_byte);
1300 info_length = vobu_admap->last_byte + 1 - VOBU_ADMAP_SIZE;
1301 /* assert(info_length > 0);
1302 Magic Knight Rayearth Daybreak is mastered very strange and has
1303 Titles with a VOBS that has no VOBUs. */
1304 assert(info_length % sizeof(uint32_t) == 0);
1306 vobu_admap->vobu_start_sectors = (uint32_t *)malloc(info_length);
1307 if(!vobu_admap->vobu_start_sectors) {
1308 return 0;
1310 if(info_length &&
1311 !(DVDReadBytes(ifofile->file,
1312 vobu_admap->vobu_start_sectors, info_length))) {
1313 free(vobu_admap->vobu_start_sectors);
1314 return 0;
1317 for(i = 0; i < info_length/sizeof(uint32_t); i++)
1318 B2N_32(vobu_admap->vobu_start_sectors[i]);
1320 return 1;
1324 static void ifoFree_VOBU_ADMAP_internal(vobu_admap_t *vobu_admap) {
1325 if(vobu_admap) {
1326 free(vobu_admap->vobu_start_sectors);
1327 free(vobu_admap);
1331 void ifoFree_VOBU_ADMAP(ifo_handle_t *ifofile) {
1332 if(!ifofile)
1333 return;
1335 ifoFree_VOBU_ADMAP_internal(ifofile->menu_vobu_admap);
1336 ifofile->menu_vobu_admap = 0;
1339 void ifoFree_TITLE_VOBU_ADMAP(ifo_handle_t *ifofile) {
1340 if(!ifofile)
1341 return;
1343 ifoFree_VOBU_ADMAP_internal(ifofile->vts_vobu_admap);
1344 ifofile->vts_vobu_admap = 0;
1347 int ifoRead_PGCIT(ifo_handle_t *ifofile) {
1349 if(!ifofile)
1350 return 0;
1352 if(!ifofile->vtsi_mat)
1353 return 0;
1355 if(ifofile->vtsi_mat->vts_pgcit == 0) /* mandatory */
1356 return 0;
1358 ifofile->vts_pgcit = (pgcit_t *)malloc(sizeof(pgcit_t));
1359 if(!ifofile->vts_pgcit)
1360 return 0;
1362 if(!ifoRead_PGCIT_internal(ifofile, ifofile->vts_pgcit,
1363 ifofile->vtsi_mat->vts_pgcit * DVD_BLOCK_LEN)) {
1364 free(ifofile->vts_pgcit);
1365 ifofile->vts_pgcit = 0;
1366 return 0;
1369 return 1;
1372 static int ifoRead_PGCIT_internal(ifo_handle_t *ifofile, pgcit_t *pgcit,
1373 unsigned int offset) {
1374 int i, info_length;
1375 uint8_t *data, *ptr;
1377 if(!DVDFileSeek_(ifofile->file, offset))
1378 return 0;
1380 if(!(DVDReadBytes(ifofile->file, pgcit, PGCIT_SIZE)))
1381 return 0;
1383 B2N_16(pgcit->nr_of_pgci_srp);
1384 B2N_32(pgcit->last_byte);
1386 CHECK_ZERO(pgcit->zero_1);
1387 /* assert(pgcit->nr_of_pgci_srp != 0);
1388 Magic Knight Rayearth Daybreak is mastered very strange and has
1389 Titles with 0 PTTs. */
1390 assert(pgcit->nr_of_pgci_srp < 10000); // ?? seen max of 1338
1392 info_length = pgcit->nr_of_pgci_srp * PGCI_SRP_SIZE;
1393 data = malloc(info_length);
1394 if(!data)
1395 return 0;
1397 if(info_length && !(DVDReadBytes(ifofile->file, data, info_length))) {
1398 free(data);
1399 return 0;
1402 pgcit->pgci_srp = malloc(pgcit->nr_of_pgci_srp * sizeof(pgci_srp_t));
1403 if(!pgcit->pgci_srp) {
1404 free(data);
1405 return 0;
1407 ptr = data;
1408 for(i = 0; i < pgcit->nr_of_pgci_srp; i++) {
1409 memcpy(&pgcit->pgci_srp[i], ptr, PGCI_LU_SIZE);
1410 ptr += PGCI_LU_SIZE;
1411 B2N_16(pgcit->pgci_srp[i].ptl_id_mask);
1412 B2N_32(pgcit->pgci_srp[i].pgc_start_byte);
1413 assert(pgcit->pgci_srp[i].unknown1 == 0);
1415 free(data);
1417 for(i = 0; i < pgcit->nr_of_pgci_srp; i++)
1418 assert(pgcit->pgci_srp[i].pgc_start_byte + PGC_SIZE <= pgcit->last_byte+1);
1420 for(i = 0; i < pgcit->nr_of_pgci_srp; i++) {
1421 pgcit->pgci_srp[i].pgc = malloc(sizeof(pgc_t));
1422 if(!pgcit->pgci_srp[i].pgc) {
1423 int j;
1424 for(j = 0; j < i; j++) {
1425 ifoFree_PGC(pgcit->pgci_srp[j].pgc);
1426 free(pgcit->pgci_srp[j].pgc);
1428 return 0;
1430 if(!ifoRead_PGC(ifofile, pgcit->pgci_srp[i].pgc,
1431 offset + pgcit->pgci_srp[i].pgc_start_byte)) {
1432 int j;
1433 for(j = 0; j < i; j++) {
1434 ifoFree_PGC(pgcit->pgci_srp[j].pgc);
1435 free(pgcit->pgci_srp[j].pgc);
1437 free(pgcit->pgci_srp);
1438 return 0;
1442 return 1;
1445 static void ifoFree_PGCIT_internal(pgcit_t *pgcit) {
1446 if(pgcit) {
1447 int i;
1448 for(i = 0; i < pgcit->nr_of_pgci_srp; i++)
1449 ifoFree_PGC(pgcit->pgci_srp[i].pgc);
1450 free(pgcit->pgci_srp);
1454 void ifoFree_PGCIT(ifo_handle_t *ifofile) {
1455 if(!ifofile)
1456 return;
1458 if(ifofile->vts_pgcit) {
1459 ifoFree_PGCIT_internal(ifofile->vts_pgcit);
1460 free(ifofile->vts_pgcit);
1461 ifofile->vts_pgcit = 0;
1466 int ifoRead_PGCI_UT(ifo_handle_t *ifofile) {
1467 pgci_ut_t *pgci_ut;
1468 unsigned int sector;
1469 unsigned int i;
1470 int info_length;
1471 uint8_t *data, *ptr;
1473 if(!ifofile)
1474 return 0;
1476 if(ifofile->vmgi_mat) {
1477 if(ifofile->vmgi_mat->vmgm_pgci_ut == 0)
1478 return 1;
1479 sector = ifofile->vmgi_mat->vmgm_pgci_ut;
1480 } else if(ifofile->vtsi_mat) {
1481 if(ifofile->vtsi_mat->vtsm_pgci_ut == 0)
1482 return 1;
1483 sector = ifofile->vtsi_mat->vtsm_pgci_ut;
1484 } else {
1485 return 0;
1488 ifofile->pgci_ut = (pgci_ut_t *)malloc(sizeof(pgci_ut_t));
1489 if(!ifofile->pgci_ut)
1490 return 0;
1492 if(!DVDFileSeek_(ifofile->file, sector * DVD_BLOCK_LEN)) {
1493 free(ifofile->pgci_ut);
1494 ifofile->pgci_ut = 0;
1495 return 0;
1498 if(!(DVDReadBytes(ifofile->file, ifofile->pgci_ut, PGCI_UT_SIZE))) {
1499 free(ifofile->pgci_ut);
1500 ifofile->pgci_ut = 0;
1501 return 0;
1504 pgci_ut = ifofile->pgci_ut;
1506 B2N_16(pgci_ut->nr_of_lus);
1507 B2N_32(pgci_ut->last_byte);
1509 CHECK_ZERO(pgci_ut->zero_1);
1510 assert(pgci_ut->nr_of_lus != 0);
1511 assert(pgci_ut->nr_of_lus < 100); // ?? 3-4 ?
1512 assert((uint32_t)pgci_ut->nr_of_lus * PGCI_LU_SIZE < pgci_ut->last_byte);
1514 info_length = pgci_ut->nr_of_lus * PGCI_LU_SIZE;
1515 data = malloc(info_length);
1516 if(!data) {
1517 free(pgci_ut);
1518 ifofile->pgci_ut = 0;
1519 return 0;
1521 if(!(DVDReadBytes(ifofile->file, data, info_length))) {
1522 free(data);
1523 free(pgci_ut);
1524 ifofile->pgci_ut = 0;
1525 return 0;
1528 pgci_ut->lu = malloc(pgci_ut->nr_of_lus * sizeof(pgci_lu_t));
1529 if(!pgci_ut->lu) {
1530 free(data);
1531 free(pgci_ut);
1532 ifofile->pgci_ut = 0;
1533 return 0;
1535 ptr = data;
1536 for(i = 0; i < pgci_ut->nr_of_lus; i++) {
1537 memcpy(&pgci_ut->lu[i], ptr, PGCI_LU_SIZE);
1538 ptr += PGCI_LU_SIZE;
1539 B2N_16(pgci_ut->lu[i].lang_code);
1540 B2N_32(pgci_ut->lu[i].lang_start_byte);
1542 free(data);
1544 for(i = 0; i < pgci_ut->nr_of_lus; i++) {
1545 CHECK_ZERO(pgci_ut->lu[i].zero_1);
1546 // Maybe this is only defined for v1.1 and later titles?
1547 /* If the bits in 'lu[i].exists' are enumerated abcd efgh then:
1548 VTS_x_yy.IFO VIDEO_TS.IFO
1549 a == 0x83 "Root" 0x82 "Title"
1550 b == 0x84 "Subpicture"
1551 c == 0x85 "Audio"
1552 d == 0x86 "Angle"
1553 e == 0x87 "PTT"
1555 assert((pgci_ut->lu[i].exists & 0x07) == 0);
1558 for(i = 0; i < pgci_ut->nr_of_lus; i++) {
1559 pgci_ut->lu[i].pgcit = malloc(sizeof(pgcit_t));
1560 if(!pgci_ut->lu[i].pgcit) {
1561 unsigned int j;
1562 for(j = 0; j < i; j++) {
1563 ifoFree_PGCIT_internal(pgci_ut->lu[j].pgcit);
1564 free(pgci_ut->lu[j].pgcit);
1566 free(pgci_ut->lu);
1567 free(pgci_ut);
1568 ifofile->pgci_ut = 0;
1569 return 0;
1571 if(!ifoRead_PGCIT_internal(ifofile, pgci_ut->lu[i].pgcit,
1572 sector * DVD_BLOCK_LEN
1573 + pgci_ut->lu[i].lang_start_byte)) {
1574 unsigned int j;
1575 for(j = 0; j < i; j++) {
1576 ifoFree_PGCIT_internal(pgci_ut->lu[j].pgcit);
1577 free(pgci_ut->lu[j].pgcit);
1579 free(pgci_ut->lu[i].pgcit);
1580 free(pgci_ut->lu);
1581 free(pgci_ut);
1582 ifofile->pgci_ut = 0;
1583 return 0;
1585 // FIXME: Iterate and verify that all menus that should exists accordingly
1586 // to pgci_ut->lu[i].exists really do?
1589 return 1;
1593 void ifoFree_PGCI_UT(ifo_handle_t *ifofile) {
1594 unsigned int i;
1596 if(!ifofile)
1597 return;
1599 if(ifofile->pgci_ut) {
1600 for(i = 0; i < ifofile->pgci_ut->nr_of_lus; i++) {
1601 ifoFree_PGCIT_internal(ifofile->pgci_ut->lu[i].pgcit);
1602 free(ifofile->pgci_ut->lu[i].pgcit);
1604 free(ifofile->pgci_ut->lu);
1605 free(ifofile->pgci_ut);
1606 ifofile->pgci_ut = 0;
1610 static int ifoRead_VTS_ATTRIBUTES(ifo_handle_t *ifofile,
1611 vts_attributes_t *vts_attributes,
1612 unsigned int offset) {
1613 unsigned int i;
1615 if(!DVDFileSeek_(ifofile->file, offset))
1616 return 0;
1618 if(!(DVDReadBytes(ifofile->file, vts_attributes, sizeof(vts_attributes_t))))
1619 return 0;
1621 B2N_32(vts_attributes->last_byte);
1622 B2N_32(vts_attributes->vts_cat);
1623 B2N_16(vts_attributes->vtsm_audio_attr.lang_code);
1624 B2N_16(vts_attributes->vtsm_subp_attr.lang_code);
1625 for(i = 0; i < 8; i++)
1626 B2N_16(vts_attributes->vtstt_audio_attr[i].lang_code);
1627 for(i = 0; i < 32; i++)
1628 B2N_16(vts_attributes->vtstt_subp_attr[i].lang_code);
1630 CHECK_ZERO(vts_attributes->zero_1);
1631 CHECK_ZERO(vts_attributes->zero_2);
1632 CHECK_ZERO(vts_attributes->zero_3);
1633 CHECK_ZERO(vts_attributes->zero_4);
1634 CHECK_ZERO(vts_attributes->zero_5);
1635 CHECK_ZERO(vts_attributes->zero_6);
1636 CHECK_ZERO(vts_attributes->zero_7);
1637 assert(vts_attributes->nr_of_vtsm_audio_streams <= 1);
1638 assert(vts_attributes->nr_of_vtsm_subp_streams <= 1);
1639 assert(vts_attributes->nr_of_vtstt_audio_streams <= 8);
1640 for(i = vts_attributes->nr_of_vtstt_audio_streams; i < 8; i++)
1641 CHECK_ZERO(vts_attributes->vtstt_audio_attr[i]);
1642 assert(vts_attributes->nr_of_vtstt_subp_streams <= 32);
1644 unsigned int nr_coded;
1645 assert(vts_attributes->last_byte + 1 >= VTS_ATTRIBUTES_MIN_SIZE);
1646 nr_coded = (vts_attributes->last_byte + 1 - VTS_ATTRIBUTES_MIN_SIZE)/6;
1647 // This is often nr_coded = 70, how do you know how many there really are?
1648 if(nr_coded > 32) { // We haven't read more from disk/file anyway
1649 nr_coded = 32;
1651 assert(vts_attributes->nr_of_vtstt_subp_streams <= nr_coded);
1652 for(i = vts_attributes->nr_of_vtstt_subp_streams; i < nr_coded; i++)
1653 CHECK_ZERO(vts_attributes->vtstt_subp_attr[i]);
1656 return 1;
1661 int ifoRead_VTS_ATRT(ifo_handle_t *ifofile) {
1662 vts_atrt_t *vts_atrt;
1663 unsigned int i, info_length, sector;
1664 uint32_t *data;
1666 if(!ifofile)
1667 return 0;
1669 if(!ifofile->vmgi_mat)
1670 return 0;
1672 if(ifofile->vmgi_mat->vts_atrt == 0) /* mandatory */
1673 return 0;
1675 sector = ifofile->vmgi_mat->vts_atrt;
1676 if(!DVDFileSeek_(ifofile->file, sector * DVD_BLOCK_LEN))
1677 return 0;
1679 vts_atrt = (vts_atrt_t *)malloc(sizeof(vts_atrt_t));
1680 if(!vts_atrt)
1681 return 0;
1683 ifofile->vts_atrt = vts_atrt;
1685 if(!(DVDReadBytes(ifofile->file, vts_atrt, VTS_ATRT_SIZE))) {
1686 free(vts_atrt);
1687 ifofile->vts_atrt = 0;
1688 return 0;
1691 B2N_16(vts_atrt->nr_of_vtss);
1692 B2N_32(vts_atrt->last_byte);
1694 CHECK_ZERO(vts_atrt->zero_1);
1695 assert(vts_atrt->nr_of_vtss != 0);
1696 assert(vts_atrt->nr_of_vtss < 100); //??
1697 assert((uint32_t)vts_atrt->nr_of_vtss * (4 + VTS_ATTRIBUTES_MIN_SIZE) +
1698 VTS_ATRT_SIZE < vts_atrt->last_byte + 1);
1700 info_length = vts_atrt->nr_of_vtss * sizeof(uint32_t);
1701 data = (uint32_t *)malloc(info_length);
1702 if(!data) {
1703 free(vts_atrt);
1704 ifofile->vts_atrt = 0;
1705 return 0;
1707 if(!(DVDReadBytes(ifofile->file, data, info_length))) {
1708 free(data);
1709 free(vts_atrt);
1710 ifofile->vts_atrt = 0;
1711 return 0;
1714 for(i = 0; i < vts_atrt->nr_of_vtss; i++) {
1715 B2N_32(data[i]);
1716 assert(data[i] + VTS_ATTRIBUTES_MIN_SIZE < vts_atrt->last_byte + 1);
1719 info_length = vts_atrt->nr_of_vtss * sizeof(vts_attributes_t);
1720 vts_atrt->vts = (vts_attributes_t *)malloc(info_length);
1721 if(!vts_atrt->vts) {
1722 free(data);
1723 free(vts_atrt);
1724 ifofile->vts_atrt = 0;
1725 return 0;
1727 for(i = 0; i < vts_atrt->nr_of_vtss; i++) {
1728 unsigned int offset = data[i];
1729 if(!ifoRead_VTS_ATTRIBUTES(ifofile, &(vts_atrt->vts[i]),
1730 (sector * DVD_BLOCK_LEN) + offset)) {
1731 free(data);
1732 free(vts_atrt);
1733 ifofile->vts_atrt = 0;
1734 return 0;
1737 // This assert cant be in ifoRead_VTS_ATTRIBUTES
1738 assert(offset + vts_atrt->vts[i].last_byte <= vts_atrt->last_byte + 1);
1739 // Is this check correct?
1741 free(data);
1743 return 1;
1747 void ifoFree_VTS_ATRT(ifo_handle_t *ifofile) {
1748 if(!ifofile)
1749 return;
1751 if(ifofile->vts_atrt) {
1752 free(ifofile->vts_atrt->vts);
1753 free(ifofile->vts_atrt);
1754 ifofile->vts_atrt = 0;
1759 int ifoRead_TXTDT_MGI(ifo_handle_t *ifofile) {
1760 txtdt_mgi_t *txtdt_mgi;
1762 if(!ifofile)
1763 return 0;
1765 if(!ifofile->vmgi_mat)
1766 return 0;
1768 /* Return successfully if there is nothing to read. */
1769 if(ifofile->vmgi_mat->txtdt_mgi == 0)
1770 return 1;
1772 if(!DVDFileSeek_(ifofile->file,
1773 ifofile->vmgi_mat->txtdt_mgi * DVD_BLOCK_LEN))
1774 return 0;
1776 txtdt_mgi = (txtdt_mgi_t *)malloc(sizeof(txtdt_mgi_t));
1777 if(!txtdt_mgi) {
1778 return 0;
1780 ifofile->txtdt_mgi = txtdt_mgi;
1782 if(!(DVDReadBytes(ifofile->file, txtdt_mgi, TXTDT_MGI_SIZE))) {
1783 fprintf(stderr, "libdvdread: Unable to read TXTDT_MGI.\n");
1784 free(txtdt_mgi);
1785 ifofile->txtdt_mgi = 0;
1786 return 0;
1789 // fprintf(stderr, "-- Not done yet --\n");
1790 return 1;
1793 void ifoFree_TXTDT_MGI(ifo_handle_t *ifofile) {
1794 if(!ifofile)
1795 return;
1797 if(ifofile->txtdt_mgi) {
1798 free(ifofile->txtdt_mgi);
1799 ifofile->txtdt_mgi = 0;