Debian Codegraph @

src/ifo_read.c

raw symbols SCIP
1 /*
2 * Copyright (C) 2000, 2001, 2002, 2003
3 * Björn Englund <[email protected]>,
4 * Håkan Hjort <[email protected]>
5 *
6 * This file is part of libdvdread.
7 *
8 * libdvdread is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * libdvdread is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with libdvdread; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 */
22
23 #include "config.h"
24
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <inttypes.h>
28 #include <string.h>
29
30 #include "bswap.h"
31 #include "dvdread/ifo_types.h"
32 #include "dvdread/ifo_read.h"
33 #include "dvdread/dvd_reader.h"
34 #include "dvdread_internal.h"
35 #include "dvdread/bitreader.h"
36
37 #ifndef HAVE_STATIC_ASSERT
38 #if defined(_MSC_VER)
39 #include <crtdbg.h>
40 #define _Static_assert(x, y) _STATIC_ASSERT(x)
41 #else
42 #define _Static_assert(x, y) // packed size checks not supported by this compiler
43 #endif
44 #endif
45
46 #define POINTER_SIZE sizeof(void*)
47
48
49 #ifndef DVD_BLOCK_LEN
50 #define DVD_BLOCK_LEN 2048
51 #endif
52
53 #define PRIV(a) container_of(a, struct ifo_handle_private_s, handle)
54
55 #define CHECK_VALUE(arg)\
56 if(!(arg)) {\
57 Log1(ifop->ctx, "CHECK_VALUE failed in %s:%i for %s",\
58 __FILE__, __LINE__, # arg );\
59 }
60
61 #ifndef NDEBUG
62 static inline char * makehexdump(const uint8_t *p_CZ, size_t i_CZ)
63 {
64 char *alloc = malloc(i_CZ * 2 + 1);
65 if(alloc)
66 {
67 *alloc = 0;
68 for(size_t i = 0; i < i_CZ; i++)
69 sprintf(&alloc[i*2], "%02x", *((uint8_t*)&p_CZ[i]));
70 }
71 return alloc;
72 }
73 #define CHECK_ZERO0(arg) \
74 if(arg != 0) { \
75 Log1(ifop->ctx, "Zero check failed in %s:%i\n for %s = 0x%x", \
76 __FILE__, __LINE__, # arg, arg); \
77 }
78 #define CHECK_ZERO(arg) \
79 if(memcmp(my_friendly_zeros, &arg, sizeof(arg))) { \
80 char *dump = makehexdump((const uint8_t *)&arg, sizeof(arg)); \
81 Log0(ifop->ctx, "Zero check failed in %s:%i for %s : 0x%s", \
82 __FILE__, __LINE__, # arg, dump ); \
83 free(dump); \
84 }
85 static const uint8_t my_friendly_zeros[2048];
86 #else
87 #define CHECK_ZERO0(arg) (void)(arg)
88 #define CHECK_ZERO(arg) (void)(arg)
89 #endif
90
91
92 /* Prototypes for internal functions */
93 static int ifoRead_VMG(ifo_handle_t *ifofile);
94 static int ifoRead_AMG(ifo_handle_t *ifofile);
95 /* Can be used to make simple dvd-a playback, no menus*/
96 static int ifoRead_SAMG(ifo_handle_t *ifofile);
97 /* for the still video set */
98 static int ifoRead_ASVS(ifo_handle_t *ifofile);
99
100 /* main DVD-VR structure */
101 static int ifoRead_RTAV_VMGI(ifo_handle_t *ifofile);
102
103 static int ifoRead_VTS(ifo_handle_t *ifofile);
104 static int ifoRead_ATS(ifo_handle_t *ifofile);
105 static int ifoRead_PGC(ifo_handle_t *ifofile, pgc_t *pgc, unsigned int offset);
106 static int ifoRead_PGC_COMMAND_TBL(ifo_handle_t *ifofile,
107 pgc_command_tbl_t *cmd_tbl,
108 unsigned int offset);
109 static int ifoRead_PGC_PROGRAM_MAP(ifo_handle_t *ifofile,
110 pgc_program_map_t *program_map,
111 unsigned int nr, unsigned int offset);
112 static int ifoRead_CELL_PLAYBACK_TBL(ifo_handle_t *ifofile,
113 cell_playback_t *cell_playback,
114 unsigned int nr, unsigned int offset);
115 static int ifoRead_CELL_POSITION_TBL(ifo_handle_t *ifofile,
116 cell_position_t *cell_position,
117 unsigned int nr, unsigned int offset);
118 static int ifoRead_VTS_ATTRIBUTES(ifo_handle_t *ifofile,
119 vts_attributes_t *vts_attributes,
120 unsigned int offset);
121 static int ifoRead_C_ADT_internal(ifo_handle_t *ifofile, c_adt_t *c_adt,
122 unsigned int sector);
123 static int ifoRead_VOBU_ADMAP_internal(ifo_handle_t *ifofile,
124 vobu_admap_t *vobu_admap,
125 unsigned int sector);
126 static int ifoRead_PGCIT_internal(ifo_handle_t *ifofile, pgcit_t *pgcit,
127 unsigned int offset);
128
129 static void ifoFree_PGC(pgc_t *pgc);
130 static void ifoFree_PGC_COMMAND_TBL(pgc_command_tbl_t *cmd_tbl);
131 static void ifoFree_PGCIT_internal(pgcit_t *pgcit);
132
133 static inline int DVDFileSeekForce_( dvd_file_t *dvd_file, uint32_t offset, int force_size ) {
134 return (DVDFileSeekForce(dvd_file, (int)offset, force_size) == (int)offset);
135 }
136
137 static inline int DVDFileSeek_( dvd_file_t *dvd_file, uint32_t offset ) {
138 return (DVDFileSeek(dvd_file, (int)offset) == (int)offset);
139 }
140
141 #define STRINGIFY_NAME( z ) STRINGIFY_NAME_( z )
142 #define STRINGIFY_NAME_( z ) #z
143
144 #define CHECK_STRUCT_SIZE(strt, ptrs, size) \
145 _Static_assert(sizeof(strt) <= size + ptrs * POINTER_SIZE, STRINGIFY_NAME(strt) " bigger than " STRINGIFY_NAME(size)); \
146 _Static_assert(sizeof(strt) >= size + ptrs * POINTER_SIZE, STRINGIFY_NAME(strt) " smaller than " STRINGIFY_NAME(size))
147
148 #define CHECK_STRUCT_SIZE_REF(strt, ptrs, size) \
149 _Static_assert(sizeof(strt) <= size + ptrs * POINTER_SIZE + sizeof(int), STRINGIFY_NAME(strt) " bigger than " STRINGIFY_NAME(size)); \
150 _Static_assert(sizeof(strt) >= size + ptrs * POINTER_SIZE + sizeof(int), STRINGIFY_NAME(strt) " smaller than " STRINGIFY_NAME(size))
151
152 /* size checks on packed structures */
153 CHECK_STRUCT_SIZE(dvd_time_t, 0, DVD_TIME_SIZE);
154 CHECK_STRUCT_SIZE(vm_cmd_t, 0, COMMAND_DATA_SIZE);
155 CHECK_STRUCT_SIZE(video_attr_t, 0, VIDEO_ATTR_SIZE);
156 CHECK_STRUCT_SIZE(audio_attr_t, 0, AUDIO_ATTR_SIZE);
157 CHECK_STRUCT_SIZE(multichannel_ext_t, 0, MULTICHANNEL_EXT_SIZE);
158 CHECK_STRUCT_SIZE(subp_attr_t, 0, SUBP_ATTR_SIZE);
159 CHECK_STRUCT_SIZE(pgc_command_tbl_t, 3, PGC_COMMAND_TBL_SIZE);
160 CHECK_STRUCT_SIZE(cell_playback_t, 0, CELL_PLAYBACK_SIZE);
161 CHECK_STRUCT_SIZE(cell_position_t, 0, CELL_POSITION_SIZE);
162 CHECK_STRUCT_SIZE(user_ops_t, 0, USER_OPS_SIZE);
163 CHECK_STRUCT_SIZE_REF(pgc_t, 4, PGC_SIZE);
164 CHECK_STRUCT_SIZE(pgci_srp_t, 1, PGCI_SRP_SIZE);
165 CHECK_STRUCT_SIZE_REF(pgcit_t, 1, PGCIT_SIZE);
166 CHECK_STRUCT_SIZE(pgci_lu_t, 1, PGCI_LU_SIZE);
167 CHECK_STRUCT_SIZE(pgci_ut_t, 1, PGCI_UT_SIZE);
168 CHECK_STRUCT_SIZE(cell_adr_t, 0, CELL_ADDR_SIZE);
169 CHECK_STRUCT_SIZE(c_adt_t, 1, C_ADT_SIZE);
170 CHECK_STRUCT_SIZE(vobu_admap_t, 1, VOBU_ADMAP_SIZE);
171 CHECK_STRUCT_SIZE(vmgi_mat_t, 0, VMGI_MAT_SIZE);
172
173 CHECK_STRUCT_SIZE(playback_type_t, 0, PLAYBACK_TYPE_SIZE);
174 CHECK_STRUCT_SIZE(title_info_t, 0, TITLE_INFO_SIZE);
175 CHECK_STRUCT_SIZE(tt_srpt_t, 1, TT_SRPT_SIZE);
176 CHECK_STRUCT_SIZE(ptl_mait_country_t, 1, PTL_MAIT_COUNTRY_SIZE);
177 CHECK_STRUCT_SIZE(ptl_mait_t, 1, PTL_MAIT_SIZE);
178 CHECK_STRUCT_SIZE(vts_attributes_t, 0, VTS_ATTRIBUTES_SIZE);
179
180 CHECK_STRUCT_SIZE(vts_atrt_t, 2, VTS_ATRT_SIZE);
181 CHECK_STRUCT_SIZE(txtdt_t, 0, TXTDT_SIZE);
182 CHECK_STRUCT_SIZE(txtdt_lu_t, 1, TXTDT_LU_SIZE);
183 CHECK_STRUCT_SIZE(txtdt_mgi_t, 1, TXTDT_MGI_SIZE);
184 CHECK_STRUCT_SIZE(vtsi_mat_t, 0, VTSI_MAT_SIZE);
185 CHECK_STRUCT_SIZE(ptt_info_t, 0, PTT_INFO_SIZE);
186 CHECK_STRUCT_SIZE(ttu_t, 1, TTU_SIZE);
187 CHECK_STRUCT_SIZE(vts_ptt_srpt_t, 2, VTS_PTT_SRPT_SIZE);
188 CHECK_STRUCT_SIZE(vts_tmap_t, 1, VTS_TMAP_SIZE);
189 CHECK_STRUCT_SIZE(vts_tmapt_t, 2, VTS_TMAPT_SIZE);
190 CHECK_STRUCT_SIZE(samg_chapter_t, 0, SAMG_CHAPTER_SIZE);
191 CHECK_STRUCT_SIZE(samg_mat_t, 1, SAMG_MAT_SIZE);
192 CHECK_STRUCT_SIZE(amgi_mat_t, 0, AMGI_MAT_SIZE);
193 CHECK_STRUCT_SIZE(track_info_t, 0, TRACK_INFO_SIZE);
194 CHECK_STRUCT_SIZE(tracks_info_table_t, 1, TRACKS_INFO_TABLE_SIZE);
195 CHECK_STRUCT_SIZE(atsi_record_t, 0, ATSI_RECORD_SIZE);
196 CHECK_STRUCT_SIZE(atsi_mat_t, 0, ATSI_MAT_SIZE);
197 CHECK_STRUCT_SIZE(atsi_title_index_t, 0, ATSI_TITLE_INDEX_SIZE);
198 CHECK_STRUCT_SIZE(atsi_track_timestamp_t, 0, ATSI_TRACK_TIMESTAMP_SIZE);
199 CHECK_STRUCT_SIZE(atsi_track_pointer_t, 0, ATSI_TRACK_POINTER_SIZE);
200 CHECK_STRUCT_SIZE(atsi_title_record_t, 4, ATSI_TITLE_ROW_TABLE_SIZE);
201 CHECK_STRUCT_SIZE(atsi_title_table_t, 2, ATSI_TITLE_TABLE_SIZE);
202 CHECK_STRUCT_SIZE(downmix_coeff_t, 0, DOWNMIX_COEFF_SIZE);
203 CHECK_STRUCT_SIZE(rtav_vmgi_t, 0, RTAV_VMGI_SIZE);
204 CHECK_STRUCT_SIZE(pgci_t, 1, PGCI_SIZE);
205 CHECK_STRUCT_SIZE(pgc_gi_t, 2, PGC_GI_SIZE);
206 CHECK_STRUCT_SIZE(ud_pgcit_t, 3, UD_PGCIT_SIZE);
207 CHECK_STRUCT_SIZE(m_c_gi_t, 1, M_C_GI_SIZE);
208 CHECK_STRUCT_SIZE(m_c_epi_t, 0, M_C_EPI_SIZE);
209
210 static void read_video_attr(video_attr_t *va) {
211 getbits_state_t state;
212 uint8_t buf[sizeof(video_attr_t)];
213
214 memcpy(buf, va, sizeof(video_attr_t));
215 if (!dvdread_getbits_init(&state, buf)) abort();
216 va->mpeg_version = dvdread_getbits(&state, 2);
217 va->video_format = dvdread_getbits(&state, 2);
218 va->display_aspect_ratio = dvdread_getbits(&state, 2);
219 va->permitted_df = dvdread_getbits(&state, 2);
220 va->line21_cc_1 = dvdread_getbits(&state, 1);
221 va->line21_cc_2 = dvdread_getbits(&state, 1);
222 va->unknown1 = dvdread_getbits(&state, 1);
223 va->bit_rate = dvdread_getbits(&state, 1);
224 va->picture_size = dvdread_getbits(&state, 2);
225 va->letterboxed = dvdread_getbits(&state, 1);
226 va->film_mode = dvdread_getbits(&state, 1);
227 }
228
229 static void read_audio_attr(audio_attr_t *aa) {
230 getbits_state_t state;
231 uint8_t buf[sizeof(audio_attr_t)];
232
233 memcpy(buf, aa, sizeof(audio_attr_t));
234 if (!dvdread_getbits_init(&state, buf)) abort();
235 aa->audio_format = dvdread_getbits(&state, 3);
236 aa->multichannel_extension = dvdread_getbits(&state, 1);
237 aa->lang_type = dvdread_getbits(&state, 2);
238 aa->application_mode = dvdread_getbits(&state, 2);
239 aa->quantization = dvdread_getbits(&state, 2);
240 aa->sample_frequency = dvdread_getbits(&state, 2);
241 aa->unknown1 = dvdread_getbits(&state, 1);
242 aa->channels = dvdread_getbits(&state, 3);
243 aa->lang_code = dvdread_getbits(&state, 16);
244 aa->lang_extension = dvdread_getbits(&state, 8);
245 aa->code_extension = dvdread_getbits(&state, 8);
246 aa->unknown3 = dvdread_getbits(&state, 8);
247 aa->app_info.karaoke.unknown4 = dvdread_getbits(&state, 1);
248 aa->app_info.karaoke.channel_assignment = dvdread_getbits(&state, 3);
249 aa->app_info.karaoke.version = dvdread_getbits(&state, 2);
250 aa->app_info.karaoke.mc_intro = dvdread_getbits(&state, 1);
251 aa->app_info.karaoke.mode = dvdread_getbits(&state, 1);
252 }
253
254 static void read_multichannel_ext(multichannel_ext_t *me) {
255 getbits_state_t state;
256 uint8_t buf[sizeof(multichannel_ext_t)];
257
258 memcpy(buf, me, sizeof(multichannel_ext_t));
259 if (!dvdread_getbits_init(&state, buf)) abort();
260 me->zero1 = dvdread_getbits(&state, 7);
261 me->ach0_gme = dvdread_getbits(&state, 1);
262 me->zero2 = dvdread_getbits(&state, 7);
263 me->ach1_gme = dvdread_getbits(&state, 1);
264 me->zero3 = dvdread_getbits(&state, 4);
265 me->ach2_gv1e = dvdread_getbits(&state, 1);
266 me->ach2_gv2e = dvdread_getbits(&state, 1);
267 me->ach2_gm1e = dvdread_getbits(&state, 1);
268 me->ach2_gm2e = dvdread_getbits(&state, 1);
269 me->zero4 = dvdread_getbits(&state, 4);
270 me->ach3_gv1e = dvdread_getbits(&state, 1);
271 me->ach3_gv2e = dvdread_getbits(&state, 1);
272 me->ach3_gmAe = dvdread_getbits(&state, 1);
273 me->ach3_se2e = dvdread_getbits(&state, 1);
274 me->zero5 = dvdread_getbits(&state, 4);
275 me->ach4_gv1e = dvdread_getbits(&state, 1);
276 me->ach4_gv2e = dvdread_getbits(&state, 1);
277 me->ach4_gmBe = dvdread_getbits(&state, 1);
278 me->ach4_seBe = dvdread_getbits(&state, 1);
279 }
280
281 static void read_subp_attr(subp_attr_t *sa) {
282 getbits_state_t state;
283 uint8_t buf[sizeof(subp_attr_t)];
284
285 memcpy(buf, sa, sizeof(subp_attr_t));
286 if (!dvdread_getbits_init(&state, buf)) abort();
287 sa->code_mode = dvdread_getbits(&state, 3);
288 sa->zero1 = dvdread_getbits(&state, 3);
289 sa->type = dvdread_getbits(&state, 2);
290 sa->zero2 = dvdread_getbits(&state, 8);
291 sa->lang_code = dvdread_getbits(&state, 16);
292 sa->lang_extension = dvdread_getbits(&state, 8);
293 sa->code_extension = dvdread_getbits(&state, 8);
294 }
295
296 static void read_user_ops(user_ops_t *uo) {
297 getbits_state_t state;
298 uint8_t buf[sizeof(user_ops_t)];
299
300 memcpy(buf, uo, sizeof(user_ops_t));
301 if (!dvdread_getbits_init(&state, buf)) abort();
302 uo->zero = dvdread_getbits(&state, 7);
303 uo->video_pres_mode_change = dvdread_getbits(&state, 1);
304 uo->karaoke_audio_pres_mode_change = dvdread_getbits(&state, 1);
305 uo->angle_change = dvdread_getbits(&state, 1);
306 uo->subpic_stream_change = dvdread_getbits(&state, 1);
307 uo->audio_stream_change = dvdread_getbits(&state, 1);
308 uo->pause_on = dvdread_getbits(&state, 1);
309 uo->still_off = dvdread_getbits(&state, 1);
310 uo->button_select_or_activate = dvdread_getbits(&state, 1);
311 uo->resume = dvdread_getbits(&state, 1);
312 uo->chapter_menu_call = dvdread_getbits(&state, 1);
313 uo->angle_menu_call = dvdread_getbits(&state, 1);
314 uo->audio_menu_call = dvdread_getbits(&state, 1);
315 uo->subpic_menu_call = dvdread_getbits(&state, 1);
316 uo->root_menu_call = dvdread_getbits(&state, 1);
317 uo->title_menu_call = dvdread_getbits(&state, 1);
318 uo->backward_scan = dvdread_getbits(&state, 1);
319 uo->forward_scan = dvdread_getbits(&state, 1);
320 uo->next_pg_search = dvdread_getbits(&state, 1);
321 uo->prev_or_top_pg_search = dvdread_getbits(&state, 1);
322 uo->time_or_chapter_search = dvdread_getbits(&state, 1);
323 uo->go_up = dvdread_getbits(&state, 1);
324 uo->stop = dvdread_getbits(&state, 1);
325 uo->title_play = dvdread_getbits(&state, 1);
326 uo->chapter_search_or_play = dvdread_getbits(&state, 1);
327 uo->title_or_time_play = dvdread_getbits(&state, 1);
328 }
329
330 static void read_pgci_srp(pgci_srp_t *ps) {
331 getbits_state_t state;
332 uint8_t buf[sizeof(pgci_srp_t)];
333
334 memcpy(buf, ps, sizeof(pgci_srp_t));
335 if (!dvdread_getbits_init(&state, buf)) abort();
336 ps->entry_id = dvdread_getbits(&state, 8);
337 ps->block_mode = dvdread_getbits(&state, 2);
338 ps->block_type = dvdread_getbits(&state, 2);
339 ps->zero_1 = dvdread_getbits(&state, 4);
340 ps->ptl_id_mask = dvdread_getbits(&state, 16);
341 ps->pgc_start_byte = dvdread_getbits(&state, 32);
342 }
343
344 static void read_cell_playback(cell_playback_t *cp) {
345 getbits_state_t state;
346 uint8_t buf[sizeof(cell_playback_t)];
347
348 memcpy(buf, cp, sizeof(cell_playback_t));
349 if (!dvdread_getbits_init(&state, buf)) abort();
350 cp->block_mode = dvdread_getbits(&state, 2);
351 cp->block_type = dvdread_getbits(&state, 2);
352 cp->seamless_play = dvdread_getbits(&state, 1);
353 cp->interleaved = dvdread_getbits(&state, 1);
354 cp->stc_discontinuity = dvdread_getbits(&state, 1);
355 cp->seamless_angle = dvdread_getbits(&state, 1);
356 cp->zero_1 = dvdread_getbits(&state, 1);
357 cp->playback_mode = dvdread_getbits(&state, 1);
358 cp->restricted = dvdread_getbits(&state, 1);
359 cp->cell_type = dvdread_getbits(&state, 5);
360 cp->still_time = dvdread_getbits(&state, 8);
361 cp->cell_cmd_nr = dvdread_getbits(&state, 8);
362
363 cp->playback_time.hour = dvdread_getbits(&state, 8);
364 cp->playback_time.minute = dvdread_getbits(&state, 8);
365 cp->playback_time.second = dvdread_getbits(&state, 8);
366 cp->playback_time.frame_u = dvdread_getbits(&state, 8);
367
368 cp->first_sector = dvdread_getbits(&state, 32);
369 cp->first_ilvu_end_sector = dvdread_getbits(&state, 32);
370 cp->last_vobu_start_sector = dvdread_getbits(&state, 32);
371 cp->last_sector = dvdread_getbits(&state, 32);
372 }
373
374 static void read_playback_type(playback_type_t *pt) {
375 getbits_state_t state;
376 uint8_t buf[sizeof(playback_type_t)];
377
378 memcpy(buf, pt, sizeof(playback_type_t));
379 if (!dvdread_getbits_init(&state, buf)) abort();
380 pt->zero_1 = dvdread_getbits(&state, 1);
381 pt->multi_or_random_pgc_title = dvdread_getbits(&state, 1);
382 pt->jlc_exists_in_cell_cmd = dvdread_getbits(&state, 1);
383 pt->jlc_exists_in_prepost_cmd = dvdread_getbits(&state, 1);
384 pt->jlc_exists_in_button_cmd = dvdread_getbits(&state, 1);
385 pt->jlc_exists_in_tt_dom = dvdread_getbits(&state, 1);
386 pt->chapter_search_or_play = dvdread_getbits(&state, 1);
387 pt->title_or_time_play = dvdread_getbits(&state, 1);
388 }
389
390 static void free_ptl_mait(ptl_mait_t* ptl_mait, int num_entries) {
391 int i;
392 for (i = 0; i < num_entries; i++)
393 free(ptl_mait->countries[i].pf_ptl_mai);
394
395 free(ptl_mait->countries);
396 free(ptl_mait);
397 }
398
399 static ifo_handle_t *ifoOpenFileOrBackup(dvd_reader_t *ctx, int title,
400 int backup) {
401 struct ifo_handle_private_s *ifop;
402 dvd_read_domain_t domain = backup ? DVD_READ_INFO_BACKUP_FILE
403 : DVD_READ_INFO_FILE;
404 char ifo_filename[MAX_UDF_FILE_NAME_LEN];
405
406 ifop = calloc(1, sizeof(*ifop));
407 if(!ifop)
408 return NULL;
409
410 ifop->ctx = ctx;
411 ifop->file = DVDOpenFile(ctx, title, domain);
412 if(!ifop->file)
413 {
414 free(ifop);
415 return NULL;
416 }
417
418 if ( ctx->dvd_type == DVD_VR )
419 snprintf(ifo_filename, 13, "VR_MANGR.IFO" );
420 else if (title)
421 snprintf(ifo_filename, 13, "%cTS_%02d_0.%s", STREAM_TYPE_STRING( ctx->dvd_type )
422 ,title, backup ? "BUP" : "IFO");
423 else
424 snprintf(ifo_filename, 13, "%s_TS.%s", DVD_TYPE_STRING( ctx->dvd_type ), backup ? "BUP" : "IFO" );
425
426 ifo_handle_t *ifofile = &ifop->handle;
427 /* First check if this is a VMGI file. */
428 if(ifoRead_VMG(ifofile)) {
429
430 ifofile->ifo_format=IFO_VIDEO;
431 /* These are both mandatory. */
432 if(!ifoRead_FP_PGC(ifofile) || !ifoRead_TT_SRPT(ifofile))
433 goto ifoOpen_fail;
434
435 ifoRead_PGCI_UT(ifofile);
436 ifoRead_PTL_MAIT(ifofile);
437
438 /* This is also mandatory. */
439 if(!ifoRead_VTS_ATRT(ifofile))
440 goto ifoOpen_fail;
441
442 ifoRead_TXTDT_MGI(ifofile);
443 ifoRead_C_ADT(ifofile);
444 ifoRead_VOBU_ADMAP(ifofile);
445
446 return ifofile;
447 }
448
449 if(ifoRead_VTS(ifofile)) {
450
451 ifofile->ifo_format=IFO_VIDEO;
452 if(!ifoRead_VTS_PTT_SRPT(ifofile) || !ifoRead_PGCIT(ifofile))
453 goto ifoOpen_fail;
454
455 ifoRead_PGCI_UT(ifofile);
456 ifoRead_VTS_TMAPT(ifofile);
457 ifoRead_C_ADT(ifofile);
458 ifoRead_VOBU_ADMAP(ifofile);
459
460 if(!ifoRead_TITLE_C_ADT(ifofile) || !ifoRead_TITLE_VOBU_ADMAP(ifofile))
461 goto ifoOpen_fail;
462
463 return ifofile;
464 }
465
466 if(ifoRead_AMG(ifofile)){
467 ifofile->ifo_format=IFO_AUDIO;
468 /* same function for both tables, will not be the same table in the case of non hybrid discs */
469 if(!ifoRead_TIF(ifofile,1))
470 goto ifoOpen_fail;
471 if(!ifoRead_TIF(ifofile,2))
472 goto ifoOpen_fail;
473
474 /* Should read SAMG as it contains location to AOB pointers */
475 DVDCloseFile(ifop->file);
476 ifop->file = DVDOpenFile(ctx, 2, DVD_READ_SAMG_INFO);
477 if(!ifop->file)
478 goto ifoOpen_fail;
479 if(!ifoRead_SAMG(ifofile))
480 goto ifoOpen_fail;
481
482 /* The Audio Still Video Set (ASVS) is optional, though usually exists */
483 DVDCloseFile(ifop->file);
484 ifop->file = DVDOpenFile(ctx, 2, backup ? DVD_READ_ASVS_INFO_BACKUP
485 : DVD_READ_ASVS_INFO);
486 if(!ifop->file)
487 Log1(ctx, "Disc does not have a still video set");
488 else {
489 if(!ifoRead_ASVS(ifofile))
490 goto ifoOpen_fail;
491 }
492
493 return ifofile;
494 }
495
496
497 if(ifoRead_ATS(ifofile)){
498 ifofile->ifo_format=IFO_AUDIO;
499
500 if(!ifoRead_TT(ifofile))
501 goto ifoOpen_fail;
502
503 return ifofile;
504 }
505
506 if(ifoRead_RTAV_VMGI(ifofile)){
507 ifofile->ifo_format=IFO_VIDEO_RECORDING;
508
509 if(!ifoRead_UD_PGCIT(ifofile))
510 goto ifoOpen_fail;
511
512 if(!ifoRead_PGCI(ifofile))
513 goto ifoOpen_fail;
514
515 if(!ifoRead_PGC_GI(ifofile))
516 goto ifoOpen_fail;
517
518 if(!ifofile->pgc_gi)
519 goto ifoOpen_fail;
520
521 return ifofile;
522 }
523
524 ifoOpen_fail:
525 Log1(ctx, "Invalid IFO for title %d (%s).", title, ifo_filename);
526 ifoClose(ifofile);
527 return NULL;
528 }
529
530 static void ifoSetBupFlag(dvd_reader_t *ctx, int title)
531 {
532 if(title > 63)
533 ctx->ifoBUPflags[0] |= UINT64_C(1) << (title - 64);
534 else
535 ctx->ifoBUPflags[1] |= UINT64_C(1) << title;
536 }
537
538 static int ifoGetBupFlag(const dvd_reader_t *ctx, int title)
539 {
540 int bupflag;
541 if(title > 63)
542 bupflag = !! (ctx->ifoBUPflags[0] & (UINT64_C(1) << (title - 64)));
543 else
544 bupflag = !! (ctx->ifoBUPflags[1] & (UINT64_C(1) << title));
545 return bupflag;
546 }
547
548 ifo_handle_t *ifoOpen(dvd_reader_t *ctx, int title) {
549 ifo_handle_t *ifofile;
550 int bupflag = ifoGetBupFlag(ctx, title);
551
552 ifofile = ifoOpenFileOrBackup(ctx, title, bupflag);
553 if(!ifofile) /* Try backup */
554 {
555 ifofile = ifoOpenFileOrBackup(ctx, title, 1);
556 if(ifofile && !bupflag)
557 ifoSetBupFlag(ctx, title);
558 }
559 return ifofile;
560 }
561
562 ifo_handle_t *ifoOpenVMGI(dvd_reader_t *ctx) {
563 struct ifo_handle_private_s *ifop;
564
565 const char *filename_prefix;
566 int (*reader_func)(ifo_handle_t *);
567 int result_format;
568
569 /* function should handle all ifo types gracefully */
570 switch(ctx->dvd_type) {
571 case DVD_VR:
572 filename_prefix = "VR_MANGR";
573 reader_func = ifoRead_RTAV_VMGI;
574 result_format = IFO_VIDEO_RECORDING;
575 break;
576 case DVD_V:
577 filename_prefix = "VIDEO_TS";
578 reader_func = ifoRead_VMG;
579 result_format = IFO_VIDEO;
580 break;
581 default: /* Fallback to DVD_A (Audio) */
582 filename_prefix = "AUDIO_TS";
583 reader_func = ifoRead_AMG;
584 result_format = IFO_AUDIO;
585 break;
586 }
587
588 for(int backup = ifoGetBupFlag(ctx, 0); backup <= 1; backup++)
589 {
590 ifop = calloc(1, sizeof(*ifop));
591 if(!ifop) return NULL;
592
593 const dvd_read_domain_t domain = backup ? DVD_READ_INFO_BACKUP_FILE
594 : DVD_READ_INFO_FILE;
595 const char *ext = backup ? "BUP" : "IFO";
596
597 ifop->ctx = ctx;
598
599 ifop->file = DVDOpenFile(ctx, 0, domain);
600
601 if(!ifop->file) {
602 Log1(ctx, "Can't open file %s.%s", filename_prefix, ext);
603 free(ifop);
604 continue;
605 }
606
607 if(reader_func(&ifop->handle)) {
608 ifop->handle.ifo_format = result_format;
609 return &ifop->handle;
610 }
611
612 Log1(ctx, "ifoOpenVMGI(): Invalid main menu IFO (%s.%s).", filename_prefix, ext);
613 ifoClose(&ifop->handle);
614 }
615
616 return NULL;
617 }
618
619 ifo_handle_t *ifoOpenVTSI(dvd_reader_t *ctx, int title) {
620 struct ifo_handle_private_s *ifop;
621
622 if(title <= 0 || title > 99) {
623 Log1(ctx, "ifoOpenVTSI invalid title (%d).", title);
624 return NULL;
625 }
626
627 for(int backup = ifoGetBupFlag(ctx, title); backup <= 1; backup++)
628 {
629 ifop = calloc(1, sizeof(*ifop));
630 if(!ifop)
631 return NULL;
632
633 const dvd_read_domain_t domain = backup ? DVD_READ_INFO_BACKUP_FILE
634 : DVD_READ_INFO_FILE;
635 const char *ext = backup ? "BUP" : "IFO";
636 ifop->ctx = ctx;
637 ifop->file = DVDOpenFile(ctx, title, domain);
638 /* Should really catch any error */
639 if(!ifop->file) {
640 Log1(ctx, "Can't open file %cTS_%02d_0.%s.",
641 STREAM_TYPE_STRING( ctx->dvd_type ), title, ext);
642 free(ifop);
643 continue;
644 }
645
646 if ((ctx->dvd_type == DVD_V
647 ? (ifoRead_VTS(&ifop->handle) && ifop->handle.vtsi_mat)
648 : (ifoRead_ATS(&ifop->handle) && ifop->handle.atsi_mat))) {
649 ifop->handle.ifo_format = ctx->dvd_type == DVD_V ? IFO_VIDEO : IFO_AUDIO;
650 return &ifop->handle;
651 }
652
653 Log1(ctx, "Invalid IFO for title %d (%cTS_%02d_0.%s).",
654 title, STREAM_TYPE_STRING( ctx->dvd_type ), title, ext);
655 ifoClose(&ifop->handle);
656 }
657
658 return NULL;
659 }
660
661 ifo_handle_t *ifoOpenASVS(dvd_reader_t *ctx) {
662
663 if ( ctx->dvd_type != DVD_A )
664 {
665 Log1(ctx, "the ASVS IFO is exclusive to DVD-Audio Discs");
666 return NULL;
667 }
668
669 struct ifo_handle_private_s *ifop;
670
671 for(int backup = ifoGetBupFlag(ctx, 0); backup <= 1; backup++)
672 {
673 ifop = calloc(1, sizeof(*ifop));
674 if(!ifop)
675 return NULL;
676
677 const dvd_read_domain_t domain = backup ? DVD_READ_ASVS_INFO_BACKUP
678 : DVD_READ_ASVS_INFO;
679 const char *ext = backup ? "BUP" : "IFO";
680
681 ifop->ctx = ctx;
682 ifop->file = DVDOpenFile(ctx, 0, domain);
683 if(!ifop->file) { /* Should really catch any error */
684 Log1(ctx, "Can't open file AUDIO_SV.%s.", ext);
685 free(ifop);
686 return NULL;
687 }
688
689
690 if (ifoRead_ASVS(&ifop->handle)) {
691 ifop->handle.ifo_format = IFO_AUDIO;
692 return &ifop->handle;
693 }
694
695 Log1(ctx, "ifoOpenASVS(): Invalid ASVS IFO (AUDIO_SV.%s).", ext);
696 ifoClose(&ifop->handle);
697 }
698 return NULL;
699
700
701 }
702
703
704 ifo_handle_t * ifoOpenSAMG(dvd_reader_t *ctx) {
705
706 if ( ctx->dvd_type != DVD_A )
707 {
708 Log1(ctx, "the SAMG IFO is exclusive to DVD-Audio Discs");
709 return NULL;
710 }
711
712 struct ifo_handle_private_s *ifop;
713
714 for(int backup = ifoGetBupFlag(ctx, 0); backup <= 1; backup++)
715 {
716 ifop = calloc(1, sizeof(*ifop));
717 if(!ifop)
718 return NULL;
719
720 ifop->ctx = ctx;
721 ifop->file = DVDOpenFile(ctx, 0, DVD_READ_SAMG_INFO);
722 if(!ifop->file) { /* Should really catch any error */
723 Log1(ctx, "Can't open file AUDIO_PP.IFO.");
724 free(ifop);
725 return NULL;
726 }
727
728
729 if (ifoRead_SAMG(&ifop->handle)) {
730 ifop->handle.ifo_format = IFO_AUDIO;
731 return &ifop->handle;
732 }
733
734 Log1(ctx, "ifoOpenSAMG(): Invalid SAMG IFO (AUDIO_PP.IFO).");
735 ifoClose(&ifop->handle);
736 }
737 return NULL;
738 }
739
740 void ifoFree_TT(ifo_handle_t *ifofile){
741 if(!ifofile)
742 return;
743
744 if (ifofile->atsi_title_table) {
745 for (int j=0;j<ifofile->atsi_title_table->nr_titles;j++){
746 free((ifofile->atsi_title_table->atsi_title_row_tables+j)->atsi_track_pointer_rows);
747 free((ifofile->atsi_title_table->atsi_title_row_tables+j)->atsi_track_timestamp_rows);
748 free((ifofile->atsi_title_table->atsi_title_row_tables+j)->ats_pg_asv_pbi_srp);
749 free((ifofile->atsi_title_table->atsi_title_row_tables+j)->dlist);
750 }
751 free(ifofile->atsi_title_table->atsi_title_row_tables);
752 free(ifofile->atsi_title_table->atsi_index_rows);
753 free(ifofile->atsi_title_table);
754 ifofile->atsi_title_table= NULL;
755 }
756 }
757
758 DVDREAD_API void ifoFree_PGC_GI(ifo_handle_t *ifofile) {
759 if(!ifofile || !ifofile->pgc_gi)
760 return;
761
762 for(int k = 0; k < ifofile->pgc_gi->nr_of_programs; k++){
763 free(ifofile->pgc_gi->pgi[k].map.time_infos);
764 free(ifofile->pgc_gi->pgi[k].map.vobu_infos);
765 }
766 free(ifofile->pgc_gi->program_offsets);
767 free(ifofile->pgc_gi->pgi);
768 free(ifofile->pgc_gi);
769 ifofile->pgc_gi= NULL;
770 }
771
772 DVDREAD_API void ifoFree_UD_PGCIT(ifo_handle_t *ifofile) {
773 for ( int j = 0; j < ifofile->ud_pgcit->total_nr_of_programs ; j++ ) {
774 if(ifofile->ud_pgcit->m_c_gi[j].c_epi_n > 0)
775 free(ifofile->ud_pgcit->m_c_gi[j].m_c_epi);
776 }
777
778 free(ifofile->ud_pgcit->m_c_gi);
779 free(ifofile->ud_pgcit->ci_offsets);
780 free(ifofile->ud_pgcit->ud_pgci_items);
781 free(ifofile->ud_pgcit);
782 ifofile->ud_pgcit = NULL;
783 }
784
785 void ifoClose(ifo_handle_t *ifofile) {
786 if(!ifofile)
787 return;
788
789 switch(ifofile->ifo_format) {
790 case IFO_VIDEO:
791 ifoFree_VOBU_ADMAP(ifofile);
792 ifoFree_TITLE_VOBU_ADMAP(ifofile);
793 ifoFree_C_ADT(ifofile);
794 ifoFree_TITLE_C_ADT(ifofile);
795 ifoFree_TXTDT_MGI(ifofile);
796 ifoFree_VTS_ATRT(ifofile);
797 ifoFree_PTL_MAIT(ifofile);
798 ifoFree_PGCI_UT(ifofile);
799 ifoFree_TT_SRPT(ifofile);
800 ifoFree_FP_PGC(ifofile);
801 ifoFree_PGCIT(ifofile);
802 ifoFree_VTS_PTT_SRPT(ifofile);
803 ifoFree_VTS_TMAPT(ifofile);
804
805 if(ifofile->vmgi_mat)
806 free(ifofile->vmgi_mat);
807
808 if(ifofile->vtsi_mat)
809 free(ifofile->vtsi_mat);
810 break;
811 case IFO_AUDIO:
812 if(ifofile->amgi_mat)
813 free(ifofile->amgi_mat);
814
815 if(ifofile->atsi_mat)
816 free(ifofile->atsi_mat);
817
818 if(ifofile->samg_mat){
819 free(ifofile->samg_mat->samg_chapters);
820 free(ifofile->samg_mat);
821 }
822
823 if(ifofile->asvs_mat){
824 free(ifofile->asvs_mat->asv_srpt);
825 free(ifofile->asvs_mat);
826 }
827
828 if(ifofile->info_table_first_sector){
829 free(ifofile->info_table_first_sector->tracks_info);
830 free(ifofile->info_table_first_sector);
831 }
832 if(ifofile->info_table_second_sector){
833 free(ifofile->info_table_second_sector->tracks_info);
834 free(ifofile->info_table_second_sector);
835 }
836 if(ifofile->atsi_title_table)
837 ifoFree_TT(ifofile);
838 break;
839 case IFO_VIDEO_RECORDING:
840 if(ifofile->rtav_vmgi)
841 free(ifofile->rtav_vmgi);
842 if(ifofile->ud_pgcit)
843 ifoFree_UD_PGCIT(ifofile);
844 if(ifofile->pgc_gi)
845 ifoFree_PGC_GI(ifofile);
846 if(ifofile->pgci){
847 free(ifofile->pgci->vob_formats);
848 free(ifofile->pgci);
849 }
850 break;
851 case IFO_UNKNOWN:
852 break;
853 }
854 struct ifo_handle_private_s *ifop = PRIV(ifofile);
855 DVDCloseFile(ifop->file);
856 free(ifop);
857 }
858
859
860 static int ifoRead_VMG(ifo_handle_t *ifofile) {
861 struct ifo_handle_private_s *ifop = PRIV(ifofile);
862 vmgi_mat_t *vmgi_mat;
863
864 vmgi_mat = calloc(1, sizeof(vmgi_mat_t));
865 if(!vmgi_mat)
866 return 0;
867
868 ifofile->vmgi_mat = vmgi_mat;
869 ifofile->ifo_format= IFO_VIDEO;
870
871 if(!DVDFileSeek_(ifop->file, 0)) {
872 free(ifofile->vmgi_mat);
873 ifofile->vmgi_mat = NULL;
874 return 0;
875 }
876
877 if(!DVDReadBytes(ifop->file, vmgi_mat, sizeof(vmgi_mat_t))) {
878 free(ifofile->vmgi_mat);
879 ifofile->vmgi_mat = NULL;
880 return 0;
881 }
882
883 if(strncmp("DVDVIDEO-VMG", vmgi_mat->vmg_identifier, 12) != 0) {
884 free(ifofile->vmgi_mat);
885 ifofile->vmgi_mat = NULL;
886 return 0;
887 }
888
889 B2N_32(vmgi_mat->vmg_last_sector);
890 B2N_32(vmgi_mat->vmgi_last_sector);
891 B2N_32(vmgi_mat->vmg_category);
892 B2N_16(vmgi_mat->vmg_nr_of_volumes);
893 B2N_16(vmgi_mat->vmg_this_volume_nr);
894 B2N_16(vmgi_mat->vmg_nr_of_title_sets);
895 B2N_64(vmgi_mat->vmg_pos_code);
896 B2N_32(vmgi_mat->vmgi_last_byte);
897 B2N_32(vmgi_mat->first_play_pgc);
898 B2N_32(vmgi_mat->vmgm_vobs);
899 B2N_32(vmgi_mat->tt_srpt);
900 B2N_32(vmgi_mat->vmgm_pgci_ut);
901 B2N_32(vmgi_mat->ptl_mait);
902 B2N_32(vmgi_mat->vts_atrt);
903 B2N_32(vmgi_mat->txtdt_mgi);
904 B2N_32(vmgi_mat->vmgm_c_adt);
905 B2N_32(vmgi_mat->vmgm_vobu_admap);
906 read_video_attr(&vmgi_mat->vmgm_video_attr);
907 read_audio_attr(&vmgi_mat->vmgm_audio_attr);
908 read_subp_attr(&vmgi_mat->vmgm_subp_attr);
909
910
911 CHECK_ZERO(vmgi_mat->zero_1);
912 CHECK_ZERO(vmgi_mat->zero_2);
913 /* DVDs created by VDR-to-DVD device LG RC590M violate the following check with
914 * vmgi_mat->zero_3 = 0x00000000010000000000000000000000000000. */
915 CHECK_ZERO(vmgi_mat->zero_3);
916 CHECK_ZERO(vmgi_mat->zero_4);
917 CHECK_ZERO(vmgi_mat->zero_5);
918 CHECK_ZERO(vmgi_mat->zero_6);
919 CHECK_ZERO(vmgi_mat->zero_7);
920 CHECK_ZERO(vmgi_mat->zero_8);
921 CHECK_ZERO(vmgi_mat->zero_9);
922 CHECK_ZERO(vmgi_mat->zero_10);
923 CHECK_VALUE(vmgi_mat->vmg_last_sector != 0);
924 CHECK_VALUE(vmgi_mat->vmgi_last_sector != 0);
925 CHECK_VALUE(vmgi_mat->vmgi_last_sector * 2 <= vmgi_mat->vmg_last_sector);
926 CHECK_VALUE(vmgi_mat->vmgi_last_sector * 2 <= vmgi_mat->vmg_last_sector);
927 CHECK_VALUE(vmgi_mat->vmg_nr_of_volumes != 0);
928 CHECK_VALUE(vmgi_mat->vmg_this_volume_nr != 0);
929 CHECK_VALUE(vmgi_mat->vmg_this_volume_nr <= vmgi_mat->vmg_nr_of_volumes);
930 CHECK_VALUE(vmgi_mat->disc_side == 1 || vmgi_mat->disc_side == 2);
931 CHECK_VALUE(vmgi_mat->vmg_nr_of_title_sets != 0);
932 CHECK_VALUE(vmgi_mat->vmgi_last_byte >= 341);
933 CHECK_VALUE(vmgi_mat->vmgi_last_byte / DVD_BLOCK_LEN <=
934 vmgi_mat->vmgi_last_sector);
935 /* It seems that first_play_pgc is optional. */
936 CHECK_VALUE(vmgi_mat->first_play_pgc < vmgi_mat->vmgi_last_byte);
937 CHECK_VALUE(vmgi_mat->vmgm_vobs == 0 ||
938 (vmgi_mat->vmgm_vobs > vmgi_mat->vmgi_last_sector &&
939 vmgi_mat->vmgm_vobs < vmgi_mat->vmg_last_sector));
940 CHECK_VALUE(vmgi_mat->tt_srpt <= vmgi_mat->vmgi_last_sector);
941 CHECK_VALUE(vmgi_mat->vmgm_pgci_ut <= vmgi_mat->vmgi_last_sector);
942 CHECK_VALUE(vmgi_mat->ptl_mait <= vmgi_mat->vmgi_last_sector);
943 CHECK_VALUE(vmgi_mat->vts_atrt <= vmgi_mat->vmgi_last_sector);
944 CHECK_VALUE(vmgi_mat->txtdt_mgi <= vmgi_mat->vmgi_last_sector);
945 CHECK_VALUE(vmgi_mat->vmgm_c_adt <= vmgi_mat->vmgi_last_sector);
946 CHECK_VALUE(vmgi_mat->vmgm_vobu_admap <= vmgi_mat->vmgi_last_sector);
947
948 CHECK_VALUE(vmgi_mat->nr_of_vmgm_audio_streams <= 1);
949 CHECK_VALUE(vmgi_mat->nr_of_vmgm_subp_streams <= 1);
950
951 return 1;
952 }
953
954 static int ifoRead_SAMG(ifo_handle_t *ifofile) {
955 struct ifo_handle_private_s *ifop = PRIV(ifofile);
956 samg_mat_t *samg_mat;
957
958 samg_mat = calloc(1, sizeof(samg_mat_t));
959 if(!samg_mat)
960 return 0;
961
962
963 ifofile->samg_mat = samg_mat;
964
965 if(!DVDFileSeek_(ifop->file, 0)) {
966 free(ifofile->samg_mat);
967 ifofile->samg_mat = NULL;
968 return 0;
969 }
970
971 if(!DVDReadBytes(ifop->file, samg_mat, SAMG_MAT_SIZE)) {
972 free(ifofile->samg_mat);
973 ifofile->samg_mat = NULL;
974 return 0;
975 }
976
977 if(strncmp("DVDAUDIOSAPP", samg_mat->samg_identifier, 12) != 0) {
978 free(ifofile->samg_mat);
979 ifofile->samg_mat = NULL;
980 return 0;
981 }
982
983 B2N_16(samg_mat->nr_chapters);
984 B2N_16(samg_mat->specification_version);
985
986 samg_mat->samg_chapters = calloc(samg_mat->nr_chapters, sizeof(samg_chapter_t));
987 if(!samg_mat->samg_chapters) {
988 free(ifofile->samg_mat);
989 ifofile->samg_mat = NULL;
990 return 0;
991 }
992
993 if(!DVDReadBytes(ifop->file, samg_mat->samg_chapters,
994 samg_mat->nr_chapters * sizeof(samg_chapter_t))) {
995 free(ifofile->samg_mat->samg_chapters);
996 free(ifofile->samg_mat);
997 ifofile->samg_mat = NULL;
998 return 0;
999 }
1000
1001 for (int i = 0; i < samg_mat->nr_chapters; i++) {
1002 samg_chapter_t *index = &samg_mat->samg_chapters[i];
1003 CHECK_ZERO(index->zero_1);
1004 B2N_32(index->timestamp_pts);
1005 B2N_32(index->chapter_len);
1006 CHECK_ZERO(index->zero_2);
1007 B2N_32(index->start_sector_1);
1008 B2N_32(index->start_sector_2);
1009 B2N_32(index->end_sector);
1010 CHECK_ZERO(index->zero_3);
1011 }
1012
1013 return 1;
1014 }
1015
1016 static int ifoRead_ASVS(ifo_handle_t *ifofile){
1017
1018 struct ifo_handle_private_s *ifop = PRIV(ifofile);
1019 asvs_mat_t *asvs_mat;
1020
1021 asvs_mat = calloc(1, sizeof(asvs_mat_t));
1022
1023 if(!asvs_mat)
1024 return 0;
1025
1026 ifofile->asvs_mat = asvs_mat;
1027
1028 if(!DVDFileSeek_(ifop->file, 0)) {
1029 free(ifofile->asvs_mat);
1030 ifofile->asvs_mat = NULL;
1031 return 0;
1032 }
1033
1034 if(!DVDReadBytes(ifop->file, asvs_mat, ASVS_MAT_SIZE)) {
1035 free(ifofile->asvs_mat);
1036 ifofile->asvs_mat = NULL;
1037 return 0;
1038 }
1039
1040 if(strncmp("DVDAUDIOASVS", asvs_mat->asvs_identifier, 12) != 0) {
1041 free(ifofile->asvs_mat);
1042 ifofile->asvs_mat = NULL;
1043 return 0;
1044 }
1045
1046 B2N_16(asvs_mat->asvs_nr_of_asvus);
1047 B2N_16(asvs_mat->specification_version);
1048 B2N_16(asvs_mat->p_vobs_ea);
1049
1050 /* a broken disc could ask for more groups than fit */
1051 if(asvs_mat->asvs_nr_of_asvus > ASVU_GI_MAX_SIZE) {
1052 free(ifofile->asvs_mat);
1053 ifofile->asvs_mat = NULL;
1054 return 0;
1055 }
1056
1057 int total_nr_frames = 0;
1058 for (int i = 0; i < asvs_mat->asvs_nr_of_asvus; i++ ) {
1059 B2N_16(asvs_mat->asvu_gi[i].start_p_vob_number);
1060 B2N_16(asvs_mat->asvu_gi[i].ref_start_sector);
1061 CHECK_ZERO(asvs_mat->asvu_gi[i].zero_1);
1062 total_nr_frames+=asvs_mat->asvu_gi[i].p_vob_ns;
1063 }
1064
1065
1066 asvs_mat->asv_srpt = calloc(total_nr_frames, ASV_SRPT_SIZE);
1067 if(!asvs_mat->asv_srpt) {
1068 free(ifofile->asvs_mat);
1069 ifofile->asvs_mat = NULL;
1070 return 0;
1071 }
1072
1073 if(!DVDReadBytes(ifop->file, asvs_mat->asv_srpt,
1074 total_nr_frames * ASV_SRPT_SIZE )) {
1075 free(asvs_mat->asv_srpt);
1076 free(ifofile->asvs_mat);
1077 ifofile->asvs_mat = NULL;
1078 return 0;
1079 }
1080
1081 for(int i = 0; i < total_nr_frames; i++)
1082 B2N_16(asvs_mat->asv_srpt[i]);
1083
1084
1085 CHECK_VALUE(asvs_mat->specification_version == 0x0012);
1086 CHECK_ZERO(asvs_mat->zero_1);
1087 CHECK_ZERO(asvs_mat->zero_2);
1088 return 1;
1089
1090 }
1091
1092 /* Read the still picture table (ASVS) of an audio title, if present. */
1093 static void ifoRead_TT_stillpics(struct ifo_handle_private_s *ifop,
1094 atsi_title_record_t *index,
1095 uint32_t record_offset) {
1096 int nr_tracks = index->nr_tracks;
1097 ats_pg_asv_pbi_srp_t *ranges;
1098 asv_dlist_t *frames;
1099 int base, max_end = 0, nr_frames;
1100
1101 if(nr_tracks <= 0)
1102 return;
1103
1104 if(!DVDFileSeek_(ifop->file, record_offset + index->ats_asv_pbit_sa
1105 + DVD_BLOCK_LEN))
1106 return;
1107
1108 ranges = calloc(nr_tracks, sizeof(ats_pg_asv_pbi_srp_t));
1109 if(!ranges)
1110 return;
1111
1112 if(!DVDReadBytes(ifop->file, ranges, nr_tracks * sizeof(ats_pg_asv_pbi_srp_t))) {
1113 free(ranges);
1114 return;
1115 }
1116
1117 for(int j = 0; j < nr_tracks; j++) {
1118 B2N_16(ranges[j].start_value);
1119 B2N_16(ranges[j].end_value);
1120 if(ranges[j].end_value > max_end)
1121 max_end = ranges[j].end_value;
1122 }
1123 index->ats_pg_asv_pbi_srp = ranges;
1124
1125 /* The still frame records follow the range table; end_values are byte
1126 * offsets into them, so the last referenced record bounds their count. */
1127 base = nr_tracks * ATS_PG_ASV_PBI_SRP_SIZE;
1128 if(max_end < base)
1129 return;
1130 nr_frames = (max_end - base) / ASV_DLIST_SIZE + 1;
1131
1132 frames = calloc(nr_frames, sizeof(asv_dlist_t));
1133 if(!frames)
1134 return;
1135
1136 if(!DVDReadBytes(ifop->file, frames, nr_frames * sizeof(asv_dlist_t))) {
1137 free(frames);
1138 return;
1139 }
1140
1141 for(int j = 0; j < nr_frames; j++) {
1142 B2N_32(frames[j].display_timing);
1143 }
1144
1145 index->dlist = frames;
1146 }
1147
1148 int ifoRead_TT(ifo_handle_t *ifofile) {
1149
1150 struct ifo_handle_private_s *ifop = PRIV(ifofile);
1151 atsi_title_table_t *atsi_title_table;
1152
1153 atsi_title_table= calloc(1, sizeof(atsi_title_table_t));
1154
1155 if(!atsi_title_table)
1156 return 0;
1157
1158 ifofile->atsi_title_table = atsi_title_table;
1159
1160 if(!DVDFileSeek_(ifop->file, DVD_BLOCK_LEN)) {
1161 free(ifofile->atsi_title_table);
1162 ifofile->atsi_title_table = NULL;
1163 return 0;
1164 }
1165
1166 if(!DVDReadBytes(ifop->file, atsi_title_table, ATSI_TITLE_TABLE_SIZE)) {
1167 free(ifofile->atsi_title_table);
1168 ifofile->atsi_title_table = NULL;
1169 return 0;
1170 }
1171
1172 B2N_16(atsi_title_table->nr_titles);
1173 B2N_32(atsi_title_table->last_byte_address);
1174
1175 atsi_title_table->atsi_index_rows = calloc(atsi_title_table->nr_titles,sizeof(atsi_title_index_t));
1176 if(!atsi_title_table->atsi_index_rows) {
1177 free(ifofile->atsi_title_table);
1178 ifofile->atsi_title_table = NULL;
1179 return 0;
1180 }
1181
1182
1183 if(!DVDReadBytes(ifop->file, atsi_title_table->atsi_index_rows,
1184 atsi_title_table->nr_titles * sizeof(atsi_title_index_t))) {
1185 free(ifofile->atsi_title_table->atsi_index_rows);
1186 free(ifofile->atsi_title_table);
1187 ifofile->atsi_title_table = NULL;
1188 return 0;
1189 }
1190
1191 for(int i=0; i<atsi_title_table->nr_titles; i++)
1192 B2N_32(atsi_title_table->atsi_index_rows[i].offset_record_table);
1193
1194
1195 atsi_title_table->atsi_title_row_tables = calloc(atsi_title_table->nr_titles, sizeof(atsi_title_record_t));
1196
1197 if(!atsi_title_table->atsi_title_row_tables) {
1198 free(ifofile->atsi_title_table->atsi_index_rows);
1199 free(ifofile->atsi_title_table);
1200 ifofile->atsi_title_table = NULL;
1201 return 0;
1202 }
1203
1204 int i;
1205 for (i=0; i < atsi_title_table->nr_titles; i++){
1206 atsi_title_record_t *index = (atsi_title_table->atsi_title_row_tables + i);
1207
1208 uint32_t record_offset = (atsi_title_table->atsi_index_rows+i)->offset_record_table;
1209 if(!DVDFileSeek_(ifop->file, record_offset + DVD_BLOCK_LEN))
1210 goto fail_audio;
1211
1212 if(!DVDReadBytes(ifop->file, index, ATSI_TITLE_ROW_TABLE_SIZE))
1213 goto fail_audio;
1214
1215 B2N_16(index->start_sector_pointers_table);
1216 B2N_16(index->ats_asv_pbit_sa);
1217 B2N_32(index->length_pts);
1218 int nr_tracks=index->nr_tracks;
1219 int nr_pointer_records = index->nr_pointer_records;
1220
1221 index->atsi_track_timestamp_rows = calloc(nr_tracks, sizeof(atsi_track_timestamp_t));
1222 if(!index->atsi_track_timestamp_rows)
1223 goto fail_audio;
1224
1225 index->atsi_track_pointer_rows = calloc(nr_pointer_records, sizeof(atsi_track_pointer_t));
1226 if(!index->atsi_track_pointer_rows)
1227 goto fail_audio;
1228
1229 if(!DVDReadBytes(ifop->file, index->atsi_track_timestamp_rows,
1230 nr_tracks * sizeof(atsi_track_timestamp_t))) {
1231 free(index->atsi_track_timestamp_rows);
1232 goto fail_audio;
1233 }
1234
1235 if(!DVDFileSeek_(ifop->file, (atsi_title_table->atsi_index_rows + i)->offset_record_table
1236 + index->start_sector_pointers_table + DVD_BLOCK_LEN)) {
1237 free(index->atsi_track_timestamp_rows);
1238 free(index->atsi_track_pointer_rows);
1239 goto fail_audio;
1240 }
1241
1242 if(!DVDReadBytes(ifop->file, index->atsi_track_pointer_rows,
1243 nr_pointer_records * sizeof(atsi_track_pointer_t))) {
1244 free(index->atsi_track_timestamp_rows);
1245 free(index->atsi_track_pointer_rows);
1246 goto fail_audio;
1247 }
1248
1249 for (int j = 0; j<nr_tracks; j++) {
1250 CHECK_ZERO(index->atsi_track_timestamp_rows[j].zero);
1251 B2N_32(index->atsi_track_timestamp_rows[j].first_pts_of_track);
1252 B2N_32(index->atsi_track_timestamp_rows[j].length_pts_of_track);
1253 }
1254
1255 for (int j = 0; j<nr_pointer_records; j++) {
1256 B2N_32(index->atsi_track_pointer_rows[j].start_sector);
1257 B2N_32(index->atsi_track_pointer_rows[j].end_sector);
1258 }
1259
1260 /* Sanity Check */
1261 CHECK_VALUE( index->start_sector_pointers_table
1262 == ( ATSI_TITLE_ROW_TABLE_SIZE
1263 + index->nr_tracks * ATSI_TRACK_TIMESTAMP_SIZE ) );
1264
1265 if(index->ats_asv_pbit_sa)
1266 ifoRead_TT_stillpics(ifop, index, record_offset);
1267
1268 }
1269 return 1;
1270
1271 fail_audio:
1272 for (int j = 0; j < i; j++){
1273 free((atsi_title_table->atsi_title_row_tables + j)->atsi_track_pointer_rows);
1274 free((atsi_title_table->atsi_title_row_tables + j)->atsi_track_timestamp_rows);
1275 free((atsi_title_table->atsi_title_row_tables + j)->ats_pg_asv_pbi_srp);
1276 free((atsi_title_table->atsi_title_row_tables + j)->dlist);
1277 }
1278 free(atsi_title_table->atsi_title_row_tables);
1279 free(ifofile->atsi_title_table->atsi_index_rows);
1280 free(ifofile->atsi_title_table);
1281 ifofile->atsi_title_table = NULL;
1282 return 0;
1283 }
1284
1285 int ifoRead_TIF(ifo_handle_t *ifofile, int sector_offset) {
1286 /* check early if sector_offset corresponds to one of the tables */
1287 if (sector_offset != 2 && sector_offset != 1)
1288 return 0;
1289
1290 struct ifo_handle_private_s *ifop = PRIV(ifofile);
1291 tracks_info_table_t *tracks_info_table;
1292
1293 tracks_info_table= calloc(1, sizeof(tracks_info_table_t));
1294 if(!tracks_info_table)
1295 return 0;
1296
1297 if(!DVDFileSeek_(ifop->file,DVD_BLOCK_LEN * sector_offset)) {
1298 free(tracks_info_table);
1299 return 0;
1300 }
1301
1302 if(!DVDReadBytes(ifop->file, tracks_info_table,TRACKS_INFO_TABLE_SIZE)) {
1303 free(tracks_info_table);
1304 return 0;
1305 }
1306
1307 B2N_16(tracks_info_table->nr_of_titles);
1308 B2N_16(tracks_info_table->last_byte_in_table);
1309
1310 tracks_info_table->tracks_info = calloc(tracks_info_table->nr_of_titles,sizeof(track_info_t));
1311 if(!tracks_info_table->tracks_info) {
1312 free(tracks_info_table);
1313 return 0;
1314 }
1315
1316 if(!DVDReadBytes(ifop->file, tracks_info_table->tracks_info,
1317 tracks_info_table->nr_of_titles * sizeof(track_info_t))) {
1318 free(tracks_info_table->tracks_info);
1319 free(tracks_info_table);
1320 return 0;
1321 }
1322
1323 /* the second table is an audio_ts only table, the first is audio_ts, video_ts, strangly the nr_titles in second table doesnt match up with the true nr_titles for this table. Need to subtract video titles*/
1324 for(int i=0; i<tracks_info_table->nr_of_titles; i++) {
1325 if(tracks_info_table->tracks_info[i].type_and_rank==0 && sector_offset == 2) {
1326 track_info_t *resized = realloc(tracks_info_table->tracks_info, i * sizeof(track_info_t));
1327 if(resized || i == 0)
1328 tracks_info_table->tracks_info = resized;
1329 tracks_info_table->nr_of_titles = i;
1330 break;
1331 }
1332 B2N_32(tracks_info_table->tracks_info[i].len_audio_zone_pts);
1333 CHECK_ZERO(tracks_info_table->tracks_info[i].zero_1);
1334 B2N_32(tracks_info_table->tracks_info[i].ts_pointer_relative_sector);
1335 }
1336
1337 /* sanity check */
1338 /* sector table two's size and end byte are always the same as sector one's table
1339 * even though it will be equal to or smaller, since it only lists audio titles
1340 * sector two's table has been resized in the ifo to match it's true size */
1341 if(sector_offset == 1)
1342 CHECK_VALUE( (TRACKS_INFO_TABLE_SIZE +
1343 tracks_info_table->nr_of_titles * TRACK_INFO_SIZE -
1344 1) == tracks_info_table->last_byte_in_table );
1345
1346 switch(sector_offset) {
1347 case 1:
1348 ifofile->info_table_first_sector = tracks_info_table;
1349 break;
1350 case 2:
1351 ifofile->info_table_second_sector = tracks_info_table;
1352 break;
1353 }
1354
1355 return 1;
1356 }
1357
1358
1359 static int ifoRead_AMG(ifo_handle_t *ifofile) {
1360 struct ifo_handle_private_s *ifop = PRIV(ifofile);
1361 amgi_mat_t *amgi_mat;
1362
1363 amgi_mat = calloc(1, sizeof(amgi_mat_t));
1364 if(!amgi_mat)
1365 return 0;
1366
1367 ifofile->amgi_mat = amgi_mat;
1368
1369 if(!DVDFileSeek_(ifop->file, 0)) {
1370 free(ifofile->amgi_mat);
1371 ifofile->amgi_mat = NULL;
1372 return 0;
1373 }
1374
1375 if(!DVDReadBytes(ifop->file, amgi_mat, sizeof(amgi_mat_t))) {
1376 free(ifofile->amgi_mat);
1377 ifofile->amgi_mat = NULL;
1378 return 0;
1379 }
1380
1381 if(strncmp("DVDAUDIO-AMG", amgi_mat->amg_identifier, 12) != 0) {
1382 free(ifofile->amgi_mat);
1383 ifofile->amgi_mat = NULL;
1384 return 0;
1385 }
1386
1387 /* Should be some checks and conversions here, will see about this later*/
1388 B2N_32(amgi_mat->audio_sv_ifo_relative_p);
1389 B2N_32(amgi_mat->amg_end_byte_address);
1390 B2N_32(amgi_mat->amg_start_sector);
1391 B2N_32(amgi_mat->amgi_last_sector);
1392 B2N_16(amgi_mat->amg_nr_of_volumes);
1393 B2N_16(amgi_mat->specification_version);
1394 B2N_16(amgi_mat->amg_this_volume_nr);
1395 B2N_16(amgi_mat->amg_nr_of_zones);
1396 CHECK_ZERO(amgi_mat->zero_1);
1397 CHECK_ZERO(amgi_mat->zero_2);
1398 CHECK_ZERO(amgi_mat->zero_3);
1399 CHECK_ZERO(amgi_mat->zero_4);
1400 CHECK_ZERO(amgi_mat->zero_5);
1401 CHECK_ZERO(amgi_mat->zero_6);
1402 CHECK_ZERO(amgi_mat->zero_7);
1403 CHECK_ZERO(amgi_mat->zero_8);
1404 CHECK_ZERO(amgi_mat->zero_9);
1405 CHECK_ZERO(amgi_mat->zero_10);
1406 CHECK_VALUE(amgi_mat->specification_version == 0x0012);
1407
1408 return 1;
1409 }
1410
1411 static int ifoRead_RTAV_VMGI(ifo_handle_t *ifofile) {
1412 struct ifo_handle_private_s *ifop = PRIV(ifofile);
1413 rtav_vmgi_t *rtav_vmgi;
1414
1415 rtav_vmgi = calloc(1, sizeof(rtav_vmgi_t));
1416 if(!rtav_vmgi)
1417 return 0;
1418
1419 ifofile->rtav_vmgi = rtav_vmgi;
1420
1421 if(!DVDFileSeek_(ifop->file, 0)) {
1422 free(ifofile->rtav_vmgi);
1423 ifofile->rtav_vmgi= NULL;
1424 return 0;
1425 }
1426
1427 if(!(DVDReadBytes(ifop->file, rtav_vmgi, sizeof(rtav_vmgi_t)))) {
1428 free(ifofile->rtav_vmgi);
1429 ifofile->rtav_vmgi= NULL;
1430 return 0;
1431 }
1432
1433 if(strncmp("DVD_RTR_VMG0", rtav_vmgi->id, 12) != 0) {
1434 free(ifofile->rtav_vmgi);
1435 ifofile->rtav_vmgi = NULL;
1436 return 0;
1437 }
1438
1439 B2N_16(ifofile->rtav_vmgi->version);
1440 B2N_16(ifofile->rtav_vmgi->still_tm);
1441 B2N_32(ifofile->rtav_vmgi->vmg_ea);
1442 B2N_32(ifofile->rtav_vmgi->vmgi_ea);
1443 B2N_32(ifofile->rtav_vmgi->org_pgci_sa);
1444 B2N_32(ifofile->rtav_vmgi->ud_pgcit_sa);
1445 B2N_32(ifofile->rtav_vmgi->org_pgci_ea);
1446 B2N_32(ifofile->rtav_vmgi->info_308_sa);
1447 B2N_32(ifofile->rtav_vmgi->info_312_sa);
1448 B2N_32(ifofile->rtav_vmgi->info_316_sa);
1449 B2N_32(ifofile->rtav_vmgi->txtdt_mg_sa);
1450 B2N_32(ifofile->rtav_vmgi->mnfit_sa);
1451
1452
1453 CHECK_ZERO(ifofile->rtav_vmgi->zero_16);
1454 CHECK_ZERO(ifofile->rtav_vmgi->zero_34);
1455 CHECK_ZERO(ifofile->rtav_vmgi->zero_226);
1456 CHECK_ZERO(ifofile->rtav_vmgi->zero_280);
1457 CHECK_ZERO(ifofile->rtav_vmgi->zero_320);
1458 CHECK_ZERO(ifofile->rtav_vmgi->zero_360);
1459 return 1;
1460
1461 }
1462
1463 int ifoRead_PGCI(ifo_handle_t *ifofile) {
1464 struct ifo_handle_private_s *ifop = PRIV(ifofile);
1465 pgci_t *pgci;
1466
1467 pgci = malloc(sizeof(pgci_t));
1468
1469 if(!pgci)
1470 return 0;
1471
1472 ifofile->pgci= pgci;
1473
1474 if(!DVDFileSeek_(ifop->file, ifofile->rtav_vmgi->org_pgci_sa)) {
1475 free(ifofile->pgci);
1476 ifofile->pgci = NULL;
1477 return 0;
1478 }
1479
1480 if(!DVDReadBytes(ifop->file, pgci, PGCI_SIZE)) {
1481 free(ifofile->pgci);
1482 ifofile->pgci = NULL;
1483 return 0;
1484 }
1485
1486 pgci->vob_formats = calloc(ifofile->pgci->nr_of_vob_formats, VOB_FORMAT_SIZE);
1487
1488 if(!pgci->vob_formats) {
1489 free(ifofile->pgci->vob_formats);
1490 free(ifofile->pgci);
1491 ifofile->pgci = NULL;
1492 return 0;
1493 }
1494
1495 if(!DVDReadBytes(ifop->file, pgci->vob_formats, pgci->nr_of_vob_formats * VOB_FORMAT_SIZE)) {
1496 free(ifofile->pgci->vob_formats);
1497 free(ifofile->pgci);
1498 ifofile->pgci = NULL;
1499 return 0;
1500 }
1501
1502 for (int i = 0; i < pgci->nr_of_vob_formats ; i++)
1503 B2N_16(pgci->vob_formats[i].video_attr);
1504
1505 B2N_32(pgci->len_org_pgci);
1506 CHECK_ZERO(pgci->zero1);
1507 return 1;
1508
1509 }
1510
1511 int ifoRead_PGC_GI(ifo_handle_t *ifofile) {
1512 struct ifo_handle_private_s *ifop = PRIV(ifofile);
1513 pgc_gi_t *pgc_gi;
1514
1515 pgc_gi = malloc(sizeof(pgc_gi_t));
1516
1517 if(!pgc_gi)
1518 return 0;
1519
1520 ifofile->pgc_gi = pgc_gi;
1521
1522 /* the pgc_gi directly follows pgci */
1523 uint32_t pgc_gi_sa = ifofile->rtav_vmgi->org_pgci_sa
1524 + PGCI_SIZE
1525 + ifofile->pgci->nr_of_vob_formats * VOB_FORMAT_SIZE;
1526
1527 if(!DVDFileSeek_(ifop->file, pgc_gi_sa )) {
1528 free(ifofile->pgc_gi);
1529 ifofile->pgc_gi = NULL;
1530 return 0;
1531 }
1532
1533 if(!DVDReadBytes(ifop->file, pgc_gi, PGC_GI_SIZE)) {
1534 free(ifofile->pgc_gi);
1535 ifofile->pgc_gi= NULL;
1536 return 0;
1537 }
1538
1539 B2N_16(pgc_gi->nr_of_programs);
1540
1541 /* there is a case where the original program info table is undefined or overwritten, regardless this should not be the default table used when we are navigating the disc */
1542 if ( pgc_gi->nr_of_programs == 0 ) {
1543 free(ifofile->pgc_gi);
1544 ifofile->pgc_gi= NULL;
1545 return 1;
1546 }
1547
1548 pgc_gi->program_offsets = calloc(pgc_gi->nr_of_programs, sizeof(uint32_t));
1549
1550 if(!pgc_gi->program_offsets) {
1551 free(ifofile->pgc_gi);
1552 ifofile->pgc_gi= NULL;
1553 return 0;
1554 }
1555
1556 if(!DVDReadBytes(ifop->file, pgc_gi->program_offsets, pgc_gi->nr_of_programs * sizeof(uint32_t))) {
1557 free(ifofile->pgc_gi->program_offsets);
1558 free(ifofile->pgc_gi);
1559 ifofile->pgc_gi= NULL;
1560 return 0;
1561 }
1562
1563 pgc_gi->pgi= calloc(pgc_gi->nr_of_programs, sizeof(pgi_t));
1564
1565 if(!pgc_gi->pgi) {
1566 free(ifofile->pgc_gi->program_offsets);
1567 free(ifofile->pgc_gi);
1568 ifofile->pgc_gi= NULL;
1569 return 0;
1570 }
1571
1572 int i, j;
1573 for(i = 0; i < pgc_gi->nr_of_programs; i++) {
1574 B2N_32(pgc_gi->program_offsets[i]);
1575
1576 if(!DVDFileSeek_(ifop->file, ifofile->rtav_vmgi->org_pgci_sa
1577 + pgc_gi->program_offsets[i]))
1578 goto fail3;
1579
1580 if(!DVDReadBytes(ifop->file, &pgc_gi->pgi[i].header, VVOB_SIZE)) {
1581 goto fail3;
1582 }
1583
1584 B2N_16(pgc_gi->pgi[i].header.vob_attr);
1585 B2N_16(pgc_gi->pgi[i].header.vob_v_e_ptm.ptm_extra);
1586 B2N_16(pgc_gi->pgi[i].header.vob_v_s_ptm.ptm_extra);
1587 B2N_32(pgc_gi->pgi[i].header.vob_v_e_ptm.ptm);
1588 B2N_32(pgc_gi->pgi[i].header.vob_v_s_ptm.ptm);
1589
1590
1591
1592 /* this entry is supposedly optional, and will not exist without this flag */
1593 if(pgc_gi->pgi[i].header.vob_attr & 0x80) {
1594 if(!DVDReadBytes(ifop->file, &pgc_gi->pgi[i].adj_info, ADJ_VOB_SIZE))
1595 goto fail3;
1596 }
1597
1598 /* since we're reading a struct that isn't packed , we have to
1599 * skip the padding this way */
1600 uint16_t pad2;
1601 if(!DVDReadBytes(ifop->file, &pad2, sizeof(pad2)))
1602 goto fail3;
1603
1604 /* header of this table */
1605 if(!DVDReadBytes(ifop->file, &pgc_gi->pgi[i].map, VOBU_MAP_SIZE))
1606 goto fail3;
1607
1608 B2N_16(pgc_gi->pgi[i].map.nr_of_time_info);
1609 B2N_16(pgc_gi->pgi[i].map.nr_of_vobu_info);
1610 B2N_16(pgc_gi->pgi[i].map.time_offset);
1611 B2N_32(pgc_gi->pgi[i].map.vob_offset);
1612
1613 if(pgc_gi->pgi[i].map.nr_of_time_info > 0) {
1614 pgc_gi->pgi[i].map.time_infos = calloc(pgc_gi->pgi[i].map.nr_of_time_info,
1615 TIME_INFO_SIZE);
1616 if(!pgc_gi->pgi[i].map.time_infos)
1617 goto fail3;
1618
1619 if(!DVDReadBytes(ifop->file, pgc_gi->pgi[i].map.time_infos,
1620 pgc_gi->pgi[i].map.nr_of_time_info * TIME_INFO_SIZE)) {
1621 free(pgc_gi->pgi[i].map.time_infos);
1622 goto fail3;
1623 }
1624
1625 for(int j = 0; j < pgc_gi->pgi[i].map.nr_of_time_info; j++) {
1626 B2N_16(pgc_gi->pgi[i].map.time_infos[j].vobu_entn);
1627 B2N_32(pgc_gi->pgi[i].map.time_infos[j].vobu_adr);
1628 }
1629
1630 } else {
1631 pgc_gi->pgi[i].map.time_infos = NULL;
1632 }
1633
1634 if(pgc_gi->pgi[i].map.nr_of_vobu_info > 0) {
1635 pgc_gi->pgi[i].map.vobu_infos = calloc(pgc_gi->pgi[i].map.nr_of_vobu_info,
1636 VOBU_INFO_SIZE);
1637 if(!pgc_gi->pgi[i].map.vobu_infos) {
1638 free(pgc_gi->pgi[i].map.time_infos);
1639 goto fail3;
1640 }
1641
1642 if(!DVDReadBytes(ifop->file, pgc_gi->pgi[i].map.vobu_infos,
1643 pgc_gi->pgi[i].map.nr_of_vobu_info * VOBU_INFO_SIZE)) {
1644 free(pgc_gi->pgi[i].map.time_infos);
1645 free(pgc_gi->pgi[i].map.vobu_infos);
1646 goto fail3;
1647 }
1648 for(j = 0; j<pgc_gi->pgi[i].map.nr_of_vobu_info; j++)
1649 B2N_16(pgc_gi->pgi[i].map.vobu_infos[j].vobu_size);
1650
1651 } else {
1652 pgc_gi->pgi[i].map.vobu_infos = NULL;
1653 }
1654 }
1655
1656 return 1;
1657
1658 fail3:
1659 for(int k = 0; k < i; k++){
1660 free(ifofile->pgc_gi->pgi[k].map.time_infos);
1661 free(ifofile->pgc_gi->pgi[k].map.vobu_infos);
1662 }
1663 free(ifofile->pgc_gi->program_offsets);
1664 free(ifofile->pgc_gi->pgi);
1665 free(ifofile->pgc_gi);
1666 ifofile->pgc_gi= NULL;
1667 return 0;
1668 }
1669
1670 int ifoRead_UD_PGCIT(ifo_handle_t *ifofile) {
1671 struct ifo_handle_private_s *ifop = PRIV(ifofile);
1672 ud_pgcit_t *ud_pgcit;
1673
1674 ud_pgcit = malloc(sizeof(ud_pgcit_t));
1675
1676 if(!ud_pgcit)
1677 return 0;
1678
1679 ifofile->ud_pgcit = ud_pgcit;
1680
1681 if(!DVDFileSeek_(ifop->file, ifofile->rtav_vmgi->ud_pgcit_sa)) {
1682 free(ifofile->ud_pgcit);
1683 ifofile->ud_pgcit = NULL;
1684 return 0;
1685 }
1686
1687 if(!DVDReadBytes(ifop->file, ud_pgcit, UD_PGCIT_SIZE)) {
1688 free(ifofile->ud_pgcit);
1689 ifofile->ud_pgcit = NULL;
1690 return 0;
1691 }
1692
1693 B2N_16(ud_pgcit->total_nr_of_programs);
1694
1695 ud_pgcit->ud_pgci_items = calloc(ud_pgcit->nr_of_pgci, UD_PGCI_SIZE);
1696
1697 if(!ud_pgcit->ud_pgci_items) {
1698 free(ifofile->ud_pgcit);
1699 ifofile->ud_pgcit = NULL;
1700 return 0;
1701 }
1702
1703 if(!DVDReadBytes(ifop->file, ud_pgcit->ud_pgci_items, ud_pgcit->nr_of_pgci * UD_PGCI_SIZE)) {
1704 free(ud_pgcit->ud_pgci_items);
1705 free(ifofile->ud_pgcit);
1706 ifofile->ud_pgcit = NULL;
1707 return 0;
1708 }
1709
1710 for(int i = 0; i < ud_pgcit->nr_of_pgci; i++) {
1711 B2N_16(ud_pgcit->ud_pgci_items[i].nr_of_programs);
1712 B2N_16(ud_pgcit->ud_pgci_items[i].prog_set_id);
1713 B2N_16(ud_pgcit->ud_pgci_items[i].first_prog_id);
1714 }
1715
1716 ud_pgcit->ci_offsets = calloc(ud_pgcit->total_nr_of_programs, CI_OFFSET_SIZE);
1717
1718 if(!ud_pgcit->ci_offsets) {
1719 free(ud_pgcit->ud_pgci_items);
1720 free(ifofile->ud_pgcit);
1721 ifofile->ud_pgcit = NULL;
1722 return 0;
1723 }
1724
1725 if(!DVDReadBytes(ifop->file, ud_pgcit->ci_offsets,
1726 ud_pgcit->total_nr_of_programs * CI_OFFSET_SIZE)) {
1727 free(ud_pgcit->ci_offsets);
1728 free(ud_pgcit->ud_pgci_items);
1729 free(ifofile->ud_pgcit);
1730 ifofile->ud_pgcit = NULL;
1731 return 0;
1732 }
1733
1734 ud_pgcit->m_c_gi = calloc(ud_pgcit->total_nr_of_programs, sizeof(m_c_gi_t) );
1735 if(!ud_pgcit->m_c_gi) {
1736 free(ud_pgcit->ci_offsets);
1737 free(ud_pgcit->ud_pgci_items);
1738 free(ifofile->ud_pgcit);
1739 ifofile->ud_pgcit = NULL;
1740 return 0;
1741 }
1742
1743 int i;
1744 for ( i = 0; i < ud_pgcit->total_nr_of_programs; i++ ) {
1745 B2N_32(ud_pgcit->ci_offsets[i]);
1746
1747 if(!DVDFileSeek_(ifop->file,
1748 ifofile->rtav_vmgi->ud_pgcit_sa + ud_pgcit->ci_offsets[i])) {
1749 for (int j = 0; j < i; j++) {
1750 if(ud_pgcit->m_c_gi[j].c_epi_n > 0)
1751 free(ud_pgcit->m_c_gi[j].m_c_epi);
1752 }
1753 free(ud_pgcit->m_c_gi);
1754 free(ifofile->ud_pgcit->ci_offsets);
1755 free(ud_pgcit->ud_pgci_items);
1756 free(ifofile->ud_pgcit);
1757 ifofile->ud_pgcit = NULL;
1758 return 0;
1759 }
1760
1761 if(!DVDReadBytes(ifop->file, &ud_pgcit->m_c_gi[i], M_C_GI_SIZE)) {
1762 for (int j = 0; j < i; j++) {
1763 if(ud_pgcit->m_c_gi[j].c_epi_n > 0)
1764 free(ud_pgcit->m_c_gi[j].m_c_epi);
1765 }
1766 free(ud_pgcit->m_c_gi);
1767 free(ud_pgcit->ci_offsets);
1768 free(ud_pgcit->ud_pgci_items);
1769 free(ifofile->ud_pgcit);
1770 ifofile->ud_pgcit = NULL;
1771 return 0;
1772 }
1773
1774 B2N_16(ud_pgcit->m_c_gi[i].m_vobi_srpn);
1775
1776 B2N_32(ud_pgcit->m_c_gi[i].c_v_s_ptm.ptm);
1777 B2N_16(ud_pgcit->m_c_gi[i].c_v_s_ptm.ptm_extra);
1778
1779 B2N_32(ud_pgcit->m_c_gi[i].c_v_e_ptm.ptm);
1780 B2N_16(ud_pgcit->m_c_gi[i].c_v_e_ptm.ptm_extra);
1781
1782 if(ud_pgcit->m_c_gi[i].c_epi_n > 0){
1783 ud_pgcit->m_c_gi[i].m_c_epi = calloc(ud_pgcit->m_c_gi[i].c_epi_n, M_C_EPI_SIZE );
1784
1785 if(!ud_pgcit->m_c_gi[i].m_c_epi)
1786 goto fail4;
1787
1788 if(!DVDReadBytes(ifop->file, ud_pgcit->m_c_gi[i].m_c_epi,
1789 ud_pgcit->m_c_gi[i].c_epi_n * M_C_EPI_SIZE)) {
1790 free(ud_pgcit->m_c_gi[i].m_c_epi);
1791 goto fail4;
1792 }
1793
1794 for( int j = 0; j < ud_pgcit->m_c_gi[i].c_epi_n; j++){
1795 B2N_32(ud_pgcit->m_c_gi[i].m_c_epi[j].ep_ptm.ptm);
1796 B2N_16(ud_pgcit->m_c_gi[i].m_c_epi[j].ep_ptm.ptm_extra);
1797 }
1798
1799 }
1800
1801 }
1802
1803 return 1;
1804
1805 fail4:
1806
1807 for ( int j = 0; j < i; j++ ) {
1808 if(ud_pgcit->m_c_gi[j].c_epi_n > 0)
1809 free(ud_pgcit->m_c_gi[j].m_c_epi);
1810 }
1811
1812 free(ud_pgcit->m_c_gi);
1813 free(ud_pgcit->ci_offsets);
1814 free(ud_pgcit->ud_pgci_items);
1815 free(ifofile->ud_pgcit);
1816 ifofile->ud_pgcit = NULL;
1817 return 0;
1818
1819 }
1820
1821 static int ifoRead_VTS(ifo_handle_t *ifofile) {
1822 struct ifo_handle_private_s *ifop = PRIV(ifofile);
1823 vtsi_mat_t *vtsi_mat;
1824 int i;
1825
1826 vtsi_mat = calloc(1, sizeof(vtsi_mat_t));
1827 if(!vtsi_mat)
1828 return 0;
1829
1830 ifofile->vtsi_mat = vtsi_mat;
1831
1832 if(!DVDFileSeek_(ifop->file, 0)) {
1833 free(ifofile->vtsi_mat);
1834 ifofile->vtsi_mat = NULL;
1835 return 0;
1836 }
1837
1838 if(!(DVDReadBytes(ifop->file, vtsi_mat, sizeof(vtsi_mat_t)))) {
1839 free(ifofile->vtsi_mat);
1840 ifofile->vtsi_mat = NULL;
1841 return 0;
1842 }
1843
1844 if(strncmp("DVDVIDEO-VTS", vtsi_mat->vts_identifier, 12) != 0) {
1845 free(ifofile->vtsi_mat);
1846 ifofile->vtsi_mat = NULL;
1847 return 0;
1848 }
1849
1850 read_video_attr(&vtsi_mat->vtsm_video_attr);
1851 read_video_attr(&vtsi_mat->vts_video_attr);
1852 read_audio_attr(&vtsi_mat->vtsm_audio_attr);
1853 for(i=0; i<8; i++)
1854 read_audio_attr(&vtsi_mat->vts_audio_attr[i]);
1855 read_subp_attr(&vtsi_mat->vtsm_subp_attr);
1856 for(i=0; i<32; i++)
1857 read_subp_attr(&vtsi_mat->vts_subp_attr[i]);
1858 B2N_32(vtsi_mat->vts_last_sector);
1859 B2N_32(vtsi_mat->vtsi_last_sector);
1860 B2N_32(vtsi_mat->vts_category);
1861 B2N_32(vtsi_mat->vtsi_last_byte);
1862 B2N_32(vtsi_mat->vtsm_vobs);
1863 B2N_32(vtsi_mat->vtstt_vobs);
1864 B2N_32(vtsi_mat->vts_ptt_srpt);
1865 B2N_32(vtsi_mat->vts_pgcit);
1866 B2N_32(vtsi_mat->vtsm_pgci_ut);
1867 B2N_32(vtsi_mat->vts_tmapt);
1868 B2N_32(vtsi_mat->vtsm_c_adt);
1869 B2N_32(vtsi_mat->vtsm_vobu_admap);
1870 B2N_32(vtsi_mat->vts_c_adt);
1871 B2N_32(vtsi_mat->vts_vobu_admap);
1872
1873
1874 CHECK_ZERO(vtsi_mat->zero_1);
1875 CHECK_ZERO(vtsi_mat->zero_2);
1876 CHECK_ZERO(vtsi_mat->zero_3);
1877 CHECK_ZERO(vtsi_mat->zero_4);
1878 CHECK_ZERO(vtsi_mat->zero_5);
1879 CHECK_ZERO(vtsi_mat->zero_6);
1880 CHECK_ZERO(vtsi_mat->zero_7);
1881 CHECK_ZERO(vtsi_mat->zero_8);
1882 CHECK_ZERO(vtsi_mat->zero_9);
1883 CHECK_ZERO(vtsi_mat->zero_10);
1884 CHECK_ZERO(vtsi_mat->zero_11);
1885 CHECK_ZERO(vtsi_mat->zero_12);
1886 CHECK_ZERO(vtsi_mat->zero_13);
1887 CHECK_ZERO(vtsi_mat->zero_14);
1888 CHECK_ZERO(vtsi_mat->zero_15);
1889 CHECK_ZERO(vtsi_mat->zero_16);
1890 CHECK_ZERO(vtsi_mat->zero_17);
1891 CHECK_ZERO(vtsi_mat->zero_18);
1892 CHECK_ZERO(vtsi_mat->zero_19);
1893 CHECK_ZERO(vtsi_mat->zero_20);
1894 CHECK_ZERO(vtsi_mat->zero_21);
1895 CHECK_VALUE(vtsi_mat->vtsi_last_sector*2 <= vtsi_mat->vts_last_sector);
1896 CHECK_VALUE(vtsi_mat->vtsi_last_byte/DVD_BLOCK_LEN <= vtsi_mat->vtsi_last_sector);
1897 CHECK_VALUE(vtsi_mat->vtsm_vobs == 0 ||
1898 (vtsi_mat->vtsm_vobs > vtsi_mat->vtsi_last_sector &&
1899 vtsi_mat->vtsm_vobs < vtsi_mat->vts_last_sector));
1900 CHECK_VALUE(vtsi_mat->vtstt_vobs == 0 ||
1901 (vtsi_mat->vtstt_vobs > vtsi_mat->vtsi_last_sector &&
1902 vtsi_mat->vtstt_vobs < vtsi_mat->vts_last_sector));
1903 CHECK_VALUE(vtsi_mat->vts_ptt_srpt <= vtsi_mat->vtsi_last_sector);
1904 CHECK_VALUE(vtsi_mat->vts_pgcit <= vtsi_mat->vtsi_last_sector);
1905 CHECK_VALUE(vtsi_mat->vtsm_pgci_ut <= vtsi_mat->vtsi_last_sector);
1906 CHECK_VALUE(vtsi_mat->vts_tmapt <= vtsi_mat->vtsi_last_sector);
1907 CHECK_VALUE(vtsi_mat->vtsm_c_adt <= vtsi_mat->vtsi_last_sector);
1908 CHECK_VALUE(vtsi_mat->vtsm_vobu_admap <= vtsi_mat->vtsi_last_sector);
1909 CHECK_VALUE(vtsi_mat->vts_c_adt <= vtsi_mat->vtsi_last_sector);
1910 CHECK_VALUE(vtsi_mat->vts_vobu_admap <= vtsi_mat->vtsi_last_sector);
1911
1912 CHECK_VALUE(vtsi_mat->nr_of_vtsm_audio_streams <= 1);
1913 CHECK_VALUE(vtsi_mat->nr_of_vtsm_subp_streams <= 1);
1914
1915 CHECK_VALUE(vtsi_mat->nr_of_vts_audio_streams <= 8);
1916 for(i = vtsi_mat->nr_of_vts_audio_streams; i < 8; i++)
1917 CHECK_ZERO(vtsi_mat->vts_audio_attr[i]);
1918
1919 CHECK_VALUE(vtsi_mat->nr_of_vts_subp_streams <= 32);
1920 for(i = vtsi_mat->nr_of_vts_subp_streams; i < 32; i++)
1921 CHECK_ZERO(vtsi_mat->vts_subp_attr[i]);
1922
1923 for(i = 0; i < 8; i++) {
1924 read_multichannel_ext(&vtsi_mat->vts_mu_audio_attr[i]);
1925 CHECK_ZERO0(vtsi_mat->vts_mu_audio_attr[i].zero1);
1926 CHECK_ZERO0(vtsi_mat->vts_mu_audio_attr[i].zero2);
1927 CHECK_ZERO0(vtsi_mat->vts_mu_audio_attr[i].zero3);
1928 CHECK_ZERO0(vtsi_mat->vts_mu_audio_attr[i].zero4);
1929 CHECK_ZERO0(vtsi_mat->vts_mu_audio_attr[i].zero5);
1930 CHECK_ZERO(vtsi_mat->vts_mu_audio_attr[i].zero6);
1931 }
1932
1933 return 1;
1934 }
1935
1936 static int ifoRead_ATS(ifo_handle_t *ifofile) {
1937 struct ifo_handle_private_s *ifop = PRIV(ifofile);
1938 atsi_mat_t *atsi_mat;
1939
1940 atsi_mat = calloc(1, sizeof(atsi_mat_t));
1941 if(!atsi_mat)
1942 return 0;
1943
1944 ifofile->atsi_mat = atsi_mat;
1945
1946 if(!DVDFileSeek_(ifop->file, 0)) {
1947 free(ifofile->atsi_mat);
1948 ifofile->atsi_mat = NULL;
1949 return 0;
1950 }
1951
1952 if(!(DVDReadBytes(ifop->file, atsi_mat, sizeof(atsi_mat_t)))) {
1953 free(ifofile->atsi_mat);
1954 ifofile->atsi_mat = NULL;
1955 return 0;
1956 }
1957
1958 if(strncmp("DVDAUDIO-ATS", atsi_mat->ats_identifier, 12) != 0) {
1959 free(ifofile->atsi_mat);
1960 ifofile->atsi_mat = NULL;
1961 return 0;
1962 }
1963
1964 for (int i=0; i < DOWNMIX_COEFF_MAX_SIZE; i++) {
1965 CHECK_ZERO(atsi_mat->downmix_coefficients[i].zero_1);
1966 CHECK_ZERO(atsi_mat->downmix_coefficients[i].zero_2);
1967 }
1968
1969
1970 B2N_32(atsi_mat->ats_last_sector);
1971 B2N_32(atsi_mat->atsi_last_sector);
1972 B2N_32(atsi_mat->atst_aobs);
1973 B2N_32(atsi_mat->atsi_last_byte);
1974 B2N_32(atsi_mat->ats_pgci_ut);
1975 B2N_32(atsi_mat->vts_tmapt);
1976 B2N_32(atsi_mat->vtsm_c_adt);
1977 B2N_32(atsi_mat->vtsm_vobu_admap);
1978 B2N_32(atsi_mat->vts_c_adt);
1979 B2N_32(atsi_mat->vts_vobu_admap);
1980
1981
1982 CHECK_ZERO(atsi_mat->zero_1);
1983 CHECK_ZERO(atsi_mat->zero_2);
1984 CHECK_ZERO(atsi_mat->zero_3);
1985 CHECK_ZERO(atsi_mat->zero_4);
1986
1987 return 1;
1988 }
1989
1990 static int ifoRead_PGC_COMMAND_TBL(ifo_handle_t *ifofile,
1991 pgc_command_tbl_t *cmd_tbl,
1992 unsigned int offset) {
1993 struct ifo_handle_private_s *ifop = PRIV(ifofile);
1994 if(!DVDFileSeek_(ifop->file, offset))
1995 return 0;
1996
1997 if(!(DVDReadBytes(ifop->file, cmd_tbl, PGC_COMMAND_TBL_SIZE)))
1998 return 0;
1999
2000 B2N_16(cmd_tbl->nr_of_pre);
2001 B2N_16(cmd_tbl->nr_of_post);
2002 B2N_16(cmd_tbl->nr_of_cell);
2003 B2N_16(cmd_tbl->last_byte);
2004
2005 uint32_t nr_cmds = (uint32_t)cmd_tbl->nr_of_pre
2006 + (uint32_t)cmd_tbl->nr_of_post
2007 + (uint32_t)cmd_tbl->nr_of_cell;
2008
2009 CHECK_VALUE(nr_cmds <= 255);
2010 CHECK_VALUE(nr_cmds * COMMAND_DATA_SIZE + PGC_COMMAND_TBL_SIZE
2011 <= (uint32_t)cmd_tbl->last_byte + 1U);
2012
2013 if(cmd_tbl->nr_of_pre != 0) {
2014 unsigned int pre_cmds_size = cmd_tbl->nr_of_pre * COMMAND_DATA_SIZE;
2015 cmd_tbl->pre_cmds = malloc(pre_cmds_size);
2016 if(!cmd_tbl->pre_cmds)
2017 return 0;
2018
2019 if(!(DVDReadBytes(ifop->file, cmd_tbl->pre_cmds, pre_cmds_size))) {
2020 free(cmd_tbl->pre_cmds);
2021 return 0;
2022 }
2023 }
2024
2025 if(cmd_tbl->nr_of_post != 0) {
2026 unsigned int post_cmds_size = cmd_tbl->nr_of_post * COMMAND_DATA_SIZE;
2027 cmd_tbl->post_cmds = malloc(post_cmds_size);
2028 if(!cmd_tbl->post_cmds) {
2029 if(cmd_tbl->pre_cmds)
2030 free(cmd_tbl->pre_cmds);
2031 return 0;
2032 }
2033 if(!(DVDReadBytes(ifop->file, cmd_tbl->post_cmds, post_cmds_size))) {
2034 if(cmd_tbl->pre_cmds)
2035 free(cmd_tbl->pre_cmds);
2036 free(cmd_tbl->post_cmds);
2037 return 0;
2038 }
2039 }
2040
2041 if(cmd_tbl->nr_of_cell != 0) {
2042 unsigned int cell_cmds_size = cmd_tbl->nr_of_cell * COMMAND_DATA_SIZE;
2043 cmd_tbl->cell_cmds = malloc(cell_cmds_size);
2044 if(!cmd_tbl->cell_cmds) {
2045 if(cmd_tbl->pre_cmds)
2046 free(cmd_tbl->pre_cmds);
2047 if(cmd_tbl->post_cmds)
2048 free(cmd_tbl->post_cmds);
2049 return 0;
2050 }
2051 if(!(DVDReadBytes(ifop->file, cmd_tbl->cell_cmds, cell_cmds_size))) {
2052 if(cmd_tbl->pre_cmds)
2053 free(cmd_tbl->pre_cmds);
2054 if(cmd_tbl->post_cmds)
2055 free(cmd_tbl->post_cmds);
2056 free(cmd_tbl->cell_cmds);
2057 return 0;
2058 }
2059 }
2060
2061 /*
2062 * Make a run over all the commands and see that we can interpret them all?
2063 */
2064 return 1;
2065 }
2066
2067
2068 static void ifoFree_PGC_COMMAND_TBL(pgc_command_tbl_t *cmd_tbl) {
2069 if(cmd_tbl) {
2070 if(cmd_tbl->nr_of_pre && cmd_tbl->pre_cmds)
2071 free(cmd_tbl->pre_cmds);
2072 if(cmd_tbl->nr_of_post && cmd_tbl->post_cmds)
2073 free(cmd_tbl->post_cmds);
2074 if(cmd_tbl->nr_of_cell && cmd_tbl->cell_cmds)
2075 free(cmd_tbl->cell_cmds);
2076 free(cmd_tbl);
2077 }
2078 }
2079
2080 static int ifoRead_PGC_PROGRAM_MAP(ifo_handle_t *ifofile,
2081 pgc_program_map_t *program_map,
2082 unsigned int nr, unsigned int offset) {
2083 struct ifo_handle_private_s *ifop = PRIV(ifofile);
2084 unsigned int size = nr * sizeof(pgc_program_map_t);
2085
2086 if(!DVDFileSeek_(ifop->file, offset))
2087 return 0;
2088
2089 if(!(DVDReadBytes(ifop->file, program_map, size)))
2090 return 0;
2091
2092 return 1;
2093 }
2094
2095 static int ifoRead_CELL_PLAYBACK_TBL(ifo_handle_t *ifofile,
2096 cell_playback_t *cell_playback,
2097 unsigned int nr, unsigned int offset) {
2098 struct ifo_handle_private_s *ifop = PRIV(ifofile);
2099 unsigned int i;
2100 unsigned int size = nr * sizeof(cell_playback_t);
2101
2102 if(!DVDFileSeek_(ifop->file, offset))
2103 return 0;
2104
2105 if(!(DVDReadBytes(ifop->file, cell_playback, size)))
2106 return 0;
2107
2108 for(i = 0; i < nr; i++) {
2109 read_cell_playback(&cell_playback[i]);
2110 /* Changed < to <= because this was false in the movie 'Pi'. */
2111 CHECK_VALUE(cell_playback[i].last_vobu_start_sector <=
2112 cell_playback[i].last_sector);
2113 CHECK_VALUE(cell_playback[i].first_sector <=
2114 cell_playback[i].last_vobu_start_sector);
2115 }
2116
2117 return 1;
2118 }
2119
2120
2121 static int ifoRead_CELL_POSITION_TBL(ifo_handle_t *ifofile,
2122 cell_position_t *cell_position,
2123 unsigned int nr, unsigned int offset) {
2124 struct ifo_handle_private_s *ifop = PRIV(ifofile);
2125 unsigned int i;
2126 unsigned int size = nr * sizeof(cell_position_t);
2127
2128 if(!DVDFileSeek_(ifop->file, offset))
2129 return 0;
2130
2131 if(!(DVDReadBytes(ifop->file, cell_position, size)))
2132 return 0;
2133
2134 for(i = 0; i < nr; i++) {
2135 B2N_16(cell_position[i].vob_id_nr);
2136 CHECK_ZERO(cell_position[i].zero_1);
2137 }
2138
2139 return 1;
2140 }
2141
2142 static int ifoRead_PGC(ifo_handle_t *ifofile, pgc_t *pgc, unsigned int offset) {
2143 struct ifo_handle_private_s *ifop = PRIV(ifofile);
2144 unsigned int i;
2145
2146 if(!DVDFileSeek_(ifop->file, offset))
2147 return 0;
2148
2149 if(!(DVDReadBytes(ifop->file, pgc, PGC_SIZE)))
2150 return 0;
2151
2152 read_user_ops(&pgc->prohibited_ops);
2153 B2N_16(pgc->next_pgc_nr);
2154 B2N_16(pgc->prev_pgc_nr);
2155 B2N_16(pgc->goup_pgc_nr);
2156 B2N_16(pgc->command_tbl_offset);
2157 B2N_16(pgc->program_map_offset);
2158 B2N_16(pgc->cell_playback_offset);
2159 B2N_16(pgc->cell_position_offset);
2160
2161 for(i = 0; i < 8; i++)
2162 B2N_16(pgc->audio_control[i]);
2163 for(i = 0; i < 32; i++)
2164 B2N_32(pgc->subp_control[i]);
2165 for(i = 0; i < 16; i++)
2166 B2N_32(pgc->palette[i]);
2167
2168 CHECK_ZERO(pgc->zero_1);
2169 CHECK_VALUE(pgc->nr_of_programs <= pgc->nr_of_cells);
2170
2171 /* verify time (look at print_time) */
2172 for(i = 0; i < 8; i++)
2173 if(!(pgc->audio_control[i] & 0x8000)) /* The 'is present' bit */
2174 CHECK_ZERO(pgc->audio_control[i]);
2175 for(i = 0; i < 32; i++)
2176 if(!(pgc->subp_control[i] & 0x80000000)) /* The 'is present' bit */
2177 CHECK_ZERO(pgc->subp_control[i]);
2178
2179 /* Check that time is 0:0:0:0 also if nr_of_programs == 0 */
2180 if(pgc->nr_of_programs == 0) {
2181 CHECK_ZERO(pgc->still_time);
2182 CHECK_ZERO(pgc->pg_playback_mode); /* ?? */
2183 CHECK_VALUE(pgc->program_map_offset == 0);
2184 CHECK_VALUE(pgc->cell_playback_offset == 0);
2185 CHECK_VALUE(pgc->cell_position_offset == 0);
2186 } else {
2187 CHECK_VALUE(pgc->program_map_offset != 0);
2188 CHECK_VALUE(pgc->cell_playback_offset != 0);
2189 CHECK_VALUE(pgc->cell_position_offset != 0);
2190 }
2191
2192 if(pgc->command_tbl_offset != 0) {
2193 pgc->command_tbl = calloc(1, sizeof(pgc_command_tbl_t));
2194 if(!pgc->command_tbl)
2195 return 0;
2196
2197 if(!ifoRead_PGC_COMMAND_TBL(ifofile, pgc->command_tbl,
2198 offset + pgc->command_tbl_offset)) {
2199 return 0;
2200 }
2201 } else {
2202 pgc->command_tbl = NULL;
2203 }
2204
2205 if(pgc->program_map_offset != 0 && pgc->nr_of_programs>0) {
2206 pgc->program_map = calloc(pgc->nr_of_programs, sizeof(pgc_program_map_t));
2207 if(!pgc->program_map) {
2208 return 0;
2209 }
2210 if(!ifoRead_PGC_PROGRAM_MAP(ifofile, pgc->program_map,pgc->nr_of_programs,
2211 offset + pgc->program_map_offset)) {
2212 return 0;
2213 }
2214 } else {
2215 pgc->program_map = NULL;
2216 }
2217
2218 if(pgc->cell_playback_offset != 0 && pgc->nr_of_cells>0) {
2219 pgc->cell_playback = calloc(pgc->nr_of_cells, sizeof(cell_playback_t));
2220 if(!pgc->cell_playback) {
2221 return 0;
2222 }
2223 if(!ifoRead_CELL_PLAYBACK_TBL(ifofile, pgc->cell_playback,
2224 pgc->nr_of_cells,
2225 offset + pgc->cell_playback_offset)) {
2226 return 0;
2227 }
2228 } else {
2229 pgc->cell_playback = NULL;
2230 }
2231
2232 if(pgc->cell_position_offset != 0 && pgc->nr_of_cells>0) {
2233 pgc->cell_position = calloc(pgc->nr_of_cells, sizeof(cell_position_t));
2234 if(!pgc->cell_position) {
2235 return 0;
2236 }
2237 if(!ifoRead_CELL_POSITION_TBL(ifofile, pgc->cell_position,
2238 pgc->nr_of_cells,
2239 offset + pgc->cell_position_offset)) {
2240 return 0;
2241 }
2242 } else {
2243 pgc->cell_position = NULL;
2244 }
2245
2246 return 1;
2247 }
2248
2249 int ifoRead_FP_PGC(ifo_handle_t *ifofile) {
2250
2251 if(!ifofile)
2252 return 0;
2253
2254 if(!ifofile->vmgi_mat)
2255 return 0;
2256
2257 /* It seems that first_play_pgc is optional after all. */
2258 ifofile->first_play_pgc = NULL;
2259 if(!ifofile->vmgi_mat->first_play_pgc)
2260 return 1;
2261
2262 ifofile->first_play_pgc = calloc(1, sizeof(pgc_t));
2263 if(!ifofile->first_play_pgc)
2264 return 0;
2265
2266 ifofile->first_play_pgc->ref_count = 1;
2267 if(!ifoRead_PGC(ifofile, ifofile->first_play_pgc,
2268 ifofile->vmgi_mat->first_play_pgc)) {
2269 ifoFree_PGC(ifofile->first_play_pgc);
2270 ifofile->first_play_pgc = NULL;
2271 return 0;
2272 }
2273
2274 return 1;
2275 }
2276
2277 static void ifoFree_PGC(pgc_t *pgc) {
2278 if(pgc && (--pgc->ref_count) <= 0) {
2279 ifoFree_PGC_COMMAND_TBL(pgc->command_tbl);
2280 if(pgc->program_map)
2281 free(pgc->program_map);
2282 if(pgc->cell_playback)
2283 free(pgc->cell_playback);
2284 if(pgc->cell_position)
2285 free(pgc->cell_position);
2286 free(pgc);
2287 }
2288 }
2289
2290 void ifoFree_FP_PGC(ifo_handle_t *ifofile) {
2291 if(!ifofile)
2292 return;
2293
2294 if(ifofile->first_play_pgc) {
2295 ifoFree_PGC(ifofile->first_play_pgc);
2296 ifofile->first_play_pgc = NULL;
2297 }
2298 }
2299
2300
2301 int ifoRead_TT_SRPT(ifo_handle_t *ifofile) {
2302 struct ifo_handle_private_s *ifop = PRIV(ifofile);
2303 tt_srpt_t *tt_srpt;
2304 unsigned int i;
2305 size_t info_length;
2306
2307 if(!ifofile)
2308 return 0;
2309
2310 if(!ifofile->vmgi_mat)
2311 return 0;
2312
2313 if(ifofile->vmgi_mat->tt_srpt == 0) /* mandatory */
2314 return 0;
2315
2316 if(!DVDFileSeek_(ifop->file, ifofile->vmgi_mat->tt_srpt * DVD_BLOCK_LEN))
2317 return 0;
2318
2319 tt_srpt = calloc(1, sizeof(tt_srpt_t));
2320 if(!tt_srpt)
2321 return 0;
2322
2323 ifofile->tt_srpt = tt_srpt;
2324
2325 if(!(DVDReadBytes(ifop->file, tt_srpt, TT_SRPT_SIZE))) {
2326 Log0(ifop->ctx, "Unable to read read TT_SRPT.");
2327 free(tt_srpt);
2328 return 0;
2329 }
2330
2331 B2N_16(tt_srpt->nr_of_srpts);
2332 B2N_32(tt_srpt->last_byte);
2333
2334 /* E-One releases don't fill this field */
2335 if(tt_srpt->last_byte == 0) {
2336 tt_srpt->last_byte = tt_srpt->nr_of_srpts * sizeof(title_info_t) - 1 + TT_SRPT_SIZE;
2337 }
2338 info_length = tt_srpt->last_byte + 1 - TT_SRPT_SIZE;
2339
2340 tt_srpt->title = calloc(1, info_length);
2341 if(!tt_srpt->title) {
2342 free(tt_srpt);
2343 ifofile->tt_srpt = NULL;
2344 return 0;
2345 }
2346 if(!(DVDReadBytes(ifop->file, tt_srpt->title, info_length))) {
2347 Log0(ifop->ctx, "libdvdread: Unable to read read TT_SRPT.");
2348 ifoFree_TT_SRPT(ifofile);
2349 return 0;
2350 }
2351
2352 if(tt_srpt->nr_of_srpts>info_length/sizeof(title_info_t)){
2353 Log1(ifop->ctx, "data mismatch: info_length (%zd)!= nr_of_srpts (%d). Truncating.",
2354 info_length/sizeof(title_info_t),tt_srpt->nr_of_srpts);
2355 tt_srpt->nr_of_srpts=info_length/sizeof(title_info_t);
2356 }
2357
2358 for(i = 0; i < tt_srpt->nr_of_srpts; i++) {
2359 B2N_16(tt_srpt->title[i].nr_of_ptts);
2360 B2N_16(tt_srpt->title[i].parental_id);
2361 B2N_32(tt_srpt->title[i].title_set_sector);
2362 }
2363
2364
2365 CHECK_ZERO(tt_srpt->zero_1);
2366 CHECK_VALUE(tt_srpt->nr_of_srpts != 0);
2367 CHECK_VALUE(tt_srpt->nr_of_srpts < 100); /* ?? */
2368 CHECK_VALUE(tt_srpt->nr_of_srpts * sizeof(title_info_t) <= info_length);
2369
2370 for(i = 0; i < tt_srpt->nr_of_srpts; i++) {
2371 read_playback_type(&tt_srpt->title[i].pb_ty);
2372 CHECK_VALUE(tt_srpt->title[i].pb_ty.zero_1 == 0);
2373 CHECK_VALUE(tt_srpt->title[i].nr_of_angles != 0);
2374 CHECK_VALUE(tt_srpt->title[i].nr_of_angles < 10);
2375 /* CHECK_VALUE(tt_srpt->title[i].nr_of_ptts != 0); */
2376 /* XXX: this assertion breaks Ghostbusters: */
2377 CHECK_VALUE(tt_srpt->title[i].nr_of_ptts < 1000); /* ?? */
2378 CHECK_VALUE(tt_srpt->title[i].title_set_nr != 0);
2379 CHECK_VALUE(tt_srpt->title[i].title_set_nr < 100); /* ?? */
2380 CHECK_VALUE(tt_srpt->title[i].vts_ttn != 0);
2381 CHECK_VALUE(tt_srpt->title[i].vts_ttn < 100); /* ?? */
2382 /* CHECK_VALUE(tt_srpt->title[i].title_set_sector != 0); */
2383 }
2384
2385 /* Make this a function */
2386 #if 0
2387 if(memcmp((uint8_t *)tt_srpt->title +
2388 tt_srpt->nr_of_srpts * sizeof(title_info_t),
2389 my_friendly_zeros,
2390 info_length - tt_srpt->nr_of_srpts * sizeof(title_info_t))) {
2391 Log1(ifop->ctx, "VMG_PTT_SRPT slack is != 0, ");
2392 hexdump((uint8_t *)tt_srpt->title +
2393 tt_srpt->nr_of_srpts * sizeof(title_info_t),
2394 info_length - tt_srpt->nr_of_srpts * sizeof(title_info_t));
2395 }
2396 #endif
2397
2398 return 1;
2399 }
2400
2401
2402 void ifoFree_TT_SRPT(ifo_handle_t *ifofile) {
2403 if(!ifofile)
2404 return;
2405
2406 if(ifofile->tt_srpt) {
2407 free(ifofile->tt_srpt->title);
2408 ifofile->tt_srpt->title = NULL;
2409 free(ifofile->tt_srpt);
2410 ifofile->tt_srpt = NULL;
2411 }
2412 }
2413
2414
2415 int ifoRead_VTS_PTT_SRPT(ifo_handle_t *ifofile) {
2416 struct ifo_handle_private_s *ifop = PRIV(ifofile);
2417 vts_ptt_srpt_t *vts_ptt_srpt = NULL;
2418 int info_length, i, j;
2419 uint32_t *data = NULL;
2420
2421 if(!ifofile)
2422 return 0;
2423
2424 if(!ifofile->vtsi_mat)
2425 return 0;
2426
2427 if(ifofile->vtsi_mat->vts_ptt_srpt == 0) /* mandatory */
2428 return 0;
2429
2430 if(!DVDFileSeek_(ifop->file,
2431 ifofile->vtsi_mat->vts_ptt_srpt * DVD_BLOCK_LEN))
2432 return 0;
2433
2434 vts_ptt_srpt = calloc(1, sizeof(vts_ptt_srpt_t));
2435 if(!vts_ptt_srpt)
2436 return 0;
2437
2438 vts_ptt_srpt->title = NULL;
2439 ifofile->vts_ptt_srpt = vts_ptt_srpt;
2440
2441 if(!(DVDReadBytes(ifop->file, vts_ptt_srpt, VTS_PTT_SRPT_SIZE))) {
2442 Log0(ifop->ctx, "Unable to read PTT search table.");
2443 goto fail;
2444 }
2445
2446 B2N_16(vts_ptt_srpt->nr_of_srpts);
2447 B2N_32(vts_ptt_srpt->last_byte);
2448
2449 CHECK_ZERO(vts_ptt_srpt->zero_1);
2450 CHECK_VALUE(vts_ptt_srpt->nr_of_srpts != 0);
2451 CHECK_VALUE(vts_ptt_srpt->nr_of_srpts < 100); /* ?? */
2452
2453 /* E-One releases don't fill this field */
2454 if(vts_ptt_srpt->last_byte == 0) {
2455 vts_ptt_srpt->last_byte = vts_ptt_srpt->nr_of_srpts * sizeof(*data) - 1 + VTS_PTT_SRPT_SIZE;
2456 }
2457 info_length = vts_ptt_srpt->last_byte + 1 - VTS_PTT_SRPT_SIZE;
2458 data = calloc(1, info_length);
2459 if(!data)
2460 goto fail;
2461
2462 if(!(DVDReadBytes(ifop->file, data, info_length))) {
2463 Log0(ifop->ctx, "Unable to read PTT search table.");
2464 goto fail;
2465 }
2466
2467 if(vts_ptt_srpt->nr_of_srpts > info_length / sizeof(*data)) {
2468 Log0(ifop->ctx, "PTT search table too small.");
2469 goto fail;
2470 }
2471
2472 if(vts_ptt_srpt->nr_of_srpts == 0) {
2473 Log0(ifop->ctx, "Zero entries in PTT search table.");
2474 goto fail;
2475 }
2476
2477 for(i = 0; i < vts_ptt_srpt->nr_of_srpts; i++) {
2478 /* Transformers 3 has PTT start bytes that point outside the SRPT PTT */
2479 uint32_t start = data[i];
2480 B2N_32(start);
2481 if(start + sizeof(ptt_info_t) > vts_ptt_srpt->last_byte + 1) {
2482 /* don't mess with any bytes beyond the end of the allocation */
2483 vts_ptt_srpt->nr_of_srpts = i;
2484 break;
2485 }
2486 data[i] = start;
2487 /* assert(data[i] + sizeof(ptt_info_t) <= vts_ptt_srpt->last_byte + 1);
2488 Magic Knight Rayearth Daybreak is mastered very strange and has
2489 Titles with 0 PTTs. They all have a data[i] offsets beyond the end of
2490 of the vts_ptt_srpt structure. */
2491 CHECK_VALUE(data[i] + sizeof(ptt_info_t) <= vts_ptt_srpt->last_byte + 1 + 4);
2492 }
2493
2494 vts_ptt_srpt->ttu_offset = data;
2495
2496 vts_ptt_srpt->title = calloc(vts_ptt_srpt->nr_of_srpts, sizeof(ttu_t));
2497 if(!vts_ptt_srpt->title)
2498 goto fail;
2499
2500 for(i = 0; i < vts_ptt_srpt->nr_of_srpts; i++) {
2501 int n;
2502 if(i < vts_ptt_srpt->nr_of_srpts - 1)
2503 n = (data[i+1] - data[i]);
2504 else
2505 n = (vts_ptt_srpt->last_byte + 1 - data[i]);
2506
2507 /* assert(n > 0 && (n % 4) == 0);
2508 Magic Knight Rayearth Daybreak is mastered very strange and has
2509 Titles with 0 PTTs. */
2510 if(n < 0) n = 0;
2511
2512 /* DVDs created by the VDR-to-DVD device LG RC590M violate the following requirement */
2513 CHECK_VALUE(n % 4 == 0);
2514
2515 vts_ptt_srpt->title[i].nr_of_ptts = n / 4;
2516 vts_ptt_srpt->title[i].ptt = calloc(n / 4, sizeof(ptt_info_t));
2517 if(!vts_ptt_srpt->title[i].ptt) {
2518 for(n = 0; n < i; n++)
2519 free(vts_ptt_srpt->title[n].ptt);
2520
2521 goto fail;
2522 }
2523 for(j = 0; j < vts_ptt_srpt->title[i].nr_of_ptts; j++) {
2524 /* The assert placed here because of Magic Knight Rayearth Daybreak */
2525 CHECK_VALUE(data[i] + sizeof(ptt_info_t) <= vts_ptt_srpt->last_byte + 1);
2526 vts_ptt_srpt->title[i].ptt[j].pgcn
2527 = *(uint16_t*)(((char *)data) + data[i] + 4*j - VTS_PTT_SRPT_SIZE);
2528 vts_ptt_srpt->title[i].ptt[j].pgn
2529 = *(uint16_t*)(((char *)data) + data[i] + 4*j + 2 - VTS_PTT_SRPT_SIZE);
2530 }
2531 }
2532
2533 for(i = 0; i < vts_ptt_srpt->nr_of_srpts; i++) {
2534 for(j = 0; j < vts_ptt_srpt->title[i].nr_of_ptts; j++) {
2535 B2N_16(vts_ptt_srpt->title[i].ptt[j].pgcn);
2536 B2N_16(vts_ptt_srpt->title[i].ptt[j].pgn);
2537 }
2538 }
2539
2540 for(i = 0; i < vts_ptt_srpt->nr_of_srpts; i++) {
2541 CHECK_VALUE(vts_ptt_srpt->title[i].nr_of_ptts < 1000); /* ?? */
2542 for(j = 0; j < vts_ptt_srpt->title[i].nr_of_ptts; j++) {
2543 CHECK_VALUE(vts_ptt_srpt->title[i].ptt[j].pgcn != 0 );
2544 CHECK_VALUE(vts_ptt_srpt->title[i].ptt[j].pgcn < 1000); /* ?? */
2545 CHECK_VALUE(vts_ptt_srpt->title[i].ptt[j].pgn != 0);
2546 CHECK_VALUE(vts_ptt_srpt->title[i].ptt[j].pgn < 100); /* ?? */
2547 //don't abort here. E-One DVDs contain PTT with pgcn or pgn == 0
2548 }
2549 }
2550
2551 return 1;
2552
2553 fail:
2554 free(data);
2555 ifofile->vts_ptt_srpt = NULL;
2556 free(vts_ptt_srpt->title);
2557 free(vts_ptt_srpt);
2558 return 0;
2559 }
2560
2561
2562 void ifoFree_VTS_PTT_SRPT(ifo_handle_t *ifofile) {
2563 if(!ifofile)
2564 return;
2565
2566 if(ifofile->vts_ptt_srpt) {
2567 int i;
2568 for(i = 0; i < ifofile->vts_ptt_srpt->nr_of_srpts; i++)
2569 free(ifofile->vts_ptt_srpt->title[i].ptt);
2570 free(ifofile->vts_ptt_srpt->ttu_offset);
2571 free(ifofile->vts_ptt_srpt->title);
2572 free(ifofile->vts_ptt_srpt);
2573 ifofile->vts_ptt_srpt = 0;
2574 }
2575 }
2576
2577
2578 int ifoRead_PTL_MAIT(ifo_handle_t *ifofile) {
2579 struct ifo_handle_private_s *ifop = PRIV(ifofile);
2580 ptl_mait_t *ptl_mait;
2581 int info_length;
2582 unsigned int i, j;
2583
2584 if(!ifofile)
2585 return 0;
2586
2587 if(!ifofile->vmgi_mat)
2588 return 0;
2589
2590 if(!ifofile->vmgi_mat->ptl_mait)
2591 return 1;
2592
2593 if(!DVDFileSeek_(ifop->file, ifofile->vmgi_mat->ptl_mait * DVD_BLOCK_LEN))
2594 return 0;
2595
2596 ptl_mait = calloc(1, sizeof(ptl_mait_t));
2597 if(!ptl_mait)
2598 return 0;
2599
2600 ifofile->ptl_mait = ptl_mait;
2601
2602 if(!(DVDReadBytes(ifop->file, ptl_mait, PTL_MAIT_SIZE))) {
2603 free(ptl_mait);
2604 ifofile->ptl_mait = NULL;
2605 return 0;
2606 }
2607
2608 B2N_16(ptl_mait->nr_of_countries);
2609 B2N_16(ptl_mait->nr_of_vtss);
2610 B2N_32(ptl_mait->last_byte);
2611
2612 CHECK_VALUE(ptl_mait->nr_of_countries != 0);
2613 CHECK_VALUE(ptl_mait->nr_of_countries < 100); /* ?? */
2614 CHECK_VALUE(ptl_mait->nr_of_vtss != 0);
2615 CHECK_VALUE(ptl_mait->nr_of_vtss < 100); /* ?? */
2616 CHECK_VALUE(ptl_mait->nr_of_countries * PTL_MAIT_COUNTRY_SIZE
2617 <= ptl_mait->last_byte + 1 - PTL_MAIT_SIZE);
2618
2619 info_length = ptl_mait->nr_of_countries * sizeof(ptl_mait_country_t);
2620 ptl_mait->countries = calloc(1, info_length);
2621 if(!ptl_mait->countries) {
2622 free(ptl_mait);
2623 ifofile->ptl_mait = NULL;
2624 return 0;
2625 }
2626 for(i = 0; i < ptl_mait->nr_of_countries; i++) {
2627 ptl_mait->countries[i].pf_ptl_mai = NULL;
2628 }
2629
2630 for(i = 0; i < ptl_mait->nr_of_countries; i++) {
2631 if(!(DVDReadBytes(ifop->file, &ptl_mait->countries[i], PTL_MAIT_COUNTRY_SIZE))) {
2632 Log0(ifop->ctx, "Unable to read PTL_MAIT.");
2633 free(ptl_mait->countries);
2634 free(ptl_mait);
2635 ifofile->ptl_mait = NULL;
2636 return 0;
2637 }
2638 }
2639
2640 for(i = 0; i < ptl_mait->nr_of_countries; i++) {
2641 B2N_16(ptl_mait->countries[i].country_code);
2642 B2N_16(ptl_mait->countries[i].pf_ptl_mai_start_byte);
2643 }
2644
2645 for(i = 0; i < ptl_mait->nr_of_countries; i++) {
2646 CHECK_ZERO(ptl_mait->countries[i].zero_1);
2647 CHECK_ZERO(ptl_mait->countries[i].zero_2);
2648 CHECK_VALUE(ptl_mait->countries[i].pf_ptl_mai_start_byte
2649 + sizeof(pf_level_t) * (ptl_mait->nr_of_vtss + 1) <= ptl_mait->last_byte + 1);
2650 }
2651
2652 for(i = 0; i < ptl_mait->nr_of_countries; i++) {
2653 uint16_t *pf_temp;
2654
2655 if(!DVDFileSeek_(ifop->file,
2656 ifofile->vmgi_mat->ptl_mait * DVD_BLOCK_LEN
2657 + ptl_mait->countries[i].pf_ptl_mai_start_byte)) {
2658 Log0(ifop->ctx, "Unable to seek PTL_MAIT table at index %d.",i);
2659 free(ptl_mait->countries);
2660 free(ptl_mait);
2661 ifofile->ptl_mait = NULL;
2662 return 0;
2663 }
2664 info_length = (ptl_mait->nr_of_vtss + 1) * sizeof(pf_level_t);
2665 pf_temp = calloc(1, info_length);
2666 if(!pf_temp) {
2667 free_ptl_mait(ptl_mait, i);
2668 ifofile->ptl_mait = NULL;
2669 return 0;
2670 }
2671 if(!(DVDReadBytes(ifop->file, pf_temp, info_length))) {
2672 Log0(ifop->ctx, "Unable to read PTL_MAIT table at index %d.",i);
2673 free(pf_temp);
2674 free_ptl_mait(ptl_mait, i);
2675 ifofile->ptl_mait = NULL;
2676 return 0;
2677 }
2678 for (j = 0; j < ((ptl_mait->nr_of_vtss + 1U) * 8U); j++) {
2679 B2N_16(pf_temp[j]);
2680 }
2681 ptl_mait->countries[i].pf_ptl_mai = calloc(1, info_length);
2682 if(!ptl_mait->countries[i].pf_ptl_mai) {
2683 free(pf_temp);
2684 free_ptl_mait(ptl_mait, i);
2685 ifofile->ptl_mait = NULL;
2686 return 0;
2687 }
2688 { /* Transpose the array so we can use C indexing. */
2689 int level, vts;
2690 for(level = 0; level < PTL_MAIT_NUM_LEVEL; level++) {
2691 for(vts = 0; vts <= ptl_mait->nr_of_vtss; vts++) {
2692 ptl_mait->countries[i].pf_ptl_mai[vts][level] =
2693 pf_temp[(7-level)*(ptl_mait->nr_of_vtss+1) + vts];
2694 }
2695 }
2696 free(pf_temp);
2697 }
2698 }
2699 return 1;
2700 }
2701
2702 void ifoFree_PTL_MAIT(ifo_handle_t *ifofile) {
2703 if(!ifofile)
2704 return;
2705
2706 if(ifofile->ptl_mait) {
2707 unsigned int i;
2708
2709 for(i = 0; i < ifofile->ptl_mait->nr_of_countries; i++) {
2710 free(ifofile->ptl_mait->countries[i].pf_ptl_mai);
2711 }
2712 free(ifofile->ptl_mait->countries);
2713 free(ifofile->ptl_mait);
2714 ifofile->ptl_mait = NULL;
2715 }
2716 }
2717
2718 int ifoRead_VTS_TMAPT(ifo_handle_t *ifofile) {
2719 struct ifo_handle_private_s *ifop = PRIV(ifofile);
2720 vts_tmapt_t *vts_tmapt;
2721 uint32_t *vts_tmap_srp;
2722 unsigned int offset;
2723 int info_length;
2724 unsigned int i, j;
2725
2726 if(!ifofile)
2727 return 0;
2728
2729 if(!ifofile->vtsi_mat)
2730 return 0;
2731
2732 if(ifofile->vtsi_mat->vts_tmapt == 0) {
2733 ifofile->vts_tmapt = NULL;
2734 return 1;
2735 }
2736
2737 offset = ifofile->vtsi_mat->vts_tmapt * DVD_BLOCK_LEN;
2738
2739 if(!DVDFileSeek_(ifop->file, offset))
2740 return 0;
2741
2742 vts_tmapt = calloc(1, sizeof(vts_tmapt_t));
2743 if(!vts_tmapt)
2744 return 0;
2745
2746 ifofile->vts_tmapt = vts_tmapt;
2747
2748 if(!(DVDReadBytes(ifop->file, vts_tmapt, VTS_TMAPT_SIZE))) {
2749 Log0(ifop->ctx, "Unable to read VTS_TMAPT.");
2750 free(vts_tmapt);
2751 ifofile->vts_tmapt = NULL;
2752 return 0;
2753 }
2754
2755 B2N_16(vts_tmapt->nr_of_tmaps);
2756 B2N_32(vts_tmapt->last_byte);
2757
2758 CHECK_ZERO(vts_tmapt->zero_1);
2759
2760 info_length = vts_tmapt->nr_of_tmaps * 4;
2761
2762 vts_tmap_srp = calloc(1, info_length);
2763 if(!vts_tmap_srp) {
2764 free(vts_tmapt);
2765 ifofile->vts_tmapt = NULL;
2766 return 0;
2767 }
2768
2769 vts_tmapt->tmap_offset = vts_tmap_srp;
2770
2771 if(!(DVDReadBytes(ifop->file, vts_tmap_srp, info_length))) {
2772 Log0(ifop->ctx, "Unable to read VTS_TMAPT.");
2773 free(vts_tmap_srp);
2774 free(vts_tmapt);
2775 ifofile->vts_tmapt = NULL;
2776 return 0;
2777 }
2778
2779 for (i = 0; i < vts_tmapt->nr_of_tmaps; i++) {
2780 B2N_32(vts_tmap_srp[i]);
2781 }
2782
2783
2784 info_length = vts_tmapt->nr_of_tmaps * sizeof(vts_tmap_t);
2785
2786 vts_tmapt->tmap = calloc(1, info_length);
2787 if(!vts_tmapt->tmap) {
2788 free(vts_tmap_srp);
2789 free(vts_tmapt);
2790 ifofile->vts_tmapt = NULL;
2791 return 0;
2792 }
2793
2794 for(i = 0; i < vts_tmapt->nr_of_tmaps; i++) {
2795 if(!DVDFileSeek_(ifop->file, offset + vts_tmap_srp[i])) {
2796 ifoFree_VTS_TMAPT(ifofile);
2797 return 0;
2798 }
2799
2800 if(!(DVDReadBytes(ifop->file, &vts_tmapt->tmap[i], VTS_TMAP_SIZE))) {
2801 Log0(ifop->ctx, "Unable to read VTS_TMAP.");
2802 ifoFree_VTS_TMAPT(ifofile);
2803 return 0;
2804 }
2805
2806 B2N_16(vts_tmapt->tmap[i].nr_of_entries);
2807 CHECK_ZERO(vts_tmapt->tmap[i].zero_1);
2808
2809 if(vts_tmapt->tmap[i].nr_of_entries == 0) { /* Early out if zero entries */
2810 vts_tmapt->tmap[i].map_ent = NULL;
2811 continue;
2812 }
2813
2814 info_length = vts_tmapt->tmap[i].nr_of_entries * sizeof(map_ent_t);
2815
2816 vts_tmapt->tmap[i].map_ent = calloc(1, info_length);
2817 if(!vts_tmapt->tmap[i].map_ent) {
2818 ifoFree_VTS_TMAPT(ifofile);
2819 return 0;
2820 }
2821
2822 if(!(DVDReadBytes(ifop->file, vts_tmapt->tmap[i].map_ent, info_length))) {
2823 Log0(ifop->ctx, "Unable to read VTS_TMAP_ENT.");
2824 ifoFree_VTS_TMAPT(ifofile);
2825 return 0;
2826 }
2827
2828 for(j = 0; j < vts_tmapt->tmap[i].nr_of_entries; j++)
2829 B2N_32(vts_tmapt->tmap[i].map_ent[j]);
2830 }
2831
2832 return 1;
2833 }
2834
2835 void ifoFree_VTS_TMAPT(ifo_handle_t *ifofile) {
2836 if(!ifofile)
2837 return;
2838
2839 if(ifofile->vts_tmapt) {
2840 unsigned int i;
2841
2842 for(i = 0; i < ifofile->vts_tmapt->nr_of_tmaps; i++)
2843 if(ifofile->vts_tmapt->tmap[i].map_ent)
2844 free(ifofile->vts_tmapt->tmap[i].map_ent);
2845 free(ifofile->vts_tmapt->tmap);
2846 free(ifofile->vts_tmapt->tmap_offset);
2847 free(ifofile->vts_tmapt);
2848 ifofile->vts_tmapt = NULL;
2849 }
2850 }
2851
2852
2853 int ifoRead_TITLE_C_ADT(ifo_handle_t *ifofile) {
2854
2855 if(!ifofile)
2856 return 0;
2857
2858 if(!ifofile->vtsi_mat)
2859 return 0;
2860
2861 if(ifofile->vtsi_mat->vts_c_adt == 0) /* mandatory */
2862 return 0;
2863
2864 ifofile->vts_c_adt = calloc(1, sizeof(c_adt_t));
2865 if(!ifofile->vts_c_adt)
2866 return 0;
2867
2868 if(!ifoRead_C_ADT_internal(ifofile, ifofile->vts_c_adt,
2869 ifofile->vtsi_mat->vts_c_adt)) {
2870 free(ifofile->vts_c_adt);
2871 ifofile->vts_c_adt = NULL;
2872 return 0;
2873 }
2874
2875 return 1;
2876 }
2877
2878 int ifoRead_C_ADT(ifo_handle_t *ifofile) {
2879 unsigned int sector;
2880
2881 if(!ifofile)
2882 return 0;
2883
2884 if(ifofile->vmgi_mat) {
2885 if(ifofile->vmgi_mat->vmgm_c_adt == 0)
2886 return 1;
2887 sector = ifofile->vmgi_mat->vmgm_c_adt;
2888 } else if(ifofile->vtsi_mat) {
2889 if(ifofile->vtsi_mat->vtsm_c_adt == 0)
2890 return 1;
2891 sector = ifofile->vtsi_mat->vtsm_c_adt;
2892 } else {
2893 return 0;
2894 }
2895
2896 ifofile->menu_c_adt = calloc(1, sizeof(c_adt_t));
2897 if(!ifofile->menu_c_adt)
2898 return 0;
2899
2900 if(!ifoRead_C_ADT_internal(ifofile, ifofile->menu_c_adt, sector)) {
2901 free(ifofile->menu_c_adt);
2902 ifofile->menu_c_adt = NULL;
2903 return 0;
2904 }
2905
2906 return 1;
2907 }
2908
2909 static int ifoRead_C_ADT_internal(ifo_handle_t *ifofile,
2910 c_adt_t *c_adt, unsigned int sector) {
2911 struct ifo_handle_private_s *ifop = PRIV(ifofile);
2912 size_t i, info_length;
2913
2914 if(!DVDFileSeek_(ifop->file, sector * DVD_BLOCK_LEN))
2915 return 0;
2916
2917 if(!(DVDReadBytes(ifop->file, c_adt, C_ADT_SIZE)))
2918 return 0;
2919
2920 B2N_16(c_adt->nr_of_vobs);
2921 B2N_32(c_adt->last_byte);
2922
2923 if(c_adt->last_byte + 1 < C_ADT_SIZE)
2924 return 0;
2925
2926 info_length = c_adt->last_byte + 1 - C_ADT_SIZE;
2927
2928 CHECK_ZERO(c_adt->zero_1);
2929 /* assert(c_adt->nr_of_vobs > 0);
2930 Magic Knight Rayearth Daybreak is mastered very strange and has
2931 Titles with a VOBS that has no cells. */
2932 CHECK_VALUE(info_length % sizeof(cell_adr_t) == 0);
2933
2934 /* assert(info_length / sizeof(cell_adr_t) >= c_adt->nr_of_vobs);
2935 Enemy of the State region 2 (de) has Titles where nr_of_vobs field
2936 is to high, they high ones are never referenced though. */
2937 if(info_length / sizeof(cell_adr_t) < c_adt->nr_of_vobs) {
2938 Log1(ifop->ctx, "C_ADT nr_of_vobs > available info entries");
2939 c_adt->nr_of_vobs = info_length / sizeof(cell_adr_t);
2940 }
2941
2942 c_adt->cell_adr_table = calloc(1, info_length);
2943 if(!c_adt->cell_adr_table)
2944 return 0;
2945
2946 if(info_length &&
2947 !(DVDReadBytes(ifop->file, c_adt->cell_adr_table, info_length))) {
2948 free(c_adt->cell_adr_table);
2949 return 0;
2950 }
2951
2952 for(i = 0; i < info_length/sizeof(cell_adr_t); i++) {
2953 B2N_16(c_adt->cell_adr_table[i].vob_id);
2954 B2N_32(c_adt->cell_adr_table[i].start_sector);
2955 B2N_32(c_adt->cell_adr_table[i].last_sector);
2956
2957 CHECK_ZERO(c_adt->cell_adr_table[i].zero_1);
2958 CHECK_VALUE(c_adt->cell_adr_table[i].vob_id > 0);
2959 CHECK_VALUE(c_adt->cell_adr_table[i].vob_id <= c_adt->nr_of_vobs);
2960 CHECK_VALUE(c_adt->cell_adr_table[i].cell_id > 0);
2961 CHECK_VALUE(c_adt->cell_adr_table[i].start_sector <
2962 c_adt->cell_adr_table[i].last_sector);
2963 }
2964
2965 return 1;
2966 }
2967
2968
2969 static void ifoFree_C_ADT_internal(c_adt_t *c_adt) {
2970 if(c_adt) {
2971 free(c_adt->cell_adr_table);
2972 free(c_adt);
2973 }
2974 }
2975
2976 void ifoFree_C_ADT(ifo_handle_t *ifofile) {
2977 if(!ifofile)
2978 return;
2979
2980 ifoFree_C_ADT_internal(ifofile->menu_c_adt);
2981 ifofile->menu_c_adt = NULL;
2982 }
2983
2984 void ifoFree_TITLE_C_ADT(ifo_handle_t *ifofile) {
2985 if(!ifofile)
2986 return;
2987
2988 ifoFree_C_ADT_internal(ifofile->vts_c_adt);
2989 ifofile->vts_c_adt = NULL;
2990 }
2991
2992 int ifoRead_TITLE_VOBU_ADMAP(ifo_handle_t *ifofile) {
2993 if(!ifofile)
2994 return 0;
2995
2996 if(!ifofile->vtsi_mat)
2997 return 0;
2998
2999 if(ifofile->vtsi_mat->vts_vobu_admap == 0) /* mandatory */
3000 return 0;
3001
3002 ifofile->vts_vobu_admap = calloc(1, sizeof(vobu_admap_t));
3003 if(!ifofile->vts_vobu_admap)
3004 return 0;
3005
3006 if(!ifoRead_VOBU_ADMAP_internal(ifofile, ifofile->vts_vobu_admap,
3007 ifofile->vtsi_mat->vts_vobu_admap)) {
3008 free(ifofile->vts_vobu_admap);
3009 ifofile->vts_vobu_admap = NULL;
3010 return 0;
3011 }
3012
3013 return 1;
3014 }
3015
3016 int ifoRead_VOBU_ADMAP(ifo_handle_t *ifofile) {
3017 unsigned int sector;
3018
3019 if(!ifofile)
3020 return 0;
3021
3022 if(ifofile->vmgi_mat) {
3023 if(ifofile->vmgi_mat->vmgm_vobu_admap == 0)
3024 return 1;
3025 sector = ifofile->vmgi_mat->vmgm_vobu_admap;
3026 } else if(ifofile->vtsi_mat) {
3027 if(ifofile->vtsi_mat->vtsm_vobu_admap == 0)
3028 return 1;
3029 sector = ifofile->vtsi_mat->vtsm_vobu_admap;
3030 } else {
3031 return 0;
3032 }
3033
3034 ifofile->menu_vobu_admap = calloc(1, sizeof(vobu_admap_t));
3035 if(!ifofile->menu_vobu_admap)
3036 return 0;
3037
3038 if(!ifoRead_VOBU_ADMAP_internal(ifofile, ifofile->menu_vobu_admap, sector)) {
3039 free(ifofile->menu_vobu_admap);
3040 ifofile->menu_vobu_admap = NULL;
3041 return 0;
3042 }
3043
3044 return 1;
3045 }
3046
3047 static int ifoRead_VOBU_ADMAP_internal(ifo_handle_t *ifofile,
3048 vobu_admap_t *vobu_admap,
3049 unsigned int sector) {
3050 struct ifo_handle_private_s *ifop = PRIV(ifofile);
3051 unsigned int i;
3052 int info_length;
3053
3054 if(!DVDFileSeekForce_(ifop->file, sector * DVD_BLOCK_LEN, sector))
3055 return 0;
3056
3057 if(!(DVDReadBytes(ifop->file, vobu_admap, VOBU_ADMAP_SIZE)))
3058 return 0;
3059
3060 B2N_32(vobu_admap->last_byte);
3061
3062 info_length = vobu_admap->last_byte + 1 - VOBU_ADMAP_SIZE;
3063 /* assert(info_length > 0);
3064 Magic Knight Rayearth Daybreak is mastered very strange and has
3065 Titles with a VOBS that has no VOBUs. */
3066 CHECK_VALUE(info_length % sizeof(uint32_t) == 0);
3067
3068 vobu_admap->vobu_start_sectors = calloc(1, info_length);
3069 if(!vobu_admap->vobu_start_sectors) {
3070 return 0;
3071 }
3072 if(info_length &&
3073 !(DVDReadBytes(ifop->file,
3074 vobu_admap->vobu_start_sectors, info_length))) {
3075 free(vobu_admap->vobu_start_sectors);
3076 return 0;
3077 }
3078
3079 for(i = 0; i < info_length/sizeof(uint32_t); i++)
3080 B2N_32(vobu_admap->vobu_start_sectors[i]);
3081
3082 return 1;
3083 }
3084
3085
3086 static void ifoFree_VOBU_ADMAP_internal(vobu_admap_t *vobu_admap) {
3087 if(vobu_admap) {
3088 free(vobu_admap->vobu_start_sectors);
3089 free(vobu_admap);
3090 }
3091 }
3092
3093 void ifoFree_VOBU_ADMAP(ifo_handle_t *ifofile) {
3094 if(!ifofile)
3095 return;
3096
3097 ifoFree_VOBU_ADMAP_internal(ifofile->menu_vobu_admap);
3098 ifofile->menu_vobu_admap = NULL;
3099 }
3100
3101 void ifoFree_TITLE_VOBU_ADMAP(ifo_handle_t *ifofile) {
3102 if(!ifofile)
3103 return;
3104
3105 ifoFree_VOBU_ADMAP_internal(ifofile->vts_vobu_admap);
3106 ifofile->vts_vobu_admap = NULL;
3107 }
3108
3109 int ifoRead_PGCIT(ifo_handle_t *ifofile) {
3110
3111 if(!ifofile)
3112 return 0;
3113
3114 if(!ifofile->vtsi_mat)
3115 return 0;
3116
3117 if(ifofile->vtsi_mat->vts_pgcit == 0) /* mandatory */
3118 return 0;
3119
3120 ifofile->vts_pgcit = calloc(1, sizeof(pgcit_t));
3121 if(!ifofile->vts_pgcit)
3122 return 0;
3123
3124 ifofile->vts_pgcit->ref_count = 1;
3125 if(!ifoRead_PGCIT_internal(ifofile, ifofile->vts_pgcit,
3126 ifofile->vtsi_mat->vts_pgcit * DVD_BLOCK_LEN)) {
3127 free(ifofile->vts_pgcit);
3128 ifofile->vts_pgcit = NULL;
3129 return 0;
3130 }
3131
3132 return 1;
3133 }
3134
3135 static int find_dup_pgc(pgci_srp_t *pgci_srp, uint32_t start_byte, int count) {
3136 int i;
3137
3138 for(i = 0; i < count; i++) {
3139 if(pgci_srp[i].pgc_start_byte == start_byte) {
3140 return i;
3141 }
3142 }
3143 return -1;
3144 }
3145
3146 static int ifoRead_PGCIT_internal(ifo_handle_t *ifofile, pgcit_t *pgcit,
3147 unsigned int offset) {
3148 struct ifo_handle_private_s *ifop = PRIV(ifofile);
3149 int i, info_length;
3150 uint8_t *data, *ptr;
3151
3152 if(!DVDFileSeek_(ifop->file, offset))
3153 return 0;
3154
3155 if(!(DVDReadBytes(ifop->file, pgcit, PGCIT_SIZE)))
3156 return 0;
3157
3158 B2N_16(pgcit->nr_of_pgci_srp);
3159 B2N_32(pgcit->last_byte);
3160
3161 CHECK_ZERO(pgcit->zero_1);
3162 /* assert(pgcit->nr_of_pgci_srp != 0);
3163 Magic Knight Rayearth Daybreak is mastered very strange and has
3164 Titles with 0 PTTs. */
3165 CHECK_VALUE(pgcit->nr_of_pgci_srp < 10000); /* ?? seen max of 1338 */
3166
3167 if (pgcit->nr_of_pgci_srp == 0) {
3168 pgcit->pgci_srp = NULL;
3169 return 1;
3170 }
3171
3172 info_length = pgcit->nr_of_pgci_srp * PGCI_SRP_SIZE;
3173 data = calloc(1, info_length);
3174 if(!data)
3175 return 0;
3176
3177 if(info_length && !(DVDReadBytes(ifop->file, data, info_length))) {
3178 free(data);
3179 return 0;
3180 }
3181
3182 pgcit->pgci_srp = calloc(pgcit->nr_of_pgci_srp, sizeof(pgci_srp_t));
3183 if(!pgcit->pgci_srp) {
3184 free(data);
3185 return 0;
3186 }
3187 ptr = data;
3188 for(i = 0; i < pgcit->nr_of_pgci_srp; i++) {
3189 memcpy(&pgcit->pgci_srp[i], ptr, PGCI_SRP_SIZE);
3190 ptr += PGCI_SRP_SIZE;
3191 read_pgci_srp(&pgcit->pgci_srp[i]);
3192 CHECK_VALUE(pgcit->pgci_srp[i].zero_1 == 0);
3193 }
3194 free(data);
3195
3196 for(i = 0; i < pgcit->nr_of_pgci_srp; i++)
3197 CHECK_VALUE(pgcit->pgci_srp[i].pgc_start_byte + PGC_SIZE <= pgcit->last_byte+1);
3198
3199 for(i = 0; i < pgcit->nr_of_pgci_srp; i++) {
3200 int dup;
3201 if((dup = find_dup_pgc(pgcit->pgci_srp, pgcit->pgci_srp[i].pgc_start_byte, i)) >= 0) {
3202 pgcit->pgci_srp[i].pgc = pgcit->pgci_srp[dup].pgc;
3203 pgcit->pgci_srp[i].pgc->ref_count++;
3204 continue;
3205 }
3206 pgcit->pgci_srp[i].pgc = calloc(1, sizeof(pgc_t));
3207 if(!pgcit->pgci_srp[i].pgc) {
3208 int j;
3209 for(j = 0; j < i; j++) {
3210 ifoFree_PGC(pgcit->pgci_srp[j].pgc);
3211 pgcit->pgci_srp[j].pgc = NULL;
3212 }
3213 goto fail;
3214 }
3215 pgcit->pgci_srp[i].pgc->ref_count = 1;
3216 if(!ifoRead_PGC(ifofile, pgcit->pgci_srp[i].pgc,
3217 offset + pgcit->pgci_srp[i].pgc_start_byte)) {
3218 Log0(ifop->ctx, "Unable to read invalid PCG");
3219 //E-One releases provide bogus PGC, ie: out of bound start_byte
3220 free(pgcit->pgci_srp[i].pgc);
3221 pgcit->pgci_srp[i].pgc = NULL;
3222 }
3223 }
3224
3225 return 1;
3226 fail:
3227 free(pgcit->pgci_srp);
3228 pgcit->pgci_srp = NULL;
3229 return 0;
3230 }
3231
3232 static void ifoFree_PGCIT_internal(pgcit_t *pgcit) {
3233 if(pgcit && (--pgcit->ref_count <= 0)) {
3234 int i;
3235 for(i = 0; i < pgcit->nr_of_pgci_srp; i++)
3236 {
3237 ifoFree_PGC(pgcit->pgci_srp[i].pgc);
3238 pgcit->pgci_srp[i].pgc = NULL;
3239 }
3240 free(pgcit->pgci_srp);
3241 free(pgcit);
3242 }
3243 }
3244
3245 void ifoFree_PGCIT(ifo_handle_t *ifofile) {
3246 if(!ifofile)
3247 return;
3248
3249 if(ifofile->vts_pgcit) {
3250 ifoFree_PGCIT_internal(ifofile->vts_pgcit);
3251 ifofile->vts_pgcit = NULL;
3252 }
3253 }
3254
3255 static int find_dup_lut(pgci_lu_t *lu, uint32_t start_byte, int count) {
3256 int i;
3257
3258 for(i = 0; i < count; i++) {
3259 if(lu[i].lang_start_byte == start_byte) {
3260 return i;
3261 }
3262 }
3263 return -1;
3264 }
3265
3266 int ifoRead_PGCI_UT(ifo_handle_t *ifofile) {
3267 struct ifo_handle_private_s *ifop = PRIV(ifofile);
3268 pgci_ut_t *pgci_ut;
3269 unsigned int sector;
3270 unsigned int i;
3271 int info_length;
3272 uint8_t *data, *ptr;
3273
3274 if(!ifofile)
3275 return 0;
3276
3277 if(ifofile->vmgi_mat) {
3278 if(ifofile->vmgi_mat->vmgm_pgci_ut == 0)
3279 return 1;
3280 sector = ifofile->vmgi_mat->vmgm_pgci_ut;
3281 } else if(ifofile->vtsi_mat) {
3282 if(ifofile->vtsi_mat->vtsm_pgci_ut == 0)
3283 return 1;
3284 sector = ifofile->vtsi_mat->vtsm_pgci_ut;
3285 } else {
3286 return 0;
3287 }
3288
3289 ifofile->pgci_ut = calloc(1, sizeof(pgci_ut_t));
3290 if(!ifofile->pgci_ut)
3291 return 0;
3292
3293 if(!DVDFileSeek_(ifop->file, sector * DVD_BLOCK_LEN)) {
3294 free(ifofile->pgci_ut);
3295 ifofile->pgci_ut = NULL;
3296 return 0;
3297 }
3298
3299 if(!(DVDReadBytes(ifop->file, ifofile->pgci_ut, PGCI_UT_SIZE))) {
3300 free(ifofile->pgci_ut);
3301 ifofile->pgci_ut = NULL;
3302 return 0;
3303 }
3304
3305 pgci_ut = ifofile->pgci_ut;
3306
3307 B2N_16(pgci_ut->nr_of_lus);
3308 B2N_32(pgci_ut->last_byte);
3309
3310 CHECK_ZERO(pgci_ut->zero_1);
3311 CHECK_VALUE(pgci_ut->nr_of_lus != 0);
3312 CHECK_VALUE(pgci_ut->nr_of_lus < 100); /* ?? 3-4 ? */
3313 CHECK_VALUE((uint32_t)pgci_ut->nr_of_lus * PGCI_LU_SIZE < pgci_ut->last_byte);
3314
3315 info_length = pgci_ut->nr_of_lus * PGCI_LU_SIZE;
3316 data = calloc(1, info_length);
3317 if(!data) {
3318 free(pgci_ut);
3319 ifofile->pgci_ut = NULL;
3320 return 0;
3321 }
3322 if(!(DVDReadBytes(ifop->file, data, info_length))) {
3323 free(data);
3324 free(pgci_ut);
3325 ifofile->pgci_ut = NULL;
3326 return 0;
3327 }
3328
3329 pgci_ut->lu = calloc(pgci_ut->nr_of_lus, sizeof(pgci_lu_t));
3330 if(!pgci_ut->lu) {
3331 free(data);
3332 free(pgci_ut);
3333 ifofile->pgci_ut = NULL;
3334 return 0;
3335 }
3336 ptr = data;
3337 for(i = 0; i < pgci_ut->nr_of_lus; i++) {
3338 memcpy(&pgci_ut->lu[i], ptr, PGCI_LU_SIZE);
3339 ptr += PGCI_LU_SIZE;
3340 B2N_16(pgci_ut->lu[i].lang_code);
3341 B2N_32(pgci_ut->lu[i].lang_start_byte);
3342 }
3343 free(data);
3344
3345 for(i = 0; i < pgci_ut->nr_of_lus; i++) {
3346 /* Maybe this is only defined for v1.1 and later titles? */
3347 /* If the bits in 'lu[i].exists' are enumerated abcd efgh then:
3348 VTS_x_yy.IFO VIDEO_TS.IFO
3349 a == 0x83 "Root" 0x82 "Title"
3350 b == 0x84 "Subpicture"
3351 c == 0x85 "Audio"
3352 d == 0x86 "Angle"
3353 e == 0x87 "PTT"
3354 */
3355 CHECK_VALUE((pgci_ut->lu[i].exists & 0x07) == 0);
3356 }
3357
3358 for(i = 0; i < pgci_ut->nr_of_lus; i++) {
3359 int dup;
3360 if((dup = find_dup_lut(pgci_ut->lu, pgci_ut->lu[i].lang_start_byte, i)) >= 0) {
3361 pgci_ut->lu[i].pgcit = pgci_ut->lu[dup].pgcit;
3362 pgci_ut->lu[i].pgcit->ref_count++;
3363 continue;
3364 }
3365 pgci_ut->lu[i].pgcit = calloc(1, sizeof(pgcit_t));
3366 if(!pgci_ut->lu[i].pgcit) {
3367 unsigned int j;
3368 for(j = 0; j < i; j++) {
3369 ifoFree_PGCIT_internal(pgci_ut->lu[j].pgcit);
3370 pgci_ut->lu[j].pgcit = NULL;
3371 }
3372 free(pgci_ut->lu);
3373 free(pgci_ut);
3374 ifofile->pgci_ut = NULL;
3375 return 0;
3376 }
3377 pgci_ut->lu[i].pgcit->ref_count = 1;
3378 if(!ifoRead_PGCIT_internal(ifofile, pgci_ut->lu[i].pgcit,
3379 sector * DVD_BLOCK_LEN
3380 + pgci_ut->lu[i].lang_start_byte)) {
3381 unsigned int j;
3382 for(j = 0; j <= i; j++) {
3383 ifoFree_PGCIT_internal(pgci_ut->lu[j].pgcit);
3384 pgci_ut->lu[j].pgcit = NULL;
3385 }
3386 free(pgci_ut->lu);
3387 free(pgci_ut);
3388 ifofile->pgci_ut = NULL;
3389 return 0;
3390 }
3391 /* FIXME: Iterate and verify that all menus that should exists accordingly
3392 * to pgci_ut->lu[i].exists really do? */
3393 }
3394
3395 return 1;
3396 }
3397
3398
3399 void ifoFree_PGCI_UT(ifo_handle_t *ifofile) {
3400 if(!ifofile)
3401 return;
3402
3403 if(ifofile->pgci_ut) {
3404 unsigned int i;
3405
3406 for(i = 0; i < ifofile->pgci_ut->nr_of_lus; i++) {
3407 ifoFree_PGCIT_internal(ifofile->pgci_ut->lu[i].pgcit);
3408 ifofile->pgci_ut->lu[i].pgcit = NULL;
3409 }
3410 free(ifofile->pgci_ut->lu);
3411 free(ifofile->pgci_ut);
3412 ifofile->pgci_ut = NULL;
3413 }
3414 }
3415
3416 static int ifoRead_VTS_ATTRIBUTES(ifo_handle_t *ifofile,
3417 vts_attributes_t *vts_attributes,
3418 unsigned int offset) {
3419 struct ifo_handle_private_s *ifop = PRIV(ifofile);
3420 unsigned int i;
3421
3422 if(!DVDFileSeek_(ifop->file, offset))
3423 return 0;
3424
3425 if(!(DVDReadBytes(ifop->file, vts_attributes, sizeof(vts_attributes_t))))
3426 return 0;
3427
3428 read_video_attr(&vts_attributes->vtsm_vobs_attr);
3429 read_video_attr(&vts_attributes->vtstt_vobs_video_attr);
3430 read_audio_attr(&vts_attributes->vtsm_audio_attr);
3431 for(i=0; i<8; i++)
3432 read_audio_attr(&vts_attributes->vtstt_audio_attr[i]);
3433 read_subp_attr(&vts_attributes->vtsm_subp_attr);
3434 for(i=0; i<32; i++)
3435 read_subp_attr(&vts_attributes->vtstt_subp_attr[i]);
3436 B2N_32(vts_attributes->last_byte);
3437 B2N_32(vts_attributes->vts_cat);
3438
3439 CHECK_ZERO(vts_attributes->zero_1);
3440 CHECK_ZERO(vts_attributes->zero_2);
3441 CHECK_ZERO(vts_attributes->zero_3);
3442 CHECK_ZERO(vts_attributes->zero_4);
3443 CHECK_ZERO(vts_attributes->zero_5);
3444 CHECK_ZERO(vts_attributes->zero_6);
3445 CHECK_ZERO(vts_attributes->zero_7);
3446 CHECK_VALUE(vts_attributes->nr_of_vtsm_audio_streams <= 1);
3447 CHECK_VALUE(vts_attributes->nr_of_vtsm_subp_streams <= 1);
3448 CHECK_VALUE(vts_attributes->nr_of_vtstt_audio_streams <= 8);
3449 for(i = vts_attributes->nr_of_vtstt_audio_streams; i < 8; i++)
3450 CHECK_ZERO(vts_attributes->vtstt_audio_attr[i]);
3451 CHECK_VALUE(vts_attributes->nr_of_vtstt_subp_streams <= 32);
3452 {
3453 unsigned int nr_coded;
3454 CHECK_VALUE(vts_attributes->last_byte + 1 >= VTS_ATTRIBUTES_MIN_SIZE);
3455 nr_coded = (vts_attributes->last_byte + 1 - VTS_ATTRIBUTES_MIN_SIZE)/6;
3456 /* This is often nr_coded = 70, how do you know how many there really are? */
3457 if(nr_coded > 32) { /* We haven't read more from disk/file anyway */
3458 nr_coded = 32;
3459 }
3460 CHECK_VALUE(vts_attributes->nr_of_vtstt_subp_streams <= nr_coded);
3461 for(i = vts_attributes->nr_of_vtstt_subp_streams; i < nr_coded; i++)
3462 CHECK_ZERO(vts_attributes->vtstt_subp_attr[i]);
3463 }
3464
3465 return 1;
3466 }
3467
3468
3469
3470 int ifoRead_VTS_ATRT(ifo_handle_t *ifofile) {
3471 struct ifo_handle_private_s *ifop = PRIV(ifofile);
3472 vts_atrt_t *vts_atrt;
3473 unsigned int i, info_length, sector;
3474 uint32_t *data;
3475
3476 if(!ifofile)
3477 return 0;
3478
3479 if(!ifofile->vmgi_mat)
3480 return 0;
3481
3482 if(ifofile->vmgi_mat->vts_atrt == 0) /* mandatory */
3483 return 0;
3484
3485 sector = ifofile->vmgi_mat->vts_atrt;
3486 if(!DVDFileSeek_(ifop->file, sector * DVD_BLOCK_LEN))
3487 return 0;
3488
3489 vts_atrt = calloc(1, sizeof(vts_atrt_t));
3490 if(!vts_atrt)
3491 return 0;
3492
3493 ifofile->vts_atrt = vts_atrt;
3494
3495 if(!(DVDReadBytes(ifop->file, vts_atrt, VTS_ATRT_SIZE))) {
3496 free(vts_atrt);
3497 ifofile->vts_atrt = NULL;
3498 return 0;
3499 }
3500
3501 B2N_16(vts_atrt->nr_of_vtss);
3502 B2N_32(vts_atrt->last_byte);
3503
3504 CHECK_ZERO(vts_atrt->zero_1);
3505 CHECK_VALUE(vts_atrt->nr_of_vtss != 0);
3506 CHECK_VALUE(vts_atrt->nr_of_vtss < 100); /* ?? */
3507 CHECK_VALUE((uint32_t)vts_atrt->nr_of_vtss * (4 + VTS_ATTRIBUTES_MIN_SIZE) +
3508 VTS_ATRT_SIZE < vts_atrt->last_byte + 1);
3509
3510 info_length = vts_atrt->nr_of_vtss * sizeof(uint32_t);
3511 data = calloc(1, info_length);
3512 if(!data) {
3513 free(vts_atrt);
3514 ifofile->vts_atrt = NULL;
3515 return 0;
3516 }
3517
3518 vts_atrt->vts_atrt_offsets = data;
3519
3520 if(!(DVDReadBytes(ifop->file, data, info_length))) {
3521 free(data);
3522 free(vts_atrt);
3523 ifofile->vts_atrt = NULL;
3524 return 0;
3525 }
3526
3527 for(i = 0; i < vts_atrt->nr_of_vtss; i++) {
3528 B2N_32(data[i]);
3529 CHECK_VALUE(data[i] + VTS_ATTRIBUTES_MIN_SIZE < vts_atrt->last_byte + 1);
3530 }
3531
3532 info_length = vts_atrt->nr_of_vtss * sizeof(vts_attributes_t);
3533 vts_atrt->vts = calloc(1, info_length);
3534 if(!vts_atrt->vts) {
3535 free(data);
3536 free(vts_atrt);
3537 ifofile->vts_atrt = NULL;
3538 return 0;
3539 }
3540 for(i = 0; i < vts_atrt->nr_of_vtss; i++) {
3541 unsigned int offset = data[i];
3542 if(!ifoRead_VTS_ATTRIBUTES(ifofile, &(vts_atrt->vts[i]),
3543 (sector * DVD_BLOCK_LEN) + offset)) {
3544 free(data);
3545 free(vts_atrt);
3546 ifofile->vts_atrt = NULL;
3547 return 0;
3548 }
3549
3550 /* This assert can't be in ifoRead_VTS_ATTRIBUTES */
3551 CHECK_VALUE(offset + vts_atrt->vts[i].last_byte <= vts_atrt->last_byte + 1);
3552 /* Is this check correct? */
3553 }
3554
3555 return 1;
3556 }
3557
3558
3559 void ifoFree_VTS_ATRT(ifo_handle_t *ifofile) {
3560 if(!ifofile)
3561 return;
3562
3563 if(ifofile->vts_atrt) {
3564 free(ifofile->vts_atrt->vts);
3565 free(ifofile->vts_atrt->vts_atrt_offsets);
3566 free(ifofile->vts_atrt);
3567 ifofile->vts_atrt = NULL;
3568 }
3569 }
3570
3571
3572 int ifoRead_TXTDT_MGI(ifo_handle_t *ifofile) {
3573 struct ifo_handle_private_s *ifop = PRIV(ifofile);
3574 txtdt_mgi_t *txtdt_mgi;
3575
3576 if(!ifofile)
3577 return 0;
3578
3579 if(!ifofile->vmgi_mat)
3580 return 0;
3581
3582 /* Return successfully if there is nothing to read. */
3583 if(ifofile->vmgi_mat->txtdt_mgi == 0)
3584 return 1;
3585
3586 if(!DVDFileSeek_(ifop->file,
3587 ifofile->vmgi_mat->txtdt_mgi * DVD_BLOCK_LEN))
3588 return 0;
3589
3590 txtdt_mgi = calloc(1, sizeof(txtdt_mgi_t));
3591 if(!txtdt_mgi) {
3592 return 0;
3593 }
3594 ifofile->txtdt_mgi = txtdt_mgi;
3595
3596 if(!(DVDReadBytes(ifop->file, txtdt_mgi, TXTDT_MGI_SIZE))) {
3597 Log0(ifop->ctx, "Unable to read TXTDT_MGI.");
3598 free(txtdt_mgi);
3599 ifofile->txtdt_mgi = NULL;
3600 return 0;
3601 }
3602
3603 /* Log1(ifop->ctx, "-- Not done yet --\n"); */
3604 return 1;
3605 }
3606
3607 void ifoFree_TXTDT_MGI(ifo_handle_t *ifofile) {
3608 if(!ifofile)
3609 return;
3610
3611 if(ifofile->txtdt_mgi) {
3612 free(ifofile->txtdt_mgi);
3613 ifofile->txtdt_mgi = NULL;
3614 }
3615 }