diff options
Diffstat (limited to 'src/input/vcd/libvcd')
51 files changed, 16757 insertions, 0 deletions
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, + §or); + + 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); + + + |