2 * This file has been modified for the cdrkit suite.
4 * The behaviour and appearence of the program code below can differ to a major
5 * extent from the version distributed by the original author(s).
7 * For details, see Changelog file distributed with the cdrkit package. If you
8 * received this file from another source then ask the distributing person for
9 * a log of modifications.
13 /* @(#)ifo_read.c 1.5 04/03/04 joerg */
15 * Copyright (C) 2002 Olaf Beck <olaf_sc@yahoo.com>
16 * Jörg Schilling <schilling@fokus.gmd.de>
17 * (making the code portable)
19 * This program is free software; you can redistribute it and/or modify
20 * it under the terms of the GNU General Public License as published by
21 * the Free Software Foundation; either version 2 of the License, or (at
22 * your option) any later version.
24 * This program is distributed in the hope that it will be useful, but
25 * WITHOUT ANY WARRANTY; without even the implied warranty of
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
27 * General Public License for more details.
29 * You should have received a copy of the GNU General Public License
30 * along with this program; if not, write to the Free Software
31 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
35 * NOTE: This is a cut down version of libdvdread for genisoimage, due
36 * to portability issues with the current libdvdread according to
37 * the maintainer of genisoimage.
38 * This cut down version only reads from a harddisk file structure
39 * and it only implements the functions necessary inorder to make
40 * genisoimage produce valid DVD-Video images.
41 * DON'T USE THIS LIBRARY IN ANY OTHER PROGRAM GET THE REAL
47 #include "genisoimage.h"
55 #define MSGEREAD "Failed to read VIDEO_TS.IFO\n"
56 #define MSGESEEK "Failed to seek VIDEO_TS.IFO\n"
57 #define MSGEOPEN "Failed to open VIDEO_TS.IFO\n"
59 static ifo_handle_t *ifoReadVTSI(int file, ifo_handle_t * ifofile);
60 static ifo_handle_t *ifoReadVGMI(int file, ifo_handle_t * ifofile);
61 ifo_handle_t *ifoOpen(dvd_reader_t *dvd, int title);
62 static void ifoFree_TT_SRPT(ifo_handle_t *ifofile);
63 void ifoClose(ifo_handle_t * ifofile);
67 ifoReadVTSI(int file, ifo_handle_t *ifofile)
72 vtsi_mat_t * vtsi_mat;
74 /* Make the VMG part NULL */
75 ifofile->vmgi_mat = NULL;
76 ifofile->tt_srpt = NULL;
78 vtsi_mat = (vtsi_mat_t *)e_malloc(sizeof (vtsi_mat_t));
80 /* fprintf(stderr, "Memmory allocation error\n");*/
85 ifofile->vtsi_mat = vtsi_mat;
87 /* Last sector of VTS i.e. last sector of BUP */
91 if (lseek(file, offset, SEEK_SET) != offset) {
95 printf(stderr, MSGESEEK);
101 if (read(file, §or, sizeof (sector)) != sizeof (sector)) {
105 printf(stderr, MSGEREAD);
113 vtsi_mat->vts_last_sector = sector;
115 /* Last sector of IFO */
119 if (lseek(file, offset, SEEK_SET) != offset) {
123 printf(stderr, MSGESEEK);
129 if (read(file, §or, sizeof (sector)) != sizeof (sector)) {
133 printf(stderr, MSGEREAD);
141 vtsi_mat->vtsi_last_sector = sector;
144 /* Star sector of VTS Menu VOB */
148 if (lseek(file, offset, SEEK_SET) != offset) {
152 printf(stderr, MSGESEEK);
159 if (read(file, §or, sizeof (sector)) != sizeof (sector)) {
163 printf(stderr, MSGEREAD);
171 vtsi_mat->vtsm_vobs = sector;
174 /* Start sector of VTS Title VOB */
178 if (lseek(file, offset, SEEK_SET) != offset) {
182 printf(stderr, MSGESEEK);
189 if (read(file, §or, sizeof (sector)) != sizeof (sector)) {
193 printf(stderr, MSGEREAD);
201 vtsi_mat->vtstt_vobs = sector;
207 static ifo_handle_t *
208 ifoReadVGMI(int file, ifo_handle_t *ifofile)
215 vmgi_mat_t *vmgi_mat;
218 /* Make the VTS part null */
219 ifofile->vtsi_mat = NULL;
221 vmgi_mat = (vmgi_mat_t *)e_malloc(sizeof (vmgi_mat_t));
223 /* fprintf(stderr, "Memmory allocation error\n");*/
228 ifofile->vmgi_mat = vmgi_mat;
230 /* Last sector of VMG i.e. last sector of BUP */
234 if (lseek(file, offset, SEEK_SET) != offset) {
238 printf(stderr, MSGESEEK);
244 if (read(file, §or, sizeof (sector)) != sizeof (sector)) {
248 printf(stderr, MSGEREAD);
256 vmgi_mat->vmg_last_sector = sector;
258 /* Last sector of IFO */
262 if (lseek(file, offset, SEEK_SET) != offset) {
266 printf(stderr, MSGESEEK);
273 if (read(file, §or, sizeof (sector)) != sizeof (sector)) {
277 printf(stderr, MSGEREAD);
285 vmgi_mat->vmgi_last_sector = sector;
288 /* Number of VTS i.e. title sets */
292 if (lseek(file, offset, SEEK_SET) != offset) {
296 printf(stderr, MSGESEEK);
303 if (read(file, &titles, sizeof (titles)) != sizeof (titles)) {
307 printf(stderr, MSGEREAD);
315 vmgi_mat->vmg_nr_of_title_sets = titles;
318 /* Star sector of VMG Menu VOB */
322 if (lseek(file, offset, SEEK_SET) != offset) {
326 printf(stderr, MSGESEEK);
333 if (read(file, §or, sizeof (sector)) != sizeof (sector)) {
337 printf(stderr, MSGEREAD);
345 vmgi_mat->vmgm_vobs = sector;
348 /* Sector offset to TT_SRPT */
352 if (lseek(file, offset, SEEK_SET) != offset) {
356 printf(stderr, MSGESEEK);
363 if (read(file, §or, sizeof (sector)) != sizeof (sector)) {
367 printf(stderr, MSGEREAD);
375 vmgi_mat->tt_srpt = sector;
377 tt_srpt = (tt_srpt_t *)e_malloc(sizeof (tt_srpt_t));
379 /* fprintf(stderr, "Memmory allocation error\n");*/
384 ifofile->tt_srpt = tt_srpt;
387 /* Number of titles in TT_SRPT */
389 offset = 2048 * vmgi_mat->tt_srpt;
391 if (lseek(file, offset, SEEK_SET) != offset) {
395 printf(stderr, MSGESEEK);
400 if (read(file, &titles, sizeof (titles)) != sizeof (titles)) {
404 printf(stderr, MSGEREAD);
411 tt_srpt->nr_of_srpts = titles;
413 tt_srpt->title = (title_info_t *)e_malloc(sizeof (title_info_t) * tt_srpt->nr_of_srpts);
414 if (!tt_srpt->title) {
415 /* fprintf(stderr, "Memmory allocation error\n");*/
420 /* Start sector of each title in TT_SRPT */
422 for (counter = 0; counter < tt_srpt->nr_of_srpts; counter++) {
423 offset = (2048 * vmgi_mat->tt_srpt) + 8 + (counter * 12) + 8;
424 if (lseek(file, offset, SEEK_SET) != offset) {
428 printf(stderr, MSGESEEK);
434 if (read(file, §or, sizeof (sector)) != sizeof (sector)) {
438 printf(stderr, MSGEREAD);
446 tt_srpt->title[counter].title_set_sector = sector;
453 ifoOpen(dvd_reader_t *dvd, int title)
455 /* The main ifofile structure */
456 ifo_handle_t *ifofile;
458 /* File handles and offset */
461 char full_path[ PATH_MAX + 1 ];
463 /* Identifier of the IFO */
466 identifier[0] = '\0';
468 ifofile = (ifo_handle_t *)e_malloc(sizeof (ifo_handle_t));
470 memset(ifofile, 0, sizeof (ifo_handle_t));
473 snprintf(full_path, sizeof (full_path),
474 "%s/VIDEO_TS/VTS_%02d_0.IFO", dvd->path_root, title);
476 snprintf(full_path, sizeof (full_path),
477 "%s/VIDEO_TS/VIDEO_TS.IFO", dvd->path_root);
480 if ((file = open(full_path, O_RDONLY | O_BINARY)) == -1) {
484 printf(stderr, MSGEOPEN);
492 /* Determine if we have a VMGI or VTSI */
494 if (read(file, identifier, sizeof (identifier)) != sizeof (identifier)) {
498 printf(stderr, MSGEREAD);
503 if ((strstr("DVDVIDEO-VMG", identifier) != 0) && (title == 0)) {
504 ifofile = ifoReadVGMI(file, ifofile);
507 } else if ((strstr("DVDVIDEO-VTS", identifier) != 0) && (title != 0)) {
508 ifofile = ifoReadVTSI(file, ifofile);
513 errmsgno(EX_BAD, "Giving up this is not a valid IFO file\n");
515 fprintf(stderr, "Giving up this is not a valid IFO file\n");
525 ifoFree_TT_SRPT(ifo_handle_t *ifofile)
530 if (ifofile->tt_srpt) {
531 if (ifofile->tt_srpt->title) {
532 free(ifofile->tt_srpt->title);
534 free(ifofile->tt_srpt);
535 ifofile->tt_srpt = 0;
540 ifoClose(ifo_handle_t *ifofile)
546 ifoFree_TT_SRPT(ifofile);
548 if (ifofile->vmgi_mat) {
549 free(ifofile->vtsi_mat);
552 if (ifofile->vtsi_mat) {
553 free(ifofile->vtsi_mat);
559 #endif /* DVD_VIDEO */