summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorDaniel Caujolle-Bert <f1rmb@users.sourceforge.net>2003-10-13 11:47:11 +0000
committerDaniel Caujolle-Bert <f1rmb@users.sourceforge.net>2003-10-13 11:47:11 +0000
commitbf6b1747ae4bad46dbbf5fa2189a18b45c1d6b38 (patch)
tree60ac612a5a60675173b00d73bf9aefc3b5085e34 /src
parentee3b51f5df1a50e02d43d7cf81fbef19f61aa942 (diff)
downloadxine-lib-bf6b1747ae4bad46dbbf5fa2189a18b45c1d6b38.tar.gz
xine-lib-bf6b1747ae4bad46dbbf5fa2189a18b45c1d6b38.tar.bz2
Add vcdx plugin(CVS) and dependent libs(no CVS, broken API).
Ability to use external libs (--with-external-vcdnav), disable vcdx build and build old vcd instead (--disable-vcdx). This last feature is useful since vcdx take /hours/ to start playback, that can help people who don't want vcd navigation. Remove all .la files, even in subdirs (eg: post) CVS patchset: 5505 CVS date: 2003/10/13 11:47:11
Diffstat (limited to 'src')
-rw-r--r--src/input/Makefile.am7
-rw-r--r--src/input/libdvdnav/diff_against_cvs.patch57
-rw-r--r--src/input/vcd/Makefile.am47
-rw-r--r--src/input/vcd/libcdio/Makefile.am71
-rw-r--r--src/input/vcd/libcdio/_cdio_bincue.c962
-rw-r--r--src/input/vcd/libcdio/_cdio_bsdi.c710
-rw-r--r--src/input/vcd/libcdio/_cdio_freebsd.c635
-rw-r--r--src/input/vcd/libcdio/_cdio_generic.c195
-rw-r--r--src/input/vcd/libcdio/_cdio_linux.c1028
-rw-r--r--src/input/vcd/libcdio/_cdio_nrg.c938
-rw-r--r--src/input/vcd/libcdio/_cdio_osx.c864
-rw-r--r--src/input/vcd/libcdio/_cdio_stdio.c212
-rw-r--r--src/input/vcd/libcdio/_cdio_stdio.h40
-rw-r--r--src/input/vcd/libcdio/_cdio_stream.c198
-rw-r--r--src/input/vcd/libcdio/_cdio_stream.h124
-rw-r--r--src/input/vcd/libcdio/_cdio_sunos.c734
-rw-r--r--src/input/vcd/libcdio/_cdio_win32.c1168
-rw-r--r--src/input/vcd/libcdio/bytesex.h196
-rw-r--r--src/input/vcd/libcdio/bytesex_asm.h123
-rw-r--r--src/input/vcd/libcdio/cd_types.c294
-rw-r--r--src/input/vcd/libcdio/cdio.c887
-rw-r--r--src/input/vcd/libcdio/cdio/Makefile.am12
-rw-r--r--src/input/vcd/libcdio/cdio/cd_types.h114
-rw-r--r--src/input/vcd/libcdio/cdio/cdio.h445
-rw-r--r--src/input/vcd/libcdio/cdio/iso9660.h428
-rw-r--r--src/input/vcd/libcdio/cdio/logging.h66
-rw-r--r--src/input/vcd/libcdio/cdio/sector.h162
-rw-r--r--src/input/vcd/libcdio/cdio/types.h236
-rw-r--r--src/input/vcd/libcdio/cdio/util.h109
-rw-r--r--src/input/vcd/libcdio/cdio/version.h1
-rw-r--r--src/input/vcd/libcdio/cdio/xa.h121
-rw-r--r--src/input/vcd/libcdio/cdio_assert.h59
-rw-r--r--src/input/vcd/libcdio/cdio_private.h304
-rw-r--r--src/input/vcd/libcdio/ds.c249
-rw-r--r--src/input/vcd/libcdio/ds.h73
-rw-r--r--src/input/vcd/libcdio/iso9660.c885
-rw-r--r--src/input/vcd/libcdio/iso9660_fs.c357
-rw-r--r--src/input/vcd/libcdio/iso9660_private.h85
-rw-r--r--src/input/vcd/libcdio/logging.c142
-rw-r--r--src/input/vcd/libcdio/scsi_mmc.h63
-rw-r--r--src/input/vcd/libcdio/sector.c111
-rw-r--r--src/input/vcd/libcdio/util.c192
-rw-r--r--src/input/vcd/libcdio/xa.c136
-rw-r--r--src/input/vcd/libvcd/Makefile.am84
-rw-r--r--src/input/vcd/libvcd/assert.h55
-rw-r--r--src/input/vcd/libvcd/bitvec.h131
-rw-r--r--src/input/vcd/libvcd/bytesex.h199
-rw-r--r--src/input/vcd/libvcd/bytesex_asm.h123
-rw-r--r--src/input/vcd/libvcd/data_structures.c526
-rw-r--r--src/input/vcd/libvcd/data_structures.h126
-rw-r--r--src/input/vcd/libvcd/dict.h165
-rw-r--r--src/input/vcd/libvcd/directory.c506
-rw-r--r--src/input/vcd/libvcd/directory.h64
-rw-r--r--src/input/vcd/libvcd/files.c1018
-rw-r--r--src/input/vcd/libvcd/image.c108
-rw-r--r--src/input/vcd/libvcd/image_bincue.c252
-rw-r--r--src/input/vcd/libvcd/image_cdrdao.c312
-rw-r--r--src/input/vcd/libvcd/image_nrg.c350
-rw-r--r--src/input/vcd/libvcd/image_sink.h80
-rw-r--r--src/input/vcd/libvcd/inf.c596
-rw-r--r--src/input/vcd/libvcd/info.c1753
-rw-r--r--src/input/vcd/libvcd/info_private.c318
-rw-r--r--src/input/vcd/libvcd/info_private.h113
-rw-r--r--src/input/vcd/libvcd/libvcd/Makefile.am12
-rw-r--r--src/input/vcd/libvcd/libvcd/files.h101
-rw-r--r--src/input/vcd/libvcd/libvcd/files_private.h562
-rw-r--r--src/input/vcd/libvcd/libvcd/inf.h300
-rw-r--r--src/input/vcd/libvcd/libvcd/info.h766
-rw-r--r--src/input/vcd/libvcd/libvcd/logging.h72
-rw-r--r--src/input/vcd/libvcd/libvcd/sector.h118
-rw-r--r--src/input/vcd/libvcd/libvcd/types.h97
-rw-r--r--src/input/vcd/libvcd/libvcd/version.h1
-rw-r--r--src/input/vcd/libvcd/logging.c132
-rw-r--r--src/input/vcd/libvcd/mpeg.c1177
-rw-r--r--src/input/vcd/libvcd/mpeg.h194
-rw-r--r--src/input/vcd/libvcd/mpeg_stream.c487
-rw-r--r--src/input/vcd/libvcd/mpeg_stream.h116
-rw-r--r--src/input/vcd/libvcd/obj.h202
-rw-r--r--src/input/vcd/libvcd/pbc.c878
-rw-r--r--src/input/vcd/libvcd/pbc.h157
-rw-r--r--src/input/vcd/libvcd/salloc.c212
-rw-r--r--src/input/vcd/libvcd/salloc.h56
-rw-r--r--src/input/vcd/libvcd/sector.c290
-rw-r--r--src/input/vcd/libvcd/sector_private.h256
-rw-r--r--src/input/vcd/libvcd/stream.c276
-rw-r--r--src/input/vcd/libvcd/stream.h123
-rw-r--r--src/input/vcd/libvcd/stream_stdio.c236
-rw-r--r--src/input/vcd/libvcd/stream_stdio.h43
-rw-r--r--src/input/vcd/libvcd/util.c179
-rw-r--r--src/input/vcd/libvcd/util.h108
-rw-r--r--src/input/vcd/libvcd/vcd.c2407
-rw-r--r--src/input/vcd/libvcd/vcd.h176
-rw-r--r--src/input/vcd/libvcd/vcd_read.c108
-rw-r--r--src/input/vcd/libvcd/vcd_read.h36
-rw-r--r--src/input/vcd/vcdio.c242
-rw-r--r--src/input/vcd/vcdio.h66
-rw-r--r--src/input/vcd/vcdplayer.c1040
-rw-r--r--src/input/vcd/vcdplayer.h297
-rw-r--r--src/input/vcd/xine-extra.c148
-rw-r--r--src/input/vcd/xine-extra.h100
-rw-r--r--src/input/vcd/xineplug_inp_vcd.c1805
101 files changed, 34236 insertions, 59 deletions
diff --git a/src/input/Makefile.am b/src/input/Makefile.am
index d14bff37f..9151b269a 100644
--- a/src/input/Makefile.am
+++ b/src/input/Makefile.am
@@ -3,9 +3,9 @@ include $(top_srcdir)/misc/Makefile.common
EXTRA_DIST = input_dvd.c input_vcd.c input_gnome_vfs.c input_rtp.c
if HAVE_DVDNAV
-SUBDIRS = dvb libreal librtsp
+SUBDIRS = vcd dvb libreal librtsp
else
-SUBDIRS = dvb libreal librtsp libdvdnav
+SUBDIRS = vcd dvb libreal librtsp libdvdnav
endif
libdir = $(XINE_PLUGINDIR)
@@ -18,7 +18,10 @@ libdir = $(XINE_PLUGINDIR)
if HAVE_CDROM_IOCTLS
in_dvd = xineplug_inp_dvd.la
+if ENABLE_VCDX
+else
in_vcd = xineplug_inp_vcd.la
+endif
#in_cda = xineplug_inp_cda.la
endif
diff --git a/src/input/libdvdnav/diff_against_cvs.patch b/src/input/libdvdnav/diff_against_cvs.patch
deleted file mode 100644
index 607923d4d..000000000
--- a/src/input/libdvdnav/diff_against_cvs.patch
+++ /dev/null
@@ -1,57 +0,0 @@
---- src/input/libdvdnav/dvdnav.c
-+++ src/input/libdvdnav/dvdnav.c
-@@ -71,7 +71,7 @@
- struct timeval time;
-
- /* Create a new structure */
-- fprintf(MSG_OUT, "libdvdnav: Using dvdnav version %s from http://dvd.sf.net\n", VERSION);
-+ fprintf(MSG_OUT, "libdvdnav: Using dvdnav version %s from http://xine.sf.net\n", VERSION);
-
- (*dest) = NULL;
- this = (dvdnav_t*)malloc(sizeof(dvdnav_t));
---- src/input/libdvdnav/dvd_reader.c
-+++ src/input/libdvdnav/dvd_reader.c
-@@ -44,6 +44,7 @@
- #include <mntent.h>
- #endif
-
-+#include "compat.h"
- #include "dvd_udf.h"
- #include "dvd_input.h"
- #include "dvd_reader.h"
-@@ -393,7 +394,7 @@
-
- if( cdir >= 0 ) {
- chdir( path_copy );
-- new_path = getcwd( NULL, PATH_MAX );
-+ new_path = getcwd( NULL, XINE_PATH_MAX );
- fchdir( cdir );
- close( cdir );
- if( new_path ) {
-@@ -573,7 +574,7 @@
-
- static int findDVDFile( dvd_reader_t *dvd, const char *file, char *filename )
- {
-- char video_path[ PATH_MAX + 1 ];
-+ char video_path[ XINE_PATH_MAX + 1 ];
- const char *nodirfile;
- int ret;
-
-@@ -607,7 +608,7 @@
- */
- static dvd_file_t *DVDOpenFilePath( dvd_reader_t *dvd, char *filename )
- {
-- char full_path[ PATH_MAX + 1 ];
-+ char full_path[ XINE_PATH_MAX + 1 ];
- dvd_file_t *dvd_file;
- struct stat fileinfo;
- dvd_input_t dev;
-@@ -691,7 +692,7 @@
- static dvd_file_t *DVDOpenVOBPath( dvd_reader_t *dvd, int title, int menu )
- {
- char filename[ MAX_UDF_FILE_NAME_LEN ];
-- char full_path[ PATH_MAX + 1 ];
-+ char full_path[ XINE_PATH_MAX + 1 ];
- struct stat fileinfo;
- dvd_file_t *dvd_file;
- int i;
diff --git a/src/input/vcd/Makefile.am b/src/input/vcd/Makefile.am
new file mode 100644
index 000000000..027c9621f
--- /dev/null
+++ b/src/input/vcd/Makefile.am
@@ -0,0 +1,47 @@
+SUBDIRS = libcdio libvcd
+
+libdir = $(XINE_PLUGINDIR)
+
+LIBTOOL = $(SHELL) $(top_builddir)/libtool-nofpic
+XINE_LIB = $(top_builddir)/src/xine-engine/libxine.la
+
+vcdx_SRCS = xineplug_inp_vcd.c vcdplayer.c vcdio.c xine-extra.c
+
+EXTRA_DIST = $(vcdx_SRCS)
+
+if ENABLE_VCDX
+lib_LTLIBRARIES = xineplug_inp_vcdx.la
+
+AM_CFLAGS = $(LIBCDIO_CFLAGS) $(LIBVCD_CFLAGS)
+
+xineplug_inp_vcdx_la_SOURCES = $(vcdx_SRCS)
+xineplug_inp_vcdx_la_LDFLAGS = -avoid-version -module
+if HAVE_VCDNAV
+xineplug_inp_vcdx_la_LIBADD = $(XINE_LIB) $(LIBVCDINFO_LIBS)
+else
+xineplug_inp_vcdx_la_LIBADD = $(XINE_LIB) $(LIBVCD_LIBS) $(LIBVCDINFO_LIBS)
+endif
+
+endif
+
+noinst_HEADERS = vcdio.h vcdplayer.h xine-extra.h
+
+debug:
+ @list='$(SUBDIRS)'; for subdir in $$list; do \
+ (cd $$subdir && $(MAKE) $@) || exit;\
+ done;
+ @$(MAKE) CFLAGS="$(DEBUG_CFLAGS) $(LIBCDIO_CFLAGS) $(LIBVCD_CFLAGS)"
+
+install-debug: debug
+ @list='$(SUBDIRS)'; for subdir in $$list; do \
+ (cd $$subdir && $(MAKE) $@) || exit;\
+ done;
+ @$(MAKE) $(AM_MAKEFLAGS) install-exec-am install-data-am
+
+mostlyclean-generic:
+ -rm -f *~ \#* .*~ .\#*
+
+maintainer-clean-generic:
+ -@echo "This command is intended for maintainers to use;"
+ -@echo "it deletes files that may require special tools to rebuild."
+ -rm -f Makefile.in
diff --git a/src/input/vcd/libcdio/Makefile.am b/src/input/vcd/libcdio/Makefile.am
new file mode 100644
index 000000000..eef45b69f
--- /dev/null
+++ b/src/input/vcd/libcdio/Makefile.am
@@ -0,0 +1,71 @@
+SUBDIRS = cdio
+
+LIBTOOL = $(SHELL) $(top_builddir)/libtool-nofpic
+INCLUDES = $(LIBCDIO_CFLAGS)
+
+libcdio_SRCS = \
+ _cdio_bincue.c \
+ _cdio_bsdi.c \
+ _cdio_freebsd.c \
+ _cdio_generic.c \
+ _cdio_linux.c \
+ _cdio_osx.c \
+ _cdio_nrg.c \
+ _cdio_stdio.c \
+ _cdio_stream.c \
+ _cdio_sunos.c \
+ _cdio_win32.c \
+ cdio.c \
+ cd_types.c \
+ ds.c \
+ logging.c \
+ sector.c \
+ util.c
+libiso9660_SRCS = \
+ iso9660.c \
+ iso9660_fs.c \
+ xa.c
+
+if ENABLE_VCDX
+if HAVE_VCDNAV
+EXTRA_DIST = $(libcdio_SRCS) $(libio9660_SRCS)
+else
+noinst_LTLIBRARIES = libcdio.la libiso9660.la
+libcdio_la_SOURCES = $(libcdio_SRCS)
+libcdio_la_LDFLAGS = -avoid-version -module
+libiso9660_la_SOURCES = $(libiso9660_SRCS)
+libiso9660_la_LDFLAGS = -avoid-version -module
+endif
+endif
+
+noinst_HEADERS = \
+ bytesex_asm.h \
+ cdio_assert.h \
+ _cdio_stdio.h \
+ ds.h \
+ scsi_mmc.h \
+ bytesex.h \
+ cdio_private.h \
+ _cdio_stream.h \
+ iso9660_private.h
+
+
+debug:
+ @list='$(SUBDIRS)'; for subdir in $$list; do \
+ (cd $$subdir && $(MAKE) $@) || exit;\
+ done;
+ @$(MAKE) CFLAGS="$(DEBUG_CFLAGS)"
+
+install-debug: debug
+ @list='$(SUBDIRS)'; for subdir in $$list; do \
+ (cd $$subdir && $(MAKE) $@) || exit;\
+ done;
+ @$(MAKE) $(AM_MAKEFLAGS) install-exec-am install-data-am
+
+mostlyclean-generic:
+ -rm -f *~ \#* .*~ .\#*
+
+maintainer-clean-generic:
+ -@echo "This command is intended for maintainers to use;"
+ -@echo "it deletes files that may require special tools to rebuild."
+ -rm -f Makefile.in
diff --git a/src/input/vcd/libcdio/_cdio_bincue.c b/src/input/vcd/libcdio/_cdio_bincue.c
new file mode 100644
index 000000000..04293c483
--- /dev/null
+++ b/src/input/vcd/libcdio/_cdio_bincue.c
@@ -0,0 +1,962 @@
+/*
+ $Id: _cdio_bincue.c,v 1.1 2003/10/13 11:47:11 f1rmb Exp $
+
+ Copyright (C) 2001 Herbert Valerio Riedel <hvr@gnu.org>
+ Copyright (C) 2002,2003 Rocky Bernstein <rocky@panix.com>
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+*/
+
+/* This code implements low-level access functions for a CD images
+ residing inside a disk file (*.bin) and its associated cue sheet.
+ (*.cue).
+*/
+
+static const char _rcsid[] = "$Id: _cdio_bincue.c,v 1.1 2003/10/13 11:47:11 f1rmb Exp $";
+
+#include "cdio_assert.h"
+#include "cdio_private.h"
+#include "_cdio_stdio.h"
+
+#include <cdio/logging.h>
+#include <cdio/sector.h>
+#include <cdio/util.h>
+
+#ifdef HAVE_STDIO_H
+#include <stdio.h>
+#endif
+#ifdef HAVE_STDLIB_H
+#include <stdlib.h>
+#endif
+#ifdef HAVE_STRING_H
+#include <string.h>
+#endif
+#ifdef HAVE_STRINGS_H
+#include <strings.h>
+#endif
+#ifdef HAVE_GLOB_H
+#include <glob.h>
+#endif
+#include <ctype.h>
+
+/* FIXME: should put in a common definition somewhere. */
+#ifdef HAVE_BZERO
+#define BZERO(ptr, size) bzero(ptr, size)
+#elif HAVE_MEMSET
+#define BZERO(ptr, size) memset(ptr, 0, size)
+#else
+ Error -- you need either memset or bzero
+#endif
+
+/* reader */
+
+#define DEFAULT_CDIO_DEVICE "videocd.bin"
+#define DEFAULT_CDIO_CUE "videocd.cue"
+
+typedef struct {
+ track_t track_num; /* Probably is index+1 */
+ msf_t start_msf;
+ lba_t start_lba;
+ int start_index;
+ int sec_count; /* Number of sectors in this track. Does not
+ include pregap */
+ int num_indices;
+ int flags; /* "DCP", "4CH", "PRE" */
+ track_format_t track_format;
+ bool track_green;
+ uint16_t datasize; /* How much is in the portion we return back? */
+ uint16_t datastart; /* Offset from begining that data starts */
+ uint16_t endsize; /* How much stuff at the end to skip over. This
+ stuff may have error correction (EDC, or ECC).*/
+ uint16_t blocksize; /* total block size = start + size + end */
+
+
+} track_info_t;
+
+typedef struct {
+ /* Things common to all drivers like this.
+ This must be first. */
+ generic_img_private_t gen;
+ internal_position_t pos;
+
+ bool sector_2336; /* Playstation (PSX) uses 2336-byte sectors */
+
+ char *cue_name;
+ char *mcn; /* Media catalog number. */
+ track_info_t tocent[100]; /* entry info for each track */
+ track_t total_tracks; /* number of tracks in image */
+ track_t first_track_num; /* track number of first track */
+ bool have_cue;
+} _img_private_t;
+
+static bool _cdio_image_read_cue (_img_private_t *_obj);
+static uint32_t _cdio_stat_size (void *env);
+
+/*!
+ Initialize image structures.
+ */
+static bool
+_cdio_init (_img_private_t *_obj)
+{
+ lsn_t lead_lsn;
+
+ if (_obj->gen.init)
+ return false;
+
+ if (!(_obj->gen.data_source = cdio_stdio_new (_obj->gen.source_name))) {
+ cdio_error ("init failed");
+ return false;
+ }
+
+ /* Have to set init before calling _cdio_stat_size() or we will
+ get into infinite recursion calling passing right here.
+ */
+ _obj->gen.init = true;
+
+ lead_lsn = _cdio_stat_size( (_img_private_t *) _obj);
+
+ if (-1 == lead_lsn)
+ return false;
+
+ /* Read in CUE sheet. */
+ if ((_obj->cue_name != NULL)) {
+ _obj->have_cue = _cdio_image_read_cue(_obj);
+ }
+
+ if (!_obj->have_cue ) {
+ /* Time to fake things...
+ Make one big track, track 0 and 1 are the same.
+ We are guessing stuff starts at msf 00:04:00 - 2 for the 150
+ sector pregap and 2 for the cue information.
+ */
+ track_info_t *this_track=&(_obj->tocent[0]);
+ int blocksize = _obj->sector_2336
+ ? M2RAW_SECTOR_SIZE : CDIO_CD_FRAMESIZE_RAW;
+
+ _obj->total_tracks = 2;
+ _obj->first_track_num = 1;
+ this_track->start_msf.m = to_bcd8(0);
+ this_track->start_msf.s = to_bcd8(4);
+ this_track->start_msf.f = to_bcd8(0);
+ this_track->start_lba = cdio_msf_to_lba(&this_track->start_msf);
+ this_track->blocksize = blocksize;
+ this_track->track_format= TRACK_FORMAT_XA;
+ this_track->track_green = true;
+
+
+ _obj->tocent[1] = _obj->tocent[0];
+ }
+
+ /* Fake out leadout track and sector count for last track*/
+ cdio_lsn_to_msf (lead_lsn, &_obj->tocent[_obj->total_tracks].start_msf);
+ _obj->tocent[_obj->total_tracks].start_lba = cdio_lsn_to_lba(lead_lsn);
+ _obj->tocent[_obj->total_tracks-1].sec_count =
+ cdio_lsn_to_lba(lead_lsn - _obj->tocent[_obj->total_tracks-1].start_lba);
+
+ return true;
+}
+
+/*!
+ Reads into buf the next size bytes.
+ Returns -1 on error.
+ Would be libc's seek() but we have to adjust for the extra track header
+ information in each sector.
+*/
+static off_t
+_cdio_lseek (void *env, off_t offset, int whence)
+{
+ _img_private_t *_obj = env;
+
+ /* real_offset is the real byte offset inside the disk image
+ The number below was determined empirically. I'm guessing
+ the 1st 24 bytes of a bin file are used for something.
+ */
+ off_t real_offset=0;
+
+ unsigned int i;
+
+ _obj->pos.lba = 0;
+ for (i=0; i<_obj->total_tracks; i++) {
+ track_info_t *this_track=&(_obj->tocent[i]);
+ _obj->pos.index = i;
+ if ( (this_track->sec_count*this_track->datasize) >= offset) {
+ int blocks = offset / this_track->datasize;
+ int rem = offset % this_track->datasize;
+ int block_offset = blocks * this_track->blocksize;
+ real_offset += block_offset + rem;
+ _obj->pos.buff_offset = rem;
+ _obj->pos.lba += blocks;
+ break;
+ }
+ real_offset += this_track->sec_count*this_track->blocksize;
+ offset -= this_track->sec_count*this_track->datasize;
+ _obj->pos.lba += this_track->sec_count;
+ }
+
+ if (i==_obj->total_tracks) {
+ cdio_warn ("seeking outside range of disk image");
+ return -1;
+ } else {
+ real_offset += _obj->tocent[i].datastart;
+ return cdio_stream_seek(_obj->gen.data_source, real_offset, whence);
+ }
+}
+
+/*!
+ Reads into buf the next size bytes.
+ Returns -1 on error.
+ FIXME:
+ At present we assume a read doesn't cross sector or track
+ boundaries.
+*/
+static ssize_t
+_cdio_read (void *env, void *data, size_t size)
+{
+ _img_private_t *_obj = env;
+ char buf[CDIO_CD_FRAMESIZE_RAW] = { 0, };
+ char *p = data;
+ ssize_t final_size=0;
+ ssize_t this_size;
+ track_info_t *this_track=&(_obj->tocent[_obj->pos.index]);
+ ssize_t skip_size = this_track->datastart + this_track->endsize;
+
+ while (size > 0) {
+ int rem = this_track->datasize - _obj->pos.buff_offset;
+ if (size <= rem) {
+ this_size = cdio_stream_read(_obj->gen.data_source, buf, size, 1);
+ final_size += this_size;
+ memcpy (p, buf, this_size);
+ break;
+ }
+
+ /* Finish off reading this sector. */
+ cdio_warn ("Reading across block boundaries not finished");
+
+ size -= rem;
+ this_size = cdio_stream_read(_obj->gen.data_source, buf, rem, 1);
+ final_size += this_size;
+ memcpy (p, buf, this_size);
+ p += this_size;
+ this_size = cdio_stream_read(_obj->gen.data_source, buf, rem, 1);
+
+ /* Skip over stuff at end of this sector and the beginning of the next.
+ */
+ cdio_stream_read(_obj->gen.data_source, buf, skip_size, 1);
+
+ /* Get ready to read another sector. */
+ _obj->pos.buff_offset=0;
+ _obj->pos.lba++;
+
+ /* Have gone into next track. */
+ if (_obj->pos.lba >= _obj->tocent[_obj->pos.index+1].start_lba) {
+ _obj->pos.index++;
+ this_track=&(_obj->tocent[_obj->pos.index]);
+ skip_size = this_track->datastart + this_track->endsize;
+ }
+ }
+ return final_size;
+}
+
+/*!
+ Return the size of the CD in logical block address (LBA) units.
+ */
+static uint32_t
+_cdio_stat_size (void *env)
+{
+ _img_private_t *_obj = env;
+ long size;
+ int blocksize = _obj->sector_2336
+ ? M2RAW_SECTOR_SIZE : CDIO_CD_FRAMESIZE_RAW;
+
+ _cdio_init (_obj);
+
+ size = cdio_stream_stat (_obj->gen.data_source);
+
+ if (size % blocksize)
+ {
+ cdio_warn ("image %s size (%ld) not multiple of blocksize (%d)",
+ _obj->gen.source_name, size, blocksize);
+ if (size % M2RAW_SECTOR_SIZE == 0)
+ cdio_warn ("this may be a 2336-type disc image");
+ else if (size % CDIO_CD_FRAMESIZE_RAW == 0)
+ cdio_warn ("this may be a 2352-type disc image");
+ /* exit (EXIT_FAILURE); */
+ }
+
+ size /= blocksize;
+
+ return size;
+}
+
+#define MAXLINE 512
+
+static bool
+_cdio_image_read_cue (_img_private_t *_obj)
+{
+ FILE *fp;
+ char line[MAXLINE];
+
+ int track_num;
+ int min,sec,frame;
+ int blocksize;
+ int start_index;
+ bool seen_first_index_for_track=false;
+
+ if ( _obj == NULL || _obj->cue_name == NULL ) return false;
+
+ fp = fopen (_obj->cue_name, "r");
+ if (fp == NULL) return false;
+
+ _obj->total_tracks=0;
+ _obj->first_track_num=1;
+ _obj->mcn=NULL;
+
+ while ((fgets(line, MAXLINE, fp)) != NULL) {
+ char s[80];
+ char *p;
+ /*printf("Retrieved line of length %zu :\n", read);
+ printf("%s", line); */
+ for (p=line; isspace(*p); p++) ;
+ if (1==sscanf(p, "FILE \"%80s[^\"]", s)) {
+ /* Should expand file name based on cue file basename.
+ free(_obj->bin_file);
+ _obj->bin_file = strdup(s);
+ */
+ /* printf("Found file name %s\n", s); */
+ } else if (1==sscanf(p, "CATALOG %80s", s)) {
+ _obj->mcn = strdup(s);
+ } else if (2==sscanf(p, "TRACK %d MODE2/%d", &track_num, &blocksize)) {
+ track_info_t *this_track=&(_obj->tocent[_obj->total_tracks]);
+ this_track->track_num = track_num;
+ this_track->num_indices = 0;
+ this_track->track_format= TRACK_FORMAT_XA;
+ this_track->track_green = true;
+ _obj->total_tracks++;
+ seen_first_index_for_track=false;
+ /*printf("Added track %d with blocksize %d\n", track_num, blocksize);*/
+
+ this_track->blocksize = blocksize;
+ switch(blocksize) {
+ case 2336:
+ this_track->datastart = CDIO_CD_SYNC_SIZE + CDIO_CD_HEADER_SIZE;
+ this_track->datasize = M2RAW_SECTOR_SIZE;
+ this_track->endsize = 0;
+ break;
+ default:
+ cdio_warn ("Unknown MODE2 size %d. Assuming 2352", blocksize);
+ case 2352:
+ if (_obj->sector_2336) {
+ this_track->datastart = 0;
+ this_track->datasize = M2RAW_SECTOR_SIZE;
+ this_track->endsize = blocksize - 2336;
+ } else {
+ this_track->datastart = CDIO_CD_SYNC_SIZE + CDIO_CD_HEADER_SIZE +
+ CDIO_CD_SUBHEADER_SIZE;
+ this_track->datasize = CDIO_CD_FRAMESIZE;
+ this_track->endsize = CDIO_CD_SYNC_SIZE + CDIO_CD_ECC_SIZE;
+ }
+ break;
+ }
+
+ } else if (2==sscanf(p, "TRACK %d MODE1/%d", &track_num, &blocksize)) {
+ track_info_t *this_track=&(_obj->tocent[_obj->total_tracks]);
+ this_track->blocksize = blocksize;
+ this_track->blocksize = blocksize;
+ switch(blocksize) {
+ case 2048:
+ /* Is the below correct? */
+ this_track->datastart = 0;
+ this_track->datasize = CDIO_CD_FRAMESIZE;
+ this_track->endsize = 0;
+ break;
+ default:
+ cdio_warn ("Unknown MODE1 size %d. Assuming 2352", blocksize);
+ case 2352:
+ this_track->datastart = CDIO_CD_SYNC_SIZE + CDIO_CD_HEADER_SIZE;
+ this_track->datasize = CDIO_CD_FRAMESIZE;
+ this_track->endsize = CDIO_CD_EDC_SIZE + CDIO_CD_M1F1_ZERO_SIZE
+ + CDIO_CD_ECC_SIZE;
+ }
+
+ this_track->track_num = track_num;
+ this_track->num_indices = 0;
+ this_track->track_format = TRACK_FORMAT_DATA;
+ this_track->track_green = false;
+ _obj->total_tracks++;
+ seen_first_index_for_track=false;
+ /*printf("Added track %d with blocksize %d\n", track_num, blocksize);*/
+
+ } else if (1==sscanf(p, "TRACK %d AUDIO", &track_num)) {
+ track_info_t *this_track=&(_obj->tocent[_obj->total_tracks]);
+ this_track->blocksize = CDIO_CD_FRAMESIZE_RAW;
+ this_track->datasize = CDIO_CD_FRAMESIZE_RAW;
+ this_track->datastart = 0;
+ this_track->endsize = 0;
+ this_track->track_num = track_num;
+ this_track->num_indices = 0;
+ this_track->track_format = TRACK_FORMAT_AUDIO;
+ this_track->track_green = false;
+ _obj->total_tracks++;
+ seen_first_index_for_track=false;
+
+ } else if (4==sscanf(p, "INDEX %d %d:%d:%d",
+ &start_index, &min, &sec, &frame)) {
+ track_info_t *this_track=&(_obj->tocent[_obj->total_tracks-1]);
+ /* FIXME! all of this is a big hack.
+ If start_index == 0, then this is the "last_cue" information.
+ The +2 below seconds is to adjust for the 150 pregap.
+ */
+ if (start_index != 0) {
+ if (!seen_first_index_for_track) {
+ this_track->start_index = start_index;
+ sec += 2;
+ if (sec >= 60) {
+ min++;
+ sec -= 60;
+ }
+ this_track->start_msf.m = to_bcd8 (min);
+ this_track->start_msf.s = to_bcd8 (sec);
+ this_track->start_msf.f = to_bcd8 (frame);
+ this_track->start_lba = cdio_msf_to_lba(&this_track->start_msf);
+ seen_first_index_for_track=true;
+ }
+
+ if (_obj->total_tracks > 1) {
+ /* Figure out number of sectors for previous track */
+ track_info_t *prev_track=&(_obj->tocent[_obj->total_tracks-2]);
+ if ( this_track->start_lba < prev_track->start_lba ) {
+ cdio_warn("track %d at LBA %lu starts before track %d at LBA %lu",
+ _obj->total_tracks,
+ (unsigned long int) this_track->start_lba,
+ _obj->total_tracks-1,
+ (unsigned long int) prev_track->start_lba);
+ prev_track->sec_count = 0;
+ } else if ( this_track->start_lba >= prev_track->start_lba
+ + CDIO_PREGAP_SECTORS ) {
+ prev_track->sec_count = this_track->start_lba -
+ prev_track->start_lba - CDIO_PREGAP_SECTORS ;
+ } else {
+ cdio_warn ("%lu fewer than pregap (%d) sectors in track %d",
+ (long unsigned int)
+ this_track->start_lba - prev_track->start_lba,
+ CDIO_PREGAP_SECTORS,
+ _obj->total_tracks-1);
+ /* Include pregap portion in sec_count. Maybe the pregap
+ was omitted. */
+ prev_track->sec_count = this_track->start_lba -
+ prev_track->start_lba;
+ }
+ }
+ this_track->num_indices++;
+ }
+ }
+ }
+ _obj->have_cue = _obj->total_tracks != 0;
+
+ fclose (fp);
+ return true;
+}
+
+/*!
+ Reads a single audio sector from CD device into data starting
+ from lsn. Returns 0 if no error.
+ */
+static int
+_cdio_read_audio_sectors (void *env, void *data, lsn_t lsn,
+ unsigned int nblocks)
+{
+ _img_private_t *_obj = env;
+ int ret;
+
+ _cdio_init (_obj);
+
+ /* Why the adjustment of 272, I don't know. It seems to work though */
+ if (lsn != 0) {
+ ret = cdio_stream_seek (_obj->gen.data_source,
+ (lsn * CDIO_CD_FRAMESIZE_RAW) - 272, SEEK_SET);
+ if (ret!=0) return ret;
+
+ ret = cdio_stream_read (_obj->gen.data_source, data,
+ CDIO_CD_FRAMESIZE_RAW, nblocks);
+ } else {
+ /* We need to pad out the first 272 bytes with 0's */
+ BZERO(data, 272);
+
+ ret = cdio_stream_seek (_obj->gen.data_source, 0, SEEK_SET);
+
+ if (ret!=0) return ret;
+
+ ret = cdio_stream_read (_obj->gen.data_source, (uint8_t *) data+272,
+ CDIO_CD_FRAMESIZE_RAW - 272, nblocks);
+ }
+
+ return ret;
+}
+
+/*!
+ Reads a single mode2 sector from cd device into data starting
+ from lsn. Returns 0 if no error.
+ */
+static int
+_cdio_read_mode2_sector (void *env, void *data, lsn_t lsn,
+ bool mode2_form2)
+{
+ _img_private_t *_obj = env;
+ int ret;
+ char buf[CDIO_CD_FRAMESIZE_RAW] = { 0, };
+ int blocksize = _obj->sector_2336
+ ? M2RAW_SECTOR_SIZE : CDIO_CD_FRAMESIZE_RAW;
+
+ _cdio_init (_obj);
+
+ ret = cdio_stream_seek (_obj->gen.data_source, lsn * blocksize, SEEK_SET);
+ if (ret!=0) return ret;
+
+ ret = cdio_stream_read (_obj->gen.data_source,
+ _obj->sector_2336
+ ? (buf + CDIO_CD_SYNC_SIZE + CDIO_CD_HEADER_SIZE)
+ : buf,
+ blocksize, 1);
+ if (ret==0) return ret;
+
+ if (mode2_form2)
+ memcpy (data, buf + CDIO_CD_SYNC_SIZE + CDIO_CD_HEADER_SIZE,
+ M2RAW_SECTOR_SIZE);
+ else
+ memcpy (data, buf + CDIO_CD_XA_SYNC_HEADER, CDIO_CD_FRAMESIZE);
+
+ return 0;
+}
+
+/*!
+ Reads nblocks of mode2 sectors from cd device into data starting
+ from lsn.
+ Returns 0 if no error.
+ */
+static int
+_cdio_read_mode2_sectors (void *env, void *data, uint32_t lsn,
+ bool mode2_form2, unsigned int nblocks)
+{
+ _img_private_t *_obj = env;
+ int i;
+ int retval;
+
+ for (i = 0; i < nblocks; i++) {
+ if (mode2_form2) {
+ if ( (retval = _cdio_read_mode2_sector (_obj,
+ ((char *)data) + (M2RAW_SECTOR_SIZE * i),
+ lsn + i, true)) )
+ return retval;
+ } else {
+ char buf[M2RAW_SECTOR_SIZE] = { 0, };
+ if ( (retval = _cdio_read_mode2_sector (_obj, buf, lsn + i, true)) )
+ return retval;
+
+ memcpy (((char *)data) + (CDIO_CD_FRAMESIZE * i),
+ buf + CDIO_CD_SUBHEADER_SIZE, CDIO_CD_FRAMESIZE);
+ }
+ }
+ return 0;
+}
+
+#define free_if_notnull(obj) \
+ if (NULL != obj) free(obj);
+
+static void
+_cdio_bincue_free (void *env) {
+ _img_private_t *_obj = env;
+
+ if (NULL == _obj) return;
+ free_if_notnull(_obj->mcn);
+ cdio_generic_stream_free(_obj);
+}
+
+/*!
+ Set the arg "key" with "value" in the source device.
+ Currently "source" to set the source device in I/O operations
+ is the only valid key.
+
+ 0 is returned if no error was found, and nonzero if there as an error.
+*/
+/*!
+ Set the device to use in I/O operations.
+*/
+static int
+_cdio_set_arg (void *env, const char key[], const char value[])
+{
+ _img_private_t *_obj = env;
+
+ if (!strcmp (key, "source"))
+ {
+ free_if_notnull (_obj->gen.source_name);
+
+ if (!value)
+ return -2;
+
+ _obj->gen.source_name = strdup (value);
+ }
+ else if (!strcmp (key, "sector"))
+ {
+ if (!strcmp (value, "2336"))
+ _obj->sector_2336 = true;
+ else if (!strcmp (value, "2352"))
+ _obj->sector_2336 = false;
+ else
+ return -2;
+ }
+ else if (!strcmp (key, "cue"))
+ {
+ free_if_notnull (_obj->cue_name);
+
+ if (!value)
+ return -2;
+
+ _obj->cue_name = strdup (value);
+ }
+ else
+ return -1;
+
+ return 0;
+}
+
+/*!
+ Return the value associated with the key "arg".
+*/
+static const char *
+_cdio_get_arg (void *env, const char key[])
+{
+ _img_private_t *_obj = env;
+
+ if (!strcmp (key, "source")) {
+ return _obj->gen.source_name;
+ } else if (!strcmp (key, "cue")) {
+ return _obj->cue_name;
+ }
+ return NULL;
+}
+
+/*!
+ Return an array of strings giving possible NRG disk images.
+ */
+char **
+cdio_get_devices_bincue (void)
+{
+ char **drives = NULL;
+ unsigned int num_files=0;
+#ifdef HAVE_GLOB_H
+ unsigned int i;
+ glob_t globbuf;
+ globbuf.gl_offs = 0;
+ glob("*.cue", GLOB_DOOFFS, NULL, &globbuf);
+ for (i=0; i<globbuf.gl_pathc; i++) {
+ cdio_add_device_list(&drives, globbuf.gl_pathv[i], &num_files);
+ }
+ globfree(&globbuf);
+#else
+ cdio_add_device_list(&drives, DEFAULT_CDIO_DEVICE, &num_files);
+#endif /*HAVE_GLOB_H*/
+ cdio_add_device_list(&drives, NULL, &num_files);
+ return drives;
+}
+
+/*!
+ Return a string containing the default CD device.
+ */
+char *
+cdio_get_default_device_bincue(void)
+{
+ char **drives = cdio_get_devices_nrg();
+ char *drive = (drives[0] == NULL) ? NULL : strdup(drives[0]);
+ cdio_free_device_list(drives);
+ return drive;
+}
+
+/*!
+ Return the number of of the first track.
+ CDIO_INVALID_TRACK is returned on error.
+*/
+static track_t
+_cdio_get_first_track_num(void *env)
+{
+ _img_private_t *_obj = env;
+
+ _cdio_init (_obj);
+
+ return _obj->first_track_num;
+}
+
+/*!
+ Return the media catalog number (MCN) from the CD or NULL if there
+ is none or we don't have the ability to get it.
+
+ Note: string is malloc'd so caller has to free() the returned
+ string when done with it.
+ */
+static char *
+_cdio_get_mcn(void *env)
+{
+ _img_private_t *_obj = env;
+
+ _cdio_init (_obj);
+
+ if (NULL == _obj->mcn) return NULL;
+ return strdup(_obj->mcn);
+}
+
+/*!
+ Return the number of tracks in the current medium.
+ If no cuesheet is available, We fake it an just say there's
+ one big track.
+ CDIO_INVALID_TRACK is returned on error.
+*/
+static track_t
+_cdio_get_num_tracks(void *env)
+{
+ _img_private_t *_obj = env;
+ _cdio_init (_obj);
+
+ return _obj->have_cue && _obj->total_tracks > 0 ? _obj->total_tracks : 1;
+}
+
+/*!
+ Return the number of tracks in the current medium.
+ CDIO_INVALID_TRACK is returned on error.
+*/
+static track_format_t
+_cdio_get_track_format(void *env, track_t track_num)
+{
+ _img_private_t *_obj = env;
+
+ if (!_obj->gen.init) _cdio_init(_obj);
+
+ if (track_num > _obj->total_tracks || track_num == 0)
+ return TRACK_FORMAT_ERROR;
+
+ return _obj->tocent[track_num-1].track_format;
+}
+
+/*!
+ Return true if we have XA data (green, mode2 form1) or
+ XA data (green, mode2 form2). That is track begins:
+ sync - header - subheader
+ 12 4 - 8
+
+ FIXME: there's gotta be a better design for this and get_track_format?
+*/
+static bool
+_cdio_get_track_green(void *env, track_t track_num)
+{
+ _img_private_t *_obj = env;
+
+ if (!_obj->gen.init) _cdio_init(_obj);
+
+ if (track_num > _obj->total_tracks || track_num == 0)
+ return false;
+
+ return _obj->tocent[track_num-1].track_green;
+}
+
+/*!
+ Return the starting LSN track number
+ track_num in obj. Track numbers start at 1.
+ The "leadout" track is specified either by
+ using track_num LEADOUT_TRACK or the total tracks+1.
+ False is returned if there is no track entry.
+*/
+static lba_t
+_cdio_get_track_lba(void *env, track_t track_num)
+{
+ _img_private_t *_obj = env;
+ _cdio_init (_obj);
+
+ if (track_num == CDIO_CDROM_LEADOUT_TRACK) track_num = _obj->total_tracks+1;
+
+ if (track_num <= _obj->total_tracks+1 && track_num != 0) {
+ return _obj->tocent[track_num-1].start_lba;
+ } else
+ return CDIO_INVALID_LBA;
+}
+
+/*!
+ Return the starting MSF (minutes/secs/frames) for the track number
+ track_num in obj. Tracks numbers start at 1.
+ The "leadout" track is specified either by
+ using track_num LEADOUT_TRACK or the total tracks+1.
+
+*/
+static bool
+_cdio_get_track_msf(void *env, track_t track_num, msf_t *msf)
+{
+ _img_private_t *_obj = env;
+ _cdio_init (_obj);
+
+ if (NULL == msf) return false;
+
+ if (track_num == CDIO_CDROM_LEADOUT_TRACK) track_num = _obj->total_tracks+1;
+
+ if (track_num <= _obj->total_tracks+1 && track_num != 0) {
+ *msf = _obj->tocent[track_num-1].start_msf;
+ return true;
+ } else
+ return false;
+}
+
+/*!
+ Return corresponding BIN file if cue_name is a cue file or NULL
+ if not a CUE file.
+
+*/
+/* Later we'll probably parse the entire file. For now though, this gets us
+ started for now.
+*/
+char *
+cdio_is_cuefile(const char *cue_name)
+{
+ int i;
+ char *bin_name;
+
+ if (cue_name == NULL) return false;
+
+ bin_name=strdup(cue_name);
+ i=strlen(bin_name)-strlen("cue");
+
+ if (i>0) {
+ if (cue_name[i]=='c' && cue_name[i+1]=='u' && cue_name[i+2]=='e') {
+ bin_name[i++]='b'; bin_name[i++]='i'; bin_name[i++]='n';
+ return bin_name;
+ }
+ else if (cue_name[i]=='C' && cue_name[i+1]=='U' && cue_name[i+2]=='E') {
+ bin_name[i++]='B'; bin_name[i++]='I'; bin_name[i++]='N';
+ return bin_name;
+ }
+ }
+ free(bin_name);
+ return NULL;
+}
+
+/*!
+ Return corresponding CUE file if bin_name is a bin file or NULL
+ if not a BIN file.
+
+*/
+/* Later we'll probably do better. For now though, this gets us
+ started for now.
+*/
+char *
+cdio_is_binfile(const char *bin_name)
+{
+ int i;
+ char *cue_name;
+
+ if (bin_name == NULL) return false;
+
+ cue_name=strdup(bin_name);
+ i=strlen(bin_name)-strlen("bin");
+
+ if (i>0) {
+ if (bin_name[i]=='b' && bin_name[i+1]=='i' && bin_name[i+2]=='n') {
+ cue_name[i++]='c'; cue_name[i++]='u'; cue_name[i++]='e';
+ return cue_name;
+ }
+ else if (bin_name[i]=='B' && bin_name[i+1]=='I' && bin_name[i+2]=='N') {
+ cue_name[i++]='C'; cue_name[i++]='U'; cue_name[i++]='E';
+ return cue_name;
+ }
+ }
+ free(cue_name);
+ return NULL;
+}
+
+static CdIo *
+cdio_open_common (_img_private_t **_data)
+{
+ cdio_funcs _funcs = {
+ .eject_media = cdio_generic_bogus_eject_media,
+ .free = _cdio_bincue_free,
+ .get_arg = _cdio_get_arg,
+ .get_default_device = cdio_get_default_device_bincue,
+ .get_first_track_num= _cdio_get_first_track_num,
+ .get_mcn = _cdio_get_mcn,
+ .get_num_tracks = _cdio_get_num_tracks,
+ .get_track_format = _cdio_get_track_format,
+ .get_track_green = _cdio_get_track_green,
+ .get_track_lba = _cdio_get_track_lba,
+ .get_track_msf = _cdio_get_track_msf,
+ .lseek = _cdio_lseek,
+ .read = _cdio_read,
+ .read_audio_sectors = _cdio_read_audio_sectors,
+ .read_mode2_sector = _cdio_read_mode2_sector,
+ .read_mode2_sectors = _cdio_read_mode2_sectors,
+ .set_arg = _cdio_set_arg,
+ .stat_size = _cdio_stat_size
+ };
+
+ *_data = _cdio_malloc (sizeof (_img_private_t));
+ (*_data)->gen.init = false;
+ (*_data)->sector_2336 = false;
+ (*_data)->cue_name = NULL;
+
+ return cdio_new (*_data, &_funcs);
+}
+
+CdIo *
+cdio_open_bincue (const char *source_name)
+{
+ char *bin_name = cdio_is_cuefile(source_name);
+
+ if (NULL != bin_name) {
+ return cdio_open_cue(source_name);
+ } else {
+ char *cue_name = cdio_is_binfile(source_name);
+ CdIo *cdio = cdio_open_cue(cue_name);
+ free(cue_name);
+ return cdio;
+ }
+}
+
+CdIo *
+cdio_open_cue (const char *cue_name)
+{
+ CdIo *ret;
+ _img_private_t *_data;
+ char *bin_name;
+
+ if (NULL == cue_name) return NULL;
+ ret = cdio_open_common(&_data);
+ if (ret == NULL) return NULL;
+
+ bin_name = cdio_is_cuefile(cue_name);
+
+ if (NULL == bin_name) {
+ cdio_error ("source name %s is not recognized as a CUE file", cue_name);
+ }
+
+ _cdio_set_arg (_data, "cue", cue_name);
+ _cdio_set_arg (_data, "source", bin_name);
+ free(bin_name);
+
+ if (_cdio_init(_data)) {
+ return ret;
+ } else {
+ cdio_generic_stream_free (_data);
+ return NULL;
+ }
+}
+
+bool
+cdio_have_bincue (void)
+{
+ return true;
+}
diff --git a/src/input/vcd/libcdio/_cdio_bsdi.c b/src/input/vcd/libcdio/_cdio_bsdi.c
new file mode 100644
index 000000000..c3770b592
--- /dev/null
+++ b/src/input/vcd/libcdio/_cdio_bsdi.c
@@ -0,0 +1,710 @@
+/*
+ $Id: _cdio_bsdi.c,v 1.1 2003/10/13 11:47:11 f1rmb Exp $
+
+ Copyright (C) 2001 Herbert Valerio Riedel <hvr@gnu.org>
+ Copyright (C) 2002,2003 Rocky Bernstein <rocky@panix.com>
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+*/
+
+/* This file contains BSDI-specific code and implements low-level
+ control of the CD drive.
+*/
+
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
+
+static const char _rcsid[] = "$Id: _cdio_bsdi.c,v 1.1 2003/10/13 11:47:11 f1rmb Exp $";
+
+#include <cdio/sector.h>
+#include <cdio/util.h>
+#include "cdio_assert.h"
+#include "cdio_private.h"
+
+#define DEFAULT_CDIO_DEVICE "/dev/rsr0c"
+#include <string.h>
+
+#ifdef HAVE_BSDI_CDROM
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <errno.h>
+#include <unistd.h>
+#include <fcntl.h>
+
+/*#define USE_ETC_FSTAB*/
+#ifdef USE_ETC_FSTAB
+#include <fstab.h>
+#endif
+
+#include <dvd.h>
+#include <sys/stat.h>
+#include <sys/types.h>
+#include <sys/ioctl.h>
+
+#define TOTAL_TRACKS (_obj->tochdr.cdth_trk1)
+#define FIRST_TRACK_NUM (_obj->tochdr.cdth_trk0)
+
+typedef struct {
+ /* Things common to all drivers like this.
+ This must be first. */
+ generic_img_private_t gen;
+
+ enum {
+ _AM_NONE,
+ _AM_IOCTL,
+ } access_mode;
+
+ char *source_name;
+
+ bool init;
+
+ /* Track information */
+ bool toc_init; /* if true, info below is valid. */
+ struct cdrom_tochdr tochdr;
+ struct cdrom_tocentry tocent[100]; /* entry info for each track */
+
+} _img_private_t;
+
+/* Check a drive to see if it is a CD-ROM
+ Return 1 if a CD-ROM. 0 if it exists but isn't a CD-ROM drive
+ and -1 if no device exists .
+*/
+static bool
+cdio_is_cdrom(char *drive, char *mnttype)
+{
+ bool is_cd=false;
+ int cdfd;
+ struct cdrom_tochdr tochdr;
+
+ /* If it doesn't exist, return -1 */
+ if ( !cdio_is_device_quiet_generic(drive) ) {
+ return(false);
+ }
+
+ /* If it does exist, verify that it's an available CD-ROM */
+ cdfd = open(drive, (O_RDONLY|O_EXCL|O_NONBLOCK), 0);
+
+ /* Should we want to test the condition in more detail:
+ ENOENT is the error for /dev/xxxxx does not exist;
+ ENODEV means there's no drive present. */
+
+ if ( cdfd >= 0 ) {
+ if ( ioctl(cdfd, CDROMREADTOCHDR, &tochdr) != -1 ) {
+ is_cd = true;
+ }
+ close(cdfd);
+ }
+ /* Even if we can't read it, it might be mounted */
+ else if ( mnttype && (strcmp(mnttype, "cd9660") == 0) ) {
+ is_cd = true;
+ }
+ return(is_cd);
+}
+
+/*!
+ Initialize CD device.
+ */
+static bool
+_cdio_init (_img_private_t *_obj)
+{
+ if (_obj->gen.init) {
+ cdio_error ("init called more than once");
+ return false;
+ }
+
+ _obj->gen.fd = open (_obj->source_name, O_RDONLY, 0);
+
+ if (_obj->gen.fd < 0)
+ {
+ cdio_error ("open (%s): %s", _obj->source_name, strerror (errno));
+ return false;
+ }
+
+ _obj->gen.init = true;
+ _obj->toc_init = false;
+ return true;
+}
+
+/* Read audio sectors
+*/
+static int
+_read_audio_sectors (void *env, void *data, lsn_t lsn,
+ unsigned int nblocks)
+{
+ char buf[CDIO_CD_FRAMESIZE_RAW] = { 0, };
+ struct cdrom_msf *msf = (struct cdrom_msf *) &buf;
+ msf_t _msf;
+
+ _img_private_t *_obj = env;
+
+ cdio_lba_to_msf (cdio_lsn_to_lba(lsn), &_msf);
+ msf->cdmsf_min0 = from_bcd8(_msf.m);
+ msf->cdmsf_sec0 = from_bcd8(_msf.s);
+ msf->cdmsf_frame0 = from_bcd8(_msf.f);
+
+ if (_obj->gen.ioctls_debugged == 75)
+ cdio_debug ("only displaying every 75th ioctl from now on");
+
+ if (_obj->gen.ioctls_debugged == 30 * 75)
+ cdio_debug ("only displaying every 30*75th ioctl from now on");
+
+ if (_obj->gen.ioctls_debugged < 75
+ || (_obj->gen.ioctls_debugged < (30 * 75)
+ && _obj->gen.ioctls_debugged % 75 == 0)
+ || _obj->gen.ioctls_debugged % (30 * 75) == 0)
+ cdio_debug ("reading %2.2d:%2.2d:%2.2d",
+ msf->cdmsf_min0, msf->cdmsf_sec0, msf->cdmsf_frame0);
+
+ _obj->gen.ioctls_debugged++;
+
+ switch (_obj->access_mode) {
+ case _AM_NONE:
+ cdio_error ("no way to read audio");
+ return 1;
+ break;
+
+ case _AM_IOCTL: {
+ unsigned int i;
+ for (i=0; i < nblocks; i++) {
+ if (ioctl (_obj->gen.fd, CDROMREADRAW, &buf) == -1) {
+ perror ("ioctl()");
+ return 1;
+ /* exit (EXIT_FAILURE); */
+ }
+ memcpy (((char *)data) + (CDIO_CD_FRAMESIZE_RAW * i), buf,
+ CDIO_CD_FRAMESIZE_RAW);
+ }
+ break;
+ }
+ }
+
+ return 0;
+}
+
+/*!
+ Reads a single mode2 sector from cd device into data starting
+ from lsn. Returns 0 if no error.
+ */
+static int
+_cdio_read_mode2_sector (void *env, void *data, lsn_t lsn,
+ bool mode2_form2)
+{
+ char buf[M2RAW_SECTOR_SIZE] = { 0, };
+ struct cdrom_msf *msf = (struct cdrom_msf *) &buf;
+ msf_t _msf;
+
+ _img_private_t *_obj = env;
+
+ cdio_lba_to_msf (cdio_lsn_to_lba(lsn), &_msf);
+ msf->cdmsf_min0 = from_bcd8(_msf.m);
+ msf->cdmsf_sec0 = from_bcd8(_msf.s);
+ msf->cdmsf_frame0 = from_bcd8(_msf.f);
+
+ if (_obj->gen.ioctls_debugged == 75)
+ cdio_debug ("only displaying every 75th ioctl from now on");
+
+ if (_obj->gen.ioctls_debugged == 30 * 75)
+ cdio_debug ("only displaying every 30*75th ioctl from now on");
+
+ if (_obj->gen.ioctls_debugged < 75
+ || (_obj->gen.ioctls_debugged < (30 * 75)
+ && _obj->gen.ioctls_debugged % 75 == 0)
+ || _obj->gen.ioctls_debugged % (30 * 75) == 0)
+ cdio_debug ("reading %2.2d:%2.2d:%2.2d",
+ msf->cdmsf_min0, msf->cdmsf_sec0, msf->cdmsf_frame0);
+
+ _obj->gen.ioctls_debugged++;
+
+ switch (_obj->access_mode)
+ {
+ case _AM_NONE:
+ cdio_error ("no way to read mode2");
+ return 1;
+ break;
+
+ case _AM_IOCTL:
+ if (ioctl (_obj->gen.fd, CDROMREADMODE2, &buf) == -1)
+ {
+ perror ("ioctl()");
+ return 1;
+ /* exit (EXIT_FAILURE); */
+ }
+ break;
+ }
+
+ if (mode2_form2)
+ memcpy (data, buf, M2RAW_SECTOR_SIZE);
+ else
+ memcpy (((char *)data), buf + CDIO_CD_SUBHEADER_SIZE, CDIO_CD_FRAMESIZE);
+
+ return 0;
+}
+
+/*!
+ Reads nblocks of mode2 sectors from cd device into data starting
+ from lsn.
+ Returns 0 if no error.
+ */
+static int
+_cdio_read_mode2_sectors (void *env, void *data, lsn_t lsn,
+ bool mode2_form2, unsigned int nblocks)
+{
+ _img_private_t *_obj = env;
+ int i;
+ int retval;
+
+ for (i = 0; i < nblocks; i++) {
+ if (mode2_form2) {
+ if ( (retval = _cdio_read_mode2_sector (_obj,
+ ((char *)data) + (M2RAW_SECTOR_SIZE * i),
+ lsn + i, true)) )
+ return retval;
+ } else {
+ char buf[M2RAW_SECTOR_SIZE] = { 0, };
+ if ( (retval = _cdio_read_mode2_sector (_obj, buf, lsn + i, true)) )
+ return retval;
+
+ memcpy (((char *)data) + (CDIO_CD_FRAMESIZE * i),
+ buf + CDIO_CD_SUBHEADER_SIZE, CDIO_CD_FRAMESIZE);
+ }
+ }
+ return 0;
+}
+
+/*!
+ Return the size of the CD in logical block address (LBA) units.
+ */
+static uint32_t
+_cdio_stat_size (void *env)
+{
+ _img_private_t *_obj = env;
+
+ struct cdrom_tocentry tocent;
+ uint32_t size;
+
+ tocent.cdte_track = CDIO_CDROM_LEADOUT_TRACK;
+ tocent.cdte_format = CDROM_LBA;
+ if (ioctl (_obj->gen.fd, CDROMREADTOCENTRY, &tocent) == -1)
+ {
+ perror ("ioctl(CDROMREADTOCENTRY)");
+ exit (EXIT_FAILURE);
+ }
+
+ size = tocent.cdte_addr.lba;
+
+ return size;
+}
+
+/*!
+ Set the key "arg" to "value" in source device.
+*/
+static int
+_cdio_set_arg (void *env, const char key[], const char value[])
+{
+ _img_private_t *_obj = env;
+
+ if (!strcmp (key, "source"))
+ {
+ if (!value)
+ return -2;
+
+ free (_obj->source_name);
+
+ _obj->source_name = strdup (value);
+ }
+ else if (!strcmp (key, "access-mode"))
+ {
+ if (!strcmp(value, "IOCTL"))
+ _obj->access_mode = _AM_IOCTL;
+ else
+ cdio_error ("unknown access type: %s. ignored.", value);
+ }
+ else
+ return -1;
+
+ return 0;
+}
+
+/*!
+ Read and cache the CD's Track Table of Contents and track info.
+ Return false if successful or true if an error.
+*/
+static bool
+_cdio_read_toc (_img_private_t *_obj)
+{
+ int i;
+
+ /* read TOC header */
+ if ( ioctl(_obj->gen.fd, CDROMREADTOCHDR, &_obj->tochdr) == -1 ) {
+ cdio_error("%s: %s\n",
+ "error in ioctl CDROMREADTOCHDR", strerror(errno));
+ return false;
+ }
+
+ /* read individual tracks */
+ for (i= FIRST_TRACK_NUM; i<=TOTAL_TRACKS; i++) {
+ _obj->tocent[i-1].cdte_track = i;
+ _obj->tocent[i-1].cdte_format = CDROM_MSF;
+ if ( ioctl(_obj->gen.fd, CDROMREADTOCENTRY, &_obj->tocent[i-1]) == -1 ) {
+ cdio_error("%s %d: %s\n",
+ "error in ioctl CDROMREADTOCENTRY for track",
+ i, strerror(errno));
+ return false;
+ }
+ /****
+ struct cdrom_msf0 *msf= &_obj->tocent[i-1].cdte_addr.msf;
+
+ fprintf (stdout, "--- track# %d (msf %2.2x:%2.2x:%2.2x)\n",
+ i, msf->minute, msf->second, msf->frame);
+ ****/
+
+ }
+
+ /* read the lead-out track */
+ _obj->tocent[TOTAL_TRACKS].cdte_track = CDIO_CDROM_LEADOUT_TRACK;
+ _obj->tocent[TOTAL_TRACKS].cdte_format = CDROM_MSF;
+
+ if (ioctl(_obj->gen.fd, CDROMREADTOCENTRY,
+ &_obj->tocent[TOTAL_TRACKS]) == -1 ) {
+ cdio_error("%s: %s\n",
+ "error in ioctl CDROMREADTOCENTRY for lead-out",
+ strerror(errno));
+ return false;
+ }
+
+ /*
+ struct cdrom_msf0 *msf= &_obj->tocent[TOTAL_TRACKS].cdte_addr.msf;
+
+ fprintf (stdout, "--- track# %d (msf %2.2x:%2.2x:%2.2x)\n",
+ i, msf->minute, msf->second, msf->frame);
+ */
+
+ return true;
+}
+
+/*!
+ Eject media in CD drive. If successful, as a side effect we
+ also free obj.
+ */
+static int
+_cdio_eject_media (void *env) {
+
+ _img_private_t *_obj = env;
+ int ret=2;
+ int status;
+ int fd;
+
+ close(_obj->gen.fd);
+ _obj->gen.fd = -1;
+ if ((fd = open (_obj->source_name, O_RDONLY|O_NONBLOCK)) > -1) {
+ if((status = ioctl(fd, CDROM_DRIVE_STATUS, (void *) CDSL_CURRENT)) > 0) {
+ switch(status) {
+ case CDS_TRAY_OPEN:
+ if((ret = ioctl(fd, CDROMCLOSETRAY, 0)) != 0) {
+ cdio_error ("ioctl CDROMCLOSETRAY failed: %s\n", strerror(errno));
+ }
+ break;
+ case CDS_DISC_OK:
+ if((ret = ioctl(fd, CDROMEJECT, 0)) != 0) {
+ cdio_error("ioctl CDROMEJECT failed: %s\n", strerror(errno));
+ }
+ break;
+ }
+ ret=0;
+ } else {
+ cdio_error ("CDROM_DRIVE_STATUS failed: %s\n", strerror(errno));
+ ret=1;
+ }
+ close(fd);
+ }
+ return 2;
+}
+
+/*!
+ Return the value associated with the key "arg".
+*/
+static const char *
+_cdio_get_arg (void *env, const char key[])
+{
+ _img_private_t *_obj = env;
+
+ if (!strcmp (key, "source")) {
+ return _obj->source_name;
+ } else if (!strcmp (key, "access-mode")) {
+ switch (_obj->access_mode) {
+ case _AM_IOCTL:
+ return "ioctl";
+ case _AM_NONE:
+ return "no access method";
+ }
+ }
+ return NULL;
+}
+
+/*!
+ Return the number of of the first track.
+ CDIO_INVALID_TRACK is returned on error.
+*/
+static track_t
+_cdio_get_first_track_num(void *env)
+{
+ _img_private_t *_obj = env;
+
+ if (!_obj->toc_init) _cdio_read_toc (_obj) ;
+
+ return FIRST_TRACK_NUM;
+}
+
+/*!
+ Return the media catalog number MCN.
+ Note: string is malloc'd so caller should free() then returned
+ string when done with it.
+ */
+static char *
+_cdio_get_mcn (void *env) {
+
+ struct cdrom_mcn mcn;
+ _img_private_t *_obj = env;
+ if (ioctl(_obj->gen.fd, CDROM_GET_MCN, &mcn) != 0)
+ return NULL;
+ return strdup(mcn.medium_catalog_number);
+}
+
+/*!
+ Return the number of tracks in the current medium.
+ CDIO_INVALID_TRACK is returned on error.
+*/
+static track_t
+_cdio_get_num_tracks(void *env)
+{
+ _img_private_t *_obj = env;
+
+ if (!_obj->toc_init) _cdio_read_toc (_obj) ;
+
+ return TOTAL_TRACKS;
+}
+
+/*!
+ Get format of track.
+*/
+static track_format_t
+_cdio_get_track_format(void *env, track_t track_num)
+{
+ _img_private_t *_obj = env;
+
+ if (!_obj->toc_init) _cdio_read_toc (_obj) ;
+
+ if (track_num > TOTAL_TRACKS || track_num == 0)
+ return TRACK_FORMAT_ERROR;
+
+ /* This is pretty much copied from the "badly broken" cdrom_count_tracks
+ in linux/cdrom.c.
+ */
+ if (_obj->tocent[track_num-1].cdte_ctrl & CDROM_DATA_TRACK) {
+ if (_obj->tocent[track_num-1].cdte_format == 0x10)
+ return TRACK_FORMAT_CDI;
+ else if (_obj->tocent[track_num-1].cdte_format == 0x20)
+ return TRACK_FORMAT_XA;
+ else
+ return TRACK_FORMAT_DATA;
+ } else
+ return TRACK_FORMAT_AUDIO;
+
+}
+
+/*!
+ Return true if we have XA data (green, mode2 form1) or
+ XA data (green, mode2 form2). That is track begins:
+ sync - header - subheader
+ 12 4 - 8
+
+ FIXME: there's gotta be a better design for this and get_track_format?
+*/
+static bool
+_cdio_get_track_green(void *env, track_t track_num)
+{
+ _img_private_t *_obj = env;
+
+ if (!_obj->toc_init) _cdio_read_toc (_obj) ;
+
+ if (track_num == CDIO_CDROM_LEADOUT_TRACK) track_num = TOTAL_TRACKS+1;
+
+ if (track_num > TOTAL_TRACKS+1 || track_num == 0)
+ return false;
+
+ /* FIXME: Dunno if this is the right way, but it's what
+ I was using in cdinfo for a while.
+ */
+ return ((_obj->tocent[track_num-1].cdte_ctrl & 2) != 0);
+}
+
+/*!
+ Return the starting MSF (minutes/secs/frames) for track number
+ track_num in obj. Track numbers start at 1.
+ The "leadout" track is specified either by
+ using track_num LEADOUT_TRACK or the total tracks+1.
+ False is returned if there is no track entry.
+*/
+static bool
+_cdio_get_track_msf(void *env, track_t track_num, msf_t *msf)
+{
+ _img_private_t *_obj = env;
+
+ if (NULL == msf) return false;
+
+ if (!_obj->toc_init) _cdio_read_toc (_obj) ;
+
+ if (track_num == CDIO_CDROM_LEADOUT_TRACK) track_num = TOTAL_TRACKS+1;
+
+ if (track_num > TOTAL_TRACKS+1 || track_num == 0) {
+ return false;
+ } else {
+ struct cdrom_msf0 *msf0= &_obj->tocent[track_num-1].cdte_addr.msf;
+ msf->m = to_bcd8(msf0->minute);
+ msf->s = to_bcd8(msf0->second);
+ msf->f = to_bcd8(msf0->frame);
+ return true;
+ }
+}
+
+#endif /* HAVE_BSDI_CDROM */
+
+/*!
+ Return an array of strings giving possible CD devices.
+ */
+char **
+cdio_get_devices_bsdi (void)
+{
+#ifndef HAVE_BSDI_CDROM
+ return NULL;
+#else
+ char drive[40];
+ char **drives = NULL;
+ unsigned int num_drives=0;
+ bool exists=true;
+ char c;
+
+ /* Scan the system for CD-ROM drives.
+ */
+
+#ifdef USE_ETC_FSTAB
+
+ struct fstab *fs;
+ setfsent();
+
+ /* Check what's in /etc/fstab... */
+ while ( (fs = getfsent()) )
+ {
+ if (strncmp(fs->fs_spec, "/dev/sr", 7))
+ cdio_add_device_list(&drives, fs->fs_spec, &num_drives);
+ }
+
+#endif
+
+ /* Scan the system for CD-ROM drives.
+ Not always 100% reliable, so use the USE_MNTENT code above first.
+ */
+ for ( c='0'; exists && c <='9'; c++ ) {
+ sprintf(drive, "/dev/rsr%cc", c);
+ exists = cdio_is_cdrom(drive, NULL);
+ if ( exists ) {
+ cdio_add_device_list(&drives, drive, &num_drives);
+ }
+ }
+ cdio_add_device_list(&drives, NULL, &num_drives);
+ return drives;
+#endif /*HAVE_BSDI_CDROM*/
+}
+
+/*!
+ Return a string containing the default CD device if none is specified.
+ */
+char *
+cdio_get_default_device_bsdi(void)
+{
+ return strdup(DEFAULT_CDIO_DEVICE);
+}
+
+/*!
+ Initialization routine. This is the only thing that doesn't
+ get called via a function pointer. In fact *we* are the
+ ones to set that up.
+ */
+CdIo *
+cdio_open_bsdi (const char *source_name)
+{
+
+#ifdef HAVE_BSDI_CDROM
+ CdIo *ret;
+ _img_private_t *_data;
+
+ cdio_funcs _funcs = {
+ .eject_media = _cdio_eject_media,
+ .free = cdio_generic_free,
+ .get_arg = _cdio_get_arg,
+ .get_default_device = cdio_get_default_device_bsdi,
+ .get_devices = cdio_get_devices_bsdi,
+ .get_first_track_num= _cdio_get_first_track_num,
+ .get_mcn = _cdio_get_mcn,
+ .get_num_tracks = _cdio_get_num_tracks,
+ .get_track_format = _cdio_get_track_format,
+ .get_track_green = _cdio_get_track_green,
+ .get_track_lba = NULL, /* This could be implemented if need be. */
+ .get_track_msf = _cdio_get_track_msf,
+ .lseek = cdio_generic_lseek,
+ .read = cdio_generic_read,
+ .read_mode2_sector = _cdio_read_mode2_sector,
+ .read_audio_sectors = _read_audio_sectors,
+ .read_mode2_sectors = _cdio_read_mode2_sectors,
+ .set_arg = _cdio_set_arg,
+ .stat_size = _cdio_stat_size
+ };
+
+ _data = _cdio_malloc (sizeof (_img_private_t));
+ _data->access_mode = _AM_IOCTL;
+ _data->gen.init = false;
+ _data->gen.fd = -1;
+
+ _cdio_set_arg(_data, "source", (NULL == source_name)
+ ? DEFAULT_CDIO_DEVICE: source_name);
+
+ ret = cdio_new (_data, &_funcs);
+ if (ret == NULL) return NULL;
+
+ if (_cdio_init(_data))
+ return ret;
+ else {
+ cdio_generic_free (_data);
+ return NULL;
+ }
+
+#else
+ return NULL;
+#endif /* HAVE_BSDI_CDROM */
+
+}
+
+bool
+cdio_have_bsdi (void)
+{
+#ifdef HAVE_BSDI_CDROM
+ return true;
+#else
+ return false;
+#endif /* HAVE_BSDI_CDROM */
+}
+
diff --git a/src/input/vcd/libcdio/_cdio_freebsd.c b/src/input/vcd/libcdio/_cdio_freebsd.c
new file mode 100644
index 000000000..1eaf227ef
--- /dev/null
+++ b/src/input/vcd/libcdio/_cdio_freebsd.c
@@ -0,0 +1,635 @@
+/*
+ $Id: _cdio_freebsd.c,v 1.1 2003/10/13 11:47:11 f1rmb Exp $
+
+ Copyright (C) 2003 Rocky Bernstein <rocky@panix.com>
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+*/
+
+/* This file contains FreeBSD-specific code and implements low-level
+ control of the CD drive. Culled I think from xine's or mplayer's
+ FreeBSD code.
+*/
+
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
+
+static const char _rcsid[] = "$Id: _cdio_freebsd.c,v 1.1 2003/10/13 11:47:11 f1rmb Exp $";
+
+#include <cdio/sector.h>
+#include <cdio/util.h>
+#include "cdio_assert.h"
+#include "cdio_private.h"
+
+/* Is this the right default? */
+#define DEFAULT_CDIO_DEVICE "/dev/acd0c"
+
+#include <string.h>
+
+#ifdef HAVE_FREEBSD_CDROM
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <errno.h>
+#include <unistd.h>
+#include <fcntl.h>
+
+#ifdef HAVE_SYS_CDIO_H
+# include <sys/cdio.h>
+#endif
+#include <sys/cdrio.h>
+
+#include <sys/stat.h>
+#include <sys/types.h>
+#include <sys/ioctl.h>
+
+#define TOTAL_TRACKS ( _obj->tochdr.ending_track \
+ - _obj->tochdr.starting_track + 1)
+#define FIRST_TRACK_NUM (_obj->tochdr.starting_track)
+
+typedef struct {
+ /* Things common to all drivers like this.
+ This must be first. */
+ generic_img_private_t gen;
+
+ enum {
+ _AM_NONE,
+ _AM_IOCTL,
+ } access_mode;
+
+ char *source_name;
+
+ bool init;
+
+ /* Track information */
+ bool toc_init; /* if true, info below is valid. */
+ struct ioc_toc_header tochdr;
+ struct cd_toc_entry tocent[100]; /* entry info for each track */
+
+} _img_private_t;
+
+/* Check a drive to see if it is a CD-ROM
+ Return 1 if a CD-ROM. 0 if it exists but isn't a CD-ROM drive
+ and -1 if no device exists .
+*/
+static bool
+cdio_is_cdrom(char *drive, char *mnttype)
+{
+ bool is_cd=false;
+ int cdfd;
+ struct ioc_toc_header tochdr;
+
+ /* If it doesn't exist, return -1 */
+ if ( !cdio_is_device_quiet_generic(drive) ) {
+ return(false);
+ }
+
+ /* If it does exist, verify that it's an available CD-ROM */
+ cdfd = open(drive, (O_RDONLY|O_EXCL|O_NONBLOCK), 0);
+
+ /* Should we want to test the condition in more detail:
+ ENOENT is the error for /dev/xxxxx does not exist;
+ ENODEV means there's no drive present. */
+
+ if ( cdfd >= 0 ) {
+ if ( ioctl(cdfd, CDIOREADTOCHEADER, &tochdr) != -1 ) {
+ is_cd = true;
+ }
+ close(cdfd);
+ }
+ /* Even if we can't read it, it might be mounted */
+ else if ( mnttype && (strcmp(mnttype, "iso9660") == 0) ) {
+ is_cd = true;
+ }
+ return(is_cd);
+}
+
+static int
+_read_mode2 (int fd, void *buf, lba_t lba, unsigned int nblocks,
+ bool _workaround)
+{
+ unsigned int l = 0;
+ int retval = 0;
+
+ while (nblocks > 0)
+ {
+ const unsigned int nblocks2 = (nblocks > 25) ? 25 : nblocks;
+ void *buf2 = ((char *)buf ) + (l * M2RAW_SECTOR_SIZE);
+
+ retval |= _read_mode2 (fd, buf2, lba + l, nblocks2, _workaround);
+
+ if (retval)
+ break;
+
+ nblocks -= nblocks2;
+ l += nblocks2;
+ }
+
+ return retval;
+}
+
+/*!
+ Reads a single mode2 sector from cd device into data starting from lsn.
+ Returns 0 if no error.
+ */
+static int
+_cdio_read_audio_sectors (void *env, void *data, lsn_t lsn,
+ unsigned int nblocks)
+{
+ _img_private_t *_obj = env;
+ unsigned char buf[CDIO_CD_FRAMESIZE_RAW] = { 0, };
+ struct ioc_read_audio cdda;
+
+ cdda.address.lba = lsn;
+ cdda.buffer = buf;
+ cdda.nframes = nblocks;
+ cdda.address_format = CD_LBA_FORMAT;
+
+ /* read a frame */
+ if(ioctl(_obj->gen.fd, CDIOCREADAUDIO, &cdda) < 0) {
+ perror("CDIOCREADAUDIO");
+ return 1;
+ }
+ memcpy (data, buf, CDIO_CD_FRAMESIZE_RAW);
+
+ return 0;
+}
+
+/*!
+ Reads a single mode2 sector from cd device into data starting
+ from lsn. Returns 0 if no error.
+ */
+static int
+_cdio_read_mode2_sector (void *env, void *data, lsn_t lsn,
+ bool mode2_form2)
+{
+ char buf[M2RAW_SECTOR_SIZE] = { 0, };
+ int retval;
+
+ if ( (retval = _cdio_read_audio_sectors (env, buf, lsn, 1)) )
+ return retval;
+
+ if (mode2_form2)
+ memcpy (data, buf + CDIO_CD_XA_SYNC_HEADER, M2RAW_SECTOR_SIZE);
+ else
+ memcpy (data, buf + CDIO_CD_XA_SYNC_HEADER, CDIO_CD_FRAMESIZE);
+
+ return 0;
+}
+
+/*!
+ Reads nblocks of mode2 sectors from cd device into data starting
+ from lsn.
+ Returns 0 if no error.
+ */
+static int
+_cdio_read_mode2_sectors (void *env, void *data, lsn_t lsn,
+ bool mode2_form2, unsigned int nblocks)
+{
+ _img_private_t *_obj = env;
+ int i;
+ int retval;
+
+ for (i = 0; i < nblocks; i++) {
+ if (mode2_form2) {
+ if ( (retval = _cdio_read_mode2_sector (_obj,
+ ((char *)data) + (M2RAW_SECTOR_SIZE * i),
+ lsn + i, true)) )
+ return retval;
+ } else {
+ char buf[M2RAW_SECTOR_SIZE] = { 0, };
+ if ( (retval = _cdio_read_mode2_sector (_obj, buf, lsn + i, true)) )
+ return retval;
+
+ memcpy (((char *)data) + (CDIO_CD_FRAMESIZE * i),
+ buf + CDIO_CD_SUBHEADER_SIZE, CDIO_CD_FRAMESIZE);
+ }
+ }
+ return 0;
+}
+
+/*!
+ Return the size of the CD in logical block address (LBA) units.
+ */
+static uint32_t
+_cdio_stat_size (void *env)
+{
+ _img_private_t *_obj = env;
+
+ struct ioc_read_toc_single_entry tocent;
+ uint32_t size;
+
+ tocent.track = CDIO_CDROM_LEADOUT_TRACK;
+ tocent.address_format = CD_LBA_FORMAT;
+ if (ioctl (_obj->gen.fd, CDIOREADTOCENTRY, &tocent) == -1)
+ {
+ perror ("ioctl(CDROMREADTOCENTRY)");
+ exit (EXIT_FAILURE);
+ }
+
+ size = tocent.entry.addr.lba;
+
+ return size;
+}
+
+/*!
+ Set the key "arg" to "value" in source device.
+*/
+static int
+_cdio_set_arg (void *env, const char key[], const char value[])
+{
+ _img_private_t *_obj = env;
+
+ if (!strcmp (key, "source"))
+ {
+ if (!value)
+ return -2;
+
+ free (_obj->gen.source_name);
+
+ _obj->gen.source_name = strdup (value);
+ }
+ else if (!strcmp (key, "access-mode"))
+ {
+ if (!strcmp(value, "IOCTL"))
+ _obj->access_mode = _AM_IOCTL;
+ else
+ cdio_error ("unknown access type: %s. ignored.", value);
+ }
+ else
+ return -1;
+
+ return 0;
+}
+
+/*!
+ Read and cache the CD's Track Table of Contents and track info.
+ Return false if successful or true if an error.
+*/
+static bool
+_cdio_read_toc (_img_private_t *_obj)
+{
+ int i;
+ struct ioc_read_toc_entry te;
+
+ /* read TOC header */
+ if ( ioctl(_obj->gen.fd, CDIOREADTOCHEADER, &_obj->tochdr) == -1 ) {
+ cdio_error("error in ioctl(CDIOREADTOCHEADER): %s\n", strerror(errno));
+ return false;
+ }
+
+ te.address_format = CD_LBA_FORMAT;
+ te.starting_track = 0;
+ te.data_len = (TOTAL_TRACKS+1) * sizeof(struct cd_toc_entry);
+
+ te.data = _obj->tocent;
+
+ if ( ioctl(_obj->gen.fd, CDIOREADTOCENTRYS, &te) == -1 ) {
+ cdio_error("%s %d: %s\n",
+ "error in ioctl CDROMREADTOCENTRYS for track",
+ i, strerror(errno));
+ return false;
+ }
+
+ return true;
+}
+
+/*!
+ Eject media. Return 1 if successful, 0 otherwise.
+ */
+static int
+_cdio_eject_media (void *env) {
+
+ _img_private_t *_obj = env;
+ int ret=2;
+ int fd;
+
+ if ((fd = open(_obj->gen.source_name, O_RDONLY|O_NONBLOCK)) > -1) {
+ ret = 1;
+ if (ioctl(fd, CDIOCALLOW) == -1) {
+ cdio_error("ioctl(fd, CDIOCALLOW) failed: %s\n", strerror(errno));
+ } else if (ioctl(fd, CDIOCEJECT) == -1) {
+ cdio_error("ioctl(CDIOCEJECT) failed: %s\n", strerror(errno));
+ } else {
+ ret = 0;
+ }
+ close(fd);
+ }
+
+ return ret;
+}
+
+/*!
+ Return the value associated with the key "arg".
+*/
+static const char *
+_cdio_get_arg (void *env, const char key[])
+{
+ _img_private_t *_obj = env;
+
+ if (!strcmp (key, "source")) {
+ return _obj->gen.source_name;
+ } else if (!strcmp (key, "access-mode")) {
+ switch (_obj->access_mode) {
+ case _AM_IOCTL:
+ return "ioctl";
+ case _AM_NONE:
+ return "no access method";
+ }
+ }
+ return NULL;
+}
+
+/*!
+ Return the number of of the first track.
+ CDIO_INVALID_TRACK is returned on error.
+*/
+static track_t
+_cdio_get_first_track_num(void *env)
+{
+ _img_private_t *_obj = env;
+
+ if (!_obj->toc_init) _cdio_read_toc (_obj) ;
+
+ return FIRST_TRACK_NUM;
+}
+
+/*!
+ Return the media catalog number MCN.
+
+ Note: string is malloc'd so caller should free() then returned
+ string when done with it.
+
+ FIXME: This is just a guess.
+
+ */
+static char *
+_cdio_get_mcn (void *env) {
+
+ _img_private_t *_obj = env;
+ struct ioc_read_subchannel subchannel;
+ struct cd_sub_channel_info subchannel_info;
+
+ subchannel.address_format = CD_LBA_FORMAT;
+ subchannel.data_format = CD_MEDIA_CATALOG;
+ subchannel.track = 0;
+ subchannel.data_len = 1;
+ subchannel.data = &subchannel_info;
+
+ if(ioctl(_obj->gen.fd, CDIOCREADSUBCHANNEL, &subchannel) < 0) {
+ perror("CDIOCREADSUBCHANNEL");
+ return NULL;
+ }
+
+ /* Probably need a loop over tracks rather than give up if we
+ can't find in track 0.
+ */
+ if (subchannel_info.what.media_catalog.mc_valid)
+ return strdup(subchannel_info.what.media_catalog.mc_number);
+ else
+ return NULL;
+}
+
+/*!
+ Return the number of tracks in the current medium.
+ CDIO_INVALID_TRACK is returned on error.
+*/
+static track_t
+_cdio_get_num_tracks(void *env)
+{
+ _img_private_t *_obj = env;
+
+ if (!_obj->toc_init) _cdio_read_toc (_obj) ;
+
+ return TOTAL_TRACKS;
+}
+
+/*!
+ Get format of track.
+
+ FIXME: We're just guessing this from the GNU/Linux code.
+
+*/
+static track_format_t
+_cdio_get_track_format(void *env, track_t track_num)
+{
+ _img_private_t *_obj = env;
+ struct ioc_read_subchannel subchannel;
+ struct cd_sub_channel_info subchannel_info;
+
+ subchannel.address_format = CD_LBA_FORMAT;
+ subchannel.data_format = CD_CURRENT_POSITION;
+ subchannel.track = track_num;
+ subchannel.data_len = 1;
+ subchannel.data = &subchannel_info;
+
+ if(ioctl(_obj->gen.fd, CDIOCREADSUBCHANNEL, &subchannel) < 0) {
+ perror("CDIOCREADSUBCHANNEL");
+ return 1;
+ }
+
+ if (subchannel_info.what.position.control == 0x04) {
+ if (subchannel_info.what.position.data_format == 0x10)
+ return TRACK_FORMAT_CDI;
+ else if (subchannel_info.what.position.data_format == 0x20)
+ return TRACK_FORMAT_XA;
+ else
+ return TRACK_FORMAT_DATA;
+ } else
+ return TRACK_FORMAT_AUDIO;
+}
+
+/*!
+ Return true if we have XA data (green, mode2 form1) or
+ XA data (green, mode2 form2). That is track begins:
+ sync - header - subheader
+ 12 4 - 8
+
+ FIXME: there's gotta be a better design for this and get_track_format?
+*/
+static bool
+_cdio_get_track_green(void *env, track_t track_num)
+{
+ _img_private_t *_obj = env;
+ struct ioc_read_subchannel subchannel;
+ struct cd_sub_channel_info subchannel_info;
+
+ subchannel.address_format = CD_LBA_FORMAT;
+ subchannel.data_format = CD_CURRENT_POSITION;
+ subchannel.track = track_num;
+ subchannel.data_len = 1;
+ subchannel.data = &subchannel_info;
+
+ if(ioctl(_obj->gen.fd, CDIOCREADSUBCHANNEL, &subchannel) < 0) {
+ perror("CDIOCREADSUBCHANNEL");
+ return 1;
+ }
+
+ /* FIXME: Dunno if this is the right way, but it's what
+ I was using in cdinfo for a while.
+ */
+ return (subchannel_info.what.position.control & 2) != 0;
+}
+
+/*!
+ Return the starting LSN track number
+ track_num in obj. Track numbers start at 1.
+ The "leadout" track is specified either by
+ using track_num LEADOUT_TRACK or the total tracks+1.
+ False is returned if there is no track entry.
+*/
+static lba_t
+_cdio_get_track_lba(void *env, track_t track_num)
+{
+ _img_private_t *_obj = env;
+
+ if (!_obj->toc_init) _cdio_read_toc (_obj) ;
+
+ if (track_num == CDIO_CDROM_LEADOUT_TRACK) track_num = TOTAL_TRACKS+1;
+
+ if (track_num > TOTAL_TRACKS+1 || track_num == 0) {
+ return CDIO_INVALID_LBA;
+ } else {
+ return _obj->tocent[track_num-1].addr.lba;
+ }
+}
+
+#endif /* HAVE_FREEBSD_CDROM */
+
+/*!
+ Return an array of strings giving possible CD devices.
+ */
+char **
+cdio_get_devices_freebsd (void)
+{
+#ifndef HAVE_FREEBSD_CDROM
+ return NULL;
+#else
+ char drive[40];
+ char **drives = NULL;
+ unsigned int num_drives=0;
+ bool exists=true;
+ char c;
+
+ /* Scan the system for CD-ROM drives.
+ */
+
+#ifdef USE_ETC_FSTAB
+
+ struct fstab *fs;
+ setfsent();
+
+ /* Check what's in /etc/fstab... */
+ while ( (fs = getfsent()) )
+ {
+ if (strncmp(fs->fs_spec, "/dev/sr", 7))
+ cdio_add_device_list(&drives, fs->fs_spec, &num_drives);
+ }
+
+#endif
+
+ /* Scan the system for CD-ROM drives.
+ Not always 100% reliable, so use the USE_MNTENT code above first.
+ */
+ for ( c='0'; exists && c <='9'; c++ ) {
+ sprintf(drive, "/dev/acd%cc", c);
+ exists = cdio_is_cdrom(drive, NULL);
+ if ( exists ) {
+ cdio_add_device_list(&drives, drive, &num_drives);
+ }
+ }
+ cdio_add_device_list(&drives, NULL, &num_drives);
+ return drives;
+#endif /*HAVE_FREEBSD_CDROM*/
+}
+
+/*!
+ Return a string containing the default CD device if none is specified.
+ */
+char *
+cdio_get_default_device_freebsd()
+{
+ return strdup(DEFAULT_CDIO_DEVICE);
+}
+
+/*!
+ Initialization routine. This is the only thing that doesn't
+ get called via a function pointer. In fact *we* are the
+ ones to set that up.
+ */
+CdIo *
+cdio_open_freebsd (const char *source_name)
+{
+
+#ifdef HAVE_FREEBSD_CDROM
+ CdIo *ret;
+ _img_private_t *_data;
+
+ cdio_funcs _funcs = {
+ .eject_media = _cdio_eject_media,
+ .free = cdio_generic_free,
+ .get_arg = _cdio_get_arg,
+ .get_default_device = cdio_get_default_device_freebsd,
+ .get_devices = cdio_get_devices_freebsd,
+ .get_first_track_num= _cdio_get_first_track_num,
+ .get_mcn = _cdio_get_mcn,
+ .get_num_tracks = _cdio_get_num_tracks,
+ .get_track_format = _cdio_get_track_format,
+ .get_track_green = _cdio_get_track_green,
+ .get_track_lba = _cdio_get_track_lba,
+ .get_track_msf = NULL,
+ .lseek = cdio_generic_lseek,
+ .read = cdio_generic_read,
+ .read_audio_sectors = _cdio_read_audio_sectors,
+ .read_mode2_sector = _cdio_read_mode2_sector,
+ .read_mode2_sectors = _cdio_read_mode2_sectors,
+ .set_arg = _cdio_set_arg,
+ .stat_size = _cdio_stat_size
+ };
+
+ _data = _cdio_malloc (sizeof (_img_private_t));
+ _data->access_mode = _AM_IOCTL;
+ _data->gen.init = false;
+ _data->gen.fd = -1;
+
+ _cdio_set_arg(_data, "source", (NULL == source_name)
+ ? DEFAULT_CDIO_DEVICE: source_name);
+
+ ret = cdio_new (_data, &_funcs);
+ if (ret == NULL) return NULL;
+
+ if (cdio_generic_init(_data))
+ return ret;
+ else {
+ cdio_generic_free (_data);
+ return NULL;
+ }
+
+#else
+ return NULL;
+#endif /* HAVE_FREEBSD_CDROM */
+
+}
+
+bool
+cdio_have_freebsd (void)
+{
+#ifdef HAVE_FREEBSD_CDROM
+ return true;
+#else
+ return false;
+#endif /* HAVE_FREEBSD_CDROM */
+}
diff --git a/src/input/vcd/libcdio/_cdio_generic.c b/src/input/vcd/libcdio/_cdio_generic.c
new file mode 100644
index 000000000..180de1086
--- /dev/null
+++ b/src/input/vcd/libcdio/_cdio_generic.c
@@ -0,0 +1,195 @@
+/*
+ $Id: _cdio_generic.c,v 1.1 2003/10/13 11:47:11 f1rmb Exp $
+
+ Copyright (C) 2001 Herbert Valerio Riedel <hvr@gnu.org>
+ Copyright (C) 2002,2003 Rocky Bernstein <rocky@panix.com>
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+*/
+
+/* This file contains Linux-specific code and implements low-level
+ control of the CD drive.
+*/
+
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
+
+static const char _rcsid[] = "$Id: _cdio_generic.c,v 1.1 2003/10/13 11:47:11 f1rmb Exp $";
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <errno.h>
+#include <unistd.h>
+#include <fcntl.h>
+
+#include <sys/stat.h>
+#include <sys/types.h>
+
+#include <cdio/sector.h>
+#include <cdio/util.h>
+#include "cdio_assert.h"
+#include "cdio_private.h"
+#include "_cdio_stdio.h"
+
+/*!
+ Eject media -- there's nothing to do here. We always return 2.
+ Should we also free resources?
+ */
+int
+cdio_generic_bogus_eject_media (void *user_data) {
+ /* Sort of a stub here. Perhaps log a message? */
+ return 2;
+}
+
+
+/*!
+ Release and free resources associated with cd.
+ */
+void
+cdio_generic_free (void *user_data)
+{
+ generic_img_private_t *_obj = user_data;
+
+ if (NULL == _obj) return;
+ free (_obj->source_name);
+
+ if (_obj->fd >= 0)
+ close (_obj->fd);
+
+ free (_obj);
+}
+
+/*!
+ Initialize CD device.
+ */
+bool
+cdio_generic_init (void *user_data)
+{
+ generic_img_private_t *_obj = user_data;
+ if (_obj->init) {
+ cdio_error ("init called more than once");
+ return false;
+ }
+
+ _obj->fd = open (_obj->source_name, O_RDONLY, 0);
+
+ if (_obj->fd < 0)
+ {
+ cdio_warn ("open (%s): %s", _obj->source_name, strerror (errno));
+ return false;
+ }
+
+ _obj->init = true;
+ _obj->toc_init = false;
+ return true;
+}
+
+/*!
+ Reads into buf the next size bytes.
+ Returns -1 on error.
+ Is in fact libc's read().
+*/
+off_t
+cdio_generic_lseek (void *user_data, off_t offset, int whence)
+{
+ generic_img_private_t *_obj = user_data;
+ return lseek(_obj->fd, offset, whence);
+}
+
+/*!
+ Reads into buf the next size bytes.
+ Returns -1 on error.
+ Is in fact libc's read().
+*/
+ssize_t
+cdio_generic_read (void *user_data, void *buf, size_t size)
+{
+ generic_img_private_t *_obj = user_data;
+ return read(_obj->fd, buf, size);
+}
+
+/*!
+ Release and free resources associated with stream or disk image.
+*/
+void
+cdio_generic_stream_free (void *user_data)
+{
+ generic_img_private_t *_obj = user_data;
+
+ if (NULL == _obj) return;
+ free (_obj->source_name);
+
+ if (_obj->data_source)
+ cdio_stream_destroy (_obj->data_source);
+
+ free (_obj);
+}
+
+
+/*!
+ Return true if source_name could be a device containing a CD-ROM.
+*/
+bool
+cdio_is_device_generic(const char *source_name)
+{
+ struct stat buf;
+ if (0 != stat(source_name, &buf)) {
+ cdio_warn ("Can't get file status for %s:\n%s", source_name,
+ strerror(errno));
+ return false;
+ }
+ return (S_ISBLK(buf.st_mode) || S_ISCHR(buf.st_mode));
+}
+
+/*!
+ Like above, but don't give a warning device doesn't exist.
+*/
+bool
+cdio_is_device_quiet_generic(const char *source_name)
+{
+ struct stat buf;
+ if (0 != stat(source_name, &buf)) {
+ return false;
+ }
+ return (S_ISBLK(buf.st_mode) || S_ISCHR(buf.st_mode));
+}
+
+/*!
+ Add/allocate a drive to the end of drives.
+ Use cdio_free_device_list() to free this device_list.
+*/
+void
+cdio_add_device_list(char **device_list[], const char *drive, int *num_drives)
+{
+ if (NULL != drive) {
+ unsigned int j;
+ for (j=0; j<*num_drives; j++) {
+ if (strcmp((*device_list)[j], drive) == 0) break;
+ }
+ if (j==*num_drives) {
+ (*num_drives)++;
+ *device_list = realloc(*device_list, (*num_drives) * sizeof(char *));
+ (*device_list)[*num_drives-1] = strdup(drive);
+ }
+ } else {
+ (*num_drives)++;
+ *device_list = realloc(*device_list, (*num_drives) * sizeof(char *));
+ (*device_list)[*num_drives-1] = NULL;
+ }
+}
+
+
diff --git a/src/input/vcd/libcdio/_cdio_linux.c b/src/input/vcd/libcdio/_cdio_linux.c
new file mode 100644
index 000000000..51afb728e
--- /dev/null
+++ b/src/input/vcd/libcdio/_cdio_linux.c
@@ -0,0 +1,1028 @@
+/*
+ $Id: _cdio_linux.c,v 1.1 2003/10/13 11:47:11 f1rmb Exp $
+
+ Copyright (C) 2001 Herbert Valerio Riedel <hvr@gnu.org>
+ Copyright (C) 2002,2003 Rocky Bernstein <rocky@panix.com>
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+*/
+
+/* This file contains Linux-specific code and implements low-level
+ control of the CD drive.
+*/
+
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
+
+static const char _rcsid[] = "$Id: _cdio_linux.c,v 1.1 2003/10/13 11:47:11 f1rmb Exp $";
+
+#include <string.h>
+
+#include <cdio/sector.h>
+#include <cdio/util.h>
+#include "cdio_assert.h"
+#include "cdio_private.h"
+#include "scsi_mmc.h"
+
+#ifdef HAVE_LINUX_CDROM
+
+#if defined(HAVE_LINUX_VERSION_H)
+# include <linux/version.h>
+# if LINUX_VERSION_CODE >= KERNEL_VERSION(2,2,16)
+# define __CDIO_LINUXCD_BUILD
+# else
+# error "You need a kernel greater than 2.2.16 to have CDROM support"
+# endif
+#else
+# error "You need <linux/version.h> to have CDROM support"
+#endif
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <errno.h>
+#include <unistd.h>
+#include <fcntl.h>
+#include <mntent.h>
+
+#include <linux/cdrom.h>
+#include <scsi/scsi.h>
+#include <scsi/sg.h>
+#include <scsi/scsi_ioctl.h>
+#include <sys/mount.h>
+
+#include <sys/stat.h>
+#include <sys/types.h>
+#include <sys/ioctl.h>
+
+#define TOTAL_TRACKS (_obj->tochdr.cdth_trk1)
+#define FIRST_TRACK_NUM (_obj->tochdr.cdth_trk0)
+
+typedef struct {
+ /* Things common to all drivers like this.
+ This must be first. */
+ generic_img_private_t gen;
+
+ enum {
+ _AM_NONE,
+ _AM_IOCTL,
+ _AM_READ_CD,
+ _AM_READ_10
+ } access_mode;
+
+ /* Track information */
+ struct cdrom_tochdr tochdr;
+ struct cdrom_tocentry tocent[100]; /* entry info for each track */
+
+} _img_private_t;
+
+/* Some ioctl() errno values which occur when the tray is empty */
+#define ERRNO_TRAYEMPTY(errno) \
+ ((errno == EIO) || (errno == ENOENT) || (errno == EINVAL))
+
+
+/* Check a drive to see if it is a CD-ROM
+ Return 1 if a CD-ROM. 0 if it exists but isn't a CD-ROM drive
+ and -1 if no device exists .
+*/
+static bool
+cdio_is_cdrom(char *drive, char *mnttype)
+{
+ bool is_cd=false;
+ int cdfd;
+ struct cdrom_tochdr tochdr;
+
+ /* If it doesn't exist, return -1 */
+ if ( !cdio_is_device_quiet_generic(drive) ) {
+ return(false);
+ }
+
+ /* If it does exist, verify that it's an available CD-ROM */
+ cdfd = open(drive, (O_RDONLY|O_EXCL|O_NONBLOCK), 0);
+ if ( cdfd >= 0 ) {
+ if ( ioctl(cdfd, CDROMREADTOCHDR, &tochdr) != -1 ) {
+ is_cd = true;
+ }
+ close(cdfd);
+ }
+ /* Even if we can't read it, it might be mounted */
+ else if ( mnttype && (strcmp(mnttype, "iso9660") == 0) ) {
+ is_cd = true;
+ }
+ return(is_cd);
+}
+
+static char *
+cdio_check_mounts(const char *mtab)
+{
+ FILE *mntfp;
+ struct mntent *mntent;
+
+ mntfp = setmntent(mtab, "r");
+ if ( mntfp != NULL ) {
+ char *tmp;
+ char *mnt_type;
+ char *mnt_dev;
+
+ while ( (mntent=getmntent(mntfp)) != NULL ) {
+ mnt_type = malloc(strlen(mntent->mnt_type) + 1);
+ if (mnt_type == NULL)
+ continue; /* maybe you'll get lucky next time. */
+
+ mnt_dev = malloc(strlen(mntent->mnt_fsname) + 1);
+ if (mnt_dev == NULL) {
+ free(mnt_type);
+ continue;
+ }
+
+ strcpy(mnt_type, mntent->mnt_type);
+ strcpy(mnt_dev, mntent->mnt_fsname);
+
+ /* Handle "supermount" filesystem mounts */
+ if ( strcmp(mnt_type, "supermount") == 0 ) {
+ tmp = strstr(mntent->mnt_opts, "fs=");
+ if ( tmp ) {
+ free(mnt_type);
+ mnt_type = strdup(tmp + strlen("fs="));
+ if ( mnt_type ) {
+ tmp = strchr(mnt_type, ',');
+ if ( tmp ) {
+ *tmp = '\0';
+ }
+ }
+ }
+ tmp = strstr(mntent->mnt_opts, "dev=");
+ if ( tmp ) {
+ free(mnt_dev);
+ mnt_dev = strdup(tmp + strlen("dev="));
+ if ( mnt_dev ) {
+ tmp = strchr(mnt_dev, ',');
+ if ( tmp ) {
+ *tmp = '\0';
+ }
+ }
+ }
+ }
+ if ( strcmp(mnt_type, "iso9660") == 0 ) {
+ if (cdio_is_cdrom(mnt_dev, mnt_type) > 0) {
+ free(mnt_dev);
+ free(mnt_type);
+ endmntent(mntfp);
+ return strdup(mnt_dev);
+ }
+ }
+ free(mnt_dev);
+ free(mnt_type);
+ }
+ endmntent(mntfp);
+ }
+ return NULL;
+}
+
+static int
+_set_bsize (int fd, unsigned int bsize)
+{
+ struct cdrom_generic_command cgc;
+
+ struct
+ {
+ uint8_t reserved1;
+ uint8_t medium;
+ uint8_t reserved2;
+ uint8_t block_desc_length;
+ uint8_t density;
+ uint8_t number_of_blocks_hi;
+ uint8_t number_of_blocks_med;
+ uint8_t number_of_blocks_lo;
+ uint8_t reserved3;
+ uint8_t block_length_hi;
+ uint8_t block_length_med;
+ uint8_t block_length_lo;
+ } mh;
+
+ memset (&mh, 0, sizeof (mh));
+ memset (&cgc, 0, sizeof (struct cdrom_generic_command));
+
+ cgc.cmd[0] = 0x15;
+ cgc.cmd[1] = 1 << 4;
+ cgc.cmd[4] = 12;
+
+ cgc.buflen = sizeof (mh);
+ cgc.buffer = (void *) &mh;
+
+ cgc.data_direction = CGC_DATA_WRITE;
+
+ mh.block_desc_length = 0x08;
+ mh.block_length_hi = (bsize >> 16) & 0xff;
+ mh.block_length_med = (bsize >> 8) & 0xff;
+ mh.block_length_lo = (bsize >> 0) & 0xff;
+
+ return ioctl (fd, CDROM_SEND_PACKET, &cgc);
+}
+
+/* Packet driver to read mode2 sectors.
+ Can read only up to 25 blocks.
+*/
+static int
+_cdio_mmc_read_sectors (int fd, void *buf, lba_t lba, int sector_type,
+ unsigned int nblocks)
+{
+ typedef struct cdrom_generic_command cgc_t;
+ cgc_t cgc;
+
+ memset (&cgc, 0, sizeof (cgc_t));
+
+ cgc.cmd[0] = CDIO_MMC_GPCMD_READ_CD;
+ CDIO_MMC_SET_READ_TYPE (cgc.cmd, sector_type);
+ CDIO_MMC_SET_READ_LBA (cgc.cmd, lba);
+ CDIO_MMC_SET_READ_LENGTH(cgc.cmd, nblocks);
+ CDIO_MMC_SET_MAIN_CHANNEL_SELECTION_BITS(cgc.cmd, CDIO_MMC_MCSB_ALL_HEADERS);
+
+ cgc.buflen = CDIO_CD_FRAMESIZE_RAW * nblocks;
+ cgc.buffer = buf;
+
+#ifdef HAVE_LINUX_CDROM_TIMEOUT
+ cgc.timeout = 500;
+#endif
+ cgc.data_direction = CGC_DATA_READ;
+
+ return ioctl (fd, CDROM_SEND_PACKET, &cgc);
+
+ return 0;
+}
+
+/* MMC driver to read audio sectors.
+ Can read only up to 25 blocks.
+*/
+static int
+_cdio_read_audio_sectors (void *env, void *buf, lsn_t lsn,
+ unsigned int nblocks)
+{
+ _img_private_t *_obj = env;
+ return _cdio_mmc_read_sectors( _obj->gen.fd, buf, lsn,
+ CDIO_MMC_READ_TYPE_CDDA, nblocks);
+}
+
+/* Packet driver to read mode2 sectors.
+ Can read only up to 25 blocks.
+*/
+static int
+__read_packet_mode2_sectors (int fd, void *buf, lba_t lba,
+ unsigned int nblocks, bool use_read_10)
+{
+ struct cdrom_generic_command cgc;
+
+ memset (&cgc, 0, sizeof (struct cdrom_generic_command));
+
+ cgc.cmd[0] = use_read_10 ? GPCMD_READ_10 : CDIO_MMC_GPCMD_READ_CD;
+
+ CDIO_MMC_SET_READ_LBA(cgc.cmd, lba);
+ CDIO_MMC_SET_READ_LENGTH(cgc.cmd, nblocks);
+
+ if (!use_read_10)
+ {
+ cgc.cmd[1] = 0; /* sector size mode2 */
+
+ cgc.cmd[9] = 0x58; /* 2336 mode2 */
+ }
+
+ cgc.buflen = 2336 * nblocks;
+ cgc.buffer = buf;
+
+#ifdef HAVE_LINUX_CDROM_TIMEOUT
+ cgc.timeout = 500;
+#endif
+ cgc.data_direction = CGC_DATA_READ;
+
+ if (use_read_10)
+ {
+ int retval;
+
+ if ((retval = _set_bsize (fd, 2336)))
+ return retval;
+
+ if ((retval = ioctl (fd, CDROM_SEND_PACKET, &cgc)))
+ {
+ _set_bsize (fd, 2048);
+ return retval;
+ }
+
+ if ((retval = _set_bsize (fd, 2048)))
+ return retval;
+ }
+ else
+ return ioctl (fd, CDROM_SEND_PACKET, &cgc);
+
+ return 0;
+}
+
+static int
+_read_packet_mode2_sectors (int fd, void *buf, lba_t lba,
+ unsigned int nblocks, bool use_read_10)
+{
+ unsigned int l = 0;
+ int retval = 0;
+
+ while (nblocks > 0)
+ {
+ const unsigned nblocks2 = (nblocks > 25) ? 25 : nblocks;
+ void *buf2 = ((char *)buf ) + (l * 2336);
+
+ retval |= __read_packet_mode2_sectors (fd, buf2, lba + l, nblocks2,
+ use_read_10);
+
+ if (retval)
+ break;
+
+ nblocks -= nblocks2;
+ l += nblocks2;
+ }
+
+ return retval;
+}
+
+/*!
+ Reads a single mode2 sector from cd device into data starting
+ from lsn. Returns 0 if no error.
+ */
+static int
+_cdio_read_mode2_sector (void *env, void *data, lsn_t lsn,
+ bool mode2_form2)
+{
+ char buf[M2RAW_SECTOR_SIZE] = { 0, };
+ struct cdrom_msf *msf = (struct cdrom_msf *) &buf;
+ msf_t _msf;
+
+ _img_private_t *_obj = env;
+
+ cdio_lba_to_msf (cdio_lsn_to_lba(lsn), &_msf);
+ msf->cdmsf_min0 = from_bcd8(_msf.m);
+ msf->cdmsf_sec0 = from_bcd8(_msf.s);
+ msf->cdmsf_frame0 = from_bcd8(_msf.f);
+
+ retry:
+ switch (_obj->access_mode)
+ {
+ case _AM_NONE:
+ cdio_error ("no way to read mode2");
+ return 1;
+ break;
+
+ case _AM_IOCTL:
+ if (ioctl (_obj->gen.fd, CDROMREADMODE2, &buf) == -1)
+ {
+ perror ("ioctl()");
+ return 1;
+ /* exit (EXIT_FAILURE); */
+ }
+ break;
+
+ case _AM_READ_CD:
+ case _AM_READ_10:
+ if (_read_packet_mode2_sectors (_obj->gen.fd, buf, lsn, 1,
+ (_obj->access_mode == _AM_READ_10)))
+ {
+ perror ("ioctl()");
+ if (_obj->access_mode == _AM_READ_CD)
+ {
+ cdio_info ("READ_CD failed; switching to READ_10 mode...");
+ _obj->access_mode = _AM_READ_10;
+ goto retry;
+ }
+ else
+ {
+ cdio_info ("READ_10 failed; switching to ioctl(CDROMREADMODE2) mode...");
+ _obj->access_mode = _AM_IOCTL;
+ goto retry;
+ }
+ return 1;
+ }
+ break;
+ }
+
+ if (mode2_form2)
+ memcpy (data, buf, M2RAW_SECTOR_SIZE);
+ else
+ memcpy (((char *)data), buf + CDIO_CD_SUBHEADER_SIZE, CDIO_CD_FRAMESIZE);
+
+ return 0;
+}
+
+/*!
+ Reads nblocks of mode2 sectors from cd device into data starting
+ from lsn.
+ Returns 0 if no error.
+ */
+static int
+_cdio_read_mode2_sectors (void *env, void *data, lsn_t lsn,
+ bool mode2_form2, unsigned int nblocks)
+{
+ _img_private_t *_obj = env;
+ unsigned int i;
+ int retval;
+
+ for (i = 0; i < nblocks; i++) {
+ if (mode2_form2) {
+ if ( (retval = _cdio_read_mode2_sector (_obj,
+ ((char *)data)
+ + (M2RAW_SECTOR_SIZE * i),
+ lsn + i, true)) )
+ return retval;
+ } else {
+ char buf[M2RAW_SECTOR_SIZE] = { 0, };
+ if ( (retval = _cdio_read_mode2_sector (_obj, buf, lsn + i, true)) )
+ return retval;
+
+ memcpy (((char *)data) + (CDIO_CD_FRAMESIZE * i),
+ buf + CDIO_CD_SUBHEADER_SIZE, CDIO_CD_FRAMESIZE);
+ }
+ }
+ return 0;
+}
+
+/*!
+ Return the size of the CD in logical block address (LBA) units.
+ */
+static uint32_t
+_cdio_stat_size (void *env)
+{
+ _img_private_t *_obj = env;
+
+ struct cdrom_tocentry tocent;
+ uint32_t size;
+
+ tocent.cdte_track = CDIO_CDROM_LEADOUT_TRACK;
+ tocent.cdte_format = CDROM_LBA;
+ if (ioctl (_obj->gen.fd, CDROMREADTOCENTRY, &tocent) == -1)
+ {
+ perror ("ioctl(CDROMREADTOCENTRY)");
+ exit (EXIT_FAILURE);
+ }
+
+ size = tocent.cdte_addr.lba;
+
+ return size;
+}
+
+/*!
+ Set the arg "key" with "value" in the source device.
+ Currently "source" and "access-mode" are valid keys.
+ "source" sets the source device in I/O operations
+ "access-mode" sets the the method of CD access
+
+ 0 is returned if no error was found, and nonzero if there as an error.
+*/
+static int
+_cdio_set_arg (void *env, const char key[], const char value[])
+{
+ _img_private_t *_obj = env;
+
+ if (!strcmp (key, "source"))
+ {
+ if (!value)
+ return -2;
+
+ free (_obj->gen.source_name);
+
+ _obj->gen.source_name = strdup (value);
+ }
+ else if (!strcmp (key, "access-mode"))
+ {
+ if (!strcmp(value, "IOCTL"))
+ _obj->access_mode = _AM_IOCTL;
+ else if (!strcmp(value, "READ_CD"))
+ _obj->access_mode = _AM_READ_CD;
+ else if (!strcmp(value, "READ_10"))
+ _obj->access_mode = _AM_READ_10;
+ else {
+ cdio_warn ("unknown access type: %s. ignored.", value);
+ }
+ }
+ else
+ return -1;
+
+ return 0;
+}
+
+/*!
+ Read and cache the CD's Track Table of Contents and track info.
+ Return false if successful or true if an error.
+*/
+static bool
+_cdio_read_toc (_img_private_t *_obj)
+{
+ int i;
+
+ /* read TOC header */
+ if ( ioctl(_obj->gen.fd, CDROMREADTOCHDR, &_obj->tochdr) == -1 ) {
+ cdio_error("%s: %s\n",
+ "error in ioctl CDROMREADTOCHDR", strerror(errno));
+ return false;
+ }
+
+ /* read individual tracks */
+ for (i= FIRST_TRACK_NUM; i<=TOTAL_TRACKS; i++) {
+ _obj->tocent[i-1].cdte_track = i;
+ _obj->tocent[i-1].cdte_format = CDROM_MSF;
+ if ( ioctl(_obj->gen.fd, CDROMREADTOCENTRY, &_obj->tocent[i-1]) == -1 ) {
+ cdio_error("%s %d: %s\n",
+ "error in ioctl CDROMREADTOCENTRY for track",
+ i, strerror(errno));
+ return false;
+ }
+ /****
+ struct cdrom_msf0 *msf= &_obj->tocent[i-1].cdte_addr.msf;
+
+ fprintf (stdout, "--- track# %d (msf %2.2x:%2.2x:%2.2x)\n",
+ i, msf->minute, msf->second, msf->frame);
+ ****/
+
+ }
+
+ /* read the lead-out track */
+ _obj->tocent[TOTAL_TRACKS].cdte_track = CDIO_CDROM_LEADOUT_TRACK;
+ _obj->tocent[TOTAL_TRACKS].cdte_format = CDROM_MSF;
+
+ if (ioctl(_obj->gen.fd, CDROMREADTOCENTRY,
+ &_obj->tocent[TOTAL_TRACKS]) == -1 ) {
+ cdio_error("%s: %s\n",
+ "error in ioctl CDROMREADTOCENTRY for lead-out",
+ strerror(errno));
+ return false;
+ }
+
+ /*
+ struct cdrom_msf0 *msf= &_obj->tocent[TOTAL_TRACKS].cdte_addr.msf;
+
+ fprintf (stdout, "--- track# %d (msf %2.2x:%2.2x:%2.2x)\n",
+ i, msf->minute, msf->second, msf->frame);
+ */
+
+ _obj->gen.toc_init = true;
+ return true;
+}
+
+/*
+ * Eject using SCSI commands. Return 1 if successful, 0 otherwise.
+ */
+static int
+_cdio_eject_scsi(int fd)
+{
+ int status;
+ struct sdata {
+ int inlen;
+ int outlen;
+ char cmd[256];
+ } scsi_cmd;
+
+ scsi_cmd.inlen = 0;
+ scsi_cmd.outlen = 0;
+ scsi_cmd.cmd[0] = ALLOW_MEDIUM_REMOVAL;
+ scsi_cmd.cmd[1] = 0;
+ scsi_cmd.cmd[2] = 0;
+ scsi_cmd.cmd[3] = 0;
+ scsi_cmd.cmd[4] = 0;
+ scsi_cmd.cmd[5] = 0;
+ status = ioctl(fd, SCSI_IOCTL_SEND_COMMAND, (void *)&scsi_cmd);
+ if (status != 0)
+ return 0;
+
+ scsi_cmd.inlen = 0;
+ scsi_cmd.outlen = 0;
+ scsi_cmd.cmd[0] = START_STOP;
+ scsi_cmd.cmd[1] = 0;
+ scsi_cmd.cmd[2] = 0;
+ scsi_cmd.cmd[3] = 0;
+ scsi_cmd.cmd[4] = 1;
+ scsi_cmd.cmd[5] = 0;
+ status = ioctl(fd, SCSI_IOCTL_SEND_COMMAND, (void *)&scsi_cmd);
+ if (status != 0)
+ return 0;
+
+ scsi_cmd.inlen = 0;
+ scsi_cmd.outlen = 0;
+ scsi_cmd.cmd[0] = START_STOP;
+ scsi_cmd.cmd[1] = 0;
+ scsi_cmd.cmd[2] = 0;
+ scsi_cmd.cmd[3] = 0;
+ scsi_cmd.cmd[4] = 2;
+ scsi_cmd.cmd[5] = 0;
+ status = ioctl(fd, SCSI_IOCTL_SEND_COMMAND, (void *)&scsi_cmd);
+ if (status != 0)
+ return 0;
+
+ /* force kernel to reread partition table when new disc inserted */
+ status = ioctl(fd, BLKRRPART);
+ return (status == 0);
+}
+
+/*!
+ Eject media in CD drive.
+ Return 0 if success and 1 for failure, and 2 if no routine.
+ */
+static int
+_cdio_eject_media (void *env) {
+
+ _img_private_t *_obj = env;
+ int ret=2;
+ int status;
+ int fd;
+
+ close(_obj->gen.fd);
+ _obj->gen.fd = -1;
+ if ((fd = open (_obj->gen.source_name, O_RDONLY|O_NONBLOCK)) > -1) {
+ if((status = ioctl(fd, CDROM_DRIVE_STATUS, CDSL_CURRENT)) > 0) {
+ switch(status) {
+ case CDS_TRAY_OPEN:
+ if((ret = ioctl(fd, CDROMCLOSETRAY)) != 0) {
+ cdio_error ("ioctl CDROMCLOSETRAY failed: %s\n", strerror(errno));
+ ret = 1;
+ }
+ break;
+ case CDS_DISC_OK:
+ if((ret = ioctl(fd, CDROMEJECT)) != 0) {
+ int eject_error = errno;
+ /* Try ejecting the SCSI way... */
+ ret = _cdio_eject_scsi(fd);
+ if (0 != ret) {
+ cdio_error("ioctl CDROMEJECT failed: %s\n", strerror(eject_error));
+ ret = 1;
+ }
+ }
+ break;
+ default:
+ cdio_error ("Unknown CD-ROM (%d)\n", status);
+ ret = 1;
+ }
+ } else {
+ cdio_error ("CDROM_DRIVE_STATUS failed: %s\n", strerror(errno));
+ ret=1;
+ }
+ close(fd);
+ return ret;
+ }
+ return 2;
+}
+
+/*!
+ Return the value associated with the key "arg".
+*/
+static const char *
+_cdio_get_arg (void *env, const char key[])
+{
+ _img_private_t *_obj = env;
+
+ if (!strcmp (key, "source")) {
+ return _obj->gen.source_name;
+ } else if (!strcmp (key, "access-mode")) {
+ switch (_obj->access_mode) {
+ case _AM_IOCTL:
+ return "ioctl";
+ case _AM_READ_CD:
+ return "READ_CD";
+ case _AM_READ_10:
+ return "READ_10";
+ case _AM_NONE:
+ return "no access method";
+ }
+ }
+ return NULL;
+}
+
+/*!
+ Return the number of of the first track.
+ CDIO_INVALID_TRACK is returned on error.
+*/
+static track_t
+_cdio_get_first_track_num(void *env)
+{
+ _img_private_t *_obj = env;
+
+ if (!_obj->gen.toc_init) _cdio_read_toc (_obj) ;
+
+ return FIRST_TRACK_NUM;
+}
+
+/*!
+ Return the media catalog number MCN.
+
+ Note: string is malloc'd so caller should free() then returned
+ string when done with it.
+
+ */
+static char *
+_cdio_get_mcn (void *env) {
+
+ struct cdrom_mcn mcn;
+ _img_private_t *_obj = env;
+ if (ioctl(_obj->gen.fd, CDROM_GET_MCN, &mcn) != 0)
+ return NULL;
+ return strdup(mcn.medium_catalog_number);
+}
+
+/*!
+ Return the number of tracks in the current medium.
+ CDIO_INVALID_TRACK is returned on error.
+*/
+static track_t
+_cdio_get_num_tracks(void *env)
+{
+ _img_private_t *_obj = env;
+
+ if (!_obj->gen.toc_init) _cdio_read_toc (_obj) ;
+
+ return TOTAL_TRACKS;
+}
+
+/*!
+ Get format of track.
+*/
+static track_format_t
+_cdio_get_track_format(void *env, track_t track_num)
+{
+ _img_private_t *_obj = env;
+
+ if (!_obj->gen.toc_init) _cdio_read_toc (_obj) ;
+
+ if (track_num > TOTAL_TRACKS || track_num == 0)
+ return TRACK_FORMAT_ERROR;
+
+ /* This is pretty much copied from the "badly broken" cdrom_count_tracks
+ in linux/cdrom.c.
+ */
+ if (_obj->tocent[track_num-1].cdte_ctrl & CDROM_DATA_TRACK) {
+ if (_obj->tocent[track_num-1].cdte_format == 0x10)
+ return TRACK_FORMAT_CDI;
+ else if (_obj->tocent[track_num-1].cdte_format == 0x20)
+ return TRACK_FORMAT_XA;
+ else
+ return TRACK_FORMAT_DATA;
+ } else
+ return TRACK_FORMAT_AUDIO;
+
+}
+
+/*!
+ Return true if we have XA data (green, mode2 form1) or
+ XA data (green, mode2 form2). That is track begins:
+ sync - header - subheader
+ 12 4 - 8
+
+ FIXME: there's gotta be a better design for this and get_track_format?
+*/
+static bool
+_cdio_get_track_green(void *env, track_t track_num)
+{
+ _img_private_t *_obj = env;
+
+ if (!_obj->gen.toc_init) _cdio_read_toc (_obj) ;
+
+ if (track_num == CDIO_CDROM_LEADOUT_TRACK) track_num = TOTAL_TRACKS+1;
+
+ if (track_num > TOTAL_TRACKS+1 || track_num == 0)
+ return false;
+
+ /* FIXME: Dunno if this is the right way, but it's what
+ I was using in cdinfo for a while.
+ */
+ return ((_obj->tocent[track_num-1].cdte_ctrl & 2) != 0);
+}
+
+/*!
+ Return the starting MSF (minutes/secs/frames) for track number
+ track_num in obj. Track numbers start at 1.
+ The "leadout" track is specified either by
+ using track_num LEADOUT_TRACK or the total tracks+1.
+ False is returned if there is no track entry.
+*/
+static bool
+_cdio_get_track_msf(void *env, track_t track_num, msf_t *msf)
+{
+ _img_private_t *_obj = env;
+
+ if (NULL == msf) return false;
+
+ if (!_obj->gen.toc_init) _cdio_read_toc (_obj) ;
+
+ if (track_num == CDIO_CDROM_LEADOUT_TRACK) track_num = TOTAL_TRACKS+1;
+
+ if (track_num > TOTAL_TRACKS+1 || track_num == 0) {
+ return false;
+ } else {
+ struct cdrom_msf0 *msf0= &_obj->tocent[track_num-1].cdte_addr.msf;
+ msf->m = to_bcd8(msf0->minute);
+ msf->s = to_bcd8(msf0->second);
+ msf->f = to_bcd8(msf0->frame);
+ return true;
+ }
+}
+
+/* checklist: /dev/cdrom, /dev/dvd /dev/hd?, /dev/scd? /dev/sr? */
+static char checklist1[][40] = {
+ {"cdrom"}, {"dvd"}, {""}
+};
+static char checklist2[][40] = {
+ {"?a hd?"}, {"?0 scd?"}, {"?0 sr?"}, {""}
+};
+
+#endif /* HAVE_LINUX_CDROM */
+
+/*!
+ Return an array of strings giving possible CD devices.
+ */
+char **
+cdio_get_devices_linux (void)
+{
+#ifndef HAVE_LINUX_CDROM
+ return NULL;
+#else
+ unsigned int i;
+ char drive[40];
+ char *ret_drive;
+ bool exists;
+ char **drives = NULL;
+ unsigned int num_drives=0;
+
+ /* Scan the system for CD-ROM drives.
+ */
+ for ( i=0; strlen(checklist1[i]) > 0; ++i ) {
+ sprintf(drive, "/dev/%s", checklist1[i]);
+ if ( (exists=cdio_is_cdrom(drive, NULL)) > 0 ) {
+ cdio_add_device_list(&drives, drive, &num_drives);
+ }
+ }
+
+ /* Now check the currently mounted CD drives */
+ if (NULL != (ret_drive = cdio_check_mounts("/etc/mtab"))) {
+ cdio_add_device_list(&drives, drive, &num_drives);
+ }
+
+ /* Finally check possible mountable drives in /etc/fstab */
+ if (NULL != (ret_drive = cdio_check_mounts("/etc/fstab"))) {
+ cdio_add_device_list(&drives, drive, &num_drives);
+ }
+
+ /* Scan the system for CD-ROM drives.
+ Not always 100% reliable, so use the USE_MNTENT code above first.
+ */
+ for ( i=0; strlen(checklist2[i]) > 0; ++i ) {
+ unsigned int j;
+ char *insert;
+ exists = true;
+ for ( j=checklist2[i][1]; exists; ++j ) {
+ sprintf(drive, "/dev/%s", &checklist2[i][3]);
+ insert = strchr(drive, '?');
+ if ( insert != NULL ) {
+ *insert = j;
+ }
+ if ( (exists=cdio_is_cdrom(drive, NULL)) > 0 ) {
+ cdio_add_device_list(&drives, drive, &num_drives);
+ }
+ }
+ }
+ cdio_add_device_list(&drives, NULL, &num_drives);
+ return drives;
+#endif /*HAVE_LINUX_CDROM*/
+}
+
+/*!
+ Return a string containing the default CD device.
+ */
+char *
+cdio_get_default_device_linux(void)
+{
+#ifndef HAVE_LINUX_CDROM
+ return NULL;
+
+#else
+ unsigned int i;
+ char drive[40];
+ bool exists;
+ char *ret_drive;
+
+ /* Scan the system for CD-ROM drives.
+ */
+ for ( i=0; strlen(checklist1[i]) > 0; ++i ) {
+ sprintf(drive, "/dev/%s", checklist1[i]);
+ if ( (exists=cdio_is_cdrom(drive, NULL)) > 0 ) {
+ return strdup(drive);
+ }
+ }
+
+ /* Now check the currently mounted CD drives */
+ if (NULL != (ret_drive = cdio_check_mounts("/etc/mtab")))
+ return ret_drive;
+
+ /* Finally check possible mountable drives in /etc/fstab */
+ if (NULL != (ret_drive = cdio_check_mounts("/etc/fstab")))
+ return ret_drive;
+
+ /* Scan the system for CD-ROM drives.
+ Not always 100% reliable, so use the USE_MNTENT code above first.
+ */
+ for ( i=0; strlen(checklist2[i]) > 0; ++i ) {
+ unsigned int j;
+ char *insert;
+ exists = true;
+ for ( j=checklist2[i][1]; exists; ++j ) {
+ sprintf(drive, "/dev/%s", &checklist2[i][3]);
+ insert = strchr(drive, '?');
+ if ( insert != NULL ) {
+ *insert = j;
+ }
+ if ( (exists=cdio_is_cdrom(drive, NULL)) > 0 ) {
+ return(strdup(drive));
+ }
+ }
+ }
+ return NULL;
+#endif /*HAVE_LINUX_CDROM*/
+}
+/*!
+ Initialization routine. This is the only thing that doesn't
+ get called via a function pointer. In fact *we* are the
+ ones to set that up.
+ */
+CdIo *
+cdio_open_linux (const char *orig_source_name)
+{
+
+#ifdef HAVE_LINUX_CDROM
+ CdIo *ret;
+ _img_private_t *_data;
+ char *source_name;
+
+ cdio_funcs _funcs = {
+ .eject_media = _cdio_eject_media,
+ .free = cdio_generic_free,
+ .get_arg = _cdio_get_arg,
+ .get_devices = cdio_get_devices_linux,
+ .get_default_device = cdio_get_default_device_linux,
+ .get_first_track_num= _cdio_get_first_track_num,
+ .get_mcn = _cdio_get_mcn,
+ .get_num_tracks = _cdio_get_num_tracks,
+ .get_track_format = _cdio_get_track_format,
+ .get_track_green = _cdio_get_track_green,
+ .get_track_lba = NULL, /* This could be implemented if need be. */
+ .get_track_msf = _cdio_get_track_msf,
+ .lseek = cdio_generic_lseek,
+ .read = cdio_generic_read,
+ .read_audio_sectors = _cdio_read_audio_sectors,
+ .read_mode2_sector = _cdio_read_mode2_sector,
+ .read_mode2_sectors = _cdio_read_mode2_sectors,
+ .set_arg = _cdio_set_arg,
+ .stat_size = _cdio_stat_size
+ };
+
+ _data = _cdio_malloc (sizeof (_img_private_t));
+ _data->access_mode = _AM_READ_CD;
+ _data->gen.init = false;
+ _data->gen.fd = -1;
+
+ if (NULL == orig_source_name) {
+ source_name=cdio_get_default_device_linux();
+ if (NULL == source_name) return NULL;
+ _cdio_set_arg(_data, "source", source_name);
+ free(source_name);
+ } else
+ _cdio_set_arg(_data, "source", orig_source_name);
+
+ ret = cdio_new (_data, &_funcs);
+ if (ret == NULL) return NULL;
+
+ if (cdio_generic_init(_data))
+ return ret;
+ else {
+ cdio_generic_free (_data);
+ return NULL;
+ }
+
+#else
+ return NULL;
+#endif /* HAVE_LINUX_CDROM */
+
+}
+
+bool
+cdio_have_linux (void)
+{
+#ifdef HAVE_LINUX_CDROM
+ return true;
+#else
+ return false;
+#endif /* HAVE_LINUX_CDROM */
+}
+
+
diff --git a/src/input/vcd/libcdio/_cdio_nrg.c b/src/input/vcd/libcdio/_cdio_nrg.c
new file mode 100644
index 000000000..4b92f105f
--- /dev/null
+++ b/src/input/vcd/libcdio/_cdio_nrg.c
@@ -0,0 +1,938 @@
+/*
+ $Id: _cdio_nrg.c,v 1.1 2003/10/13 11:47:11 f1rmb Exp $
+
+ Copyright (C) 2001,2003 Herbert Valerio Riedel <hvr@gnu.org>
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+*/
+/*! This code implements low-level access functions for the Nero native
+ CD-image format residing inside a disk file (*.nrg).
+*/
+
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
+
+#ifdef HAVE_STDIO_H
+#include <stdio.h>
+#endif
+#ifdef HAVE_STDLIB_H
+#include <stdlib.h>
+#endif
+#ifdef HAVE_STRING_H
+#include <string.h>
+#endif
+#ifdef HAVE_GLOB_H
+#include <glob.h>
+#endif
+
+#include <cdio/logging.h>
+#include <cdio/sector.h>
+#include <cdio/util.h>
+#include "cdio_assert.h"
+#include "bytesex.h"
+#include "ds.h"
+#include "cdio_private.h"
+#include "_cdio_stdio.h"
+
+static const char _rcsid[] = "$Id: _cdio_nrg.c,v 1.1 2003/10/13 11:47:11 f1rmb Exp $";
+
+/* structures used */
+
+/* this ugly image format is typical for lazy win32 programmers... at
+ least structure were set big endian, so at reverse
+ engineering wasn't such a big headache... */
+
+PRAGMA_BEGIN_PACKED
+typedef struct {
+ uint32_t start GNUC_PACKED;
+ uint32_t length GNUC_PACKED;
+ uint32_t type GNUC_PACKED; /* only 0x3 seen so far...
+ -> MIXED_MODE2 2336 blocksize */
+ uint32_t start_lsn GNUC_PACKED; /* does not include any pre-gaps! */
+ uint32_t _unknown GNUC_PACKED; /* wtf is this for? -- always zero... */
+} _etnf_array_t;
+
+/* finally they realized that 32bit offsets are a bit outdated for IA64 *eg* */
+typedef struct {
+ uint64_t start GNUC_PACKED;
+ uint64_t length GNUC_PACKED;
+ uint32_t type GNUC_PACKED;
+ uint32_t start_lsn GNUC_PACKED;
+ uint64_t _unknown GNUC_PACKED; /* wtf is this for? -- always zero... */
+} _etn2_array_t;
+
+typedef struct {
+ uint8_t _unknown1 GNUC_PACKED; /* 0x41 == 'A' */
+ uint8_t track GNUC_PACKED; /* binary or BCD?? */
+ uint8_t index GNUC_PACKED; /* makes 0->1 transitions */
+ uint8_t _unknown2 GNUC_PACKED; /* ?? */
+ uint32_t lsn GNUC_PACKED;
+} _cuex_array_t;
+
+typedef struct {
+ uint32_t id GNUC_PACKED;
+ uint32_t len GNUC_PACKED;
+ char data[EMPTY_ARRAY_SIZE] GNUC_PACKED;
+} _chunk_t;
+
+PRAGMA_END_PACKED
+
+/* to be converted into BE */
+#define CUEX_ID 0x43554558
+#define CUES_ID 0x43554553
+#define DAOX_ID 0x44414f58
+#define DAOI_ID 0x44414f49
+#define END1_ID 0x454e4421
+#define ETN2_ID 0x45544e32
+#define ETNF_ID 0x45544e46
+#define NER5_ID 0x4e455235
+#define NERO_ID 0x4e45524f
+#define SINF_ID 0x53494e46
+
+/* reader */
+
+#define DEFAULT_CDIO_DEVICE "image.nrg"
+
+typedef struct {
+ int track_num; /* Probably is index+1 */
+ msf_t start_msf;
+ lba_t start_lba;
+ int start_index;
+ int sec_count; /* Number of sectors in track. Does not
+ include pregap before next entry. */
+ int flags; /* "DCP", "4CH", "PRE" */
+ track_format_t track_format;
+ bool track_green;
+ uint16_t datasize; /* How much is in the portion we return back? */
+ long int datastart; /* Offset from begining that data starts */
+ uint16_t endsize; /* How much stuff at the end to skip over. This
+ stuff may have error correction (EDC, or ECC).*/
+ uint16_t blocksize; /* total block size = start + size + end */
+} track_info_t;
+
+/*
+ Link element of track structure as a linked list.
+ Possibly redundant with above track_info_t */
+typedef struct {
+ uint32_t start_lsn;
+ uint32_t sec_count; /* Number of sectors in track. Does not
+ include pregap before next entry. */
+ uint64_t img_offset; /* Bytes offset from beginning of disk image file.*/
+} _mapping_t;
+
+
+typedef struct {
+ /* Things common to all drivers like this.
+ This must be first. */
+ generic_img_private_t gen;
+ internal_position_t pos;
+
+ /* This is a hack because I don't really understnad NERO better. */
+ bool is_cues;
+
+ bool sector_2336;
+ track_info_t tocent[100]; /* entry info for each track */
+ track_t total_tracks; /* number of tracks in image */
+ track_t first_track_num; /* track number of first track */
+ CdioList *mapping; /* List of track information */
+ uint32_t size;
+} _img_private_t;
+
+static bool _cdio_parse_nero_footer (_img_private_t *_obj);
+static uint32_t _cdio_stat_size (void *env);
+
+/* Updates internal track TOC, so we can later
+ simulate ioctl(CDROMREADTOCENTRY).
+ */
+static void
+_register_mapping (_img_private_t *_obj, lsn_t start_lsn, uint32_t sec_count,
+ uint64_t img_offset, uint32_t blocksize)
+{
+ const int track_num=_obj->total_tracks;
+ track_info_t *this_track=&(_obj->tocent[_obj->total_tracks]);
+ _mapping_t *_map = _cdio_malloc (sizeof (_mapping_t));
+
+ if (!_obj->mapping)
+ _obj->mapping = _cdio_list_new ();
+
+ _cdio_list_append (_obj->mapping, _map);
+ _map->start_lsn = start_lsn;
+ _map->sec_count = sec_count;
+
+ _map->img_offset = img_offset;
+
+ _obj->size = MAX (_obj->size, (start_lsn + sec_count));
+
+ /* Update *this_track and track_num. These structures are
+ in a sense redundant witht the obj->mapping list. Perhaps one
+ or the other can be eliminated.
+ */
+
+ cdio_lba_to_msf (cdio_lsn_to_lba(start_lsn), &(this_track->start_msf));
+ this_track->start_lba = cdio_msf_to_lba(&this_track->start_msf);
+ this_track->track_num = track_num+1;
+ this_track->blocksize = blocksize;
+ if (_obj->is_cues)
+ this_track->datastart = img_offset + 8;
+ else
+ this_track->datastart = 8;
+
+ this_track->sec_count = sec_count;
+
+ _obj->total_tracks++;
+
+ /* FIXME: These are probably not right. Probably we need to look at
+ "type." But this hasn't been reverse engineered yet.
+ */
+ this_track->track_format= TRACK_FORMAT_XA;
+ this_track->track_green = true;
+
+ /* cdio_debug ("map: %d +%d -> %ld", start_lsn, sec_count, img_offset); */
+}
+
+
+/*
+ Disk and track information for a Nero file are located at the end
+ of the file. This routine extracts that information.
+ */
+static bool
+_cdio_parse_nero_footer (_img_private_t *_obj)
+{
+ long unsigned int footer_start;
+ long unsigned int size;
+ char *footer_buf = NULL;
+
+ if (_obj->size)
+ return 0;
+
+ size = cdio_stream_stat (_obj->gen.data_source);
+ if (-1 == size) return false;
+
+ {
+PRAGMA_BEGIN_PACKED
+ union {
+ struct {
+ uint32_t __x GNUC_PACKED;
+ uint32_t ID GNUC_PACKED;
+ uint32_t footer_ofs GNUC_PACKED;
+ } v50;
+ struct {
+ uint32_t ID GNUC_PACKED;
+ uint64_t footer_ofs GNUC_PACKED;
+ } v55;
+ } buf;
+PRAGMA_END_PACKED
+
+ cdio_assert (sizeof (buf) == 12);
+
+ cdio_stream_seek (_obj->gen.data_source, size - sizeof (buf), SEEK_SET);
+ cdio_stream_read (_obj->gen.data_source, (void *) &buf, sizeof (buf), 1);
+
+ if (buf.v50.ID == UINT32_TO_BE (0x4e45524f)) /* "NERO" */
+ {
+ cdio_info ("detected v50 (32bit offsets) NRG magic");
+ footer_start = uint32_to_be (buf.v50.footer_ofs);
+ }
+ else if (buf.v55.ID == UINT32_TO_BE (0x4e455235)) /* "NER5" */
+ {
+ cdio_info ("detected v55 (64bit offsets) NRG magic");
+ footer_start = uint64_from_be (buf.v55.footer_ofs);
+ }
+ else
+ {
+ cdio_warn ("Image not recognized as either v50 or v55 type NRG");
+ return -1;
+ }
+
+ cdio_debug ("nrg footer start = %ld, length = %ld",
+ (long) footer_start, (long) (size - footer_start));
+
+ cdio_assert (IN ((size - footer_start), 0, 4096));
+
+ footer_buf = _cdio_malloc (size - footer_start);
+
+ cdio_stream_seek (_obj->gen.data_source, footer_start, SEEK_SET);
+ cdio_stream_read (_obj->gen.data_source, footer_buf, size - footer_start, 1);
+ }
+
+ {
+ int pos = 0;
+
+ while (pos < size - footer_start) {
+ _chunk_t *chunk = (void *) (footer_buf + pos);
+
+ bool break_out = false;
+
+ switch (UINT32_FROM_BE (chunk->id)) {
+ case CUES_ID: { /* "CUES" Seems to have sector size 2336 and 150 sector
+ pregap seems to be included at beginning of image.
+ */
+
+
+ unsigned entries = UINT32_FROM_BE (chunk->len);
+ _cuex_array_t *_entries = (void *) chunk->data;
+
+ cdio_assert (_obj->mapping == NULL);
+
+ cdio_assert (sizeof (_cuex_array_t) == 8);
+ cdio_assert ( UINT32_FROM_BE(chunk->len) % sizeof(_cuex_array_t)
+ == 0 );
+
+ entries /= sizeof (_cuex_array_t);
+
+ cdio_info ("CUES type image detected");
+
+ {
+ lsn_t lsn = UINT32_FROM_BE (_entries[0].lsn);
+ int idx;
+
+ /*cdio_assert (lsn == 0?);*/
+
+ _obj->is_cues = true; /* HACK alert. */
+ _obj->sector_2336 = true;
+ _obj->total_tracks = 0;
+ _obj->first_track_num = 1;
+ for (idx = 1; idx < entries-1; idx += 2) {
+ lsn_t sec_count;
+
+ cdio_assert (_entries[idx].index == 0);
+ cdio_assert (_entries[idx].track == _entries[idx + 1].track);
+
+ /* lsn and sec_count*2 aren't correct, but it comes closer on the
+ single example I have: svcdgs.nrg
+ We are picking up the wrong fields and/or not interpreting
+ them correctly.
+ */
+
+ lsn = UINT32_FROM_BE (_entries[idx].lsn);
+ sec_count = UINT32_FROM_BE (_entries[idx + 1].lsn);
+
+ _register_mapping (_obj, lsn, sec_count*2,
+ (lsn+CDIO_PREGAP_SECTORS) * M2RAW_SECTOR_SIZE,
+ M2RAW_SECTOR_SIZE);
+ }
+ }
+ break;
+ }
+
+ case CUEX_ID: /* "CUEX" */ {
+ unsigned entries = UINT32_FROM_BE (chunk->len);
+ _cuex_array_t *_entries = (void *) chunk->data;
+
+ cdio_assert (_obj->mapping == NULL);
+
+ cdio_assert ( sizeof (_cuex_array_t) == 8 );
+ cdio_assert ( UINT32_FROM_BE (chunk->len) % sizeof(_cuex_array_t)
+ == 0 );
+
+ entries /= sizeof (_cuex_array_t);
+
+ cdio_info ("DAO type image detected");
+
+ {
+ uint32_t lsn = UINT32_FROM_BE (_entries[0].lsn);
+ int idx;
+
+ cdio_assert (lsn == 0xffffff6a);
+
+ for (idx = 2; idx < entries; idx += 2) {
+ lsn_t lsn2;
+
+ cdio_assert (_entries[idx].index == 1);
+ cdio_assert (_entries[idx].track != _entries[idx + 1].track);
+
+ lsn = UINT32_FROM_BE (_entries[idx].lsn);
+ lsn2 = UINT32_FROM_BE (_entries[idx + 1].lsn);
+
+ _register_mapping (_obj, lsn, lsn2 - lsn,
+ (lsn + CDIO_PREGAP_SECTORS)*M2RAW_SECTOR_SIZE,
+ M2RAW_SECTOR_SIZE);
+ }
+ }
+ break;
+ }
+
+ case DAOI_ID: /* "DAOI" */
+ cdio_debug ("DAOI tag detected...");
+ break;
+ case DAOX_ID: /* "DAOX" */
+ cdio_debug ("DAOX tag detected...");
+ break;
+
+ case NER5_ID: /* "NER5" */
+ cdio_error ("unexpected nrg magic ID NER5 detected");
+ return -1;
+ break;
+
+ case NERO_ID: /* "NER0" */
+ cdio_error ("unexpected nrg magic ID NER0 detected");
+ return -1;
+ break;
+
+ case END1_ID: /* "END!" */
+ cdio_debug ("nrg end tag detected");
+ break_out = true;
+ break;
+
+ case ETNF_ID: /* "ETNF" */ {
+ unsigned entries = UINT32_FROM_BE (chunk->len);
+ _etnf_array_t *_entries = (void *) chunk->data;
+
+ cdio_assert (_obj->mapping == NULL);
+
+ cdio_assert ( sizeof (_etnf_array_t) == 20 );
+ cdio_assert ( UINT32_FROM_BE(chunk->len) % sizeof(_etnf_array_t)
+ == 0 );
+
+ entries /= sizeof (_etnf_array_t);
+
+ cdio_info ("SAO type image (ETNF) detected");
+
+ _obj->sector_2336 = true;
+
+ {
+ int idx;
+ for (idx = 0; idx < entries; idx++) {
+ uint32_t _len = UINT32_FROM_BE (_entries[idx].length);
+ uint32_t _start = UINT32_FROM_BE (_entries[idx].start_lsn);
+ uint32_t _start2 = UINT32_FROM_BE (_entries[idx].start);
+
+ cdio_assert (UINT32_FROM_BE (_entries[idx].type) == 3);
+ cdio_assert (_len % M2RAW_SECTOR_SIZE == 0);
+
+ _len /= M2RAW_SECTOR_SIZE;
+
+ cdio_assert (_start * M2RAW_SECTOR_SIZE == _start2);
+
+ _start += idx * CDIO_PREGAP_SECTORS;
+ _register_mapping (_obj, _start, _len, _start2, M2RAW_SECTOR_SIZE);
+
+ }
+ }
+ break;
+ }
+
+ case ETN2_ID: { /* "ETN2", same as above, but with 64bit stuff instead */
+ unsigned entries = uint32_from_be (chunk->len);
+ _etn2_array_t *_entries = (void *) chunk->data;
+
+ cdio_assert (_obj->mapping == NULL);
+
+ cdio_assert (sizeof (_etn2_array_t) == 32);
+ cdio_assert (uint32_from_be (chunk->len) % sizeof (_etn2_array_t) == 0);
+
+ entries /= sizeof (_etn2_array_t);
+
+ cdio_info ("SAO type image (ETN2) detected");
+
+ _obj->sector_2336 = true;
+
+ {
+ int idx;
+ for (idx = 0; idx < entries; idx++) {
+ uint32_t _len = uint64_from_be (_entries[idx].length);
+ uint32_t _start = uint32_from_be (_entries[idx].start_lsn);
+ uint32_t _start2 = uint64_from_be (_entries[idx].start);
+
+ cdio_assert (uint32_from_be (_entries[idx].type) == 3);
+ cdio_assert (_len % M2RAW_SECTOR_SIZE == 0);
+
+ _len /= M2RAW_SECTOR_SIZE;
+
+ cdio_assert (_start * M2RAW_SECTOR_SIZE == _start2);
+
+ _start += idx * CDIO_PREGAP_SECTORS;
+ _register_mapping (_obj, _start, _len, _start2, M2RAW_SECTOR_SIZE);
+ }
+ }
+ break;
+ }
+
+ case SINF_ID: { /* "SINF" */
+
+ uint32_t *_sessions = (void *) chunk->data;
+
+ cdio_assert (UINT32_FROM_BE (chunk->len) == 4);
+
+ cdio_debug ("SINF: %lu sessions",
+ (long unsigned int) UINT32_FROM_BE (*_sessions));
+ }
+ break;
+
+ default:
+ cdio_warn ("unknown tag %8.8x seen",
+ (unsigned int) UINT32_FROM_BE (chunk->id));
+ break;
+ }
+
+ if (break_out)
+ break;
+
+ pos += 8;
+ pos += UINT32_FROM_BE (chunk->len);
+ }
+ }
+
+ /* Fake out leadout track. */
+ /* Don't use _cdio_stat_size since that will lead to recursion since
+ we haven't fully initialized things yet.
+ */
+ cdio_lsn_to_msf (_obj->size, &_obj->tocent[_obj->total_tracks].start_msf);
+ _obj->tocent[_obj->total_tracks].start_lba = cdio_lsn_to_lba(_obj->size);
+ _obj->tocent[_obj->total_tracks-1].sec_count =
+ cdio_lsn_to_lba(_obj->size - _obj->tocent[_obj->total_tracks-1].start_lba);
+
+ return 0;
+}
+
+/*!
+ Initialize image structures.
+ */
+static bool
+_cdio_init (_img_private_t *_obj)
+{
+ if (_obj->gen.init) {
+ cdio_error ("init called more than once");
+ return false;
+ }
+
+ if (!(_obj->gen.data_source = cdio_stdio_new (_obj->gen.source_name))) {
+ cdio_error ("init failed");
+ return false;
+ }
+
+ _cdio_parse_nero_footer (_obj);
+ _obj->gen.init = true;
+ return true;
+
+}
+
+/*!
+ Reads into buf the next size bytes.
+ Returns -1 on error.
+ Would be libc's seek() but we have to adjust for the extra track header
+ information in each sector.
+*/
+static off_t
+_cdio_lseek (void *env, off_t offset, int whence)
+{
+ _img_private_t *_obj = env;
+
+ /* real_offset is the real byte offset inside the disk image
+ The number below was determined empirically. I'm guessing
+ the 1st 24 bytes of a bin file are used for something.
+ */
+ off_t real_offset=0;
+
+ unsigned int i;
+ unsigned int user_datasize;
+
+ for (i=0; i<_obj->total_tracks; i++) {
+ track_info_t *this_track=&(_obj->tocent[i]);
+ switch (this_track->track_format) {
+ case TRACK_FORMAT_AUDIO:
+ user_datasize=CDIO_CD_FRAMESIZE_RAW;
+ break;
+ case TRACK_FORMAT_CDI:
+ user_datasize=CDIO_CD_FRAMESIZE;
+ break;
+ case TRACK_FORMAT_XA:
+ user_datasize=CDIO_CD_FRAMESIZE;
+ break;
+ default:
+ user_datasize=CDIO_CD_FRAMESIZE_RAW;
+ cdio_warn ("track %d has unknown format %d",
+ i+1, this_track->track_format);
+ }
+
+ if ( (this_track->sec_count*user_datasize) >= offset) {
+ int blocks = offset / user_datasize;
+ int rem = offset % user_datasize;
+ int block_offset = blocks * (M2RAW_SECTOR_SIZE);
+ real_offset += block_offset + rem;
+ break;
+ }
+ real_offset += this_track->sec_count*(M2RAW_SECTOR_SIZE);
+
+ offset -= this_track->sec_count*user_datasize;
+ }
+
+ if (i==_obj->total_tracks) {
+ cdio_warn ("seeking outside range of disk image");
+ return -1;
+ } else
+ real_offset += _obj->tocent[i].datastart;
+ return cdio_stream_seek(_obj->gen.data_source, real_offset, whence);
+}
+
+/*!
+ Reads into buf the next size bytes.
+ Returns -1 on error.
+ FIXME:
+ At present we assume a read doesn't cross sector or track
+ boundaries.
+*/
+static ssize_t
+_cdio_read (void *env, void *buf, size_t size)
+{
+ _img_private_t *_obj = env;
+ return cdio_stream_read(_obj->gen.data_source, buf, size, 1);
+}
+
+static uint32_t
+_cdio_stat_size (void *env)
+{
+ _img_private_t *_obj = env;
+
+ return _obj->size;
+}
+
+/*!
+ Reads a single audio sector from CD device into data starting
+ from LSN. Returns 0 if no error.
+ */
+static int
+_cdio_read_audio_sectors (void *env, void *data, lsn_t lsn,
+ unsigned int nblocks)
+{
+ _img_private_t *_obj = env;
+
+ CdioListNode *node;
+
+ if (lsn >= _obj->size)
+ {
+ cdio_warn ("trying to read beyond image size (%lu >= %lu)",
+ (long unsigned int) lsn, (long unsigned int) _obj->size);
+ return -1;
+ }
+
+ _CDIO_LIST_FOREACH (node, _obj->mapping) {
+ _mapping_t *_map = _cdio_list_node_data (node);
+
+ if (IN (lsn, _map->start_lsn, (_map->start_lsn + _map->sec_count - 1))) {
+ int ret;
+ long int img_offset = _map->img_offset;
+
+ img_offset += (lsn - _map->start_lsn) * CDIO_CD_FRAMESIZE_RAW;
+
+ ret = cdio_stream_seek (_obj->gen.data_source, img_offset,
+ SEEK_SET);
+ if (ret!=0) return ret;
+ ret = cdio_stream_read (_obj->gen.data_source, data,
+ CDIO_CD_FRAMESIZE_RAW, nblocks);
+ if (ret==0) return ret;
+ break;
+ }
+ }
+
+ if (!node) cdio_warn ("reading into pre gap (lsn %lu)",
+ (long unsigned int) lsn);
+
+ return 0;
+}
+
+static int
+_cdio_read_mode2_sector (void *env, void *data, lsn_t lsn,
+ bool mode2_form2)
+{
+ _img_private_t *_obj = env;
+ char buf[CDIO_CD_FRAMESIZE_RAW] = { 0, };
+ int blocksize = _obj->sector_2336
+ ? M2RAW_SECTOR_SIZE : CDIO_CD_FRAMESIZE_RAW;
+
+ CdioListNode *node;
+
+ if (lsn >= _obj->size)
+ {
+ cdio_warn ("trying to read beyond image size (%lu >= %lu)",
+ (long unsigned int) lsn, (long unsigned int) _obj->size);
+ return -1;
+ }
+
+ _CDIO_LIST_FOREACH (node, _obj->mapping) {
+ _mapping_t *_map = _cdio_list_node_data (node);
+
+ if (IN (lsn, _map->start_lsn, (_map->start_lsn + _map->sec_count - 1))) {
+ int ret;
+ long int img_offset = _map->img_offset;
+
+ img_offset += (lsn - _map->start_lsn) * blocksize;
+
+ ret = cdio_stream_seek (_obj->gen.data_source, img_offset,
+ SEEK_SET);
+ if (ret!=0) return ret;
+ ret = cdio_stream_read (_obj->gen.data_source, _obj->sector_2336
+ ? (buf + CDIO_CD_SYNC_SIZE + CDIO_CD_HEADER_SIZE)
+ : buf,
+ blocksize, 1);
+ if (ret==0) return ret;
+ break;
+ }
+ }
+
+ if (!node)
+ cdio_warn ("reading into pre gap (lsn %lu)", (long unsigned int) lsn);
+
+ if (mode2_form2)
+ memcpy (data, buf + CDIO_CD_SYNC_SIZE + CDIO_CD_HEADER_SIZE,
+ M2RAW_SECTOR_SIZE);
+ else
+ memcpy (data, buf + CDIO_CD_XA_SYNC_HEADER, CDIO_CD_FRAMESIZE);
+
+ return 0;
+}
+
+/*!
+ Reads nblocks of mode2 sectors from cd device into data starting
+ from lsn.
+ Returns 0 if no error.
+ */
+static int
+_cdio_read_mode2_sectors (void *env, void *data, uint32_t lsn,
+ bool mode2_form2, unsigned nblocks)
+{
+ _img_private_t *_obj = env;
+ int i;
+ int retval;
+
+ for (i = 0; i < nblocks; i++) {
+ if (mode2_form2) {
+ if ( (retval = _cdio_read_mode2_sector (_obj,
+ ((char *)data) + (M2RAW_SECTOR_SIZE * i),
+ lsn + i, true)) )
+ return retval;
+ } else {
+ char buf[M2RAW_SECTOR_SIZE] = { 0, };
+ if ( (retval = _cdio_read_mode2_sector (_obj, buf, lsn + i, true)) )
+ return retval;
+
+ memcpy (((char *)data) + (CDIO_CD_FRAMESIZE * i),
+ buf + CDIO_CD_SUBHEADER_SIZE, CDIO_CD_FRAMESIZE);
+ }
+ }
+ return 0;
+}
+
+/*!
+ Set the device to use in I/O operations.
+*/
+static int
+_cdio_set_arg (void *env, const char key[], const char value[])
+{
+ _img_private_t *_obj = env;
+
+ if (!strcmp (key, "source"))
+ {
+ free (_obj->gen.source_name);
+
+ if (!value)
+ return -2;
+
+ _obj->gen.source_name = strdup (value);
+ }
+ else
+ return -1;
+
+ return 0;
+}
+
+/*!
+ Return the value associated with the key "arg".
+*/
+static const char *
+_cdio_get_arg (void *env, const char key[])
+{
+ _img_private_t *_obj = env;
+
+ if (!strcmp (key, "source")) {
+ return _obj->gen.source_name;
+ }
+ return NULL;
+}
+
+/*!
+ Return an array of strings giving possible NRG disk images.
+ */
+char **
+cdio_get_devices_nrg (void)
+{
+ char **drives = NULL;
+ unsigned int num_files=0;
+#ifdef HAVE_GLOB_H
+ unsigned int i;
+ glob_t globbuf;
+ globbuf.gl_offs = 0;
+ glob("*.nrg", GLOB_DOOFFS, NULL, &globbuf);
+ for (i=0; i<globbuf.gl_pathc; i++) {
+ cdio_add_device_list(&drives, globbuf.gl_pathv[i], &num_files);
+ }
+ globfree(&globbuf);
+#else
+ cdio_add_device_list(&drives, DEFAULT_CDIO_DEVICE, &num_files);
+#endif /*HAVE_GLOB_H*/
+ cdio_add_device_list(&drives, NULL, &num_files);
+ return drives;
+}
+
+/*!
+ Return a string containing the default CD device.
+ */
+char *
+cdio_get_default_device_nrg(void)
+{
+ char **drives = cdio_get_devices_nrg();
+ char *drive = (drives[0] == NULL) ? NULL : strdup(drives[0]);
+ cdio_free_device_list(drives);
+ return drive;
+}
+
+/*!
+ Return the number of of the first track.
+ CDIO_INVALID_TRACK is returned on error.
+*/
+static track_t
+_cdio_get_first_track_num(void *env)
+{
+ _img_private_t *_obj = env;
+
+ return _obj->first_track_num;
+}
+
+/*!
+ Return the number of tracks. We fake it an just say there's
+ one big track.
+*/
+static track_t
+_cdio_get_num_tracks(void *env)
+{
+ _img_private_t *_obj = env;
+
+ return _obj->total_tracks;
+}
+
+/*!
+ Return the number of tracks in the current medium.
+ CDIO_INVALID_TRACK is returned on error.
+*/
+static track_format_t
+_cdio_get_track_format(void *env, track_t track_num)
+{
+ _img_private_t *_obj = env;
+
+ if (track_num > _obj->total_tracks || track_num == 0)
+ return TRACK_FORMAT_ERROR;
+
+ return _obj->tocent[track_num-1].track_format;
+}
+
+/*!
+ Return true if we have XA data (green, mode2 form1) or
+ XA data (green, mode2 form2). That is track begins:
+ sync - header - subheader
+ 12 4 - 8
+
+ FIXME: there's gotta be a better design for this and get_track_format?
+*/
+static bool
+_cdio_get_track_green(void *env, track_t track_num)
+{
+ _img_private_t *_obj = env;
+
+ if (track_num > _obj->total_tracks || track_num == 0)
+ return false;
+
+ return _obj->tocent[track_num-1].track_green;
+}
+
+/*!
+ Return the starting MSF (minutes/secs/frames) for the track number
+ track_num in obj. Tracks numbers start at 1.
+ The "leadout" track is specified either by
+ using track_num LEADOUT_TRACK or the total tracks+1.
+*/
+static bool
+_cdio_get_track_msf(void *env, track_t track_num, msf_t *msf)
+{
+ _img_private_t *_obj = env;
+
+ if (NULL == msf) return 1;
+
+ if (track_num == CDIO_CDROM_LEADOUT_TRACK) track_num = _obj->total_tracks+1;
+
+ if (track_num <= _obj->total_tracks+1 && track_num != 0) {
+ *msf = _obj->tocent[track_num-1].start_msf;
+ return true;
+ } else
+ return false;
+}
+
+CdIo *
+cdio_open_nrg (const char *source_name)
+{
+ CdIo *ret;
+ _img_private_t *_data;
+
+ cdio_funcs _funcs = {
+ .eject_media = cdio_generic_bogus_eject_media,
+ .free = cdio_generic_stream_free,
+ .get_arg = _cdio_get_arg,
+ .get_devices = cdio_get_devices_nrg,
+ .get_default_device = cdio_get_default_device_nrg,
+ .get_first_track_num= _cdio_get_first_track_num,
+ .get_num_tracks = _cdio_get_num_tracks,
+ .get_track_format = _cdio_get_track_format,
+ .get_track_green = _cdio_get_track_green,
+ .get_track_lba = NULL, /* Will use generic routine via msf */
+ .get_track_msf = _cdio_get_track_msf,
+ .lseek = _cdio_lseek,
+ .read = _cdio_read,
+ .read_audio_sectors = _cdio_read_audio_sectors,
+ .read_mode2_sector = _cdio_read_mode2_sector,
+ .read_mode2_sectors = _cdio_read_mode2_sectors,
+ .set_arg = _cdio_set_arg,
+ .stat_size = _cdio_stat_size,
+ };
+
+ _data = _cdio_malloc (sizeof (_img_private_t));
+ _data->gen.init = false;
+
+
+ _data->total_tracks = 0;
+ _data->first_track_num= 1;
+ _data->sector_2336 = false; /* FIXME: remove sector_2336 */
+ _data->is_cues = false; /* FIXME: remove is_cues. */
+
+
+ _cdio_set_arg(_data, "source", (NULL == source_name)
+ ? DEFAULT_CDIO_DEVICE: source_name);
+
+ ret = cdio_new (_data, &_funcs);
+ if (ret == NULL) return NULL;
+
+ if (_cdio_init(_data))
+ return ret;
+ else {
+ cdio_generic_stream_free (_data);
+ return NULL;
+ }
+
+}
+
+bool
+cdio_have_nrg (void)
+{
+ return true;
+}
diff --git a/src/input/vcd/libcdio/_cdio_osx.c b/src/input/vcd/libcdio/_cdio_osx.c
new file mode 100644
index 000000000..b02151f30
--- /dev/null
+++ b/src/input/vcd/libcdio/_cdio_osx.c
@@ -0,0 +1,864 @@
+/*
+ $Id: _cdio_osx.c,v 1.1 2003/10/13 11:47:11 f1rmb Exp $
+
+ Copyright (C) 2003 Rocky Bernstein <rocky@panix.com> from vcdimager code
+ Copyright (C) 2001 Herbert Valerio Riedel <hvr@gnu.org>
+ and VideoLAN code Copyright (C) 1998-2001 VideoLAN
+ Authors: Johan Bilien <jobi@via.ecp.fr>
+ Gildas Bazin <gbazin@netcourrier.com>
+ Jon Lech Johansen <jon-vl@nanocrew.net>
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+*/
+
+/* This file contains OSX-specific code and implements low-level
+ control of the CD drive.
+*/
+
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
+
+static const char _rcsid[] = "$Id: _cdio_osx.c,v 1.1 2003/10/13 11:47:11 f1rmb Exp $";
+
+#include <cdio/sector.h>
+#include <cdio/util.h>
+#include "cdio_assert.h"
+#include "cdio_private.h"
+
+/* Is this the right default? */
+#define DEFAULT_CDIO_DEVICE "/dev/rdisk2"
+
+#include <string.h>
+
+#ifdef HAVE_DARWIN_CDROM
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <errno.h>
+#include <unistd.h>
+#include <fcntl.h>
+
+#include <sys/stat.h>
+#include <sys/types.h>
+#include <sys/ioctl.h>
+
+#include <CoreFoundation/CFBase.h>
+#include <IOKit/IOKitLib.h>
+#include <IOKit/storage/IOCDTypes.h>
+#include <IOKit/storage/IOCDMedia.h>
+#include <IOKit/storage/IOCDMediaBSDClient.h>
+
+#define TOTAL_TRACKS (_obj->num_tracks)
+
+typedef struct {
+ /* Things common to all drivers like this.
+ This must be first. */
+ generic_img_private_t gen;
+
+ enum {
+ _AM_NONE,
+ _AM_OSX,
+ } access_mode;
+
+ /* Track information */
+ bool toc_init; /* if true, info below is valid. */
+ CDTOC *pTOC;
+ int i_descriptors;
+ track_t num_tracks;
+ lsn_t *pp_lba;
+
+} _img_private_t;
+
+static void
+_cdio_osx_free (void *env) {
+ _img_private_t *_obj = env;
+ if (NULL == _obj) return;
+ cdio_generic_free(_obj);
+ if (NULL != _obj->pp_lba) free((void *) _obj->pp_lba);
+ if (NULL != _obj->pTOC) free((void *) _obj->pTOC);
+}
+
+/****************************************************************************
+ cdio_getNumberOfTracks: get number of tracks in TOC
+ This is an internal routine and is called once per CD open.
+ ****************************************************************************/
+static track_t
+_cdio_getNumberOfTracks( CDTOC *pTOC, int i_descriptors )
+{
+ track_t track = CDIO_INVALID_TRACK;
+ int i;
+ int i_tracks = 0;
+ CDTOCDescriptor *pTrackDescriptors;
+
+ pTrackDescriptors = pTOC->descriptors;
+
+ for( i = i_descriptors; i >= 0; i-- )
+ {
+ track = pTrackDescriptors[i].point;
+
+ if( track > CDIO_CD_MAX_TRACKS || track < CDIO_CD_MIN_TRACK_NO )
+ continue;
+
+ i_tracks++;
+ }
+
+ return( i_tracks );
+}
+
+/*!
+ Reads nblocks of mode2 form2 sectors from cd device into data starting
+ from lsn.
+ Returns 0 if no error.
+ */
+static int
+_cdio_read_mode2_form2_sectors (int device_handle, void *data, lsn_t lsn,
+ bool mode2_form2, unsigned int nblocks)
+{
+ dk_cd_read_t cd_read;
+
+ memset( &cd_read, 0, sizeof(cd_read) );
+
+ cd_read.offset = lsn * CDIO_CD_FRAMESIZE_RAW;
+ cd_read.sectorArea = kCDSectorAreaSync | kCDSectorAreaHeader |
+ kCDSectorAreaSubHeader | kCDSectorAreaUser |
+ kCDSectorAreaAuxiliary;
+ cd_read.sectorType = kCDSectorTypeUnknown;
+
+ cd_read.buffer = data;
+ cd_read.bufferLength = CDIO_CD_FRAMESIZE_RAW * nblocks;
+
+ if( ioctl( device_handle, DKIOCCDREAD, &cd_read ) == -1 )
+ {
+ cdio_error( "could not read block %d", lsn );
+ return -1;
+ }
+ return 0;
+}
+
+
+/*!
+ Reads nblocks of mode2 sectors from cd device into data starting
+ from lsn.
+ Returns 0 if no error.
+ */
+static int
+_cdio_read_mode2_sectors (void *env, void *data, lsn_t lsn,
+ bool mode2_form2, unsigned int nblocks)
+{
+ _img_private_t *_obj = env;
+ int i;
+ int retval;
+
+ if (mode2_form2) {
+ return _cdio_read_mode2_form2_sectors(_obj->gen.fd, data, lsn,
+ mode2_form2, nblocks);
+ }
+
+ for (i = 0; i < nblocks; i++) {
+ char buf[M2RAW_SECTOR_SIZE] = { 0, };
+ retval = _cdio_read_mode2_form2_sectors (_obj->gen.fd, buf, lsn + i,
+ mode2_form2, 1);
+ if ( retval ) return retval;
+
+ memcpy (((char *)data) + (CDIO_CD_FRAMESIZE * i),
+ buf + CDIO_CD_SUBHEADER_SIZE, CDIO_CD_FRAMESIZE);
+ }
+ return 0;
+}
+
+/*!
+ Reads a single audio sector from CD device into data starting from lsn.
+ Returns 0 if no error.
+ */
+static int
+_cdio_read_audio_sectors (void *env, void *data, lsn_t lsn,
+ unsigned int nblocks)
+{
+ return _cdio_read_mode2_sectors(env, data, lsn, true, nblocks);
+}
+
+/*!
+ Reads a single mode2 sector from cd device into data starting
+ from lsn. Returns 0 if no error.
+ */
+static int
+_cdio_read_mode2_sector (void *env, void *data, lsn_t lsn,
+ bool mode2_form2)
+{
+ return _cdio_read_mode2_sectors(env, data, lsn, mode2_form2, 1);
+}
+
+/*!
+ Set the key "arg" to "value" in source device.
+*/
+static int
+_cdio_set_arg (void *env, const char key[], const char value[])
+{
+ _img_private_t *_obj = env;
+
+ if (!strcmp (key, "source"))
+ {
+ if (!value)
+ return -2;
+
+ free (_obj->gen.source_name);
+
+ _obj->gen.source_name = strdup (value);
+ }
+ else if (!strcmp (key, "access-mode"))
+ {
+ if (!strcmp(value, "OSX"))
+ _obj->access_mode = _AM_OSX;
+ else
+ cdio_error ("unknown access type: %s. ignored.", value);
+ }
+ else
+ return -1;
+
+ return 0;
+}
+
+/*!
+ Read and cache the CD's Track Table of Contents and track info.
+ Return false if successful or true if an error.
+*/
+static bool
+_cdio_read_toc (_img_private_t *_obj)
+{
+ mach_port_t port;
+ char *psz_devname;
+ kern_return_t ret;
+ io_iterator_t iterator;
+ io_registry_entry_t service;
+ CFMutableDictionaryRef properties;
+ CFDataRef data;
+
+ _obj->gen.fd = open( _obj->gen.source_name, O_RDONLY | O_NONBLOCK );
+ if (-1 == _obj->gen.fd) {
+ cdio_error("Failed to open %s: %s", _obj->gen.source_name,
+ strerror(errno));
+ return false;
+ }
+
+ /* get the device name */
+ if( ( psz_devname = strrchr( _obj->gen.source_name, '/') ) != NULL )
+ ++psz_devname;
+ else
+ psz_devname = _obj->gen.source_name;
+
+ /* unraw the device name */
+ if( *psz_devname == 'r' )
+ ++psz_devname;
+
+ /* get port for IOKit communication */
+ if( ( ret = IOMasterPort( MACH_PORT_NULL, &port ) ) != KERN_SUCCESS )
+ {
+ cdio_error( "IOMasterPort: 0x%08x", ret );
+ return false;
+ }
+
+ /* get service iterator for the device */
+ if( ( ret = IOServiceGetMatchingServices(
+ port, IOBSDNameMatching( port, 0, psz_devname ),
+ &iterator ) ) != KERN_SUCCESS )
+ {
+ cdio_error( "IOServiceGetMatchingServices: 0x%08x", ret );
+ return false;
+ }
+
+ /* first service */
+ service = IOIteratorNext( iterator );
+ IOObjectRelease( iterator );
+
+ /* search for kIOCDMediaClass */
+ while( service && !IOObjectConformsTo( service, kIOCDMediaClass ) )
+ {
+
+ ret = IORegistryEntryGetParentIterator( service, kIOServicePlane,
+ &iterator );
+ if( ret != KERN_SUCCESS )
+ {
+ cdio_error( "IORegistryEntryGetParentIterator: 0x%08x", ret );
+ IOObjectRelease( service );
+ return false;
+ }
+
+ IOObjectRelease( service );
+ service = IOIteratorNext( iterator );
+ IOObjectRelease( iterator );
+ }
+
+ if( service == 0 )
+ {
+ cdio_error( "search for kIOCDMediaClass came up empty" );
+ return false;
+ }
+
+ /* create a CF dictionary containing the TOC */
+ ret = IORegistryEntryCreateCFProperties( service, &properties,
+ kCFAllocatorDefault, kNilOptions );
+
+ if( ret != KERN_SUCCESS )
+ {
+ cdio_error( "IORegistryEntryCreateCFProperties: 0x%08x", ret );
+ IOObjectRelease( service );
+ return false;
+ }
+
+ /* get the TOC from the dictionary */
+ data = (CFDataRef) CFDictionaryGetValue( properties,
+ CFSTR(kIOCDMediaTOCKey) );
+ if( data != NULL )
+ {
+ CFRange range;
+ CFIndex buf_len;
+
+ buf_len = CFDataGetLength( data ) + 1;
+ range = CFRangeMake( 0, buf_len );
+
+ if( ( _obj->pTOC = (CDTOC *)malloc( buf_len ) ) != NULL ) {
+ CFDataGetBytes( data, range, (u_char *) _obj->pTOC );
+ } else {
+ cdio_error( "Trouble allocating CDROM TOC" );
+ return false;
+ }
+ }
+ else
+ {
+ cdio_error( "CFDictionaryGetValue failed" );
+ }
+
+ CFRelease( properties );
+ IOObjectRelease( service );
+
+ _obj->i_descriptors = CDTOCGetDescriptorCount ( _obj->pTOC );
+ _obj->num_tracks = _cdio_getNumberOfTracks(_obj->pTOC, _obj->i_descriptors);
+
+ /* Read in starting sectors */
+ {
+ int i, i_leadout = -1;
+ CDTOCDescriptor *pTrackDescriptors;
+ track_t track;
+ int i_tracks;
+
+ _obj->pp_lba = malloc( (_obj->num_tracks + 1) * sizeof(int) );
+ if( _obj->pp_lba == NULL )
+ {
+ cdio_error("Out of memory in allocating track starting LSNs" );
+ free( _obj->pTOC );
+ return false;
+ }
+
+ pTrackDescriptors = _obj->pTOC->descriptors;
+
+ for( i_tracks = 0, i = 0; i <= _obj->i_descriptors; i++ )
+ {
+ track = pTrackDescriptors[i].point;
+
+ if( track == 0xA2 )
+ /* Note leadout should be 0xAA, But OSX seems to use 0xA2. */
+ i_leadout = i;
+
+ if( track > CDIO_CD_MAX_TRACKS || track < CDIO_CD_MIN_TRACK_NO )
+ continue;
+
+ _obj->pp_lba[i_tracks++] =
+ cdio_lsn_to_lba(CDConvertMSFToLBA( pTrackDescriptors[i].p ));
+ }
+
+ if( i_leadout == -1 )
+ {
+ cdio_error( "CD leadout not found" );
+ free( _obj->pp_lba );
+ free( (void *) _obj->pTOC );
+ return false;
+ }
+
+ /* set leadout sector */
+ _obj->pp_lba[i_tracks] =
+ cdio_lsn_to_lba(CDConvertMSFToLBA( pTrackDescriptors[i_leadout].p ));
+ }
+
+ _obj->toc_init = true;
+
+ return( true );
+
+}
+
+/*!
+ Return the starting LSN track number
+ track_num in obj. Track numbers start at 1.
+ The "leadout" track is specified either by
+ using track_num LEADOUT_TRACK or the total tracks+1.
+ False is returned if there is no track entry.
+*/
+static lsn_t
+_cdio_get_track_lba(void *env, track_t track_num)
+{
+ _img_private_t *_obj = env;
+
+ if (!_obj->toc_init) _cdio_read_toc (_obj) ;
+
+ if (track_num == CDIO_CDROM_LEADOUT_TRACK) track_num = TOTAL_TRACKS+1;
+
+ if (track_num > TOTAL_TRACKS+1 || track_num == 0) {
+ return CDIO_INVALID_LSN;
+ } else {
+ return _obj->pp_lba[track_num-1];
+ }
+}
+
+/*!
+ Eject media . Return 1 if successful, 0 otherwise.
+
+ The only way to cleanly unmount the disc under MacOS X is to use the
+ 'disktool' command line utility. It uses the non-public Disk
+ Arbitration API, which can not be used by Cocoa or Carbon
+ applications.
+
+ */
+
+static int
+_cdio_eject_media (void *env) {
+
+ _img_private_t *_obj = env;
+
+ FILE *p_eject;
+ char *psz_disk;
+ char sz_cmd[32];
+
+ if( ( psz_disk = (char *)strstr( _obj->gen.source_name, "disk" ) ) != NULL &&
+ strlen( psz_disk ) > 4 )
+ {
+#define EJECT_CMD "/usr/sbin/disktool -e %s 0"
+ snprintf( sz_cmd, sizeof(sz_cmd), EJECT_CMD, psz_disk );
+#undef EJECT_CMD
+
+ if( ( p_eject = popen( sz_cmd, "r" ) ) != NULL )
+ {
+ char psz_result[0x200];
+ int i_ret = fread( psz_result, 1, sizeof(psz_result) - 1, p_eject );
+
+ if( i_ret == 0 && ferror( p_eject ) != 0 )
+ {
+ pclose( p_eject );
+ return 0;
+ }
+
+ pclose( p_eject );
+
+ psz_result[ i_ret ] = 0;
+
+ if( strstr( psz_result, "Disk Ejected" ) != NULL )
+ {
+ return 1;
+ }
+ }
+ }
+
+ return 0;
+}
+
+/*!
+ Return the size of the CD in logical block address (LBA) units.
+ */
+static uint32_t
+_cdio_stat_size (void *env)
+{
+ return _cdio_get_track_lba(env, CDIO_CDROM_LEADOUT_TRACK);
+}
+
+/*!
+ Return the value associated with the key "arg".
+*/
+static const char *
+_cdio_get_arg (void *env, const char key[])
+{
+ _img_private_t *_obj = env;
+
+ if (!strcmp (key, "source")) {
+ return _obj->gen.source_name;
+ } else if (!strcmp (key, "access-mode")) {
+ switch (_obj->access_mode) {
+ case _AM_OSX:
+ return "OS X";
+ case _AM_NONE:
+ return "no access method";
+ }
+ }
+ return NULL;
+}
+
+/*!
+ Return the number of the first track.
+ CDIO_INVALID_TRACK is returned on error.
+*/
+static track_t
+_cdio_get_first_track_num(void *env)
+{
+ _img_private_t *_obj = env;
+
+ if (!_obj->toc_init) _cdio_read_toc (_obj) ;
+
+ {
+ track_t track = CDIO_INVALID_TRACK;
+ int i;
+ CDTOCDescriptor *pTrackDescriptors;
+
+ pTrackDescriptors = _obj->pTOC->descriptors;
+
+ for( i = 0; i < _obj->i_descriptors; i++ )
+ {
+ track = pTrackDescriptors[i].point;
+
+ if( track > CDIO_CD_MAX_TRACKS || track < CDIO_CD_MIN_TRACK_NO )
+ continue;
+ return ( track );
+ }
+ }
+
+ return CDIO_INVALID_TRACK;
+}
+
+/*!
+ Return the media catalog number MCN.
+ */
+static char *
+_cdio_get_mcn (void *env) {
+ _img_private_t *_obj = env;
+ dk_cd_read_mcn_t cd_read;
+
+ memset( &cd_read, 0, sizeof(cd_read) );
+
+ if( ioctl( _obj->gen.fd, DKIOCCDREADMCN, &cd_read ) < 0 )
+ {
+ cdio_error( "could not read MCN, %s", strerror(errno) );
+ return -1;
+ }
+ return strdup((char*)cd_read.mcn);
+}
+
+/*!
+ Return the number of tracks in the current medium.
+ CDIO_INVALID_TRACK is returned on error.
+ This is the externally called interface.
+*/
+static track_t
+_cdio_get_num_tracks(void *env)
+{
+ _img_private_t *_obj = env;
+
+ if (!_obj->toc_init) _cdio_read_toc (_obj) ;
+ return( _obj->num_tracks );
+}
+
+/*!
+ Get format of track.
+*/
+static track_format_t
+_cdio_get_track_format(void *env, track_t track_num)
+{
+ _img_private_t *_obj = env;
+
+ if (!_obj->toc_init) _cdio_read_toc (_obj) ;
+
+ if (track_num > TOTAL_TRACKS || track_num == 0)
+ return TRACK_FORMAT_ERROR;
+
+#if 0
+ if (_obj->tocent[track_num-1].entry.control & CDROM_DATA_TRACK) {
+ if (_obj->tocent[track_num-1].cdte_format == 0x10)
+ return TRACK_FORMAT_CDI;
+ else if (_obj->tocent[track_num-1].cdte_format == 0x20)
+ return TRACK_FORMAT_XA;
+ else
+ return TRACK_FORMAT_DATA;
+ } else
+ return TRACK_FORMAT_AUDIO;
+#else
+ /* FIXME! Figure out how to do. */
+ return TRACK_FORMAT_DATA;
+#endif
+
+}
+
+/*!
+ Return true if we have XA data (green, mode2 form1) or
+ XA data (green, mode2 form2). That is track begins:
+ sync - header - subheader
+ 12 4 - 8
+
+ FIXME: there's gotta be a better design for this and get_track_format?
+*/
+static bool
+_cdio_get_track_green(void *env, track_t track_num)
+{
+
+#if 0
+ if (!_obj->toc_init) _cdio_read_toc (_obj) ;
+
+ if (track_num == CDIO_LEADOUT_TRACK) track_num = TOTAL_TRACKS+1;
+
+ if (track_num > TOTAL_TRACKS+1 || track_num == 0)
+ return false;
+
+ /* FIXME: Dunno if this is the right way, but it's what
+ I was using in cdinfo for a while.
+ */
+ return ((_obj->tocent[track_num-1].cdte_ctrl & 2) != 0);
+#else
+ /* FIXME! Figure out how to do. */
+ return true;
+#endif
+}
+
+#endif /* HAVE_DARWIN_CDROM */
+
+/*!
+ Return a string containing the default CD device if none is specified.
+ */
+char **
+cdio_get_devices_osx(void)
+{
+#ifndef HAVE_DARWIN_CDROM
+ return NULL;
+#else
+ io_object_t next_media;
+ mach_port_t master_port;
+ kern_return_t kern_result;
+ io_iterator_t media_iterator;
+ CFMutableDictionaryRef classes_to_match;
+ char **drives = NULL;
+ unsigned int num_drives=0;
+
+ kern_result = IOMasterPort( MACH_PORT_NULL, &master_port );
+ if( kern_result != KERN_SUCCESS )
+ {
+ return( nil );
+ }
+
+ classes_to_match = IOServiceMatching( kIOCDMediaClass );
+ if( classes_to_match == NULL )
+ {
+ return( nil );
+ }
+
+ CFDictionarySetValue( classes_to_match, CFSTR(kIOMediaEjectable),
+ kCFBooleanTrue );
+
+ kern_result = IOServiceGetMatchingServices( master_port,
+ classes_to_match,
+ &media_iterator );
+ if( kern_result != KERN_SUCCESS )
+ {
+ return( nil );
+ }
+
+ next_media = IOIteratorNext( media_iterator );
+ if( next_media != 0 )
+ {
+ char psz_buf[0x32];
+ size_t dev_path_length;
+ CFTypeRef str_bsd_path;
+
+ do
+ {
+ str_bsd_path = IORegistryEntryCreateCFProperty( next_media,
+ CFSTR( kIOBSDName ),
+ kCFAllocatorDefault,
+ 0 );
+ if( str_bsd_path == NULL )
+ {
+ IOObjectRelease( next_media );
+ continue;
+ }
+
+ snprintf( psz_buf, sizeof(psz_buf), "%s%c", _PATH_DEV, 'r' );
+ dev_path_length = strlen( psz_buf );
+
+ if( CFStringGetCString( str_bsd_path,
+ (char*)&psz_buf + dev_path_length,
+ sizeof(psz_buf) - dev_path_length,
+ kCFStringEncodingASCII ) )
+ {
+ CFRelease( str_bsd_path );
+ IOObjectRelease( next_media );
+ IOObjectRelease( media_iterator );
+ cdio_add_device_list(&drives, psz_buf, &num_drives);
+ }
+
+ CFRelease( str_bsd_path );
+ IOObjectRelease( next_media );
+
+ } while( ( next_media = IOIteratorNext( media_iterator ) ) != 0 );
+ }
+ IOObjectRelease( media_iterator );
+ cdio_add_device_list(&drives, NULL, &num_drives);
+ return drives;
+#endif /* HAVE_DARWIN_CDROM */
+}
+
+/*!
+ Return a string containing the default CD device if none is specified.
+ */
+char *
+cdio_get_default_device_osx(void)
+{
+#ifndef HAVE_DARWIN_CDROM
+ return NULL;
+#else
+ io_object_t next_media;
+ mach_port_t master_port;
+ kern_return_t kern_result;
+ io_iterator_t media_iterator;
+ CFMutableDictionaryRef classes_to_match;
+
+ kern_result = IOMasterPort( MACH_PORT_NULL, &master_port );
+ if( kern_result != KERN_SUCCESS )
+ {
+ return( nil );
+ }
+
+ classes_to_match = IOServiceMatching( kIOCDMediaClass );
+ if( classes_to_match == NULL )
+ {
+ return( nil );
+ }
+
+ CFDictionarySetValue( classes_to_match, CFSTR(kIOMediaEjectable),
+ kCFBooleanTrue );
+
+ kern_result = IOServiceGetMatchingServices( master_port,
+ classes_to_match,
+ &media_iterator );
+ if( kern_result != KERN_SUCCESS )
+ {
+ return( nil );
+ }
+
+ next_media = IOIteratorNext( media_iterator );
+ if( next_media != 0 )
+ {
+ char psz_buf[0x32];
+ size_t dev_path_length;
+ CFTypeRef str_bsd_path;
+
+ do
+ {
+ str_bsd_path = IORegistryEntryCreateCFProperty( next_media,
+ CFSTR( kIOBSDName ),
+ kCFAllocatorDefault,
+ 0 );
+ if( str_bsd_path == NULL )
+ {
+ IOObjectRelease( next_media );
+ continue;
+ }
+
+ snprintf( psz_buf, sizeof(psz_buf), "%s%c", _PATH_DEV, 'r' );
+ dev_path_length = strlen( psz_buf );
+
+ if( CFStringGetCString( str_bsd_path,
+ (char*)&psz_buf + dev_path_length,
+ sizeof(psz_buf) - dev_path_length,
+ kCFStringEncodingASCII ) )
+ {
+ CFRelease( str_bsd_path );
+ IOObjectRelease( next_media );
+ IOObjectRelease( media_iterator );
+ return strdup( psz_buf );
+ }
+
+ CFRelease( str_bsd_path );
+ IOObjectRelease( next_media );
+
+ } while( ( next_media = IOIteratorNext( media_iterator ) ) != 0 );
+ }
+ IOObjectRelease( media_iterator );
+ return NULL;
+#endif /* HAVE_DARWIN_CDROM */
+}
+
+/*!
+ Initialization routine. This is the only thing that doesn't
+ get called via a function pointer. In fact *we* are the
+ ones to set that up.
+ */
+CdIo *
+cdio_open_osx (const char *source_name)
+{
+
+#ifdef HAVE_DARWIN_CDROM
+ CdIo *ret;
+ _img_private_t *_data;
+
+ cdio_funcs _funcs = {
+ .eject_media = _cdio_eject_media,
+ .free = _cdio_osx_free,
+ .get_arg = _cdio_get_arg,
+ .get_default_device = cdio_get_default_device_osx,
+ .get_devices = cdio_get_devices_osx,
+ .get_first_track_num= _cdio_get_first_track_num,
+ .get_mcn = _cdio_get_mcn,
+ .get_num_tracks = _cdio_get_num_tracks,
+ .get_track_format = _cdio_get_track_format,
+ .get_track_green = _cdio_get_track_green,
+ .get_track_lba = _cdio_get_track_lba,
+ .get_track_msf = NULL,
+ .lseek = cdio_generic_lseek,
+ .read = cdio_generic_read,
+ .read_audio_sectors = _cdio_read_audio_sectors,
+ .read_mode2_sector = _cdio_read_mode2_sector,
+ .read_mode2_sectors = _cdio_read_mode2_sectors,
+ .set_arg = _cdio_set_arg,
+ .stat_size = _cdio_stat_size
+ };
+
+ _data = _cdio_malloc (sizeof (_img_private_t));
+ _data->access_mode = _AM_OSX;
+ _data->gen.init = false;
+ _data->gen.fd = -1;
+
+ _cdio_set_arg(_data, "source", (NULL == source_name)
+ ? DEFAULT_CDIO_DEVICE: source_name);
+
+ ret = cdio_new (_data, &_funcs);
+ if (ret == NULL) return NULL;
+
+ if (cdio_generic_init(_data))
+ return ret;
+ else {
+ cdio_generic_free (_data);
+ return NULL;
+ }
+
+#else
+ return NULL;
+#endif /* HAVE_DARWIN_CDROM */
+
+}
+
+bool
+cdio_have_osx (void)
+{
+#ifdef HAVE_DARWIN_CDROM
+ return true;
+#else
+ return false;
+#endif /* HAVE_DARWIN_CDROM */
+}
+
+
diff --git a/src/input/vcd/libcdio/_cdio_stdio.c b/src/input/vcd/libcdio/_cdio_stdio.c
new file mode 100644
index 000000000..b8d8daa67
--- /dev/null
+++ b/src/input/vcd/libcdio/_cdio_stdio.c
@@ -0,0 +1,212 @@
+/*
+ $Id: _cdio_stdio.c,v 1.1 2003/10/13 11:47:11 f1rmb Exp $
+
+ Copyright (C) 2000 Herbert Valerio Riedel <hvr@gnu.org>
+ Copyright (C) 2003 Rocky Bernstein <rocky@panix.com>
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+*/
+
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include <sys/stat.h>
+#include <errno.h>
+
+#include <cdio/logging.h>
+#include <cdio/util.h>
+#include "_cdio_stream.h"
+#include "_cdio_stdio.h"
+
+static const char _rcsid[] = "$Id: _cdio_stdio.c,v 1.1 2003/10/13 11:47:11 f1rmb Exp $";
+
+#define CDIO_STDIO_BUFSIZE (128*1024)
+
+typedef struct {
+ char *pathname;
+ FILE *fd;
+ char *fd_buf;
+ off_t st_size; /* used only for source */
+} _UserData;
+
+static int
+_stdio_open (void *user_data)
+{
+ _UserData *const ud = user_data;
+
+ if ((ud->fd = fopen (ud->pathname, "rb")))
+ {
+ ud->fd_buf = _cdio_malloc (CDIO_STDIO_BUFSIZE);
+ setvbuf (ud->fd, ud->fd_buf, _IOFBF, CDIO_STDIO_BUFSIZE);
+ }
+
+ return (ud->fd == NULL);
+}
+
+static int
+_stdio_close(void *user_data)
+{
+ _UserData *const ud = user_data;
+
+ if (fclose (ud->fd))
+ cdio_error ("fclose (): %s", strerror (errno));
+
+ ud->fd = NULL;
+
+ free (ud->fd_buf);
+ ud->fd_buf = NULL;
+
+ return 0;
+}
+
+static void
+_stdio_free(void *user_data)
+{
+ _UserData *const ud = user_data;
+
+ if (ud->pathname)
+ free(ud->pathname);
+
+ if (ud->fd) /* should be NULL anyway... */
+ _stdio_close(user_data);
+
+ free(ud);
+}
+
+/*!
+ Like fseek(3) and in fact may be the same.
+
+ This function sets the file position indicator for the stream
+ pointed to by stream. The new position, measured in bytes, is obtained
+ by adding offset bytes to the position specified by whence. If whence
+ is set to SEEK_SET, SEEK_CUR, or SEEK_END, the offset is relative to
+ the start of the file, the current position indicator, or end-of-file,
+ respectively. A successful call to the fseek function clears the end-
+ of-file indicator for the stream and undoes any effects of the
+ ungetc(3) function on the same stream.
+
+ RETURN VALUE
+ Upon successful completion, return 0,
+ Otherwise, -1 is returned and the global variable errno is set to indi-
+ cate the error.
+*/
+static long
+_stdio_seek(void *user_data, long offset, int whence)
+{
+ _UserData *const ud = user_data;
+
+ if ( (offset=fseek (ud->fd, offset, whence)) ) {
+ cdio_error ("fseek (): %s", strerror (errno));
+ }
+
+ return offset;
+}
+
+static long int
+_stdio_stat(void *user_data)
+{
+ const _UserData *const ud = user_data;
+
+ return ud->st_size;
+}
+
+/*!
+ Like fread(3) and in fact is about the same.
+
+ DESCRIPTION:
+ The function fread reads nmemb elements of data, each size bytes long,
+ from the stream pointed to by stream, storing them at the location
+ given by ptr.
+
+ RETURN VALUE:
+ return the number of items successfully read or written (i.e.,
+ not the number of characters). If an error occurs, or the
+ end-of-file is reached, the return value is a short item count
+ (or zero).
+
+ We do not distinguish between end-of-file and error, and callers
+ must use feof(3) and ferror(3) to determine which occurred.
+ */
+static long
+_stdio_read(void *user_data, void *buf, long count)
+{
+ _UserData *const ud = user_data;
+ long read;
+
+ read = fread(buf, 1, count, ud->fd);
+
+ if (read != count)
+ { /* fixme -- ferror/feof */
+ if (feof (ud->fd))
+ {
+ cdio_debug ("fread (): EOF encountered");
+ clearerr (ud->fd);
+ }
+ else if (ferror (ud->fd))
+ {
+ cdio_error ("fread (): %s", strerror (errno));
+ clearerr (ud->fd);
+ }
+ else
+ cdio_debug ("fread (): short read and no EOF?!?");
+ }
+
+ return read;
+}
+
+CdioDataSource*
+cdio_stdio_new(const char pathname[])
+{
+ CdioDataSource *new_obj = NULL;
+ cdio_stream_io_functions funcs = { 0, };
+ _UserData *ud = NULL;
+ struct stat statbuf;
+
+ if (stat (pathname, &statbuf) == -1)
+ {
+ cdio_error ("could not stat() file `%s': %s", pathname, strerror (errno));
+ return NULL;
+ }
+
+ ud = _cdio_malloc (sizeof (_UserData));
+
+ ud->pathname = strdup(pathname);
+ ud->st_size = statbuf.st_size; /* let's hope it doesn't change... */
+
+ funcs.open = _stdio_open;
+ funcs.seek = _stdio_seek;
+ funcs.stat = _stdio_stat;
+ funcs.read = _stdio_read;
+ funcs.close = _stdio_close;
+ funcs.free = _stdio_free;
+
+ new_obj = cdio_stream_new(ud, &funcs);
+
+ return new_obj;
+}
+
+
+/*
+ * Local variables:
+ * c-file-style: "gnu"
+ * tab-width: 8
+ * indent-tabs-mode: nil
+ * End:
+ */
diff --git a/src/input/vcd/libcdio/_cdio_stdio.h b/src/input/vcd/libcdio/_cdio_stdio.h
new file mode 100644
index 000000000..013dff58b
--- /dev/null
+++ b/src/input/vcd/libcdio/_cdio_stdio.h
@@ -0,0 +1,40 @@
+/*
+ $Id: _cdio_stdio.h,v 1.1 2003/10/13 11:47:11 f1rmb Exp $
+
+ Copyright (C) 2000 Herbert Valerio Riedel <hvr@gnu.org>
+ Copyright (C) 2003 Rocky Bernstein <rocky@panix.com>
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+*/
+
+
+#ifndef __CDIO_STDIO_H__
+#define __CDIO_STDIO_H__
+
+#include "_cdio_stream.h"
+
+CdioDataSource*
+cdio_stdio_new(const char pathname[]);
+
+#endif /* __CDIO_STREAM_STDIO_H__ */
+
+
+/*
+ * Local variables:
+ * c-file-style: "gnu"
+ * tab-width: 8
+ * indent-tabs-mode: nil
+ * End:
+ */
diff --git a/src/input/vcd/libcdio/_cdio_stream.c b/src/input/vcd/libcdio/_cdio_stream.c
new file mode 100644
index 000000000..ee2f2b086
--- /dev/null
+++ b/src/input/vcd/libcdio/_cdio_stream.c
@@ -0,0 +1,198 @@
+/*
+ $Id: _cdio_stream.c,v 1.1 2003/10/13 11:47:11 f1rmb Exp $
+
+ Copyright (C) 2000 Herbert Valerio Riedel <hvr@gnu.org>
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+*/
+
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <stdarg.h>
+#include "cdio_assert.h"
+
+/* #define STREAM_DEBUG */
+
+#include <cdio/logging.h>
+#include <cdio/util.h>
+#include "_cdio_stream.h"
+
+static const char _rcsid[] = "$Id: _cdio_stream.c,v 1.1 2003/10/13 11:47:11 f1rmb Exp $";
+
+/*
+ * DataSource implementations
+ */
+
+struct _CdioDataSource {
+ void* user_data;
+ cdio_stream_io_functions op;
+ int is_open;
+ long position;
+};
+
+/*
+ Open if not already open.
+ Return false if we hit an error. Errno should be set for that error.
+*/
+static bool
+_cdio_stream_open_if_necessary(CdioDataSource *obj)
+{
+ cdio_assert (obj != NULL);
+
+ if (!obj->is_open) {
+ if (obj->op.open(obj->user_data)) {
+ cdio_warn ("could not open input stream...");
+ return false;
+ } else {
+ cdio_debug ("opened source...");
+ obj->is_open = 1;
+ obj->position = 0;
+ }
+ }
+ return true;
+}
+
+/*!
+ Like 3 fseek and in fact may be the same.
+
+ This function sets the file position indicator for the stream
+ pointed to by stream. The new position, measured in bytes, is obtained
+ by adding offset bytes to the position specified by whence. If whence
+ is set to SEEK_SET, SEEK_CUR, or SEEK_END, the offset is relative to
+ the start of the file, the current position indicator, or end-of-file,
+ respectively. A successful call to the fseek function clears the end-
+ of-file indicator for the stream and undoes any effects of the
+ ungetc(3) function on the same stream.
+
+ RETURN VALUE
+ Upon successful completion, return 0,
+ Otherwise, -1 is returned and the global variable errno is set to indi-
+ cate the error.
+*/
+long
+cdio_stream_seek(CdioDataSource* obj, long offset, int whence)
+{
+ cdio_assert (obj != NULL);
+
+ if (!_cdio_stream_open_if_necessary(obj))
+ /* errno is set by _cdio_stream_open_if necessary. */
+ return -1;
+
+ if (obj->position != offset) {
+#ifdef STREAM_DEBUG
+ cdio_warn("had to reposition DataSource from %ld to %ld!", obj->position, offset);
+#endif
+ obj->position = offset;
+ return obj->op.seek(obj->user_data, offset, whence);
+ }
+
+ return 0;
+}
+
+CdioDataSource*
+cdio_stream_new(void *user_data, const cdio_stream_io_functions *funcs)
+{
+ CdioDataSource *new_obj;
+
+ new_obj = _cdio_malloc (sizeof (CdioDataSource));
+
+ new_obj->user_data = user_data;
+ memcpy(&(new_obj->op), funcs, sizeof(cdio_stream_io_functions));
+
+ return new_obj;
+}
+
+/*!
+ Like fread(3) and in fact may be the same.
+
+ DESCRIPTION:
+ The function fread reads nmemb elements of data, each size bytes long,
+ from the stream pointed to by stream, storing them at the location
+ given by ptr.
+
+ RETURN VALUE:
+ return the number of items successfully read or written (i.e.,
+ not the number of characters). If an error occurs, or the
+ end-of-file is reached, the return value is a short item count
+ (or zero).
+
+ We do not distinguish between end-of-file and error, and callers
+ must use feof(3) and ferror(3) to determine which occurred.
+*/
+long
+cdio_stream_read(CdioDataSource* obj, void *ptr, long size, long nmemb)
+{
+ long read_bytes;
+
+ cdio_assert (obj != NULL);
+
+ if (!_cdio_stream_open_if_necessary(obj)) return 0;
+
+ read_bytes = obj->op.read(obj->user_data, ptr, size*nmemb);
+ obj->position += read_bytes;
+
+ return read_bytes;
+}
+
+/*!
+ Return whatever size of stream reports, I guess unit size is bytes.
+ On error return -1;
+ */
+long int
+cdio_stream_stat(CdioDataSource* obj)
+{
+ cdio_assert (obj != NULL);
+
+ if (!_cdio_stream_open_if_necessary(obj)) return -1;
+
+ return obj->op.stat(obj->user_data);
+}
+
+void
+cdio_stream_close(CdioDataSource* obj)
+{
+ cdio_assert (obj != NULL);
+
+ if (obj->is_open) {
+ cdio_debug ("closed source...");
+ obj->op.close(obj->user_data);
+ obj->is_open = 0;
+ obj->position = 0;
+ }
+}
+
+void
+cdio_stream_destroy(CdioDataSource* obj)
+{
+ cdio_assert (obj != NULL);
+
+ cdio_stream_close(obj);
+
+ obj->op.free(obj->user_data);
+}
+
+
+/*
+ * Local variables:
+ * c-file-style: "gnu"
+ * tab-width: 8
+ * indent-tabs-mode: nil
+ * End:
+ */
diff --git a/src/input/vcd/libcdio/_cdio_stream.h b/src/input/vcd/libcdio/_cdio_stream.h
new file mode 100644
index 000000000..562c18bf8
--- /dev/null
+++ b/src/input/vcd/libcdio/_cdio_stream.h
@@ -0,0 +1,124 @@
+/*
+ $Id: _cdio_stream.h,v 1.1 2003/10/13 11:47:11 f1rmb Exp $
+
+ Copyright (C) 2000 Herbert Valerio Riedel <hvr@gnu.org>
+ Copyright (C) 2003 Rocky Bernstein <rocky@panix.com>
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+*/
+
+
+#ifndef __CDIO_STREAM_H__
+#define __CDIO_STREAM_H__
+
+#include <cdio/types.h>
+#include "cdio_private.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif /* __cplusplus */
+
+ /* typedef'ed IO functions prototypes */
+
+ typedef int(*cdio_data_open_t)(void *user_data);
+
+ typedef long(*cdio_data_read_t)(void *user_data, void *buf, long count);
+
+ typedef long(*cdio_data_seek_t)(void *user_data, long offset, int whence);
+
+ typedef long(*cdio_data_stat_t)(void *user_data);
+
+ typedef int(*cdio_data_close_t)(void *user_data);
+
+ typedef void(*cdio_data_free_t)(void *user_data);
+
+
+ /* abstract data source */
+
+ typedef struct {
+ cdio_data_open_t open;
+ cdio_data_seek_t seek;
+ cdio_data_stat_t stat;
+ cdio_data_read_t read;
+ cdio_data_close_t close;
+ cdio_data_free_t free;
+ } cdio_stream_io_functions;
+
+ CdioDataSource*
+ cdio_stream_new(void *user_data, const cdio_stream_io_functions *funcs);
+
+ /*!
+ Like fread(3) and in fact may be the same.
+
+ DESCRIPTION:
+ The function fread reads nmemb elements of data, each size bytes long,
+ from the stream pointed to by stream, storing them at the location
+ given by ptr.
+
+ RETURN VALUE:
+ return the number of items successfully read or written (i.e.,
+ not the number of characters). If an error occurs, or the
+ end-of-file is reached, the return value is a short item count
+ (or zero).
+
+ We do not distinguish between end-of-file and error, and callers
+ must use feof(3) and ferror(3) to determine which occurred.
+ */
+ long
+ cdio_stream_read(CdioDataSource* obj, void *ptr, long size, long nmemb);
+
+ /*!
+ Like fseek(3) and in fact may be the same.
+
+ This function sets the file position indicator for the stream
+ pointed to by stream. The new position, measured in bytes, is obtained
+ by adding offset bytes to the position specified by whence. If whence
+ is set to SEEK_SET, SEEK_CUR, or SEEK_END, the offset is relative to
+ the start of the file, the current position indicator, or end-of-file,
+ respectively. A successful call to the fseek function clears the end-
+ of-file indicator for the stream and undoes any effects of the
+ ungetc(3) function on the same stream.
+
+ RETURN VALUE
+ Upon successful completion, return 0,
+ Otherwise, -1 is returned and the global variable errno is set to indi-
+ cate the error.
+ */
+ long cdio_stream_seek(CdioDataSource* obj, long offset, int whence);
+
+ /*!
+ Return whatever size of stream reports, I guess unit size is bytes.
+ On error return -1;
+ */
+ long cdio_stream_stat(CdioDataSource* obj);
+
+ void cdio_stream_destroy(CdioDataSource* obj);
+
+ void cdio_stream_close(CdioDataSource* obj);
+
+#ifdef __cplusplus
+}
+#endif /* __cplusplus */
+
+#endif /* __CDIO_STREAM_H__ */
+
+
+/*
+ * Local variables:
+ * c-file-style: "gnu"
+ * tab-width: 8
+ * indent-tabs-mode: nil
+ * End:
+ */
diff --git a/src/input/vcd/libcdio/_cdio_sunos.c b/src/input/vcd/libcdio/_cdio_sunos.c
new file mode 100644
index 000000000..e7faf57cd
--- /dev/null
+++ b/src/input/vcd/libcdio/_cdio_sunos.c
@@ -0,0 +1,734 @@
+/*
+ $Id: _cdio_sunos.c,v 1.1 2003/10/13 11:47:11 f1rmb Exp $
+
+ Copyright (C) 2001 Herbert Valerio Riedel <hvr@gnu.org>
+ Copyright (C) 2002,2003 Rocky Bernstein <rocky@panix.com>
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+*/
+
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
+
+#ifdef HAVE_STRING_H
+#include <string.h>
+#endif
+#ifdef HAVE_GLOB_H
+#include <glob.h>
+#endif
+
+
+#include <cdio/logging.h>
+#include <cdio/sector.h>
+#include <cdio/util.h>
+#include "cdio_assert.h"
+#include "cdio_private.h"
+#include "scsi_mmc.h"
+
+#define DEFAULT_CDIO_DEVICE "/vol/dev/aliases/cdrom0"
+
+#ifdef HAVE_SOLARIS_CDROM
+
+static const char _rcsid[] = "$Id: _cdio_sunos.c,v 1.1 2003/10/13 11:47:11 f1rmb Exp $";
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <errno.h>
+#include <unistd.h>
+#include <fcntl.h>
+
+#ifdef HAVE_SYS_CDIO_H
+# include <sys/cdio.h> /* CDIOCALLOW etc... */
+#else
+#error "You need <sys/cdio.h> to have CDROM support"
+#endif
+
+#include <sys/dkio.h>
+#include <sys/scsi/generic/commands.h>
+#include <sys/scsi/impl/uscsi.h>
+
+#include <sys/stat.h>
+#include <sys/types.h>
+#include <sys/ioctl.h>
+
+#define TOTAL_TRACKS (_obj->tochdr.cdth_trk1)
+#define FIRST_TRACK_NUM (_obj->tochdr.cdth_trk0)
+
+/* reader */
+
+typedef struct {
+ /* Things common to all drivers like this.
+ This must be first. */
+ generic_img_private_t gen;
+
+ enum {
+ _AM_NONE,
+ _AM_SUN_CTRL_ATAPI,
+ _AM_SUN_CTRL_SCSI
+#if FINISHED
+ _AM_READ_CD,
+ _AM_READ_10
+#endif
+ } access_mode;
+
+
+ /* Track information */
+ struct cdrom_tochdr tochdr;
+ struct cdrom_tocentry tocent[100]; /* entry info for each track */
+
+} _img_private_t;
+
+/*!
+ Initialize CD device.
+ */
+static bool
+_cdio_init (_img_private_t *_obj)
+{
+
+ struct dk_cinfo cinfo;
+
+ if (!cdio_generic_init(_obj)) return false;
+
+ /*
+ * CDROMCDXA/CDROMREADMODE2 are broken on IDE/ATAPI devices.
+ * Try to send MMC3 SCSI commands via the uscsi interface on
+ * ATAPI devices.
+ */
+ if ( ioctl(_obj->gen.fd, DKIOCINFO, &cinfo) == 0
+ && ((strcmp(cinfo.dki_cname, "ide") == 0)
+ || (strncmp(cinfo.dki_cname, "pci", 3) == 0)) ) {
+ _obj->access_mode = _AM_SUN_CTRL_ATAPI;
+ } else {
+ _obj->access_mode = _AM_SUN_CTRL_SCSI;
+ }
+
+ return true;
+}
+
+static int
+_cdio_mmc_read_sectors (int fd, void *buf, lsn_t lsn, int sector_type,
+ unsigned int nblocks)
+{
+ struct uscsi_cmd sc;
+ union scsi_cdb cdb;
+ int sub_channel = 0;
+
+ memset(&cdb, 0, sizeof(cdb));
+ memset(&sc, 0, sizeof(sc));
+
+ cdb.scc_cmd = CDIO_MMC_GPCMD_READ_CD;
+ CDIO_MMC_SET_READ_TYPE(cdb.cdb_opaque, sector_type);
+ CDIO_MMC_SET_READ_LBA(cdb.cdb_opaque, lsn);
+ CDIO_MMC_SET_READ_LENGTH(cdb.cdb_opaque, nblocks);
+ CDIO_MMC_SET_MAIN_CHANNEL_SELECTION_BITS(cdb.cdb_opaque,
+ CDIO_MMC_MCSB_ALL_HEADERS);
+ cdb.cdb_opaque[10] = sub_channel;
+
+ sc.uscsi_cdb = (caddr_t)&cdb;
+ sc.uscsi_cdblen = 12;
+ sc.uscsi_bufaddr = (caddr_t) buf;
+ sc.uscsi_buflen = CDIO_CD_FRAMESIZE_RAW;
+ sc.uscsi_flags = USCSI_ISOLATE | USCSI_READ;
+ sc.uscsi_timeout = 20;
+ if (ioctl(fd, USCSICMD, &sc)) {
+ perror("USCSICMD: READ CD");
+ return 1;
+ }
+ if (sc.uscsi_status) {
+ cdio_error("SCSI command failed with status %d\n",
+ sc.uscsi_status);
+ }
+
+ return sc.uscsi_status;
+}
+
+/*!
+ Reads a single mode2 sector from cd device into data starting from lsn.
+ Returns 0 if no error.
+ */
+static int
+_cdio_read_mode2_sector (void *env, void *data, lsn_t lsn,
+ bool mode2_form2)
+{
+ char buf[M2RAW_SECTOR_SIZE] = { 0, };
+ struct cdrom_msf *msf = (struct cdrom_msf *) &buf;
+ msf_t _msf;
+
+ _img_private_t *_obj = env;
+
+ cdio_lba_to_msf (cdio_lsn_to_lba(lsn), &_msf);
+ msf->cdmsf_min0 = from_bcd8(_msf.m);
+ msf->cdmsf_sec0 = from_bcd8(_msf.s);
+ msf->cdmsf_frame0 = from_bcd8(_msf.f);
+
+ if (_obj->gen.ioctls_debugged == 75)
+ cdio_debug ("only displaying every 75th ioctl from now on");
+
+ if (_obj->gen.ioctls_debugged == 30 * 75)
+ cdio_debug ("only displaying every 30*75th ioctl from now on");
+
+ if (_obj->gen.ioctls_debugged < 75
+ || (_obj->gen.ioctls_debugged < (30 * 75)
+ && _obj->gen.ioctls_debugged % 75 == 0)
+ || _obj->gen.ioctls_debugged % (30 * 75) == 0)
+ cdio_debug ("reading %2.2d:%2.2d:%2.2d",
+ msf->cdmsf_min0, msf->cdmsf_sec0, msf->cdmsf_frame0);
+
+ _obj->gen.ioctls_debugged++;
+
+ switch (_obj->access_mode)
+ {
+ case _AM_NONE:
+ cdio_error ("No way to read CD mode2.");
+ return 1;
+ break;
+
+ case _AM_SUN_CTRL_SCSI:
+ if (ioctl (_obj->gen.fd, CDROMREADMODE2, buf) == -1) {
+ perror ("ioctl(..,CDROMREADMODE2,..)");
+ return 1;
+ /* exit (EXIT_FAILURE); */
+ }
+ break;
+
+ case _AM_SUN_CTRL_ATAPI:
+ {
+ if (_cdio_mmc_read_sectors(_obj->gen.fd, data, lsn,
+ CDIO_MMC_READ_TYPE_MODE2, 1)) {
+ return 1;
+ }
+ break;
+ }
+ }
+
+ if (mode2_form2)
+ memcpy (data, buf, M2RAW_SECTOR_SIZE);
+ else
+ memcpy (((char *)data), buf + CDIO_CD_SUBHEADER_SIZE, CDIO_CD_FRAMESIZE);
+
+ return 0;
+}
+
+/*!
+ Reads single audio sectors from CD device into data starting from lsn.
+ Returns 0 if no error.
+
+ May have to check size of nblocks. There may be a limit that
+ can be read in one go, e.g. 25 blocks.
+*/
+
+static int
+_cdio_read_audio_sectors (void *env, void *data, lsn_t lsn,
+ unsigned int nblocks)
+{
+ _img_private_t *_obj = env;
+
+ if (_obj->gen.ioctls_debugged == 75)
+ cdio_debug ("only displaying every 75th ioctl from now on");
+
+ if (_obj->gen.ioctls_debugged == 30 * 75)
+ cdio_debug ("only displaying every 30*75th ioctl from now on");
+
+ if (_obj->gen.ioctls_debugged < 75
+ || (_obj->gen.ioctls_debugged < (30 * 75)
+ && _obj->gen.ioctls_debugged % 75 == 0)
+ || _obj->gen.ioctls_debugged % (30 * 75) == 0)
+ cdio_debug ("reading %d", lsn);
+
+ _obj->gen.ioctls_debugged++;
+
+ switch (_obj->access_mode)
+ {
+ case _AM_NONE:
+ cdio_error ("No way to read CD audio");
+ return 1;
+ break;
+
+ case _AM_SUN_CTRL_SCSI:
+ {
+ struct cdrom_cdda cdda;
+ cdda.cdda_addr = lsn;
+ cdda.cdda_length = nblocks;
+ cdda.cdda_data = (caddr_t) data;
+ if (ioctl (_obj->gen.fd, CDROMCDDA, &cdda) == -1) {
+ perror ("ioctl(..,CDROMCDDA,..)");
+ return 1;
+ /* exit (EXIT_FAILURE); */
+ }
+ }
+ break;
+
+ case _AM_SUN_CTRL_ATAPI:
+ {
+ if (_cdio_mmc_read_sectors(_obj->gen.fd, data, lsn,
+ CDIO_MMC_READ_TYPE_CDDA, nblocks)) {
+ return 1;
+ }
+ break;
+ }
+ }
+
+ return 0;
+}
+
+/*!
+ Reads nblocks of mode2 sectors from cd device into data starting
+ from lsn.
+ Returns 0 if no error.
+ */
+static int
+_cdio_read_mode2_sectors (void *env, void *data, uint32_t lsn,
+ bool mode2_form2, unsigned int nblocks)
+{
+ _img_private_t *_obj = env;
+ unsigned int i;
+ int retval;
+
+ for (i = 0; i < nblocks; i++) {
+ if (mode2_form2) {
+ if ( (retval = _cdio_read_mode2_sector (_obj,
+ ((char *)data) + (M2RAW_SECTOR_SIZE * i),
+ lsn + i, true)) )
+ return retval;
+ } else {
+ char buf[M2RAW_SECTOR_SIZE] = { 0, };
+ if ( (retval = _cdio_read_mode2_sector (_obj, buf, lsn + i, true)) )
+ return retval;
+
+ memcpy (((char *)data) + (CDIO_CD_FRAMESIZE * i),
+ buf + CDIO_CD_SUBHEADER_SIZE, CDIO_CD_FRAMESIZE);
+ }
+ }
+ return 0;
+}
+
+/*!
+ Return the size of the CD in logical block address (LBA) units.
+ */
+static uint32_t
+_cdio_stat_size (void *env)
+{
+ _img_private_t *_obj = env;
+
+ struct cdrom_tocentry tocent;
+ uint32_t size;
+
+ tocent.cdte_track = CDIO_CDROM_LEADOUT_TRACK;
+ tocent.cdte_format = CDROM_LBA;
+ if (ioctl (_obj->gen.fd, CDROMREADTOCENTRY, &tocent) == -1)
+ {
+ perror ("ioctl(CDROMREADTOCENTRY)");
+ exit (EXIT_FAILURE);
+ }
+
+ size = tocent.cdte_addr.lba;
+
+ return size;
+}
+
+/*!
+ Set the arg "key" with "value" in the source device.
+ Currently "source" and "access-mode" are valid keys.
+ "source" sets the source device in I/O operations
+ "access-mode" sets the the method of CD access
+
+ 0 is returned if no error was found, and nonzero if there as an error.
+*/
+static int
+_cdio_set_arg (void *env, const char key[], const char value[])
+{
+ _img_private_t *_obj = env;
+
+ if (!strcmp (key, "source"))
+ {
+ if (!value)
+ return -2;
+
+ free (_obj->gen.source_name);
+
+ _obj->gen.source_name = strdup (value);
+ }
+ else if (!strcmp (key, "access-mode"))
+ {
+ if (!strcmp(value, "ATAPI"))
+ _obj->access_mode = _AM_SUN_CTRL_ATAPI;
+ else if (!strcmp(value, "SCSI"))
+ _obj->access_mode = _AM_SUN_CTRL_SCSI;
+ else
+ cdio_warn ("unknown access type: %s. ignored.", value);
+ }
+ else
+ return -1;
+
+ return 0;
+}
+
+/*!
+ Read and cache the CD's Track Table of Contents and track info.
+ Return true if successful or false if an error.
+*/
+static bool
+_cdio_read_toc (_img_private_t *_obj)
+{
+ int i;
+
+ /* read TOC header */
+ if ( ioctl(_obj->gen.fd, CDROMREADTOCHDR, &_obj->tochdr) == -1 ) {
+ cdio_error("%s: %s\n",
+ "error in ioctl CDROMREADTOCHDR", strerror(errno));
+ return false;
+ }
+
+ /* read individual tracks */
+ for (i=_obj->tochdr.cdth_trk0; i<=_obj->tochdr.cdth_trk1; i++) {
+ _obj->tocent[i-1].cdte_track = i;
+ _obj->tocent[i-1].cdte_format = CDROM_MSF;
+ if ( ioctl(_obj->gen.fd, CDROMREADTOCENTRY, &_obj->tocent[i-1]) == -1 ) {
+ cdio_error("%s %d: %s\n",
+ "error in ioctl CDROMREADTOCENTRY for track",
+ i, strerror(errno));
+ return false;
+ }
+ }
+
+ /* read the lead-out track */
+ _obj->tocent[_obj->tochdr.cdth_trk1].cdte_track = CDIO_CDROM_LEADOUT_TRACK;
+ _obj->tocent[_obj->tochdr.cdth_trk1].cdte_format = CDROM_MSF;
+
+ if (ioctl(_obj->gen.fd, CDROMREADTOCENTRY,
+ &_obj->tocent[_obj->tochdr.cdth_trk1]) == -1 ) {
+ cdio_error("%s: %s\n",
+ "error in ioctl CDROMREADTOCENTRY for lead-out",
+ strerror(errno));
+ return false;
+ }
+
+ _obj->gen.toc_init = true;
+ return true;
+}
+
+/*!
+ Eject media in CD drive. If successful, as a side effect we
+ also free obj.
+ */
+static int
+_cdio_eject_media (void *env) {
+
+ _img_private_t *_obj = env;
+ int ret;
+
+ close(_obj->gen.fd);
+ _obj->gen.fd = -1;
+ if (_obj->gen.fd > -1) {
+ if ((ret = ioctl(_obj->gen.fd, CDROMEJECT)) != 0) {
+ cdio_generic_free((void *) _obj);
+ cdio_error ("CDROMEJECT failed: %s\n", strerror(errno));
+ return 1;
+ } else {
+ return 0;
+ }
+ }
+ return 2;
+}
+
+
+static void *
+_cdio_malloc_and_zero(size_t size) {
+ void *ptr;
+
+ if( !size ) size++;
+
+ if((ptr = malloc(size)) == NULL) {
+ cdio_error("malloc() failed: %s", strerror(errno));
+ return NULL;
+ }
+
+ memset(ptr, 0, size);
+ return ptr;
+}
+
+/*!
+ Return the value associated with the key "arg".
+*/
+static const char *
+_cdio_get_arg (void *env, const char key[])
+{
+ _img_private_t *_obj = env;
+
+ if (!strcmp (key, "source")) {
+ return _obj->gen.source_name;
+ } else if (!strcmp (key, "access-mode")) {
+ switch (_obj->access_mode) {
+ case _AM_SUN_CTRL_ATAPI:
+ return "ATAPI";
+ case _AM_SUN_CTRL_SCSI:
+ return "SCSI";
+ case _AM_NONE:
+ return "no access method";
+ }
+ }
+ return NULL;
+}
+
+/*!
+ Return a string containing the default CD device if none is specified.
+ */
+char *
+cdio_get_default_device_solaris(void)
+{
+ char *volume_device;
+ char *volume_name;
+ char *volume_action;
+ char *device;
+ struct stat stb;
+
+ if ((volume_device = getenv("VOLUME_DEVICE")) != NULL &&
+ (volume_name = getenv("VOLUME_NAME")) != NULL &&
+ (volume_action = getenv("VOLUME_ACTION")) != NULL &&
+ strcmp(volume_action, "insert") == 0) {
+
+ device = _cdio_malloc_and_zero(strlen(volume_device)
+ + strlen(volume_name) + 2);
+ if (device == NULL)
+ return strdup(DEFAULT_CDIO_DEVICE);
+ sprintf(device, "%s/%s", volume_device, volume_name);
+ if (stat(device, &stb) != 0 || !S_ISCHR(stb.st_mode)) {
+ free(device);
+ return strdup(DEFAULT_CDIO_DEVICE);
+ }
+ return device;
+ }
+ return strdup(DEFAULT_CDIO_DEVICE);
+}
+
+/*!
+ Return the number of of the first track.
+ CDIO_INVALID_TRACK is returned on error.
+*/
+static track_t
+_cdio_get_first_track_num(void *env)
+{
+ _img_private_t *_obj = env;
+
+ if (!_obj->gen.toc_init) _cdio_read_toc (_obj) ;
+
+ return FIRST_TRACK_NUM;
+}
+
+/*!
+ Return the number of tracks in the current medium.
+*/
+static track_t
+_cdio_get_num_tracks(void *env)
+{
+ _img_private_t *_obj = env;
+
+ if (!_obj->gen.toc_init) _cdio_read_toc (_obj) ;
+
+ return TOTAL_TRACKS;
+}
+
+/*!
+ Get format of track.
+*/
+static track_format_t
+_cdio_get_track_format(void *env, track_t track_num)
+{
+ _img_private_t *_obj = env;
+
+ if (!_obj->gen.init) _cdio_init(_obj);
+ if (!_obj->gen.toc_init) _cdio_read_toc (_obj) ;
+
+ if (track_num > TOTAL_TRACKS || track_num == 0)
+ return TRACK_FORMAT_ERROR;
+
+ /* This is pretty much copied from the "badly broken" cdrom_count_tracks
+ in linux/cdrom.c.
+ */
+ if (_obj->tocent[track_num-1].cdte_ctrl & CDROM_DATA_TRACK) {
+ if (_obj->tocent[track_num-1].cdte_format == 0x10)
+ return TRACK_FORMAT_CDI;
+ else if (_obj->tocent[track_num-1].cdte_format == 0x20)
+ return TRACK_FORMAT_XA;
+ else
+ return TRACK_FORMAT_DATA;
+ } else
+ return TRACK_FORMAT_AUDIO;
+
+}
+
+/*!
+ Return true if we have XA data (green, mode2 form1) or
+ XA data (green, mode2 form2). That is track begins:
+ sync - header - subheader
+ 12 4 - 8
+
+ FIXME: there's gotta be a better design for this and get_track_format?
+*/
+static bool
+_cdio_get_track_green(void *env, track_t track_num)
+{
+ _img_private_t *_obj = env;
+
+ if (!_obj->gen.init) _cdio_init(_obj);
+ if (!_obj->gen.toc_init) _cdio_read_toc (_obj) ;
+
+ if (track_num == CDIO_CDROM_LEADOUT_TRACK) track_num = TOTAL_TRACKS+1;
+
+ if (track_num > TOTAL_TRACKS+1 || track_num == 0)
+ return false;
+
+ /* FIXME: Dunno if this is the right way, but it's what
+ I was using in cdinfo for a while.
+ */
+ return ((_obj->tocent[track_num-1].cdte_ctrl & 2) != 0);
+}
+
+/*!
+ Return the starting MSF (minutes/secs/frames) for track number
+ track_num in obj. Tracks numbers start at 1.
+ The "leadout" track is specified either by
+ using track_num LEADOUT_TRACK or the total tracks+1.
+ False is returned if there is no entry.
+*/
+static bool
+_cdio_get_track_msf(void *env, track_t track_num, msf_t *msf)
+{
+ _img_private_t *_obj = env;
+
+ if (NULL == msf) return false;
+
+ if (!_obj->gen.init) _cdio_init(_obj);
+ if (!_obj->gen.toc_init) _cdio_read_toc (_obj) ;
+
+ if (track_num == CDIO_CDROM_LEADOUT_TRACK) track_num = TOTAL_TRACKS+1;
+
+ if (track_num > TOTAL_TRACKS+1 || track_num == 0) {
+ return false;
+ } else {
+ struct cdrom_tocentry *msf0 = &_obj->tocent[track_num-1];
+ msf->m = to_bcd8(msf0->cdte_addr.msf.minute);
+ msf->s = to_bcd8(msf0->cdte_addr.msf.second);
+ msf->f = to_bcd8(msf0->cdte_addr.msf.frame);
+ return true;
+ }
+}
+
+#else
+/*!
+ Return a string containing the default VCD device if none is specified.
+ */
+char *
+cdio_get_default_device_solaris(void)
+{
+ return strdup(DEFAULT_CDIO_DEVICE);
+}
+#endif /* HAVE_SOLARIS_CDROM */
+
+/*!
+ Return an array of strings giving possible CD devices.
+ */
+char **
+cdio_get_devices_solaris (void)
+{
+#ifndef HAVE_SOLARIS_CDROM
+ return NULL;
+#else
+ char **drives = NULL;
+ unsigned int num_files=0;
+#ifdef HAVE_GLOB_H
+ unsigned int i;
+ glob_t globbuf;
+ globbuf.gl_offs = 0;
+ glob("/vol/dev/aliases/cdrom*", GLOB_DOOFFS, NULL, &globbuf);
+ for (i=0; i<globbuf.gl_pathc; i++) {
+ cdio_add_device_list(&drives, globbuf.gl_pathv[i], &num_files);
+ }
+ globfree(&globbuf);
+#else
+ cdio_add_device_list(&drives, DEFAULT_CDIO_DEVICE, &num_files);
+#endif /*HAVE_GLOB_H*/
+ cdio_add_device_list(&drives, NULL, &num_files);
+ return drives;
+#endif /*HAVE_SOLARIS_CDROM*/
+}
+
+/*!
+ Initialization routine. This is the only thing that doesn't
+ get called via a function pointer. In fact *we* are the
+ ones to set that up.
+ */
+CdIo *
+cdio_open_solaris (const char *source_name)
+{
+
+#ifdef HAVE_SOLARIS_CDROM
+ CdIo *ret;
+ _img_private_t *_data;
+
+ cdio_funcs _funcs = {
+ .eject_media = _cdio_eject_media,
+ .free = cdio_generic_free,
+ .get_arg = _cdio_get_arg,
+ .get_devices = cdio_get_devices_solaris,
+ .get_default_device = cdio_get_default_device_solaris,
+ .get_first_track_num= _cdio_get_first_track_num,
+ .get_num_tracks = _cdio_get_num_tracks,
+ .get_track_format = _cdio_get_track_format,
+ .get_track_green = _cdio_get_track_green,
+ .get_track_lba = NULL, /* This could be implemented if need be. */
+ .get_track_msf = _cdio_get_track_msf,
+ .lseek = cdio_generic_lseek,
+ .read = cdio_generic_read,
+ .read_audio_sectors = _cdio_read_audio_sectors,
+ .read_mode2_sector = _cdio_read_mode2_sector,
+ .read_mode2_sectors = _cdio_read_mode2_sectors,
+ .stat_size = _cdio_stat_size,
+ .set_arg = _cdio_set_arg
+ };
+
+ _data = _cdio_malloc (sizeof (_img_private_t));
+ _data->gen.fd = -1;
+
+ _cdio_set_arg(_data, "source", (NULL == source_name)
+ ? DEFAULT_CDIO_DEVICE: source_name);
+
+ ret = cdio_new (_data, &_funcs);
+ if (ret == NULL) return NULL;
+
+ if (_cdio_init(_data))
+ return ret;
+ else {
+ cdio_generic_free (_data);
+ return NULL;
+ }
+
+#else
+ return NULL;
+#endif /* HAVE_SOLARIS_CDROM */
+
+}
+
+bool
+cdio_have_solaris (void)
+{
+#ifdef HAVE_SOLARIS_CDROM
+ return true;
+#else
+ return false;
+#endif /* HAVE_SOLARIS_CDROM */
+}
+
diff --git a/src/input/vcd/libcdio/_cdio_win32.c b/src/input/vcd/libcdio/_cdio_win32.c
new file mode 100644
index 000000000..418beb86c
--- /dev/null
+++ b/src/input/vcd/libcdio/_cdio_win32.c
@@ -0,0 +1,1168 @@
+/*
+ $Id: _cdio_win32.c,v 1.1 2003/10/13 11:47:11 f1rmb Exp $
+
+ Copyright (C) 2003 Rocky Bernstein <rocky@panix.com>
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+*/
+
+/* This file contains Win32-specific code and implements low-level
+ control of the CD drive. Inspired by vlc's cdrom.h code
+*/
+
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
+
+static const char _rcsid[] = "$Id: _cdio_win32.c,v 1.1 2003/10/13 11:47:11 f1rmb Exp $";
+
+#include <cdio/cdio.h>
+#include <cdio/sector.h>
+#include <cdio/util.h>
+#include "cdio_assert.h"
+#include "cdio_private.h"
+#include "scsi_mmc.h"
+
+/* LBA = msf.frame + 75 * ( msf.second - 2 + 60 * msf.minute ) */
+#define MSF_TO_LBA2(min, sec, frame) ((int)frame + 75 * (sec -2 + 60 * min))
+
+#include <string.h>
+
+#ifdef HAVE_WIN32_CDROM
+
+#include <ctype.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <errno.h>
+#include <unistd.h>
+#include <fcntl.h>
+
+#include <windows.h>
+#include <winioctl.h>
+
+#include <sys/stat.h>
+#include <sys/types.h>
+
+/* Win32 DeviceIoControl specifics */
+#ifndef MAXIMUM_NUMBER_TRACKS
+# define MAXIMUM_NUMBER_TRACKS 100
+#endif
+typedef struct _TRACK_DATA {
+ UCHAR Reserved;
+ UCHAR Control : 4;
+ UCHAR Adr : 4;
+ UCHAR TrackNumber;
+ UCHAR Reserved1;
+ UCHAR Address[4];
+} TRACK_DATA, *PTRACK_DATA;
+typedef struct _CDROM_TOC {
+ UCHAR Length[2];
+ UCHAR FirstTrack;
+ UCHAR LastTrack;
+ TRACK_DATA TrackData[MAXIMUM_NUMBER_TRACKS];
+} CDROM_TOC, *PCDROM_TOC;
+typedef enum _TRACK_MODE_TYPE {
+ YellowMode2,
+ XAForm2,
+ CDDA
+} TRACK_MODE_TYPE, *PTRACK_MODE_TYPE;
+typedef struct __RAW_READ_INFO {
+ LARGE_INTEGER DiskOffset;
+ ULONG SectorCount;
+ TRACK_MODE_TYPE TrackMode;
+} RAW_READ_INFO, *PRAW_READ_INFO;
+
+#ifndef IOCTL_CDROM_BASE
+# define IOCTL_CDROM_BASE FILE_DEVICE_CD_ROM
+#endif
+#ifndef IOCTL_CDROM_READ_TOC
+# define IOCTL_CDROM_READ_TOC CTL_CODE(IOCTL_CDROM_BASE, 0x0000, \
+ METHOD_BUFFERED, FILE_READ_ACCESS)
+#endif
+#ifndef IOCTL_CDROM_RAW_READ
+#define IOCTL_CDROM_RAW_READ CTL_CODE(IOCTL_CDROM_BASE, 0x000F, \
+ METHOD_OUT_DIRECT, FILE_READ_ACCESS)
+#endif
+
+/* Win32 aspi specific */
+#define WIN_NT ( GetVersion() < 0x80000000 )
+#define ASPI_HAID 0
+#define ASPI_TARGET 0
+#define DTYPE_CDROM 0x05
+
+#define SENSE_LEN 0x0E
+#define SC_GET_DEV_TYPE 0x01
+#define SC_EXEC_SCSI_CMD 0x02
+#define SC_GET_DISK_INFO 0x06
+#define SS_COMP 0x01
+#define SS_PENDING 0x00
+#define SS_NO_ADAPTERS 0xE8
+#define SRB_DIR_IN 0x08
+#define SRB_DIR_OUT 0x10
+#define SRB_EVENT_NOTIFY 0x40
+
+#define SECTOR_TYPE_MODE2 0x14
+#define READ_CD_USERDATA_MODE2 0x10
+
+#define READ_TOC 0x43
+#define READ_TOC_FORMAT_TOC 0x0
+
+#pragma pack(1)
+
+struct SRB_GetDiskInfo
+{
+ unsigned char SRB_Cmd;
+ unsigned char SRB_Status;
+ unsigned char SRB_HaId;
+ unsigned char SRB_Flags;
+ unsigned long SRB_Hdr_Rsvd;
+ unsigned char SRB_Target;
+ unsigned char SRB_Lun;
+ unsigned char SRB_DriveFlags;
+ unsigned char SRB_Int13HDriveInfo;
+ unsigned char SRB_Heads;
+ unsigned char SRB_Sectors;
+ unsigned char SRB_Rsvd1[22];
+};
+
+struct SRB_GDEVBlock
+{
+ unsigned char SRB_Cmd;
+ unsigned char SRB_Status;
+ unsigned char SRB_HaId;
+ unsigned char SRB_Flags;
+ unsigned long SRB_Hdr_Rsvd;
+ unsigned char SRB_Target;
+ unsigned char SRB_Lun;
+ unsigned char SRB_DeviceType;
+ unsigned char SRB_Rsvd1;
+};
+
+struct SRB_ExecSCSICmd
+{
+ unsigned char SRB_Cmd;
+ unsigned char SRB_Status;
+ unsigned char SRB_HaId;
+ unsigned char SRB_Flags;
+ unsigned long SRB_Hdr_Rsvd;
+ unsigned char SRB_Target;
+ unsigned char SRB_Lun;
+ unsigned short SRB_Rsvd1;
+ unsigned long SRB_BufLen;
+ unsigned char *SRB_BufPointer;
+ unsigned char SRB_SenseLen;
+ unsigned char SRB_CDBLen;
+ unsigned char SRB_HaStat;
+ unsigned char SRB_TargStat;
+ unsigned long *SRB_PostProc;
+ unsigned char SRB_Rsvd2[20];
+ unsigned char CDBByte[16];
+ unsigned char SenseArea[SENSE_LEN+2];
+};
+
+#pragma pack()
+
+typedef struct {
+ lsn_t start_lsn;
+} track_info_t;
+
+typedef struct {
+ /* Things common to all drivers like this.
+ This must be first. */
+ generic_img_private_t gen;
+
+ HANDLE h_device_handle; /* device descriptor */
+ long hASPI;
+ short i_sid;
+ long (*lpSendCommand)( void* );
+
+ /* Track information */
+ bool toc_init; /* if true, info below is valid. */
+ track_info_t tocent[100]; /* entry info for each track */
+ track_t total_tracks; /* number of tracks in image */
+ track_t first_track_num; /* track number of first track */
+
+} _img_private_t;
+
+/* General ioctl() CD-ROM command function */
+static bool
+_cdio_mciSendCommand(int id, UINT msg, DWORD flags, void *arg)
+{
+ MCIERROR mci_error;
+
+ mci_error = mciSendCommand(id, msg, flags, (DWORD)arg);
+ if ( mci_error ) {
+ char error[256];
+
+ mciGetErrorString(mci_error, error, 256);
+ cdio_error("mciSendCommand() error: %s", error);
+ }
+ return(mci_error == 0);
+}
+
+static const char *
+cdio_is_cdrom(const char drive_letter) {
+ static char psz_win32_drive[7];
+ static char root_path_name[8];
+ _img_private_t obj;
+
+ /* Initializations */
+ obj.h_device_handle = NULL;
+ obj.i_sid = 0;
+ obj.hASPI = 0;
+ obj.lpSendCommand = 0;
+
+ if ( WIN_NT ) {
+ sprintf( psz_win32_drive, "\\\\.\\%c:", drive_letter );
+ sprintf( root_path_name, "\\\\.\\%c:\\", drive_letter );
+
+ obj.h_device_handle = CreateFile( psz_win32_drive, GENERIC_READ,
+ FILE_SHARE_READ | FILE_SHARE_WRITE,
+ NULL, OPEN_EXISTING,
+ FILE_FLAG_NO_BUFFERING |
+ FILE_FLAG_RANDOM_ACCESS, NULL );
+ if (obj.h_device_handle != NULL
+ && (DRIVE_CDROM == GetDriveType(root_path_name))) {
+ CloseHandle(obj.h_device_handle);
+ return strdup(psz_win32_drive);
+ } else {
+ CloseHandle(obj.h_device_handle);
+ return NULL;
+ }
+ } else {
+ HMODULE hASPI = NULL;
+ long (*lpGetSupport)( void ) = NULL;
+ long (*lpSendCommand)( void* ) = NULL;
+ DWORD dwSupportInfo;
+ int j, i_hostadapters;
+ char c_drive;
+
+ hASPI = LoadLibrary( "wnaspi32.dll" );
+ if( hASPI != NULL ) {
+ (FARPROC) lpGetSupport = GetProcAddress( hASPI,
+ "GetASPI32SupportInfo" );
+ (FARPROC) lpSendCommand = GetProcAddress( hASPI,
+ "SendASPI32Command" );
+ }
+
+ if( hASPI == NULL || lpGetSupport == NULL || lpSendCommand == NULL ) {
+ cdio_debug("Unable to load ASPI or get ASPI function pointers");
+ if( hASPI ) FreeLibrary( hASPI );
+ return NULL;
+ }
+
+ /* ASPI support seems to be there */
+
+ dwSupportInfo = lpGetSupport();
+
+ if( HIBYTE( LOWORD ( dwSupportInfo ) ) == SS_NO_ADAPTERS ) {
+ cdio_debug("no host adapters found (ASPI)");
+ FreeLibrary( hASPI );
+ return NULL;
+ }
+
+ if( HIBYTE( LOWORD ( dwSupportInfo ) ) != SS_COMP ) {
+ cdio_debug("Unable to initalize ASPI layer");
+ FreeLibrary( hASPI );
+ return NULL;
+ }
+
+ i_hostadapters = LOBYTE( LOWORD( dwSupportInfo ) );
+ if( i_hostadapters == 0 ) {
+ FreeLibrary( hASPI );
+ return NULL;
+ }
+
+ c_drive = toupper(drive_letter) - 'A';
+
+ for( j = 0; j < 15; j++ ) {
+ struct SRB_GetDiskInfo srbDiskInfo;
+
+ srbDiskInfo.SRB_Cmd = SC_GET_DISK_INFO;
+ srbDiskInfo.SRB_HaId = 0;
+ srbDiskInfo.SRB_Flags = 0;
+ srbDiskInfo.SRB_Hdr_Rsvd = 0;
+ srbDiskInfo.SRB_Target = j;
+ srbDiskInfo.SRB_Lun = 0;
+
+ lpSendCommand( (void*) &srbDiskInfo );
+
+ if( (srbDiskInfo.SRB_Status == SS_COMP) &&
+ (srbDiskInfo.SRB_Int13HDriveInfo == c_drive) ) {
+ /* Make sure this is a cdrom device */
+ struct SRB_GDEVBlock srbGDEVBlock;
+
+ memset( &srbGDEVBlock, 0, sizeof(struct SRB_GDEVBlock) );
+ srbGDEVBlock.SRB_Cmd = SC_GET_DEV_TYPE;
+ srbGDEVBlock.SRB_HaId = 0;
+ srbGDEVBlock.SRB_Target = j;
+
+ lpSendCommand( (void*) &srbGDEVBlock );
+
+ if( ( srbGDEVBlock.SRB_Status == SS_COMP ) &&
+ ( srbGDEVBlock.SRB_DeviceType == DTYPE_CDROM ) ) {
+ sprintf( psz_win32_drive, "%c:", drive_letter );
+ FreeLibrary( hASPI );
+ return(psz_win32_drive);
+ }
+ }
+ }
+ FreeLibrary( hASPI );
+ }
+ return NULL;
+
+}
+
+/*!
+ Initialize CD device.
+ */
+static bool
+_cdio_init_win32 (void *user_data)
+{
+ _img_private_t *_obj = user_data;
+ if (_obj->gen.init) {
+ cdio_error ("init called more than once");
+ return false;
+ }
+
+ _obj->gen.init = true;
+ _obj->toc_init = false;
+
+
+ /* Initializations */
+ _obj->h_device_handle = NULL;
+ _obj->i_sid = 0;
+ _obj->hASPI = 0;
+ _obj->lpSendCommand = 0;
+
+ if ( WIN_NT ) {
+ char psz_win32_drive[7];
+ unsigned int len=strlen(_obj->gen.source_name);
+
+ cdio_debug("using winNT/2K/XP ioctl layer");
+
+ if (cdio_is_device_win32(_obj->gen.source_name)) {
+ sprintf( psz_win32_drive, "\\\\.\\%c:", _obj->gen.source_name[len-2] );
+
+ _obj->h_device_handle = CreateFile( psz_win32_drive, GENERIC_READ,
+ FILE_SHARE_READ | FILE_SHARE_WRITE,
+ NULL, OPEN_EXISTING,
+ FILE_FLAG_NO_BUFFERING |
+ FILE_FLAG_RANDOM_ACCESS, NULL );
+ return (_obj->h_device_handle == NULL) ? false : true;
+ } else
+ return false;
+ } else {
+ HMODULE hASPI = NULL;
+ long (*lpGetSupport)( void ) = NULL;
+ long (*lpSendCommand)( void* ) = NULL;
+ DWORD dwSupportInfo;
+ int i, j, i_hostadapters;
+ char c_drive = _obj->gen.source_name[0];
+
+ hASPI = LoadLibrary( "wnaspi32.dll" );
+ if( hASPI != NULL ) {
+ (FARPROC) lpGetSupport = GetProcAddress( hASPI,
+ "GetASPI32SupportInfo" );
+ (FARPROC) lpSendCommand = GetProcAddress( hASPI,
+ "SendASPI32Command" );
+ }
+
+ if( hASPI == NULL || lpGetSupport == NULL || lpSendCommand == NULL ) {
+ cdio_debug("Unable to load ASPI or get ASPI function pointers");
+ if( hASPI ) FreeLibrary( hASPI );
+ return false;
+ }
+
+ /* ASPI support seems to be there */
+
+ dwSupportInfo = lpGetSupport();
+
+ if( HIBYTE( LOWORD ( dwSupportInfo ) ) == SS_NO_ADAPTERS ) {
+ cdio_debug("no host adapters found (ASPI)");
+ FreeLibrary( hASPI );
+ return -1;
+ }
+
+ if( HIBYTE( LOWORD ( dwSupportInfo ) ) != SS_COMP ) {
+ cdio_debug("unable to initalize ASPI layer");
+ FreeLibrary( hASPI );
+ return -1;
+ }
+
+ i_hostadapters = LOBYTE( LOWORD( dwSupportInfo ) );
+ if( i_hostadapters == 0 ) {
+ FreeLibrary( hASPI );
+ return -1;
+ }
+
+ c_drive = toupper(c_drive) - 'A';
+
+ for( i = 0; i < i_hostadapters; i++ ) {
+ for( j = 0; j < 15; j++ ) {
+ struct SRB_GetDiskInfo srbDiskInfo;
+
+ srbDiskInfo.SRB_Cmd = SC_GET_DISK_INFO;
+ srbDiskInfo.SRB_HaId = i;
+ srbDiskInfo.SRB_Flags = 0;
+ srbDiskInfo.SRB_Hdr_Rsvd = 0;
+ srbDiskInfo.SRB_Target = j;
+ srbDiskInfo.SRB_Lun = 0;
+
+ lpSendCommand( (void*) &srbDiskInfo );
+
+ if( (srbDiskInfo.SRB_Status == SS_COMP) &&
+ (srbDiskInfo.SRB_Int13HDriveInfo == c_drive) ) {
+ /* Make sure this is a cdrom device */
+ struct SRB_GDEVBlock srbGDEVBlock;
+
+ memset( &srbGDEVBlock, 0, sizeof(struct SRB_GDEVBlock) );
+ srbGDEVBlock.SRB_Cmd = SC_GET_DEV_TYPE;
+ srbGDEVBlock.SRB_HaId = i;
+ srbGDEVBlock.SRB_Target = j;
+
+ lpSendCommand( (void*) &srbGDEVBlock );
+
+ if( ( srbGDEVBlock.SRB_Status == SS_COMP ) &&
+ ( srbGDEVBlock.SRB_DeviceType == DTYPE_CDROM ) ) {
+ _obj->i_sid = MAKEWORD( i, j );
+ _obj->hASPI = (long)hASPI;
+ _obj->lpSendCommand = lpSendCommand;
+ cdio_debug("Using ASPI layer");
+
+ return true;
+ } else {
+ FreeLibrary( hASPI );
+ cdio_debug( "%c: is not a CD-ROM drive",
+ _obj->gen.source_name[0] );
+ return false;
+ }
+ }
+ }
+ }
+
+ FreeLibrary( hASPI );
+ cdio_debug( "Unable to get HaId and target (ASPI)" );
+
+ }
+
+ return false;
+}
+
+/*!
+ Release and free resources associated with cd.
+ */
+static void
+_cdio_win32_free (void *user_data)
+{
+ _img_private_t *_obj = user_data;
+
+ if (NULL == _obj) return;
+ free (_obj->gen.source_name);
+
+ if( _obj->h_device_handle )
+ CloseHandle( _obj->h_device_handle );
+ if( _obj->hASPI )
+ FreeLibrary( (HMODULE)_obj->hASPI );
+
+ free (_obj);
+}
+
+/*!
+ Reads a single mode2 sector from cd device into data starting from lsn.
+ Returns 0 if no error.
+ */
+static int
+_cdio_mmc_read_sectors (void *user_data, void *data, lsn_t lsn,
+ int sector_type, unsigned int nblocks)
+{
+ _img_private_t *_obj = user_data;
+ unsigned char buf[CDIO_CD_FRAMESIZE_RAW] = { 0, };
+
+ if( _obj->hASPI ) {
+ HANDLE hEvent;
+ struct SRB_ExecSCSICmd ssc;
+
+ /* Create the transfer completion event */
+ hEvent = CreateEvent( NULL, TRUE, FALSE, NULL );
+ if( hEvent == NULL ) {
+ return 1;
+ }
+
+ /* Data selection */
+
+ memset( &ssc, 0, sizeof( ssc ) );
+
+ ssc.SRB_Cmd = SC_EXEC_SCSI_CMD;
+ ssc.SRB_Flags = SRB_DIR_IN | SRB_EVENT_NOTIFY;
+ ssc.SRB_HaId = LOBYTE( _obj->i_sid );
+ ssc.SRB_Target = HIBYTE( _obj->i_sid );
+ ssc.SRB_SenseLen = SENSE_LEN;
+
+ ssc.SRB_PostProc = (LPVOID) hEvent;
+ ssc.SRB_CDBLen = 12;
+
+ /* Operation code */
+ ssc.CDBByte[ 0 ] = CDIO_MMC_GPCMD_READ_CD;
+
+ CDIO_MMC_SET_READ_TYPE(ssc.CDBByte, sector_type);
+ CDIO_MMC_SET_READ_LBA(ssc.CDBByte, lsn);
+ CDIO_MMC_SET_READ_LENGTH(ssc.CDBByte, nblocks);
+ CDIO_MMC_SET_MAIN_CHANNEL_SELECTION_BITS(ssc.CDBByte,
+ CDIO_MMC_MCSB_ALL_HEADERS);
+
+ /* Result buffer */
+ ssc.SRB_BufPointer = buf;
+ ssc.SRB_BufLen = CDIO_CD_FRAMESIZE_RAW;
+
+ /* Initiate transfer */
+ ResetEvent( hEvent );
+ _obj->lpSendCommand( (void*) &ssc );
+
+ /* If the command has still not been processed, wait until it's
+ * finished */
+ if( ssc.SRB_Status == SS_PENDING ) {
+ WaitForSingleObject( hEvent, INFINITE );
+ }
+ CloseHandle( hEvent );
+
+ /* check that the transfer went as planned */
+ if( ssc.SRB_Status != SS_COMP ) {
+ return 1;
+ }
+
+ } else {
+ DWORD dwBytesReturned;
+ RAW_READ_INFO cdrom_raw;
+
+ /* Initialize CDROM_RAW_READ structure */
+ cdrom_raw.DiskOffset.QuadPart = CDIO_CD_FRAMESIZE * lsn;
+ cdrom_raw.SectorCount = 1;
+ cdrom_raw.TrackMode = XAForm2;
+
+ if( DeviceIoControl( _obj->h_device_handle,
+ IOCTL_CDROM_RAW_READ, &cdrom_raw,
+ sizeof(RAW_READ_INFO), buf,
+ sizeof(buf), &dwBytesReturned, NULL )
+ == 0 ) {
+ return 1;
+ }
+ }
+
+ /* FIXME! remove the 8 (SUBHEADER size) below... */
+ memcpy (data, buf, CDIO_CD_FRAMESIZE_RAW);
+
+ return 0;
+}
+
+/*!
+ Reads an audio device into data starting from lsn.
+ Returns 0 if no error.
+ */
+static int
+_cdio_read_audio_sectors (void *user_data, void *data, lsn_t lsn,
+ unsigned int nblocks)
+{
+ return _cdio_mmc_read_sectors( user_data, data, lsn,
+ CDIO_MMC_READ_TYPE_CDDA, nblocks );
+}
+
+/*!
+ Reads a single mode2 sector from cd device into data starting
+ from lsn. Returns 0 if no error.
+ */
+static int
+_cdio_read_mode2_sector (void *user_data, void *data, lsn_t lsn,
+ bool mode2_form2)
+{
+ char buf[CDIO_CD_FRAMESIZE_RAW] = { 0, };
+ _img_private_t *_obj = user_data;
+ int ret;
+
+ if (_obj->gen.ioctls_debugged == 75)
+ cdio_debug ("only displaying every 75th ioctl from now on");
+
+ if (_obj->gen.ioctls_debugged == 30 * 75)
+ cdio_debug ("only displaying every 30*75th ioctl from now on");
+
+ if (_obj->gen.ioctls_debugged < 75
+ || (_obj->gen.ioctls_debugged < (30 * 75)
+ && _obj->gen.ioctls_debugged % 75 == 0)
+ || _obj->gen.ioctls_debugged % (30 * 75) == 0)
+ cdio_debug ("reading %lu", (unsigned long int) lsn);
+
+ _obj->gen.ioctls_debugged++;
+
+ ret = _cdio_mmc_read_sectors(user_data, buf, lsn, CDIO_MMC_READ_TYPE_ANY, 1);
+
+ if( ret != 0 ) return ret;
+
+ if (mode2_form2)
+ memcpy (data, buf, M2RAW_SECTOR_SIZE);
+ else
+ memcpy (((char *)data), buf + CDIO_CD_SUBHEADER_SIZE, CDIO_CD_FRAMESIZE);
+
+ return 0;
+}
+
+/*!
+ Reads nblocks of mode2 sectors from cd device into data starting
+ from lsn.
+ Returns 0 if no error.
+ */
+static int
+_cdio_read_mode2_sectors (void *user_data, void *data, lsn_t lsn,
+ bool mode2_form2, unsigned int nblocks)
+{
+ _img_private_t *_obj = user_data;
+ int i;
+ int retval;
+
+ for (i = 0; i < nblocks; i++) {
+ if (mode2_form2) {
+ if ( (retval = _cdio_read_mode2_sector (_obj,
+ ((char *)data) + (M2RAW_SECTOR_SIZE * i),
+ lsn + i, true)) )
+ return retval;
+ } else {
+ char buf[M2RAW_SECTOR_SIZE] = { 0, };
+ if ( (retval = _cdio_read_mode2_sector (_obj, buf, lsn + i, true)) )
+ return retval;
+
+ memcpy (((char *)data) + (CDIO_CD_FRAMESIZE * i),
+ buf + CDIO_CD_SUBHEADER_SIZE, CDIO_CD_FRAMESIZE);
+ }
+ }
+ return 0;
+}
+
+/*!
+ Return the size of the CD in logical block address (LBA) units.
+ */
+static uint32_t
+_cdio_stat_size (void *user_data)
+{
+ _img_private_t *_obj = user_data;
+
+ return _obj->tocent[_obj->total_tracks].start_lsn;
+}
+
+/*!
+ Set the key "arg" to "value" in source device.
+*/
+static int
+_cdio_set_arg (void *user_data, const char key[], const char value[])
+{
+ _img_private_t *_obj = user_data;
+
+ if (!strcmp (key, "source"))
+ {
+ if (!value)
+ return -2;
+
+ free (_obj->gen.source_name);
+
+ _obj->gen.source_name = strdup (value);
+ }
+ else
+ return -1;
+
+ return 0;
+}
+
+/*!
+ Read and cache the CD's Track Table of Contents and track info.
+ Return true if successful or false if an error.
+*/
+static bool
+_cdio_read_toc (_img_private_t *_obj)
+{
+
+ if( _obj->hASPI ) {
+ HANDLE hEvent;
+ struct SRB_ExecSCSICmd ssc;
+ unsigned char p_tocheader[ 4 ];
+
+ /* Create the transfer completion event */
+ hEvent = CreateEvent( NULL, TRUE, FALSE, NULL );
+ if( hEvent == NULL ) {
+ return false;
+ }
+
+ memset( &ssc, 0, sizeof( ssc ) );
+
+ ssc.SRB_Cmd = SC_EXEC_SCSI_CMD;
+ ssc.SRB_Flags = SRB_DIR_IN | SRB_EVENT_NOTIFY;
+ ssc.SRB_HaId = LOBYTE( _obj->i_sid );
+ ssc.SRB_Target = HIBYTE( _obj->i_sid );
+ ssc.SRB_SenseLen = SENSE_LEN;
+
+ ssc.SRB_PostProc = (LPVOID) hEvent;
+ ssc.SRB_CDBLen = 10;
+
+ /* Operation code */
+ ssc.CDBByte[ 0 ] = READ_TOC;
+
+ /* Format */
+ ssc.CDBByte[ 2 ] = READ_TOC_FORMAT_TOC;
+
+ /* Starting track */
+ ssc.CDBByte[ 6 ] = 0;
+
+ /* Allocation length and buffer */
+ ssc.SRB_BufLen = sizeof( p_tocheader );
+ ssc.SRB_BufPointer = p_tocheader;
+ ssc.CDBByte[ 7 ] = ( ssc.SRB_BufLen >> 8 ) & 0xff;
+ ssc.CDBByte[ 8 ] = ( ssc.SRB_BufLen ) & 0xff;
+
+ /* Initiate transfer */
+ ResetEvent( hEvent );
+ _obj->lpSendCommand( (void*) &ssc );
+
+ /* If the command has still not been processed, wait until it's
+ * finished */
+ if( ssc.SRB_Status == SS_PENDING )
+ WaitForSingleObject( hEvent, INFINITE );
+
+ /* check that the transfer went as planned */
+ if( ssc.SRB_Status != SS_COMP ) {
+ CloseHandle( hEvent );
+ return false;
+ }
+
+ _obj->first_track_num = p_tocheader[2];
+ _obj->total_tracks = p_tocheader[3] - p_tocheader[2] + 1;
+
+ {
+ int i, i_toclength;
+ unsigned char *p_fulltoc;
+
+ i_toclength = 4 /* header */ + p_tocheader[0] +
+ ((unsigned int)p_tocheader[1] << 8);
+
+ p_fulltoc = malloc( i_toclength );
+
+ if( p_fulltoc == NULL ) {
+ cdio_error( "out of memory" );
+ CloseHandle( hEvent );
+ return false;
+ }
+
+ /* Allocation length and buffer */
+ ssc.SRB_BufLen = i_toclength;
+ ssc.SRB_BufPointer = p_fulltoc;
+ ssc.CDBByte[ 7 ] = ( ssc.SRB_BufLen >> 8 ) & 0xff;
+ ssc.CDBByte[ 8 ] = ( ssc.SRB_BufLen ) & 0xff;
+
+ /* Initiate transfer */
+ ResetEvent( hEvent );
+ _obj->lpSendCommand( (void*) &ssc );
+
+ /* If the command has still not been processed, wait until it's
+ * finished */
+ if( ssc.SRB_Status == SS_PENDING )
+ WaitForSingleObject( hEvent, INFINITE );
+
+ /* check that the transfer went as planned */
+ if( ssc.SRB_Status != SS_COMP )
+ _obj->total_tracks = 0;
+
+ for( i = 0 ; i <= _obj->total_tracks ; i++ ) {
+ int i_index = 8 + 8 * i;
+ _obj->tocent[ i ].start_lsn = ((int)p_fulltoc[ i_index ] << 24) +
+ ((int)p_fulltoc[ i_index+1 ] << 16) +
+ ((int)p_fulltoc[ i_index+2 ] << 8) +
+ (int)p_fulltoc[ i_index+3 ];
+
+ cdio_debug( "p_sectors: %i %lu",
+ i, (unsigned long int) _obj->tocent[i].start_lsn );
+ }
+
+ free( p_fulltoc );
+ }
+
+ CloseHandle( hEvent );
+ return true;
+
+ } else {
+ DWORD dwBytesReturned;
+ CDROM_TOC cdrom_toc;
+ int i;
+
+ if( DeviceIoControl( _obj->h_device_handle,
+ IOCTL_CDROM_READ_TOC,
+ NULL, 0, &cdrom_toc, sizeof(CDROM_TOC),
+ &dwBytesReturned, NULL ) == 0 ) {
+ cdio_debug( "could not read TOCHDR" );
+ return false;
+ }
+
+ _obj->first_track_num = cdrom_toc.FirstTrack;
+ _obj->total_tracks = cdrom_toc.LastTrack - cdrom_toc.FirstTrack + 1;
+
+
+ for( i = 0 ; i <= _obj->total_tracks ; i++ ) {
+ _obj->tocent[ i ].start_lsn = MSF_TO_LBA2(
+ cdrom_toc.TrackData[i].Address[1],
+ cdrom_toc.TrackData[i].Address[2],
+ cdrom_toc.TrackData[i].Address[3] );
+ cdio_debug("p_sectors: %i, %lu", i,
+ (unsigned long int) (_obj->tocent[i].start_lsn));
+ }
+ }
+ return true;
+}
+
+/*!
+ Eject media. Return 1 if successful, 0 otherwise.
+ */
+static int
+_cdio_eject_media (void *user_data) {
+
+ _img_private_t *_obj = user_data;
+
+
+ MCI_OPEN_PARMS op;
+ MCI_STATUS_PARMS st;
+ DWORD i_flags;
+ char psz_drive[4];
+ int ret;
+
+ memset( &op, 0, sizeof(MCI_OPEN_PARMS) );
+ op.lpstrDeviceType = (LPCSTR)MCI_DEVTYPE_CD_AUDIO;
+ strcpy( psz_drive, "X:" );
+ psz_drive[0] = _obj->gen.source_name[0];
+ op.lpstrElementName = psz_drive;
+
+ /* Set the flags for the device type */
+ i_flags = MCI_OPEN_TYPE | MCI_OPEN_TYPE_ID |
+ MCI_OPEN_ELEMENT | MCI_OPEN_SHAREABLE;
+
+ if( !mciSendCommand( 0, MCI_OPEN, i_flags, (unsigned long)&op ) ) {
+ st.dwItem = MCI_STATUS_READY;
+ /* Eject disc */
+ ret = mciSendCommand( op.wDeviceID, MCI_SET, MCI_SET_DOOR_OPEN, 0 ) != 0;
+ /* Release access to the device */
+ mciSendCommand( op.wDeviceID, MCI_CLOSE, MCI_WAIT, 0 );
+ } else
+ ret = 0;
+
+ return ret;
+}
+
+/*!
+ Return the value associated with the key "arg".
+*/
+static const char *
+_cdio_get_arg (void *user_data, const char key[])
+{
+ _img_private_t *_obj = user_data;
+
+ if (!strcmp (key, "source")) {
+ return _obj->gen.source_name;
+ } else if (!strcmp (key, "access-mode")) {
+ if ( WIN_NT )
+ return "winNT/2K/XP ioctl";
+ else if (_obj->hASPI)
+ return "ASPI";
+ else
+ return "undefined WIN32";
+ }
+ return NULL;
+}
+
+/*!
+ Return the number of of the first track.
+ CDIO_INVALID_TRACK is returned on error.
+*/
+static track_t
+_cdio_get_first_track_num(void *user_data)
+{
+ _img_private_t *_obj = user_data;
+
+ if (!_obj->toc_init) _cdio_read_toc (_obj) ;
+
+ return _obj->first_track_num;
+}
+
+/*!
+ Return the number of tracks in the current medium.
+ CDIO_INVALID_TRACK is returned on error.
+*/
+static track_t
+_cdio_get_num_tracks(void *user_data)
+{
+ _img_private_t *_obj = user_data;
+
+ if (!_obj->toc_init) _cdio_read_toc (_obj) ;
+
+ return _obj->total_tracks;
+}
+
+/*!
+ Get format of track.
+*/
+static track_format_t
+_cdio_get_track_format(void *user_data, track_t track_num)
+{
+ _img_private_t *_obj = user_data;
+
+ MCI_OPEN_PARMS op;
+ MCI_STATUS_PARMS st;
+ DWORD i_flags;
+ int ret;
+
+ memset( &op, 0, sizeof(MCI_OPEN_PARMS) );
+ op.lpstrDeviceType = (LPCSTR)MCI_DEVTYPE_CD_AUDIO;
+ op.lpstrElementName = _obj->gen.source_name;
+
+ /* Set the flags for the device type */
+ i_flags = MCI_OPEN_TYPE | MCI_OPEN_TYPE_ID |
+ MCI_OPEN_ELEMENT | MCI_OPEN_SHAREABLE;
+
+ if( !mciSendCommand( 0, MCI_OPEN, i_flags, (unsigned long)&op ) ) {
+ st.dwItem = MCI_CDA_STATUS_TYPE_TRACK;
+ st.dwTrack = track_num;
+ i_flags = MCI_TRACK | MCI_STATUS_ITEM ;
+ ret = mciSendCommand( op.wDeviceID, MCI_STATUS, i_flags,
+ (unsigned long) &st );
+
+ /* Release access to the device */
+ mciSendCommand( op.wDeviceID, MCI_CLOSE, MCI_WAIT, 0 );
+
+ switch(st.dwReturn) {
+ case MCI_CDA_TRACK_AUDIO:
+ return TRACK_FORMAT_AUDIO;
+ case MCI_CDA_TRACK_OTHER:
+ return TRACK_FORMAT_DATA;
+ default:
+ return TRACK_FORMAT_XA;
+ }
+ }
+
+ return TRACK_FORMAT_ERROR;
+
+}
+
+/*!
+ Return true if we have XA data (green, mode2 form1) or
+ XA data (green, mode2 form2). That is track begins:
+ sync - header - subheader
+ 12 4 - 8
+
+ FIXME: there's gotta be a better design for this and get_track_format?
+*/
+static bool
+_cdio_get_track_green(void *user_data, track_t track_num)
+{
+ _img_private_t *_obj = user_data;
+
+ if (!_obj->toc_init) _cdio_read_toc (_obj) ;
+
+ if (track_num == CDIO_CDROM_LEADOUT_TRACK) track_num = _obj->total_tracks+1;
+
+ if (track_num > _obj->total_tracks+1 || track_num == 0)
+ return false;
+
+ /* FIXME! */
+ return true;
+}
+
+/*!
+ Return the starting MSF (minutes/secs/frames) for track number
+ track_num in obj. Track numbers start at 1.
+ The "leadout" track is specified either by
+ using track_num LEADOUT_TRACK or the total tracks+1.
+ False is returned if there is no track entry.
+*/
+static bool
+_cdio_get_track_msf(void *user_data, track_t track_num, msf_t *msf)
+{
+ _img_private_t *_obj = user_data;
+
+ if (NULL == msf) return false;
+
+ if (!_obj->toc_init) _cdio_read_toc (_obj) ;
+
+ if (track_num == CDIO_CDROM_LEADOUT_TRACK) track_num = _obj->total_tracks+1;
+
+ if (track_num > _obj->total_tracks+1 || track_num == 0) {
+ return false;
+ } else {
+ cdio_lsn_to_msf(_obj->tocent[track_num-1].start_lsn, msf);
+ return true;
+ }
+}
+
+#endif /* HAVE_WIN32_CDROM */
+
+/*!
+ Return an array of strings giving possible CD devices.
+ */
+char **
+cdio_get_devices_win32 (void)
+{
+#ifndef HAVE_WIN32_CDROM
+ return NULL;
+#else
+ char **drives = NULL;
+ unsigned int num_drives=0;
+ char drive_letter;
+
+ /* Scan the system for CD-ROM drives.
+ */
+
+#if FINISHED
+ /* Now check the currently mounted CD drives */
+ if (NULL != (ret_drive = cdio_check_mounts("/etc/mtab"))) {
+ cdio_add_device_list(&drives, drive, &num_drives);
+ }
+
+ /* Finally check possible mountable drives in /etc/fstab */
+ if (NULL != (ret_drive = cdio_check_mounts("/etc/fstab"))) {
+ cdio_add_device_list(&drives, drive, &num_drives);
+ }
+#endif
+
+ /* Scan the system for CD-ROM drives.
+ Not always 100% reliable, so use the USE_MNTENT code above first.
+ */
+ for (drive_letter='A'; drive_letter <= 'Z'; drive_letter++) {
+ const char *drive_str=cdio_is_cdrom(drive_letter);
+ if (drive_str != NULL) {
+ cdio_add_device_list(&drives, drive_str, &num_drives);
+ }
+ }
+ cdio_add_device_list(&drives, NULL, &num_drives);
+ return drives;
+#endif /*HAVE_WIN32_CDROM*/
+}
+
+/*!
+ Return a string containing the default CD device if none is specified.
+ if CdIo is NULL (we haven't initialized a specific device driver),
+ then find a suitable one and return the default device for that.
+
+ NULL is returned if we couldn't get a default device.
+*/
+char *
+cdio_get_default_device_win32(void)
+{
+
+#ifdef HAVE_WIN32_CDROM
+ char drive_letter;
+
+ for (drive_letter='A'; drive_letter <= 'Z'; drive_letter++) {
+ const char *drive_str=cdio_is_cdrom(drive_letter);
+ if (drive_str != NULL) {
+ return strdup(drive_str);
+ }
+ }
+#endif
+ return NULL;
+}
+
+/*!
+ Return true if source_name could be a device containing a CD-ROM.
+*/
+bool
+cdio_is_device_win32(const char *source_name)
+{
+ unsigned int len;
+ len = strlen(source_name);
+
+ if (NULL == source_name) return false;
+
+#ifdef HAVE_WIN32_CDROM
+ if ( WIN_NT )
+ /* Really should test to see if of form: \\.\x: */
+ return ((len == 6) && isalpha(source_name[len-2])
+ && (source_name[len-1] == ':'));
+ else
+ /* See if is of form: x: */
+ return ((len == 2) && isalpha(source_name[0])
+ && (source_name[len-1] == ':'));
+#else
+ return false;
+#endif
+}
+
+/*!
+ Initialization routine. This is the only thing that doesn't
+ get called via a function pointer. In fact *we* are the
+ ones to set that up.
+ */
+CdIo *
+cdio_open_win32 (const char *source_name)
+{
+
+#ifdef HAVE_WIN32_CDROM
+ CdIo *ret;
+ _img_private_t *_data;
+
+ cdio_funcs _funcs = {
+ .eject_media = _cdio_eject_media,
+ .free = _cdio_win32_free,
+ .get_arg = _cdio_get_arg,
+ .get_default_device = cdio_get_default_device_win32,
+ .get_devices = cdio_get_devices_win32,
+ .get_first_track_num= _cdio_get_first_track_num,
+ .get_mcn = NULL,
+ .get_num_tracks = _cdio_get_num_tracks,
+ .get_track_format = _cdio_get_track_format,
+ .get_track_green = _cdio_get_track_green,
+ .get_track_lba = NULL, /* This could be implemented if need be. */
+ .get_track_msf = _cdio_get_track_msf,
+ .lseek = NULL,
+ .read = NULL,
+ .read_audio_sectors = _cdio_read_audio_sectors,
+ .read_mode2_sector = _cdio_read_mode2_sector,
+ .read_mode2_sectors = _cdio_read_mode2_sectors,
+ .set_arg = _cdio_set_arg,
+ .stat_size = _cdio_stat_size
+ };
+
+ _data = _cdio_malloc (sizeof (_img_private_t));
+ _data->gen.init = false;
+ _data->gen.fd = -1;
+
+ _cdio_set_arg(_data, "source", (NULL == source_name)
+ ? cdio_get_default_device_win32(): source_name);
+
+ ret = cdio_new (_data, &_funcs);
+ if (ret == NULL) return NULL;
+
+ if (_cdio_init_win32(_data))
+ return ret;
+ else {
+ _cdio_win32_free (_data);
+ return NULL;
+ }
+
+#else
+ return NULL;
+#endif /* HAVE_WIN32_CDROM */
+
+}
+
+bool
+cdio_have_win32 (void)
+{
+#ifdef HAVE_WIN32_CDROM
+ return true;
+#else
+ return false;
+#endif /* HAVE_WIN32_CDROM */
+}
diff --git a/src/input/vcd/libcdio/bytesex.h b/src/input/vcd/libcdio/bytesex.h
new file mode 100644
index 000000000..431de9799
--- /dev/null
+++ b/src/input/vcd/libcdio/bytesex.h
@@ -0,0 +1,196 @@
+/*
+ $Id: bytesex.h,v 1.1 2003/10/13 11:47:11 f1rmb Exp $
+
+ Copyright (C) 2000 Herbert Valerio Riedel <hvr@gnu.org>
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+*/
+
+#ifndef __CDIO_BYTESEX_H__
+#define __CDIO_BYTESEX_H__
+
+#include <cdio/logging.h>
+#include <cdio/types.h>
+#include "bytesex_asm.h"
+
+/* generic byteswap routines */
+
+#define UINT16_SWAP_LE_BE_C(val) ((uint16_t) ( \
+ (((uint16_t) (val) & (uint16_t) 0x00ffU) << 8) | \
+ (((uint16_t) (val) & (uint16_t) 0xff00U) >> 8)))
+
+#define UINT32_SWAP_LE_BE_C(val) ((uint32_t) ( \
+ (((uint32_t) (val) & (uint32_t) 0x000000ffU) << 24) | \
+ (((uint32_t) (val) & (uint32_t) 0x0000ff00U) << 8) | \
+ (((uint32_t) (val) & (uint32_t) 0x00ff0000U) >> 8) | \
+ (((uint32_t) (val) & (uint32_t) 0xff000000U) >> 24)))
+
+#define UINT64_SWAP_LE_BE_C(val) ((uint64_t) ( \
+ (((uint64_t) (val) & (uint64_t) UINT64_C(0x00000000000000ff)) << 56) | \
+ (((uint64_t) (val) & (uint64_t) UINT64_C(0x000000000000ff00)) << 40) | \
+ (((uint64_t) (val) & (uint64_t) UINT64_C(0x0000000000ff0000)) << 24) | \
+ (((uint64_t) (val) & (uint64_t) UINT64_C(0x00000000ff000000)) << 8) | \
+ (((uint64_t) (val) & (uint64_t) UINT64_C(0x000000ff00000000)) >> 8) | \
+ (((uint64_t) (val) & (uint64_t) UINT64_C(0x0000ff0000000000)) >> 24) | \
+ (((uint64_t) (val) & (uint64_t) UINT64_C(0x00ff000000000000)) >> 40) | \
+ (((uint64_t) (val) & (uint64_t) UINT64_C(0xff00000000000000)) >> 56)))
+
+#ifndef UINT16_SWAP_LE_BE
+# define UINT16_SWAP_LE_BE UINT16_SWAP_LE_BE_C
+#endif
+
+#ifndef UINT32_SWAP_LE_BE
+# define UINT32_SWAP_LE_BE UINT32_SWAP_LE_BE_C
+#endif
+
+#ifndef UINT64_SWAP_LE_BE
+# define UINT64_SWAP_LE_BE UINT64_SWAP_LE_BE_C
+#endif
+
+inline static
+uint16_t uint16_swap_le_be (const uint16_t val)
+{
+ return UINT16_SWAP_LE_BE (val);
+}
+
+inline static
+uint32_t uint32_swap_le_be (const uint32_t val)
+{
+ return UINT32_SWAP_LE_BE (val);
+}
+
+inline static
+uint64_t uint64_swap_le_be (const uint64_t val)
+{
+ return UINT64_SWAP_LE_BE (val);
+}
+
+# define UINT8_TO_BE(val) ((uint8_t) (val))
+# define UINT8_TO_LE(val) ((uint8_t) (val))
+#ifdef WORDS_BIGENDIAN
+# define UINT16_TO_BE(val) ((uint16_t) (val))
+# define UINT16_TO_LE(val) ((uint16_t) UINT16_SWAP_LE_BE(val))
+
+# define UINT32_TO_BE(val) ((uint32_t) (val))
+# define UINT32_TO_LE(val) ((uint32_t) UINT32_SWAP_LE_BE(val))
+
+# define UINT64_TO_BE(val) ((uint64_t) (val))
+# define UINT64_TO_LE(val) ((uint64_t) UINT64_SWAP_LE_BE(val))
+#else
+# define UINT16_TO_BE(val) ((uint16_t) UINT16_SWAP_LE_BE(val))
+# define UINT16_TO_LE(val) ((uint16_t) (val))
+
+# define UINT32_TO_BE(val) ((uint32_t) UINT32_SWAP_LE_BE(val))
+# define UINT32_TO_LE(val) ((uint32_t) (val))
+
+# define UINT64_TO_BE(val) ((uint64_t) UINT64_SWAP_LE_BE(val))
+# define UINT64_TO_LE(val) ((uint64_t) (val))
+#endif
+
+/* symmetric conversions */
+#define UINT8_FROM_BE(val) (UINT8_TO_BE (val))
+#define UINT8_FROM_LE(val) (UINT8_TO_LE (val))
+#define UINT16_FROM_BE(val) (UINT16_TO_BE (val))
+#define UINT16_FROM_LE(val) (UINT16_TO_LE (val))
+#define UINT32_FROM_BE(val) (UINT32_TO_BE (val))
+#define UINT32_FROM_LE(val) (UINT32_TO_LE (val))
+#define UINT64_FROM_BE(val) (UINT64_TO_BE (val))
+#define UINT64_FROM_LE(val) (UINT64_TO_LE (val))
+
+/* converter function template */
+#define CVT_TO_FUNC(bits) \
+ static inline uint ## bits ## _t \
+ uint ## bits ## _to_be (uint ## bits ## _t val) \
+ { return UINT ## bits ## _TO_BE (val); } \
+ static inline uint ## bits ## _t \
+ uint ## bits ## _to_le (uint ## bits ## _t val) \
+ { return UINT ## bits ## _TO_LE (val); } \
+
+CVT_TO_FUNC(8)
+CVT_TO_FUNC(16)
+CVT_TO_FUNC(32)
+CVT_TO_FUNC(64)
+
+#undef CVT_TO_FUNC
+
+#define uint8_from_be(val) (uint8_to_be (val))
+#define uint8_from_le(val) (uint8_to_le (val))
+#define uint16_from_be(val) (uint16_to_be (val))
+#define uint16_from_le(val) (uint16_to_le (val))
+#define uint32_from_be(val) (uint32_to_be (val))
+#define uint32_from_le(val) (uint32_to_le (val))
+#define uint64_from_be(val) (uint64_to_be (val))
+#define uint64_from_le(val) (uint64_to_le (val))
+
+/* ISO9660 related stuff */
+
+#define to_711(i) uint8_to_le(i)
+#define from_711(i) uint8_from_le(i)
+
+#define to_721(i) uint16_to_le(i)
+#define from_721(i) uint16_from_le(i)
+
+#define to_721(i) uint16_to_le(i)
+#define from_721(i) uint16_from_le(i)
+
+#define to_722(i) uint16_to_be(i)
+#define from_722(i) uint16_from_be(i)
+
+static inline uint32_t
+to_723(uint16_t i)
+{
+ return uint32_swap_le_be(i) | i;
+}
+
+static inline uint16_t
+from_723 (uint32_t p)
+{
+ if (uint32_swap_le_be (p) != p)
+ cdio_warn ("from_723: broken byte order");
+
+ return (0xFFFF & p);
+}
+
+#define to_731(i) uint32_to_le(i)
+#define from_731(i) uint32_from_le(i)
+
+#define to_732(i) uint32_to_be(i)
+#define from_732(i) uint32_from_be(i)
+
+static inline uint64_t
+to_733(uint32_t i)
+{
+ return uint64_swap_le_be(i) | i;
+}
+
+static inline uint32_t
+from_733 (uint64_t p)
+{
+ if (uint64_swap_le_be (p) != p)
+ cdio_warn ("from_733: broken byte order");
+
+ return (UINT32_C(0xFFFFFFFF) & p);
+}
+
+#endif /* __CDIO_BYTESEX_H__ */
+
+
+/*
+ * Local variables:
+ * c-file-style: "gnu"
+ * tab-width: 8
+ * indent-tabs-mode: nil
+ * End:
+ */
diff --git a/src/input/vcd/libcdio/bytesex_asm.h b/src/input/vcd/libcdio/bytesex_asm.h
new file mode 100644
index 000000000..34ef5e605
--- /dev/null
+++ b/src/input/vcd/libcdio/bytesex_asm.h
@@ -0,0 +1,123 @@
+/*
+ $Id: bytesex_asm.h,v 1.1 2003/10/13 11:47:11 f1rmb Exp $
+
+ Copyright (C) 2001 Sven Ottemann <ac-logic@freenet.de>
+ 2001 Herbert Valerio Riedel <hvr@gnu.org>
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+*/
+
+#ifndef __CDIO_BYTESEX_ASM_H__
+#define __CDIO_BYTESEX_ASM_H__
+#if !defined(DISABLE_ASM_OPTIMIZE)
+
+#include <cdio/types.h>
+
+#if defined(__powerpc__) && defined(__GNUC__)
+
+inline static
+uint32_t uint32_swap_le_be_asm(const uint32_t a)
+{
+ uint32_t b;
+
+ __asm__ ("lwbrx %0,0,%1"
+ :"=r"(b)
+ :"r"(&a), "m"(a));
+
+ return b;
+}
+
+inline static
+uint16_t uint16_swap_le_be_asm(const uint16_t a)
+{
+ uint32_t b;
+
+ __asm__ ("lhbrx %0,0,%1"
+ :"=r"(b)
+ :"r"(&a), "m"(a));
+
+ return b;
+}
+
+#define UINT16_SWAP_LE_BE uint16_swap_le_be_asm
+#define UINT32_SWAP_LE_BE uint32_swap_le_be_asm
+
+#elif defined(__mc68000__) && defined(__STORMGCC__)
+
+inline static
+uint32_t uint32_swap_le_be_asm(uint32_t a __asm__("d0"))
+{
+ /* __asm__("rolw #8,%0; swap %0; rolw #8,%0" : "=d" (val) : "0" (val)); */
+
+ __asm__("move.l %1,d0;rol.w #8,d0;swap d0;rol.w #8,d0;move.l d0,%0"
+ :"=r"(a)
+ :"r"(a));
+
+ return(a);
+}
+
+inline static
+uint16_t uint16_swap_le_be_asm(uint16_t a __asm__("d0"))
+{
+ __asm__("move.l %1,d0;rol.w #8,d0;move.l d0,%0"
+ :"=r"(a)
+ :"r"(a));
+
+ return(a);
+}
+
+#define UINT16_SWAP_LE_BE uint16_swap_le_be_asm
+#define UINT32_SWAP_LE_BE uint32_swap_le_be_asm
+
+#elif 0 && defined(__i386__) && defined(__GNUC__)
+
+inline static
+uint32_t uint32_swap_le_be_asm(uint32_t a)
+{
+ __asm__("xchgb %b0,%h0\n\t" /* swap lower bytes */
+ "rorl $16,%0\n\t" /* swap words */
+ "xchgb %b0,%h0" /* swap higher bytes */
+ :"=q" (a)
+ : "0" (a));
+
+ return(a);
+}
+
+inline static
+uint16_t uint16_swap_le_be_asm(uint16_t a)
+{
+ __asm__("xchgb %b0,%h0" /* swap bytes */
+ : "=q" (a)
+ : "0" (a));
+
+ return(a);
+}
+
+#define UINT16_SWAP_LE_BE uint16_swap_le_be_asm
+#define UINT32_SWAP_LE_BE uint32_swap_le_be_asm
+
+#endif
+
+#endif /* !defined(DISABLE_ASM_OPTIMIZE) */
+#endif /* __CDIO_BYTESEX_ASM_H__ */
+
+
+/*
+ * Local variables:
+ * c-file-style: "gnu"
+ * tab-width: 8
+ * indent-tabs-mode: nil
+ * End:
+ */
diff --git a/src/input/vcd/libcdio/cd_types.c b/src/input/vcd/libcdio/cd_types.c
new file mode 100644
index 000000000..0dd6c8410
--- /dev/null
+++ b/src/input/vcd/libcdio/cd_types.c
@@ -0,0 +1,294 @@
+/*
+ $Id: cd_types.c,v 1.1 2003/10/13 11:47:11 f1rmb Exp $
+
+ Copyright (C) 2003 Rocky Bernstein <rocky@panix.com>
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+*/
+/*
+ This tries to determine what kind of CD-image or filesystems on a
+ track we've got.
+*/
+#include "config.h"
+
+#ifdef HAVE_STDIO_H
+#include <stdio.h>
+#endif
+
+#ifdef HAVE_STDLIB_H
+#include <stdlib.h>
+#endif
+
+#ifdef HAVE_UNISTD_H
+#include <unistd.h>
+#endif
+
+#ifdef HAVE_STRING_H
+#include <string.h>
+#endif
+
+#include <cdio/cdio.h>
+#include <cdio/iso9660.h>
+#include <cdio/logging.h>
+#include <cdio/util.h>
+#include <cdio/cd_types.h>
+
+/*
+Subject: -65- How can I read an IRIX (EFS) CD-ROM on a machine which
+ doesn't use EFS?
+Date: 18 Jun 1995 00:00:01 EST
+
+ You want 'efslook', at
+ ftp://viz.tamu.edu/pub/sgi/software/efslook.tar.gz.
+
+and
+! Robert E. Seastrom <rs@access4.digex.net>'s software (with source
+! code) for using an SGI CD-ROM on a Macintosh is at
+! ftp://bifrost.seastrom.com/pub/mac/CDROM-Jumpstart.sit151.hqx.
+
+*/
+
+static char buffer[6][CDIO_CD_FRAMESIZE_RAW]; /* for CD-Data */
+
+/* Some interesting sector numbers stored in the above buffer. */
+#define UFS_SUPERBLOCK_SECTOR 4 /* buffer[2] */
+#define BOOT_SECTOR 17 /* buffer[3] */
+#define VCD_INFO_SECTOR 150 /* buffer[4] */
+
+
+typedef struct signature
+{
+ unsigned int buf_num;
+ unsigned int offset;
+ const char *sig_str;
+ const char *description;
+} signature_t;
+
+static signature_t sigs[] =
+ {
+/*buffer[x] off look for description */
+ {0, 1, ISO_STANDARD_ID, "ISO 9660"},
+ {0, 1, "CD-I", "CD-I"},
+ {0, 8, "CDTV", "CDTV"},
+ {0, 8, "CD-RTOS", "CD-RTOS"},
+ {0, 9, "CDROM", "HIGH SIERRA"},
+ {0, 16, "CD-BRIDGE", "BRIDGE"},
+ {0, ISO_XA_MARKER_OFFSET, ISO_XA_MARKER_STRING, "XA"},
+ {1, 64, "PPPPHHHHOOOOTTTTOOOO____CCCCDDDD", "PHOTO CD"},
+ {1, 0x438, "\x53\xef", "EXT2 FS"},
+ {2, 1372, "\x54\x19\x01\x0", "UFS"},
+ {3, 7, "EL TORITO", "BOOTABLE"},
+ {4, 0, "VIDEO_CD", "VIDEO CD"},
+ {4, 0, "SUPERVCD", "SVCD or Chaoji VCD"},
+ { 0 }
+ };
+
+
+/* The below index into the above sigs array. Make sure things match. */
+#define INDEX_ISOFS 0
+#define INDEX_CD_I 1
+#define INDEX_CDTV 2
+#define INDEX_CD_RTOS 3
+#define INDEX_HS 4
+#define INDEX_BRIDGE 5
+#define INDEX_XA 6
+#define INDEX_PHOTO_CD 7
+#define INDEX_EXT2 8
+#define INDEX_UFS 9
+#define INDEX_BOOTABLE 10
+#define INDEX_VIDEO_CD 11 /* Video CD */
+#define INDEX_SVCD 12 /* CVD *or* SVCD */
+
+
+/*
+ Read a particular block into the global array to be used for further
+ analysis later.
+*/
+static int
+_cdio_read_block(const CdIo *cdio, int superblock, uint32_t offset,
+ uint8_t bufnum, track_t track_num)
+{
+ unsigned int track_sec_count = cdio_get_track_sec_count(cdio, track_num);
+ memset(buffer[bufnum], 0, CDIO_CD_FRAMESIZE);
+
+ if ( track_sec_count < superblock) {
+ cdio_debug("reading block %u skipped track %d has only %u sectors\n",
+ superblock, track_num, track_sec_count);
+ return -1;
+ }
+
+ cdio_debug("about to read sector %lu\n",
+ (long unsigned int) offset+superblock);
+
+ if (cdio_get_track_green(cdio, track_num)) {
+ if (0 > cdio_read_mode2_sector(cdio, buffer[bufnum],
+ offset+superblock, false))
+ return -1;
+ } else {
+ if (0 > cdio_read_mode1_sector(cdio, buffer[bufnum],
+ offset+superblock, false))
+ return -1;
+ }
+
+ return 0;
+}
+
+/*
+ Return true if the previously read-in buffer contains a "signature" that
+ matches index "num".
+ */
+static bool
+_cdio_is_it(int num)
+{
+ signature_t *sigp=&sigs[num];
+ int len=strlen(sigp->sig_str);
+
+ /* TODO: check that num < largest sig. */
+ return 0 == memcmp(&buffer[sigp->buf_num][sigp->offset], sigp->sig_str, len);
+}
+
+static int
+_cdio_is_hfs(void)
+{
+ return (0 == memcmp(&buffer[1][512],"PM",2)) ||
+ (0 == memcmp(&buffer[1][512],"TS",2)) ||
+ (0 == memcmp(&buffer[1][1024], "BD",2));
+}
+
+static int
+_cdio_is_3do(void)
+{
+ return (0 == memcmp(&buffer[1][0],"\x01\x5a\x5a\x5a\x5a\x5a\x01", 7)) &&
+ (0 == memcmp(&buffer[1][40], "CD-ROM", 6));
+}
+
+static int
+_cdio_is_joliet(void)
+{
+ return 2 == buffer[3][0] && buffer[3][88] == 0x25 && buffer[3][89] == 0x2f;
+}
+
+/* ISO 9660 volume space in M2F1_SECTOR_SIZE byte units */
+static int
+_cdio_get_iso9660_fs_sec_count(void)
+{
+ return ((buffer[0][80] & 0xff) |
+ ((buffer[0][81] & 0xff) << 8) |
+ ((buffer[0][82] & 0xff) << 16) |
+ ((buffer[0][83] & 0xff) << 24));
+}
+
+static int
+_cdio_get_joliet_level( void )
+{
+ switch (buffer[3][90]) {
+ case 0x40: return 1;
+ case 0x43: return 2;
+ case 0x45: return 3;
+ }
+ return 0;
+}
+
+/*
+ Try to determine what kind of CD-image and/or filesystem we
+ have at track track_num. Return information about the CD image
+ is returned in cdio_analysis and the return value.
+*/
+cdio_fs_anal_t
+cdio_guess_cd_type(const CdIo *cdio, int start_session, track_t track_num,
+ /*out*/ cdio_analysis_t *cdio_analysis)
+{
+ int ret = 0;
+ bool sector0_read_ok;
+
+ if (TRACK_FORMAT_AUDIO == cdio_get_track_format(cdio, track_num))
+ return CDIO_FS_AUDIO;
+
+ if ( _cdio_read_block(cdio, ISO_PVD_SECTOR, start_session,
+ 0, track_num) < 0 )
+ return CDIO_FS_UNKNOWN;
+
+ /* We have something that smells of a filesystem. */
+ if (_cdio_is_it(INDEX_CD_I) && _cdio_is_it(INDEX_CD_RTOS)
+ && !_cdio_is_it(INDEX_BRIDGE) && !_cdio_is_it(INDEX_XA)) {
+ return CDIO_FS_INTERACTIVE;
+ } else {
+ /* read sector 0 ONLY, when NO greenbook CD-I !!!! */
+
+ sector0_read_ok =
+ _cdio_read_block(cdio, 0, start_session, 1, track_num) == 0;
+
+ if (_cdio_is_it(INDEX_HS))
+ ret |= CDIO_FS_HIGH_SIERRA;
+ else if (_cdio_is_it(INDEX_ISOFS)) {
+ if (_cdio_is_it(INDEX_CD_RTOS) && _cdio_is_it(INDEX_BRIDGE))
+ ret = CDIO_FS_ISO_9660_INTERACTIVE;
+ else if (_cdio_is_hfs())
+ ret = CDIO_FS_ISO_HFS;
+ else
+ ret = CDIO_FS_ISO_9660;
+ cdio_analysis->isofs_size = _cdio_get_iso9660_fs_sec_count();
+ sprintf(cdio_analysis->iso_label, buffer[0]+40);
+
+#if 0
+ if (_cdio_is_rockridge())
+ ret |= CDIO_FS_ANAL_ROCKRIDGE;
+#endif
+
+ if (_cdio_read_block(cdio, BOOT_SECTOR, start_session, 3, track_num) < 0)
+ return ret;
+
+ if (_cdio_is_joliet()) {
+ cdio_analysis->joliet_level = _cdio_get_joliet_level();
+ ret |= CDIO_FS_ANAL_JOLIET;
+ }
+ if (_cdio_is_it(INDEX_BOOTABLE))
+ ret |= CDIO_FS_ANAL_BOOTABLE;
+
+ if ( _cdio_is_it(INDEX_XA) && _cdio_is_it(INDEX_ISOFS)
+ && !(sector0_read_ok && _cdio_is_it(INDEX_PHOTO_CD)) ) {
+
+ if ( _cdio_read_block(cdio, VCD_INFO_SECTOR, start_session, 4,
+ track_num) < 0 )
+ return ret;
+
+ if (_cdio_is_it(INDEX_BRIDGE) && _cdio_is_it(INDEX_CD_RTOS)) {
+ if (_cdio_is_it(INDEX_VIDEO_CD)) ret |= CDIO_FS_ANAL_VIDEOCD;
+ else if (_cdio_is_it(INDEX_SVCD)) ret |= CDIO_FS_ANAL_SVCD;
+ } else if (_cdio_is_it(INDEX_SVCD)) ret |= CDIO_FS_ANAL_CVD;
+
+ }
+ }
+ else if (_cdio_is_hfs()) ret |= CDIO_FS_HFS;
+ else if (sector0_read_ok && _cdio_is_it(INDEX_EXT2)) ret |= CDIO_FS_EXT2;
+ else if (_cdio_is_3do()) ret |= CDIO_FS_3DO;
+ else {
+ if ( _cdio_read_block(cdio, UFS_SUPERBLOCK_SECTOR, start_session, 2,
+ track_num) < 0 )
+ return ret;
+
+ if (sector0_read_ok && _cdio_is_it(INDEX_UFS))
+ ret |= CDIO_FS_UFS;
+ else
+ ret |= CDIO_FS_UNKNOWN;
+ }
+ }
+
+ /* other checks */
+ if (_cdio_is_it(INDEX_XA)) ret |= CDIO_FS_ANAL_XA;
+ if (_cdio_is_it(INDEX_PHOTO_CD)) ret |= CDIO_FS_ANAL_PHOTO_CD;
+ if (_cdio_is_it(INDEX_CDTV)) ret |= CDIO_FS_ANAL_CDTV;
+ return ret;
+}
diff --git a/src/input/vcd/libcdio/cdio.c b/src/input/vcd/libcdio/cdio.c
new file mode 100644
index 000000000..8d6c39bed
--- /dev/null
+++ b/src/input/vcd/libcdio/cdio.c
@@ -0,0 +1,887 @@
+/*
+ $Id: cdio.c,v 1.1 2003/10/13 11:47:11 f1rmb Exp $
+
+ Copyright (C) 2003 Rocky Bernstein <rocky@panix.com>
+ Copyright (C) 2001 Herbert Valerio Riedel <hvr@gnu.org>
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+*/
+
+
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
+
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <unistd.h>
+#include <errno.h>
+#include <string.h>
+
+#include "cdio_assert.h"
+#include <cdio/cdio.h>
+#include <cdio/cd_types.h>
+#include <cdio/util.h>
+#include <cdio/logging.h>
+#include "cdio_private.h"
+
+static const char _rcsid[] = "$Id: cdio.c,v 1.1 2003/10/13 11:47:11 f1rmb Exp $";
+
+
+const char *track_format2str[6] =
+ {
+ "audio", "CD-i", "XA", "data", "PSX", "error"
+ };
+
+/* The below array gives of the drivers that are currently available for
+ on a particular host. */
+
+CdIo_driver_t CdIo_driver[CDIO_MAX_DRIVER] = { {0} };
+
+/* The last valid entry of Cdio_driver.
+ -1 or (CDIO_DRIVER_UNINIT) means uninitialzed.
+ -2 means some sort of error.
+*/
+
+#define CDIO_DRIVER_UNINIT -1
+int CdIo_last_driver = CDIO_DRIVER_UNINIT;
+
+static bool
+cdio_have_false(void)
+{
+ return false;
+}
+
+
+/* The below array gives all drivers that can possibly appear.
+ on a particular host. */
+
+CdIo_driver_t CdIo_all_drivers[CDIO_MAX_DRIVER+1] = {
+ {DRIVER_UNKNOWN,
+ 0,
+ "Unknown",
+ "No driver",
+ &cdio_have_false,
+ NULL,
+ NULL,
+ NULL,
+ NULL
+ },
+
+ {DRIVER_BSDI,
+ CDIO_SRC_IS_DEVICE_MASK|CDIO_SRC_IS_NATIVE_MASK|CDIO_SRC_IS_SCSI_MASK,
+ "BSDI",
+ "BSDI ATAPI and SCSI driver",
+ &cdio_have_bsdi,
+ &cdio_open_bsdi,
+ &cdio_get_default_device_bsdi,
+ &cdio_is_device_generic,
+ &cdio_get_devices_bsdi
+ },
+
+ {DRIVER_FREEBSD,
+ CDIO_SRC_IS_DEVICE_MASK|CDIO_SRC_IS_NATIVE_MASK|CDIO_SRC_IS_SCSI_MASK,
+ "FreeBSD",
+ "FreeBSD driver",
+ &cdio_have_freebsd,
+ &cdio_open_freebsd,
+ &cdio_get_default_device_freebsd,
+ &cdio_is_device_generic,
+ NULL
+ },
+
+ {DRIVER_LINUX,
+ CDIO_SRC_IS_DEVICE_MASK|CDIO_SRC_IS_NATIVE_MASK,
+ "GNU/Linux",
+ "GNU/Linux ioctl and MMC driver",
+ &cdio_have_linux,
+ &cdio_open_linux,
+ &cdio_get_default_device_linux,
+ &cdio_is_device_generic,
+ &cdio_get_devices_linux
+ },
+
+ {DRIVER_SOLARIS,
+ CDIO_SRC_IS_DEVICE_MASK|CDIO_SRC_IS_NATIVE_MASK|CDIO_SRC_IS_SCSI_MASK,
+ "Solaris",
+ "Solaris ATAPI and SCSI driver",
+ &cdio_have_solaris,
+ &cdio_open_solaris,
+ &cdio_get_default_device_solaris,
+ &cdio_is_device_generic,
+ &cdio_get_devices_solaris
+ },
+
+ {DRIVER_OSX,
+ CDIO_SRC_IS_DEVICE_MASK|CDIO_SRC_IS_NATIVE_MASK|CDIO_SRC_IS_SCSI_MASK,
+ "OS X",
+ "Apple Darwin OS X driver",
+ &cdio_have_osx,
+ &cdio_open_osx,
+ &cdio_get_default_device_osx,
+ &cdio_is_device_generic,
+ &cdio_get_devices_osx
+ },
+
+ {DRIVER_WIN32,
+ CDIO_SRC_IS_DEVICE_MASK|CDIO_SRC_IS_NATIVE_MASK|CDIO_SRC_IS_SCSI_MASK,
+ "WIN32",
+ "Windows 32-bit ASPI and winNT/2K/XP ioctl driver",
+ &cdio_have_win32,
+ &cdio_open_win32,
+ &cdio_get_default_device_win32,
+ &cdio_is_device_win32,
+ &cdio_get_devices_win32
+ },
+
+ {DRIVER_BINCUE,
+ CDIO_SRC_IS_DISK_IMAGE_MASK,
+ "BIN/CUE",
+ "bin/cuesheet disk image driver",
+ &cdio_have_bincue,
+ &cdio_open_bincue,
+ &cdio_get_default_device_bincue,
+ NULL,
+ &cdio_get_devices_bincue
+ },
+
+ {DRIVER_NRG,
+ CDIO_SRC_IS_DISK_IMAGE_MASK,
+ "NRG",
+ "Nero NRG disk image driver",
+ &cdio_have_nrg,
+ &cdio_open_nrg,
+ &cdio_get_default_device_nrg,
+ NULL,
+ &cdio_get_devices_nrg
+ }
+
+};
+
+static CdIo *
+scan_for_driver(driver_id_t start, driver_id_t end, const char *source_name)
+{
+ driver_id_t driver_id;
+
+ for (driver_id=start; driver_id<=end; driver_id++) {
+ if ((*CdIo_all_drivers[driver_id].have_driver)()) {
+ CdIo *ret=(*CdIo_all_drivers[driver_id].driver_open)(source_name);
+ if (ret != NULL) {
+ ret->driver_id = driver_id;
+ return ret;
+ }
+ }
+ }
+ return NULL;
+}
+
+const char *
+cdio_driver_describe(driver_id_t driver_id)
+{
+ return CdIo_all_drivers[driver_id].describe;
+}
+
+/*!
+ Eject media in CD drive if there is a routine to do so.
+ Return 0 if success and 1 for failure, and 2 if no routine.
+ If the CD is ejected *obj is freed and obj set to NULL.
+ */
+int
+cdio_eject_media (CdIo **obj)
+{
+
+ if ((obj == NULL) || (*obj == NULL)) return 1;
+
+ if ((*obj)->op.eject_media) {
+ int ret = (*obj)->op.eject_media ((*obj)->env);
+ if (0 == ret) {
+ cdio_destroy(*obj);
+ *obj = NULL;
+ }
+ return ret;
+ } else {
+ cdio_destroy(*obj);
+ *obj = NULL;
+ return 2;
+ }
+}
+
+/*!
+ Free device list returned by cdio_get_devices or
+ cdio_get_devices_with_cap.
+*/
+void cdio_free_device_list (char * device_list[])
+{
+ if (NULL == device_list) return;
+ for ( ; *device_list != NULL ; device_list++ )
+ free(*device_list);
+}
+
+
+/*!
+ Return the value associatied with key. NULL is returned if obj is NULL
+ or "key" does not exist.
+ */
+const char *
+cdio_get_arg (const CdIo *obj, const char key[])
+{
+ if (obj == NULL) return NULL;
+
+ if (obj->op.get_arg) {
+ return obj->op.get_arg (obj->env, key);
+ } else {
+ return NULL;
+ }
+}
+
+/*!
+ Return a string containing the default CD device if none is specified.
+ if CdIo is NULL (we haven't initialized a specific device driver),
+ then find a suitable one and return the default device for that.
+
+ NULL is returned if we couldn't get a default device.
+ */
+char *
+cdio_get_default_device (const CdIo *obj)
+{
+ if (obj == NULL) {
+ driver_id_t driver_id;
+ /* Scan for driver */
+ for (driver_id=DRIVER_UNKNOWN; driver_id<=CDIO_MAX_DRIVER; driver_id++) {
+ if ( (*CdIo_all_drivers[driver_id].have_driver)() &&
+ *CdIo_all_drivers[driver_id].get_default_device ) {
+ return (*CdIo_all_drivers[driver_id].get_default_device)();
+ }
+ }
+ return NULL;
+ }
+
+ if (obj->op.get_default_device) {
+ return obj->op.get_default_device ();
+ } else {
+ return NULL;
+ }
+}
+
+/*!Return an array of device names. If you want a specific
+ devices, dor a driver give that device, if you want hardware
+ devices, give DRIVER_DEVICE and if you want all possible devices,
+ image drivers and hardware drivers give DRIVER_UNKNOWN.
+
+ NULL is returned if we couldn't return a list of devices.
+*/
+char **
+cdio_get_devices (driver_id_t driver_id)
+{
+ CdIo *cdio;
+
+ switch (driver_id) {
+ /* FIXME: spit out unknown to give image drivers as well. */
+ case DRIVER_UNKNOWN:
+ case DRIVER_DEVICE:
+ cdio = scan_for_driver(DRIVER_UNKNOWN, CDIO_MAX_DRIVER, NULL);
+ break;
+ default:
+ return (*CdIo_all_drivers[driver_id].get_devices)();
+ }
+
+ if (cdio == NULL) return NULL;
+ if (cdio->op.get_devices) {
+ return cdio->op.get_devices ();
+ } else {
+ return NULL;
+ }
+}
+
+/*!
+ Return an array of device names in search_devices that have at
+ least the capabilities listed by cap. If search_devices is NULL,
+ then we'll search all possible CD drives.
+
+ If "any" is set false then every capability listed in the extended
+ portion of capabilities (i.e. not the basic filesystem) must be
+ satisified. If "any" is set true, then if any of the capabilities
+ matches, we call that a success.
+
+ To find a CD-drive of any type, use the mask CDIO_FS_MATCH_ALL.
+
+ NULL is returned if we couldn't get a default device.
+*/
+char **
+cdio_get_devices_with_cap (char* search_devices[],
+ cdio_fs_anal_t capabilities, bool any)
+{
+ char **drives=search_devices;
+ char **drives_ret=NULL;
+ int num_drives=0;
+
+ if (NULL == drives) drives=cdio_get_devices(DRIVER_DEVICE);
+ if (NULL == drives) return NULL;
+
+ if (capabilities == CDIO_FS_MATCH_ALL) {
+ /* Duplicate drives into drives_ret. */
+ for( ; *drives != NULL; drives++ ) {
+ cdio_add_device_list(&drives_ret, *drives, &num_drives);
+ }
+ } else {
+ cdio_fs_anal_t got_fs=0;
+ cdio_fs_anal_t need_fs = CDIO_FSTYPE(capabilities);
+ cdio_fs_anal_t need_fs_ext;
+ need_fs_ext = capabilities & ~CDIO_FS_MASK;
+
+ for( ; *drives != NULL; drives++ ) {
+ CdIo *cdio = cdio_open(*drives, DRIVER_UNKNOWN);
+
+ if (NULL != cdio) {
+ track_t first_track = cdio_get_first_track_num(cdio);
+ cdio_analysis_t cdio_analysis;
+ got_fs = cdio_guess_cd_type(cdio, 0, first_track,
+ &cdio_analysis);
+ /* Match on fs and add */
+ if ( (CDIO_FS_UNKNOWN == need_fs || CDIO_FSTYPE(got_fs) == need_fs) )
+ {
+ bool doit = any
+ ? (got_fs & need_fs_ext) != 0
+ : (got_fs | ~need_fs_ext) == -1;
+ if (doit)
+ cdio_add_device_list(&drives_ret, *drives, &num_drives);
+ }
+
+ cdio_destroy(cdio);
+ }
+ }
+ cdio_add_device_list(&drives_ret, NULL, &num_drives);
+ }
+ return drives_ret;
+}
+
+/*!
+ Return a string containing the name of the driver in use.
+ if CdIo is NULL (we haven't initialized a specific device driver),
+ then return NULL.
+*/
+const char *
+cdio_get_driver_name (const CdIo *cdio)
+{
+ return CdIo_all_drivers[cdio->driver_id].name;
+}
+
+
+/*!
+ Return the number of of the first track.
+ CDIO_INVALID_TRACK is returned on error.
+*/
+track_t
+cdio_get_first_track_num (const CdIo *cdio)
+{
+ cdio_assert (cdio != NULL);
+
+ if (cdio->op.get_first_track_num) {
+ return cdio->op.get_first_track_num (cdio->env);
+ } else {
+ return CDIO_INVALID_TRACK;
+ }
+}
+
+/*!
+ Return a string containing the name of the driver in use.
+ if CdIo is NULL (we haven't initialized a specific device driver),
+ then return NULL.
+*/
+char *
+cdio_get_mcn (const CdIo *cdio)
+{
+ if (cdio->op.get_mcn) {
+ return cdio->op.get_mcn (cdio->env);
+ } else {
+ return NULL;
+ }
+}
+
+/*!
+ Return the number of tracks in the current medium.
+ CDIO_INVALID_TRACK is returned on error.
+*/
+track_t
+cdio_get_num_tracks (const CdIo *cdio)
+{
+ if (cdio == NULL) return CDIO_INVALID_TRACK;
+
+ if (cdio->op.get_num_tracks) {
+ return cdio->op.get_num_tracks (cdio->env);
+ } else {
+ return CDIO_INVALID_TRACK;
+ }
+}
+
+/*!
+ Get format of track.
+*/
+track_format_t
+cdio_get_track_format(const CdIo *cdio, track_t track_num)
+{
+ cdio_assert (cdio != NULL);
+
+ if (cdio->op.get_track_format) {
+ return cdio->op.get_track_format (cdio->env, track_num);
+ } else {
+ return TRACK_FORMAT_ERROR;
+ }
+}
+
+/*!
+ Return true if we have XA data (green, mode2 form1) or
+ XA data (green, mode2 form2). That is track begins:
+ sync - header - subheader
+ 12 4 - 8
+
+ FIXME: there's gotta be a better design for this and get_track_format?
+*/
+bool
+cdio_get_track_green(const CdIo *cdio, track_t track_num)
+{
+ cdio_assert (cdio != NULL);
+
+ if (cdio->op.get_track_green) {
+ return cdio->op.get_track_green (cdio->env, track_num);
+ } else {
+ return false;
+ }
+}
+
+/*!
+ Return the starting LBA for track number
+ track_num in cdio. Tracks numbers start at 1.
+ The "leadout" track is specified either by
+ using track_num LEADOUT_TRACK or the total tracks+1.
+ CDIO_INVALID_LBA is returned on error.
+*/
+lba_t
+cdio_get_track_lba(const CdIo *cdio, track_t track_num)
+{
+ if (cdio == NULL) return CDIO_INVALID_LBA;
+
+ if (cdio->op.get_track_lba) {
+ return cdio->op.get_track_lba (cdio->env, track_num);
+ } else {
+ msf_t msf;
+ if (cdio->op.get_track_msf)
+ if (cdio_get_track_msf(cdio, track_num, &msf))
+ return cdio_msf_to_lba(&msf);
+ return CDIO_INVALID_LBA;
+ }
+}
+
+/*!
+ Return the starting LSN for track number
+ track_num in cdio. Tracks numbers start at 1.
+ The "leadout" track is specified either by
+ using track_num LEADOUT_TRACK or the total tracks+1.
+ CDIO_INVALID_LBA is returned on error.
+*/
+lsn_t
+cdio_get_track_lsn(const CdIo *cdio, track_t track_num)
+{
+ if (cdio == NULL) return CDIO_INVALID_LBA;
+
+ if (cdio->op.get_track_lba) {
+ return cdio_lba_to_lsn(cdio->op.get_track_lba (cdio->env, track_num));
+ } else {
+ msf_t msf;
+ if (cdio_get_track_msf(cdio, track_num, &msf))
+ return cdio_msf_to_lsn(&msf);
+ return CDIO_INVALID_LSN;
+ }
+}
+
+/*!
+ Return the starting MSF (minutes/secs/frames) for track number
+ track_num in cdio. Track numbers start at 1.
+ The "leadout" track is specified either by
+ using track_num LEADOUT_TRACK or the total tracks+1.
+ False is returned if there is no track entry.
+*/
+bool
+cdio_get_track_msf(const CdIo *cdio, track_t track_num, /*out*/ msf_t *msf)
+{
+ cdio_assert (cdio != NULL);
+
+ if (cdio->op.get_track_msf) {
+ return cdio->op.get_track_msf (cdio->env, track_num, msf);
+ } else if (cdio->op.get_track_lba) {
+ lba_t lba = cdio->op.get_track_lba (cdio->env, track_num);
+ if (lba == CDIO_INVALID_LBA) return false;
+ cdio_lba_to_msf(lba, msf);
+ return true;
+ } else {
+ return false;
+ }
+}
+
+/*!
+ Return the number of sectors between this track an the next. This
+ includes any pregap sectors before the start of the next track.
+ Tracks start at 1.
+ 0 is returned if there is an error.
+*/
+unsigned int
+cdio_get_track_sec_count(const CdIo *cdio, track_t track_num)
+{
+ track_t num_tracks = cdio_get_num_tracks(cdio);
+
+ if (track_num >=1 && track_num <= num_tracks)
+ return ( cdio_get_track_lba(cdio, track_num+1)
+ - cdio_get_track_lba(cdio, track_num) );
+ return 0;
+}
+
+bool
+cdio_have_driver(driver_id_t driver_id)
+{
+ return (*CdIo_all_drivers[driver_id].have_driver)();
+}
+
+bool
+cdio_is_device(const char *source_name, driver_id_t driver_id)
+{
+ if (CdIo_all_drivers[driver_id].is_device == NULL) return false;
+ return (*CdIo_all_drivers[driver_id].is_device)(source_name);
+}
+
+
+/*!
+ Initialize CD Reading and control routines. Should be called first.
+ May be implicitly called by other routines if not called first.
+*/
+bool
+cdio_init(void)
+{
+
+ CdIo_driver_t *all_dp;
+ CdIo_driver_t *dp = CdIo_driver;
+ driver_id_t driver_id;
+
+ if (CdIo_last_driver != CDIO_DRIVER_UNINIT) {
+ cdio_warn ("Init routine called more than once.");
+ return false;
+ }
+
+ for (driver_id=DRIVER_UNKNOWN; driver_id<=CDIO_MAX_DRIVER; driver_id++) {
+ all_dp = &CdIo_all_drivers[driver_id];
+ if ((*CdIo_all_drivers[driver_id].have_driver)()) {
+ *dp++ = *all_dp;
+ CdIo_last_driver++;
+ }
+ }
+
+ return true;
+}
+
+CdIo *
+cdio_new (void *env, const cdio_funcs *funcs)
+{
+ CdIo *new_cdio;
+
+ new_cdio = _cdio_malloc (sizeof (CdIo));
+
+ new_cdio->env = env;
+ new_cdio->op = *funcs;
+
+ return new_cdio;
+}
+
+/*!
+ Free any resources associated with cdio.
+*/
+void
+cdio_destroy (CdIo *cdio)
+{
+ CdIo_last_driver = CDIO_DRIVER_UNINIT;
+ if (cdio == NULL) return;
+
+ if (cdio->op.free != NULL)
+ cdio->op.free (cdio->env);
+ free (cdio);
+}
+
+/*!
+ lseek - reposition read/write file offset
+ Returns (off_t) -1 on error.
+ Similar to (if not the same as) libc's lseek()
+*/
+off_t
+cdio_lseek (const CdIo *cdio, off_t offset, int whence)
+{
+ if (cdio == NULL) return -1;
+
+ if (cdio->op.lseek)
+ return cdio->op.lseek (cdio->env, offset, whence);
+ return -1;
+}
+
+/*!
+ Reads into buf the next size bytes.
+ Returns -1 on error.
+ Similar to (if not the same as) libc's read()
+*/
+ssize_t
+cdio_read (const CdIo *cdio, void *buf, size_t size)
+{
+ if (cdio == NULL) return -1;
+
+ if (cdio->op.read)
+ return cdio->op.read (cdio->env, buf, size);
+ return -1;
+}
+
+int
+cdio_read_audio_sector (const CdIo *cdio, void *buf, lsn_t lsn)
+{
+ cdio_assert (cdio != NULL);
+ cdio_assert (buf != NULL);
+
+ if (cdio->op.read_audio_sectors != NULL)
+ return cdio->op.read_audio_sectors (cdio->env, buf, lsn, 1);
+ return -1;
+}
+
+int
+cdio_read_audio_sectors (const CdIo *cdio, void *buf, lsn_t lsn,
+ unsigned int nblocks)
+{
+ cdio_assert (cdio != NULL);
+ cdio_assert (buf != NULL);
+
+ if (cdio->op.read_audio_sectors != NULL)
+ return cdio->op.read_audio_sectors (cdio->env, buf, lsn, nblocks);
+ return -1;
+}
+
+/*!
+ Reads a single mode1 form1 or form2 sector from cd device
+ into data starting from lsn. Returns 0 if no error.
+ */
+int
+cdio_read_mode1_sector (const CdIo *cdio, void *data, lsn_t lsn, bool is_form2)
+{
+ uint32_t size = is_form2 ? M2RAW_SECTOR_SIZE : CDIO_CD_FRAMESIZE ;
+ char buf[M2RAW_SECTOR_SIZE] = { 0, };
+ int ret;
+
+ cdio_assert (cdio != NULL);
+ cdio_assert (data != NULL);
+
+ if (cdio->op.lseek && cdio->op.read) {
+ if (0 > cdio_lseek(cdio, CDIO_CD_FRAMESIZE*lsn, SEEK_SET))
+ return -1;
+ if (0 > cdio_read(cdio, buf, CDIO_CD_FRAMESIZE))
+ return -1;
+ memcpy (data, buf, size);
+ return 0;
+ } else {
+ ret = cdio_read_mode2_sector(cdio, data, lsn, is_form2);
+ if (ret == 0)
+ memcpy (data, buf+CDIO_CD_SUBHEADER_SIZE, size);
+ }
+ return ret;
+
+}
+
+int
+cdio_read_mode1_sectors (const CdIo *cdio, void *data, lsn_t lsn,
+ bool is_form2, unsigned int num_sectors)
+{
+ uint32_t size = is_form2 ? M2RAW_SECTOR_SIZE : CDIO_CD_FRAMESIZE ;
+ int retval;
+ int i;
+
+ cdio_assert (cdio != NULL);
+
+ for (i = 0; i < num_sectors; i++) {
+ if ( (retval = cdio_read_mode1_sector (cdio,
+ ((char *)data) + (size * i),
+ lsn + i, is_form2)) )
+ return retval;
+ }
+ return 0;
+}
+
+/*!
+ Reads a single mode2 sector from cd device into data starting
+ from lsn. Returns 0 if no error.
+ */
+int
+cdio_read_mode2_sector (const CdIo *cdio, void *buf, lsn_t lsn,
+ bool is_form2)
+{
+ cdio_assert (cdio != NULL);
+ cdio_assert (buf != NULL);
+ cdio_assert (cdio->op.read_mode2_sector != NULL
+ || cdio->op.read_mode2_sectors != NULL);
+
+ if (cdio->op.read_mode2_sector)
+ return cdio->op.read_mode2_sector (cdio->env, buf, lsn, is_form2);
+
+ /* fallback */
+ if (cdio->op.read_mode2_sectors != NULL)
+ return cdio_read_mode2_sectors (cdio, buf, lsn, is_form2, 1);
+ return 1;
+}
+
+int
+cdio_read_mode2_sectors (const CdIo *cdio, void *buf, lsn_t lsn, bool mode2raw,
+ unsigned num_sectors)
+{
+ cdio_assert (cdio != NULL);
+ cdio_assert (buf != NULL);
+ cdio_assert (cdio->op.read_mode2_sectors != NULL);
+
+ return cdio->op.read_mode2_sectors (cdio->env, buf, lsn,
+ mode2raw, num_sectors);
+}
+
+uint32_t
+cdio_stat_size (const CdIo *cdio)
+{
+ cdio_assert (cdio != NULL);
+
+ return cdio->op.stat_size (cdio->env);
+}
+
+/*!
+ Set the arg "key" with "value" in the source device.
+*/
+int
+cdio_set_arg (CdIo *cdio, const char key[], const char value[])
+{
+ cdio_assert (cdio != NULL);
+ cdio_assert (cdio->op.set_arg != NULL);
+ cdio_assert (key != NULL);
+
+ return cdio->op.set_arg (cdio->env, key, value);
+}
+
+/*! Sets up to read from place specified by source_name and
+ driver_id. This should be called before using any other routine,
+ except cdio_init. This will call cdio_init, if that hasn't been
+ done previously.
+
+ NULL is returned on error.
+*/
+CdIo *
+cdio_open (const char *orig_source_name, driver_id_t driver_id)
+{
+ char *source_name;
+
+ if (CdIo_last_driver == -1) cdio_init();
+
+ if (NULL == orig_source_name || strlen(orig_source_name)==0)
+ source_name = cdio_get_default_device(NULL);
+ else
+ source_name = strdup(orig_source_name);
+
+ retry:
+ switch (driver_id) {
+ case DRIVER_UNKNOWN:
+ {
+ CdIo *cdio=scan_for_driver(CDIO_MIN_DRIVER, CDIO_MAX_DRIVER,
+ source_name);
+ if (cdio != NULL && cdio_is_device(source_name, cdio->driver_id)) {
+ driver_id = cdio->driver_id;
+ } else {
+ struct stat buf;
+ if (0 != stat(source_name, &buf)) {
+ return NULL;
+ }
+ if (S_ISREG(buf.st_mode)) {
+ /* FIXME: check to see if is a text file. If so, then
+ set SOURCE_CUE. */
+ int i=strlen(source_name)-strlen("bin");
+ if (i > 0
+ && ( (source_name)[i] =='n' || (source_name)[i] =='N' )
+ && ( (source_name)[i+1] =='r' || (source_name)[i+1] =='R' )
+ && ( (source_name)[i+2] =='g' || (source_name)[i+2] =='G' ) )
+ driver_id = DRIVER_NRG;
+ else if (i > 0
+ && ( (source_name)[i] =='c' || (source_name)[i] =='C')
+ && ( (source_name)[i+1] =='u' || (source_name)[i+1] =='U')
+ && ( (source_name)[i+2] =='e' || (source_name)[i+2] =='E') )
+ driver_id = DRIVER_BINCUE;
+ else
+ driver_id = DRIVER_BINCUE;
+ } else {
+ cdio_destroy(cdio);
+ return NULL;
+ }
+
+ }
+ cdio_destroy(cdio);
+ goto retry;
+ }
+ case DRIVER_DEVICE:
+ {
+ /* Scan for a driver. */
+ CdIo *ret = cdio_open_cd(source_name);
+ free(source_name);
+ return ret;
+ }
+ break;
+ case DRIVER_BSDI:
+ case DRIVER_FREEBSD:
+ case DRIVER_LINUX:
+ case DRIVER_SOLARIS:
+ case DRIVER_WIN32:
+ case DRIVER_OSX:
+ case DRIVER_NRG:
+ case DRIVER_BINCUE:
+ if ((*CdIo_all_drivers[driver_id].have_driver)()) {
+ CdIo *ret = (*CdIo_all_drivers[driver_id].driver_open)(source_name);
+ if (ret) ret->driver_id = driver_id;
+ return ret;
+ }
+ }
+
+ free(source_name);
+ return NULL;
+}
+
+
+/* In the future we'll have more complicated code to allow selection
+ of an I/O routine as well as code to find an appropriate default
+ routine among the "registered" routines. Possibly classes too
+ disk-based, SCSI-based, native-based, vendor (e.g. Sony, or
+ Plextor) based
+
+ For now though, we'll start more simply...
+*/
+CdIo *
+cdio_open_cd (const char *source_name)
+{
+ if (CdIo_last_driver == -1) cdio_init();
+
+ /* Scan for a driver. */
+ return scan_for_driver(CDIO_MIN_DEVICE_DRIVER, CDIO_MAX_DEVICE_DRIVER,
+ source_name);
+}
+
+
+
+/*
+ * Local variables:
+ * c-file-style: "gnu"
+ * tab-width: 8
+ * indent-tabs-mode: nil
+ * End:
+ */
diff --git a/src/input/vcd/libcdio/cdio/Makefile.am b/src/input/vcd/libcdio/cdio/Makefile.am
new file mode 100644
index 000000000..e2564e9bd
--- /dev/null
+++ b/src/input/vcd/libcdio/cdio/Makefile.am
@@ -0,0 +1,12 @@
+noinst_HEADERS = cdio.h cd_types.h iso9660.h logging.h sector.h types.h util.h version.h xa.h
+
+debug:
+install-debug: install
+
+mostlyclean-generic:
+ -rm -f *~ \#* .*~ .\#*
+
+maintainer-clean-generic:
+ -@echo "This command is intended for maintainers to use;"
+ -@echo "it deletes files that may require special tools to rebuild."
+ -rm -f Makefile.in
diff --git a/src/input/vcd/libcdio/cdio/cd_types.h b/src/input/vcd/libcdio/cdio/cd_types.h
new file mode 100644
index 000000000..91b36453c
--- /dev/null
+++ b/src/input/vcd/libcdio/cdio/cd_types.h
@@ -0,0 +1,114 @@
+/*
+ $Id: cd_types.h,v 1.1 2003/10/13 11:47:12 f1rmb Exp $
+
+ Copyright (C) 2003 Rocky Bernstein <rocky@panix.com>
+ Copyright (C) 1996,1997,1998 Gerd Knorr <kraxel@bytesex.org>
+ and Heiko Eißfeldt <heiko@hexco.de>
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+*/
+
+/*
+ Filesystem types we understand. The highest-numbered fs type should
+ be less than CDIO_FS_MASK defined below.
+*/
+
+#ifndef __CDIO_CD_TYPES_H__
+#define __CDIO_CD_TYPES_H__
+
+#ifdef __cplusplus
+extern "C" {
+#endif /* __cplusplus */
+
+#define CDIO_FS_AUDIO 1 /* audio only - not really a fs */
+#define CDIO_FS_HIGH_SIERRA 2
+#define CDIO_FS_ISO_9660 3
+#define CDIO_FS_INTERACTIVE 4
+#define CDIO_FS_HFS 5
+#define CDIO_FS_UFS 6
+
+/* EXT2 was the GNU/Linux native filesystem for early kernels. Newer
+ GNU/Linux OS's may use EXT3 which EXT2 with a journal. */
+#define CDIO_FS_EXT2 7
+
+#define CDIO_FS_ISO_HFS 8 /* both hfs & isofs filesystem */
+#define CDIO_FS_ISO_9660_INTERACTIVE 9 /* both CD-RTOS and isofs filesystem */
+
+
+/* The 3DO is, technically, a set of specifications created by the 3DO
+company. These specs are for making a 3DO Interactive Multiplayer
+which uses a CD-player. Panasonic in the early 90's was the first
+company to manufacture and market a 3DO player. */
+#define CDIO_FS_3DO 10
+
+#define CDIO_FS_MASK 15 /* Should be 2*n-1 and > above */
+#define CDIO_FS_UNKNOWN CDIO_FS_MASK
+
+/* Macro to extract just the FS type portion defined above */
+#define CDIO_FSTYPE(fs) (fs & CDIO_FS_MASK)
+
+/*
+ Bit masks for the classes of CD-images. These are generally
+ higher-level than the fs-type information above and may be determined
+ based of the fs type information.
+ */
+#define CDIO_FS_ANAL_XA 16
+#define CDIO_FS_ANAL_MULTISESSION 32
+#define CDIO_FS_ANAL_PHOTO_CD 64
+#define CDIO_FS_ANAL_HIDDEN_TRACK 128
+#define CDIO_FS_ANAL_CDTV 256
+#define CDIO_FS_ANAL_BOOTABLE 512
+#define CDIO_FS_ANAL_VIDEOCD 1024 /* VCD 1.1 */
+#define CDIO_FS_ANAL_ROCKRIDGE 2048
+#define CDIO_FS_ANAL_JOLIET 4096
+#define CDIO_FS_ANAL_SVCD 8192 /* Super VCD or Choiji Video CD */
+#define CDIO_FS_ANAL_CVD 16384 /* Choiji Video CD */
+
+/* Pattern which can be used by cdio_get_devices to specify matching
+ any sort of CD.
+*/
+#define CDIO_FS_MATCH_ALL (cdio_fs_anal_t) (~CDIO_FS_MASK)
+
+
+typedef struct
+{
+ unsigned int joliet_level;
+ char iso_label[33]; /* 32 + 1 for null byte at the end in
+ formatting the string */
+ unsigned int isofs_size;
+} cdio_analysis_t;
+
+/*
+ Try to determine what kind of CD-image and/or filesystem we
+ have at track track_num. Return information about the CD image
+ is returned in cdio_analysis and the return value.
+*/
+cdio_fs_anal_t cdio_guess_cd_type(const CdIo *cdio, int start_session,
+ track_t track_num,
+ /*out*/ cdio_analysis_t *cdio_analysis);
+
+#ifdef __cplusplus
+}
+#endif /* __cplusplus */
+
+#endif /* __CDIO_CD_TYPES_H__ */
+
+/*
+ * Local variables:
+ * c-file-style: "gnu"
+ * tab-width: 8
+ * indent-tabs-mode: nil
+ * End:
+ */
diff --git a/src/input/vcd/libcdio/cdio/cdio.h b/src/input/vcd/libcdio/cdio/cdio.h
new file mode 100644
index 000000000..d0b304b38
--- /dev/null
+++ b/src/input/vcd/libcdio/cdio/cdio.h
@@ -0,0 +1,445 @@
+/* -*- c -*-
+ $Id: cdio.h,v 1.1 2003/10/13 11:47:12 f1rmb Exp $
+
+ Copyright (C) 2001 Herbert Valerio Riedel <hvr@gnu.org>
+ Copyright (C) 2003 Rocky Bernstein <rocky@panix.com>
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+*/
+
+/* Public CD Input and Control Interface . */
+
+
+#ifndef __CDIO_H__
+#define __CDIO_H__
+
+/* Application Interface or Protocol version number. If the public
+ interface changes, we increase this number.
+ */
+#define CDIO_API_VERSION 1
+
+#include <cdio/version.h>
+
+#ifdef HAVE_SYS_TYPES_H
+#include <sys/types.h>
+#endif
+#ifdef HAVE_UNISTD_H
+#include <unistd.h>
+#endif
+
+#include <cdio/types.h>
+#include <cdio/sector.h>
+
+/* Flags specifying the category of device to open or is opened. */
+#define CDIO_SRC_IS_DISK_IMAGE_MASK 0x0001
+#define CDIO_SRC_IS_DEVICE_MASK 0x0002
+#define CDIO_SRC_IS_SCSI_MASK 0x0004
+#define CDIO_SRC_IS_NATIVE_MASK 0x0008
+
+#ifdef __cplusplus
+extern "C" {
+#endif /* __cplusplus */
+
+ /* opaque structure */
+ typedef struct _CdIo CdIo;
+
+ /* The below enumerations may be used to tag a specific driver
+ that is opened or is desired to be opened. Note that this is
+ different than what is available on a given host.
+
+ Order is a little significant since the order is used in scans.
+ We have to start with UNKNOWN and devices should come before
+ disk-image readers. By putting something towards the top (a lower
+ enumeration number), in an iterative scan we prefer that to something
+ with a higher enumeration number.
+
+ NOTE: IF YOU MODIFY ENUM MAKE SURE INITIALIZATION IN CDIO.C AGREES.
+
+ */
+ typedef enum {
+ DRIVER_UNKNOWN,
+ DRIVER_BSDI,
+ DRIVER_FREEBSD,
+ DRIVER_LINUX,
+ DRIVER_SOLARIS,
+ DRIVER_OSX,
+ DRIVER_WIN32,
+ DRIVER_BINCUE, /* Prefer bincue over nrg when both exist */
+ DRIVER_NRG,
+ DRIVER_DEVICE /* Is really a set of the above; should come last */
+ } driver_id_t;
+
+ /* Make sure what's listed below is the last one above. Since we have
+ a bogus (but useful) 0th entry above we don't have to add one below.
+ */
+#define CDIO_MIN_DRIVER DRIVER_BSDI
+#define CDIO_MIN_DEVICE_DRIVER CDIO_MIN_DRIVER
+#define CDIO_MAX_DRIVER DRIVER_NRG
+#define CDIO_MAX_DEVICE_DRIVER DRIVER_WIN32
+
+ typedef enum {
+ TRACK_FORMAT_AUDIO, /* Audio track, e.g. CD-DA */
+ TRACK_FORMAT_CDI, /* CD-i. How this is different from DATA below? */
+ TRACK_FORMAT_XA, /* Mode2 of some sort */
+ TRACK_FORMAT_DATA, /* Mode1 of some sort */
+ TRACK_FORMAT_PSX, /* Playstation CD. Like audio but only 2336 bytes
+ of user data.
+ */
+ TRACK_FORMAT_ERROR /* Dunno what is or some other error. */
+ } track_format_t;
+
+ /* Printable tags for above enumeration. */
+ extern const char *track_format2str[6];
+
+ /*!
+ Eject media in CD drive if there is a routine to do so.
+ Return 0 if success and 1 for failure, and 2 if no routine.
+ If the CD is ejected *obj is freed and obj set to NULL.
+ */
+ int cdio_eject_media (CdIo **obj);
+
+ /*!
+ Free any resources associated with obj.
+ */
+ void cdio_destroy (CdIo *obj);
+
+ /*!
+ Free device list returned by cdio_get_devices or
+ cdio_get_devices_with_cap.
+ */
+ void cdio_free_device_list (char * device_list[]);
+
+ /*!
+ Return the value associatied with key. NULL is returned if obj is NULL
+ or "key" does not exist.
+ */
+ const char * cdio_get_arg (const CdIo *obj, const char key[]);
+
+ /*!
+ Return an array of device names in search_devices that have at
+ least the capabilities listed by cap. If search_devices is NULL,
+ then we'll search all possible CD drives.
+
+ If "any" is set false then every capability listed in the extended
+ portion of capabilities (i.e. not the basic filesystem) must be
+ satisified. If "any" is set true, then if any of the capabilities
+ matches, we call that a success.
+
+ To find a CD-drive of any type, use the mask CDIO_FS_MATCH_ALL.
+
+ NULL is returned if we couldn't get a default device.
+ */
+ char ** cdio_get_devices_with_cap (char* search_devices[],
+ cdio_fs_anal_t capabilities, bool any);
+
+ /*!Return an array of device names. If you want a specific
+ devices, dor a driver give that device, if you want hardware
+ devices, give DRIVER_DEVICE and if you want all possible devices,
+ image drivers and hardware drivers give DRIVER_UNKNOWN.
+
+ NULL is returned if we couldn't return a list of devices.
+ */
+ char ** cdio_get_devices (driver_id_t driver);
+
+ /*!
+ Return a string containing the default CD device.
+ if obj is NULL (we haven't initialized a specific device driver),
+ then find a suitable one and return the default device for that.
+
+ NULL is returned if we couldn't get a default device.
+ */
+ char * cdio_get_default_device (const CdIo *obj);
+
+ /*!
+ Return the media catalog number (MCN) from the CD or NULL if there
+ is none or we don't have the ability to get it.
+
+ Note: string is malloc'd so caller has to free() the returned
+ string when done with it.
+ */
+ char *cdio_get_mcn (const CdIo *obj);
+
+ /*!
+ Return a string containing the name of the driver in use.
+ if CdIo is NULL (we haven't initialized a specific device driver),
+ then return NULL.
+ */
+ const char * cdio_get_driver_name (const CdIo *obj);
+
+ /*!
+ Return the number of the first track.
+ CDIO_INVALID_TRACK is returned on error.
+ */
+ track_t cdio_get_first_track_num(const CdIo *obj);
+
+ /*!
+ Return a string containing the default CD device if none is specified.
+ */
+ track_t cdio_get_num_tracks (const CdIo *obj);
+
+ /*!
+ Get format of track.
+ */
+ track_format_t cdio_get_track_format(const CdIo *obj, track_t track_num);
+
+ /*!
+ Return true if we have XA data (green, mode2 form1) or
+ XA data (green, mode2 form2). That is track begins:
+ sync - header - subheader
+ 12 4 - 8
+
+ FIXME: there's gotta be a better design for this and get_track_format?
+ */
+ bool cdio_get_track_green(const CdIo *obj, track_t track_num);
+
+ /*!
+ Return the starting LBA for track number
+ track_num in obj. Tracks numbers start at 1.
+ The "leadout" track is specified either by
+ using track_num LEADOUT_TRACK or the total tracks+1.
+ CDIO_INVALID_LBA is returned on error.
+ */
+ lba_t cdio_get_track_lba(const CdIo *obj, track_t track_num);
+
+ /*!
+ Return the starting LSN for track number
+ track_num in obj. Tracks numbers start at 1.
+ The "leadout" track is specified either by
+ using track_num LEADOUT_TRACK or the total tracks+1.
+ CDIO_INVALID_LBA is returned on error.
+ */
+ lsn_t cdio_get_track_lsn(const CdIo *obj, track_t track_num);
+
+ /*!
+ Return the starting MSF (minutes/secs/frames) for track number
+ track_num in obj. Track numbers start at 1.
+ The "leadout" track is specified either by
+ using track_num LEADOUT_TRACK or the total tracks+1.
+ False is returned if there is no track entry.
+ */
+ bool cdio_get_track_msf(const CdIo *obj, track_t track_num,
+ /*out*/ msf_t *msf);
+
+ /*!
+ Return the number of sectors between this track an the next. This
+ includes any pregap sectors before the start of the next track.
+ Tracks start at 1.
+ 0 is returned if there is an error.
+ */
+ unsigned int cdio_get_track_sec_count(const CdIo *obj, track_t track_num);
+
+ /*!
+ lseek - reposition read/write file offset
+ Returns (off_t) -1 on error.
+ Similar to (if not the same as) libc's lseek()
+ */
+ off_t cdio_lseek(const CdIo *obj, off_t offset, int whence);
+
+ /*!
+ Reads into buf the next size bytes.
+ Returns -1 on error.
+ Similar to (if not the same as) libc's read()
+ */
+ ssize_t cdio_read(const CdIo *obj, void *buf, size_t size);
+
+ /*!
+ Reads a audio sector from cd device into data starting
+ from lsn. Returns 0 if no error.
+ */
+ int cdio_read_audio_sector (const CdIo *obj, void *buf, lsn_t lsn);
+
+ /*!
+ Reads a audio sector from cd device into data starting
+ from lsn. Returns 0 if no error.
+ */
+ int cdio_read_audio_sectors (const CdIo *obj, void *buf, lsn_t lsn,
+ unsigned int nblocks);
+
+ /*!
+ Reads a single mode1 sector from cd device into data starting
+ from lsn. Returns 0 if no error.
+ */
+ int cdio_read_mode1_sector (const CdIo *obj, void *buf, lsn_t lsn,
+ bool is_form2);
+
+ /*!
+ Reads nblocks of mode1 sectors from cd device into data starting
+ from lsn. Returns 0 if no error.
+ */
+ int cdio_read_mode1_sectors (const CdIo *obj, void *buf, lsn_t lsn,
+ bool is_form2, unsigned int num_sectors);
+
+ /*!
+ Reads a single mode2 sector from cd device into data starting
+ from lsn. Returns 0 if no error.
+ */
+ int cdio_read_mode2_sector (const CdIo *obj, void *buf, lsn_t lsn,
+ bool is_form2);
+
+ /*!
+ Reads nblocks of mode2 sectors from cd device into data starting
+ from lsn.
+ Returns 0 if no error.
+ */
+ int cdio_read_mode2_sectors (const CdIo *obj, void *buf, lsn_t lsn,
+ bool is_form2, unsigned int num_sectors);
+
+ /*!
+ Set the arg "key" with "value" in the source device.
+ 0 is returned if no error was found, and nonzero if there as an error.
+ */
+ int cdio_set_arg (CdIo *obj, const char key[], const char value[]);
+
+ /*!
+ Return the size of the CD in logical block address (LBA) units.
+ */
+ uint32_t cdio_stat_size (const CdIo *obj);
+
+ /*!
+ Initialize CD Reading and control routines. Should be called first.
+ */
+ bool cdio_init(void);
+
+ /* True if xxx driver is available. where xxx=linux, solaris, nrg, ...
+ */
+ bool cdio_have_bsdi (void);
+ bool cdio_have_freebsd (void);
+ bool cdio_have_linux (void);
+ bool cdio_have_solaris (void);
+ bool cdio_have_osx (void);
+ bool cdio_have_win32 (void);
+ bool cdio_have_nrg (void);
+ bool cdio_have_bincue (void);
+
+ /* Like above but uses the enumeration instead. */
+ bool cdio_have_driver (driver_id_t driver_id);
+
+ /* Return a string decribing driver_id. */
+ const char *cdio_driver_describe (driver_id_t driver_id);
+
+ /*! Sets up to read from place specified by source_name and
+ driver_id This should be called before using any other routine,
+ except cdio_init. This will call cdio_init, if that hasn't been
+ done previously. to call one of the specific routines below.
+
+ NULL is returned on error.
+ */
+ CdIo * cdio_open (const char *source_name, driver_id_t driver_id);
+
+ /*! cdrao BIN/CUE CD disk-image routines. Source is the .bin file
+
+ NULL is returned on error.
+ */
+ CdIo * cdio_open_bincue (const char *bin_name);
+
+ char * cdio_get_default_device_bincue(void);
+
+ char **cdio_get_devices_bincue(void);
+
+ /*! CD routines. Source is the some sort of device.
+
+ NULL is returned on error.
+ */
+ CdIo * cdio_open_cd (const char *device_name);
+
+ /*! cdrao BIN/CUE CD disk-image routines. Source is the .cue file
+
+ NULL is returned on error.
+ */
+ CdIo * cdio_open_cue (const char *cue_name);
+
+ /*! BSDI CD-reading routines.
+ NULL is returned on error.
+ */
+ CdIo * cdio_open_bsdi (const char *source_name);
+
+ char * cdio_get_default_device_bsdi(void);
+
+ char **cdio_get_devices_bsdi(void);
+
+ /*! BSDI CD-reading routines.
+ NULL is returned on error.
+ */
+ CdIo * cdio_open_freebsd (const char *source_name);
+
+ char * cdio_get_default_device_freebsd(void);
+
+ char **cdio_get_devices_freebsd(void);
+
+ /*! Linux CD-reading routines.
+ NULL is returned on error.
+ */
+ CdIo * cdio_open_linux (const char *source_name);
+
+ char * cdio_get_default_device_linux(void);
+
+ char **cdio_get_devices_linux(void);
+
+ /*! Solaris CD-reading routines.
+ NULL is returned on error.
+ */
+ CdIo * cdio_open_solaris (const char *source_name);
+
+ char * cdio_get_default_device_solaris(void);
+
+ char **cdio_get_devices_solaris(void);
+
+ /*! Darwin OS X CD-reading routines.
+ NULL is returned on error.
+ */
+ CdIo * cdio_open_osx (const char *source_name);
+
+ char * cdio_get_default_device_osx(void);
+
+ char **cdio_get_devices_osx(void);
+
+ /*! Win32 CD-reading routines.
+ NULL is returned on error.
+ */
+ CdIo * cdio_open_win32 (const char *source_name);
+
+ char * cdio_get_default_device_win32(void);
+
+ char **cdio_get_devices_win32(void);
+
+ /*! Nero CD disk-image routines.
+ NULL is returned on error.
+ */
+ CdIo * cdio_open_nrg (const char *source_name);
+
+ char * cdio_get_default_device_nrg(void);
+
+ char **cdio_get_devices_nrg(void);
+
+ /*! Return corresponding BIN file if cue_name is a cue file or NULL
+ if not a CUE file.
+ */
+ char *cdio_is_cuefile(const char *cue_name);
+
+ /*! Return corresponding CUE file if bin_name is a fin file or NULL
+ if not a BIN file. NOTE: when we handle TOC something will have to
+ change here....
+ */
+ char *cdio_is_binfile(const char *bin_name);
+
+ /*! Return true if source name is a device.
+ */
+ bool cdio_is_device(const char *source_name, driver_id_t driver_id);
+
+#ifdef __cplusplus
+}
+#endif /* __cplusplus */
+
+#endif /* __CDIO_H__ */
diff --git a/src/input/vcd/libcdio/cdio/iso9660.h b/src/input/vcd/libcdio/cdio/iso9660.h
new file mode 100644
index 000000000..c2cabe5fc
--- /dev/null
+++ b/src/input/vcd/libcdio/cdio/iso9660.h
@@ -0,0 +1,428 @@
+/*
+ $Id: iso9660.h,v 1.1 2003/10/13 11:47:12 f1rmb Exp $
+
+ Copyright (C) 2000 Herbert Valerio Riedel <hvr@gnu.org>
+ Copyright (C) 2003 Rocky Bernstein <rocky@panix.com>
+
+ See also iso9660.h by Eric Youngdale (1993).
+
+ Copyright 1993 Yggdrasil Computing, Incorporated
+ Copyright (c) 1999,2000 J. Schilling
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+*/
+/*
+ * Header file iso9660.h - assorted structure definitions and typecasts.
+ specific to iso9660 filesystem.
+*/
+
+
+#ifndef __CDIO_ISO9660_H__
+#define __CDIO_ISO9660_H__
+
+#include <cdio/cdio.h>
+#include <cdio/xa.h>
+
+#include <time.h>
+
+#define _delta(from, to) ((to) - (from) + 1)
+
+#define MIN_TRACK_SIZE 4*75
+#define MIN_ISO_SIZE MIN_TRACK_SIZE
+
+/*
+ A ISO filename is: "abcde.eee;1" -> <filename> '.' <ext> ';' <version #>
+
+ The maximum needed string length is:
+ 30 chars (filename + ext)
+ + 2 chars ('.' + ';')
+ + strlen("32767")
+ + null byte
+ ================================
+ = 38 chars
+*/
+#define LEN_ISONAME 31
+#define MAX_ISONAME 37
+
+#define MAX_ISOPATHNAME 255
+
+/*
+ * ISO 9660 directory flags.
+ */
+#define ISO_FILE 0 /* Not really a flag... */
+#define ISO_EXISTENCE 1 /* Do not make existence known (hidden) */
+#define ISO_DIRECTORY 2 /* This file is a directory */
+#define ISO_ASSOCIATED 4 /* This file is an assiciated file */
+#define ISO_RECORD 8 /* Record format in extended attr. != 0 */
+#define ISO_PROTECTION 16 /* No read/execute perm. in ext. attr. */
+#define ISO_DRESERVED1 32 /* Reserved bit 5 */
+#define ISO_DRESERVED2 64 /* Reserved bit 6 */
+#define ISO_MULTIEXTENT 128 /* Not final entry of a mult. ext. file */
+
+/* Volume descriptor types */
+#define ISO_VD_PRIMARY 1
+#define ISO_VD_SUPPLEMENTARY 2 /* Used by Joliet */
+#define ISO_VD_END 255
+
+#define ISO_PVD_SECTOR 16
+#define ISO_EVD_SECTOR 17
+
+#define ISO_STANDARD_ID "CD001"
+#define ISO_BLOCKSIZE 2048
+
+#ifdef __cplusplus
+extern "C" {
+#endif /* __cplusplus */
+
+enum strncpy_pad_check {
+ ISO9660_NOCHECK = 0,
+ ISO9660_7BIT,
+ ISO9660_ACHARS,
+ ISO9660_DCHARS
+};
+
+PRAGMA_BEGIN_PACKED
+
+struct iso9660_dtime {
+ uint8_t dt_year;
+ uint8_t dt_month; /* 1..12. Note 1 origin not 0, like a tm struct. */
+ uint8_t dt_day;
+ uint8_t dt_hour;
+ uint8_t dt_minute;
+ uint8_t dt_second;
+ int8_t dt_gmtoff; /* GMT values -48 .. + 52 in 15 min intervals */
+} GNUC_PACKED;
+
+typedef struct iso9660_dtime iso9660_dtime_t;
+
+struct iso9660_ltime {
+ char lt_year [_delta( 1, 4)]; /* Add 1900 for Julian year */
+ char lt_month [_delta( 5, 6)]; /* 1..12. Note starts at 1. */
+ char lt_day [_delta( 7, 8)];
+ char lt_hour [_delta( 9, 10)];
+ char lt_minute [_delta( 11, 12)];
+ char lt_second [_delta( 13, 14)];
+ char lt_hsecond [_delta( 15, 16)]; /* 1/100's of a second */
+ int8_t lt_gmtoff [_delta( 17, 17)];
+} GNUC_PACKED;
+
+typedef struct iso9660_ltime iso9660_ltime_t;
+
+/* ISO-9660 Primary Volume Descriptor.
+ */
+struct iso9660_pvd {
+ uint8_t type; /* 711 */
+ char id[5];
+ uint8_t version; /* 711 */
+ char unused1[1];
+ char system_id[32]; /* achars */
+ char volume_id[32]; /* dchars */
+ char unused2[8];
+ uint64_t volume_space_size; /* 733 */
+ char escape_sequences[32];
+ uint32_t volume_set_size; /* 723 */
+ uint32_t volume_sequence_number; /* 723 */
+ uint32_t logical_block_size; /* 723 */
+ uint64_t path_table_size; /* 733 */
+ uint32_t type_l_path_table; /* 731 */
+ uint32_t opt_type_l_path_table; /* 731 */
+ uint32_t type_m_path_table; /* 732 */
+ uint32_t opt_type_m_path_table; /* 732 */
+ char root_directory_record[34]; /* 9.1 */
+ char volume_set_id[128]; /* dchars */
+ char publisher_id[128]; /* achars */
+ char preparer_id[128]; /* achars */
+ char application_id[128]; /* achars */
+ char copyright_file_id[37]; /* 7.5 dchars */
+ char abstract_file_id[37]; /* 7.5 dchars */
+ char bibliographic_file_id[37]; /* 7.5 dchars */
+ iso9660_ltime_t creation_date; /* 8.4.26.1 */
+ iso9660_ltime_t modification_date; /* 8.4.26.1 */
+ iso9660_ltime_t expiration_date; /* 8.4.26.1 */
+ iso9660_ltime_t effective_date; /* 8.4.26.1 */
+ uint8_t file_structure_version; /* 711 */
+ char unused4[1];
+ char application_data[512];
+ char unused5[653];
+} GNUC_PACKED;
+
+typedef struct iso9660_dir iso9660_dir_t;
+typedef struct iso9660_pvd iso9660_pvd_t;
+typedef struct iso9660_stat iso9660_stat_t;
+
+#ifndef EMPTY_ARRAY_SIZE
+#define EMPTY_ARRAY_SIZE 0
+#endif
+
+/*
+ * XXX JS: The next structure may have an odd length!
+ * Some compilers (e.g. on Sun3/mc68020) padd the structures to even length.
+ * For this reason, we cannot use sizeof (struct iso_path_table) or
+ * sizeof (struct iso_directory_record) to compute on disk sizes.
+ * Instead, we use offsetof(..., name) and add the name size.
+ * See mkisofs.h
+ */
+
+/* Format of an ISO-9660 directory record */
+struct iso9660_dir {
+ uint8_t length; /* 711 */
+ uint8_t xa_length; /* 711 */
+ uint64_t extent; /* 733 */
+ uint64_t size; /* 733 */
+ iso9660_dtime_t recording_time; /* 7 by 711 */
+ uint8_t file_flags;
+ uint8_t file_unit_size; /* 711 */
+ uint8_t interleave_gap; /* 711 */
+ uint32_t volume_sequence_number; /* 723 */
+ uint8_t filename_len; /* 711 */
+ char filename[EMPTY_ARRAY_SIZE];
+} GNUC_PACKED;
+
+
+/* The following structure is not part of ISO 9660. We just use it
+ for our own purposes for communicating info back that's pulled out.
+*/
+struct iso9660_stat { /* big endian!! */
+ enum { _STAT_FILE = 1, _STAT_DIR = 2 } type;
+ lsn_t lsn; /* start logical sector number */
+ uint32_t size; /* total size in bytes */
+ uint32_t secsize; /* number of sectors allocated */
+ iso9660_xa_t xa; /* XA attributes */
+ struct tm tm; /* time on entry */
+} ;
+
+PRAGMA_END_PACKED
+
+/*====================================
+ Character file/dirname's
+=====================================*/
+
+/*!
+ Return true if c is a DCHAR - a character that can appear in an an
+ ISO-9600 level 1 directory name. These are the ASCII capital
+ letters A-Z, the digits 0-9 and an underscore.
+*/
+bool iso9660_isdchar (int c);
+
+/*!
+ Return true if c is an ACHAR -
+ These are the DCHAR's plus some ASCII symbols including the space
+ symbol.
+*/
+bool iso9660_isachar (int c);
+
+/*!
+ Convert ISO-9660 file name that stored in a directory entry into
+ what's usually listed as the file name in a listing.
+ Lowercase name, and drop deal with trailing ;1's or .;1's or
+ ; version numbers.
+
+ The length of the translated string is returned.
+*/
+int iso9660_name_translate(const char *old, char *new);
+
+/*!
+ Pad string src with spaces to size len and copy this to dst. If
+ len is less than the length of src, dst will be truncated to the
+ first len characters of src.
+
+ src can also be scanned to see if it contains only ACHARs, DCHARs,
+ 7-bit ASCII chars depending on the enumeration _check.
+
+ In addition to getting changed, dst is the return value.
+ Note: this string might not be NULL terminated.
+ */
+char *iso9660_strncpy_pad(char dst[], const char src[], size_t len,
+ enum strncpy_pad_check _check);
+
+/*!
+ Set time in format used in ISO 9660 directory index record
+ from a Unix time structure. */
+ void iso9660_set_dtime (const struct tm *tm,
+ /*out*/ iso9660_dtime_t *idr_date);
+
+
+/*!
+ Set "long" time in format used in ISO 9660 primary volume descriptor
+ from a Unix time structure. */
+ void iso9660_set_ltime (const struct tm *_tm,
+ /*out*/ iso9660_ltime_t *pvd_date);
+
+/*!
+ Get Unix time structure from format use in an ISO 9660 directory index
+ record. Even though tm_wday and tm_yday fields are not explicitly in
+ idr_date, they are calculated from the other fields.
+
+ If tm is to reflect the localtime, set "use_localtime" true, otherwise
+ tm will reported in GMT.
+*/
+ void iso9660_get_dtime (const iso9660_dtime_t *idr_date, bool use_localtime,
+ /*out*/ struct tm *tm);
+
+
+/*=====================================================================
+ file/dirname's
+======================================================================*/
+
+/*!
+ Check that pathname is a valid ISO-9660 directory name.
+
+ A valid directory name should not start out with a slash (/),
+ dot (.) or null byte, should be less than 37 characters long,
+ have no more than 8 characters in a directory component
+ which is separated by a /, and consist of only DCHARs.
+
+ True is returned if pathname is valid.
+ */
+bool iso9660_dirname_valid_p (const char pathname[]);
+
+/*!
+ Take pathname and a version number and turn that into a ISO-9660
+ pathname. (That's just the pathname followd by ";" and the version
+ number. For example, mydir/file.ext -> mydir/file.ext;1 for version
+ 1. The resulting ISO-9660 pathname is returned.
+*/
+char *iso9660_pathname_isofy (const char pathname[], uint16_t version);
+
+/*!
+ Check that pathname is a valid ISO-9660 pathname.
+
+ A valid pathname contains a valid directory name, if one appears and
+ the filename portion should be no more than 8 characters for the
+ file prefix and 3 characters in the extension (or portion after a
+ dot). There should be exactly one dot somewhere in the filename
+ portion and the filename should be composed of only DCHARs.
+
+ True is returned if pathname is valid.
+ */
+bool iso9660_pathname_valid_p (const char pathname[]);
+
+/*=====================================================================
+ directory tree
+======================================================================*/
+
+void
+iso9660_dir_init_new (void *dir, uint32_t self, uint32_t ssize,
+ uint32_t parent, uint32_t psize,
+ const time_t *dir_time);
+
+void
+iso9660_dir_init_new_su (void *dir, uint32_t self, uint32_t ssize,
+ const void *ssu_data, unsigned int ssu_size,
+ uint32_t parent, uint32_t psize,
+ const void *psu_data, unsigned int psu_size,
+ const time_t *dir_time);
+
+void
+iso9660_dir_add_entry_su (void *dir, const char filename[], uint32_t extent,
+ uint32_t size, uint8_t file_flags,
+ const void *su_data,
+ unsigned int su_size, const time_t *entry_time);
+
+unsigned int
+iso9660_dir_calc_record_size (unsigned int namelen, unsigned int su_len);
+
+/*!
+ Given a directory pointer, find the filesystem entry that contains
+ lsn and return information about it in stat.
+
+ Returns true if we found an entry with the lsn and false if not.
+ */
+bool iso9660_find_fs_lsn(const CdIo *cdio, lsn_t lsn,
+ /*out*/ iso9660_stat_t *stat);
+
+/*!
+ Get file status for pathname into stat. As with libc's stat, 0 is returned
+ if no error and -1 on error.
+ */
+int iso9660_fs_stat (const CdIo *obj, const char pathname[],
+ /*out*/ iso9660_stat_t *stat, bool is_mode2);
+
+void * /* list of char* -- caller must free it */
+iso9660_fs_readdir (const CdIo *obj, const char pathname[], bool mode2);
+
+uint8_t
+iso9660_get_dir_len(const iso9660_dir_t *idr);
+
+#if FIXME
+uint8_t
+iso9660_get_dir_size(const iso9660_dir_t *idr);
+
+lsn_t
+iso9660_get_dir_extent(const iso9660_dir_t *idr);
+#endif
+
+uint8_t
+iso9660_get_pvd_type(const iso9660_pvd_t *pvd);
+
+const char *
+iso9660_get_pvd_id(const iso9660_pvd_t *pvd);
+
+int
+iso9660_get_pvd_space_size(const iso9660_pvd_t *pvd);
+
+int
+iso9660_get_pvd_block_size(const iso9660_pvd_t *pvd) ;
+
+/*! Return the primary volume id version number (of pvd).
+ If there is an error 0 is returned.
+ */
+int iso9660_get_pvd_version(const iso9660_pvd_t *pvd) ;
+
+/*! Return the LSN of the root directory for pvd.
+ If there is an error CDIO_INVALID_LSN is returned.
+ */
+lsn_t iso9660_get_root_lsn(const iso9660_pvd_t *pvd);
+
+/* pathtable */
+
+/*! Zero's out pathable. Do this first. */
+void iso9660_pathtable_init (void *pt);
+
+unsigned int iso9660_pathtable_get_size (const void *pt);
+
+uint16_t
+iso9660_pathtable_l_add_entry (void *pt, const char name[], uint32_t extent,
+ uint16_t parent);
+
+uint16_t
+iso9660_pathtable_m_add_entry (void *pt, const char name[], uint32_t extent,
+ uint16_t parent);
+
+/* volume descriptors */
+
+void
+iso9660_set_pvd (void *pd, const char volume_id[], const char application_id[],
+ const char publisher_id[], const char preparer_id[],
+ uint32_t iso_size, const void *root_dir,
+ uint32_t path_table_l_extent, uint32_t path_table_m_extent,
+ uint32_t path_table_size, const time_t *pvd_time);
+
+void
+iso9660_set_evd (void *pd);
+
+#ifdef __cplusplus
+}
+#endif /* __cplusplus */
+
+#endif /* __CDIO_ISO9660_H__ */
+
+/*
+ * Local variables:
+ * c-file-style: "gnu"
+ * tab-width: 8
+ * indent-tabs-mode: nil
+ * End:
+ */
diff --git a/src/input/vcd/libcdio/cdio/logging.h b/src/input/vcd/libcdio/cdio/logging.h
new file mode 100644
index 000000000..9a600b437
--- /dev/null
+++ b/src/input/vcd/libcdio/cdio/logging.h
@@ -0,0 +1,66 @@
+/*
+ $Id: logging.h,v 1.1 2003/10/13 11:47:12 f1rmb Exp $
+
+ Copyright (C) 2000 Herbert Valerio Riedel <hvr@gnu.org>
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+*/
+
+#ifndef __LOGGING_H__
+#define __LOGGING_H__
+
+#include <cdio/types.h>
+
+typedef enum {
+ CDIO_LOG_DEBUG = 1,
+ CDIO_LOG_INFO,
+ CDIO_LOG_WARN,
+ CDIO_LOG_ERROR,
+ CDIO_LOG_ASSERT
+} cdio_log_level_t;
+
+extern int cdio_loglevel_default;
+
+void
+cdio_log (cdio_log_level_t level, const char format[], ...) GNUC_PRINTF(2, 3);
+
+typedef void (*cdio_log_handler_t) (cdio_log_level_t level,
+ const char message[]);
+
+cdio_log_handler_t
+cdio_log_set_handler (cdio_log_handler_t new_handler);
+
+void
+cdio_debug (const char format[], ...) GNUC_PRINTF(1,2);
+
+void
+cdio_info (const char format[], ...) GNUC_PRINTF(1,2);
+
+void
+cdio_warn (const char format[], ...) GNUC_PRINTF(1,2);
+
+void
+cdio_error (const char format[], ...) GNUC_PRINTF(1,2);
+
+#endif /* __LOGGING_H__ */
+
+
+/*
+ * Local variables:
+ * c-file-style: "gnu"
+ * tab-width: 8
+ * indent-tabs-mode: nil
+ * End:
+ */
diff --git a/src/input/vcd/libcdio/cdio/sector.h b/src/input/vcd/libcdio/cdio/sector.h
new file mode 100644
index 000000000..39dd0fefb
--- /dev/null
+++ b/src/input/vcd/libcdio/cdio/sector.h
@@ -0,0 +1,162 @@
+/*
+ $Id: sector.h,v 1.1 2003/10/13 11:47:12 f1rmb Exp $
+
+ Copyright (C) 2000 Herbert Valerio Riedel <hvr@gnu.org>
+ Copyright (C) 2003 Rocky Bernstein <rocky@panix.com>
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+*/
+/*
+ Things related to CDROM layout. Sector sizes, MSFs, LBAs,
+*/
+
+#ifndef _CDIO_SECTOR_H_
+#define _CDIO_SECTOR_H_
+
+#ifdef __cplusplus
+ extern "C" {
+#endif
+
+#include <cdio/types.h>
+
+#define CDIO_PREGAP_SECTORS 150
+#define CDIO_POSTGAP_SECTORS 150
+
+/*
+ * A CD-ROM physical sector size is 2048, 2052, 2056, 2324, 2332, 2336,
+ * 2340, or 2352 bytes long.
+
+* Sector types of the standard CD-ROM data formats:
+ *
+ * format sector type user data size (bytes)
+ * -----------------------------------------------------------------------------
+ * 1 (Red Book) CD-DA 2352 (CDIO_CD_FRAMESIZE_RAW)
+ * 2 (Yellow Book) Mode1 Form1 2048 (CDIO_CD_FRAMESIZE)
+ * 3 (Yellow Book) Mode1 Form2 2336 (M2RAW_SECTOR_SIZE)
+ * 4 (Green Book) Mode2 Form1 2048 (CDIO_CD_FRAMESIZE)
+ * 5 (Green Book) Mode2 Form2 2328 (2324+4 spare bytes)
+ *
+ *
+ * The layout of the standard CD-ROM data formats:
+ * -----------------------------------------------------------------------------
+ * - audio (red): | audio_sample_bytes |
+ * | 2352 |
+ *
+ * - data (yellow, mode1): | sync - head - data - EDC - zero - ECC |
+ * | 12 - 4 - 2048 - 4 - 8 - 276 |
+ *
+ * - data (yellow, mode2): | sync - head - data |
+ * | 12 - 4 - 2336 |
+ *
+ * - XA data (green, mode2 form1): | sync - head - sub - data - EDC - ECC |
+ * | 12 - 4 - 8 - 2048 - 4 - 276 |
+ *
+ * - XA data (green, mode2 form2): | sync - head - sub - data - Spare |
+ * | 12 - 4 - 8 - 2324 - 4 |
+ *
+ */
+
+/*
+ Some generally useful CD-ROM information -- mostly based on the above.
+ This is from linux.h - not to slight other OS's. This was the first
+ place I came across such useful stuff.
+*/
+#define CDIO_CD_MINS 74 /* max. minutes per CD, not really a limit */
+#define CDIO_CD_SECS_PER_MIN 60 /* seconds per minute */
+#define CDIO_CD_FRAMES_PER_SEC 75 /* frames per second */
+#define CDIO_CD_SYNC_SIZE 12 /* 12 sync bytes per raw data frame */
+#define CDIO_CD_CHUNK_SIZE 24 /* lowest-level "data bytes piece" */
+#define CDIO_CD_NUM_OF_CHUNKS 98 /* chunks per frame */
+#define CDIO_CD_FRAMESIZE_SUB 96 /* subchannel data "frame" size */
+#define CDIO_CD_HEADER_SIZE 4 /* header (address) bytes per raw data
+ frame */
+#define CDIO_CD_SUBHEADER_SIZE 8 /* subheader bytes per raw XA data frame */
+#define CDIO_CD_EDC_SIZE 4 /* bytes EDC per most raw data frame types */
+#define CDIO_CD_M1F1_ZERO_SIZE 8 /* bytes zero per yellow book mode 1 frame */
+#define CDIO_CD_ECC_SIZE 276 /* bytes ECC per most raw data frame types */
+#define CDIO_CD_FRAMESIZE 2048 /* bytes per frame, "cooked" mode */
+#define CDIO_CD_FRAMESIZE_RAW 2352/* bytes per frame, "raw" mode */
+#define CDIO_CD_FRAMESIZE_RAWER 2646 /* The maximum possible returned bytes */
+#define CDIO_CD_FRAMESIZE_RAW1 (CD_FRAMESIZE_RAW-CD_SYNC_SIZE) /*2340*/
+#define CDIO_CD_FRAMESIZE_RAW0 (CD_FRAMESIZE_RAW-CD_SYNC_SIZE-CD_HEAD_SIZE) /*2336*/
+
+/* "before data" part of raw XA (green, mode2) frame */
+#define CDIO_CD_XA_HEADER (CDIO_CD_HEADER_SIZE+CDIO_CD_SUBHEADER_SIZE)
+
+/* "after data" part of raw XA (green, mode2 form1) frame */
+#define CDIO_CD_XA_TAIL (CDIO_CD_EDC_SIZE+CDIO_CD_ECC_SIZE)
+
+/* "before data" sync bytes + header of XA (green, mode2) frame */
+#define CDIO_CD_XA_SYNC_HEADER (CDIO_CD_SYNC_SIZE+CDIO_CD_XA_HEADER)
+
+/* CD-ROM address types (Linux cdrom_tocentry.cdte_format) */
+#define CDIO_CDROM_LBA 0x01 /* "logical block": first frame is #0 */
+#define CDIO_CDROM_MSF 0x02 /* "minute-second-frame": binary, not bcd here! */
+
+#define CDIO_CDROM_DATA_TRACK 0x04
+
+/* The leadout track is always 0xAA, regardless of # of tracks on disc */
+#define CDIO_CDROM_LEADOUT_TRACK 0xAA
+
+#define M2F2_SECTOR_SIZE 2324
+#define M2SUB_SECTOR_SIZE 2332
+#define M2RAW_SECTOR_SIZE 2336
+
+#define CDIO_CD_MAX_TRACKS 99
+#define CDIO_CD_MIN_TRACK_NO 1
+
+#define CDIO_CD_FRAMES_PER_MIN \
+ (CDIO_CD_FRAMES_PER_SEC*CDIO_CD_SECS_PER_MIN)
+
+#define CDIO_CD_74MIN_SECTORS (UINT32_C(74)*CDIO_CD_FRAMES_PER_MIN)
+#define CDIO_CD_80MIN_SECTORS (UINT32_C(80)*CDIO_CD_FRAMES_PER_MIN)
+#define CDIO_CD_90MIN_SECTORS (UINT32_C(90)*CDIO_CD_FRAMES_PER_MIN)
+
+#define CDIO_CD_MAX_SECTORS \
+ (UINT32_C(100)*CDIO_CD_FRAMES_PER_MIN-CDIO_PREGAP_SECTORS)
+
+#define msf_t_SIZEOF 3
+
+/* warning, returns new allocated string */
+char *cdio_lba_to_msf_str (lba_t lba);
+
+lba_t cdio_lba_to_lsn (lba_t lba);
+
+void cdio_lba_to_msf(lba_t lba, msf_t *msf);
+
+lba_t cdio_lsn_to_lba (lsn_t lsn);
+
+void cdio_lsn_to_msf (lsn_t lsn, msf_t *msf);
+
+lba_t
+cdio_msf_to_lba (const msf_t *msf);
+
+lsn_t
+cdio_msf_to_lsn (const msf_t *msf);
+
+#ifdef __cplusplus
+ }
+#endif
+
+#endif /* _CDIO_SECTOR_H_ */
+
+
+/*
+ * Local variables:
+ * c-file-style: "gnu"
+ * tab-width: 8
+ * indent-tabs-mode: nil
+ * End:
+ */
diff --git a/src/input/vcd/libcdio/cdio/types.h b/src/input/vcd/libcdio/cdio/types.h
new file mode 100644
index 000000000..e3c3ff3e5
--- /dev/null
+++ b/src/input/vcd/libcdio/cdio/types.h
@@ -0,0 +1,236 @@
+/*
+ $Id: types.h,v 1.1 2003/10/13 11:47:12 f1rmb Exp $
+
+ Copyright (C) 2000 Herbert Valerio Riedel <hvr@gnu.org>
+ Copyright (C) 2002,2003 Rocky Bernstein <rocky@panix.com>
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+*/
+
+#ifndef __CDIO_TYPES_H__
+#define __CDIO_TYPES_H__
+
+#ifdef __cplusplus
+extern "C" {
+#endif /* __cplusplus */
+
+ /* provide some C99 definitions */
+
+#if defined(HAVE_SYS_TYPES_H)
+#include <sys/types.h>
+#endif
+
+#if defined(HAVE_STDINT_H)
+# include <stdint.h>
+#elif defined(HAVE_INTTYPES_H)
+# include <inttypes.h>
+#elif defined(AMIGA) || defined(__linux__)
+ typedef u_int8_t uint8_t;
+ typedef u_int16_t uint16_t;
+ typedef u_int32_t uint32_t;
+ typedef u_int64_t uint64_t;
+#else
+ /* warning ISO/IEC 9899:1999 <stdint.h> was missing and even <inttypes.h> */
+ /* fixme */
+#endif /* HAVE_STDINT_H */
+
+ /* default HP/UX macros are broken */
+#if defined(__hpux__)
+# undef UINT16_C
+# undef UINT32_C
+# undef UINT64_C
+# undef INT64_C
+#endif
+
+#if defined (MINGW32)
+ typedef int ssize_t;
+#endif
+
+ /* if it's still not defined, take a good guess... should work for
+ most 32bit and 64bit archs */
+
+#ifndef UINT16_C
+# define UINT16_C(c) c ## U
+#endif
+
+#ifndef UINT32_C
+# if defined (SIZEOF_INT) && SIZEOF_INT == 4
+# define UINT32_C(c) c ## U
+# elif defined (SIZEOF_LONG) && SIZEOF_LONG == 4
+# define UINT32_C(c) c ## UL
+# else
+# define UINT32_C(c) c ## U
+# endif
+#endif
+
+#ifndef UINT64_C
+# if defined (SIZEOF_LONG) && SIZEOF_LONG == 8
+# define UINT64_C(c) c ## UL
+# elif defined (SIZEOF_INT) && SIZEOF_INT == 8
+# define UINT64_C(c) c ## U
+# else
+# define UINT64_C(c) c ## ULL
+# endif
+#endif
+
+#ifndef INT64_C
+# if defined (SIZEOF_LONG) && SIZEOF_LONG == 8
+# define INT64_C(c) c ## L
+# elif defined (SIZEOF_INT) && SIZEOF_INT == 8
+# define INT64_C(c) c
+# else
+# define INT64_C(c) c ## LL
+# endif
+#endif
+
+#if defined(HAVE_STDBOOL_H)
+#include <stdbool.h>
+#else
+ /* ISO/IEC 9899:1999 <stdbool.h> missing -- enabling workaround */
+
+# ifndef __cplusplus
+ typedef enum
+ {
+ false = 0,
+ true = 1
+ } _Bool;
+
+# define false false
+# define true true
+# define bool _Bool
+# endif
+#endif
+
+ /* some GCC optimizations -- gcc 2.5+ */
+
+#if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ > 4)
+#define GNUC_PRINTF( format_idx, arg_idx ) \
+ __attribute__((format (printf, format_idx, arg_idx)))
+#define GNUC_SCANF( format_idx, arg_idx ) \
+ __attribute__((format (scanf, format_idx, arg_idx)))
+#define GNUC_FORMAT( arg_idx ) \
+ __attribute__((format_arg (arg_idx)))
+#define GNUC_NORETURN \
+ __attribute__((noreturn))
+#define GNUC_CONST \
+ __attribute__((const))
+#define GNUC_UNUSED \
+ __attribute__((unused))
+#define GNUC_PACKED \
+ __attribute__((packed))
+#else /* !__GNUC__ */
+#define GNUC_PRINTF( format_idx, arg_idx )
+#define GNUC_SCANF( format_idx, arg_idx )
+#define GNUC_FORMAT( arg_idx )
+#define GNUC_NORETURN
+#define GNUC_CONST
+#define GNUC_UNUSED
+#define GNUC_PACKED
+#endif /* !__GNUC__ */
+
+#if defined(__GNUC__)
+ /* for GCC we try to use GNUC_PACKED */
+# define PRAGMA_BEGIN_PACKED
+# define PRAGMA_END_PACKED
+#elif defined(HAVE_ISOC99_PRAGMA)
+ /* should work with most EDG-frontend based compilers */
+# define PRAGMA_BEGIN_PACKED _Pragma("pack(1)")
+# define PRAGMA_END_PACKED _Pragma("pack()")
+#else /* neither gcc nor _Pragma() available... */
+ /* ...so let's be naive and hope the regression testsuite is run... */
+# define PRAGMA_BEGIN_PACKED
+# define PRAGMA_END_PACKED
+#endif
+
+ /*
+ * user directed static branch prediction gcc 2.96+
+ */
+#if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ > 95)
+# define GNUC_LIKELY(x) __builtin_expect((x),true)
+# define GNUC_UNLIKELY(x) __builtin_expect((x),false)
+#else
+# define GNUC_LIKELY(x) (x)
+# define GNUC_UNLIKELY(x) (x)
+#endif
+
+#ifndef NULL
+# define NULL ((void*) 0)
+#endif
+
+ /* our own offsetof()-like macro */
+#define __cd_offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
+
+ /* In many structures on the disk a sector address is stored as a
+ BCD-encoded mmssff in three bytes. */
+ PRAGMA_BEGIN_PACKED
+ typedef struct {
+ uint8_t m, s, f;
+ } GNUC_PACKED msf_t;
+ PRAGMA_END_PACKED
+
+#define msf_t_SIZEOF 3
+
+ /* type used for bit-fields in structs (1 <= bits <= 8) */
+#if defined(__GNUC__)
+ /* this is strict ISO C99 which allows only 'unsigned int', 'signed
+ int' and '_Bool' explicitly as bit-field type */
+ typedef unsigned int bitfield_t;
+#else
+ /* other compilers might increase alignment requirements to match the
+ 'unsigned int' type -- fixme: find out how unalignment accesses can
+ be pragma'ed on non-gcc compilers */
+ typedef uint8_t bitfield_t;
+#endif
+
+ /* The type of a Logical Block Address. */
+ typedef uint32_t lba_t;
+
+ /* The type of an Logical Sector Number. */
+ typedef uint32_t lsn_t;
+
+ /* The type of an track number 0..99. */
+ typedef uint8_t track_t;
+
+ /*!
+ Constant for invalid track number
+ */
+#define CDIO_INVALID_TRACK 0xFF
+
+ /*!
+ Constant for invalid LBA
+ */
+#define CDIO_INVALID_LBA 0xFFFFFFFF
+
+ /*!
+ Constant for invalid LSN
+ */
+#define CDIO_INVALID_LSN 0xFFFFFFFF
+
+typedef int cdio_fs_anal_t;
+
+#ifdef __cplusplus
+}
+#endif /* __cplusplus */
+
+#endif /* __CDIO_TYPES_H__ */
+
+
+/*
+ * Local variables:
+ * c-file-style: "gnu"
+ * tab-width: 8
+ * indent-tabs-mode: nil
+ * End:
+ */
diff --git a/src/input/vcd/libcdio/cdio/util.h b/src/input/vcd/libcdio/cdio/util.h
new file mode 100644
index 000000000..147a32365
--- /dev/null
+++ b/src/input/vcd/libcdio/cdio/util.h
@@ -0,0 +1,109 @@
+/*
+ $Id: util.h,v 1.1 2003/10/13 11:47:12 f1rmb Exp $
+
+ Copyright (C) 2000 Herbert Valerio Riedel <hvr@gnu.org>
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+*/
+
+#ifndef __CDIO_UTIL_H__
+#define __CDIO_UTIL_H__
+
+#include <stdlib.h>
+
+#undef MAX
+#define MAX(a, b) (((a) > (b)) ? (a) : (b))
+
+#undef MIN
+#define MIN(a, b) (((a) < (b)) ? (a) : (b))
+
+#undef IN
+#define IN(x, low, high) ((x) >= (low) && (x) <= (high))
+
+#undef CLAMP
+#define CLAMP(x, low, high) (((x) > (high)) ? (high) : (((x) < (low)) ? (low) : (x)))
+
+static inline unsigned
+_cdio_len2blocks (unsigned len, int blocksize)
+{
+ unsigned blocks;
+
+ blocks = len / blocksize;
+ if (len % blocksize)
+ blocks++;
+
+ return blocks;
+}
+
+/* round up to next block boundary */
+static inline unsigned
+_cdio_ceil2block (unsigned offset, int blocksize)
+{
+ return _cdio_len2blocks (offset, blocksize) * blocksize;
+}
+
+static inline unsigned
+_cdio_ofs_add (unsigned offset, unsigned length, int blocksize)
+{
+ if (blocksize - (offset % blocksize) < length)
+ offset = _cdio_ceil2block (offset, blocksize);
+
+ offset += length;
+
+ return offset;
+}
+
+void *
+_cdio_malloc (size_t size);
+
+void *
+_cdio_memdup (const void *mem, size_t count);
+
+char *
+_cdio_strdup_upper (const char str[]);
+
+void
+_cdio_strfreev(char **strv);
+
+char *
+_cdio_strjoin (char *strv[], unsigned count, const char delim[]);
+
+size_t
+_cdio_strlenv(char **str_array);
+
+char **
+_cdio_strsplit(const char str[], char delim);
+
+static inline const char *
+_cdio_bool_str (bool b)
+{
+ return b ? "yes" : "no";
+}
+
+/* BCD */
+
+uint8_t to_bcd8(uint8_t n);
+uint8_t from_bcd8(uint8_t p);
+
+#endif /* __CDIO_UTIL_H__ */
+
+
+/*
+ * Local variables:
+ * c-file-style: "gnu"
+ * tab-width: 8
+ * indent-tabs-mode: nil
+ * End:
+ */
diff --git a/src/input/vcd/libcdio/cdio/version.h b/src/input/vcd/libcdio/cdio/version.h
new file mode 100644
index 000000000..c8bbce698
--- /dev/null
+++ b/src/input/vcd/libcdio/cdio/version.h
@@ -0,0 +1 @@
+#define CDIO_VERSION "0.64-cvs"
diff --git a/src/input/vcd/libcdio/cdio/xa.h b/src/input/vcd/libcdio/cdio/xa.h
new file mode 100644
index 000000000..06e5df250
--- /dev/null
+++ b/src/input/vcd/libcdio/cdio/xa.h
@@ -0,0 +1,121 @@
+/*
+ $Id: xa.h,v 1.1 2003/10/13 11:47:12 f1rmb Exp $
+
+ Copyright (C) 2000 Herbert Valerio Riedel <hvr@gnu.org>
+ Copyright (C) 2003 Rocky Bernstein <rocky@panix.com>
+
+ See also iso9660.h by Eric Youngdale (1993) and in cdrtools. These
+ are
+
+ Copyright 1993 Yggdrasil Computing, Incorporated
+ Copyright (c) 1999,2000 J. Schilling
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+*/
+
+#ifndef __CDIO_XA_H__
+#define __CDIO_XA_H__
+
+#include <cdio/types.h>
+
+#define ISO_XA_MARKER_STRING "CD-XA001"
+#define ISO_XA_MARKER_OFFSET 1024
+
+/* XA attribute definitions */
+#define XA_PERM_RSYS 0x0001 /* System Group Read */
+#define XA_PERM_XSYS 0x0004 /* System Group Execute */
+
+#define XA_PERM_RUSR 0x0010 /* User (owner) Read */
+#define XA_PERM_XUSR 0x0040 /* User (owner) Execute */
+
+#define XA_PERM_RGRP 0x0100 /* Group Read */
+#define XA_PERM_XGRP 0x0400 /* Group Execute */
+
+#define XA_PERM_ROTH 0x1000 /* Other (world) Read */
+#define XA_PERM_XOTH 0x4000 /* Other (world) Execute */
+
+#define XA_ATTR_MODE2FORM1 (1 << 11)
+#define XA_ATTR_MODE2FORM2 (1 << 12)
+#define XA_ATTR_INTERLEAVED (1 << 13)
+#define XA_ATTR_CDDA (1 << 14)
+#define XA_ATTR_DIRECTORY (1 << 15)
+
+/* some aggregations */
+#define XA_PERM_ALL_READ (XA_PERM_RUSR | XA_PERM_RSYS | XA_PERM_RGRP)
+#define XA_PERM_ALL_EXEC (XA_PERM_XUSR | XA_PERM_XSYS | XA_PERM_XGRP)
+#define XA_PERM_ALL_ALL (XA_PERM_ALL_READ | XA_PERM_ALL_EXEC)
+
+#define XA_FORM1_DIR (XA_ATTR_DIRECTORY | XA_ATTR_MODE2FORM1 | XA_PERM_ALL_ALL)
+#define XA_FORM1_FILE (XA_ATTR_MODE2FORM1 | XA_PERM_ALL_ALL)
+#define XA_FORM2_FILE (XA_ATTR_MODE2FORM2 | XA_PERM_ALL_ALL)
+
+/*
+ * Extended Attributes record according to Yellow Book.
+ */
+typedef struct iso9660_xa /* big endian!! */
+{
+ uint16_t group_id; /* 0 */
+ uint16_t user_id; /* 0 */
+ uint16_t attributes; /* XA_ATTR_ */
+ uint8_t signature[2]; /* { 'X', 'A' } */
+ uint8_t filenum; /* file number, see also XA subheader */
+ uint8_t reserved[5]; /* zero */
+} iso9660_xa_t GNUC_PACKED;
+
+
+/*!
+ Returns a string which interpreting the extended attribute xa_attr.
+ For example:
+ \verbatim
+ d---1xrxrxr
+ ---2--r-r-r
+ -a--1xrxrxr
+ \endverbatim
+
+ A description of the characters in the string follows
+ The 1st character is either "d" if the entry is a directory, or "-" if not
+ The 2nd character is either "a" if the entry is CDDA (audio), or "-" if not
+ The 3rd character is either "i" if the entry is interleaved, or "-" if not
+ The 4th character is either "2" if the entry is mode2 form2 or "-" if not
+ The 5th character is either "1" if the entry is mode2 form1 or "-" if not
+ Note that an entry will either be in mode2 form1 or mode form2. That
+ is you will either see "2-" or "-1" in the 4th & 5th positions.
+
+ The 6th and 7th characters refer to permissions for a user while the
+ the 8th and 9th characters refer to permissions for a group while, and
+ the 10th and 11th characters refer to permissions for everyone.
+
+ In each of these pairs the first character (6, 8, 10) is "x" if the
+ entry is executable. For a directory this means the directory is
+ allowed to be listed or "searched".
+ The second character of a pair (7, 9, 11) is "r" if the entry is allowed
+ to be read.
+*/
+const char *
+iso9660_get_xa_attr_str (uint16_t xa_attr);
+
+iso9660_xa_t *
+iso9660_xa_init (iso9660_xa_t *_xa, uint16_t uid, uint16_t gid, uint16_t attr,
+ uint8_t filenum);
+
+#endif /* __CDIO_XA_H__ */
+
+/*
+ * Local variables:
+ * c-file-style: "gnu"
+ * tab-width: 8
+ * indent-tabs-mode: nil
+ * End:
+ */
diff --git a/src/input/vcd/libcdio/cdio_assert.h b/src/input/vcd/libcdio/cdio_assert.h
new file mode 100644
index 000000000..03ea16763
--- /dev/null
+++ b/src/input/vcd/libcdio/cdio_assert.h
@@ -0,0 +1,59 @@
+/*
+ $Id: cdio_assert.h,v 1.1 2003/10/13 11:47:11 f1rmb Exp $
+
+ Copyright (C) 2000 Herbert Valerio Riedel <hvr@gnu.org>
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+*/
+
+#ifndef __CDIO_ASSERT_H__
+#define __CDIO_ASSERT_H__
+
+#if defined(__GNUC__)
+
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
+
+#include <cdio/types.h>
+#include <cdio/logging.h>
+
+#define cdio_assert(expr) \
+ { \
+ if (GNUC_UNLIKELY (!(expr))) cdio_log (CDIO_LOG_ASSERT, \
+ "file %s: line %d (%s): assertion failed: (%s)", \
+ __FILE__, __LINE__, __PRETTY_FUNCTION__, #expr); \
+ }
+
+#define cdio_assert_not_reached() \
+ { \
+ cdio_log (CDIO_LOG_ASSERT, \
+ "file %s: line %d (%s): should not be reached", \
+ __FILE__, __LINE__, __PRETTY_FUNCTION__); \
+ }
+
+#else /* non GNU C */
+
+#include <assert.h>
+
+#define cdio_assert(expr) \
+ assert(expr)
+
+#define cdio_assert_not_reached() \
+ assert(0)
+
+#endif
+
+#endif /* __CDIO_ASSERT_H__ */
diff --git a/src/input/vcd/libcdio/cdio_private.h b/src/input/vcd/libcdio/cdio_private.h
new file mode 100644
index 000000000..85c92946d
--- /dev/null
+++ b/src/input/vcd/libcdio/cdio_private.h
@@ -0,0 +1,304 @@
+/*
+ $Id: cdio_private.h,v 1.1 2003/10/13 11:47:12 f1rmb Exp $
+
+ Copyright (C) 2003 Rocky Bernstein <rocky@panix.com>
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+*/
+
+/* Internal routines for CD I/O drivers. */
+
+
+#ifndef __CDIO_PRIVATE_H__
+#define __CDIO_PRIVATE_H__
+
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
+
+#include <cdio/cdio.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif /* __cplusplus */
+
+ /* Opaque type */
+ typedef struct _CdioDataSource CdioDataSource;
+
+ typedef struct {
+
+ /*!
+ Eject media in CD drive. If successful, as a side effect we
+ also free obj. Return 0 if success and 1 for failure.
+ */
+ int (*eject_media) (void *env);
+
+ /*!
+ Release and free resources associated with cd.
+ */
+ void (*free) (void *env);
+
+ /*!
+ Return the value associated with the key "arg".
+ */
+ const char * (*get_arg) (void *env, const char key[]);
+
+ /*!
+ Return an array of device names. if CdIo is NULL (we haven't
+ initialized a specific device driver), then find a suitable device
+ driver.
+
+ NULL is returned if we couldn't return a list of devices.
+ */
+ char ** (*get_devices) (void);
+
+ /*!
+ Return a string containing the default CD device if none is specified.
+ */
+ char * (*get_default_device)(void);
+
+ /*!
+ Return the media catalog number MCN from the CD or NULL if
+ there is none or we don't have the ability to get it.
+ */
+ char * (*get_mcn) (void *env);
+
+ /*!
+ Return the number of of the first track.
+ CDIO_INVALID_TRACK is returned on error.
+ */
+ track_t (*get_first_track_num) (void *env);
+
+ /*!
+ Return the number of tracks in the current medium.
+ CDIO_INVALID_TRACK is returned on error.
+ */
+ track_t (*get_num_tracks) (void *env);
+
+ /*!
+ Return the starting LBA for track number
+ track_num in obj. Tracks numbers start at 1.
+ The "leadout" track is specified either by
+ using track_num LEADOUT_TRACK or the total tracks+1.
+ CDIO_INVALID_LBA is returned on error.
+ */
+ lba_t (*get_track_lba) (void *env, track_t track_num);
+
+ /*!
+ Get format of track.
+ */
+ track_format_t (*get_track_format) (void *env, track_t track_num);
+
+ /*!
+ Return true if we have XA data (green, mode2 form1) or
+ XA data (green, mode2 form2). That is track begins:
+ sync - header - subheader
+ 12 4 - 8
+
+ FIXME: there's gotta be a better design for this and get_track_format?
+ */
+ bool (*get_track_green) (void *env, track_t track_num);
+
+ /*!
+ Return the starting MSF (minutes/secs/frames) for track number
+ track_num in obj. Tracks numbers start at 1.
+ The "leadout" track is specified either by
+ using track_num LEADOUT_TRACK or the total tracks+1.
+ False is returned on error.
+ */
+ bool (*get_track_msf) (void *env, track_t track_num, msf_t *msf);
+
+ /*!
+ lseek - reposition read/write file offset
+ Returns (off_t) -1 on error.
+ Similar to libc's lseek()
+ */
+ off_t (*lseek) (void *env, off_t offset, int whence);
+
+ /*!
+ Reads into buf the next size bytes.
+ Returns -1 on error.
+ Similar to libc's read()
+ */
+ ssize_t (*read) (void *env, void *buf, size_t size);
+
+ /*!
+ Reads a single mode2 sector from cd device into buf starting
+ from lsn. Returns 0 if no error.
+ */
+ int (*read_audio_sectors) (void *env, void *buf, lsn_t lsn,
+ unsigned int nblocks);
+
+ /*!
+ Reads a single mode2 sector from cd device into buf starting
+ from lsn. Returns 0 if no error.
+ */
+ int (*read_mode2_sector) (void *env, void *buf, lsn_t lsn,
+ bool mode2raw);
+
+ /*!
+ Reads nblocks of mode2 sectors from cd device into data starting
+ from lsn.
+ Returns 0 if no error.
+ */
+ int (*read_mode2_sectors) (void *env, void *buf, lsn_t lsn,
+ bool mode2raw, unsigned int nblocks);
+
+ /*!
+ Set the arg "key" with "value" in the source device.
+ */
+ int (*set_arg) (void *env, const char key[], const char value[]);
+
+ /*!
+ Return the size of the CD in logical block address (LBA) units.
+ */
+ uint32_t (*stat_size) (void *env);
+
+ } cdio_funcs;
+
+
+ /* Implementation of CdIo type */
+ struct _CdIo {
+ driver_id_t driver_id; /* Particular driver opened. */
+ cdio_funcs op; /* driver-specific routines handling implimentatin*/
+ void *env; /* environment. Passed to routine above. */
+ };
+
+ /*!
+ Things common to private device structures. Even though not all
+ devices may have some of these fields, by listing common ones
+ we facilitate writing generic routines and even cut-and-paste
+ code.
+ */
+ typedef struct {
+ char *source_name; /* Name used in open. */
+ bool init; /* True if structure has been initialized */
+ bool toc_init; /* True TOC read in */
+ int ioctls_debugged; /* for debugging */
+
+ /* Only one of the below is used. The first is for CD-ROM devices
+ and the second for stream reading (bincue, nrg, toc, network).
+ */
+ int fd; /* File descriptor of device */
+ CdioDataSource *data_source;
+ } generic_img_private_t;
+
+ /* This is used in drivers that must keep their own internal
+ position pointer for doing seeks. Stream-based drivers (like bincue,
+ nrg, toc, network) would use this.
+ */
+ typedef struct
+ {
+ off_t buff_offset; /* buffer offset in disk-image seeks. */
+ track_t index; /* Current track index in tocent. */
+ lba_t lba; /* Current LBA */
+ } internal_position_t;
+
+ CdIo * cdio_new (void *env, const cdio_funcs *funcs);
+
+ /* The below structure describes a specific CD Input driver */
+ typedef struct
+ {
+ driver_id_t id;
+ unsigned int flags;
+ const char *name;
+ const char *describe;
+ bool (*have_driver) (void);
+ CdIo *(*driver_open) (const char *source_name);
+ char *(*get_default_device) (void);
+ bool (*is_device) (const char *source_name);
+ char **(*get_devices) (void);
+ } CdIo_driver_t;
+
+ /* The below array gives of the drivers that are currently available for
+ on a particular host. */
+ extern CdIo_driver_t CdIo_driver[CDIO_MAX_DRIVER];
+
+ /* The last valid entry of Cdio_driver. -1 means uninitialzed. -2
+ means some sort of error.
+ */
+ extern int CdIo_last_driver;
+
+ /* The below array gives all drivers that can possibly appear.
+ on a particular host. */
+ extern CdIo_driver_t CdIo_all_drivers[CDIO_MAX_DRIVER+1];
+
+ /*!
+ Add/allocate a drive to the end of drives.
+ Use cdio_free_device_list() to free this device_list.
+ */
+ void cdio_add_device_list(char **device_list[], const char *drive,
+ int *num_drives);
+
+ /*!
+ Bogus eject media when there is no ejectable media, e.g. a disk image
+ We always return 2. Should we also free resources?
+ */
+ int cdio_generic_bogus_eject_media (void *env);
+
+ /*!
+ Release and free resources associated with cd.
+ */
+ void cdio_generic_free (void *env);
+
+ /*!
+ Initialize CD device.
+ */
+ bool cdio_generic_init (void *env);
+
+ /*!
+ Reads into buf the next size bytes.
+ Returns -1 on error.
+ Is in fact libc's read().
+ */
+ off_t cdio_generic_lseek (void *env, off_t offset, int whence);
+
+ /*!
+ Reads into buf the next size bytes.
+ Returns -1 on error.
+ Is in fact libc's read().
+ */
+ ssize_t cdio_generic_read (void *env, void *buf, size_t size);
+
+ /*!
+ Release and free resources associated with stream or disk image.
+ */
+ void cdio_generic_stream_free (void *env);
+
+ /*!
+ Return true if source_name could be a device containing a CD-ROM on
+ Win32
+ */
+ bool cdio_is_device_win32(const char *source_name);
+
+
+ /*!
+ Return true if source_name could be a device containing a CD-ROM on
+ most Unix servers with block and character devices.
+ */
+ bool cdio_is_device_generic(const char *source_name);
+
+
+ /*!
+ Like above, but don't give a warning device doesn't exist.
+ */
+ bool cdio_is_device_quiet_generic(const char *source_name);
+
+
+#ifdef __cplusplus
+}
+#endif /* __cplusplus */
+
+#endif /* __CDIO_PRIVATE_H__ */
diff --git a/src/input/vcd/libcdio/ds.c b/src/input/vcd/libcdio/ds.c
new file mode 100644
index 000000000..7cc62a8f9
--- /dev/null
+++ b/src/input/vcd/libcdio/ds.c
@@ -0,0 +1,249 @@
+/*
+ $Id: ds.c,v 1.1 2003/10/13 11:47:12 f1rmb Exp $
+
+ Copyright (C) 2000 Herbert Valerio Riedel <hvr@gnu.org>
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+*/
+
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
+
+#include <stdlib.h>
+#include <string.h>
+
+#include "ds.h"
+#include <cdio/util.h>
+#include <cdio/types.h>
+#include "cdio_assert.h"
+
+static const char _rcsid[] = "$Id: ds.c,v 1.1 2003/10/13 11:47:12 f1rmb Exp $";
+
+struct _CdioList
+{
+ unsigned length;
+
+ CdioListNode *begin;
+ CdioListNode *end;
+};
+
+struct _CdioListNode
+{
+ CdioList *list;
+
+ CdioListNode *next;
+
+ void *data;
+};
+
+/* impl */
+
+CdioList *
+_cdio_list_new (void)
+{
+ CdioList *new_obj = _cdio_malloc (sizeof (CdioList));
+
+ return new_obj;
+}
+
+void
+_cdio_list_free (CdioList *list, int free_data)
+{
+ while (_cdio_list_length (list))
+ _cdio_list_node_free (_cdio_list_begin (list), free_data);
+
+ free (list);
+}
+
+unsigned
+_cdio_list_length (const CdioList *list)
+{
+ cdio_assert (list != NULL);
+
+ return list->length;
+}
+
+void
+_cdio_list_prepend (CdioList *list, void *data)
+{
+ CdioListNode *new_node;
+
+ cdio_assert (list != NULL);
+
+ new_node = _cdio_malloc (sizeof (CdioListNode));
+
+ new_node->list = list;
+ new_node->next = list->begin;
+ new_node->data = data;
+
+ list->begin = new_node;
+ if (list->length == 0)
+ list->end = new_node;
+
+ list->length++;
+}
+
+void
+_cdio_list_append (CdioList *list, void *data)
+{
+ cdio_assert (list != NULL);
+
+ if (list->length == 0)
+ {
+ _cdio_list_prepend (list, data);
+ }
+ else
+ {
+ CdioListNode *new_node = _cdio_malloc (sizeof (CdioListNode));
+
+ new_node->list = list;
+ new_node->next = NULL;
+ new_node->data = data;
+
+ list->end->next = new_node;
+ list->end = new_node;
+
+ list->length++;
+ }
+}
+
+void
+_cdio_list_foreach (CdioList *list, _cdio_list_iterfunc func, void *user_data)
+{
+ CdioListNode *node;
+
+ cdio_assert (list != NULL);
+ cdio_assert (func != 0);
+
+ for (node = _cdio_list_begin (list);
+ node != NULL;
+ node = _cdio_list_node_next (node))
+ func (_cdio_list_node_data (node), user_data);
+}
+
+CdioListNode *
+_cdio_list_find (CdioList *list, _cdio_list_iterfunc cmp_func, void *user_data)
+{
+ CdioListNode *node;
+
+ cdio_assert (list != NULL);
+ cdio_assert (cmp_func != 0);
+
+ for (node = _cdio_list_begin (list);
+ node != NULL;
+ node = _cdio_list_node_next (node))
+ if (cmp_func (_cdio_list_node_data (node), user_data))
+ break;
+
+ return node;
+}
+
+CdioListNode *
+_cdio_list_begin (const CdioList *list)
+{
+ cdio_assert (list != NULL);
+
+ return list->begin;
+}
+
+CdioListNode *
+_cdio_list_end (CdioList *list)
+{
+ cdio_assert (list != NULL);
+
+ return list->end;
+}
+
+CdioListNode *
+_cdio_list_node_next (CdioListNode *node)
+{
+ if (node)
+ return node->next;
+
+ return NULL;
+}
+
+void
+_cdio_list_node_free (CdioListNode *node, int free_data)
+{
+ CdioList *list;
+ CdioListNode *prev_node;
+
+ cdio_assert (node != NULL);
+
+ list = node->list;
+
+ cdio_assert (_cdio_list_length (list) > 0);
+
+ if (free_data)
+ free (_cdio_list_node_data (node));
+
+ if (_cdio_list_length (list) == 1)
+ {
+ cdio_assert (list->begin == list->end);
+
+ list->end = list->begin = NULL;
+ list->length = 0;
+ free (node);
+ return;
+ }
+
+ cdio_assert (list->begin != list->end);
+
+ if (list->begin == node)
+ {
+ list->begin = node->next;
+ free (node);
+ list->length--;
+ return;
+ }
+
+ for (prev_node = list->begin; prev_node->next; prev_node = prev_node->next)
+ if (prev_node->next == node)
+ break;
+
+ cdio_assert (prev_node->next != NULL);
+
+ if (list->end == node)
+ list->end = prev_node;
+
+ prev_node->next = node->next;
+
+ list->length--;
+
+ free (node);
+}
+
+void *
+_cdio_list_node_data (CdioListNode *node)
+{
+ if (node)
+ return node->data;
+
+ return NULL;
+}
+
+/* eof */
+
+
+/*
+ * Local variables:
+ * c-file-style: "gnu"
+ * tab-width: 8
+ * indent-tabs-mode: nil
+ * End:
+ */
+
diff --git a/src/input/vcd/libcdio/ds.h b/src/input/vcd/libcdio/ds.h
new file mode 100644
index 000000000..046a5b0b0
--- /dev/null
+++ b/src/input/vcd/libcdio/ds.h
@@ -0,0 +1,73 @@
+/*
+ $Id: ds.h,v 1.1 2003/10/13 11:47:12 f1rmb Exp $
+
+ Copyright (C) 2000 Herbert Valerio Riedel <hvr@gnu.org>
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+*/
+
+#ifndef __CDIO_DS_H__
+#define __CDIO_DS_H__
+
+#include <cdio/types.h>
+
+/* opaque... */
+typedef struct _CdioList CdioList;
+typedef struct _CdioListNode CdioListNode;
+
+typedef int (*_cdio_list_cmp_func) (void *data1, void *data2);
+
+typedef int (*_cdio_list_iterfunc) (void *data, void *user_data);
+
+/* methods */
+CdioList *_cdio_list_new (void);
+
+void _cdio_list_free (CdioList *list, int free_data);
+
+unsigned _cdio_list_length (const CdioList *list);
+
+void _cdio_list_prepend (CdioList *list, void *data);
+
+void _cdio_list_append (CdioList *list, void *data);
+
+void _cdio_list_foreach (CdioList *list, _cdio_list_iterfunc func, void *user_data);
+
+CdioListNode *_cdio_list_find (CdioList *list, _cdio_list_iterfunc cmp_func, void *user_data);
+
+#define _CDIO_LIST_FOREACH(node, list) \
+ for (node = _cdio_list_begin (list); node; node = _cdio_list_node_next (node))
+
+/* node ops */
+
+CdioListNode *_cdio_list_begin (const CdioList *list);
+
+CdioListNode *_cdio_list_end (CdioList *list);
+
+CdioListNode *_cdio_list_node_next (CdioListNode *node);
+
+void _cdio_list_node_free (CdioListNode *node, int free_data);
+
+void *_cdio_list_node_data (CdioListNode *node);
+
+#endif /* __CDIO_DS_H__ */
+
+/*
+ * Local variables:
+ * c-file-style: "gnu"
+ * tab-width: 8
+ * indent-tabs-mode: nil
+ * End:
+ */
+
diff --git a/src/input/vcd/libcdio/iso9660.c b/src/input/vcd/libcdio/iso9660.c
new file mode 100644
index 000000000..c7d6f8a54
--- /dev/null
+++ b/src/input/vcd/libcdio/iso9660.c
@@ -0,0 +1,885 @@
+/*
+ $Id: iso9660.c,v 1.1 2003/10/13 11:47:12 f1rmb Exp $
+
+ Copyright (C) 2000 Herbert Valerio Riedel <hvr@gnu.org>
+ Copyright (C) 2003 Rocky Bernstein <rocky@panix.com>
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+*/
+
+/* Private headers */
+#include "iso9660_private.h"
+#include "cdio_assert.h"
+#include "bytesex.h"
+
+/* Public headers */
+#include <cdio/iso9660.h>
+#include <cdio/util.h>
+
+#include <time.h>
+#include <ctype.h>
+#ifdef HAVE_STRING_H
+#include <string.h>
+#endif
+#ifdef HAVE_STDIO_H
+#include <stdio.h>
+#endif
+
+static const char _rcsid[] = "$Id: iso9660.c,v 1.1 2003/10/13 11:47:12 f1rmb Exp $";
+
+/* some parameters... */
+#define SYSTEM_ID "CD-RTOS CD-BRIDGE"
+#define VOLUME_SET_ID ""
+
+static void
+pathtable_get_size_and_entries(const void *pt, unsigned int *size,
+ unsigned int *entries);
+
+/*!
+ Get time structure from structure in an ISO 9660 directory index
+ record. Even though tm_wday and tm_yday fields are not explicitly in
+ idr_date, the are calculated from the other fields.
+
+ If tm is to reflect the localtime set use_localtime true, otherwise
+ tm will reported in GMT.
+*/
+void
+iso9660_get_dtime (const iso9660_dtime_t *idr_date, bool use_localtime,
+ /*out*/ struct tm *tm)
+{
+ time_t t;
+ struct tm *temp_tm;
+
+ if (!idr_date) return;
+
+ tm->tm_year = idr_date->dt_year;
+ tm->tm_mon = idr_date->dt_month - 1;
+ tm->tm_mday = idr_date->dt_day;
+ tm->tm_hour = idr_date->dt_hour;
+ tm->tm_min = idr_date->dt_minute;
+ tm->tm_sec = idr_date->dt_second;
+
+ /* Recompute tm_wday and tm_yday via mktime. */
+ t = mktime(tm);
+
+ if (use_localtime)
+ temp_tm = localtime(&t);
+ else
+ temp_tm = gmtime(&t);
+
+ memcpy(tm, temp_tm, sizeof(struct tm));
+}
+
+/*!
+ Set time in format used in ISO 9660 directory index record
+ from a Unix time structure. */
+void
+iso9660_set_dtime (const struct tm *tm, /*out*/ iso9660_dtime_t *idr_date)
+{
+ memset (idr_date, 0, 7);
+
+ if (!tm) return;
+
+ idr_date->dt_year = tm->tm_year;
+ idr_date->dt_month = tm->tm_mon + 1;
+ idr_date->dt_day = tm->tm_mday;
+ idr_date->dt_hour = tm->tm_hour;
+ idr_date->dt_minute = tm->tm_min;
+ idr_date->dt_second = tm->tm_sec;
+
+#ifdef HAVE_TM_GMTOFF
+ /* The ISO 9660 timezone is in the range -48..+52 and each unit
+ represents a 15-minute interval. */
+ idr_date->dt_gmtoff = tm->tm_gmtoff / (15 * 60);
+
+ if (tm->tm_isdst) idr_date->dt_gmtoff -= 4;
+
+ if (idr_date->dt_gmtoff < -48 ) {
+
+ cdio_warn ("Converted ISO 9660 timezone %d is less than -48. Adjusted",
+ idr_date->dt_gmtoff);
+ idr_date->dt_gmtoff = -48;
+ } else if (idr_date->dt_gmtoff > 52) {
+ cdio_warn ("Converted ISO 9660 timezone %d is over 52. Adjusted",
+ idr_date->dt_gmtoff);
+ idr_date->dt_gmtoff = 52;
+ }
+#else
+ idr_date->dt_gmtoff = 0;
+#endif
+}
+
+/*!
+ Set "long" time in format used in ISO 9660 primary volume descriptor
+ from a Unix time structure. */
+void
+iso9660_set_ltime (const struct tm *_tm, /*out*/ iso9660_ltime_t *pvd_date)
+{
+ char *_pvd_date = (char *) pvd_date;
+
+ memset (_pvd_date, '0', 16);
+ _pvd_date[16] = (int8_t) 0; /* tz */
+
+ if (!_tm) return;
+
+ snprintf(_pvd_date, 17,
+ "%4.4d%2.2d%2.2d" "%2.2d%2.2d%2.2d" "%2.2d",
+ _tm->tm_year + 1900, _tm->tm_mon + 1, _tm->tm_mday,
+ _tm->tm_hour, _tm->tm_min, _tm->tm_sec,
+ 0 /* 1/100 secs */ );
+
+ _pvd_date[16] = (int8_t) 0; /* tz */
+}
+
+/*!
+ Convert ISO-9660 file name that stored in a directory entry into
+ what's usually listed as the file name in a listing.
+ Lowercase name, and trailing ;1's or .;1's and turn the
+ other ;'s into version numbers.
+
+ The length of the translated string is returned.
+*/
+int
+iso9660_name_translate(const char *old, char *new)
+{
+ int len = strlen(old);
+ int i;
+
+ for (i = 0; i < len; i++) {
+ unsigned char c = old[i];
+ if (!c)
+ break;
+
+ /* lower case */
+ if (isupper(c)) c = tolower(c);
+
+ /* Drop trailing '.;1' (ISO 9660:1988 7.5.1 requires period) */
+ if (c == '.' && i == len - 3 && old[i + 1] == ';' && old[i + 2] == '1')
+ break;
+
+ /* Drop trailing ';1' */
+ if (c == ';' && i == len - 2 && old[i + 1] == '1')
+ break;
+
+ /* Convert remaining ';' to '.' */
+ if (c == ';')
+ c = '.';
+
+ new[i] = c;
+ }
+ new[i] = '\0';
+ return i;
+}
+
+/*!
+ Pad string src with spaces to size len and copy this to dst. If
+ len is less than the length of src, dst will be truncated to the
+ first len characters of src.
+
+ src can also be scanned to see if it contains only ACHARs, DCHARs,
+ 7-bit ASCII chars depending on the enumeration _check.
+
+ In addition to getting changed, dst is the return value.
+ Note: this string might not be NULL terminated.
+ */
+char *
+iso9660_strncpy_pad(char dst[], const char src[], size_t len,
+ enum strncpy_pad_check _check)
+{
+ size_t rlen;
+
+ cdio_assert (dst != NULL);
+ cdio_assert (src != NULL);
+ cdio_assert (len > 0);
+
+ switch (_check)
+ {
+ int idx;
+ case ISO9660_NOCHECK:
+ break;
+
+ case ISO9660_7BIT:
+ for (idx = 0; src[idx]; idx++)
+ if ((int8_t) src[idx] < 0)
+ {
+ cdio_warn ("string '%s' fails 7bit constraint (pos = %d)",
+ src, idx);
+ break;
+ }
+ break;
+
+ case ISO9660_ACHARS:
+ for (idx = 0; src[idx]; idx++)
+ if (!iso9660_isachar (src[idx]))
+ {
+ cdio_warn ("string '%s' fails a-character constraint (pos = %d)",
+ src, idx);
+ break;
+ }
+ break;
+
+ case ISO9660_DCHARS:
+ for (idx = 0; src[idx]; idx++)
+ if (!iso9660_isdchar (src[idx]))
+ {
+ cdio_warn ("string '%s' fails d-character constraint (pos = %d)",
+ src, idx);
+ break;
+ }
+ break;
+
+ default:
+ cdio_assert_not_reached ();
+ break;
+ }
+
+ rlen = strlen (src);
+
+ if (rlen > len)
+ cdio_warn ("string '%s' is getting truncated to %d characters",
+ src, (unsigned int) len);
+
+ strncpy (dst, src, len);
+ if (rlen < len)
+ memset(dst+rlen, ' ', len-rlen);
+ return dst;
+}
+
+/*!
+ Return true if c is a DCHAR - a valid ISO-9660 level 1 character.
+ These are the ASCSII capital letters A-Z, the digits 0-9 and an
+ underscore.
+*/
+bool
+iso9660_isdchar (int c)
+{
+ if (!IN (c, 0x30, 0x5f)
+ || IN (c, 0x3a, 0x40)
+ || IN (c, 0x5b, 0x5e))
+ return false;
+
+ return true;
+}
+
+
+/*!
+ Return true if c is an ACHAR -
+ These are the DCHAR's plus some ASCII symbols including the space
+ symbol.
+*/
+bool
+iso9660_isachar (int c)
+{
+ if (!IN (c, 0x20, 0x5f)
+ || IN (c, 0x23, 0x24)
+ || c == 0x40
+ || IN (c, 0x5b, 0x5e))
+ return false;
+
+ return true;
+}
+
+void
+iso9660_set_evd(void *pd)
+{
+ struct iso_volume_descriptor ied;
+
+ cdio_assert (sizeof(struct iso_volume_descriptor) == ISO_BLOCKSIZE);
+
+ cdio_assert (pd != NULL);
+
+ memset(&ied, 0, sizeof(ied));
+
+ ied.type = to_711(ISO_VD_END);
+ iso9660_strncpy_pad (ied.id, ISO_STANDARD_ID, sizeof(ied.id), ISO9660_DCHARS);
+ ied.version = to_711(ISO_VERSION);
+
+ memcpy(pd, &ied, sizeof(ied));
+}
+
+void
+iso9660_set_pvd(void *pd,
+ const char volume_id[],
+ const char publisher_id[],
+ const char preparer_id[],
+ const char application_id[],
+ uint32_t iso_size,
+ const void *root_dir,
+ uint32_t path_table_l_extent,
+ uint32_t path_table_m_extent,
+ uint32_t path_table_size,
+ const time_t *pvd_time
+ )
+{
+ iso9660_pvd_t ipd;
+
+ cdio_assert (sizeof(iso9660_pvd_t) == ISO_BLOCKSIZE);
+
+ cdio_assert (pd != NULL);
+ cdio_assert (volume_id != NULL);
+ cdio_assert (application_id != NULL);
+
+ memset(&ipd,0,sizeof(ipd)); /* paranoia? */
+
+ /* magic stuff ... thatis CD XA marker... */
+ strcpy(((char*)&ipd)+ISO_XA_MARKER_OFFSET, ISO_XA_MARKER_STRING);
+
+ ipd.type = to_711(ISO_VD_PRIMARY);
+ iso9660_strncpy_pad (ipd.id, ISO_STANDARD_ID, 5, ISO9660_DCHARS);
+ ipd.version = to_711(ISO_VERSION);
+
+ iso9660_strncpy_pad (ipd.system_id, SYSTEM_ID, 32, ISO9660_ACHARS);
+ iso9660_strncpy_pad (ipd.volume_id, volume_id, 32, ISO9660_DCHARS);
+
+ ipd.volume_space_size = to_733(iso_size);
+
+ ipd.volume_set_size = to_723(1);
+ ipd.volume_sequence_number = to_723(1);
+ ipd.logical_block_size = to_723(ISO_BLOCKSIZE);
+
+ ipd.path_table_size = to_733(path_table_size);
+ ipd.type_l_path_table = to_731(path_table_l_extent);
+ ipd.type_m_path_table = to_732(path_table_m_extent);
+
+ cdio_assert (sizeof(ipd.root_directory_record) == 34);
+ memcpy(ipd.root_directory_record, root_dir, sizeof(ipd.root_directory_record));
+ ipd.root_directory_record[0] = 34;
+
+ iso9660_strncpy_pad (ipd.volume_set_id, VOLUME_SET_ID, 128, ISO9660_DCHARS);
+
+ iso9660_strncpy_pad (ipd.publisher_id, publisher_id, 128, ISO9660_ACHARS);
+ iso9660_strncpy_pad (ipd.preparer_id, preparer_id, 128, ISO9660_ACHARS);
+ iso9660_strncpy_pad (ipd.application_id, application_id, 128, ISO9660_ACHARS);
+
+ iso9660_strncpy_pad (ipd.copyright_file_id , "", 37, ISO9660_DCHARS);
+ iso9660_strncpy_pad (ipd.abstract_file_id , "", 37, ISO9660_DCHARS);
+ iso9660_strncpy_pad (ipd.bibliographic_file_id, "", 37, ISO9660_DCHARS);
+
+ iso9660_set_ltime (gmtime (pvd_time), &(ipd.creation_date));
+ iso9660_set_ltime (gmtime (pvd_time), &(ipd.modification_date));
+ iso9660_set_ltime (NULL, &(ipd.expiration_date));
+ iso9660_set_ltime (NULL, &(ipd.effective_date));
+
+ ipd.file_structure_version = to_711(1);
+
+ /* we leave ipd.application_data = 0 */
+
+ memcpy(pd, &ipd, sizeof(ipd)); /* copy stuff to arg ptr */
+}
+
+unsigned int
+iso9660_dir_calc_record_size(unsigned int namelen, unsigned int su_len)
+{
+ unsigned int length;
+
+ length = sizeof(iso9660_dir_t);
+ length += namelen;
+ if (length % 2) /* pad to word boundary */
+ length++;
+ length += su_len;
+ if (length % 2) /* pad to word boundary again */
+ length++;
+
+ return length;
+}
+
+void
+iso9660_dir_add_entry_su(void *dir,
+ const char filename[],
+ uint32_t extent,
+ uint32_t size,
+ uint8_t file_flags,
+ const void *su_data,
+ unsigned int su_size,
+ const time_t *entry_time)
+{
+ iso9660_dir_t *idr = dir;
+ uint8_t *dir8 = dir;
+ unsigned int offset = 0;
+ uint32_t dsize = from_733(idr->size);
+ int length, su_offset;
+ cdio_assert (sizeof(iso9660_dir_t) == 33);
+
+ if (!dsize && !idr->length)
+ dsize = ISO_BLOCKSIZE; /* for when dir lacks '.' entry */
+
+ cdio_assert (dsize > 0 && !(dsize % ISO_BLOCKSIZE));
+ cdio_assert (dir != NULL);
+ cdio_assert (extent > 17);
+ cdio_assert (filename != NULL);
+ cdio_assert (strlen(filename) <= MAX_ISOPATHNAME);
+
+ length = sizeof(iso9660_dir_t);
+ length += strlen(filename);
+ length = _cdio_ceil2block (length, 2); /* pad to word boundary */
+ su_offset = length;
+ length += su_size;
+ length = _cdio_ceil2block (length, 2); /* pad to word boundary again */
+
+ /* find the last entry's end */
+ {
+ unsigned int ofs_last_rec = 0;
+
+ offset = 0;
+ while (offset < dsize)
+ {
+ if (!dir8[offset])
+ {
+ offset++;
+ continue;
+ }
+
+ offset += dir8[offset];
+ ofs_last_rec = offset;
+ }
+
+ cdio_assert (offset == dsize);
+
+ offset = ofs_last_rec;
+ }
+
+ /* be sure we don't cross sectors boundaries */
+ offset = _cdio_ofs_add (offset, length, ISO_BLOCKSIZE);
+ offset -= length;
+
+ cdio_assert (offset + length <= dsize);
+
+ idr = (iso9660_dir_t *) &dir8[offset];
+
+ cdio_assert (offset+length < dsize);
+
+ memset(idr, 0, length);
+
+ idr->length = to_711(length);
+ idr->extent = to_733(extent);
+ idr->size = to_733(size);
+
+ iso9660_set_dtime (gmtime(entry_time), &(idr->recording_time));
+
+ idr->file_flags = to_711(file_flags);
+
+ idr->volume_sequence_number = to_723(1);
+
+ idr->filename_len = to_711(strlen(filename)
+ ? strlen(filename) : 1); /* working hack! */
+
+ memcpy(idr->filename, filename, from_711(idr->filename_len));
+ memcpy(&dir8[offset] + su_offset, su_data, su_size);
+}
+
+void
+iso9660_dir_init_new (void *dir,
+ uint32_t self,
+ uint32_t ssize,
+ uint32_t parent,
+ uint32_t psize,
+ const time_t *dir_time)
+{
+ iso9660_dir_init_new_su (dir, self, ssize, NULL, 0, parent, psize, NULL,
+ 0, dir_time);
+}
+
+void
+iso9660_dir_init_new_su (void *dir,
+ uint32_t self,
+ uint32_t ssize,
+ const void *ssu_data,
+ unsigned int ssu_size,
+ uint32_t parent,
+ uint32_t psize,
+ const void *psu_data,
+ unsigned int psu_size,
+ const time_t *dir_time)
+{
+ cdio_assert (ssize > 0 && !(ssize % ISO_BLOCKSIZE));
+ cdio_assert (psize > 0 && !(psize % ISO_BLOCKSIZE));
+ cdio_assert (dir != NULL);
+
+ memset (dir, 0, ssize);
+
+ /* "\0" -- working hack due to padding */
+ iso9660_dir_add_entry_su (dir, "\0", self, ssize, ISO_DIRECTORY, ssu_data,
+ ssu_size, dir_time);
+
+ iso9660_dir_add_entry_su (dir, "\1", parent, psize, ISO_DIRECTORY, psu_data,
+ psu_size, dir_time);
+}
+
+/* Zero's out pathable. Do this first. */
+void
+iso9660_pathtable_init (void *pt)
+{
+ cdio_assert (sizeof (struct iso_path_table) == 8);
+
+ cdio_assert (pt != NULL);
+
+ memset (pt, 0, ISO_BLOCKSIZE); /* fixme */
+}
+
+static const struct iso_path_table*
+pathtable_get_entry (const void *pt, unsigned int entrynum)
+{
+ const uint8_t *tmp = pt;
+ unsigned int offset = 0;
+ unsigned int count = 0;
+
+ cdio_assert (pt != NULL);
+
+ while (from_711 (*tmp))
+ {
+ if (count == entrynum)
+ break;
+
+ cdio_assert (count < entrynum);
+
+ offset += sizeof (struct iso_path_table);
+ offset += from_711 (*tmp);
+ if (offset % 2)
+ offset++;
+ tmp = (uint8_t *)pt + offset;
+ count++;
+ }
+
+ if (!from_711 (*tmp))
+ return NULL;
+
+ return (const void *) tmp;
+}
+
+void
+pathtable_get_size_and_entries (const void *pt,
+ unsigned int *size,
+ unsigned int *entries)
+{
+ const uint8_t *tmp = pt;
+ unsigned int offset = 0;
+ unsigned int count = 0;
+
+ cdio_assert (pt != NULL);
+
+ while (from_711 (*tmp))
+ {
+ offset += sizeof (struct iso_path_table);
+ offset += from_711 (*tmp);
+ if (offset % 2)
+ offset++;
+ tmp = (uint8_t *)pt + offset;
+ count++;
+ }
+
+ if (size)
+ *size = offset;
+
+ if (entries)
+ *entries = count;
+}
+
+unsigned int
+iso9660_pathtable_get_size (const void *pt)
+{
+ unsigned int size = 0;
+ pathtable_get_size_and_entries (pt, &size, NULL);
+ return size;
+}
+
+uint16_t
+iso9660_pathtable_l_add_entry (void *pt,
+ const char name[],
+ uint32_t extent,
+ uint16_t parent)
+{
+ struct iso_path_table *ipt =
+ (struct iso_path_table*)((char *)pt + iso9660_pathtable_get_size (pt));
+ size_t name_len = strlen (name) ? strlen (name) : 1;
+ unsigned int entrynum = 0;
+
+ cdio_assert (iso9660_pathtable_get_size (pt) < ISO_BLOCKSIZE); /*fixme */
+
+ memset (ipt, 0, sizeof (struct iso_path_table) + name_len); /* paranoia */
+
+ ipt->name_len = to_711 (name_len);
+ ipt->extent = to_731 (extent);
+ ipt->parent = to_721 (parent);
+ memcpy (ipt->name, name, name_len);
+
+ pathtable_get_size_and_entries (pt, NULL, &entrynum);
+
+ if (entrynum > 1)
+ {
+ const struct iso_path_table *ipt2
+ = pathtable_get_entry (pt, entrynum - 2);
+
+ cdio_assert (ipt2 != NULL);
+
+ cdio_assert (from_721 (ipt2->parent) <= parent);
+ }
+
+ return entrynum;
+}
+
+uint16_t
+iso9660_pathtable_m_add_entry (void *pt,
+ const char name[],
+ uint32_t extent,
+ uint16_t parent)
+{
+ struct iso_path_table *ipt =
+ (struct iso_path_table*)((char *)pt + iso9660_pathtable_get_size (pt));
+ size_t name_len = strlen (name) ? strlen (name) : 1;
+ unsigned int entrynum = 0;
+
+ cdio_assert (iso9660_pathtable_get_size(pt) < ISO_BLOCKSIZE); /* fixme */
+
+ memset(ipt, 0, sizeof (struct iso_path_table) + name_len); /* paranoia */
+
+ ipt->name_len = to_711 (name_len);
+ ipt->extent = to_732 (extent);
+ ipt->parent = to_722 (parent);
+ memcpy (ipt->name, name, name_len);
+
+ pathtable_get_size_and_entries (pt, NULL, &entrynum);
+
+ if (entrynum > 1)
+ {
+ const struct iso_path_table *ipt2
+ = pathtable_get_entry (pt, entrynum - 2);
+
+ cdio_assert (ipt2 != NULL);
+
+ cdio_assert (from_722 (ipt2->parent) <= parent);
+ }
+
+ return entrynum;
+}
+
+/*!
+ Check that pathname is a valid ISO-9660 directory name.
+
+ A valid directory name should not start out with a slash (/),
+ dot (.) or null byte, should be less than 37 characters long,
+ have no more than 8 characters in a directory component
+ which is separated by a /, and consist of only DCHARs.
+ */
+bool
+iso9660_dirname_valid_p (const char pathname[])
+{
+ const char *p = pathname;
+ int len;
+
+ cdio_assert (pathname != NULL);
+
+ if (*p == '/' || *p == '.' || *p == '\0')
+ return false;
+
+ if (strlen (pathname) > MAX_ISOPATHNAME)
+ return false;
+
+ len = 0;
+ for (; *p; p++)
+ if (iso9660_isdchar (*p))
+ {
+ len++;
+ if (len > 8)
+ return false;
+ }
+ else if (*p == '/')
+ {
+ if (!len)
+ return false;
+ len = 0;
+ }
+ else
+ return false; /* unexpected char */
+
+ if (!len)
+ return false; /* last char may not be '/' */
+
+ return true;
+}
+
+/*!
+ Check that pathname is a valid ISO-9660 pathname.
+
+ A valid pathname contains a valid directory name, if one appears and
+ the filename portion should be no more than 8 characters for the
+ file prefix and 3 characters in the extension (or portion after a
+ dot). There should be exactly one dot somewhere in the filename
+ portion and the filename should be composed of only DCHARs.
+
+ True is returned if pathname is valid.
+ */
+bool
+iso9660_pathname_valid_p (const char pathname[])
+{
+ const char *p = NULL;
+
+ cdio_assert (pathname != NULL);
+
+ if ((p = strrchr (pathname, '/')))
+ {
+ bool rc;
+ char *_tmp = strdup (pathname);
+
+ *strrchr (_tmp, '/') = '\0';
+
+ rc = iso9660_dirname_valid_p (_tmp);
+
+ free (_tmp);
+
+ if (!rc)
+ return false;
+
+ p++;
+ }
+ else
+ p = pathname;
+
+ if (strlen (pathname) > (MAX_ISOPATHNAME - 6))
+ return false;
+
+ {
+ int len = 0;
+ int dots = 0;
+
+ for (; *p; p++)
+ if (iso9660_isdchar (*p))
+ {
+ len++;
+ if (dots == 0 ? len > 8 : len > 3)
+ return false;
+ }
+ else if (*p == '.')
+ {
+ dots++;
+ if (dots > 1)
+ return false;
+ if (!len)
+ return false;
+ len = 0;
+ }
+ else
+ return false;
+
+ if (dots != 1)
+ return false;
+ }
+
+ return true;
+}
+
+/*!
+ Take pathname and a version number and turn that into a ISO-9660
+ pathname. (That's just the pathname followd by ";" and the version
+ number. For example, mydir/file.ext -> mydir/file.ext;1 for version
+ 1. The resulting ISO-9660 pathname is returned.
+*/
+char *
+iso9660_pathname_isofy (const char pathname[], uint16_t version)
+{
+ char tmpbuf[1024] = { 0, };
+
+ cdio_assert (strlen (pathname) < (sizeof (tmpbuf) - sizeof (";65535")));
+
+ snprintf (tmpbuf, sizeof(tmpbuf), "%s;%d", pathname, version);
+
+ return strdup (tmpbuf);
+}
+
+#if FIXME
+lsn_t
+iso9660_get_dir_extent(const iso9660_dir_t *idr)
+{
+ if (NULL == idr) return 0;
+ return from_733(idr->extent);
+}
+#endif
+
+uint8_t
+iso9660_get_dir_len(const iso9660_dir_t *idr)
+{
+ if (NULL == idr) return 0;
+ return idr->length;
+}
+
+#if FIXME
+uint8_t
+iso9660_get_dir_size(const iso9660_dir_t *idr)
+{
+ if (NULL == idr) return 0;
+ return from_733(idr->size);
+}
+#endif
+
+uint8_t
+iso9660_get_pvd_type(const iso9660_pvd_t *pvd)
+{
+ if (NULL == pvd) return 255;
+ return(pvd->type);
+}
+
+const char *
+iso9660_get_pvd_id(const iso9660_pvd_t *pvd)
+{
+ if (NULL == pvd) return "ERR";
+ return(pvd->id);
+}
+
+int
+iso9660_get_pvd_space_size(const iso9660_pvd_t *pvd)
+{
+ if (NULL == pvd) return 0;
+ return from_733(pvd->volume_space_size);
+}
+
+int
+iso9660_get_pvd_block_size(const iso9660_pvd_t *pvd)
+{
+ if (NULL == pvd) return 0;
+ return from_723(pvd->logical_block_size);
+}
+
+/*! Return the primary volume id version number (of pvd).
+ If there is an error 0 is returned.
+ */
+int
+iso9660_get_pvd_version(const iso9660_pvd_t *pvd)
+{
+ if (NULL == pvd) return 0;
+ return pvd->version;
+}
+
+/*! Return the LSN of the root directory for pvd.
+ If there is an error CDIO_INVALID_LSN is returned.
+ */
+lsn_t
+iso9660_get_root_lsn(const iso9660_pvd_t *pvd)
+{
+ if (NULL == pvd)
+ return CDIO_INVALID_LSN;
+ else {
+ iso9660_dir_t *idr = (void *) pvd->root_directory_record;
+ if (NULL == idr) return CDIO_INVALID_LSN;
+ return(from_733 (idr->extent));
+ }
+}
+
+
+/*
+ * Local variables:
+ * c-file-style: "gnu"
+ * tab-width: 8
+ * indent-tabs-mode: nil
+ * End:
+ */
diff --git a/src/input/vcd/libcdio/iso9660_fs.c b/src/input/vcd/libcdio/iso9660_fs.c
new file mode 100644
index 000000000..b54cf2c0f
--- /dev/null
+++ b/src/input/vcd/libcdio/iso9660_fs.c
@@ -0,0 +1,357 @@
+/*
+ $Id: iso9660_fs.c,v 1.1 2003/10/13 11:47:12 f1rmb Exp $
+
+ Copyright (C) 2001 Herbert Valerio Riedel <hvr@gnu.org>
+ Copyright (C) 2003 Rocky Bernstein <rocky@panix.com>
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+*/
+
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
+
+#ifdef HAVE_STRING_H
+#include <string.h>
+#endif
+
+#include <cdio/cdio.h>
+#include <cdio/iso9660.h>
+#include <cdio/util.h>
+
+/* Private headers */
+#include "cdio_assert.h"
+#include "bytesex.h"
+#include "ds.h"
+
+#include <stdio.h>
+
+static const char _rcsid[] = "$Id: iso9660_fs.c,v 1.1 2003/10/13 11:47:12 f1rmb Exp $";
+
+static void
+_idr2statbuf (const iso9660_dir_t *idr, iso9660_stat_t *stat, bool is_mode2)
+{
+ iso9660_xa_t *xa_data = NULL;
+ uint8_t dir_len= iso9660_get_dir_len(idr);
+
+ memset ((void *) stat, 0, sizeof (iso9660_stat_t));
+
+ if (!dir_len) return;
+
+ stat->type = (idr->file_flags & ISO_DIRECTORY) ? _STAT_DIR : _STAT_FILE;
+ stat->lsn = from_733 (idr->extent);
+ stat->size = from_733 (idr->size);
+ stat->secsize = _cdio_len2blocks (stat->size, ISO_BLOCKSIZE);
+
+ iso9660_get_dtime(&(idr->recording_time), true, &(stat->tm));
+
+ cdio_assert (dir_len >= sizeof (iso9660_dir_t));
+
+ if (is_mode2) {
+ int su_length = iso9660_get_dir_len(idr) - sizeof (iso9660_dir_t);
+ su_length -= idr->filename_len;
+
+ if (su_length % 2)
+ su_length--;
+
+ if (su_length < 0 || su_length < sizeof (iso9660_xa_t))
+ return;
+
+ xa_data = (void *) (((char *) idr) + (iso9660_get_dir_len(idr) - su_length));
+
+ if (xa_data->signature[0] != 'X'
+ || xa_data->signature[1] != 'A')
+ {
+ cdio_warn ("XA signature not found in ISO9660's system use area;"
+ " ignoring XA attributes for this file entry.");
+ cdio_debug ("%d %d %d, '%c%c' (%d, %d)", iso9660_get_dir_len(idr),
+ idr->filename_len,
+ su_length,
+ xa_data->signature[0], xa_data->signature[1],
+ xa_data->signature[0], xa_data->signature[1]);
+ return;
+ }
+ stat->xa = *xa_data;
+ }
+
+}
+
+static char *
+_idr2name (const iso9660_dir_t *idr)
+{
+ char namebuf[256] = { 0, };
+ uint8_t len=iso9660_get_dir_len(idr);
+
+ if (!len) return NULL;
+
+ cdio_assert (len >= sizeof (iso9660_dir_t));
+
+ /* (idr->file_flags & ISO_DIRECTORY) */
+
+ if (idr->filename[0] == '\0')
+ strcpy (namebuf, ".");
+ else if (idr->filename[0] == '\1')
+ strcpy (namebuf, "..");
+ else
+ strncpy (namebuf, idr->filename, idr->filename_len);
+
+ return strdup (namebuf);
+}
+
+
+static void
+_fs_stat_root (const CdIo *cdio, iso9660_stat_t *stat, bool is_mode2)
+{
+ char block[ISO_BLOCKSIZE] = { 0, };
+ const iso9660_pvd_t *pvd = (void *) &block;
+ const iso9660_dir_t *idr = (void *) pvd->root_directory_record;
+
+ if (is_mode2) {
+ if (cdio_read_mode2_sector (cdio, &block, ISO_PVD_SECTOR, false))
+ cdio_assert_not_reached ();
+ } else {
+ if (cdio_read_mode1_sector (cdio, &block, ISO_PVD_SECTOR, false))
+ cdio_assert_not_reached ();
+ }
+
+ _idr2statbuf (idr, stat, is_mode2);
+}
+
+static int
+_fs_stat_traverse (const CdIo *cdio, const iso9660_stat_t *_root,
+ char **splitpath, /*out*/ iso9660_stat_t *buf,
+ bool is_mode2)
+{
+ unsigned offset = 0;
+ uint8_t *_dirbuf = NULL;
+
+ if (!splitpath[0])
+ {
+ *buf = *_root;
+ return 0;
+ }
+
+ if (_root->type == _STAT_FILE)
+ return -1;
+
+ cdio_assert (_root->type == _STAT_DIR);
+
+ if (_root->size != ISO_BLOCKSIZE * _root->secsize)
+ {
+ cdio_warn ("bad size for ISO9660 directory (%ud) should be (%lu)!",
+ (unsigned) _root->size,
+ (unsigned long int) ISO_BLOCKSIZE * _root->secsize);
+ }
+
+ _dirbuf = _cdio_malloc (_root->secsize * ISO_BLOCKSIZE);
+
+ if (is_mode2) {
+ if (cdio_read_mode2_sectors (cdio, _dirbuf, _root->lsn, false,
+ _root->secsize))
+ cdio_assert_not_reached ();
+ } else {
+ if (cdio_read_mode1_sectors (cdio, _dirbuf, _root->lsn, false,
+ _root->secsize))
+ cdio_assert_not_reached ();
+ }
+
+ while (offset < (_root->secsize * ISO_BLOCKSIZE))
+ {
+ const iso9660_dir_t *idr = (void *) &_dirbuf[offset];
+ iso9660_stat_t stat;
+ char *name;
+
+ if (!iso9660_get_dir_len(idr))
+ {
+ offset++;
+ continue;
+ }
+
+ name = _idr2name (idr);
+ _idr2statbuf (idr, &stat, is_mode2);
+
+ if (!strcmp (splitpath[0], name))
+ {
+ int retval = _fs_stat_traverse (cdio, &stat, &splitpath[1], buf,
+ is_mode2);
+ free (name);
+ free (_dirbuf);
+ return retval;
+ }
+
+ free (name);
+
+ offset += iso9660_get_dir_len(idr);
+ }
+
+ cdio_assert (offset == (_root->secsize * ISO_BLOCKSIZE));
+
+ /* not found */
+ free (_dirbuf);
+ return -1;
+}
+
+/*!
+ Get file status for pathname into stat. As with libc's stat, 0 is returned
+ if no error and -1 on error.
+ */
+int
+iso9660_fs_stat (const CdIo *cdio, const char pathname[],
+ /*out*/ iso9660_stat_t *stat, bool is_mode2)
+{
+ iso9660_stat_t _root;
+ int retval;
+ char **splitpath;
+
+ cdio_assert (cdio != NULL);
+ cdio_assert (pathname != NULL);
+ cdio_assert (stat != NULL);
+
+ _fs_stat_root (cdio, &_root, is_mode2);
+
+ splitpath = _cdio_strsplit (pathname, '/');
+ retval = _fs_stat_traverse (cdio, &_root, splitpath, stat, is_mode2);
+ _cdio_strfreev (splitpath);
+
+ return retval;
+}
+
+void * /* list of char* -- caller must free it */
+iso9660_fs_readdir (const CdIo *cdio, const char pathname[], bool is_mode2)
+{
+ iso9660_stat_t stat;
+
+ cdio_assert (cdio != NULL);
+ cdio_assert (pathname != NULL);
+
+ if (iso9660_fs_stat (cdio, pathname, &stat, is_mode2))
+ return NULL;
+
+ if (stat.type != _STAT_DIR)
+ return NULL;
+
+ {
+ unsigned offset = 0;
+ uint8_t *_dirbuf = NULL;
+ CdioList *retval = _cdio_list_new ();
+
+ if (stat.size != ISO_BLOCKSIZE * stat.secsize)
+ {
+ cdio_warn ("bad size for ISO9660 directory (%ud) should be (%lu)!",
+ (unsigned) stat.size,
+ (unsigned long int) ISO_BLOCKSIZE * stat.secsize);
+ }
+
+ _dirbuf = _cdio_malloc (stat.secsize * ISO_BLOCKSIZE);
+
+ if (is_mode2) {
+ if (cdio_read_mode2_sectors (cdio, _dirbuf, stat.lsn, false,
+ stat.secsize))
+ cdio_assert_not_reached ();
+ } else {
+ if (cdio_read_mode1_sectors (cdio, _dirbuf, stat.lsn, false,
+ stat.secsize))
+ cdio_assert_not_reached ();
+ }
+
+ while (offset < (stat.secsize * ISO_BLOCKSIZE))
+ {
+ const iso9660_dir_t *idr = (void *) &_dirbuf[offset];
+
+ if (!iso9660_get_dir_len(idr))
+ {
+ offset++;
+ continue;
+ }
+
+ _cdio_list_append (retval, _idr2name (idr));
+
+ offset += iso9660_get_dir_len(idr);
+ }
+
+ cdio_assert (offset == (stat.secsize * ISO_BLOCKSIZE));
+
+ free (_dirbuf);
+ return retval;
+ }
+}
+
+static bool
+find_fs_lsn_recurse (const CdIo *cdio, const char pathname[],
+ /*out*/ iso9660_stat_t *statbuf, lsn_t lsn)
+{
+ CdioList *entlist = iso9660_fs_readdir (cdio, pathname, true);
+ CdioList *dirlist = _cdio_list_new ();
+ CdioListNode *entnode;
+
+ cdio_assert (entlist != NULL);
+
+ /* iterate over each entry in the directory */
+
+ _CDIO_LIST_FOREACH (entnode, entlist)
+ {
+ char *name = _cdio_list_node_data (entnode);
+ char _fullname[4096] = { 0, };
+
+ snprintf (_fullname, sizeof (_fullname), "%s%s", pathname, name);
+
+ if (iso9660_fs_stat (cdio, _fullname, statbuf, true))
+ cdio_assert_not_reached ();
+
+ strncat (_fullname, "/", sizeof (_fullname));
+
+ if (statbuf->type == _STAT_DIR
+ && strcmp (name, ".")
+ && strcmp (name, ".."))
+ _cdio_list_append (dirlist, strdup (_fullname));
+
+ if (statbuf->lsn == lsn) {
+ _cdio_list_free (entlist, true);
+ _cdio_list_free (dirlist, true);
+ return true;
+ }
+
+ }
+
+ _cdio_list_free (entlist, true);
+
+ /* now recurse/descend over directories encountered */
+
+ _CDIO_LIST_FOREACH (entnode, dirlist)
+ {
+ char *_fullname = _cdio_list_node_data (entnode);
+
+ if (find_fs_lsn_recurse (cdio, _fullname, statbuf, lsn)) {
+ _cdio_list_free (dirlist, true);
+ return true;
+ }
+ }
+
+ _cdio_list_free (dirlist, true);
+ return false;
+}
+
+/*!
+ Given a directory pointer, find the filesystem entry that contains
+ lsn and return information about it in stat.
+
+ Returns true if we found an entry with the lsn and false if not.
+ */
+bool
+iso9660_find_fs_lsn(const CdIo *cdio, lsn_t lsn, /*out*/ iso9660_stat_t *stat)
+{
+ return find_fs_lsn_recurse (cdio, "/", stat, lsn);
+}
+
diff --git a/src/input/vcd/libcdio/iso9660_private.h b/src/input/vcd/libcdio/iso9660_private.h
new file mode 100644
index 000000000..411664c38
--- /dev/null
+++ b/src/input/vcd/libcdio/iso9660_private.h
@@ -0,0 +1,85 @@
+/*
+ $Id: iso9660_private.h,v 1.1 2003/10/13 11:47:12 f1rmb Exp $
+
+ Copyright (C) 2000 Herbert Valerio Riedel <hvr@gnu.org>
+ Copyright (C) 2003 Rocky Bernstein <rocky@panix.com>
+
+ See also iso9660.h by Eric Youngdale (1993).
+
+ Copyright 1993 Yggdrasil Computing, Incorporated
+ Copyright (c) 1999,2000 J. Schilling
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+*/
+
+#ifndef __CDIO_ISO9660_PRIVATE_H__
+#define __CDIO_ISO9660_PRIVATE_H__
+
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
+
+#include <cdio/types.h>
+
+#define ISO_VERSION 1
+
+PRAGMA_BEGIN_PACKED
+
+struct iso_volume_descriptor {
+ uint8_t type; /* 711 */
+ char id[5];
+ uint8_t version; /* 711 */
+ char data[2041];
+} GNUC_PACKED;
+
+#define struct_iso_volume_descriptor_SIZEOF ISO_BLOCKSIZE
+
+#define struct_iso9660_pvd_SIZEOF ISO_BLOCKSIZE
+
+/*
+ * XXX JS: The next structure has an odd length!
+ * Some compilers (e.g. on Sun3/mc68020) padd the structures to even length.
+ * For this reason, we cannot use sizeof (struct iso_path_table) or
+ * sizeof (struct iso_directory_record) to compute on disk sizes.
+ * Instead, we use offsetof(..., name) and add the name size.
+ * See mkisofs.h
+ */
+
+/* We use this to help us look up the parent inode numbers. */
+
+struct iso_path_table {
+ uint8_t name_len; /* 711 */
+ uint8_t xa_len; /* 711 */
+ uint32_t extent; /* 731/732 */
+ uint16_t parent; /* 721/722 */
+ char name[EMPTY_ARRAY_SIZE];
+} GNUC_PACKED;
+
+#define struct_iso_path_table_SIZEOF 8
+
+#define struct_iso9660_dir_SIZEOF 33
+
+PRAGMA_END_PACKED
+
+#endif /* __CDIO_ISO9660_PRIVATE_H__ */
+
+
+/*
+ * Local variables:
+ * c-file-style: "gnu"
+ * tab-width: 8
+ * indent-tabs-mode: nil
+ * End:
+ */
diff --git a/src/input/vcd/libcdio/logging.c b/src/input/vcd/libcdio/logging.c
new file mode 100644
index 000000000..522d98d9a
--- /dev/null
+++ b/src/input/vcd/libcdio/logging.c
@@ -0,0 +1,142 @@
+/*
+ $Id: logging.c,v 1.1 2003/10/13 11:47:12 f1rmb Exp $
+
+ Copyright (C) 2000 Herbert Valerio Riedel <hvr@gnu.org>
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+*/
+
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
+
+#include <stdlib.h>
+#include <stdarg.h>
+#include <stdio.h>
+
+#include <cdio/logging.h>
+#include "cdio_assert.h"
+
+
+static const char _rcsid[] = "$Id: logging.c,v 1.1 2003/10/13 11:47:12 f1rmb Exp $";
+
+int cdio_loglevel_default = CDIO_LOG_WARN;
+
+static void
+default_cdio_log_handler (cdio_log_level_t level, const char message[])
+{
+ switch (level)
+ {
+ case CDIO_LOG_ERROR:
+ if (level >= cdio_loglevel_default) {
+ fprintf (stderr, "**ERROR: %s\n", message);
+ fflush (stderr);
+ }
+ exit (EXIT_FAILURE);
+ break;
+ case CDIO_LOG_DEBUG:
+ if (level >= cdio_loglevel_default) {
+ fprintf (stdout, "--DEBUG: %s\n", message);
+ }
+ break;
+ case CDIO_LOG_WARN:
+ if (level >= cdio_loglevel_default) {
+ fprintf (stdout, "++ WARN: %s\n", message);
+ }
+ break;
+ case CDIO_LOG_INFO:
+ if (level >= cdio_loglevel_default) {
+ fprintf (stdout, " INFO: %s\n", message);
+ }
+ break;
+ case CDIO_LOG_ASSERT:
+ if (level >= cdio_loglevel_default) {
+ fprintf (stderr, "!ASSERT: %s\n", message);
+ fflush (stderr);
+ }
+ abort ();
+ break;
+ default:
+ cdio_assert_not_reached ();
+ break;
+ }
+
+ fflush (stdout);
+}
+
+static cdio_log_handler_t _handler = default_cdio_log_handler;
+
+cdio_log_handler_t
+cdio_log_set_handler (cdio_log_handler_t new_handler)
+{
+ cdio_log_handler_t old_handler = _handler;
+
+ _handler = new_handler;
+
+ return old_handler;
+}
+
+static void
+cdio_logv (cdio_log_level_t level, const char format[], va_list args)
+{
+ char buf[1024] = { 0, };
+ static int in_recursion = 0;
+
+ if (in_recursion)
+ cdio_assert_not_reached ();
+
+ in_recursion = 1;
+
+ vsnprintf(buf, sizeof(buf)-1, format, args);
+
+ _handler(level, buf);
+
+ in_recursion = 0;
+}
+
+void
+cdio_log (cdio_log_level_t level, const char format[], ...)
+{
+ va_list args;
+ va_start (args, format);
+ cdio_logv (level, format, args);
+ va_end (args);
+}
+
+#define CDIO_LOG_TEMPLATE(level, LEVEL) \
+void \
+cdio_ ## level (const char format[], ...) \
+{ \
+ va_list args; \
+ va_start (args, format); \
+ cdio_logv (CDIO_LOG_ ## LEVEL, format, args); \
+ va_end (args); \
+}
+
+CDIO_LOG_TEMPLATE(debug, DEBUG)
+CDIO_LOG_TEMPLATE(info, INFO)
+CDIO_LOG_TEMPLATE(warn, WARN)
+CDIO_LOG_TEMPLATE(error, ERROR)
+
+#undef CDIO_LOG_TEMPLATE
+
+
+/*
+ * Local variables:
+ * c-file-style: "gnu"
+ * tab-width: 8
+ * indent-tabs-mode: nil
+ * End:
+ */
diff --git a/src/input/vcd/libcdio/scsi_mmc.h b/src/input/vcd/libcdio/scsi_mmc.h
new file mode 100644
index 000000000..a0c22a86f
--- /dev/null
+++ b/src/input/vcd/libcdio/scsi_mmc.h
@@ -0,0 +1,63 @@
+/*
+ $Id: scsi_mmc.h,v 1.1 2003/10/13 11:47:12 f1rmb Exp $
+
+ Copyright (C) 2003 Rocky Bernstein <rocky@panix.com>
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+*/
+
+/*
+ This file contains common definitions/routines for SCSI MMC
+ (Multimedia commands).
+*/
+
+#ifndef __SCSI_MMC_H__
+#define __SCSI_MMC_H__
+
+/* Leval values that can go into READ_CD */
+#define CDIO_MMC_READ_TYPE_ANY 0 /* All types */
+#define CDIO_MMC_READ_TYPE_CDDA 1 /* Only CD-DA sectors */
+#define CDIO_MMC_READ_TYPE_MODE1 2 /* Only mode1 sectors (user data = 2048) */
+#define CDIO_MMC_READ_TYPE_MODE2 3 /* mode2 sectors form1 or form2 */
+#define CDIO_MMC_READ_TYPE_M2F1 4 /* mode2 sectors form1 */
+#define CDIO_MMC_READ_TYPE_M2F2 5 /* mode2 sectors form2 */
+
+/* The generic packet command opcodes for CD/DVD Logical Units,
+ * From Table 57 of the SFF8090 Ver. 3 (Mt. Fuji) draft standard. */
+#define CDIO_MMC_GPCMD_READ_CD 0xbe
+#define CDIO_MMC_GPCMD_READ_10 0x28
+#define CDIO_MMC_GPCMD_READ_12 0xa8
+
+#define CDIO_MMC_SET_READ_TYPE(rec, sector_type) \
+ rec[1] = (sector_type << 2)
+
+
+#define CDIO_MMC_SET_READ_LBA(rec, lba) \
+ rec[2] = (lba >> 24) & 0xff; \
+ rec[3] = (lba >> 16) & 0xff; \
+ rec[4] = (lba >> 8) & 0xff; \
+ rec[5] = (lba ) & 0xff
+
+#define CDIO_MMC_SET_READ_LENGTH(rec, len) \
+ rec[6] = (len >> 16) & 0xff; \
+ rec[7] = (len >> 8) & 0xff; \
+ rec[8] = (len ) & 0xff
+
+#define CDIO_MMC_MCSB_ALL_HEADERS 0x78
+
+#define CDIO_MMC_SET_MAIN_CHANNEL_SELECTION_BITS(rec, val) \
+ rec[9] = val;
+
+#endif /* __SCSI_MMC_H__ */
diff --git a/src/input/vcd/libcdio/sector.c b/src/input/vcd/libcdio/sector.c
new file mode 100644
index 000000000..24082bc2d
--- /dev/null
+++ b/src/input/vcd/libcdio/sector.c
@@ -0,0 +1,111 @@
+/*
+ $Id: sector.c,v 1.1 2003/10/13 11:47:12 f1rmb Exp $
+
+ Copyright (C) 2000 Herbert Valerio Riedel <hvr@gnu.org>
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+*/
+
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
+
+#include <cdio/sector.h>
+#include <cdio/util.h>
+#include "cdio_assert.h"
+
+#include <stdio.h>
+#ifdef HAVE_STRING_H
+#include <string.h>
+#endif
+
+
+static const char _rcsid[] = "$Id: sector.c,v 1.1 2003/10/13 11:47:12 f1rmb Exp $";
+
+lba_t
+cdio_lba_to_lsn (lba_t lba)
+{
+ return lba - CDIO_PREGAP_SECTORS;
+}
+
+void
+cdio_lba_to_msf (lba_t lba, msf_t *msf)
+{
+ cdio_assert (msf != 0);
+
+ msf->m = to_bcd8 (lba / (60 * 75));
+ msf->s = to_bcd8 ((lba / 75) % 60);
+ msf->f = to_bcd8 (lba % 75);
+}
+
+/* warning, returns new allocated string */
+char *
+cdio_lba_to_msf_str (lba_t lba)
+{
+ char buf[16];
+ msf_t _msf = { .m = 0, .s = 0, .f = 0 };
+
+ cdio_lba_to_msf (lba, &_msf);
+
+ snprintf (buf, sizeof (buf), "%.2x:%.2x.%.2x", _msf.m, _msf.s, _msf.f);
+
+ return strdup (buf);
+}
+
+lba_t
+cdio_lsn_to_lba (lsn_t lsn)
+{
+ return lsn + CDIO_PREGAP_SECTORS;
+}
+
+void
+cdio_lsn_to_msf (lsn_t lsn, msf_t *msf)
+{
+ cdio_assert (msf != 0);
+ cdio_lba_to_msf(cdio_lsn_to_lba(lsn), msf);
+}
+
+uint32_t
+cdio_msf_to_lba (const msf_t *msf)
+{
+ uint32_t lba = 0;
+
+ cdio_assert (msf != 0);
+
+ lba = from_bcd8 (msf->m);
+ lba *= 60;
+
+ lba += from_bcd8 (msf->s);
+ lba *= 75;
+
+ lba += from_bcd8 (msf->f);
+
+ return lba;
+}
+
+uint32_t
+cdio_msf_to_lsn (const msf_t *msf)
+{
+ return cdio_lba_to_lsn(cdio_msf_to_lba (msf));
+}
+
+
+/*
+ * Local variables:
+ * c-file-style: "gnu"
+ * tab-width: 8
+ * indent-tabs-mode: nil
+ * End:
+ */
diff --git a/src/input/vcd/libcdio/util.c b/src/input/vcd/libcdio/util.c
new file mode 100644
index 000000000..3b5b832cc
--- /dev/null
+++ b/src/input/vcd/libcdio/util.c
@@ -0,0 +1,192 @@
+/*
+ $Id: util.c,v 1.1 2003/10/13 11:47:12 f1rmb Exp $
+
+ Copyright (C) 2000 Herbert Valerio Riedel <hvr@gnu.org>
+ Copyright (C) 2003 Rocky Bernstein <rocky@panix.com>
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+*/
+
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
+
+#include <ctype.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+
+#include "cdio_assert.h"
+#include <cdio/util.h>
+
+static const char _rcsid[] = "$Id: util.c,v 1.1 2003/10/13 11:47:12 f1rmb Exp $";
+
+size_t
+_cdio_strlenv(char **str_array)
+{
+ size_t n = 0;
+
+ cdio_assert (str_array != NULL);
+
+ while(str_array[n])
+ n++;
+
+ return n;
+}
+
+void
+_cdio_strfreev(char **strv)
+{
+ int n;
+
+ cdio_assert (strv != NULL);
+
+ for(n = 0; strv[n]; n++)
+ free(strv[n]);
+
+ free(strv);
+}
+
+char *
+_cdio_strjoin (char *strv[], unsigned count, const char delim[])
+{
+ size_t len;
+ char *new_str;
+ unsigned n;
+
+ cdio_assert (strv != NULL);
+ cdio_assert (delim != NULL);
+
+ len = (count-1) * strlen (delim);
+
+ for (n = 0;n < count;n++)
+ len += strlen (strv[n]);
+
+ len++;
+
+ new_str = _cdio_malloc (len);
+ new_str[0] = '\0';
+
+ for (n = 0;n < count;n++)
+ {
+ if (n)
+ strcat (new_str, delim);
+ strcat (new_str, strv[n]);
+ }
+
+ return new_str;
+}
+
+char **
+_cdio_strsplit(const char str[], char delim) /* fixme -- non-reentrant */
+{
+ int n;
+ char **strv = NULL;
+ char *_str, *p;
+ char _delim[2] = { 0, 0 };
+
+ cdio_assert (str != NULL);
+
+ _str = strdup(str);
+ _delim[0] = delim;
+
+ cdio_assert (_str != NULL);
+
+ n = 1;
+ p = _str;
+ while(*p)
+ if (*(p++) == delim)
+ n++;
+
+ strv = _cdio_malloc (sizeof (char *) * (n+1));
+
+ n = 0;
+ while((p = strtok(n ? NULL : _str, _delim)) != NULL)
+ strv[n++] = strdup(p);
+
+ free(_str);
+
+ return strv;
+}
+
+void *
+_cdio_malloc (size_t size)
+{
+ void *new_mem = malloc (size);
+
+ cdio_assert (new_mem != NULL);
+
+ memset (new_mem, 0, size);
+
+ return new_mem;
+}
+
+void *
+_cdio_memdup (const void *mem, size_t count)
+{
+ void *new_mem = NULL;
+
+ if (mem)
+ {
+ new_mem = _cdio_malloc (count);
+ memcpy (new_mem, mem, count);
+ }
+
+ return new_mem;
+}
+
+char *
+_cdio_strdup_upper (const char str[])
+{
+ char *new_str = NULL;
+
+ if (str)
+ {
+ char *p;
+
+ p = new_str = strdup (str);
+
+ while (*p)
+ {
+ *p = toupper (*p);
+ p++;
+ }
+ }
+
+ return new_str;
+}
+
+uint8_t
+to_bcd8 (uint8_t n)
+{
+ cdio_assert (n < 100);
+
+ return ((n/10)<<4) | (n%10);
+}
+
+uint8_t
+from_bcd8(uint8_t p)
+{
+ return (0xf & p)+(10*(p >> 4));
+}
+
+
+/*
+ * Local variables:
+ * c-file-style: "gnu"
+ * tab-width: 8
+ * indent-tabs-mode: nil
+ * End:
+ */
diff --git a/src/input/vcd/libcdio/xa.c b/src/input/vcd/libcdio/xa.c
new file mode 100644
index 000000000..31c5e110a
--- /dev/null
+++ b/src/input/vcd/libcdio/xa.c
@@ -0,0 +1,136 @@
+/*
+ $Id: xa.c,v 1.1 2003/10/13 11:47:12 f1rmb Exp $
+
+ Copyright (C) 2000 Herbert Valerio Riedel <hvr@gnu.org>
+ Copyright (C) 2003 Rocky Bernstein <rocky@panix.com>
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+*/
+
+
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
+
+#ifdef HAVE_STRING_H
+#include <string.h>
+#endif
+
+/* Public headers */
+#include <cdio/iso9660.h>
+#include <cdio/util.h>
+
+/* Private headers */
+#include "cdio_assert.h"
+#include "bytesex.h"
+
+#define BUF_COUNT 16
+#define BUF_SIZE 80
+
+/* Return a pointer to a internal free buffer */
+static char *
+_getbuf (void)
+{
+ static char _buf[BUF_COUNT][BUF_SIZE];
+ static int _num = -1;
+
+ _num++;
+ _num %= BUF_COUNT;
+
+ memset (_buf[_num], 0, BUF_SIZE);
+
+ return _buf[_num];
+}
+
+/*!
+ Returns a string which interpreting the extended attribute xa_attr.
+ For example:
+ \verbatim
+ d---1xrxrxr
+ ---2--r-r-r
+ -a--1xrxrxr
+ \endverbatim
+
+ A description of the characters in the string follows
+ The 1st character is either "d" if the entry is a directory, or "-" if not.
+ The 2nd character is either "a" if the entry is CDDA (audio), or "-" if not.
+ The 3rd character is either "i" if the entry is interleaved, or "-" if not.
+ The 4th character is either "2" if the entry is mode2 form2 or "-" if not.
+ The 5th character is either "1" if the entry is mode2 form1 or "-" if not.
+ Note that an entry will either be in mode2 form1 or mode form2. That
+ is you will either see "2-" or "-1" in the 4th & 5th positions.
+
+ The 6th and 7th characters refer to permissions for a user while the
+ the 8th and 9th characters refer to permissions for a group while, and
+ the 10th and 11th characters refer to permissions for a others.
+
+ In each of these pairs the first character (6, 8, 10) is "x" if the
+ entry is executable. For a directory this means the directory is
+ allowed to be listed or "searched".
+ The second character of a pair (7, 9, 11) is "r" if the entry is allowed
+ to be read.
+*/
+
+const char *
+iso9660_get_xa_attr_str (uint16_t xa_attr)
+{
+ char *result = _getbuf();
+
+ xa_attr = uint16_from_be (xa_attr);
+
+ result[ 0] = (xa_attr & XA_ATTR_DIRECTORY) ? 'd' : '-';
+ result[ 1] = (xa_attr & XA_ATTR_CDDA) ? 'a' : '-';
+ result[ 2] = (xa_attr & XA_ATTR_INTERLEAVED) ? 'i' : '-';
+ result[ 3] = (xa_attr & XA_ATTR_MODE2FORM2) ? '2' : '-';
+ result[ 4] = (xa_attr & XA_ATTR_MODE2FORM1) ? '1' : '-';
+
+ result[ 5] = (xa_attr & XA_PERM_XUSR) ? 'x' : '-';
+ result[ 6] = (xa_attr & XA_PERM_RUSR) ? 'r' : '-';
+
+ result[ 7] = (xa_attr & XA_PERM_XGRP) ? 'x' : '-';
+ result[ 8] = (xa_attr & XA_PERM_RGRP) ? 'r' : '-';
+
+ /* Hack alert: wonder if this should be ROTH and XOTH? */
+ result[ 9] = (xa_attr & XA_PERM_XSYS) ? 'x' : '-';
+ result[10] = (xa_attr & XA_PERM_RSYS) ? 'r' : '-';
+
+ result[11] = '\0';
+
+ return result;
+}
+
+iso9660_xa_t *
+iso9660_xa_init (iso9660_xa_t *_xa, uint16_t uid, uint16_t gid, uint16_t attr,
+ uint8_t filenum)
+{
+ cdio_assert (_xa != NULL);
+
+ _xa->user_id = uint16_to_be (uid);
+ _xa->group_id = uint16_to_be (gid);
+ _xa->attributes = uint16_to_be (attr);
+
+ _xa->signature[0] = 'X';
+ _xa->signature[1] = 'A';
+
+ _xa->filenum = filenum;
+
+ _xa->reserved[0]
+ = _xa->reserved[1]
+ = _xa->reserved[2]
+ = _xa->reserved[3]
+ = _xa->reserved[4] = 0x00;
+
+ return _xa;
+}
diff --git a/src/input/vcd/libvcd/Makefile.am b/src/input/vcd/libvcd/Makefile.am
new file mode 100644
index 000000000..8703aadcf
--- /dev/null
+++ b/src/input/vcd/libvcd/Makefile.am
@@ -0,0 +1,84 @@
+SUBDIR = libvcd
+
+LIBTOOL = $(SHELL) $(top_builddir)/libtool-nofpic
+INCLUDES = $(LIBCDIO_CFLAGS)
+
+libvcd_SRCS = \
+ vcd.c \
+ data_structures.c \
+ directory.c \
+ files.c \
+ image.c \
+ image_bincue.c \
+ image_cdrdao.c \
+ image_nrg.c \
+ logging.c \
+ mpeg.c \
+ mpeg_stream.c \
+ pbc.c \
+ salloc.c \
+ sector.c \
+ stream.c \
+ stream_stdio.c \
+ util.c
+
+libvcdinfo_SRCS = \
+ info.c \
+ inf.c \
+ info_private.c \
+ vcd_read.c
+
+if ENABLE_VCDX
+if HAVE_VCDNAV
+EXTRA_DIST = $(libvcd_SRCS) $(libvcdinfo_SRCS)
+else
+noinst_LTLIBRARIES = libvcd.la libvcdinfo.la
+libvcd_la_SOURCES = $(libvcd_SRCS)
+libvcd_la_LDFLAGS = -avoid-version -module
+libvcd_la_LIBADD = $(LIBCDIO_LIBS) $(LIBISO9660_LIBS)
+libvcdinfo_la_SOURCES = $(libvcdinfo_SRCS)
+libvcdinfo_la_LDFLAGS = -avoid-version -module
+endif
+endif
+
+noinst_HEADERS = \
+ assert.h \
+ data_structures.h \
+ info_private.h \
+ pbc.h \
+ stream_stdio.h \
+ bitvec.h \
+ dict.h \
+ mpeg.h \
+ salloc.h \
+ util.h \
+ bytesex_asm.h \
+ directory.h \
+ mpeg_stream.h \
+ sector_private.h \
+ vcd.h \
+ bytesex.h \
+ image_sink.h \
+ obj.h \
+ stream.h \
+ vcd_read.h
+
+debug:
+ @list='$(SUBDIRS)'; for subdir in $$list; do \
+ (cd $$subdir && $(MAKE) $@) || exit;\
+ done;
+ @$(MAKE) CFLAGS="$(DEBUG_CFLAGS)"
+
+install-debug: debug
+ @list='$(SUBDIRS)'; for subdir in $$list; do \
+ (cd $$subdir && $(MAKE) $@) || exit;\
+ done;
+ @$(MAKE) $(AM_MAKEFLAGS) install-exec-am install-data-am
+
+mostlyclean-generic:
+ -rm -f *~ \#* .*~ .\#*
+
+maintainer-clean-generic:
+ -@echo "This command is intended for maintainers to use;"
+ -@echo "it deletes files that may require special tools to rebuild."
+ -rm -f Makefile.in
diff --git a/src/input/vcd/libvcd/assert.h b/src/input/vcd/libvcd/assert.h
new file mode 100644
index 000000000..6013c4c43
--- /dev/null
+++ b/src/input/vcd/libvcd/assert.h
@@ -0,0 +1,55 @@
+/*
+ $Id: assert.h,v 1.1 2003/10/13 11:47:12 f1rmb Exp $
+
+ Copyright (C) 2000 Herbert Valerio Riedel <hvr@gnu.org>
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+*/
+
+#ifndef __VCD_ASSERT_H__
+#define __VCD_ASSERT_H__
+
+#if defined(__GNUC__)
+
+#include <libvcd/types.h>
+#include <libvcd/logging.h>
+
+#define vcd_assert(expr) \
+ { \
+ if (GNUC_UNLIKELY (!(expr))) vcd_log (VCD_LOG_ASSERT, \
+ "file %s: line %d (%s): assertion failed: (%s)", \
+ __FILE__, __LINE__, __PRETTY_FUNCTION__, #expr); \
+ }
+
+#define vcd_assert_not_reached() \
+ { \
+ vcd_log (VCD_LOG_ASSERT, \
+ "file %s: line %d (%s): should not be reached", \
+ __FILE__, __LINE__, __PRETTY_FUNCTION__); \
+ }
+
+#else /* non GNU C */
+
+#include <assert.h>
+
+#define vcd_assert(expr) \
+ assert(expr)
+
+#define vcd_assert_not_reached() \
+ assert(0)
+
+#endif
+
+#endif /* __VCD_ASSERT_H__ */
diff --git a/src/input/vcd/libvcd/bitvec.h b/src/input/vcd/libvcd/bitvec.h
new file mode 100644
index 000000000..ca0198ca2
--- /dev/null
+++ b/src/input/vcd/libvcd/bitvec.h
@@ -0,0 +1,131 @@
+/*
+ $Id: bitvec.h,v 1.1 2003/10/13 11:47:12 f1rmb Exp $
+
+ Copyright (C) 2000 Herbert Valerio Riedel <hvr@gnu.org>
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+*/
+
+#ifndef __VCD_BITVEC_H__
+#define __VCD_BITVEC_H__
+
+#include <libvcd/types.h>
+
+#include "assert.h"
+
+static inline bool
+_vcd_bit_set_p (const uint32_t n, const unsigned bit)
+{
+ return ((n >> bit) & 0x1) == 0x1;
+}
+
+static inline int
+vcd_bitvec_align (int value, const int boundary)
+{
+ if (value % boundary)
+ value += (boundary - (value % boundary));
+
+ return value;
+}
+
+/*
+ * PEEK
+ */
+
+#define vcd_bitvec_peek_bits16(bitvec, offset) \
+ vcd_bitvec_peek_bits ((bitvec), (offset), 16)
+
+static inline uint32_t
+vcd_bitvec_peek_bits (const uint8_t bitvec[],
+ const unsigned offset,
+ const unsigned bits)
+{
+ uint32_t result = 0;
+ unsigned i = offset;
+
+ vcd_assert (bits > 0 && bits <= 32);
+
+#if 0
+ j = 0;
+ while (j < bits)
+ if (i % 8 || (bits - j) < 8)
+ {
+ result <<= 1;
+ if (_vcd_bit_set_p (bitvec[i >> 3], 7 - (i % 8)))
+ result |= 0x1;
+ j++, i++;
+ }
+ else
+ {
+ result <<= 8;
+ result |= bitvec[i >> 3];
+ j += 8, i += 8;
+ }
+#else
+ if (!(offset % 8) && !(bits % 8)) /* optimization */
+ for (i = offset; i < (offset + bits); i+= 8)
+ {
+ result <<= 8;
+ result |= bitvec[i >> 3];
+ }
+ else /* general case */
+ for (i = offset; i < (offset + bits); i++)
+ {
+ result <<= 1;
+ if (_vcd_bit_set_p (bitvec[i >> 3], 7 - (i % 8)))
+ result |= 0x1;
+ }
+#endif
+
+ return result;
+}
+
+static inline uint32_t
+vcd_bitvec_peek_bits32 (const uint8_t bitvec[], unsigned offset)
+{
+ if (offset % 8)
+ return vcd_bitvec_peek_bits (bitvec, offset, 32);
+
+ offset >>= 3;
+
+ return (bitvec[offset] << 24
+ | bitvec[offset + 1] << 16
+ | bitvec[offset + 2] << 8
+ | bitvec[offset + 3]);
+}
+
+/*
+ * READ
+ */
+
+static inline uint32_t
+vcd_bitvec_read_bits (const uint8_t bitvec[], unsigned *offset, const unsigned bits)
+{
+ const unsigned i = *offset;
+
+ *offset += bits;
+
+ return vcd_bitvec_peek_bits (bitvec, i, bits);
+}
+
+static inline bool
+vcd_bitvec_read_bit (const uint8_t bitvec[], unsigned *offset)
+{
+ const unsigned i = (*offset)++;
+
+ return _vcd_bit_set_p (bitvec[i >> 3], 7 - (i % 8));
+}
+
+#endif /* __VCD_BITVEC_H__ */
diff --git a/src/input/vcd/libvcd/bytesex.h b/src/input/vcd/libvcd/bytesex.h
new file mode 100644
index 000000000..7bfdee8c7
--- /dev/null
+++ b/src/input/vcd/libvcd/bytesex.h
@@ -0,0 +1,199 @@
+/*
+ $Id: bytesex.h,v 1.1 2003/10/13 11:47:12 f1rmb Exp $
+
+ Copyright (C) 2000 Herbert Valerio Riedel <hvr@gnu.org>
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+*/
+
+#ifndef __VCD_BYTESEX_H__
+#define __VCD_BYTESEX_H__
+
+#include <cdio/cdio.h>
+#include <libvcd/types.h>
+#include <libvcd/logging.h>
+
+/* Private includes */
+#include "bytesex_asm.h"
+
+/* generic byteswap routines */
+
+#define UINT16_SWAP_LE_BE_C(val) ((uint16_t) ( \
+ (((uint16_t) (val) & (uint16_t) 0x00ffU) << 8) | \
+ (((uint16_t) (val) & (uint16_t) 0xff00U) >> 8)))
+
+#define UINT32_SWAP_LE_BE_C(val) ((uint32_t) ( \
+ (((uint32_t) (val) & (uint32_t) 0x000000ffU) << 24) | \
+ (((uint32_t) (val) & (uint32_t) 0x0000ff00U) << 8) | \
+ (((uint32_t) (val) & (uint32_t) 0x00ff0000U) >> 8) | \
+ (((uint32_t) (val) & (uint32_t) 0xff000000U) >> 24)))
+
+#define UINT64_SWAP_LE_BE_C(val) ((uint64_t) ( \
+ (((uint64_t) (val) & (uint64_t) UINT64_C(0x00000000000000ff)) << 56) | \
+ (((uint64_t) (val) & (uint64_t) UINT64_C(0x000000000000ff00)) << 40) | \
+ (((uint64_t) (val) & (uint64_t) UINT64_C(0x0000000000ff0000)) << 24) | \
+ (((uint64_t) (val) & (uint64_t) UINT64_C(0x00000000ff000000)) << 8) | \
+ (((uint64_t) (val) & (uint64_t) UINT64_C(0x000000ff00000000)) >> 8) | \
+ (((uint64_t) (val) & (uint64_t) UINT64_C(0x0000ff0000000000)) >> 24) | \
+ (((uint64_t) (val) & (uint64_t) UINT64_C(0x00ff000000000000)) >> 40) | \
+ (((uint64_t) (val) & (uint64_t) UINT64_C(0xff00000000000000)) >> 56)))
+
+#ifndef UINT16_SWAP_LE_BE
+# define UINT16_SWAP_LE_BE UINT16_SWAP_LE_BE_C
+#endif
+
+#ifndef UINT32_SWAP_LE_BE
+# define UINT32_SWAP_LE_BE UINT32_SWAP_LE_BE_C
+#endif
+
+#ifndef UINT64_SWAP_LE_BE
+# define UINT64_SWAP_LE_BE UINT64_SWAP_LE_BE_C
+#endif
+
+inline static
+uint16_t uint16_swap_le_be (const uint16_t val)
+{
+ return UINT16_SWAP_LE_BE (val);
+}
+
+inline static
+uint32_t uint32_swap_le_be (const uint32_t val)
+{
+ return UINT32_SWAP_LE_BE (val);
+}
+
+inline static
+uint64_t uint64_swap_le_be (const uint64_t val)
+{
+ return UINT64_SWAP_LE_BE (val);
+}
+
+# define UINT8_TO_BE(val) ((uint8_t) (val))
+# define UINT8_TO_LE(val) ((uint8_t) (val))
+#ifdef WORDS_BIGENDIAN
+# define UINT16_TO_BE(val) ((uint16_t) (val))
+# define UINT16_TO_LE(val) ((uint16_t) UINT16_SWAP_LE_BE(val))
+
+# define UINT32_TO_BE(val) ((uint32_t) (val))
+# define UINT32_TO_LE(val) ((uint32_t) UINT32_SWAP_LE_BE(val))
+
+# define UINT64_TO_BE(val) ((uint64_t) (val))
+# define UINT64_TO_LE(val) ((uint64_t) UINT64_SWAP_LE_BE(val))
+#else
+# define UINT16_TO_BE(val) ((uint16_t) UINT16_SWAP_LE_BE(val))
+# define UINT16_TO_LE(val) ((uint16_t) (val))
+
+# define UINT32_TO_BE(val) ((uint32_t) UINT32_SWAP_LE_BE(val))
+# define UINT32_TO_LE(val) ((uint32_t) (val))
+
+# define UINT64_TO_BE(val) ((uint64_t) UINT64_SWAP_LE_BE(val))
+# define UINT64_TO_LE(val) ((uint64_t) (val))
+#endif
+
+/* symmetric conversions */
+#define UINT8_FROM_BE(val) (UINT8_TO_BE (val))
+#define UINT8_FROM_LE(val) (UINT8_TO_LE (val))
+#define UINT16_FROM_BE(val) (UINT16_TO_BE (val))
+#define UINT16_FROM_LE(val) (UINT16_TO_LE (val))
+#define UINT32_FROM_BE(val) (UINT32_TO_BE (val))
+#define UINT32_FROM_LE(val) (UINT32_TO_LE (val))
+#define UINT64_FROM_BE(val) (UINT64_TO_BE (val))
+#define UINT64_FROM_LE(val) (UINT64_TO_LE (val))
+
+/* converter function template */
+#define CVT_TO_FUNC(bits) \
+ static inline uint ## bits ## _t \
+ uint ## bits ## _to_be (uint ## bits ## _t val) \
+ { return UINT ## bits ## _TO_BE (val); } \
+ static inline uint ## bits ## _t \
+ uint ## bits ## _to_le (uint ## bits ## _t val) \
+ { return UINT ## bits ## _TO_LE (val); } \
+
+CVT_TO_FUNC(8)
+CVT_TO_FUNC(16)
+CVT_TO_FUNC(32)
+CVT_TO_FUNC(64)
+
+#undef CVT_TO_FUNC
+
+#define uint8_from_be(val) (uint8_to_be (val))
+#define uint8_from_le(val) (uint8_to_le (val))
+#define uint16_from_be(val) (uint16_to_be (val))
+#define uint16_from_le(val) (uint16_to_le (val))
+#define uint32_from_be(val) (uint32_to_be (val))
+#define uint32_from_le(val) (uint32_to_le (val))
+#define uint64_from_be(val) (uint64_to_be (val))
+#define uint64_from_le(val) (uint64_to_le (val))
+
+/* ISO9660 related stuff */
+
+#define to_711(i) uint8_to_le(i)
+#define from_711(i) uint8_from_le(i)
+
+#define to_721(i) uint16_to_le(i)
+#define from_721(i) uint16_from_le(i)
+
+#define to_721(i) uint16_to_le(i)
+#define from_721(i) uint16_from_le(i)
+
+#define to_722(i) uint16_to_be(i)
+#define from_722(i) uint16_from_be(i)
+
+static inline uint32_t
+to_723(uint16_t i)
+{
+ return uint32_swap_le_be(i) | i;
+}
+
+static inline uint16_t
+from_723 (uint32_t p)
+{
+ if (uint32_swap_le_be (p) != p)
+ vcd_warn ("from_723: broken byte order");
+
+ return (0xFFFF & p);
+}
+
+#define to_731(i) uint32_to_le(i)
+#define from_731(i) uint32_from_le(i)
+
+#define to_732(i) uint32_to_be(i)
+#define from_732(i) uint32_from_be(i)
+
+static inline uint64_t
+to_733(uint32_t i)
+{
+ return uint64_swap_le_be(i) | i;
+}
+
+static inline uint32_t
+from_733 (uint64_t p)
+{
+ if (uint64_swap_le_be (p) != p)
+ vcd_warn ("from_733: broken byte order");
+
+ return (UINT32_C(0xFFFFFFFF) & p);
+}
+
+#endif /* __VCD_BYTESEX_H__ */
+
+
+/*
+ * Local variables:
+ * c-file-style: "gnu"
+ * tab-width: 8
+ * indent-tabs-mode: nil
+ * End:
+ */
diff --git a/src/input/vcd/libvcd/bytesex_asm.h b/src/input/vcd/libvcd/bytesex_asm.h
new file mode 100644
index 000000000..21c7402da
--- /dev/null
+++ b/src/input/vcd/libvcd/bytesex_asm.h
@@ -0,0 +1,123 @@
+/*
+ $Id: bytesex_asm.h,v 1.1 2003/10/13 11:47:12 f1rmb Exp $
+
+ Copyright (C) 2001 Sven Ottemann <ac-logic@freenet.de>
+ 2001 Herbert Valerio Riedel <hvr@gnu.org>
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+*/
+
+#ifndef __VCD_BYTESEX_ASM_H__
+#define __VCD_BYTESEX_ASM_H__
+#if !defined(DISABLE_ASM_OPTIMIZE)
+
+#include <libvcd/types.h>
+
+#if defined(__powerpc__) && defined(__GNUC__)
+
+inline static
+uint32_t uint32_swap_le_be_asm(const uint32_t a)
+{
+ uint32_t b;
+
+ __asm__ ("lwbrx %0,0,%1"
+ :"=r"(b)
+ :"r"(&a), "m"(a));
+
+ return b;
+}
+
+inline static
+uint16_t uint16_swap_le_be_asm(const uint16_t a)
+{
+ uint32_t b;
+
+ __asm__ ("lhbrx %0,0,%1"
+ :"=r"(b)
+ :"r"(&a), "m"(a));
+
+ return b;
+}
+
+#define UINT16_SWAP_LE_BE uint16_swap_le_be_asm
+#define UINT32_SWAP_LE_BE uint32_swap_le_be_asm
+
+#elif defined(__mc68000__) && defined(__STORMGCC__)
+
+inline static
+uint32_t uint32_swap_le_be_asm(uint32_t a __asm__("d0"))
+{
+ /* __asm__("rolw #8,%0; swap %0; rolw #8,%0" : "=d" (val) : "0" (val)); */
+
+ __asm__("move.l %1,d0;rol.w #8,d0;swap d0;rol.w #8,d0;move.l d0,%0"
+ :"=r"(a)
+ :"r"(a));
+
+ return(a);
+}
+
+inline static
+uint16_t uint16_swap_le_be_asm(uint16_t a __asm__("d0"))
+{
+ __asm__("move.l %1,d0;rol.w #8,d0;move.l d0,%0"
+ :"=r"(a)
+ :"r"(a));
+
+ return(a);
+}
+
+#define UINT16_SWAP_LE_BE uint16_swap_le_be_asm
+#define UINT32_SWAP_LE_BE uint32_swap_le_be_asm
+
+#elif 0 && defined(__i386__) && defined(__GNUC__)
+
+inline static
+uint32_t uint32_swap_le_be_asm(uint32_t a)
+{
+ __asm__("xchgb %b0,%h0\n\t" /* swap lower bytes */
+ "rorl $16,%0\n\t" /* swap words */
+ "xchgb %b0,%h0" /* swap higher bytes */
+ :"=q" (a)
+ : "0" (a));
+
+ return(a);
+}
+
+inline static
+uint16_t uint16_swap_le_be_asm(uint16_t a)
+{
+ __asm__("xchgb %b0,%h0" /* swap bytes */
+ : "=q" (a)
+ : "0" (a));
+
+ return(a);
+}
+
+#define UINT16_SWAP_LE_BE uint16_swap_le_be_asm
+#define UINT32_SWAP_LE_BE uint32_swap_le_be_asm
+
+#endif
+
+#endif /* !defined(DISABLE_ASM_OPTIMIZE) */
+#endif /* __VCD_BYTESEX_ASM_H__ */
+
+
+/*
+ * Local variables:
+ * c-file-style: "gnu"
+ * tab-width: 8
+ * indent-tabs-mode: nil
+ * End:
+ */
diff --git a/src/input/vcd/libvcd/data_structures.c b/src/input/vcd/libvcd/data_structures.c
new file mode 100644
index 000000000..5b3785b88
--- /dev/null
+++ b/src/input/vcd/libvcd/data_structures.c
@@ -0,0 +1,526 @@
+/*
+ $Id: data_structures.c,v 1.1 2003/10/13 11:47:12 f1rmb Exp $
+
+ Copyright (C) 2000 Herbert Valerio Riedel <hvr@gnu.org>
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+*/
+
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
+
+#include <stdlib.h>
+#include <string.h>
+
+#include <cdio/cdio.h>
+
+/* Public headers */
+#include <libvcd/types.h>
+
+/* Private headers */
+#include "assert.h"
+#include "data_structures.h"
+#include "util.h"
+
+static const char _rcsid[] = "$Id: data_structures.c,v 1.1 2003/10/13 11:47:12 f1rmb Exp $";
+
+struct _VcdList
+{
+ unsigned length;
+
+ VcdListNode *begin;
+ VcdListNode *end;
+};
+
+struct _VcdListNode
+{
+ VcdList *list;
+
+ VcdListNode *next;
+
+ void *data;
+};
+
+/* impl */
+
+VcdList *
+_vcd_list_new (void)
+{
+ VcdList *new_obj = _vcd_malloc (sizeof (VcdList));
+
+ return new_obj;
+}
+
+void
+_vcd_list_free (VcdList *list, int free_data)
+{
+ while (_vcd_list_length (list))
+ _vcd_list_node_free (_vcd_list_begin (list), free_data);
+
+ free (list);
+}
+
+unsigned
+_vcd_list_length (const VcdList *list)
+{
+ vcd_assert (list != NULL);
+
+ return list->length;
+}
+
+static bool
+_bubble_sort_iteration (VcdList *list, _vcd_list_cmp_func cmp_func)
+{
+ VcdListNode **pnode;
+ bool changed = false;
+
+ for (pnode = &(list->begin);
+ (*pnode) != NULL && (*pnode)->next != NULL;
+ pnode = &((*pnode)->next))
+ {
+ VcdListNode *node = *pnode;
+
+ if (cmp_func (node->data, node->next->data) <= 0)
+ continue; /* n <= n->next */
+
+ /* exch n n->next */
+ *pnode = node->next;
+ node->next = node->next->next;
+ (*pnode)->next = node;
+
+ changed = true;
+
+ if (node->next == NULL)
+ list->end = node;
+ }
+
+ return changed;
+}
+
+void _vcd_list_sort (VcdList *list, _vcd_list_cmp_func cmp_func)
+{
+ /* fixme -- this is bubble sort -- worst sorting algo... */
+
+ vcd_assert (list != NULL);
+ vcd_assert (cmp_func != 0);
+
+ while (_bubble_sort_iteration (list, cmp_func));
+}
+
+void
+_vcd_list_prepend (VcdList *list, void *data)
+{
+ VcdListNode *new_node;
+
+ vcd_assert (list != NULL);
+
+ new_node = _vcd_malloc (sizeof (VcdListNode));
+
+ new_node->list = list;
+ new_node->next = list->begin;
+ new_node->data = data;
+
+ list->begin = new_node;
+ if (list->length == 0)
+ list->end = new_node;
+
+ list->length++;
+}
+
+void
+_vcd_list_append (VcdList *list, void *data)
+{
+ vcd_assert (list != NULL);
+
+ if (list->length == 0)
+ {
+ _vcd_list_prepend (list, data);
+ }
+ else
+ {
+ VcdListNode *new_node = _vcd_malloc (sizeof (VcdListNode));
+
+ new_node->list = list;
+ new_node->next = NULL;
+ new_node->data = data;
+
+ list->end->next = new_node;
+ list->end = new_node;
+
+ list->length++;
+ }
+}
+
+void
+_vcd_list_foreach (VcdList *list, _vcd_list_iterfunc func, void *user_data)
+{
+ VcdListNode *node;
+
+ vcd_assert (list != NULL);
+ vcd_assert (func != 0);
+
+ for (node = _vcd_list_begin (list);
+ node != NULL;
+ node = _vcd_list_node_next (node))
+ func (_vcd_list_node_data (node), user_data);
+}
+
+VcdListNode *
+_vcd_list_find (VcdList *list, _vcd_list_iterfunc cmp_func, void *user_data)
+{
+ VcdListNode *node;
+
+ vcd_assert (list != NULL);
+ vcd_assert (cmp_func != 0);
+
+ for (node = _vcd_list_begin (list);
+ node != NULL;
+ node = _vcd_list_node_next (node))
+ if (cmp_func (_vcd_list_node_data (node), user_data))
+ break;
+
+ return node;
+}
+
+/* node ops */
+
+VcdListNode *
+_vcd_list_at (VcdList *list, int idx)
+{
+ VcdListNode *node = _vcd_list_begin (list);
+
+ if (idx < 0)
+ return _vcd_list_at (list, _vcd_list_length (list) + idx);
+
+ vcd_assert (idx >= 0);
+
+ while (node && idx)
+ {
+ node = _vcd_list_node_next (node);
+ idx--;
+ }
+
+ return node;
+}
+
+VcdListNode *
+_vcd_list_begin (const VcdList *list)
+{
+ vcd_assert (list != NULL);
+
+ return list->begin;
+}
+
+VcdListNode *
+_vcd_list_end (VcdList *list)
+{
+ vcd_assert (list != NULL);
+
+ return list->end;
+}
+
+VcdListNode *
+_vcd_list_node_next (VcdListNode *node)
+{
+ if (node)
+ return node->next;
+
+ return NULL;
+}
+
+void
+_vcd_list_node_free (VcdListNode *node, int free_data)
+{
+ VcdList *list;
+ VcdListNode *prev_node;
+
+ vcd_assert (node != NULL);
+
+ list = node->list;
+
+ vcd_assert (_vcd_list_length (list) > 0);
+
+ if (free_data)
+ free (_vcd_list_node_data (node));
+
+ if (_vcd_list_length (list) == 1)
+ {
+ vcd_assert (list->begin == list->end);
+
+ list->end = list->begin = NULL;
+ list->length = 0;
+ free (node);
+ return;
+ }
+
+ vcd_assert (list->begin != list->end);
+
+ if (list->begin == node)
+ {
+ list->begin = node->next;
+ free (node);
+ list->length--;
+ return;
+ }
+
+ for (prev_node = list->begin; prev_node->next; prev_node = prev_node->next)
+ if (prev_node->next == node)
+ break;
+
+ vcd_assert (prev_node->next != NULL);
+
+ if (list->end == node)
+ list->end = prev_node;
+
+ prev_node->next = node->next;
+
+ list->length--;
+
+ free (node);
+}
+
+void *
+_vcd_list_node_data (VcdListNode *node)
+{
+ if (node)
+ return node->data;
+
+ return NULL;
+}
+
+/*
+ * n-way tree based on list -- somewhat inefficent
+ */
+
+struct _VcdTree
+{
+ VcdTreeNode *root;
+};
+
+struct _VcdTreeNode
+{
+ void *data;
+
+ VcdListNode *listnode;
+ VcdTree *tree;
+ VcdTreeNode *parent;
+ VcdList *children;
+};
+
+VcdTree *
+_vcd_tree_new (void *root_data)
+{
+ VcdTree *new_tree;
+
+ new_tree = _vcd_malloc (sizeof (VcdTree));
+
+ new_tree->root = _vcd_malloc (sizeof (VcdTreeNode));
+
+ new_tree->root->data = root_data;
+ new_tree->root->tree = new_tree;
+ new_tree->root->parent = NULL;
+ new_tree->root->children = NULL;
+ new_tree->root->listnode = NULL;
+
+ return new_tree;
+}
+
+void
+_vcd_tree_destroy (VcdTree *tree, bool free_data)
+{
+ _vcd_tree_node_destroy (tree->root, free_data);
+
+ free (tree->root);
+ free (tree);
+}
+
+void
+_vcd_tree_node_destroy (VcdTreeNode *node, bool free_data)
+{
+ VcdTreeNode *child, *nxt_child;
+
+ vcd_assert (node != NULL);
+
+ child = _vcd_tree_node_first_child (node);
+ while(child) {
+ nxt_child = _vcd_tree_node_next_sibling (child);
+ _vcd_tree_node_destroy (child, free_data);
+ child = nxt_child;
+ }
+
+ if (node->children)
+ {
+ vcd_assert (_vcd_list_length (node->children) == 0);
+ _vcd_list_free (node->children, true);
+ node->children = NULL;
+ }
+
+ if (free_data)
+ free (_vcd_tree_node_set_data (node, NULL));
+
+ if (node->parent)
+ _vcd_list_node_free (node->listnode, true);
+ else
+ _vcd_tree_node_set_data (node, NULL);
+}
+
+VcdTreeNode *
+_vcd_tree_root (VcdTree *tree)
+{
+ return tree->root;
+}
+
+void *
+_vcd_tree_node_data (VcdTreeNode *node)
+{
+ return node->data;
+}
+
+void *
+_vcd_tree_node_set_data (VcdTreeNode *node, void *new_data)
+{
+ void *old_data = node->data;
+
+ node->data = new_data;
+
+ return old_data;
+}
+
+VcdTreeNode *
+_vcd_tree_node_append_child (VcdTreeNode *pnode, void *cdata)
+{
+ VcdTreeNode *nnode;
+
+ vcd_assert (pnode != NULL);
+
+ if (!pnode->children)
+ pnode->children = _vcd_list_new ();
+
+ nnode = _vcd_malloc (sizeof (VcdTreeNode));
+
+ _vcd_list_append (pnode->children, nnode);
+
+ nnode->data = cdata;
+ nnode->parent = pnode;
+ nnode->tree = pnode->tree;
+ nnode->listnode = _vcd_list_end (pnode->children);
+
+ return nnode;
+}
+
+VcdTreeNode *
+_vcd_tree_node_first_child (VcdTreeNode *node)
+{
+ vcd_assert (node != NULL);
+
+ if (!node->children)
+ return NULL;
+
+ return _vcd_list_node_data (_vcd_list_begin (node->children));
+}
+
+VcdTreeNode *
+_vcd_tree_node_next_sibling (VcdTreeNode *node)
+{
+ vcd_assert (node != NULL);
+
+ return _vcd_list_node_data (_vcd_list_node_next (node->listnode));
+}
+
+void
+_vcd_tree_node_sort_children (VcdTreeNode *node, _vcd_tree_node_cmp_func cmp_func)
+{
+ vcd_assert (node != NULL);
+
+ if (node->children)
+ _vcd_list_sort (node->children, (_vcd_list_cmp_func) cmp_func);
+}
+
+void
+_vcd_tree_node_traverse (VcdTreeNode *node,
+ _vcd_tree_node_traversal_func trav_func,
+ void *user_data) /* pre-order */
+{
+ VcdTreeNode *child;
+
+ vcd_assert (node != NULL);
+
+ trav_func (node, user_data);
+
+ _VCD_CHILD_FOREACH (child, node)
+ {
+ _vcd_tree_node_traverse (child, trav_func, user_data);
+ }
+}
+
+void
+_vcd_tree_node_traverse_bf (VcdTreeNode *node,
+ _vcd_tree_node_traversal_func trav_func,
+ void *user_data) /* breath-first */
+{
+ VcdList *queue;
+
+ vcd_assert (node != NULL);
+
+ queue = _vcd_list_new ();
+
+ _vcd_list_prepend (queue, node);
+
+ while (_vcd_list_length (queue))
+ {
+ VcdListNode *lastnode = _vcd_list_end (queue);
+ VcdTreeNode *treenode = _vcd_list_node_data (lastnode);
+ VcdTreeNode *childnode;
+
+ _vcd_list_node_free (lastnode, false);
+
+ trav_func (treenode, user_data);
+
+ _VCD_CHILD_FOREACH (childnode, treenode)
+ {
+ _vcd_list_prepend (queue, childnode);
+ }
+ }
+
+ _vcd_list_free (queue, false);
+}
+
+VcdTreeNode *_vcd_tree_node_parent (VcdTreeNode *node)
+{
+ return node->parent;
+}
+
+VcdTreeNode *_vcd_tree_node_root (VcdTreeNode *node)
+{
+ return node->tree->root;
+}
+
+bool _vcd_tree_node_is_root (VcdTreeNode *node)
+{
+ return (node->parent == NULL);
+}
+
+/* eof */
+
+
+/*
+ * Local variables:
+ * c-file-style: "gnu"
+ * tab-width: 8
+ * indent-tabs-mode: nil
+ * End:
+ */
+
diff --git a/src/input/vcd/libvcd/data_structures.h b/src/input/vcd/libvcd/data_structures.h
new file mode 100644
index 000000000..332f731d9
--- /dev/null
+++ b/src/input/vcd/libvcd/data_structures.h
@@ -0,0 +1,126 @@
+/*
+ $Id: data_structures.h,v 1.1 2003/10/13 11:47:12 f1rmb Exp $
+
+ Copyright (C) 2000 Herbert Valerio Riedel <hvr@gnu.org>
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+*/
+
+#ifndef __VCD_DATA_STRUCTURES_H__
+#define __VCD_DATA_STRUCTURES_H__
+
+#include <libvcd/types.h>
+
+/* opaque... */
+
+typedef int (*_vcd_list_cmp_func) (void *data1, void *data2);
+
+typedef int (*_vcd_list_iterfunc) (void *data, void *user_data);
+
+/* methods */
+VcdList *_vcd_list_new (void);
+
+void _vcd_list_free (VcdList *list, int free_data);
+
+unsigned _vcd_list_length (const VcdList *list);
+
+void _vcd_list_sort (VcdList *list, _vcd_list_cmp_func cmp_func);
+
+void _vcd_list_prepend (VcdList *list, void *data);
+
+void _vcd_list_append (VcdList *list, void *data);
+
+void _vcd_list_foreach (VcdList *list, _vcd_list_iterfunc func, void *user_data);
+
+VcdListNode *_vcd_list_find (VcdList *list, _vcd_list_iterfunc cmp_func, void *user_data);
+
+#define _VCD_LIST_FOREACH(node, list) \
+ for (node = _vcd_list_begin (list); node; node = _vcd_list_node_next (node))
+
+/* node ops */
+
+VcdListNode *_vcd_list_at (VcdList *list, int idx);
+
+VcdListNode *_vcd_list_begin (const VcdList *list);
+
+VcdListNode *_vcd_list_end (VcdList *list);
+
+VcdListNode *_vcd_list_node_next (VcdListNode *node);
+
+void _vcd_list_node_free (VcdListNode *node, int free_data);
+
+void *_vcd_list_node_data (VcdListNode *node);
+
+
+/* n-way tree */
+
+typedef struct _VcdTree VcdTree;
+typedef struct _VcdTreeNode VcdTreeNode;
+
+#define _VCD_CHILD_FOREACH(child, parent) \
+ for (child = _vcd_tree_node_first_child (parent); child; child = _vcd_tree_node_next_sibling (child))
+
+typedef int (*_vcd_tree_node_cmp_func) (VcdTreeNode *node1,
+ VcdTreeNode *node2);
+
+typedef void (*_vcd_tree_node_traversal_func) (VcdTreeNode *node,
+ void *user_data);
+
+VcdTree *_vcd_tree_new (void *root_data);
+
+void _vcd_tree_destroy (VcdTree *tree, bool free_data);
+
+VcdTreeNode *_vcd_tree_root (VcdTree *tree);
+
+void _vcd_tree_node_sort_children (VcdTreeNode *node,
+ _vcd_tree_node_cmp_func cmp_func);
+
+void *_vcd_tree_node_data (VcdTreeNode *node);
+
+void _vcd_tree_node_destroy (VcdTreeNode *node, bool free_data);
+
+void *_vcd_tree_node_set_data (VcdTreeNode *node, void *new_data);
+
+VcdTreeNode *_vcd_tree_node_append_child (VcdTreeNode *pnode, void *cdata);
+
+VcdTreeNode *_vcd_tree_node_first_child (VcdTreeNode *node);
+
+VcdTreeNode *_vcd_tree_node_next_sibling (VcdTreeNode *node);
+
+VcdTreeNode *_vcd_tree_node_parent (VcdTreeNode *node);
+
+VcdTreeNode *_vcd_tree_node_root (VcdTreeNode *node);
+
+bool _vcd_tree_node_is_root (VcdTreeNode *node);
+
+void _vcd_tree_node_traverse (VcdTreeNode *node,
+ _vcd_tree_node_traversal_func trav_func,
+ void *user_data);
+
+void
+_vcd_tree_node_traverse_bf (VcdTreeNode *node,
+ _vcd_tree_node_traversal_func trav_func,
+ void *user_data);
+
+#endif /* __VCD_DATA_STRUCTURES_H__ */
+
+/*
+ * Local variables:
+ * c-file-style: "gnu"
+ * tab-width: 8
+ * indent-tabs-mode: nil
+ * End:
+ */
+
diff --git a/src/input/vcd/libvcd/dict.h b/src/input/vcd/libvcd/dict.h
new file mode 100644
index 000000000..cade2a3d4
--- /dev/null
+++ b/src/input/vcd/libvcd/dict.h
@@ -0,0 +1,165 @@
+/*
+ $Id: dict.h,v 1.1 2003/10/13 11:47:12 f1rmb Exp $
+
+ Copyright (C) 2000 Herbert Valerio Riedel <hvr@gnu.org>
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+*/
+
+#ifndef __VCD_DICT_H__
+#define __VCD_DICT_H__
+
+/* Public headers */
+#include <libvcd/types.h>
+
+/* Private headers */
+#include "assert.h"
+#include "obj.h"
+#include "util.h"
+
+struct _dict_t
+{
+ char *key;
+ uint32_t sector;
+ uint32_t length;
+ void *buf;
+ uint8_t flags;
+};
+
+static void
+_dict_insert (VcdObj *obj, const char key[], uint32_t sector, uint32_t length,
+ uint8_t end_flags)
+{
+ struct _dict_t *_new_node;
+
+ vcd_assert (key != NULL);
+ vcd_assert (length > 0);
+
+ if ((sector =_vcd_salloc (obj->iso_bitmap, sector, length)) == SECTOR_NIL)
+ vcd_assert_not_reached ();
+
+ _new_node = _vcd_malloc (sizeof (struct _dict_t));
+
+ _new_node->key = strdup (key);
+ _new_node->sector = sector;
+ _new_node->length = length;
+ _new_node->buf = _vcd_malloc (length * ISO_BLOCKSIZE);
+ _new_node->flags = end_flags;
+
+ _vcd_list_prepend (obj->buffer_dict_list, _new_node);
+}
+
+static
+int _dict_key_cmp (struct _dict_t *a, char *b)
+{
+ vcd_assert (a != NULL);
+ vcd_assert (b != NULL);
+
+ return !strcmp (a->key, b);
+}
+
+static
+int _dict_sector_cmp (struct _dict_t *a, uint32_t *b)
+{
+ vcd_assert (a != NULL);
+ vcd_assert (b != NULL);
+
+ return (a->sector <= *b && (*b - a->sector) < a->length);
+}
+
+static const struct _dict_t *
+_dict_get_bykey (VcdObj *obj, const char key[])
+{
+ VcdListNode *node;
+
+ vcd_assert (obj != NULL);
+ vcd_assert (key != NULL);
+
+ node = _vcd_list_find (obj->buffer_dict_list,
+ (_vcd_list_iterfunc) _dict_key_cmp,
+ (char *) key);
+
+ if (node)
+ return _vcd_list_node_data (node);
+
+ return NULL;
+}
+
+static const struct _dict_t *
+_dict_get_bysector (VcdObj *obj, uint32_t sector)
+{
+ VcdListNode *node;
+
+ vcd_assert (obj != NULL);
+ vcd_assert (sector != SECTOR_NIL);
+
+ node = _vcd_list_find (obj->buffer_dict_list,
+ (_vcd_list_iterfunc) _dict_sector_cmp,
+ &sector);
+
+ if (node)
+ return _vcd_list_node_data (node);
+
+ return NULL;
+}
+
+static uint8_t
+_dict_get_sector_flags (VcdObj *obj, uint32_t sector)
+{
+ const struct _dict_t *p;
+
+ vcd_assert (sector != SECTOR_NIL);
+
+ p = _dict_get_bysector (obj, sector);
+
+ if (p)
+ return (((sector - p->sector)+1 == p->length)
+ ? p->flags : 0);
+
+ return 0;
+}
+
+static void *
+_dict_get_sector (VcdObj *obj, uint32_t sector)
+{
+ const struct _dict_t *p;
+
+ vcd_assert (sector != SECTOR_NIL);
+
+ p = _dict_get_bysector (obj, sector);
+
+ if (p)
+ return ((char *)p->buf) + ((sector - p->sector) * ISO_BLOCKSIZE);
+
+ return NULL;
+}
+
+static void
+_dict_clean (VcdObj *obj)
+{
+ VcdListNode *node;
+
+ while ((node = _vcd_list_begin (obj->buffer_dict_list)))
+ {
+ struct _dict_t *p = _vcd_list_node_data (node);
+
+ free (p->key);
+ free (p->buf);
+
+ _vcd_list_node_free (node, true);
+ }
+}
+
+#endif /* __VCD_DICT_H__ */
diff --git a/src/input/vcd/libvcd/directory.c b/src/input/vcd/libvcd/directory.c
new file mode 100644
index 000000000..9df691f99
--- /dev/null
+++ b/src/input/vcd/libvcd/directory.c
@@ -0,0 +1,506 @@
+/*
+ $Id: directory.c,v 1.1 2003/10/13 11:47:12 f1rmb Exp $
+
+ Copyright (C) 2000 Herbert Valerio Riedel <hvr@gnu.org>
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+*/
+
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
+
+#include <stdlib.h>
+#include <string.h>
+
+/* Public headers */
+#include <cdio/iso9660.h>
+#include <libvcd/logging.h>
+
+/* Private headers */
+#include "assert.h"
+#include "bytesex.h"
+#include "directory.h"
+#include "util.h"
+
+static const char _rcsid[] = "$Id: directory.c,v 1.1 2003/10/13 11:47:12 f1rmb Exp $";
+
+/* CD-ROM XA */
+
+/* tree data structure */
+
+typedef struct
+{
+ bool is_dir;
+ char *name;
+ uint16_t version;
+ uint16_t xa_attributes;
+ uint8_t xa_filenum;
+ uint32_t extent;
+ uint32_t size;
+ unsigned pt_id;
+} data_t;
+
+typedef VcdTreeNode VcdDirNode;
+
+#define EXTENT(anode) (DATAP(anode)->extent)
+#define SIZE(anode) (DATAP(anode)->size)
+#define PT_ID(anode) (DATAP(anode)->pt_id)
+
+#define DATAP(anode) ((data_t*) _vcd_tree_node_data (anode))
+
+/* important date to celebrate (for me at least =)
+ -- until user customization is implemented... */
+static const time_t _vcd_time = 269222400L;
+
+/* implementation */
+
+static void
+traverse_get_dirsizes(VcdDirNode *node, void *data)
+{
+ data_t *d = DATAP(node);
+ unsigned *sum = data;
+
+ if (d->is_dir)
+ {
+ vcd_assert (d->size % ISO_BLOCKSIZE == 0);
+
+ *sum += (d->size / ISO_BLOCKSIZE);
+ }
+}
+
+static unsigned
+get_dirsizes (VcdDirNode* dirnode)
+{
+ unsigned result = 0;
+
+ _vcd_tree_node_traverse (dirnode, traverse_get_dirsizes, &result);
+
+ return result;
+}
+
+static void
+traverse_update_dirextents (VcdDirNode *dirnode, void *data)
+{
+ data_t *d = DATAP(dirnode);
+
+ if (d->is_dir)
+ {
+ VcdDirNode* child;
+ unsigned dirextent = d->extent;
+
+ vcd_assert (d->size % ISO_BLOCKSIZE == 0);
+
+ dirextent += d->size / ISO_BLOCKSIZE;
+
+ _VCD_CHILD_FOREACH (child, dirnode)
+ {
+ data_t *d = DATAP(child);
+
+ vcd_assert (d != NULL);
+
+ if (d->is_dir)
+ {
+ d->extent = dirextent;
+ dirextent += get_dirsizes (child);
+ }
+ }
+ }
+}
+
+static void
+update_dirextents (VcdDirectory *dir, uint32_t extent)
+{
+ data_t *dirdata = DATAP(_vcd_tree_root (dir));
+
+ dirdata->extent = extent;
+ _vcd_tree_node_traverse (_vcd_tree_root (dir),
+ traverse_update_dirextents, NULL);
+}
+
+static void
+traverse_update_sizes(VcdDirNode *node, void *data)
+{
+ data_t *dirdata = DATAP(node);
+
+ if (dirdata->is_dir)
+ {
+ VcdDirNode* child;
+ unsigned offset = 0;
+
+ offset += iso9660_dir_calc_record_size (1, sizeof(iso9660_xa_t)); /* '.' */
+ offset += iso9660_dir_calc_record_size (1, sizeof(iso9660_xa_t)); /* '..' */
+
+ _VCD_CHILD_FOREACH (child, node)
+ {
+ data_t *d = DATAP(child);
+ unsigned reclen;
+ char *pathname = (d->is_dir
+ ? strdup (d->name)
+ : iso9660_pathname_isofy (d->name, d->version));
+
+ vcd_assert (d != NULL);
+
+ reclen = iso9660_dir_calc_record_size (strlen (pathname),
+ sizeof (iso9660_xa_t));
+
+ free (pathname);
+
+ offset = _vcd_ofs_add (offset, reclen, ISO_BLOCKSIZE);
+ }
+
+ vcd_assert (offset > 0);
+
+ dirdata->size = _vcd_ceil2block (offset, ISO_BLOCKSIZE);
+ }
+}
+
+static void
+update_sizes (VcdDirectory *dir)
+{
+ _vcd_tree_node_traverse (_vcd_tree_root(dir), traverse_update_sizes, NULL);
+}
+
+
+/* exported stuff: */
+
+VcdDirectory *
+_vcd_directory_new (void)
+{
+ data_t *data;
+ VcdDirectory *dir = NULL;
+
+ vcd_assert (sizeof(iso9660_xa_t) == 14);
+
+ data = _vcd_malloc (sizeof (data_t));
+ dir = _vcd_tree_new (data);
+
+ data->is_dir = true;
+ data->name = _vcd_memdup("\0", 2);
+ data->xa_attributes = XA_FORM1_DIR;
+ data->xa_filenum = 0x00;
+
+ return dir;
+}
+
+static void
+traverse_vcd_directory_done (VcdDirNode *node, void *data)
+{
+ data_t *dirdata = DATAP (node);
+
+ free (dirdata->name);
+}
+
+void
+_vcd_directory_destroy (VcdDirectory *dir)
+{
+ vcd_assert (dir != NULL);
+
+ _vcd_tree_node_traverse (_vcd_tree_root (dir),
+ traverse_vcd_directory_done, NULL);
+
+ _vcd_tree_destroy (dir, true);
+}
+
+static VcdDirNode*
+lookup_child (VcdDirNode* node, const char name[])
+{
+ VcdDirNode* child;
+
+ _VCD_CHILD_FOREACH (child, node)
+ {
+ data_t *d = DATAP(child);
+
+ if (!strcmp (d->name, name))
+ return child;
+ }
+
+ return child; /* NULL */
+}
+
+static int
+_iso_dir_cmp (VcdDirNode *node1, VcdDirNode *node2)
+{
+ data_t *d1 = DATAP(node1);
+ data_t *d2 = DATAP(node2);
+ int result = 0;
+
+ result = strcmp (d1->name, d2->name);
+
+ return result;
+}
+
+int
+_vcd_directory_mkdir (VcdDirectory *dir, const char pathname[])
+{
+ char **splitpath;
+ unsigned level, n;
+ VcdDirNode* pdir = _vcd_tree_root (dir);
+
+ vcd_assert (dir != NULL);
+ vcd_assert (pathname != NULL);
+
+ splitpath = _vcd_strsplit (pathname, '/');
+
+ level = _vcd_strlenv (splitpath);
+
+ for (n = 0; n < level-1; n++)
+ if (!(pdir = lookup_child(pdir, splitpath[n])))
+ {
+ vcd_error("mkdir: parent dir `%s' (level=%d) for `%s' missing!",
+ splitpath[n], n, pathname);
+ vcd_assert_not_reached ();
+ }
+
+ if (lookup_child (pdir, splitpath[level-1]))
+ {
+ vcd_error ("mkdir: `%s' already exists", pathname);
+ vcd_assert_not_reached ();
+ }
+
+ {
+ data_t *data = _vcd_malloc (sizeof (data_t));
+
+ _vcd_tree_node_append_child (pdir, data);
+
+ data->is_dir = true;
+ data->name = strdup(splitpath[level-1]);
+ data->xa_attributes = XA_FORM1_DIR;
+ data->xa_filenum = 0x00;
+ /* .. */
+ }
+
+ _vcd_tree_node_sort_children (pdir, _iso_dir_cmp);
+
+ _vcd_strfreev (splitpath);
+
+ return 0;
+}
+
+int
+_vcd_directory_mkfile (VcdDirectory *dir, const char pathname[],
+ uint32_t start, uint32_t size,
+ bool form2_flag, uint8_t filenum)
+{
+ char **splitpath;
+ unsigned level, n;
+ const int file_version = 1;
+
+ VcdDirNode* pdir = NULL;
+
+ vcd_assert (dir != NULL);
+ vcd_assert (pathname != NULL);
+
+ splitpath = _vcd_strsplit (pathname, '/');
+
+ level = _vcd_strlenv (splitpath);
+
+ while (!pdir)
+ {
+ pdir = _vcd_tree_root (dir);
+
+ for (n = 0; n < level-1; n++)
+ if (!(pdir = lookup_child (pdir, splitpath[n])))
+ {
+ char *newdir = _vcd_strjoin (splitpath, n+1, "/");
+
+ vcd_info ("autocreating directory `%s' for file `%s'",
+ newdir, pathname);
+
+ _vcd_directory_mkdir (dir, newdir);
+
+ free (newdir);
+
+ vcd_assert (pdir == NULL);
+
+ break;
+ }
+ else if (!DATAP(pdir)->is_dir)
+ {
+ char *newdir = _vcd_strjoin (splitpath, n+1, "/");
+
+ vcd_error ("mkfile: `%s' not a directory", newdir);
+
+ free (newdir);
+
+ return -1;
+ }
+
+ }
+
+ if (lookup_child (pdir, splitpath[level-1]))
+ {
+ vcd_error ("mkfile: `%s' already exists", pathname);
+ return -1;
+ }
+
+ {
+ data_t *data = _vcd_malloc (sizeof (data_t));
+
+ _vcd_tree_node_append_child (pdir, data);
+
+ data->is_dir = false;
+ data->name = strdup (splitpath[level-1]);
+ data->version = file_version;
+ data->xa_attributes = form2_flag ? XA_FORM2_FILE : XA_FORM1_FILE;
+ data->xa_filenum = filenum;
+ data->size = size;
+ data->extent = start;
+ /* .. */
+ }
+
+ _vcd_tree_node_sort_children (pdir, _iso_dir_cmp);
+
+ _vcd_strfreev (splitpath);
+
+ return 0;
+}
+
+uint32_t
+_vcd_directory_get_size (VcdDirectory *dir)
+{
+ vcd_assert (dir != NULL);
+
+ update_sizes (dir);
+ return get_dirsizes (_vcd_tree_root (dir));
+}
+
+static void
+traverse_vcd_directory_dump_entries (VcdDirNode *node, void *data)
+{
+ data_t *d = DATAP(node);
+ iso9660_xa_t xa_su;
+
+ uint32_t root_extent = EXTENT(_vcd_tree_node_root (node));
+
+ uint32_t parent_extent =
+ (!_vcd_tree_node_is_root (node))
+ ? EXTENT(_vcd_tree_node_parent (node))
+ : EXTENT(node);
+
+ uint32_t parent_size =
+ (!_vcd_tree_node_is_root (node))
+ ? SIZE(_vcd_tree_node_parent (node))
+ : SIZE(node);
+
+ void *dirbufp = (char*) data + ISO_BLOCKSIZE * (parent_extent - root_extent);
+
+ iso9660_xa_init (&xa_su, 0, 0, d->xa_attributes, d->xa_filenum);
+
+ if (!_vcd_tree_node_is_root (node))
+ {
+ char *pathname = (d->is_dir
+ ? strdup (d->name)
+ : iso9660_pathname_isofy (d->name, d->version));
+
+ iso9660_dir_add_entry_su (dirbufp, pathname, d->extent, d->size,
+ d->is_dir ? ISO_DIRECTORY : ISO_FILE,
+ &xa_su, sizeof (xa_su),
+ &_vcd_time);
+
+ free (pathname);
+ }
+
+ /* if this is a directory, create the new directory node */
+ if (d->is_dir)
+ {
+ dirbufp = (char*)data + ISO_BLOCKSIZE * (d->extent - root_extent);
+
+ iso9660_dir_init_new_su (dirbufp,
+ d->extent, d->size, &xa_su, sizeof (xa_su),
+ parent_extent, parent_size, &xa_su,
+ sizeof (xa_su), &_vcd_time);
+ }
+}
+
+void
+_vcd_directory_dump_entries (VcdDirectory *dir, void *buf, uint32_t extent)
+{
+ vcd_assert (dir != NULL);
+
+ update_sizes (dir); /* better call it one time more than one less */
+ update_dirextents (dir, extent);
+
+ _vcd_tree_node_traverse (_vcd_tree_root (dir),
+ traverse_vcd_directory_dump_entries, buf);
+}
+
+typedef struct
+{
+ void *ptl;
+ void *ptm;
+} _vcd_directory_dump_pathtables_t;
+
+static void
+_dump_pathtables_helper (_vcd_directory_dump_pathtables_t *args,
+ data_t *d, uint16_t parent_id)
+{
+ uint16_t id_l, id_m;
+
+ vcd_assert (args != NULL);
+ vcd_assert (d != NULL);
+
+ vcd_assert (d->is_dir);
+
+ id_l = iso9660_pathtable_l_add_entry (args->ptl, d->name, d->extent,
+ parent_id);
+
+ id_m = iso9660_pathtable_m_add_entry (args->ptm, d->name, d->extent,
+ parent_id);
+
+ vcd_assert (id_l == id_m);
+
+ d->pt_id = id_m;
+}
+
+static void
+traverse_vcd_directory_dump_pathtables (VcdDirNode *node, void *data)
+{
+ _vcd_directory_dump_pathtables_t *args = data;
+
+ if (DATAP (node)->is_dir)
+ {
+ VcdDirNode* parent = _vcd_tree_node_parent (node);
+ uint16_t parent_id = parent ? PT_ID (parent) : 1;
+
+ _dump_pathtables_helper (args, DATAP (node), parent_id);
+ }
+}
+
+void
+_vcd_directory_dump_pathtables (VcdDirectory *dir, void *ptl, void *ptm)
+{
+ _vcd_directory_dump_pathtables_t args;
+
+ vcd_assert (dir != NULL);
+
+ iso9660_pathtable_init (ptl);
+ iso9660_pathtable_init (ptm);
+
+ args.ptl = ptl;
+ args.ptm = ptm;
+
+ _vcd_tree_node_traverse_bf (_vcd_tree_root (dir),
+ traverse_vcd_directory_dump_pathtables, &args);
+}
+
+
+/*
+ * Local variables:
+ * c-file-style: "gnu"
+ * tab-width: 8
+ * indent-tabs-mode: nil
+ * End:
+ */
diff --git a/src/input/vcd/libvcd/directory.h b/src/input/vcd/libvcd/directory.h
new file mode 100644
index 000000000..13e44da89
--- /dev/null
+++ b/src/input/vcd/libvcd/directory.h
@@ -0,0 +1,64 @@
+/*
+ $Id: directory.h,v 1.1 2003/10/13 11:47:12 f1rmb Exp $
+
+ Copyright (C) 2000 Herbert Valerio Riedel <hvr@gnu.org>
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+*/
+
+#ifndef _DIRECTORY_H_
+#define _DIRECTORY_H_
+
+#include <libvcd/types.h>
+
+/* Private headers */
+#include "data_structures.h"
+
+/* opaque data structure representing the ISO directory tree */
+typedef VcdTree VcdDirectory;
+
+VcdDirectory *
+_vcd_directory_new (void);
+
+void
+_vcd_directory_destroy (VcdDirectory *dir);
+
+int
+_vcd_directory_mkdir (VcdDirectory *dir, const char pathname[]);
+
+int
+_vcd_directory_mkfile (VcdDirectory *dir, const char pathname[],
+ uint32_t start, uint32_t size,
+ bool form2_flag, uint8_t filenum);
+
+uint32_t
+_vcd_directory_get_size (VcdDirectory *dir);
+
+void
+_vcd_directory_dump_entries (VcdDirectory *dir, void *buf, uint32_t extent);
+
+void
+_vcd_directory_dump_pathtables (VcdDirectory *dir, void *ptl, void *ptm);
+
+#endif /* _DIRECTORY_H_ */
+
+
+/*
+ * Local variables:
+ * c-file-style: "gnu"
+ * tab-width: 8
+ * indent-tabs-mode: nil
+ * End:
+ */
diff --git a/src/input/vcd/libvcd/files.c b/src/input/vcd/libvcd/files.c
new file mode 100644
index 000000000..e0757355d
--- /dev/null
+++ b/src/input/vcd/libvcd/files.c
@@ -0,0 +1,1018 @@
+/*
+ $Id: files.c,v 1.1 2003/10/13 11:47:12 f1rmb Exp $
+
+ Copyright (C) 2000 Herbert Valerio Riedel <hvr@gnu.org>
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+*/
+
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
+
+#include <string.h>
+#include <stdlib.h>
+#include <stddef.h>
+#include <math.h>
+
+#include <cdio/cdio.h>
+#include <cdio/util.h>
+
+/* Public headers */
+#include <libvcd/files.h>
+#include <libvcd/types.h>
+#include <libvcd/logging.h>
+
+/* FIXME! Make this local */
+#include <libvcd/files_private.h>
+
+/* Private headers */
+#include "assert.h"
+#include "bytesex.h"
+#include "mpeg_stream.h"
+#include "obj.h"
+#include "pbc.h"
+#include "util.h"
+
+static const char _rcsid[] = "$Id: files.c,v 1.1 2003/10/13 11:47:12 f1rmb Exp $";
+
+inline static bool
+_pal_p (const struct vcd_mpeg_stream_vid_info *_info)
+{
+ return (_info->vsize == 288 || _info->vsize == 576);
+}
+
+static int
+_derive_vid_type (const struct vcd_mpeg_stream_info *_info, bool svcd)
+{
+ if (_info->shdr[0].seen)
+ return _pal_p (&_info->shdr[0]) ? 0x7 : 0x3;
+
+ if (_info->shdr[2].seen)
+ {
+ if (svcd)
+ vcd_warn ("stream with 0xE2 still stream id not allowed for IEC62107 compliant SVCDs");
+ return _pal_p (&_info->shdr[2]) ? 0x6 : 0x2;
+ }
+
+ if (_info->shdr[1].seen)
+ return _pal_p (&_info->shdr[1]) ? 0x5 : 0x1;
+
+ return 0;
+}
+
+static int
+_derive_ogt_type (const struct vcd_mpeg_stream_info *_info, bool svcd)
+{
+
+ if (!svcd)
+ return 0;
+
+ if ((_info->ogt[3] || _info->ogt[2])
+ && _info->ogt[1] && _info->ogt[0])
+ return 0x3;
+
+ if (_info->ogt[1] && _info->ogt[0])
+ return 0x2;
+
+ if (_info->ogt[0])
+ return 0x1;
+
+ vcd_debug ("OGT streams available: %d %d %d %d",
+ _info->ogt[0], _info->ogt[1],
+ _info->ogt[2], _info->ogt[3]);
+
+ return 0x0;
+}
+
+static int
+_derive_aud_type (const struct vcd_mpeg_stream_info *_info, bool svcd)
+{
+ if (!_info->ahdr[0].seen)
+ return 0; /* no MPEG audio */
+
+ if (svcd)
+ {
+ if (_info->ahdr[2].seen)
+ return 3; /* MC */
+
+ if (_info->ahdr[1].seen)
+ return 2; /* 2 streams */
+
+ return 1; /* just one stream */
+ }
+ else
+ switch (_info->ahdr[0].mode)
+ {
+ case MPEG_SINGLE_CHANNEL:
+ return 1;
+ break;
+
+ case MPEG_STEREO:
+ case MPEG_JOINT_STEREO:
+ return 2;
+ break;
+
+ case MPEG_DUAL_CHANNEL:
+ return 3;
+ break;
+ }
+
+ return 0;
+}
+
+void
+set_entries_vcd (VcdObj *obj, void *buf)
+{
+ VcdListNode *node = NULL;
+ int idx = 0;
+ int track_idx = 0;
+ EntriesVcd entries_vcd;
+
+ vcd_assert (sizeof(EntriesVcd) == 2048);
+
+ vcd_assert (_vcd_list_length (obj->mpeg_track_list) <= MAX_ENTRIES);
+ vcd_assert (_vcd_list_length (obj->mpeg_track_list) > 0);
+
+ memset(&entries_vcd, 0, sizeof(entries_vcd)); /* paranoia / fixme */
+
+ switch (obj->type)
+ {
+ case VCD_TYPE_VCD:
+ strncpy(entries_vcd.ID, ENTRIES_ID_VCD, 8);
+ entries_vcd.version = ENTRIES_VERSION_VCD;
+ entries_vcd.sys_prof_tag = ENTRIES_SPTAG_VCD;
+ break;
+
+ case VCD_TYPE_VCD11:
+ strncpy(entries_vcd.ID, ENTRIES_ID_VCD, 8);
+ entries_vcd.version = ENTRIES_VERSION_VCD11;
+ entries_vcd.sys_prof_tag = ENTRIES_SPTAG_VCD11;
+ break;
+
+ case VCD_TYPE_VCD2:
+ strncpy(entries_vcd.ID, ENTRIES_ID_VCD, 8);
+ entries_vcd.version = ENTRIES_VERSION_VCD2;
+ entries_vcd.sys_prof_tag = ENTRIES_SPTAG_VCD2;
+ break;
+
+ case VCD_TYPE_SVCD:
+ if (!obj->svcd_vcd3_entrysvd)
+ strncpy(entries_vcd.ID, ENTRIES_ID_SVCD, 8);
+ else
+ {
+ vcd_warn ("setting ENTRYSVD signature for *DEPRECATED* VCD 3.0 type SVCD");
+ strncpy(entries_vcd.ID, ENTRIES_ID_VCD3, 8);
+ }
+ entries_vcd.version = ENTRIES_VERSION_SVCD;
+ entries_vcd.sys_prof_tag = ENTRIES_SPTAG_SVCD;
+ break;
+
+ case VCD_TYPE_HQVCD:
+ strncpy(entries_vcd.ID, ENTRIES_ID_SVCD, 8);
+ entries_vcd.version = ENTRIES_VERSION_HQVCD;
+ entries_vcd.sys_prof_tag = ENTRIES_SPTAG_HQVCD;
+ break;
+
+ default:
+ vcd_assert_not_reached ();
+ break;
+ }
+
+ idx = 0;
+ track_idx = 2;
+ _VCD_LIST_FOREACH (node, obj->mpeg_sequence_list)
+ {
+ mpeg_sequence_t *track = _vcd_list_node_data (node);
+ uint32_t lsect = track->relative_start_extent;
+ VcdListNode *node2;
+
+ lsect += obj->iso_size;
+
+ entries_vcd.entry[idx].n = to_bcd8(track_idx);
+ cdio_lba_to_msf(cdio_lsn_to_lba(lsect),
+ &(entries_vcd.entry[idx].msf));
+
+ idx++;
+ lsect += obj->track_front_margin;
+
+ _VCD_LIST_FOREACH (node2, track->entry_list)
+ {
+ entry_t *_entry = _vcd_list_node_data (node2);
+ /* additional entries */
+
+ vcd_assert (idx < MAX_ENTRIES);
+
+ entries_vcd.entry[idx].n = to_bcd8(track_idx);
+ cdio_lba_to_msf(lsect + cdio_lsn_to_lba(_entry->aps.packet_no),
+ &(entries_vcd.entry[idx].msf));
+
+ idx++;
+ }
+
+ track_idx++;
+ }
+
+ entries_vcd.entry_count = uint16_to_be (idx);
+
+ memcpy(buf, &entries_vcd, sizeof(entries_vcd));
+}
+
+static void
+_set_bit (uint8_t bitset[], unsigned bitnum)
+{
+ unsigned _byte = bitnum / 8;
+ unsigned _bit = bitnum % 8;
+
+ bitset[_byte] |= (1 << _bit);
+}
+
+uint32_t
+get_psd_size (VcdObj *obj, bool extended)
+{
+ if (extended)
+ vcd_assert (_vcd_obj_has_cap_p (obj, _CAP_PBC_X));
+
+ if (!_vcd_pbc_available (obj))
+ return 0;
+
+ if (extended)
+ return obj->psdx_size;
+
+ return obj->psd_size;
+}
+
+void
+set_psd_vcd (VcdObj *obj, void *buf, bool extended)
+{
+ VcdListNode *node;
+
+ if (extended)
+ vcd_assert (_vcd_obj_has_cap_p (obj, _CAP_PBC_X));
+
+ vcd_assert (_vcd_pbc_available (obj));
+
+ _VCD_LIST_FOREACH (node, obj->pbc_list)
+ {
+ pbc_t *_pbc = _vcd_list_node_data (node);
+ char *_buf = buf;
+ unsigned offset = (extended ? _pbc->offset_ext : _pbc->offset);
+
+ vcd_assert (offset % INFO_OFFSET_MULT == 0);
+
+ _vcd_pbc_node_write (obj, _pbc, _buf + offset, extended);
+ }
+}
+
+void
+set_lot_vcd(VcdObj *obj, void *buf, bool extended)
+{
+ LotVcd *lot_vcd = NULL;
+ VcdListNode *node;
+
+ if (extended)
+ vcd_assert (_vcd_obj_has_cap_p (obj, _CAP_PBC_X));
+
+ vcd_assert (_vcd_pbc_available (obj));
+
+ lot_vcd = _vcd_malloc (sizeof (LotVcd));
+ memset(lot_vcd, 0xff, sizeof(LotVcd));
+
+ lot_vcd->reserved = 0x0000;
+
+ _VCD_LIST_FOREACH (node, obj->pbc_list)
+ {
+ pbc_t *_pbc = _vcd_list_node_data (node);
+ unsigned offset = extended ? _pbc->offset_ext : _pbc->offset;
+
+ vcd_assert (offset % INFO_OFFSET_MULT == 0);
+
+ if (_pbc->rejected)
+ continue;
+
+ offset /= INFO_OFFSET_MULT;
+
+ lot_vcd->offset[_pbc->lid - 1] = uint16_to_be (offset);
+ }
+
+ memcpy(buf, lot_vcd, sizeof(LotVcd));
+ free(lot_vcd);
+}
+
+void
+set_info_vcd(VcdObj *obj, void *buf)
+{
+ InfoVcd info_vcd;
+ VcdListNode *node = NULL;
+ int n = 0;
+
+ vcd_assert (sizeof (InfoVcd) == 2048);
+ vcd_assert (_vcd_list_length (obj->mpeg_track_list) <= 98);
+
+ memset (&info_vcd, 0, sizeof (info_vcd));
+
+ switch (obj->type)
+ {
+ case VCD_TYPE_VCD:
+ strncpy (info_vcd.ID, INFO_ID_VCD, sizeof (info_vcd.ID));
+ info_vcd.version = INFO_VERSION_VCD;
+ info_vcd.sys_prof_tag = INFO_SPTAG_VCD;
+ break;
+
+ case VCD_TYPE_VCD11:
+ strncpy (info_vcd.ID, INFO_ID_VCD, sizeof (info_vcd.ID));
+ info_vcd.version = INFO_VERSION_VCD11;
+ info_vcd.sys_prof_tag = INFO_SPTAG_VCD11;
+ break;
+
+ case VCD_TYPE_VCD2:
+ strncpy (info_vcd.ID, INFO_ID_VCD, sizeof (info_vcd.ID));
+ info_vcd.version = INFO_VERSION_VCD2;
+ info_vcd.sys_prof_tag = INFO_SPTAG_VCD2;
+ break;
+
+ case VCD_TYPE_SVCD:
+ strncpy (info_vcd.ID, INFO_ID_SVCD, sizeof (info_vcd.ID));
+ info_vcd.version = INFO_VERSION_SVCD;
+ info_vcd.sys_prof_tag = INFO_SPTAG_SVCD;
+ break;
+
+ case VCD_TYPE_HQVCD:
+ strncpy (info_vcd.ID, INFO_ID_HQVCD, sizeof (info_vcd.ID));
+ info_vcd.version = INFO_VERSION_HQVCD;
+ info_vcd.sys_prof_tag = INFO_SPTAG_HQVCD;
+ break;
+
+ default:
+ vcd_assert_not_reached ();
+ break;
+ }
+
+ iso9660_strncpy_pad (info_vcd.album_desc,
+ obj->info_album_id,
+ sizeof(info_vcd.album_desc), ISO9660_DCHARS);
+ /* fixme, maybe it's VCD_ACHARS? */
+
+ info_vcd.vol_count = uint16_to_be (obj->info_volume_count);
+ info_vcd.vol_id = uint16_to_be (obj->info_volume_number);
+
+ if (_vcd_obj_has_cap_p (obj, _CAP_PAL_BITS))
+ {
+ /* NTSC/PAL bitset */
+
+ n = 0;
+ _VCD_LIST_FOREACH (node, obj->mpeg_track_list)
+ {
+ mpeg_track_t *track = _vcd_list_node_data (node);
+
+ const struct vcd_mpeg_stream_vid_info *_info = &track->info->shdr[0];
+
+ if (vcd_mpeg_get_norm (_info) == MPEG_NORM_PAL
+ || vcd_mpeg_get_norm (_info) == MPEG_NORM_PAL_S)
+ _set_bit(info_vcd.pal_flags, n);
+ else if (_pal_p (_info))
+ {
+ vcd_warn ("INFO.{VCD,SVD}: assuming PAL-type resolution for track #%d"
+ " -- are we creating a X(S)VCD?", n);
+ _set_bit(info_vcd.pal_flags, n);
+ }
+
+ n++;
+ }
+ }
+
+ if (_vcd_obj_has_cap_p (obj, _CAP_PBC))
+ {
+ info_vcd.flags.restriction = obj->info_restriction;
+ info_vcd.flags.use_lid2 = obj->info_use_lid2;
+ info_vcd.flags.use_track3 = obj->info_use_seq2;
+
+ if (_vcd_obj_has_cap_p (obj, _CAP_PBC_X)
+ &&_vcd_pbc_available (obj))
+ info_vcd.flags.pbc_x = true;
+
+ info_vcd.psd_size = uint32_to_be (get_psd_size (obj, false));
+ info_vcd.offset_mult = _vcd_pbc_available (obj) ? INFO_OFFSET_MULT : 0;
+ info_vcd.lot_entries = uint16_to_be (_vcd_pbc_max_lid (obj));
+
+ if (_vcd_list_length (obj->mpeg_segment_list))
+ {
+ unsigned segments = 0;
+
+ if (!_vcd_pbc_available (obj))
+ vcd_warn ("segment items available, but no PBC items set!"
+ " SPIs will be unreachable");
+
+ _VCD_LIST_FOREACH (node, obj->mpeg_segment_list)
+ {
+ mpeg_segment_t *segment = _vcd_list_node_data (node);
+ unsigned idx;
+ InfoSpiContents contents = { 0, };
+
+ contents.video_type =
+ _derive_vid_type (segment->info,
+ _vcd_obj_has_cap_p (obj, _CAP_4C_SVCD));
+
+ contents.audio_type =
+ _derive_aud_type (segment->info,
+ _vcd_obj_has_cap_p (obj, _CAP_4C_SVCD));
+
+ contents.ogt =
+ _derive_ogt_type (segment->info,
+ _vcd_obj_has_cap_p (obj, _CAP_4C_SVCD));
+
+ if (!contents.video_type && !contents.audio_type)
+ vcd_warn ("segment item '%s' seems contains neither video nor audio",
+ segment->id);
+
+ for (idx = 0; idx < segment->segment_count; idx++)
+ {
+ vcd_assert (segments + idx < MAX_SEGMENTS);
+
+ info_vcd.spi_contents[segments + idx] = contents;
+
+ if (!contents.item_cont)
+ contents.item_cont = true;
+ }
+
+ segments += idx;
+ }
+
+ info_vcd.item_count = uint16_to_be (segments);
+
+ cdio_lba_to_msf (cdio_lsn_to_lba(obj->mpeg_segment_start_extent),
+ &info_vcd.first_seg_addr);
+ }
+ }
+
+ memcpy(buf, &info_vcd, sizeof(info_vcd));
+}
+
+static void
+set_tracks_svd_v30 (VcdObj *obj, void *buf)
+{
+ char tracks_svd_buf[ISO_BLOCKSIZE] = { 0, };
+ TracksSVD_v30 *tracks_svd = (void *) tracks_svd_buf;
+ VcdListNode *node;
+ double playtime;
+ int n;
+
+ strncpy (tracks_svd->file_id, TRACKS_SVD_FILE_ID,
+ sizeof (TRACKS_SVD_FILE_ID));
+ tracks_svd->version = TRACKS_SVD_VERSION;
+ tracks_svd->tracks = _vcd_list_length (obj->mpeg_track_list);
+
+ n = 0;
+ playtime = 0;
+ _VCD_LIST_FOREACH (node, obj->mpeg_track_list)
+ {
+ mpeg_track_t *track = _vcd_list_node_data (node);
+ int i;
+
+ playtime += track->info->playing_time;
+
+ tracks_svd->track[n].audio_info = track->info->ahdr[0].seen ? 0x2 : 0x0; /* fixme */
+ tracks_svd->track[n].audio_info |= track->info->ahdr[1].seen ? 0x20 : 0x0; /* fixme */
+
+ tracks_svd->track[n].ogt_info = 0x0;
+ for (i = 0; i < 4; i++)
+ if (track->info->ogt[i])
+ tracks_svd->track[n].ogt_info |= 1 << (i * 2); /* fixme */
+
+ /* setting playtime */
+
+ {
+ double i, f;
+
+ while (playtime >= 6000.0)
+ playtime -= 6000.0;
+
+ f = modf(playtime, &i);
+
+ cdio_lba_to_msf (i * 75, &tracks_svd->track[n].cum_playing_time);
+ tracks_svd->track[n].cum_playing_time.f = to_bcd8 (floor (f * 75.0));
+ }
+
+ n++;
+ }
+
+ memcpy (buf, &tracks_svd_buf, sizeof(tracks_svd_buf));
+}
+
+void
+set_tracks_svd (VcdObj *obj, void *buf)
+{
+ char tracks_svd[ISO_BLOCKSIZE] = { 0, };
+ TracksSVD *tracks_svd1 = (void *) tracks_svd;
+ TracksSVD2 *tracks_svd2;
+ VcdListNode *node;
+ int n;
+
+ vcd_assert (_vcd_obj_has_cap_p (obj, _CAP_4C_SVCD));
+
+ if (obj->svcd_vcd3_tracksvd)
+ {
+ set_tracks_svd_v30 (obj, buf);
+ return;
+ }
+
+ vcd_assert (sizeof (SVDTrackContent) == 1);
+
+ strncpy (tracks_svd1->file_id, TRACKS_SVD_FILE_ID, sizeof (TRACKS_SVD_FILE_ID));
+ tracks_svd1->version = TRACKS_SVD_VERSION;
+
+ tracks_svd1->tracks = _vcd_list_length (obj->mpeg_track_list);
+
+ tracks_svd2 = (void *) &(tracks_svd1->playing_time[tracks_svd1->tracks]);
+
+ n = 0;
+
+ _VCD_LIST_FOREACH (node, obj->mpeg_track_list)
+ {
+ mpeg_track_t *track = _vcd_list_node_data (node);
+ const double playtime = track->info->playing_time;
+
+ int _video;
+
+ _video = tracks_svd2->contents[n].video =
+ _derive_vid_type (track->info, true);
+
+ tracks_svd2->contents[n].audio =
+ _derive_aud_type (track->info, true);
+
+ tracks_svd2->contents[n].ogt =
+ _derive_ogt_type (track->info, true);
+
+ if (_video != 0x3 && _video != 0x7)
+ vcd_warn("SVCD/TRACKS.SVCD: No MPEG motion video for track #%d?", n);
+
+ /* setting playtime */
+
+ {
+ double i, f;
+
+ f = modf(playtime, &i);
+
+ if (playtime >= 6000.0)
+ {
+ vcd_warn ("SVCD/TRACKS.SVD: playing time value (%d seconds) to great,"
+ " clipping to 100 minutes", (int) i);
+ i = 5999.0;
+ f = 74.0 / 75.0;
+ }
+
+ cdio_lba_to_msf (i * 75, &(tracks_svd1->playing_time[n]));
+ tracks_svd1->playing_time[n].f = to_bcd8 (floor (f * 75.0));
+ }
+
+ n++;
+ }
+
+ memcpy (buf, &tracks_svd, sizeof(tracks_svd));
+}
+
+static double
+_get_cumulative_playing_time (const VcdObj *obj, unsigned up_to_track_no)
+{
+ double result = 0;
+ VcdListNode *node;
+
+ _VCD_LIST_FOREACH (node, obj->mpeg_track_list)
+ {
+ mpeg_track_t *track = _vcd_list_node_data (node);
+
+ if (!up_to_track_no)
+ break;
+
+ result += track->info->playing_time;
+ up_to_track_no--;
+ }
+
+ if (up_to_track_no)
+ vcd_warn ("internal error...");
+
+ return result;
+}
+
+static unsigned
+_get_scanpoint_count (const VcdObj *obj)
+{
+ double total_playing_time;
+
+ total_playing_time = _get_cumulative_playing_time (obj, _vcd_list_length (obj->mpeg_track_list));
+
+ return ceil (total_playing_time * 2.0);
+}
+
+uint32_t
+get_search_dat_size (const VcdObj *obj)
+{
+ return sizeof (SearchDat)
+ + (_get_scanpoint_count (obj) * sizeof (msf_t));
+}
+
+static VcdList *
+_make_track_scantable (const VcdObj *obj)
+{
+ VcdList *all_aps = _vcd_list_new ();
+ VcdList *scantable = _vcd_list_new ();
+ unsigned scanpoints = _get_scanpoint_count (obj);
+ unsigned track_no;
+ VcdListNode *node;
+
+ track_no = 0;
+ _VCD_LIST_FOREACH (node, obj->mpeg_track_list)
+ {
+ mpeg_track_t *track = _vcd_list_node_data (node);
+ VcdListNode *node2;
+
+ _VCD_LIST_FOREACH (node2, track->info->shdr[0].aps_list)
+ {
+ struct aps_data *_data = _vcd_malloc (sizeof (struct aps_data));
+
+ *_data = *(struct aps_data *)_vcd_list_node_data (node2);
+
+ _data->timestamp += _get_cumulative_playing_time (obj, track_no);
+ _data->packet_no += obj->iso_size + track->relative_start_extent;
+ _data->packet_no += obj->track_front_margin;
+
+ _vcd_list_append (all_aps, _data);
+ }
+ track_no++;
+ }
+
+ {
+ VcdListNode *aps_node = _vcd_list_begin (all_aps);
+ VcdListNode *n;
+ struct aps_data *_data;
+ double aps_time;
+ double playing_time;
+ int aps_packet;
+ double t;
+
+ playing_time = scanpoints;
+ playing_time /= 2;
+
+ vcd_assert (aps_node != NULL);
+
+ _data = _vcd_list_node_data (aps_node);
+ aps_time = _data->timestamp;
+ aps_packet = _data->packet_no;
+
+ for (t = 0; t < playing_time; t += 0.5)
+ {
+ for(n = _vcd_list_node_next (aps_node); n; n = _vcd_list_node_next (n))
+ {
+ _data = _vcd_list_node_data (n);
+
+ if (fabs (_data->timestamp - t) < fabs (aps_time - t))
+ {
+ aps_node = n;
+ aps_time = _data->timestamp;
+ aps_packet = _data->packet_no;
+ }
+ else
+ break;
+ }
+
+ {
+ uint32_t *lsect = _vcd_malloc (sizeof (uint32_t));
+
+ *lsect = aps_packet;
+ _vcd_list_append (scantable, lsect);
+ }
+
+ }
+
+ }
+
+ _vcd_list_free (all_aps, true);
+
+ vcd_assert (scanpoints == _vcd_list_length (scantable));
+
+ return scantable;
+}
+
+void
+set_search_dat (VcdObj *obj, void *buf)
+{
+ VcdList *scantable;
+ VcdListNode *node;
+ SearchDat search_dat;
+ unsigned n;
+
+ vcd_assert (_vcd_obj_has_cap_p (obj, _CAP_4C_SVCD));
+ /* vcd_assert (sizeof (SearchDat) == ?) */
+
+ memset (&search_dat, 0, sizeof (search_dat));
+
+ strncpy (search_dat.file_id, SEARCH_FILE_ID, sizeof (SEARCH_FILE_ID));
+
+ search_dat.version = SEARCH_VERSION;
+ search_dat.scan_points = uint16_to_be (_get_scanpoint_count (obj));
+ search_dat.time_interval = SEARCH_TIME_INTERVAL;
+
+ memcpy (buf, &search_dat, sizeof (search_dat));
+
+ scantable = _make_track_scantable (obj);
+
+ n = 0;
+ _VCD_LIST_FOREACH (node, scantable)
+ {
+ SearchDat *search_dat2 = buf;
+ uint32_t sect = *(uint32_t *) _vcd_list_node_data (node);
+
+ cdio_lba_to_msf(cdio_lsn_to_lba(sect), &(search_dat2->points[n]));
+ n++;
+ }
+
+ vcd_assert (n = _get_scanpoint_count (obj));
+
+ _vcd_list_free (scantable, true);
+}
+
+static uint32_t
+_get_scandata_count (const struct vcd_mpeg_stream_info *info)
+{
+ return ceil (info->playing_time * 2.0);
+}
+
+static uint32_t *
+_get_scandata_table (const struct vcd_mpeg_stream_info *info)
+{
+ VcdListNode *n, *aps_node = _vcd_list_begin (info->shdr[0].aps_list);
+ struct aps_data *_data;
+ double aps_time, t;
+ int aps_packet;
+ uint32_t *retval;
+ unsigned i;
+
+ retval = _vcd_malloc (_get_scandata_count (info) * sizeof (uint32_t));
+
+ _data = _vcd_list_node_data (aps_node);
+ aps_time = _data->timestamp;
+ aps_packet = _data->packet_no;
+
+ for (t = 0, i = 0; t < info->playing_time; t += 0.5, i++)
+ {
+ for(n = _vcd_list_node_next (aps_node); n; n = _vcd_list_node_next (n))
+ {
+ _data = _vcd_list_node_data (n);
+
+ if (fabs (_data->timestamp - t) < fabs (aps_time - t))
+ {
+ aps_node = n;
+ aps_time = _data->timestamp;
+ aps_packet = _data->packet_no;
+ }
+ else
+ break;
+ }
+
+ /* vcd_debug ("%f %f %d", t, aps_time, aps_packet); */
+
+ vcd_assert (i < _get_scandata_count (info));
+
+ retval[i] = aps_packet;
+ }
+
+ vcd_assert (i = _get_scandata_count (info));
+
+ return retval;
+}
+
+uint32_t
+get_scandata_dat_size (const VcdObj *obj)
+{
+ uint32_t retval = 0;
+
+ /* struct 1 */
+ retval += sizeof (ScandataDat1);
+ retval += sizeof (msf_t) * _vcd_list_length (obj->mpeg_track_list);
+
+ /* struct 2 */
+ /* vcd_assert (sizeof (ScandataDat2) == 0);
+ retval += sizeof (ScandataDat2); */
+ retval += sizeof (uint16_t) * 0;
+
+ /* struct 3 */
+ retval += sizeof (ScandataDat3);
+ retval += (sizeof (uint8_t) + sizeof (uint16_t)) * _vcd_list_length (obj->mpeg_track_list);
+
+ /* struct 4 */
+ /* vcd_assert (sizeof (ScandataDat4) == 0);
+ retval += sizeof (ScandataDat4); */
+ {
+ VcdListNode *node;
+ _VCD_LIST_FOREACH (node, obj->mpeg_track_list)
+ {
+ const mpeg_track_t *track = _vcd_list_node_data (node);
+
+ retval += sizeof (msf_t) * _get_scandata_count (track->info);
+ }
+ }
+
+ return retval;
+}
+
+void
+set_scandata_dat (VcdObj *obj, void *buf)
+{
+ const unsigned tracks = _vcd_list_length (obj->mpeg_track_list);
+
+ ScandataDat1 *scandata_dat1 = (ScandataDat1 *) buf;
+ ScandataDat2 *scandata_dat2 =
+ (ScandataDat2 *) &(scandata_dat1->cum_playtimes[tracks]);
+ ScandataDat3 *scandata_dat3 =
+ (ScandataDat3 *) &(scandata_dat2->spi_indexes[0]);
+ ScandataDat4 *scandata_dat4 =
+ (ScandataDat4 *) &(scandata_dat3->mpeg_track_offsets[tracks]);
+
+ const uint16_t _begin_offset =
+ __cd_offsetof (ScandataDat3, mpeg_track_offsets[tracks])
+ - __cd_offsetof (ScandataDat3, mpeg_track_offsets);
+
+ VcdListNode *node;
+ unsigned n;
+ uint16_t _tmp_offset;
+
+ vcd_assert (_vcd_obj_has_cap_p (obj, _CAP_4C_SVCD));
+
+ /* memset (buf, 0, get_scandata_dat_size (obj)); */
+
+ /* struct 1 */
+ strncpy (scandata_dat1->file_id, SCANDATA_FILE_ID, sizeof (SCANDATA_FILE_ID));
+
+ scandata_dat1->version = SCANDATA_VERSION_SVCD;
+ scandata_dat1->reserved = 0x00;
+ scandata_dat1->scandata_count = uint16_to_be (_get_scanpoint_count (obj));
+
+ scandata_dat1->track_count = uint16_to_be (tracks);
+ scandata_dat1->spi_count = uint16_to_be (0);
+
+ for (n = 0; n < tracks; n++)
+ {
+ double playtime = _get_cumulative_playing_time (obj, n + 1);
+ double i = 0, f = 0;
+
+ f = modf(playtime, &i);
+
+ while (i >= (60 * 100))
+ i -= (60 * 100);
+
+ vcd_assert (i >= 0);
+
+ cdio_lba_to_msf (i * 75, &(scandata_dat1->cum_playtimes[n]));
+ scandata_dat1->cum_playtimes[n].f = to_bcd8 (floor (f * 75.0));
+ }
+
+ /* struct 2 -- nothing yet */
+
+ /* struct 3/4 */
+
+ vcd_assert ((_begin_offset % sizeof (msf_t) == 0)
+ && _begin_offset > 0);
+
+ _tmp_offset = 0;
+
+ scandata_dat3->mpegtrack_start_index = uint16_to_be (_begin_offset);
+
+ n = 0;
+ _VCD_LIST_FOREACH (node, obj->mpeg_track_list)
+ {
+ const mpeg_track_t *track = _vcd_list_node_data (node);
+ uint32_t *_table;
+ const unsigned scanpoints = _get_scandata_count (track->info);
+ const unsigned _table_ofs =
+ (_tmp_offset * sizeof (msf_t)) + _begin_offset;
+ unsigned point;
+
+ scandata_dat3->mpeg_track_offsets[n].track_num = n + 2;
+ scandata_dat3->mpeg_track_offsets[n].table_offset = uint16_to_be (_table_ofs);
+
+ _table = _get_scandata_table (track->info);
+
+ for (point = 0; point < scanpoints; point++)
+ {
+ uint32_t lsect = _table[point];
+
+ lsect += obj->iso_size;
+ lsect += track->relative_start_extent;
+ lsect += obj->track_front_margin;
+
+ /* vcd_debug ("lsect %d %d", point, lsect); */
+
+ cdio_lba_to_msf(cdio_lsn_to_lba(lsect),
+ &(scandata_dat4->scandata_table[_tmp_offset + point]));
+ }
+
+ free (_table);
+
+ _tmp_offset += scanpoints;
+ n++;
+ }
+
+ /* struct 4 */
+
+
+}
+
+vcd_type_t
+vcd_files_info_detect_type (const void *info_buf)
+{
+ const InfoVcd *_info = info_buf;
+ vcd_type_t _type = VCD_TYPE_INVALID;
+
+ vcd_assert (info_buf != NULL);
+
+ if (!strncmp (_info->ID, INFO_ID_VCD, sizeof (_info->ID)))
+ switch (_info->version)
+ {
+ case INFO_VERSION_VCD2:
+ if (_info->sys_prof_tag != INFO_SPTAG_VCD2)
+ vcd_warn ("INFO.VCD: unexpected system profile tag %d encountered",
+ _info->version);
+ _type = VCD_TYPE_VCD2;
+ break;
+
+ case INFO_VERSION_VCD:
+ /* case INFO_VERSION_VCD11: */
+ switch (_info->sys_prof_tag)
+ {
+ case INFO_SPTAG_VCD:
+ _type = VCD_TYPE_VCD;
+ break;
+ case INFO_SPTAG_VCD11:
+ _type = VCD_TYPE_VCD11;
+ break;
+ default:
+ vcd_warn ("INFO.VCD: unexpected system profile tag %d "
+ "encountered, assuming VCD 1.1", _info->sys_prof_tag);
+ break;
+ }
+ break;
+
+ default:
+ vcd_warn ("unexpected VCD version %d encountered -- assuming VCD 2.0",
+ _info->version);
+ break;
+ }
+ else if (!strncmp (_info->ID, INFO_ID_SVCD, sizeof (_info->ID)))
+ switch (_info->version)
+ {
+ case INFO_VERSION_SVCD:
+ if (_info->sys_prof_tag != INFO_SPTAG_SVCD)
+ vcd_warn ("INFO.SVD: unexpected system profile tag value %d "
+ "-- assuming SVCD", _info->sys_prof_tag);
+ _type = VCD_TYPE_SVCD;
+ break;
+
+ default:
+ vcd_warn ("INFO.SVD: unexpected version value %d seen "
+ " -- still assuming SVCD", _info->version);
+ _type = VCD_TYPE_SVCD;
+ break;
+ }
+ else if (!strncmp (_info->ID, INFO_ID_HQVCD, sizeof (_info->ID)))
+ switch (_info->version)
+ {
+ case INFO_VERSION_HQVCD:
+ if (_info->sys_prof_tag != INFO_SPTAG_HQVCD)
+ vcd_warn ("INFO.SVD: unexpected system profile tag value -- assuming hqvcd");
+ _type = VCD_TYPE_HQVCD;
+ break;
+
+ default:
+ vcd_warn ("INFO.SVD: unexpected version value %d seen "
+ "-- still assuming HQVCD", _info->version);
+ _type = VCD_TYPE_HQVCD;
+ break;
+ }
+ else
+ vcd_warn ("INFO.SVD: signature not found");
+
+ return _type;
+}
+
+/* eof */
+
+
+/*
+ * Local variables:
+ * c-file-style: "gnu"
+ * tab-width: 8
+ * indent-tabs-mode: nil
+ * End:
+ */
diff --git a/src/input/vcd/libvcd/image.c b/src/input/vcd/libvcd/image.c
new file mode 100644
index 000000000..ae5b6aeb6
--- /dev/null
+++ b/src/input/vcd/libvcd/image.c
@@ -0,0 +1,108 @@
+/*
+ $Id: image.c,v 1.1 2003/10/13 11:47:12 f1rmb Exp $
+
+ Copyright (C) 2001 Herbert Valerio Riedel <hvr@gnu.org>
+ 2002 Rocky Bernstein <rocky@panix.com>
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+*/
+
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
+
+#include <cdio/cdio.h>
+
+/* Public headers */
+#include <libvcd/sector.h>
+#include <cdio/iso9660.h>
+
+/* Private headers */
+#include "assert.h"
+#include "image_sink.h"
+#include "util.h"
+
+static const char _rcsid[] = "$Id: image.c,v 1.1 2003/10/13 11:47:12 f1rmb Exp $";
+
+/*
+ * VcdImageSink routines next.
+ */
+
+struct _VcdImageSink {
+ void *user_data;
+ vcd_image_sink_funcs op;
+};
+
+VcdImageSink *
+vcd_image_sink_new (void *user_data, const vcd_image_sink_funcs *funcs)
+{
+ VcdImageSink *new_obj;
+
+ new_obj = _vcd_malloc (sizeof (VcdImageSink));
+
+ new_obj->user_data = user_data;
+ new_obj->op = *funcs;
+
+ return new_obj;
+}
+
+void
+vcd_image_sink_destroy (VcdImageSink *obj)
+{
+ vcd_assert (obj != NULL);
+
+ obj->op.free (obj->user_data);
+ free (obj);
+}
+
+int
+vcd_image_sink_set_cuesheet (VcdImageSink *obj, const VcdList *vcd_cue_list)
+{
+ vcd_assert (obj != NULL);
+
+ return obj->op.set_cuesheet (obj->user_data, vcd_cue_list);
+}
+
+int
+vcd_image_sink_write (VcdImageSink *obj, void *buf, uint32_t lsn)
+{
+ vcd_assert (obj != NULL);
+
+ return obj->op.write (obj->user_data, buf, lsn);
+}
+
+/*!
+ Set the arg "key" with "value" in the target device.
+*/
+
+int
+vcd_image_sink_set_arg (VcdImageSink *obj, const char key[],
+ const char value[])
+{
+ vcd_assert (obj != NULL);
+ vcd_assert (obj->op.set_arg != NULL);
+ vcd_assert (key != NULL);
+
+ return obj->op.set_arg (obj->user_data, key, value);
+}
+
+
+/*
+ * Local variables:
+ * c-file-style: "gnu"
+ * tab-width: 8
+ * indent-tabs-mode: nil
+ * End:
+ */
diff --git a/src/input/vcd/libvcd/image_bincue.c b/src/input/vcd/libvcd/image_bincue.c
new file mode 100644
index 000000000..b5e598a61
--- /dev/null
+++ b/src/input/vcd/libvcd/image_bincue.c
@@ -0,0 +1,252 @@
+/*
+ $Id: image_bincue.c,v 1.1 2003/10/13 11:47:12 f1rmb Exp $
+
+ Copyright (C) 2001 Herbert Valerio Riedel <hvr@gnu.org>
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+*/
+
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
+
+
+#include <stdlib.h>
+#include <string.h>
+
+#include <cdio/cdio.h>
+#include <cdio/iso9660.h>
+
+/* Public headers */
+#include <libvcd/sector.h>
+#include <libvcd/logging.h>
+
+/* Private headers */
+#include "assert.h"
+#include "bytesex.h"
+#include "image_sink.h"
+#include "stream_stdio.h"
+#include "util.h"
+
+static const char _rcsid[] = "$Id: image_bincue.c,v 1.1 2003/10/13 11:47:12 f1rmb Exp $";
+
+/* reader */
+
+#define DEFAULT_VCD_DEVICE "videocd.bin"
+
+/****************************************************************************
+ * writer
+ */
+
+typedef struct {
+ bool sector_2336_flag;
+ VcdDataSink *bin_snk;
+ VcdDataSink *cue_snk;
+ char *bin_fname;
+ char *cue_fname;
+
+ bool init;
+} _img_bincue_snk_t;
+
+static void
+_sink_init (_img_bincue_snk_t *_obj)
+{
+ if (_obj->init)
+ return;
+
+ if (!(_obj->bin_snk = vcd_data_sink_new_stdio (_obj->bin_fname)))
+ vcd_error ("init failed");
+
+ if (!(_obj->cue_snk = vcd_data_sink_new_stdio (_obj->cue_fname)))
+ vcd_error ("init failed");
+
+ _obj->init = true;
+}
+
+static void
+_sink_free (void *user_data)
+{
+ _img_bincue_snk_t *_obj = user_data;
+
+ vcd_data_sink_destroy (_obj->bin_snk);
+ vcd_data_sink_destroy (_obj->cue_snk);
+ free (_obj->bin_fname);
+ free (_obj->cue_fname);
+ free (_obj);
+}
+
+static int
+_set_cuesheet (void *user_data, const VcdList *vcd_cue_list)
+{
+ _img_bincue_snk_t *_obj = user_data;
+ VcdListNode *node;
+ int track_no, index_no;
+ const vcd_cue_t *_last_cue = 0;
+
+ _sink_init (_obj);
+
+ vcd_data_sink_printf (_obj->cue_snk, "FILE \"%s\" BINARY\r\n",
+ _obj->bin_fname);
+
+ track_no = 0;
+ index_no = 0;
+ _VCD_LIST_FOREACH (node, (VcdList *) vcd_cue_list)
+ {
+ const vcd_cue_t *_cue = _vcd_list_node_data (node);
+
+ msf_t _msf = { 0, 0, 0 };
+
+ switch (_cue->type)
+ {
+ case VCD_CUE_TRACK_START:
+ track_no++;
+ index_no = 0;
+
+ vcd_data_sink_printf (_obj->cue_snk,
+ " TRACK %2.2d MODE2/%d\r\n"
+ " FLAGS DCP\r\n",
+ track_no, (_obj->sector_2336_flag ? 2336 : 2352));
+
+ if (_last_cue && _last_cue->type == VCD_CUE_PREGAP_START)
+ {
+ cdio_lba_to_msf (_last_cue->lsn, &_msf);
+
+ vcd_data_sink_printf (_obj->cue_snk,
+ " INDEX %2.2d %2.2x:%2.2x:%2.2x\r\n",
+ index_no, _msf.m, _msf.s, _msf.f);
+ }
+
+ index_no++;
+
+ cdio_lba_to_msf (_cue->lsn, &_msf);
+
+ vcd_data_sink_printf (_obj->cue_snk,
+ " INDEX %2.2d %2.2x:%2.2x:%2.2x\r\n",
+ index_no, _msf.m, _msf.s, _msf.f);
+ break;
+
+ case VCD_CUE_PREGAP_START:
+ /* handled in next iteration */
+ break;
+
+ case VCD_CUE_SUBINDEX:
+ vcd_assert (_last_cue != 0);
+
+ index_no++;
+ vcd_assert (index_no < 100);
+
+ cdio_lba_to_msf (_cue->lsn, &_msf);
+
+ vcd_data_sink_printf (_obj->cue_snk,
+ " INDEX %2.2d %2.2x:%2.2x:%2.2x\r\n",
+ index_no, _msf.m, _msf.s, _msf.f);
+ break;
+
+ case VCD_CUE_END:
+ vcd_data_sink_close (_obj->cue_snk);
+ return 0;
+ break;
+
+ case VCD_CUE_LEADIN:
+ break;
+ }
+
+ _last_cue = _cue;
+ }
+
+ vcd_assert_not_reached ();
+
+ return -1;
+}
+
+static int
+_vcd_image_bincue_write (void *user_data, const void *data, uint32_t lsn)
+{
+ const char *buf = data;
+ _img_bincue_snk_t *_obj = user_data;
+ long offset = lsn;
+
+ _sink_init (_obj);
+
+ offset *= _obj->sector_2336_flag ? M2RAW_SECTOR_SIZE : CDIO_CD_FRAMESIZE_RAW;
+
+ vcd_data_sink_seek(_obj->bin_snk, offset);
+
+ if (_obj->sector_2336_flag)
+ vcd_data_sink_write(_obj->bin_snk, buf + 12 + 4, M2RAW_SECTOR_SIZE, 1);
+ else
+ vcd_data_sink_write(_obj->bin_snk, buf, CDIO_CD_FRAMESIZE_RAW, 1);
+
+ return 0;
+}
+
+static int
+_sink_set_arg (void *user_data, const char key[], const char value[])
+{
+ _img_bincue_snk_t *_obj = user_data;
+
+ if (!strcmp (key, "bin"))
+ {
+ free (_obj->bin_fname);
+
+ if (!value)
+ return -2;
+
+ _obj->bin_fname = strdup (value);
+ }
+ else if (!strcmp (key, "cue"))
+ {
+ free (_obj->cue_fname);
+
+ if (!value)
+ return -2;
+
+ _obj->cue_fname = strdup (value);
+ }
+ else if (!strcmp (key, "sector"))
+ {
+ if (!strcmp (value, "2336"))
+ _obj->sector_2336_flag = true;
+ else if (!strcmp (value, "2352"))
+ _obj->sector_2336_flag = false;
+ else
+ return -2;
+ }
+ else
+ return -1;
+
+ return 0;
+}
+
+VcdImageSink *
+vcd_image_sink_new_bincue (void)
+{
+ _img_bincue_snk_t *_data;
+
+ vcd_image_sink_funcs _funcs = {
+ .set_cuesheet = _set_cuesheet,
+ .write = _vcd_image_bincue_write,
+ .free = _sink_free,
+ .set_arg = _sink_set_arg
+ };
+
+ _data = _vcd_malloc (sizeof (_img_bincue_snk_t));
+
+ _data->bin_fname = strdup ("videocd.bin");
+ _data->cue_fname = strdup ("videocd.cue");
+
+ return vcd_image_sink_new (_data, &_funcs);
+}
+
diff --git a/src/input/vcd/libvcd/image_cdrdao.c b/src/input/vcd/libvcd/image_cdrdao.c
new file mode 100644
index 000000000..3754721c2
--- /dev/null
+++ b/src/input/vcd/libvcd/image_cdrdao.c
@@ -0,0 +1,312 @@
+/*
+ $Id: image_cdrdao.c,v 1.1 2003/10/13 11:47:12 f1rmb Exp $
+
+ Copyright (C) 2001 Herbert Valerio Riedel <hvr@gnu.org>
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+*/
+
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+/* Public headers */
+#include <cdio/iso9660.h>
+#include <libvcd/sector.h>
+#include <libvcd/logging.h>
+
+/* Private headers */
+#include "assert.h"
+#include "bytesex.h"
+#include "image_sink.h"
+#include "stream_stdio.h"
+#include "util.h"
+#include "vcd.h"
+
+static const char _rcsid[] = "$Id: image_cdrdao.c,v 1.1 2003/10/13 11:47:12 f1rmb Exp $";
+
+/* reader */
+
+/****************************************************************************
+ * writer
+ */
+
+typedef struct {
+ bool sector_2336_flag;
+ char *toc_fname;
+ char *img_base;
+
+ VcdDataSink *last_bin_snk;
+ int last_snk_idx;
+ bool last_pause;
+
+ VcdList *vcd_cue_list;
+} _img_cdrdao_snk_t;
+
+static void
+_sink_free (void *user_data)
+{
+ _img_cdrdao_snk_t *_obj = user_data;
+
+ /* fixme -- destroy cue list */
+
+ vcd_data_sink_destroy (_obj->last_bin_snk);
+ free (_obj->toc_fname);
+ free (_obj->img_base);
+ free (_obj);
+}
+
+static int
+_set_cuesheet (void *user_data, const VcdList *vcd_cue_list)
+{
+ _img_cdrdao_snk_t *_obj = user_data;
+ VcdDataSink *toc_snk = vcd_data_sink_new_stdio (_obj->toc_fname);
+
+ VcdListNode *node;
+
+ int track_no, index_no;
+ const vcd_cue_t *_last_cue = 0;
+ unsigned last_track_lsn = 0;
+
+ vcd_data_sink_printf (toc_snk,
+ "// CDRDAO TOC\n"
+ "// generated by %s\n\n"
+ "CD_ROM_XA\n", vcd_version_string (false));
+
+ _obj->vcd_cue_list = _vcd_list_new ();
+
+ index_no = track_no = 0;
+ _VCD_LIST_FOREACH (node, (VcdList *) vcd_cue_list)
+ {
+ const vcd_cue_t *_cue = _vcd_list_node_data (node);
+
+ /* copy cue list while traversing */
+ {
+ vcd_cue_t *_cue2 = _vcd_malloc (sizeof (vcd_cue_t));
+ *_cue2 = *_cue;
+ _vcd_list_append (_obj->vcd_cue_list, _cue2);
+ }
+
+ switch (_cue->type)
+ {
+ case VCD_CUE_TRACK_START:
+ track_no++;
+ index_no = 0;
+
+ last_track_lsn = _cue->lsn;
+
+ vcd_data_sink_printf (toc_snk,
+ "\n// Track %d\n"
+ "TRACK %s\n COPY\n",
+ track_no,
+ (_obj->sector_2336_flag ? "MODE2_FORM_MIX" : "MODE2_RAW"));
+
+ if (_last_cue && _last_cue->type == VCD_CUE_PREGAP_START)
+ vcd_data_sink_printf (toc_snk,
+ " DATAFILE \"%s_%.2d_pregap.img\"\n"
+ " START\n",
+ _obj->img_base, track_no);
+
+ index_no++;
+
+ vcd_data_sink_printf (toc_snk,
+ " DATAFILE \"%s_%.2d.img\"\n",
+ _obj->img_base, track_no);
+ break;
+
+ case VCD_CUE_PREGAP_START:
+ /* handled in next iteration */
+ break;
+
+ case VCD_CUE_SUBINDEX:
+ index_no++;
+
+ {
+ msf_t _msf = { 0, 0, 0 };
+
+ cdio_lba_to_msf (_cue->lsn - last_track_lsn, &_msf);
+
+ vcd_data_sink_printf (toc_snk,
+ " INDEX %2.2x:%2.2x:%2.2x\n",
+ _msf.m, _msf.s, _msf.f);
+ }
+ break;
+
+ case VCD_CUE_LEADIN:
+ break;
+
+ case VCD_CUE_END:
+ vcd_data_sink_printf (toc_snk, "\n// EOF\n");
+
+ vcd_data_sink_close (toc_snk);
+ vcd_data_sink_destroy (toc_snk);
+
+ return 0;
+ break;
+ }
+
+ _last_cue = _cue;
+ }
+
+ vcd_assert_not_reached ();
+
+ return -1;
+}
+
+static int
+_vcd_image_cdrdao_write (void *user_data, const void *data, uint32_t lsn)
+{
+ const char *buf = data;
+ _img_cdrdao_snk_t *_obj = user_data;
+ long offset;
+
+ {
+ VcdListNode *node;
+ uint32_t _last = 0;
+ uint32_t _ofs = 0;
+ bool _lpregap = false;
+ bool _pregap = false;
+
+ int num = 0, in_track = 0;
+ _VCD_LIST_FOREACH (node, _obj->vcd_cue_list)
+ {
+ const vcd_cue_t *_cue = _vcd_list_node_data (node);
+
+ switch (_cue->type)
+ {
+ case VCD_CUE_PREGAP_START:
+ case VCD_CUE_END:
+ case VCD_CUE_TRACK_START:
+ if (_cue->lsn && IN (lsn, _last, _cue->lsn - 1))
+ {
+ vcd_assert (in_track == 0);
+ in_track = num;
+ _ofs = _last;
+ _pregap = _lpregap;
+ }
+
+ _last = _cue->lsn;
+ _lpregap = (_cue->type == VCD_CUE_PREGAP_START);
+
+ if (_cue->type == VCD_CUE_TRACK_START)
+ num++;
+ break;
+
+ default:
+ /* noop */
+ break;
+ }
+ }
+
+ vcd_assert (in_track != 0);
+ vcd_assert (_obj->last_snk_idx <= in_track);
+
+ if (_obj->last_snk_idx != in_track
+ || _obj->last_pause != _pregap)
+ {
+ char buf[4096] = { 0, };
+
+ if (_obj->last_bin_snk)
+ vcd_data_sink_destroy (_obj->last_bin_snk);
+
+ snprintf (buf, sizeof (buf),
+ "%s_%.2d%s.img",
+ _obj->img_base,
+ (_pregap ? in_track + 1 : in_track),
+ (_pregap ? "_pregap" : ""));
+
+ _obj->last_bin_snk = vcd_data_sink_new_stdio (buf);
+ _obj->last_snk_idx = in_track;
+ _obj->last_pause = _pregap;
+ }
+
+ vcd_assert (lsn >= _ofs);
+ offset = lsn - _ofs;
+ }
+
+ offset *= _obj->sector_2336_flag ? M2RAW_SECTOR_SIZE : CDIO_CD_FRAMESIZE_RAW;
+
+ vcd_data_sink_seek(_obj->last_bin_snk, offset);
+
+ if (_obj->sector_2336_flag)
+ vcd_data_sink_write(_obj->last_bin_snk, buf + 12 + 4, M2RAW_SECTOR_SIZE, 1);
+ else
+ vcd_data_sink_write(_obj->last_bin_snk, buf, CDIO_CD_FRAMESIZE_RAW, 1);
+
+ return 0;
+}
+
+static int
+_sink_set_arg (void *user_data, const char key[], const char value[])
+{
+ _img_cdrdao_snk_t *_obj = user_data;
+
+ if (!strcmp (key, "toc"))
+ {
+ free (_obj->toc_fname);
+
+ if (!value)
+ return -2;
+
+ _obj->toc_fname = strdup (value);
+ }
+ else if (!strcmp (key, "img_base"))
+ {
+ free (_obj->img_base);
+
+ if (!value)
+ return -2;
+
+ _obj->img_base = strdup (value);
+ }
+ else if (!strcmp (key, "sector"))
+ {
+ if (!strcmp (value, "2336"))
+ _obj->sector_2336_flag = true;
+ else if (!strcmp (value, "2352"))
+ _obj->sector_2336_flag = false;
+ else
+ return -2;
+ }
+ else
+ return -1;
+
+ return 0;
+}
+
+VcdImageSink *
+vcd_image_sink_new_cdrdao (void)
+{
+ _img_cdrdao_snk_t *_data;
+
+ vcd_image_sink_funcs _funcs = {
+ .set_cuesheet = _set_cuesheet,
+ .write = _vcd_image_cdrdao_write,
+ .free = _sink_free,
+ .set_arg = _sink_set_arg
+ };
+
+ _data = _vcd_malloc (sizeof (_img_cdrdao_snk_t));
+
+ _data->toc_fname = strdup ("videocd.toc");
+ _data->img_base = strdup ("videocd");
+
+ return vcd_image_sink_new (_data, &_funcs);
+}
+
diff --git a/src/input/vcd/libvcd/image_nrg.c b/src/input/vcd/libvcd/image_nrg.c
new file mode 100644
index 000000000..e94e62b4e
--- /dev/null
+++ b/src/input/vcd/libvcd/image_nrg.c
@@ -0,0 +1,350 @@
+/*
+ $Id: image_nrg.c,v 1.1 2003/10/13 11:47:12 f1rmb Exp $
+
+ Copyright (C) 2001,2003 Herbert Valerio Riedel <hvr@gnu.org>
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+*/
+
+/*! This code implements low-level access functions for Nero's native
+ CD-image format residing inside a disk file (*.nrg).
+*/
+
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include <cdio/cdio.h>
+#include <cdio/iso9660.h>
+
+/* Public headers */
+#include <libvcd/sector.h>
+#include <libvcd/logging.h>
+
+/* Private headers */
+#include "assert.h"
+#include "bytesex.h"
+#include "image_sink.h"
+#include "stream_stdio.h"
+#include "util.h"
+
+static const char _rcsid[] = "$Id: image_nrg.c,v 1.1 2003/10/13 11:47:12 f1rmb Exp $";
+
+/* structures used */
+
+/* this ugly image format is typical for lazy win32 programmers... at
+ least structure were set big endian, so at reverse
+ engineering wasn't such a big headache... */
+
+PRAGMA_BEGIN_PACKED
+typedef struct {
+ uint32_t start GNUC_PACKED;
+ uint32_t length GNUC_PACKED;
+ uint32_t type GNUC_PACKED; /* only 0x3 seen so far...
+ -> MIXED_MODE2 2336 blocksize */
+ uint32_t start_lsn GNUC_PACKED; /* does not include any pre-gaps! */
+ uint32_t _unknown GNUC_PACKED; /* wtf is this for? -- always zero... */
+} _etnf_array_t;
+
+/* finally they realized that 32bit offsets are a bit outdated for IA64 *eg* */
+typedef struct {
+ uint64_t start GNUC_PACKED;
+ uint64_t length GNUC_PACKED;
+ uint32_t type GNUC_PACKED;
+ uint32_t start_lsn GNUC_PACKED;
+ uint64_t _unknown GNUC_PACKED; /* wtf is this for? -- always zero... */
+} _etn2_array_t;
+
+typedef struct {
+ uint8_t _unknown1 GNUC_PACKED; /* 0x41 == 'A' */
+ uint8_t track GNUC_PACKED; /* binary or BCD?? */
+ uint8_t index GNUC_PACKED; /* makes 0->1 transitions */
+ uint8_t _unknown2 GNUC_PACKED; /* ?? */
+ uint32_t lsn GNUC_PACKED;
+} _cuex_array_t;
+
+typedef struct {
+ uint32_t id GNUC_PACKED;
+ uint32_t len GNUC_PACKED;
+ char data[EMPTY_ARRAY_SIZE] GNUC_PACKED;
+} _chunk_t;
+
+PRAGMA_END_PACKED
+
+/* to be converted into BE */
+#define CUEX_ID 0x43554558
+#define CUES_ID 0x43554553
+#define DAOX_ID 0x44414f58
+#define DAOI_ID 0x44414f49
+#define END1_ID 0x454e4421
+#define ETN2_ID 0x45544e32
+#define ETNF_ID 0x45544e46
+#define NER5_ID 0x4e455235
+#define NERO_ID 0x4e45524f
+#define SINF_ID 0x53494e46
+
+/****************************************************************************
+ * writer
+ */
+
+typedef struct {
+ VcdDataSink *nrg_snk;
+ char *nrg_fname;
+
+ VcdList *vcd_cue_list;
+ int tracks;
+ uint32_t cue_end_lsn;
+
+ bool init;
+} _img_nrg_snk_t;
+
+static void
+_sink_init (_img_nrg_snk_t *_obj)
+{
+ if (_obj->init)
+ return;
+
+ if (!(_obj->nrg_snk = vcd_data_sink_new_stdio (_obj->nrg_fname)))
+ vcd_error ("init failed");
+
+ _obj->init = true;
+}
+
+
+static void
+_sink_free (void *user_data)
+{
+ _img_nrg_snk_t *_obj = user_data;
+
+ free (_obj->nrg_fname);
+ vcd_data_sink_destroy (_obj->nrg_snk);
+
+ free (_obj);
+}
+
+static int
+_set_cuesheet (void *user_data, const VcdList *vcd_cue_list)
+{
+ _img_nrg_snk_t *_obj = user_data;
+ VcdListNode *node;
+ int num;
+
+ _sink_init (_obj);
+
+ _obj->vcd_cue_list = _vcd_list_new ();
+
+ num = 0;
+ _VCD_LIST_FOREACH (node, (VcdList *) vcd_cue_list)
+ {
+ const vcd_cue_t *_cue = _vcd_list_node_data (node);
+ vcd_cue_t *_cue2 = _vcd_malloc (sizeof (vcd_cue_t));
+ *_cue2 = *_cue;
+ _vcd_list_append (_obj->vcd_cue_list, _cue2);
+
+ if (_cue->type == VCD_CUE_TRACK_START)
+ num++;
+
+ if (_cue->type == VCD_CUE_END)
+ _obj->cue_end_lsn = _cue->lsn;
+ }
+
+ _obj->tracks = num;
+
+ vcd_assert (num > 0 && num < 100);
+
+ return 0;
+}
+
+static uint32_t
+_map (_img_nrg_snk_t *_obj, uint32_t lsn)
+{
+ VcdListNode *node;
+ uint32_t result = lsn;
+ vcd_cue_t *_cue = NULL, *_last = NULL;
+
+ vcd_assert (_obj->cue_end_lsn > lsn);
+
+ _VCD_LIST_FOREACH (node, _obj->vcd_cue_list)
+ {
+ _cue = _vcd_list_node_data (node);
+
+ if (lsn < _cue->lsn)
+ break;
+
+ switch (_cue->type)
+ {
+ case VCD_CUE_TRACK_START:
+ result -= _cue->lsn;
+ break;
+ case VCD_CUE_PREGAP_START:
+ result += _cue->lsn;
+ break;
+ default:
+ break;
+ }
+
+ _last = _cue;
+ }
+
+ vcd_assert (node != NULL);
+
+ switch (_last->type)
+ {
+ case VCD_CUE_TRACK_START:
+ return result;
+ break;
+
+ case VCD_CUE_PREGAP_START:
+ return -1;
+ break;
+
+ default:
+ case VCD_CUE_END:
+ vcd_assert_not_reached ();
+ break;
+ }
+
+ return -1;
+}
+
+static int
+_write_tail (_img_nrg_snk_t *_obj, uint32_t offset)
+{
+ VcdListNode *node;
+ int _size;
+ _chunk_t _chunk;
+
+ vcd_data_sink_seek (_obj->nrg_snk, offset);
+
+ _size = _obj->tracks * sizeof (_etnf_array_t);
+ _chunk.id = UINT32_TO_BE (ETNF_ID);
+ _chunk.len = uint32_to_be (_size);
+
+ vcd_data_sink_write (_obj->nrg_snk, &_chunk, sizeof (_chunk_t), 1);
+
+ _VCD_LIST_FOREACH (node, _obj->vcd_cue_list)
+ {
+ vcd_cue_t *_cue = _vcd_list_node_data (node);
+
+ if (_cue->type == VCD_CUE_TRACK_START)
+ {
+ vcd_cue_t *_cue2 = _vcd_list_node_data (_vcd_list_node_next (node));
+
+ _etnf_array_t _etnf = { 0, };
+
+ _etnf.type = UINT32_TO_BE (0x3);
+ _etnf.start_lsn = uint32_to_be (_map (_obj, _cue->lsn));
+ _etnf.start = uint32_to_be (_map (_obj, _cue->lsn) * M2RAW_SECTOR_SIZE);
+
+ _etnf.length = uint32_to_be ((_cue2->lsn - _cue->lsn) * M2RAW_SECTOR_SIZE);
+
+ vcd_data_sink_write (_obj->nrg_snk, &_etnf, sizeof (_etnf_array_t), 1);
+ }
+
+ }
+
+ {
+ uint32_t tracks = uint32_to_be (_obj->tracks);
+
+ _chunk.id = UINT32_TO_BE (SINF_ID);
+ _chunk.len = UINT32_TO_BE (sizeof (uint32_t));
+ vcd_data_sink_write (_obj->nrg_snk, &_chunk, sizeof (_chunk_t), 1);
+
+ vcd_data_sink_write (_obj->nrg_snk, &tracks, sizeof (uint32_t), 1);
+ }
+
+ _chunk.id = UINT32_TO_BE (END1_ID);
+ _chunk.len = UINT32_TO_BE (0);
+ vcd_data_sink_write (_obj->nrg_snk, &_chunk, sizeof (_chunk_t), 1);
+
+ _chunk.id = UINT32_TO_BE (NERO_ID);
+ _chunk.len = uint32_to_be (offset);
+ vcd_data_sink_write (_obj->nrg_snk, &_chunk, sizeof (_chunk_t), 1);
+
+ return 0;
+}
+
+static int
+_vcd_image_nrg_write (void *user_data, const void *data, uint32_t lsn)
+{
+ const char *buf = data;
+ _img_nrg_snk_t *_obj = user_data;
+ uint32_t _lsn = _map (_obj, lsn);
+
+ _sink_init (_obj);
+
+ if (_lsn == -1)
+ {
+ /* vcd_debug ("ignoring %d", lsn); */
+ return 0;
+ }
+
+ vcd_data_sink_seek(_obj->nrg_snk, _lsn * M2RAW_SECTOR_SIZE);
+ vcd_data_sink_write(_obj->nrg_snk, buf + 12 + 4, M2RAW_SECTOR_SIZE, 1);
+
+ if (_obj->cue_end_lsn - 1 == lsn)
+ {
+ vcd_debug ("ENDLSN reached! (%lu == %lu)",
+ (long unsigned int) lsn, (long unsigned int) _lsn);
+ return _write_tail (_obj, (_lsn + 1) * M2RAW_SECTOR_SIZE);
+ }
+
+ return 0;
+}
+
+static int
+_sink_set_arg (void *user_data, const char key[], const char value[])
+{
+ _img_nrg_snk_t *_obj = user_data;
+
+ if (!strcmp (key, "nrg"))
+ {
+ free (_obj->nrg_fname);
+
+ if (!value)
+ return -2;
+
+ _obj->nrg_fname = strdup (value);
+ }
+ else
+ return -1;
+
+ return 0;
+}
+
+VcdImageSink *
+vcd_image_sink_new_nrg (void)
+{
+ _img_nrg_snk_t *_data;
+
+ vcd_image_sink_funcs _funcs = {
+ .set_cuesheet = _set_cuesheet,
+ .write = _vcd_image_nrg_write,
+ .free = _sink_free,
+ .set_arg = _sink_set_arg
+ };
+
+ _data = _vcd_malloc (sizeof (_img_nrg_snk_t));
+ _data->nrg_fname = strdup ("videocd.nrg");
+
+ vcd_warn ("opening TAO NRG image for writing; TAO (S)VCD are NOT 100%% compliant!");
+
+ return vcd_image_sink_new (_data, &_funcs);
+}
+
diff --git a/src/input/vcd/libvcd/image_sink.h b/src/input/vcd/libvcd/image_sink.h
new file mode 100644
index 000000000..40d0f4867
--- /dev/null
+++ b/src/input/vcd/libvcd/image_sink.h
@@ -0,0 +1,80 @@
+/*
+ $Id: image_sink.h,v 1.1 2003/10/13 11:47:12 f1rmb Exp $
+
+ Copyright (C) 2001 Herbert Valerio Riedel <hvr@gnu.org>
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+*/
+
+#ifndef __VCD_IMAGE_SINK_H__
+#define __VCD_IMAGE_SINK_H__
+
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
+
+#include <cdio/cdio.h>
+#include <libvcd/types.h>
+
+/* Private includes */
+#include "data_structures.h"
+#include "stream.h"
+
+/* VcdImageSink ( --> image writer) */
+
+typedef struct _VcdImageSink VcdImageSink;
+
+typedef struct {
+ uint32_t lsn;
+ enum {
+ VCD_CUE_TRACK_START = 1, /* INDEX 0 -> 1 transition, TOC entry */
+ VCD_CUE_PREGAP_START, /* INDEX = 0 start */
+ VCD_CUE_SUBINDEX, /* INDEX++; sub-index */
+ VCD_CUE_END, /* lead-out start */
+ VCD_CUE_LEADIN, /* lead-in start */
+ } type;
+} vcd_cue_t;
+
+typedef struct {
+ int (*set_cuesheet) (void *user_data, const VcdList *vcd_cue_list);
+ int (*write) (void *user_data, const void *buf, lsn_t lsn);
+ void (*free) (void *user_data);
+ int (*set_arg) (void *user_data, const char key[], const char value[]);
+} vcd_image_sink_funcs;
+
+VcdImageSink *
+vcd_image_sink_new (void *user_data, const vcd_image_sink_funcs *funcs);
+
+void
+vcd_image_sink_destroy (VcdImageSink *obj);
+
+int
+vcd_image_sink_set_cuesheet (VcdImageSink *obj, const VcdList *vcd_cue_list);
+
+int
+vcd_image_sink_write (VcdImageSink *obj, void *buf, lsn_t lsn);
+
+/*!
+ Set the arg "key" with "value" in the target device.
+*/
+int
+vcd_image_sink_set_arg (VcdImageSink *obj, const char key[],
+ const char value[]);
+
+VcdImageSink * vcd_image_sink_new_nrg (void);
+VcdImageSink * vcd_image_sink_new_bincue (void);
+VcdImageSink * vcd_image_sink_new_cdrdao (void);
+
+#endif /* __VCD_IMAGE_SINK_H__ */
diff --git a/src/input/vcd/libvcd/inf.c b/src/input/vcd/libvcd/inf.c
new file mode 100644
index 000000000..699bc24ad
--- /dev/null
+++ b/src/input/vcd/libvcd/inf.c
@@ -0,0 +1,596 @@
+/*
+ $Id: inf.c,v 1.1 2003/10/13 11:47:12 f1rmb Exp $
+
+ Copyright (C) 2002,2003 Rocky Bernstein <rocky@panix.com>
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Foundation
+ Software, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+*/
+
+/*
+ Things here refer to lower-level structures using a structure other
+ than vcdinfo_t. For higher-level structures via the vcdinfo_t, see
+ info.c
+*/
+
+
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
+
+#include <stdio.h>
+#include <stddef.h>
+#include <errno.h>
+
+#ifdef HAVE_STDLIB_H
+#include <stdlib.h>
+#endif
+#ifdef HAVE_STRING_H
+#include <string.h>
+#endif
+#ifdef HAVE_SYS_TYPES_H
+#include <sys/types.h>
+#endif
+#ifdef HAVE_SYS_STAT_H
+#include <sys/stat.h>
+#endif
+#ifdef HAVE_UNISTD_H
+#include <unistd.h>
+#endif
+
+#include <cdio/cdio.h>
+#include <cdio/util.h>
+
+/* Eventually move above libvcd includes but having vcdinfo including. */
+#include <libvcd/info.h>
+
+/* Private headers */
+#include "bytesex.h"
+#include "info_private.h"
+#include "pbc.h"
+
+static const char _rcsid[] = "$Id: inf.c,v 1.1 2003/10/13 11:47:12 f1rmb Exp $";
+
+#define BUF_COUNT 16
+#define BUF_SIZE 80
+
+/* Return a pointer to a internal free buffer */
+static char *
+_getbuf (void)
+{
+ static char _buf[BUF_COUNT][BUF_SIZE];
+ static int _num = -1;
+
+ _num++;
+ _num %= BUF_COUNT;
+
+ memset (_buf[_num], 0, BUF_SIZE);
+
+ return _buf[_num];
+}
+
+const char *
+vcdinf_area_str (const struct psd_area_t *_area)
+{
+ char *buf;
+
+ if (!_area->x1
+ && !_area->y1
+ && !_area->x2
+ && !_area->y2)
+ return "disabled";
+
+ buf = _getbuf ();
+
+ snprintf (buf, BUF_SIZE, "[%3d,%3d] - [%3d,%3d]",
+ _area->x1, _area->y1,
+ _area->x2, _area->y2);
+
+ return buf;
+}
+
+/*!
+ Return a string containing the VCD album id, or NULL if there is
+ some problem in getting this.
+*/
+const char *
+vcdinf_get_album_id(const InfoVcd *info)
+{
+ if (NULL==info) return NULL;
+ return vcdinfo_strip_trail (info->album_desc, MAX_ALBUM_LEN);
+}
+
+/*!
+ Return the VCD application ID.
+ NULL is returned if there is some problem in getting this.
+*/
+const char *
+vcdinf_get_application_id(const iso9660_pvd_t *pvd)
+{
+ if (NULL==pvd) return NULL;
+ return(vcdinfo_strip_trail(pvd->application_id, MAX_APPLICATION_ID));
+}
+
+/*!
+ Get autowait time value for PsdPlayListDescriptor *d.
+ Time is in seconds unless it is -1 (unlimited).
+*/
+int
+vcdinf_get_autowait_time (const PsdPlayListDescriptor *d)
+{
+ return vcdinfo_get_wait_time (d->atime);
+}
+
+/*!
+ Return the base selection number. VCD_INVALID_BSN is returned if there
+ is an error.
+*/
+unsigned int
+vcdinf_get_bsn(const PsdSelectionListDescriptor *psd)
+{
+ if (NULL==psd) return VCDINFO_INVALID_BSN;
+ return(psd->bsn);
+}
+
+/*!
+ Return a string giving VCD format (VCD 1.0 VCD 1.1, SVCD, ...
+ for this object.
+*/
+const char *
+vcdinf_get_format_version_str (vcd_type_t vcd_type)
+{
+ switch (vcd_type)
+ {
+ case VCD_TYPE_VCD:
+ return ("VCD 1.0");
+ break;
+ case VCD_TYPE_VCD11:
+ return ("VCD 1.1");
+ break;
+ case VCD_TYPE_VCD2:
+ return ("VCD 2.0");
+ break;
+ case VCD_TYPE_SVCD:
+ return ("SVCD");
+ break;
+ case VCD_TYPE_HQVCD:
+ return ("HQVCD");
+ break;
+ case VCD_TYPE_INVALID:
+ return ("INVALID");
+ break;
+ default:
+ return ( "????");
+ }
+}
+
+/*!
+ Return loop count. 0 is infinite loop.
+*/
+uint16_t
+vcdinf_get_loop_count (const PsdSelectionListDescriptor *psd)
+{
+ return 0x7f & psd->loop;
+}
+
+/*!
+ Return LOT offset
+*/
+uint16_t
+vcdinf_get_lot_offset (const LotVcd *lot, unsigned int n)
+{
+ return uint16_from_be (lot->offset[n]);
+}
+
+/*!
+ Return the number of entries in the VCD.
+*/
+unsigned int
+vcdinf_get_num_entries(const EntriesVcd *entries)
+{
+ if (NULL==entries) return 0;
+ return (uint16_from_be (entries->entry_count));
+}
+
+/*!
+ Return the number of segments in the VCD.
+*/
+segnum_t
+vcdinf_get_num_segments(const InfoVcd *info)
+{
+ if (NULL==info) return 0;
+ return (uint16_from_be (info->item_count));
+}
+
+/*!
+ Return number of LIDs.
+*/
+lid_t
+vcdinf_get_num_LIDs (const InfoVcd *info)
+{
+ if (NULL==info) return 0;
+ /* Should probably use _vcd_pbc_max_lid instead? */
+ return uint16_from_be (info->lot_entries);
+}
+
+/*!
+ Return the number of menu selections for selection list descriptor psd.
+*/
+unsigned int
+vcdinf_get_num_selections(const PsdSelectionListDescriptor *psd)
+{
+ return psd->nos;
+}
+
+/*!
+ Get play time value for PsdPlayListDescriptor *d.
+ Time is in 1/15-second units.
+*/
+uint16_t
+vcdinf_get_play_time (const PsdPlayListDescriptor *d)
+{
+ if (NULL==d) return 0;
+ return uint16_from_be (d->ptime);
+}
+
+/*!
+ Return a string containing the VCD preparer id with trailing
+ blanks removed.
+*/
+const char *
+vcdinf_get_preparer_id(const iso9660_pvd_t *pvd)
+{
+ if (NULL==pvd) return NULL;
+ return(vcdinfo_strip_trail(pvd->preparer_id, MAX_PREPARER_ID));
+}
+
+/*!
+ Return a string containing the VCD publisher id with trailing
+ blanks removed.
+*/
+const char *
+vcdinf_get_publisher_id(const iso9660_pvd_t *pvd)
+{
+ if (NULL==pvd) return NULL;
+ return(vcdinfo_strip_trail(pvd->publisher_id, MAX_PUBLISHER_ID));
+}
+
+/*!
+ Return number of bytes in PSD.
+*/
+uint32_t
+vcdinf_get_psd_size (const InfoVcd *info)
+{
+ if (NULL==info) return 0;
+ return uint32_from_be (info->psd_size);
+}
+
+/*!
+ Return a string containing the VCD system id with trailing
+ blanks removed.
+*/
+const char *
+vcdinf_get_system_id(const iso9660_pvd_t *pvd)
+{
+ if (NULL==pvd) return NULL;
+ return(vcdinfo_strip_trail(pvd->system_id, MAX_SYSTEM_ID));
+}
+
+/*!
+ Get timeout wait time value for PsdPlayListDescriptor *d.
+ Return VCDINFO_INVALID_OFFSET if d is NULL;
+ Time is in seconds unless it is -1 (unlimited).
+*/
+uint16_t
+vcdinf_get_timeout_offset (const PsdSelectionListDescriptor *d)
+{
+ if (NULL == d) return VCDINFO_INVALID_OFFSET;
+ return uint16_from_be (d->timeout_ofs);
+}
+
+/*!
+ Get timeout wait time value for PsdPlayListDescriptor *d.
+ Time is in seconds unless it is -1 (unlimited).
+*/
+int
+vcdinf_get_timeout_time (const PsdSelectionListDescriptor *d)
+{
+ return vcdinfo_get_wait_time (d->totime);
+}
+
+/*!
+ Return the track number for entry n in obj. The first track starts
+ at 1. Note this is one less than the track number reported in vcddump.
+ (We don't count the header track?)
+*/
+track_t
+vcdinf_get_track(const EntriesVcd *entries, const unsigned int entry_num)
+{
+ const unsigned int entry_count = uint16_from_be (entries->entry_count);
+ /* Note entry_num is 0 origin. */
+ return entry_num < entry_count ?
+ from_bcd8 (entries->entry[entry_num].n):
+ VCDINFO_INVALID_TRACK;
+}
+
+/*!
+ Return the VCD volume count - the number of CD's in the collection.
+*/
+unsigned int
+vcdinf_get_volume_count(const InfoVcd *info)
+{
+ if (NULL==info) return 0;
+ return(uint16_from_be( info->vol_count));
+}
+
+/*!
+ Return the VCD ID.
+*/
+const char *
+vcdinf_get_volume_id(const iso9660_pvd_t *pvd)
+{
+ if (NULL == pvd) return NULL;
+ return(vcdinfo_strip_trail(pvd->volume_id, MAX_VOLUME_ID));
+}
+
+/*!
+ Return the VCD volume num - the number of the CD in the collection.
+ This is a number between 1 and the volume count.
+*/
+unsigned int
+vcdinf_get_volume_num(const InfoVcd *info)
+{
+ if (NULL == info) return 0;
+ return uint16_from_be(info->vol_id);
+}
+
+/*!
+ Return the VCD volumeset ID.
+ NULL is returned if there is some problem in getting this.
+*/
+const char *
+vcdinf_get_volumeset_id(const iso9660_pvd_t *pvd)
+{
+ if ( NULL == pvd ) return NULL;
+ return vcdinfo_strip_trail(pvd->volume_set_id, MAX_VOLUMESET_ID);
+}
+
+/*!
+ Get wait time value for PsdPlayListDescriptor *d.
+ Time is in seconds unless it is -1 (unlimited).
+*/
+int
+vcdinf_get_wait_time (const PsdPlayListDescriptor *d)
+{
+ return vcdinfo_get_wait_time (d->wtime);
+}
+
+/*!
+ Return true if loop has a jump delay
+*/
+bool
+vcdinf_has_jump_delay (const PsdSelectionListDescriptor *psd)
+{
+ if (NULL==psd) return false;
+ return ((0x80 & psd->loop) != 0);
+}
+
+/*!
+ Comparison routine used in sorting. We compare LIDs and if those are
+ equal, use the offset.
+ Note: we assume an unassigned LID is 0 and this compares as a high value.
+
+ NOTE: Consider making static.
+*/
+int
+vcdinf_lid_t_cmp (vcdinfo_offset_t *a, vcdinfo_offset_t *b)
+{
+ if (a->lid && b->lid)
+ {
+ if (a->lid > b->lid) return +1;
+ if (a->lid < b->lid) return -1;
+ vcd_warn ("LID %d at offset %d has same nunber as LID of offset %d",
+ a->lid, a->offset, b->offset);
+ }
+ else if (a->lid) return -1;
+ else if (b->lid) return +1;
+
+ /* Failed to sort on LID, try offset now. */
+
+ if (a->offset > b->offset) return +1;
+ if (a->offset < b->offset) return -1;
+
+ /* LIDS and offsets are equal. */
+ return 0;
+}
+
+/* Get the LID from a given play-list descriptor.
+ VCDINFO_REJECTED_MASK is returned d on error or pld is NULL.
+*/
+lid_t
+vcdinf_pld_get_lid(const PsdPlayListDescriptor *pld)
+{
+ return (pld != NULL)
+ ? uint16_from_be (pld->lid) & VCDINFO_LID_MASK
+ : VCDINFO_REJECTED_MASK;
+}
+
+/**
+ \fn vcdinfo_pld_get_next_offset(const PsdPlayListDescriptor *pld);
+ \brief Get next offset for a given PSD selector descriptor.
+ \return VCDINFO_INVALID_OFFSET is returned on error or if pld has no "next"
+ entry or pld is NULL. Otherwise the LID offset is returned.
+ */
+uint16_t
+vcdinf_pld_get_next_offset(const PsdPlayListDescriptor *pld)
+{
+ if (NULL == pld) return VCDINFO_INVALID_OFFSET;
+ return uint16_from_be (pld->next_ofs);
+}
+
+/*!
+ Return number of items in LIDs. Return 0 if error or not found.
+*/
+int
+vcdinf_pld_get_noi (const PsdPlayListDescriptor *pld)
+{
+ if ( NULL == pld ) return 0;
+ return pld->noi;
+}
+
+/*!
+ Return the playlist item i in d.
+*/
+uint16_t
+vcdinf_pld_get_play_item(const PsdPlayListDescriptor *pld, unsigned int i)
+{
+ if (NULL==pld) return 0;
+ return uint16_from_be(pld->itemid[i]);
+}
+
+/**
+ \fn vcdinf_pld_get_prev_offset(const PsdPlayListDescriptor *pld);
+ \brief Get prev offset for a given PSD selector descriptor.
+ \return VCDINFO_INVALID_OFFSET is returned on error or if pld has no "prev"
+ entry or pld is NULL. Otherwise the LID offset is returned.
+ */
+uint16_t
+vcdinf_pld_get_prev_offset(const PsdPlayListDescriptor *pld)
+{
+ return (pld != NULL) ?
+ uint16_from_be (pld->prev_ofs) : VCDINFO_INVALID_OFFSET;
+}
+
+/**
+ \fn vcdinf_pld_get_return_offset(const PsdPlayListDescriptor *pld);
+ \brief Get return offset for a given PLD selector descriptor.
+ \return VCDINFO_INVALID_OFFSET is returned on error or if pld has no
+ "return" entry or pld is NULL. Otherwise the LID offset is returned.
+ */
+uint16_t
+vcdinf_pld_get_return_offset(const PsdPlayListDescriptor *pld)
+{
+ return (pld != NULL) ?
+ uint16_from_be (pld->return_ofs) : VCDINFO_INVALID_OFFSET;
+}
+
+/**
+ * \fn vcdinfo_psd_get_default_offset(const PsdSelectionListDescriptor *psd);
+ * \brief Get next offset for a given PSD selector descriptor.
+ * \return VCDINFO_INVALID_OFFSET is returned on error or if psd is
+ * NULL. Otherwise the LID offset is returned.
+ */
+uint16_t
+vcdinf_psd_get_default_offset(const PsdSelectionListDescriptor *psd)
+{
+ if (NULL == psd) return VCDINFO_INVALID_OFFSET;
+ return uint16_from_be (psd->default_ofs);
+}
+
+/*!
+ Get the item id for a given selection-list descriptor.
+ VCDINFO_REJECTED_MASK is returned on error or if psd is NULL.
+*/
+uint16_t
+vcdinf_psd_get_itemid(const PsdSelectionListDescriptor *psd)
+{
+ return (psd != NULL) ? uint16_from_be(psd->itemid) : VCDINFO_REJECTED_MASK;
+}
+
+/*!
+ Get the LID from a given selection-list descriptor.
+ VCDINFO_REJECTED_MASK is returned on error or psd is NULL.
+*/
+lid_t
+vcdinf_psd_get_lid(const PsdSelectionListDescriptor *psd)
+{
+ return (psd != NULL)
+ ? uint16_from_be (psd->lid) & VCDINFO_LID_MASK
+ : VCDINFO_REJECTED_MASK;
+}
+
+/*!
+ Get the LID rejected status for a given PSD selector descriptor.
+ true is also returned d is NULL.
+*/
+bool
+vcdinf_psd_get_lid_rejected(const PsdSelectionListDescriptor *psd)
+{
+ return (psd != NULL)
+ ? vcdinfo_is_rejected(uint16_from_be(psd->lid))
+ : true;
+}
+
+/**
+ * \fn vcdinf_psd_get_next_offset(const PsdSelectionListDescriptor *psd);
+ * \brief Get "next" offset for a given PSD selector descriptor.
+ * \return VCDINFO_INVALID_OFFSET is returned on error or if psd is
+ * NULL. Otherwise the LID offset is returned.
+ */
+uint16_t
+vcdinf_psd_get_next_offset(const PsdSelectionListDescriptor *psd)
+{
+ if (NULL == psd) return VCDINFO_INVALID_OFFSET;
+ return uint16_from_be (psd->next_ofs);
+}
+
+/**
+ * \fn vcdinf_psd_get_offset(const PsdSelectionListDescriptor *d,
+ * unsigned int entry_num);
+ * \brief Get offset entry_num for a given PSD selector descriptor.
+ * \return VCDINFO_INVALID_OFFSET is returned if d on error or d is
+ * NULL. Otherwise the LID offset is returned.
+ */
+uint16_t
+vcdinf_psd_get_offset(const PsdSelectionListDescriptor *psd,
+ unsigned int entry_num)
+{
+ return (psd != NULL && entry_num < vcdinf_get_num_selections(psd))
+ ? uint16_from_be (psd->ofs[entry_num]) : VCDINFO_INVALID_OFFSET;
+}
+
+/**
+ \fn vcdinf_psd_get_prev_offset(const PsdSelectionListDescriptor *psd);
+ \brief Get "prev" offset for a given PSD selector descriptor.
+ \return VCDINFO_INVALID_OFFSET is returned on error or if psd has no "prev"
+ entry or psd is NULL. Otherwise the LID offset is returned.
+ */
+uint16_t
+vcdinf_psd_get_prev_offset(const PsdSelectionListDescriptor *psd)
+{
+ return (psd != NULL) ?
+ uint16_from_be (psd->prev_ofs) : VCDINFO_INVALID_OFFSET;
+}
+
+/**
+ * \fn vcdinf_psd_get_return_offset(const PsdSelectionListDescriptor *psd);
+ * \brief Get return offset for a given PSD selector descriptor.
+ \return VCDINFO_INVALID_OFFSET is returned on error or if psd has no
+ "return" entry or psd is NULL. Otherwise the LID offset is returned.
+ */
+uint16_t
+vcdinf_psd_get_return_offset(const PsdSelectionListDescriptor *psd)
+{
+ return (psd != NULL) ?
+ uint16_from_be (psd->return_ofs) : VCDINFO_INVALID_OFFSET;
+}
+
+
+/*
+ * Local variables:
+ * c-file-style: "gnu"
+ * tab-width: 8
+ * indent-tabs-mode: nil
+ * End:
+ */
diff --git a/src/input/vcd/libvcd/info.c b/src/input/vcd/libvcd/info.c
new file mode 100644
index 000000000..6e13637c2
--- /dev/null
+++ b/src/input/vcd/libvcd/info.c
@@ -0,0 +1,1753 @@
+/*
+ $Id: info.c,v 1.1 2003/10/13 11:47:12 f1rmb Exp $
+
+ Copyright (C) 2002,2003 Rocky Bernstein <rocky@panix.com>
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Foundation
+ Software, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+*/
+/*
+ Things here refer to higher-level structures usually accessed via
+ vcdinfo_t. For lower-level access which generally use
+ structures other than vcdinfo_t, see inf.c
+*/
+
+
+/* Private headers */
+#include "info_private.h"
+#include "assert.h"
+#include "bytesex.h"
+#include "pbc.h"
+#include "util.h"
+#include "vcd_read.h"
+
+#ifdef HAVE_STDLIB_H
+#include <stdlib.h>
+#endif
+#ifdef HAVE_STRING_H
+#include <string.h>
+#endif
+#ifdef HAVE_SYS_TYPES_H
+#include <sys/types.h>
+#endif
+#ifdef HAVE_SYS_STAT_H
+#include <sys/stat.h>
+#endif
+#ifdef HAVE_UNISTD_H
+#include <unistd.h>
+#endif
+
+#include <cdio/cdio.h>
+#include <cdio/cd_types.h>
+#include <cdio/util.h>
+
+/* Eventually move above libvcd includes but having vcdinfo including. */
+#include <libvcd/info.h>
+
+#include <stdio.h>
+#include <stddef.h>
+#include <errno.h>
+
+static const char _rcsid[] = "$Id: info.c,v 1.1 2003/10/13 11:47:12 f1rmb Exp $";
+
+#define MIN_ENCODED_TRACK_NUM 100
+#define MIN_ENCODED_SEGMENT_NUM 1000
+#define MAX_ENCODED_SEGMENT_NUM 2979
+
+#define BUF_COUNT 16
+#define BUF_SIZE 80
+
+/* Return a pointer to a internal free buffer */
+static char *
+_getbuf (void)
+{
+ static char _buf[BUF_COUNT][BUF_SIZE];
+ static int _num = -1;
+
+ _num++;
+ _num %= BUF_COUNT;
+
+ memset (_buf[_num], 0, BUF_SIZE);
+
+ return _buf[_num];
+}
+
+/*
+ Initialize/allocate segment portion of vcdinfo_obj_t.
+
+ Getting exact segments sizes is done in a rather complicated way.
+ A simple approach would be to use the fixed size allocated on disk,
+ but players have trouble with the internal fragmentation padding.
+ More accurate results are obtained by consulting with ISO 9660
+ information for the corresponding file entry.
+
+ Another approach to get segment sizes is to read/scan the
+ MPEGs. That would be rather slow.
+*/
+static void
+_init_segments (vcdinfo_obj_t *obj)
+{
+ InfoVcd *info = vcdinfo_get_infoVcd(obj);
+ segnum_t num_segments = vcdinfo_get_num_segments(obj);
+ int i;
+ iso9660_stat_t statbuf;
+
+ obj->first_segment_lsn = cdio_msf_to_lsn(&info->first_seg_addr);
+
+ obj->seg_sizes = _vcd_malloc( num_segments * sizeof(uint32_t *));
+ if (NULL == obj->seg_sizes) return;
+
+ /* Figure all of the segment sector sizes */
+ for (i=0; i < num_segments; i++) {
+
+ obj->seg_sizes[i] = VCDINFO_SEGMENT_SECTOR_SIZE;
+
+ if ( !info->spi_contents[i].item_cont ) {
+ /* Should have an entry in the ISO 9660 filesytem. Get and save
+ in statbuf.secsize this size.
+ */
+ lsn_t lsn = vcdinfo_get_seg_lsn(obj, i);
+ if (iso9660_find_fs_lsn(obj->img, lsn, &statbuf)) {
+ obj->seg_sizes[i] = statbuf.secsize;
+ } else {
+ vcd_warn ("Trouble finding ISO 9660 size for segment %d.", i);
+ }
+ }
+ }
+}
+
+/*!
+ Return the number of audio channels implied by "audio_type".
+ 0 is returned on error.
+*/
+unsigned int
+vcdinfo_audio_type_num_channels(const vcdinfo_obj_t *obj,
+ unsigned int audio_type)
+{
+ const int audio_types[2][5] =
+ {
+ { /* VCD 2.0 */
+ 0, /* no audio*/
+ 1, /* single channel */
+ 1, /* stereo */
+ 2, /* dual channel */
+ 0}, /* error */
+
+ { /* SVCD, HQVCD */
+ 0, /* no stream */
+ 1, /* 1 stream */
+ 2, /* 2 streams */
+ 1, /* 1 multi-channel stream (surround sound) */
+ 0} /* error */
+ };
+
+ /* We should also check that the second index is in range too. */
+ if (audio_type > 4) {
+ return 0;
+ }
+
+ /* Get first index entry into above audio_type array from vcd_type */
+ switch (obj->vcd_type) {
+
+ case VCD_TYPE_VCD:
+ case VCD_TYPE_VCD11:
+ return 1;
+
+ case VCD_TYPE_VCD2:
+ return 3;
+ break;
+
+ case VCD_TYPE_HQVCD:
+ case VCD_TYPE_SVCD:
+ return audio_types[1][audio_type];
+ break;
+
+ case VCD_TYPE_INVALID:
+ default:
+ /* We have an invalid entry. Set to handle below. */
+ return 0;
+ }
+}
+
+/*!
+ Return a string describing an audio type.
+*/
+const char *
+vcdinfo_audio_type2str(const vcdinfo_obj_t *obj, unsigned int audio_type)
+{
+ const char *audio_types[3][5] =
+ {
+ /* INVALID, VCD 1.0, or VCD 1.1 */
+ { "unknown", "invalid", "", "", "" },
+
+ /*VCD 2.0 */
+ { "no audio", "single channel", "stereo", "dual channel", "error" },
+
+ /* SVCD, HQVCD */
+ { "no stream", "1 stream", "2 streams",
+ "1 multi-channel stream (surround sound)", "error"},
+ };
+
+ unsigned int first_index = 0;
+
+ /* Get first index entry into above audio_type array from vcd_type */
+ switch (obj->vcd_type) {
+
+ case VCD_TYPE_VCD:
+ case VCD_TYPE_VCD11:
+ case VCD_TYPE_VCD2:
+ first_index=1;
+ break;
+
+ case VCD_TYPE_HQVCD:
+ case VCD_TYPE_SVCD:
+ first_index=2;
+ break;
+
+ case VCD_TYPE_INVALID:
+ default:
+ /* We have an invalid entry. Set to handle below. */
+ audio_type=4;
+ }
+
+ /* We should also check that the second index is in range too. */
+ if (audio_type > 3) {
+ first_index=0;
+ audio_type=1;
+ }
+
+ return audio_types[first_index][audio_type];
+}
+
+/*!
+ Note first seg_num is 0!
+*/
+const char *
+vcdinfo_ogt2str(const vcdinfo_obj_t *obj, segnum_t seg_num)
+{
+ const InfoVcd *info = &obj->info;
+ const char *ogt_str[] =
+ {
+ "None",
+ "1 available",
+ "0 & 1 available",
+ "all 4 available"
+ };
+
+ return ogt_str[info->spi_contents[seg_num].ogt];
+}
+
+
+const char *
+vcdinfo_video_type2str(const vcdinfo_obj_t *obj, segnum_t seg_num)
+{
+ const char *video_types[] =
+ {
+ "no stream",
+ "NTSC still",
+ "NTSC still (lo+hires)",
+ "NTSC motion",
+ "reserved (0x4)",
+ "PAL still",
+ "PAL still (lo+hires)",
+ "PAL motion"
+ "INVALID ENTRY"
+ };
+
+ return video_types[vcdinfo_get_video_type(obj, seg_num)];
+}
+
+/*!
+ \brief Classify itemid_num into the kind of item it is: track #, entry #,
+ segment #.
+ \param itemid is set to contain this classification an the converted
+ entry number.
+*/
+void
+vcdinfo_classify_itemid (uint16_t itemid_num,
+ /*out*/ vcdinfo_itemid_t *itemid)
+{
+
+ itemid->num = itemid_num;
+ if (itemid_num < 2)
+ itemid->type = VCDINFO_ITEM_TYPE_NOTFOUND;
+ else if (itemid_num < MIN_ENCODED_TRACK_NUM) {
+ itemid->type = VCDINFO_ITEM_TYPE_TRACK;
+ itemid->num--;
+ } else if (itemid_num < 600) {
+ itemid->type = VCDINFO_ITEM_TYPE_ENTRY;
+ itemid->num -= MIN_ENCODED_TRACK_NUM;
+ } else if (itemid_num < MIN_ENCODED_SEGMENT_NUM)
+ itemid->type = VCDINFO_ITEM_TYPE_LID;
+ else if (itemid_num <= MAX_ENCODED_SEGMENT_NUM) {
+ itemid->type = VCDINFO_ITEM_TYPE_SEGMENT;
+ itemid->num -= (MIN_ENCODED_SEGMENT_NUM);
+ } else
+ itemid->type = VCDINFO_ITEM_TYPE_SPAREID2;
+}
+
+const char *
+vcdinfo_pin2str (uint16_t itemid_num)
+{
+ char *buf = _getbuf ();
+ vcdinfo_itemid_t itemid;
+
+ vcdinfo_classify_itemid(itemid_num, &itemid);
+ strcpy (buf, "??");
+
+ switch(itemid.type) {
+ case VCDINFO_ITEM_TYPE_NOTFOUND:
+ snprintf (buf, BUF_SIZE, "play nothing (0x%4.4x)", itemid.num);
+ break;
+ case VCDINFO_ITEM_TYPE_TRACK:
+ snprintf (buf, BUF_SIZE, "SEQUENCE[%d] (0x%4.4x)", itemid.num,
+ itemid_num);
+ break;
+ case VCDINFO_ITEM_TYPE_ENTRY:
+ snprintf (buf, BUF_SIZE, "ENTRY[%d] (0x%4.4x)", itemid.num, itemid_num);
+ break;
+ case VCDINFO_ITEM_TYPE_SEGMENT:
+ snprintf (buf, BUF_SIZE, "SEGMENT[%d] (0x%4.4x)", itemid.num, itemid_num);
+ break;
+ case VCDINFO_ITEM_TYPE_LID:
+ snprintf (buf, BUF_SIZE, "spare id (0x%4.4x)", itemid.num);
+ break;
+ case VCDINFO_ITEM_TYPE_SPAREID2:
+ snprintf (buf, BUF_SIZE, "spare id2 (0x%4.4x)", itemid.num);
+ break;
+ }
+
+ return buf;
+}
+
+/*!
+ Return a string containing the VCD album id, or NULL if there is
+ some problem in getting this.
+*/
+const char *
+vcdinfo_get_album_id(const vcdinfo_obj_t *obj)
+{
+ if ( NULL == obj ) return (NULL);
+ return vcdinf_get_album_id(&obj->info);
+}
+
+/*!
+ Return the VCD ID.
+ NULL is returned if there is some problem in getting this.
+*/
+const char *
+vcdinfo_get_application_id(const vcdinfo_obj_t *obj)
+{
+ if ( NULL == obj ) return (NULL);
+ return(vcdinf_get_application_id(&obj->pvd));
+}
+
+/*!
+ Return a pointer to the cdio structure for the CD image opened or
+ NULL if error.
+*/
+CdIo *
+vcdinfo_get_cd_image (const vcdinfo_obj_t *vcd_obj)
+{
+ if (NULL == vcd_obj) return NULL;
+ return vcd_obj->img;
+}
+
+
+/**
+ \fn vcdinfo_get_default_offset(const vcdinfo_obj_t *obj, unsinged int lid);
+ \brief Get return offset for a given PLD selector descriptor.
+ \return VCDINFO_INVALID_OFFSET is returned on error or if pld has no
+ "return" entry or pld is NULL. Otherwise the LID offset is returned.
+ */
+uint16_t
+vcdinfo_get_default_offset(const vcdinfo_obj_t *obj, unsigned int lid)
+{
+ if (NULL != obj) {
+
+ PsdListDescriptor pxd;
+
+ vcdinfo_lid_get_pxd(obj, &pxd, lid);
+
+ switch (pxd.descriptor_type) {
+ case PSD_TYPE_EXT_SELECTION_LIST:
+ case PSD_TYPE_SELECTION_LIST:
+ return vcdinf_psd_get_default_offset(pxd.psd);
+ break;
+ case PSD_TYPE_PLAY_LIST:
+ case PSD_TYPE_END_LIST:
+ case PSD_TYPE_COMMAND_LIST:
+ break;
+ }
+ }
+ return VCDINFO_INVALID_OFFSET;
+}
+
+/*!
+ Return a string containing the default VCD device if none is specified.
+ Return NULL we can't get this information.
+*/
+char *
+vcdinfo_get_default_device (const vcdinfo_obj_t *vcd_obj)
+{
+
+ /* If device not already open, then we'll open it temporarily and
+ let CdIo select a driver, get the default for that and then
+ close/destroy the temporary we created.
+ */
+ CdIo *cdio=NULL;
+ if (vcd_obj != NULL && vcd_obj->img != NULL)
+ cdio = vcd_obj->img;
+
+ return cdio_get_default_device(cdio);
+}
+
+/*!
+ Return number of sector units in of an entry. 0 is returned if entry_num
+ is out of range.
+ The first entry number is 0.
+*/
+uint32_t
+vcdinfo_get_entry_sect_count (const vcdinfo_obj_t *obj, unsigned int entry_num)
+{
+ const EntriesVcd *entries = &obj->entries;
+ const unsigned int entry_count = vcdinf_get_num_entries(entries);
+ if (entry_num > entry_count)
+ return 0;
+ else {
+ const lsn_t this_lsn = vcdinfo_get_entry_lsn(obj, entry_num);
+ lsn_t next_lsn;
+ if (entry_num < entry_count-1) {
+ track_t track=vcdinfo_get_track(obj, entry_num);
+ track_t next_track=vcdinfo_get_track(obj, entry_num+1);
+ next_lsn = vcdinfo_get_entry_lsn(obj, entry_num+1);
+ /* If we've changed tracks, don't include pregap sector between
+ tracks.
+ */
+ if (track != next_track) next_lsn -= CDIO_PREGAP_SECTORS;
+ } else {
+ /* entry_num == entry_count -1. Or the last entry.
+ This is really really ugly. There's probably a better
+ way to do it.
+ Below we get the track of the current entry and then the LBA of the
+ beginning of the following (leadout?) track.
+
+ Wait! It's uglier than that! Since VCD's can be created
+ *without* a pregap to the leadout track, we try not to use
+ that if we can get the entry from the ISO 9660 filesystem.
+ */
+ track_t track = vcdinfo_get_track(obj, entry_num);
+ if (track != VCDINFO_INVALID_TRACK) {
+ iso9660_stat_t statbuf;
+ const lsn_t lsn = vcdinfo_get_track_lsn(obj, track);
+
+ /* Try to get the sector count from the ISO 9660 filesystem */
+ if (iso9660_find_fs_lsn(obj->img, lsn, &statbuf)) {
+ next_lsn = lsn + statbuf.secsize;
+ } else {
+ /* Failed on ISO 9660 filesystem. Use next track or
+ LEADOUT track. */
+ next_lsn = vcdinfo_get_track_lsn(obj, track+1);
+ }
+ if (next_lsn == VCDINFO_NULL_LSN)
+ return 0;
+ } else {
+ /* Something went wrong. Set up size to zero. */
+ return 0;
+ }
+ }
+ return (next_lsn - this_lsn);
+ }
+}
+
+/*! Return the starting MSF (minutes/secs/frames) for sequence
+ entry_num in obj. NULL is returned if there is no entry.
+ The first entry number is 0.
+*/
+const msf_t *
+vcdinfo_get_entry_msf(const vcdinfo_obj_t *obj, unsigned int entry_num)
+{
+ const EntriesVcd *entries = &obj->entries;
+ return vcdinf_get_entry_msf(entries, entry_num);
+}
+
+/*! Return the starting LBA (logical block address) for sequence
+ entry_num in obj. VCDINFO_NULL_LBA is returned if there is no entry.
+*/
+lba_t
+vcdinfo_get_entry_lba(const vcdinfo_obj_t *obj, unsigned int entry_num)
+{
+ if ( NULL == obj ) return VCDINFO_NULL_LBA;
+ else {
+ const msf_t *msf = vcdinfo_get_entry_msf(obj, entry_num);
+ msf = vcdinfo_get_entry_msf(obj, entry_num);
+ return (msf != NULL) ? cdio_msf_to_lba(msf) : VCDINFO_NULL_LBA;
+ }
+}
+
+/*! Return the starting LBA (logical block address) for sequence
+ entry_num in obj. VCDINFO_NULL_LBA is returned if there is no entry.
+*/
+lsn_t
+vcdinfo_get_entry_lsn(const vcdinfo_obj_t *obj, unsigned int entry_num)
+{
+ if ( NULL == obj ) return VCDINFO_NULL_LBA;
+ else {
+ const msf_t *msf = vcdinfo_get_entry_msf(obj, entry_num);
+ return (msf != NULL) ? cdio_msf_to_lsn(msf) : VCDINFO_NULL_LSN;
+ }
+}
+
+/*!
+ Get the VCD format (VCD 1.0 VCD 1.1, SVCD, ... for this object.
+ The type is also set inside obj.
+*/
+vcd_type_t
+vcdinfo_get_format_version (vcdinfo_obj_t *obj)
+{
+ return obj->vcd_type;
+}
+
+/*!
+ Return a string giving VCD format (VCD 1.0 VCD 1.1, SVCD, ...
+ for this object.
+*/
+const char *
+vcdinfo_get_format_version_str (const vcdinfo_obj_t *obj)
+{
+ if (NULL == obj) return "*Uninitialized*";
+ return vcdinf_get_format_version_str(obj->vcd_type);
+}
+
+EntriesVcd *
+vcdinfo_get_entriesVcd (vcdinfo_obj_t *obj)
+{
+ if (NULL == obj) return NULL;
+ return &obj->entries;
+}
+
+InfoVcd *
+vcdinfo_get_infoVcd (vcdinfo_obj_t *obj)
+{
+ if (NULL == obj) return NULL;
+ return &obj->info;
+}
+
+void *
+vcdinfo_get_tracksSVD (vcdinfo_obj_t *obj)
+{
+ if (NULL == obj) return NULL;
+ return obj->tracks_buf;
+}
+
+void *
+vcdinfo_get_scandata (vcdinfo_obj_t *obj)
+{
+ if (NULL == obj) return NULL;
+ return obj->scandata_buf;
+}
+
+void *
+vcdinfo_get_searchDat (vcdinfo_obj_t *obj)
+{
+ if (NULL == obj) return NULL;
+ return obj->search_buf;
+}
+
+void *
+vcdinfo_get_pvd (vcdinfo_obj_t *obj)
+{
+ if (NULL == obj) return NULL;
+ return &obj->pvd;
+}
+
+/*!
+ Get the itemid for a given list ID.
+ VCDINFO_REJECTED_MASK is returned on error or if obj is NULL.
+*/
+uint16_t
+vcdinfo_lid_get_itemid(const vcdinfo_obj_t *obj, lid_t lid)
+{
+ PsdListDescriptor pxd;
+
+ if (obj == NULL) return VCDINFO_REJECTED_MASK;
+ vcdinfo_lid_get_pxd(obj, &pxd, lid);
+ switch (pxd.descriptor_type) {
+ case PSD_TYPE_SELECTION_LIST:
+ case PSD_TYPE_EXT_SELECTION_LIST:
+ if (pxd.psd == NULL) return VCDINFO_REJECTED_MASK;
+ return vcdinf_psd_get_itemid(pxd.psd);
+ break;
+ case PSD_TYPE_PLAY_LIST:
+ /* FIXME: There is an array of items */
+ case PSD_TYPE_END_LIST:
+ case PSD_TYPE_COMMAND_LIST:
+ return VCDINFO_REJECTED_MASK;
+ }
+
+ return VCDINFO_REJECTED_MASK;
+
+}
+
+/*!
+ Get the LOT pointer.
+*/
+LotVcd *
+vcdinfo_get_lot(const vcdinfo_obj_t *obj)
+{
+ if (NULL == obj) return NULL;
+ return obj->lot;
+}
+
+/*!
+ Get the extended LOT pointer.
+*/
+LotVcd *
+vcdinfo_get_lot_x(const vcdinfo_obj_t *obj)
+{
+ if (NULL == obj) return NULL;
+ return obj->lot_x;
+}
+
+/*!
+ Return number of LIDs.
+*/
+lid_t
+vcdinfo_get_num_LIDs (const vcdinfo_obj_t *obj)
+{
+ /* Should probably use _vcd_pbc_max_lid instead? */
+ if (NULL==obj) return 0;
+ return vcdinf_get_num_LIDs(&obj->info);
+}
+
+/*!
+ Return the number of entries in the VCD.
+*/
+unsigned int
+vcdinfo_get_num_entries(const vcdinfo_obj_t *obj)
+{
+ const EntriesVcd *entries = &obj->entries;
+ return vcdinf_get_num_entries(entries);
+}
+
+/*!
+ Return the number of segments in the VCD. Return 0 if there is some
+ problem.
+*/
+segnum_t
+vcdinfo_get_num_segments(const vcdinfo_obj_t *obj)
+{
+ if (NULL==obj) return 0;
+ return vcdinf_get_num_segments(&obj->info);
+}
+
+/*!
+ \fn vcdinfo_get_offset_from_lid(const vcdinfo_obj_t *obj, unsigned int entry_num);
+ \brief Get offset entry_num for a given LID.
+ \return VCDINFO_INVALID_OFFSET is returned if obj on error or obj
+ is NULL. Otherwise the LID offset is returned.
+*/
+uint16_t
+vcdinfo_lid_get_offset(const vcdinfo_obj_t *obj, lid_t lid,
+ unsigned int entry_num)
+{
+ PsdListDescriptor pxd;
+
+ if (obj == NULL) return VCDINFO_INVALID_OFFSET;
+ vcdinfo_lid_get_pxd(obj, &pxd, lid);
+
+ switch (pxd.descriptor_type) {
+ case PSD_TYPE_SELECTION_LIST:
+ case PSD_TYPE_EXT_SELECTION_LIST:
+ if (pxd.psd == NULL) return VCDINFO_INVALID_OFFSET;
+ return vcdinf_psd_get_offset(pxd.psd, entry_num-1);
+ break;
+ case PSD_TYPE_PLAY_LIST:
+ /* FIXME: There is an array of items */
+ case PSD_TYPE_END_LIST:
+ case PSD_TYPE_COMMAND_LIST:
+ return VCDINFO_INVALID_OFFSET;
+ }
+ return VCDINFO_INVALID_OFFSET;
+
+}
+
+/*!
+ NULL is returned on error.
+*/
+static vcdinfo_offset_t *
+_vcdinfo_get_offset_t (const vcdinfo_obj_t *obj, unsigned int offset, bool ext)
+{
+ VcdListNode *node;
+ VcdList *offset_list = ext ? obj->offset_x_list : obj->offset_list;
+
+ switch (offset) {
+ case PSD_OFS_DISABLED:
+ case PSD_OFS_MULTI_DEF:
+ case PSD_OFS_MULTI_DEF_NO_NUM:
+ return NULL;
+ default: ;
+ }
+
+ _VCD_LIST_FOREACH (node, offset_list)
+ {
+ vcdinfo_offset_t *ofs = _vcd_list_node_data (node);
+ if (offset == ofs->offset)
+ return ofs;
+ }
+ return NULL;
+}
+
+/*!
+ Get the VCD info list.
+*/
+VcdList *
+vcdinfo_get_offset_list(const vcdinfo_obj_t *obj)
+{
+ if (NULL == obj) return NULL;
+ return obj->offset_list;
+}
+
+
+/*!
+ Get the VCD info extended offset list.
+*/
+VcdList *
+vcdinfo_get_offset_x_list(const vcdinfo_obj_t *obj)
+{
+ if (NULL == obj) return NULL;
+ return obj->offset_x_list;
+}
+
+/*!
+ Get the VCD info offset multiplier.
+*/
+unsigned int vcdinfo_get_offset_mult(const vcdinfo_obj_t *obj)
+{
+ if (NULL == obj) return 0xFFFF;
+ return obj->info.offset_mult;
+}
+
+/*!
+ Get entry in offset list for the item that has offset. This entry
+ has for example the LID. NULL is returned on error.
+*/
+vcdinfo_offset_t *
+vcdinfo_get_offset_t (const vcdinfo_obj_t *obj, unsigned int offset)
+{
+ vcdinfo_offset_t *off_p= _vcdinfo_get_offset_t (obj, offset, true);
+ if (NULL != off_p)
+ return off_p;
+ return _vcdinfo_get_offset_t (obj, offset, false);
+}
+
+/*!
+ Return a string containing the VCD publisher id with trailing
+ blanks removed, or NULL if there is some problem in getting this.
+*/
+const char *
+vcdinfo_get_preparer_id(const vcdinfo_obj_t *obj)
+{
+ if ( NULL == obj ) return (NULL);
+ return vcdinf_get_preparer_id(&obj->pvd);
+}
+
+/*!
+ Get the PSD.
+*/
+uint8_t *
+vcdinfo_get_psd(const vcdinfo_obj_t *obj)
+{
+ if ( NULL == obj ) return (NULL);
+ return obj->psd;
+}
+
+/*!
+ Get the extended PSD.
+*/
+uint8_t *
+vcdinfo_get_psd_x(const vcdinfo_obj_t *obj)
+{
+ if ( NULL == obj ) return (NULL);
+ return obj->psd_x;
+}
+
+/*!
+ Return number of bytes in PSD. Return 0 if there's an error.
+*/
+uint32_t
+vcdinfo_get_psd_size (const vcdinfo_obj_t *obj)
+{
+ if ( NULL == obj ) return 0;
+ return vcdinf_get_psd_size(&obj->info);
+}
+
+/*!
+ Return number of bytes in the extended PSD. Return 0 if there's an error.
+*/
+uint32_t
+vcdinfo_get_psd_x_size (const vcdinfo_obj_t *obj)
+{
+ if ( NULL == obj ) return 0;
+ return obj->psd_x_size;
+}
+
+/*!
+ Return a string containing the VCD publisher id with trailing
+ blanks removed, or NULL if there is some problem in getting this.
+*/
+const char *
+vcdinfo_get_publisher_id(const vcdinfo_obj_t *obj)
+{
+ if ( NULL == obj ) return (NULL);
+ return vcdinf_get_publisher_id(&obj->pvd);
+}
+
+/*!
+ Get the PSD Selection List Descriptor for a given lid.
+ NULL is returned if error or not found.
+*/
+static bool
+_vcdinfo_lid_get_pxd(const vcdinfo_obj_t *obj, PsdListDescriptor *pxd,
+ uint16_t lid, bool ext)
+{
+ VcdListNode *node;
+ unsigned mult = obj->info.offset_mult;
+ const uint8_t *psd = ext ? obj->psd_x : obj->psd;
+ VcdList *offset_list = ext ? obj->offset_x_list : obj->offset_list;
+
+ if (offset_list == NULL) return false;
+
+ _VCD_LIST_FOREACH (node, offset_list)
+ {
+ vcdinfo_offset_t *ofs = _vcd_list_node_data (node);
+ unsigned _rofs = ofs->offset * mult;
+
+ pxd->descriptor_type = psd[_rofs];
+
+ switch (pxd->descriptor_type)
+ {
+ case PSD_TYPE_PLAY_LIST:
+ {
+ pxd->pld = (PsdPlayListDescriptor *) (psd + _rofs);
+ if (vcdinf_pld_get_lid(pxd->pld) == lid) {
+ return true;
+ }
+ break;
+ }
+
+ case PSD_TYPE_EXT_SELECTION_LIST:
+ case PSD_TYPE_SELECTION_LIST:
+ {
+ pxd->psd = (PsdSelectionListDescriptor *) (psd + _rofs);
+ if (vcdinf_psd_get_lid(pxd->psd) == lid) {
+ return true;
+ }
+ break;
+ }
+ default: ;
+ }
+ }
+ return false;
+}
+
+/*!
+ Get the PSD Selection List Descriptor for a given lid.
+ False is returned if not found.
+*/
+bool
+vcdinfo_lid_get_pxd(const vcdinfo_obj_t *obj, PsdListDescriptor *pxd,
+ uint16_t lid)
+{
+ if (_vcdinfo_lid_get_pxd(obj, pxd, lid, true))
+ return true;
+ return _vcdinfo_lid_get_pxd(obj, pxd, lid, false);
+}
+
+/**
+ \fn vcdinfo_get_return_offset(const vcdinfo_obj_t *obj);
+ \brief Get return offset for a given LID.
+ \return VCDINFO_INVALID_OFFSET is returned on error or if LID has no
+ "return" entry. Otherwise the LID offset is returned.
+ */
+uint16_t
+vcdinfo_get_return_offset(const vcdinfo_obj_t *obj, lid_t lid)
+{
+ if (NULL != obj) {
+
+ PsdListDescriptor pxd;
+
+ vcdinfo_lid_get_pxd(obj, &pxd, lid);
+
+ switch (pxd.descriptor_type) {
+ case PSD_TYPE_PLAY_LIST:
+ return vcdinf_pld_get_return_offset(pxd.pld);
+ case PSD_TYPE_SELECTION_LIST:
+ case PSD_TYPE_EXT_SELECTION_LIST:
+ return vcdinf_psd_get_return_offset(pxd.psd);
+ break;
+ case PSD_TYPE_END_LIST:
+ case PSD_TYPE_COMMAND_LIST:
+ break;
+ }
+ }
+
+ return VCDINFO_INVALID_OFFSET;
+}
+
+/*!
+ Return the audio type for a given segment.
+ VCDINFO_INVALID_AUDIO_TYPE is returned on error.
+*/
+unsigned int
+vcdinfo_get_seg_audio_type(const vcdinfo_obj_t *obj, segnum_t seg_num)
+{
+ if ( NULL == obj || NULL == &obj->info
+ || seg_num >= vcdinfo_get_num_segments(obj) )
+ return VCDINFO_INVALID_AUDIO_TYPE;
+ return(obj->info.spi_contents[seg_num].audio_type);
+}
+
+/*!
+ Return true if this segment is supposed to continue to the next one,
+ (is part of an "item" or listing in the ISO 9660 filesystem).
+*/
+bool
+vcdinfo_get_seg_continue(const vcdinfo_obj_t *obj, segnum_t seg_num)
+{
+ if ( NULL == obj || NULL == &obj->info
+ || seg_num >= vcdinfo_get_num_segments(obj) )
+ return false;
+ return(obj->info.spi_contents[seg_num].item_cont);
+}
+
+/*! Return the starting LBA (logical block address) for segment
+ entry_num in obj. VCDINFO_LBA_NULL is returned if there is no entry.
+
+ Note first seg_num is 0.
+*/
+lba_t
+vcdinfo_get_seg_lba(const vcdinfo_obj_t *obj, segnum_t seg_num)
+{
+ if (obj == NULL) return VCDINFO_NULL_LBA;
+ return cdio_lsn_to_lba(vcdinfo_get_seg_lba(obj, seg_num));
+}
+
+/*! Return the starting LBA (logical block address) for segment
+ entry_num in obj. VCDINFO_LSN_NULL is returned if there is no entry.
+
+ Note first seg_num is 0.
+*/
+lsn_t
+vcdinfo_get_seg_lsn(const vcdinfo_obj_t *obj, segnum_t seg_num)
+{
+ if (obj == NULL || seg_num >= vcdinfo_get_num_segments(obj))
+ return VCDINFO_NULL_LSN;
+ return obj->first_segment_lsn + (VCDINFO_SEGMENT_SECTOR_SIZE * seg_num);
+}
+
+/*! Return the starting MSF (minutes/secs/frames) for segment
+ entry_num in obj. NULL is returned if there is no entry.
+
+ Note first seg_num is 0!
+*/
+const msf_t *
+vcdinfo_get_seg_msf(const vcdinfo_obj_t *obj, segnum_t seg_num)
+{
+ if (obj == NULL || seg_num >= vcdinfo_get_num_segments(obj))
+ return NULL;
+ else {
+ lsn_t lsn = vcdinfo_get_seg_lsn(obj, seg_num);
+ static msf_t msf;
+ cdio_lsn_to_msf(lsn, &msf);
+ return &msf;
+ }
+}
+
+/*!
+ Return the number of sectors for segment
+ entry_num in obj. 0 is returned if there is no entry.
+
+ Use this routine to figure out the actual number of bytes a physical
+ region of a disk or CD takes up for a segment.
+
+ If an item has been broken up into a number of "continued" segments,
+ we will report the item size for the first segment and 0 for the
+ remaining ones. We may revisit this decision later.
+*/
+uint32_t
+vcdinfo_get_seg_sector_count(const vcdinfo_obj_t *obj, segnum_t seg_num)
+{
+ if (obj == NULL || seg_num >= vcdinfo_get_num_segments(obj))
+ return 0;
+ return obj->seg_sizes[seg_num];
+}
+
+/*!
+ Return a string containing the VCD system id with trailing
+ blanks removed, or NULL if there is some problem in getting this.
+*/
+const char *
+vcdinfo_get_system_id(const vcdinfo_obj_t *obj)
+{
+ if ( NULL == obj || NULL == &obj->pvd ) return (NULL);
+ return(vcdinf_get_system_id(&obj->pvd));
+}
+
+/*!
+ Return the track number for entry n in obj.
+
+ In contrast to libcdio we start numbering at 0 which is the
+ ISO9660 and metadata information for the Video CD. Thus track
+ 1 is the first track the first complete MPEG track generally.
+*/
+track_t
+vcdinfo_get_track(const vcdinfo_obj_t *obj, const unsigned int entry_num)
+{
+ const EntriesVcd *entries = &obj->entries;
+ const unsigned int entry_count = vcdinf_get_num_entries(entries);
+ /* Note entry_num is 0 origin. */
+ return entry_num < entry_count ?
+ vcdinf_get_track(entries, entry_num)-1: VCDINFO_INVALID_TRACK;
+}
+
+/*!
+ Return the audio type for a given track.
+ VCDINFO_INVALID_AUDIO_TYPE is returned on error.
+
+ Note: track 1 is usually the first track.
+*/
+unsigned int
+vcdinfo_get_track_audio_type(const vcdinfo_obj_t *obj, track_t track_num)
+{
+ TracksSVD *tracks;
+ TracksSVD2 *tracks2;
+ if ( NULL == obj || NULL == &obj->info ) return VCDINFO_INVALID_AUDIO_TYPE;
+ tracks = obj->tracks_buf;
+
+ if ( NULL == tracks ) return 0;
+ tracks2 = (TracksSVD2 *) &(tracks->playing_time[tracks->tracks]);
+ return(tracks2->contents[track_num-1].audio);
+}
+
+/*!
+ Return the highest track number in the current medium.
+
+ Because we track start numbering at 0 (which is the ISO 9660 track
+ containing Video CD naviagion and disk information), this is one
+ less than the number of tracks.
+
+ If there are no tracks, we return -1.
+*/
+unsigned int
+vcdinfo_get_num_tracks(const vcdinfo_obj_t *obj)
+{
+ if (obj == NULL || obj->img == NULL) return 0;
+
+ return cdio_get_num_tracks(obj->img)-1;
+}
+
+
+/*!
+ Return the starting LBA (logical block address) for track number
+ track_num in obj.
+
+ The IS0-9660 filesystem track has number 0. Tracks associated
+ with playable entries numbers start at 1.
+
+ The "leadout" track is specified either by
+ using track_num LEADOUT_TRACK or the total tracks+1.
+ VCDINFO_NULL_LBA is returned on failure.
+*/
+lba_t
+vcdinfo_get_track_lba(const vcdinfo_obj_t *obj, track_t track_num)
+{
+ if (NULL == obj || NULL == obj->img)
+ return VCDINFO_NULL_LBA;
+
+
+ /* CdIo tracks start at 1 rather than 0. */
+ return cdio_get_track_lba(obj->img, track_num+1);
+}
+
+/*!
+ Return the starting LSN (logical sector number) for track number
+ track_num in obj.
+
+ The IS0-9660 filesystem track has number 0. Tracks associated
+ with playable entries numbers start at 1.
+
+ The "leadout" track is specified either by
+ using track_num LEADOUT_TRACK or the total tracks+1.
+ VCDINFO_NULL_LBA is returned on failure.
+*/
+lsn_t
+vcdinfo_get_track_lsn(const vcdinfo_obj_t *obj, track_t track_num)
+{
+ if (NULL == obj || NULL == obj->img)
+ return VCDINFO_NULL_LSN;
+
+ /* CdIo tracks start at 1 rather than 0. */
+ return cdio_get_track_lsn(obj->img, track_num+1);
+}
+
+/*!
+ Return the starting MSF (minutes/secs/frames) for track number
+ track_num in obj.
+
+ The IS0-9660 filesystem track has number 0. Tracks associated
+ with playable entries numbers start at 1.
+
+ The "leadout" track is specified either by
+ using track_num LEADOUT_TRACK or the total tracks+1.
+ VCDINFO_NULL_LBA is returned on failure.
+*/
+int
+vcdinfo_get_track_msf(const vcdinfo_obj_t *obj, track_t track_num,
+ uint8_t *min, uint8_t *sec, uint8_t *frame)
+{
+ msf_t msf;
+
+ if (NULL == obj || NULL == obj->img)
+ return 1;
+
+ /* CdIo tracks start at 1 rather than 0. */
+ if (cdio_get_track_msf(obj->img, track_num+1, &msf)) {
+ *min = from_bcd8(msf.m);
+ *sec = from_bcd8(msf.s);
+ *frame = from_bcd8(msf.f);
+ return 0;
+ }
+
+ return 1;
+}
+
+/*!
+ Return the size in sectors for track n.
+
+ The IS0-9660 filesystem track has number 0. Tracks associated
+ with playable entries numbers start at 1.
+
+ FIXME: Whether we count the track pregap sectors is a bit haphazard.
+ We should add a parameter to indicate whether this is wanted or not.
+
+*/
+unsigned int
+vcdinfo_get_track_sect_count(const vcdinfo_obj_t *obj, const track_t track_num)
+{
+ if (NULL == obj || VCDINFO_INVALID_TRACK == track_num)
+ return 0;
+
+ {
+ iso9660_stat_t statbuf;
+ const lsn_t lsn = vcdinfo_get_track_lsn(obj, track_num);
+
+ /* Try to get the sector count from the ISO 9660 filesystem */
+ if (obj->has_xa && iso9660_find_fs_lsn(obj->img, lsn, &statbuf)) {
+ return statbuf.secsize;
+ } else {
+ const lsn_t next_lsn=vcdinfo_get_track_lsn(obj, track_num+1);
+ /* Failed on ISO 9660 filesystem. Use track information. */
+ return next_lsn > lsn ? next_lsn - lsn : 0;
+ }
+ }
+ return 0;
+}
+
+/*!
+ Return size in bytes for track number for entry n in obj.
+
+ The IS0-9660 filesystem track has number 1. Tracks associated
+ with playable entries numbers start at 2.
+
+ FIXME: Whether we count the track pregap sectors is a bit haphazard.
+ We should add a parameter to indicate whether this is wanted or not.
+*/
+unsigned int
+vcdinfo_get_track_size(const vcdinfo_obj_t *obj, track_t track_num)
+{
+ if (NULL == obj || VCDINFO_INVALID_TRACK == track_num)
+ return 0;
+
+ {
+ iso9660_stat_t statbuf;
+ const lsn_t lsn = cdio_lba_to_lsn(vcdinfo_get_track_lba(obj, track_num));
+
+ /* Try to get the sector count from the ISO 9660 filesystem */
+ if (obj->has_xa && iso9660_find_fs_lsn(obj->img, lsn, &statbuf)) {
+ return statbuf.size;
+ }
+#if 0
+ else {
+ /* Failed on ISO 9660 filesystem. Use track information. */
+ if (obj->img != NULL)
+ return cdio_get_track_size(obj->img);
+ }
+#endif
+ }
+ return 0;
+}
+
+/*!
+ \brief Get the kind of video stream segment of segment seg_num in obj.
+ \return VCDINFO_FILES_VIDEO_INVALID is returned if on error or obj is
+ null. Otherwise the enumeration type.
+
+ Note first seg_num is 0!
+*/
+vcdinfo_video_segment_type_t
+vcdinfo_get_video_type(const vcdinfo_obj_t *obj, segnum_t seg_num)
+{
+ const InfoVcd *info;
+ if (obj == NULL) return VCDINFO_FILES_VIDEO_INVALID;
+ info = &obj->info;
+ if (info == NULL) return VCDINFO_FILES_VIDEO_INVALID;
+ return info->spi_contents[seg_num].video_type;
+}
+
+/*!
+ \brief Get the kind of VCD that obj refers to.
+*/
+vcd_type_t
+vcdinfo_get_VCD_type(const vcdinfo_obj_t *obj)
+{
+ if (NULL == obj) return VCD_TYPE_INVALID;
+ return obj->vcd_type;
+}
+
+
+/*!
+ Return the VCD volume count - the number of CD's in the collection.
+ O is returned if there is some problem in getting this.
+*/
+unsigned int
+vcdinfo_get_volume_count(const vcdinfo_obj_t *obj)
+{
+ if ( NULL == obj ) return 0;
+ return vcdinf_get_volume_count(&obj->info);
+}
+
+/*!
+ Return the VCD ID.
+ NULL is returned if there is some problem in getting this.
+*/
+const char *
+vcdinfo_get_volume_id(const vcdinfo_obj_t *obj)
+{
+ if ( NULL == obj || NULL == &obj->pvd ) return (NULL);
+ return(vcdinf_get_volume_id(&obj->pvd));
+}
+
+/*!
+ Return the VCD volumeset ID.
+ NULL is returned if there is some problem in getting this.
+*/
+const char *
+vcdinfo_get_volumeset_id(const vcdinfo_obj_t *obj)
+{
+ if ( NULL == obj || NULL == &obj->pvd ) return (NULL);
+ return(vcdinfo_strip_trail(obj->pvd.volume_set_id, MAX_VOLUMESET_ID));
+}
+
+/*!
+ Return the VCD volume num - the number of the CD in the collection.
+ This is a number between 1 and the volume count.
+ O is returned if there is some problem in getting this.
+*/
+unsigned int
+vcdinfo_get_volume_num(const vcdinfo_obj_t *obj)
+{
+ if ( NULL == obj ) return 0;
+ return(uint16_from_be( obj->info.vol_id));
+}
+
+int
+vcdinfo_get_wait_time (uint16_t wtime)
+{
+ /* Note: this doesn't agree exactly with _wtime */
+ if (wtime < 61)
+ return wtime;
+ else if (wtime < 255)
+ return (wtime - 60) * 10 + 60;
+ else
+ return -1;
+}
+
+/*!
+ Return true is there is playback control.
+*/
+bool
+vcdinfo_has_pbc (const vcdinfo_obj_t *obj)
+{
+ return (obj && obj->info.psd_size!=0);
+}
+
+/*!
+ Return true if VCD has "extended attributes" (XA). Extended attributes
+ add meta-data attributes to a entries of file describing the file.
+ See also cdio_get_xa_attr_str() which returns a string similar to
+ a string you might get on a Unix filesystem listing ("ls").
+*/
+bool
+vcdinfo_has_xa(const vcdinfo_obj_t *obj)
+{
+ return obj->has_xa;
+}
+
+/*!
+ Add one to the MSF.
+*/
+void
+vcdinfo_inc_msf (uint8_t *min, uint8_t *sec, int8_t *frame)
+{
+ (*frame)++;
+ if (*frame>=CDIO_CD_FRAMES_PER_SEC) {
+ *frame = 0;
+ (*sec)++;
+ if (*sec>=CDIO_CD_SECS_PER_MIN) {
+ *sec = 0;
+ (*min)++;
+ }
+ }
+}
+
+/*!
+ Convert minutes, seconds and frame (MSF components) into a
+ logical block address (or LBA).
+ See also cdio_msf_to_lba which uses msf_t as its single parameter.
+*/
+lba_t
+vcdinfo_msf2lba (uint8_t min, uint8_t sec, int8_t frame)
+{
+ return CDIO_CD_FRAMES_PER_SEC*(CDIO_CD_SECS_PER_MIN*min + sec) + frame;
+}
+
+/*!
+ Convert minutes, seconds and frame (MSF components) into a
+ logical block address (or LBA).
+ See also cdio_msf_to_lba which uses msf_t as its single parameter.
+*/
+void
+vcdinfo_lba2msf (lba_t lba, uint8_t *min, uint8_t *sec, uint8_t *frame)
+{
+ *min = lba / (60*75);
+ lba %= (60*75);
+ *sec = lba / 75;
+ *frame = lba % 75;
+}
+
+/*!
+ Convert minutes, seconds and frame (MSF components) into a
+ logical sector number (or LSN).
+*/
+lsn_t
+vcdinfo_msf2lsn (uint8_t min, uint8_t sec, int8_t frame)
+{
+ lba_t lba=75*(60*min + sec) + frame;
+ if (lba < CDIO_PREGAP_SECTORS) {
+ vcd_error ("lba (%u) less than pregap sector (%u)",
+ lba, CDIO_PREGAP_SECTORS);
+ return lba;
+ }
+ return lba - CDIO_PREGAP_SECTORS;
+}
+
+const char *
+vcdinfo_ofs2str (const vcdinfo_obj_t *obj, unsigned int offset, bool ext)
+{
+ vcdinfo_offset_t *ofs;
+ char *buf;
+
+ switch (offset) {
+ case PSD_OFS_DISABLED:
+ return "disabled";
+ case PSD_OFS_MULTI_DEF:
+ return "multi_def";
+ case PSD_OFS_MULTI_DEF_NO_NUM:
+ return "multi_def_no_num";
+ default: ;
+ }
+
+ buf = _getbuf ();
+ ofs = _vcdinfo_get_offset_t(obj, offset, ext);
+ if (ofs != NULL) {
+ if (ofs->lid)
+ snprintf (buf, BUF_SIZE, "LID[%d] @0x%4.4x",
+ ofs->lid, ofs->offset);
+ else
+ snprintf (buf, BUF_SIZE, "PSD[?] @0x%4.4x",
+ ofs->offset);
+ } else {
+ snprintf (buf, BUF_SIZE, "? @0x%4.4x", offset);
+ }
+ return buf;
+}
+
+bool
+vcdinfo_read_psd (vcdinfo_obj_t *obj)
+{
+ unsigned psd_size = vcdinfo_get_psd_size (obj);
+
+ if (psd_size)
+ {
+ if (psd_size > 256*1024)
+ {
+ vcd_error ("weird psd size (%u) -- aborting", psd_size);
+ return false;
+ }
+
+ obj->lot = _vcd_malloc (ISO_BLOCKSIZE * LOT_VCD_SIZE);
+ obj->psd = _vcd_malloc (ISO_BLOCKSIZE * _vcd_len2blocks (psd_size,
+ ISO_BLOCKSIZE));
+
+ if (cdio_read_mode2_sectors (obj->img, (void *) obj->lot, LOT_VCD_SECTOR,
+ false, LOT_VCD_SIZE))
+ return false;
+
+ if (cdio_read_mode2_sectors (obj->img, (void *) obj->psd, PSD_VCD_SECTOR,
+ false, _vcd_len2blocks (psd_size,
+ ISO_BLOCKSIZE)))
+ return false;
+
+ } else {
+ return false;
+ }
+ return true;
+}
+
+void
+vcdinfo_visit_lot (vcdinfo_obj_t *obj, bool extended)
+{
+ struct _vcdinf_pbc_ctx pbc_ctx;
+
+ pbc_ctx.psd_size = vcdinfo_get_psd_size (obj);
+ pbc_ctx.psd_x_size = obj->psd_x_size;
+ pbc_ctx.offset_mult = 8;
+ pbc_ctx.maximum_lid = vcdinfo_get_num_LIDs(obj);
+ pbc_ctx.offset_x_list = NULL;
+ pbc_ctx.offset_list = NULL;
+ pbc_ctx.psd = obj->psd;
+ pbc_ctx.psd_x = obj->psd_x;
+ pbc_ctx.lot = obj->lot;
+ pbc_ctx.lot_x = obj->lot_x;
+ pbc_ctx.extended = extended;
+
+ vcdinf_visit_lot(&pbc_ctx);
+ if (NULL != obj->offset_x_list)
+ _vcd_list_free(obj->offset_x_list, true);
+ obj->offset_x_list = pbc_ctx.offset_x_list;
+ if (NULL != obj->offset_list)
+ _vcd_list_free(obj->offset_list, true);
+ obj->offset_list = pbc_ctx.offset_list;
+}
+
+/*!
+ Change trailing blanks in str to nulls. Str has a maximum size of
+ n characters.
+*/
+const char *
+vcdinfo_strip_trail (const char str[], size_t n)
+{
+ static char buf[1024];
+ int j;
+
+ vcd_assert (n < 1024);
+
+ strncpy (buf, str, n);
+ buf[n] = '\0';
+
+ for (j = strlen (buf) - 1; j >= 0; j--)
+ {
+ if (buf[j] != ' ')
+ break;
+
+ buf[j] = '\0';
+ }
+
+ return buf;
+}
+
+/*!
+ Return true if offset is "rejected". That is shouldn't be displayed
+ in a list of entries.
+*/
+bool
+vcdinfo_is_rejected(uint16_t offset)
+{
+ return (offset & VCDINFO_REJECTED_MASK) != 0;
+}
+
+/*!
+ Nulls/zeros vcdinfo_obj_t structures; The caller should have
+ ensured that obj != NULL.
+ routines using obj are called.
+*/
+static void
+_vcdinfo_zero(vcdinfo_obj_t *obj)
+{
+ memset(obj, 0, sizeof(vcdinfo_obj_t));
+ obj->vcd_type = VCD_TYPE_INVALID;
+ obj->img = NULL;
+ obj->lot = NULL;
+ obj->source_name = NULL;
+ obj->seg_sizes = NULL;
+}
+
+/*!
+ Initialize the vcdinfo structure "obj". Should be done before other
+ routines using obj are called.
+*/
+bool
+vcdinfo_init(vcdinfo_obj_t *obj)
+{
+ if (NULL == obj) return false;
+ _vcdinfo_zero(obj);
+ return cdio_init();
+}
+
+/*!
+ Set up vcdinfo structure "obj" for reading from a particular
+ medium. This should be done before after initialization but before
+ any routines that need to retrieve data.
+
+ source_name is the device or file to use for inspection, and
+ source_type indicates what driver to use or class of drivers in the
+ case of DRIVER_DEVICE.
+ access_mode gives the CD access method for reading should the driver
+ allow for more than one kind of access method (e.g. MMC versus ioctl
+ on GNU/Linux)
+
+ If source_name is NULL we'll fill in the appropriate default device
+ name for the given source_type. However if in addtion source_type is
+ DRIVER_UNKNOWN, then we'll scan for a drive containing a VCD.
+
+ VCDINFO_OPEN_VCD is returned if everything went okay;
+ VCDINFO_OPEN_ERROR if there was an error and VCDINFO_OPEN_OTHER if the
+ medium is something other than a VCD.
+ */
+vcdinfo_open_return_t
+vcdinfo_open(vcdinfo_obj_t **obj_p, char *source_name[],
+ driver_id_t source_type, const char access_mode[])
+{
+ CdIo *img;
+ iso9660_stat_t statbuf;
+ vcdinfo_obj_t *obj = _vcd_malloc(sizeof(vcdinfo_obj_t));
+
+
+ /* If we don't specify a driver_id or a source_name, scan the
+ system for a CD that contains a VCD.
+ */
+ if (NULL == *source_name && source_type == DRIVER_UNKNOWN) {
+ char **cd_drives=NULL;
+ cd_drives = cdio_get_devices_with_cap(NULL,
+ (CDIO_FS_ANAL_SVCD|CDIO_FS_ANAL_CVD|CDIO_FS_ANAL_VIDEOCD
+ |CDIO_FS_UNKNOWN),
+ true);
+ if ( NULL == cd_drives || NULL == cd_drives[0] ) {
+ return VCDINFO_OPEN_ERROR;
+ }
+ *source_name = strdup(cd_drives[0]);
+ cdio_free_device_list(cd_drives);
+ }
+
+ img = cdio_open(*source_name, source_type);
+ if (NULL == img) {
+ return VCDINFO_OPEN_ERROR;
+ }
+
+ *obj_p = obj;
+
+ if (access_mode != NULL)
+ cdio_set_arg (img, "access-mode", access_mode);
+
+ if (NULL == *source_name)
+ *source_name = cdio_get_default_device(img);
+
+ memset (obj, 0, sizeof (vcdinfo_obj_t));
+ obj->img = img; /* Note we do this after the above wipeout! */
+
+ if (!read_pvd(obj->img, &(obj->pvd))) {
+ return VCDINFO_OPEN_ERROR;
+ }
+
+ /* Determine if VCD has XA attributes. */
+ {
+
+ iso9660_pvd_t const *pvd = &obj->pvd;
+
+ obj->has_xa = !strncmp ((char *) pvd + ISO_XA_MARKER_OFFSET,
+ ISO_XA_MARKER_STRING,
+ strlen (ISO_XA_MARKER_STRING));
+ }
+
+ if (!read_info(obj->img, &(obj->info), &(obj->vcd_type)))
+ return VCDINFO_OPEN_OTHER;
+
+ if (vcdinfo_get_format_version (obj) == VCD_TYPE_INVALID)
+ return VCDINFO_OPEN_OTHER;
+
+ if (!read_entries(obj->img, &(obj->entries)))
+ return VCDINFO_OPEN_OTHER;
+
+ {
+ size_t len = strlen(*source_name)+1;
+ obj->source_name = (char *) malloc(len * sizeof(char));
+ strncpy(obj->source_name, *source_name, len);
+ }
+
+ if (obj->vcd_type == VCD_TYPE_SVCD || obj->vcd_type == VCD_TYPE_HQVCD) {
+ if (!iso9660_fs_stat (obj->img, "MPEGAV", &statbuf, true))
+ vcd_warn ("non compliant /MPEGAV folder detected!");
+
+ if (!iso9660_fs_stat (obj->img, "SVCD/TRACKS.SVD;1", &statbuf, true))
+ {
+ if (statbuf.size != ISO_BLOCKSIZE)
+ vcd_warn ("TRACKS.SVD filesize != %d!", ISO_BLOCKSIZE);
+
+ obj->tracks_buf = _vcd_malloc (ISO_BLOCKSIZE);
+
+ if (cdio_read_mode2_sector (obj->img, obj->tracks_buf, statbuf.lsn,
+ false))
+ return VCDINFO_OPEN_ERROR;
+ }
+ }
+
+ _init_segments (obj);
+
+ switch (obj->vcd_type) {
+ case VCD_TYPE_VCD2: {
+ if (!iso9660_fs_stat (img, "EXT/PSD_X.VCD;1", &statbuf, true)) {
+ vcd_debug ("found /EXT/PSD_X.VCD at sector %lu",
+ (long unsigned int) statbuf.lsn);
+
+ obj->psd_x = _vcd_malloc (ISO_BLOCKSIZE * statbuf.secsize);
+ obj->psd_x_size = statbuf.size;
+
+ if (cdio_read_mode2_sectors (img, obj->psd_x, statbuf.lsn,
+ false, statbuf.secsize))
+ return VCDINFO_OPEN_ERROR;
+ }
+
+ if (!iso9660_fs_stat (img, "EXT/LOT_X.VCD;1", &statbuf, true)) {
+ vcd_debug ("found /EXT/LOT_X.VCD at sector %lu",
+ (unsigned long int) statbuf.lsn);
+
+ obj->lot_x = _vcd_malloc (ISO_BLOCKSIZE * statbuf.secsize);
+
+ if (cdio_read_mode2_sectors (img, obj->lot_x, statbuf.lsn, false,
+ statbuf.secsize))
+ return VCDINFO_OPEN_ERROR;
+
+ if (statbuf.size != LOT_VCD_SIZE * ISO_BLOCKSIZE)
+ vcd_warn ("LOT_X.VCD size != 65535");
+ }
+ break;
+ }
+ case VCD_TYPE_SVCD:
+ case VCD_TYPE_HQVCD: {
+ if (!iso9660_fs_stat (img, "MPEGAV", &statbuf, true))
+ vcd_warn ("non compliant /MPEGAV folder detected!");
+
+ if (iso9660_fs_stat (img, "SVCD/TRACKS.SVD;1", &statbuf, true))
+ vcd_warn ("mandatory /SVCD/TRACKS.SVD not found!");
+ else
+ vcd_debug ("found TRACKS.SVD signature at sector %lu",
+ (unsigned long int) statbuf.lsn);
+
+ if (iso9660_fs_stat (img, "SVCD/SEARCH.DAT;1", &statbuf, true))
+ vcd_warn ("mandatory /SVCD/SEARCH.DAT not found!");
+ else {
+ uint32_t size;
+ vcd_debug ("found SEARCH.DAT at sector %lu",
+ (unsigned long int) statbuf.lsn);
+
+ obj->search_buf = _vcd_malloc (ISO_BLOCKSIZE * statbuf.secsize);
+
+ if (cdio_read_mode2_sectors (img, obj->search_buf, statbuf.lsn,
+ false, statbuf.secsize))
+ return VCDINFO_OPEN_ERROR;
+
+ size = (3 * uint16_from_be (((SearchDat *)obj->search_buf)->scan_points))
+ + sizeof (SearchDat);
+
+ if (size > statbuf.size) {
+ vcd_warn ("number of scanpoints leads to bigger size than "
+ "file size of SEARCH.DAT! -- rereading");
+
+ free (obj->search_buf);
+ obj->search_buf = _vcd_malloc (ISO_BLOCKSIZE
+ * _vcd_len2blocks(size, ISO_BLOCKSIZE));
+
+ if (cdio_read_mode2_sectors (img, obj->search_buf,statbuf.lsn,
+ false, statbuf.secsize))
+ return VCDINFO_OPEN_ERROR;
+ }
+ }
+ break;
+ }
+ default:
+ ;
+ }
+
+ if (!iso9660_fs_stat (img, "EXT/SCANDATA.DAT;1", &statbuf, true)) {
+ vcd_debug ("found /EXT/SCANDATA.DAT at sector %d", statbuf.lsn);
+
+ obj->scandata_buf = _vcd_malloc (ISO_BLOCKSIZE * statbuf.secsize);
+
+ if (cdio_read_mode2_sectors (img, obj->scandata_buf, statbuf.lsn,
+ false, statbuf.secsize))
+ return VCDINFO_OPEN_ERROR;
+ }
+
+ return VCDINFO_OPEN_VCD;
+
+}
+
+/*!
+ Dispose of any resources associated with vcdinfo structure "obj".
+ Call this when "obj" it isn't needed anymore.
+
+ True is returned is everything went okay, and false if not.
+*/
+bool
+vcdinfo_close(vcdinfo_obj_t *obj)
+{
+ if (obj != NULL) {
+ if (obj->offset_list != NULL)
+ _vcd_list_free(obj->offset_list, true);
+ if (obj->offset_x_list != NULL)
+ _vcd_list_free(obj->offset_x_list, true);
+ free(obj->seg_sizes);
+ free(obj->lot);
+ free(obj->lot_x);
+ free(obj->tracks_buf);
+ free(obj->search_buf);
+ free(obj->source_name);
+
+ if (obj->img != NULL) cdio_destroy (obj->img);
+ _vcdinfo_zero(obj);
+ }
+
+ free(obj);
+ return(true);
+}
+
+
+/*
+ * Local variables:
+ * c-file-style: "gnu"
+ * tab-width: 8
+ * indent-tabs-mode: nil
+ * End:
+ */
diff --git a/src/input/vcd/libvcd/info_private.c b/src/input/vcd/libvcd/info_private.c
new file mode 100644
index 000000000..e0297d344
--- /dev/null
+++ b/src/input/vcd/libvcd/info_private.c
@@ -0,0 +1,318 @@
+/*
+ $Id: info_private.c,v 1.1 2003/10/13 11:47:12 f1rmb Exp $
+
+ Copyright (C) 2003 Rocky Bernstein <rocky@panix.com>
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Foundation
+ Software, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+*/
+/*
+ Like vcdinfo but exposes more of the internal structure. It is probably
+ better to use vcdinfo, when possible.
+*/
+
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
+
+#include <stdio.h>
+#include <stddef.h>
+#include <errno.h>
+
+#ifdef HAVE_STDLIB_H
+#include <stdlib.h>
+#endif
+#ifdef HAVE_STRING_H
+#include <string.h>
+#endif
+#ifdef HAVE_SYS_TYPES_H
+#include <sys/types.h>
+#endif
+#ifdef HAVE_SYS_STAT_H
+#include <sys/stat.h>
+#endif
+#ifdef HAVE_UNISTD_H
+#include <unistd.h>
+#endif
+
+#include <cdio/cdio.h>
+#include <cdio/util.h>
+
+#include <libvcd/types.h>
+#include <libvcd/files.h>
+
+#include <libvcd/info.h>
+
+/* Private headers */
+#include "assert.h"
+#include "bytesex.h"
+#include "data_structures.h"
+#include "info_private.h"
+#include "pbc.h"
+
+static const char _rcsid[] = "$Id: info_private.c,v 1.1 2003/10/13 11:47:12 f1rmb Exp $";
+
+/*
+ This fills in unassigned LIDs in the offset table. Due to
+ "rejected" LOT entries, some of these might not have gotten filled
+ in while scanning PBC (if in fact there even was a PBC).
+
+ Note: We assume that an unassigned LID is one whose value is 0.
+ */
+static void
+vcdinf_update_offset_list(struct _vcdinf_pbc_ctx *obj, bool extended)
+{
+ if (NULL==obj) return;
+ {
+ VcdListNode *node;
+ VcdList *unused_lids = _vcd_list_new();
+ VcdListNode *next_unused_node = _vcd_list_begin(unused_lids);
+
+ unsigned int last_lid=0;
+ VcdList *offset_list = extended ? obj->offset_x_list : obj->offset_list;
+
+ lid_t max_seen_lid=0;
+
+ _VCD_LIST_FOREACH (node, offset_list)
+ {
+ vcdinfo_offset_t *ofs = _vcd_list_node_data (node);
+ if (!ofs->lid) {
+ /* We have a customer! Assign a LID from the free pool
+ or take one from the end if no skipped LIDs.
+ */
+ VcdListNode *node=_vcd_list_node_next(next_unused_node);
+ if (node != NULL) {
+ lid_t *next_unused_lid=_vcd_list_node_data(node);
+ ofs->lid = *next_unused_lid;
+ next_unused_node=node;
+ } else {
+ max_seen_lid++;
+ ofs->lid = max_seen_lid;
+ }
+ } else {
+ /* See if we've skipped any LID numbers. */
+ last_lid++;
+ while (last_lid != ofs->lid ) {
+ lid_t * lid=_vcd_malloc (sizeof(lid_t));
+ *lid = last_lid;
+ _vcd_list_append(unused_lids, lid);
+ }
+ if (last_lid > max_seen_lid) max_seen_lid=last_lid;
+ }
+ }
+ _vcd_list_free(unused_lids, true);
+ }
+}
+
+/*!
+ Calls recursive routine to populate obj->offset_list or obj->offset_x_list
+ by going through LOT.
+*/
+void
+vcdinf_visit_lot (struct _vcdinf_pbc_ctx *obj)
+{
+ const LotVcd *lot = obj->extended ? obj->lot_x : obj->lot;
+ unsigned int n, tmp;
+
+ if (obj->extended) {
+ if (!obj->psd_x_size) return;
+ } else if (!obj->psd_size) return;
+
+ for (n = 0; n < LOT_VCD_OFFSETS; n++)
+ if ((tmp = vcdinf_get_lot_offset(lot, n)) != PSD_OFS_DISABLED)
+ vcdinf_visit_pbc (obj, n + 1, tmp, true);
+
+ _vcd_list_sort (obj->extended ? obj->offset_x_list : obj->offset_list,
+ (_vcd_list_cmp_func) vcdinf_lid_t_cmp);
+
+ /* Now really complete the offset table with LIDs. This routine
+ might obviate the need for vcdinf_visit_pbc() or some of it which is
+ more complex. */
+ vcdinf_update_offset_list(obj, obj->extended);
+}
+
+/*!
+ Recursive routine to populate obj->offset_list or obj->offset_x_list
+ by reading playback control entries referred to via lid.
+*/
+void
+vcdinf_visit_pbc (struct _vcdinf_pbc_ctx *obj, lid_t lid, unsigned int offset,
+ bool in_lot)
+{
+ VcdListNode *node;
+ vcdinfo_offset_t *ofs;
+ unsigned int psd_size = obj->extended ? obj->psd_x_size : obj->psd_size;
+ const uint8_t *psd = obj->extended ? obj->psd_x : obj->psd;
+ unsigned int _rofs = offset * obj->offset_mult;
+ VcdList *offset_list;
+
+ vcd_assert (psd_size % 8 == 0);
+
+ switch (offset)
+ {
+ case PSD_OFS_DISABLED:
+ case PSD_OFS_MULTI_DEF:
+ case PSD_OFS_MULTI_DEF_NO_NUM:
+ return;
+
+ default:
+ break;
+ }
+
+ if (_rofs >= psd_size)
+ {
+ if (obj->extended)
+ vcd_error ("psd offset out of range in extended PSD"
+ " (try --no-ext-psd option)");
+ else
+ vcd_warn ("psd offset out of range (%d >= %d)", _rofs, psd_size);
+ return;
+ }
+
+ vcd_assert (_rofs < psd_size);
+
+ if (!obj->offset_list)
+ obj->offset_list = _vcd_list_new ();
+
+ if (!obj->offset_x_list)
+ obj->offset_x_list = _vcd_list_new ();
+
+ if (obj->extended) {
+ offset_list = obj->offset_x_list;
+ } else
+ offset_list = obj->offset_list;
+
+ _VCD_LIST_FOREACH (node, offset_list)
+ {
+ ofs = _vcd_list_node_data (node);
+
+ if (offset == ofs->offset)
+ {
+ if (in_lot)
+ ofs->in_lot = true;
+
+ if (lid) {
+ /* Our caller thinks she knows what our LID is.
+ This should help out getting the LID for end descriptors
+ if not other things as well.
+ */
+ ofs->lid = lid;
+ }
+
+ ofs->ext = obj->extended;
+
+ return; /* already been there... */
+ }
+ }
+
+ ofs = _vcd_malloc (sizeof (vcdinfo_offset_t));
+
+ ofs->ext = obj->extended;
+ ofs->in_lot = in_lot;
+ ofs->lid = lid;
+ ofs->offset = offset;
+ ofs->type = psd[_rofs];
+
+ switch (ofs->type)
+ {
+ case PSD_TYPE_PLAY_LIST:
+ _vcd_list_append (offset_list, ofs);
+ {
+ const PsdPlayListDescriptor *d = (const void *) (psd + _rofs);
+ const lid_t lid = vcdinf_pld_get_lid(d);
+
+ if (!ofs->lid)
+ ofs->lid = lid;
+ else
+ if (ofs->lid != lid)
+ vcd_warn ("LOT entry assigned LID %d, but descriptor has LID %d",
+ ofs->lid, lid);
+
+ vcdinf_visit_pbc (obj, 0, vcdinf_pld_get_prev_offset(d), false);
+ vcdinf_visit_pbc (obj, 0, vcdinf_pld_get_next_offset(d), false);
+ vcdinf_visit_pbc (obj, 0, vcdinf_pld_get_return_offset(d), false);
+ }
+ break;
+
+ case PSD_TYPE_EXT_SELECTION_LIST:
+ case PSD_TYPE_SELECTION_LIST:
+ _vcd_list_append (offset_list, ofs);
+ {
+ const PsdSelectionListDescriptor *d =
+ (const void *) (psd + _rofs);
+
+ int idx;
+
+ if (!ofs->lid)
+ ofs->lid = uint16_from_be (d->lid) & 0x7fff;
+ else
+ if (ofs->lid != (uint16_from_be (d->lid) & 0x7fff))
+ vcd_warn ("LOT entry assigned LID %d, but descriptor has LID %d",
+ ofs->lid, uint16_from_be (d->lid) & 0x7fff);
+
+ vcdinf_visit_pbc (obj, 0, vcdinf_psd_get_prev_offset(d), false);
+ vcdinf_visit_pbc (obj, 0, vcdinf_psd_get_next_offset(d), false);
+ vcdinf_visit_pbc (obj, 0, vcdinf_psd_get_return_offset(d), false);
+ vcdinf_visit_pbc (obj, 0, vcdinf_psd_get_default_offset(d), false);
+ vcdinf_visit_pbc (obj, 0, uint16_from_be (d->timeout_ofs), false);
+
+ for (idx = 0; idx < vcdinf_get_num_selections(d); idx++)
+ vcdinf_visit_pbc (obj, 0, vcdinf_psd_get_offset(d, idx),
+ false);
+ }
+ break;
+
+ case PSD_TYPE_END_LIST:
+ _vcd_list_append (offset_list, ofs);
+ break;
+
+ default:
+ vcd_warn ("corrupt PSD???????");
+ free (ofs);
+ return;
+ break;
+ }
+}
+
+/*! Return the starting LBA (logical block address) for sequence
+ entry_num in obj. VCDINFO_NULL_LBA is returned if there is no entry.
+*/
+lba_t
+vcdinf_get_entry_lba(const EntriesVcd *entries, unsigned int entry_num)
+{
+ const msf_t *msf = vcdinf_get_entry_msf(entries, entry_num);
+ return (msf != NULL) ? cdio_msf_to_lba(msf) : VCDINFO_NULL_LBA;
+}
+
+/*! Return the starting MSF (minutes/secs/frames) for sequence
+ entry_num in obj. NULL is returned if there is no entry.
+ The first entry number is 0.
+*/
+const msf_t *
+vcdinf_get_entry_msf(const EntriesVcd *entries, unsigned int entry_num)
+{
+ const unsigned int entry_count = uint16_from_be (entries->entry_count);
+ return entry_num < entry_count ?
+ &(entries->entry[entry_num].msf)
+ : NULL;
+}
+
+
+/*
+ * Local variables:
+ * c-file-style: "gnu"
+ * tab-width: 8
+ * indent-tabs-mode: nil
+ * End:
+ */
diff --git a/src/input/vcd/libvcd/info_private.h b/src/input/vcd/libvcd/info_private.h
new file mode 100644
index 000000000..14e947e14
--- /dev/null
+++ b/src/input/vcd/libvcd/info_private.h
@@ -0,0 +1,113 @@
+/*!
+ \file vcdinf.h
+
+ Copyright (C) 2002,2003 Rocky Bernstein <rocky@panix.com>
+
+ \verbatim
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Foundation
+ Software, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+
+ Like vcdinfo but exposes more of the internal structure. It is probably
+ better to use vcdinfo, when possible.
+ \endverbatim
+*/
+
+#ifndef _VCD_INFO_PRIVATE_H
+#define _VCD_INFO_PRIVATE_H
+
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
+
+#include <cdio/cdio.h>
+#include <cdio/iso9660.h>
+#include <libvcd/types.h>
+#include <libvcd/files_private.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif /* __cplusplus */
+
+ struct _VcdInfo {
+ vcd_type_t vcd_type;
+
+ CdIo *img;
+
+ iso9660_pvd_t pvd;
+
+ InfoVcd info;
+ EntriesVcd entries;
+
+ VcdList *offset_list;
+ VcdList *offset_x_list;
+ uint32_t *seg_sizes;
+ lsn_t first_segment_lsn;
+
+ LotVcd *lot;
+ LotVcd *lot_x;
+ uint8_t *psd;
+ uint8_t *psd_x;
+ unsigned int psd_x_size;
+ bool extended;
+
+ bool has_xa; /* True if has extended attributes (XA) */
+
+ void *tracks_buf;
+ void *search_buf;
+ void *scandata_buf;
+
+ char *source_name; /* VCD device or file currently open */
+
+ };
+
+ /*! Return the starting MSF (minutes/secs/frames) for sequence
+ entry_num in obj. NULL is returned if there is no entry.
+ The first entry number is 0.
+ */
+ const msf_t * vcdinf_get_entry_msf(const EntriesVcd *entries,
+ unsigned int entry_num);
+
+ struct _vcdinf_pbc_ctx {
+ unsigned int psd_size;
+ lid_t maximum_lid;
+ unsigned offset_mult;
+ VcdList *offset_x_list;
+ VcdList *offset_list;
+
+ LotVcd *lot;
+ LotVcd *lot_x;
+ uint8_t *psd;
+ uint8_t *psd_x;
+ unsigned int psd_x_size;
+ bool extended;
+ };
+
+ /*!
+ Calls recursive routine to populate obj->offset_list or obj->offset_x_list
+ by going through LOT.
+ */
+ void vcdinf_visit_lot (struct _vcdinf_pbc_ctx *obj);
+
+ /*!
+ Recursive routine to populate obj->offset_list or obj->offset_x_list
+ by reading playback control entries referred to via lid.
+ */
+ void vcdinf_visit_pbc (struct _vcdinf_pbc_ctx *obj, lid_t lid,
+ unsigned int offset, bool in_lot);
+
+#ifdef __cplusplus
+}
+#endif /* __cplusplus */
+
+#endif /*_VCD_INFO_PRIVATE_H*/
diff --git a/src/input/vcd/libvcd/libvcd/Makefile.am b/src/input/vcd/libvcd/libvcd/Makefile.am
new file mode 100644
index 000000000..5474bcbe4
--- /dev/null
+++ b/src/input/vcd/libvcd/libvcd/Makefile.am
@@ -0,0 +1,12 @@
+noinst_HEADERS = files.h inf.h logging.h types.h files_private.h info.h sector.h version.h
+
+debug:
+install-debug: install
+
+mostlyclean-generic:
+ -rm -f *~ \#* .*~ .\#*
+
+maintainer-clean-generic:
+ -@echo "This command is intended for maintainers to use;"
+ -@echo "it deletes files that may require special tools to rebuild."
+ -rm -f Makefile.in
diff --git a/src/input/vcd/libvcd/libvcd/files.h b/src/input/vcd/libvcd/libvcd/files.h
new file mode 100644
index 000000000..a3a8352db
--- /dev/null
+++ b/src/input/vcd/libvcd/libvcd/files.h
@@ -0,0 +1,101 @@
+/*
+ $Id: files.h,v 1.1 2003/10/13 11:47:12 f1rmb Exp $
+
+ Copyright (C) 2000 Herbert Valerio Riedel <hvr@gnu.org>
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+*/
+
+#ifndef VCDFILES_H
+#define VCDFILES_H
+
+#include <libvcd/types.h>
+
+#define INFO_VCD_SECTOR 150
+#define ENTRIES_VCD_SECTOR 151
+#define LOT_VCD_SECTOR 152
+#define LOT_VCD_SIZE 32
+#define PSD_VCD_SECTOR (LOT_VCD_SECTOR+LOT_VCD_SIZE)
+
+#define MAX_SEGMENTS 1980
+#define MAX_ENTRIES 500
+#define MAX_SEQ_ENTRIES 99
+
+/* these are used for SVCDs only */
+#define TRACKS_SVD_SECTOR (PSD_VCD_SECTOR+1)
+#define SEARCH_DAT_SECTOR (TRACKS_SVD_SECTOR+1)
+
+/* Maximum index of optional LOT.VCD (the List ID Offset Table.) */
+#define LOT_VCD_OFFSETS ((1 << 15)-1)
+
+typedef enum {
+ PSD_TYPE_PLAY_LIST = 0x10, /* Play List */
+ PSD_TYPE_SELECTION_LIST = 0x18, /* Selection List (+Ext. for SVCD) */
+ PSD_TYPE_EXT_SELECTION_LIST = 0x1a, /* Extended Selection List (VCD2.0) */
+ PSD_TYPE_END_LIST = 0x1f, /* End List */
+ PSD_TYPE_COMMAND_LIST = 0x20 /* Command List */
+} psd_descriptor_types;
+
+#define ENTRIES_ID_VCD "ENTRYVCD"
+#define ENTRIES_ID_VCD3 "ENTRYSVD"
+#define ENTRIES_ID_SVCD "ENTRYVCD" /* not ENTRYSVD! */
+
+#define SCANDATA_VERSION_VCD2 0x02
+#define SCANDATA_VERSION_SVCD 0x01
+
+void
+set_entries_vcd(VcdObj *obj, void *buf);
+
+void
+set_info_vcd (VcdObj *obj, void *buf);
+
+uint32_t
+get_psd_size (VcdObj *obj, bool extended);
+
+void
+set_lot_vcd (VcdObj *obj, void *buf, bool extended);
+
+void
+set_psd_vcd (VcdObj *obj, void *buf, bool extended);
+
+void
+set_tracks_svd (VcdObj *obj, void *buf);
+
+uint32_t
+get_search_dat_size (const VcdObj *obj);
+
+void
+set_search_dat (VcdObj *obj, void *buf);
+
+uint32_t
+get_scandata_dat_size (const VcdObj *obj);
+
+void
+set_scandata_dat (VcdObj *obj, void *buf);
+
+
+vcd_type_t
+vcd_files_info_detect_type (const void *info_buf);
+
+#endif /* VCDFILES_H */
+
+
+/*
+ * Local variables:
+ * c-file-style: "gnu"
+ * tab-width: 8
+ * indent-tabs-mode: nil
+ * End:
+ */
diff --git a/src/input/vcd/libvcd/libvcd/files_private.h b/src/input/vcd/libvcd/libvcd/files_private.h
new file mode 100644
index 000000000..b4c41b635
--- /dev/null
+++ b/src/input/vcd/libvcd/libvcd/files_private.h
@@ -0,0 +1,562 @@
+/*
+ $Id: files_private.h,v 1.1 2003/10/13 11:47:12 f1rmb Exp $
+
+ Copyright (C) 2000 Herbert Valerio Riedel <hvr@gnu.org>
+ (C) 2000 Jens B. Jorgensen <jbj1@ultraemail.net>
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+*/
+
+#ifndef __VCD_FILES_PRIVATE_H__
+#define __VCD_FILES_PRIVATE_H__
+
+#include <libvcd/files.h>
+#include <libvcd/types.h>
+
+/* random note: most stuff is big endian here */
+
+#define ENTRIES_ID_VCD "ENTRYVCD"
+#define ENTRIES_ID_VCD3 "ENTRYSVD"
+#define ENTRIES_ID_SVCD "ENTRYVCD" /* not ENTRYSVD! */
+
+#define ENTRIES_VERSION_VCD 0x01
+#define ENTRIES_SPTAG_VCD 0x00
+
+#define ENTRIES_VERSION_VCD11 0x01
+#define ENTRIES_SPTAG_VCD11 0x00
+
+#define ENTRIES_VERSION_VCD2 0x02
+#define ENTRIES_SPTAG_VCD2 0x00
+
+#define ENTRIES_VERSION_SVCD 0x01
+#define ENTRIES_SPTAG_SVCD 0x00
+
+#define ENTRIES_VERSION_HQVCD 0x01
+#define ENTRIES_SPTAG_HQVCD 0x00
+
+PRAGMA_BEGIN_PACKED
+
+typedef struct _EntriesVcd_tag {
+ char ID[8]; /* "ENTRYVCD" */
+ uint8_t version; /* 0x02 --- VCD2.0
+ 0x01 --- SVCD, should be
+ same as version in
+ INFO.SVD */
+ uint8_t sys_prof_tag; /* 0x01 if VCD1.1
+ 0x00 else */
+ uint16_t entry_count; /* 1 <= tracks <= 500 */
+ struct { /* all fields are BCD */
+ uint8_t n; /* cd track no 2 <= n <= 99 */
+ msf_t msf;
+ } GNUC_PACKED entry[MAX_ENTRIES];
+ uint8_t reserved2[36]; /* RESERVED, must be 0x00 */
+} GNUC_PACKED _EntriesVcd; /* sector 00:04:01 */
+
+#define EntriesVcd_SIZEOF ISO_BLOCKSIZE
+
+
+#define INFO_ID_VCD "VIDEO_CD"
+#define INFO_ID_SVCD "SUPERVCD"
+#define INFO_ID_HQVCD "HQ-VCD "
+
+#define INFO_VERSION_VCD 0x01
+#define INFO_SPTAG_VCD 0x00
+
+#define INFO_VERSION_VCD11 0x01
+#define INFO_SPTAG_VCD11 0x01
+
+#define INFO_VERSION_VCD2 0x02
+#define INFO_SPTAG_VCD2 0x00
+
+#define INFO_VERSION_SVCD 0x01
+#define INFO_SPTAG_SVCD 0x00
+
+#define INFO_VERSION_HQVCD 0x01
+#define INFO_SPTAG_HQVCD 0x01
+
+#define INFO_OFFSET_MULT 0x08
+
+/* This one-byte field describes certain characteristics of the disc */
+typedef struct {
+#if defined(BITFIELD_LSBF)
+ bool reserved1 : 1; /* Reserved, must be zero */
+ bitfield_t restriction : 2; /* restriction, eg. "unsuitable
+ for kids":
+ 0x0 ==> unrestricted,
+ 0x1 ==> restricted category 1,
+ 0x2 ==> restricted category 2,
+ 0x3 ==> restricted category 3 */
+ bool special_info : 1; /* Special Information is encoded
+ in the pictures */
+ bool user_data_cc : 1; /* MPEG User Data is used
+ for Closed Caption */
+ bool use_lid2 : 1; /* If == 1 and the PSD is
+ interpreted and the next
+ disc has the same album
+ id then start the next
+ disc at List ID #2,
+ otherwise List ID #1 */
+ bool use_track3 : 1; /* If == 1 and the PSD is
+ not interpreted and
+ next disc has same album
+ id, then start next disc
+ with track 3, otherwise
+ start with track 2 */
+ bool pbc_x : 1; /* extended PBC available */
+#else
+ bool pbc_x : 1;
+ bool use_track3 : 1;
+ bool use_lid2 : 1;
+ bool user_data_cc : 1;
+ bool special_info : 1;
+ bitfield_t restriction : 2;
+ bool reserved1 : 1;
+#endif
+} GNUC_PACKED InfoStatusFlags;
+
+#define InfoStatusFlags_SIZEOF 1
+
+enum {
+ VCD_FILES_VIDEO_NOSTREAM = 0,
+ VCD_FILES_VIDEO_NTSC_STILL = 1,
+ VCD_FILES_VIDEO_NTSC_STILL2 = 2,
+ VCD_FILES_VIDEO_NTSC_MOTION = 3,
+ VCD_FILES_VIDEO_PAL_STILL = 5,
+ VCD_FILES_VIDEO_PAL_STILL2 = 6,
+ VCD_FILES_VIDEO_PAL_MOTION = 7
+};
+
+typedef struct
+{
+#if defined(BITFIELD_LSBF)
+ bitfield_t audio_type : 2; /* Audio characteristics:
+ 0x0 - No MPEG audio stream
+ 0x1 - One MPEG1 or MPEG2 audio
+ stream without extension
+ 0x2 - Two MPEG1 or MPEG2 audio
+ streams without extension
+ 0x3 - One MPEG2 multi-channel
+ audio stream w/ extension*/
+ bitfield_t video_type : 3; /* Video characteristics:
+ 0x0 - No MPEG video data
+ 0x1 - NTSC still picture
+ 0x2 - Reserved (NTSC hires?)
+ 0x3 - NTSC motion picture
+ 0x4 - Reserved
+ 0x5 - PAL still picture
+ 0x6 - Reserved (PAL hires?)
+ 0x7 - PAL motion picture */
+ bool item_cont : 1; /* Indicates segment is continuation
+ 0x0 - 1st or only segment of item
+ 0x1 - 2nd or later
+ segment of item */
+ bitfield_t ogt : 2; /* 0x0 - no OGT substream
+ 0x1 - sub-stream 0 available
+ 0x2 - sub-stream 0 & 1 available
+ 0x3 - all OGT sub-substreams
+ available */
+#else
+ bitfield_t ogt : 2;
+ bool item_cont : 1;
+ bitfield_t video_type : 3;
+ bitfield_t audio_type : 2;
+#endif
+} GNUC_PACKED InfoSpiContents;
+
+#define InfoSpiContents_SIZEOF 1
+
+typedef struct _InfoVcd_tag {
+ char ID[8]; /* const "VIDEO_CD" for
+ VCD, "SUPERVCD" or
+ "HQ-VCD " for SVCD */
+ uint8_t version; /* 0x02 -- VCD2.0,
+ 0x01 for SVCD and VCD1.x */
+ uint8_t sys_prof_tag; /* System Profile Tag, used
+ to define the set of
+ mandatory parts to be
+ applied for compatibility;
+ 0x00 for "SUPERVCD",
+ 0x01 for "HQ-VCD ",
+ 0x0n for VCDx.n */
+ char album_desc[16]; /* album identification/desc. */
+ uint16_t vol_count; /* number of volumes in album */
+ uint16_t vol_id; /* number id of this volume in album */
+ uint8_t pal_flags[13]; /* bitset of 98 PAL(=set)/NTSC flags */
+ InfoStatusFlags flags; /* status flags bit field */
+ uint32_t psd_size; /* size of PSD.VCD file */
+ msf_t first_seg_addr; /* first segment addresses,
+ coded BCD The location
+ of the first sector of
+ the Segment Play Item
+ Area, in the form
+ mm:ss:00. Must be
+ 00:00:00 if the PSD size
+ is 0. */
+ uint8_t offset_mult; /* offset multiplier, must be 8 */
+ uint16_t lot_entries; /* offsets in lot */
+ uint16_t item_count; /* segments used for segmentitems */
+ InfoSpiContents spi_contents[MAX_SEGMENTS]; /* The next 1980 bytes
+ contain one byte for each possible
+ segment play item. Each byte indicates
+ contents. */
+
+ uint16_t playing_time[5]; /* in seconds */
+ char reserved[2]; /* Reserved, must be zero */
+} GNUC_PACKED _InfoVcd;
+
+#define InfoVcd_SIZEOF ISO_BLOCKSIZE
+
+/* LOT.VCD
+ This optional file is only necessary if the PSD size is not zero.
+ This List ID Offset Table allows you to start playing the PSD from
+ lists other than the default List ID number. This table has a fixed length
+ of 32 sectors and maps List ID numbers into List Offsets. It's got
+ an entry for each List ID Number with the 16-bit offset. Note that
+ List ID 1 has an offset of 0x0000. All unused or non-user-accessible
+ entries must be 0xffff. */
+
+#define LOT_VCD_OFFSETS ((1 << 15)-1)
+
+typedef struct _LotVcd_tag {
+ uint16_t reserved; /* Reserved, must be zero */
+ uint16_t offset[LOT_VCD_OFFSETS]; /* offset given in 8 byte units */
+} GNUC_PACKED _LotVcd;
+
+#define LotVcd_SIZEOF (32*ISO_BLOCKSIZE)
+
+/* PSD.VCD
+ The PSD controls the "user interaction" mode which can be used to make
+ menus, etc. The PSD contains a set of Lists. Each List defines a set of
+ Items which are played in sequence. An Item can be an mpeg track (in whole
+ or part) or a Segment Play Item which can subsequently be mpeg video
+ with or without audio, one more more mpeg still pictures (with or without
+ audio) or mpeg audio only.
+
+ The Selection List defines the action to be taken in response to a set
+ of defined user actions: Next, Previous, Default Select, Numeric, Return.
+
+ The End List terminates the control flow or switches to the next
+ disc volume.
+
+ Each list has a unique list id number. The first must be 1, the others can
+ be anything (up to 32767).
+
+ References to PSD list addresses are expressed as an offset into the PSD
+ file. The offset indicated in the file must be multiplied by the Offset
+ Multiplier found in the info file (although this seems to always have to
+ be 8). Unused areas are filled with zeros. List ID 1 starts at offset 0.
+*/
+
+/* ...difficult to represent as monolithic C struct... */
+
+typedef struct {
+ uint8_t type; /* PSD_TYPE_END_LIST */
+ uint8_t next_disc; /* 0x00 to stop PBC or 0xnn to switch to disc no nn */
+ uint16_t change_pic; /* 0 or 1000..2979, should be still image */
+ uint8_t reserved[4]; /* padded with 0x00 */
+} GNUC_PACKED PsdEndListDescriptor;
+
+#define PsdEndListDescriptor_SIZEOF 8
+
+typedef struct {
+#if defined(BITFIELD_LSBF)
+ bool SelectionAreaFlag : 1;
+ bool CommandListFlag : 1;
+ bitfield_t reserved : 6;
+#else
+ bitfield_t reserved : 6;
+ bool CommandListFlag : 1;
+ bool SelectionAreaFlag : 1;
+#endif
+} GNUC_PACKED PsdSelectionListFlags;
+
+#define PsdSelectionListFlags_SIZEOF 1
+
+typedef struct _PsdSelectionListDescriptor_tag {
+ uint8_t type;
+ PsdSelectionListFlags flags;
+ uint8_t nos;
+ uint8_t bsn;
+ uint16_t lid;
+ uint16_t prev_ofs;
+ uint16_t next_ofs;
+ uint16_t return_ofs;
+ uint16_t default_ofs;
+ uint16_t timeout_ofs;
+ uint8_t totime;
+ uint8_t loop;
+ uint16_t itemid;
+ uint16_t ofs[EMPTY_ARRAY_SIZE]; /* variable length */
+ /* PsdSelectionListDescriptorExtended */
+} GNUC_PACKED _PsdSelectionListDescriptor;
+
+#define PsdSelectionListDescriptor_SIZEOF 20
+
+typedef struct {
+ struct psd_area_t prev_area;
+ struct psd_area_t next_area;
+ struct psd_area_t return_area;
+ struct psd_area_t default_area;
+ struct psd_area_t area[EMPTY_ARRAY_SIZE]; /* variable length */
+} GNUC_PACKED PsdSelectionListDescriptorExtended;
+
+#define PsdSelectionListDescriptorExtended_SIZEOF 16
+
+typedef struct {
+ uint8_t type;
+ uint16_t command_count;
+ uint16_t lid;
+ uint16_t command[EMPTY_ARRAY_SIZE]; /* variable length */
+} GNUC_PACKED PsdCommandListDescriptor;
+
+#define PsdCommandListDescriptor_SIZEOF 5
+
+typedef struct _PsdPlayListDescriptor_tag {
+ uint8_t type;
+ uint8_t noi; /* number of items */
+ uint16_t lid; /* list id: high-bit means this list is rejected in
+ the LOT (also, can't use 0) */
+ uint16_t prev_ofs; /* previous list offset (0xffff disables) */
+ uint16_t next_ofs; /* next list offset (0xffff disables) */
+ uint16_t return_ofs; /* return list offset (0xffff disables) */
+ uint16_t ptime; /* play time in 1/15 s, 0x0000 meaning full item */
+ uint8_t wtime; /* delay after, in seconds, if 1 <= wtime <= 60 wait
+ is wtime else if 61 <= wtime <= 254 wait is
+ (wtime-60) * 10 + 60 else wtime == 255 wait is
+ infinite */
+ uint8_t atime; /* auto pause wait time calculated same as wtime,
+ used for each item in list if the auto pause flag
+ in a sector is true */
+ uint16_t itemid[EMPTY_ARRAY_SIZE]; /* item number
+ 0 <= n <= 1 - play nothing
+ 2 <= n <= 99 - play track n
+ 100 <= n <= 599 - play entry
+ (n - 99) from entries
+ table to end of track
+ 600 <= n <= 999 - reserved
+ 1000 <= n <= 2979 - play segment
+ play item (n - 999)
+ 2980 <= n <= 0xffff - reserved */
+} GNUC_PACKED _PsdPlayListDescriptor;
+
+#define PsdPlayListDescriptor_SIZEOF 14
+
+/* TRACKS.SVD
+ SVCD\TRACKS.SVD is a mandatory file which describes the numbers and types
+ of MPEG tracks on the disc. */
+
+/* SVDTrackContent indicates the audio/video content of an MPEG Track */
+
+typedef struct {
+#if defined(BITFIELD_LSBF)
+ bitfield_t audio : 2; /* Audio Content
+ 0x00 : No MPEG audio stream
+ 0x01 : One MPEG{1|2} audio stream
+ 0x02 : Two MPEG{1|2} streams
+ 0x03 : One MPEG2 multi-channel
+ audio stream with
+ extension */
+ bitfield_t video : 3; /* Video Content
+ 0x00 : No MPEG video
+ 0x03 : NTSC video
+ 0x07 : PAL video */
+ bool reserved1 : 1; /* Reserved, must be zero */
+ bitfield_t ogt : 2; /* 0x0 - no OGT substream
+ 0x1 - sub-stream 0 available
+ 0x2 - sub-stream 0 & 1 available
+ 0x3 - all OGT sub-substreams
+ available */
+#else
+ bitfield_t ogt : 2;
+ bool reserved1 : 1;
+ bitfield_t video : 3;
+ bitfield_t audio : 2;
+#endif
+} GNUC_PACKED SVDTrackContent;
+
+#define SVDTrackContent_SIZEOF 1
+
+/* The file contains a series of structures, one for each
+ track, which indicates the track's playing time (in sectors, not actually
+ real time) and contents. */
+
+#define TRACKS_SVD_FILE_ID "TRACKSVD"
+#define TRACKS_SVD_VERSION 0x01
+
+typedef struct {
+ char file_id[8]; /* == "TRACKSVD" */
+ uint8_t version; /* == 0x01 */
+ uint8_t reserved; /* Reserved, must be zero */
+ uint8_t tracks; /* number of MPEG tracks */
+ msf_t playing_time[EMPTY_ARRAY_SIZE]; /* per track, BCD coded
+ mm:ss:ff */
+} GNUC_PACKED TracksSVD;
+
+#define TracksSVD_SIZEOF 11
+
+typedef struct {
+ /* TracksSVD tracks_svd; */
+ SVDTrackContent contents[1]; /* should be [], but C99 doesn't allow it
+ indicates track contents */
+} GNUC_PACKED TracksSVD2;
+
+#define TracksSVD2_SIZEOF SVDTrackContent_SIZEOF
+
+/* VCD30 tracks svd */
+
+typedef struct {
+ char file_id[8]; /* == "TRACKSVD" */
+ uint8_t version; /* == 0x01 */
+ uint8_t reserved; /* Reserved, must be zero */
+ uint8_t tracks; /* number of MPEG tracks */
+ struct {
+ msf_t cum_playing_time; /* BCD coded mm:ss:ff */
+ uint8_t ogt_info;
+ uint8_t audio_info;
+ } GNUC_PACKED track[EMPTY_ARRAY_SIZE];
+} GNUC_PACKED TracksSVD_v30;
+
+#define TracksSVD_v30_SIZEOF 11
+
+/* SEARCH.DAT
+ This file defines where the scan points are. It covers all mpeg tracks
+ together. A scan point at time T is the nearest I-picture in the MPEG
+ stream to the given time T. Scan points are given at every half-second
+ for the entire duration of the disc. */
+
+#define SEARCH_FILE_ID "SEARCHSV"
+#define SEARCH_VERSION 0x01
+#define SEARCH_TIME_INTERVAL 0x01
+
+typedef struct {
+ char file_id[8]; /* = "SEARCHSV" */
+ uint8_t version; /* = 0x01 */
+ uint8_t reserved; /* Reserved, must be zero */
+ uint16_t scan_points; /* the number of scan points */
+ uint8_t time_interval; /* The interval of time in
+ between scan points, in units
+ of 0.5 seconds, must be 0x01 */
+ msf_t points[EMPTY_ARRAY_SIZE]; /* The series of scan points */
+} GNUC_PACKED SearchDat;
+
+#define SearchDat_SIZEOF 13
+
+/* SPICONTX.SVD
+ */
+
+#define SPICONTX_FILE_ID "SPICONSV"
+#define SPICONTX_VERSION 0x01
+
+typedef struct {
+ char file_id[8]; /* = "SPICONSV" */
+ uint8_t version; /* = 0x01 */
+ uint8_t reserved; /* Reserved, must be zero */
+ struct {
+ uint8_t ogt_info;
+ uint8_t audio_info;
+ } GNUC_PACKED spi[MAX_SEGMENTS];
+ uint8_t reserved2[126]; /* 0x00 */
+} GNUC_PACKED SpicontxSvd;
+
+#define SpicontxSvd_SIZEOF (2*ISO_BLOCKSIZE)
+
+/* SCANDATA.DAT for VCD 2.0 */
+
+#define SCANDATA_FILE_ID "SCAN_VCD"
+#define SCANDATA_VERSION_VCD2 0x02
+#define SCANDATA_VERSION_SVCD 0x01
+
+typedef struct {
+ char file_id[8]; /* = "SCAN_VCD" */
+ uint8_t version; /* = 0x02 */
+ uint8_t reserved; /* Reserved, must be zero */
+ uint16_t scan_points; /* the number of scan points */
+ msf_t points[EMPTY_ARRAY_SIZE]; /* actual scan points
+ points[time(iframe)/0.5] */
+} GNUC_PACKED ScandataDat_v2;
+
+#define ScandataDat_v2_SIZEOF 12
+
+/* SCANDATA.DAT for SVCD
+ This file fulfills much the same purpose of the SEARCH.DAT file except
+ that this file is mandatory only if the System Profile Tag of the
+ INFO.SVD file is 0x01 (HQ-VCD) and also that it contains sector addresses
+ also for each video Segment Play Items in addition to the regular MPEG
+ tracks. */
+
+typedef struct {
+ char file_id[8]; /* = "SCAN_VCD" */
+ uint8_t version; /* = 0x01 */
+ uint8_t reserved; /* Reserved, must be zero */
+ uint16_t scandata_count; /* number of 3-byte entries in
+ the table */
+ uint16_t track_count; /* number of mpeg tracks on disc */
+ uint16_t spi_count; /* number of consecutively recorded
+ play item segments (as opposed
+ to the number of segment play
+ items). */
+ msf_t cum_playtimes[EMPTY_ARRAY_SIZE]; /* cumulative playing
+ time up to track
+ N. Track time just wraps
+ at 99:59:74 */
+} GNUC_PACKED ScandataDat1;
+
+#define ScandataDat1_SIZEOF 16
+
+typedef struct {
+ /* ScandataDat head; */
+ uint16_t spi_indexes[1]; /* should be [], but C doesn't allow that;
+ Indexes into the following scandata
+ table */
+} GNUC_PACKED ScandataDat2;
+
+#define ScandataDat2_SIZEOF sizeof(uint16_t)
+
+typedef struct {
+ /* ScandataDat2 head; */
+ uint16_t mpegtrack_start_index; /* Index into the
+ following scandata table
+ where the MPEG track
+ scan points start */
+
+ /* The scandata table starts here */
+ struct {
+ uint8_t track_num; /* Track number as in TOC */
+ uint16_t table_offset; /* Index into scandata table */
+ } GNUC_PACKED mpeg_track_offsets[EMPTY_ARRAY_SIZE];
+} GNUC_PACKED ScandataDat3;
+
+#define ScandataDat3_SIZEOF 2
+
+typedef struct {
+ /* ScandataDat3 head; */
+ msf_t scandata_table[1]; /* should be [] but C99 doesn't allow that */
+} GNUC_PACKED ScandataDat4;
+
+#define ScandataDat4_SIZEOF msf_t_SIZEOF
+
+PRAGMA_END_PACKED
+
+#endif /* __VCD_FILES_PRIVATE_H__ */
+
+
+/*
+ * Local variables:
+ * c-file-style: "gnu"
+ * tab-width: 8
+ * indent-tabs-mode: nil
+ * End:
+ */
diff --git a/src/input/vcd/libvcd/libvcd/inf.h b/src/input/vcd/libvcd/libvcd/inf.h
new file mode 100644
index 000000000..8da69264a
--- /dev/null
+++ b/src/input/vcd/libvcd/libvcd/inf.h
@@ -0,0 +1,300 @@
+/*!
+ \file inf.h
+
+ Copyright (C) 2002,2003 Rocky Bernstein <rocky@panix.com>
+
+ \verbatim
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Foundation
+ Software, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ \endverbatim
+*/
+
+/*
+ Things here refer to lower-level structures using a structure other
+ than vcdinfo_t. For higher-level structures via the vcdinfo_t, see
+ info.h
+*/
+
+#ifndef _VCD_INF_H
+#define _VCD_INF_H
+
+#include <cdio/iso9660.h>
+#include <libvcd/info.h>
+
+ const char * vcdinf_area_str (const struct psd_area_t *_area);
+
+ /*!
+ Return a string containing the VCD album id.
+ */
+ const char * vcdinf_get_album_id(const InfoVcd *info);
+
+ /*!
+ Return the VCD application ID.
+ NULL is returned if there is some problem in getting this.
+ */
+ const char * vcdinf_get_application_id(const iso9660_pvd_t *pvd);
+
+ /*!
+ Get autowait time value for PsdPlayListDescriptor *d.
+ Time is in seconds unless it is -1 (unlimited).
+ */
+ int vcdinf_get_autowait_time (const PsdPlayListDescriptor *d);
+
+ /*!
+ Return the base selection number. VCD_INVALID_BSN is returned if there
+ is an error.
+ */
+ unsigned int vcdinf_get_bsn(const PsdSelectionListDescriptor *psd);
+
+ /*! Return the starting LBA (logical block address) for sequence
+ entry_num in obj. VCDINFO_NULL_LBA is returned if there is no entry.
+ The first entry number is 0.
+ */
+ lba_t vcdinf_get_entry_lba(const EntriesVcd *entries,
+ unsigned int entry_num);
+
+ const char * vcdinf_get_format_version_str (vcd_type_t vcd_type);
+
+ /*!
+ Return loop count. 0 is infinite loop.
+ */
+ uint16_t vcdinf_get_loop_count (const PsdSelectionListDescriptor *psd);
+
+ /*!
+ Return LOT offset
+ */
+ uint16_t vcdinf_get_lot_offset (const LotVcd *lot, unsigned int n);
+
+ /*!
+ Return a string containing the VCD preparer id with trailing
+ blanks removed.
+ NULL is returned if there is some problem in getting this.
+ */
+ const char * vcdinf_get_preparer_id(const iso9660_pvd_t *pvd);
+
+ /*!
+ Return number of bytes in PSD.
+ */
+ uint32_t vcdinf_get_psd_size (const InfoVcd *info);
+
+ /*!
+ Return a string containing the VCD publisher id with trailing
+ blanks removed.
+ NULL is returned if there is some problem in getting this.
+ */
+ const char * vcdinf_get_publisher_id(const iso9660_pvd_t *pvd);
+
+ /*!
+ Return the number of segments in the VCD.
+ */
+ unsigned int vcdinf_get_num_entries(const EntriesVcd *entries);
+
+ /*!
+ Return number of LIDs.
+ */
+ lid_t vcdinf_get_num_LIDs (const InfoVcd *info);
+
+ /*!
+ Return the number of segments in the VCD.
+ */
+ segnum_t vcdinf_get_num_segments(const InfoVcd *info);
+
+ /*!
+ Return the number of menu selections for selection-list descriptor d.
+ */
+ unsigned int vcdinf_get_num_selections(const PsdSelectionListDescriptor *d);
+
+ /*!
+ Get play-time value for PsdPlayListDescriptor *d.
+ Time is in 1/15-second units.
+ */
+ uint16_t vcdinf_get_play_time (const PsdPlayListDescriptor *d);
+
+ /*!
+ Return a string containing the VCD system id with trailing
+ blanks removed.
+ NULL is returned if there is some problem in getting this.
+ */
+ const char * vcdinf_get_system_id(const iso9660_pvd_t *pvd);
+
+ /*!
+ Get timeout offset for PsdPlayListDescriptor *d. Return
+ VCDINFO_INVALID_OFFSET if d is NULL;
+ Time is in seconds unless it is -1 (unlimited).
+ */
+ uint16_t vcdinf_get_timeout_offset (const PsdSelectionListDescriptor *d);
+
+ /*!
+ Get timeout wait value for PsdPlayListDescriptor *d.
+ Time is in seconds unless it is -1 (unlimited).
+ */
+ int vcdinf_get_timeout_time (const PsdSelectionListDescriptor *d);
+
+ /*!
+ Return the track number for entry n in obj. The first track starts
+ at 1.
+ */
+ track_t vcdinf_get_track(const EntriesVcd *entries,
+ const unsigned int entry_num);
+
+ /*!
+ Return the VCD volume num - the number of the CD in the collection.
+ This is a number between 1 and the volume count.
+ */
+ unsigned int vcdinf_get_volume_num(const InfoVcd *info);
+
+ /*!
+ Return the VCD volume count - the number of CD's in the collection.
+ */
+ unsigned int vcdinf_get_volume_count(const InfoVcd *info);
+
+ /*!
+ Return the VCD ID.
+ NULL is returned if there is some problem in getting this.
+ */
+ const char * vcdinf_get_volume_id(const iso9660_pvd_t *pvd);
+
+ /*!
+ Return the VCD volumeset ID.
+ NULL is returned if there is some problem in getting this.
+ */
+ const char * vcdinf_get_volumeset_id(const iso9660_pvd_t *pvd);
+
+ /*!
+ Get wait time value for PsdPlayListDescriptor *d.
+ Time is in seconds unless it is -1 (unlimited).
+ */
+ int vcdinf_get_wait_time (const PsdPlayListDescriptor *d);
+
+ /*!
+ Return true if loop has a jump delay
+ */
+ bool vcdinf_has_jump_delay (const PsdSelectionListDescriptor *psd);
+
+ /*!
+ Comparison routine used in sorting. We compare LIDs and if those are
+ equal, use the offset.
+ Note: we assume an unassigned LID is 0 and this compares as a high value.
+
+ NOTE: Consider making static.
+ */
+ int vcdinf_lid_t_cmp (vcdinfo_offset_t *a, vcdinfo_offset_t *b);
+
+ /**
+ \fn vcdinf_pld_get_next_offset(const PsdPlayListDescriptor *pld);
+ \brief Get next offset for a given PSD selector descriptor.
+ \return VCDINFO_INVALID_OFFSET is returned on error or if pld has no
+ "next" entry or pld is NULL. Otherwise the LID offset is returned.
+ */
+ uint16_t vcdinf_pld_get_next_offset(const PsdPlayListDescriptor *pld);
+
+ /*!
+ Get the LID from a given play-list descriptor.
+ VCDINFO_REJECTED_MASK is returned on error or pld is NULL.
+ */
+ uint16_t vcdinf_pld_get_lid(const PsdPlayListDescriptor *pld);
+
+ /*!
+ Return the playlist item i in d.
+ */
+ uint16_t vcdinf_pld_get_play_item(const PsdPlayListDescriptor *pld,
+ unsigned int i);
+
+ /**
+ \fn vcdinf_pld_get_prev_offset(const PsdSelectionListDescriptor *pld);
+ \brief Get prev offset for a given PSD selector descriptor.
+ \return VCDINFO_INVALID_OFFSET is returned on error or if pld has no
+ "prev" entry or pld is NULL. Otherwise the LID offset is returned.
+ */
+ uint16_t vcdinf_pld_get_prev_offset(const PsdPlayListDescriptor *pld);
+
+ /**
+ \fn vcdinf_pld_get_return_offset(const PsdPlayListDescriptor *pld);
+ \brief Get return offset for a given PLD selector descriptor.
+ \return VCDINFO_INVALID_OFFSET is returned on error or if pld has no
+ "return" entry or pld is NULL. Otherwise the LID offset is returned.
+ */
+ uint16_t vcdinf_pld_get_return_offset(const PsdPlayListDescriptor *pld);
+
+ /*!
+ Return number of items in LIDs. Return 0 if error or not found.
+ */
+ int vcdinf_pld_get_noi (const PsdPlayListDescriptor *pld);
+
+ /**
+ * \fn vcdinfo_psd_get_default_offset(const PsdSelectionListDescriptor *psd);
+ * \brief Get next offset for a given PSD selector descriptor.
+ * \return VCDINFO_INVALID_OFFSET is returned on error or if psd is
+ * NULL. Otherwise the LID offset is returned.
+ */
+ uint16_t vcdinf_psd_get_default_offset(const PsdSelectionListDescriptor *psd);
+
+ /*!
+ Get the item id for a given selection-list descriptor.
+ VCDINFO_REJECTED_MASK is returned on error or if psd is NULL.
+ */
+ uint16_t vcdinf_psd_get_itemid(const PsdSelectionListDescriptor *psd);
+
+ /*!
+ Get the LID from a given selection-list descriptor.
+ VCDINFO_REJECTED_MASK is returned on error or psd is NULL.
+ */
+ uint16_t vcdinf_psd_get_lid(const PsdSelectionListDescriptor *psd);
+
+ /*!
+ Get the LID rejected status for a given selection-list descriptor.
+ true is also returned d is NULL.
+ */
+ bool
+ vcdinf_psd_get_lid_rejected(const PsdSelectionListDescriptor *psd);
+
+ /**
+ \fn vcdinf_psd_get_next_offset(const PsdSelectionListDescriptor *psd);
+ \brief Get "next" offset for a given PSD selector descriptor.
+ \return VCDINFO_INVALID_OFFSET is returned on error or if psd has no
+ "next" entry or psd is NULL. Otherwise the LID offset is returned.
+ */
+ lid_t vcdinf_psd_get_next_offset(const PsdSelectionListDescriptor *psd);
+
+ /*!
+ \brief Get offset entry_num for a given PSD selector descriptor.
+ \param d PSD selector containing the entry_num we query
+ \param entry_num entry number that we want the LID offset for.
+ \return VCDINFO_INVALID_OFFSET is returned if d on error or d is
+ NULL. Otherwise the LID offset is returned.
+ */
+ uint16_t vcdinf_psd_get_offset(const PsdSelectionListDescriptor *d,
+ unsigned int entry_num);
+ /**
+ \fn vcdinf_psd_get_prev_offset(const PsdPlayListDescriptor *psd);
+ \brief Get "prev" offset for a given PSD selector descriptor.
+ \return VCDINFO_INVALID_OFFSET is returned on error or if psd has no
+ "prev"
+ entry or psd is NULL. Otherwise the LID offset is returned.
+ */
+ uint16_t vcdinf_psd_get_prev_offset(const PsdSelectionListDescriptor *psd);
+
+ /**
+ * \fn vcdinf_psd_get_return_offset(const PsdSelectionListDescriptor *psd);
+ * \brief Get "return" offset for a given PSD selector descriptor.
+ \return VCDINFO_INVALID_OFFSET is returned on error or if psd has no
+ "return" entry or psd is NULL. Otherwise the LID offset is returned.
+ */
+ uint16_t vcdinf_psd_get_return_offset(const PsdSelectionListDescriptor *psd);
+
+#ifdef __cplusplus
+}
+#endif /* __cplusplus */
+
+#endif /*_VCD_INF_H*/
diff --git a/src/input/vcd/libvcd/libvcd/info.h b/src/input/vcd/libvcd/libvcd/info.h
new file mode 100644
index 000000000..93bc1d0e9
--- /dev/null
+++ b/src/input/vcd/libvcd/libvcd/info.h
@@ -0,0 +1,766 @@
+/*!
+ \file info.h
+
+ Copyright (C) 2002,2003 Rocky Bernstein <rocky@panix.com>
+
+ \verbatim
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Foundation
+ Software, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ \endverbatim
+*/
+/*
+ Things here refer to higher-level structures usually accessed via
+ vcdinfo_t. For lower-level access which generally use
+ structures other than vcdinfo_t, see inf.h
+*/
+
+
+#ifndef _VCD_INFO_H
+#define _VCD_INFO_H
+
+#include <libvcd/types.h>
+#include <libvcd/files.h>
+#include <cdio/cdio.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif /* __cplusplus */
+
+/*========== Move somewhere else? ================*/
+
+/*! \def Max # characters in an album id. */
+#define MAX_ALBUM_LEN 16
+#define MAX_APPLICATION_ID 128
+#define MAX_PREPARER_ID 128
+#define MAX_PUBLISHER_ID 128
+#define MAX_SYSTEM_ID 32
+#define MAX_VOLUME_ID 32
+#define MAX_VOLUMESET_ID 128
+
+/*!
+ Invalid LBA, Note: VCD player uses the fact that this is a very high
+ value.
+ */
+#define VCDINFO_NULL_LBA CDIO_INVALID_LBA
+
+/*!
+ Invalid LSN, Note: VCD player uses the fact that this is a very high
+ value.
+ */
+#define VCDINFO_NULL_LSN VCDINFO_NULL_LBA
+
+/*========== End move somewhere else? ================*/
+
+/*!
+ Portion of uint16_t which determines whether offset is
+ rejected or not.
+*/
+#define VCDINFO_REJECTED_MASK (0x8000)
+
+/*!
+ Portion of uint16_t which contains the offset.
+*/
+#define VCDINFO_OFFSET_MASK (VCDINFO_REJECTED_MASK-1)
+
+/*!
+ Portion of uint16_t which contains the lid.
+*/
+#define VCDINFO_LID_MASK (VCDINFO_REJECTED_MASK-1)
+
+/*!
+ Constant for invalid track number
+*/
+#define VCDINFO_INVALID_TRACK 0xFF
+
+/*!
+ Constant for invalid LID offset.
+*/
+#define VCDINFO_INVALID_OFFSET 0xFFFF
+
+/*!
+ Constant for ending or "leadout" track.
+*/
+#define VCDINFO_LEADOUT_TRACK 0xaa
+
+/*!
+ Constant for invalid sequence entry.
+*/
+#define VCDINFO_INVALID_ENTRY 0xFFFF
+
+/*!
+ Constant for invalid LID.
+ FIXME: player needs these to be the same.
+ VCDimager code requres 0 for an UNINITIALIZED LID.
+
+*/
+#define VCDINFO_INVALID_LID VCDINFO_INVALID_ENTRY
+#define VCDINFO_UNINIT_LID 0
+
+/*!
+ Constant for invalid itemid
+*/
+#define VCDINFO_INVALID_ITEMID 0xFFFF
+
+/*!
+ Constant for invalid audio type
+*/
+#define VCDINFO_INVALID_AUDIO_TYPE 4
+
+/*!
+ Constant for invalid base selection number (BSN)
+*/
+#define VCDINFO_INVALID_BSN 200
+
+/* The number of sectors allocated in a Video CD segment is a fixed: 150.
+
+ NOTE: The actual number of sectors used is often less and can sometimes
+ be gleaned by looking at the correspoinding ISO 9660 file entry (or
+ by scanning the MPEG segment which may be slow).
+ Some media players get confused by or complain about padding at the end
+ a segment.
+*/
+#define VCDINFO_SEGMENT_SECTOR_SIZE 150
+
+ /* Opaque type used in most routines below. */
+ typedef struct _VcdInfo vcdinfo_obj_t;
+
+ /* See enum in vcd_files_private.h */
+ typedef enum {
+ VCDINFO_FILES_VIDEO_NOSTREAM = 0,
+ VCDINFO_FILES_VIDEO_NTSC_STILL = 1,
+ VCDINFO_FILES_VIDEO_NTSC_STILL2 = 2, /* lo+hires*/
+ VCDINFO_FILES_VIDEO_NTSC_MOTION = 3,
+ VCDINFO_FILES_VIDEO_PAL_STILL = 5,
+ VCDINFO_FILES_VIDEO_PAL_STILL2 = 6, /* lo+hires*/
+ VCDINFO_FILES_VIDEO_PAL_MOTION = 7,
+ VCDINFO_FILES_VIDEO_INVALID = 8
+ } vcdinfo_video_segment_type_t;
+
+ /*!
+ Used in working with LOT - list of offsets and lid's
+ */
+ typedef struct {
+ uint8_t type;
+ lid_t lid;
+ uint16_t offset;
+ bool in_lot; /* Is listed in LOT. */
+ bool ext; /* True if entry comes from offset_x_list. */
+ } vcdinfo_offset_t;
+
+ /*!
+ The kind of entry associated with an selection-item id
+ */
+ /* See corresponding enum in vcd_pbc.h. */
+ typedef enum {
+ VCDINFO_ITEM_TYPE_TRACK,
+ VCDINFO_ITEM_TYPE_ENTRY,
+ VCDINFO_ITEM_TYPE_SEGMENT,
+ VCDINFO_ITEM_TYPE_LID,
+ VCDINFO_ITEM_TYPE_SPAREID2,
+ VCDINFO_ITEM_TYPE_NOTFOUND,
+ } vcdinfo_item_enum_t;
+
+ typedef struct {
+ uint16_t num;
+ vcdinfo_item_enum_t type;
+ } vcdinfo_itemid_t;
+
+ typedef enum {
+ VCDINFO_OPEN_ERROR, /* Error */
+ VCDINFO_OPEN_VCD, /* Is VCD of some sort */
+ VCDINFO_OPEN_OTHER, /* Is not VCD but something else */
+ } vcdinfo_open_return_t;
+
+ typedef struct
+ {
+
+ psd_descriptor_types descriptor_type;
+ /* Only one of pld or psd is used below. Not all
+ C compiler accept the anonymous unions commented out below. */
+ /* union { */
+ PsdPlayListDescriptor *pld;
+ PsdSelectionListDescriptor *psd;
+ /* }; */
+
+ } PsdListDescriptor;
+
+ /*!
+ Return the number of audio channels implied by "audio_type".
+ 0 is returned on error.
+ */
+ unsigned int
+ vcdinfo_audio_type_num_channels(const vcdinfo_obj_t *obj,
+ unsigned int audio_type);
+
+ /*!
+ Return a string describing an audio type.
+ */
+ const char * vcdinfo_audio_type2str(const vcdinfo_obj_t *obj,
+ unsigned int audio_type);
+
+ /*!
+ Note first seg_num is 0!
+ */
+ const char *
+ vcdinfo_ogt2str(const vcdinfo_obj_t *obj, segnum_t seg_num);
+
+ /*!
+ Note first seg_num is 0!
+ */
+ const char *
+ vcdinfo_video_type2str(const vcdinfo_obj_t *obj, segnum_t seg_num);
+
+ const char *
+ vcdinfo_pin2str (uint16_t itemid);
+
+ /*!
+ \brief Classify itemid_num into the kind of item it is: track #, entry #,
+ segment #.
+ \param itemid is set to contain this classifcation an the converted
+ entry number.
+ */
+ void
+ vcdinfo_classify_itemid (uint16_t itemid_num,
+ /*out*/ vcdinfo_itemid_t *itemid);
+
+ /*!
+ Return a string containing the VCD album id, or NULL if there is
+ some problem in getting this.
+ */
+ const char *
+ vcdinfo_get_album_id(const vcdinfo_obj_t *obj);
+
+ /*!
+ Return the VCD application ID.
+ NULL is returned if there is some problem in getting this.
+ */
+ const char *
+ vcdinfo_get_application_id(const vcdinfo_obj_t *obj);
+
+ /*!
+ Return a pointer to the cdio structure for the CD image opened or
+ NULL if error.
+ */
+ CdIo *
+ vcdinfo_get_cd_image (const vcdinfo_obj_t *vcd_obj);
+
+ /*!
+ Return a string containing the default VCD device if none is specified.
+ This might be something like "/dev/cdrom" on Linux or
+ "/vol/dev/aliases/cdrom0" on Solaris, or maybe "VIDEOCD.CUE" for
+ if bin/cue I/O routines are in effect.
+
+ Return NULL we can't get this information.
+ */
+ char *
+ vcdinfo_get_default_device (const vcdinfo_obj_t *vcd_obj);
+
+ /*!
+ \brief Get default LID offset.
+ \return VCDINFO_INVALID_OFFSET is returned on error or if
+ no "return" entry. Otherwise the LID offset is returned.
+ */
+ uint16_t
+ vcdinfo_get_default_offset(const vcdinfo_obj_t *obj, unsigned int lid);
+
+ /*!
+ Return number of sector units in of an entry. 0 is returned if
+ entry_num is invalid.
+ */
+ uint32_t
+ vcdinfo_get_entry_sect_count (const vcdinfo_obj_t *obj,
+ unsigned int entry_num);
+
+ /*! Return the starting LBA (logical block address) for sequence
+ entry_num in obj. VCDINFO_NULL_LBA is returned if there is no entry.
+ The first entry number is 0.
+ */
+ lba_t
+ vcdinfo_get_entry_lba(const vcdinfo_obj_t *obj, unsigned int entry_num);
+
+ /*! Return the starting LSN (logical sector number) for sequence
+ entry_num in obj. VCDINFO_NULL_LSN is returned if there is no entry.
+ The first entry number is 0.
+ */
+ lsn_t
+ vcdinfo_get_entry_lsn(const vcdinfo_obj_t *obj, unsigned int entry_num);
+
+ /*! Return the starting MSF (minutes/secs/frames) for sequence
+ entry_num in obj. NULL is returned if there is no entry.
+ The first entry number is 0.
+ */
+ const msf_t *
+ vcdinfo_get_entry_msf(const vcdinfo_obj_t *obj, unsigned int entry_num);
+
+ /*!
+ Get the VCD format (VCD 1.0 VCD 1.1, SVCD, ... for this object.
+ The type is also set inside obj.
+ The first entry number is 0.
+ */
+ vcd_type_t
+ vcdinfo_get_format_version (vcdinfo_obj_t *obj);
+
+ /*!
+ Return a string giving VCD format (VCD 1.0 VCD 1.1, SVCD, ...
+ for this object.
+ */
+ const char *
+ vcdinfo_get_format_version_str (const vcdinfo_obj_t *obj);
+
+ EntriesVcd * vcdinfo_get_entriesVcd (vcdinfo_obj_t *obj);
+
+ InfoVcd * vcdinfo_get_infoVcd (vcdinfo_obj_t *obj);
+
+ void * vcdinfo_get_tracksSVD (vcdinfo_obj_t *obj);
+
+ void * vcdinfo_get_scandata (vcdinfo_obj_t *obj);
+
+ void * vcdinfo_get_searchDat (vcdinfo_obj_t *obj);
+
+ void * vcdinfo_get_pvd (vcdinfo_obj_t *obj);
+
+ /*!
+ Get the item id for a given list ID.
+ VCDINFO_REJECTED_MASK is returned on error or if obj is NULL.
+ */
+ uint16_t
+ vcdinfo_lid_get_itemid(const vcdinfo_obj_t *obj, lid_t lid);
+
+ /*!
+ Get the LOT pointer.
+ */
+ LotVcd *
+ vcdinfo_get_lot(const vcdinfo_obj_t *obj);
+
+ /*!
+ Get the extended LOT pointer.
+ */
+ LotVcd *
+ vcdinfo_get_lot_x(const vcdinfo_obj_t *obj);
+
+ /*!
+ Return Number of LIDs.
+ */
+ lid_t
+ vcdinfo_get_num_LIDs (const vcdinfo_obj_t *obj);
+
+ /*!
+ Return the audio type for a given track.
+ VCDINFO_INVALID_AUDIO_TYPE is returned on error.
+ */
+ unsigned int
+ vcdinfo_get_num_audio_channels(unsigned int audio_type);
+
+ /*!
+ Return the number of entries in the VCD.
+ */
+ unsigned int
+ vcdinfo_get_num_entries(const vcdinfo_obj_t *obj);
+
+ /*!
+ Return the number of segments in the VCD.
+ */
+ segnum_t
+ vcdinfo_get_num_segments(const vcdinfo_obj_t *obj);
+
+ /*!
+ Return the highest track number in the current medium.
+
+ Because we track start numbering at 0 (which is the ISO 9660 track
+ containing Video CD naviagion and disk information), this is one
+ less than the number of tracks.
+
+ If there are no tracks, we return -1.
+ */
+ unsigned int
+ vcdinfo_get_num_tracks(const vcdinfo_obj_t *obj);
+
+ /*!
+ \fn vcdinfo_lid_get_offset(const vcdinfo_obj_t *obj,
+ unsigned int entry_num);
+ \brief Get offset entry_num for a given LID.
+ \return VCDINFO_INVALID_OFFSET is returned if obj on error or obj
+ is NULL. Otherwise the LID offset is returned.
+ */
+ uint16_t vcdinfo_lid_get_offset(const vcdinfo_obj_t *obj, lid_t lid,
+ unsigned int entry_num);
+
+ /*!
+ Get the VCD info list.
+ */
+ VcdList *vcdinfo_get_offset_list(const vcdinfo_obj_t *obj);
+
+ /*!
+ Get the VCD info extended offset list.
+ */
+ VcdList *vcdinfo_get_offset_x_list(const vcdinfo_obj_t *obj);
+
+ /*!
+ Get the VCD info offset multiplier.
+ */
+ unsigned int vcdinfo_get_offset_mult(const vcdinfo_obj_t *obj);
+
+ /*!
+ Get entry in offset list for the item that has offset. This entry
+ has for example the LID. NULL is returned on error.
+ */
+ vcdinfo_offset_t *
+ vcdinfo_get_offset_t (const vcdinfo_obj_t *obj, unsigned int offset);
+
+ /*!
+ Return a string containing the VCD preparer id with trailing
+ blanks removed, or NULL if there is some problem in getting this.
+ */
+ const char *
+ vcdinfo_get_preparer_id(const vcdinfo_obj_t *obj);
+
+ /*!
+ Get the PSD.
+ */
+ uint8_t *vcdinfo_get_psd(const vcdinfo_obj_t *obj);
+
+ /*!
+ Get the extended PSD.
+ */
+ uint8_t *vcdinfo_get_psd_x(const vcdinfo_obj_t *obj);
+
+ /*!
+ Return number of bytes in PSD.
+ */
+ uint32_t vcdinfo_get_psd_size (const vcdinfo_obj_t *obj);
+
+ /*!
+ Return number of bytes in the extended PSD.
+ */
+ uint32_t vcdinfo_get_psd_x_size (const vcdinfo_obj_t *obj);
+
+ /*!
+ Get the PSD Selection List Descriptor for a given lid.
+ False is returned if not found.
+ */
+ bool vcdinfo_lid_get_pxd(const vcdinfo_obj_t *obj, PsdListDescriptor *pxd,
+ uint16_t lid);
+
+ /*!
+ Return a string containing the VCD publisher id with trailing
+ blanks removed, or NULL if there is some problem in getting this.
+ */
+ const char * vcdinfo_get_publisher_id(const vcdinfo_obj_t *obj);
+
+ /**
+ \fn vcdinfo_get_return_offset(const vcdinfo_obj_t *obj);
+ \brief Get return offset for a given LID.
+ \return VCDINFO_INVALID_OFFSET is returned on error or if LID has no
+ "return" entry. Otherwise the LID offset is returned.
+ */
+ lid_t
+ vcdinfo_get_return_offset(const vcdinfo_obj_t *obj, lid_t lid);
+
+ /*!
+ Return the audio type for a given segment.
+ VCDINFO_INVALID_AUDIO_TYPE is returned on error.
+ */
+ unsigned int
+ vcdinfo_get_seg_audio_type(const vcdinfo_obj_t *obj, segnum_t seg_num);
+
+ /*!
+ Return true if this segment is supposed to continue to the next one,
+ (is part of an "item" or listing in the ISO 9660 filesystem).
+ */
+ bool vcdinfo_get_seg_continue(const vcdinfo_obj_t *obj, segnum_t seg_num);
+
+ /*! Return the starting LBA (logical block address) for segment
+ entry_num in obj. VCDINFO_NULL_LBA is returned if there is no entry.
+
+ Note first seg_num is 0.
+ */
+ lba_t
+ vcdinfo_get_seg_lba(const vcdinfo_obj_t *obj, segnum_t seg_num);
+
+ /*! Return the starting LSN (logical sector number) for segment
+ entry_num in obj. VCDINFO_NULL_LBA is returned if there is no entry.
+
+ Note first seg_num is 0.
+ */
+ lsn_t
+ vcdinfo_get_seg_lsn(const vcdinfo_obj_t *obj, segnum_t seg_num);
+
+ /*! Return the starting MSF (minutes/secs/frames) for segment
+ entry_num in obj. NULL is returned if there is no entry.
+
+ Note first seg_num is 0.
+ */
+ const msf_t *
+ vcdinfo_get_seg_msf(const vcdinfo_obj_t *obj, segnum_t seg_num);
+
+ /*!
+ Return the number of sectors for segment
+ entry_num in obj. 0 is returned if there is no entry.
+
+ Use this routine to figure out the actual number of bytes a physical
+ region of a disk or CD takes up for a segment.
+
+ If an item has been broken up into a number of "continued" segments,
+ we will report the item size for the first segment and 0 for the
+ remaining ones. We may revisit this decision later.
+ */
+ uint32_t
+ vcdinfo_get_seg_sector_count(const vcdinfo_obj_t *obj, segnum_t seg_num);
+
+ /*!
+ Return a string containing the VCD system id with trailing
+ blanks removed, or NULL if there is some problem in getting this.
+ */
+ const char *
+ vcdinfo_get_system_id(const vcdinfo_obj_t *obj);
+
+ /*!
+ Return the track number for entry n in obj.
+
+ In contrast to libcdio we start numbering at 0 which is the
+ ISO9660 and metadata information for the Video CD. Thus track
+ 1 is the first track the first complete MPEG track generally.
+ */
+ track_t
+ vcdinfo_get_track(const vcdinfo_obj_t *obj, const unsigned int entry_num);
+
+ /*!
+ Return the audio type for a given track.
+ VCDINFO_INVALID_AUDIO_TYPE is returned on error.
+
+ Note: track 1 is usually the first track.
+ */
+ unsigned int
+ vcdinfo_get_track_audio_type(const vcdinfo_obj_t *obj, track_t track_num);
+
+ /*!
+ Return the starting LBA (logical block address) for track number
+ track_num in obj.
+
+ The IS0-9660 filesystem track has number 0. Tracks associated
+ with playable entries numbers start at 1.
+
+ The "leadout" track is specified either by
+ using track_num LEADOUT_TRACK or the total tracks+1.
+ VCDINFO_NULL_LBA is returned on failure.
+ */
+ lba_t
+ vcdinfo_get_track_lba(const vcdinfo_obj_t *obj, track_t track_num);
+
+ /*!
+ Return the starting LSN (logical sector number) for track number
+ track_num in obj.
+
+ The IS0-9660 filesystem track has number 0. Tracks associated
+ with playable entries numbers start at 1.
+
+ The "leadout" track is specified either by
+ using track_num LEADOUT_TRACK or the total tracks+1.
+ VCDINFO_NULL_LBA is returned on failure.
+ */
+ lsn_t
+ vcdinfo_get_track_lsn(const vcdinfo_obj_t *obj, track_t track_num);
+
+ /*!
+ Return the starting MSF (minutes/secs/frames) for track number
+ track_num in obj.
+
+ The IS0-9660 filesystem track has number 0. Tracks associated
+ with playable entries numbers start at 1.
+
+ The "leadout" track is specified either by
+ using track_num LEADOUT_TRACK or the total tracks+1.
+ VCDINFO_NULL_LBA is returned on failure.
+ */
+ int
+ vcdinfo_get_track_msf(const vcdinfo_obj_t *obj, track_t track_num,
+ uint8_t *min, uint8_t *sec, uint8_t *frame);
+
+ /*!
+ Return the size in sectors for track n.
+
+ The IS0-9660 filesystem track has number 1. Tracks associated
+ with playable entries numbers start at 2.
+
+ FIXME: Whether we count the track pregap sectors is a bit haphazard.
+ We should add a parameter to indicate whether this is wanted or not.
+ */
+ unsigned int
+ vcdinfo_get_track_sect_count(const vcdinfo_obj_t *obj,
+ const track_t track_num);
+
+ /*!
+ Return size in bytes for track number for entry n in obj.
+
+ The IS0-9660 filesystem track has number 0. Tracks associated
+ with playable entries numbers start at 1.
+
+ FIXME: Do we count the track pregap sectors is a bit haphazard.
+ We should add a parameter to indicate whether this is wanted or not.
+ */
+ unsigned int
+ vcdinfo_get_track_size(const vcdinfo_obj_t *obj, track_t track_num);
+
+ /*!
+ \brief Get the kind of video stream segment of segment seg_num in obj.
+ \return VCDINFO_FILES_VIDEO_INVALID is returned if on error or obj is
+ null. Otherwise the enumeration type.
+
+ Note first seg_num is 0!
+ */
+ vcdinfo_video_segment_type_t
+ vcdinfo_get_video_type(const vcdinfo_obj_t *obj, segnum_t seg_num);
+
+ /*!
+ \brief Get the kind of VCD that obj refers to.
+ */
+ vcd_type_t
+ vcdinfo_get_VCD_type(const vcdinfo_obj_t *obj);
+
+ /*!
+ Return the VCD volume count - the number of CD's in the collection.
+ O is returned if there is some problem in getting this.
+ */
+ unsigned int
+ vcdinfo_get_volume_count(const vcdinfo_obj_t *obj);
+
+ /*!
+ Return the VCD ID.
+ NULL is returned if there is some problem in getting this.
+ */
+ const char *
+ vcdinfo_get_volume_id(const vcdinfo_obj_t *obj);
+
+ /*!
+ Return the VCD volumeset ID.
+ NULL is returned if there is some problem in getting this.
+ */
+ const char *
+ vcdinfo_get_volumeset_id(const vcdinfo_obj_t *obj);
+
+ /*!
+ Return the VCD volume num - the number of the CD in the collection.
+ This is a number between 1 and the volume count.
+ O is returned if there is some problem in getting this.
+ */
+ unsigned int
+ vcdinfo_get_volume_num(const vcdinfo_obj_t *obj);
+
+ int vcdinfo_get_wait_time (uint16_t wtime);
+
+ /*!
+ Return true if there is playback control.
+ */
+ bool vcdinfo_has_pbc (const vcdinfo_obj_t *obj);
+
+ /*!
+ Return true if VCD has "extended attributes" (XA). Extended attributes
+ add meta-data attributes to a entries of file describing the file.
+ See also cdio_get_xa_attr_str() which returns a string similar to
+ a string you might get on a Unix filesystem listing ("ls").
+ */
+ bool vcdinfo_has_xa(const vcdinfo_obj_t *obj);
+
+ /*!
+ Add one to the MSF.
+ */
+ void vcdinfo_inc_msf (uint8_t *min, uint8_t *sec, int8_t *frame);
+
+ /*!
+ Convert minutes, seconds and frame (MSF components) into a
+ logical block address (or LBA).
+ See also msf_to_lba which uses msf_t as its single parameter.
+ */
+ void
+ vcdinfo_lba2msf (lba_t lba, uint8_t *min, uint8_t *sec, uint8_t *frame);
+
+ /*!
+ Convert minutes, seconds and frame (MSF components) into a
+ logical block address (or LBA).
+ See also msf_to_lba which uses msf_t as its single parameter.
+ */
+ lba_t vcdinfo_msf2lba (uint8_t min, uint8_t sec, int8_t frame);
+
+ /*!
+ Convert minutes, seconds and frame (MSF components) into a
+ logical sector number (or LSN).
+ */
+ lsn_t vcdinfo_msf2lsn (uint8_t min, uint8_t sec, int8_t frame);
+
+ const char *
+ vcdinfo_ofs2str (const vcdinfo_obj_t *obj, unsigned int offset, bool ext);
+
+ void vcdinfo_visit_lot (vcdinfo_obj_t *obj, bool extended);
+
+ bool vcdinfo_read_psd (vcdinfo_obj_t *obj);
+
+ /*!
+ Change trailing blanks in str to nulls. Str has a maximum size of
+ n characters.
+ */
+ const char * vcdinfo_strip_trail (const char str[], size_t n);
+
+ /*!
+ Initialize the vcdinfo structure "obj". Should be done before other
+ routines using obj are called.
+ */
+ bool vcdinfo_init(vcdinfo_obj_t *obj);
+
+ /*!
+ Set up vcdinfo structure "obj" for reading from a particular
+ medium. This should be done before after initialization but before
+ any routines that need to retrieve data.
+
+ source_name is the device or file to use for inspection, and
+ source_type indicates what driver to use or class of drivers in the
+ case of DRIVER_DEVICE.
+ access_mode gives the CD access method for reading should the driver
+ allow for more than one kind of access method (e.g. MMC versus ioctl
+ on GNU/Linux)
+
+ If source_name is NULL we'll fill in the appropriate default device
+ name for the given source_type. However if in addtion source_type is
+ DRIVER_UNKNOWN, then we'll scan for a drive containing a VCD.
+
+ VCDINFO_OPEN_VCD is returned if everything went okay;
+ VCDINFO_OPEN_ERROR if there was an error and VCDINFO_OPEN_OTHER if the
+ medium is something other than a VCD.
+ */
+ vcdinfo_open_return_t
+ vcdinfo_open(vcdinfo_obj_t **p_obj, char *source_name[],
+ driver_id_t source_type, const char access_mode[]);
+
+
+ /*!
+ Dispose of any resources associated with vcdinfo structure "obj".
+ Call this when "obj" it isn't needed anymore.
+
+ True is returned is everything went okay, and false if not.
+ */
+ bool vcdinfo_close(vcdinfo_obj_t *obj);
+
+ /*!
+ Return true if offset is "rejected". That is shouldn't be displayed
+ in a list of entries.
+ */
+ bool vcdinfo_is_rejected(uint16_t offset);
+
+/* Include lower-level access as well. */
+#include <libvcd/inf.h>
+
+#ifdef __cplusplus
+}
+#endif /* __cplusplus */
+
+#endif /*_VCD_INFO_H*/
diff --git a/src/input/vcd/libvcd/libvcd/logging.h b/src/input/vcd/libvcd/libvcd/logging.h
new file mode 100644
index 000000000..8c3158248
--- /dev/null
+++ b/src/input/vcd/libvcd/libvcd/logging.h
@@ -0,0 +1,72 @@
+/*
+ $Id: logging.h,v 1.1 2003/10/13 11:47:12 f1rmb Exp $
+
+ Copyright (C) 2000 Herbert Valerio Riedel <hvr@gnu.org>
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+*/
+
+#ifndef __VCD_LOGGING_H__
+#define __VCD_LOGGING_H__
+
+#include <libvcd/types.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif /* __cplusplus */
+
+typedef enum {
+ VCD_LOG_DEBUG = 1,
+ VCD_LOG_INFO,
+ VCD_LOG_WARN,
+ VCD_LOG_ERROR,
+ VCD_LOG_ASSERT
+} vcd_log_level_t;
+
+void
+vcd_log (vcd_log_level_t level, const char format[], ...) GNUC_PRINTF(2, 3);
+
+typedef void (*vcd_log_handler_t) (vcd_log_level_t level,
+ const char message[]);
+
+vcd_log_handler_t
+vcd_log_set_handler (vcd_log_handler_t new_handler);
+
+void
+vcd_debug (const char format[], ...) GNUC_PRINTF(1,2);
+
+void
+vcd_info (const char format[], ...) GNUC_PRINTF(1,2);
+
+void
+vcd_warn (const char format[], ...) GNUC_PRINTF(1,2);
+
+void
+vcd_error (const char format[], ...) GNUC_PRINTF(1,2);
+
+#ifdef __cplusplus
+}
+#endif /* __cplusplus */
+
+#endif /* __VCD_LOGGING_H__ */
+
+
+/*
+ * Local variables:
+ * c-file-style: "gnu"
+ * tab-width: 8
+ * indent-tabs-mode: nil
+ * End:
+ */
diff --git a/src/input/vcd/libvcd/libvcd/sector.h b/src/input/vcd/libvcd/libvcd/sector.h
new file mode 100644
index 000000000..c49f644b6
--- /dev/null
+++ b/src/input/vcd/libvcd/libvcd/sector.h
@@ -0,0 +1,118 @@
+/*
+ $Id: sector.h,v 1.1 2003/10/13 11:47:12 f1rmb Exp $
+
+ Copyright (C) 2000 Herbert Valerio Riedel <hvr@gnu.org>
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+*/
+
+#ifndef _VCD_SECTOR_H_
+#define _VCD_SECTOR_H_
+
+#include <libvcd/types.h>
+
+/* subheader */
+
+/*
+
+ SVCD 1.0
+ ~~~~~~~~
+
+ empty sector: fn=0 cn=0 sm=%00100000 ci=0
+ data sector: fn=0 cn=0 sm=%x0001000 ci=0
+ mpeg sector: fn=1 cn=1 sm=%x11x001x ci=0x80
+
+ VCD 2.0
+ ~~~~~~~
+
+ /MPEGAV/AVSEQyy.DAT
+ empty sector: fn=yy cn=0 sm=%x11x000x ci=0
+ video sector: fn=yy cn=1 sm=%x11x001x ci=0x0f
+ audio sector: fn=yy cn=1 sm=%x11x010x ci=0x7f
+
+ /SEGMENT/ITEMzzzz.DAT
+ empty sector: fn=1 cn=0 sm=%x11x000x ci=0
+ video sector: fn=1 cn=1 sm=%x11x001x ci=0x0f
+ lores still: fn=1 cn=2 sm=%x11x001x ci=0x1f
+ hires still: fn=1 cn=3 sm=%x11x001x ci=0x3f
+ audio sector: fn=1 cn=1 sm=%x11x010x ci=0x7f
+
+ /VCD/ *.VCD
+ data sector: fn=0 cn=0 sm=%x000100x ci=0
+
+ *.*
+ data sector: fn=1 cn=0 sm=%x0001000 ci=0
+
+*/
+
+/* file numbers */
+
+/* dynamic */
+
+/* channel numbers */
+#define CN_VIDEO 0x01
+#define CN_STILL 0x02
+#define CN_STILL2 0x03
+#define CN_AUDIO 0x01
+#define CN_AUDIO2 0x02
+#define CN_OGT 0x02 /* fixme -- is it 0x04 ?? */
+#define CN_PAD 0x00
+#define CN_EMPTY 0x00
+
+/* submode byte */
+#define SM_EOF (1<<7)
+#define SM_REALT (1<<6)
+#define SM_FORM2 (1<<5)
+#define SM_TRIG (1<<4)
+#define SM_DATA (1<<3)
+#define SM_AUDIO (1<<2)
+#define SM_VIDEO (1<<1)
+#define SM_EOR (1<<0)
+
+/* coding information */
+#define CI_VIDEO 0x0f
+#define CI_STILL 0x1f
+#define CI_STILL2 0x3f
+#define CI_AUDIO 0x7f
+#define CI_AUDIO2 0x7f
+#define CI_OGT 0x0f
+#define CI_PAD 0x1f
+#define CI_MPEG2 0x80
+#define CI_EMPTY 0x00
+
+/* make mode 2 form 1/2 sector
+ *
+ * data must be a buffer of size 2048 or 2324 for SM_FORM2
+ * raw_sector must be a writable buffer of size 2352
+ */
+void
+_vcd_make_mode2 (void *raw_sector, const void *data, uint32_t extent,
+ uint8_t fnum, uint8_t cnum, uint8_t sm, uint8_t ci);
+
+/* ...data must be a buffer of size 2336 */
+
+void
+_vcd_make_raw_mode2 (void *raw_sector, const void *data, uint32_t extent);
+
+#endif /* _VCD_SECTOR_H_ */
+
+
+/*
+ * Local variables:
+ * c-file-style: "gnu"
+ * tab-width: 8
+ * indent-tabs-mode: nil
+ * End:
+ */
diff --git a/src/input/vcd/libvcd/libvcd/types.h b/src/input/vcd/libvcd/libvcd/types.h
new file mode 100644
index 000000000..50d6903f7
--- /dev/null
+++ b/src/input/vcd/libvcd/libvcd/types.h
@@ -0,0 +1,97 @@
+/*
+ $Id: types.h,v 1.1 2003/10/13 11:47:12 f1rmb Exp $
+
+ Copyright (C) 2000 Herbert Valerio Riedel <hvr@gnu.org>
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+*/
+
+#ifndef __VCD_TYPES_H__
+#define __VCD_TYPES_H__
+
+#include <cdio/types.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif /* __cplusplus */
+
+ /* Opaque types ... */
+
+ /* Defined fully in data_structures.c */
+ typedef struct _VcdList VcdList;
+ typedef struct _VcdListNode VcdListNode;
+
+ /* Defined fully in files_private.h */
+ typedef struct _InfoVcd_tag InfoVcd;
+ typedef struct _EntriesVcd_tag EntriesVcd;
+ typedef struct _LotVcd_tag LotVcd;
+
+ typedef struct _PsdPlayListDescriptor_tag PsdPlayListDescriptor;
+ typedef struct _PsdSelectionListDescriptor_tag PsdSelectionListDescriptor;
+
+ /* Overall data structure representing a VideoCD object.
+ Defined fully in info_private.h.
+ */
+ typedef struct _VcdObj VcdObj;
+
+ /* enum defining supported VideoCD types */
+ typedef enum
+ {
+ VCD_TYPE_INVALID = 0,
+ VCD_TYPE_VCD,
+ VCD_TYPE_VCD11,
+ VCD_TYPE_VCD2,
+ VCD_TYPE_SVCD,
+ VCD_TYPE_HQVCD
+ }
+ vcd_type_t;
+
+ /* The type of an playback control list ID (LID). */
+ typedef uint16_t lid_t;
+
+ /* The type of a segment number 0..1980 segment items possible. */
+ typedef uint16_t segnum_t;
+
+ /* (0,0) == upper left , (255,255) == lower right
+ setting all to zero disables area */
+ PRAGMA_BEGIN_PACKED
+ struct psd_area_t
+ {
+ uint8_t x1; /* upper left */
+ uint8_t y1; /* upper left */
+ uint8_t x2; /* lower right */
+ uint8_t y2; /* lower right */
+ } GNUC_PACKED;
+ PRAGMA_END_PACKED
+
+#define struct_psd_area_t_SIZEOF 4
+
+#define PSD_OFS_DISABLED 0xffff
+#define PSD_OFS_MULTI_DEF 0xfffe
+#define PSD_OFS_MULTI_DEF_NO_NUM 0xfffd
+
+#ifdef __cplusplus
+}
+#endif /* __cplusplus */
+
+#endif /* __VCD_TYPES_H__ */
+
+/*
+ * Local variables:
+ * c-file-style: "gnu"
+ * tab-width: 8
+ * indent-tabs-mode: nil
+ * End:
+ */
diff --git a/src/input/vcd/libvcd/libvcd/version.h b/src/input/vcd/libvcd/libvcd/version.h
new file mode 100644
index 000000000..b8739cb8a
--- /dev/null
+++ b/src/input/vcd/libvcd/libvcd/version.h
@@ -0,0 +1 @@
+#define LIBVCD_VERSION "0.7.19-cdio"
diff --git a/src/input/vcd/libvcd/logging.c b/src/input/vcd/libvcd/logging.c
new file mode 100644
index 000000000..dce5b8668
--- /dev/null
+++ b/src/input/vcd/libvcd/logging.c
@@ -0,0 +1,132 @@
+/*
+ $Id: logging.c,v 1.1 2003/10/13 11:47:12 f1rmb Exp $
+
+ Copyright (C) 2000 Herbert Valerio Riedel <hvr@gnu.org>
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+*/
+
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
+
+#include <stdlib.h>
+#include <stdarg.h>
+#include <stdio.h>
+
+/* Public headers */
+#include <libvcd/logging.h>
+
+/* Private headers */
+#include "assert.h"
+
+static const char _rcsid[] = "$Id: logging.c,v 1.1 2003/10/13 11:47:12 f1rmb Exp $";
+
+static void
+default_vcd_log_handler (vcd_log_level_t level, const char message[])
+{
+ switch (level)
+ {
+ case VCD_LOG_ERROR:
+ fprintf (stderr, "**ERROR: %s\n", message);
+ fflush (stderr);
+ exit (EXIT_FAILURE);
+ break;
+ case VCD_LOG_DEBUG:
+ fprintf (stdout, "--DEBUG: %s\n", message);
+ break;
+ case VCD_LOG_WARN:
+ fprintf (stdout, "++ WARN: %s\n", message);
+ break;
+ case VCD_LOG_INFO:
+ fprintf (stdout, " INFO: %s\n", message);
+ break;
+ case VCD_LOG_ASSERT:
+ fprintf (stderr, "!ASSERT: %s\n", message);
+ fflush (stderr);
+ abort ();
+ break;
+ default:
+ vcd_assert_not_reached ();
+ break;
+ }
+
+ fflush (stdout);
+}
+
+static vcd_log_handler_t _handler = default_vcd_log_handler;
+
+vcd_log_handler_t
+vcd_log_set_handler (vcd_log_handler_t new_handler)
+{
+ vcd_log_handler_t old_handler = _handler;
+
+ _handler = new_handler;
+
+ return old_handler;
+}
+
+static void
+vcd_logv (vcd_log_level_t level, const char format[], va_list args)
+{
+ char buf[1024] = { 0, };
+ static int in_recursion = 0;
+
+ if (in_recursion)
+ vcd_assert_not_reached ();
+
+ in_recursion = 1;
+
+ vsnprintf(buf, sizeof(buf)-1, format, args);
+
+ _handler(level, buf);
+
+ in_recursion = 0;
+}
+
+void
+vcd_log (vcd_log_level_t level, const char format[], ...)
+{
+ va_list args;
+ va_start (args, format);
+ vcd_logv (level, format, args);
+ va_end (args);
+}
+
+#define VCD_LOG_TEMPLATE(level, LEVEL) \
+void \
+vcd_ ## level (const char format[], ...) \
+{ \
+ va_list args; \
+ va_start (args, format); \
+ vcd_logv (VCD_LOG_ ## LEVEL, format, args); \
+ va_end (args); \
+}
+
+VCD_LOG_TEMPLATE(debug, DEBUG)
+VCD_LOG_TEMPLATE(info, INFO)
+VCD_LOG_TEMPLATE(warn, WARN)
+VCD_LOG_TEMPLATE(error, ERROR)
+
+#undef VCD_LOG_TEMPLATE
+
+
+/*
+ * Local variables:
+ * c-file-style: "gnu"
+ * tab-width: 8
+ * indent-tabs-mode: nil
+ * End:
+ */
diff --git a/src/input/vcd/libvcd/mpeg.c b/src/input/vcd/libvcd/mpeg.c
new file mode 100644
index 000000000..9b453fb36
--- /dev/null
+++ b/src/input/vcd/libvcd/mpeg.c
@@ -0,0 +1,1177 @@
+/*
+ $Id: mpeg.c,v 1.1 2003/10/13 11:47:12 f1rmb Exp $
+
+ Copyright (C) 2000 Herbert Valerio Riedel <hvr@gnu.org>
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+*/
+
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
+
+#include <stdio.h>
+
+#include <cdio/cdio.h>
+
+/* Private headers */
+#include "bitvec.h"
+#include "mpeg.h"
+#include "util.h"
+
+static const char _rcsid[] = "$Id: mpeg.c,v 1.1 2003/10/13 11:47:12 f1rmb Exp $";
+
+#define MPEG_START_CODE_PATTERN ((uint32_t) 0x00000100)
+#define MPEG_START_CODE_MASK ((uint32_t) 0xffffff00)
+
+#define MPEG_PICTURE_CODE ((uint32_t) 0x00000100)
+/* [...slice codes... 0x1a7] */
+
+#define MPEG_USER_CODE ((uint32_t) 0x000001b2)
+#define MPEG_SEQUENCE_CODE ((uint32_t) 0x000001b3)
+#define MPEG_EXT_CODE ((uint32_t) 0x000001b5)
+#define MPEG_SEQ_END_CODE ((uint32_t) 0x000001b7)
+#define MPEG_GOP_CODE ((uint32_t) 0x000001b8)
+#define MPEG_PROGRAM_END_CODE ((uint32_t) 0x000001b9)
+#define MPEG_PACK_HEADER_CODE ((uint32_t) 0x000001ba)
+#define MPEG_SYSTEM_HEADER_CODE ((uint32_t) 0x000001bb)
+#define MPEG_PRIVATE_1_CODE ((uint32_t) 0x000001bd)
+#define MPEG_PAD_CODE ((uint32_t) 0x000001be)
+
+#define MPEG_AUDIO_C0_CODE ((uint32_t) 0x000001c0) /* default */
+#define MPEG_AUDIO_C1_CODE ((uint32_t) 0x000001c1) /* 2nd audio stream id (dual channel) */
+#define MPEG_AUDIO_C2_CODE ((uint32_t) 0x000001c2) /* 3rd audio stream id (surround sound) */
+
+#define MPEG_VIDEO_E0_CODE ((uint32_t) 0x000001e0) /* motion */
+#define MPEG_VIDEO_E1_CODE ((uint32_t) 0x000001e1) /* lowres still */
+#define MPEG_VIDEO_E2_CODE ((uint32_t) 0x000001e2) /* hires still */
+
+#define PICT_TYPE_I 1
+#define PICT_TYPE_P 2
+#define PICT_TYPE_B 3
+#define PICT_TYPE_D 4
+
+static struct {
+ mpeg_norm_t norm;
+ unsigned hsize;
+ unsigned vsize;
+ int frate_idx;
+} const norm_table[] = {
+ { MPEG_NORM_FILM, 352, 240, 1 },
+ { MPEG_NORM_PAL, 352, 288, 3 },
+ { MPEG_NORM_NTSC, 352, 240, 4 },
+ { MPEG_NORM_PAL_S, 480, 576, 3 },
+ { MPEG_NORM_NTSC_S, 480, 480, 4 },
+ { MPEG_NORM_OTHER, }
+};
+
+static const double frame_rates[16] = {
+ 0.0, 24000.0/1001, 24.0, 25.0,
+ 30000.0/1001, 30.0, 50.0, 60000.0/1001,
+ 60.0, 0.0,
+};
+
+#ifdef DEBUG
+# define MARKER(buf, offset) \
+ vcd_assert (vcd_bitvec_read_bit (buf, offset) == 1)
+#else
+# define MARKER(buf, offset) \
+ { if (GNUC_UNLIKELY (vcd_bitvec_read_bit (buf, offset) != 1)) vcd_debug ("mpeg: some marker is not set..."); }
+#endif
+
+static inline bool
+_start_code_p (uint32_t code)
+{
+ return (code & MPEG_START_CODE_MASK) == MPEG_START_CODE_PATTERN;
+}
+
+static inline int
+_vid_streamid_idx (uint8_t streamid)
+{
+ switch (streamid | MPEG_START_CODE_PATTERN)
+ {
+ case MPEG_VIDEO_E0_CODE:
+ return 0;
+ break;
+
+ case MPEG_VIDEO_E1_CODE:
+ return 1;
+ break;
+
+ case MPEG_VIDEO_E2_CODE:
+ return 2;
+ break;
+
+ default:
+ vcd_assert_not_reached ();
+ break;
+ }
+
+ return -1;
+}
+
+static inline int
+_aud_streamid_idx (uint8_t streamid)
+{
+ switch (streamid | MPEG_START_CODE_PATTERN)
+ {
+ case MPEG_AUDIO_C0_CODE:
+ return 0;
+ break;
+
+ case MPEG_AUDIO_C1_CODE:
+ return 1;
+ break;
+
+ case MPEG_AUDIO_C2_CODE:
+ return 2;
+ break;
+
+ default:
+ vcd_assert_not_reached ();
+ break;
+ }
+
+ return -1;
+}
+
+/* used for SCR, PTS and DTS */
+static inline uint64_t
+_parse_timecode (const uint8_t *buf, unsigned *offset)
+{
+ uint64_t _retval;
+
+ _retval = vcd_bitvec_read_bits (buf, offset, 3);
+
+ MARKER (buf, offset);
+
+ _retval <<= 15;
+ _retval |= vcd_bitvec_read_bits (buf, offset, 15);
+
+ MARKER (buf, offset);
+
+ _retval <<= 15;
+ _retval |= vcd_bitvec_read_bits (buf, offset, 15);
+
+ MARKER (buf, offset);
+
+ return _retval;
+}
+
+static void
+_parse_sequence_header (uint8_t streamid, const uint8_t *buf,
+ VcdMpegStreamCtx *state)
+{
+ unsigned offset = 0;
+ unsigned hsize, vsize, aratio, frate, brate, bufsize, constr;
+ const uint8_t *data = buf;
+ const int vid_idx = _vid_streamid_idx (streamid);
+
+ const double aspect_ratios[16] =
+ {
+ 0.0000, 1.0000, 0.6735, 0.7031,
+ 0.7615, 0.8055, 0.8437, 0.8935,
+ 0.9375, 0.9815, 1.0255, 1.0695,
+ 1.1250, 1.1575, 1.2015, 0.0000
+ };
+
+ if (state->stream.shdr[vid_idx].seen) /* we have it already */
+ return;
+
+ hsize = vcd_bitvec_read_bits (data, &offset, 12);
+
+ vsize = vcd_bitvec_read_bits (data, &offset, 12);
+
+ aratio = vcd_bitvec_read_bits (data, &offset, 4);
+
+ frate = vcd_bitvec_read_bits (data, &offset, 4);
+
+ brate = vcd_bitvec_read_bits (data, &offset, 18);
+
+ MARKER (data, &offset);
+
+ bufsize = vcd_bitvec_read_bits (data, &offset, 10);
+
+ constr = vcd_bitvec_read_bits (data, &offset, 1);
+
+ /* skip intra quantizer matrix */
+
+ if (vcd_bitvec_read_bits (data, &offset, 1))
+ offset += 64 << 3;
+
+ /* skip non-intra quantizer matrix */
+
+ if (vcd_bitvec_read_bits (data, &offset, 1))
+ offset += 64 << 3;
+
+ state->stream.shdr[vid_idx].hsize = hsize;
+ state->stream.shdr[vid_idx].vsize = vsize;
+ state->stream.shdr[vid_idx].aratio = aspect_ratios[aratio];
+ state->stream.shdr[vid_idx].frate = frame_rates[frate];
+ state->stream.shdr[vid_idx].bitrate = 400 * brate;
+ state->stream.shdr[vid_idx].vbvsize = bufsize * 16 * 1024;
+ state->stream.shdr[vid_idx].constrained_flag = (constr != 0);
+
+ state->stream.shdr[vid_idx].seen = true;
+}
+
+static void
+_parse_gop_header (uint8_t streamid, const uint8_t *buf,
+ VcdMpegStreamCtx *state)
+{
+ const uint8_t *data = buf;
+ unsigned offset = 0;
+
+ bool drop_flag;
+ /* bool close_gop; */
+ /* bool broken_link; */
+
+ unsigned hour, minute, second, frame;
+
+ drop_flag = vcd_bitvec_read_bits(data, &offset, 1) != 0;
+
+ hour = vcd_bitvec_read_bits(data, &offset, 5);
+
+ minute = vcd_bitvec_read_bits(data, &offset, 6);
+
+ MARKER (data, &offset);
+
+ second = vcd_bitvec_read_bits(data, &offset, 6);
+
+ frame = vcd_bitvec_read_bits(data, &offset, 6);
+
+ /* close_gop = vcd_bitvec_read_bits(data, &offset, 1) != 0; */
+
+ /* broken_link = vcd_bitvec_read_bits(data, &offset, 1) != 0; */
+
+ state->packet.gop = true;
+ state->packet.gop_timecode.h = hour;
+ state->packet.gop_timecode.m = minute;
+ state->packet.gop_timecode.s = second;
+ state->packet.gop_timecode.f = frame;
+}
+
+static inline void
+_check_scan_data (const char str[], const msf_t *msf,
+ VcdMpegStreamCtx *state)
+{
+ char tmp[16];
+
+ if (state->stream.scan_data_warnings > VCD_MPEG_SCAN_DATA_WARNS)
+ return;
+
+ if (state->stream.scan_data_warnings == VCD_MPEG_SCAN_DATA_WARNS)
+ {
+ vcd_warn ("mpeg user scan data: from now on, scan information "
+ "data errors will not be reported anymore---consider"
+ " enabling the 'update scan offsets' option, "
+ "if it is not enabled already!");
+ state->stream.scan_data_warnings++;
+ return;
+ }
+
+ if (msf->m == 0xff
+ && msf->s == 0xff
+ && msf->f == 0xff)
+ return;
+
+ if ((msf->s & 0x80) == 0
+ || (msf->f & 0x80) == 0)
+ {
+ snprintf (tmp, sizeof (tmp), "%.2x:%.2x.%.2x", msf->m, msf->s, msf->f);
+
+ vcd_warn ("mpeg user scan data: msb of second or frame field "
+ "not set for '%s': [%s]", str, tmp);
+
+ state->stream.scan_data_warnings++;
+
+ return;
+ }
+
+ if ((msf->m >> 4) > 9
+ || ((0x80 ^ msf->s) >> 4) > 9
+ || ((0x80 ^ msf->f) >> 4) > 9
+ || (msf->m & 0xf) > 9
+ || (msf->s & 0xf) > 9
+ || (msf->f & 0xf) > 9)
+ {
+ snprintf (tmp, sizeof (tmp), "%.2x:%.2x.%.2x",
+ msf->m, 0x80 ^ msf->s, 0x80 ^ msf->f);
+
+ vcd_warn ("mpeg user scan data: one or more BCD fields out of range "
+ "for '%s': [%s]", str, tmp);
+
+ state->stream.scan_data_warnings++;
+ }
+}
+
+static void
+_parse_user_data (uint8_t streamid, const void *buf, unsigned len,
+ unsigned offset,
+ VcdMpegStreamCtx *state)
+{
+ unsigned pos = 0;
+ PRAGMA_BEGIN_PACKED
+ struct {
+ uint8_t tag;
+ uint8_t len;
+ uint8_t data[EMPTY_ARRAY_SIZE];
+ } GNUC_PACKED const *udg = buf;
+ PRAGMA_END_PACKED
+
+ if (udg->tag == 0x00) /* if first tag's already 0x00 */
+ {
+ vcd_debug ("strange (possibly non-compliant) user_data seen...");
+ }
+ else while (pos + 2 < len)
+ {
+ if (udg->tag == 0x00)
+ break;
+
+ if (pos + udg->len >= len)
+ break;
+
+ if (udg->len < 2)
+ break;
+
+ switch (udg->tag)
+ {
+ case 0x00:
+ vcd_assert_not_reached ();
+ break;
+
+ case 0x10: /* scan information */
+ {
+ struct vcd_mpeg_scan_data_t *usdi = (void *) udg;
+ vcd_assert (sizeof (struct vcd_mpeg_scan_data_t) == 14);
+
+ if (GNUC_UNLIKELY (usdi->len != 14))
+ {
+ vcd_warn ("invalid user scan data length (%d != 14)", usdi->len);
+ break;
+ }
+
+ vcd_assert (usdi->len == 14);
+ _check_scan_data ("previous_I_offset", &usdi->prev_ofs, state);
+ _check_scan_data ("next_I_offset ", &usdi->next_ofs, state);
+ _check_scan_data ("backward_I_offset", &usdi->back_ofs, state);
+ _check_scan_data ("forward_I_offset ", &usdi->forw_ofs, state);
+
+ state->packet.scan_data_ptr = usdi;
+ state->stream.scan_data++;
+ }
+ break;
+
+ case 0x11: /* closed caption data */
+ vcd_debug ("closed caption data seen -- not supported yet (len = %d)", udg->len);
+ break;
+
+ default:
+ vcd_warn ("unknown user data tag id 0x%.2x encountered", udg->tag);
+ return; /* since we cannot rely on udg->len anymore... */
+ break;
+ }
+
+
+ pos += udg->len;
+ vcd_assert (udg->len >= 2);
+ udg = (void *) &udg->data[udg->len - 2];
+ }
+
+ vcd_assert (pos <= len);
+}
+
+static int
+_analyze_pes_header (const uint8_t *buf, int len,
+ VcdMpegStreamCtx *state)
+{
+ bool _has_pts = false;
+ bool _has_dts = false;
+ int64_t pts = 0;
+ mpeg_vers_t pes_mpeg_ver = MPEG_VERS_INVALID;
+
+ int pos;
+
+ if (vcd_bitvec_peek_bits (buf, 0, 2) == 2) /* %10 - ISO13818-1 */
+ {
+ unsigned pos2 = 0;
+
+ pes_mpeg_ver = MPEG_VERS_MPEG2;
+
+ pos2 += 2;
+
+ pos2 += 2; /* PES_scrambling_control */
+ pos2++; /* PES_priority */
+ pos2++; /* data_alignment_indicator */
+ pos2++; /* copyright */
+ pos2++; /* original_or_copy */
+
+ switch (vcd_bitvec_read_bits (buf, &pos2, 2)) /* PTS_DTS_flags */
+ {
+ case 2: /* %10 */
+ _has_pts = true;
+ break;
+
+ case 3: /* %11 */
+ _has_dts = _has_pts = true;
+ break;
+
+ default:
+ /* NOOP */
+ break;
+ }
+
+ pos2++; /* ESCR_flag */
+
+ pos2++; /* */
+ pos2++; /* */
+ pos2++; /* */
+ pos2++; /* */
+
+ pos2++; /* PES_extension_flag */
+
+ pos = vcd_bitvec_read_bits (buf, &pos2, 8); /* PES_header_data_length */
+ pos += pos2 >> 3;
+
+ if (_has_pts && _has_dts)
+ {
+ vcd_assert (vcd_bitvec_peek_bits (buf, pos2, 4) == 3); /* %0011 */
+ pos2 += 4;
+
+ pts = _parse_timecode (buf, &pos2);
+
+ vcd_assert (vcd_bitvec_peek_bits (buf, pos2, 4) == 1); /* %0001 */
+ pos2 += 4;
+
+ /* dts = */ _parse_timecode (buf, &pos2);
+ }
+ else if (_has_pts)
+ {
+ vcd_assert (vcd_bitvec_peek_bits (buf, pos2, 4) == 2); /* %0010 */
+ pos2 += 4;
+
+ pts = _parse_timecode (buf, &pos2);
+ }
+ }
+ else /* ISO11172-1 */
+ {
+ unsigned pos2 = 0;
+
+ pes_mpeg_ver = MPEG_VERS_MPEG1;
+
+ /* get rid of stuffing bytes */
+ while (((pos2 + 8) < (len << 3))
+ && vcd_bitvec_peek_bits (buf, pos2, 8) == 0xff)
+ pos2 += 8;
+
+ if (vcd_bitvec_peek_bits (buf, pos2, 2) == 1) /* %01 */
+ {
+ pos2 += 2;
+
+ pos2++; /* STD_buffer_scale */
+ pos2 += 13; /* STD_buffer_size */
+ }
+
+ switch (vcd_bitvec_peek_bits (buf, pos2, 4))
+ {
+ case 0x2: /* %0010 */
+ pos2 += 4;
+ _has_pts = true;
+
+ pts = _parse_timecode (buf, &pos2);
+ break;
+
+ case 0x3: /* %0011 */
+ pos2 += 4;
+
+ _has_dts = _has_pts = true;
+ pts = _parse_timecode (buf, &pos2);
+
+ vcd_assert (vcd_bitvec_peek_bits (buf, pos2, 4) == 1); /* %0001 */
+ pos2 += 4;
+
+ /* dts = */ _parse_timecode (buf, &pos2);
+ break;
+
+ case 0x0: /* %0000 */
+ vcd_assert (vcd_bitvec_peek_bits (buf, pos2, 8) == 0x0f);
+ pos2 += 8;
+ break;
+
+ case 0xf: /* %1111 - actually a syntax error! */
+ vcd_assert (vcd_bitvec_peek_bits (buf, pos2, 8) == 0xff);
+ vcd_warn ("Unexpected stuffing byte noticed in ISO11172 PES header!");
+ pos2 += 8;
+ break;
+
+ default:
+ vcd_error ("Error in ISO11172 PES header");
+ break;
+ }
+
+ pos = pos2 >> 3;
+ }
+
+ if (_has_pts)
+ {
+ double pts2;
+
+ pts2 = (double) pts / 90000.0;
+
+ if (!state->stream.seen_pts)
+ {
+ state->stream.max_pts = state->stream.min_pts = pts2;
+ state->stream.seen_pts = true;
+ }
+ else
+ {
+ state->stream.max_pts = MAX (state->stream.max_pts, pts2);
+ state->stream.min_pts = MIN (state->stream.min_pts, pts2);
+ }
+
+ state->packet.has_pts = true;
+ state->packet.pts = pts2;
+ }
+
+ if (state->stream.version != pes_mpeg_ver)
+ vcd_warn ("pack header mpeg version does not match pes header mpeg version");
+
+ return pos;
+}
+
+static void
+_analyze_audio_pes (uint8_t streamid, const uint8_t *buf, int len, bool only_pts,
+ VcdMpegStreamCtx *state)
+{
+ const int aud_idx = _aud_streamid_idx (streamid);
+ unsigned bitpos;
+
+ vcd_assert (aud_idx != -1);
+
+ bitpos = _analyze_pes_header (buf, len, state);
+
+ /* if only pts extraction was needed, we are done here... */
+ if (only_pts)
+ return;
+
+ if (state->stream.ahdr[aud_idx].seen)
+ return;
+
+ bitpos <<= 3;
+
+ while (bitpos <= (len << 3))
+ {
+ unsigned syncword = vcd_bitvec_peek_bits (buf, bitpos, 12);
+
+ if (syncword != 0xfff)
+ {
+ bitpos += 8;
+ continue;
+ }
+
+ bitpos += 12;
+
+ if (GNUC_UNLIKELY (!vcd_bitvec_read_bits (buf, &bitpos, 1)))
+ {
+ vcd_debug ("non-MPEG1 audio stream header seen");
+ break;
+ }
+
+ switch (vcd_bitvec_read_bits (buf, &bitpos, 2)) /* layer */
+ {
+ case 3: /* %11 */
+ state->stream.ahdr[aud_idx].layer = 1;
+ break;
+ case 2: /* %10 */
+ state->stream.ahdr[aud_idx].layer = 2;
+ break;
+ case 1: /* %01 */
+ state->stream.ahdr[aud_idx].layer = 3;
+ break;
+ case 0: /* %00 */
+ state->stream.ahdr[aud_idx].layer = 0;
+ break;
+ }
+
+ bitpos++; /* protection_bit */
+
+ {
+ const int bits = vcd_bitvec_read_bits (buf, &bitpos, 4);
+
+ const unsigned bit_rates[4][16] = {
+ {0, },
+ {0, 32, 64, 96, 128, 160, 192, 224, 256, 288, 320, 352, 384, 416, 448, 0},
+ {0, 32, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320, 384, 0},
+ {0, 32, 40, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320, 0}
+ };
+
+ vcd_assert (IN(state->stream.ahdr[aud_idx].layer, 0, 3));
+ vcd_assert (IN(bits, 0, 15));
+
+ state->stream.ahdr[aud_idx].bitrate = 1024 * bit_rates[state->stream.ahdr[aud_idx].layer][bits];
+ }
+
+ switch (vcd_bitvec_read_bits (buf, &bitpos, 2)) /* sampling_frequency */
+ {
+ case 0: /* %00 */
+ state->stream.ahdr[aud_idx].sampfreq = 44100;
+ break;
+ case 1: /* %01 */
+ state->stream.ahdr[aud_idx].sampfreq = 48000;
+ break;
+ case 2: /* %10 */
+ state->stream.ahdr[aud_idx].sampfreq = 32000;
+ break;
+ case 3: /* %11 */
+ state->stream.ahdr[aud_idx].sampfreq = 0;
+ break;
+ }
+
+ bitpos++; /* padding_bit */
+
+ bitpos++; /* private_bit */
+
+ state->stream.ahdr[aud_idx].mode = 1 + vcd_bitvec_read_bits (buf, &bitpos, 2); /* mode */
+
+ state->stream.ahdr[aud_idx].seen = true;
+
+ /* we got the info, let's jump outta here */
+ break;
+ }
+}
+
+static void
+_analyze_video_pes (uint8_t streamid, const uint8_t *buf, int len, bool only_pts,
+ VcdMpegStreamCtx *state)
+{
+ const int vid_idx = _vid_streamid_idx (streamid);
+
+ int pos, pes_header;
+ int sequence_header_pos = -1;
+ int gop_header_pos = -1;
+ int ipicture_header_pos = -1;
+
+ vcd_assert (vid_idx != -1);
+
+ pes_header = pos = _analyze_pes_header (buf, len, state);
+
+ /* if only pts extraction was needed, we are done here... */
+ if (only_pts)
+ return;
+
+ while (pos + 4 <= len)
+ {
+ uint32_t code = vcd_bitvec_peek_bits32 (buf, pos << 3);
+
+ if (!_start_code_p (code))
+ {
+ pos++;
+ continue;
+ }
+
+ switch (code)
+ {
+ case MPEG_PICTURE_CODE:
+ pos += 4;
+
+ if (vcd_bitvec_peek_bits (buf, (pos << 3) + 10, 3) == 1)
+ ipicture_header_pos = pos;
+ break;
+
+ case MPEG_SEQUENCE_CODE:
+ pos += 4;
+ sequence_header_pos = pos;
+ _parse_sequence_header (streamid, buf + pos, state);
+ break;
+
+ case MPEG_GOP_CODE:
+ pos += 4;
+ if (pos + 4 > len)
+ break;
+ gop_header_pos = pos;
+ _parse_gop_header (streamid, buf + pos, state);
+ state->packet.gop = true;
+ break;
+
+ case MPEG_USER_CODE:
+ pos += 4;
+ if (pos + 4 > len)
+ break;
+ _parse_user_data (streamid, buf + pos, len - pos, pos, state);
+ break;
+
+ case MPEG_EXT_CODE:
+ default:
+ pos += 4;
+ break;
+ }
+ }
+
+ /* decide whether this packet qualifies as access point */
+ state->packet.aps = APS_NONE; /* paranoia */
+
+ if (state->packet.has_pts
+ && ipicture_header_pos != -1)
+ {
+ enum aps_t _aps_type = APS_NONE;
+
+ switch (state->stream.version)
+ {
+ case MPEG_VERS_MPEG1:
+ case MPEG_VERS_MPEG2:
+ if (sequence_header_pos != -1
+ && sequence_header_pos < gop_header_pos
+ && gop_header_pos < ipicture_header_pos)
+ _aps_type = (sequence_header_pos - 4 == pes_header) ? APS_ASGI : APS_SGI;
+ else if (gop_header_pos != 1
+ && gop_header_pos < ipicture_header_pos)
+ _aps_type = APS_GI;
+ else
+ _aps_type = APS_I;
+
+ break;
+
+ default:
+ break;
+ }
+
+ if (_aps_type)
+ {
+ const double pts2 = state->packet.pts;
+
+ if (state->stream.shdr[vid_idx].last_aps_pts > pts2)
+ vcd_warn ("APS' pts seems out of order (actual pts %f, last seen pts %f) "
+ "-- ignoring this aps",
+ pts2, state->stream.shdr[vid_idx].last_aps_pts);
+ else
+ {
+ state->packet.aps_idx = vid_idx;
+ state->packet.aps = _aps_type;
+ state->packet.aps_pts = pts2;
+ state->stream.shdr[vid_idx].last_aps_pts = pts2;
+ }
+ }
+ }
+}
+
+static void
+_register_streamid (uint8_t streamid, VcdMpegStreamCtx *state)
+{
+ const uint32_t code = MPEG_START_CODE_PATTERN | streamid;
+
+ switch (code)
+ {
+ case MPEG_VIDEO_E0_CODE:
+ case MPEG_VIDEO_E1_CODE:
+ case MPEG_VIDEO_E2_CODE:
+ state->packet.video[_vid_streamid_idx (streamid)] = true;
+ break;
+
+ case MPEG_AUDIO_C0_CODE:
+ case MPEG_AUDIO_C1_CODE:
+ case MPEG_AUDIO_C2_CODE:
+ state->packet.audio[_aud_streamid_idx (streamid)] = true;
+ break;
+
+ case MPEG_PAD_CODE:
+ state->packet.padding = true;
+ break;
+
+ case MPEG_SYSTEM_HEADER_CODE:
+ state->packet.system_header = true;
+ break;
+ }
+}
+
+static void
+_analyze_system_header (const uint8_t *buf, int len,
+ VcdMpegStreamCtx *state)
+{
+ unsigned bitpos = 0;
+
+ MARKER (buf, &bitpos);
+
+ bitpos += 22; /* rate_bound */
+
+ MARKER (buf, &bitpos);
+
+ bitpos += 6; /* audio_bound */
+
+ bitpos++; /* fixed_flag */
+ bitpos++; /* CSPS_flag */
+ bitpos++; /* system_audio_lock_flag */
+ bitpos++; /* system_video_lock_flag */
+
+ MARKER (buf, &bitpos);
+
+ bitpos += 5; /* video_bound */
+
+ bitpos += 1; /* packet_rate_restriction_flag -- only ISO 13818-1 */
+ bitpos += 7; /* reserved */
+
+ while (vcd_bitvec_peek_bits (buf, bitpos, 1) == 1
+ && bitpos <= (len << 3))
+ {
+ const uint8_t stream_id = vcd_bitvec_read_bits (buf, &bitpos, 8);
+
+ bitpos += 2; /* %11 */
+
+ bitpos++; /* P-STD_buffer_bound_scale */
+ bitpos += 13; /* P-STD_buffer_size_bound */
+
+ _register_streamid (stream_id, state);
+ }
+
+ vcd_assert (bitpos <= (len << 3));
+}
+
+static void
+_analyze_private_1_stream (const uint8_t *buf, int len,
+ VcdMpegStreamCtx *state)
+{
+ unsigned bitpos = _analyze_pes_header (buf, len, state);
+ int ogt_idx = -1;
+
+ uint8_t private_data_id;
+
+ bitpos <<= 3;
+
+ private_data_id = vcd_bitvec_read_bits (buf, &bitpos, 8);
+
+ switch (private_data_id)
+ {
+ uint8_t sub_stream_id;
+
+ case 0x00:
+ case 0x01:
+ case 0x02:
+ case 0x03:
+ /* CVD subs */
+ ogt_idx = private_data_id;
+
+ if (!state->stream.ogt[ogt_idx])
+ vcd_debug ("Assuming CVD-style subtitles for data_id 0x%.2x in private stream 1", ogt_idx);
+
+ break;
+
+ case 0x70:
+ /* SVCD OGT */
+ sub_stream_id = vcd_bitvec_read_bits (buf, &bitpos, 8);
+
+ if (sub_stream_id < 4)
+ {
+ ogt_idx = sub_stream_id;
+ if (!state->stream.ogt[ogt_idx])
+ vcd_debug ("subtitles detect for channel 0x%.2x", ogt_idx);
+ }
+ else
+ vcd_warn ("sub_stream_id out of range (0x%.2x)", sub_stream_id);
+ break;
+
+ default:
+ vcd_warn ("unknown private_data_id for private stream 1 seen (0x%.2x)",
+ private_data_id);
+ return;
+ break;
+ }
+
+ if (ogt_idx >= 0)
+ state->stream.ogt[ogt_idx] = state->packet.ogt[ogt_idx] = true;
+}
+
+int
+vcd_mpeg_parse_packet (const void *_buf, unsigned buflen, bool parse_pes,
+ VcdMpegStreamCtx *ctx)
+{
+ const uint8_t *buf = _buf;
+ int pos;
+
+ vcd_assert (buf != NULL);
+ vcd_assert (ctx != NULL);
+
+ /* clear packet info */
+ memset (&(ctx->packet), 0, sizeof (ctx->packet));
+
+ ctx->stream.packets++;
+
+ for (pos = 0; pos < buflen && !buf[pos]; pos++);
+
+ if (pos == buflen)
+ {
+ ctx->packet.zero = true;
+ return buflen;
+ }
+
+ /* verify the packet begins with a pack header */
+ if (vcd_bitvec_peek_bits32 (buf, 0) != MPEG_PACK_HEADER_CODE)
+ {
+ const uint32_t _code = vcd_bitvec_peek_bits32 (buf, 0);
+
+ vcd_warn ("mpeg scan: pack header code (0x%8.8x) expected, "
+ "but 0x%8.8x found (buflen = %d)",
+ (unsigned int) MPEG_PACK_HEADER_CODE,
+ (unsigned int) _code, buflen);
+
+ ctx->stream.packets--;
+
+ if (!ctx->stream.packets)
+ {
+ if (_code == MPEG_SEQUENCE_CODE)
+ vcd_warn ("...this looks like a elementary video stream"
+ " but a multiplexed program stream was required.");
+
+ if ((0xfff00000 & _code) == 0xfff00000)
+ vcd_warn ("...this looks like a elementary audio stream"
+ " but a multiplexed program stream was required.");
+
+ if (_code == 0x52494646)
+ vcd_warn ("...this looks like a RIFF header"
+ " but a plain multiplexed program stream was required.");
+ }
+ else if (_code == MPEG_PROGRAM_END_CODE)
+ vcd_warn ("...PEM (program end marker) found instead of pack header;"
+ " should be in last 4 bytes of pack");
+
+ return 0;
+ }
+
+ /* take a look at the pack header */
+ pos = 0;
+
+ while (pos + 4 <= buflen)
+ {
+ uint32_t code = vcd_bitvec_peek_bits32 (buf, pos << 3);
+
+ /* skip zero bytes... */
+ if (!code)
+ {
+ pos += (pos + 4 == buflen) ? 4 : 2;
+ continue;
+ }
+
+ /* continue until start code seen */
+ if (!_start_code_p (code))
+ {
+ pos++;
+ continue;
+ }
+
+ switch (code)
+ {
+ uint16_t size;
+ int bits;
+ unsigned bitpos;
+
+ case MPEG_PACK_HEADER_CODE:
+ if (pos)
+ return pos;
+
+ pos += 4;
+
+ bitpos = pos << 3;
+ bits = vcd_bitvec_peek_bits (buf, bitpos, 4);
+
+ if (bits == 0x2) /* %0010 ISO11172-1 */
+ {
+ uint64_t _scr;
+ uint32_t _muxrate;
+
+ bitpos += 4;
+
+ if (!ctx->stream.version)
+ ctx->stream.version = MPEG_VERS_MPEG1;
+
+ if (ctx->stream.version != MPEG_VERS_MPEG1)
+ vcd_warn ("mixed mpeg versions?");
+
+ _scr = _parse_timecode (buf, &bitpos);
+
+ MARKER (buf, &bitpos);
+
+ _muxrate = vcd_bitvec_read_bits (buf, &bitpos, 22);
+
+ MARKER (buf, &bitpos);
+
+ vcd_assert (bitpos % 8 == 0);
+ pos = bitpos >> 3;
+
+ ctx->packet.scr = _scr;
+ ctx->stream.muxrate = ctx->packet.muxrate = _muxrate * 50 * 8;
+ }
+ else if (bits >> 2 == 0x1) /* %01xx ISO13818-1 */
+ {
+ uint64_t _scr;
+ uint32_t _muxrate;
+ int tmp;
+
+ bitpos += 2;
+
+ if (!ctx->stream.version)
+ ctx->stream.version = MPEG_VERS_MPEG2;
+
+ if (ctx->stream.version != MPEG_VERS_MPEG2)
+ vcd_warn ("mixed mpeg versions?");
+
+ _scr = _parse_timecode (buf, &bitpos);
+
+ _scr *= 300;
+ _scr += vcd_bitvec_read_bits (buf, &bitpos, 9); /* SCR ext */
+
+ MARKER (buf, &bitpos);
+
+ _muxrate = vcd_bitvec_read_bits (buf, &bitpos, 22);
+
+ MARKER (buf, &bitpos);
+ MARKER (buf, &bitpos);
+
+ bitpos += 5; /* reserved */
+
+ tmp = vcd_bitvec_read_bits (buf, &bitpos, 3) << 3;
+
+ bitpos += tmp;
+
+ vcd_assert (bitpos % 8 == 0);
+ pos = bitpos >> 3;
+
+ ctx->packet.scr = _scr;
+ ctx->stream.muxrate = ctx->packet.muxrate = _muxrate * 50 * 8;
+ }
+ else
+ {
+ vcd_warn ("packet not recognized as either version 1 or 2 (%d)"
+ " -- assuming v1", bits);
+ }
+
+ break;
+
+ case MPEG_SYSTEM_HEADER_CODE:
+ case MPEG_PAD_CODE:
+ case MPEG_PRIVATE_1_CODE:
+ case MPEG_VIDEO_E0_CODE:
+ case MPEG_VIDEO_E1_CODE:
+ case MPEG_VIDEO_E2_CODE:
+ case MPEG_AUDIO_C0_CODE:
+ case MPEG_AUDIO_C1_CODE:
+ case MPEG_AUDIO_C2_CODE:
+ pos += 4;
+ size = vcd_bitvec_peek_bits16 (buf, pos << 3);
+ pos += 2;
+
+ if (pos + size > buflen)
+ {
+ vcd_warn ("packet length beyond buffer"
+ " (pos = %d + size = %d > buflen = %d) "
+ "-- stream may be truncated or packet length > 2324 bytes!",
+ pos, size, buflen);
+ ctx->stream.packets--;
+ return 0;
+ }
+
+ _register_streamid (code & 0xff, ctx);
+
+ switch (code)
+ {
+ case MPEG_SYSTEM_HEADER_CODE:
+ _analyze_system_header (buf + pos, size, ctx);
+ break;
+
+ case MPEG_VIDEO_E0_CODE:
+ case MPEG_VIDEO_E1_CODE:
+ case MPEG_VIDEO_E2_CODE:
+ _analyze_video_pes (code & 0xff, buf + pos, size, !parse_pes, ctx);
+ break;
+
+ case MPEG_AUDIO_C0_CODE:
+ case MPEG_AUDIO_C1_CODE:
+ case MPEG_AUDIO_C2_CODE:
+ _analyze_audio_pes (code & 0xff, buf + pos, size, !parse_pes, ctx);
+ break;
+
+ case MPEG_PRIVATE_1_CODE:
+ _analyze_private_1_stream (buf + pos, size, ctx);
+ break;
+ }
+
+ pos += size;
+ break;
+
+ case MPEG_PROGRAM_END_CODE:
+ ctx->packet.pem = true;
+ pos += 4;
+ break;
+
+ case MPEG_PICTURE_CODE:
+ pos += 3;
+ break;
+
+ default:
+ vcd_debug ("unexpected start code 0x%8.8x", code);
+ pos += 4;
+ break;
+ }
+ }
+
+ if (pos != buflen)
+ vcd_debug ("pos != buflen (%d != %d)", pos, buflen); /* fixme? */
+
+ return buflen;
+}
+
+mpeg_norm_t
+vcd_mpeg_get_norm (const struct vcd_mpeg_stream_vid_info *_info)
+{
+ int i;
+
+ for (i = 0; norm_table[i].norm != MPEG_NORM_OTHER;i++)
+ if (norm_table[i].hsize == _info->hsize
+ && norm_table[i].vsize == _info->vsize
+ && frame_rates[norm_table[i].frate_idx] == _info->frate)
+ break;
+
+ return norm_table[i].norm;
+}
+
+enum vcd_mpeg_packet_type
+vcd_mpeg_packet_get_type (const struct vcd_mpeg_packet_info *_info)
+{
+ if (_info->video[0]
+ || _info->video[1]
+ || _info->video[2])
+ return PKT_TYPE_VIDEO;
+ else if (_info->audio[0]
+ || _info->audio[1]
+ || _info->audio[2])
+ return PKT_TYPE_AUDIO;
+ else if (_info->zero)
+ return PKT_TYPE_ZERO;
+ else if (_info->ogt[0]
+ || _info->ogt[1]
+ || _info->ogt[2]
+ || _info->ogt[3])
+ return PKT_TYPE_OGT;
+ else if (_info->system_header || _info->padding)
+ return PKT_TYPE_EMPTY;
+
+ return PKT_TYPE_INVALID;
+}
+
+
+/*
+ * Local variables:
+ * c-file-style: "gnu"
+ * tab-width: 8
+ * indent-tabs-mode: nil
+ * End:
+ */
diff --git a/src/input/vcd/libvcd/mpeg.h b/src/input/vcd/libvcd/mpeg.h
new file mode 100644
index 000000000..cfe8a294f
--- /dev/null
+++ b/src/input/vcd/libvcd/mpeg.h
@@ -0,0 +1,194 @@
+/*
+ $Id: mpeg.h,v 1.1 2003/10/13 11:47:12 f1rmb Exp $
+
+ Copyright (C) 2000 Herbert Valerio Riedel <hvr@gnu.org>
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+*/
+
+#ifndef __VCD_MPEG_H__
+#define __VCD_MPEG_H__
+
+#include <string.h>
+
+/* Public headers */
+#include <libvcd/logging.h>
+#include <libvcd/types.h>
+
+/* Private headers */
+#include "data_structures.h"
+
+typedef enum {
+ MPEG_VERS_INVALID = 0,
+ MPEG_VERS_MPEG1 = 1,
+ MPEG_VERS_MPEG2 = 2
+} mpeg_vers_t;
+
+PRAGMA_BEGIN_PACKED
+struct vcd_mpeg_scan_data_t {
+ uint8_t tag;
+ uint8_t len;
+ msf_t prev_ofs;
+ msf_t next_ofs;
+ msf_t back_ofs;
+ msf_t forw_ofs;
+} GNUC_PACKED;
+PRAGMA_END_PACKED
+
+#define struct_vcd_mpeg_scan_data_t_SIZEOF 14
+
+#define VCD_MPEG_SCAN_DATA_WARNS 8
+
+typedef struct {
+ struct vcd_mpeg_packet_info {
+ bool video[3];
+ bool audio[3];
+ bool ogt[4];
+
+ bool padding;
+ bool pem;
+ bool zero;
+ bool system_header;
+
+ struct vcd_mpeg_scan_data_t *scan_data_ptr; /* points into actual packet memory! */
+
+ enum aps_t {
+ APS_NONE = 0,
+ APS_I, /* iframe */
+ APS_GI, /* gop + iframe */
+ APS_SGI, /* sequence + gop + iframe */
+ APS_ASGI /* aligned sequence + gop + iframe */
+ } aps;
+ double aps_pts;
+ int aps_idx;
+
+ bool has_pts;
+ double pts;
+
+ uint64_t scr;
+ unsigned muxrate;
+
+ bool gop;
+ struct {
+ uint8_t h, m, s, f;
+ } gop_timecode;
+ } packet;
+
+ struct vcd_mpeg_stream_info {
+ unsigned packets;
+
+ mpeg_vers_t version;
+
+ bool ogt[4];
+
+ struct vcd_mpeg_stream_vid_info {
+ bool seen;
+ unsigned hsize;
+ unsigned vsize;
+ double aratio;
+ double frate;
+ unsigned bitrate;
+ unsigned vbvsize;
+ bool constrained_flag;
+
+ VcdList *aps_list; /* filled up by vcd_mpeg_source */
+ double last_aps_pts; /* temp, see ->packet */
+
+ } shdr[3];
+
+ struct vcd_mpeg_stream_aud_info {
+ bool seen;
+ unsigned layer;
+ unsigned bitrate;
+ unsigned sampfreq;
+ enum {
+ MPEG_STEREO = 1,
+ MPEG_JOINT_STEREO,
+ MPEG_DUAL_CHANNEL,
+ MPEG_SINGLE_CHANNEL
+ } mode;
+ } ahdr[3];
+
+ unsigned muxrate;
+
+ bool seen_pts;
+ double min_pts;
+ double max_pts;
+
+ double playing_time;
+
+ unsigned scan_data;
+ unsigned scan_data_warnings;
+ } stream;
+} VcdMpegStreamCtx;
+
+int
+vcd_mpeg_parse_packet (const void *buf, unsigned buflen, bool parse_pes,
+ VcdMpegStreamCtx *ctx);
+
+typedef enum {
+ MPEG_NORM_OTHER,
+ MPEG_NORM_PAL,
+ MPEG_NORM_NTSC,
+ MPEG_NORM_FILM,
+ MPEG_NORM_PAL_S,
+ MPEG_NORM_NTSC_S
+} mpeg_norm_t;
+
+mpeg_norm_t
+vcd_mpeg_get_norm (const struct vcd_mpeg_stream_vid_info *_info);
+
+enum vcd_mpeg_packet_type {
+ PKT_TYPE_INVALID = 0,
+ PKT_TYPE_VIDEO,
+ PKT_TYPE_AUDIO,
+ PKT_TYPE_OGT,
+ PKT_TYPE_ZERO,
+ PKT_TYPE_EMPTY
+};
+
+enum vcd_mpeg_packet_type
+vcd_mpeg_packet_get_type (const struct vcd_mpeg_packet_info *_info);
+
+struct vcd_mpeg_stream_vid_type {
+ enum {
+ VID_TYPE_NONE = 0,
+ VID_TYPE_MOTION,
+ VID_TYPE_STILL
+ } type;
+ enum {
+ VID_NORM_OTHER = 0,
+ VID_NORM_PAL,
+ VID_NORM_NTSC
+ } norm;
+ enum {
+ VID_RES_OTHER = 0,
+ VID_RES_SIF,
+ VID_RES_HALF_D1,
+ VID_RES_2_3_D1,
+ VID_RES_FULL_D2
+ } resolution;
+};
+
+#endif /* __VCD_MPEG_H__ */
+
+
+/*
+ * Local variables:
+ * c-file-style: "gnu"
+ * tab-width: 8
+ * indent-tabs-mode: nil
+ * End:
+ */
diff --git a/src/input/vcd/libvcd/mpeg_stream.c b/src/input/vcd/libvcd/mpeg_stream.c
new file mode 100644
index 000000000..3cff87dc8
--- /dev/null
+++ b/src/input/vcd/libvcd/mpeg_stream.c
@@ -0,0 +1,487 @@
+/*
+ $Id: mpeg_stream.c,v 1.1 2003/10/13 11:47:12 f1rmb Exp $
+
+ Copyright (C) 2000 Herbert Valerio Riedel <hvr@gnu.org>
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+*/
+
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
+
+#include <string.h>
+#include <stdio.h>
+#include <stdlib.h>
+
+#include <cdio/cdio.h>
+
+#include <libvcd/logging.h>
+
+/* Private headers */
+#include "assert.h"
+#include "mpeg_stream.h"
+#include "bytesex.h"
+#include "data_structures.h"
+#include "mpeg.h"
+#include "util.h"
+
+static const char _rcsid[] = "$Id: mpeg_stream.c,v 1.1 2003/10/13 11:47:12 f1rmb Exp $";
+
+struct _VcdMpegSource
+{
+ VcdDataSource *data_source;
+
+ bool scanned;
+
+ /* _get_packet cache */
+ unsigned _read_pkt_pos;
+ unsigned _read_pkt_no;
+
+ struct vcd_mpeg_stream_info info;
+};
+
+/*
+ * access functions
+ */
+
+VcdMpegSource *
+vcd_mpeg_source_new (VcdDataSource *mpeg_file)
+{
+ VcdMpegSource *new_obj;
+
+ vcd_assert (mpeg_file != NULL);
+
+ new_obj = _vcd_malloc (sizeof (VcdMpegSource));
+
+ new_obj->data_source = mpeg_file;
+ new_obj->scanned = false;
+
+ return new_obj;
+}
+
+void
+vcd_mpeg_source_destroy (VcdMpegSource *obj, bool destroy_file_obj)
+{
+ int i;
+ vcd_assert (obj != NULL);
+
+ if (destroy_file_obj)
+ vcd_data_source_destroy (obj->data_source);
+
+ for (i = 0; i < 3; i++)
+ if (obj->info.shdr[i].aps_list)
+ _vcd_list_free (obj->info.shdr[i].aps_list, true);
+
+ free (obj);
+}
+
+const struct vcd_mpeg_stream_info *
+vcd_mpeg_source_get_info (VcdMpegSource *obj)
+{
+ vcd_assert (obj != NULL);
+
+ vcd_assert (obj->scanned);
+
+ return &(obj->info);
+}
+
+long
+vcd_mpeg_source_stat (VcdMpegSource *obj)
+{
+ vcd_assert (obj != NULL);
+ vcd_assert (!obj->scanned);
+
+ return obj->info.packets * 2324;
+}
+
+void
+vcd_mpeg_source_scan (VcdMpegSource *obj, bool strict_aps, bool fix_scan_info,
+ vcd_mpeg_prog_cb_t callback, void *user_data)
+{
+ unsigned length = 0;
+ unsigned pos = 0;
+ unsigned pno = 0;
+ unsigned padbytes = 0;
+ unsigned padpackets = 0;
+ VcdMpegStreamCtx state;
+ VcdListNode *n;
+ vcd_mpeg_prog_info_t _progress = { 0, };
+
+ vcd_assert (obj != NULL);
+
+ if (obj->scanned)
+ {
+ vcd_debug ("already scanned... not rescanning");
+ return;
+ }
+
+ vcd_assert (!obj->scanned);
+
+ memset (&state, 0, sizeof (state));
+
+ if (fix_scan_info)
+ state.stream.scan_data_warnings = VCD_MPEG_SCAN_DATA_WARNS + 1;
+
+ vcd_data_source_seek (obj->data_source, 0);
+ length = vcd_data_source_stat (obj->data_source);
+
+ if (callback)
+ {
+ _progress.length = length;
+ callback (&_progress, user_data);
+ }
+
+
+ while (pos < length)
+ {
+ char buf[2324] = { 0, };
+ int read_len = MIN (sizeof (buf), (length - pos));
+ int pkt_len;
+
+ vcd_data_source_read (obj->data_source, buf, read_len, 1);
+
+ pkt_len = vcd_mpeg_parse_packet (buf, read_len, true, &state);
+
+ if (!pkt_len)
+ {
+ if (!pno)
+ vcd_error ("input mpeg stream has been deemed invalid -- aborting");
+
+ vcd_warn ("bad packet at packet #%d (stream byte offset %d)"
+ " -- remaining %d bytes of stream will be ignored",
+ pno, pos, length - pos);
+
+ pos = length; /* don't fall into assert... */
+ break;
+ }
+
+ if (callback && (pos - _progress.current_pos) > (length / 100))
+ {
+ _progress.current_pos = pos;
+ _progress.current_pack = pno;
+ callback (&_progress, user_data);
+ }
+
+ switch (state.packet.aps)
+ {
+ case APS_NONE:
+ break;
+
+ case APS_I:
+ case APS_GI:
+ if (strict_aps)
+ break; /* allow only if now strict aps */
+
+ case APS_SGI:
+ case APS_ASGI:
+ {
+ struct aps_data *_data = _vcd_malloc (sizeof (struct aps_data));
+
+ _data->packet_no = pno;
+ _data->timestamp = state.packet.aps_pts;
+
+ if (!state.stream.shdr[state.packet.aps_idx].aps_list)
+ state.stream.shdr[state.packet.aps_idx].aps_list = _vcd_list_new ();
+
+ _vcd_list_append (state.stream.shdr[state.packet.aps_idx].aps_list, _data);
+ }
+ break;
+
+ default:
+ vcd_assert_not_reached ();
+ break;
+ }
+
+ pos += pkt_len;
+ pno++;
+
+ if (pkt_len != read_len)
+ {
+ padbytes += (2324 - pkt_len);
+
+ if (!padpackets)
+ vcd_warn ("mpeg stream will be padded on the fly -- hope that's ok for you!");
+
+ padpackets++;
+
+ vcd_data_source_seek (obj->data_source, pos);
+ }
+ }
+
+ vcd_data_source_close (obj->data_source);
+
+ if (callback)
+ {
+ _progress.current_pos = pos;
+ _progress.current_pack = pno;
+ callback (&_progress, user_data);
+ }
+
+ vcd_assert (pos == length);
+
+ obj->info = state.stream;
+ obj->scanned = true;
+
+ obj->info.playing_time = obj->info.max_pts - obj->info.min_pts;
+
+ if (obj->info.min_pts)
+ vcd_debug ("pts start offset %f (max pts = %f)",
+ obj->info.min_pts, obj->info.max_pts);
+
+ vcd_debug ("playing time %f", obj->info.playing_time);
+
+ if (!state.stream.scan_data && state.stream.version == MPEG_VERS_MPEG2)
+ vcd_warn ("mpeg stream contained no scan information (user) data");
+
+ {
+ int i;
+
+ for (i = 0; i < 3; i++)
+ if (obj->info.shdr[i].aps_list)
+ _VCD_LIST_FOREACH (n, obj->info.shdr[i].aps_list)
+ {
+ struct aps_data *_data = _vcd_list_node_data (n);
+
+ _data->timestamp -= obj->info.min_pts;
+ }
+ }
+
+ if (padpackets)
+ vcd_warn ("autopadding requires to insert additional %d zero bytes"
+ " into MPEG stream (due to %d unaligned packets of %d total)",
+ padbytes, padpackets, state.stream.packets);
+
+ obj->info.version = state.stream.version;
+}
+
+static double
+_approx_pts (VcdList *aps_list, uint32_t packet_no)
+{
+ double retval = 0;
+ VcdListNode *node;
+
+ struct aps_data *_laps = NULL;
+
+ double last_pts_ratio = 0;
+
+ _VCD_LIST_FOREACH (node, aps_list)
+ {
+ struct aps_data *_aps = _vcd_list_node_data (node);
+
+ if (_laps)
+ {
+ long p = _aps->packet_no;
+ double t = _aps->timestamp;
+
+ p -= _laps->packet_no;
+ t -= _laps->timestamp;
+
+ last_pts_ratio = t / p;
+ }
+
+ if (_aps->packet_no >= packet_no)
+ break;
+
+ _laps = _aps;
+ }
+
+ retval = packet_no;
+ retval -= _laps->packet_no;
+ retval *= last_pts_ratio;
+ retval += _laps->timestamp;
+
+ return retval;
+}
+
+static void
+_set_scan_msf (msf_t *_msf, long lsn)
+{
+ if (lsn == -1)
+ {
+ _msf->m = _msf->s = _msf->f = 0xff;
+ return;
+ }
+
+ cdio_lba_to_msf (lsn, _msf);
+ _msf->s |= 0x80;
+ _msf->f |= 0x80;
+}
+
+static void
+_fix_scan_info (struct vcd_mpeg_scan_data_t *scan_data_ptr,
+ unsigned packet_no, double pts, VcdList *aps_list)
+{
+ VcdListNode *node;
+ long _next = -1, _prev = -1, _forw = -1, _back = -1;
+
+ _VCD_LIST_FOREACH (node, aps_list)
+ {
+ struct aps_data *_aps = _vcd_list_node_data (node);
+
+ if (_aps->packet_no == packet_no)
+ continue;
+ else if (_aps->packet_no < packet_no)
+ {
+ _prev = _aps->packet_no;
+
+ if (pts - _aps->timestamp < 10 && _back == -1)
+ _back = _aps->packet_no;
+ }
+ else if (_aps->packet_no > packet_no)
+ {
+ if (_next == -1)
+ _next = _aps->packet_no;
+
+ if (_aps->timestamp - pts < 10)
+ _forw = _aps->packet_no;
+ }
+ }
+
+ if (_back == -1)
+ _back = packet_no;
+
+ if (_forw == -1)
+ _forw = packet_no;
+
+ _set_scan_msf (&scan_data_ptr->prev_ofs, _prev);
+ _set_scan_msf (&scan_data_ptr->next_ofs, _next);
+ _set_scan_msf (&scan_data_ptr->back_ofs, _back);
+ _set_scan_msf (&scan_data_ptr->forw_ofs, _forw);
+}
+
+int
+vcd_mpeg_source_get_packet (VcdMpegSource *obj, unsigned long packet_no,
+ void *packet_buf, struct vcd_mpeg_packet_info *flags,
+ bool fix_scan_info)
+{
+ unsigned length;
+ unsigned pos;
+ unsigned pno;
+ VcdMpegStreamCtx state;
+
+ vcd_assert (obj != NULL);
+ vcd_assert (obj->scanned);
+ vcd_assert (packet_buf != NULL);
+
+ if (packet_no >= obj->info.packets)
+ {
+ vcd_error ("invalid argument");
+ return -1;
+ }
+
+ if (packet_no < obj->_read_pkt_no)
+ {
+ vcd_warn ("rewinding mpeg stream...");
+ obj->_read_pkt_no = 0;
+ obj->_read_pkt_pos = 0;
+ }
+
+ memset (&state, 0, sizeof (state));
+ state.stream.seen_pts = true;
+ state.stream.min_pts = obj->info.min_pts;
+ state.stream.scan_data_warnings = VCD_MPEG_SCAN_DATA_WARNS + 1;
+
+ pos = obj->_read_pkt_pos;
+ pno = obj->_read_pkt_no;
+ length = vcd_data_source_stat (obj->data_source);
+
+ vcd_data_source_seek (obj->data_source, pos);
+
+ while (pos < length)
+ {
+ char buf[2324] = { 0, };
+ int read_len = MIN (sizeof (buf), (length - pos));
+ int pkt_len;
+
+ vcd_data_source_read (obj->data_source, buf, read_len, 1);
+
+ pkt_len = vcd_mpeg_parse_packet (buf, read_len,
+ fix_scan_info, &state);
+
+ vcd_assert (pkt_len > 0);
+
+ if (pno == packet_no)
+ {
+ /* optimized for sequential access,
+ thus save pointer to next mpeg pack */
+ obj->_read_pkt_pos = pos + pkt_len;
+ obj->_read_pkt_no = pno + 1;
+
+ if (fix_scan_info
+ && state.packet.scan_data_ptr
+ && obj->info.version == MPEG_VERS_MPEG2)
+ {
+ int vid_idx = 0;
+ double _pts;
+
+ if (state.packet.video[2])
+ vid_idx = 2;
+ else if (state.packet.video[1])
+ vid_idx = 1;
+ else
+ vid_idx = 0;
+
+ if (state.packet.has_pts)
+ _pts = state.packet.pts - obj->info.min_pts;
+ else
+ _pts = _approx_pts (obj->info.shdr[vid_idx].aps_list, packet_no);
+
+ _fix_scan_info (state.packet.scan_data_ptr, packet_no,
+ _pts, obj->info.shdr[vid_idx].aps_list);
+ }
+
+ memset (packet_buf, 0, 2324);
+ memcpy (packet_buf, buf, pkt_len);
+
+ if (flags)
+ {
+ *flags = state.packet;
+ flags->pts -= obj->info.min_pts;
+ }
+
+ return 0; /* breaking out */
+ }
+
+ pos += pkt_len;
+ pno++;
+
+ if (pkt_len != read_len)
+ vcd_data_source_seek (obj->data_source, pos);
+ }
+
+ vcd_assert (pos == length);
+
+ vcd_error ("shouldnt be reached...");
+
+ return -1;
+}
+
+void
+vcd_mpeg_source_close (VcdMpegSource *obj)
+{
+ vcd_assert (obj != NULL);
+
+ vcd_data_source_close (obj->data_source);
+}
+
+
+/*
+ * Local variables:
+ * c-file-style: "gnu"
+ * tab-width: 8
+ * indent-tabs-mode: nil
+ * End:
+ */
diff --git a/src/input/vcd/libvcd/mpeg_stream.h b/src/input/vcd/libvcd/mpeg_stream.h
new file mode 100644
index 000000000..68c51cc55
--- /dev/null
+++ b/src/input/vcd/libvcd/mpeg_stream.h
@@ -0,0 +1,116 @@
+/*
+ $Id: mpeg_stream.h,v 1.1 2003/10/13 11:47:12 f1rmb Exp $
+
+ Copyright (C) 2000 Herbert Valerio Riedel <hvr@gnu.org>
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+*/
+
+#ifndef __VCD_MPEG_STREAM__
+#define __VCD_MPEG_STREAM__
+
+#include <libvcd/types.h>
+
+/* Private includes */
+#include "stream.h"
+#include "data_structures.h"
+#include "mpeg.h"
+
+#define MPEG_PACKET_SIZE 2324
+
+typedef struct _VcdMpegSource VcdMpegSource;
+
+/* used in APS list */
+
+struct aps_data
+{
+ uint32_t packet_no;
+ double timestamp;
+};
+
+/* enums */
+
+typedef enum {
+ MPEG_AUDIO_NOSTREAM = 0,
+ MPEG_AUDIO_1STREAM = 1,
+ MPEG_AUDIO_2STREAM = 2,
+ MPEG_AUDIO_EXT_STREAM = 3
+} mpeg_audio_t;
+
+typedef enum {
+ MPEG_VIDEO_NOSTREAM = 0,
+ MPEG_VIDEO_NTSC_STILL = 1,
+ MPEG_VIDEO_NTSC_STILL2 = 2,
+ MPEG_VIDEO_NTSC_MOTION = 3,
+
+ MPEG_VIDEO_PAL_STILL = 5,
+ MPEG_VIDEO_PAL_STILL2 = 6,
+ MPEG_VIDEO_PAL_MOTION = 7
+} mpeg_video_t;
+
+/* mpeg stream info */
+
+struct vcd_mpeg_stream_info;
+
+/* mpeg packet info */
+
+struct vcd_mpeg_packet_info;
+
+/* access functions */
+
+VcdMpegSource *
+vcd_mpeg_source_new (VcdDataSource *mpeg_file);
+
+/* scan the mpeg file... needed to be called only once */
+typedef struct {
+ long current_pack;
+ long current_pos;
+ long length;
+} vcd_mpeg_prog_info_t;
+
+typedef int (*vcd_mpeg_prog_cb_t) (const vcd_mpeg_prog_info_t *progress_info,
+ void *user_data);
+
+void
+vcd_mpeg_source_scan (VcdMpegSource *obj, bool strict_aps, bool fix_scan_info,
+ vcd_mpeg_prog_cb_t callback, void *user_data);
+
+/* gets the packet at given position */
+int
+vcd_mpeg_source_get_packet (VcdMpegSource *obj, unsigned long packet_no,
+ void *packet_buf, struct vcd_mpeg_packet_info *flags,
+ bool fix_scan_info);
+
+void
+vcd_mpeg_source_close (VcdMpegSource *obj);
+
+const struct vcd_mpeg_stream_info *
+vcd_mpeg_source_get_info (VcdMpegSource *obj);
+
+long
+vcd_mpeg_source_stat (VcdMpegSource *obj);
+
+void
+vcd_mpeg_source_destroy (VcdMpegSource *obj, bool destroy_file_obj);
+
+#endif /* __VCD_MPEG_STREAM__ */
+
+/*
+ * Local variables:
+ * c-file-style: "gnu"
+ * tab-width: 8
+ * indent-tabs-mode: nil
+ * End:
+ */
diff --git a/src/input/vcd/libvcd/obj.h b/src/input/vcd/libvcd/obj.h
new file mode 100644
index 000000000..3efa86833
--- /dev/null
+++ b/src/input/vcd/libvcd/obj.h
@@ -0,0 +1,202 @@
+/*
+ $Id: obj.h,v 1.1 2003/10/13 11:47:12 f1rmb Exp $
+
+ Copyright (C) 2000 Herbert Valerio Riedel <hvr@gnu.org>
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+*/
+
+#ifndef __VCD_OBJ_H__
+#define __VCD_OBJ_H__
+
+#include <cdio/iso9660.h>
+#include <libvcd/files.h>
+
+/* Private headers */
+#include "data_structures.h"
+#include "directory.h"
+#include "image_sink.h"
+#include "mpeg_stream.h"
+#include "salloc.h"
+#include "vcd.h"
+
+typedef struct {
+ double time;
+ struct aps_data aps;
+ char *id;
+} entry_t;
+
+typedef struct {
+ double time;
+ char *id;
+} pause_t;
+
+typedef struct {
+ VcdMpegSource *source;
+ char *id;
+ const struct vcd_mpeg_stream_info *info;
+
+ VcdList *pause_list; /* pause_t */
+
+ char *default_entry_id;
+ VcdList *entry_list; /* entry_t */
+
+ /* pbc ref check */
+ bool referenced;
+
+ /* computed on sector allocation */
+ unsigned relative_start_extent; /* relative to iso data end */
+} mpeg_sequence_t;
+
+/* work in progress -- fixme rename all occurences */
+#define mpeg_track_t mpeg_sequence_t
+#define mpeg_track_list mpeg_sequence_list
+
+typedef struct {
+ VcdMpegSource *source;
+ char *id;
+ const struct vcd_mpeg_stream_info *info;
+
+ VcdList *pause_list; /* pause_t */
+
+ /* pbc ref check */
+ bool referenced;
+
+ /* computed through info */
+ unsigned segment_count;
+
+ /* computed on sector allocation */
+ unsigned start_extent;
+} mpeg_segment_t;
+
+
+typedef struct {
+ char *iso_pathname;
+ VcdDataSource *file;
+ bool raw_flag;
+
+ uint32_t size;
+ uint32_t start_extent;
+ uint32_t sectors;
+} custom_file_t;
+
+struct _VcdObj {
+ vcd_type_t type;
+
+ /* VCD 3.0 chinese SVCD compat flags */
+ bool svcd_vcd3_mpegav;
+ bool svcd_vcd3_entrysvd;
+ bool svcd_vcd3_tracksvd;
+ bool svcd_vcd3_spiconsv;
+
+ bool update_scan_offsets;
+ bool relaxed_aps;
+
+ unsigned leadout_pregap;
+ unsigned track_pregap;
+ unsigned track_front_margin;
+ unsigned track_rear_margin;
+
+ /* output */
+ VcdImageSink *image_sink;
+
+ /* ... */
+ unsigned iso_size;
+ char *iso_volume_label;
+ char *iso_publisher_id;
+ char *iso_application_id;
+ char *iso_preparer_id;
+
+ char *info_album_id;
+ unsigned info_volume_count;
+ unsigned info_volume_number;
+ unsigned info_restriction;
+ bool info_use_seq2;
+ bool info_use_lid2;
+
+ /* input */
+ unsigned mpeg_segment_start_extent;
+ VcdList *mpeg_segment_list; /* mpeg_segment_t */
+
+ VcdList *mpeg_sequence_list; /* mpeg_sequence_t */
+
+ unsigned relative_end_extent; /* last mpeg sequence track end extent */
+
+ /* PBC */
+ VcdList *pbc_list; /* pbc_t */
+ unsigned psd_size;
+ unsigned psdx_size;
+
+ /* custom files */
+ unsigned ext_file_start_extent;
+ unsigned custom_file_start_extent;
+ VcdList *custom_file_list; /* custom_file_t */
+ VcdList *custom_dir_list; /* char */
+
+ /* dictionary */
+ VcdList *buffer_dict_list;
+
+ /* aggregates */
+ VcdSalloc *iso_bitmap;
+
+ VcdDirectory *dir;
+
+ /* state info */
+ bool in_output;
+
+ unsigned sectors_written;
+ unsigned in_track;
+
+ long last_cb_call;
+
+ progress_callback_t progress_callback;
+ void *callback_user_data;
+};
+
+/* private functions */
+
+mpeg_sequence_t *
+_vcd_obj_get_sequence_by_id (VcdObj *obj, const char sequence_id[]);
+
+mpeg_sequence_t *
+_vcd_obj_get_sequence_by_entry_id (VcdObj *obj, const char entry_id[]);
+
+mpeg_segment_t *
+_vcd_obj_get_segment_by_id (VcdObj *obj, const char segment_id[]);
+
+enum vcd_capability_t {
+ _CAP_VALID,
+ _CAP_MPEG1,
+ _CAP_MPEG2,
+ _CAP_PBC,
+ _CAP_PBC_X,
+ _CAP_TRACK_MARGINS,
+ _CAP_4C_SVCD,
+ _CAP_PAL_BITS
+};
+
+bool
+_vcd_obj_has_cap_p (const VcdObj *obj, enum vcd_capability_t capability);
+
+#endif /* __VCD_OBJ_H__ */
+
+
+/*
+ * Local variables:
+ * c-file-style: "gnu"
+ * tab-width: 8
+ * indent-tabs-mode: nil
+ * End:
+ */
diff --git a/src/input/vcd/libvcd/pbc.c b/src/input/vcd/libvcd/pbc.c
new file mode 100644
index 000000000..4b30a34f2
--- /dev/null
+++ b/src/input/vcd/libvcd/pbc.c
@@ -0,0 +1,878 @@
+/*
+ $Id: pbc.c,v 1.1 2003/10/13 11:47:12 f1rmb Exp $
+
+ Copyright (C) 2000 Herbert Valerio Riedel <hvr@gnu.org>
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+*/
+
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
+
+#include <string.h>
+#include <stddef.h>
+#include <math.h>
+
+#include <cdio/cdio.h>
+
+/* Public headers */
+#include <libvcd/logging.h>
+#include <libvcd/files.h>
+#include <libvcd/types.h>
+
+/* FIXME! Make this really private. */
+#include <libvcd/files_private.h>
+
+/* Private headers */
+#include "assert.h"
+#include "bytesex.h"
+#include "obj.h"
+#include "pbc.h"
+#include "util.h"
+
+static const char _rcsid[] = "$Id: pbc.c,v 1.1 2003/10/13 11:47:12 f1rmb Exp $";
+
+static uint8_t
+_wtime (int seconds)
+{
+ if (seconds < 0)
+ return 255;
+
+ if (seconds <= 60)
+ return seconds;
+
+ if (seconds <= 2000)
+ {
+ double _tmp;
+
+ _tmp = seconds;
+ _tmp -= 60;
+ _tmp /= 10;
+ _tmp += 60;
+
+ return rint (_tmp);
+ }
+
+ vcd_warn ("wait time of %ds clipped to 2000s", seconds);
+
+ return 254;
+}
+
+static pbc_t *
+_vcd_pbc_byid(const VcdObj *obj, const char item_id[])
+{
+ VcdListNode *node;
+
+ _VCD_LIST_FOREACH (node, obj->pbc_list)
+ {
+ pbc_t *_pbc = _vcd_list_node_data (node);
+
+ if (_pbc->id && !strcmp (item_id, _pbc->id))
+ return _pbc;
+ }
+
+ /* not found */
+ return NULL;
+}
+
+unsigned
+_vcd_pbc_lid_lookup (const VcdObj *obj, const char item_id[])
+{
+ VcdListNode *node;
+ unsigned n = 1;
+
+ _VCD_LIST_FOREACH (node, obj->pbc_list)
+ {
+ pbc_t *_pbc = _vcd_list_node_data (node);
+
+ vcd_assert (n < 0x8000);
+
+ if (_pbc->id && !strcmp (item_id, _pbc->id))
+ return n;
+
+ n++;
+ }
+
+ /* not found */
+ return 0;
+}
+
+static void
+_set_area_helper (pbc_area_t *dest, const pbc_area_t *src, const char sel_id[])
+{
+ memset (dest, 0, sizeof (pbc_area_t));
+
+ if (src)
+ {
+ if (src->x1 || src->x2 || src->y1 || src->y2) /* not disabled */
+ {
+ if (src->x1 >= src->x2)
+ vcd_error ("selection '%s': area x1 >= x2 (%d >= %d)",
+ sel_id, src->x1, src->x2);
+
+ if (src->y1 >= src->y2)
+ vcd_error ("selection '%s': area y1 >= y2 (%d >= %d)",
+ sel_id, src->y1, src->y2);
+ }
+
+ *dest = *src;
+ }
+}
+
+enum item_type_t
+_vcd_pbc_lookup (const VcdObj *obj, const char item_id[])
+{
+ unsigned id;
+
+ vcd_assert (item_id != NULL);
+
+ if ((id = _vcd_pbc_pin_lookup (obj, item_id)))
+ {
+ if (id < 2)
+ return ITEM_TYPE_NOTFOUND;
+ else if (id < 100)
+ return ITEM_TYPE_TRACK;
+ else if (id < 600)
+ return ITEM_TYPE_ENTRY;
+ else if (id < 2980)
+ return ITEM_TYPE_SEGMENT;
+ else
+ vcd_assert_not_reached ();
+ }
+ else if (_vcd_pbc_lid_lookup (obj, item_id))
+ return ITEM_TYPE_PBC;
+
+ return ITEM_TYPE_NOTFOUND;
+}
+
+uint16_t
+_vcd_pbc_pin_lookup (const VcdObj *obj, const char item_id[])
+{
+ int n;
+ VcdListNode *node;
+
+ if (!item_id)
+ return 0;
+
+ /* check sequence items */
+
+ n = 0;
+ _VCD_LIST_FOREACH (node, obj->mpeg_sequence_list)
+ {
+ mpeg_sequence_t *_sequence = _vcd_list_node_data (node);
+
+ vcd_assert (n < 98);
+
+ if (_sequence->id && !strcmp (item_id, _sequence->id))
+ return n + 2;
+
+ n++;
+ }
+
+ /* check entry points */
+
+ n = 0;
+ _VCD_LIST_FOREACH (node, obj->mpeg_sequence_list)
+ {
+ mpeg_sequence_t *_sequence = _vcd_list_node_data (node);
+ VcdListNode *node2;
+
+ /* default entry point */
+
+ if (_sequence->default_entry_id
+ && !strcmp (item_id, _sequence->default_entry_id))
+ return n + 100;
+ n++;
+
+ /* additional entry points */
+
+ _VCD_LIST_FOREACH (node2, _sequence->entry_list)
+ {
+ entry_t *_entry = _vcd_list_node_data (node2);
+
+ vcd_assert (n < 500);
+
+ if (_entry->id && !strcmp (item_id, _entry->id))
+ return n + 100;
+
+ n++;
+ }
+ }
+
+ /* check sequence items */
+
+ n = 0;
+ _VCD_LIST_FOREACH (node, obj->mpeg_segment_list)
+ {
+ mpeg_segment_t *_segment = _vcd_list_node_data (node);
+
+ vcd_assert (n < 1980);
+
+ if (_segment->id && !strcmp (item_id, _segment->id))
+ return n + 1000;
+
+ n += _segment->segment_count;
+ }
+
+ return 0;
+}
+
+bool
+_vcd_pbc_available (const VcdObj *obj)
+{
+ vcd_assert (obj != NULL);
+ vcd_assert (obj->pbc_list != NULL);
+
+ if (!_vcd_list_length (obj->pbc_list))
+ return false;
+
+ if (!_vcd_obj_has_cap_p (obj, _CAP_PBC))
+ {
+ vcd_warn ("PBC list not empty but VCD type not capable of PBC!");
+ return false;
+ }
+
+ return true;
+}
+
+uint16_t
+_vcd_pbc_max_lid (const VcdObj *obj)
+{
+ uint16_t retval = 0;
+
+ if (_vcd_pbc_available (obj))
+ retval = _vcd_list_length (obj->pbc_list);
+
+ return retval;
+}
+
+static size_t
+_vcd_pbc_node_length (const VcdObj *obj, const pbc_t *_pbc, bool extended)
+{
+ size_t retval = 0;
+
+ if (extended)
+ vcd_assert (_vcd_obj_has_cap_p (obj, _CAP_PBC_X));
+
+ switch (_pbc->type)
+ {
+ int n;
+
+ case PBC_PLAYLIST:
+ n = _vcd_list_length (_pbc->item_id_list);
+ retval = __cd_offsetof (_PsdPlayListDescriptor, itemid[n]);
+ break;
+
+ case PBC_SELECTION:
+ n = _vcd_list_length (_pbc->select_id_list);
+
+ retval = __cd_offsetof (PsdSelectionListDescriptor, ofs[n]);
+
+ if (extended || _vcd_obj_has_cap_p (obj, _CAP_4C_SVCD))
+ retval += __cd_offsetof (PsdSelectionListDescriptorExtended, area[n]);
+ break;
+
+ case PBC_END:
+ retval = sizeof (PsdEndListDescriptor);
+ break;
+
+ default:
+ vcd_assert_not_reached ();
+ break;
+ }
+
+ return retval;
+}
+
+static uint16_t
+_lookup_psd_offset (const VcdObj *obj, const char item_id[], bool extended)
+{
+ VcdListNode *node;
+
+ if (extended)
+ vcd_assert (_vcd_obj_has_cap_p (obj, _CAP_PBC_X));
+
+ /* disable it */
+ if (!item_id)
+ return PSD_OFS_DISABLED;
+
+ _VCD_LIST_FOREACH (node, obj->pbc_list)
+ {
+ pbc_t *_pbc = _vcd_list_node_data (node);
+
+ if (!_pbc->id || strcmp (item_id, _pbc->id))
+ continue;
+
+ return (extended ? _pbc->offset_ext : _pbc->offset) / INFO_OFFSET_MULT;
+ }
+
+ vcd_error ("PSD: referenced PSD '%s' not found", item_id);
+
+ /* not found */
+ return PSD_OFS_DISABLED;
+}
+
+static void
+_vcd_pin_mark_id (const VcdObj *obj, const char _id[])
+{
+ mpeg_sequence_t *_seq;
+ mpeg_segment_t *_seg;
+
+ vcd_assert (obj != NULL);
+
+ if (!_id)
+ return;
+
+ if ((_seq = _vcd_obj_get_sequence_by_id ((VcdObj *) obj, _id)))
+ _seq->referenced = true;
+
+ if ((_seg = _vcd_obj_get_segment_by_id ((VcdObj *) obj, _id)))
+ _seg->referenced = true;
+}
+
+static void
+_vcd_pbc_mark_id (const VcdObj *obj, const char _id[])
+{
+ pbc_t *_pbc;
+
+ vcd_assert (obj != NULL);
+
+ if (!_id)
+ return;
+
+ _pbc = _vcd_pbc_byid (obj, _id);
+
+ if (!_pbc) /* not found */
+ return;
+
+ if (_pbc->referenced) /* already marked */
+ return;
+
+ _pbc->referenced = true;
+
+ switch (_pbc->type)
+ {
+ case PBC_PLAYLIST:
+ {
+ VcdListNode *node;
+
+ _vcd_pbc_mark_id (obj, _pbc->prev_id);
+ _vcd_pbc_mark_id (obj, _pbc->next_id);
+ _vcd_pbc_mark_id (obj, _pbc->retn_id);
+
+ _VCD_LIST_FOREACH (node, _pbc->item_id_list)
+ {
+ const char *_id = _vcd_list_node_data (node);
+
+ _vcd_pin_mark_id (obj, _id);
+ }
+ }
+ break;
+
+ case PBC_SELECTION:
+ {
+ VcdListNode *node;
+
+ _vcd_pbc_mark_id (obj, _pbc->prev_id);
+ _vcd_pbc_mark_id (obj, _pbc->next_id);
+ _vcd_pbc_mark_id (obj, _pbc->retn_id);
+
+ if (_pbc->selection_type == _SEL_NORMAL)
+ _vcd_pbc_mark_id (obj, _pbc->default_id);
+
+ _vcd_pbc_mark_id (obj, _pbc->timeout_id);
+
+ _vcd_pin_mark_id (obj, _pbc->item_id);
+
+ _VCD_LIST_FOREACH (node, _pbc->select_id_list)
+ {
+ const char *_id = _vcd_list_node_data (node);
+
+ _vcd_pbc_mark_id (obj, _id);
+ }
+ }
+ break;
+
+ case PBC_END:
+ _vcd_pin_mark_id (obj, _pbc->image_id);
+ break;
+
+ default:
+ vcd_assert_not_reached ();
+ break;
+ }
+}
+
+void
+_vcd_pbc_check_unreferenced (const VcdObj *obj)
+{
+ VcdListNode *node;
+
+ /* clear all flags */
+
+ _VCD_LIST_FOREACH (node, obj->pbc_list)
+ {
+ pbc_t *_pbc = _vcd_list_node_data (node);
+
+ _pbc->referenced = false;
+ }
+
+ _VCD_LIST_FOREACH (node, obj->mpeg_sequence_list)
+ {
+ mpeg_sequence_t *_sequence = _vcd_list_node_data (node);
+
+ _sequence->referenced = false;
+ }
+
+ _VCD_LIST_FOREACH (node, obj->mpeg_segment_list)
+ {
+ mpeg_segment_t *_segment = _vcd_list_node_data (node);
+
+ _segment->referenced = false;
+ }
+
+ /* start from non-rejected lists */
+
+ _VCD_LIST_FOREACH (node, obj->pbc_list)
+ {
+ pbc_t *_pbc = _vcd_list_node_data (node);
+
+ vcd_assert (_pbc->id != NULL);
+
+ if (_pbc->rejected)
+ continue;
+
+ _vcd_pbc_mark_id (obj, _pbc->id);
+ }
+
+ /* collect flags */
+
+ _VCD_LIST_FOREACH (node, obj->pbc_list)
+ {
+ pbc_t *_pbc = _vcd_list_node_data (node);
+
+ if (!_pbc->referenced)
+ vcd_warn ("PSD item '%s' is unreachable", _pbc->id);
+ }
+
+ _VCD_LIST_FOREACH (node, obj->mpeg_sequence_list)
+ {
+ mpeg_sequence_t *_sequence = _vcd_list_node_data (node);
+
+ if (!_sequence->referenced)
+ vcd_warn ("sequence '%s' is not reachable by PBC", _sequence->id);
+ }
+
+ _VCD_LIST_FOREACH (node, obj->mpeg_segment_list)
+ {
+ mpeg_segment_t *_segment = _vcd_list_node_data (node);
+
+ if (!_segment->referenced)
+ vcd_warn ("segment item '%s' is unreachable", _segment->id);
+ }
+
+}
+
+void
+_vcd_pbc_node_write (const VcdObj *obj, const pbc_t *_pbc, void *buf,
+ bool extended)
+{
+ vcd_assert (obj != NULL);
+ vcd_assert (_pbc != NULL);
+ vcd_assert (buf != NULL);
+
+ if (extended)
+ vcd_assert (_vcd_obj_has_cap_p (obj, _CAP_PBC_X));
+
+ switch (_pbc->type)
+ {
+ case PBC_PLAYLIST:
+ {
+ _PsdPlayListDescriptor *_md = buf;
+ VcdListNode *node;
+ int n;
+
+ _md->type = PSD_TYPE_PLAY_LIST;
+ _md->noi = _vcd_list_length (_pbc->item_id_list);
+
+ vcd_assert (_pbc->lid < 0x8000);
+ _md->lid = uint16_to_be (_pbc->lid | (_pbc->rejected ? 0x8000 : 0));
+
+ _md->prev_ofs =
+ uint16_to_be (_lookup_psd_offset (obj, _pbc->prev_id, extended));
+ _md->next_ofs =
+ uint16_to_be (_lookup_psd_offset (obj, _pbc->next_id, extended));
+ _md->return_ofs =
+ uint16_to_be (_lookup_psd_offset (obj, _pbc->retn_id, extended));
+ _md->ptime = uint16_to_be (rint (_pbc->playing_time * 15.0));
+ _md->wtime = _wtime (_pbc->wait_time);
+ _md->atime = _wtime (_pbc->auto_pause_time);
+
+ n = 0;
+ _VCD_LIST_FOREACH (node, _pbc->item_id_list)
+ {
+ const char *_id = _vcd_list_node_data (node);
+ uint16_t _pin;
+
+ if (_id)
+ {
+ _pin = _vcd_pbc_pin_lookup (obj, _id);
+
+ if (!_pin)
+ vcd_error ("PSD: referenced play item '%s' not found", _id);
+
+ _md->itemid[n] = uint16_to_be (_pin);
+ }
+ else
+ _md->itemid[n] = 0; /* play nothing */
+
+ n++;
+ }
+ }
+ break;
+
+ case PBC_SELECTION:
+ {
+ PsdSelectionListDescriptor *_md = buf;
+
+ const unsigned _nos = _vcd_list_length (_pbc->select_id_list);
+
+ if (extended)
+ _md->type = PSD_TYPE_EXT_SELECTION_LIST;
+ else
+ _md->type = PSD_TYPE_SELECTION_LIST;
+
+ if (!IN (_pbc->bsn, 1, 99))
+ vcd_error ("selection '%s': BSN (%d) not in range [1..99]",
+ _pbc->id, _pbc->bsn);
+
+ if (!IN (_nos, 0, 99))
+ vcd_error ("selection '%s': too many selections (%d > 99)",
+ _pbc->id, _nos);
+
+ if (_nos + _pbc->bsn > 100)
+ vcd_error ("selection '%s': BSN + NOS (%d + %d) > 100",
+ _pbc->id, _pbc->bsn, _nos);
+
+ _md->bsn = _pbc->bsn;
+ _md->nos = _nos;
+
+ vcd_assert (sizeof (PsdSelectionListFlags) == 1);
+
+ /* selection flags */
+ if (_vcd_obj_has_cap_p (obj, _CAP_4C_SVCD))
+ _md->flags.SelectionAreaFlag = true;
+ else
+ _md->flags.SelectionAreaFlag = false;
+
+ _md->flags.CommandListFlag = false;
+
+ vcd_assert (_pbc->lid < 0x8000);
+ _md->lid = uint16_to_be (_pbc->lid | (_pbc->rejected ? 0x8000 : 0));
+
+ _md->prev_ofs =
+ uint16_to_be (_lookup_psd_offset (obj, _pbc->prev_id, extended));
+ _md->next_ofs =
+ uint16_to_be (_lookup_psd_offset (obj, _pbc->next_id, extended));
+ _md->return_ofs =
+ uint16_to_be (_lookup_psd_offset (obj, _pbc->retn_id, extended));
+
+ switch (_pbc->selection_type)
+ {
+ case _SEL_NORMAL:
+ _md->default_ofs =
+ uint16_to_be (_lookup_psd_offset (obj, _pbc->default_id, extended));
+ break;
+
+ case _SEL_MULTI_DEF:
+ _md->default_ofs = uint16_to_be (PSD_OFS_MULTI_DEF);
+ if (_pbc->default_id)
+ vcd_warn ("ignoring default target '%s' for multi default selection '%s'",
+ _pbc->default_id, _pbc->id);
+ break;
+
+ case _SEL_MULTI_DEF_NO_NUM:
+ _md->default_ofs = uint16_to_be (PSD_OFS_MULTI_DEF_NO_NUM);
+ if (_pbc->default_id)
+ vcd_warn ("ignoring default target '%s' for multi default (w/o num) selection '%s'",
+ _pbc->default_id, _pbc->id);
+ break;
+
+ default:
+ vcd_assert_not_reached ();
+ break;
+ }
+
+ _md->timeout_ofs =
+ uint16_to_be (_lookup_psd_offset (obj, _pbc->timeout_id, extended));
+ _md->totime = _wtime (_pbc->timeout_time);
+
+ if (_pbc->loop_count > 0x7f)
+ vcd_warn ("loop count %d > 127", _pbc->loop_count);
+
+ _md->loop = (_pbc->loop_count > 0x7f) ? 0x7f : _pbc->loop_count;
+
+ if (_pbc->jump_delayed)
+ _md->loop |= 0x80;
+
+ /* timeout related sanity checks */
+ if (_pbc->loop_count > 0
+ && _pbc->timeout_time >= 0
+ && !_pbc->timeout_id
+ && !_nos)
+ vcd_warn ("PSD: selection '%s': neither timeout nor select target available, but neither loop count is infinite nor timeout wait time", _pbc->id);
+
+ if (_pbc->timeout_id && (_pbc->timeout_time < 0 || _pbc->loop_count <= 0))
+ vcd_warn ("PSD: selection '%s': timeout target '%s' is never used due to loop count or timeout wait time given", _pbc->id, _pbc->timeout_id);
+
+ if (_pbc->item_id)
+ {
+ const uint16_t _pin = _vcd_pbc_pin_lookup (obj, _pbc->item_id);
+
+ if (!_pin)
+ vcd_error ("PSD: referenced play item '%s' not found", _pbc->item_id);
+
+ _md->itemid = uint16_to_be (_pin);
+ }
+ else
+ _md->itemid = 0; /* play nothing */
+
+ /* sanity checks */
+ switch (_pbc->selection_type)
+ {
+ case _SEL_NORMAL:
+ break;
+
+ case _SEL_MULTI_DEF:
+ case _SEL_MULTI_DEF_NO_NUM:
+ if (_pbc->jump_delayed)
+ vcd_warn ("selection '%s': jump timing shall be immediate", _pbc->id);
+
+ if (_pbc->bsn != 1)
+ vcd_error ("selection '%s': BSN != 1 for multi default selection",
+ _pbc->id);
+
+ /* checking NOS == NOE */
+ if (!_pbc->item_id)
+ vcd_error ("selection '%s': play nothing play item not allowed for multidefault list",
+ _pbc->id);
+
+ {
+ mpeg_sequence_t *_seq;
+
+ if ((_seq = _vcd_obj_get_sequence_by_id ((VcdObj *) obj, _pbc->item_id))
+ || (_seq = _vcd_obj_get_sequence_by_entry_id ((VcdObj *) obj, _pbc->item_id)))
+ {
+ const unsigned _entries = _vcd_list_length (_seq->entry_list) + 1;
+
+ if (_nos != _entries)
+ vcd_error ("selection '%s': number of entrypoints"
+ " (%d for sequence '%s') != number of selections (%d)",
+ _pbc->id, _entries, _pbc->item_id, _nos);
+ }
+ else
+ vcd_error ("selection '%s': play item '%s'"
+ " is requried to be sequence or entry point"
+ " item for multi default selecton",
+ _pbc->id, _pbc->item_id);
+ }
+
+ break;
+
+ default:
+ vcd_assert_not_reached ();
+ break;
+ }
+
+ /* fill selection array */
+ {
+ VcdListNode *node = NULL;
+ int idx = 0;
+
+ idx = 0;
+ _VCD_LIST_FOREACH (node, _pbc->select_id_list)
+ {
+ const char *_id = _vcd_list_node_data (node);
+
+ _md->ofs[idx] =
+ uint16_to_be (_lookup_psd_offset (obj, _id, extended));
+
+ idx++;
+ }
+ }
+
+ if (extended || _vcd_obj_has_cap_p (obj, _CAP_4C_SVCD))
+ {
+ PsdSelectionListDescriptorExtended *_md2;
+ VcdListNode *node;
+ int n;
+
+ /* append extended selection areas */
+
+ _md2 = (void *) &_md->ofs[_nos];
+
+ _set_area_helper (&_md2->next_area, _pbc->next_area, _pbc->id);
+ _set_area_helper (&_md2->prev_area, _pbc->prev_area, _pbc->id);
+ _set_area_helper (&_md2->return_area, _pbc->return_area, _pbc->id);
+
+ _set_area_helper (&_md2->default_area, _pbc->default_area, _pbc->id);
+
+ n = 0;
+ if (_pbc->select_area_list)
+ _VCD_LIST_FOREACH (node, _pbc->select_area_list)
+ {
+ const pbc_area_t *_area = _vcd_list_node_data (node);
+
+ _set_area_helper (&_md2->area[n], _area, _pbc->id);
+
+ n++;
+ }
+
+ vcd_assert (n == _nos);
+ }
+ }
+ break;
+
+ case PBC_END:
+ {
+ PsdEndListDescriptor *_md = buf;
+
+ _md->type = PSD_TYPE_END_LIST;
+
+ if (_vcd_obj_has_cap_p (obj, _CAP_4C_SVCD))
+ {
+ _md->next_disc = _pbc->next_disc;
+
+ if (_pbc->image_id)
+ {
+ uint16_t _pin = _vcd_pbc_pin_lookup (obj, _pbc->image_id);
+ mpeg_segment_t *_segment;
+
+ if (!_pbc->next_disc)
+ vcd_warn ("PSD: endlist '%s': change disc picture given,"
+ " but next volume is 0", _pbc->id);
+
+ if (!_pin)
+ vcd_error ("PSD: referenced play item '%s' not found",
+ _pbc->item_id);
+
+ _md->change_pic = uint16_to_be (_pin);
+
+ /* sanity checks */
+
+ _segment = _vcd_obj_get_segment_by_id ((VcdObj *) obj,
+ _pbc->image_id);
+
+ if (!_segment)
+ vcd_warn ("PSD: endlist '%s': referenced play item '%s'"
+ " is not a segment play item",
+ _pbc->id, _pbc->image_id);
+ else if (_segment->info->shdr[0].seen
+ || !(_segment->info->shdr[1].seen || _segment->info->shdr[2].seen))
+ vcd_warn ("PSD: endlist '%s': referenced play item '%s'"
+ " should be a still picture",
+ _pbc->id, _pbc->image_id);
+ }
+ }
+ else if (_pbc->next_disc || _pbc->image_id)
+ vcd_warn ("extended end list attributes ignored for non-SVCD");
+ }
+ break;
+
+ default:
+ vcd_assert_not_reached ();
+ break;
+ }
+}
+
+pbc_t *
+vcd_pbc_new (enum pbc_type_t type)
+{
+ pbc_t *_pbc;
+
+ _pbc = _vcd_malloc (sizeof (pbc_t));
+ _pbc->type = type;
+
+ switch (type)
+ {
+ case PBC_PLAYLIST:
+ _pbc->item_id_list = _vcd_list_new ();
+ break;
+
+ case PBC_SELECTION:
+ _pbc->select_id_list = _vcd_list_new ();
+ _pbc->select_area_list = _vcd_list_new ();
+ break;
+
+ case PBC_END:
+ break;
+
+ default:
+ vcd_assert_not_reached ();
+ break;
+ }
+
+ return _pbc;
+}
+
+/*
+ */
+
+bool
+_vcd_pbc_finalize (VcdObj *obj)
+{
+ VcdListNode *node;
+ unsigned offset = 0, offset_ext = 0;
+ unsigned lid;
+
+ lid = 1;
+ _VCD_LIST_FOREACH (node, obj->pbc_list)
+ {
+ pbc_t *_pbc = _vcd_list_node_data (node);
+ unsigned length, length_ext = 0;
+
+ length = _vcd_pbc_node_length (obj, _pbc, false);
+ if (_vcd_obj_has_cap_p (obj, _CAP_PBC_X))
+ length_ext = _vcd_pbc_node_length (obj, _pbc, true);
+
+ /* round them up to... */
+ length = _vcd_ceil2block (length, INFO_OFFSET_MULT);
+ if (_vcd_obj_has_cap_p (obj, _CAP_PBC_X))
+ length_ext = _vcd_ceil2block (length_ext, INFO_OFFSET_MULT);
+
+ /* node may not cross sector boundary! */
+ offset = _vcd_ofs_add (offset, length, ISO_BLOCKSIZE);
+ if (_vcd_obj_has_cap_p (obj, _CAP_PBC_X))
+ offset_ext = _vcd_ofs_add (offset_ext, length_ext, ISO_BLOCKSIZE);
+
+ /* save start offsets */
+ _pbc->offset = offset - length;
+ if (_vcd_obj_has_cap_p (obj, _CAP_PBC_X))
+ _pbc->offset_ext = offset_ext - length_ext;
+
+ _pbc->lid = lid;
+
+ lid++;
+ }
+
+ obj->psd_size = offset;
+ if (_vcd_obj_has_cap_p (obj, _CAP_PBC_X))
+ obj->psdx_size = offset_ext;
+
+ vcd_debug ("pbc: psd size %d (extended psd %d)", offset, offset_ext);
+
+ return true;
+}
diff --git a/src/input/vcd/libvcd/pbc.h b/src/input/vcd/libvcd/pbc.h
new file mode 100644
index 000000000..29c6f5081
--- /dev/null
+++ b/src/input/vcd/libvcd/pbc.h
@@ -0,0 +1,157 @@
+/*
+ $Id: pbc.h,v 1.1 2003/10/13 11:47:12 f1rmb Exp $
+
+ Copyright (C) 2000 Herbert Valerio Riedel <hvr@gnu.org>
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+*/
+
+#ifndef __VCD_PBC_H__
+#define __VCD_PBC_H__
+
+#include <libvcd/types.h>
+
+/* Private includes */
+#include "data_structures.h"
+#include "util.h"
+#include "vcd.h"
+
+enum pbc_type_t {
+ PBC_INVALID = 0,
+ PBC_PLAYLIST,
+ PBC_SELECTION,
+ PBC_END
+};
+
+typedef struct psd_area_t pbc_area_t; /* fixme */
+#define pbc_area_t_SIZEOF struct_psd_area_t_SIZEOF
+
+static inline pbc_area_t *
+vcd_pbc_area_new (uint8_t x1, uint8_t y1, uint8_t x2, uint8_t y2)
+{
+ pbc_area_t *_new_area = _vcd_malloc (sizeof (pbc_area_t));
+
+ _new_area->x1 = x1;
+ _new_area->y1 = y1;
+ _new_area->x2 = x2;
+ _new_area->y2 = y2;
+
+ return _new_area;
+}
+
+/* typedef struct _pbc_t pbc_t; */
+
+struct _pbc_t {
+ enum pbc_type_t type;
+
+ char *id;
+
+ bool rejected;
+
+ /* pbc ref check */
+ bool referenced;
+
+ /* used for play/selection lists */
+ char *prev_id;
+ char *next_id;
+ char *retn_id;
+
+ /* used for play lists */
+ double playing_time;
+ int wait_time;
+ int auto_pause_time;
+ VcdList *item_id_list; /* char */
+
+ /* used for selection lists */
+ enum selection_type_t {
+ _SEL_NORMAL = 0,
+ _SEL_MULTI_DEF,
+ _SEL_MULTI_DEF_NO_NUM
+ } selection_type;
+
+ pbc_area_t *prev_area;
+ pbc_area_t *next_area;
+ pbc_area_t *return_area;
+ pbc_area_t *default_area; /* depends on selection_type */
+ VcdList *select_area_list; /* pbc_area_t */
+
+ unsigned bsn;
+ char *default_id;
+ char *timeout_id;
+ int timeout_time;
+ unsigned loop_count;
+ bool jump_delayed;
+ char *item_id;
+ VcdList *select_id_list; /* char */
+
+ /* used for end lists */
+ char *image_id;
+ unsigned next_disc;
+
+ /* computed values */
+ unsigned lid;
+ unsigned offset;
+ unsigned offset_ext;
+};
+
+enum item_type_t {
+ ITEM_TYPE_NOTFOUND = 0,
+ ITEM_TYPE_NOOP,
+ ITEM_TYPE_TRACK,
+ ITEM_TYPE_ENTRY,
+ ITEM_TYPE_SEGMENT,
+ ITEM_TYPE_PBC
+};
+
+/* functions */
+
+pbc_t *
+vcd_pbc_new (enum pbc_type_t type);
+
+pbc_t *
+_vcd_pbc_init (pbc_t *_pbc);
+
+void
+vcd_pbc_destroy (pbc_t *obj);
+
+unsigned
+_vcd_pbc_lid_lookup (const VcdObj *obj, const char item_id[]);
+
+enum item_type_t
+_vcd_pbc_lookup (const VcdObj *obj, const char item_id[]);
+
+uint16_t
+_vcd_pbc_pin_lookup (const VcdObj *obj, const char item_id[]);
+
+unsigned
+_vcd_pbc_list_calc_size (const pbc_t *_pbc, bool extended);
+
+bool
+_vcd_pbc_finalize (VcdObj *obj);
+
+bool
+_vcd_pbc_available (const VcdObj *obj);
+
+uint16_t
+_vcd_pbc_max_lid (const VcdObj *obj);
+
+void
+_vcd_pbc_node_write (const VcdObj *obj, const pbc_t *_pbc, void *buf,
+ bool extended);
+
+void
+_vcd_pbc_check_unreferenced (const VcdObj *obj);
+
+#endif /* __VCD_PBC_H__ */
diff --git a/src/input/vcd/libvcd/salloc.c b/src/input/vcd/libvcd/salloc.c
new file mode 100644
index 000000000..a7599ec58
--- /dev/null
+++ b/src/input/vcd/libvcd/salloc.c
@@ -0,0 +1,212 @@
+/*
+ $Id: salloc.c,v 1.1 2003/10/13 11:47:12 f1rmb Exp $
+
+ Copyright (C) 2000 Herbert Valerio Riedel <hvr@gnu.org>
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+*/
+
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
+
+#include <string.h>
+#include <stdlib.h>
+
+#include <cdio/cdio.h>
+
+/* Public headers */
+#include <libvcd/types.h>
+#include <libvcd/logging.h>
+
+/* Private headers */
+#include "assert.h"
+#include "salloc.h"
+#include "util.h"
+
+static const char _rcsid[] = "$Id: salloc.c,v 1.1 2003/10/13 11:47:12 f1rmb Exp $";
+
+#define VCD_SALLOC_CHUNK_SIZE 16
+
+struct _VcdSalloc
+{
+ uint8_t *data;
+ uint32_t len;
+ uint32_t alloced_chunks;
+};
+
+static void
+_vcd_salloc_set_size (VcdSalloc *bitmap, uint32_t newlen)
+{
+ uint32_t new_alloced_chunks;
+
+ vcd_assert (bitmap != NULL);
+ vcd_assert (newlen >= bitmap->len);
+
+ new_alloced_chunks = newlen / VCD_SALLOC_CHUNK_SIZE;
+ if (newlen % VCD_SALLOC_CHUNK_SIZE)
+ new_alloced_chunks++;
+
+ if (bitmap->alloced_chunks < new_alloced_chunks)
+ {
+ bitmap->data =
+ realloc (bitmap->data, new_alloced_chunks * VCD_SALLOC_CHUNK_SIZE);
+ memset (bitmap->data + (VCD_SALLOC_CHUNK_SIZE * bitmap->alloced_chunks),
+ 0,
+ VCD_SALLOC_CHUNK_SIZE * (new_alloced_chunks -
+ bitmap->alloced_chunks));
+ bitmap->alloced_chunks = new_alloced_chunks;
+ }
+
+ bitmap->len = newlen;
+}
+
+static bool
+_vcd_salloc_is_set (const VcdSalloc *bitmap, uint32_t sector)
+{
+ unsigned _byte = sector / 8;
+ unsigned _bit = sector % 8;
+
+ if (_byte < bitmap->len)
+ return (bitmap->data[_byte] & (1 << _bit)) != 0;
+ else
+ return false;
+}
+
+static void
+_vcd_salloc_set (VcdSalloc *bitmap, uint32_t sector)
+{
+ unsigned _byte = sector / 8;
+ unsigned _bit = sector % 8;
+
+ if (_byte >= bitmap->len)
+ {
+ unsigned oldlen = bitmap->len;
+ _vcd_salloc_set_size (bitmap, _byte + 1);
+ memset (bitmap->data + oldlen, 0x00, _byte + 1 - oldlen);
+ }
+
+ bitmap->data[_byte] |= (1 << _bit);
+}
+
+static void
+_vcd_salloc_unset (VcdSalloc *bitmap, uint32_t sector)
+{
+ unsigned _byte = sector / 8;
+ unsigned _bit = sector % 8;
+
+ if (_byte >= bitmap->len)
+ vcd_assert_not_reached ();
+
+ bitmap->data[_byte] &= ~(1 << _bit);
+}
+
+/* exported */
+
+uint32_t _vcd_salloc (VcdSalloc *bitmap, uint32_t hint, uint32_t size)
+{
+ if (!size)
+ {
+ size++;
+ vcd_warn
+ ("request of 0 sectors allocment fixed up to 1 sector (this is harmless)");
+ }
+
+ vcd_assert (size > 0);
+
+ if (hint != SECTOR_NIL)
+ {
+ uint32_t i;
+ for (i = 0; i < size; i++)
+ if (_vcd_salloc_is_set (bitmap, hint + i))
+ return SECTOR_NIL;
+
+ /* everything's ok for allocing */
+
+ i = size;
+ while (i)
+ _vcd_salloc_set (bitmap, hint + (--i));
+ /* we begin with highest byte, in order to minimizing
+ realloc's in sector_set */
+
+ return hint;
+ }
+
+ /* find the lowest possible ... */
+
+ hint = 0;
+
+ while (_vcd_salloc (bitmap, hint, size) == SECTOR_NIL)
+ hint++;
+
+ return hint;
+}
+
+void
+_vcd_salloc_free (VcdSalloc *bitmap, uint32_t sec, uint32_t size)
+{
+ uint32_t i;
+
+ for (i = 0; i < size; i++)
+ {
+ vcd_assert (_vcd_salloc_is_set (bitmap, sec + i));
+
+ _vcd_salloc_unset (bitmap, sec + i);
+ }
+}
+
+VcdSalloc *
+_vcd_salloc_new (void)
+{
+ VcdSalloc *newobj = _vcd_malloc (sizeof (VcdSalloc));
+ return newobj;
+}
+
+void
+_vcd_salloc_destroy (VcdSalloc *bitmap)
+{
+ vcd_assert (bitmap != NULL);
+
+ free (bitmap->data);
+ free (bitmap);
+}
+
+uint32_t _vcd_salloc_get_highest (const VcdSalloc *bitmap)
+{
+ uint8_t last;
+ unsigned n;
+
+ vcd_assert (bitmap != NULL);
+
+ last = bitmap->data[bitmap->len - 1];
+
+ vcd_assert (last != 0);
+
+ n = 8;
+ while (n)
+ if ((1 << --n) & last)
+ break;
+
+ return (bitmap->len - 1) * 8 + n;
+}
+
+
+/*
+ * Local variables:
+ * c-file-style: "gnu"
+ * tab-width: 8
+ * indent-tabs-mode: nil
+ * End:
+ */
diff --git a/src/input/vcd/libvcd/salloc.h b/src/input/vcd/libvcd/salloc.h
new file mode 100644
index 000000000..959e2f1bd
--- /dev/null
+++ b/src/input/vcd/libvcd/salloc.h
@@ -0,0 +1,56 @@
+/*
+ $Id: salloc.h,v 1.1 2003/10/13 11:47:12 f1rmb Exp $
+
+ Copyright (C) 2000 Herbert Valerio Riedel <hvr@gnu.org>
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+*/
+
+/* sector allocation management */
+
+#ifndef _SALLOC_H_
+#define _SALLOC_H_
+
+#include <libvcd/types.h>
+
+#define SECTOR_NIL ((uint32_t)(-1))
+
+typedef struct _VcdSalloc VcdSalloc;
+
+VcdSalloc *
+_vcd_salloc_new (void);
+
+void
+_vcd_salloc_destroy (VcdSalloc *bitmap);
+
+uint32_t
+_vcd_salloc (VcdSalloc *bitmap, uint32_t hint, uint32_t size);
+
+void
+_vcd_salloc_free (VcdSalloc *bitmap, uint32_t sec, uint32_t size);
+
+uint32_t
+_vcd_salloc_get_highest (const VcdSalloc *bitmap);
+
+#endif /* _SALLOC_H_ */
+
+
+/*
+ * Local variables:
+ * c-file-style: "gnu"
+ * tab-width: 8
+ * indent-tabs-mode: nil
+ * End:
+ */
diff --git a/src/input/vcd/libvcd/sector.c b/src/input/vcd/libvcd/sector.c
new file mode 100644
index 000000000..19393e9cd
--- /dev/null
+++ b/src/input/vcd/libvcd/sector.c
@@ -0,0 +1,290 @@
+/*
+ $Id: sector.c,v 1.1 2003/10/13 11:47:12 f1rmb Exp $
+
+ Copyright (C) 2000 Herbert Valerio Riedel <hvr@gnu.org>
+ (C) 1998 Heiko Eissfeldt <heiko@colossus.escape.de>
+ portions used & Chris Smith
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+*/
+
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
+
+#include <string.h>
+
+#include <cdio/cdio.h>
+
+#include <libvcd/types.h>
+
+#include <libvcd/sector.h>
+
+/* Private includes */
+#include "assert.h"
+#include "bytesex.h"
+#include "salloc.h"
+#include "sector_private.h"
+
+static const char _rcsid[] = "$Id: sector.c,v 1.1 2003/10/13 11:47:12 f1rmb Exp $";
+
+static const uint8_t sync_pattern[12] = {
+ 0x00, 0xff, 0xff, 0xff,
+ 0xff, 0xff, 0xff, 0xff,
+ 0xff, 0xff, 0xff, 0x00
+};
+
+static void
+build_address (void *buf, sectortype_t sectortype, uint32_t address)
+{
+ raw_cd_sector_t *sector = buf;
+
+ vcd_assert (sizeof(raw_cd_sector_t) == CDIO_CD_FRAMESIZE_RAW-DATA_LEN);
+
+ cdio_lba_to_msf(address, &(sector->msf));
+
+ switch(sectortype) {
+ case MODE_0:
+ sector->mode = 0;
+ break;
+ case MODE_2:
+ case MODE_2_FORM_1:
+ case MODE_2_FORM_2:
+ sector->mode = 2;
+ break;
+ default:
+ vcd_assert_not_reached ();
+ break;
+ }
+}
+
+static uint32_t
+build_edc (const void *in, unsigned from, unsigned upto)
+{
+ const uint8_t *p = (uint8_t*)in+from;
+ uint32_t result = 0;
+
+ for (; from <= upto; from++)
+ result = EDC_crctable[(result ^ *p++) & 0xffL] ^ (result >> 8);
+
+ return result;
+}
+
+static void
+encode_L2_Q (uint8_t inout[4 + L2_RAW + 4 + 8 + L2_P + L2_Q])
+{
+ uint8_t *Q;
+ int i,j;
+
+ Q = inout + 4 + L2_RAW + 4 + 8 + L2_P;
+ memset (Q, 0, L2_Q);
+ for (j = 0; j < 26; j++) {
+ for (i = 0; i < 43; i++) {
+ uint8_t data;
+
+ /* LSB */
+ data = inout[(j*43*2+i*2*44) % (4 + L2_RAW + 4 + 8 + L2_P)];
+ if (data != 0) {
+ uint32_t base = rs_l12_log[data];
+
+ uint32_t sum = base + DQ[0][i];
+ if (sum >= ((1 << RS_L12_BITS)-1))
+ sum -= (1 << RS_L12_BITS)-1;
+
+ Q[0] ^= rs_l12_alog[sum];
+
+ sum = base + DQ[1][i];
+ if (sum >= ((1 << RS_L12_BITS)-1))
+ sum -= (1 << RS_L12_BITS)-1;
+
+ Q[26*2] ^= rs_l12_alog[sum];
+ }
+ /* MSB */
+ data = inout[(j*43*2+i*2*44+1) % (4 + L2_RAW + 4 + 8 + L2_P)];
+ if (data != 0) {
+ uint32_t base = rs_l12_log[data];
+
+ uint32_t sum = base+DQ[0][i];
+ if (sum >= ((1 << RS_L12_BITS)-1))
+ sum -= (1 << RS_L12_BITS)-1;
+
+ Q[1] ^= rs_l12_alog[sum];
+
+ sum = base + DQ[1][i];
+ if (sum >= ((1 << RS_L12_BITS)-1))
+ sum -= (1 << RS_L12_BITS)-1;
+
+ Q[26*2+1] ^= rs_l12_alog[sum];
+ }
+ }
+ Q += 2;
+ }
+}
+
+static void
+encode_L2_P (uint8_t inout[4 + L2_RAW + 4 + 8 + L2_P])
+{
+ uint8_t *P;
+ int i,j;
+
+ P = inout + 4 + L2_RAW + 4 + 8;
+ memset(P, 0, L2_P);
+ for (j = 0; j < 43; j++) {
+ for (i = 0; i < 24; i++) {
+ uint8_t data;
+
+ /* LSB */
+ data = inout[i*2*43];
+ if (data != 0) {
+ uint32_t base = rs_l12_log[data];
+
+ uint32_t sum = base + DP[0][i];
+ if (sum >= ((1 << RS_L12_BITS)-1))
+ sum -= (1 << RS_L12_BITS)-1;
+
+ P[0] ^= rs_l12_alog[sum];
+
+ sum = base + DP[1][i];
+ if (sum >= ((1 << RS_L12_BITS)-1))
+ sum -= (1 << RS_L12_BITS)-1;
+
+ P[43*2] ^= rs_l12_alog[sum];
+ }
+ /* MSB */
+ data = inout[i*2*43+1];
+ if (data != 0) {
+ uint32_t base = rs_l12_log[data];
+
+ uint32_t sum = base + DP[0][i];
+ if (sum >= ((1 << RS_L12_BITS)-1))
+ sum -= (1 << RS_L12_BITS)-1;
+
+ P[1] ^= rs_l12_alog[sum];
+
+ sum = base + DP[1][i];
+ if (sum >= ((1 << RS_L12_BITS)-1))
+ sum -= (1 << RS_L12_BITS)-1;
+
+ P[43*2+1] ^= rs_l12_alog[sum];
+ }
+ }
+ P += 2;
+ inout += 2;
+ }
+}
+
+/* Layer 2 Product code en/decoder */
+static void
+do_encode_L2 (void *buf, sectortype_t sectortype, uint32_t address)
+{
+ raw_cd_sector_t *raw_sector = buf;
+
+ vcd_assert (buf != NULL);
+
+ vcd_assert (sizeof (sync_pattern) == SYNC_LEN);
+ vcd_assert (sizeof (mode2_form1_sector_t) == CDIO_CD_FRAMESIZE_RAW);
+ vcd_assert (sizeof (mode2_form2_sector_t) == CDIO_CD_FRAMESIZE_RAW);
+ vcd_assert (sizeof (mode0_sector_t) == CDIO_CD_FRAMESIZE_RAW);
+ vcd_assert (sizeof (raw_cd_sector_t) == SYNC_LEN+HEADER_LEN);
+
+ memset (raw_sector, 0, SYNC_LEN+HEADER_LEN);
+ memcpy (raw_sector->sync, sync_pattern, sizeof (sync_pattern));
+
+ switch (sectortype) {
+ case MODE_0:
+ {
+ mode0_sector_t *sector = buf;
+
+ memset(sector->data, 0, sizeof(sector->data));
+ }
+ break;
+ case MODE_2:
+ break;
+ case MODE_2_FORM_1:
+ {
+ mode2_form1_sector_t *sector = buf;
+
+ sector->edc = uint32_to_le(build_edc(buf, 16, 16+8+2048-1));
+
+ encode_L2_P((uint8_t*)buf+SYNC_LEN);
+ encode_L2_Q((uint8_t*)buf+SYNC_LEN);
+ }
+ break;
+ case MODE_2_FORM_2:
+ {
+ mode2_form2_sector_t *sector = buf;
+
+ sector->edc = uint32_to_le(build_edc(buf, 16, 16+8+2324-1));
+ }
+ break;
+ default:
+ vcd_assert_not_reached ();
+ }
+
+ build_address (buf, sectortype, address);
+}
+
+void
+_vcd_make_mode2 (void *raw_sector, const void *data, uint32_t extent,
+ uint8_t fnum, uint8_t cnum, uint8_t sm, uint8_t ci)
+{
+ uint8_t *subhdr = (uint8_t*)raw_sector+16;
+
+ vcd_assert (raw_sector != NULL);
+ vcd_assert (data != NULL);
+ vcd_assert (extent != SECTOR_NIL);
+
+ memset (raw_sector, 0, CDIO_CD_FRAMESIZE_RAW);
+
+ subhdr[0] = subhdr[4] = fnum;
+ subhdr[1] = subhdr[5] = cnum;
+ subhdr[2] = subhdr[6] = sm;
+ subhdr[3] = subhdr[7] = ci;
+
+ if (sm & SM_FORM2)
+ {
+ memcpy ((char*)raw_sector+CDIO_CD_XA_SYNC_HEADER, data,
+ M2F2_SECTOR_SIZE);
+ do_encode_L2 (raw_sector, MODE_2_FORM_2, extent+CDIO_PREGAP_SECTORS);
+ }
+ else
+ {
+ memcpy ((char*)raw_sector+CDIO_CD_XA_SYNC_HEADER, data,
+ CDIO_CD_FRAMESIZE);
+ do_encode_L2 (raw_sector, MODE_2_FORM_1, extent+CDIO_PREGAP_SECTORS);
+ }
+}
+
+void
+_vcd_make_raw_mode2 (void *raw_sector, const void *data, uint32_t extent)
+{
+ vcd_assert (raw_sector != NULL);
+ vcd_assert (data != NULL);
+ vcd_assert (extent != SECTOR_NIL);
+
+ memset (raw_sector, 0, CDIO_CD_FRAMESIZE_RAW);
+
+ memcpy ((char*)raw_sector+12+4, data, M2RAW_SECTOR_SIZE);
+ do_encode_L2 (raw_sector, MODE_2, extent+CDIO_PREGAP_SECTORS);
+}
+
+
+/*
+ * Local variables:
+ * c-file-style: "gnu"
+ * tab-width: 8
+ * indent-tabs-mode: nil
+ * End:
+ */
diff --git a/src/input/vcd/libvcd/sector_private.h b/src/input/vcd/libvcd/sector_private.h
new file mode 100644
index 000000000..8cdae270e
--- /dev/null
+++ b/src/input/vcd/libvcd/sector_private.h
@@ -0,0 +1,256 @@
+/*
+ $Id: sector_private.h,v 1.1 2003/10/13 11:47:12 f1rmb Exp $
+
+ Copyright (C) 2000 Herbert Valerio Riedel <hvr@gnu.org>
+ (C) 1998 Heiko Eissfeldt <heiko@colossus.escape.de>
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+*/
+
+#ifndef __VCD_CD_SECTOR_PRIVATE_H__
+#define __VCD_CD_SECTOR_PRIVATE_H__
+
+#define RS_L12_BITS 8
+
+#define L2_RAW (1024*2)
+#define L2_Q (26*2*2)
+#define L2_P (43*2*2)
+
+typedef enum {
+ MODE_0,
+ MODE_2,
+ MODE_2_FORM_1,
+ MODE_2_FORM_2
+} sectortype_t;
+
+#define SYNC_LEN 12
+#define DATA_LEN 2336
+#define HEADER_LEN 4
+
+typedef struct {
+ uint8_t sync[SYNC_LEN];
+ msf_t msf;
+ uint8_t mode;
+ uint8_t user_data[EMPTY_ARRAY_SIZE];
+} raw_cd_sector_t;
+
+#define raw_cd_sector_t_SIZEOF (SYNC_LEN+HEADER_LEN)
+
+typedef struct {
+ uint8_t sync[SYNC_LEN];
+ msf_t msf;
+ uint8_t mode;
+} sector_header_t;
+
+#define sector_header_t_SIZEOF (SYNC_LEN+HEADER_LEN)
+
+typedef struct {
+ sector_header_t sector_header;
+ uint8_t data[M2RAW_SECTOR_SIZE];
+} mode0_sector_t;
+
+#define mode0_sector_t_SIZEOF CDIO_CD_FRAMESIZE_RAW
+
+typedef struct {
+ sector_header_t sector_header;
+ uint8_t subheader[CDIO_CD_SUBHEADER_SIZE];
+ uint8_t data[CDIO_CD_FRAMESIZE];
+ uint32_t edc;
+ uint8_t l2_p[L2_P];
+ uint8_t l2_q[L2_Q];
+} mode2_form1_sector_t;
+
+#define mode2_form1_sector_t_SIZEOF CDIO_CD_FRAMESIZE_RAW
+
+typedef struct {
+ sector_header_t sector_header;
+ uint8_t subheader[CDIO_CD_SUBHEADER_SIZE];
+ uint8_t data[M2F2_SECTOR_SIZE];
+ uint32_t edc;
+} mode2_form2_sector_t;
+
+#define mode2_form2_sector_t_SIZEOF CDIO_CD_FRAMESIZE_RAW
+
+static const uint32_t EDC_crctable[256] = {
+ 0x00000000l, 0x90910101l, 0x91210201l, 0x01b00300l,
+ 0x92410401l, 0x02d00500l, 0x03600600l, 0x93f10701l,
+ 0x94810801l, 0x04100900l, 0x05a00a00l, 0x95310b01l,
+ 0x06c00c00l, 0x96510d01l, 0x97e10e01l, 0x07700f00l,
+ 0x99011001l, 0x09901100l, 0x08201200l, 0x98b11301l,
+ 0x0b401400l, 0x9bd11501l, 0x9a611601l, 0x0af01700l,
+ 0x0d801800l, 0x9d111901l, 0x9ca11a01l, 0x0c301b00l,
+ 0x9fc11c01l, 0x0f501d00l, 0x0ee01e00l, 0x9e711f01l,
+ 0x82012001l, 0x12902100l, 0x13202200l, 0x83b12301l,
+ 0x10402400l, 0x80d12501l, 0x81612601l, 0x11f02700l,
+ 0x16802800l, 0x86112901l, 0x87a12a01l, 0x17302b00l,
+ 0x84c12c01l, 0x14502d00l, 0x15e02e00l, 0x85712f01l,
+ 0x1b003000l, 0x8b913101l, 0x8a213201l, 0x1ab03300l,
+ 0x89413401l, 0x19d03500l, 0x18603600l, 0x88f13701l,
+ 0x8f813801l, 0x1f103900l, 0x1ea03a00l, 0x8e313b01l,
+ 0x1dc03c00l, 0x8d513d01l, 0x8ce13e01l, 0x1c703f00l,
+ 0xb4014001l, 0x24904100l, 0x25204200l, 0xb5b14301l,
+ 0x26404400l, 0xb6d14501l, 0xb7614601l, 0x27f04700l,
+ 0x20804800l, 0xb0114901l, 0xb1a14a01l, 0x21304b00l,
+ 0xb2c14c01l, 0x22504d00l, 0x23e04e00l, 0xb3714f01l,
+ 0x2d005000l, 0xbd915101l, 0xbc215201l, 0x2cb05300l,
+ 0xbf415401l, 0x2fd05500l, 0x2e605600l, 0xbef15701l,
+ 0xb9815801l, 0x29105900l, 0x28a05a00l, 0xb8315b01l,
+ 0x2bc05c00l, 0xbb515d01l, 0xbae15e01l, 0x2a705f00l,
+ 0x36006000l, 0xa6916101l, 0xa7216201l, 0x37b06300l,
+ 0xa4416401l, 0x34d06500l, 0x35606600l, 0xa5f16701l,
+ 0xa2816801l, 0x32106900l, 0x33a06a00l, 0xa3316b01l,
+ 0x30c06c00l, 0xa0516d01l, 0xa1e16e01l, 0x31706f00l,
+ 0xaf017001l, 0x3f907100l, 0x3e207200l, 0xaeb17301l,
+ 0x3d407400l, 0xadd17501l, 0xac617601l, 0x3cf07700l,
+ 0x3b807800l, 0xab117901l, 0xaaa17a01l, 0x3a307b00l,
+ 0xa9c17c01l, 0x39507d00l, 0x38e07e00l, 0xa8717f01l,
+ 0xd8018001l, 0x48908100l, 0x49208200l, 0xd9b18301l,
+ 0x4a408400l, 0xdad18501l, 0xdb618601l, 0x4bf08700l,
+ 0x4c808800l, 0xdc118901l, 0xdda18a01l, 0x4d308b00l,
+ 0xdec18c01l, 0x4e508d00l, 0x4fe08e00l, 0xdf718f01l,
+ 0x41009000l, 0xd1919101l, 0xd0219201l, 0x40b09300l,
+ 0xd3419401l, 0x43d09500l, 0x42609600l, 0xd2f19701l,
+ 0xd5819801l, 0x45109900l, 0x44a09a00l, 0xd4319b01l,
+ 0x47c09c00l, 0xd7519d01l, 0xd6e19e01l, 0x46709f00l,
+ 0x5a00a000l, 0xca91a101l, 0xcb21a201l, 0x5bb0a300l,
+ 0xc841a401l, 0x58d0a500l, 0x5960a600l, 0xc9f1a701l,
+ 0xce81a801l, 0x5e10a900l, 0x5fa0aa00l, 0xcf31ab01l,
+ 0x5cc0ac00l, 0xcc51ad01l, 0xcde1ae01l, 0x5d70af00l,
+ 0xc301b001l, 0x5390b100l, 0x5220b200l, 0xc2b1b301l,
+ 0x5140b400l, 0xc1d1b501l, 0xc061b601l, 0x50f0b700l,
+ 0x5780b800l, 0xc711b901l, 0xc6a1ba01l, 0x5630bb00l,
+ 0xc5c1bc01l, 0x5550bd00l, 0x54e0be00l, 0xc471bf01l,
+ 0x6c00c000l, 0xfc91c101l, 0xfd21c201l, 0x6db0c300l,
+ 0xfe41c401l, 0x6ed0c500l, 0x6f60c600l, 0xfff1c701l,
+ 0xf881c801l, 0x6810c900l, 0x69a0ca00l, 0xf931cb01l,
+ 0x6ac0cc00l, 0xfa51cd01l, 0xfbe1ce01l, 0x6b70cf00l,
+ 0xf501d001l, 0x6590d100l, 0x6420d200l, 0xf4b1d301l,
+ 0x6740d400l, 0xf7d1d501l, 0xf661d601l, 0x66f0d700l,
+ 0x6180d800l, 0xf111d901l, 0xf0a1da01l, 0x6030db00l,
+ 0xf3c1dc01l, 0x6350dd00l, 0x62e0de00l, 0xf271df01l,
+ 0xee01e001l, 0x7e90e100l, 0x7f20e200l, 0xefb1e301l,
+ 0x7c40e400l, 0xecd1e501l, 0xed61e601l, 0x7df0e700l,
+ 0x7a80e800l, 0xea11e901l, 0xeba1ea01l, 0x7b30eb00l,
+ 0xe8c1ec01l, 0x7850ed00l, 0x79e0ee00l, 0xe971ef01l,
+ 0x7700f000l, 0xe791f101l, 0xe621f201l, 0x76b0f300l,
+ 0xe541f401l, 0x75d0f500l, 0x7460f600l, 0xe4f1f701l,
+ 0xe381f801l, 0x7310f900l, 0x72a0fa00l, 0xe231fb01l,
+ 0x71c0fc00l, 0xe151fd01l, 0xe0e1fe01l, 0x7070ff00l
+};
+
+static const uint8_t rs_l12_alog[255] = {
+ 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80,
+ 0x1d, 0x3a, 0x74, 0xe8, 0xcd, 0x87, 0x13, 0x26,
+ 0x4c, 0x98, 0x2d, 0x5a, 0xb4, 0x75, 0xea, 0xc9,
+ 0x8f, 0x03, 0x06, 0x0c, 0x18, 0x30, 0x60, 0xc0,
+ 0x9d, 0x27, 0x4e, 0x9c, 0x25, 0x4a, 0x94, 0x35,
+ 0x6a, 0xd4, 0xb5, 0x77, 0xee, 0xc1, 0x9f, 0x23,
+ 0x46, 0x8c, 0x05, 0x0a, 0x14, 0x28, 0x50, 0xa0,
+ 0x5d, 0xba, 0x69, 0xd2, 0xb9, 0x6f, 0xde, 0xa1,
+ 0x5f, 0xbe, 0x61, 0xc2, 0x99, 0x2f, 0x5e, 0xbc,
+ 0x65, 0xca, 0x89, 0x0f, 0x1e, 0x3c, 0x78, 0xf0,
+ 0xfd, 0xe7, 0xd3, 0xbb, 0x6b, 0xd6, 0xb1, 0x7f,
+ 0xfe, 0xe1, 0xdf, 0xa3, 0x5b, 0xb6, 0x71, 0xe2,
+ 0xd9, 0xaf, 0x43, 0x86, 0x11, 0x22, 0x44, 0x88,
+ 0x0d, 0x1a, 0x34, 0x68, 0xd0, 0xbd, 0x67, 0xce,
+ 0x81, 0x1f, 0x3e, 0x7c, 0xf8, 0xed, 0xc7, 0x93,
+ 0x3b, 0x76, 0xec, 0xc5, 0x97, 0x33, 0x66, 0xcc,
+ 0x85, 0x17, 0x2e, 0x5c, 0xb8, 0x6d, 0xda, 0xa9,
+ 0x4f, 0x9e, 0x21, 0x42, 0x84, 0x15, 0x2a, 0x54,
+ 0xa8, 0x4d, 0x9a, 0x29, 0x52, 0xa4, 0x55, 0xaa,
+ 0x49, 0x92, 0x39, 0x72, 0xe4, 0xd5, 0xb7, 0x73,
+ 0xe6, 0xd1, 0xbf, 0x63, 0xc6, 0x91, 0x3f, 0x7e,
+ 0xfc, 0xe5, 0xd7, 0xb3, 0x7b, 0xf6, 0xf1, 0xff,
+ 0xe3, 0xdb, 0xab, 0x4b, 0x96, 0x31, 0x62, 0xc4,
+ 0x95, 0x37, 0x6e, 0xdc, 0xa5, 0x57, 0xae, 0x41,
+ 0x82, 0x19, 0x32, 0x64, 0xc8, 0x8d, 0x07, 0x0e,
+ 0x1c, 0x38, 0x70, 0xe0, 0xdd, 0xa7, 0x53, 0xa6,
+ 0x51, 0xa2, 0x59, 0xb2, 0x79, 0xf2, 0xf9, 0xef,
+ 0xc3, 0x9b, 0x2b, 0x56, 0xac, 0x45, 0x8a, 0x09,
+ 0x12, 0x24, 0x48, 0x90, 0x3d, 0x7a, 0xf4, 0xf5,
+ 0xf7, 0xf3, 0xfb, 0xeb, 0xcb, 0x8b, 0x0b, 0x16,
+ 0x2c, 0x58, 0xb0, 0x7d, 0xfa, 0xe9, 0xcf, 0x83,
+ 0x1b, 0x36, 0x6c, 0xd8, 0xad, 0x47, 0x8e
+};
+
+static const uint8_t rs_l12_log[256] = {
+ 0x00, 0x00, 0x01, 0x19, 0x02, 0x32, 0x1a, 0xc6,
+ 0x03, 0xdf, 0x33, 0xee, 0x1b, 0x68, 0xc7, 0x4b,
+ 0x04, 0x64, 0xe0, 0x0e, 0x34, 0x8d, 0xef, 0x81,
+ 0x1c, 0xc1, 0x69, 0xf8, 0xc8, 0x08, 0x4c, 0x71,
+ 0x05, 0x8a, 0x65, 0x2f, 0xe1, 0x24, 0x0f, 0x21,
+ 0x35, 0x93, 0x8e, 0xda, 0xf0, 0x12, 0x82, 0x45,
+ 0x1d, 0xb5, 0xc2, 0x7d, 0x6a, 0x27, 0xf9, 0xb9,
+ 0xc9, 0x9a, 0x09, 0x78, 0x4d, 0xe4, 0x72, 0xa6,
+ 0x06, 0xbf, 0x8b, 0x62, 0x66, 0xdd, 0x30, 0xfd,
+ 0xe2, 0x98, 0x25, 0xb3, 0x10, 0x91, 0x22, 0x88,
+ 0x36, 0xd0, 0x94, 0xce, 0x8f, 0x96, 0xdb, 0xbd,
+ 0xf1, 0xd2, 0x13, 0x5c, 0x83, 0x38, 0x46, 0x40,
+ 0x1e, 0x42, 0xb6, 0xa3, 0xc3, 0x48, 0x7e, 0x6e,
+ 0x6b, 0x3a, 0x28, 0x54, 0xfa, 0x85, 0xba, 0x3d,
+ 0xca, 0x5e, 0x9b, 0x9f, 0x0a, 0x15, 0x79, 0x2b,
+ 0x4e, 0xd4, 0xe5, 0xac, 0x73, 0xf3, 0xa7, 0x57,
+ 0x07, 0x70, 0xc0, 0xf7, 0x8c, 0x80, 0x63, 0x0d,
+ 0x67, 0x4a, 0xde, 0xed, 0x31, 0xc5, 0xfe, 0x18,
+ 0xe3, 0xa5, 0x99, 0x77, 0x26, 0xb8, 0xb4, 0x7c,
+ 0x11, 0x44, 0x92, 0xd9, 0x23, 0x20, 0x89, 0x2e,
+ 0x37, 0x3f, 0xd1, 0x5b, 0x95, 0xbc, 0xcf, 0xcd,
+ 0x90, 0x87, 0x97, 0xb2, 0xdc, 0xfc, 0xbe, 0x61,
+ 0xf2, 0x56, 0xd3, 0xab, 0x14, 0x2a, 0x5d, 0x9e,
+ 0x84, 0x3c, 0x39, 0x53, 0x47, 0x6d, 0x41, 0xa2,
+ 0x1f, 0x2d, 0x43, 0xd8, 0xb7, 0x7b, 0xa4, 0x76,
+ 0xc4, 0x17, 0x49, 0xec, 0x7f, 0x0c, 0x6f, 0xf6,
+ 0x6c, 0xa1, 0x3b, 0x52, 0x29, 0x9d, 0x55, 0xaa,
+ 0xfb, 0x60, 0x86, 0xb1, 0xbb, 0xcc, 0x3e, 0x5a,
+ 0xcb, 0x59, 0x5f, 0xb0, 0x9c, 0xa9, 0xa0, 0x51,
+ 0x0b, 0xf5, 0x16, 0xeb, 0x7a, 0x75, 0x2c, 0xd7,
+ 0x4f, 0xae, 0xd5, 0xe9, 0xe6, 0xe7, 0xad, 0xe8,
+ 0x74, 0xd6, 0xf4, 0xea, 0xa8, 0x50, 0x58, 0xaf
+};
+
+static const uint8_t DQ[2][43] = {
+ { 0xbe, 0x60, 0xfa, 0x84, 0x3b, 0x51, 0x9f, 0x9a,
+ 0xc8, 0x07, 0x6f, 0xf5, 0x0a, 0x14, 0x29, 0x9c,
+ 0xa8, 0x4f, 0xad, 0xe7, 0xe5, 0xab, 0xd2, 0xf0,
+ 0x11, 0x43, 0xd7, 0x2b, 0x78, 0x08, 0xc7, 0x4a,
+ 0x66, 0xdc, 0xfb, 0x5f, 0xaf, 0x57, 0xa6, 0x71,
+ 0x4b, 0xc6, 0x19 },
+ { 0x61, 0xfb, 0x85, 0x3c, 0x52, 0xa0, 0x9b, 0xc9,
+ 0x08, 0x70, 0xf6, 0x0b, 0x15, 0x2a, 0x9d, 0xa9,
+ 0x50, 0xae, 0xe8, 0xe6, 0xac, 0xd3, 0xf1, 0x12,
+ 0x44, 0xd8, 0x2c, 0x79, 0x09, 0xc8, 0x4b, 0x67,
+ 0xdd, 0xfc, 0x60, 0xb0, 0x58, 0xa7, 0x72, 0x4c,
+ 0xc7, 0x1a, 0x01 },
+};
+
+static const uint8_t DP[2][24] = {
+ { 0xe7, 0xe5, 0xab, 0xd2, 0xf0, 0x11, 0x43, 0xd7,
+ 0x2b, 0x78, 0x08, 0xc7, 0x4a, 0x66, 0xdc, 0xfb,
+ 0x5f, 0xaf, 0x57, 0xa6, 0x71, 0x4b, 0xc6, 0x19 },
+ { 0xe6, 0xac, 0xd3, 0xf1, 0x12, 0x44, 0xd8, 0x2c,
+ 0x79, 0x09, 0xc8, 0x4b, 0x67, 0xdd, 0xfc, 0x60,
+ 0xb0, 0x58, 0xa7, 0x72, 0x4c, 0xc7, 0x1a, 0x01 },
+};
+
+#endif /* __VCD_CD_SECTOR_PRIVATE_H__ */
+
+
+/*
+ * Local variables:
+ * c-file-style: "gnu"
+ * tab-width: 8
+ * indent-tabs-mode: nil
+ * End:
+ */
diff --git a/src/input/vcd/libvcd/stream.c b/src/input/vcd/libvcd/stream.c
new file mode 100644
index 000000000..7866a62c2
--- /dev/null
+++ b/src/input/vcd/libvcd/stream.c
@@ -0,0 +1,276 @@
+/*
+ $Id: stream.c,v 1.1 2003/10/13 11:47:12 f1rmb Exp $
+
+ Copyright (C) 2000 Herbert Valerio Riedel <hvr@gnu.org>
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+*/
+
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <stdarg.h>
+
+#include <cdio/cdio.h>
+
+/* #define STREAM_DEBUG */
+
+/* Public headers */
+
+#include <libvcd/logging.h>
+
+/* Private headers */
+#include "assert.h"
+#include "stream.h"
+#include "util.h"
+
+static const char _rcsid[] = "$Id: stream.c,v 1.1 2003/10/13 11:47:12 f1rmb Exp $";
+
+/*
+ * DataSource implementations
+ */
+
+struct _VcdDataSink {
+ void* user_data;
+ vcd_data_sink_io_functions op;
+ int is_open;
+ long position;
+};
+
+static void
+_vcd_data_sink_open_if_necessary(VcdDataSink *obj)
+{
+ vcd_assert (obj != NULL);
+
+ if (!obj->is_open) {
+ if (obj->op.open(obj->user_data))
+ vcd_error("could not opening output stream...");
+ else {
+ obj->is_open = 1;
+ obj->position = 0;
+ }
+ }
+}
+
+VcdDataSink*
+vcd_data_sink_new(void *user_data, const vcd_data_sink_io_functions *funcs)
+{
+ VcdDataSink *new_obj;
+
+ new_obj = _vcd_malloc(sizeof(VcdDataSink));
+
+ new_obj->user_data = user_data;
+ memcpy(&(new_obj->op), funcs, sizeof(vcd_data_sink_io_functions));
+
+ return new_obj;
+}
+
+long
+vcd_data_sink_seek(VcdDataSink* obj, long offset)
+{
+ vcd_assert (obj != NULL);
+
+ _vcd_data_sink_open_if_necessary(obj);
+
+ if (obj->position != offset) {
+ vcd_warn("had to reposition DataSink from %ld to %ld!", obj->position, offset);
+ obj->position = offset;
+ return obj->op.seek(obj->user_data, offset);
+ }
+
+ return 0;
+}
+
+long
+vcd_data_sink_write(VcdDataSink* obj, const void *ptr, long size, long nmemb)
+{
+ long written;
+
+ vcd_assert (obj != NULL);
+
+ _vcd_data_sink_open_if_necessary(obj);
+
+ written = obj->op.write(obj->user_data, ptr, size*nmemb);
+ obj->position += written;
+
+ return written;
+}
+
+long
+vcd_data_sink_printf (VcdDataSink *obj, const char format[], ...)
+{
+ char buf[4096] = { 0, };
+ long retval;
+ int len;
+
+ va_list args;
+ va_start (args, format);
+
+ len = vsnprintf (buf, sizeof(buf), format, args);
+
+ if (len < 0 || len > (sizeof (buf) - 1))
+ vcd_error ("vsnprintf() returned %d", len);
+
+ retval = vcd_data_sink_write (obj, buf, 1, len);
+
+ va_end (args);
+
+ return retval;
+}
+
+void
+vcd_data_sink_close(VcdDataSink* obj)
+{
+ vcd_assert (obj != NULL);
+
+ if (obj->is_open) {
+ obj->op.close(obj->user_data);
+ obj->is_open = 0;
+ obj->position = 0;
+ }
+}
+
+void
+vcd_data_sink_destroy(VcdDataSink* obj)
+{
+ vcd_assert (obj != NULL);
+
+ vcd_data_sink_close(obj);
+
+ obj->op.free(obj->user_data);
+}
+
+/*
+ * DataSource implementations
+ */
+
+struct _VcdDataSource {
+ void* user_data;
+ vcd_data_source_io_functions op;
+ int is_open;
+ long position;
+};
+
+static void
+_vcd_data_source_open_if_necessary(VcdDataSource *obj)
+{
+ vcd_assert (obj != NULL);
+
+ if (!obj->is_open) {
+ if (obj->op.open(obj->user_data))
+ vcd_error ("could not opening input stream...");
+ else {
+#ifdef STREAM_DEBUG
+ vcd_debug ("opened source...");
+#endif
+ obj->is_open = 1;
+ obj->position = 0;
+ }
+ }
+}
+
+long
+vcd_data_source_seek(VcdDataSource* obj, long offset)
+{
+ vcd_assert (obj != NULL);
+
+ _vcd_data_source_open_if_necessary(obj);
+
+ if (obj->position != offset) {
+#ifdef STREAM_DEBUG
+ vcd_warn("had to reposition DataSource from %ld to %ld!", obj->position, offset);
+#endif
+ obj->position = offset;
+ return obj->op.seek(obj->user_data, offset);
+ }
+
+ return 0;
+}
+
+VcdDataSource*
+vcd_data_source_new(void *user_data, const vcd_data_source_io_functions *funcs)
+{
+ VcdDataSource *new_obj;
+
+ new_obj = _vcd_malloc (sizeof (VcdDataSource));
+
+ new_obj->user_data = user_data;
+ memcpy(&(new_obj->op), funcs, sizeof(vcd_data_source_io_functions));
+
+ return new_obj;
+}
+
+long
+vcd_data_source_read(VcdDataSource* obj, void *ptr, long size, long nmemb)
+{
+ long read_bytes;
+
+ vcd_assert (obj != NULL);
+
+ _vcd_data_source_open_if_necessary(obj);
+
+ read_bytes = obj->op.read(obj->user_data, ptr, size*nmemb);
+ obj->position += read_bytes;
+
+ return read_bytes;
+}
+
+long
+vcd_data_source_stat(VcdDataSource* obj)
+{
+ vcd_assert (obj != NULL);
+
+ _vcd_data_source_open_if_necessary(obj);
+
+ return obj->op.stat(obj->user_data);
+}
+
+void
+vcd_data_source_close(VcdDataSource* obj)
+{
+ vcd_assert (obj != NULL);
+
+ if (obj->is_open) {
+#ifdef STREAM_DEBUG
+ vcd_debug ("closed source...");
+#endif
+ obj->op.close(obj->user_data);
+ obj->is_open = 0;
+ obj->position = 0;
+ }
+}
+
+void
+vcd_data_source_destroy(VcdDataSource* obj)
+{
+ vcd_assert (obj != NULL);
+
+ vcd_data_source_close(obj);
+
+ obj->op.free(obj->user_data);
+}
+
+
+/*
+ * Local variables:
+ * c-file-style: "gnu"
+ * tab-width: 8
+ * indent-tabs-mode: nil
+ * End:
+ */
diff --git a/src/input/vcd/libvcd/stream.h b/src/input/vcd/libvcd/stream.h
new file mode 100644
index 000000000..dcee55ac4
--- /dev/null
+++ b/src/input/vcd/libvcd/stream.h
@@ -0,0 +1,123 @@
+/*
+ $Id: stream.h,v 1.1 2003/10/13 11:47:12 f1rmb Exp $
+
+ Copyright (C) 2000 Herbert Valerio Riedel <hvr@gnu.org>
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+*/
+
+
+#ifndef __VCD_STREAM_H__
+#define __VCD_STREAM_H__
+
+#include <libvcd/types.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif /* __cplusplus */
+
+/* typedef'ed IO functions prototypes */
+
+typedef int(*vcd_data_open_t)(void *user_data);
+
+typedef long(*vcd_data_read_t)(void *user_data, void *buf, long count);
+
+typedef long(*vcd_data_write_t)(void *user_data, const void *buf,
+ long count);
+
+typedef long(*vcd_data_seek_t)(void *user_data, long offset);
+
+typedef long(*vcd_data_stat_t)(void *user_data);
+
+typedef int(*vcd_data_close_t)(void *user_data);
+
+typedef void(*vcd_data_free_t)(void *user_data);
+
+
+/* abstract data sink */
+
+typedef struct _VcdDataSink VcdDataSink;
+
+typedef struct {
+ vcd_data_open_t open;
+ vcd_data_seek_t seek;
+ vcd_data_write_t write;
+ vcd_data_close_t close;
+ vcd_data_free_t free;
+} vcd_data_sink_io_functions;
+
+VcdDataSink*
+vcd_data_sink_new(void *user_data, const vcd_data_sink_io_functions *funcs);
+
+long
+vcd_data_sink_write(VcdDataSink* obj, const void *ptr, long size, long nmemb);
+
+long
+vcd_data_sink_printf (VcdDataSink *obj, const char format[], ...) GNUC_PRINTF(2, 3);
+
+long
+vcd_data_sink_seek(VcdDataSink* obj, long offset);
+
+void
+vcd_data_sink_destroy(VcdDataSink* obj);
+
+void
+vcd_data_sink_close(VcdDataSink* obj);
+
+/* abstract data source */
+
+typedef struct _VcdDataSource VcdDataSource;
+
+typedef struct {
+ vcd_data_open_t open;
+ vcd_data_seek_t seek;
+ vcd_data_stat_t stat;
+ vcd_data_read_t read;
+ vcd_data_close_t close;
+ vcd_data_free_t free;
+} vcd_data_source_io_functions;
+
+VcdDataSource*
+vcd_data_source_new(void *user_data, const vcd_data_source_io_functions *funcs);
+
+long
+vcd_data_source_read(VcdDataSource* obj, void *ptr, long size, long nmemb);
+
+long
+vcd_data_source_seek(VcdDataSource* obj, long offset);
+
+long
+vcd_data_source_stat(VcdDataSource* obj);
+
+void
+vcd_data_source_destroy(VcdDataSource* obj);
+
+void
+vcd_data_source_close(VcdDataSource* obj);
+
+#ifdef __cplusplus
+}
+#endif /* __cplusplus */
+
+#endif /* __VCD_STREAM_H__ */
+
+
+/*
+ * Local variables:
+ * c-file-style: "gnu"
+ * tab-width: 8
+ * indent-tabs-mode: nil
+ * End:
+ */
diff --git a/src/input/vcd/libvcd/stream_stdio.c b/src/input/vcd/libvcd/stream_stdio.c
new file mode 100644
index 000000000..e3a79a494
--- /dev/null
+++ b/src/input/vcd/libvcd/stream_stdio.c
@@ -0,0 +1,236 @@
+/*
+ $Id: stream_stdio.c,v 1.1 2003/10/13 11:47:12 f1rmb Exp $
+
+ Copyright (C) 2000 Herbert Valerio Riedel <hvr@gnu.org>
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+*/
+
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include <sys/stat.h>
+#include <errno.h>
+
+#include <cdio/cdio.h>
+
+#include <libvcd/logging.h>
+
+/* Private headers */
+#include "stream_stdio.h"
+#include "util.h"
+
+static const char _rcsid[] = "$Id: stream_stdio.c,v 1.1 2003/10/13 11:47:12 f1rmb Exp $";
+
+#define VCD_STREAM_STDIO_BUFSIZE (128*1024)
+
+typedef struct {
+ char *pathname;
+ FILE *fd;
+ char *fd_buf;
+ off_t st_size; /* used only for source */
+} _UserData;
+
+static int
+_stdio_open_source (void *user_data)
+{
+ _UserData *const ud = user_data;
+
+ if ((ud->fd = fopen (ud->pathname, "rb")))
+ {
+ ud->fd_buf = _vcd_malloc (VCD_STREAM_STDIO_BUFSIZE);
+ setvbuf (ud->fd, ud->fd_buf, _IOFBF, VCD_STREAM_STDIO_BUFSIZE);
+ }
+
+ return (ud->fd == NULL);
+}
+
+static int
+_stdio_open_sink (void *user_data)
+{
+ _UserData *const ud = user_data;
+
+ if ((ud->fd = fopen (ud->pathname, "wb")))
+ {
+ ud->fd_buf = _vcd_malloc (VCD_STREAM_STDIO_BUFSIZE);
+ setvbuf (ud->fd, ud->fd_buf, _IOFBF, VCD_STREAM_STDIO_BUFSIZE);
+ }
+
+ return (ud->fd == NULL);
+}
+
+static int
+_stdio_close(void *user_data)
+{
+ _UserData *const ud = user_data;
+
+ if (fclose (ud->fd))
+ vcd_error ("fclose (): %s", strerror (errno));
+
+ ud->fd = NULL;
+
+ free (ud->fd_buf);
+ ud->fd_buf = NULL;
+
+ return 0;
+}
+
+static void
+_stdio_free(void *user_data)
+{
+ _UserData *const ud = user_data;
+
+ if (ud->pathname)
+ free(ud->pathname);
+
+ if (ud->fd) /* should be NULL anyway... */
+ _stdio_close(user_data);
+
+ free(ud);
+}
+
+static long
+_stdio_seek(void *user_data, long offset)
+{
+ _UserData *const ud = user_data;
+
+ if (fseek (ud->fd, offset, SEEK_SET))
+ vcd_error ("fseek (): %s", strerror (errno));
+
+ return offset;
+}
+
+static long
+_stdio_stat(void *user_data)
+{
+ const _UserData *const ud = user_data;
+
+ return ud->st_size;
+}
+
+static long
+_stdio_read(void *user_data, void *buf, long count)
+{
+ _UserData *const ud = user_data;
+ long read;
+
+ read = fread(buf, 1, count, ud->fd);
+
+ if (read != count)
+ { /* fixme -- ferror/feof */
+ if (feof (ud->fd))
+ {
+ vcd_debug ("fread (): EOF encountered");
+ clearerr (ud->fd);
+ }
+ else if (ferror (ud->fd))
+ {
+ vcd_error ("fread (): %s", strerror (errno));
+ clearerr (ud->fd);
+ }
+ else
+ vcd_debug ("fread (): short read and no EOF?!?");
+ }
+
+ return read;
+}
+
+static long
+_stdio_write(void *user_data, const void *buf, long count)
+{
+ _UserData *const ud = user_data;
+ long written;
+
+ written = fwrite(buf, 1, count, ud->fd);
+
+ if (written != count)
+ vcd_error ("fwrite (): %s", strerror (errno));
+
+ return written;
+}
+
+VcdDataSource*
+vcd_data_source_new_stdio(const char pathname[])
+{
+ VcdDataSource *new_obj = NULL;
+ vcd_data_source_io_functions funcs = { 0, };
+ _UserData *ud = NULL;
+ struct stat statbuf;
+
+ if (stat (pathname, &statbuf) == -1)
+ {
+ vcd_error ("could not stat() file `%s': %s", pathname, strerror (errno));
+ return NULL;
+ }
+
+ ud = _vcd_malloc (sizeof (_UserData));
+
+ ud->pathname = strdup(pathname);
+ ud->st_size = statbuf.st_size; /* let's hope it doesn't change... */
+
+ funcs.open = _stdio_open_source;
+ funcs.seek = _stdio_seek;
+ funcs.stat = _stdio_stat;
+ funcs.read = _stdio_read;
+ funcs.close = _stdio_close;
+ funcs.free = _stdio_free;
+
+ new_obj = vcd_data_source_new(ud, &funcs);
+
+ return new_obj;
+}
+
+
+VcdDataSink*
+vcd_data_sink_new_stdio(const char pathname[])
+{
+ VcdDataSink *new_obj = NULL;
+ vcd_data_sink_io_functions funcs;
+ _UserData *ud = NULL;
+ struct stat statbuf;
+
+ if (stat (pathname, &statbuf) != -1)
+ vcd_warn ("file `%s' exist already, will get overwritten!", pathname);
+
+ ud = _vcd_malloc (sizeof (_UserData));
+
+ memset (&funcs, 0, sizeof (funcs));
+
+ ud->pathname = strdup (pathname);
+
+ funcs.open = _stdio_open_sink;
+ funcs.seek = _stdio_seek;
+ funcs.write = _stdio_write;
+ funcs.close = _stdio_close;
+ funcs.free = _stdio_free;
+
+ new_obj = vcd_data_sink_new (ud, &funcs);
+
+ return new_obj;
+}
+
+
+/*
+ * Local variables:
+ * c-file-style: "gnu"
+ * tab-width: 8
+ * indent-tabs-mode: nil
+ * End:
+ */
diff --git a/src/input/vcd/libvcd/stream_stdio.h b/src/input/vcd/libvcd/stream_stdio.h
new file mode 100644
index 000000000..9141b370e
--- /dev/null
+++ b/src/input/vcd/libvcd/stream_stdio.h
@@ -0,0 +1,43 @@
+/*
+ $Id: stream_stdio.h,v 1.1 2003/10/13 11:47:12 f1rmb Exp $
+
+ Copyright (C) 2000 Herbert Valerio Riedel <hvr@gnu.org>
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+*/
+
+
+#ifndef __VCD_STREAM_STDIO_H__
+#define __VCD_STREAM_STDIO_H__
+
+/* Private headers */
+#include "stream.h"
+
+VcdDataSink*
+vcd_data_sink_new_stdio(const char pathname[]);
+
+VcdDataSource*
+vcd_data_source_new_stdio(const char pathname[]);
+
+#endif /* __VCD_STREAM_STDIO_H__ */
+
+
+/*
+ * Local variables:
+ * c-file-style: "gnu"
+ * tab-width: 8
+ * indent-tabs-mode: nil
+ * End:
+ */
diff --git a/src/input/vcd/libvcd/util.c b/src/input/vcd/libvcd/util.c
new file mode 100644
index 000000000..1607ca87d
--- /dev/null
+++ b/src/input/vcd/libvcd/util.c
@@ -0,0 +1,179 @@
+/*
+ $Id: util.c,v 1.1 2003/10/13 11:47:12 f1rmb Exp $
+
+ Copyright (C) 2000 Herbert Valerio Riedel <hvr@gnu.org>
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+*/
+
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
+
+#include <ctype.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+
+/* Private includes */
+#include "assert.h"
+#include "bytesex.h"
+#include "util.h"
+
+static const char _rcsid[] = "$Id: util.c,v 1.1 2003/10/13 11:47:12 f1rmb Exp $";
+
+size_t
+_vcd_strlenv(char **str_array)
+{
+ size_t n = 0;
+
+ vcd_assert (str_array != NULL);
+
+ while(str_array[n])
+ n++;
+
+ return n;
+}
+
+void
+_vcd_strfreev(char **strv)
+{
+ int n;
+
+ vcd_assert (strv != NULL);
+
+ for(n = 0; strv[n]; n++)
+ free(strv[n]);
+
+ free(strv);
+}
+
+char *
+_vcd_strjoin (char *strv[], unsigned count, const char delim[])
+{
+ size_t len;
+ char *new_str;
+ unsigned n;
+
+ vcd_assert (strv != NULL);
+ vcd_assert (delim != NULL);
+
+ len = (count-1) * strlen (delim);
+
+ for (n = 0;n < count;n++)
+ len += strlen (strv[n]);
+
+ len++;
+
+ new_str = _vcd_malloc (len);
+ new_str[0] = '\0';
+
+ for (n = 0;n < count;n++)
+ {
+ if (n)
+ strcat (new_str, delim);
+ strcat (new_str, strv[n]);
+ }
+
+ return new_str;
+}
+
+char **
+_vcd_strsplit(const char str[], char delim) /* fixme -- non-reentrant */
+{
+ int n;
+ char **strv = NULL;
+ char *_str, *p;
+ char _delim[2] = { 0, 0 };
+
+ vcd_assert (str != NULL);
+
+ _str = strdup(str);
+ _delim[0] = delim;
+
+ vcd_assert (_str != NULL);
+
+ n = 1;
+ p = _str;
+ while(*p)
+ if (*(p++) == delim)
+ n++;
+
+ strv = _vcd_malloc (sizeof (char *) * (n+1));
+
+ n = 0;
+ while((p = strtok(n ? NULL : _str, _delim)) != NULL)
+ strv[n++] = strdup(p);
+
+ free(_str);
+
+ return strv;
+}
+
+void *
+_vcd_malloc (size_t size)
+{
+ void *new_mem = malloc (size);
+
+ vcd_assert (new_mem != NULL);
+
+ memset (new_mem, 0, size);
+
+ return new_mem;
+}
+
+void *
+_vcd_memdup (const void *mem, size_t count)
+{
+ void *new_mem = NULL;
+
+ if (mem)
+ {
+ new_mem = _vcd_malloc (count);
+ memcpy (new_mem, mem, count);
+ }
+
+ return new_mem;
+}
+
+char *
+_vcd_strdup_upper (const char str[])
+{
+ char *new_str = NULL;
+
+ if (str)
+ {
+ char *p;
+
+ p = new_str = strdup (str);
+
+ while (*p)
+ {
+ *p = toupper (*p);
+ p++;
+ }
+ }
+
+ return new_str;
+}
+
+
+/*
+ * Local variables:
+ * c-file-style: "gnu"
+ * tab-width: 8
+ * indent-tabs-mode: nil
+ * End:
+ */
diff --git a/src/input/vcd/libvcd/util.h b/src/input/vcd/libvcd/util.h
new file mode 100644
index 000000000..02cd5de1d
--- /dev/null
+++ b/src/input/vcd/libvcd/util.h
@@ -0,0 +1,108 @@
+/*
+ $Id: util.h,v 1.1 2003/10/13 11:47:12 f1rmb Exp $
+
+ Copyright (C) 2000 Herbert Valerio Riedel <hvr@gnu.org>
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+*/
+
+#ifndef __VCD_UTIL_H__
+#define __VCD_UTIL_H__
+
+#include <stdlib.h>
+#include <libvcd/types.h>
+
+#ifndef __CDIO_UTIL_H__
+#undef MAX
+#define MAX(a, b) (((a) > (b)) ? (a) : (b))
+
+#undef MIN
+#define MIN(a, b) (((a) < (b)) ? (a) : (b))
+
+#undef IN
+#define IN(x, low, high) ((x) >= (low) && (x) <= (high))
+
+#undef CLAMP
+#define CLAMP(x, low, high) (((x) > (high)) ? (high) : (((x) < (low)) ? (low) : (x)))
+
+#endif
+
+static inline unsigned
+_vcd_len2blocks (unsigned len, int blocksize)
+{
+ unsigned blocks;
+
+ blocks = len / blocksize;
+ if (len % blocksize)
+ blocks++;
+
+ return blocks;
+}
+
+/* round up to next block boundary */
+static inline unsigned
+_vcd_ceil2block (unsigned offset, int blocksize)
+{
+ return _vcd_len2blocks (offset, blocksize) * blocksize;
+}
+
+static inline unsigned
+_vcd_ofs_add (unsigned offset, unsigned length, int blocksize)
+{
+ if (blocksize - (offset % blocksize) < length)
+ offset = _vcd_ceil2block (offset, blocksize);
+
+ offset += length;
+
+ return offset;
+}
+
+size_t
+_vcd_strlenv(char **str_array);
+
+char *
+_vcd_strjoin (char *strv[], unsigned count, const char delim[]);
+
+char **
+_vcd_strsplit(const char str[], char delim);
+
+void
+_vcd_strfreev(char **strv);
+
+void *
+_vcd_malloc (size_t size);
+
+void *
+_vcd_memdup (const void *mem, size_t count);
+
+char *
+_vcd_strdup_upper (const char str[]);
+
+static inline const char *
+_vcd_bool_str (bool b)
+{
+ return b ? "yes" : "no";
+}
+
+#endif /* __VCD_UTIL_H__ */
+
+
+/*
+ * Local variables:
+ * c-file-style: "gnu"
+ * tab-width: 8
+ * indent-tabs-mode: nil
+ * End:
+ */
diff --git a/src/input/vcd/libvcd/vcd.c b/src/input/vcd/libvcd/vcd.c
new file mode 100644
index 000000000..b61cf0d27
--- /dev/null
+++ b/src/input/vcd/libvcd/vcd.c
@@ -0,0 +1,2407 @@
+/*
+ $Id: vcd.c,v 1.1 2003/10/13 11:47:12 f1rmb Exp $
+
+ Copyright (C) 2000 Herbert Valerio Riedel <hvr@gnu.org>
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+*/
+
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <ctype.h>
+#include <math.h>
+
+#include <cdio/cdio.h>
+#include <cdio/iso9660.h>
+
+/* public headers */
+#include <libvcd/types.h>
+#include <libvcd/info.h>
+
+#include <libvcd/files.h>
+#include <libvcd/sector.h>
+#include <libvcd/logging.h>
+
+/* Private headers */
+#include "assert.h"
+#include "dict.h"
+#include "directory.h"
+#include "obj.h"
+#include "pbc.h"
+#include "salloc.h"
+#include "util.h"
+#include "vcd.h"
+
+static const char _rcsid[] = "$Id: vcd.c,v 1.1 2003/10/13 11:47:12 f1rmb Exp $";
+
+static const char zero[CDIO_CD_FRAMESIZE_RAW] = { 0, };
+
+#define DEFAULT_ISO_PREPARER_ID "GNU VCDImager " VERSION " " HOST_ARCH
+
+/* exported private functions
+ */
+
+mpeg_sequence_t *
+_vcd_obj_get_sequence_by_id (VcdObj *obj, const char sequence_id[])
+{
+ VcdListNode *node;
+
+ vcd_assert (sequence_id != NULL);
+ vcd_assert (obj != NULL);
+
+ _VCD_LIST_FOREACH (node, obj->mpeg_sequence_list)
+ {
+ mpeg_sequence_t *_sequence = _vcd_list_node_data (node);
+
+ if (_sequence->id && !strcmp (sequence_id, _sequence->id))
+ return _sequence;
+ }
+
+ return NULL;
+}
+
+mpeg_sequence_t *
+_vcd_obj_get_sequence_by_entry_id (VcdObj *obj, const char entry_id[])
+{
+ VcdListNode *node;
+
+ vcd_assert (entry_id != NULL);
+ vcd_assert (obj != NULL);
+
+ _VCD_LIST_FOREACH (node, obj->mpeg_sequence_list)
+ {
+ mpeg_sequence_t *_sequence = _vcd_list_node_data (node);
+ VcdListNode *node2;
+
+ /* default entry point */
+ if (_sequence->default_entry_id
+ && !strcmp (entry_id, _sequence->default_entry_id))
+ return _sequence;
+
+ /* additional entry points */
+ _VCD_LIST_FOREACH (node2, _sequence->entry_list)
+ {
+ entry_t *_entry = _vcd_list_node_data (node2);
+
+ if (_entry->id
+ && !strcmp (entry_id, _entry->id))
+ return _sequence;
+ }
+ }
+
+ /* not found */
+
+ return NULL;
+}
+
+mpeg_segment_t *
+_vcd_obj_get_segment_by_id (VcdObj *obj, const char segment_id[])
+{
+ VcdListNode *node;
+
+ vcd_assert (segment_id != NULL);
+ vcd_assert (obj != NULL);
+
+ _VCD_LIST_FOREACH (node, obj->mpeg_segment_list)
+ {
+ mpeg_segment_t *_segment = _vcd_list_node_data (node);
+
+ if (_segment->id && !strcmp (segment_id, _segment->id))
+ return _segment;
+ }
+
+ return NULL;
+}
+
+bool
+_vcd_obj_has_cap_p (const VcdObj *obj, enum vcd_capability_t capability)
+{
+ switch (capability)
+ {
+ case _CAP_VALID:
+ switch (obj->type)
+ {
+ case VCD_TYPE_VCD:
+ case VCD_TYPE_VCD11:
+ case VCD_TYPE_VCD2:
+ case VCD_TYPE_SVCD:
+ case VCD_TYPE_HQVCD:
+ return true;
+ break;
+
+ case VCD_TYPE_INVALID:
+ return false;
+ break;
+ }
+ break;
+
+ case _CAP_MPEG2:
+ switch (obj->type)
+ {
+ case VCD_TYPE_VCD:
+ case VCD_TYPE_VCD11:
+ case VCD_TYPE_VCD2:
+ case VCD_TYPE_INVALID:
+ return false;
+ break;
+
+ case VCD_TYPE_SVCD:
+ case VCD_TYPE_HQVCD:
+ return true;
+ break;
+ }
+ break;
+
+ case _CAP_PBC:
+ switch (obj->type)
+ {
+ case VCD_TYPE_VCD:
+ case VCD_TYPE_VCD11:
+ case VCD_TYPE_INVALID:
+ return false;
+ break;
+
+ case VCD_TYPE_VCD2:
+ case VCD_TYPE_SVCD:
+ case VCD_TYPE_HQVCD:
+ return true;
+ break;
+ }
+ break;
+
+ case _CAP_PBC_X:
+ switch (obj->type)
+ {
+ case VCD_TYPE_VCD:
+ case VCD_TYPE_VCD11:
+ case VCD_TYPE_INVALID:
+ case VCD_TYPE_SVCD:
+ case VCD_TYPE_HQVCD:
+ return false;
+ break;
+
+ case VCD_TYPE_VCD2:
+ return true;
+ break;
+ }
+ break;
+
+ case _CAP_4C_SVCD:
+ switch (obj->type)
+ {
+ case VCD_TYPE_VCD:
+ case VCD_TYPE_VCD11:
+ case VCD_TYPE_INVALID:
+ case VCD_TYPE_VCD2:
+ return false;
+ break;
+
+ case VCD_TYPE_SVCD:
+ case VCD_TYPE_HQVCD:
+ return true;
+ break;
+ }
+ break;
+
+ case _CAP_PAL_BITS:
+ return _vcd_obj_has_cap_p (obj, _CAP_PBC); /* for now */
+ break;
+
+ case _CAP_MPEG1:
+ return !_vcd_obj_has_cap_p (obj, _CAP_MPEG2); /* for now */
+ break;
+
+ case _CAP_TRACK_MARGINS:
+ return !_vcd_obj_has_cap_p (obj, _CAP_MPEG2); /* for now */
+ break;
+ }
+
+ vcd_assert_not_reached ();
+ return false;
+}
+
+/*
+ * public methods
+ */
+
+VcdObj *
+vcd_obj_new (vcd_type_t vcd_type)
+{
+ VcdObj *new_obj = NULL;
+ static bool _first = true;
+
+ if (_first)
+ {
+#if defined(_DEVELOPMENT_)
+ vcd_warn ("initializing libvcd %s [%s]", VERSION, HOST_ARCH);
+ vcd_warn (" ");
+ vcd_warn (" this is the UNSTABLE development branch!");
+ vcd_warn (" use only if you know what you are doing");
+ vcd_warn (" see http://www.hvrlab.org/~hvr/vcdimager/ for more information");
+ vcd_warn (" ");
+#else
+ vcd_debug ("initializing libvcd %s [%s]", VERSION, HOST_ARCH);
+#endif
+ _first = false;
+ }
+
+ new_obj = _vcd_malloc (sizeof (VcdObj));
+ new_obj->type = vcd_type;
+
+ if (!_vcd_obj_has_cap_p (new_obj, _CAP_VALID))
+ {
+ vcd_error ("VCD type not supported");
+ free (new_obj);
+ return NULL;
+ }
+
+ if (vcd_type == VCD_TYPE_VCD)
+ vcd_warn ("VCD 1.0 support is experimental -- user feedback needed!");
+
+ new_obj->iso_volume_label = strdup ("");
+ new_obj->iso_publisher_id = strdup ("");
+ new_obj->iso_application_id = strdup ("");
+ new_obj->iso_preparer_id = _vcd_strdup_upper (DEFAULT_ISO_PREPARER_ID);
+ new_obj->info_album_id = strdup ("");
+ new_obj->info_volume_count = 1;
+ new_obj->info_volume_number = 1;
+
+ new_obj->custom_file_list = _vcd_list_new ();
+ new_obj->custom_dir_list = _vcd_list_new ();
+
+
+ new_obj->mpeg_sequence_list = _vcd_list_new ();
+
+ new_obj->mpeg_segment_list = _vcd_list_new ();
+
+ new_obj->pbc_list = _vcd_list_new ();
+
+ /* gap's defined by IEC-10149 / ECMA-130 */
+
+ /* pre-gap's for tracks but the first one */
+ new_obj->track_pregap = CDIO_PREGAP_SECTORS;
+ /* post-gap after last track */
+ new_obj->leadout_pregap = CDIO_POSTGAP_SECTORS;
+
+ if (_vcd_obj_has_cap_p (new_obj, _CAP_TRACK_MARGINS))
+ {
+ new_obj->track_front_margin = 30;
+ new_obj->track_rear_margin = 45;
+ }
+ else
+ {
+ new_obj->track_front_margin = 0;
+ new_obj->track_rear_margin = 0;
+ }
+
+ return new_obj;
+}
+
+int
+vcd_obj_remove_item (VcdObj *obj, const char id[])
+{
+ vcd_warn ("vcd_obj_remove_item('%s') not implemented yet!", id);
+
+ return -1;
+}
+
+static void
+_vcd_obj_remove_mpeg_track (VcdObj *obj, int track_id)
+{
+ int length;
+ mpeg_sequence_t *track = NULL;
+ VcdListNode *node = NULL;
+
+ vcd_assert (track_id >= 0);
+
+ node = _vcd_list_at (obj->mpeg_sequence_list, track_id);
+
+ vcd_assert (node != NULL);
+
+ track = (mpeg_sequence_t *) _vcd_list_node_data (node);
+
+ vcd_mpeg_source_destroy (track->source, true);
+
+ length = track->info->packets;
+ length += obj->track_pregap + obj->track_front_margin + 0 + obj->track_rear_margin;
+
+ /* fixup offsets */
+ {
+ VcdListNode *node2 = node;
+ while ((node2 = _vcd_list_node_next (node2)) != NULL)
+ ((mpeg_sequence_t *) _vcd_list_node_data (node))->relative_start_extent -= length;
+ }
+
+ obj->relative_end_extent -= length;
+
+ /* shift up */
+ _vcd_list_node_free (node, true);
+}
+
+int
+vcd_obj_append_segment_play_item (VcdObj *obj, VcdMpegSource *mpeg_source,
+ const char item_id[])
+{
+ mpeg_segment_t *segment = NULL;
+
+ vcd_assert (obj != NULL);
+ vcd_assert (mpeg_source != NULL);
+
+ if (!_vcd_obj_has_cap_p (obj, _CAP_PBC))
+ {
+ vcd_error ("segment play items not supported for this vcd type");
+ return -1;
+ }
+
+ if (!item_id)
+ {
+ vcd_error ("no id given for segment play item");
+ return -1;
+ }
+
+ if (_vcd_pbc_lookup (obj, item_id))
+ {
+ vcd_error ("item id (%s) exists already", item_id);
+ return -1;
+ }
+
+ vcd_info ("scanning mpeg segment item #%d for scanpoints...",
+ _vcd_list_length (obj->mpeg_segment_list));
+
+ vcd_mpeg_source_scan (mpeg_source, !obj->relaxed_aps,
+ obj->update_scan_offsets, NULL, NULL);
+
+ if (vcd_mpeg_source_get_info (mpeg_source)->packets == 0)
+ {
+ vcd_error ("mpeg is empty?");
+ return -1;
+ }
+
+ /* create list node */
+
+ segment = _vcd_malloc (sizeof (mpeg_sequence_t));
+
+ segment->source = mpeg_source;
+
+ segment->id = strdup (item_id);
+
+ segment->info = vcd_mpeg_source_get_info (mpeg_source);
+ segment->segment_count = _vcd_len2blocks (segment->info->packets, 150);
+
+ segment->pause_list = _vcd_list_new ();
+
+ vcd_debug ("SPI length is %d sector(s), allocated %d segment(s)",
+ segment->info->packets,
+ segment->segment_count);
+
+ _vcd_list_append (obj->mpeg_segment_list, segment);
+
+ return 0;
+}
+
+int
+vcd_obj_append_sequence_play_item (VcdObj *obj, VcdMpegSource *mpeg_source,
+ const char item_id[],
+ const char default_entry_id[])
+{
+ unsigned length;
+ mpeg_sequence_t *sequence = NULL;
+ int track_no = _vcd_list_length (obj->mpeg_sequence_list);
+
+ vcd_assert (obj != NULL);
+ vcd_assert (mpeg_source != NULL);
+
+ if (item_id && _vcd_pbc_lookup (obj, item_id))
+ {
+ vcd_error ("item id (%s) exist already", item_id);
+ return -1;
+ }
+
+ if (default_entry_id && _vcd_pbc_lookup (obj, default_entry_id))
+ {
+ vcd_error ("default entry id (%s) exist already", default_entry_id);
+ return -1;
+ }
+
+ if (default_entry_id && item_id && !strcmp (item_id, default_entry_id))
+ {
+ vcd_error ("default entry id == item id (%s)", item_id);
+ return -1;
+ }
+
+ vcd_info ("scanning mpeg sequence item #%d for scanpoints...", track_no);
+ vcd_mpeg_source_scan (mpeg_source, !obj->relaxed_aps,
+ obj->update_scan_offsets, NULL, NULL);
+
+ sequence = _vcd_malloc (sizeof (mpeg_sequence_t));
+
+ sequence->source = mpeg_source;
+
+ if (item_id)
+ sequence->id = strdup (item_id);
+
+ if (default_entry_id)
+ sequence->default_entry_id = strdup (default_entry_id);
+
+ sequence->info = vcd_mpeg_source_get_info (mpeg_source);
+ length = sequence->info->packets;
+
+ sequence->entry_list = _vcd_list_new ();
+ sequence->pause_list = _vcd_list_new ();
+
+ obj->relative_end_extent += obj->track_pregap;
+ sequence->relative_start_extent = obj->relative_end_extent;
+
+ obj->relative_end_extent += obj->track_front_margin + length + obj->track_rear_margin;
+
+ /* sanity checks */
+
+ if (length < 75)
+ vcd_warn ("mpeg stream shorter than 75 sectors");
+
+ if (!_vcd_obj_has_cap_p (obj, _CAP_PAL_BITS)
+ && vcd_mpeg_get_norm (&sequence->info->shdr[0]) != MPEG_NORM_FILM
+ && vcd_mpeg_get_norm (&sequence->info->shdr[0]) != MPEG_NORM_NTSC)
+ vcd_warn ("VCD 1.x should contain only NTSC/FILM video (may work with PAL nevertheless)");
+
+ if (!_vcd_obj_has_cap_p (obj, _CAP_MPEG1)
+ && sequence->info->version == MPEG_VERS_MPEG1)
+ vcd_warn ("this VCD type should not contain MPEG1 streams");
+
+ if (!_vcd_obj_has_cap_p (obj, _CAP_MPEG2)
+ && sequence->info->version == MPEG_VERS_MPEG2)
+ vcd_warn ("this VCD type should not contain MPEG2 streams");
+
+ if (!sequence->info->shdr[0].seen
+ || sequence->info->shdr[1].seen
+ || sequence->info->shdr[2].seen)
+ vcd_warn ("sequence items should contain a motion video stream!");
+
+ {
+ int i;
+
+ for (i = 0; i < 3; i++)
+ {
+ if (sequence->info->ahdr[i].seen)
+ {
+ if (i && !_vcd_obj_has_cap_p (obj, _CAP_MPEG2))
+ vcd_warn ("audio stream #%d not supported by this VCD type", i);
+
+ if (sequence->info->ahdr[i].sampfreq != 44100)
+ vcd_warn ("audio stream #%d has sampling frequency %d Hz (should be 44100 Hz)",
+ i, sequence->info->ahdr[i].sampfreq);
+
+ if (sequence->info->ahdr[i].layer != 2)
+ vcd_warn ("audio stream #%d is not layer II", i);
+
+ if (_vcd_obj_has_cap_p (obj, _CAP_MPEG1)
+ && sequence->info->ahdr[i].bitrate != 224*1024)
+ vcd_warn ("audio stream #%d has bitrate %d kbps (should be 224 kbps for this vcd type)",
+ i, sequence->info->ahdr[i].bitrate);
+ }
+ else if (!i && !_vcd_obj_has_cap_p (obj, _CAP_MPEG2))
+ {
+ vcd_warn ("this VCD type requires an audio stream to be present");
+ }
+ }
+ }
+
+ /* vcd_debug ("track# %d's detected playing time: %.2f seconds", */
+ /* track_no, sequence->info->playing_time); */
+
+ _vcd_list_append (obj->mpeg_sequence_list, sequence);
+
+ return track_no;
+}
+
+static int
+_pause_cmp (pause_t *ent1, pause_t *ent2)
+{
+ if (ent1->time < ent2->time)
+ return -1;
+
+ if (ent1->time > ent2->time)
+ return 1;
+
+ return 0;
+}
+
+int
+vcd_obj_add_sequence_pause (VcdObj *obj, const char sequence_id[],
+ double pause_time, const char pause_id[])
+{
+ mpeg_sequence_t *_sequence;
+
+ vcd_assert (obj != NULL);
+
+ if (sequence_id)
+ _sequence = _vcd_obj_get_sequence_by_id (obj, sequence_id);
+ else
+ _sequence = _vcd_list_node_data (_vcd_list_end (obj->mpeg_sequence_list));
+
+ if (!_sequence)
+ {
+ vcd_error ("sequence id `%s' not found", sequence_id);
+ return -1;
+ }
+
+ if (pause_id)
+ vcd_warn ("pause id ignored...");
+
+ {
+ pause_t *_pause = _vcd_malloc (sizeof (pause_t));
+
+ if (pause_id)
+ _pause->id = strdup (pause_id);
+ _pause->time = pause_time;
+
+ _vcd_list_append (_sequence->pause_list, _pause);
+ }
+
+ _vcd_list_sort (_sequence->pause_list,
+ (_vcd_list_cmp_func) _pause_cmp);
+
+ vcd_debug ("added autopause point at %f", pause_time);
+
+ return 0;
+}
+
+int
+vcd_obj_add_segment_pause (VcdObj *obj, const char segment_id[],
+ double pause_time, const char pause_id[])
+{
+ mpeg_segment_t *_segment;
+
+ vcd_assert (obj != NULL);
+
+ if (segment_id)
+ _segment = _vcd_obj_get_segment_by_id (obj, segment_id);
+ else
+ _segment = _vcd_list_node_data (_vcd_list_end (obj->mpeg_segment_list));
+
+ if (!_segment)
+ {
+ vcd_error ("segment id `%s' not found", segment_id);
+ return -1;
+ }
+
+ if (pause_id)
+ vcd_warn ("pause id ignored...");
+
+ {
+ pause_t *_pause = _vcd_malloc (sizeof (pause_t));
+
+ if (pause_id)
+ _pause->id = strdup (pause_id);
+ _pause->time = pause_time;
+
+ _vcd_list_append (_segment->pause_list, _pause);
+ }
+
+ _vcd_list_sort (_segment->pause_list,
+ (_vcd_list_cmp_func) _pause_cmp);
+
+ vcd_debug ("added autopause point at %f", pause_time);
+
+ return 0;
+}
+
+static int
+_entry_cmp (entry_t *ent1, entry_t *ent2)
+{
+ if (ent1->time < ent2->time)
+ return -1;
+
+ if (ent1->time > ent2->time)
+ return 1;
+
+ return 0;
+}
+
+int
+vcd_obj_add_sequence_entry (VcdObj *obj, const char sequence_id[],
+ double entry_time, const char entry_id[])
+{
+ mpeg_sequence_t *_sequence;
+
+ vcd_assert (obj != NULL);
+
+ if (sequence_id)
+ _sequence = _vcd_obj_get_sequence_by_id (obj, sequence_id);
+ else
+ _sequence = _vcd_list_node_data (_vcd_list_end (obj->mpeg_sequence_list));
+
+ if (!_sequence)
+ {
+ vcd_error ("sequence id `%s' not found", sequence_id);
+ return -1;
+ }
+
+ if (_vcd_list_length (_sequence->entry_list) >= MAX_SEQ_ENTRIES)
+ {
+ vcd_error ("only %d entries per sequence allowed!", MAX_SEQ_ENTRIES);
+ return -1;
+ }
+
+ if (entry_id && _vcd_pbc_lookup (obj, entry_id))
+ {
+ vcd_error ("item id (%s) exists already", entry_id);
+ return -1;
+ }
+
+ {
+ entry_t *_entry = _vcd_malloc (sizeof (entry_t));
+
+ if (entry_id)
+ _entry->id = strdup (entry_id);
+ _entry->time = entry_time;
+
+ _vcd_list_append (_sequence->entry_list, _entry);
+ }
+
+ _vcd_list_sort (_sequence->entry_list,
+ (_vcd_list_cmp_func) _entry_cmp);
+
+ return 0;
+}
+
+void
+vcd_obj_destroy (VcdObj *obj)
+{
+ VcdListNode *node;
+
+ vcd_assert (obj != NULL);
+ vcd_assert (!obj->in_output);
+
+ free (obj->iso_volume_label);
+ free (obj->iso_application_id);
+
+ _VCD_LIST_FOREACH (node, obj->custom_file_list)
+ {
+ custom_file_t *p = _vcd_list_node_data (node);
+
+ free (p->iso_pathname);
+ }
+
+ _vcd_list_free (obj->custom_file_list, true);
+
+ _vcd_list_free (obj->custom_dir_list, true);
+
+ while (_vcd_list_length (obj->mpeg_sequence_list))
+ _vcd_obj_remove_mpeg_track (obj, 0);
+ _vcd_list_free (obj->mpeg_sequence_list, true);
+
+ free (obj);
+}
+
+int
+vcd_obj_set_param_uint (VcdObj *obj, vcd_parm_t param, unsigned arg)
+{
+ vcd_assert (obj != NULL);
+
+ switch (param)
+ {
+ case VCD_PARM_VOLUME_COUNT:
+ obj->info_volume_count = arg;
+ if (!IN (obj->info_volume_count, 1, 65535))
+ {
+ obj->info_volume_count = CLAMP (obj->info_volume_count, 1, 65535);
+ vcd_warn ("volume count out of range, clamping to range");
+ }
+ vcd_debug ("changed volume count to %u", obj->info_volume_count);
+ break;
+
+ case VCD_PARM_VOLUME_NUMBER:
+ obj->info_volume_number = arg;
+ if (!IN (obj->info_volume_number, 0, 65534))
+ {
+ obj->info_volume_number = CLAMP (obj->info_volume_number, 0, 65534);
+ vcd_warn ("volume number out of range, clamping to range");
+ }
+ vcd_debug ("changed volume number to %u", obj->info_volume_number);
+ break;
+
+ case VCD_PARM_RESTRICTION:
+ obj->info_restriction = arg;
+ if (!IN (obj->info_restriction, 0, 3))
+ {
+ obj->info_restriction = CLAMP (obj->info_restriction, 0, 65534);
+ vcd_warn ("restriction out of range, clamping to range");
+ }
+ vcd_debug ("changed restriction number to %u", obj->info_restriction);
+ break;
+
+ case VCD_PARM_LEADOUT_PREGAP:
+ obj->leadout_pregap = arg;
+ if (!IN (obj->leadout_pregap, 0, 300))
+ {
+ obj->leadout_pregap = CLAMP (obj->leadout_pregap, 0, 300);
+ vcd_warn ("ledout pregap out of range, clamping to allowed range");
+ }
+ if (obj->leadout_pregap < CDIO_PREGAP_SECTORS)
+ vcd_warn ("track leadout pregap set below %d sectors; created (s)vcd may be non-working",
+ CDIO_PREGAP_SECTORS);
+
+ vcd_debug ("changed leadout pregap to %u", obj->leadout_pregap);
+ break;
+
+ case VCD_PARM_TRACK_PREGAP:
+ obj->track_pregap = arg;
+ if (!IN (obj->track_pregap, 1, 300))
+ {
+ obj->track_pregap = CLAMP (obj->track_pregap, 1, 300);
+ vcd_warn ("track pregap out of range, clamping to allowed range");
+ }
+ if (obj->track_pregap < CDIO_PREGAP_SECTORS)
+ vcd_warn ("track pre gap set below %d sectors; created (S)VCD may be non-working",
+ CDIO_PREGAP_SECTORS);
+ vcd_debug ("changed track pregap to %u", obj->track_pregap);
+ break;
+
+ case VCD_PARM_TRACK_FRONT_MARGIN:
+ obj->track_front_margin = arg;
+ if (!IN (obj->track_front_margin, 0, CDIO_PREGAP_SECTORS))
+ {
+ obj->track_front_margin = CLAMP (obj->track_front_margin, 0,
+ CDIO_PREGAP_SECTORS);
+ vcd_warn ("front margin out of range, clamping to allowed range");
+ }
+ if (_vcd_obj_has_cap_p (obj, _CAP_TRACK_MARGINS)
+ && obj->track_front_margin < 15)
+ vcd_warn ("front margin set smaller than recommended (%d < 15 sectors) for disc type used",
+ obj->track_front_margin);
+
+ vcd_debug ("changed front margin to %u", obj->track_front_margin);
+ break;
+
+ case VCD_PARM_TRACK_REAR_MARGIN:
+ obj->track_rear_margin = arg;
+ if (!IN (obj->track_rear_margin, 0, CDIO_POSTGAP_SECTORS))
+ {
+ obj->track_rear_margin = CLAMP (obj->track_rear_margin, 0,
+ CDIO_POSTGAP_SECTORS);
+ vcd_warn ("rear margin out of range, clamping to allowed range");
+ }
+ if (_vcd_obj_has_cap_p (obj, _CAP_TRACK_MARGINS)
+ && obj->track_rear_margin < 15)
+ vcd_warn ("rear margin set smaller than recommended (%d < 15 sectors) for disc type used",
+ obj->track_rear_margin);
+ vcd_debug ("changed rear margin to %u", obj->track_rear_margin);
+ break;
+
+ default:
+ vcd_assert_not_reached ();
+ break;
+ }
+
+ return 0;
+}
+
+int
+vcd_obj_set_param_str (VcdObj *obj, vcd_parm_t param, const char *arg)
+{
+ vcd_assert (obj != NULL);
+ vcd_assert (arg != NULL);
+
+ switch (param)
+ {
+ case VCD_PARM_VOLUME_ID:
+ free (obj->iso_volume_label);
+ obj->iso_volume_label = strdup (arg);
+ if (strlen (obj->iso_volume_label) > 32)
+ {
+ obj->iso_volume_label[32] = '\0';
+ vcd_warn ("Volume label too long, will be truncated");
+ }
+ vcd_debug ("changed volume label to `%s'", obj->iso_volume_label);
+ break;
+
+ case VCD_PARM_PUBLISHER_ID:
+ free (obj->iso_publisher_id);
+ obj->iso_publisher_id = strdup (arg);
+ if (strlen (obj->iso_publisher_id) > 128)
+ {
+ obj->iso_publisher_id[128] = '\0';
+ vcd_warn ("Publisher ID too long, will be truncated");
+ }
+ vcd_debug ("changed publisher id to `%s'", obj->iso_publisher_id);
+ break;
+
+ case VCD_PARM_PREPARER_ID:
+ free (obj->iso_preparer_id);
+ obj->iso_preparer_id = strdup (arg);
+ if (strlen (obj->iso_preparer_id) > 128)
+ {
+ obj->iso_preparer_id[128] = '\0';
+ vcd_warn ("Preparer ID too long, will be truncated");
+ }
+ vcd_debug ("changed preparer id to `%s'", obj->iso_preparer_id);
+ break;
+
+ case VCD_PARM_APPLICATION_ID:
+ free (obj->iso_application_id);
+ obj->iso_application_id = strdup (arg);
+ if (strlen (obj->iso_application_id) > 128)
+ {
+ obj->iso_application_id[128] = '\0';
+ vcd_warn ("Application ID too long, will be truncated");
+ }
+ vcd_debug ("changed application id to `%s'", obj->iso_application_id);
+ break;
+
+ case VCD_PARM_ALBUM_ID:
+ free (obj->info_album_id);
+ obj->info_album_id = strdup (arg);
+ if (strlen (obj->info_album_id) > 16)
+ {
+ obj->info_album_id[16] = '\0';
+ vcd_warn ("Album ID too long, will be truncated");
+ }
+ vcd_debug ("changed album id to `%s'", obj->info_album_id);
+ break;
+
+ default:
+ vcd_assert_not_reached ();
+ break;
+ }
+
+ return 0;
+}
+
+int
+vcd_obj_set_param_bool (VcdObj *obj, vcd_parm_t param, bool arg)
+{
+ vcd_assert (obj != NULL);
+
+ switch (param)
+ {
+ case VCD_PARM_RELAXED_APS:
+ obj->relaxed_aps = arg ? true : false;
+ vcd_debug ("changing 'relaxed aps' to %d", obj->relaxed_aps);
+ break;
+
+ case VCD_PARM_NEXT_VOL_LID2:
+ obj->info_use_lid2 = arg ? true : false;
+ vcd_debug ("changing 'next volume use lid 2' to %d", obj->info_use_lid2);
+ break;
+
+ case VCD_PARM_NEXT_VOL_SEQ2:
+ obj->info_use_seq2 = arg ? true : false;
+ vcd_debug ("changing 'next volume use sequence 2' to %d", obj->info_use_seq2);
+ break;
+
+ case VCD_PARM_SVCD_VCD3_MPEGAV:
+ if (obj->type == VCD_TYPE_SVCD)
+ {
+ if ((obj->svcd_vcd3_mpegav = arg ? true : false))
+ vcd_warn ("!! enabling deprecated VCD3.0 MPEGAV folder --"
+ " SVCD will not be IEC62107 compliant !!");
+ }
+ else
+ vcd_error ("parameter not applicable for vcd type");
+ break;
+
+ case VCD_PARM_SVCD_VCD3_ENTRYSVD:
+ if (obj->type == VCD_TYPE_SVCD)
+ {
+ if ((obj->svcd_vcd3_entrysvd = arg ? true : false))
+ vcd_warn ("!! enabling deprecated VCD3.0 ENTRYSVD signature --"
+ " SVCD will not be IEC62107 compliant !!");
+ }
+ else
+ vcd_error ("parameter not applicable for vcd type");
+ break;
+
+ case VCD_PARM_SVCD_VCD3_TRACKSVD:
+ if (obj->type == VCD_TYPE_SVCD)
+ {
+ if ((obj->svcd_vcd3_tracksvd = arg ? true : false))
+ vcd_warn ("!! enabling deprecated VCD3.0 TRACK.SVD format --"
+ " SVCD will not be IEC62107 compliant !!");
+ }
+ else
+ vcd_error ("parameter not applicable for vcd type");
+ break;
+
+ case VCD_PARM_UPDATE_SCAN_OFFSETS:
+ if (_vcd_obj_has_cap_p (obj, _CAP_4C_SVCD))
+ {
+ obj->update_scan_offsets = arg ? true : false;
+ vcd_debug ("changing 'update scan offsets' to %d", obj->update_scan_offsets);
+ }
+ else
+ vcd_error ("parameter not applicable for vcd type");
+ break;
+
+ case VCD_PARM_LEADOUT_PAUSE:
+ vcd_warn ("use of 'leadout pause' is deprecated and may be removed in later releases;"
+ " use 'leadout pregap' instead");
+ vcd_obj_set_param_uint (obj, VCD_PARM_LEADOUT_PREGAP,
+ (arg ? CDIO_PREGAP_SECTORS : 0));
+ break;
+
+ default:
+ vcd_assert_not_reached ();
+ break;
+ }
+
+ return 0;
+}
+
+int
+vcd_obj_add_dir (VcdObj *obj, const char iso_pathname[])
+{
+ char *_iso_pathname;
+
+ vcd_assert (obj != NULL);
+ vcd_assert (iso_pathname != NULL);
+
+ _iso_pathname = _vcd_strdup_upper (iso_pathname);
+
+ if (!iso9660_dirname_valid_p (_iso_pathname))
+ {
+ vcd_error("pathname `%s' is not a valid iso pathname",
+ _iso_pathname);
+ free (_iso_pathname);
+ return 1;
+ }
+
+ _vcd_list_append (obj->custom_dir_list, _iso_pathname);
+
+ _vcd_list_sort (obj->custom_dir_list,
+ (_vcd_list_cmp_func) strcmp);
+
+ return 0;
+}
+
+int
+vcd_obj_add_file (VcdObj *obj, const char iso_pathname[],
+ VcdDataSource *file, bool raw_flag)
+{
+ uint32_t size = 0, sectors = 0;
+
+ vcd_assert (obj != NULL);
+ vcd_assert (file != NULL);
+ vcd_assert (iso_pathname != NULL);
+ vcd_assert (strlen (iso_pathname) > 0);
+ vcd_assert (file != NULL);
+
+ size = vcd_data_source_stat (file);
+
+ /* close file to save file descriptors */
+ vcd_data_source_close (file);
+
+ if (raw_flag)
+ {
+ if (!size)
+ {
+ vcd_error("raw mode2 file must not be empty\n");
+ return 1;
+ }
+
+ sectors = size / M2RAW_SECTOR_SIZE;
+
+ if (size % M2RAW_SECTOR_SIZE)
+ {
+ vcd_error("raw mode2 file must have size multiple of %d \n",
+ M2RAW_SECTOR_SIZE);
+ return 1;
+ }
+ }
+ else
+ sectors = _vcd_len2blocks (size, CDIO_CD_FRAMESIZE);
+
+ {
+ custom_file_t *p;
+ char *_iso_pathname = _vcd_strdup_upper (iso_pathname);
+
+ if (!iso9660_pathname_valid_p (_iso_pathname))
+ {
+ vcd_error("pathname `%s' is not a valid iso pathname",
+ _iso_pathname);
+ free (_iso_pathname);
+ return 1;
+ }
+
+ p = _vcd_malloc (sizeof (custom_file_t));
+
+ p->file = file;
+ p->iso_pathname = _iso_pathname;
+ p->raw_flag = raw_flag;
+
+ p->size = size;
+ p->start_extent = 0;
+ p->sectors = sectors;
+
+ _vcd_list_append (obj->custom_file_list, p);
+ }
+
+ return 0;
+}
+
+static void
+_finalize_vcd_iso_track_allocation (VcdObj *obj)
+{
+ int n;
+ VcdListNode *node;
+
+ uint32_t dir_secs = SECTOR_NIL;
+
+ _dict_clean (obj);
+
+ /* pre-alloc 16 blocks of ISO9660 required silence */
+ if (_vcd_salloc (obj->iso_bitmap, 0, 16) == SECTOR_NIL)
+ vcd_assert_not_reached ();
+
+ /* keep karaoke sectors blank -- well... guess I'm too paranoid :) */
+ if (_vcd_salloc (obj->iso_bitmap, 75, 75) == SECTOR_NIL)
+ vcd_assert_not_reached ();
+
+ /* pre-alloc descriptors, PVD */
+ _dict_insert (obj, "pvd", ISO_PVD_SECTOR, 1, SM_EOR); /* EOR */
+ /* EVD */
+ _dict_insert (obj, "evd", ISO_EVD_SECTOR, 1, SM_EOR|SM_EOF); /* EOR+EOF */
+
+ /* reserve for iso directory */
+ dir_secs = _vcd_salloc (obj->iso_bitmap, 18, 75-18);
+
+ /* VCD information area */
+
+ _dict_insert (obj, "info", INFO_VCD_SECTOR, 1, SM_EOF); /* INFO.VCD */ /* EOF */
+ _dict_insert (obj, "entries", ENTRIES_VCD_SECTOR, 1, SM_EOF); /* ENTRIES.VCD */ /* EOF */
+
+ /* PBC */
+
+ if (_vcd_pbc_available (obj))
+ {
+ _dict_insert (obj, "lot", LOT_VCD_SECTOR, LOT_VCD_SIZE, SM_EOF); /* LOT.VCD */ /* EOF */
+ _dict_insert (obj, "psd", PSD_VCD_SECTOR,
+ _vcd_len2blocks (get_psd_size (obj, false), ISO_BLOCKSIZE), SM_EOF); /* PSD.VCD */ /* EOF */
+ }
+
+ if (_vcd_obj_has_cap_p (obj, _CAP_4C_SVCD))
+ {
+ _dict_insert (obj, "tracks", SECTOR_NIL, 1, SM_EOF); /* TRACKS.SVD */
+ _dict_insert (obj, "search", SECTOR_NIL,
+ _vcd_len2blocks (get_search_dat_size (obj), ISO_BLOCKSIZE), SM_EOF); /* SEARCH.DAT */
+
+ vcd_assert (_dict_get_bykey (obj, "tracks")->sector > INFO_VCD_SECTOR);
+ vcd_assert (_dict_get_bykey (obj, "search")->sector > INFO_VCD_SECTOR);
+ }
+
+ /* done with primary information area */
+
+ obj->mpeg_segment_start_extent =
+ _vcd_len2blocks (_vcd_salloc_get_highest (obj->iso_bitmap) + 1, 75) * 75;
+
+ /* salloc up to end of vcd sector */
+ for(n = 0;n < obj->mpeg_segment_start_extent;n++)
+ _vcd_salloc (obj->iso_bitmap, n, 1);
+
+ vcd_assert (_vcd_salloc_get_highest (obj->iso_bitmap) + 1 == obj->mpeg_segment_start_extent);
+
+ /* insert segments */
+
+ _VCD_LIST_FOREACH (node, obj->mpeg_segment_list)
+ {
+ mpeg_segment_t *_segment = _vcd_list_node_data (node);
+
+ _segment->start_extent =
+ _vcd_salloc (obj->iso_bitmap, SECTOR_NIL,
+ _segment->segment_count * VCDINFO_SEGMENT_SECTOR_SIZE);
+
+ vcd_assert (_segment->start_extent % 75 == 0);
+ vcd_assert (_vcd_salloc_get_highest (obj->iso_bitmap) + 1
+ == _segment->start_extent
+ + _segment->segment_count * VCDINFO_SEGMENT_SECTOR_SIZE);
+ }
+
+ obj->ext_file_start_extent = _vcd_salloc_get_highest (obj->iso_bitmap) + 1;
+
+ vcd_assert (obj->ext_file_start_extent % 75 == 0);
+
+ /* go on with EXT area */
+
+ if (_vcd_obj_has_cap_p (obj, _CAP_4C_SVCD))
+ {
+ _dict_insert (obj, "scandata", SECTOR_NIL,
+ _vcd_len2blocks (get_scandata_dat_size (obj),
+ ISO_BLOCKSIZE),
+ SM_EOF);
+ }
+
+ if (_vcd_obj_has_cap_p (obj, _CAP_PBC_X)
+ &&_vcd_pbc_available (obj))
+ {
+ _dict_insert (obj, "lot_x", SECTOR_NIL, LOT_VCD_SIZE, SM_EOF);
+
+ _dict_insert (obj, "psd_x", SECTOR_NIL,
+ _vcd_len2blocks (get_psd_size (obj, true), ISO_BLOCKSIZE),
+ SM_EOF);
+ }
+
+
+ obj->custom_file_start_extent =
+ _vcd_salloc_get_highest (obj->iso_bitmap) + 1;
+
+ /* now for the custom files */
+
+ _VCD_LIST_FOREACH (node, obj->custom_file_list)
+ {
+ custom_file_t *p = _vcd_list_node_data (node);
+
+ if (p->sectors)
+ {
+ p->start_extent =
+ _vcd_salloc(obj->iso_bitmap, SECTOR_NIL, p->sectors);
+ vcd_assert (p->start_extent != SECTOR_NIL);
+ }
+ else /* zero sized files -- set dummy extent */
+ p->start_extent = obj->custom_file_start_extent;
+ }
+
+ /* calculate iso size -- after this point no sector shall be
+ allocated anymore */
+
+ obj->iso_size =
+ MAX (MIN_ISO_SIZE, _vcd_salloc_get_highest (obj->iso_bitmap) + 1);
+
+ vcd_debug ("iso9660: highest alloced sector is %lu (using %d as isosize)",
+ (unsigned long int) _vcd_salloc_get_highest (obj->iso_bitmap),
+ obj->iso_size);
+
+ /* after this point the ISO9660's size is frozen */
+}
+
+static void
+_finalize_vcd_iso_track_filesystem (VcdObj *obj)
+{
+ int n;
+ VcdListNode *node;
+
+ /* create filesystem entries */
+
+ switch (obj->type) {
+ case VCD_TYPE_VCD:
+ case VCD_TYPE_VCD11:
+ case VCD_TYPE_VCD2:
+ /* add only necessary directories! */
+ /* _vcd_directory_mkdir (obj->dir, "CDDA"); */
+ /* _vcd_directory_mkdir (obj->dir, "CDI"); */
+ _vcd_directory_mkdir (obj->dir, "EXT");
+ /* _vcd_directory_mkdir (obj->dir, "KARAOKE"); */
+ _vcd_directory_mkdir (obj->dir, "MPEGAV");
+ _vcd_directory_mkdir (obj->dir, "VCD");
+
+ /* add segment dir only when there are actually segment play items */
+ if (_vcd_list_length (obj->mpeg_segment_list))
+ _vcd_directory_mkdir (obj->dir, "SEGMENT");
+
+ _vcd_directory_mkfile (obj->dir, "VCD/ENTRIES.VCD",
+ _dict_get_bykey (obj, "entries")->sector,
+ ISO_BLOCKSIZE, false, 0);
+ _vcd_directory_mkfile (obj->dir, "VCD/INFO.VCD",
+ _dict_get_bykey (obj, "info")->sector,
+ ISO_BLOCKSIZE, false, 0);
+
+ /* only for vcd2.0 */
+ if (_vcd_pbc_available (obj))
+ {
+ _vcd_directory_mkfile (obj->dir, "VCD/LOT.VCD",
+ _dict_get_bykey (obj, "lot")->sector,
+ ISO_BLOCKSIZE*LOT_VCD_SIZE, false, 0);
+ _vcd_directory_mkfile (obj->dir, "VCD/PSD.VCD",
+ _dict_get_bykey (obj, "psd")->sector,
+ get_psd_size (obj, false), false, 0);
+ }
+ break;
+
+ case VCD_TYPE_SVCD:
+ case VCD_TYPE_HQVCD:
+ _vcd_directory_mkdir (obj->dir, "EXT");
+
+ if (!obj->svcd_vcd3_mpegav)
+ _vcd_directory_mkdir (obj->dir, "MPEG2");
+ else
+ {
+ vcd_warn ("adding MPEGAV dir for *DEPRECATED* SVCD VCD30 mode");
+ _vcd_directory_mkdir (obj->dir, "MPEGAV");
+ }
+
+ /* add segment dir only when there are actually segment play items */
+ if (_vcd_list_length (obj->mpeg_segment_list))
+ _vcd_directory_mkdir (obj->dir, "SEGMENT");
+
+ _vcd_directory_mkdir (obj->dir, "SVCD");
+
+ _vcd_directory_mkfile (obj->dir, "SVCD/ENTRIES.SVD",
+ _dict_get_bykey (obj, "entries")->sector,
+ ISO_BLOCKSIZE, false, 0);
+ _vcd_directory_mkfile (obj->dir, "SVCD/INFO.SVD",
+ _dict_get_bykey (obj, "info")->sector,
+ ISO_BLOCKSIZE, false, 0);
+
+ if (_vcd_pbc_available (obj))
+ {
+ _vcd_directory_mkfile (obj->dir, "SVCD/LOT.SVD",
+ _dict_get_bykey (obj, "lot")->sector,
+ ISO_BLOCKSIZE*LOT_VCD_SIZE, false, 0);
+ _vcd_directory_mkfile (obj->dir, "SVCD/PSD.SVD",
+ _dict_get_bykey (obj, "psd")->sector,
+ get_psd_size (obj, false), false, 0);
+ }
+
+ _vcd_directory_mkfile (obj->dir, "SVCD/SEARCH.DAT",
+ _dict_get_bykey (obj, "search")->sector,
+ get_search_dat_size (obj), false, 0);
+ _vcd_directory_mkfile (obj->dir, "SVCD/TRACKS.SVD",
+ _dict_get_bykey (obj, "tracks")->sector,
+ ISO_BLOCKSIZE, false, 0);
+ break;
+
+ default:
+ vcd_assert_not_reached ();
+ break;
+ }
+
+ /* SEGMENTS */
+
+ n = 1;
+ _VCD_LIST_FOREACH (node, obj->mpeg_segment_list)
+ {
+ mpeg_segment_t *segment = _vcd_list_node_data (node);
+ char segment_pathname[128] = { 0, };
+ const char *fmt = NULL;
+ uint8_t fnum = 0;
+
+ switch (obj->type)
+ {
+ case VCD_TYPE_VCD2:
+ fmt = "SEGMENT/ITEM%4.4d.DAT";
+ fnum = 1;
+ break;
+ case VCD_TYPE_SVCD:
+ case VCD_TYPE_HQVCD:
+ fmt = "SEGMENT/ITEM%4.4d.MPG";
+ fnum = 0;
+ break;
+ default:
+ vcd_assert_not_reached ();
+ }
+
+ snprintf (segment_pathname, sizeof (segment_pathname), fmt, n);
+
+ _vcd_directory_mkfile (obj->dir, segment_pathname, segment->start_extent,
+ segment->info->packets * ISO_BLOCKSIZE,
+ true, fnum);
+
+ vcd_assert (n <= MAX_SEGMENTS);
+
+ n += segment->segment_count;
+ }
+
+ /* EXT files */
+
+ if (_vcd_obj_has_cap_p (obj, _CAP_PBC_X)
+ &&_vcd_pbc_available (obj))
+ {
+ /* psd_x -- extended PSD */
+ _vcd_directory_mkfile (obj->dir, "EXT/PSD_X.VCD",
+ _dict_get_bykey (obj, "psd_x")->sector,
+ get_psd_size (obj, true), false, 1);
+
+ /* lot_x -- extended LOT */
+ _vcd_directory_mkfile (obj->dir, "EXT/LOT_X.VCD",
+ _dict_get_bykey (obj, "lot_x")->sector,
+ ISO_BLOCKSIZE*LOT_VCD_SIZE, false, 1);
+
+ vcd_assert (obj->type == VCD_TYPE_VCD2);
+ }
+
+ if (_vcd_obj_has_cap_p (obj, _CAP_4C_SVCD))
+ {
+ /* scandata.dat -- scanpoints */
+ _vcd_directory_mkfile (obj->dir, "EXT/SCANDATA.DAT",
+ _dict_get_bykey (obj, "scandata")->sector,
+ get_scandata_dat_size (obj), false, 0);
+ }
+
+ /* custom files/dirs */
+ _VCD_LIST_FOREACH (node, obj->custom_dir_list)
+ {
+ char *p = _vcd_list_node_data (node);
+ _vcd_directory_mkdir (obj->dir, p);
+ }
+
+ _VCD_LIST_FOREACH (node, obj->custom_file_list)
+ {
+ custom_file_t *p = _vcd_list_node_data (node);
+
+ _vcd_directory_mkfile (obj->dir, p->iso_pathname, p->start_extent,
+ (p->raw_flag
+ ? (ISO_BLOCKSIZE * (p->size / M2RAW_SECTOR_SIZE))
+ : p->size),
+ p->raw_flag, 1);
+ }
+
+
+ n = 0;
+ _VCD_LIST_FOREACH (node, obj->mpeg_sequence_list)
+ {
+ char avseq_pathname[128] = { 0, };
+ const char *fmt = NULL;
+ mpeg_sequence_t *_sequence = _vcd_list_node_data (node);
+ uint32_t extent = _sequence->relative_start_extent;
+ uint8_t file_num = 0;
+
+ extent += obj->iso_size;
+
+ switch (obj->type)
+ {
+ case VCD_TYPE_VCD:
+ fmt = "MPEGAV/MUSIC%2.2d.DAT";
+ file_num = n + 1;
+ break;
+
+ case VCD_TYPE_VCD11:
+ case VCD_TYPE_VCD2:
+ fmt = "MPEGAV/AVSEQ%2.2d.DAT";
+ file_num = n + 1;
+ break;
+ case VCD_TYPE_SVCD:
+ case VCD_TYPE_HQVCD:
+ fmt = "MPEG2/AVSEQ%2.2d.MPG";
+ file_num = 0;
+
+ /* if vcd3 compat mode, override */
+ if (obj->svcd_vcd3_mpegav)
+ {
+ fmt = "MPEGAV/AVSEQ%2.2d.MPG";
+ file_num = n + 1;
+ }
+
+ break;
+ default:
+ vcd_assert_not_reached ();
+ }
+
+ vcd_assert (n < 98);
+
+ snprintf (avseq_pathname, sizeof (avseq_pathname), fmt, n + 1);
+
+ /* file entry contains front margin, mpeg stream and rear margin */
+ _vcd_directory_mkfile (obj->dir, avseq_pathname, extent,
+ (obj->track_front_margin
+ + _sequence->info->packets
+ + obj->track_rear_margin) * ISO_BLOCKSIZE,
+ true, file_num);
+
+ n++;
+ }
+
+ /* register isofs dir structures */
+ {
+ uint32_t dirs_size = _vcd_directory_get_size (obj->dir);
+
+ /* be sure to stay out of information areas */
+
+ switch (obj->type)
+ {
+ case VCD_TYPE_VCD:
+ case VCD_TYPE_VCD11:
+ case VCD_TYPE_VCD2:
+ /* karaoke area starts at 03:00 */
+ if (16 + 2 + dirs_size + 2 >= 75)
+ vcd_error ("directory section to big for a VCD");
+ break;
+
+ case VCD_TYPE_SVCD:
+ case VCD_TYPE_HQVCD:
+ /* since no karaoke exists the next fixed area starts at 04:00 */
+ if (16 + 2 + dirs_size + 2 >= 150)
+ vcd_error ("directory section to big for a SVCD");
+ break;
+ default:
+ vcd_assert_not_reached ();
+ }
+
+ /* un-alloc small area */
+
+ _vcd_salloc_free (obj->iso_bitmap, 18, dirs_size + 2);
+
+ /* alloc it again! */
+
+ _dict_insert (obj, "dir", 18, dirs_size, SM_EOR|SM_EOF);
+ _dict_insert (obj, "ptl", 18 + dirs_size, 1, SM_EOR|SM_EOF);
+ _dict_insert (obj, "ptm", 18 + dirs_size + 1, 1, SM_EOR|SM_EOF);
+ }
+}
+
+static void
+_finalize_vcd_iso_track (VcdObj *obj)
+{
+ _vcd_pbc_finalize (obj);
+ _finalize_vcd_iso_track_allocation (obj);
+ _finalize_vcd_iso_track_filesystem (obj);
+}
+
+static int
+_callback_wrapper (VcdObj *obj, int force)
+{
+ const int cb_frequency = 75;
+
+ if (obj->last_cb_call + cb_frequency > obj->sectors_written && !force)
+ return 0;
+
+ obj->last_cb_call = obj->sectors_written;
+
+ if (obj->progress_callback) {
+ progress_info_t _pi;
+
+ _pi.sectors_written = obj->sectors_written;
+ _pi.total_sectors = obj->relative_end_extent + obj->iso_size;
+ _pi.in_track = obj->in_track;
+ _pi.total_tracks = _vcd_list_length (obj->mpeg_sequence_list) + 1;
+
+ return obj->progress_callback (&_pi, obj->callback_user_data);
+ }
+ else
+ return 0;
+}
+
+static int
+_write_m2_image_sector (VcdObj *obj, const void *data, uint32_t extent,
+ uint8_t fnum, uint8_t cnum, uint8_t sm, uint8_t ci)
+{
+ char buf[CDIO_CD_FRAMESIZE_RAW] = { 0, };
+
+ vcd_assert (extent == obj->sectors_written);
+
+ _vcd_make_mode2(buf, data, extent, fnum, cnum, sm, ci);
+
+ vcd_image_sink_write (obj->image_sink, buf, extent);
+
+ obj->sectors_written++;
+
+ return _callback_wrapper (obj, false);
+}
+
+static int
+_write_m2_raw_image_sector (VcdObj *obj, const void *data, uint32_t extent)
+{
+ char buf[CDIO_CD_FRAMESIZE_RAW] = { 0, };
+
+ vcd_assert (extent == obj->sectors_written);
+
+ _vcd_make_raw_mode2(buf, data, extent);
+
+ vcd_image_sink_write (obj->image_sink, buf, extent);
+
+ obj->sectors_written++;
+
+ return _callback_wrapper (obj, false);
+}
+
+static void
+_write_source_mode2_raw (VcdObj *obj, VcdDataSource *source, uint32_t extent)
+{
+ int n;
+ uint32_t sectors;
+
+ sectors = vcd_data_source_stat (source) / M2RAW_SECTOR_SIZE;
+
+ vcd_data_source_seek (source, 0);
+
+ for (n = 0;n < sectors;n++) {
+ char buf[M2RAW_SECTOR_SIZE] = { 0, };
+
+ vcd_data_source_read (source, buf, M2RAW_SECTOR_SIZE, 1);
+
+ if (_write_m2_raw_image_sector (obj, buf, extent+n))
+ break;
+ }
+
+ vcd_data_source_close (source);
+}
+
+static void
+_write_source_mode2_form1 (VcdObj *obj, VcdDataSource *source, uint32_t extent)
+{
+ int n;
+ uint32_t sectors, size, last_block_size;
+
+ size = vcd_data_source_stat (source);
+
+ sectors = _vcd_len2blocks (size, CDIO_CD_FRAMESIZE);
+
+ last_block_size = size % CDIO_CD_FRAMESIZE;
+ if (!last_block_size)
+ last_block_size = CDIO_CD_FRAMESIZE;
+
+ vcd_data_source_seek (source, 0);
+
+ for (n = 0;n < sectors;n++) {
+ char buf[CDIO_CD_FRAMESIZE] = { 0, };
+
+ vcd_data_source_read (source, buf,
+ ((n + 1 == sectors)
+ ? last_block_size
+ : CDIO_CD_FRAMESIZE), 1);
+
+ if (_write_m2_image_sector (obj, buf, extent+n, 1, 0,
+ ((n+1 < sectors)
+ ? SM_DATA
+ : SM_DATA |SM_EOF),
+ 0))
+ break;
+ }
+
+ vcd_data_source_close (source);
+}
+
+static int
+_write_sequence (VcdObj *obj, int track_idx)
+{
+ mpeg_sequence_t *track =
+ _vcd_list_node_data (_vcd_list_at (obj->mpeg_sequence_list, track_idx));
+ VcdListNode *pause_node;
+ int n, lastsect = obj->sectors_written;
+ char buf[2324];
+ struct {
+ int audio;
+ int video;
+ int zero;
+ int ogt;
+ int unknown;
+ } mpeg_packets = {0, };
+
+
+ {
+ char *norm_str = NULL;
+ const struct vcd_mpeg_stream_vid_info *_info = &track->info->shdr[0];
+
+ switch (vcd_mpeg_get_norm (_info)) {
+ case MPEG_NORM_PAL:
+ norm_str = strdup ("PAL SIF (352x288/25fps)");
+ break;
+ case MPEG_NORM_NTSC:
+ norm_str = strdup ("NTSC SIF (352x240/29.97fps)");
+ break;
+ case MPEG_NORM_FILM:
+ norm_str = strdup ("FILM SIF (352x240/24fps)");
+ break;
+ case MPEG_NORM_PAL_S:
+ norm_str = strdup ("PAL 2/3 D1 (480x576/25fps)");
+ break;
+ case MPEG_NORM_NTSC_S:
+ norm_str = strdup ("NTSC 2/3 D1 (480x480/29.97fps)");
+ break;
+
+ case MPEG_NORM_OTHER:
+ {
+ char buf[1024] = { 0, };
+ switch (_info->vsize)
+ {
+ case 480:
+ case 240:
+ snprintf (buf, sizeof (buf), "NTSC UNKNOWN (%dx%d/%2.2ffps)",
+ _info->hsize, _info->vsize, _info->frate);
+ break;
+ case 288:
+ case 576:
+ snprintf (buf, sizeof (buf), "PAL UNKNOWN (%dx%d/%2.2ffps)",
+ _info->hsize, _info->vsize, _info->frate);
+ break;
+ default:
+ snprintf (buf, sizeof (buf), "UNKNOWN (%dx%d/%2.2ffps)",
+ _info->hsize, _info->vsize, _info->frate);
+ break;
+ }
+ norm_str = strdup (buf);
+ }
+ break;
+ }
+
+ {
+ char buf[1024] = { 0, }, buf2[1024] = { 0, };
+ int i;
+
+ for (i = 0; i < 3; i++)
+ if (track->info->ahdr[i].seen)
+ {
+ const char *_mode_str[] = {
+ 0,
+ "stereo",
+ "jstereo",
+ "dual",
+ "single",
+ 0
+ };
+
+ snprintf (buf, sizeof (buf), "audio[%d]: l%d/%2.1fkHz/%dkbps/%s ",
+ i,
+ track->info->ahdr[i].layer,
+ track->info->ahdr[i].sampfreq / 1000.0,
+ track->info->ahdr[i].bitrate / 1024,
+ _mode_str[track->info->ahdr[i].mode]);
+
+ strncat (buf2, buf, sizeof(buf));
+ }
+
+ vcd_info ("writing track %d, %s, %s, %s...", track_idx + 2,
+ (track->info->version == MPEG_VERS_MPEG1 ? "MPEG1" : "MPEG2"),
+ norm_str, buf2);
+ }
+
+ free (norm_str);
+ }
+
+ for (n = 0; n < obj->track_pregap; n++)
+ _write_m2_image_sector (obj, zero, lastsect++, 0, 0, SM_FORM2, 0);
+
+ for (n = 0; n < obj->track_front_margin;n++)
+ _write_m2_image_sector (obj, zero, lastsect++, track_idx + 1,
+ 0, SM_FORM2|SM_REALT, 0);
+
+ pause_node = _vcd_list_begin (track->pause_list);
+
+ for (n = 0; n < track->info->packets; n++) {
+ int ci = 0, sm = 0, cnum = 0, fnum = 0;
+ struct vcd_mpeg_packet_info pkt_flags;
+ bool set_trigger = false;
+
+ vcd_mpeg_source_get_packet (track->source, n, buf, &pkt_flags,
+ obj->update_scan_offsets);
+
+ while (pause_node)
+ {
+ pause_t *_pause = _vcd_list_node_data (pause_node);
+
+ if (!pkt_flags.has_pts)
+ break; /* no pts */
+
+ if (pkt_flags.pts < _pause->time)
+ break; /* our time has not come yet */
+
+ /* seems it's time to trigger! */
+ set_trigger = true;
+
+ vcd_debug ("setting auto pause trigger for time %f (pts %f) @%d",
+ _pause->time, pkt_flags.pts, n);
+
+ pause_node = _vcd_list_node_next (pause_node);
+ }
+
+ switch (vcd_mpeg_packet_get_type (&pkt_flags))
+ {
+ case PKT_TYPE_VIDEO:
+ mpeg_packets.video++;
+ sm = SM_FORM2|SM_REALT|SM_VIDEO;
+ ci = CI_VIDEO;
+ cnum = CN_VIDEO;
+ break;
+
+ case PKT_TYPE_OGT:
+ mpeg_packets.ogt++;
+ sm = SM_FORM2|SM_REALT|SM_VIDEO;
+ ci = CI_OGT;
+ cnum = CN_OGT;
+ break;
+
+ case PKT_TYPE_AUDIO:
+ mpeg_packets.audio++;
+ sm = SM_FORM2|SM_REALT|SM_AUDIO;
+ ci = CI_AUDIO;
+ cnum = CN_AUDIO;
+ if (pkt_flags.audio[1] || pkt_flags.audio[2])
+ {
+ ci = CI_AUDIO2;
+ cnum = CN_AUDIO2;
+ }
+ break;
+
+
+ case PKT_TYPE_ZERO:
+ mpeg_packets.zero++;
+ mpeg_packets.unknown--;
+ case PKT_TYPE_EMPTY:
+ mpeg_packets.unknown++;
+ sm = SM_FORM2|SM_REALT;
+ ci = CI_EMPTY;
+ cnum = CN_EMPTY;
+ break;
+
+ case PKT_TYPE_INVALID:
+ vcd_error ("invalid mpeg packet found at packet# %d"
+ " -- please fix this mpeg file!", n);
+ vcd_mpeg_source_close (track->source);
+ return 1;
+ break;
+
+ default:
+ vcd_assert_not_reached ();
+ }
+
+ if (n == track->info->packets - 1)
+ {
+ sm |= SM_EOR;
+ if (!obj->track_rear_margin) /* if no rear margin... */
+ sm |= SM_EOF;
+ }
+
+ if (set_trigger)
+ sm |= SM_TRIG;
+
+ fnum = track_idx + 1;
+
+ if (_vcd_obj_has_cap_p (obj, _CAP_4C_SVCD)
+ && !obj->svcd_vcd3_mpegav) /* IEC62107 SVCDs have a
+ simplified subheader */
+ {
+ fnum = 1;
+ ci = CI_MPEG2;
+ }
+
+ if (_write_m2_image_sector (obj, buf, lastsect++, fnum, cnum, sm, ci))
+ break;
+ }
+
+ vcd_mpeg_source_close (track->source);
+
+ for (n = 0; n < obj->track_rear_margin; n++)
+ {
+ const uint8_t ci = 0, cnum = 0;
+ uint8_t fnum = track_idx + 1;
+ uint8_t sm = SM_FORM2 | SM_REALT;
+
+ if (n + 1 == obj->track_rear_margin)
+ sm |= SM_EOF;
+
+ _write_m2_image_sector (obj, zero, lastsect++, fnum, cnum, sm, ci);
+ }
+
+ vcd_debug ("MPEG packet statistics: %d video, %d audio, %d zero, %d ogt, %d unknown",
+ mpeg_packets.video, mpeg_packets.audio, mpeg_packets.zero, mpeg_packets.ogt,
+ mpeg_packets.unknown);
+
+ return 0;
+}
+
+static int
+_write_segment (VcdObj *obj, mpeg_segment_t *_segment)
+{
+ VcdListNode *pause_node;
+ unsigned packet_no;
+ int n = obj->sectors_written;
+
+ vcd_assert (_segment->start_extent == n);
+
+ pause_node = _vcd_list_begin (_segment->pause_list);
+
+ for (packet_no = 0;
+ packet_no < (_segment->segment_count * VCDINFO_SEGMENT_SECTOR_SIZE);
+ packet_no++)
+ {
+ uint8_t buf[M2F2_SECTOR_SIZE] = { 0, };
+ uint8_t fn, cn, sm, ci;
+
+ if (packet_no < _segment->info->packets)
+ {
+ struct vcd_mpeg_packet_info pkt_flags;
+ bool set_trigger = false;
+ bool _need_eor = false;
+
+ vcd_mpeg_source_get_packet (_segment->source, packet_no,
+ buf, &pkt_flags, obj->update_scan_offsets);
+
+ fn = 1;
+ cn = CN_EMPTY;
+ sm = SM_FORM2 | SM_REALT;
+ ci = CI_EMPTY;
+
+ while (pause_node)
+ {
+ pause_t *_pause = _vcd_list_node_data (pause_node);
+
+ if (!pkt_flags.has_pts)
+ break; /* no pts */
+
+ if (pkt_flags.pts < _pause->time)
+ break; /* our time has not come yet */
+
+ /* seems it's time to trigger! */
+ set_trigger = true;
+
+ vcd_debug ("setting auto pause trigger for time %f (pts %f) @%d",
+ _pause->time, pkt_flags.pts, n);
+
+ pause_node = _vcd_list_node_next (pause_node);
+ }
+
+ switch (vcd_mpeg_packet_get_type (&pkt_flags))
+ {
+ case PKT_TYPE_VIDEO:
+ sm = SM_FORM2 | SM_REALT | SM_VIDEO;
+
+ ci = CI_VIDEO;
+ cn = CN_VIDEO;
+
+ if (pkt_flags.video[1])
+ ci = CI_STILL, cn = CN_STILL;
+ else if (pkt_flags.video[2])
+ ci = CI_STILL2, cn = CN_STILL2;
+
+ if (pkt_flags.video[1] || pkt_flags.video[2])
+ { /* search for endcode -- hack */
+ int idx;
+
+ for (idx = 0; idx <= 2320; idx++)
+ if (buf[idx] == 0x00
+ && buf[idx + 1] == 0x00
+ && buf[idx + 2] == 0x01
+ && buf[idx + 3] == 0xb7)
+ {
+ _need_eor = true;
+ break;
+ }
+ }
+ break;
+
+ case PKT_TYPE_AUDIO:
+ sm = SM_FORM2 | SM_REALT | SM_AUDIO;
+
+ ci = CI_AUDIO;
+ cn = CN_AUDIO;
+ break;
+
+ case PKT_TYPE_EMPTY:
+ ci = CI_EMPTY;
+ cn = CN_EMPTY;
+ break;
+
+ default:
+ /* fixme -- check.... */
+ break;
+ }
+
+ if (_vcd_obj_has_cap_p (obj, _CAP_4C_SVCD))
+ {
+ cn = 1;
+ sm = SM_FORM2 | SM_REALT | SM_VIDEO;
+ ci = CI_MPEG2;
+ }
+
+ if (packet_no + 1 == _segment->info->packets)
+ sm |= SM_EOF;
+
+ if (set_trigger)
+ sm |= SM_TRIG;
+
+ if (_need_eor)
+ {
+ vcd_debug ("setting EOR for SeqEnd at packet# %d ('%s')",
+ packet_no, _segment->id);
+ sm |= SM_EOR;
+ }
+ }
+ else
+ {
+ fn = 1;
+ cn = CN_EMPTY;
+ sm = SM_FORM2 | SM_REALT;
+ ci = CI_EMPTY;
+
+ if (_vcd_obj_has_cap_p (obj, _CAP_4C_SVCD))
+ {
+ fn = 0;
+ sm = SM_FORM2;
+ }
+
+ }
+
+ _write_m2_image_sector (obj, buf, n, fn, cn, sm, ci);
+
+ n++;
+ }
+
+ vcd_mpeg_source_close (_segment->source);
+
+ return 0;
+}
+
+static uint32_t
+_get_closest_aps (const struct vcd_mpeg_stream_info *_mpeg_info, double t,
+ struct aps_data *_best_aps)
+{
+ VcdListNode *node;
+ struct aps_data best_aps;
+ bool first = true;
+
+ vcd_assert (_mpeg_info != NULL);
+ vcd_assert (_mpeg_info->shdr[0].aps_list != NULL);
+
+ _VCD_LIST_FOREACH (node, _mpeg_info->shdr[0].aps_list)
+ {
+ struct aps_data *_aps = _vcd_list_node_data (node);
+
+ if (first)
+ {
+ best_aps = *_aps;
+ first = false;
+ }
+ else if (fabs (_aps->timestamp - t) < fabs (best_aps.timestamp - t))
+ best_aps = *_aps;
+ else
+ break;
+ }
+
+ if (_best_aps)
+ *_best_aps = best_aps;
+
+ return best_aps.packet_no;
+}
+
+static void
+_update_entry_points (VcdObj *obj)
+{
+ VcdListNode *sequence_node;
+
+ _VCD_LIST_FOREACH (sequence_node, obj->mpeg_sequence_list)
+ {
+ mpeg_sequence_t *_sequence = _vcd_list_node_data (sequence_node);
+ VcdListNode *entry_node;
+ unsigned last_packet_no = 0;
+
+ _VCD_LIST_FOREACH (entry_node, _sequence->entry_list)
+ {
+ entry_t *_entry = _vcd_list_node_data (entry_node);
+
+ _get_closest_aps (_sequence->info, _entry->time, &_entry->aps);
+
+ vcd_log ((fabs (_entry->aps.timestamp - _entry->time) > 1
+ ? VCD_LOG_WARN
+ : VCD_LOG_DEBUG),
+ "requested entry point (id=%s) at %f, "
+ "closest possible entry point at %f",
+ _entry->id, _entry->time, _entry->aps.timestamp);
+
+ if (last_packet_no == _entry->aps.packet_no)
+ vcd_warn ("entry point '%s' falls into same sector as previous one!",
+ _entry->id);
+
+ last_packet_no = _entry->aps.packet_no;
+ }
+ }
+}
+
+static int
+_write_vcd_iso_track (VcdObj *obj, const time_t *create_time)
+{
+ VcdListNode *node;
+ int n;
+
+ /* generate dir sectors */
+
+ _vcd_directory_dump_entries (obj->dir,
+ _dict_get_bykey (obj, "dir")->buf,
+ _dict_get_bykey (obj, "dir")->sector);
+
+ _vcd_directory_dump_pathtables (obj->dir,
+ _dict_get_bykey (obj, "ptl")->buf,
+ _dict_get_bykey (obj, "ptm")->buf);
+
+ /* generate PVD and EVD at last... */
+ iso9660_set_pvd (_dict_get_bykey (obj, "pvd")->buf,
+ obj->iso_volume_label,
+ obj->iso_publisher_id,
+ obj->iso_preparer_id,
+ obj->iso_application_id,
+ obj->iso_size,
+ _dict_get_bykey (obj, "dir")->buf,
+ _dict_get_bykey (obj, "ptl")->sector,
+ _dict_get_bykey (obj, "ptm")->sector,
+ iso9660_pathtable_get_size (_dict_get_bykey (obj, "ptm")->buf),
+ create_time
+);
+
+ iso9660_set_evd (_dict_get_bykey (obj, "evd")->buf);
+
+ /* fill VCD relevant files with data */
+
+ set_info_vcd (obj, _dict_get_bykey (obj, "info")->buf);
+ set_entries_vcd (obj, _dict_get_bykey (obj, "entries")->buf);
+
+ if (_vcd_pbc_available (obj))
+ {
+ if (_vcd_obj_has_cap_p (obj, _CAP_PBC_X))
+ {
+ set_lot_vcd (obj, _dict_get_bykey (obj, "lot_x")->buf, true);
+ set_psd_vcd (obj, _dict_get_bykey (obj, "psd_x")->buf, true);
+ }
+
+ _vcd_pbc_check_unreferenced (obj);
+
+ set_lot_vcd (obj, _dict_get_bykey (obj, "lot")->buf, false);
+ set_psd_vcd (obj, _dict_get_bykey (obj, "psd")->buf, false);
+ }
+
+ if (_vcd_obj_has_cap_p (obj, _CAP_4C_SVCD))
+ {
+ set_tracks_svd (obj, _dict_get_bykey (obj, "tracks")->buf);
+ set_search_dat (obj, _dict_get_bykey (obj, "search")->buf);
+ set_scandata_dat (obj, _dict_get_bykey (obj, "scandata")->buf);
+ }
+
+ /* start actually writing stuff */
+
+ vcd_info ("writing track 1 (ISO9660)...");
+
+ /* 00:02:00 -> 00:04:74 */
+ for (n = 0;n < obj->mpeg_segment_start_extent; n++)
+ {
+ const void *content = NULL;
+ uint8_t flags = SM_DATA;
+
+ content = _dict_get_sector (obj, n);
+ flags |= _dict_get_sector_flags (obj, n);
+
+ if (content == NULL)
+ content = zero;
+
+ _write_m2_image_sector (obj, content, n, 0, 0, flags, 0);
+ }
+
+ /* SEGMENTS */
+
+ vcd_assert (n == obj->mpeg_segment_start_extent);
+
+ _VCD_LIST_FOREACH (node, obj->mpeg_segment_list)
+ {
+ mpeg_segment_t *_segment = _vcd_list_node_data (node);
+
+ _write_segment (obj, _segment);
+ }
+
+ n = obj->sectors_written;
+
+ /* EXT stuff */
+
+ vcd_assert (n == obj->ext_file_start_extent);
+
+ for (;n < obj->custom_file_start_extent; n++)
+ {
+ const void *content = NULL;
+ uint8_t flags = SM_DATA;
+ uint8_t fileno = _vcd_obj_has_cap_p (obj, _CAP_4C_SVCD) ? 0 : 1;
+
+ content = _dict_get_sector (obj, n);
+ flags |= _dict_get_sector_flags (obj, n);
+
+ if (content == NULL)
+ {
+ vcd_debug ("unexpected empty EXT sector");
+ content = zero;
+ }
+
+ _write_m2_image_sector (obj, content, n, fileno, 0, flags, 0);
+ }
+
+ /* write custom files */
+
+ vcd_assert (n == obj->custom_file_start_extent);
+
+ _VCD_LIST_FOREACH (node, obj->custom_file_list)
+ {
+ custom_file_t *p = _vcd_list_node_data (node);
+
+ vcd_info ("writing file `%s' (%d bytes%s)",
+ p->iso_pathname, p->size,
+ p->raw_flag ? ", raw sectors file": "");
+ if (p->raw_flag)
+ _write_source_mode2_raw (obj, p->file, p->start_extent);
+ else
+ _write_source_mode2_form1 (obj, p->file, p->start_extent);
+ }
+
+ /* blank unalloced tracks */
+ while ((n = _vcd_salloc (obj->iso_bitmap, SECTOR_NIL, 1)) < obj->iso_size)
+ _write_m2_image_sector (obj, zero, n, 0, 0, SM_DATA, 0);
+
+ return 0;
+}
+
+
+long
+vcd_obj_get_image_size (VcdObj *obj)
+{
+ long size_sectors = -1;
+
+ vcd_assert (!obj->in_output);
+
+ if (_vcd_list_length (obj->mpeg_sequence_list) > 0)
+ {
+ /* fixme -- make this efficient */
+ size_sectors = vcd_obj_begin_output (obj);
+ vcd_obj_end_output (obj);
+ }
+
+ return size_sectors;
+}
+
+long
+vcd_obj_begin_output (VcdObj *obj)
+{
+ uint32_t image_size;
+
+ vcd_assert (obj != NULL);
+ vcd_assert (_vcd_list_length (obj->mpeg_sequence_list) > 0);
+
+ vcd_assert (!obj->in_output);
+ obj->in_output = true;
+
+ obj->in_track = 1;
+ obj->sectors_written = 0;
+
+ obj->iso_bitmap = _vcd_salloc_new ();
+
+ obj->dir = _vcd_directory_new ();
+
+ obj->buffer_dict_list = _vcd_list_new ();
+
+ _finalize_vcd_iso_track (obj);
+
+ _update_entry_points (obj);
+
+ image_size = obj->relative_end_extent + obj->iso_size;
+
+ image_size += obj->leadout_pregap;
+
+ if (image_size > CDIO_CD_MAX_SECTORS)
+ vcd_error ("image too big (%d sectors > %d sectors)",
+ (unsigned) image_size, (unsigned) CDIO_CD_MAX_SECTORS);
+
+ {
+ char *_tmp = cdio_lba_to_msf_str (image_size);
+
+ if (image_size > CDIO_CD_74MIN_SECTORS)
+ vcd_warn ("generated image (%d sectors [%s]) may not fit "
+ "on 74min CDRs (%d sectors)",
+ (unsigned) image_size, _tmp, (unsigned) CDIO_CD_74MIN_SECTORS);
+
+ free (_tmp);
+ }
+
+ return image_size;
+}
+
+
+void
+vcd_obj_end_output (VcdObj *obj)
+{
+ vcd_assert (obj != NULL);
+
+ vcd_assert (obj->in_output);
+ obj->in_output = false;
+
+ _vcd_directory_destroy (obj->dir);
+ _vcd_salloc_destroy (obj->iso_bitmap);
+
+ _dict_clean (obj);
+ _vcd_list_free (obj->buffer_dict_list, true);
+}
+
+int
+vcd_obj_append_pbc_node (VcdObj *obj, struct _pbc_t *_pbc)
+{
+ vcd_assert (obj != NULL);
+ vcd_assert (_pbc != NULL);
+
+ if (!_vcd_obj_has_cap_p (obj, _CAP_PBC))
+ {
+ vcd_error ("PBC not supported for current VCD type");
+ return -1;
+ }
+
+ if (_pbc->item_id && _vcd_pbc_lookup (obj, _pbc->item_id))
+ {
+ vcd_error ("item id (%s) exists already", _pbc->item_id);
+ return -1;
+ }
+
+ _vcd_list_append (obj->pbc_list, _pbc);
+
+ return 0;
+}
+
+int
+vcd_obj_write_image (VcdObj *obj, VcdImageSink *image_sink,
+ progress_callback_t callback, void *user_data,
+ const time_t *create_time)
+{
+ VcdListNode *node;
+
+ vcd_assert (obj != NULL);
+ vcd_assert (obj->in_output);
+
+ if (!image_sink)
+ return -1;
+
+ /* start with meta info */
+
+ {
+ VcdList *cue_list;
+ vcd_cue_t *_cue;
+
+ cue_list = _vcd_list_new ();
+
+ _vcd_list_append (cue_list, (_cue = _vcd_malloc (sizeof (vcd_cue_t))));
+
+ _cue->lsn = 0;
+ _cue->type = VCD_CUE_TRACK_START;
+
+ _VCD_LIST_FOREACH (node, obj->mpeg_sequence_list)
+ {
+ mpeg_sequence_t *track = _vcd_list_node_data (node);
+ VcdListNode *entry_node;
+
+ _vcd_list_append (cue_list, (_cue = _vcd_malloc (sizeof (vcd_cue_t))));
+
+ _cue->lsn = track->relative_start_extent + obj->iso_size;
+ _cue->lsn -= obj->track_pregap;
+ _cue->type = VCD_CUE_PREGAP_START;
+
+ _vcd_list_append (cue_list, (_cue = _vcd_malloc (sizeof (vcd_cue_t))));
+
+ _cue->lsn = track->relative_start_extent + obj->iso_size;
+ _cue->type = VCD_CUE_TRACK_START;
+
+ _VCD_LIST_FOREACH (entry_node, track->entry_list)
+ {
+ entry_t *_entry = _vcd_list_node_data (entry_node);
+
+ _vcd_list_append (cue_list, (_cue = _vcd_malloc (sizeof (vcd_cue_t))));
+
+ _cue->lsn = obj->iso_size;
+ _cue->lsn += track->relative_start_extent;
+ _cue->lsn += obj->track_front_margin;
+ _cue->lsn += _entry->aps.packet_no;
+
+ _cue->type = VCD_CUE_SUBINDEX;
+ }
+ }
+
+ /* add last one... */
+
+ _vcd_list_append (cue_list, (_cue = _vcd_malloc (sizeof (vcd_cue_t))));
+
+ _cue->lsn = obj->relative_end_extent + obj->iso_size;
+
+ _cue->lsn += obj->leadout_pregap;
+
+ _cue->type = VCD_CUE_END;
+
+ /* send it to image object */
+
+ vcd_image_sink_set_cuesheet (image_sink, cue_list);
+
+ _vcd_list_free (cue_list, true);
+ }
+
+ /* and now for the pay load */
+
+ {
+ unsigned track;
+
+ vcd_assert (obj != NULL);
+ vcd_assert (obj->sectors_written == 0);
+
+ vcd_assert (obj->in_output);
+
+ obj->progress_callback = callback;
+ obj->callback_user_data = user_data;
+ obj->image_sink = image_sink;
+
+ if (_callback_wrapper (obj, true))
+ return 1;
+
+ if (_write_vcd_iso_track (obj, create_time))
+ return 1;
+
+ if (obj->update_scan_offsets)
+ vcd_info ("'update scan offsets' option enabled for the following tracks!");
+
+ for (track = 0;track < _vcd_list_length (obj->mpeg_sequence_list);track++)
+ {
+ obj->in_track++;
+
+ if (_callback_wrapper (obj, true))
+ return 1;
+
+ if (_write_sequence (obj, track))
+ return 1;
+ }
+
+ if (obj->leadout_pregap)
+ {
+ int n, lastsect = obj->sectors_written;
+
+ vcd_debug ("writting post-gap ('leadout pregap')...");
+
+ for (n = 0; n < obj->leadout_pregap; n++)
+ _write_m2_image_sector (obj, zero, lastsect++, 0, 0, SM_FORM2, 0);
+ }
+
+ if (_callback_wrapper (obj, true))
+ return 1;
+
+ obj->image_sink = NULL;
+
+ vcd_image_sink_destroy (image_sink);
+
+ return 0; /* ok */
+ }
+}
+
+const char *
+vcd_version_string (bool full_text)
+{
+ if (!full_text)
+ return ("GNU VCDImager " VERSION " [" HOST_ARCH "]");
+
+ return ("%s (GNU VCDImager) " VERSION "\n"
+ "Written by Herbert Valerio Riedel and Rocky Bernstein.\n"
+ "\n"
+ "http://www.gnu.org/software/vcdimager/\n"
+ "\n"
+ "Copyright (C) 2000-2003 Herbert Valerio Riedel <hvr@gnu.org>\n"
+ " 2003 Rocky Bernstein <rocky@panix.com>\n"
+ "\n"
+ "This is free software; see the source for copying conditions. There is NO\n"
+ "warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n");
+}
+
+
+/*
+ * Local variables:
+ * c-file-style: "gnu"
+ * tab-width: 8
+ * indent-tabs-mode: nil
+ * End:
+ */
diff --git a/src/input/vcd/libvcd/vcd.h b/src/input/vcd/libvcd/vcd.h
new file mode 100644
index 000000000..b2899e1c3
--- /dev/null
+++ b/src/input/vcd/libvcd/vcd.h
@@ -0,0 +1,176 @@
+/*
+ $Id: vcd.h,v 1.1 2003/10/13 11:47:12 f1rmb Exp $
+
+ Copyright (C) 2000 Herbert Valerio Riedel <hvr@gnu.org>
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+*/
+
+/* libvcd main header */
+
+#ifndef __VCD_H__
+#define __VCD_H__
+
+/* Private headers */
+#include "image_sink.h"
+#include "mpeg_stream.h"
+#include "stream.h"
+
+#include <libvcd/types.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif /* __cplusplus */
+
+/* allocates and initializes a new VideoCD object */
+VcdObj *
+vcd_obj_new (vcd_type_t vcd_type);
+
+/* VideoCD parameters */
+typedef enum {
+ VCD_PARM_INVALID = 0,
+ VCD_PARM_VOLUME_ID, /* char * max length 32 */
+ VCD_PARM_PUBLISHER_ID, /* char * max length 128 */
+ VCD_PARM_PREPARER_ID, /* char * max length 128 */
+ VCD_PARM_ALBUM_ID, /* char * max length 16 */
+ VCD_PARM_VOLUME_COUNT, /* unsigned [1..65535] */
+ VCD_PARM_VOLUME_NUMBER, /* unsigned [0..65535] */
+ VCD_PARM_RESTRICTION, /* unsigned [0..3] */
+ VCD_PARM_NEXT_VOL_LID2, /* bool */
+ VCD_PARM_NEXT_VOL_SEQ2, /* bool */
+ VCD_PARM_APPLICATION_ID, /* char * max length 128 */
+ VCD_PARM_SEC_TYPE, /* unsigned [2336, 2352] */
+ VCD_PARM_SVCD_VCD3_MPEGAV, /* bool */
+ VCD_PARM_SVCD_VCD3_ENTRYSVD, /* bool */
+ VCD_PARM_SVCD_VCD3_TRACKSVD, /* bool */
+ VCD_PARM_UPDATE_SCAN_OFFSETS, /* bool */
+ VCD_PARM_RELAXED_APS, /* bool */
+ VCD_PARM_LEADOUT_PAUSE, /* bool */
+ VCD_PARM_LEADOUT_PREGAP, /* unsigned [0..300] */
+ VCD_PARM_TRACK_PREGAP, /* unsigned [1..300] */
+ VCD_PARM_TRACK_FRONT_MARGIN, /* unsigned [0..150] */
+ VCD_PARM_TRACK_REAR_MARGIN /* unsigned [0..150] */
+} vcd_parm_t;
+
+/* sets VideoCD parameter */
+int
+vcd_obj_set_param_uint (VcdObj *obj, vcd_parm_t param, unsigned arg);
+
+int
+vcd_obj_set_param_str (VcdObj *obj, vcd_parm_t param, const char *arg);
+
+int
+vcd_obj_set_param_bool (VcdObj *obj, vcd_parm_t param, bool arg);
+
+/* add custom files; if raw_flag set, the data source has to include a
+ mode2 subheader, and thus needs to be a multiple of 2336 byte blocksize */
+int
+vcd_obj_add_file (VcdObj *obj, const char iso_pathname[],
+ VcdDataSource *file, bool raw_flag);
+
+int
+vcd_obj_add_dir (VcdObj *obj, const char iso_pathname[]);
+
+/* this is for actually adding mpeg items to VCD, returns
+ a negative value for error.. */
+
+int
+vcd_obj_append_sequence_play_item (VcdObj *obj, VcdMpegSource *mpeg_source,
+ const char item_id[],
+ const char default_entry_id[]);
+
+int
+vcd_obj_add_sequence_entry (VcdObj *obj, const char sequence_id[],
+ double entry_time, const char entry_id[]);
+
+int
+vcd_obj_add_sequence_pause (VcdObj *obj, const char sequence_id[],
+ double pause_timestamp, const char pause_id[]);
+
+int
+vcd_obj_add_segment_pause (VcdObj *obj, const char segment_id[],
+ double pause_timestamp, const char pause_id[]);
+
+int
+vcd_obj_append_segment_play_item (VcdObj *obj, VcdMpegSource *mpeg_source,
+ const char item_id[]);
+
+/* warning -- api will change for pbc */
+typedef struct _pbc_t pbc_t;
+
+int
+vcd_obj_append_pbc_node (VcdObj *obj, struct _pbc_t *_pbc);
+
+/* removes item (sequence, entry, segment, ...) by id, returns
+ negative value on error */
+int
+vcd_obj_remove_item (VcdObj *obj, const char id[]);
+
+/* returns image size in sectors */
+long
+vcd_obj_get_image_size (VcdObj *obj);
+
+/* this one is to be called when every parameter has been set and the
+ image is about to be written. returns sectors to be written... */
+long
+vcd_obj_begin_output (VcdObj *obj);
+
+/* callback hook called every few (>|<) iterations, if it returns a value != 0
+ the writing process gets aborted */
+typedef struct
+{
+ long sectors_written;
+ long total_sectors;
+ int in_track;
+ int total_tracks;
+}
+progress_info_t;
+
+typedef int (*progress_callback_t) (const progress_info_t *progress_info,
+ void *user_data);
+
+/* writes the actual bin image file; a return value != 0 means the
+ action was aborted by user or some other error has occured... */
+int
+vcd_obj_write_image (VcdObj *obj, VcdImageSink *image_sink,
+ progress_callback_t callback, void *user_data,
+ const time_t *create_time);
+
+/* this should be called writing the bin and/or cue file is done---even if
+ an error occurred */
+void
+vcd_obj_end_output (VcdObj *obj);
+
+/* destructor for VideoCD objects; call this to destory a VideoCD
+ object created by vcd_obj_new () */
+void
+vcd_obj_destroy (VcdObj *obj);
+
+const char *
+vcd_version_string (bool full_text);
+
+#ifdef __cplusplus
+}
+#endif /* __cplusplus */
+
+#endif /* __VCD_H__ */
+
+/*
+ * Local variables:
+ * c-file-style: "gnu"
+ * tab-width: 8
+ * indent-tabs-mode: nil
+ * End:
+ */
diff --git a/src/input/vcd/libvcd/vcd_read.c b/src/input/vcd/libvcd/vcd_read.c
new file mode 100644
index 000000000..97db8899a
--- /dev/null
+++ b/src/input/vcd/libvcd/vcd_read.c
@@ -0,0 +1,108 @@
+/*
+ $Id: vcd_read.c,v 1.1 2003/10/13 11:47:12 f1rmb Exp $
+
+ Copyright (C) 2001,2003 Herbert Valerio Riedel <hvr@gnu.org>
+ Copyright (C) 2003 Rocky Bernstein <rocky@gnu.org>
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+*/
+
+
+#include "vcd_read.h"
+#include "assert.h"
+#include <libvcd/inf.h>
+#include <libvcd/files.h>
+#include <libvcd/logging.h>
+
+#ifdef HAVE_STRING_H
+#include <string.h>
+#endif
+
+bool
+read_pvd(CdIo *cdio, iso9660_pvd_t *pvd)
+{
+ if (cdio_read_mode2_sector (cdio, pvd, ISO_PVD_SECTOR, false)) {
+ vcd_error ("error reading PVD sector (%d)", ISO_PVD_SECTOR);
+ return false;
+ }
+
+ if (pvd->type != ISO_VD_PRIMARY) {
+ vcd_error ("unexpected PVD type %d", pvd->type);
+ return false;
+ }
+
+ if (strncmp (pvd->id, ISO_STANDARD_ID, strlen (ISO_STANDARD_ID)))
+ {
+ vcd_error ("unexpected ID encountered (expected `"
+ ISO_STANDARD_ID "', got `%.5s'", pvd->id);
+ return false;
+ }
+ return true;
+}
+
+bool
+read_entries(CdIo *cdio, EntriesVcd *entries)
+{
+ if (cdio_read_mode2_sector (cdio, entries, ENTRIES_VCD_SECTOR, false)) {
+ vcd_error ("error reading Entries sector (%d)", ENTRIES_VCD_SECTOR);
+ return false;
+ }
+
+ /* analyze signature/type */
+
+ if (!strncmp (entries->ID, ENTRIES_ID_VCD, sizeof (entries->ID)))
+ return true;
+ else if (!strncmp (entries->ID, "ENTRYSVD", sizeof (entries->ID))) {
+ vcd_warn ("found (non-compliant) SVCD ENTRIES.SVD signature");
+ return true;
+ } else {
+ vcd_error ("unexpected ID signature encountered `%.8s'", entries->ID);
+ return false;
+ }
+}
+
+bool
+read_info(CdIo *cdio, InfoVcd *info, vcd_type_t *vcd_type)
+{
+ if (cdio_read_mode2_sector (cdio, info, INFO_VCD_SECTOR, false)) {
+ vcd_error ("error reading Info sector (%d)", INFO_VCD_SECTOR);
+ return false;
+ }
+
+ *vcd_type = vcd_files_info_detect_type (info);
+
+ /* analyze signature/type */
+
+ switch (*vcd_type)
+ {
+ case VCD_TYPE_VCD:
+ case VCD_TYPE_VCD11:
+ case VCD_TYPE_VCD2:
+ case VCD_TYPE_SVCD:
+ case VCD_TYPE_HQVCD:
+ vcd_debug ("%s detected", vcdinf_get_format_version_str(*vcd_type));
+ break;
+ case VCD_TYPE_INVALID:
+ vcd_error ("unknown ID encountered -- maybe not a proper (S)VCD?");
+ return false;
+ break;
+ default:
+ vcd_assert_not_reached ();
+ break;
+ }
+
+ return true;
+}
+
diff --git a/src/input/vcd/libvcd/vcd_read.h b/src/input/vcd/libvcd/vcd_read.h
new file mode 100644
index 000000000..8731fd4f6
--- /dev/null
+++ b/src/input/vcd/libvcd/vcd_read.h
@@ -0,0 +1,36 @@
+/*
+ $Id: vcd_read.h,v 1.1 2003/10/13 11:47:12 f1rmb Exp $
+
+ Copyright (C) 2003 Rocky Bernstein <rocky@gnu.org>
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+*/
+
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
+
+#include <cdio/cdio.h>
+#include <cdio/iso9660.h>
+
+/* FIXME: make this really private: */
+#include <libvcd/files_private.h>
+
+bool read_pvd(CdIo *cdio, iso9660_pvd_t *pvd);
+bool read_entries(CdIo *cdio, EntriesVcd *entries);
+bool read_info(CdIo *cdio, InfoVcd *info, vcd_type_t *vcd_type);
+
+
+
diff --git a/src/input/vcd/vcdio.c b/src/input/vcd/vcdio.c
new file mode 100644
index 000000000..c4b127935
--- /dev/null
+++ b/src/input/vcd/vcdio.c
@@ -0,0 +1,242 @@
+/*
+ $Id: vcdio.c,v 1.1 2003/10/13 11:47:11 f1rmb Exp $
+
+ Copyright (C) 2002,2003 Rocky Bernstein <rocky@panix.com>
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
+
+ */
+
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+
+/* Standard includes */
+#include <stdio.h>
+#include <stdlib.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <unistd.h>
+#include <fcntl.h>
+#include <time.h>
+#include <string.h>
+#ifdef HAVE_STRINGS_H
+#include <strings.h>
+#endif
+#include <errno.h>
+
+#include <sys/wait.h>
+#include <sys/ioctl.h>
+
+#ifdef HAVE_VCDNAV
+#include <libvcd/types.h>
+#include <libvcd/files.h>
+#include <cdio/iso9660.h>
+#else
+#include "libvcd/types.h"
+#include "libvcd/files.h"
+#include "cdio/iso9660.h"
+#endif
+
+#include "vcdplayer.h"
+#include "vcdio.h"
+
+#define LOG_ERR(this, s, args...) \
+ if (this != NULL && this->log_err != NULL) \
+ this->log_err("%s: "s, __func__ , ##args)
+
+#define FREE_AND_NULL(ptr) if (NULL != ptr) free(ptr); ptr = NULL;
+
+/*! Closes VCD device specified via "this", and also wipes memory of it
+ from it inside "this". */
+int
+vcdio_close(vcdplayer_input_t *this)
+{
+ this->opened = false;
+
+ FREE_AND_NULL(this->current_vcd_device);
+ FREE_AND_NULL(this->track);
+ FREE_AND_NULL(this->segment);
+ FREE_AND_NULL(this->entry);
+
+ return vcdinfo_close(this->vcd);
+}
+
+
+/*! Opens VCD device and initializes things.
+
+ - do nothing if the device had already been open and is the same device.
+ - if the device had been open and is a different, close it before trying
+ to open new device.
+*/
+bool
+vcdio_open(vcdplayer_input_t *this, char *intended_vcd_device)
+{
+ vcdinfo_obj_t *obj = this->vcd;
+ unsigned int i;
+
+ dbg_print(INPUT_DBG_CALL, "called with %s\n", intended_vcd_device);
+
+ if ( this->opened ) {
+ if ( strcmp(intended_vcd_device, this->current_vcd_device)==0 ) {
+ /* Already open and the same device, so do nothing */
+ return true;
+ } else {
+ /* Changing VCD device */
+ vcdio_close(this);
+ }
+ }
+
+ if ( vcdinfo_open(&this->vcd, &intended_vcd_device, DRIVER_UNKNOWN, NULL) !=
+ VCDINFO_OPEN_VCD) {
+ return false;
+ }
+
+ obj = this->vcd;
+
+ this->current_vcd_device=strdup(intended_vcd_device);
+ this->opened = true;
+ this->num_LIDs = vcdinfo_get_num_LIDs(obj);
+
+ if (vcdinfo_read_psd (obj)) {
+
+ vcdinfo_visit_lot (obj, false);
+
+ if (vcdinfo_get_psd_x_size(obj))
+ vcdinfo_visit_lot (obj, true);
+ }
+
+ /*
+ Save summary info on tracks, segments and entries...
+ */
+
+ if ( 0 < (this->num_tracks = vcdinfo_get_num_tracks(obj)) ) {
+ this->track = (vcdplayer_play_item_info *)
+ calloc(this->num_tracks, sizeof(vcdplayer_play_item_info));
+
+ for (i=0; i<this->num_tracks; i++) {
+ unsigned int track_num=i+1;
+ this->track[i].size = vcdinfo_get_track_sect_count(obj, track_num);
+ this->track[i].start_LSN = vcdinfo_get_track_lsn(obj, track_num);
+ }
+ } else
+ this->track = NULL;
+
+ if ( 0 < (this->num_entries = vcdinfo_get_num_entries(obj)) ) {
+ this->entry = (vcdplayer_play_item_info *)
+ calloc(this->num_entries, sizeof(vcdplayer_play_item_info));
+
+ for (i=0; i<this->num_entries; i++) {
+ this->entry[i].size = vcdinfo_get_entry_sect_count(obj, i);
+ this->entry[i].start_LSN = vcdinfo_get_entry_lsn(obj, i);
+ }
+ } else
+ this->entry = NULL;
+
+ if ( 0 < (this->num_segments = vcdinfo_get_num_segments(obj)) ) {
+ this->segment = (vcdplayer_play_item_info *)
+ calloc(this->num_segments, sizeof(vcdplayer_play_item_info));
+
+ for (i=0; i<this->num_segments; i++) {
+ this->segment[i].size = vcdinfo_get_seg_sector_count(obj, i);
+ this->segment[i].start_LSN = vcdinfo_get_seg_lsn(obj, i);
+ }
+ } else
+ this->segment = NULL;
+
+ return true;
+}
+
+/*!
+ seek position, return new position
+
+ if seeking failed, -1 is returned
+*/
+off_t
+vcdio_seek (vcdplayer_input_t *this, off_t offset, int origin)
+{
+
+ switch (origin) {
+ case SEEK_SET:
+ {
+ lsn_t old_lsn = this->cur_lsn;
+ this->cur_lsn = this->origin_lsn + (offset / M2F2_SECTOR_SIZE);
+
+ dbg_print(INPUT_DBG_SEEK_SET, "seek_set to %ld => %u (start is %u)\n",
+ (long int) offset, this->cur_lsn, this->origin_lsn);
+
+ /* Seek was successful. Invalidate entry location by setting
+ entry number back to 1. Over time it will adjust upward
+ to the correct value. */
+ if ( !vcdplayer_pbc_is_on(this)
+ && this->play_item.type != VCDINFO_ITEM_TYPE_TRACK
+ && this->cur_lsn < old_lsn) {
+ dbg_print(INPUT_DBG_SEEK_SET, "seek_set entry backwards\n");
+ this->next_entry = 1;
+ }
+ break;
+ }
+
+ case SEEK_CUR:
+ {
+ off_t diff;
+ if (offset) {
+ LOG_ERR(this, "%s: %d\n",
+ _("SEEK_CUR not implemented for nozero offset"),
+ (int) offset);
+ return (off_t) -1;
+ }
+
+ if (this->slider_length == VCDPLAYER_SLIDER_LENGTH_TRACK) {
+ diff = this->cur_lsn - this->track_lsn;
+ dbg_print(INPUT_DBG_SEEK_CUR,
+ "current pos: %u, track diff %ld\n",
+ this->cur_lsn, (long int) diff);
+ } else {
+ diff = this->cur_lsn - this->origin_lsn;
+ dbg_print(INPUT_DBG_SEEK_CUR,
+ "current pos: %u, entry diff %ld\n",
+ this->cur_lsn, (long int) diff);
+ }
+
+ if (diff < 0) {
+ dbg_print(INPUT_DBG_SEEK_CUR, "Error: diff < 0\n");
+ return (off_t) 0;
+ } else {
+ return (off_t)diff * M2F2_SECTOR_SIZE;
+ }
+
+ break;
+ }
+
+ case SEEK_END:
+ LOG_ERR(this, "%s\n", _("SEEK_END not implemented yet."));
+ return (off_t) -1;
+ default:
+ LOG_ERR(this, "%s %d\n", _("seek not implemented yet for"),
+ origin);
+ return (off_t) -1;
+ }
+
+ return offset ; /* FIXME */
+}
+
+/*
+ * Local variables:
+ * c-file-style: "gnu"
+ * tab-width: 8
+ * indent-tabs-mode: nil
+ * End:
+ */
diff --git a/src/input/vcd/vcdio.h b/src/input/vcd/vcdio.h
new file mode 100644
index 000000000..98a236d42
--- /dev/null
+++ b/src/input/vcd/vcdio.h
@@ -0,0 +1,66 @@
+/*
+ $Id: vcdio.h,v 1.1 2003/10/13 11:47:11 f1rmb Exp $
+
+ Copyright (C) 2002 Rocky Bernstein <rocky@panix.com>
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
+
+*/
+
+#ifndef _VCDIO_H_
+#define _VCDIO_H_
+
+/*!
+ From xine plugin spec:
+
+ read nlen bytes, return number of bytes read.
+*/
+off_t
+vcdio_read (vcdplayer_input_t *this, char *buf, const off_t nlen);
+
+/*! Opens VCD device and initializes things.
+
+ - do nothing if the device had already been open and is the same device.
+ - if the device had been open and is a different, close it before trying
+ to open new device.
+*/
+bool
+vcdio_open(vcdplayer_input_t *this, char *intended_vcd_device);
+
+/*! Closes VCD device specified via "this", and also wipes memory of it
+ from it inside "this". */
+/* FIXME Move player stuff to player. */
+int
+vcdio_close(vcdplayer_input_t *this);
+
+/*!
+ From xine plugin spec:
+
+ seek position, return new position
+
+ if seeking failed, -1 is returned
+*/
+off_t
+vcdio_seek (vcdplayer_input_t *this, off_t offset, int origin);
+
+#endif /* _VCDIO_H_ */
+
+/*
+ * Local variables:
+ * c-file-style: "gnu"
+ * tab-width: 8
+ * indent-tabs-mode: nil
+ * End:
+ */
diff --git a/src/input/vcd/vcdplayer.c b/src/input/vcd/vcdplayer.c
new file mode 100644
index 000000000..c3f9069da
--- /dev/null
+++ b/src/input/vcd/vcdplayer.c
@@ -0,0 +1,1040 @@
+/*
+ $Id: vcdplayer.c,v 1.1 2003/10/13 11:47:11 f1rmb Exp $
+
+ Copyright (C) 2002,2003 Rocky Bernstein <rocky@panix.com>
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
+
+ */
+
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+
+/* Standard includes */
+#include <stdio.h>
+#include <stdlib.h>
+#include <sys/types.h>
+#include <unistd.h>
+#include <time.h>
+#include <string.h>
+#ifdef HAVE_STRINGS_H
+#include <strings.h>
+#endif
+#include <errno.h>
+
+#ifdef HAVE_VCDNAV
+#include <libvcd/files.h>
+#include <cdio/iso9660.h>
+#else
+#include "libvcd/files.h"
+#include "cdio/iso9660.h"
+#endif
+
+#include "vcdplayer.h"
+#include "vcdio.h"
+
+#define LOG_ERR(this, s, args...) \
+ if (this != NULL && this->log_err != NULL) \
+ this->log_err("%s: "s, __func__ , ##args)
+
+unsigned long int vcdplayer_debug = 0;
+
+static void _vcdplayer_set_origin(vcdplayer_input_t *this);
+
+/*!
+ Return true if playback control (PBC) is on
+*/
+bool
+vcdplayer_pbc_is_on(const vcdplayer_input_t *this)
+{
+ return VCDINFO_INVALID_ENTRY != this->cur_lid;
+}
+
+/* Given an itemid, return the size for the object (via information
+ previously stored when opening the vcd). */
+static size_t
+_vcdplayer_get_item_size(vcdplayer_input_t *this, vcdinfo_itemid_t itemid)
+{
+ switch (itemid.type) {
+ case VCDINFO_ITEM_TYPE_ENTRY:
+ return this->entry[itemid.num].size;
+ break;
+ case VCDINFO_ITEM_TYPE_SEGMENT:
+ return this->segment[itemid.num].size;
+ break;
+ case VCDINFO_ITEM_TYPE_TRACK:
+ return this->track[itemid.num-1].size;
+ break;
+ case VCDINFO_ITEM_TYPE_LID:
+ /* Play list number (LID) */
+ return 0;
+ break;
+ case VCDINFO_ITEM_TYPE_NOTFOUND:
+ case VCDINFO_ITEM_TYPE_SPAREID2:
+ default:
+ LOG_ERR(this, "%s %d\n", _("bad item type"), itemid.type);
+ return 0;
+ }
+}
+
+#define add_format_str_info(val) \
+ { \
+ const char *str = val; \
+ unsigned int len; \
+ if (val != NULL) { \
+ len=strlen(str); \
+ if (len != 0) { \
+ strncat(tp, str, TEMP_STR_LEN-(tp-temp_str)); \
+ tp += len; \
+ } \
+ saw_control_prefix = false; \
+ } \
+ }
+
+#define add_format_num_info(val, fmt) \
+ { \
+ char num_str[10]; \
+ unsigned int len; \
+ sprintf(num_str, fmt, val); \
+ len=strlen(num_str); \
+ if (len != 0) { \
+ strncat(tp, num_str, TEMP_STR_LEN-(tp-temp_str)); \
+ tp += len; \
+ } \
+ saw_control_prefix = false; \
+ }
+
+/*!
+ Take a format string and expand escape sequences, that is sequences that
+ begin with %, with information from the current VCD.
+ The expanded string is returned. Here is a list of escape sequences:
+
+ %A : The album information
+ %C : The VCD volume count - the number of CD's in the collection.
+ %c : The VCD volume num - the number of the CD in the collection.
+ %F : The VCD Format, e.g. VCD 1.0, VCD 1.1, VCD 2.0, or SVCD
+ %I : The current entry/segment/playback type, e.g. ENTRY, TRACK, SEGMENT...
+ %L : The playlist ID prefixed with " LID" if it exists
+ %N : The current number of the above - a decimal number
+ %P : The publisher ID
+ %p : The preparer ID
+ %S : If we are in a segment (menu), the kind of segment
+ %T : The track number
+ %V : The volume set ID
+ %v : The volume ID
+ A number between 1 and the volume count.
+ %% : a %
+*/
+char *
+vcdplayer_format_str(vcdplayer_input_t *this, const char format_str[])
+{
+#define TEMP_STR_SIZE 256
+#define TEMP_STR_LEN (TEMP_STR_SIZE-1)
+ static char temp_str[TEMP_STR_SIZE];
+ size_t i;
+ char * tp = temp_str;
+ bool saw_control_prefix = false;
+ size_t format_len = strlen(format_str);
+ vcdinfo_obj_t *obj = this->vcd;
+
+ bzero(temp_str, TEMP_STR_SIZE);
+
+ for (i=0; i<format_len; i++) {
+
+ if (!saw_control_prefix && format_str[i] != '%') {
+ *tp++ = format_str[i];
+ saw_control_prefix = false;
+ continue;
+ }
+
+ switch(format_str[i]) {
+ case '%':
+ if (saw_control_prefix) {
+ *tp++ = '%';
+ }
+ saw_control_prefix = !saw_control_prefix;
+ break;
+ case 'A':
+ add_format_str_info(vcdinfo_strip_trail(vcdinfo_get_album_id(obj),
+ MAX_ALBUM_LEN));
+ break;
+
+ case 'c':
+ add_format_num_info(vcdinfo_get_volume_num(obj), "%d");
+ break;
+
+ case 'C':
+ add_format_num_info(vcdinfo_get_volume_count(obj), "%d");
+ break;
+
+ case 'F':
+ add_format_str_info(vcdinfo_get_format_version_str(obj));
+ break;
+
+ case 'I':
+ {
+ switch (this->play_item.type) {
+ case VCDINFO_ITEM_TYPE_TRACK:
+ strncat(tp, "Track", TEMP_STR_LEN-(tp-temp_str));
+ tp += strlen("Track");
+ break;
+ case VCDINFO_ITEM_TYPE_ENTRY:
+ strncat(tp, "Entry", TEMP_STR_LEN-(tp-temp_str));
+ tp += strlen("Entry");
+ break;
+ case VCDINFO_ITEM_TYPE_SEGMENT:
+ strncat(tp, "Segment", TEMP_STR_LEN-(tp-temp_str));
+ tp += strlen("Segment");
+ break;
+ case VCDINFO_ITEM_TYPE_LID:
+ strncat(tp, "List ID", TEMP_STR_LEN-(tp-temp_str));
+ tp += strlen("List ID");
+ break;
+ case VCDINFO_ITEM_TYPE_SPAREID2:
+ strncat(tp, "Navigation", TEMP_STR_LEN-(tp-temp_str));
+ tp += strlen("Navigation");
+ break;
+ default:
+ /* What to do? */
+ ;
+ }
+ saw_control_prefix = false;
+ }
+ break;
+
+ case 'L':
+ if (vcdplayer_pbc_is_on(this)) {
+ char num_str[10];
+ sprintf(num_str, " List ID %d", this->cur_lid);
+ strncat(tp, num_str, TEMP_STR_LEN-(tp-temp_str));
+ tp += strlen(num_str);
+ }
+ saw_control_prefix = false;
+ break;
+
+ case 'N':
+ add_format_num_info(this->play_item.num, "%d");
+ break;
+
+ case 'p':
+ add_format_str_info(vcdinfo_get_preparer_id(obj));
+ break;
+
+ case 'P':
+ add_format_str_info(vcdinfo_get_publisher_id(obj));
+ break;
+
+ case 'S':
+ if ( VCDINFO_ITEM_TYPE_SEGMENT==this->play_item.type ) {
+ char seg_type_str[10];
+
+ sprintf(seg_type_str, " %s",
+ vcdinfo_video_type2str(obj, this->play_item.num));
+ strncat(tp, seg_type_str, TEMP_STR_LEN-(tp-temp_str));
+ tp += strlen(seg_type_str);
+ }
+ saw_control_prefix = false;
+ break;
+
+ case 'T':
+ add_format_num_info(this->cur_track, "%d");
+ break;
+
+ case 'V':
+ add_format_str_info(vcdinfo_get_volumeset_id(obj));
+ break;
+
+ case 'v':
+ add_format_str_info(vcdinfo_get_volume_id(obj));
+ break;
+
+ default:
+ *tp++ = '%';
+ *tp++ = format_str[i];
+ saw_control_prefix = false;
+ }
+ }
+ return strdup(temp_str);
+}
+
+static void
+_vcdplayer_update_entry(vcdinfo_obj_t *obj, uint16_t ofs, uint16_t *entry,
+ const char *label)
+{
+ if ( ofs == VCDINFO_INVALID_OFFSET ) {
+ *entry = VCDINFO_INVALID_ENTRY;
+ } else {
+ vcdinfo_offset_t *off_t = vcdinfo_get_offset_t(obj, ofs);
+ if (off_t != NULL) {
+ *entry = off_t->lid;
+ dbg_print(INPUT_DBG_PBC, "%s: %d\n", label, off_t->lid);
+ } else
+ *entry = VCDINFO_INVALID_ENTRY;
+ }
+}
+
+/*!
+ Update next/prev/return/default navigation buttons (via this->cur_lid).
+ Update size of play-item (via this->play_item).
+*/
+void
+vcdplayer_update_nav(vcdplayer_input_t *this)
+{
+ int play_item = this->play_item.num;
+ vcdinfo_obj_t *obj = this->vcd;
+
+ int min_entry = 1;
+ int max_entry = 0;
+
+ if (vcdplayer_pbc_is_on(this)) {
+
+ vcdinfo_lid_get_pxd(obj, &(this->pxd), this->cur_lid);
+
+ switch (this->pxd.descriptor_type) {
+ case PSD_TYPE_SELECTION_LIST:
+ case PSD_TYPE_EXT_SELECTION_LIST:
+ if (this->pxd.psd == NULL) return;
+ _vcdplayer_update_entry(obj, vcdinf_psd_get_prev_offset(this->pxd.psd),
+ &(this->prev_entry), "prev");
+
+ _vcdplayer_update_entry(obj, vcdinf_psd_get_next_offset(this->pxd.psd),
+ &(this->next_entry), "next");
+
+ _vcdplayer_update_entry(obj, vcdinf_psd_get_return_offset(this->pxd.psd),
+ &(this->return_entry), "return");
+
+ _vcdplayer_update_entry(obj,
+ vcdinfo_get_default_offset(obj, this->cur_lid),
+ &(this->default_entry), "default");
+ break;
+ case PSD_TYPE_PLAY_LIST:
+ if (this->pxd.pld == NULL) return;
+ _vcdplayer_update_entry(obj, vcdinf_pld_get_prev_offset(this->pxd.pld),
+ &(this->prev_entry), "prev");
+
+ _vcdplayer_update_entry(obj, vcdinf_pld_get_next_offset(this->pxd.pld),
+ &(this->next_entry), "next");
+
+ _vcdplayer_update_entry(obj, vcdinf_pld_get_return_offset(this->pxd.pld),
+ &(this->return_entry), "return");
+ this->default_entry = VCDINFO_INVALID_ENTRY;
+ break;
+ case PSD_TYPE_END_LIST:
+ this->origin_lsn = this->cur_lsn = this->end_lsn = VCDINFO_NULL_LSN;
+ /* Fall through */
+ case PSD_TYPE_COMMAND_LIST:
+ this->next_entry = this->prev_entry = this->return_entry =
+ this->default_entry = VCDINFO_INVALID_ENTRY;
+ break;
+ }
+
+ this->update_title();
+ return;
+ }
+
+ /* PBC is not on. Set up for simplified next, prev, and return. */
+
+ switch (this->play_item.type) {
+ case VCDINFO_ITEM_TYPE_ENTRY:
+ case VCDINFO_ITEM_TYPE_SEGMENT:
+ case VCDINFO_ITEM_TYPE_TRACK:
+
+ switch (this->play_item.type) {
+ case VCDINFO_ITEM_TYPE_ENTRY:
+ max_entry = this->num_entries;
+ min_entry = 0; /* Can remove when Entries start at 1. */
+ this->cur_track = vcdinfo_get_track(obj, play_item);
+ this->track_lsn = vcdinfo_get_track_lsn(obj, this->cur_track);
+ break;
+ case VCDINFO_ITEM_TYPE_SEGMENT:
+ max_entry = this->num_segments;
+ this->cur_track = VCDINFO_INVALID_TRACK;
+
+ break;
+ case VCDINFO_ITEM_TYPE_TRACK:
+ max_entry = this->num_tracks;
+ this->cur_track = this->play_item.num;
+ this->track_lsn = vcdinfo_get_track_lsn(obj, this->cur_track);
+ break;
+ default: ; /* Handle exceptional cases below */
+ }
+
+ _vcdplayer_set_origin(this);
+ /* Set next, prev, return and default to simple and hopefully
+ useful values.
+ */
+ if (play_item+1 >= max_entry)
+ this->next_entry = VCDINFO_INVALID_ENTRY;
+ else
+ this->next_entry = play_item+1;
+
+ if (play_item-1 >= min_entry)
+ this->prev_entry = play_item-1;
+ else
+ this->prev_entry = VCDINFO_INVALID_ENTRY;
+
+ this->default_entry = play_item;
+ this->return_entry = min_entry;
+ break;
+
+ case VCDINFO_ITEM_TYPE_LID:
+ {
+ /* Should have handled above. */
+ break;
+ }
+ default: ;
+ }
+ this->update_title();
+}
+
+/*!
+ Set reading to play an entire track.
+*/
+static void
+_vcdplayer_set_track(vcdplayer_input_t *this, unsigned int track_num)
+{
+ if (track_num < 1 || track_num > this->num_tracks)
+ return;
+ else {
+ vcdinfo_obj_t *obj = this->vcd;
+ vcdinfo_itemid_t itemid;
+
+ itemid.num = track_num;
+ itemid.type = VCDINFO_ITEM_TYPE_TRACK;
+ this->in_still = 0;
+ this->cur_lsn = vcdinfo_get_track_lsn(obj, track_num);
+ this->play_item = itemid;
+ this->cur_track = track_num;
+ this->track_lsn = this->cur_lsn;
+
+ _vcdplayer_set_origin(this);
+
+ dbg_print(INPUT_DBG_LSN, "LSN: %u\n", this->cur_lsn);
+ }
+}
+
+/*!
+ Set reading to play an entry
+*/
+static void
+_vcdplayer_set_entry(vcdplayer_input_t *this, unsigned int num)
+{
+ vcdinfo_obj_t *obj = this->vcd;
+ unsigned int num_entries = vcdinfo_get_num_entries(obj);
+
+ if (num >= num_entries) {
+ LOG_ERR(this, "%s %d\n", _("bad entry number"), num);
+ return;
+ } else {
+ vcdinfo_itemid_t itemid;
+
+ itemid.num = num;
+ itemid.type = VCDINFO_ITEM_TYPE_ENTRY;
+ this->in_still = 0;
+ this->cur_lsn = vcdinfo_get_entry_lsn(obj, num);
+ this->play_item = itemid;
+ this->cur_track = vcdinfo_get_track(obj, num);
+ this->track_lsn = vcdinfo_get_track_lsn(obj, this->cur_track);
+ this->track_end_lsn = this->track_lsn +
+ this->track[this->cur_track-1].size;
+
+ _vcdplayer_set_origin(this);
+
+ dbg_print(INPUT_DBG_LSN, "LSN: %u, track_end LSN: %u\n",
+ this->cur_lsn, this->track_end_lsn);
+ }
+}
+
+/*!
+ Set reading to play an segment (e.g. still frame)
+*/
+static void
+_vcdplayer_set_segment(vcdplayer_input_t *this, unsigned int num)
+{
+ vcdinfo_obj_t *obj = this->vcd;
+ segnum_t num_segs = vcdinfo_get_num_segments(obj);
+
+ if (num >= num_segs) {
+ LOG_ERR(this, "%s %d\n", _("bad segment number"), num);
+ return;
+ } else {
+ vcdinfo_itemid_t itemid;
+
+ this->cur_lsn = vcdinfo_get_seg_lsn(obj, num);
+ this->cur_track = 0;
+
+ if (VCDINFO_NULL_LSN==this->cur_lsn) {
+ LOG_ERR(this, "%s %d\n",
+ _("Error in getting current segment number"), num);
+ return;
+ }
+
+ itemid.num = num;
+ itemid.type = VCDINFO_ITEM_TYPE_SEGMENT;
+ this->play_item = itemid;
+
+ _vcdplayer_set_origin(this);
+
+ dbg_print(INPUT_DBG_LSN, "LSN: %u\n", this->cur_lsn);
+ }
+}
+
+/* Play entry. */
+/* Play a single item. */
+static void
+vcdplayer_play_single_item(vcdplayer_input_t *this, vcdinfo_itemid_t itemid)
+{
+ vcdinfo_obj_t *obj = this->vcd;
+
+ dbg_print(INPUT_DBG_CALL, "called itemid.num: %d, itemid.type: %d\n",
+ itemid.num, itemid.type);
+
+ this->in_still = 0;
+
+ switch (itemid.type) {
+ case VCDINFO_ITEM_TYPE_SEGMENT:
+ {
+ vcdinfo_video_segment_type_t segtype
+ = vcdinfo_get_video_type(obj, itemid.num);
+ segnum_t num_segs = vcdinfo_get_num_segments(obj);
+
+ dbg_print(INPUT_DBG_PBC, "%s (%d), itemid.num: %d\n",
+ vcdinfo_video_type2str(obj, itemid.num),
+ (int) segtype, itemid.num);
+
+ if (itemid.num >= num_segs) return;
+ _vcdplayer_set_segment(this, itemid.num);
+
+ switch (segtype)
+ {
+ case VCDINFO_FILES_VIDEO_NTSC_STILL:
+ case VCDINFO_FILES_VIDEO_NTSC_STILL2:
+ case VCDINFO_FILES_VIDEO_PAL_STILL:
+ case VCDINFO_FILES_VIDEO_PAL_STILL2:
+ this->in_still = -5;
+ break;
+ default:
+ this->in_still = 0;
+ }
+
+ break;
+ }
+
+ case VCDINFO_ITEM_TYPE_TRACK:
+ dbg_print(INPUT_DBG_PBC, "track %d\n", itemid.num);
+ if (itemid.num < 1 || itemid.num > this->num_tracks) return;
+ _vcdplayer_set_track(this, itemid.num);
+ break;
+
+ case VCDINFO_ITEM_TYPE_ENTRY:
+ {
+ unsigned int num_entries = vcdinfo_get_num_entries(obj);
+ dbg_print(INPUT_DBG_PBC, "entry %d\n", itemid.num);
+ if (itemid.num >= num_entries) return;
+ _vcdplayer_set_entry(this, itemid.num);
+ break;
+ }
+
+ case VCDINFO_ITEM_TYPE_LID:
+ LOG_ERR(this, "%s\n", _("Should have converted this above"));
+ break;
+
+ case VCDINFO_ITEM_TYPE_NOTFOUND:
+ dbg_print(INPUT_DBG_PBC, "play nothing\n");
+ this->cur_lsn = this->end_lsn;
+ return;
+
+ default:
+ LOG_ERR(this, "item type %d not implemented.\n", itemid.type);
+ return;
+ }
+
+ this->play_item = itemid;
+
+ vcdplayer_update_nav(this);
+
+ /* Some players like xine, have a fifo queue of audio and video buffers
+ that need to be flushed when playing a new selection. */
+ /* if (this->flush_buffers)
+ this->flush_buffers(); */
+
+}
+
+/*
+ Get the next play-item in the list given in the LIDs. Note play-item
+ here refers to list of play-items for a single LID It shouldn't be
+ confused with a user's list of favorite things to play or the
+ "next" field of a LID which moves us to a different LID.
+ */
+static bool
+_vcdplayer_inc_play_item(vcdplayer_input_t *this)
+{
+ int noi;
+
+ dbg_print(INPUT_DBG_CALL, "called pli: %d\n", this->pdi);
+
+ if ( NULL == this || NULL == this->pxd.pld ) return false;
+
+ noi = vcdinf_pld_get_noi(this->pxd.pld);
+
+ if ( noi <= 0 ) return false;
+
+ /* Handle delays like autowait or wait here? */
+
+ this->pdi++;
+
+ if ( this->pdi < 0 || this->pdi >= noi ) return false;
+
+ else {
+ uint16_t trans_itemid_num=vcdinf_pld_get_play_item(this->pxd.pld,
+ this->pdi);
+ vcdinfo_itemid_t trans_itemid;
+
+ if (VCDINFO_INVALID_ITEMID == trans_itemid_num) return false;
+
+ vcdinfo_classify_itemid(trans_itemid_num, &trans_itemid);
+ dbg_print(INPUT_DBG_PBC, " play-item[%d]: %s\n",
+ this->pdi, vcdinfo_pin2str (trans_itemid_num));
+ vcdplayer_play_single_item(this, trans_itemid);
+ return true;
+ }
+}
+
+void
+vcdplayer_play(vcdplayer_input_t *this, vcdinfo_itemid_t itemid)
+{
+ dbg_print(INPUT_DBG_CALL, "called itemid.num: %d itemid.type: %d\n",
+ itemid.num, itemid.type);
+
+ if (!vcdplayer_pbc_is_on(this)) {
+ vcdplayer_play_single_item(this, itemid);
+ } else {
+ /* PBC on - Itemid.num is LID. */
+
+ vcdinfo_obj_t *obj = this->vcd;
+
+ if (obj == NULL) return;
+
+ this->cur_lid = itemid.num;
+ vcdinfo_lid_get_pxd(obj, &(this->pxd), itemid.num);
+
+ switch (this->pxd.descriptor_type) {
+
+ case PSD_TYPE_SELECTION_LIST:
+ case PSD_TYPE_EXT_SELECTION_LIST: {
+ vcdinfo_itemid_t trans_itemid;
+ uint16_t trans_itemid_num;
+
+ if (this->pxd.psd == NULL) return;
+ trans_itemid_num = vcdinf_psd_get_itemid(this->pxd.psd);
+ vcdinfo_classify_itemid(trans_itemid_num, &trans_itemid);
+ this->loop_count = 1;
+ this->loop_item = trans_itemid;
+ vcdplayer_play_single_item(this, trans_itemid);
+ break;
+ }
+
+ case PSD_TYPE_PLAY_LIST: {
+ if (this->pxd.pld == NULL) return;
+ this->pdi = -1;
+ _vcdplayer_inc_play_item(this);
+ break;
+ }
+
+ case PSD_TYPE_END_LIST:
+ case PSD_TYPE_COMMAND_LIST:
+
+ default:
+ ;
+ }
+ }
+}
+
+/*
+ Set's start origin and size for subsequent seeks.
+ input: this->cur_lsn, this->play_item
+ changed: this->origin_lsn, this->end_lsn
+*/
+static void
+_vcdplayer_set_origin(vcdplayer_input_t *this)
+{
+ size_t size = _vcdplayer_get_item_size(this, this->play_item);
+
+ this->end_lsn = this->cur_lsn + size;
+ this->origin_lsn = this->cur_lsn;
+
+ dbg_print((INPUT_DBG_CALL|INPUT_DBG_LSN), "end LSN: %u\n", this->end_lsn);
+}
+
+#define RETURN_NULL_BLOCK \
+ memset (buf, 0, M2F2_SECTOR_SIZE); \
+ buf[0] = 0; buf[1] = 0; buf[2] = 0x01; \
+ return READ_BLOCK
+
+#define RETURN_NULL_STILL \
+ memset (buf, 0, M2F2_SECTOR_SIZE); \
+ buf[0] = 0; buf[1] = 0; buf[2] = 0x01; \
+ return READ_STILL_FRAME
+
+#define SLEEP_1_SEC_AND_HANDLE_EVENTS \
+ if (this->handle_events()) goto skip_next_play; \
+ this->sleep(250000); \
+ if (this->handle_events()) goto skip_next_play; \
+ this->sleep(250000); \
+ if (this->handle_events()) goto skip_next_play; \
+ this->sleep(250000); \
+ if (this->handle_events()) goto skip_next_play; \
+ this->sleep(250000);
+/* if (this->in_still) this->force_redisplay(); */
+
+
+/* Handles PBC navigation when reaching the end of a play item. */
+static vcdplayer_read_status_t
+vcdplayer_pbc_nav (vcdplayer_input_t *this, uint8_t *buf)
+{
+ /* We are in playback control. */
+ vcdinfo_itemid_t itemid;
+
+ if (0 != this->in_still && this->in_still != -5) {
+ SLEEP_1_SEC_AND_HANDLE_EVENTS;
+ if (this->in_still > 0) this->in_still--;
+ return READ_STILL_FRAME;
+ }
+
+ /* The end of an entry is really the end of the associated
+ sequence (or track). */
+
+ if ( (VCDINFO_ITEM_TYPE_ENTRY == this->play_item.type) &&
+ (this->cur_lsn < this->track_end_lsn) ) {
+ /* Set up to just continue to the next entry */
+ this->play_item.num++;
+ dbg_print( (INPUT_DBG_LSN|INPUT_DBG_PBC),
+ "continuing into next entry: %u\n", this->play_item.num);
+ vcdplayer_play_single_item(this, this->play_item);
+ this->update_title();
+ goto skip_next_play;
+ }
+
+ switch (this->pxd.descriptor_type) {
+ case PSD_TYPE_END_LIST:
+ return READ_END;
+ break;
+ case PSD_TYPE_PLAY_LIST: {
+ int wait_time = vcdinf_get_wait_time(this->pxd.pld);
+
+ dbg_print(INPUT_DBG_PBC, "playlist wait_time: %d\n", wait_time);
+
+ if (_vcdplayer_inc_play_item(this))
+ goto skip_next_play;
+
+ /* Handle any wait time given. */
+ if (-5 == this->in_still) {
+ if (wait_time != 0) {
+ this->in_still = wait_time - 1;
+ SLEEP_1_SEC_AND_HANDLE_EVENTS ;
+ return READ_STILL_FRAME;
+ }
+ }
+ break;
+ }
+ case PSD_TYPE_SELECTION_LIST: /* Selection List (+Ext. for SVCD) */
+ case PSD_TYPE_EXT_SELECTION_LIST: /* Extended Selection List (VCD2.0) */
+ {
+ int wait_time = vcdinf_get_timeout_time(this->pxd.psd);
+ uint16_t timeout_offs = vcdinf_get_timeout_offset(this->pxd.psd);
+ uint16_t max_loop = vcdinf_get_loop_count(this->pxd.psd);
+ vcdinfo_offset_t *offset_timeout_LID =
+ vcdinfo_get_offset_t(this->vcd, timeout_offs);
+
+ dbg_print(INPUT_DBG_PBC, "wait_time: %d, looped: %d, max_loop %d\n",
+ wait_time, this->loop_count, max_loop);
+
+ /* Handle any wait time given */
+ if (-5 == this->in_still) {
+ this->in_still = wait_time - 1;
+ SLEEP_1_SEC_AND_HANDLE_EVENTS ;
+ return READ_STILL_FRAME;
+ }
+
+ /* Handle any looping given. */
+ if ( max_loop == 0 || this->loop_count < max_loop ) {
+ this->loop_count++;
+ if (this->loop_count == 0x7f) this->loop_count = 0;
+ vcdplayer_play_single_item(this, this->loop_item);
+ if (this->in_still) this->force_redisplay();
+ goto skip_next_play;
+ }
+
+ /* Looping finished and wait finished. Move to timeout
+ entry or next entry, or handle still. */
+
+ if (NULL != offset_timeout_LID) {
+ /* Handle timeout_LID */
+ itemid.num = offset_timeout_LID->lid;
+ itemid.type = VCDINFO_ITEM_TYPE_LID;
+ dbg_print(INPUT_DBG_PBC, "timeout to: %d\n", itemid.num);
+ vcdplayer_play(this, itemid);
+ goto skip_next_play;
+ } else {
+ int num_selections = vcdinf_get_num_selections(this->pxd.psd);
+ if (num_selections > 0) {
+ /* Pick a random selection. */
+ unsigned int bsn=vcdinf_get_bsn(this->pxd.psd);
+ int rand_selection=bsn +
+ (int) ((num_selections+0.0)*rand()/(RAND_MAX+1.0));
+ lid_t rand_lid=vcdplayer_selection2lid (this, rand_selection);
+ itemid.num = rand_lid;
+ itemid.type = VCDINFO_ITEM_TYPE_LID;
+ dbg_print(INPUT_DBG_PBC, "random selection %d, lid: %d\n",
+ rand_selection - bsn, rand_lid);
+ vcdplayer_play(this, itemid);
+ goto skip_next_play;
+ } else if (this->in_still) {
+ /* Hack: Just go back and do still again */
+ SLEEP_1_SEC_AND_HANDLE_EVENTS ;
+ RETURN_NULL_STILL ;
+ }
+ }
+
+ break;
+ }
+ case VCDINFO_ITEM_TYPE_NOTFOUND:
+ LOG_ERR(this, "NOTFOUND in PBC -- not supposed to happen\n");
+ break;
+ case VCDINFO_ITEM_TYPE_SPAREID2:
+ LOG_ERR(this, "SPAREID2 in PBC -- not supposed to happen\n");
+ break;
+ case VCDINFO_ITEM_TYPE_LID:
+ LOG_ERR(this, "LID in PBC -- not supposed to happen\n");
+ break;
+
+ default:
+ ;
+ }
+ /* FIXME: Should handle autowait ... */
+ itemid.num = this->next_entry;
+ itemid.type = VCDINFO_ITEM_TYPE_LID;
+ vcdplayer_play(this, itemid);
+ skip_next_play: ;
+ return READ_BLOCK;
+}
+
+/* Handles navigation when NOT in PBC reaching the end of a play item.
+ The navigations rules here we are sort of made up, but the intent
+ is to do something that's probably right or helpful.
+*/
+static vcdplayer_read_status_t
+vcdplayer_non_pbc_nav (vcdplayer_input_t *this, uint8_t *buf)
+{
+ /* Not in playback control. Do we advance automatically or stop? */
+ switch (this->play_item.type) {
+ case VCDINFO_ITEM_TYPE_TRACK:
+ case VCDINFO_ITEM_TYPE_ENTRY:
+ if (this->autoadvance && this->next_entry != VCDINFO_INVALID_ENTRY) {
+ this->play_item.num=this->next_entry;
+ vcdplayer_update_nav(this);
+ } else
+ return READ_END;
+ break;
+ case VCDINFO_ITEM_TYPE_SPAREID2:
+ printf("SPAREID2\n");
+ if (this->in_still) {
+ RETURN_NULL_STILL ;
+ /* Hack: Just go back and do still again */
+ /*this->force_redisplay();
+ this->cur_lsn = this->origin_lsn;*/
+ }
+ return READ_END;
+
+ case VCDINFO_ITEM_TYPE_NOTFOUND:
+ LOG_ERR(this, "NOTFOUND outside PBC -- not supposed to happen\n");
+ if (this->in_still) {
+ RETURN_NULL_STILL ;
+ /* Hack: Just go back and do still again */
+ /*this->force_redisplay();
+ this->cur_lsn = this->origin_lsn;*/
+ } else
+ return READ_END;
+ break;
+
+ case VCDINFO_ITEM_TYPE_LID:
+ LOG_ERR(this, "LID outside PBC -- not supposed to happen\n");
+ if (this->in_still) {
+ RETURN_NULL_STILL ;
+ /* Hack: Just go back and do still again */
+ /* this->force_redisplay();
+ this->cur_lsn = this->origin_lsn; */
+ } else
+ return READ_END;
+ break;
+
+ case VCDINFO_ITEM_TYPE_SEGMENT:
+ if (this->in_still) {
+ /* Hack: Just go back and do still again */
+ RETURN_NULL_STILL ;
+ }
+ return READ_END;
+ }
+ return READ_BLOCK;
+}
+
+
+/*!
+ Read nlen bytes into buf and return the status back.
+
+ This routine is a bit complicated because on reaching the end of
+ a track or entry we may automatically advance to the item, or
+ interpret the next item in the playback-control list.
+*/
+vcdplayer_read_status_t
+vcdplayer_read (vcdplayer_input_t *this, uint8_t *buf, const off_t nlen)
+{
+
+ this->handle_events ();
+
+ if ( this->cur_lsn >= this->end_lsn ) {
+ vcdplayer_read_status_t read_status;
+
+ /* We've run off of the end of this entry. Do we continue or stop? */
+ dbg_print( (INPUT_DBG_LSN|INPUT_DBG_PBC),
+ "end reached, cur: %u, end: %u\n", this->cur_lsn, this->end_lsn);
+
+ handle_item_continuation:
+ read_status = vcdplayer_pbc_is_on(this)
+ ? vcdplayer_pbc_nav(this, buf)
+ : vcdplayer_non_pbc_nav(this, buf);
+
+ if (READ_BLOCK != read_status) return read_status;
+ }
+
+ /* Read the next block.
+
+ Important note: we probably speed things up by removing "data"
+ and the memcpy to it by extending vcd_image_source_read_mode2
+ to allow a mode to do what's below in addition to its
+ "raw" and "block" mode. It also would probably improve the modularity
+ a little bit as well.
+ */
+
+ {
+ CdIo *img = vcdinfo_get_cd_image(this->vcd);
+ typedef struct {
+ uint8_t subheader [8];
+ uint8_t data [M2F2_SECTOR_SIZE];
+ } vcdsector_t;
+ vcdsector_t vcd_sector;
+
+ do {
+ dbg_print(INPUT_DBG_LSN, "LSN: %u\n", this->cur_lsn);
+ if (cdio_read_mode2_sector(img, &vcd_sector, this->cur_lsn, true)!=0) {
+ dbg_print(INPUT_DBG_LSN, "read error\n");
+ return READ_ERROR;
+ }
+ this->cur_lsn++;
+
+ if ( this->cur_lsn >= this->end_lsn ) {
+ /* We've run off of the end of this entry. Do we continue or stop? */
+ dbg_print( (INPUT_DBG_LSN|INPUT_DBG_PBC),
+ "end reached in reading, cur: %u, end: %u\n",
+ this->cur_lsn, this->end_lsn);
+ break;
+ }
+
+ /* Check header ID for a padding sector and simply discard
+ these. It is alleged that VCD's put these in to keep the
+ bitrate constant.
+ */
+ } while((vcd_sector.subheader[2]&~0x01)==0x60);
+
+ if ( this->cur_lsn >= this->end_lsn )
+ /* We've run off of the end of this entry. Do we continue or stop? */
+ goto handle_item_continuation;
+
+ memcpy (buf, vcd_sector.data, M2F2_SECTOR_SIZE);
+ return READ_BLOCK;
+ }
+}
+
+/* Do if needed */
+void
+vcdplayer_send_button_update(vcdplayer_input_t *this, const int mode)
+{
+ /* dbg_print(INPUT_DBG_CALL, "Called\n"); */
+ return;
+}
+
+lid_t
+vcdplayer_selection2lid (vcdplayer_input_t *this, int entry_num)
+{
+ /* FIXME: Some of this probably gets moved to vcdinfo. */
+ /* Convert selection number to lid and then entry number...*/
+ unsigned int offset;
+ unsigned int bsn=vcdinf_get_bsn(this->pxd.psd);
+ vcdinfo_obj_t *obj = this->vcd;
+
+ dbg_print( (INPUT_DBG_CALL|INPUT_DBG_PBC),
+ "Called lid %u, entry_num %d bsn %d\n", this->cur_lid,
+ entry_num, bsn);
+
+ if ( (entry_num - bsn + 1) > 0) {
+ offset = vcdinfo_lid_get_offset(obj, this->cur_lid, entry_num-bsn+1);
+ } else {
+ LOG_ERR(this, "Selection number %u too small. bsn %u\n",
+ entry_num, bsn);
+ return VCDINFO_INVALID_LID;
+ }
+
+ if (offset != VCDINFO_INVALID_OFFSET) {
+ vcdinfo_offset_t *ofs;
+ int old = entry_num;
+
+ switch (offset) {
+ case PSD_OFS_DISABLED:
+ LOG_ERR(this, "Selection %u disabled\n", entry_num);
+ return VCDINFO_INVALID_LID;
+ case PSD_OFS_MULTI_DEF:
+ LOG_ERR(this, "Selection %u multi_def\n", entry_num);
+ return VCDINFO_INVALID_LID;
+ case PSD_OFS_MULTI_DEF_NO_NUM:
+ LOG_ERR(this, "Selection %u multi_def_no_num\n", entry_num);
+ return VCDINFO_INVALID_LID;
+ default: ;
+ }
+
+ ofs = vcdinfo_get_offset_t(obj, offset);
+
+ if (NULL == ofs) {
+ LOG_ERR(this, "error in vcdinfo_get_offset\n");
+ return -1;
+ }
+ dbg_print(INPUT_DBG_PBC,
+ "entry %u turned into selection lid %u\n",
+ old, ofs->lid);
+ return ofs->lid;
+
+ } else {
+ LOG_ERR(this, "invalid or unset entry %u\n", entry_num);
+ return VCDINFO_INVALID_LID;
+ }
+}
+
+/*
+ * Local variables:
+ * c-file-style: "gnu"
+ * tab-width: 8
+ * indent-tabs-mode: nil
+ * End:
+ */
diff --git a/src/input/vcd/vcdplayer.h b/src/input/vcd/vcdplayer.h
new file mode 100644
index 000000000..09bd84936
--- /dev/null
+++ b/src/input/vcd/vcdplayer.h
@@ -0,0 +1,297 @@
+/*
+ $Id: vcdplayer.h,v 1.1 2003/10/13 11:47:11 f1rmb Exp $
+
+ Copyright (C) 2002,2003 Rocky Bernstein <rocky@panix.com>
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
+
+*/
+
+#ifndef _VCDPLAYER_H_
+#define _VCDPLAYER_H_
+
+#ifdef HAVE_VCDNAV
+#include <libvcd/info.h>
+#else
+#include "libvcd/info.h"
+#endif
+
+#ifdef ENABLE_NLS
+#include <locale.h>
+# include <libintl.h>
+# define _(String) dgettext ("libxine1", String)
+#else
+/* Stubs that do something close enough. */
+# define _(String) (String)
+#endif
+
+/*------------------------------------------------------------------
+ DEBUGGING
+---------------------------------------------------------------------*/
+
+/* Print *any* debug messages? */
+#define INPUT_DEBUG 1
+
+/* Debugging masks */
+
+#define INPUT_DBG_META 1 /* Meta information */
+#define INPUT_DBG_EVENT 2 /* input (keyboard/mouse) events */
+#define INPUT_DBG_MRL 4
+#define INPUT_DBG_EXT 8 /* Calls from external routines */
+#define INPUT_DBG_CALL 16 /* routine calls */
+#define INPUT_DBG_LSN 32 /* LSN changes */
+#define INPUT_DBG_PBC 64 /* Playback control */
+#define INPUT_DBG_CDIO 128 /* Debugging from CDIO */
+#define INPUT_DBG_SEEK_SET 256 /* Seeks to set location */
+#define INPUT_DBG_SEEK_CUR 512 /* Seeks to find current location */
+#define INPUT_DBG_STILL 1024 /* Still-frame */
+#define INPUT_DBG_VCDINFO 2048 /* Debugging from VCDINFO */
+
+/* Current debugging setting use above masks to interpret meaning of value. */
+extern unsigned long int vcdplayer_debug;
+
+#if INPUT_DEBUG
+#define dbg_print(mask, s, args...) \
+ if (vcdplayer_debug & mask) \
+ fprintf(stderr, "%s: "s, __func__ , ##args)
+#else
+#define dbg_print(mask, s, args...)
+#endif
+
+/*------------------------------------------------------------------
+ General definitions and structures.
+---------------------------------------------------------------------*/
+
+#define VCDPLAYER_IN_STILL 65535
+
+/* Some configuration enumerations. */
+typedef enum {
+ VCDPLAYER_SLIDER_LENGTH_AUTO,
+ VCDPLAYER_SLIDER_LENGTH_TRACK,
+ VCDPLAYER_SLIDER_LENGTH_ENTRY,
+} vcdplayer_slider_length_t;
+
+typedef enum {
+ VCDPLAYER_AUTOPLAY_TRACK = VCDINFO_ITEM_TYPE_TRACK,
+ VCDPLAYER_AUTOPLAY_ENTRY = VCDINFO_ITEM_TYPE_ENTRY,
+ VCDPLAYER_AUTOPLAY_SEGMENT = VCDINFO_ITEM_TYPE_SEGMENT,
+ VCDPLAYER_AUTOPLAY_PBC = VCDINFO_ITEM_TYPE_LID,
+} vcdplayer_autoplay_t;
+
+typedef struct {
+ lsn_t start_LSN; /* LSN where play item starts */
+ size_t size; /* size in sector units of play item. */
+} vcdplayer_play_item_info;
+
+typedef int (*generic_fn)();
+
+typedef struct vcdplayer_input_struct {
+ void *user_data; /* environment. Passed to called routines. */
+ int32_t buttonN;
+ vcdinfo_obj_t *vcd; /* Pointer to libvcd structures. */
+ int in_still; /* 0 if not in still,
+ -2 if in infinite loop
+ -5 if a still but haven't read wait time yet
+ >0 number of seconds yet to wait */
+
+ /*------------------------------------------------------------------
+ Callback functions - players and higher-level routines can use
+ this to customize their behavior when using this player-independent
+ code.
+ ---------------------------------------------------------------------*/
+
+ generic_fn log_msg; /* function to log a message in the player */
+ generic_fn log_err; /* function to log an error in the player */
+
+
+ /* Function to flush any audio or video buffers */
+ void (*flush_buffers) (void);
+
+ /* Function to flush sleep for a number of milliseconds. */
+ void (*sleep) (unsigned int usecs);
+
+ /* Function to flush sleep for a number of milliseconds. */
+ void (*force_redisplay) (void);
+
+ /* Function to handle player events. Returns true if play item changed. */
+ bool (*handle_events) (void);
+
+ /* Function to update title of selection. */
+ void (*update_title) ();
+
+ /*-------------------------------------------------------------
+ Playback control fields
+ --------------------------------------------------------------*/
+ int cur_lid; /* LID that play item is in. Implies PBC is.
+ on. VCDPLAYER_BAD_ENTRY if not none or
+ not in PBC */
+ PsdListDescriptor pxd; /* If PBC is on, the relevant PSD/PLD */
+ int pdi; /* current pld index of pxd. -1 if
+ no index*/
+
+ vcdinfo_itemid_t play_item; /* play-item, VCDPLAYER_BAD_ENTRY if none */
+ vcdinfo_itemid_t loop_item; /* Where do we loop back to? Meaningful only
+ in a selection list */
+ int loop_count; /* # of times play-item has been played.
+ Meaningful only in a selection list.
+ */
+ track_t cur_track; /* current track number. */
+
+ /*-----------------------------------
+ Navigation and location fields
+ ------------------------------------*/
+ uint16_t next_entry; /* where to go if next is pressed,
+ VCDPLAYER_BAD_ENTRY if none */
+ uint16_t prev_entry; /* where to fo if prev is pressed,
+ VCDPLAYER_BAD_ENTRY if none */
+ uint16_t return_entry; /* Entry index to use if return is pressed */
+ uint16_t default_entry; /* Default selection entry. */
+
+ lsn_t cur_lsn; /* LSN of where we are right now */
+ lsn_t end_lsn; /* LSN of end of current entry/segment/track. */
+ lsn_t origin_lsn; /* LSN of start of slider position. */
+ lsn_t track_lsn; /* LSN of start track origin of track we are in. */
+ lsn_t track_end_lsn; /* LSN of end of current track (if entry). */
+
+ /*--------------------------------------------------------------
+ Medium information
+ ---------------------------------------------------------------*/
+
+ char *current_vcd_device; /* VCD device currently open */
+ bool opened; /* true if initialized */
+
+ track_t num_tracks; /* Number of tracks in medium */
+ segnum_t num_segments; /* Number of segments in medium */
+ unsigned int num_entries; /* Number of entries in medium */
+ lid_t num_LIDs; /* Number of LIDs in medium */
+
+ /* Tracks, segment, and entry information. The number of entries for
+ each is given by the corresponding num_* field above. */
+ vcdplayer_play_item_info *track;
+ vcdplayer_play_item_info *segment;
+ vcdplayer_play_item_info *entry;
+
+ /*--------------------------------------------------------------
+ Configuration variables
+ ---------------------------------------------------------------*/
+
+ /* What type to use on autoplay */
+ vcdplayer_autoplay_t default_autoplay;
+
+ /* When hitting end of entry or track do we advance automatically
+ to next entry/track or stop? Only valid if PBC is off. */
+ bool autoadvance;
+
+ /* Do next/prev wrap around? Only valid if PBC is off. */
+ bool wrap_next_prev;
+
+ /* Show and be able to select rejected LIDs? */
+ bool show_rejected;
+
+ /* Whether GUI slider is track size or entry size. */
+ vcdplayer_slider_length_t slider_length;
+
+} vcdplayer_input_t;
+
+/* vcdplayer_read return status */
+typedef enum {
+ READ_BLOCK,
+ READ_STILL_FRAME,
+ READ_ERROR,
+ READ_END,
+} vcdplayer_read_status_t;
+
+
+/* ----------------------------------------------------------------------
+ Function Prototypes
+ -----------------------------------------------------------------------*/
+
+/*!
+ Return true if playback control (PBC) is on
+*/
+bool
+vcdplayer_pbc_is_on(const vcdplayer_input_t *this);
+
+/*!
+ Take a format string and expand escape sequences, that is sequences that
+ begin with %, with information from the current VCD.
+ The expanded string is returned. Here is a list of escape sequences:
+
+ %A : The album information
+ %C : The VCD volume count - the number of CD's in the collection.
+ %c : The VCD volume num - the number of the CD in the collection.
+ %F : The VCD Format, e.g. VCD 1.0, VCD 1.1, VCD 2.0, or SVCD
+ %I : The current entry/segment/playback type, e.g. ENTRY, TRACK, SEGMENT...
+ %L : The playlist ID prefixed with " LID" if it exists
+ %N : The current number of the above - a decimal number
+ %P : The publisher ID
+ %p : The preparer ID
+ %V : The volume set ID
+ %v : The volume ID
+ A number between 1 and the volume count.
+ %% : a %
+*/
+char *
+vcdplayer_format_str(vcdplayer_input_t *this, const char format_str[]);
+
+/*!
+ Update next/prev/return/default navigation buttons.
+*/
+void
+vcdplayer_update_nav(vcdplayer_input_t *this);
+
+/*! Update the player title text. */
+void
+vcdplayer_update_title_display(vcdplayer_input_t *this);
+
+/*! Play title part. If part is -1, use the first title. */
+void
+vcdplayer_play(vcdplayer_input_t *this, vcdinfo_itemid_t itemid);
+
+bool
+vcdplayer_open(vcdplayer_input_t *this, char *intended_vcd_device);
+
+/*!
+ Read nlen bytes into buf and return the status back.
+*/
+vcdplayer_read_status_t
+vcdplayer_read (vcdplayer_input_t *this, uint8_t *buf, const off_t nlen);
+
+/*!
+ seek position, return new position
+
+ if seeking failed, -1 is returned
+*/
+off_t
+vcdplayer_seek (vcdplayer_input_t *this, off_t offset, int origin);
+
+/*!
+ Get the number of tracks or titles of the VCD. The result is stored
+ in "titles".
+ */
+void
+vcdplayer_send_button_update(vcdplayer_input_t *this, int mode);
+
+lid_t
+vcdplayer_selection2lid (vcdplayer_input_t *this, int entry_num);
+
+#endif /* _VCDPLAYER_H_ */
+/*
+ * Local variables:
+ * c-file-style: "gnu"
+ * tab-width: 8
+ * indent-tabs-mode: nil
+ * End:
+ */
diff --git a/src/input/vcd/xine-extra.c b/src/input/vcd/xine-extra.c
new file mode 100644
index 000000000..27c89da22
--- /dev/null
+++ b/src/input/vcd/xine-extra.c
@@ -0,0 +1,148 @@
+/*
+ $Id: xine-extra.c,v 1.1 2003/10/13 11:47:11 f1rmb Exp $
+
+ Copyright (C) 2002 Rocky Bernstein <rocky@panix.com>
+
+ Program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
+
+ These are routines that probably should be in xine, but for whatever
+ reason aren't - yet.
+*/
+
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+
+#ifdef HAVE_VCDNAV
+#include <cdio/types.h>
+#else
+#include "cdio/types.h"
+#endif
+
+#include "xine-extra.h"
+
+static xine_t *my_xine = NULL;
+
+/* This is modified from xine_log() and should really reside inside
+ xine-lib/src/xine-engine/xine.c. It logs a message to "buf" and also
+ prints it to stream.
+*/
+
+static void
+xine_vflog(xine_t *this, FILE *stream, int buf, const char *format,
+ va_list args)
+{
+ this->log_buffers[buf]->scratch_printf(this->log_buffers[buf], format, args);
+ vfprintf(stream, format, args);
+}
+
+/*!
+ This routine is like xine_log, except it takes a va_list instead of
+ a variable number of arguments. It might be useful as a function
+ pointer where one wants a specific prototype.
+
+ In short this writes a message to buffer 'buf' and to stdout.
+*/
+
+void
+xine_vlog_msg(xine_t *this, int buf, const char *format, va_list args)
+{
+ xine_vflog(this, stdout, buf, format, args);
+}
+
+/*! This routine is like xine_log, except it takes a va_list instead
+ of a variable number of arguments and writes to stderr rather than
+ stdout. It might be useful as a function pointer where one wants a
+ specific prototype.
+
+ In short this writes a message to buffer 'buf' and to stderr.
+*/
+void
+xine_vlog_err(xine_t *this, int buf, const char *format, va_list args)
+{
+ xine_vflog(this, stderr, buf, format, args);
+}
+
+/*! Call this before calling any of the xine_log_msg or xine_log_err
+ routines. It sets up the xine buffer that will be used in error
+ logging.
+
+ \return true if everything went okay; false is returned if
+ logging was already initialized, in which case nothing is done.
+
+ */
+bool
+xine_log_init(xine_t *this)
+{
+ if (NULL == this) return false;
+ my_xine = this;
+ return true;
+}
+
+/*! This routine is like xine_log without any xine-specific paramenters.
+ Before calling this routine you should have set up a xine log buffer via
+ xine_log_init().
+
+ In short this writes a message to buffer 'buf' and to stdout.
+
+ \return true if everything went okay; false is there was
+ an error, such as logging wasn't initialized. On error, nothing is
+ logged.
+*/
+bool
+xine_log_msg(const char *format, ...)
+{
+ va_list args;
+
+ if (NULL == my_xine) return false;
+ va_start(args, format);
+ xine_vlog_msg(my_xine, XINE_LOG_MSG, format, args);
+ va_end(args);
+ return true;
+}
+
+/*! This routine is like xine_log without any xine-specific paramenters.
+ Before calling this routine you should have set up a xine log buffer via
+ xine_log_init().
+
+ In short this writes a message to buffer 'buf' and to stdout.
+
+ \return true if everything went okay; false is there was
+ an error, such as logging wasn't initialized. On error, nothing is
+ logged.
+*/
+bool
+xine_log_err(const char *format, ...)
+{
+ va_list args;
+
+ va_start(args, format);
+ if (NULL == my_xine) return false;
+ xine_vlog_err(my_xine, XINE_LOG_MSG, format, args);
+ va_end(args);
+ return true;
+}
+
+void
+xine_free_mrls(int *num_mrls, xine_mrl_t **mrls)
+{
+ (*num_mrls)--;
+ for ( ; *num_mrls >= 0; (*num_mrls)-- ) {
+ MRL_ZERO(mrls[*num_mrls]);
+ free(mrls[*num_mrls]);
+ }
+ mrls=NULL;
+ *num_mrls=0;
+}
diff --git a/src/input/vcd/xine-extra.h b/src/input/vcd/xine-extra.h
new file mode 100644
index 000000000..5a2e0b127
--- /dev/null
+++ b/src/input/vcd/xine-extra.h
@@ -0,0 +1,100 @@
+/*
+ $Id: xine-extra.h,v 1.1 2003/10/13 11:47:11 f1rmb Exp $
+
+ Copyright (C) 2002 Rocky Bernstein <rocky@panix.com>
+
+ Program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
+
+ These are routines that probably should be in xine, but for whatever
+ reason aren't - yet.
+*/
+
+#ifndef XINE_EXTRA_H
+#define XINE_EXTRA_H 1
+
+#define LOG_ERR(s, args...) \
+ xine_log_err("%s: "s"\n", __func__ , ##args)
+
+#define LOG_MSG(s, args...) \
+ xine_log_msg("%s: "s"\n", __func__ , ##args)
+
+#ifdef HAVE_VCDNAV
+#include <cdio/types.h>
+#else
+#include "cdio/types.h"
+#endif
+
+/* Xine includes */
+#include "xine_internal.h"
+#include "input_plugin.h"
+#include "xineutils.h"
+
+/*!
+ This routine is like xine_log, except it takes a va_list instead of
+ a variable number of arguments. It might be useful as a function
+ pointer where one wants a specific prototype.
+
+ In short this writes a message to buffer 'buf' and to stdout.
+*/
+void
+xine_vlog_msg(xine_t *this, int buf, const char *format, va_list args);
+
+/*! This routine is like xine_log, except it takes a va_list instead
+ of a variable number of arguments and writes to stderr rather than
+ stdout. It might be useful as a function pointer where one wants a
+ specific prototype.
+
+ In short this writes a message to buffer 'buf' and to stderr.
+*/
+void xine_vlog_err(xine_t *this, int buf, const char *format, va_list args);
+
+/*! Call this before calling any of the xine_log_msg or xine_log_err
+ routines. It sets up the xine buffer that will be used in error
+ logging.
+
+ \return true if everything went okay; false is returned if
+ logging was already initialized, in which case nothing is done.
+
+ */
+bool xine_log_init(xine_t *this);
+
+/*! This routine is like xine_log without any xine-specific paramenters.
+ Before calling this routine you should have set up a xine log buffer via
+ xine_log_init().
+
+ In short this writes a message to buffer 'buf' and to stdout.
+
+ \return true if everything went okay; false is there was
+ an error, such as logging wasn't initialized. On error, nothing is
+ logged.
+*/
+bool xine_log_msg(const char *format, ...);
+
+/*! This routine is like xine_log without any xine-specific paramenters.
+ Before calling this routine you should have set up a xine log buffer via
+ xine_log_init().
+
+ In short this writes a message to buffer 'buf' and to stdout.
+
+ \return true if everything went okay; false is there was
+ an error, such as logging wasn't initialized. On error, nothing is
+ logged.
+*/
+bool xine_log_err(const char *format, ...);
+
+/* Free all (num_mrls) MRLS. */
+void xine_free_mrls(int *num_mrls, xine_mrl_t **mrls);
+
+#endif /*XINE_EXTRA_H*/
diff --git a/src/input/vcd/xineplug_inp_vcd.c b/src/input/vcd/xineplug_inp_vcd.c
new file mode 100644
index 000000000..3084a32c2
--- /dev/null
+++ b/src/input/vcd/xineplug_inp_vcd.c
@@ -0,0 +1,1805 @@
+/*
+ $Id: xineplug_inp_vcd.c,v 1.1 2003/10/13 11:47:11 f1rmb Exp $
+
+ Copyright (C) 2002,2003 Rocky Bernstein <rocky@panix.com>
+
+ Program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
+*/
+
+/*
+ These are plugin routines called by the xine engine. See
+ Chapter 4. Extending xine's input
+ http://xinehq.de/index.php/hackersguide/index.php?resource=5.3&action=default#INPUT
+ and the comments in input_plugin.h
+
+ This is what is referred to below a "the xine plugin spec"
+
+ Please don't add any OS-specific code in here - #if defined(__sun)
+ or or #if defined(__linux__) are harbingers of such stuff. It took a
+ great deal of effort to get it *out* of here (or most of it); If you
+ feel the need to do so, you are doing something wrong and breaking
+ modularity.
+*/
+
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+
+/* Standard includes */
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <string.h>
+#include <errno.h>
+#include <ctype.h>
+
+#define SHORT_PLUGIN_NAME "VCDX"
+#define MRL_PREFIX "vcdx://"
+#define MRL_PREFIX_LEN strlen(MRL_PREFIX)
+
+#define xine_config_entry_t xine_cfg_entry_t
+
+/* Xine includes */
+#include "xineutils.h"
+#include "input_plugin.h"
+
+#include "xine-extra.h"
+
+#ifdef HAVE_VCDNAV
+#include <cdio/logging.h>
+#include <cdio/iso9660.h>
+#include <cdio/cd_types.h>
+
+/* libvcd includes */
+#include <libvcd/files.h>
+#include <libvcd/logging.h>
+#else
+#include "cdio/logging.h"
+#include "cdio/iso9660.h"
+#include "cdio/cd_types.h"
+/* libvcd includes */
+#include "libvcd/files.h"
+#include "libvcd/logging.h"
+#endif
+
+#include "vcdplayer.h"
+#include "vcdio.h"
+
+
+/* A xine define. */
+#ifndef BUF_DEMUX_BLOCK
+#define BUF_DEMUX_BLOCK 0x05000000
+#endif
+
+/*
+ Convert an autoplay enumeration into an vcdinfo itemtype enumeration.
+ See definitions in vcdplayer.h and vcdinfo.h to get the below correct.
+*/
+vcdinfo_item_enum_t autoplay2itemtype[]={
+ VCDINFO_ITEM_TYPE_TRACK, /* VCDPLAYER_AUTOPLAY_TRACK */
+ VCDINFO_ITEM_TYPE_ENTRY, /* VCDPLAYER_AUTOPLAY_ENTRY */
+ VCDINFO_ITEM_TYPE_SEGMENT, /* VCDPLAYER_AUTOPLAY_SEGMENT */
+ VCDINFO_ITEM_TYPE_LID /* VCDPLAYER_AUTOPLAY_PBC */
+};
+
+typedef struct
+{
+ char *title_format; /* Format string of GUI display title */
+ char *comment_format; /* Format string of stream comment meta */
+} vcd_config_t;
+
+typedef struct vcd_input_plugin_tag vcd_input_plugin_t;
+
+typedef struct {
+ input_class_t input_class;
+ xine_t *xine;
+ config_values_t *config; /* Pointer to XineRC config file. */
+ vcd_input_plugin_t *ip;
+
+ vcd_config_t v_config; /* config stuff passed to child */
+ xine_mrl_t **mrls; /* list of mrl entries for medium */
+ int num_mrls; /* count of above */
+ char *vcd_device;/* Device name to use when
+ none specified in MRL */
+ /*--------------------------------------------------------------
+ Media resource locator (MRL) info.
+
+ For the below offsets, use play_item + mrl_xxx_offset to get index
+ into "mrls" array
+ ---------------------------------------------------------------*/
+ int mrl_track_offset; /* perhaps -1 for tracks staring with 1*/
+ int mrl_entry_offset; /* num_tracks for entries starting with 0 */
+ int mrl_play_offset; /* num_tracks for entries starting with 0 */
+ int mrl_segment_offset; /* num_tracks + num_entries if segs start 1*/
+
+
+} vcd_input_class_t;
+
+vcd_input_class_t *vcd_class;
+
+struct vcd_input_plugin_tag {
+ input_plugin_t input_plugin; /* input plugin interface as defined by
+ by player. For xine it contains a
+ structure of functions that need
+ to be implemented.
+ */
+ xine_stream_t *stream;
+ xine_event_queue_t *event_queue;
+
+ vcd_input_class_t *class;
+ vcd_config_t v_config; /* Config stuff initially inherited */
+ bool jumped; /* True if we changed tracks or any
+ sort of discontinuity in playing */
+ char *mrl;
+
+ vcdplayer_input_t player ;
+};
+
+vcd_input_plugin_t my_vcd;
+
+/* Prototype definitions */
+static bool vcd_handle_events (void);
+static void vcd_close(vcd_input_class_t *class);
+
+/*
+ If class->vcd_device is NULL or the empty string,
+ Use libcdio to find a CD drive with a VCD in it.
+*/
+static bool
+vcd_get_default_device(vcd_input_class_t *class, bool log_msg_if_fail)
+{
+ if (NULL == class->vcd_device || strlen(class->vcd_device)==0) {
+ char **cd_drives=NULL;
+ cd_drives = cdio_get_devices_with_cap(NULL,
+(CDIO_FS_ANAL_SVCD|CDIO_FS_ANAL_CVD|CDIO_FS_ANAL_VIDEOCD|CDIO_FS_UNKNOWN),
+ true);
+ if (NULL == cd_drives || NULL == cd_drives[0]) {
+ LOG_MSG("%s", _("failed to find a device with a VCD"));
+ return false;
+ }
+ class->vcd_device = strdup(cd_drives[0]);
+ cdio_free_device_list(cd_drives);
+ }
+ return true;
+}
+
+
+static void
+meta_info_assign(int field, xine_stream_t *stream, const char * info)
+{
+ if (NULL != info) {
+ if (stream->meta_info[field])
+ free(stream->meta_info[field]);
+ dbg_print(INPUT_DBG_META, "meta[%d]: %s\n", field, info);
+ stream->meta_info[field] = strdup(info);
+ }
+}
+
+#define stream_info_assign(field, stream, info) \
+ stream->stream_info[field] = info;
+
+/* Set stream information. */
+static void
+vcd_set_meta_info (vcd_input_plugin_t *xine_vcd)
+{
+ vcdinfo_obj_t *obj= xine_vcd->player.vcd;
+ meta_info_assign(XINE_META_INFO_ALBUM, xine_vcd->stream,
+ vcdinfo_get_album_id(obj));
+ meta_info_assign(XINE_META_INFO_ARTIST, xine_vcd->stream,
+ vcdinfo_get_preparer_id(obj));
+ meta_info_assign(XINE_META_INFO_COMMENT, xine_vcd->stream,
+ vcdplayer_format_str(&xine_vcd->player,
+ xine_vcd->v_config.comment_format));
+ meta_info_assign(XINE_META_INFO_GENRE, xine_vcd->stream,
+ vcdinfo_get_format_version_str(obj));
+}
+
+static void
+vcd_force_redisplay (void)
+{
+#if 1
+ my_vcd.stream->xine->clock->adjust_clock(my_vcd.stream->xine->clock,
+ my_vcd.stream->xine->clock->get_current_time(my_vcd.stream->xine->clock)
+ + 30 * 90000 );
+#else
+ /* Alternate method that causes too much disruption... */
+ xine_set_param(my_vcd.stream, XINE_PARAM_VO_ASPECT_RATIO,
+ (xine_get_param(my_vcd.stream, XINE_PARAM_VO_ASPECT_RATIO)));
+#endif
+}
+
+/*! Add another MRL to the MRL list inside "this" to be displayed.
+ mrl is the string name to add; size is the size of the entry in bytes.
+ The number of mrls in "this" is incremented.
+*/
+static void
+vcd_add_mrl_slot(vcd_input_class_t *this, const char *mrl, off_t size,
+ unsigned int *i)
+{
+ dbg_print(INPUT_DBG_MRL, "called to add slot %d: %s, size %u\n",
+ *i, mrl, (unsigned int) size);
+
+ this->mrls[*i] = malloc(sizeof(xine_mrl_t));
+ if (NULL==this->mrls[*i]) {
+ LOG_ERR("Can't malloc %d bytes for MRL slot %d (%s)",
+ sizeof(xine_mrl_t), *i, mrl);
+ return;
+ }
+ this->mrls[*i]->link = NULL;
+ this->mrls[*i]->origin = NULL;
+ this->mrls[*i]->type = mrl_vcd;
+ this->mrls[*i]->size = size * M2F2_SECTOR_SIZE;
+
+ this->mrls[*i]->mrl = (char *) malloc(strlen(mrl) + 1);
+ if (NULL==this->mrls[*i]->mrl) {
+ LOG_ERR("Can't malloc %d bytes for MRL name %s", sizeof(xine_mrl_t), mrl);
+ } else {
+ sprintf(this->mrls[*i]->mrl, "%s", mrl);
+ }
+ (*i)++;
+}
+
+/*!
+ Return the associated mrl_offset for the given type.
+*/
+static int
+vcd_get_mrl_type_offset(vcd_input_plugin_t *inp,
+ vcdinfo_item_enum_t type, int *size)
+{
+ switch (type) {
+ case VCDINFO_ITEM_TYPE_ENTRY:
+ *size = inp->class->mrl_play_offset - inp->class->mrl_entry_offset + 1;
+ return inp->class->mrl_entry_offset;
+ break;
+ case VCDINFO_ITEM_TYPE_SEGMENT:
+ *size = inp->class->num_mrls - inp->class->mrl_segment_offset - 1;
+ return inp->class->mrl_segment_offset;
+ case VCDINFO_ITEM_TYPE_TRACK:
+ *size = inp->class->mrl_entry_offset;
+ return inp->class->mrl_track_offset;
+ case VCDINFO_ITEM_TYPE_LID:
+ /* Play list number (LID) */
+ *size = (inp->player.num_LIDs > 0) ? 1 : 0;
+ return inp->class->mrl_play_offset;
+ case VCDINFO_ITEM_TYPE_NOTFOUND:
+ case VCDINFO_ITEM_TYPE_SPAREID2:
+ default:
+ return -2;
+ }
+}
+
+/*!
+ Create a MRL list inside "class". Any existing MRL list is freed.
+ */
+static bool
+vcd_build_mrl_list(vcd_input_class_t *class, char *vcd_device)
+{
+
+ char mrl[1024];
+ vcdplayer_input_t *player;
+ unsigned int n, i=0;
+ unsigned int num_entries;
+ vcdinfo_obj_t *obj;
+
+ if (NULL == class) {
+ LOG_MSG("%s", _("was passed a null class parameter"));
+ return false;
+ }
+
+ player = &(my_vcd.player);
+
+ /* If VCD already open, we gotta close and stop it. */
+ if (player->opened) {
+ vcd_close(class);
+ }
+
+ if (NULL == vcd_device) {
+ if (!vcd_get_default_device(class, true)) return false;
+ vcd_device = class->vcd_device;
+ }
+
+ if (!vcdio_open(player, vcd_device)) {
+ /* Error should have been logged in vcdio_open. If not do the below:
+ LOG_ERR(player, "%s: %s.\n", _("unable to open"),
+ class->vcd_device, strerror(errno));
+ */
+ return false;
+ }
+
+ obj = player->vcd;
+ num_entries = player->num_entries;
+ class->mrl_track_offset = -1;
+
+ xine_free_mrls(&(class->num_mrls), class->mrls);
+
+ /* Figure out number of MRLs. Calculation would be real simple if
+ didn't have to possibly remove rejected LIDs from list done in the
+ loop below.
+ */
+ class->num_mrls = player->num_tracks + player->num_entries
+ + player->num_segments + player->num_LIDs;
+
+ if (!player->show_rejected && vcdinfo_get_lot(player->vcd)) {
+ /* Remove rejected LIDs from count. */
+ for (n=0; n<player->num_LIDs; n++) {
+ if ( vcdinf_get_lot_offset(vcdinfo_get_lot(player->vcd), n)
+ == PSD_OFS_DISABLED )
+ class->num_mrls--;
+ }
+ }
+
+ class->mrls = calloc(class->num_mrls, sizeof(xine_mrl_t *));
+ if (NULL == class->mrls) {
+ LOG_ERR("Can't calloc %d MRL entries", class->mrls);
+ class->num_mrls = 0;
+ return false;
+ }
+
+ /* Record MRL's for tracks */
+ for (n=1; n<=player->num_tracks; n++) {
+ memset(&mrl, 0, sizeof (mrl));
+ sprintf(mrl, "%s%s@T%u", MRL_PREFIX, vcd_device, n);
+ vcd_add_mrl_slot(class, mrl, player->track[n-1].size, &i);
+ }
+
+ class->mrl_entry_offset = player->num_tracks;
+ class->mrl_play_offset = class->mrl_entry_offset + num_entries - 1;
+
+ /* Record MRL's for entries */
+ if (num_entries > 0) {
+ for (n=0; n<num_entries; n++) {
+ memset(&mrl, 0, sizeof (mrl));
+ sprintf(mrl, "%s%s@E%u", MRL_PREFIX, vcd_device, n);
+ vcd_add_mrl_slot(class, mrl, player->entry[n].size, &i);
+ }
+ }
+
+ /* Record MRL's for LID entries or selection entries*/
+ class->mrl_segment_offset = class->mrl_play_offset;
+ if (vcdinfo_get_lot(player->vcd)) {
+ for (n=0; n<player->num_LIDs; n++) {
+ uint16_t ofs = vcdinf_get_lot_offset(vcdinfo_get_lot(player->vcd), n);
+ if (ofs != PSD_OFS_DISABLED || player->show_rejected) {
+ memset(&mrl, 0, sizeof (mrl));
+ sprintf(mrl, "%s%s@P%u%s", MRL_PREFIX, vcd_device, n+1,
+ ofs == PSD_OFS_DISABLED ? "*" : "");
+ vcd_add_mrl_slot(class, mrl, 0, &i);
+ class->mrl_segment_offset++;
+ }
+ }
+ }
+
+ /* Record MRL's for segments */
+ {
+ segnum_t num_segments = player->num_segments;
+ for (n=0; n<num_segments; n++) {
+ vcdinfo_video_segment_type_t segtype
+ = vcdinfo_get_video_type(obj, n);
+ char c='S';
+ switch (segtype) {
+ {
+ case VCDINFO_FILES_VIDEO_NTSC_STILL:
+ case VCDINFO_FILES_VIDEO_NTSC_STILL2:
+ case VCDINFO_FILES_VIDEO_NTSC_MOTION:
+ c='s';
+ break;
+ case VCDINFO_FILES_VIDEO_PAL_STILL:
+ case VCDINFO_FILES_VIDEO_PAL_STILL2:
+ case VCDINFO_FILES_VIDEO_PAL_MOTION:
+ c='S';
+ break;
+ default: ;
+ }
+ }
+
+ memset(&mrl, 0, sizeof (mrl));
+ sprintf(mrl, "%s%s@%c%u", MRL_PREFIX, vcd_device, c, n);
+ vcd_add_mrl_slot(class, mrl, player->segment[n].size, &i);
+ }
+ }
+
+ dbg_print(INPUT_DBG_MRL,
+ "offsets are track: %d, entry: %d, play: %d seg: %d\n",
+ class->mrl_track_offset, class->mrl_entry_offset,
+ class->mrl_play_offset, class->mrl_segment_offset);
+
+ return true;
+}
+
+/*!
+ parses a MRL which has the format
+
+ vcdx://[vcd_path][@[EPTS]?number]\*?
+
+ Examples
+ vcdx:// - Play (navigate) default device: /dev/cdrom
+ vcdx://@ - same as above
+ vcdx:///dev/cdrom - probably same as above
+ vcdx:///dev/cdrom2 - Play (navigate) /dev/cdrom2
+ vcdx:///dev/cdrom2@ - same as above
+ vcdx:///dev/cdrom2@T1 - Play Track 1 from /dev/cdrom2
+ vcdx:///dev/cdrom@S1 - Play selection id 1 from /dev/cdrom
+ vcdx://dev/cdrom@E0 - Play Entry id 0 from default device
+ vcdx://@P1 - probably same as above.
+ If there is no playback control, MRL will
+ get converted into vcdx://@E0
+ vcdx://@P1* - probably same as above.
+ vcdx://@S0 - Play segment 0 from default device
+ vcdx://@3 - Play track 3 from default device
+ vcdx:///dev/cdrom2@1 - Play track 1 from /dev/cdrom2
+ vcdx:///tmp/ntsc.bin@ - Play default item from /tmp/ntsc.bin
+ vcdx:///tmp/ntsc.bin/@E0 - Play entry 0 of /tmp/ntsc.bin
+
+parameters:
+ mrl : mrl to parse
+ default_vcd_device: name of device to use when none given
+ auto_type : type of selection (entry, track, LID) when none given
+ used_default : true iff auto_type was used.
+
+ */
+static bool
+vcd_parse_mrl(/*in*/ const char *default_vcd_device, /*in*/ char *mrl,
+ /*out*/ char *device_str, /*out*/ vcdinfo_itemid_t *itemid,
+ /*in */ vcdplayer_autoplay_t auto_type,
+ /*out*/ bool *used_default)
+{
+ char type_str[2];
+ int count;
+ char *p;
+ unsigned int num = 0;
+
+ dbg_print(INPUT_DBG_CALL, "called mrl %s\n", mrl);
+
+ type_str[0] ='\0';
+ itemid->type = (vcdinfo_item_enum_t) auto_type;
+ *used_default = false;
+
+ if ( NULL != mrl && !strncasecmp(mrl, MRL_PREFIX, MRL_PREFIX_LEN) )
+ p = &mrl[MRL_PREFIX_LEN];
+ else {
+ return false;
+ }
+
+ count = sscanf (p, "%[^@]@%1[EePpSsTt]%u",
+ device_str, type_str, &num);
+ itemid->num = num;
+
+ switch (count) {
+ case 1:
+ /* Matched device, but nothing beyond that */
+ if (strlen(device_str)!=0 && device_str[0] != ':') {
+ /* See if we have old-style MRL with no type specifier.
+ If so, we assume "track". */
+ count = sscanf (p, "%u", &num);
+ itemid->num = num;
+ if (1==count) {
+ type_str[0] = 'T';
+ }
+
+ break;
+ }
+
+ case 0:
+ case EOF:
+ {
+ /* No device/file given, so use the default device and try again. */
+ if (NULL == default_vcd_device) return false;
+ strcpy(device_str, default_vcd_device);
+ if (p[0] == '@') p++;
+ count = sscanf (p, "%1[EePpSsTt]%u", type_str, &num);
+ type_str[0] = toupper(type_str[0]);
+ itemid->num = num;
+
+ switch (count) {
+ case EOF:
+ /* Default PBC navigation. */
+ return true;
+ case 0:
+ /* See if we have old-style MRL with no type specifier.
+ If so, we assume "track". */
+ count = sscanf (p, "%u", &num);
+ if (1==count) {
+ type_str[0] = 'T';
+ break;
+ }
+ /* Default PBC navigation. */
+ return true;
+ case 1:
+ /* Type given, but no number. Entries start at 0, other things
+ start at 1 */
+ if (type_str[0] == 'P' || type_str[0] == 'T') itemid->num = 1;
+ }
+ }
+ }
+ /* We have some sort of track/selection/entry number */
+ switch (type_str[0]) {
+ case 'E':
+ itemid->type = VCDINFO_ITEM_TYPE_ENTRY;
+ break;
+ case '\0':
+ /* None specified, use config value. */
+ itemid->type = (vcdinfo_item_enum_t) auto_type;
+ *used_default = true;
+ break;
+ case 'P':
+ itemid->type = VCDINFO_ITEM_TYPE_LID;
+ break;
+ case 'S':
+ itemid->type = VCDINFO_ITEM_TYPE_SEGMENT;
+ break;
+ case 'T':
+ itemid->type = VCDINFO_ITEM_TYPE_TRACK;
+ break;
+ default: ;
+ }
+
+ if ( 0==itemid->num
+ && ( (VCDINFO_ITEM_TYPE_LID == itemid->type)
+ || (VCDINFO_ITEM_TYPE_TRACK == itemid->type) ) )
+ itemid->num = 1;
+
+ return true;
+}
+
+/*!
+ From xine plugin spec:
+
+ return capabilities of input source
+*/
+static uint32_t
+vcd_plugin_get_capabilities (input_plugin_t *this_gen)
+{
+
+ uint32_t ret =
+ INPUT_CAP_AUDIOLANG | INPUT_CAP_BLOCK |
+ INPUT_CAP_CHAPTERS | INPUT_CAP_PREVIEW |
+ (my_vcd.player.in_still ? 0: INPUT_CAP_SEEKABLE) |
+ INPUT_CAP_SPULANG;
+
+ dbg_print((INPUT_DBG_CALL|INPUT_DBG_EXT), "returning %d\n", ret);
+ vcd_handle_events();
+ return ret;
+}
+
+# if FINISHED
+/* If needed, will fill out later... */
+static void
+vcd_read_ahead_cb(void *this_gen, xine_cfg_entry_t *entry)
+{
+ return;
+}
+#endif
+
+static void
+vcd_flush_buffers(void)
+{
+ xine_demux_flush_engine(my_vcd.stream);
+}
+
+/*!
+ From xine plugin spec:
+
+ read nlen bytes, return number of bytes read.
+*/
+static off_t
+vcd_plugin_read (input_plugin_t *this_gen, char *buf, const off_t nlen)
+{
+
+ dbg_print((INPUT_DBG_CALL|INPUT_DBG_EXT),
+ "Called with nlen %u\n", (unsigned int) nlen);
+
+ /* FIXME: Tricking the demux_mpeg_block plugin */
+ buf[0] = 0;
+ buf[1] = 0;
+ buf[2] = 0x01;
+ buf[3] = 0xba;
+ return (off_t) 1;
+}
+
+/*!
+ From xine plugin spec:
+
+ read one block, return newly allocated block (or NULL on failure)
+ for blocked input sources len must be == blocksize the fifo
+ parameter is only used to get access to the buffer_pool_alloc
+ function
+*/
+static buf_element_t *
+vcd_plugin_read_block (input_plugin_t *this_gen, fifo_buffer_t *fifo,
+ const off_t nlen)
+{
+ vcdplayer_input_t *this = &my_vcd.player;
+ buf_element_t *buf;
+ uint8_t data[M2F2_SECTOR_SIZE];
+
+ if (fifo == NULL) {
+ dbg_print(INPUT_DBG_CALL, "NULL fifo");
+ return NULL;
+ }
+
+ dbg_print(INPUT_DBG_CALL, "Called with nlen %u\n", (unsigned int) nlen);
+
+ /* Should we change this to <= instead of !=? */
+ if (nlen != M2F2_SECTOR_SIZE) return NULL;
+
+ switch (vcdplayer_read(this, data, nlen)) {
+ case READ_END:
+ /* End reached. Return NULL to indicated this. */
+ return NULL;
+ case READ_ERROR:
+ /* Some sort of error. */
+ return NULL;
+ case READ_STILL_FRAME:
+ {
+ dbg_print(INPUT_DBG_STILL, "Handled still event\n");
+ buf = fifo->buffer_pool_alloc (fifo);
+ buf->type = BUF_CONTROL_NOP;
+ break;
+ }
+
+ default:
+ case READ_BLOCK:
+ /* Read buffer */
+ buf = fifo->buffer_pool_alloc (fifo);
+ buf->type = BUF_DEMUX_BLOCK;
+ }
+
+ buf->content = buf->mem;
+
+ /* Ideally this should probably be nlen. */
+ memcpy (buf->mem, data, M2F2_SECTOR_SIZE);
+
+ return buf;
+}
+
+/*!
+ From xine plugin spec:
+
+ seek position, return new position
+
+ if seeking failed, -1 is returned
+*/
+static off_t
+vcd_plugin_seek (input_plugin_t *this_gen, off_t offset, int origin)
+{
+ return vcdio_seek (&my_vcd.player, offset, origin);
+}
+
+
+/*!
+ From xine plugin spec:
+ return length of input (-1 => unlimited, e.g. stream)
+
+ length size is bytes.
+*/
+
+static vcdinfo_itemid_t old_play_item = {VCDINFO_ITEM_TYPE_NOTFOUND, 0};
+
+static off_t old_get_length = 0;
+static vcdplayer_slider_length_t old_slider_length;
+
+/* This routine is called a bit. Make reasonably fast. */
+static off_t
+vcd_plugin_get_length (input_plugin_t *this_gen) {
+
+ vcd_input_plugin_t *ip= (vcd_input_plugin_t *) this_gen;
+ vcdplayer_input_t *this = &(ip->player);
+
+ int n = this->play_item.num;
+
+ if (this->play_item.num == old_play_item.num
+ && this->play_item.type == old_play_item.type
+ && this->slider_length == old_slider_length)
+ return old_get_length;
+
+ old_slider_length = this->slider_length;
+ old_play_item = this->play_item;
+
+ switch (this->play_item.type) {
+ case VCDINFO_ITEM_TYPE_ENTRY:
+ switch (this->slider_length) {
+ case VCDPLAYER_SLIDER_LENGTH_AUTO:
+ case VCDPLAYER_SLIDER_LENGTH_ENTRY:
+ n += ip->class->mrl_entry_offset;
+ break;
+ case VCDPLAYER_SLIDER_LENGTH_TRACK:
+ n = vcdinfo_get_track(this->vcd, n) + ip->class->mrl_track_offset;
+ break;
+ default:
+ /* FIXME? */
+ return -1;
+ }
+ break;
+ case VCDINFO_ITEM_TYPE_TRACK:
+ n += ip->class->mrl_track_offset;
+ break;
+ case VCDINFO_ITEM_TYPE_SEGMENT:
+ n += ip->class->mrl_segment_offset;
+ break;
+ case VCDINFO_ITEM_TYPE_LID:
+ /* This is the only situation where the size of the current play item
+ is not static. It depends what the current play-item is.
+ */
+ old_get_length = (this->end_lsn - this->origin_lsn) *
+ M2F2_SECTOR_SIZE;
+ return old_get_length;
+ break;
+ case VCDINFO_ITEM_TYPE_NOTFOUND:
+ case VCDINFO_ITEM_TYPE_SPAREID2:
+ default:
+ /* FIXME? */
+ return -1;
+ }
+
+ if (n >= 0 && n < ip->class->num_mrls) {
+ old_get_length = ip->class->mrls[n]->size;
+ dbg_print(INPUT_DBG_MRL, "item: %u, slot %u, size %ld\n",
+ this->play_item.num,
+ (unsigned int) n, (long int) old_get_length);
+ }
+ return old_get_length;
+}
+
+/*!
+ * From xine plugin spec:
+ * get current position in stream.
+ *
+ */
+static off_t
+vcd_plugin_get_current_pos (input_plugin_t *this_gen){
+ // trace_print("Called\n");
+ return (vcd_plugin_seek (this_gen, 0, SEEK_CUR));
+}
+
+
+/*!
+ * From xine plugin spec:
+ * return block size of input source (if supported, 0 otherwise)
+ */
+static uint32_t
+vcd_plugin_get_blocksize (input_plugin_t *this_gen) {
+ dbg_print((INPUT_DBG_CALL|INPUT_DBG_EXT), "called\n");
+
+ return M2F2_SECTOR_SIZE;
+}
+
+/*!
+ From xine plugin spec:
+ ls function
+ return value: NULL => filename is a file, **char=> filename is a dir
+
+-- This list returned forms the entries of the GUI MRL "browser".
+*/
+
+static xine_mrl_t **
+vcd_class_get_dir (input_class_t *this_gen, const char *filename,
+ int *num_files) {
+
+ char intended_vcd_device[1024]="";
+ vcdinfo_itemid_t itemid;
+
+ vcd_input_class_t *class = (vcd_input_class_t *) this_gen;
+ vcdplayer_input_t *player = &my_vcd.player;
+
+ bool used_default;
+
+ if (filename == NULL) {
+ dbg_print((INPUT_DBG_CALL|INPUT_DBG_EXT),
+ "called with NULL\n");
+ if ( class->mrls != NULL && NULL != class->mrls[0] ) goto have_mrls;
+
+ if ( !vcd_build_mrl_list(class, player->current_vcd_device) ) {
+ goto no_mrls;
+ }
+ } else {
+ char *mrl = strdup(filename);
+ dbg_print((INPUT_DBG_CALL|INPUT_DBG_EXT),
+ "called with %s\n", filename);
+ if (!vcd_get_default_device(class, true)) goto no_mrls;
+ if (!vcd_parse_mrl(class->vcd_device, mrl,
+ intended_vcd_device, &itemid,
+ player->default_autoplay, &used_default)) {
+ free (mrl);
+ goto no_mrls;
+ }
+ free (mrl);
+ }
+
+ have_mrls:
+ *num_files = class->num_mrls;
+ return class->mrls;
+
+ no_mrls:
+ *num_files = 0;
+ return NULL;
+}
+
+#define FREE_AND_NULL(ptr) if (NULL != ptr) free(ptr); ptr = NULL;
+
+static void
+vcd_close(vcd_input_class_t *class)
+{
+ xine_free_mrls(&(class->num_mrls), class->mrls);
+ FREE_AND_NULL(my_vcd.mrl);
+ vcdio_close(&my_vcd.player);
+}
+
+
+/*!
+ * From plugin xine spec:
+ * eject/load the media (if it's possible)
+ *
+ * returns 0 for temporary failures
+ */
+static int
+vcd_class_eject_media (input_class_t *this_gen)
+{
+ int ret;
+ CdIo *cdio=vcdinfo_get_cd_image(my_vcd.player.vcd);
+
+ dbg_print((INPUT_DBG_CALL|INPUT_DBG_EXT), "called\n");
+ if (NULL == cdio) return 0;
+
+ ret = cdio_eject_media(&cdio);
+ if ((ret == 0) || (ret == 2)) {
+ vcdio_close(&my_vcd.player);
+ return 1;
+ } else return 0;
+}
+
+/*!
+ * From spec:
+ * return current MRL
+ */
+static char *
+vcd_plugin_get_mrl (input_plugin_t *this_gen)
+{
+ vcd_input_plugin_t *t = (vcd_input_plugin_t *) this_gen;
+ vcdplayer_input_t *this = &my_vcd.player;
+ unsigned int n;
+ int size; /* need something to feed get_mrl_type_offset */
+ int offset;
+
+ if (vcdplayer_pbc_is_on(this)) {
+ n = this->cur_lid;
+ offset = vcd_get_mrl_type_offset(t, VCDINFO_ITEM_TYPE_LID, &size);
+ } else {
+ n = this->play_item.num;
+ offset = vcd_get_mrl_type_offset(t, this->play_item.type, &size);
+ }
+
+ if (-2 == offset) {
+ /* Bad type. */
+ LOG_ERR("%s %d", _("Invalid current entry type"),
+ this->play_item.type);
+ return strdup("");
+ } else {
+ n += offset;
+ if (n < t->class->num_mrls) {
+ dbg_print(INPUT_DBG_CALL, "Called, returning %s\n",
+ t->class->mrls[n]->mrl);
+ return t->class->mrls[n]->mrl;
+ } else {
+ return strdup("");
+ }
+ }
+}
+
+/*!
+ From xine plugin spec:
+
+ return human readable (verbose = 1 line) description for this plugin
+*/
+static char *
+vcd_class_get_description (input_class_t *this_gen)
+{
+ dbg_print((INPUT_DBG_CALL|INPUT_DBG_EXT), "called\n");
+ return _(strdup("Video CD plugin with PBC and support for: (X)VCD, (X)SVCD, HQVCD, CVD ... "));
+}
+
+/*!
+ From xine plugin spec:
+
+ return short, human readable identifier for this plugin
+ this is used for GUI buttons, The identifier must have max. 4 characters
+ characters (max. 5 including terminating \0)
+*/
+static char *
+vcd_class_get_identifier (input_class_t *this_gen) {
+ dbg_print((INPUT_DBG_CALL|INPUT_DBG_EXT), "called\n");
+ return strdup(SHORT_PLUGIN_NAME);
+}
+
+/*
+ Handle a keyboard/mouse event. Return TRUE if this causes a change
+ in the play item.
+*/
+
+static bool
+vcd_handle_events (void)
+{
+ vcdplayer_input_t *this = &my_vcd.player;
+ xine_event_t *event;
+ int digit_entered=0;
+
+ /* What you add to the last input number entry. It accumulates all of
+ the 10_ADD keypresses */
+ static unsigned int number_addend = 0;
+
+ while ((event = xine_event_get(my_vcd.event_queue))) {
+
+ dbg_print( (INPUT_DBG_CALL|INPUT_DBG_EVENT),
+ "processing %d\n", event->type );
+ digit_entered=0;
+
+ switch(event->type) {
+
+ case XINE_EVENT_INPUT_NUMBER_10_ADD:
+ number_addend += 10;
+ dbg_print(INPUT_DBG_EVENT, "10 added to number. Is now: %d\n",
+ number_addend);
+ break;
+
+ /* The method used below is oblivious to XINE_EVENT_INPUT encodings
+ In particular, it does not assume XINE_EVENT_INPUT_NUMBE_9 =
+ XINE_EVENT_INPUT_NUMBER_0 + 9.
+ */
+ case XINE_EVENT_INPUT_NUMBER_9:
+ digit_entered++;
+ case XINE_EVENT_INPUT_NUMBER_8:
+ digit_entered++;
+ case XINE_EVENT_INPUT_NUMBER_7:
+ digit_entered++;
+ case XINE_EVENT_INPUT_NUMBER_6:
+ digit_entered++;
+ case XINE_EVENT_INPUT_NUMBER_5:
+ digit_entered++;
+ case XINE_EVENT_INPUT_NUMBER_4:
+ digit_entered++;
+ case XINE_EVENT_INPUT_NUMBER_3:
+ digit_entered++;
+ case XINE_EVENT_INPUT_NUMBER_2:
+ digit_entered++;
+ case XINE_EVENT_INPUT_NUMBER_1:
+ digit_entered++;
+ case XINE_EVENT_INPUT_NUMBER_0:
+ {
+ number_addend *= 10;
+ number_addend += digit_entered;
+ dbg_print(INPUT_DBG_EVENT,
+ "digit added number is now: %d\n", number_addend);
+ break;
+ }
+ case XINE_EVENT_INPUT_MENU3:
+ dbg_print(INPUT_DBG_EVENT, "menu3 setting debug: %d\n", number_addend);
+ vcdplayer_debug = number_addend;
+ number_addend = 0;
+ break;
+ case XINE_EVENT_INPUT_MENU1:
+ case XINE_EVENT_INPUT_MENU2:
+ case XINE_EVENT_INPUT_NEXT:
+ case XINE_EVENT_INPUT_PREVIOUS:
+ {
+ int num = number_addend;
+ vcdinfo_itemid_t itemid;
+
+ number_addend = 0;
+
+ /* If no number was given it's really the same as 1, not 0. */
+ if (num == 0) num++;
+
+ dbg_print(INPUT_DBG_EVENT,
+ "RETURN/NEXT/PREV/DEFAULT (%d) iteration count %d\n",
+ event->type, num);
+
+ for ( ; num > 0; num--) {
+ itemid = this->play_item;
+ switch (event->type) {
+ case XINE_EVENT_INPUT_MENU1:
+ if (this->return_entry == VCDINFO_INVALID_ENTRY) {
+ LOG_MSG("%s\n", _("selection has no return entry"));
+ return false;
+ }
+ itemid.num = this->return_entry;
+ dbg_print(INPUT_DBG_PBC, "RETURN to %d\n", itemid.num);
+ /* Don't loop around -- doesn't make sense to loop a return*/
+ num = 0;
+ break;
+ case XINE_EVENT_INPUT_MENU2:
+ if (this->default_entry == VCDINFO_INVALID_ENTRY) {
+ LOG_MSG("%s\n", _("selection has no default entry"));
+ return false;
+ }
+ itemid.num = this->default_entry;
+ dbg_print(INPUT_DBG_PBC, "DEFAULT to %d\n", itemid.num);
+ /* Don't loop around -- doesn't make sense to loop a return*/
+ num = 0;
+ break;
+ case XINE_EVENT_INPUT_NEXT:
+ if (this->next_entry == VCDINFO_INVALID_ENTRY) {
+ LOG_MSG("%s\n", _("selection has no next entry"));
+ return false;
+ }
+ itemid.num = this->next_entry;
+ dbg_print(INPUT_DBG_PBC, "NEXT to %d\n", itemid.num);
+ break;
+ case XINE_EVENT_INPUT_PREVIOUS:
+ if (this->prev_entry == VCDINFO_INVALID_ENTRY) {
+ LOG_MSG("%s\n", _("selection has no previous entry"));
+ return false;
+ }
+ itemid.num = this->prev_entry;
+ dbg_print(INPUT_DBG_PBC, "PREVIOUS to %d\n", itemid.num);
+ break;
+ default:
+ LOG_MSG("%s %d\n", _("Unknown event type: "), event->type);
+ }
+ xine_demux_flush_engine(my_vcd.stream);
+ vcdplayer_play(this, itemid);
+ return true;
+ }
+ break;
+ }
+ case XINE_EVENT_INPUT_SELECT:
+ {
+ /* In the future will have to test to see if we are in a menu
+ selection. But if not... */
+ vcdinfo_itemid_t itemid = this->play_item;
+
+ itemid.num = number_addend;
+ number_addend = 0;
+
+ if (vcdplayer_pbc_is_on(this)) {
+ lid_t next_num=vcdplayer_selection2lid(this, itemid.num);
+ if (VCDINFO_INVALID_LID != next_num) {
+ itemid.num = next_num;
+ xine_demux_flush_engine(my_vcd.stream);
+ vcdplayer_play(this, itemid);
+ return true;
+ }
+ }
+ break;
+ }
+ case XINE_EVENT_INPUT_MOUSE_BUTTON:
+ {
+ vcdplayer_send_button_update(this, 1);
+ }
+ break;
+ case XINE_EVENT_INPUT_BUTTON_FORCE:
+ break;
+ case XINE_EVENT_INPUT_MOUSE_MOVE:
+ {
+ vcdplayer_send_button_update(this, 0);
+ }
+ break;
+ case XINE_EVENT_INPUT_UP:
+ dbg_print(INPUT_DBG_EVENT, "Called with up\n");
+ vcdplayer_send_button_update(this, 0);
+ break;
+ case XINE_EVENT_INPUT_DOWN:
+ dbg_print(INPUT_DBG_EVENT, "Called with down\n");
+ vcdplayer_send_button_update(this, 0);
+ break;
+ case XINE_EVENT_INPUT_LEFT:
+ dbg_print(INPUT_DBG_EVENT, "Called with left\n");
+ vcdplayer_send_button_update(this, 0);
+ break;
+ case XINE_EVENT_INPUT_RIGHT:
+ dbg_print(INPUT_DBG_EVENT, "Called with right\n");
+ vcdplayer_send_button_update(this, 0);
+ break;
+ }
+ }
+ return false;
+}
+
+/*!
+ From xine plugin spec:
+
+ request optional data from input plugin.
+*/
+static int
+vcd_get_optional_data (input_plugin_t *this_gen,
+ void *data, int data_type) {
+
+ dbg_print( (INPUT_DBG_CALL|INPUT_DBG_EXT),
+ "called with %d\n", data_type);
+
+ if (NULL == my_vcd.stream) return INPUT_OPTIONAL_UNSUPPORTED;
+
+ /* Fill this out more fully... */
+ switch(data_type) {
+
+ case INPUT_OPTIONAL_DATA_AUDIOLANG:
+ {
+ int8_t channel;
+ channel = (int8_t) xine_get_audio_channel(my_vcd.stream);
+
+ dbg_print(INPUT_DBG_EXT, "AUDIO CHANNEL = %d\n", channel);
+ if (-1 == channel) {
+ sprintf(data, " %s", "auto");
+ } else {
+ const vcdinfo_obj_t *obj= my_vcd.player.vcd;
+ unsigned int audio_type;
+ unsigned int num_channels;
+ unsigned int track_num = my_vcd.player.cur_track;
+ audio_type = vcdinfo_get_track_audio_type(obj, track_num);
+ num_channels = vcdinfo_audio_type_num_channels(obj, audio_type);
+
+ if (channel >= num_channels) {
+ sprintf(data, "%d ERR", channel);
+ } else {
+ sprintf(data, "%1d", channel);
+ }
+ }
+ return INPUT_OPTIONAL_SUCCESS;
+ }
+
+ case INPUT_OPTIONAL_DATA_SPULANG:
+ {
+ /*uint16_t lang;*/
+ int8_t channel;
+ channel = (int8_t) xine_get_spu_channel(my_vcd.stream);
+ dbg_print(INPUT_DBG_EXT, "SPU CHANNEL = %d\n", channel);
+ if (-1 == channel) {
+ sprintf(data, " %s", "auto");
+ } else {
+ sprintf(data, " %1d", channel);
+ }
+
+ }
+
+ default: ;
+ }
+
+ return INPUT_OPTIONAL_UNSUPPORTED;
+}
+
+/* Array to hold MRLs returned by get_autoplay_list */
+#define MAX_DIR_ENTRIES 250
+
+/*!
+ From xine plugin spec:
+
+ generate autoplay list
+ return value: list of MRLs
+
+-- The list of MRLs returned goes into the playlist.
+ This is called when the SHORT_PLUGIN_NAME button is pressed.
+*/
+
+static char **
+vcd_class_get_autoplay_list (input_class_t *this_gen, int *num_files)
+{
+ vcd_input_class_t *class = (vcd_input_class_t *) this_gen;
+ static char *filelist[MAX_DIR_ENTRIES];
+
+ dbg_print((INPUT_DBG_CALL|INPUT_DBG_EXT), "called\n");
+
+ if ( !vcd_build_mrl_list(class, my_vcd.player.current_vcd_device) ) {
+ *num_files = 0;
+ return NULL;
+ } else {
+ unsigned int i;
+ int size;
+ vcdinfo_item_enum_t itemtype =
+ autoplay2itemtype[my_vcd.player.default_autoplay];
+
+ int offset = vcd_get_mrl_type_offset(&my_vcd, itemtype, &size);
+
+ /* A VCD is not required to have PBC or LID's, default to entry if
+ this is the case...
+ */
+ if (VCDINFO_ITEM_TYPE_LID == itemtype && size==0) {
+ itemtype=VCDINFO_ITEM_TYPE_ENTRY;
+ offset = vcd_get_mrl_type_offset(&my_vcd, itemtype, &size);
+ }
+
+ /* This is because entries start at 0 while other playable units
+ start at 1. Can remove the below when everything has the same
+ origin.
+ */
+ if (VCDINFO_ITEM_TYPE_ENTRY != itemtype) offset++;
+
+ for (i=0; i<size; i++) {
+ if (class->mrls[offset+i] != NULL) {
+ filelist[i] = class->mrls[offset+i]->mrl;
+ dbg_print((INPUT_DBG_MRL), "filelist[%d]: %s\n", i, filelist[i]);
+ } else {
+ filelist[i] = NULL;
+ dbg_print((INPUT_DBG_MRL), "filelist[%d]: NULL\n", i);
+ }
+ }
+ *num_files = i;
+ return filelist;
+ }
+}
+
+/*!
+ Things that need to be done when a stream is closed.
+*/
+static void
+vcd_plugin_dispose(input_plugin_t *this_gen)
+{
+
+ /* Not sure there's much more to do here...
+ In open_plugin we are given a stream which
+ we save...
+ */
+ dbg_print((INPUT_DBG_CALL|INPUT_DBG_EXT), "called\n");
+
+#if 0
+ /*FIXME - this causes termination to in waits and adds sleeps. Why? */
+ free(my_vcd.stream->meta_info[XINE_META_INFO_ALBUM]);
+ free(my_vcd.stream->meta_info[XINE_META_INFO_ARTIST]);
+ free(my_vcd.stream->meta_info[XINE_META_INFO_COMMENT]);
+ free(my_vcd.stream->meta_info[XINE_META_INFO_GENRE]);
+ free(my_vcd.stream->meta_info[XINE_META_INFO_TITLE]);
+#endif
+ my_vcd.stream = NULL;
+
+#if 0
+ vcd_input_plugin_t *t= (vcd_input_plugin_t *) this_gen;
+ vcdplayer_input_t *this = t->v;
+
+ if (NULL==this) return;
+#endif
+}
+
+/* Pointer to vcdimager default log handler. Set by init_input_plugin
+ routine. Perhaps can remove. */
+static vcd_log_handler_t gl_default_vcd_log_handler = NULL;
+static cdio_log_handler_t gl_default_cdio_log_handler = NULL;
+
+/*! This routine is called by libvcd routines on error.
+ Setup is done by init_input_plugin.
+*/
+static void
+vcd_log_handler (vcd_log_level_t level, const char message[])
+{
+ switch (level) {
+ case VCD_LOG_DEBUG:
+ case VCD_LOG_INFO:
+ if (!(vcdplayer_debug & INPUT_DBG_VCDINFO))
+ return;
+ /* Fall through if to warn case */
+ case VCD_LOG_WARN:
+ LOG_MSG("%s", message);
+ break;
+ case VCD_LOG_ERROR:
+ case VCD_LOG_ASSERT:
+ LOG_ERR("%s", message);
+ break;
+ default:
+ LOG_ERR("%s\n%s %d",
+ message,
+ _("The above message had unknown vcdimager log level"),
+ level);
+ }
+
+ /* gl_default_vcd_log_handler (level, message); */
+}
+
+/*! This routine is called by libcdio routines on error.
+ Setup is done by init_input_plugin.
+*/
+static void
+cdio_log_handler (cdio_log_level_t level, const char message[])
+{
+ switch (level) {
+ case CDIO_LOG_DEBUG:
+ case CDIO_LOG_INFO:
+ if (!(vcdplayer_debug & INPUT_DBG_CDIO)) return;
+ /* Fall through if to warn case */
+ default:
+ vcd_log_handler (level, message);
+ }
+}
+
+/*! This routine is when xine is not around.
+ Setup is done by vcd_class_dispose.
+*/
+static void
+uninit_log_handler (vcd_log_level_t level, const char message[])
+{
+ switch (level) {
+ case VCD_LOG_DEBUG:
+ case VCD_LOG_INFO:
+ if (!(vcdplayer_debug & (INPUT_DBG_VCDINFO|INPUT_DBG_CDIO)))
+ return;
+ /* Fall through if to warn case */
+ case VCD_LOG_WARN:
+ fprintf(stderr, "WARN: %s\n", message);
+ break;
+ case VCD_LOG_ERROR:
+ fprintf(stderr, "ERROR: %s\n", message);
+ break;
+ case VCD_LOG_ASSERT:
+ fprintf(stderr, "ASSERT ERROR: %s\n", message);
+ break;
+ default:
+ fprintf(stderr, "UNKNOWN ERROR: %s\n%s %d",
+ message,
+ _("The above message had unknown vcdimager log level"),
+ level);
+ }
+
+ /* gl_default_vcd_log_handler (level, message); */
+}
+
+/*!
+ Things that need to be done the vcdx plugin is closed.
+*/
+static void
+vcd_class_dispose (input_class_t *this_gen) {
+ vcd_input_class_t *class = (vcd_input_class_t *) this_gen;
+
+ gl_default_vcd_log_handler = vcd_log_set_handler (uninit_log_handler);
+ gl_default_cdio_log_handler = cdio_log_set_handler (uninit_log_handler);
+
+ dbg_print((INPUT_DBG_CALL|INPUT_DBG_EXT), "called\n");
+
+ vcd_close(class);
+}
+
+/* Update the xine player title text. */
+static void
+vcd_update_title(void)
+{
+ xine_event_t uevent;
+ xine_ui_data_t data;
+
+ char *title_str;
+
+ title_str = vcdplayer_format_str(&my_vcd.player,
+ my_vcd.v_config.title_format);
+
+ meta_info_assign(XINE_META_INFO_TITLE, my_vcd.stream, title_str);
+ meta_info_assign(XINE_META_INFO_COMMENT, my_vcd.stream,
+ vcdplayer_format_str(&my_vcd.player,
+ my_vcd.v_config.comment_format));
+ stream_info_assign(XINE_STREAM_INFO_VIDEO_HAS_STILL, my_vcd.stream,
+ my_vcd.player.in_still);
+
+ /* Set_str title/chapter display */
+ dbg_print((INPUT_DBG_MRL|INPUT_DBG_CALL),
+ "Changing title to read '%s'\n", title_str);
+ uevent.type = XINE_EVENT_UI_SET_TITLE;
+ uevent.stream = my_vcd.stream;
+ uevent.data = &data;
+ uevent.data_length = sizeof(data);
+
+ memcpy(data.str, title_str, strlen(title_str) + 1);
+ data.str_len = strlen(title_str) + 1;
+
+ xine_event_send(my_vcd.stream, &uevent);
+}
+
+/*
+ No special initialization needed here. All of the initialization
+ is either done in the class or when we have an actual MRL we want
+ to deal with.
+*/
+static int
+vcd_plugin_open (input_plugin_t *this_gen ) {
+ vcd_input_class_t *class = (vcd_input_class_t *) this_gen->input_class;
+
+ gl_default_vcd_log_handler = vcd_log_set_handler (vcd_log_handler);
+ gl_default_cdio_log_handler = cdio_log_set_handler (cdio_log_handler);
+
+ /* actually, this is also done by class initialization. But just in
+ case... */
+ class->ip = &my_vcd;
+
+ return 1;
+}
+
+/*!
+ This basically sets up stream specified by MRL for playing. After this
+ routine is called, xine-lib can read blocks from the thing
+ specified by the MRL, set the position of the thing specified by the
+ MRL, get its size or read its current position...
+
+ See vcdplayer_parses_mrl for the for the format that a valid MRL can take.
+
+ Return values:
+ pointer to input plugin
+ NULL on failure
+
+*/
+static input_plugin_t *
+vcd_class_get_instance (input_class_t *class_gen, xine_stream_t *stream,
+ const char *mrl)
+{
+ vcd_input_class_t *class = (vcd_input_class_t *) class_gen;
+
+ char intended_vcd_device[1024]="";
+ vcdinfo_itemid_t itemid;
+ char *check_mrl=NULL;
+ bool used_default;
+
+ if (mrl == NULL)
+ check_mrl = strdup(MRL_PREFIX);
+ else
+ check_mrl = strdup(mrl);
+
+ dbg_print((INPUT_DBG_CALL|INPUT_DBG_EXT), "called with %s\n", mrl);
+
+ vcd_get_default_device(class, false);
+
+ if (!vcd_parse_mrl(class->vcd_device, check_mrl,
+ intended_vcd_device, &itemid,
+ my_vcd.player.default_autoplay, &used_default)) {
+ dbg_print(INPUT_DBG_MRL, "parsing MRL %s failed", check_mrl);
+ goto free_and_return;
+ }
+
+ free(my_vcd.mrl);
+ my_vcd.mrl = strdup(check_mrl);
+ my_vcd.stream = stream;
+ my_vcd.event_queue = xine_event_new_queue (stream);
+ class->ip = &my_vcd; /* Can probably get rid of this... */
+
+ if (!vcd_build_mrl_list(class, intended_vcd_device)) {
+ goto free_and_return;
+ }
+
+ my_vcd.player.user_data = (void *) class;
+
+ /* Do we set PBC (via LID) on? */
+ my_vcd.player.cur_lid =
+ ( VCDINFO_ITEM_TYPE_LID == itemid.type
+ && my_vcd.player.num_LIDs > itemid.num )
+ ? itemid.num
+ : VCDINFO_INVALID_ENTRY;
+
+ if ( VCDINFO_ITEM_TYPE_LID == itemid.type && used_default) {
+ /* LID was selected automatically but we don't have PBC for this VCD.
+ So silently change LID to track and continue.
+ */
+ itemid.type=VCDINFO_ITEM_TYPE_TRACK;
+ }
+
+ if ( 0==itemid.num
+ && ( (VCDINFO_ITEM_TYPE_LID == itemid.type)
+ || (VCDINFO_ITEM_TYPE_TRACK == itemid.type) ) )
+ itemid.num = 1;
+
+ dbg_print(INPUT_DBG_PBC, "Jumping to NUM >%i<, type >%i<\n",
+ itemid.num, itemid.type);
+
+ vcd_set_meta_info(&my_vcd);
+ vcdplayer_play(&my_vcd.player, itemid);
+
+
+ dbg_print(INPUT_DBG_MRL, "Successfully opened MRL %s.\n",
+ my_vcd.mrl);
+
+ free (check_mrl);
+ return &(my_vcd.input_plugin);
+
+ free_and_return:
+ free (check_mrl);
+ return NULL;
+}
+
+#define VCD_NUM_CALLBACK(fn_name, var) \
+ static void fn_name(void *this_gen, xine_cfg_entry_t *entry) \
+{ \
+\
+ dbg_print(INPUT_DBG_CALL, "Called setting %d\n", entry->num_value); \
+\
+ my_vcd.player.var = entry->num_value; \
+}
+
+#define VCD_ENUM_CALLBACK(fn_name, enum_type, var) \
+ static void fn_name(void *this_gen, xine_cfg_entry_t *entry) \
+{ \
+ dbg_print(INPUT_DBG_CALL, "Called setting %d\n", entry->num_value); \
+\
+ my_vcd.player.var = (enum_type) entry->num_value; \
+}
+
+#define VCD_STR_CALLBACK(fn_name, var) \
+ static void fn_name(void *this_gen, xine_cfg_entry_t *entry) \
+{ \
+\
+ dbg_print(INPUT_DBG_CALL, "Called setting %s\n", entry->str_value); \
+ if ( NULL == entry->str_value ) return; \
+\
+ free(var); \
+ var = strdup(entry->str_value); \
+}
+
+VCD_STR_CALLBACK(vcd_default_dev_changed_cb,
+ my_vcd.class->vcd_device)
+
+VCD_STR_CALLBACK(vcd_title_format_changed_cb,
+ my_vcd.v_config.title_format)
+
+VCD_STR_CALLBACK(vcd_comment_format_changed_cb,
+ my_vcd.v_config.comment_format)
+
+VCD_NUM_CALLBACK(vcd_show_rejected_cb, show_rejected)
+
+VCD_NUM_CALLBACK(vcd_autoadvance_cb, autoadvance)
+
+VCD_ENUM_CALLBACK(vcd_slider_length_cb, vcdplayer_slider_length_t,
+ slider_length)
+
+VCD_ENUM_CALLBACK(vcd_default_autoplay_cb, vcdinfo_item_enum_t,
+ default_autoplay)
+
+static void
+vcd_debug_cb(void *this_gen, xine_cfg_entry_t *entry)
+{
+ dbg_print(INPUT_DBG_CALL, "Called setting %d\n", entry->num_value);
+ vcdplayer_debug = entry->num_value;
+}
+
+static void *
+vcd_init (xine_t *xine, void *data)
+{
+ vcd_input_class_t *class;
+ config_values_t *config;
+
+ dbg_print(INPUT_DBG_CALL, "Called\n");
+
+ class = (vcd_input_class_t *) xine_xmalloc (sizeof (vcd_input_class_t));
+
+ class->xine = xine;
+ class->config = config = xine->config;
+
+ vcd_class = class;
+ class->mrls = NULL;
+
+ if (INPUT_PLUGIN_IFACE_VERSION >= 14) {
+ LOG_MSG("%s%d\n",
+ _("This plugin should be looked over to see if it is compatible with this input plugin API: "), INPUT_PLUGIN_IFACE_VERSION);
+ }
+
+
+#if INPUT_PLUGIN_IFACE_VERSION == 11
+ class->input_class.open_plugin = vcd_class_get_instance;
+#else
+ class->input_class.get_instance = vcd_class_get_instance;
+#endif
+
+ class->input_class.get_identifier = vcd_class_get_identifier;
+ class->input_class.get_description = vcd_class_get_description;
+ class->input_class.get_dir = vcd_class_get_dir;
+ class->input_class.get_autoplay_list = vcd_class_get_autoplay_list;
+ class->input_class.dispose = vcd_class_dispose;
+ class->input_class.eject_media = vcd_class_eject_media;
+
+ memset(&my_vcd, 0, sizeof(my_vcd));
+
+ /*------------------------------------------------------------------
+ Callback functions.
+ ---------------------------------------------------------------------*/
+ my_vcd.player.flush_buffers = &vcd_flush_buffers;
+ my_vcd.player.update_title = &vcd_update_title;
+ my_vcd.player.log_err = (generic_fn) &xine_log_err;
+ my_vcd.player.log_msg = (generic_fn) &xine_log_msg;
+ my_vcd.player.sleep = &xine_usec_sleep;
+ my_vcd.player.force_redisplay = &vcd_force_redisplay;
+ my_vcd.player.handle_events = &vcd_handle_events;
+
+ /*-------------------------------------------------------------
+ Playback control-specific fields
+ --------------------------------------------------------------*/
+
+ my_vcd.player.cur_lid = VCDINFO_INVALID_ENTRY;
+ my_vcd.player.end_lsn = VCDINFO_NULL_LSN;
+
+ my_vcd.player.pdi = -1;
+ my_vcd.player.pxd.psd = NULL;
+
+ /*-----------------------------------
+ Navigation fields
+ ------------------------------------*/
+ my_vcd.player.next_entry = -1;
+ my_vcd.player.prev_entry = -1;
+ my_vcd.player.return_entry = -1;
+ my_vcd.player.default_entry = -1;
+
+ /*--------------------------------------------------------------
+ Configuration variables
+ ---------------------------------------------------------------*/
+
+ {
+ /*Note: these labels have to be listed in the same order as the
+ enumeration vcdplayer_autoplay_t in vcdplayer.h.
+ */
+ static const char *autoplay_modes[] =
+ { "track", "entry", "segment", "playlist", NULL };
+
+ /*Note: these labels have to be listed in the same order as the
+ enumeration vcdplayer_slider_length_t in vcdplayer.h.
+ */
+ static const char *length_reporting_modes[] =
+ { "auto", "track", "entry", NULL };
+
+ my_vcd.player.default_autoplay =
+ config->register_enum(config,
+ "vcdx.autoplay",
+ VCDPLAYER_AUTOPLAY_PBC,
+ (char **) autoplay_modes,
+ _("default type to use on VCD autoplay"),
+_("What play unit to use when none is specified in an MRL, e.g. "
+ "vcdx:// or vcdx:///dev/dvd:"),
+ 0,
+ vcd_default_autoplay_cb, class);
+
+
+ class->vcd_device =
+ config->register_string(config,
+ "vcdx.default_device",
+ "",
+ _("default CD drive used for VCD when none given"),
+_("What to use if no drive specified. If null, we'll scan for CD drives."),
+ 20,
+ vcd_default_dev_changed_cb,
+ (void *) class);
+
+ my_vcd.player.slider_length =
+ config->register_enum(config,
+ "vcdx.length_reporting",
+ VCDPLAYER_SLIDER_LENGTH_AUTO,
+ (char **) length_reporting_modes,
+ _("position slider range"),
+_("The range the stream playback position slider represents when playing."),
+ 0,
+ vcd_slider_length_cb, NULL);
+
+#if READAHEAD_FINISHED
+ my_vcd.player.readahead =
+ config->register_bool(config, "vcdx.use_readahead",
+ (int) false,
+ _("Do we use read-ahead caching?"),
+ _("Class "
+ "may lead to jerky playback on low-end "
+ "machines."),
+ vcd_read_ahead_cb, NULL);
+#endif
+
+ my_vcd.player.autoadvance =
+ config->register_bool(config,
+ "vcdx.autoadvance",
+ (int) true,
+ _("Automatically advance track/entry?"),
+_("If set, we should we automatically advance to the next entry or track. Used only when playback control (PBC) is not on."),
+ 0,
+ vcd_autoadvance_cb,
+ NULL);
+
+ my_vcd.player.show_rejected =
+ config->register_bool(config,
+ "vcdx.show_rejected",
+ (int) false,
+ _("Show 'rejected' LIDs?"),
+"Some playback list IDs (LIDs) are marked not showable, "
+"but you can see then in the MRL list if this is set. Rejected entries "
+"are marked with an asterisk (*) appended in the MRL.",
+ 0,
+ vcd_show_rejected_cb,
+ NULL);
+
+ my_vcd.v_config.title_format =
+ strdup(config->register_string(config,
+ "vcdx.title_format",
+ "%F - %I %N%L%S, disk %c of %C - %v %A",
+ _("format string for display banner"),
+_("Format used in the GUI Title. Similar to the Unix date "
+"command. Format specifiers that start with a percent sign. Specifiers are "
+"%A, %C, %c, %F, %I, %L, %N, %P, %p, %S, %T, %V, %v, and %%."),
+ 0,
+ vcd_title_format_changed_cb,
+ NULL));
+
+ my_vcd.v_config.comment_format =
+ strdup(config->register_string(config,
+ "vcdx.comment_format",
+ "%P - Track %T",
+ _("format string for stream comment field"),
+_("Format used in the GUI Title. Similar to the Unix date "
+"command. Format specifiers that start with a percent sign. Specifiers are "
+"same as the title_format."),
+ 0,
+ vcd_comment_format_changed_cb,
+ NULL));
+
+ vcdplayer_debug =
+ config->register_num(config,
+ "vcdx.debug",
+ 0,
+ _("debug flag mask"),
+ _("This integer when viewed in binary is a debugging mask"),
+ 0,
+ vcd_debug_cb,
+ class);
+ }
+
+ gl_default_vcd_log_handler = vcd_log_set_handler (uninit_log_handler);
+ gl_default_cdio_log_handler = cdio_log_set_handler (uninit_log_handler);
+
+#if INPUT_PLUGIN_IFACE_VERSION != 11
+ my_vcd.input_plugin.open = vcd_plugin_open;
+#endif
+ my_vcd.input_plugin.get_capabilities = vcd_plugin_get_capabilities;
+ my_vcd.input_plugin.read = vcd_plugin_read;
+ my_vcd.input_plugin.read_block = vcd_plugin_read_block;
+ my_vcd.input_plugin.seek = vcd_plugin_seek;
+ my_vcd.input_plugin.get_current_pos = vcd_plugin_get_current_pos;
+ my_vcd.input_plugin.get_length = vcd_plugin_get_length;
+ my_vcd.input_plugin.get_blocksize = vcd_plugin_get_blocksize;
+ my_vcd.input_plugin.get_mrl = vcd_plugin_get_mrl;
+ my_vcd.input_plugin.get_optional_data = vcd_get_optional_data;
+ my_vcd.input_plugin.dispose = vcd_plugin_dispose;
+ my_vcd.input_plugin.input_class = (input_class_t *) class;
+
+ my_vcd.stream = NULL;
+ my_vcd.class = class;
+
+ my_vcd.player.buttonN = 0;
+ my_vcd.player.current_vcd_device = NULL;
+
+ my_vcd.player.opened = false;
+ my_vcd.player.play_item.num = VCDINFO_INVALID_ENTRY;
+ my_vcd.player.play_item.type = VCDINFO_ITEM_TYPE_ENTRY;
+
+ return class;
+}
+
+/*
+ Exported plugin catalog entries.
+
+ All plugins listing only the current API number break when the API
+ number is increased. This is by design.
+
+ Sometimes in the rush to get out a buggy release, the API number is
+ increased without communication let alone a concern for whether it
+ is necessary or how many plugins it might break. And that is
+ precisely when what happened between API release 12 and API
+ 13. Input plugin API numbers 12 and 13 are functionally identical.
+
+ Because of problems like this, we'll just put in a future API
+ release. If the number was increased for a reason that doesn't
+ affect us (such as for nor reason at all), then this plugin will
+ work unmodified that future APIs. If on the other hand there was
+ incompatible change, we are no worse off than if we hadn't entered
+ the next API number since in both cases the plugin is broken.
+ */
+
+const plugin_info_t xine_plugin_info[] = {
+ /* type, API, "name", version, special_info, init_function */
+ { PLUGIN_INPUT, 11, (char *) SHORT_PLUGIN_NAME, XINE_VERSION_CODE,
+ NULL, vcd_init },
+ { PLUGIN_INPUT, 12, (char *) SHORT_PLUGIN_NAME, XINE_VERSION_CODE,
+ NULL, vcd_init },
+ { PLUGIN_INPUT, 13, (char *) SHORT_PLUGIN_NAME, XINE_VERSION_CODE,
+ NULL, vcd_init },
+ { PLUGIN_INPUT, 14, (char *) SHORT_PLUGIN_NAME, XINE_VERSION_CODE,
+ NULL, vcd_init },
+ { PLUGIN_NONE, 0, (char *) "", 0, NULL, NULL }
+};
+
+/*
+ * Local variables:
+ * c-file-style: "gnu"
+ * tab-width: 8
+ * indent-tabs-mode: nil
+ * End:
+ */