diff options
Diffstat (limited to 'contrib/libvcd')
51 files changed, 17981 insertions, 0 deletions
diff --git a/contrib/libvcd/Makefile.am b/contrib/libvcd/Makefile.am new file mode 100644 index 000000000..c922685e3 --- /dev/null +++ b/contrib/libvcd/Makefile.am @@ -0,0 +1,57 @@ +include $(top_srcdir)/misc/Makefile.common + +AM_CFLAGS = $(DEFAULT_OCFLAGS) $(VISIBILITY_FLAG) + +INCLUDES = -I$(top_srcdir)/include -I$(top_builddir)/include -I$(top_srcdir)/lib -I$(top_builddir)/lib + +SUBDIRS = libvcd + +noinst_HEADERS = \ + vcd_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 + +noinst_LTLIBRARIES = libvcd.la libvcdinfo.la +libvcd_la_SOURCES = \ + 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 +libvcd_la_CFLAGS = $(AM_CFLAGS) $(LIBCDIO_CFLAGS) + +libvcdinfo_la_SOURCES = \ + info.c \ + inf.c \ + info_private.c \ + vcd_read.c +libvcdinfo_la_CFLAGS = $(AM_CFLAGS) $(LIBCDIO_CFLAGS) diff --git a/contrib/libvcd/bitvec.h b/contrib/libvcd/bitvec.h new file mode 100644 index 000000000..f45059db0 --- /dev/null +++ b/contrib/libvcd/bitvec.h @@ -0,0 +1,131 @@ +/* + $Id: bitvec.h,v 1.2 2004/04/11 12:20:32 miguelfreitas 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 "vcd_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/contrib/libvcd/bytesex.h b/contrib/libvcd/bytesex.h new file mode 100644 index 000000000..9a212693d --- /dev/null +++ b/contrib/libvcd/bytesex.h @@ -0,0 +1,145 @@ +/* + $Id: bytesex.h,v 1.2 2004/04/11 12:20:32 miguelfreitas 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 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)) + +#endif /* __VCD_BYTESEX_H__ */ + + +/* + * Local variables: + * c-file-style: "gnu" + * tab-width: 8 + * indent-tabs-mode: nil + * End: + */ diff --git a/contrib/libvcd/bytesex_asm.h b/contrib/libvcd/bytesex_asm.h new file mode 100644 index 000000000..3265c592b --- /dev/null +++ b/contrib/libvcd/bytesex_asm.h @@ -0,0 +1,123 @@ +/* + $Id: bytesex_asm.h,v 1.2 2004/04/11 12:20:32 miguelfreitas 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/contrib/libvcd/data_structures.c b/contrib/libvcd/data_structures.c new file mode 100644 index 000000000..1fdca95c9 --- /dev/null +++ b/contrib/libvcd/data_structures.c @@ -0,0 +1,342 @@ +/* + $Id: data_structures.c,v 1.3 2005/01/01 02:43:59 rockyb Exp $ + + Copyright (C) 2000 Herbert Valerio Riedel <hvr@gnu.org> + Copyright (C) 2004 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 <stdlib.h> +#include <string.h> + +#include <cdio/cdio.h> + +/* Public headers */ +#include <libvcd/types.h> + +/* Private headers */ +#include "vcd_assert.h" +#include "data_structures.h" +#include "util.h" + +static const char _rcsid[] = "$Id: data_structures.c,v 1.3 2005/01/01 02:43:59 rockyb Exp $"; + +struct _CdioList +{ + unsigned length; + + CdioListNode *begin; + CdioListNode *end; +}; + +struct _CdioListNode +{ + CdioList *list; + + CdioListNode *next; + + void *data; +}; + +/* impl */ + +static bool +_bubble_sort_iteration (CdioList *list, _cdio_list_cmp_func cmp_func) +{ + CdioListNode **pnode; + bool changed = false; + + for (pnode = &(list->begin); + (*pnode) != NULL && (*pnode)->next != NULL; + pnode = &((*pnode)->next)) + { + CdioListNode *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 (CdioList *list, _cdio_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)); +} + +/* node ops */ + +CdioListNode * +_vcd_list_at (CdioList *list, int idx) +{ + CdioListNode *node = _cdio_list_begin (list); + + if (idx < 0) + return _vcd_list_at (list, _cdio_list_length (list) + idx); + + vcd_assert (idx >= 0); + + while (node && idx) + { + node = _cdio_list_node_next (node); + idx--; + } + + return node; +} + +/* + * n-way tree based on list -- somewhat inefficent + */ + +struct _VcdTree +{ + VcdTreeNode *root; +}; + +struct _VcdTreeNode +{ + void *data; + + CdioListNode *listnode; + VcdTree *tree; + VcdTreeNode *parent; + CdioList *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 (_cdio_list_length (node->children) == 0); + _cdio_list_free (node->children, true); + node->children = NULL; + } + + if (free_data) + free (_vcd_tree_node_set_data (node, NULL)); + + if (node->parent) + _cdio_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 = _cdio_list_new (); + + nnode = _vcd_malloc (sizeof (VcdTreeNode)); + + _cdio_list_append (pnode->children, nnode); + + nnode->data = cdata; + nnode->parent = pnode; + nnode->tree = pnode->tree; + nnode->listnode = _cdio_list_end (pnode->children); + + return nnode; +} + +VcdTreeNode * +_vcd_tree_node_first_child (VcdTreeNode *node) +{ + vcd_assert (node != NULL); + + if (!node->children) + return NULL; + + return _cdio_list_node_data (_cdio_list_begin (node->children)); +} + +VcdTreeNode * +_vcd_tree_node_next_sibling (VcdTreeNode *node) +{ + vcd_assert (node != NULL); + + return _cdio_list_node_data (_cdio_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, (_cdio_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 */ +{ + CdioList *queue; + + vcd_assert (node != NULL); + + queue = _cdio_list_new (); + + _cdio_list_prepend (queue, node); + + while (_cdio_list_length (queue)) + { + CdioListNode *lastnode = _cdio_list_end (queue); + VcdTreeNode *treenode = _cdio_list_node_data (lastnode); + VcdTreeNode *childnode; + + _cdio_list_node_free (lastnode, false); + + trav_func (treenode, user_data); + + _VCD_CHILD_FOREACH (childnode, treenode) + { + _cdio_list_prepend (queue, childnode); + } + } + + _cdio_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/contrib/libvcd/data_structures.h b/contrib/libvcd/data_structures.h new file mode 100644 index 000000000..70fe88045 --- /dev/null +++ b/contrib/libvcd/data_structures.h @@ -0,0 +1,92 @@ +/* + $Id: data_structures.h,v 1.3 2005/01/01 02:43:59 rockyb 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 <cdio/ds.h> +#include <libvcd/types.h> + +/* node ops */ + +CdioListNode *_vcd_list_at (CdioList *list, int idx); + +void _vcd_list_sort (CdioList *list, _cdio_list_cmp_func cmp_func); + +/* 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/contrib/libvcd/dict.h b/contrib/libvcd/dict.h new file mode 100644 index 000000000..229ad5706 --- /dev/null +++ b/contrib/libvcd/dict.h @@ -0,0 +1,165 @@ +/* + $Id: dict.h,v 1.3 2005/01/01 02:43:59 rockyb Exp $ + + Copyright (C) 2000, 2004 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 "vcd_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; + + _cdio_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[]) +{ + CdioListNode *node; + + vcd_assert (obj != NULL); + vcd_assert (key != NULL); + + node = _cdio_list_find (obj->buffer_dict_list, + (_cdio_list_iterfunc) _dict_key_cmp, + (char *) key); + + if (node) + return _cdio_list_node_data (node); + + return NULL; +} + +static const struct _dict_t * +_dict_get_bysector (VcdObj *obj, uint32_t sector) +{ + CdioListNode *node; + + vcd_assert (obj != NULL); + vcd_assert (sector != SECTOR_NIL); + + node = _cdio_list_find (obj->buffer_dict_list, + (_cdio_list_iterfunc) _dict_sector_cmp, + §or); + + if (node) + return _cdio_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) +{ + CdioListNode *node; + + while ((node = _cdio_list_begin (obj->buffer_dict_list))) + { + struct _dict_t *p = _cdio_list_node_data (node); + + free (p->key); + free (p->buf); + + _cdio_list_node_free (node, true); + } +} + +#endif /* __VCD_DICT_H__ */ diff --git a/contrib/libvcd/directory.c b/contrib/libvcd/directory.c new file mode 100644 index 000000000..992d728c6 --- /dev/null +++ b/contrib/libvcd/directory.c @@ -0,0 +1,507 @@ +/* + $Id: directory.c,v 1.4 2006/09/26 19:26:57 dgp85 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/bytesex.h> +#include <cdio/iso9660.h> +#include <libvcd/logging.h> + +/* Private headers */ +#include "vcd_assert.h" +#include "directory.h" +#include "util.h" + +static const char _rcsid[] = "$Id: directory.c,v 1.4 2006/09/26 19:26:57 dgp85 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); + _vcd_strfreev(splitpath); + 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/contrib/libvcd/directory.h b/contrib/libvcd/directory.h new file mode 100644 index 000000000..c4cba9efc --- /dev/null +++ b/contrib/libvcd/directory.h @@ -0,0 +1,64 @@ +/* + $Id: directory.h,v 1.2 2004/04/11 12:20:32 miguelfreitas 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/contrib/libvcd/files.c b/contrib/libvcd/files.c new file mode 100644 index 000000000..38780edf0 --- /dev/null +++ b/contrib/libvcd/files.c @@ -0,0 +1,1020 @@ +/* + $Id: files.c,v 1.4 2006/09/26 21:18:18 dgp85 Exp $ + + Copyright (C) 2000, 2004 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/bytesex.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 "vcd_assert.h" +#include "mpeg_stream.h" +#include "obj.h" +#include "pbc.h" +#include "util.h" + +static const char _rcsid[] = "$Id: files.c,v 1.4 2006/09/26 21:18:18 dgp85 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) +{ + CdioListNode *node = NULL; + int idx = 0; + int track_idx = 0; + EntriesVcd_t entries_vcd; + + vcd_assert (sizeof(EntriesVcd_t) == 2048); + + vcd_assert (_cdio_list_length (obj->mpeg_track_list) <= MAX_ENTRIES); + vcd_assert (_cdio_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; + _CDIO_LIST_FOREACH (node, obj->mpeg_sequence_list) + { + mpeg_sequence_t *track = _cdio_list_node_data (node); + uint32_t lsect = track->relative_start_extent; + CdioListNode *node2; + + lsect += obj->iso_size; + + entries_vcd.entry[idx].n = cdio_to_bcd8(track_idx); + cdio_lba_to_msf(cdio_lsn_to_lba(lsect), + &(entries_vcd.entry[idx].msf)); + + idx++; + lsect += obj->track_front_margin; + + _CDIO_LIST_FOREACH (node2, track->entry_list) + { + entry_t *_entry = _cdio_list_node_data (node2); + /* additional entries */ + + vcd_assert (idx < MAX_ENTRIES); + + entries_vcd.entry[idx].n = cdio_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) +{ + CdioListNode *node; + + if (extended) + vcd_assert (_vcd_obj_has_cap_p (obj, _CAP_PBC_X)); + + vcd_assert (_vcd_pbc_available (obj)); + + _CDIO_LIST_FOREACH (node, obj->pbc_list) + { + pbc_t *_pbc = _cdio_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_t *lot_vcd = NULL; + CdioListNode *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_t)); + memset(lot_vcd, 0xff, sizeof(LotVcd_t)); + + lot_vcd->reserved = 0x0000; + + _CDIO_LIST_FOREACH (node, obj->pbc_list) + { + pbc_t *_pbc = _cdio_list_node_data (node); + unsigned int 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_t)); + free(lot_vcd); +} + +void +set_info_vcd(VcdObj *obj, void *buf) +{ + InfoVcd_t info_vcd; + CdioListNode *node = NULL; + int n = 0; + + vcd_assert (sizeof (InfoVcd_t) == 2048); + vcd_assert (_cdio_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; + _CDIO_LIST_FOREACH (node, obj->mpeg_track_list) + { + mpeg_track_t *track = _cdio_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 (_cdio_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"); + + _CDIO_LIST_FOREACH (node, obj->mpeg_segment_list) + { + mpeg_segment_t *segment = _cdio_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; + CdioListNode *node; + double playtime; + int n; + + strncpy (tracks_svd->file_id, TRACKS_SVD_FILE_ID, + sizeof (TRACKS_SVD_FILE_ID)-1); + tracks_svd->version = TRACKS_SVD_VERSION; + tracks_svd->tracks = _cdio_list_length (obj->mpeg_track_list); + + n = 0; + playtime = 0; + _CDIO_LIST_FOREACH (node, obj->mpeg_track_list) + { + mpeg_track_t *track = _cdio_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 = + cdio_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; + CdioListNode *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)-1); + tracks_svd1->version = TRACKS_SVD_VERSION; + + tracks_svd1->tracks = _cdio_list_length (obj->mpeg_track_list); + + tracks_svd2 = (void *) &(tracks_svd1->playing_time[tracks_svd1->tracks]); + + n = 0; + + _CDIO_LIST_FOREACH (node, obj->mpeg_track_list) + { + mpeg_track_t *track = _cdio_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 = cdio_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; + CdioListNode *node; + + _CDIO_LIST_FOREACH (node, obj->mpeg_track_list) + { + mpeg_track_t *track = _cdio_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, _cdio_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 CdioList * +_make_track_scantable (const VcdObj *obj) +{ + CdioList *all_aps = _cdio_list_new (); + CdioList *scantable = _cdio_list_new (); + unsigned scanpoints = _get_scanpoint_count (obj); + unsigned track_no; + CdioListNode *node; + + track_no = 0; + _CDIO_LIST_FOREACH (node, obj->mpeg_track_list) + { + mpeg_track_t *track = _cdio_list_node_data (node); + CdioListNode *node2; + + _CDIO_LIST_FOREACH (node2, track->info->shdr[0].aps_list) + { + struct aps_data *_data = _vcd_malloc (sizeof (struct aps_data)); + + *_data = *(struct aps_data *)_cdio_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; + + _cdio_list_append (all_aps, _data); + } + track_no++; + } + + { + CdioListNode *aps_node = _cdio_list_begin (all_aps); + CdioListNode *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 = _cdio_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 = _cdio_list_node_next (aps_node); n; + n = _cdio_list_node_next (n)) + { + _data = _cdio_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; + _cdio_list_append (scantable, lsect); + } + + } + + } + + _cdio_list_free (all_aps, true); + + vcd_assert (scanpoints == _cdio_list_length (scantable)); + + return scantable; +} + +void +set_search_dat (VcdObj *obj, void *buf) +{ + CdioList *scantable; + CdioListNode *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)-1); + + 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; + _CDIO_LIST_FOREACH (node, scantable) + { + SearchDat *search_dat2 = buf; + uint32_t sect = *(uint32_t *) _cdio_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)); + + _cdio_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) +{ + CdioListNode *n, *aps_node = _cdio_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 = _cdio_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 = _cdio_list_node_next (aps_node); n; n = _cdio_list_node_next (n)) + { + _data = _cdio_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) * _cdio_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)) * _cdio_list_length (obj->mpeg_track_list); + + /* struct 4 */ + /* vcd_assert (sizeof (ScandataDat4) == 0); + retval += sizeof (ScandataDat4); */ + { + CdioListNode *node; + _CDIO_LIST_FOREACH (node, obj->mpeg_track_list) + { + const mpeg_track_t *track = _cdio_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 = _cdio_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); + + CdioListNode *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)-1); + + 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 = cdio_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; + _CDIO_LIST_FOREACH (node, obj->mpeg_track_list) + { + const mpeg_track_t *track = _cdio_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_t *_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/contrib/libvcd/image.c b/contrib/libvcd/image.c new file mode 100644 index 000000000..d26bb0910 --- /dev/null +++ b/contrib/libvcd/image.c @@ -0,0 +1,108 @@ +/* + $Id: image.c,v 1.3 2005/01/01 02:43:59 rockyb 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 "vcd_assert.h" +#include "image_sink.h" +#include "util.h" + +static const char _rcsid[] = "$Id: image.c,v 1.3 2005/01/01 02:43:59 rockyb 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 CdioList *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, lsn_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/contrib/libvcd/image_bincue.c b/contrib/libvcd/image_bincue.c new file mode 100644 index 000000000..9c447a42a --- /dev/null +++ b/contrib/libvcd/image_bincue.c @@ -0,0 +1,259 @@ +/* + $Id: image_bincue.c,v 1.3 2005/01/01 02:43:59 rockyb Exp $ + + Copyright (C) 2001, 2004 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/bytesex.h> +#include <cdio/iso9660.h> + +/* Public headers */ +#include <libvcd/sector.h> +#include <libvcd/logging.h> + +/* Private headers */ +#include "vcd_assert.h" +#include "image_sink.h" +#include "stream_stdio.h" +#include "util.h" + +static const char _rcsid[] = "$Id: image_bincue.c,v 1.3 2005/01/01 02:43:59 rockyb 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 CdioList *vcd_cue_list) +{ + _img_bincue_snk_t *_obj = user_data; + CdioListNode *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; + _CDIO_LIST_FOREACH (node, (CdioList *) vcd_cue_list) + { + const vcd_cue_t *_cue = _cdio_list_node_data (node); + char *psz_msf; + + 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); + psz_msf = cdio_msf_to_str(&_msf); + + vcd_data_sink_printf (_obj->cue_snk, + " INDEX %2.2d %s\r\n", + index_no, psz_msf); + free(psz_msf); + } + + index_no++; + + cdio_lba_to_msf (_cue->lsn, &_msf); + psz_msf = cdio_msf_to_str(&_msf); + + vcd_data_sink_printf (_obj->cue_snk, + " INDEX %2.2d %s\r\n", + index_no, psz_msf); + free(psz_msf); + 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 <= CDIO_CD_MAX_TRACKS); + + cdio_lba_to_msf (_cue->lsn, &_msf); + psz_msf = cdio_msf_to_str(&_msf); + + vcd_data_sink_printf (_obj->cue_snk, + " INDEX %2.2d %s\r\n", + index_no, psz_msf); + free(psz_msf); + 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, lsn_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/contrib/libvcd/image_cdrdao.c b/contrib/libvcd/image_cdrdao.c new file mode 100644 index 000000000..5e00c14ee --- /dev/null +++ b/contrib/libvcd/image_cdrdao.c @@ -0,0 +1,313 @@ +/* + $Id: image_cdrdao.c,v 1.3 2005/01/01 02:43:59 rockyb 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 <cdio/bytesex.h> +#include <libvcd/sector.h> +#include <libvcd/logging.h> + +/* Private headers */ +#include "vcd_assert.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.3 2005/01/01 02:43:59 rockyb 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; + + CdioList *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 CdioList *vcd_cue_list) +{ + _img_cdrdao_snk_t *_obj = user_data; + VcdDataSink *toc_snk = vcd_data_sink_new_stdio (_obj->toc_fname); + + CdioListNode *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 = _cdio_list_new (); + + index_no = track_no = 0; + _CDIO_LIST_FOREACH (node, (CdioList *) vcd_cue_list) + { + const vcd_cue_t *_cue = _cdio_list_node_data (node); + + /* copy cue list while traversing */ + { + vcd_cue_t *_cue2 = _vcd_malloc (sizeof (vcd_cue_t)); + *_cue2 = *_cue; + _cdio_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 }; + char *psz_msf; + + cdio_lba_to_msf (_cue->lsn - last_track_lsn, &_msf); + psz_msf = cdio_msf_to_str(&_msf); + + vcd_data_sink_printf (toc_snk, " INDEX %s\n", psz_msf); + free(psz_msf); + } + 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, lsn_t lsn) +{ + const char *buf = data; + _img_cdrdao_snk_t *_obj = user_data; + long offset; + + { + CdioListNode *node; + uint32_t _last = 0; + uint32_t _ofs = 0; + bool _lpregap = false; + bool _pregap = false; + + int num = 0, in_track = 0; + _CDIO_LIST_FOREACH (node, _obj->vcd_cue_list) + { + const vcd_cue_t *_cue = _cdio_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/contrib/libvcd/image_nrg.c b/contrib/libvcd/image_nrg.c new file mode 100644 index 000000000..1faeb98a9 --- /dev/null +++ b/contrib/libvcd/image_nrg.c @@ -0,0 +1,352 @@ +/* + $Id: image_nrg.c,v 1.4 2006/09/27 05:41:40 dgp85 Exp $ + + Copyright (C) 2001, 2003, 2004 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/bytesex.h> +#include <cdio/iso9660.h> + +/* Public headers */ +#include <libvcd/sector.h> +#include <libvcd/logging.h> + +/* Private headers */ +#include "vcd_assert.h" +#include "image_sink.h" +#include "stream_stdio.h" +#include "util.h" + +static const char _rcsid[] = "$Id: image_nrg.c,v 1.4 2006/09/27 05:41:40 dgp85 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; + + CdioList *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 CdioList *vcd_cue_list) +{ + _img_nrg_snk_t *_obj = user_data; + CdioListNode *node; + int num; + + _sink_init (_obj); + + _obj->vcd_cue_list = _cdio_list_new (); + + num = 0; + _CDIO_LIST_FOREACH (node, (CdioList *) vcd_cue_list) + { + const vcd_cue_t *_cue = _cdio_list_node_data (node); + vcd_cue_t *_cue2 = _vcd_malloc (sizeof (vcd_cue_t)); + *_cue2 = *_cue; + _cdio_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 (CDIO_CD_MIN_TRACK_NO >= 1 && num <= CDIO_CD_MAX_TRACKS); + + return 0; +} + +static uint32_t +_map (_img_nrg_snk_t *_obj, uint32_t lsn) +{ + CdioListNode *node; + uint32_t result = lsn; + vcd_cue_t *_cue = NULL, *_last = NULL; + + vcd_assert (_obj->cue_end_lsn > lsn); + + _CDIO_LIST_FOREACH (node, _obj->vcd_cue_list) + { + _cue = _cdio_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); + vcd_assert (_last != 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) +{ + CdioListNode *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); + + _CDIO_LIST_FOREACH (node, _obj->vcd_cue_list) + { + vcd_cue_t *_cue = _cdio_list_node_data (node); + + if (_cue->type == VCD_CUE_TRACK_START) + { + vcd_cue_t *_cue2 = + _cdio_list_node_data (_cdio_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, lsn_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/contrib/libvcd/image_sink.h b/contrib/libvcd/image_sink.h new file mode 100644 index 000000000..a71e30199 --- /dev/null +++ b/contrib/libvcd/image_sink.h @@ -0,0 +1,80 @@ +/* + $Id: image_sink.h,v 1.3 2005/01/01 02:43:59 rockyb 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 CdioList *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 CdioList *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/contrib/libvcd/inf.c b/contrib/libvcd/inf.c new file mode 100644 index 000000000..588c8e5f7 --- /dev/null +++ b/contrib/libvcd/inf.c @@ -0,0 +1,531 @@ +/* + $Id: inf.c,v 1.3 2005/01/01 02:43:59 rockyb 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/bytesex.h> +#include <cdio/util.h> + +/* Eventually move above libvcd includes but having vcdinfo including. */ +#include <libvcd/info.h> + +/* Private headers */ +#include "info_private.h" +#include "pbc.h" + +static const char _rcsid[] = "$Id: inf.c,v 1.3 2005/01/01 02:43:59 rockyb 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_t *info) +{ + if (NULL==info) return NULL; + return vcdinfo_strip_trail (info->album_desc, MAX_ALBUM_LEN); +} + +/*! + Get autowait time value for PsdPlayListDescriptor *d. + Time is in seconds unless it is -1 (unlimited). +*/ +int +vcdinf_get_autowait_time (const PsdPlayListDescriptor_t *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_t *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_t *psd) +{ + return 0x7f & psd->loop; +} + +/*! + Return LOT offset +*/ +uint16_t +vcdinf_get_lot_offset (const LotVcd_t *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_t *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_t *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_t *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_t *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_t *d) +{ + if (NULL==d) return 0; + return uint16_from_be (d->ptime); +} + +/*! + Return number of bytes in PSD. +*/ +uint32_t +vcdinf_get_psd_size (const InfoVcd_t *info) +{ + if (NULL==info) return 0; + return uint32_from_be (info->psd_size); +} + +/*! + 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_t *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_t *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_t *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 ? + cdio_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_t *info) +{ + if (NULL==info) return 0; + return(uint16_from_be( info->vol_count)); +} + +/*! + 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_t *info) +{ + if (NULL == info) return 0; + return uint16_from_be(info->vol_id); +} + +/*! + Get wait time value for PsdPlayListDescriptor *d. + Time is in seconds unless it is -1 (unlimited). +*/ +int +vcdinf_get_wait_time (const PsdPlayListDescriptor_t *d) +{ + return vcdinfo_get_wait_time (d->wtime); +} + +/*! + Return true if loop has a jump delay +*/ +bool +vcdinf_has_jump_delay (const PsdSelectionListDescriptor_t *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_t *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_t *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_t *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_t *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_t *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_t *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_t *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_t *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_t *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_t *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_t *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_t *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_t *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_t *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/contrib/libvcd/info.c b/contrib/libvcd/info.c new file mode 100644 index 000000000..b01bd6eee --- /dev/null +++ b/contrib/libvcd/info.c @@ -0,0 +1,2103 @@ +/* + $Id: info.c,v 1.8 2007/03/23 21:47:31 dsalt Exp $ + + Copyright (C) 2002, 2003, 2004 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 "vcd_assert.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/bytesex.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.8 2007/03/23 21:47:31 dsalt 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]; +} + +/* + 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_t *info = vcdinfo_get_infoVcd(obj); + segnum_t num_segments = vcdinfo_get_num_segments(obj); + CdioListNode *entnode; + CdioList *entlist; + int i; + lsn_t last_lsn=0; + + 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 || 0 == num_segments) return; + + entlist = iso9660_fs_readdir(obj->img, "SEGMENT", true); + + i=0; + _CDIO_LIST_FOREACH (entnode, entlist) { + iso9660_stat_t *statbuf = _cdio_list_node_data (entnode); + + if (statbuf->type == _STAT_DIR) continue; + + while(info->spi_contents[i].item_cont) { + obj->seg_sizes[i] = VCDINFO_SEGMENT_SECTOR_SIZE; + i++; + } + + /* Should have an entry in the ISO 9660 filesystem. Get and save + in statbuf.secsize this size. + */ + obj->seg_sizes[i] = statbuf->secsize; + + if (last_lsn >= statbuf->lsn) + vcd_warn ("Segments if ISO 9660 directory out of order lsn %ul >= %ul", + (unsigned int) last_lsn, (unsigned int) statbuf->lsn); + last_lsn = statbuf->lsn; + + i++; + } + + while(i < num_segments && info->spi_contents[i].item_cont) { + obj->seg_sizes[i] = VCDINFO_SEGMENT_SECTOR_SIZE; + i++; + } + + if (i != num_segments) + vcd_warn ("Number of segments found %d is not number of segments %d", + i, num_segments); + + _cdio_list_free (entlist, true); + + +#if 0 + /* 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 filesystem. Get and save + in statbuf.secsize this size. + */ + lsn_t lsn = vcdinfo_get_seg_lsn(obj, i); + iso9660_stat_t *statbuf =iso9660_find_fs_lsn(obj->img, lsn); + if (NULL != statbuf) { + obj->seg_sizes[i] = statbuf->secsize; + free(statbuf); + } else { + vcd_warn ("Trouble finding ISO 9660 size for segment %d.", i); + } + } + } +#endif + +} + +/*! + 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_t *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-1, + 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. +*/ +char * +vcdinfo_get_application_id(vcdinfo_obj_t *p_obj) +{ + if ( NULL == p_obj ) return (NULL); + return iso9660_get_application_id(&p_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_selection_get_lid(const vcdinfo_obj_t *obj, lid_t lid, + unsigned int selection); + + \brief Get offset of a selection for a given lid. + + Return the LID offset associated with a the selection number of the + passed-in LID parameter. + + \return VCDINFO_INVALID_LID is returned if obj on error or obj + is NULL. Otherwise the LID offset is returned. +*/ +lid_t vcdinfo_selection_get_lid(const vcdinfo_obj_t *obj, lid_t lid, + unsigned int selection) +{ + unsigned int offset; + + if (NULL == obj) return VCDINFO_INVALID_LID; + + offset = vcdinfo_selection_get_offset(obj, lid, selection); + switch (offset) { + case VCDINFO_INVALID_OFFSET: + case PSD_OFS_MULTI_DEF: + case PSD_OFS_MULTI_DEF_NO_NUM: + return VCDINFO_INVALID_LID; + default: + { + vcdinfo_offset_t *ofs = vcdinfo_get_offset_t(obj, offset); + return ofs->lid; + } + } +} + +/*! + \fn vcdinfo_selection_get_offset(const vcdinfo_obj_t *obj, lid_t lid, + unsigned int selection); + + \brief Get offset of a selection for a given lid. + + Return the LID offset associated with a the selection number of the + passed-in LID parameter. + + \return VCDINFO_INVALID_OFFSET is returned if error, obj is NULL or + the lid is not some type of selection list. Otherwise the LID offset + is returned. +*/ +uint16_t vcdinfo_selection_get_offset(const vcdinfo_obj_t *obj, lid_t lid, + unsigned int selection) +{ + unsigned int bsn; + + PsdListDescriptor_t pxd; + vcdinfo_lid_get_pxd(obj, &pxd, lid); + if (pxd.descriptor_type != PSD_TYPE_SELECTION_LIST && + pxd.descriptor_type != PSD_TYPE_EXT_SELECTION_LIST) { + vcd_warn( "Requesting selection of LID %i which not a selection list -" + " type is 0x%x", + lid, pxd.descriptor_type ); + return VCDINFO_INVALID_OFFSET; + } + + bsn=vcdinf_get_bsn(pxd.psd); + + if ( (selection - bsn + 1) > 0) { + return vcdinfo_lid_get_offset(obj, lid, selection-bsn+1); + } else { + vcd_warn( "Selection number %u too small. bsn %u", selection, bsn ); + return VCDINFO_INVALID_OFFSET; + } +} + +/** + \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, lid_t lid) +{ + if (NULL != obj) { + + PsdListDescriptor_t 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; +} + +/*! + \brief Get default or multi-default LID. + + Return the LID offset associated with a the "default" entry of the + passed-in LID parameter. Note "default" entries are associated + with PSDs that are (extended) selection lists. If the "default" + offset is a multi-default, we use entry_num to find the proper + "default" LID. Otherwise this routine is exactly like + vcdinfo_get_default_lid with the exception of requiring an + additional "entry_num" parameter. + + \return VCDINFO_INVALID_LID is returned on error, or if the LID + is not a selection list or no "default" entry. Otherwise the LID + offset is returned. +*/ +lid_t +vcdinfo_get_multi_default_lid(const vcdinfo_obj_t *obj, lid_t lid, + lsn_t lsn) +{ + unsigned int offset; + unsigned int entry_num; + + entry_num = vcdinfo_lsn_get_entry(obj, lsn); + offset = vcdinfo_get_multi_default_offset(obj, lid, entry_num); + + switch (offset) { + case VCDINFO_INVALID_OFFSET: + case PSD_OFS_MULTI_DEF: + case PSD_OFS_MULTI_DEF_NO_NUM: + return VCDINFO_INVALID_LID; + default: + { + vcdinfo_offset_t *ofs = vcdinfo_get_offset_t(obj, offset); + return ofs->lid; + } + } +} + +/*! + \brief Get default or multi-default LID offset. + + Return the LID offset associated with a the "default" entry of the + passed-in LID parameter. Note "default" entries are associated + with PSDs that are (extended) selection lists. If the "default" + offset is a multi-default, we use entry_num to find the proper + "default" offset. Otherwise this routine is exactly like + vcdinfo_get_default_offset with the exception of requiring an + additional "entry_num" parameter. + + \return VCDINFO_INVALID_OFFSET is returned on error, or if the LID + is not a selection list or no "default" entry. Otherwise the LID + offset is returned. +*/ +uint16_t +vcdinfo_get_multi_default_offset(const vcdinfo_obj_t *obj, lid_t lid, + unsigned int entry_num) +{ + uint16_t offset=vcdinfo_get_default_offset(obj, lid); + + switch (offset) { + case PSD_OFS_MULTI_DEF: + case PSD_OFS_MULTI_DEF_NO_NUM: + { + /* Have some work todo... Figure the selection number. */ + PsdListDescriptor_t pxd; + + vcdinfo_lid_get_pxd(obj, &pxd, lid); + + switch (pxd.descriptor_type) { + + case PSD_TYPE_SELECTION_LIST: + case PSD_TYPE_EXT_SELECTION_LIST: { + vcdinfo_itemid_t selection_itemid; + uint16_t selection_itemid_num; + unsigned int start_entry_num; + + if (pxd.psd == NULL) return VCDINFO_INVALID_OFFSET; + selection_itemid_num = vcdinf_psd_get_itemid(pxd.psd); + vcdinfo_classify_itemid(selection_itemid_num, &selection_itemid); + if (selection_itemid.type != VCDINFO_ITEM_TYPE_TRACK) { + return VCDINFO_INVALID_OFFSET; + } + + start_entry_num = vcdinfo_track_get_entry(obj, selection_itemid.num); + return vcdinfo_selection_get_offset(obj, lid, + entry_num-start_entry_num); + } + default: ; + } + } + default: + 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_t *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 */ + statbuf = iso9660_find_fs_lsn(obj->img, lsn); + + if (NULL != statbuf) { + next_lsn = lsn + statbuf->secsize; + free(statbuf); + } 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_t *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; + } +} + +EntriesVcd_t * +vcdinfo_get_entriesVcd (vcdinfo_obj_t *obj) +{ + if (NULL == obj) return NULL; + return &obj->entries; +} + +/*! + 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); +} + +InfoVcd_t * +vcdinfo_get_infoVcd (vcdinfo_obj_t *obj) +{ + if (NULL == obj) return NULL; + return &obj->info; +} + +/*! Return the entry number closest and before the given LSN. + */ +unsigned int +vcdinfo_lsn_get_entry(const vcdinfo_obj_t *obj, lsn_t lsn) +{ + + /* Do a binary search to find the entry. */ + unsigned int i = 0; + unsigned int j = vcdinfo_get_num_entries(obj); + unsigned int mid; + unsigned int mid_lsn; + do { + mid = (i+j)/2; + mid_lsn = vcdinfo_get_entry_lsn(obj, mid); + if ( lsn <= mid_lsn ) j = mid-1; + if ( lsn >= mid_lsn ) i = mid+1; + } while (i <= j); + + /* We want the entry closest but before. */ + return (lsn == mid_lsn) ? mid : mid-1; +} + + +void * +vcdinfo_get_pvd (vcdinfo_obj_t *obj) +{ + if (NULL == obj) return NULL; + return &obj->pvd; +} + +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_tracksSVD (vcdinfo_obj_t *obj) +{ + if (NULL == obj) return NULL; + return obj->tracks_buf; +} + +/*! + 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_t 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_t * +vcdinfo_get_lot(const vcdinfo_obj_t *obj) +{ + if (NULL == obj) return NULL; + return obj->lot; +} + +/*! + Get the extended LOT pointer. +*/ +LotVcd_t * +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_t *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_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_t 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) +{ + CdioListNode *node; + CdioList *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: ; + } + + _CDIO_LIST_FOREACH (node, offset_list) + { + vcdinfo_offset_t *ofs = _cdio_list_node_data (node); + if (offset == ofs->offset) + return ofs; + } + return NULL; +} + +/*! + Get the VCD info list. +*/ +CdioList * +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. +*/ +CdioList * +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 iso9660_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. +*/ +char * +vcdinfo_get_publisher_id(const vcdinfo_obj_t *obj) +{ + if ( NULL == obj ) return (NULL); + return iso9660_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_t *pxd, + uint16_t lid, bool ext) +{ + CdioListNode *node; + unsigned mult = obj->info.offset_mult; + const uint8_t *psd = ext ? obj->psd_x : obj->psd; + CdioList *offset_list = ext ? obj->offset_x_list : obj->offset_list; + + if (offset_list == NULL) return false; + + _CDIO_LIST_FOREACH (node, offset_list) + { + vcdinfo_offset_t *ofs = _cdio_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_t *) (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_t *) (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_t *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_t 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 x-y resolution for a given segment. + Note first i_seg is 0. +*/ +void +vcdinfo_get_seg_resolution(const vcdinfo_obj_t *p_vcdinfo, segnum_t i_seg, + /*out*/ uint16_t *max_x, /*out*/ uint16_t *max_y) +{ + vcdinfo_video_segment_type_t segtype + = vcdinfo_get_video_type(p_vcdinfo, i_seg); + segnum_t i_segs = vcdinfo_get_num_segments(p_vcdinfo); + + if (i_seg >= i_segs) return; + + switch (segtype) { + case VCDINFO_FILES_VIDEO_NTSC_STILL: + *max_x = 704; + *max_y = 480; + break; + case VCDINFO_FILES_VIDEO_NTSC_STILL2: + *max_x = 352; + *max_y = 240; + break; + case VCDINFO_FILES_VIDEO_PAL_STILL: + *max_x = 704; + *max_y = 576; + break; + case VCDINFO_FILES_VIDEO_PAL_STILL2: + *max_x = 352; + *max_y = 288; + break; + default: + /* */ + switch (vcdinfo_get_format_version(p_vcdinfo)) { + case VCD_TYPE_VCD: + *max_x = 352; + *max_y = 240; + break; + case VCD_TYPE_VCD11: + case VCD_TYPE_VCD2: + *max_x = 352; + switch(segtype) { + case VCDINFO_FILES_VIDEO_NTSC_MOTION: + *max_y = 240; + break; + case VCDINFO_FILES_VIDEO_PAL_MOTION: + *max_y = 288; + default: + *max_y = 289; + } + break; + default: ; + } + } +} + + + +/*! + 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(iso9660_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_t *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 = cdio_from_bcd8(msf.m); + *sec = cdio_from_bcd8(msf.s); + *frame = cdio_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 && (statbuf = iso9660_find_fs_lsn(obj->img, lsn))) { + unsigned int secsize = statbuf->secsize; + free(statbuf); + return 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)) { + 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_t *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(iso9660_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, ISO_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)", + (unsigned int) 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-default"; + 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; +} + +/*! Return the entry number for the given track. */ +unsigned int +vcdinfo_track_get_entry(const vcdinfo_obj_t *obj, track_t i_track) +{ + /* FIXME: Add structure to directly map track to first entry number. + Until then... + */ + lsn_t lsn= vcdinfo_get_track_lsn(obj, i_track); + return vcdinfo_lsn_get_entry(obj, lsn); +} + +/*! + Calls recursive routine to populate obj->offset_list or obj->offset_x_list + by going through LOT. + + Returns false if there was some error. +*/ +bool +vcdinfo_visit_lot (vcdinfo_obj_t *obj, bool extended) +{ + struct _vcdinf_pbc_ctx pbc_ctx; + bool ret; + + 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; + + ret = vcdinf_visit_lot(&pbc_ctx); + if (NULL != obj->offset_x_list) + _cdio_list_free(obj->offset_x_list, true); + obj->offset_x_list = pbc_ctx.offset_x_list; + if (NULL != obj->offset_list) + _cdio_list_free(obj->offset_list, true); + obj->offset_list = pbc_ctx.offset_list; + return ret; +} + +/*! + 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[1025]; + 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; + vcdinfo_obj_t *obj = _vcd_malloc(sizeof(vcdinfo_obj_t)); + iso9660_stat_t *statbuf; + + + /* 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_ret(NULL, + (CDIO_FS_ANAL_SVCD|CDIO_FS_ANAL_CVD|CDIO_FS_ANAL_VIDEOCD + |CDIO_FS_UNKNOWN), + true, &source_type); + 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); + if (NULL == *source_name) return VCDINFO_OPEN_ERROR; + } + + memset (obj, 0, sizeof (vcdinfo_obj_t)); + obj->img = img; /* Note we do this after the above wipeout! */ + + if (!iso9660_fs_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)) || + vcdinfo_get_format_version (obj) == VCD_TYPE_INVALID || + !read_entries(obj->img, &(obj->entries))) { + free (obj); /* match 0.7.23's behaviour */ + 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) { + statbuf = iso9660_fs_stat (obj->img, "MPEGAV"); + + if (NULL != statbuf) { + vcd_warn ("non compliant /MPEGAV folder detected!"); + free(statbuf); + } + + + statbuf = iso9660_fs_stat (obj->img, "SVCD/TRACKS.SVD;1"); + if (NULL != statbuf) { + lsn_t lsn = statbuf->lsn; + if (statbuf->size != ISO_BLOCKSIZE) + vcd_warn ("TRACKS.SVD filesize != %d!", ISO_BLOCKSIZE); + + obj->tracks_buf = _vcd_malloc (ISO_BLOCKSIZE); + + free(statbuf); + if (cdio_read_mode2_sector (obj->img, obj->tracks_buf, lsn, false)) + return VCDINFO_OPEN_ERROR; + } + } + + _init_segments (obj); + + switch (obj->vcd_type) { + case VCD_TYPE_VCD2: { + /* FIXME: Can reduce CD reads by using + iso9660_fs_readdir(img, "EXT", true) and then scanning for + the files listed below. + */ + statbuf = iso9660_fs_stat (img, "EXT/PSD_X.VCD;1"); + if (NULL != statbuf) { + lsn_t lsn = statbuf->lsn; + uint32_t secsize = statbuf->secsize; + + obj->psd_x = _vcd_malloc (ISO_BLOCKSIZE * secsize); + obj->psd_x_size = statbuf->size; + + vcd_debug ("found /EXT/PSD_X.VCD at sector %lu", + (long unsigned int) lsn); + + free(statbuf); + if (cdio_read_mode2_sectors (img, obj->psd_x, lsn, false, secsize)) + return VCDINFO_OPEN_ERROR; + } + + statbuf = iso9660_fs_stat (img, "EXT/LOT_X.VCD;1"); + if (NULL != statbuf) { + lsn_t lsn = statbuf->lsn; + uint32_t secsize = statbuf->secsize; + obj->lot_x = _vcd_malloc (ISO_BLOCKSIZE * secsize); + + vcd_debug ("found /EXT/LOT_X.VCD at sector %lu", + (unsigned long int) lsn); + + if (statbuf->size != LOT_VCD_SIZE * ISO_BLOCKSIZE) + vcd_warn ("LOT_X.VCD size != 65535"); + + free(statbuf); + if (cdio_read_mode2_sectors (img, obj->lot_x, lsn, false, secsize)) + return VCDINFO_OPEN_ERROR; + + } + break; + } + case VCD_TYPE_SVCD: + case VCD_TYPE_HQVCD: { + /* FIXME: Can reduce CD reads by using + iso9660_fs_readdir(img, "SVCD", true) and then scanning for + the files listed below. + */ + statbuf = iso9660_fs_stat (img, "MPEGAV"); + if (NULL != statbuf) { + vcd_warn ("non compliant /MPEGAV folder detected!"); + free(statbuf); + } + + statbuf = iso9660_fs_stat (img, "SVCD/TRACKS.SVD;1"); + if (NULL == statbuf) + vcd_warn ("mandatory /SVCD/TRACKS.SVD not found!"); + else { + vcd_debug ("found TRACKS.SVD signature at sector %lu", + (unsigned long int) statbuf->lsn); + free(statbuf); + } + + statbuf = iso9660_fs_stat (img, "SVCD/SEARCH.DAT;1"); + if (NULL == statbuf) + vcd_warn ("mandatory /SVCD/SEARCH.DAT not found!"); + else { + lsn_t lsn = statbuf->lsn; + uint32_t secsize = statbuf->secsize; + uint32_t stat_size = statbuf->size; + uint32_t size; + + vcd_debug ("found SEARCH.DAT at sector %lu", (unsigned long int) lsn); + + obj->search_buf = _vcd_malloc (ISO_BLOCKSIZE * secsize); + + if (cdio_read_mode2_sectors (img, obj->search_buf, lsn, false, secsize)) + return VCDINFO_OPEN_ERROR; + + size = (3 * uint16_from_be (((SearchDat *)obj->search_buf)->scan_points)) + + sizeof (SearchDat); + + free(statbuf); + if (size > stat_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, lsn, false, + secsize)) + return VCDINFO_OPEN_ERROR; + } + } + break; + } + default: + ; + } + + statbuf = iso9660_fs_stat (img, "EXT/SCANDATA.DAT;1"); + if (statbuf != NULL) { + lsn_t lsn = statbuf->lsn; + uint32_t secsize = statbuf->secsize; + + vcd_debug ("found /EXT/SCANDATA.DAT at sector %u", (unsigned int) lsn); + + obj->scandata_buf = _vcd_malloc (ISO_BLOCKSIZE * secsize); + + free(statbuf); + if (cdio_read_mode2_sectors (img, obj->scandata_buf, lsn, false, 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) + _cdio_list_free(obj->offset_list, true); + if (obj->offset_x_list != NULL) + _cdio_list_free(obj->offset_x_list, true); + free(obj->seg_sizes); + free(obj->lot); + free(obj->lot_x); + if (obj->psd_x) free(obj->psd_x); + if (obj->psd) free(obj->psd); + if (obj->scandata_buf) free(obj->scandata_buf); + 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/contrib/libvcd/info_private.c b/contrib/libvcd/info_private.c new file mode 100644 index 000000000..9f89a3dcb --- /dev/null +++ b/contrib/libvcd/info_private.c @@ -0,0 +1,328 @@ +/* + $Id: info_private.c,v 1.3 2005/01/01 02:43:59 rockyb 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/bytesex.h> +#include <cdio/util.h> + +#include <libvcd/types.h> +#include <libvcd/files.h> + +#include <libvcd/info.h> + +/* Private headers */ +#include "vcd_assert.h" +#include "data_structures.h" +#include "info_private.h" +#include "pbc.h" + +static const char _rcsid[] = "$Id: info_private.c,v 1.3 2005/01/01 02:43:59 rockyb 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; + { + CdioListNode *node; + CdioList *unused_lids = _cdio_list_new(); + CdioListNode *next_unused_node = _cdio_list_begin(unused_lids); + + unsigned int last_lid=0; + CdioList *offset_list = extended ? obj->offset_x_list : obj->offset_list; + + lid_t max_seen_lid=0; + + _CDIO_LIST_FOREACH (node, offset_list) + { + vcdinfo_offset_t *ofs = _cdio_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. + */ + CdioListNode *node=_cdio_list_node_next(next_unused_node); + if (node != NULL) { + lid_t *next_unused_lid=_cdio_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; + _cdio_list_append(unused_lids, lid); + } + if (last_lid > max_seen_lid) max_seen_lid=last_lid; + } + } + _cdio_list_free(unused_lids, true); + } +} + +/*! + Calls recursive routine to populate obj->offset_list or obj->offset_x_list + by going through LOT. + + Returns false if there was some error. +*/ +bool +vcdinf_visit_lot (struct _vcdinf_pbc_ctx *obj) +{ + const LotVcd_t *lot = obj->extended ? obj->lot_x : obj->lot; + unsigned int n, tmp; + bool ret=true; + + if (obj->extended) { + if (!obj->psd_x_size) return false; + } else if (!obj->psd_size) return false; + + for (n = 0; n < LOT_VCD_OFFSETS; n++) + if ((tmp = vcdinf_get_lot_offset(lot, n)) != PSD_OFS_DISABLED) + ret &= vcdinf_visit_pbc (obj, n + 1, tmp, true); + + _vcd_list_sort (obj->extended ? obj->offset_x_list : obj->offset_list, + (_cdio_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); + return ret; +} + +/*! + Recursive routine to populate obj->offset_list or obj->offset_x_list + by reading playback control entries referred to via lid. + + Returns false if there was some error. +*/ +bool +vcdinf_visit_pbc (struct _vcdinf_pbc_ctx *obj, lid_t lid, unsigned int offset, + bool in_lot) +{ + CdioListNode *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; + CdioList *offset_list; + bool ret=true; + + 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 true; + + default: + break; + } + + if (_rofs >= psd_size) + { + if (obj->extended) + vcd_warn ("psd offset out of range in extended PSD (%d >= %d)", + _rofs, psd_size); + else + vcd_warn ("psd offset out of range (%d >= %d)", _rofs, psd_size); + return false; + } + + if (!obj->offset_list) + obj->offset_list = _cdio_list_new (); + + if (!obj->offset_x_list) + obj->offset_x_list = _cdio_list_new (); + + if (obj->extended) { + offset_list = obj->offset_x_list; + } else + offset_list = obj->offset_list; + + _CDIO_LIST_FOREACH (node, offset_list) + { + ofs = _cdio_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 true; /* 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: + _cdio_list_append (offset_list, ofs); + { + const PsdPlayListDescriptor_t *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); + + ret &= vcdinf_visit_pbc (obj, 0, vcdinf_pld_get_prev_offset(d), false); + ret &= vcdinf_visit_pbc (obj, 0, vcdinf_pld_get_next_offset(d), false); + ret &= vcdinf_visit_pbc (obj, 0, vcdinf_pld_get_return_offset(d), + false); + } + break; + + case PSD_TYPE_EXT_SELECTION_LIST: + case PSD_TYPE_SELECTION_LIST: + _cdio_list_append (offset_list, ofs); + { + const PsdSelectionListDescriptor_t *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); + + ret &= vcdinf_visit_pbc (obj, 0, vcdinf_psd_get_prev_offset(d), false); + ret &= vcdinf_visit_pbc (obj, 0, vcdinf_psd_get_next_offset(d), false); + ret &= vcdinf_visit_pbc (obj, 0, vcdinf_psd_get_return_offset(d), + false); + ret &= vcdinf_visit_pbc (obj, 0, vcdinf_psd_get_default_offset(d), + false); + ret &= vcdinf_visit_pbc (obj, 0, uint16_from_be (d->timeout_ofs), + false); + + for (idx = 0; idx < vcdinf_get_num_selections(d); idx++) + ret &= vcdinf_visit_pbc (obj, 0, vcdinf_psd_get_offset(d, idx), + false); + } + break; + + case PSD_TYPE_END_LIST: + _cdio_list_append (offset_list, ofs); + break; + + default: + vcd_warn ("corrupt PSD???????"); + free (ofs); + return false; + break; + } + return ret; +} + +/*! 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_t *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_t *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/contrib/libvcd/info_private.h b/contrib/libvcd/info_private.h new file mode 100644 index 000000000..d17b6680b --- /dev/null +++ b/contrib/libvcd/info_private.h @@ -0,0 +1,118 @@ +/*! + \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/ds.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_t info; + EntriesVcd_t entries; + + CdioList *offset_list; + CdioList *offset_x_list; + uint32_t *seg_sizes; + lsn_t first_segment_lsn; + + LotVcd_t *lot; + LotVcd_t *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_t *entries, + unsigned int entry_num); + + struct _vcdinf_pbc_ctx { + unsigned int psd_size; + lid_t maximum_lid; + unsigned offset_mult; + CdioList *offset_x_list; + CdioList *offset_list; + + LotVcd_t *lot; + LotVcd_t *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. + + Returns false if there was some error. + */ + bool 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. + + Returns false if there was some error. + */ + bool 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/contrib/libvcd/libvcd/Makefile.am b/contrib/libvcd/libvcd/Makefile.am new file mode 100644 index 000000000..ea29859f6 --- /dev/null +++ b/contrib/libvcd/libvcd/Makefile.am @@ -0,0 +1,3 @@ +include $(top_srcdir)/misc/Makefile.common + +noinst_HEADERS = files.h inf.h logging.h types.h files_private.h info.h sector.h version.h diff --git a/contrib/libvcd/libvcd/files.h b/contrib/libvcd/libvcd/files.h new file mode 100644 index 000000000..91eeafad8 --- /dev/null +++ b/contrib/libvcd/libvcd/files.h @@ -0,0 +1,101 @@ +/* + $Id: files.h,v 1.2 2004/04/11 12:20:32 miguelfreitas 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/contrib/libvcd/libvcd/files_private.h b/contrib/libvcd/libvcd/files_private.h new file mode 100644 index 000000000..aabb6ab0c --- /dev/null +++ b/contrib/libvcd/libvcd/files_private.h @@ -0,0 +1,562 @@ +/* + $Id: files_private.h,v 1.3 2006/09/26 21:16:59 dgp85 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[sizeof(TRACKS_SVD_FILE_ID)-1]; /* == "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[sizeof(TRACKS_SVD_FILE_ID)-1]; /* == "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[sizeof(SEARCH_FILE_ID)-1]; /* = "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[sizeof(SPICONTX_FILE_ID)-1]; /* = "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[sizeof(SCANDATA_FILE_ID)-1]; /* = "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[sizeof(SCANDATA_FILE_ID)-1]; /* = "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/contrib/libvcd/libvcd/inf.h b/contrib/libvcd/libvcd/inf.h new file mode 100644 index 000000000..fcd9098e2 --- /dev/null +++ b/contrib/libvcd/libvcd/inf.h @@ -0,0 +1,260 @@ +/*! + \file inf.h + + Copyright (C) 2002, 2003, 2004 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 <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_t *info); + + /*! + Get autowait time value for PsdPlayListDescriptor *d. + Time is in seconds unless it is -1 (unlimited). + */ + int vcdinf_get_autowait_time (const PsdPlayListDescriptor_t *d); + + /*! + Return the base selection number. VCD_INVALID_BSN is returned if there + is an error. + */ + unsigned int vcdinf_get_bsn(const PsdSelectionListDescriptor_t *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_t *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_t *psd); + + /*! + Return LOT offset + */ + uint16_t vcdinf_get_lot_offset (const LotVcd_t *lot, unsigned int n); + + /*! + Return number of bytes in PSD. + */ + uint32_t vcdinf_get_psd_size (const InfoVcd_t *info); + + /*! + Return the number of segments in the VCD. + */ + unsigned int vcdinf_get_num_entries(const EntriesVcd_t *entries); + + /*! + Return number of LIDs. + */ + lid_t vcdinf_get_num_LIDs (const InfoVcd_t *info); + + /*! + Return the number of segments in the VCD. + */ + segnum_t vcdinf_get_num_segments(const InfoVcd_t *info); + + /*! + Return the number of menu selections for selection-list descriptor d. + */ + unsigned int vcdinf_get_num_selections(const PsdSelectionListDescriptor_t *d); + + /*! + Get play-time value for PsdPlayListDescriptor *d. + Time is in 1/15-second units. + */ + uint16_t vcdinf_get_play_time (const PsdPlayListDescriptor_t *d); + + /*! + 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_t *d); + + /*! + Get timeout wait value for PsdPlayListDescriptor *d. + Time is in seconds unless it is -1 (unlimited). + */ + int vcdinf_get_timeout_time (const PsdSelectionListDescriptor_t *d); + + /*! + Return the track number for entry n in obj. The first track starts + at 1. + */ + track_t vcdinf_get_track(const EntriesVcd_t *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_t *info); + + /*! + Return the VCD volume count - the number of CD's in the collection. + */ + unsigned int vcdinf_get_volume_count(const InfoVcd_t *info); + + /*! + Get wait time value for PsdPlayListDescriptor *d. + Time is in seconds unless it is -1 (unlimited). + */ + int vcdinf_get_wait_time (const PsdPlayListDescriptor_t *d); + + /*! + Return true if loop has a jump delay + */ + bool vcdinf_has_jump_delay (const PsdSelectionListDescriptor_t *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_t *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_t *pld); + + /*! + Return the playlist item i in d. + */ + uint16_t vcdinf_pld_get_play_item(const PsdPlayListDescriptor_t *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_t *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_t *pld); + + /*! + Return number of items in LIDs. Return 0 if error or not found. + */ + int vcdinf_pld_get_noi (const PsdPlayListDescriptor_t *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_t *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_t *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_t *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_t *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_t *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_t *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_t *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_t *psd); + +#ifdef __cplusplus +} +#endif /* __cplusplus */ + +#endif /*_VCD_INF_H*/ diff --git a/contrib/libvcd/libvcd/info.h b/contrib/libvcd/libvcd/info.h new file mode 100644 index 000000000..91b25c956 --- /dev/null +++ b/contrib/libvcd/libvcd/info.h @@ -0,0 +1,860 @@ +/*! + \file info.h + + Copyright (C) 2002, 2003, 2004 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/version.h> +#include <libvcd/types.h> +#include <libvcd/files.h> +#include <cdio/cdio.h> +#include <cdio/ds.h> + +#ifdef __cplusplus +extern "C" { +#endif /* __cplusplus */ + +/*========== Move somewhere else? ================*/ + +/*! \def Max # characters in an album id. */ +#define MAX_ALBUM_LEN 16 + +/*! \def Max # of selections allowed in a PBC selection list. */ +#define MAX_PBC_SELECTIONS 99 + +#define MIN_ENCODED_TRACK_NUM 100 +#define MIN_ENCODED_SEGMENT_NUM 1000 +#define MAX_ENCODED_SEGMENT_NUM 2979 + +/*! + 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_t *pld; + PsdSelectionListDescriptor_t *psd; + /* }; */ + + } PsdListDescriptor_t; + + /* For backwards compatibility. Don't use PsdListDescriptor. */ +#define PsdListDescriptor PsdListDescriptor_t + + /*! + 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. + */ + char * + vcdinfo_get_application_id(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 the LID offset associated with a the "default" entry of the + passed-in LID parameter. Note "default" entries are associated with + PSDs that are (extended) selection lists. + + \return VCDINFO_INVALID_OFFSET is returned on error, or if the LID + is not a selection list or no "default" entry. Otherwise the LID + offset is returned. + */ + uint16_t + vcdinfo_get_default_offset(const vcdinfo_obj_t *obj, lid_t 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_t * vcdinfo_get_entriesVcd (vcdinfo_obj_t *obj); + + InfoVcd_t * vcdinfo_get_infoVcd (vcdinfo_obj_t *obj); + + /*! + \brief Get default or multi-default LID. + + Return the LID offset associated with a the "default" entry of the + passed-in LID parameter. Note "default" entries are associated + with PSDs that are (extended) selection lists. If the "default" + is a multi-default, we use entry_num to find the proper + "default" LID. Otherwise this routine is exactly like + vcdinfo_get_default_offset with the exception of requiring an + additional "entry_num" parameter. + + \return VCDINFO_INVALID_LID is returned on error, or if the LID + is not a selection list or no "default" entry. Otherwise the LID + offset is returned. + */ + lid_t + vcdinfo_get_multi_default_lid(const vcdinfo_obj_t *obj, lid_t lid, + lsn_t lsn); + + /*! + \brief Get default or multi-default LID offset. + + Return the LID offset associated with a the "default" entry of the + passed-in LID parameter. Note "default" entries are associated + with PSDs that are (extended) selection lists. If the "default" + is a multi-default, we use entry_num to find the proper + "default" offset. Otherwise this routine is exactly like + vcdinfo_get_default_offset with the exception of requiring an + additional "entry_num" parameter. + + \return VCDINFO_INVALID_OFFSET is returned on error, or if the LID + is not a selection list or no "default" entry. Otherwise the LID + offset is returned. + */ + uint16_t + vcdinfo_get_multi_default_offset(const vcdinfo_obj_t *obj, lid_t lid, + unsigned int selection); + + void * vcdinfo_get_pvd (vcdinfo_obj_t *obj); + + void * vcdinfo_get_scandata (vcdinfo_obj_t *obj); + + void * vcdinfo_get_searchDat (vcdinfo_obj_t *obj); + + void * vcdinfo_get_tracksSVD (vcdinfo_obj_t *obj); + + /*! + Get the LOT pointer. + */ + LotVcd_t * + vcdinfo_get_lot(const vcdinfo_obj_t *obj); + + /*! + Get the extended LOT pointer. + */ + LotVcd_t * + 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); + + /*! + Get the VCD info list. + */ + CdioList *vcdinfo_get_offset_list(const vcdinfo_obj_t *obj); + + /*! + Get the VCD info extended offset list. + */ + CdioList *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); + + /*! + Return a string containing the VCD publisher id with trailing + blanks removed, or NULL if there is some problem in getting this. + */ + 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); + + /*! + 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); + + /*! + \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 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_t *pxd, + uint16_t lid); + + /*! Return the entry number closest and before the given LSN. + */ + unsigned int + vcdinfo_lsn_get_entry(const vcdinfo_obj_t *obj, lsn_t lsn); + + /*! + 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); + + /*! + Calls recursive routine to populate obj->offset_list or obj->offset_x_list + by going through LOT. + + Returns false if there was some error. + */ + bool vcdinfo_visit_lot (vcdinfo_obj_t *obj, bool extended); + + bool vcdinfo_read_psd (vcdinfo_obj_t *obj); + + /*! + \fn vcdinfo_selection_get_lid(const vcdinfo_obj_t *obj, lid_t lid, + unsigned int selection); + \brief Get the "default" lid of a selection for a given lid. + + Return the LID offset associated with a the selection number of the + passed-in LID parameter. + + \return VCDINFO_INVALID_LID is returned if obj on error or obj + is NULL. Otherwise the LID offset is returned. + */ + lid_t vcdinfo_selection_get_lid(const vcdinfo_obj_t *obj, lid_t lid, + unsigned int selection); + + /*! + \fn vcdinfo_selection_get_offset(const vcdinfo_obj_t *obj, lid_t lid, + unsigned int selection); + \brief Get offset of a selection for a given LID. + + Return the LID offset associated with a the selection number of the + passed-in LID parameter. + + \return VCDINFO_INVALID_OFFSET is returned if obj on error or obj + is NULL. Otherwise the LID offset is returned. + */ + uint16_t vcdinfo_selection_get_offset(const vcdinfo_obj_t *obj, lid_t lid, + unsigned int selection); + + /*! + 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); + + /*! Return the entry number for the given track. + */ + unsigned int + vcdinfo_track_get_entry(const vcdinfo_obj_t *obj, track_t track); + + /*! + 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/contrib/libvcd/libvcd/logging.h b/contrib/libvcd/libvcd/logging.h new file mode 100644 index 000000000..bc088ed1c --- /dev/null +++ b/contrib/libvcd/libvcd/logging.h @@ -0,0 +1,137 @@ +/* + $Id: logging.h,v 1.2 2004/04/11 12:20:32 miguelfreitas Exp $ + + Copyright (C) 2000 Herbert Valerio Riedel <hvr@gnu.org> + Copyright (C) 2003 Rocky Bernstein <rocky@panix.com> + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +*/ + +#ifndef __VCD_LOGGING_H__ +#define __VCD_LOGGING_H__ + +#include <libvcd/types.h> + +#ifdef __cplusplus +extern "C" { +#endif /* __cplusplus */ + +/** + * The different log levels supported. + */ +typedef enum { + VCD_LOG_DEBUG = 1, /**< Debug-level messages - helps debug what's up. */ + VCD_LOG_INFO, /**< Informational - indicates perhaps something of + interest. */ + VCD_LOG_WARN, /**< Warning conditions - something that looks funny. */ + VCD_LOG_ERROR, /**< Error conditions - may terminate program. */ + VCD_LOG_ASSERT /**< Critical conditions - may abort program. */ +} vcd_log_level_t; + +/** + * The place to save the preference concerning how much verbosity + * is desired. This is used by the internal default log handler, but + * it could be use by applications which provide their own log handler. + */ +extern vcd_log_level_t vcd_loglevel_default; + +/** + * This type defines the signature of a log handler. For every + * message being logged, the handler will receive the log level and + * the message string. + * + * @see vcd_log_set_handler + * @see vcd_log_level_t + * + * @param level The log level. + * @param message The log message. + */ +typedef void (*vcd_log_handler_t) (vcd_log_level_t level, + const char message[]); + +/** + * Set a custom log handler for libcdio. The return value is the log + * handler being replaced. If the provided parameter is NULL, then + * the handler will be reset to the default handler. + * + * @see vcd_log_handler_t + * + * @param new_handler The new log handler. + * @return The previous log handler. + */ +vcd_log_handler_t +vcd_log_set_handler (vcd_log_handler_t new_handler); + +/** + * Handle an message with the given log level + * + * @see vcd_debug + * @see vcd_info + * @see vcd_warn + * @see vcd_error + + * @param level The log level. + * @param format printf-style format string + * @param ... remaining arguments needed by format string + */ +void +vcd_log (vcd_log_level_t level, const char format[], ...) GNUC_PRINTF(2, 3); + +/** + * Handle a debugging message. + * + * @see vcd_log for a more generic routine + */ +void +vcd_debug (const char format[], ...) GNUC_PRINTF(1,2); + +/** + * Handle an informative message. + * + * @see vcd_log for a more generic routine + */ +void +vcd_info (const char format[], ...) GNUC_PRINTF(1,2); + +/** + * Handle a warning message. + * + * @see vcd_log for a more generic routine + */ +void +vcd_warn (const char format[], ...) GNUC_PRINTF(1,2); + +/** + * Handle an error message. + * + * @see vcd_log for a more generic routine. + */ +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/contrib/libvcd/libvcd/sector.h b/contrib/libvcd/libvcd/sector.h new file mode 100644 index 000000000..32c2cc89a --- /dev/null +++ b/contrib/libvcd/libvcd/sector.h @@ -0,0 +1,118 @@ +/* + $Id: sector.h,v 1.2 2004/04/11 12:20:32 miguelfreitas 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/contrib/libvcd/libvcd/types.h b/contrib/libvcd/libvcd/types.h new file mode 100644 index 000000000..56a07d447 --- /dev/null +++ b/contrib/libvcd/libvcd/types.h @@ -0,0 +1,97 @@ +/* + $Id: types.h,v 1.3 2005/01/01 02:43:59 rockyb Exp $ + + Copyright (C) 2000, 2004 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_t; + typedef struct _EntriesVcd_tag EntriesVcd_t; + typedef struct _LotVcd_tag LotVcd_t; + + typedef struct _PsdPlayListDescriptor_tag PsdPlayListDescriptor_t; + typedef struct _PsdSelectionListDescriptor_tag PsdSelectionListDescriptor_t; + + /* 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/contrib/libvcd/libvcd/version.h b/contrib/libvcd/libvcd/version.h new file mode 100644 index 000000000..0cd2096d3 --- /dev/null +++ b/contrib/libvcd/libvcd/version.h @@ -0,0 +1,11 @@ +/* $Id: version.h,v 1.4 2005/04/27 23:28:42 rockyb Exp $ */ +/** \file version.h + * \brief A file simply containing the library version number. + */ + +/*! LIBVCD_VERSION can as a string in programs to show what version is used. */ +#define LIBVCD_VERSION "0.7.21-" + +/*! LIBCDIO_VERSION_NUM can be used for testing in the C preprocessor */ +#define LIBVCD_VERSION_NUM 21 + diff --git a/contrib/libvcd/logging.c b/contrib/libvcd/logging.c new file mode 100644 index 000000000..652a8090b --- /dev/null +++ b/contrib/libvcd/logging.c @@ -0,0 +1,145 @@ +/* + $Id: logging.c,v 1.2 2004/04/11 12:20:32 miguelfreitas Exp $ + + Copyright (C) 2000 Herbert Valerio Riedel <hvr@gnu.org> + Copyright (C) 2003 Rocky Bernstein <rocky@panix.com> + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +*/ + +#ifdef HAVE_CONFIG_H +# include "config.h" +#endif + +#include <stdlib.h> +#include <stdarg.h> +#include <stdio.h> + +/* Public headers */ +#include <libvcd/logging.h> + +/* Private headers */ +#include "vcd_assert.h" + +static const char _rcsid[] = "$Id: logging.c,v 1.2 2004/04/11 12:20:32 miguelfreitas Exp $"; + +vcd_log_level_t vcd_loglevel_default = VCD_LOG_WARN; + +static void +default_vcd_log_handler (vcd_log_level_t level, const char message[]) +{ + switch (level) + { + case VCD_LOG_ERROR: + if (level >= vcd_loglevel_default) { + fprintf (stderr, "**ERROR: %s\n", message); + fflush (stderr); + exit (EXIT_FAILURE); + } + break; + case VCD_LOG_DEBUG: + if (level >= vcd_loglevel_default) { + fprintf (stdout, "--DEBUG: %s\n", message); + } + break; + case VCD_LOG_WARN: + if (level >= vcd_loglevel_default) { + fprintf (stdout, "++ WARN: %s\n", message); + } + break; + case VCD_LOG_INFO: + if (level >= vcd_loglevel_default) { + fprintf (stdout, " INFO: %s\n", message); + } + break; + case VCD_LOG_ASSERT: + if (level >= vcd_loglevel_default) { + 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/contrib/libvcd/mpeg.c b/contrib/libvcd/mpeg.c new file mode 100644 index 000000000..545a44fbc --- /dev/null +++ b/contrib/libvcd/mpeg.c @@ -0,0 +1,1177 @@ +/* + $Id: mpeg.c,v 1.2 2004/04/11 12:20:32 miguelfreitas 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.2 2004/04/11 12:20:32 miguelfreitas 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", (unsigned int) 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/contrib/libvcd/mpeg.h b/contrib/libvcd/mpeg.h new file mode 100644 index 000000000..2d28d2aef --- /dev/null +++ b/contrib/libvcd/mpeg.h @@ -0,0 +1,194 @@ +/* + $Id: mpeg.h,v 1.3 2005/01/01 02:43:59 rockyb 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; + + CdioList *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/contrib/libvcd/mpeg_stream.c b/contrib/libvcd/mpeg_stream.c new file mode 100644 index 000000000..e5466766a --- /dev/null +++ b/contrib/libvcd/mpeg_stream.c @@ -0,0 +1,487 @@ +/* + $Id: mpeg_stream.c,v 1.3 2005/01/01 02:43:59 rockyb Exp $ + + Copyright (C) 2000, 2004 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 <cdio/bytesex.h> + +#include <libvcd/logging.h> + +/* Private headers */ +#include "vcd_assert.h" +#include "mpeg_stream.h" +#include "data_structures.h" +#include "mpeg.h" +#include "util.h" + +static const char _rcsid[] = "$Id: mpeg_stream.c,v 1.3 2005/01/01 02:43:59 rockyb 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) + _cdio_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; + CdioListNode *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 = _cdio_list_new (); + + _cdio_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) + _CDIO_LIST_FOREACH (n, obj->info.shdr[i].aps_list) + { + struct aps_data *_data = _cdio_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 (CdioList *aps_list, uint32_t packet_no) +{ + double retval = 0; + CdioListNode *node; + + struct aps_data *_laps = NULL; + + double last_pts_ratio = 0; + + _CDIO_LIST_FOREACH (node, aps_list) + { + struct aps_data *_aps = _cdio_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, CdioList *aps_list) +{ + CdioListNode *node; + long _next = -1, _prev = -1, _forw = -1, _back = -1; + + _CDIO_LIST_FOREACH (node, aps_list) + { + struct aps_data *_aps = _cdio_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/contrib/libvcd/mpeg_stream.h b/contrib/libvcd/mpeg_stream.h new file mode 100644 index 000000000..14f73d928 --- /dev/null +++ b/contrib/libvcd/mpeg_stream.h @@ -0,0 +1,116 @@ +/* + $Id: mpeg_stream.h,v 1.2 2004/04/11 12:20:32 miguelfreitas 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/contrib/libvcd/obj.h b/contrib/libvcd/obj.h new file mode 100644 index 000000000..d6849a3ab --- /dev/null +++ b/contrib/libvcd/obj.h @@ -0,0 +1,202 @@ +/* + $Id: obj.h,v 1.3 2005/01/01 02:43:59 rockyb 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; + + CdioList *pause_list; /* pause_t */ + + char *default_entry_id; + CdioList *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; + + CdioList *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; + CdioList *mpeg_segment_list; /* mpeg_segment_t */ + + CdioList *mpeg_sequence_list; /* mpeg_sequence_t */ + + unsigned relative_end_extent; /* last mpeg sequence track end extent */ + + /* PBC */ + CdioList *pbc_list; /* pbc_t */ + unsigned psd_size; + unsigned psdx_size; + + /* custom files */ + unsigned ext_file_start_extent; + unsigned custom_file_start_extent; + CdioList *custom_file_list; /* custom_file_t */ + CdioList *custom_dir_list; /* char */ + + /* dictionary */ + CdioList *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/contrib/libvcd/pbc.c b/contrib/libvcd/pbc.c new file mode 100644 index 000000000..1b4f1a6c1 --- /dev/null +++ b/contrib/libvcd/pbc.c @@ -0,0 +1,880 @@ +/* + $Id: pbc.c,v 1.3 2005/01/01 02:43:59 rockyb Exp $ + + Copyright (C) 2000, 2004 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> +#include <cdio/bytesex.h> + +/* Public headers */ +#include <libvcd/logging.h> +#include <libvcd/files.h> +#include <libvcd/types.h> +#include <libvcd/info.h> + +/* FIXME! Make this really private. */ +#include <libvcd/files_private.h> + +/* Private headers */ +#include "vcd_assert.h" +#include "obj.h" +#include "pbc.h" +#include "util.h" + +static const char _rcsid[] = "$Id: pbc.c,v 1.3 2005/01/01 02:43:59 rockyb 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[]) +{ + CdioListNode *node; + + _CDIO_LIST_FOREACH (node, obj->pbc_list) + { + pbc_t *_pbc = _cdio_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[]) +{ + CdioListNode *node; + unsigned n = 1; + + _CDIO_LIST_FOREACH (node, obj->pbc_list) + { + pbc_t *_pbc = _cdio_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 < MIN_ENCODED_TRACK_NUM) + return ITEM_TYPE_TRACK; + else if (id < 600) + return ITEM_TYPE_ENTRY; + else if (id <= MAX_ENCODED_SEGMENT_NUM) + 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; + CdioListNode *node; + + if (!item_id) + return 0; + + /* check sequence items */ + + n = 0; + _CDIO_LIST_FOREACH (node, obj->mpeg_sequence_list) + { + mpeg_sequence_t *_sequence = _cdio_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; + _CDIO_LIST_FOREACH (node, obj->mpeg_sequence_list) + { + mpeg_sequence_t *_sequence = _cdio_list_node_data (node); + CdioListNode *node2; + + /* default entry point */ + + if (_sequence->default_entry_id + && !strcmp (item_id, _sequence->default_entry_id)) + return n + 100; + n++; + + /* additional entry points */ + + _CDIO_LIST_FOREACH (node2, _sequence->entry_list) + { + entry_t *_entry = _cdio_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; + _CDIO_LIST_FOREACH (node, obj->mpeg_segment_list) + { + mpeg_segment_t *_segment = _cdio_list_node_data (node); + + vcd_assert (n < 1980); + + if (_segment->id && !strcmp (item_id, _segment->id)) + return n + MIN_ENCODED_SEGMENT_NUM; + + n += _segment->segment_count; + } + + return 0; +} + +bool +_vcd_pbc_available (const VcdObj *obj) +{ + vcd_assert (obj != NULL); + vcd_assert (obj->pbc_list != NULL); + + if (!_cdio_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 = _cdio_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 = _cdio_list_length (_pbc->item_id_list); + retval = __cd_offsetof (_PsdPlayListDescriptor, itemid[n]); + break; + + case PBC_SELECTION: + n = _cdio_list_length (_pbc->select_id_list); + + retval = __cd_offsetof (PsdSelectionListDescriptor_t, 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) +{ + CdioListNode *node; + + if (extended) + vcd_assert (_vcd_obj_has_cap_p (obj, _CAP_PBC_X)); + + /* disable it */ + if (!item_id) + return PSD_OFS_DISABLED; + + _CDIO_LIST_FOREACH (node, obj->pbc_list) + { + pbc_t *_pbc = _cdio_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: + { + CdioListNode *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); + + _CDIO_LIST_FOREACH (node, _pbc->item_id_list) + { + const char *_id = _cdio_list_node_data (node); + + _vcd_pin_mark_id (obj, _id); + } + } + break; + + case PBC_SELECTION: + { + CdioListNode *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); + + _CDIO_LIST_FOREACH (node, _pbc->select_id_list) + { + const char *_id = _cdio_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) +{ + CdioListNode *node; + + /* clear all flags */ + + _CDIO_LIST_FOREACH (node, obj->pbc_list) + { + pbc_t *_pbc = _cdio_list_node_data (node); + + _pbc->referenced = false; + } + + _CDIO_LIST_FOREACH (node, obj->mpeg_sequence_list) + { + mpeg_sequence_t *_sequence = _cdio_list_node_data (node); + + _sequence->referenced = false; + } + + _CDIO_LIST_FOREACH (node, obj->mpeg_segment_list) + { + mpeg_segment_t *_segment = _cdio_list_node_data (node); + + _segment->referenced = false; + } + + /* start from non-rejected lists */ + + _CDIO_LIST_FOREACH (node, obj->pbc_list) + { + pbc_t *_pbc = _cdio_list_node_data (node); + + vcd_assert (_pbc->id != NULL); + + if (_pbc->rejected) + continue; + + _vcd_pbc_mark_id (obj, _pbc->id); + } + + /* collect flags */ + + _CDIO_LIST_FOREACH (node, obj->pbc_list) + { + pbc_t *_pbc = _cdio_list_node_data (node); + + if (!_pbc->referenced) + vcd_warn ("PSD item '%s' is unreachable", _pbc->id); + } + + _CDIO_LIST_FOREACH (node, obj->mpeg_sequence_list) + { + mpeg_sequence_t *_sequence = _cdio_list_node_data (node); + + if (!_sequence->referenced) + vcd_warn ("sequence '%s' is not reachable by PBC", _sequence->id); + } + + _CDIO_LIST_FOREACH (node, obj->mpeg_segment_list) + { + mpeg_segment_t *_segment = _cdio_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; + CdioListNode *node; + int n; + + _md->type = PSD_TYPE_PLAY_LIST; + _md->noi = _cdio_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; + _CDIO_LIST_FOREACH (node, _pbc->item_id_list) + { + const char *_id = _cdio_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_t *_md = buf; + + const unsigned int _nos = _cdio_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, MAX_PBC_SELECTIONS)) + vcd_error ("selection '%s': BSN (%d) not in range [1..%d]", + _pbc->id, _pbc->bsn, MAX_PBC_SELECTIONS); + + if (!IN (_nos, 0, MAX_PBC_SELECTIONS)) + vcd_error ("selection '%s': too many selections (%d > %d)", + _pbc->id, _nos, MAX_PBC_SELECTIONS); + + 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 = + _cdio_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 */ + { + CdioListNode *node = NULL; + int idx = 0; + + idx = 0; + _CDIO_LIST_FOREACH (node, _pbc->select_id_list) + { + const char *_id = _cdio_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; + CdioListNode *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) + _CDIO_LIST_FOREACH (node, _pbc->select_area_list) + { + const pbc_area_t *_area = _cdio_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 = _cdio_list_new (); + break; + + case PBC_SELECTION: + _pbc->select_id_list = _cdio_list_new (); + _pbc->select_area_list = _cdio_list_new (); + break; + + case PBC_END: + break; + + default: + vcd_assert_not_reached (); + break; + } + + return _pbc; +} + +/* + */ + +bool +_vcd_pbc_finalize (VcdObj *obj) +{ + CdioListNode *node; + unsigned offset = 0, offset_ext = 0; + unsigned lid; + + lid = 1; + _CDIO_LIST_FOREACH (node, obj->pbc_list) + { + pbc_t *_pbc = _cdio_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/contrib/libvcd/pbc.h b/contrib/libvcd/pbc.h new file mode 100644 index 000000000..9972c3143 --- /dev/null +++ b/contrib/libvcd/pbc.h @@ -0,0 +1,157 @@ +/* + $Id: pbc.h,v 1.3 2005/01/01 02:43:59 rockyb 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; + CdioList *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 */ + CdioList *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; + CdioList *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/contrib/libvcd/salloc.c b/contrib/libvcd/salloc.c new file mode 100644 index 000000000..d6fc41c68 --- /dev/null +++ b/contrib/libvcd/salloc.c @@ -0,0 +1,212 @@ +/* + $Id: salloc.c,v 1.2 2004/04/11 12:20:32 miguelfreitas 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 "vcd_assert.h" +#include "salloc.h" +#include "util.h" + +static const char _rcsid[] = "$Id: salloc.c,v 1.2 2004/04/11 12:20:32 miguelfreitas 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/contrib/libvcd/salloc.h b/contrib/libvcd/salloc.h new file mode 100644 index 000000000..4676f9b45 --- /dev/null +++ b/contrib/libvcd/salloc.h @@ -0,0 +1,56 @@ +/* + $Id: salloc.h,v 1.2 2004/04/11 12:20:32 miguelfreitas 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/contrib/libvcd/sector.c b/contrib/libvcd/sector.c new file mode 100644 index 000000000..d072a4f17 --- /dev/null +++ b/contrib/libvcd/sector.c @@ -0,0 +1,268 @@ +/* + $Id: sector.c,v 1.2 2004/04/11 12:20:32 miguelfreitas 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 "vcd_assert.h" +#include "bytesex.h" +#include "salloc.h" +#include "sector_private.h" + +static const char _rcsid[] = "$Id: sector.c,v 1.2 2004/04/11 12:20:32 miguelfreitas 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; + } +} + +/* From cdrtools-1.11a25 */ +static uint32_t +build_edc(const uint8_t inout[], int from, int upto) +{ + const uint8_t *p = inout+from; + uint32_t result = 0; + + upto -= from-1; + upto /= 4; + while (--upto >= 0) { + result = EDC_crctable[(result ^ *p++) & 0xffL] ^ (result >> 8); + result = EDC_crctable[(result ^ *p++) & 0xffL] ^ (result >> 8); + result = EDC_crctable[(result ^ *p++) & 0xffL] ^ (result >> 8); + result = EDC_crctable[(result ^ *p++) & 0xffL] ^ (result >> 8); + } + return (result); +} + +/* From cdrtools-1.11a40 */ +static void +encode_L2_Q(uint8_t inout[4 + L2_RAW + 4 + 8 + L2_P + L2_Q]) +{ + uint8_t *dps; + uint8_t *dp; + uint8_t *Q; + int i, j; + + Q = inout + 4 + L2_RAW + 4 + 8 + L2_P; + + dps = inout; + for (j = 0; j < 26; j++) { + uint16_t a, b; + + a = b = 0; + dp = dps; + for (i = 0; i < 43; i++) { + + /* LSB */ + a ^= L2sq[i][*dp++]; + + /* MSB */ + b ^= L2sq[i][*dp]; + + dp += 2*44-1; + if (dp >= &inout[(4 + L2_RAW + 4 + 8 + L2_P)]) { + dp -= (4 + L2_RAW + 4 + 8 + L2_P); + } + } + Q[0] = a >> 8; + Q[26*2] = a; + Q[1] = b >> 8; + Q[26*2+1] = b; + + Q += 2; + dps += 2*43; + } +} + +static void +encode_L2_P (uint8_t inout[4 + L2_RAW + 4 + 8 + L2_P]) +{ + uint8_t *dp; + unsigned char *P; + int i, j; + + P = inout + 4 + L2_RAW + 4 + 8; + + for (j = 0; j < 43; j++) { + uint16_t a; + uint16_t b; + + a = b = 0; + dp = inout; + for (i = 19; i < 43; i++) { + + /* LSB */ + a ^= L2sq[i][*dp++]; + + /* MSB */ + b ^= L2sq[i][*dp]; + + dp += 2*43 -1; + } + P[0] = a >> 8; + P[43*2] = a; + P[1] = b >> 8; + P[43*2+1] = b; + + 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/contrib/libvcd/sector_private.h b/contrib/libvcd/sector_private.h new file mode 100644 index 000000000..9411b5420 --- /dev/null +++ b/contrib/libvcd/sector_private.h @@ -0,0 +1,1348 @@ +/* + $Id: sector_private.h,v 1.2 2004/04/11 12:20:32 miguelfreitas 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 + +/*****************************************************************/ +/* */ +/* CRC LOOKUP TABLE */ +/* ================ */ +/* The following CRC lookup table was generated automagically */ +/* by the Rocksoft^tm Model CRC Algorithm Table Generation */ +/* Program V1.0 using the following model parameters: */ +/* */ +/* Width : 4 bytes. */ +/* Poly : 0x8001801BL */ +/* Reverse : TRUE. */ +/* */ +/* For more information on the Rocksoft^tm Model CRC Algorithm, */ +/* see the document titled "A Painless Guide to CRC Error */ +/* Detection Algorithms" by Ross Williams */ +/* (ross@guest.adelaide.edu.au.). This document is likely to be */ +/* in the FTP archive "ftp.adelaide.edu.au/pub/rocksoft". */ +/* */ +/*****************************************************************/ + +static const uint32_t EDC_crctable[256] = +{ + 0x00000000U, 0x90910101U, 0x91210201U, 0x01B00300U, + 0x92410401U, 0x02D00500U, 0x03600600U, 0x93F10701U, + 0x94810801U, 0x04100900U, 0x05A00A00U, 0x95310B01U, + 0x06C00C00U, 0x96510D01U, 0x97E10E01U, 0x07700F00U, + 0x99011001U, 0x09901100U, 0x08201200U, 0x98B11301U, + 0x0B401400U, 0x9BD11501U, 0x9A611601U, 0x0AF01700U, + 0x0D801800U, 0x9D111901U, 0x9CA11A01U, 0x0C301B00U, + 0x9FC11C01U, 0x0F501D00U, 0x0EE01E00U, 0x9E711F01U, + 0x82012001U, 0x12902100U, 0x13202200U, 0x83B12301U, + 0x10402400U, 0x80D12501U, 0x81612601U, 0x11F02700U, + 0x16802800U, 0x86112901U, 0x87A12A01U, 0x17302B00U, + 0x84C12C01U, 0x14502D00U, 0x15E02E00U, 0x85712F01U, + 0x1B003000U, 0x8B913101U, 0x8A213201U, 0x1AB03300U, + 0x89413401U, 0x19D03500U, 0x18603600U, 0x88F13701U, + 0x8F813801U, 0x1F103900U, 0x1EA03A00U, 0x8E313B01U, + 0x1DC03C00U, 0x8D513D01U, 0x8CE13E01U, 0x1C703F00U, + 0xB4014001U, 0x24904100U, 0x25204200U, 0xB5B14301U, + 0x26404400U, 0xB6D14501U, 0xB7614601U, 0x27F04700U, + 0x20804800U, 0xB0114901U, 0xB1A14A01U, 0x21304B00U, + 0xB2C14C01U, 0x22504D00U, 0x23E04E00U, 0xB3714F01U, + 0x2D005000U, 0xBD915101U, 0xBC215201U, 0x2CB05300U, + 0xBF415401U, 0x2FD05500U, 0x2E605600U, 0xBEF15701U, + 0xB9815801U, 0x29105900U, 0x28A05A00U, 0xB8315B01U, + 0x2BC05C00U, 0xBB515D01U, 0xBAE15E01U, 0x2A705F00U, + 0x36006000U, 0xA6916101U, 0xA7216201U, 0x37B06300U, + 0xA4416401U, 0x34D06500U, 0x35606600U, 0xA5F16701U, + 0xA2816801U, 0x32106900U, 0x33A06A00U, 0xA3316B01U, + 0x30C06C00U, 0xA0516D01U, 0xA1E16E01U, 0x31706F00U, + 0xAF017001U, 0x3F907100U, 0x3E207200U, 0xAEB17301U, + 0x3D407400U, 0xADD17501U, 0xAC617601U, 0x3CF07700U, + 0x3B807800U, 0xAB117901U, 0xAAA17A01U, 0x3A307B00U, + 0xA9C17C01U, 0x39507D00U, 0x38E07E00U, 0xA8717F01U, + 0xD8018001U, 0x48908100U, 0x49208200U, 0xD9B18301U, + 0x4A408400U, 0xDAD18501U, 0xDB618601U, 0x4BF08700U, + 0x4C808800U, 0xDC118901U, 0xDDA18A01U, 0x4D308B00U, + 0xDEC18C01U, 0x4E508D00U, 0x4FE08E00U, 0xDF718F01U, + 0x41009000U, 0xD1919101U, 0xD0219201U, 0x40B09300U, + 0xD3419401U, 0x43D09500U, 0x42609600U, 0xD2F19701U, + 0xD5819801U, 0x45109900U, 0x44A09A00U, 0xD4319B01U, + 0x47C09C00U, 0xD7519D01U, 0xD6E19E01U, 0x46709F00U, + 0x5A00A000U, 0xCA91A101U, 0xCB21A201U, 0x5BB0A300U, + 0xC841A401U, 0x58D0A500U, 0x5960A600U, 0xC9F1A701U, + 0xCE81A801U, 0x5E10A900U, 0x5FA0AA00U, 0xCF31AB01U, + 0x5CC0AC00U, 0xCC51AD01U, 0xCDE1AE01U, 0x5D70AF00U, + 0xC301B001U, 0x5390B100U, 0x5220B200U, 0xC2B1B301U, + 0x5140B400U, 0xC1D1B501U, 0xC061B601U, 0x50F0B700U, + 0x5780B800U, 0xC711B901U, 0xC6A1BA01U, 0x5630BB00U, + 0xC5C1BC01U, 0x5550BD00U, 0x54E0BE00U, 0xC471BF01U, + 0x6C00C000U, 0xFC91C101U, 0xFD21C201U, 0x6DB0C300U, + 0xFE41C401U, 0x6ED0C500U, 0x6F60C600U, 0xFFF1C701U, + 0xF881C801U, 0x6810C900U, 0x69A0CA00U, 0xF931CB01U, + 0x6AC0CC00U, 0xFA51CD01U, 0xFBE1CE01U, 0x6B70CF00U, + 0xF501D001U, 0x6590D100U, 0x6420D200U, 0xF4B1D301U, + 0x6740D400U, 0xF7D1D501U, 0xF661D601U, 0x66F0D700U, + 0x6180D800U, 0xF111D901U, 0xF0A1DA01U, 0x6030DB00U, + 0xF3C1DC01U, 0x6350DD00U, 0x62E0DE00U, 0xF271DF01U, + 0xEE01E001U, 0x7E90E100U, 0x7F20E200U, 0xEFB1E301U, + 0x7C40E400U, 0xECD1E501U, 0xED61E601U, 0x7DF0E700U, + 0x7A80E800U, 0xEA11E901U, 0xEBA1EA01U, 0x7B30EB00U, + 0xE8C1EC01U, 0x7850ED00U, 0x79E0EE00U, 0xE971EF01U, + 0x7700F000U, 0xE791F101U, 0xE621F201U, 0x76B0F300U, + 0xE541F401U, 0x75D0F500U, 0x7460F600U, 0xE4F1F701U, + 0xE381F801U, 0x7310F900U, 0x72A0FA00U, 0xE231FB01U, + 0x71C0FC00U, 0xE151FD01U, 0xE0E1FE01U, 0x7070FF00U, +}; + + +static const uint16_t L2sq[43][256] = { + { 0, 44719, 16707, 61420, 33414, 11305, 50117, 28010, 6417, 47038, + 22610, 63229, 39831, 13624, 56020, 29819, 12834, 40077, 29537, 56782, + 45220, 7691, 61927, 24392, 11059, 34204, 27248, 50399, 43445, 1818, + 59638, 18009, 25668, 51947, 9479, 35752, 59074, 18541, 42881, 2350, + 32085, 54266, 15382, 37561, 65491, 20860, 48784, 4159, 22118, 63689, + 5925, 47498, 54496, 31311, 38307, 15116, 20343, 57816, 3636, 41115, + 52721, 25438, 36018, 8733, 51336, 26151, 35275, 10084, 18958, 58529, + 2893, 42466, 53657, 32566, 37082, 15989, 21279, 64944, 4700, 48371, + 64170, 21509, 48105, 5446, 30764, 54915, 14703, 38848, 58299, 19732, + 41720, 3159, 24893, 53138, 8318, 36561, 44236, 611, 60815, 17184, + 11850, 32997, 28425, 49574, 46557, 7026, 62622, 23089, 14171, 39412, + 30232, 55479, 40686, 12353, 57261, 28930, 7272, 45767, 23851, 62340, + 34815, 10576, 50876, 26643, 1401, 43990, 17466, 60053, 36109, 9122, + 52302, 25313, 3979, 41252, 20168, 57447, 37916, 15027, 54623, 31728, + 5786, 47157, 22489, 63862, 48943, 4480, 65132, 20675, 15785, 37638, + 31978, 53829, 42558, 2193, 59261, 18898, 9400, 35351, 26107, 52052, + 59721, 18406, 43018, 1701, 27599, 50528, 10892, 33827, 61528, 24311, + 45339, 8116, 29406, 56433, 13213, 40242, 56171, 30148, 39464, 13447, + 23021, 63298, 6318, 46593, 49786, 27861, 33593, 11670, 16636, 61011, + 447, 44816, 17797, 60202, 1222, 43625, 50947, 27052, 34368, 10479, + 23700, 62011, 7639, 45944, 56850, 28861, 40785, 12798, 30631, 55560, + 14052, 38987, 62753, 23438, 46178, 6861, 28342, 49177, 12277, 33114, + 60464, 17055, 44403, 988, 8641, 36718, 24706, 52781, 41799, 3560, + 57860, 19627, 14544, 38527, 31123, 55100, 47702, 5369, 64277, 21946, + 5091, 48460, 21152, 64527, 37221, 16330, 53286, 32393, 2802, 42077, + 19377, 58654, 34932, 9947, 51511, 26520, + }, + { 0, 55768, 44973, 30325, 17223, 39583, 60650, 13618, 34446, 24406, + 10531, 61691, 50633, 7185, 27236, 46012, 4353, 51417, 48812, 26484, + 21062, 35742, 65003, 9267, 38799, 20055, 14370, 57850, 54472, 3344, + 31589, 41661, 8706, 64474, 36271, 21623, 24901, 47261, 52968, 5936, + 42124, 32084, 2849, 54009, 59339, 15891, 18534, 37310, 13059, 60123, + 40110, 17782, 28740, 43420, 57321, 1585, 46477, 27733, 6688, 50168, + 63178, 12050, 22887, 32959, 17412, 40412, 60329, 12913, 1859, 56987, + 43246, 28982, 49802, 6994, 27943, 46335, 33229, 22549, 11872, 63416, + 21765, 36061, 64168, 9072, 5698, 53146, 47599, 24631, 54155, 2643, + 31782, 42494, 37068, 18708, 16225, 59065, 26118, 49118, 51627, 4211, + 9537, 64665, 35564, 21300, 57480, 14672, 20261, 38653, 41935, 31255, + 3170, 54714, 30471, 44767, 55466, 370, 13376, 60824, 39917, 16949, + 61833, 10321, 24100, 34812, 45774, 27414, 7523, 50363, 34824, 20944, + 10149, 65149, 52047, 4759, 25826, 48442, 3718, 55134, 41259, 30963, + 19905, 37913, 57964, 15284, 39177, 16593, 13988, 61308, 55886, 918, + 30179, 44091, 8071, 50783, 45098, 27122, 23744, 34072, 62317, 10933, + 43530, 29650, 1447, 56447, 59725, 12437, 18144, 40760, 11396, 62812, + 33577, 23281, 28611, 46619, 49262, 6582, 47883, 25299, 5286, 52606, + 63564, 8596, 22497, 36409, 15749, 58461, 37416, 19440, 32450, 42778, + 53615, 2231, 52236, 5588, 25505, 47737, 36683, 22163, 8422, 63806, + 19074, 37722, 58671, 15607, 2501, 53277, 42600, 32688, 56589, 1237, + 29344, 43896, 40522, 18322, 12775, 59455, 23427, 33371, 62510, 11766, + 6340, 49436, 46953, 28337, 60942, 14294, 16803, 39035, 44361, 29841, + 740, 56124, 26752, 45400, 50989, 7925, 11207, 61983, 33898, 23986, + 65295, 9943, 20642, 35194, 48200, 26000, 5093, 51773, 31105, 41049, + 54828, 4084, 15046, 58142, 38251, 19635, + }, + { 0, 27757, 55514, 46263, 44457, 49604, 30067, 6430, 18255, 11042, + 40853, 62456, 60134, 34443, 12860, 24145, 36510, 58099, 22084, 14889, + 9015, 20314, 64493, 38784, 51665, 42428, 4363, 32102, 25720, 2069, + 48290, 53455, 289, 27980, 55803, 46486, 44168, 49381, 29778, 6207, + 18030, 10755, 40628, 62169, 60359, 34730, 13085, 24432, 36799, 58322, + 22373, 15112, 8726, 20091, 64204, 38561, 51440, 42141, 4138, 31815, + 25945, 2356, 48515, 53742, 578, 28207, 55960, 46837, 45035, 50054, + 30513, 7004, 17677, 10592, 40407, 61882, 59556, 33993, 12414, 23571, + 36060, 57521, 21510, 14443, 8565, 19736, 63919, 38338, 52115, 43006, + 4937, 32548, 26170, 2647, 48864, 53901, 867, 28430, 56249, 47060, + 44746, 49831, 30224, 6781, 17452, 10305, 40182, 61595, 59781, 34280, + 12639, 23858, 36349, 57744, 21799, 14666, 8276, 19513, 63630, 38115, + 51890, 42719, 4712, 32261, 26395, 2934, 49089, 54188, 1156, 26857, + 56414, 45107, 43309, 50496, 29175, 7578, 17355, 12198, 39697, 63356, + 61026, 33295, 14008, 23253, 35354, 58999, 21184, 16045, 10163, 19422, + 65385, 37636, 52565, 41272, 5519, 31202, 24828, 3217, 47142, 54347, + 1445, 27080, 56703, 45330, 43020, 50273, 28886, 7355, 17130, 11911, + 39472, 63069, 61251, 33582, 14233, 23540, 35643, 59222, 21473, 16268, + 9874, 19199, 65096, 37413, 52340, 40985, 5294, 30915, 25053, 3504, + 47367, 54634, 1734, 27307, 56860, 45681, 43887, 50946, 29621, 8152, + 16777, 11748, 39251, 62782, 60448, 32845, 13562, 22679, 34904, 58421, + 20610, 15599, 9713, 18844, 64811, 37190, 53015, 41850, 6093, 31648, + 25278, 3795, 47716, 54793, 2023, 27530, 57149, 45904, 43598, 50723, + 29332, 7929, 16552, 11461, 39026, 62495, 60673, 33132, 13787, 22966, + 35193, 58644, 20899, 15822, 9424, 18621, 64522, 36967, 52790, 41563, + 5868, 31361, 25503, 4082, 47941, 55080, + }, + { 0, 47289, 28015, 54742, 56030, 25191, 47025, 3848, 43425, 4376, + 50382, 31863, 29567, 52166, 7696, 42665, 20319, 63462, 8752, 39561, + 38273, 11576, 63726, 16471, 59134, 24135, 35729, 13096, 15392, 33945, + 20815, 59894, 40638, 9735, 62417, 19304, 17504, 64729, 10511, 37302, + 14111, 36774, 23152, 58057, 60865, 21880, 32942, 14359, 53729, 26968, + 48270, 1079, 2879, 45958, 26192, 57065, 30784, 49401, 5423, 44438, + 41630, 6695, 53233, 30536, 8545, 39384, 19470, 62647, 64447, 17158, + 38608, 11881, 35008, 12409, 58799, 23830, 21022, 60071, 16241, 34760, + 28222, 54919, 849, 48104, 46304, 3161, 55695, 24886, 51103, 32550, + 43760, 4681, 7489, 42488, 28718, 51351, 49119, 1894, 53936, 27145, + 25857, 56760, 2158, 45271, 5758, 44743, 31505, 50088, 52384, 29721, + 41423, 6518, 61568, 18489, 40431, 9558, 10846, 37607, 18225, 65416, + 22817, 57752, 13390, 36087, 33791, 15174, 61072, 22057, 17090, 64123, + 12205, 38676, 38940, 8357, 62835, 19914, 60259, 21466, 34316, 16053, + 12733, 35076, 23762, 58475, 3485, 46372, 24818, 55371, 55107, 28666, + 47660, 661, 42044, 7301, 51539, 29162, 32482, 50779, 5005, 43828, + 56444, 25797, 45331, 2474, 1698, 48667, 27597, 54132, 30173, 52580, + 6322, 40971, 44803, 6074, 49772, 31445, 37667, 11162, 65100, 18165, + 18941, 61764, 9362, 39979, 14978, 33339, 22509, 61268, 57436, 22757, + 36147, 13706, 25507, 56090, 3788, 46709, 47485, 452, 54290, 27819, + 51714, 29371, 42861, 8148, 4316, 43109, 32179, 50442, 11516, 37957, + 16787, 63786, 63010, 20123, 39757, 9204, 34141, 15844, 59442, 20619, + 24451, 59194, 13036, 35413, 64797, 17828, 36978, 10443, 10179, 40826, + 19116, 61973, 21692, 60421, 14803, 33130, 36450, 14043, 58125, 23476, + 45634, 2811, 57133, 26516, 26780, 53285, 1523, 48458, 7139, 41818, + 30348, 52789, 49469, 31108, 44114, 5355, + }, + { 0, 53971, 47547, 27496, 28523, 48568, 54992, 1027, 57046, 3077, + 26477, 46526, 45501, 25454, 2054, 56021, 41393, 29538, 6154, 51929, + 52954, 7177, 30561, 42418, 32615, 44468, 50908, 5135, 4108, 49887, + 43447, 31588, 24447, 36268, 59076, 13335, 12308, 58055, 35247, 23420, + 33193, 21370, 14354, 60097, 61122, 15377, 22393, 34218, 65230, 11293, + 18293, 38310, 37285, 17270, 10270, 64205, 8216, 62155, 39331, 19312, + 20339, 40352, 63176, 9243, 48894, 27693, 1861, 54678, 53653, 838, + 26670, 47869, 24616, 45819, 55699, 2880, 3907, 56720, 46840, 25643, + 8015, 52636, 42740, 29735, 28708, 41719, 51615, 6988, 49561, 4938, + 30754, 43761, 44786, 31777, 5961, 50586, 57729, 13138, 22586, 35561, + 36586, 23609, 14161, 58754, 16215, 60804, 34540, 21567, 20540, 33519, + 59783, 15188, 16432, 37603, 63883, 11096, 12123, 64904, 38624, 17459, + 40678, 19509, 10077, 62862, 61837, 9054, 18486, 39653, 25057, 45874, + 55386, 2697, 3722, 56409, 46897, 26082, 48951, 28132, 1676, 54367, + 53340, 655, 27111, 47924, 49232, 4739, 31211, 43832, 44859, 32232, + 5760, 50259, 7814, 52309, 42813, 30190, 29165, 41790, 51286, 6789, + 16030, 60493, 34597, 22006, 20981, 33574, 59470, 15005, 57416, 12955, + 23027, 35616, 36643, 24048, 13976, 58443, 40751, 19964, 9876, 62535, + 61508, 8855, 18943, 39724, 16889, 37674, 63554, 10897, 11922, 64577, + 38697, 17914, 57119, 3532, 26276, 46199, 45172, 25255, 2511, 56092, + 457, 54042, 47218, 27297, 28322, 48241, 55065, 1482, 32430, 44157, + 50965, 5574, 4549, 49942, 43134, 31405, 41080, 29355, 6595, 51984, + 53011, 7616, 30376, 42107, 32864, 21171, 14811, 60168, 61195, 15832, + 22192, 33891, 24246, 35941, 59149, 13790, 12765, 58126, 34918, 23221, + 8657, 62210, 39018, 19129, 20154, 40041, 63233, 9682, 65287, 11732, + 18108, 37999, 36972, 17087, 10711, 64260, + }, + { 0, 59366, 54225, 13367, 48063, 23641, 26734, 36744, 27491, 35973, + 47282, 24404, 53468, 14138, 781, 58603, 54982, 12576, 1303, 58097, + 28025, 35487, 48808, 22862, 48549, 23107, 28276, 35218, 1562, 57852, + 54731, 12845, 45457, 22135, 25152, 34214, 2606, 60872, 55807, 15897, + 56050, 15636, 2339, 61125, 24909, 34475, 45724, 21882, 26455, 32945, + 46214, 21344, 56552, 15118, 3897, 59615, 3124, 60370, 57317, 14339, + 46987, 20589, 25690, 33724, 32575, 39129, 44270, 19208, 50304, 9062, + 5969, 61623, 5212, 62394, 51085, 8299, 45027, 18437, 31794, 39892, + 43513, 19999, 31272, 40398, 4678, 62880, 49559, 9841, 49818, 9596, + 4427, 63149, 31013, 40643, 43764, 19730, 52910, 10568, 7551, 64153, + 29969, 37623, 42688, 16678, 42445, 16939, 30236, 37370, 7794, 63892, + 52643, 10821, 6248, 65422, 52153, 11359, 41943, 17457, 28678, 38880, + 29451, 38125, 41178, 18236, 51380, 12114, 7013, 64643, 65150, 6552, + 11695, 51785, 17857, 41511, 38416, 29174, 38173, 29435, 18124, 41258, + 11938, 51524, 64883, 6805, 10424, 53086, 64361, 7311, 37639, 29921, + 16598, 42800, 17371, 42045, 36874, 30700, 63588, 8066, 11189, 52307, + 20463, 43017, 39998, 31704, 62544, 5046, 10113, 49255, 9356, 50026, + 63325, 4283, 40755, 30933, 19682, 43780, 39209, 32463, 19192, 44318, + 8854, 50544, 61767, 5793, 62026, 5548, 8603, 50813, 18933, 44563, + 39460, 32194, 33089, 26279, 21136, 46454, 15102, 56600, 59695, 3785, + 59938, 3524, 14835, 56853, 20893, 46715, 33356, 26026, 22407, 45153, + 33878, 25520, 60472, 3038, 16361, 55311, 15588, 56066, 61237, 2259, + 34651, 24765, 21642, 45932, 12496, 55094, 58113, 1255, 35695, 27785, + 22718, 48984, 23475, 48213, 34914, 28548, 57356, 2026, 13277, 54331, + 58902, 496, 13767, 53793, 23977, 47695, 36472, 27038, 36213, 27283, + 24228, 47426, 14026, 53548, 58651, 765, + }, + { 0, 29554, 59108, 38294, 53717, 41639, 14129, 17475, 49079, 52421, + 22867, 10785, 28258, 7440, 34950, 64500, 25459, 4097, 34199, 63205, + 45734, 49620, 21570, 10032, 56516, 44982, 14880, 18770, 3345, 32355, + 60405, 39047, 50918, 46484, 8194, 21360, 5939, 25665, 61911, 33445, + 31057, 2595, 40885, 60615, 43140, 56310, 20064, 15634, 42389, 55015, + 17265, 12291, 29760, 1842, 37540, 57814, 6690, 26960, 64710, 36788, + 52215, 47237, 11539, 24161, 37329, 58019, 30517, 1095, 16388, 13174, + 42720, 54674, 11878, 23828, 51330, 48112, 65459, 36033, 6487, 27173, + 62114, 33232, 5190, 26420, 9079, 20485, 50579, 46817, 19733, 15975, + 44017, 55427, 40128, 61362, 31268, 2390, 22327, 9285, 45523, 49825, + 34530, 62864, 24582, 4980, 59520, 39922, 3684, 32022, 14677, 18983, + 57265, 44227, 13380, 18230, 53920, 41426, 58769, 38627, 885, 28679, + 35827, 63617, 27927, 7781, 23078, 10580, 48322, 53168, 16319, 19661, + 55643, 43561, 61034, 40216, 2190, 31740, 32776, 62330, 26348, 5534, + 20957, 8879, 46905, 50251, 23756, 12222, 47656, 51546, 36121, 65131, + 27645, 6287, 58235, 36873, 1439, 30445, 12974, 16860, 54346, 42808, + 63833, 35371, 8125, 27855, 10380, 23550, 52840, 48410, 18158, 13724, + 40970, 54136, 38715, 58441, 29151, 685, 39466, 59736, 31950, 4028, + 19455, 14477, 44315, 56937, 9629, 22255, 50041, 45067, 62536, 34618, + 4780, 25054, 44654, 56604, 18570, 15352, 32699, 3273, 39263, 59949, + 4569, 25259, 63293, 33871, 49164, 45950, 9960, 21914, 52509, 48751, + 11257, 22667, 7368, 28602, 64044, 35166, 29354, 472, 37966, 59196, + 41855, 53261, 17819, 14057, 26760, 7162, 36460, 64798, 47453, 51759, + 24505, 11467, 55103, 42061, 12763, 17065, 1770, 30104, 57358, 37756, + 3067, 30857, 60703, 40557, 55854, 43356, 15562, 20408, 46156, 51006, + 21160, 8666, 26009, 5867, 33661, 61455, + }, + { 0, 14648, 29296, 19272, 58592, 56792, 38544, 44968, 54749, 60645, + 42925, 40597, 12605, 2053, 17229, 31349, 47015, 36511, 50647, 64751, + 21319, 27263, 8503, 6159, 25210, 23362, 4106, 10546, 34458, 49058, + 62698, 52690, 29523, 19051, 291, 14363, 38835, 44683, 58819, 56571, + 42638, 40886, 54526, 60870, 17006, 31574, 12318, 2342, 50420, 64972, + 46724, 36796, 8212, 6444, 21092, 27484, 4393, 10257, 25433, 23137, + 62921, 52465, 34745, 48769, 59046, 57246, 38102, 44526, 582, 15230, + 28726, 18702, 13179, 2627, 16651, 30771, 55195, 61091, 42475, 40147, + 20737, 26681, 9073, 6729, 46561, 36057, 51089, 65193, 34012, 48612, + 63148, 53140, 24636, 22788, 4684, 11124, 38389, 44237, 59269, 57021, + 28949, 18477, 869, 14941, 16424, 30992, 12888, 2912, 42184, 40432, + 54968, 61312, 8786, 7018, 20514, 26906, 50866, 65418, 46274, 36346, + 63375, 52919, 34303, 48327, 4975, 10839, 24863, 22567, 53585, 59497, + 41761, 39449, 13745, 3209, 18369, 32505, 1164, 15796, 30460, 20420, + 57452, 55636, 37404, 43812, 26358, 24526, 5254, 11710, 33302, 47918, + 61542, 51550, 45867, 35347, 49499, 63587, 22475, 28403, 9659, 7299, + 41474, 39738, 53362, 59722, 18146, 32730, 13458, 3498, 30687, 20199, + 1455, 15511, 37695, 43527, 57679, 55415, 5541, 11421, 26581, 24301, + 61765, 51325, 33589, 47629, 49272, 63808, 45576, 35632, 9368, 7584, + 22248, 28624, 14327, 3791, 17799, 31935, 54039, 59951, 41319, 39007, + 57898, 56082, 36954, 43362, 1738, 16370, 29882, 19842, 32848, 47464, + 61984, 51992, 25776, 23944, 5824, 12280, 21901, 27829, 10237, 7877, + 45421, 34901, 49949, 64037, 17572, 32156, 14036, 4076, 41028, 39292, + 53812, 60172, 37241, 43073, 58121, 55857, 30105, 19617, 2025, 16081, + 62211, 51771, 33139, 47179, 6115, 11995, 26003, 23723, 9950, 8166, + 21678, 28054, 49726, 64262, 45134, 35190, + }, + { 0, 7197, 14394, 9255, 28788, 27753, 18510, 21587, 57576, 64757, + 55506, 50383, 37020, 35969, 43174, 46267, 56781, 49616, 58871, 63978, + 44473, 45476, 38275, 35230, 15653, 8504, 1311, 6402, 19793, 20812, + 30059, 26998, 42887, 48026, 40893, 33696, 55283, 52206, 61385, 62420, + 18287, 23410, 32597, 25416, 14107, 11014, 3873, 4924, 31306, 26199, + 17008, 24173, 2622, 5667, 12804, 11801, 39586, 34495, 41624, 48773, + 60118, 63179, 53996, 52977, 21267, 20238, 27433, 30516, 9063, 16250, + 7005, 1856, 46075, 45030, 35777, 38876, 50063, 57234, 64437, 59304, + 36574, 37571, 46820, 43769, 65194, 58039, 50832, 55949, 28214, 29227, + 22028, 18961, 7746, 607, 9848, 14949, 62612, 59529, 52398, 53427, + 34016, 39165, 48346, 41159, 5244, 2145, 11334, 12379, 25608, 30741, + 23602, 16431, 10585, 13636, 4451, 3454, 22829, 17712, 24855, 32010, + 51633, 54700, 61835, 60822, 47557, 42456, 33279, 40418, 42534, 47675, + 40476, 33281, 54866, 51791, 61032, 62069, 18126, 23251, 32500, 25321, + 14010, 10919, 3712, 4765, 31723, 26614, 17361, 24524, 2975, 6018, + 13221, 12216, 39683, 34590, 41785, 48932, 60279, 63338, 54093, 53072, + 417, 7612, 14747, 9606, 29141, 28104, 18927, 22002, 57673, 64852, + 55667, 50542, 37181, 36128, 43271, 46362, 56428, 49265, 58454, 63563, + 44056, 45061, 37922, 34879, 15492, 8345, 1214, 6307, 19696, 20717, + 29898, 26839, 62773, 59688, 52495, 53522, 34113, 39260, 48507, 41318, + 5597, 2496, 11751, 12794, 26025, 31156, 23955, 16782, 10488, 13541, + 4290, 3295, 22668, 17553, 24758, 31915, 51216, 54285, 61482, 60471, + 47204, 42105, 32862, 40003, 21170, 20143, 27272, 30357, 8902, 16091, + 6908, 1761, 45658, 44615, 35424, 38525, 49710, 56883, 64020, 58889, + 36735, 37730, 46917, 43864, 65291, 58134, 50993, 56108, 28567, 29578, + 22445, 19376, 8163, 1022, 10201, 15300, + }, + { 0, 32897, 7455, 40350, 14910, 47807, 10017, 42912, 29820, 62717, + 26979, 59874, 20034, 52931, 21341, 54236, 59640, 26745, 62951, 30054, + 53958, 21063, 53209, 20312, 40068, 7173, 33179, 282, 42682, 9787, + 48037, 15140, 52717, 19820, 53490, 20595, 63443, 30546, 60108, 27213, + 47505, 14608, 42126, 9231, 33711, 814, 40624, 7729, 9493, 42388, + 14346, 47243, 7979, 40874, 564, 33461, 20841, 53736, 19574, 52471, + 27479, 60374, 30280, 63177, 34759, 1862, 39640, 6745, 48633, 15736, + 41190, 8295, 62395, 29498, 61092, 28197, 51589, 18692, 54426, 21531, + 28479, 61374, 29216, 62113, 21761, 54656, 18462, 51359, 6979, 39874, + 1628, 34525, 8573, 41468, 15458, 48355, 18986, 51883, 22325, 55220, + 28692, 61589, 27915, 60810, 15958, 48855, 9033, 41928, 1128, 34025, + 6519, 39414, 41682, 8787, 49101, 16204, 39148, 6253, 34291, 1394, + 54958, 22063, 52145, 19248, 60560, 27665, 61839, 28942, 5011, 37650, + 3724, 36365, 10669, 43308, 13490, 46131, 26607, 59246, 31472, 64113, + 24017, 56656, 16590, 49231, 64363, 31722, 58996, 26357, 49493, 16852, + 56394, 23755, 36631, 3990, 37384, 4745, 46377, 13736, 43062, 10423, + 56958, 24319, 50017, 17376, 58432, 25793, 63839, 31198, 43522, 10883, + 46877, 14236, 36924, 4285, 36131, 3490, 13958, 46599, 11161, 43800, + 3256, 35897, 4519, 37158, 17146, 49787, 24549, 57188, 30916, 63557, + 26075, 58714, 37972, 5333, 35147, 2506, 44650, 12011, 45941, 13300, + 57384, 24745, 64823, 32182, 55830, 23191, 50953, 18312, 31916, 64557, + 25011, 57650, 18066, 50707, 23437, 56076, 2256, 34897, 5583, 38222, + 13038, 45679, 12273, 44912, 22969, 55608, 17574, 50215, 25479, 58118, + 32408, 65049, 11717, 44356, 12506, 45147, 6139, 38778, 2788, 35429, + 45377, 12736, 44126, 11487, 35711, 3070, 38496, 5857, 50493, 17852, + 55330, 22691, 65283, 32642, 57884, 25245, + }, + { 0, 52943, 33155, 20300, 7963, 53716, 40600, 20567, 15926, 61689, + 49077, 29050, 8493, 61410, 41134, 28257, 31852, 45731, 65007, 13088, + 25463, 44472, 58100, 11323, 16986, 35989, 50137, 3350, 23873, 37774, + 56514, 4621, 63704, 13847, 31067, 46996, 59331, 10508, 26176, 43151, + 50926, 2081, 18285, 35234, 55797, 5946, 22646, 38585, 33972, 19067, + 1335, 52216, 39855, 21856, 6700, 54499, 47746, 29773, 15105, 62926, + 42393, 27478, 9242, 60117, 60845, 9058, 27694, 41697, 62134, 15481, + 29493, 48634, 54171, 7508, 21016, 40151, 52352, 591, 19715, 33740, + 37313, 24334, 4162, 56973, 36570, 16405, 3929, 49558, 45047, 24888, + 11892, 57531, 45292, 32291, 12655, 65440, 5493, 56250, 38134, 23097, + 2670, 50337, 35821, 17698, 11075, 58764, 43712, 25615, 13400, 64151, + 46555, 31508, 26905, 42966, 59546, 9813, 30210, 47309, 63361, 14670, + 22319, 39392, 54956, 6243, 18484, 34555, 51639, 1912, 51015, 2440, + 18116, 34827, 55388, 5779, 23007, 38672, 63857, 14270, 30962, 46653, + 58986, 10405, 26601, 43302, 47915, 30180, 15016, 62567, 42032, 27391, + 9651, 60284, 34077, 19410, 1182, 51793, 39430, 21705, 7045, 54602, + 16287, 61776, 48668, 28883, 8324, 61003, 41223, 28616, 425, 53094, + 32810, 20197, 7858, 53373, 40753, 20990, 17395, 36156, 49776, 3263, + 23784, 37415, 56683, 5028, 32197, 45834, 64582, 12937, 25310, 44049, + 58205, 11666, 10986, 58405, 43881, 26022, 13809, 64318, 46194, 31421, + 5340, 55827, 38239, 23440, 3015, 50440, 35396, 17547, 22150, 38985, + 55045, 6602, 18845, 34642, 51230, 1745, 26800, 42623, 59699, 10236, + 30635, 47460, 63016, 14567, 53810, 7421, 21425, 40318, 52521, 998, + 19626, 33381, 60420, 8907, 28039, 41800, 62239, 15824, 29340, 48211, + 44638, 24721, 12253, 57618, 45381, 32650, 12486, 65033, 36968, 24231, + 4587, 57124, 36723, 16828, 3824, 49215, + }, + { 0, 59880, 53197, 9765, 33671, 27247, 19530, 42402, 6931, 62203, + 54494, 15670, 39060, 29052, 22361, 48817, 13862, 57294, 63979, 4099, + 46497, 23625, 31340, 37764, 11573, 50397, 58104, 2832, 44722, 18266, + 24959, 34967, 27724, 34212, 41857, 19049, 61387, 1571, 8198, 51694, + 30559, 40631, 47250, 20858, 62680, 7472, 15125, 54013, 23146, 45954, + 38311, 31823, 55789, 12293, 5664, 65480, 16761, 43153, 36532, 26460, + 49918, 11030, 3379, 58587, 55448, 12656, 5973, 65213, 23327, 45815, + 38098, 32058, 50059, 10851, 3142, 58798, 16396, 43492, 36801, 26153, + 61118, 1878, 8563, 51355, 27961, 34001, 41716, 19228, 62893, 7237, + 14944, 54152, 30250, 40898, 47591, 20495, 46292, 23868, 31513, 37617, + 14163, 57019, 63646, 4470, 44999, 17967, 24586, 35298, 11328, 50600, + 58253, 2661, 33522, 27418, 19775, 42199, 373, 59549, 52920, 10064, + 39393, 28681, 22060, 49092, 6758, 62350, 54699, 15427, 44333, 17605, + 25312, 35592, 11946, 51010, 57703, 2191, 46654, 24534, 31219, 36891, + 13753, 56401, 64116, 5020, 39691, 29411, 21702, 48430, 6284, 61796, + 55105, 16041, 32792, 27120, 20437, 42557, 927, 60023, 52306, 9658, + 49505, 10377, 3756, 59204, 17126, 43790, 36139, 25795, 55922, 13210, + 5567, 64599, 23029, 45085, 38456, 32720, 63303, 7855, 14474, 53602, + 29888, 40232, 47885, 21221, 60500, 1468, 9113, 51825, 28627, 34363, + 40990, 18934, 30133, 40029, 47736, 21392, 63026, 8154, 14847, 53271, + 28326, 34638, 41323, 18563, 60705, 1225, 8940, 51972, 17299, 43643, + 35934, 26038, 49172, 10748, 4057, 58929, 22656, 45416, 38733, 32421, + 56071, 13039, 5322, 64802, 6649, 61457, 54836, 16348, 39550, 29590, + 21939, 48219, 746, 60162, 52519, 9423, 33133, 26757, 20128, 42824, + 12255, 50743, 57362, 2554, 44120, 17840, 25493, 35453, 13516, 56612, + 64257, 4841, 46923, 24227, 30854, 37230, + }, + { 0, 29813, 59626, 40095, 52681, 47548, 9507, 20822, 34703, 62458, + 28517, 6928, 19014, 15923, 41644, 55001, 4867, 26486, 64489, 36764, + 57034, 43711, 13856, 16981, 38028, 57593, 31846, 2067, 22853, 11568, + 45487, 50650, 9734, 21107, 52972, 47769, 60367, 40890, 805, 30544, + 41353, 54780, 18787, 15638, 27712, 6197, 33962, 61663, 13573, 16752, + 56815, 43418, 63692, 36025, 4134, 25683, 45706, 50943, 23136, 11797, + 32579, 2870, 38825, 58332, 19468, 14457, 42214, 53395, 33221, 62896, + 26927, 7514, 52099, 49142, 9065, 22300, 1610, 29247, 61088, 39637, + 24335, 11130, 47077, 50064, 37574, 59059, 31276, 3673, 55424, 44277, + 12394, 17439, 5449, 24892, 64931, 35286, 27146, 7807, 33504, 63125, + 42947, 54198, 20265, 15196, 60805, 39408, 1391, 28954, 8268, 21561, + 51366, 48339, 30985, 3452, 37347, 58774, 46272, 49333, 23594, 10335, + 65158, 35571, 5740, 25113, 13135, 18234, 56229, 45008, 38936, 60525, + 28914, 1159, 21969, 8612, 48443, 51534, 8087, 27618, 63357, 33544, + 53854, 42539, 15028, 20161, 35611, 65390, 25585, 6020, 18130, 12967, + 44600, 55885, 3220, 30945, 58494, 36875, 49501, 46376, 10679, 24002, + 48670, 51819, 22260, 8833, 29655, 1954, 39741, 61256, 14737, 19940, + 53627, 42254, 62552, 32813, 7346, 26823, 44317, 55656, 17911, 12674, + 24788, 5281, 34878, 64587, 10898, 24295, 49784, 46605, 59227, 37678, + 4017, 31684, 54292, 41057, 15614, 18571, 6621, 28072, 61751, 34114, + 21403, 10222, 47985, 52996, 40530, 59943, 30392, 717, 50967, 45922, + 12285, 23432, 2782, 32427, 57908, 38465, 16536, 13549, 43122, 56327, + 36177, 63780, 26043, 4558, 61970, 34407, 6904, 28301, 16347, 19374, + 55089, 41796, 30109, 488, 40311, 59650, 47188, 52257, 20670, 9419, + 57617, 38244, 2555, 32142, 11480, 22701, 50226, 45127, 26270, 4843, + 36468, 64001, 43863, 57122, 17341, 14280, + }, + { 0, 46261, 30071, 49602, 60142, 24155, 40857, 11052, 51649, 32116, + 48310, 2051, 9007, 38810, 22104, 58093, 36767, 15146, 64232, 20061, + 25969, 53700, 4102, 42163, 18014, 62187, 13097, 34716, 44208, 6149, + 55751, 28018, 803, 46998, 30292, 49889, 59853, 23928, 40122, 10255, + 51938, 32343, 49045, 2848, 8204, 38073, 21883, 57806, 36028, 14345, + 63947, 19838, 26194, 53991, 4901, 42896, 17789, 61896, 12298, 33983, + 44947, 6950, 56036, 28241, 1606, 45811, 29489, 51076, 60584, 22557, + 39391, 11626, 53127, 31538, 47856, 3653, 9577, 37340, 20510, 58539, + 35289, 15724, 64686, 18459, 25399, 55170, 5696, 41717, 16408, 62637, + 13679, 33242, 43766, 7747, 57217, 27444, 1381, 45520, 28690, 50343, + 61323, 23358, 39676, 11849, 52388, 30737, 47571, 3430, 9802, 37631, + 21309, 59272, 35578, 15951, 65421, 19256, 24596, 54433, 5475, 41430, + 17211, 63374, 13900, 33529, 43477, 7520, 56482, 26647, 3212, 47161, + 31227, 52558, 58978, 21207, 37653, 10144, 50509, 29176, 45114, 1167, + 12195, 39702, 23252, 61025, 33555, 14246, 63076, 17105, 27133, 56648, + 7306, 43071, 19154, 65127, 16293, 35600, 41020, 5257, 54603, 25086, + 4015, 47898, 31448, 52845, 58689, 20980, 36918, 9347, 50798, 29403, + 45849, 1964, 11392, 38965, 23031, 60738, 32816, 13445, 62791, 16882, + 27358, 56939, 8105, 43804, 18929, 64836, 15494, 34867, 41759, 6058, + 54888, 25309, 2762, 48767, 32701, 51976, 57380, 21649, 38227, 8678, + 49931, 30654, 46716, 713, 10725, 40272, 23698, 59431, 34133, 12768, + 61474, 17559, 28603, 56078, 6860, 44665, 19604, 63521, 14819, 36182, + 42618, 4815, 54029, 26552, 2537, 48476, 31902, 51243, 58119, 22450, + 38512, 8901, 49192, 29853, 46431, 490, 10950, 40563, 24497, 60164, + 34422, 12995, 62209, 18356, 27800, 55341, 6639, 44378, 20407, 64258, + 15040, 36469, 42329, 4588, 53294, 25755, + }, + { 0, 54485, 46519, 24930, 30579, 41894, 49860, 5649, 61158, 14899, + 23377, 36740, 39317, 19776, 11298, 63735, 49617, 5380, 29798, 41139, + 46754, 25207, 789, 55232, 12087, 64482, 39552, 20053, 22596, 35985, + 60915, 14630, 40895, 19306, 10760, 65245, 59596, 15385, 23931, 35246, + 29017, 42380, 50414, 4155, 1578, 54015, 45981, 26440, 24174, 35515, + 60377, 16140, 10525, 64968, 40106, 18559, 45192, 25693, 1343, 53738, + 51195, 4910, 29260, 42649, 9059, 63414, 38612, 16897, 21520, 32965, + 57767, 13682, 52613, 6480, 30770, 44263, 47862, 28195, 3905, 56212, + 58034, 13927, 22277, 33744, 38337, 16660, 8310, 62627, 3156, 55425, + 47587, 27958, 31527, 45042, 52880, 6725, 48348, 26633, 2411, 56766, + 52143, 8058, 32280, 43725, 21050, 34543, 59277, 13144, 9545, 61852, + 37118, 17451, 32013, 43480, 51386, 7279, 2686, 57003, 49097, 27420, + 37867, 18238, 9820, 62089, 58520, 12365, 20783, 34298, 18118, 37395, + 62321, 10148, 12725, 58720, 33794, 20695, 43040, 31989, 7575, 51522, + 57171, 2950, 27364, 48689, 34583, 21442, 12960, 58997, 61540, 9393, + 17875, 37126, 27121, 48420, 56390, 2195, 7810, 51799, 43829, 32736, + 55673, 3500, 27854, 47131, 44554, 31455, 7101, 53096, 14239, 58186, + 33320, 22269, 16620, 37945, 62811, 8590, 6312, 52349, 44319, 31178, + 28635, 47886, 55916, 3769, 63054, 8859, 17401, 38700, 33085, 21992, + 13450, 57439, 26021, 45424, 53266, 1223, 4822, 50691, 42849, 29620, + 35651, 24470, 16116, 59937, 64560, 10469, 18823, 40274, 42100, 28833, + 4547, 50454, 54023, 2002, 26288, 45669, 19090, 40519, 65317, 11248, + 15841, 59700, 34902, 23683, 64026, 11983, 20397, 39800, 36201, 22972, + 14558, 60427, 5372, 49193, 41291, 30110, 25487, 46938, 54840, 749, + 15307, 61214, 36476, 23209, 19640, 39021, 63759, 11738, 54573, 504, + 24730, 46159, 41566, 30347, 6121, 49980, + }, + { 0, 58597, 54743, 12594, 47027, 21334, 25188, 34433, 29563, 38814, + 42668, 16969, 50376, 8237, 4383, 62970, 59126, 531, 13089, 55236, + 20805, 46496, 33938, 24695, 38285, 29032, 16474, 42175, 8766, 50907, + 63465, 4876, 53745, 13588, 1062, 57539, 26178, 33447, 45973, 22384, + 41610, 18031, 30557, 37816, 5433, 61916, 49390, 9227, 14087, 54242, + 58064, 1589, 32948, 25681, 21859, 45446, 17532, 41113, 37291, 30030, + 62415, 5930, 9752, 49917, 49151, 23322, 27176, 36557, 2124, 60585, + 56731, 14718, 52356, 10337, 6483, 64950, 31543, 40914, 44768, 18949, + 22793, 48620, 36062, 26683, 61114, 2655, 15213, 57224, 10866, 52887, + 65445, 6976, 40385, 31012, 18454, 44275, 28174, 35563, 48089, 24380, + 55741, 15704, 3178, 59535, 7541, 63888, 51362, 11335, 43718, 20003, + 32529, 39924, 35064, 27677, 23855, 47562, 16203, 56238, 60060, 3705, + 64387, 8038, 11860, 51889, 19504, 43221, 39399, 32002, 25571, 34566, + 46644, 21201, 54352, 12469, 391, 58722, 4248, 62589, 50511, 8618, + 42795, 17358, 29436, 38425, 34069, 25072, 20674, 46119, 12966, 54851, + 59249, 916, 63086, 4747, 9145, 51036, 16861, 42296, 37898, 28911, + 45586, 22263, 26565, 33568, 1441, 57668, 53366, 13459, 49513, 9612, + 5310, 61531, 30426, 37439, 41741, 18408, 21732, 45057, 33075, 26070, + 58199, 1970, 13952, 53861, 10143, 50042, 62024, 5805, 36908, 29897, + 17915, 41246, 56348, 14585, 2507, 60718, 27567, 36682, 48760, 23197, + 44903, 19330, 31408, 40533, 6356, 64561, 52483, 10726, 15082, 56847, + 61245, 3032, 36185, 27068, 22670, 48235, 18833, 44404, 40006, 30883, + 65058, 6855, 11253, 53008, 3565, 59656, 55354, 15583, 47710, 24251, + 28553, 35692, 32406, 39539, 43841, 20388, 51493, 11712, 7410, 63511, + 60187, 4094, 16076, 55849, 23720, 47181, 35199, 28058, 39008, 31877, + 19895, 43346, 12243, 52022, 64004, 7905, + }, + { 0, 64765, 58855, 6426, 55251, 11054, 12852, 52937, 46011, 20294, + 22108, 43681, 25704, 39061, 33167, 32114, 31595, 34710, 40588, 25201, + 44216, 20549, 18783, 46498, 51408, 13357, 11575, 53706, 7939, 58366, + 64228, 1561, 63190, 2603, 4913, 61388, 8453, 56824, 50402, 14367, + 17773, 47504, 41098, 23671, 37566, 28227, 30553, 35748, 36285, 28992, + 26714, 38055, 23150, 42643, 49033, 17268, 15878, 49915, 56289, 10012, + 59861, 5416, 3122, 61647, 61873, 3404, 5206, 59563, 9826, 55967, + 50053, 16248, 16906, 48887, 42989, 23312, 38361, 26916, 28734, 36035, + 35546, 30247, 28477, 37824, 23817, 41460, 47342, 17427, 14689, 50588, + 56454, 8315, 61106, 4687, 2901, 63400, 1895, 64410, 57984, 7805, + 53428, 11337, 13651, 51630, 46300, 18465, 20795, 44486, 25359, 40946, + 34536, 31253, 31756, 33009, 39403, 25878, 43999, 22306, 20024, 45765, + 53175, 13130, 10832, 54957, 6244, 58521, 64899, 382, 65407, 898, + 6808, 58981, 10412, 54353, 52555, 12726, 19652, 45113, 43299, 21982, + 39703, 26602, 32496, 33293, 33812, 30953, 25075, 40206, 21447, 44858, + 46624, 19165, 14255, 52050, 53832, 11957, 57468, 7297, 1435, 63846, + 2473, 62804, 60494, 4275, 56954, 8839, 15261, 51040, 47634, 18159, + 24565, 41736, 28097, 37180, 34854, 29915, 29378, 36415, 38693, 27608, + 42257, 23020, 16630, 48139, 49529, 15748, 9374, 55395, 5802, 59991, + 62285, 4016, 3790, 62003, 60201, 6100, 55581, 9696, 15610, 49159, + 48501, 16776, 22674, 42095, 27302, 38491, 36673, 29628, 30117, 35160, + 36930, 27839, 41590, 24203, 18321, 47980, 50718, 15075, 9209, 57092, + 4557, 60720, 62506, 2263, 63512, 1253, 7679, 57602, 12235, 54070, + 51756, 14033, 19363, 46942, 44612, 21177, 40048, 24717, 31127, 34154, + 33651, 32654, 26260, 39529, 21664, 43101, 45383, 19898, 12488, 52277, + 54575, 10706, 59163, 7142, 764, 65025, + }, + { 0, 61681, 65023, 3342, 59363, 5906, 6684, 60141, 54235, 9002, + 11812, 57045, 13368, 50377, 51655, 14646, 48043, 19290, 18004, 46757, + 23624, 44217, 41399, 20806, 26736, 39041, 38287, 25982, 36755, 32610, + 29292, 33437, 27467, 39866, 38580, 26181, 36008, 31833, 29015, 33190, + 47248, 18529, 17775, 46494, 24435, 44930, 41612, 21117, 53472, 8209, + 11551, 56814, 14083, 51186, 51964, 14861, 827, 62410, 65220, 3637, + 58584, 5161, 6439, 59862, 54934, 9831, 11113, 56216, 12661, 49540, + 52362, 15483, 1357, 62908, 63666, 2115, 58030, 4703, 8017, 61344, + 27965, 40396, 37058, 24627, 35550, 31279, 30497, 34768, 48870, 19991, + 17177, 46056, 22789, 43508, 42234, 21515, 48605, 19756, 16418, 45267, + 23102, 43727, 42945, 22320, 28166, 40695, 37881, 25352, 35301, 30996, + 29722, 34027, 1654, 63111, 64393, 2936, 57749, 4452, 7274, 60571, + 54701, 9564, 10322, 55459, 12878, 49855, 53169, 16192, 45361, 16832, + 19662, 48191, 22226, 42531, 43821, 23516, 25322, 37403, 40725, 28644, + 34057, 30200, 30966, 34823, 2714, 64107, 63333, 1940, 60793, 7560, + 4230, 57463, 55617, 10672, 9406, 54351, 16034, 52819, 50013, 13228, + 55930, 10891, 10117, 55156, 15769, 52584, 49254, 12439, 2465, 63824, + 62558, 1199, 60994, 7859, 5053, 58188, 25041, 37152, 39982, 27871, + 34354, 30403, 31693, 35644, 45578, 17147, 20469, 48900, 21993, 42264, + 43030, 22759, 26535, 38742, 39512, 27305, 32836, 28853, 32187, 36170, + 46204, 17549, 18819, 47474, 21407, 41838, 44640, 24209, 56332, 11517, + 8691, 53506, 15343, 51998, 50704, 14049, 4055, 65318, 61992, 729, + 59444, 6341, 5579, 58682, 3308, 64541, 61715, 482, 60175, 7166, + 5872, 58881, 57143, 12230, 8904, 53817, 14548, 51237, 50475, 13786, + 46919, 18358, 19128, 47689, 20644, 41045, 44379, 23978, 25756, 37997, + 39267, 27026, 33663, 29582, 32384, 36465, + }, + { 0, 63223, 61939, 1796, 65531, 2316, 3592, 63743, 58347, 5404, + 4632, 58607, 7184, 60135, 60899, 6932, 56267, 11580, 10808, 56527, + 9264, 53959, 54723, 9012, 14368, 52951, 51667, 16164, 51163, 12588, + 13864, 49375, 43915, 23932, 23160, 44175, 21616, 41607, 42371, 21364, + 18528, 48791, 47507, 20324, 47003, 16748, 18024, 45215, 28736, 34487, + 33203, 30532, 36795, 31052, 32328, 35007, 37803, 25948, 25176, 38063, + 27728, 39591, 40355, 27476, 19211, 48636, 47864, 19471, 46320, 16903, + 17667, 46068, 43232, 24087, 22803, 45028, 22299, 41452, 42728, 20511, + 37056, 26167, 24883, 38852, 28475, 39372, 40648, 26687, 29483, 34268, + 33496, 29743, 36048, 31271, 32035, 35796, 57472, 5751, 4467, 59268, + 8059, 59788, 61064, 6271, 875, 62876, 62104, 1135, 64656, 2663, + 3427, 64404, 15179, 52668, 51896, 15439, 50352, 12871, 13635, 50100, + 55456, 11863, 10579, 57252, 10075, 53676, 54952, 8287, 38422, 24801, + 26597, 37138, 27117, 40730, 38942, 28393, 30205, 33546, 33806, 29433, + 35334, 31985, 31733, 36098, 19933, 47914, 48174, 19161, 45606, 17617, + 17365, 46370, 44598, 22721, 24517, 43314, 20941, 42810, 41022, 22217, + 15773, 52074, 52334, 15001, 49766, 13457, 13205, 50530, 56950, 10369, + 12165, 55666, 8589, 55162, 53374, 9865, 58966, 4257, 6053, 57682, + 6573, 61274, 59486, 7849, 1469, 62282, 62542, 697, 64070, 3249, + 2997, 64834, 56605, 11242, 11502, 55833, 8934, 54289, 54037, 9698, + 16118, 51201, 52997, 14834, 49421, 14330, 12542, 50697, 1750, 61473, + 63269, 466, 63789, 4058, 2270, 65065, 58685, 5066, 5326, 57913, + 6854, 60465, 60213, 7618, 30358, 32865, 34661, 29074, 35181, 32666, + 30878, 36457, 38269, 25482, 25742, 37497, 27270, 40049, 39797, 28034, + 44381, 23466, 23726, 43609, 21158, 42065, 41813, 21922, 20150, 47169, + 48965, 18866, 45389, 18362, 16574, 46665, + }, + { 0, 62964, 63477, 513, 62455, 1539, 1026, 61942, 64499, 3591, + 3078, 63986, 2052, 65008, 65521, 2565, 60411, 7695, 7182, 59898, + 6156, 60920, 61433, 6669, 4104, 58876, 59389, 4617, 58367, 5643, + 5130, 57854, 52203, 15903, 15390, 51690, 14364, 52712, 53225, 14877, + 12312, 50668, 51181, 12825, 50159, 13851, 13338, 49646, 8208, 54756, + 55269, 8721, 54247, 9747, 9234, 53734, 56291, 11799, 11286, 55778, + 10260, 56800, 57313, 10773, 35787, 32319, 31806, 35274, 30780, 36296, + 36809, 31293, 28728, 34252, 34765, 29241, 33743, 30267, 29754, 33230, + 24624, 38340, 38853, 25137, 37831, 26163, 25650, 37318, 39875, 28215, + 27702, 39362, 26676, 40384, 40897, 27189, 16416, 46548, 47061, 16929, + 46039, 17955, 17442, 45526, 48083, 20007, 19494, 47570, 18468, 48592, + 49105, 18981, 43995, 24111, 23598, 43482, 22572, 44504, 45017, 23085, + 20520, 42460, 42973, 21033, 41951, 22059, 21546, 41438, 2955, 65151, + 64638, 2442, 63612, 3464, 3977, 64125, 61560, 1420, 1933, 62073, + 911, 63099, 62586, 398, 57456, 5508, 6021, 57969, 4999, 58995, + 58482, 4486, 7043, 61047, 60534, 6530, 59508, 7552, 8065, 60021, + 49248, 13716, 14229, 49761, 13207, 50787, 50274, 12694, 15251, 52839, + 52326, 14738, 51300, 15760, 16273, 51813, 11163, 56943, 56430, 10650, + 55404, 11672, 12185, 55917, 53352, 9628, 10141, 53865, 9119, 54891, + 54378, 8606, 32832, 30132, 30645, 33345, 29623, 34371, 33858, 29110, + 31667, 36423, 35910, 31154, 34884, 32176, 32689, 35397, 27579, 40527, + 40014, 27066, 38988, 28088, 28601, 39501, 36936, 26044, 26557, 37449, + 25535, 38475, 37962, 25022, 19371, 48735, 48222, 18858, 47196, 19880, + 20393, 47709, 45144, 17836, 18349, 45657, 17327, 46683, 46170, 16814, + 41040, 21924, 22437, 41553, 21415, 42579, 42066, 20902, 23459, 44631, + 44118, 22946, 43092, 23968, 24481, 43605, + }, + { 0, 31355, 62710, 36493, 62961, 36746, 263, 31612, 63487, 36228, + 777, 31090, 526, 30837, 63224, 35971, 62435, 35224, 1813, 32110, + 1554, 31849, 62180, 34975, 1052, 32359, 61674, 35473, 61933, 35734, + 1307, 32608, 64475, 33184, 3885, 30038, 3626, 29777, 64220, 32935, + 3108, 30303, 63698, 33449, 63957, 33710, 3363, 30552, 2104, 29251, + 64718, 34485, 64969, 34738, 2367, 29508, 65479, 34236, 2865, 29002, + 2614, 28749, 65216, 33979, 60331, 37328, 8029, 25894, 7770, 25633, + 60076, 37079, 7252, 26159, 59554, 37593, 59813, 37854, 7507, 26408, + 6216, 25139, 60606, 38597, 60857, 38850, 6479, 25396, 61367, 38348, + 6977, 24890, 6726, 24637, 61104, 38091, 4208, 27147, 58502, 40701, + 58753, 40954, 4471, 27404, 59279, 40436, 4985, 26882, 4734, 26629, + 59016, 40179, 58259, 39400, 5989, 27934, 5730, 27673, 58004, 39151, + 5228, 28183, 57498, 39649, 57757, 39910, 5483, 28432, 52043, 45360, + 16317, 17862, 16058, 17601, 51788, 45111, 15540, 18127, 51266, 45625, + 51525, 45886, 15795, 18376, 14504, 17107, 52318, 46629, 52569, 46882, + 14767, 17364, 53079, 46380, 15265, 16858, 15014, 16605, 52816, 46123, + 12432, 19179, 50278, 48669, 50529, 48922, 12695, 19436, 51055, 48404, + 13209, 18914, 12958, 18661, 50792, 48147, 50035, 47368, 14213, 19966, + 13954, 19705, 49780, 47119, 13452, 20215, 49274, 47617, 49533, 47878, + 13707, 20464, 8416, 23195, 54294, 44653, 54545, 44906, 8679, 23452, + 55071, 44388, 9193, 22930, 8942, 22677, 54808, 44131, 54019, 43384, + 10229, 23950, 9970, 23689, 53764, 43135, 9468, 24199, 53258, 43633, + 53517, 43894, 9723, 24448, 56123, 41280, 12237, 21942, 11978, 21681, + 55868, 41031, 11460, 22207, 55346, 41545, 55605, 41806, 11715, 22456, + 10456, 21155, 56366, 42581, 56617, 42834, 10719, 21412, 57127, 42332, + 11217, 20906, 10966, 20653, 56864, 42075, + }, + { 0, 46002, 31609, 51403, 63218, 17728, 36235, 15929, 61945, 16971, + 35456, 14642, 1803, 46265, 31858, 53184, 65519, 19549, 33942, 14116, + 2333, 47791, 29284, 49622, 3606, 48548, 30063, 50909, 63716, 19286, + 33693, 12335, 58307, 20593, 39098, 11016, 5425, 42627, 28232, 56826, + 4666, 41352, 26947, 56049, 58568, 22394, 40881, 11267, 7212, 44958, + 26453, 54503, 60126, 22892, 37287, 8725, 60885, 24167, 38572, 9502, + 6951, 43157, 24670, 54252, 56219, 26665, 41186, 4944, 11625, 40667, + 22032, 58786, 10850, 39376, 20763, 58025, 56464, 28450, 42985, 5211, + 9332, 38854, 24333, 60607, 53894, 24884, 43519, 6733, 54669, 26175, + 44788, 7494, 9087, 37069, 22534, 60340, 14424, 35818, 17185, 61587, + 52906, 32024, 46547, 1633, 51617, 31251, 45784, 362, 16211, 36065, + 17450, 63384, 51127, 29701, 48334, 3964, 12613, 33527, 19004, 63886, + 13902, 34300, 19767, 65157, 49340, 29454, 48069, 2167, 43819, 6297, + 53330, 25568, 24025, 61035, 9888, 38162, 23250, 59744, 8619, 37401, + 44064, 8082, 55129, 25835, 21700, 59254, 12221, 39951, 41526, 4484, + 55631, 27389, 42301, 5775, 56900, 28150, 21455, 57469, 10422, 39684, + 18664, 64346, 13201, 32803, 48666, 3496, 50531, 30417, 47377, 2723, + 49768, 29146, 20451, 64593, 13466, 34600, 46855, 1205, 52350, 32716, + 16885, 62023, 14988, 35134, 18174, 62796, 15751, 36405, 45068, 958, + 52085, 30919, 28848, 49922, 3017, 47227, 34370, 13808, 64827, 20105, + 33097, 13051, 64048, 18818, 30651, 50185, 3266, 49008, 36703, 15597, + 62502, 18324, 31149, 51743, 724, 45414, 32422, 52500, 1503, 46701, + 34900, 15334, 62253, 16543, 37747, 8385, 59402, 23480, 25985, 54835, + 7928, 44362, 25226, 53560, 6643, 43585, 38008, 10186, 61185, 23731, + 27804, 57134, 6117, 42071, 39534, 10716, 57623, 21157, 40293, 11991, + 58908, 21934, 27543, 55333, 4334, 41820, + }, + { 0, 22872, 45744, 60392, 31101, 8229, 52173, 37525, 62202, 43938, + 16458, 6418, 35719, 53983, 14647, 24687, 63977, 41137, 19289, 4609, + 32916, 55756, 12836, 27516, 2835, 21067, 47523, 57595, 29294, 11062, + 49374, 39302, 61391, 46743, 23935, 1063, 38578, 53226, 9218, 32090, + 7477, 17517, 44933, 63197, 25672, 15632, 55032, 36768, 5670, 20350, + 42134, 64974, 28507, 13827, 56811, 33971, 58588, 48516, 22124, 3892, + 40353, 50425, 12049, 30281, 50051, 39643, 28979, 10347, 47870, 58278, + 2126, 20758, 12665, 26657, 33737, 55953, 18436, 4444, 64180, 41964, + 14954, 25394, 35034, 53634, 17175, 6735, 61863, 43263, 51344, 37320, + 31264, 9080, 45549, 59573, 861, 23045, 11340, 29972, 40700, 51108, + 21809, 3177, 59265, 48857, 57014, 34798, 27654, 13662, 42955, 65171, + 5499, 19491, 54693, 36093, 26389, 15949, 44248, 62848, 7784, 18224, + 10079, 32263, 38383, 52407, 24098, 1914, 60562, 46538, 39707, 49731, + 10667, 28915, 57958, 47934, 20694, 2446, 27105, 12473, 56145, 33289, + 4252, 18884, 41516, 64372, 25330, 15274, 53314, 35098, 7055, 17111, + 43327, 61543, 36872, 51536, 8888, 31712, 59765, 45101, 23493, 669, + 29908, 11660, 50788, 40764, 3497, 21745, 48921, 58945, 34350, 57206, + 13470, 28102, 65363, 42507, 19939, 5307, 36157, 54373, 16269, 26325, + 62528, 44312, 18160, 8104, 32711, 9887, 52599, 37935, 1722, 24546, + 46090, 60754, 22680, 448, 59944, 45936, 8677, 30909, 37717, 51725, + 43618, 62266, 6354, 16778, 54047, 35399, 25007, 14583, 41329, 63529, + 5057, 19097, 55308, 33108, 27324, 13284, 21387, 2771, 57659, 47203, + 10998, 29614, 38982, 49438, 46935, 60943, 1511, 23743, 52778, 38770, + 31898, 9666, 17837, 7413, 63261, 44613, 15568, 25992, 36448, 55096, + 20158, 6118, 64526, 42326, 14275, 28315, 34163, 56363, 48196, 58652, + 3828, 22444, 50489, 40033, 30601, 11985, + }, + { 0, 11309, 22618, 29815, 45236, 40089, 59630, 50371, 32117, 20824, + 9519, 2306, 52673, 57836, 38299, 47542, 64234, 54983, 41648, 36509, + 19038, 26227, 4612, 15913, 34719, 43954, 57285, 62440, 14123, 6918, + 28529, 17244, 59849, 50660, 45459, 40382, 22909, 30032, 295, 11530, + 38076, 47249, 52454, 57547, 9224, 2085, 31826, 20607, 4899, 16142, + 19321, 26452, 41879, 36794, 64461, 55264, 28246, 17019, 13836, 6689, + 57058, 62159, 34488, 43669, 53135, 58274, 38869, 48120, 32571, 21270, + 10081, 2892, 45818, 40663, 60064, 50829, 590, 11875, 23060, 30265, + 13669, 6472, 27967, 16658, 34257, 43516, 56715, 61862, 18448, 25661, + 4170, 15463, 63652, 54409, 41214, 36051, 9798, 2667, 32284, 21041, + 38642, 47839, 52904, 57989, 23347, 30494, 873, 12100, 60295, 51114, + 46045, 40944, 56492, 61569, 34038, 43227, 27672, 16437, 13378, 6255, + 41433, 36340, 63875, 54702, 4461, 15680, 18743, 25882, 33539, 44846, + 56153, 63348, 13239, 8090, 27629, 18368, 65142, 53851, 42540, 35329, + 20162, 25327, 5784, 15029, 31209, 21956, 8627, 3486, 51549, 58736, + 37127, 48426, 1180, 10417, 23750, 28907, 46120, 38917, 60530, 49247, + 27338, 18151, 12944, 7869, 55934, 63059, 33316, 44553, 6079, 15250, + 20453, 25544, 42763, 35622, 65361, 54140, 36896, 48141, 51322, 58455, + 8340, 3257, 30926, 21731, 60757, 49528, 46351, 39202, 24033, 29132, + 1467, 10646, 19596, 24737, 5334, 14587, 64568, 53269, 42082, 34895, + 12793, 7636, 27043, 17806, 33101, 44384, 55575, 62778, 46694, 39499, + 60988, 49681, 1746, 11007, 24200, 29349, 51987, 59198, 37705, 48996, + 31655, 22410, 9213, 4048, 42309, 35176, 64799, 53554, 5617, 14812, + 19883, 24966, 55344, 62493, 32874, 44103, 26756, 17577, 12510, 7411, + 24495, 29570, 2037, 11224, 61211, 49974, 46913, 39788, 8922, 3831, + 31360, 22189, 37486, 48707, 51764, 58905, + }, + { 0, 39065, 11567, 46518, 23134, 49863, 30577, 61416, 46268, 11301, + 39315, 266, 61154, 30331, 50125, 23380, 30053, 60924, 22602, 49363, + 12091, 47010, 532, 39565, 49625, 22848, 60662, 29807, 39815, 798, + 46760, 11825, 60106, 29267, 51173, 24444, 45204, 10253, 40379, 1314, + 24182, 50927, 29529, 60352, 1064, 40113, 10503, 45470, 40879, 1846, + 45696, 10777, 50673, 23912, 59614, 28743, 11027, 45962, 1596, 40613, + 29005, 59860, 23650, 50427, 51593, 20752, 58534, 31807, 37847, 2894, + 48888, 9825, 32053, 58796, 20506, 51331, 10091, 49138, 2628, 37597, + 48364, 9333, 37315, 2394, 59058, 32299, 52125, 21252, 2128, 37065, + 9599, 48614, 21006, 51863, 32545, 59320, 9027, 48090, 3692, 38645, + 31005, 57732, 21554, 52395, 38911, 3942, 47824, 8777, 52641, 21816, + 57486, 30743, 22054, 52927, 31497, 58256, 3192, 38113, 8535, 47566, + 58010, 31235, 53173, 22316, 47300, 8285, 38379, 3442, 36623, 6038, + 41504, 15033, 54609, 19912, 63614, 24807, 15283, 41770, 5788, 36357, + 25069, 63860, 19650, 54363, 64106, 25331, 55109, 20444, 41012, 14509, + 36123, 5506, 20182, 54863, 25593, 64352, 5256, 35857, 14759, 41278, + 26053, 64860, 18666, 53363, 16283, 42754, 4788, 35373, 53625, 18912, + 64598, 25807, 35623, 5054, 42504, 16017, 4256, 34873, 15759, 42262, + 19198, 53863, 26577, 65352, 42012, 15493, 35123, 4522, 65090, 26331, + 54125, 19444, 18054, 56863, 27561, 62256, 7384, 33857, 12791, 43374, + 62010, 27299, 57109, 18316, 43108, 12541, 34123, 7634, 13283, 43898, + 7884, 34389, 27069, 61732, 17554, 56331, 34655, 8134, 43632, 13033, + 56577, 17816, 61486, 26807, 44108, 13525, 33123, 6650, 62994, 28299, + 56125, 17316, 6384, 32873, 13791, 44358, 17070, 55863, 28545, 63256, + 55593, 16816, 62470, 27807, 33655, 7150, 44632, 14017, 28053, 62732, + 16570, 55331, 14283, 44882, 6884, 33405, + }, + { 0, 49859, 39323, 23384, 12075, 60904, 46768, 29811, 24150, 40085, + 51149, 1294, 29053, 46014, 59622, 10789, 48300, 32367, 9527, 59380, + 37767, 20804, 2588, 51423, 58106, 8249, 31585, 47522, 52689, 3858, + 21578, 38537, 25925, 42886, 64734, 15901, 19054, 34989, 54261, 4406, + 15123, 63952, 41608, 24651, 5176, 55035, 36259, 20320, 55785, 6954, + 16498, 33457, 63170, 13313, 28505, 44442, 34751, 17788, 7716, 56551, + 43156, 27223, 12559, 62412, 51850, 2121, 21265, 37330, 58785, 10082, + 31802, 48889, 38108, 22047, 3399, 53124, 48119, 31028, 8812, 57519, + 30246, 46309, 61373, 11646, 22797, 39886, 49302, 597, 10352, 60083, + 45547, 29480, 1883, 50584, 40640, 23555, 45007, 27916, 13908, 62615, + 32996, 16935, 6527, 56252, 61849, 13146, 26626, 43713, 57010, 7281, + 18217, 34282, 4963, 53664, 35576, 18491, 15432, 65163, 42451, 26384, + 19765, 36854, 54446, 5741, 25118, 41181, 64389, 14662, 35081, 19402, + 4242, 53841, 42530, 25825, 16313, 64890, 55135, 5532, 20164, 35847, + 63604, 15031, 25071, 41772, 13733, 63334, 44094, 28413, 6798, 55373, + 33557, 16854, 27635, 43312, 62056, 12459, 17624, 34331, 56643, 8064, + 60492, 11919, 30167, 46868, 50023, 420, 23292, 38975, 45594, 28889, + 11137, 59714, 40241, 24562, 1194, 50793, 20704, 37411, 51579, 3000, + 32715, 48392, 58960, 9363, 3766, 52341, 38701, 21998, 8605, 58206, + 47110, 31429, 17283, 33088, 55832, 6363, 27816, 44651, 62771, 14320, + 7637, 57110, 33870, 18061, 13054, 61501, 43877, 27046, 65327, 15852, + 26292, 42103, 53252, 4807, 18847, 35676, 41337, 25530, 14562, 64033, + 36434, 19601, 6089, 54538, 9926, 58373, 48989, 32158, 2541, 52014, + 36982, 21173, 30864, 47699, 57611, 9160, 22459, 38264, 52768, 3299, + 39530, 22697, 1009, 49458, 46401, 30594, 11482, 60953, 50236, 1791, + 23975, 40804, 60183, 10708, 29324, 45135, + }, + { 0, 61422, 50113, 11311, 39839, 29809, 22622, 47024, 11043, 50381, + 59618, 1804, 45244, 24402, 29565, 40083, 22086, 47528, 38279, 31337, + 52697, 8759, 3608, 57846, 32101, 37515, 48804, 20810, 59130, 2324, + 9531, 51925, 44172, 17250, 28493, 32931, 14099, 55549, 62674, 6972, + 34735, 26689, 17518, 43904, 7216, 62430, 57329, 12319, 64202, 5412, + 14603, 55013, 24917, 36539, 41620, 19834, 53737, 15879, 4648, 64966, + 19062, 42392, 35255, 26201, 17669, 43755, 34500, 26922, 56986, 12660, + 7515, 62133, 28198, 33224, 44519, 16905, 62905, 6743, 13944, 55702, + 4931, 64685, 53378, 16236, 35036, 26418, 19229, 42227, 14432, 55182, + 64417, 5199, 41983, 19473, 24638, 36816, 59785, 1639, 10824, 50598, + 29206, 40440, 45527, 24121, 49834, 11588, 363, 61061, 22837, 46811, + 39668, 29978, 49103, 20513, 31758, 37856, 9296, 52158, 59281, 2175, + 38124, 31490, 22317, 47299, 3955, 57501, 52402, 9052, 35338, 26084, + 18891, 42533, 4501, 65147, 53844, 15802, 41257, 20167, 25320, 36102, + 15030, 54616, 63863, 5785, 56396, 13218, 8077, 61539, 18387, 43069, + 33810, 27644, 63343, 6273, 13486, 56128, 27888, 33566, 44849, 16607, + 9862, 51560, 58695, 2729, 48409, 21239, 32472, 37174, 3493, 57931, + 52836, 8586, 38458, 31188, 22011, 47637, 28864, 40750, 45825, 23791, + 60255, 1201, 10398, 51056, 23523, 46093, 38946, 30668, 49276, 12178, + 957, 60499, 53007, 8417, 3278, 58144, 21648, 47998, 38737, 30911, + 58412, 3010, 10221, 51203, 32691, 36957, 48242, 21404, 39241, 30375, + 23176, 46438, 726, 60728, 49431, 12025, 45674, 23940, 29099, 40517, + 10741, 50715, 59956, 1498, 25475, 35949, 41026, 20396, 63516, 6130, + 15325, 54323, 18592, 42830, 35681, 25743, 54079, 15569, 4350, 65296, + 13765, 55851, 62980, 6634, 44634, 16820, 28059, 33397, 7910, 61704, + 56615, 13001, 34169, 27287, 18104, 43350, + }, + { 0, 30582, 61164, 39322, 49605, 46771, 12073, 22623, 40855, 59617, + 29051, 1549, 24146, 10532, 45246, 51144, 9011, 21573, 52703, 47785, + 58102, 38272, 3098, 31596, 48292, 52178, 21064, 9534, 32097, 2583, + 37773, 58619, 18022, 12560, 43146, 57340, 34723, 61653, 26959, 7737, + 55793, 44679, 14109, 16491, 6196, 28482, 63192, 33198, 25941, 4643, + 35769, 64719, 42128, 54246, 19068, 15626, 64194, 36276, 5166, 25432, + 15111, 19569, 54763, 41629, 36044, 64442, 25120, 5462, 19721, 14975, + 41957, 54419, 4955, 25645, 64951, 35521, 53918, 42472, 15474, 19204, + 45055, 55433, 16659, 13925, 28218, 6476, 32982, 63392, 12392, 18206, + 56964, 43506, 61869, 34523, 8001, 26679, 51882, 48604, 9286, 21296, + 2927, 31769, 58755, 37621, 21821, 8779, 48081, 52391, 38136, 58254, + 31252, 3426, 59801, 40687, 1909, 28675, 10332, 24362, 50864, 45510, + 30222, 376, 39138, 61332, 47051, 49341, 22823, 11857, 1413, 29427, + 60265, 39967, 50240, 45878, 10924, 24026, 39442, 60772, 29950, 904, + 23511, 11425, 46395, 49741, 9910, 20928, 51290, 48940, 59251, 36869, + 2463, 32489, 47393, 52823, 22477, 8379, 30948, 3986, 38408, 57726, + 17379, 13461, 44303, 55929, 33318, 62800, 27850, 7100, 56436, 43778, + 12952, 17902, 7601, 27335, 62301, 33835, 24784, 6054, 36412, 63818, + 41237, 54883, 20473, 14479, 65351, 34865, 4523, 26333, 16002, 18932, + 53358, 42776, 35145, 65087, 26533, 4307, 18572, 16378, 42592, 53526, + 5854, 25000, 63538, 36676, 55067, 41069, 14839, 20097, 43642, 56588, + 17558, 13280, 27583, 7369, 34131, 61989, 13805, 17051, 56065, 44151, + 62504, 33630, 6852, 28082, 53039, 47193, 8643, 22197, 3818, 31132, + 57350, 38768, 20664, 10190, 48724, 51490, 37245, 58891, 32657, 2279, + 60444, 39786, 752, 30086, 11737, 23215, 49973, 46147, 29579, 1277, + 40295, 59921, 45646, 50488, 23714, 11220, + }, + { 0, 15162, 30324, 19790, 60648, 55250, 39580, 41382, 50637, 65271, + 46009, 34947, 10533, 4639, 24401, 25707, 38791, 44221, 57843, 56009, + 31599, 16469, 3355, 13857, 21066, 26992, 9278, 7940, 48802, 34200, + 51414, 62444, 13075, 2089, 17767, 32349, 57339, 58561, 43407, 37557, + 63198, 52708, 32938, 48016, 6710, 8460, 27714, 22392, 42132, 40878, + 53984, 59866, 18556, 29510, 15880, 1330, 24921, 23139, 5933, 11287, + 36273, 46731, 64453, 49407, 26150, 23836, 4178, 11112, 35534, 45556, + 64698, 51072, 41963, 39121, 54687, 61093, 20227, 29753, 14711, 589, + 61857, 51867, 34773, 48367, 7497, 9843, 27453, 20487, 13420, 3926, + 16920, 31010, 55428, 58302, 44784, 38346, 21813, 28175, 9025, 6267, + 47581, 33511, 53161, 62611, 37112, 43970, 59020, 56758, 31760, 18218, + 2660, 12638, 49842, 63880, 46278, 36860, 11866, 5472, 22574, 25364, + 1919, 15429, 28939, 18993, 60311, 53421, 40419, 42713, 52300, 63350, + 47672, 33026, 8356, 7070, 22224, 28138, 2433, 12987, 32757, 17615, + 58729, 56915, 37661, 43047, 23499, 24817, 11711, 5765, 46883, 35865, + 49495, 64109, 40454, 42300, 59506, 54088, 29422, 18900, 1178, 16288, + 65375, 50277, 35115, 45585, 5047, 10381, 26051, 24313, 14994, 424, + 19686, 30684, 54906, 60736, 40974, 39732, 26840, 21474, 7852, 9622, + 33840, 48906, 62020, 51582, 44309, 38447, 56161, 57435, 16893, 31431, + 14217, 3251, 43626, 37200, 56350, 59172, 18050, 32184, 12534, 3020, + 28583, 21661, 6611, 8937, 33615, 47221, 62779, 52737, 15853, 1751, + 19353, 28835, 53509, 59967, 42865, 40011, 63520, 49946, 36436, 46446, + 5320, 12274, 25276, 22918, 39289, 41539, 61197, 54327, 30097, 20139, + 997, 14559, 23732, 26510, 10944, 4602, 45148, 35686, 50728, 64786, + 3838, 13764, 30858, 17328, 57878, 55596, 37986, 44888, 52019, 61449, + 48455, 34429, 10203, 7393, 20911, 27285, + }, + { 0, 7452, 14904, 10020, 29808, 26988, 20040, 21332, 59616, 62972, + 53976, 53188, 40080, 33164, 42664, 48052, 52701, 53441, 63461, 60153, + 47533, 42161, 33685, 40585, 9533, 14369, 7941, 537, 20813, 19537, + 27509, 30313, 34727, 39611, 48543, 41091, 62423, 61131, 51695, 54515, + 28487, 29275, 21887, 18531, 6967, 1579, 8463, 15379, 19066, 22374, + 28738, 27998, 15882, 8982, 1074, 6446, 41626, 49030, 39074, 34238, + 55018, 52214, 60626, 61902, 4947, 3663, 10603, 13431, 26403, 31295, + 23835, 16391, 64435, 59055, 49547, 56471, 36803, 37599, 46587, 43239, + 56974, 50066, 58550, 63914, 43774, 47074, 37062, 36314, 13934, 11122, + 3158, 4426, 16926, 24322, 30758, 25914, 38132, 35304, 44748, 46032, + 57476, 64920, 55996, 51104, 31764, 24840, 17964, 23344, 2148, 5496, + 12892, 12096, 22825, 17461, 25361, 32269, 11609, 12357, 5985, 2685, + 45513, 44245, 35825, 38637, 50617, 55461, 65409, 58013, 9894, 15290, + 7326, 386, 21206, 20426, 26862, 30194, 52806, 54106, 62590, 59746, + 47670, 42794, 32782, 40210, 60283, 63079, 53571, 52319, 40715, 33303, + 42291, 47151, 923, 7815, 14755, 9407, 30699, 27383, 19923, 20687, + 41217, 48157, 39737, 34341, 54641, 51309, 61257, 62037, 18913, 21757, + 29657, 28357, 15761, 8333, 1961, 6837, 27868, 29120, 22244, 19448, + 6316, 1456, 8852, 16264, 33852, 39200, 48644, 41752, 61516, 60752, + 51828, 55144, 13813, 10473, 4045, 4817, 16773, 23705, 31677, 26273, + 56597, 49161, 59181, 64049, 43365, 46201, 37725, 36417, 63528, 58676, + 49680, 57100, 35928, 37188, 46688, 43900, 4296, 3540, 10992, 14316, + 25784, 31140, 24192, 17308, 45650, 44878, 34922, 38262, 50722, 56126, + 64538, 57606, 23218, 18350, 24714, 32150, 11970, 13278, 5370, 2534, + 32655, 25235, 17847, 22699, 3071, 5859, 12743, 11483, 38767, 35443, + 44375, 45131, 58143, 65027, 55591, 50235, + }, + { 0, 3599, 7198, 4625, 14396, 13875, 9250, 10797, 28792, 32375, + 27750, 25193, 18500, 17995, 21594, 23125, 57584, 61183, 64750, 62177, + 55500, 54979, 50386, 51933, 37000, 40583, 35990, 33433, 43188, 42683, + 46250, 47781, 56829, 54258, 49635, 53228, 58817, 60366, 63967, 63440, + 44421, 41866, 45467, 49044, 38329, 39862, 35239, 34728, 15629, 13058, + 8467, 12060, 1329, 2878, 6447, 5920, 19829, 17274, 20843, 24420, + 30025, 31558, 26967, 26456, 42983, 43496, 48121, 46582, 40923, 37332, + 33733, 36298, 55199, 55696, 52097, 50574, 61347, 57772, 62397, 64946, + 18199, 18712, 23305, 21766, 32555, 28964, 25397, 27962, 14191, 14688, + 11121, 9598, 3923, 348, 4941, 7490, 31258, 29717, 26116, 26635, + 16934, 19497, 24120, 20535, 2658, 1133, 5756, 6259, 12894, 15441, + 11840, 8271, 39658, 38117, 34548, 35067, 41686, 44249, 48840, 45255, + 60050, 58525, 63116, 63619, 53934, 56481, 52912, 49343, 21459, 24028, + 20429, 16834, 27631, 26080, 30705, 31230, 9131, 11684, 16309, 12730, + 7063, 5528, 1929, 2438, 45859, 48428, 44861, 41266, 35615, 34064, + 38657, 39182, 50011, 52564, 57157, 53578, 64359, 62824, 59257, 59766, + 36398, 32801, 37424, 39999, 46610, 47133, 43532, 41987, 65110, 61529, + 57928, 60487, 50794, 51301, 55924, 54395, 28382, 24785, 29376, 31951, + 22242, 22765, 19196, 17651, 7846, 4265, 696, 3255, 9882, 10389, + 14980, 13451, 62516, 64059, 59434, 58917, 52232, 49671, 53270, 56857, + 33868, 35395, 38994, 38493, 48240, 45695, 41070, 44641, 5316, 6859, + 2266, 1749, 11512, 8951, 12518, 16105, 25788, 27315, 30882, 30381, + 23680, 21135, 16542, 20113, 10697, 10182, 13783, 15320, 4597, 8186, + 3563, 996, 22961, 22462, 17839, 19360, 24973, 28546, 32147, 29596, + 51513, 50998, 54567, 56104, 61701, 65290, 60699, 58132, 47425, 46926, + 42335, 43856, 33149, 36722, 40291, 37740, + }, + { 0, 35208, 3853, 34437, 7706, 38802, 4375, 39071, 15412, 46524, + 13113, 47793, 8750, 43942, 11555, 42155, 30824, 61920, 30565, 65261, + 26226, 61434, 27007, 57591, 17500, 52692, 19281, 49881, 23110, 54222, + 21835, 56515, 61648, 31064, 65501, 30293, 61130, 26434, 57799, 26703, + 52452, 17772, 50153, 19041, 54014, 23414, 56819, 21627, 35000, 304, + 34741, 3645, 38562, 7978, 39343, 4135, 46220, 15620, 48001, 12809, + 43670, 8990, 42395, 11283, 64957, 29749, 62128, 31544, 58279, 27183, + 60586, 25890, 49545, 18433, 52868, 18188, 57235, 22043, 53406, 22806, + 34261, 3165, 35544, 848, 39887, 4679, 38082, 7498, 47585, 12393, + 46828, 16228, 43003, 11891, 43254, 8574, 3437, 34021, 608, 35816, + 4983, 39679, 7290, 38386, 12633, 47313, 15956, 47068, 12099, 42699, + 8270, 43462, 29957, 64653, 31240, 62336, 27423, 58007, 25618, 60826, + 18737, 49337, 17980, 53172, 22315, 56995, 22566, 53678, 59239, 28399, + 59498, 25058, 63869, 28917, 63088, 32760, 56147, 21211, 54366, 24022, + 50505, 19649, 51780, 17356, 40719, 5767, 36866, 6538, 33045, 2205, + 36376, 1936, 41787, 10931, 44086, 9662, 48417, 13481, 45612, 15268, + 6071, 40511, 6330, 37170, 2477, 32805, 1696, 36648, 11139, 41483, + 9358, 44294, 13721, 48145, 14996, 45852, 28639, 58967, 24786, 59738, + 29125, 63565, 32456, 63296, 21483, 55907, 23782, 54638, 19953, 50297, + 17148, 52084, 6874, 37714, 5591, 40031, 1216, 36168, 3021, 33349, + 9966, 44902, 10723, 41067, 14580, 45436, 14329, 48753, 25266, 60218, + 28095, 58423, 31912, 62752, 29605, 64045, 24198, 55054, 20875, 55299, + 16540, 51476, 20369, 50713, 59914, 25474, 58631, 27791, 62480, 32152, + 64285, 29333, 54846, 24502, 55603, 20667, 51236, 16812, 50985, 20129, + 37474, 7146, 40303, 5351, 35960, 1520, 33653, 2813, 44630, 10206, + 41307, 10451, 45132, 14788, 48961, 14025, + }, + { 0, 17477, 34954, 52431, 3337, 18764, 34179, 49606, 6674, 24151, + 37528, 55005, 5915, 21342, 40849, 56276, 13348, 28769, 48302, 63723, + 14637, 32104, 45479, 62946, 11830, 27251, 42684, 58105, 9023, 26490, + 43957, 61424, 26696, 11277, 57538, 42119, 25921, 8452, 60875, 43406, + 29274, 13855, 64208, 48789, 32595, 15126, 63449, 45980, 23660, 6185, + 54502, 37027, 20837, 5408, 55791, 40362, 18046, 571, 52980, 35505, + 19319, 3890, 50173, 34744, 53392, 38101, 22554, 7263, 56729, 39388, + 21779, 4438, 51842, 36551, 16904, 1613, 51083, 33742, 20225, 2884, + 58548, 41201, 27710, 10363, 59837, 44536, 24887, 9586, 65190, 47843, + 30252, 12905, 62383, 47082, 31525, 16224, 47320, 64669, 12370, 29719, + 46545, 61844, 15707, 31006, 41674, 59023, 10816, 28165, 44995, 60294, + 10057, 25356, 36092, 51385, 1142, 16435, 33269, 50608, 2431, 19770, + 38638, 53931, 7780, 23073, 39911, 57250, 4973, 22312, 48445, 63864, + 13751, 29170, 45108, 62577, 14526, 31995, 42799, 58218, 12197, 27616, + 43558, 61027, 8876, 26345, 35097, 52572, 403, 17878, 33808, 49237, + 3226, 18655, 37643, 55118, 7041, 24516, 40450, 55879, 5768, 21197, + 54645, 37168, 24063, 6586, 55420, 39993, 20726, 5299, 53095, 35618, + 18413, 936, 49774, 34347, 19172, 3745, 57681, 42260, 27099, 11678, + 60504, 43037, 25810, 8343, 64323, 48902, 29641, 14220, 63050, 45583, + 32448, 14981, 28077, 10728, 58663, 41314, 24740, 9441, 59438, 44139, + 30655, 13306, 65333, 47984, 31414, 16115, 62012, 46713, 22921, 7628, + 53507, 38214, 21632, 4293, 56330, 38991, 17307, 2014, 51985, 36692, + 20114, 2775, 50712, 33373, 1509, 16800, 36207, 51498, 2284, 19625, + 32870, 50211, 8183, 23474, 38781, 54072, 4862, 22203, 39540, 56881, + 12737, 30084, 47435, 64782, 15560, 30861, 46146, 61447, 11219, 28566, + 41817, 59164, 9946, 25247, 44624, 59925, + }, + { 0, 44205, 17735, 59882, 35470, 9763, 53193, 25444, 2305, 42412, + 19526, 57579, 33679, 12066, 50888, 27237, 4610, 48815, 22341, 64488, + 39052, 13345, 56779, 29030, 6915, 47022, 24132, 62185, 37261, 15648, + 54474, 30823, 9220, 34985, 24899, 52718, 44682, 551, 60365, 18272, + 11525, 33192, 26690, 50415, 42891, 2854, 58060, 20065, 13830, 39595, + 29505, 57324, 48264, 4133, 63951, 21858, 16135, 37802, 31296, 55021, + 46473, 6436, 61646, 23651, 18440, 58533, 3407, 41442, 49798, 28203, + 34753, 11116, 16649, 60836, 1102, 43235, 52103, 26410, 36544, 8813, + 23050, 63143, 8013, 46048, 53380, 31785, 38339, 14702, 21259, 65446, + 5708, 47841, 55685, 29992, 40130, 12399, 27660, 49313, 10571, 34278, + 59010, 18991, 41925, 3944, 25869, 51616, 8266, 36071, 61315, 17198, + 43716, 1641, 32270, 53923, 15177, 38884, 62592, 22573, 45511, 7530, + 30479, 56226, 12872, 40677, 64897, 20780, 47302, 5227, 36880, 15549, + 54615, 31226, 6814, 46643, 24537, 62324, 39185, 13756, 56406, 28923, + 5023, 48946, 22232, 64117, 33298, 11967, 51029, 27640, 2204, 42033, + 19931, 57718, 35603, 10174, 52820, 25337, 413, 44336, 17626, 59511, + 46100, 6329, 61779, 24062, 16026, 37431, 31709, 55152, 48405, 4536, + 63570, 21759, 14235, 39734, 29404, 56945, 42518, 2747, 58193, 20476, + 11416, 32821, 27103, 50546, 44823, 954, 59984, 18173, 9625, 35124, + 24798, 52339, 55320, 29877, 40287, 12786, 21142, 65083, 6097, 47996, + 53529, 32180, 37982, 14579, 23447, 63290, 7888, 45693, 51738, 26295, + 36701, 9200, 16532, 60473, 1491, 43390, 49947, 28598, 34396, 10993, + 18837, 58680, 3282, 41087, 64540, 20657, 47451, 5622, 30354, 55871, + 13269, 40824, 62749, 22960, 45146, 7415, 32659, 54078, 15060, 38521, + 60958, 17075, 43865, 2036, 25744, 51261, 8663, 36218, 59167, 19378, + 41560, 3829, 28049, 49468, 10454, 33915, + }, + { 0, 55513, 44463, 30070, 18243, 40858, 60140, 12853, 36486, 22111, + 9001, 64496, 51653, 4380, 25706, 48307, 273, 55752, 44222, 29799, + 18002, 40587, 60413, 13092, 36759, 22350, 8760, 64225, 51412, 4109, + 25979, 48546, 546, 56059, 44941, 30548, 17761, 40376, 59598, 12311, + 36004, 21629, 8459, 63954, 52199, 4926, 26184, 48785, 819, 56298, + 44700, 30277, 17520, 40105, 59871, 12550, 36277, 21868, 8218, 63683, + 51958, 4655, 26457, 49024, 1092, 56477, 43499, 28978, 17159, 39902, + 61096, 13937, 35522, 21019, 10093, 65460, 52609, 5464, 24622, 47351, + 1365, 56716, 43258, 28707, 16918, 39631, 61369, 14176, 35795, 21258, + 9852, 65189, 52368, 5193, 24895, 47590, 1638, 57023, 43977, 29456, + 16677, 39420, 60554, 13395, 35040, 20537, 9551, 64918, 53155, 6010, + 25100, 47829, 1911, 57262, 43736, 29185, 16436, 39149, 60827, 13634, + 35313, 20776, 9310, 64647, 52914, 5739, 25373, 48068, 2184, 53329, + 42279, 32254, 20427, 38674, 57956, 15037, 34318, 24279, 11169, 62328, + 49485, 6548, 27874, 46139, 2457, 53568, 42038, 31983, 20186, 38403, + 58229, 15276, 34591, 24518, 10928, 62057, 49244, 6277, 28147, 46378, + 2730, 53875, 42757, 32732, 19945, 38192, 57414, 14495, 33836, 23797, + 10627, 61786, 50031, 7094, 28352, 46617, 3003, 54114, 42516, 32461, + 19704, 37921, 57687, 14734, 34109, 24036, 10386, 61515, 49790, 6823, + 28625, 46856, 3276, 54293, 41315, 31162, 19343, 37718, 58912, 16121, + 33354, 23187, 12261, 63292, 50441, 7632, 26790, 45183, 3549, 54532, + 41074, 30891, 19102, 37447, 59185, 16360, 33627, 23426, 12020, 63021, + 50200, 7361, 27063, 45422, 3822, 54839, 41793, 31640, 18861, 37236, + 58370, 15579, 32872, 22705, 11719, 62750, 50987, 8178, 27268, 45661, + 4095, 55078, 41552, 31369, 18620, 36965, 58643, 15818, 33145, 22944, + 11478, 62479, 50746, 7907, 27541, 45900, + }, + { 0, 58083, 55771, 15160, 44971, 19784, 30320, 38035, 17227, 41384, + 39568, 30835, 60640, 3587, 13627, 55256, 34454, 25717, 24397, 48558, + 10557, 52190, 61670, 4613, 50653, 10046, 7174, 65253, 27254, 34965, + 45997, 20814, 4401, 62418, 51434, 10761, 48794, 23673, 26433, 34210, + 21114, 45209, 35745, 26946, 64977, 7986, 9226, 50921, 38823, 30020, + 20092, 44191, 14348, 56047, 57815, 820, 54508, 13839, 3383, 61396, + 31559, 39332, 41628, 16511, 8802, 49281, 64441, 6490, 36297, 28458, + 21522, 46833, 24873, 33738, 47346, 23057, 52866, 11361, 5977, 62906, + 42228, 17943, 32047, 40908, 2911, 59836, 53892, 12391, 59327, 1372, + 15972, 56455, 18452, 43767, 37327, 29484, 13139, 53680, 60040, 2155, + 40184, 32283, 17699, 42944, 28696, 37627, 43459, 19232, 57267, 15696, + 1640, 58507, 46533, 22310, 27678, 36605, 6766, 63629, 50101, 8534, + 63118, 5229, 12117, 52662, 22821, 48070, 33022, 25117, 17604, 42535, + 40223, 32764, 60271, 2444, 12980, 53335, 1935, 58732, 56916, 15543, + 43044, 19143, 29183, 37660, 49746, 8369, 7049, 63850, 28153, 36634, + 46114, 22209, 33049, 25594, 22722, 47649, 11954, 52305, 63337, 5514, + 22005, 46870, 35886, 28365, 64094, 6333, 9093, 49510, 5822, 62557, + 53093, 11654, 47381, 23542, 24782, 33325, 54115, 12672, 2744, 59483, + 31944, 40491, 42259, 18416, 36904, 29387, 18931, 43792, 16259, 56672, + 58968, 1211, 26278, 33861, 49021, 23966, 51469, 11246, 4310, 62005, + 9709, 50958, 64566, 7893, 35398, 26789, 21405, 45438, 57392, 723, + 14827, 56072, 20379, 44408, 38464, 29859, 41851, 16792, 31392, 38979, + 3280, 60979, 54539, 14312, 30615, 38260, 44620, 19631, 55356, 15071, + 487, 58116, 13532, 54847, 60679, 4068, 39799, 31124, 17068, 41039, + 61697, 5090, 10458, 51769, 24234, 48201, 34673, 26002, 45642, 20649, + 27537, 35186, 7649, 65282, 50234, 9945, + }, + { 0, 65534, 58337, 7199, 56287, 9249, 14398, 51136, 43939, 21597, + 18498, 47036, 28796, 36738, 37789, 27747, 19291, 46245, 43194, 22340, + 36996, 28538, 29541, 35995, 57592, 7942, 793, 64743, 15143, 50393, + 55494, 10040, 38582, 26952, 30039, 35497, 19817, 45719, 44680, 20854, + 15637, 49899, 57076, 8458, 59082, 6452, 1323, 64213, 56813, 8723, + 15884, 49650, 1586, 63948, 58835, 6701, 30286, 35248, 38319, 27217, + 44433, 21103, 20080, 45454, 12657, 52879, 53904, 11630, 60078, 5456, + 2383, 63153, 39634, 25900, 31027, 34509, 16653, 48883, 41708, 23826, + 31274, 34260, 39371, 26165, 41461, 24075, 16916, 48618, 53641, 11895, + 12904, 52630, 2646, 62888, 59831, 5705, 42951, 22585, 17446, 48088, + 31768, 33766, 40953, 24583, 3172, 62362, 61317, 4219, 55227, 10309, + 13402, 52132, 60572, 4962, 3965, 61571, 14147, 51389, 54434, 11100, + 18239, 47297, 42206, 23328, 40160, 25374, 32513, 33023, 25314, 40220, + 33027, 32509, 47421, 18115, 23260, 42274, 51521, 14015, 10912, 54622, + 4766, 60768, 61823, 3713, 10681, 54855, 51800, 13734, 62054, 3480, + 4487, 61049, 33306, 32228, 25083, 40453, 22981, 42555, 47652, 17882, + 62548, 2986, 6069, 59467, 12171, 53365, 52330, 13204, 24567, 40969, + 48150, 17384, 33832, 31702, 26569, 38967, 48911, 16625, 23790, 41744, + 25808, 39726, 34609, 30927, 5292, 60242, 63309, 2227, 53107, 12429, + 11410, 54124, 21395, 44141, 45170, 20364, 34892, 30642, 27565, 37971, + 63536, 1998, 7121, 58415, 9199, 56337, 49166, 16368, 6344, 59190, + 64297, 1239, 49943, 15593, 8438, 57096, 45931, 19605, 20618, 44916, + 26804, 38730, 35669, 29867, 50469, 15067, 9924, 55610, 7930, 57604, + 64795, 741, 28294, 37240, 36199, 29337, 46425, 19111, 22200, 43334, + 36478, 29056, 28063, 37473, 21921, 43615, 46656, 18878, 9693, 55843, + 50748, 14786, 65026, 508, 7651, 57885, + }, + { 0, 32638, 65276, 33154, 57829, 40603, 7961, 24679, 57303, 41129, + 8491, 24149, 15922, 16716, 49358, 49072, 41907, 56525, 23887, 8753, + 16982, 15656, 48298, 50132, 31844, 794, 33432, 64998, 40321, 58111, + 25469, 7171, 23419, 9221, 42375, 56057, 47774, 50656, 17506, 15132, + 33964, 64466, 31312, 1326, 25929, 6711, 39861, 58571, 63688, 34742, + 1588, 31050, 6445, 26195, 59345, 39087, 10015, 22625, 55779, 42653, + 50938, 47492, 14342, 18296, 46838, 51592, 18442, 14196, 22291, 10349, + 43503, 54929, 26913, 5727, 38877, 59555, 35012, 63418, 30264, 2374, + 5445, 27195, 60345, 38087, 62624, 35806, 2652, 29986, 51858, 46572, + 13422, 19216, 11127, 21513, 54667, 43765, 60813, 37619, 4977, 27663, + 3176, 29462, 62100, 36330, 12890, 19748, 52390, 46040, 54207, 44225, + 11587, 21053, 20030, 12608, 45250, 53180, 45019, 53413, 20775, 11865, + 37353, 61079, 28437, 4203, 28684, 3954, 36592, 61838, 29169, 3727, + 36621, 61555, 36884, 61290, 28392, 4502, 44582, 53592, 20698, 12196, + 20419, 12477, 45375, 52801, 53826, 44348, 11454, 21440, 13223, 19673, + 52571, 45605, 3477, 29419, 62313, 35863, 60528, 37646, 4748, 28146, + 10890, 22004, 54390, 43784, 52079, 46097, 13715, 19181, 62813, 35363, + 2977, 29919, 5304, 27590, 59972, 38202, 35129, 63047, 30661, 2235, + 26844, 6050, 38432, 59742, 22254, 10640, 43026, 55148, 46859, 51317, + 18935, 13961, 50951, 47225, 14843, 18053, 9954, 22940, 55326, 42848, + 6352, 26542, 58924, 39250, 63797, 34379, 1993, 30903, 25780, 7114, + 39496, 58678, 34129, 64047, 31661, 1235, 47971, 50205, 17823, 15073, + 23174, 9720, 42106, 56068, 40060, 58114, 25216, 7678, 32153, 743, + 33637, 64539, 17323, 15573, 48471, 49705, 41550, 56624, 23730, 9164, + 16335, 16561, 49459, 48717, 56874, 41300, 8406, 24488, 57368, 40806, + 7908, 24986, 509, 32387, 65281, 32895, + }, + { 0, 16190, 32380, 16706, 64760, 50118, 33412, 48570, 58861, 56019, + 39825, 42159, 6421, 9771, 26473, 22615, 55239, 59641, 43451, 38533, + 11071, 5121, 21827, 27261, 12842, 3348, 19542, 29544, 52946, 61932, + 45230, 36752, 45971, 36013, 52719, 62161, 20331, 28757, 12567, 3625, + 22142, 26944, 10242, 5948, 43654, 38328, 54522, 60356, 25684, 23402, + 6696, 9494, 39084, 42898, 59088, 55790, 33209, 48775, 65477, 49403, + 32065, 17023, 829, 15363, 31547, 17413, 1351, 14969, 34755, 47357, + 63935, 50817, 40662, 41448, 57514, 57236, 25134, 23824, 7250, 9068, + 44284, 37826, 53888, 60862, 20484, 28474, 11896, 4422, 18705, 30255, + 14189, 2131, 46569, 35543, 52117, 62635, 51368, 63382, 46804, 35306, + 13392, 2926, 18988, 29970, 11589, 4731, 21305, 27655, 53693, 61059, + 44993, 37119, 8047, 8273, 24851, 24109, 58263, 56489, 40427, 41685, + 64130, 50620, 34046, 48064, 1658, 14660, 30726, 18232, 63094, 51528, + 34826, 46900, 2702, 13744, 29938, 19404, 5019, 11429, 28135, 21209, + 61283, 53341, 37151, 44577, 8625, 7823, 24525, 24819, 56649, 57975, + 41781, 39947, 50268, 64354, 47648, 34078, 14500, 1946, 18136, 31206, + 17893, 31451, 15257, 1191, 47389, 34339, 51041, 63583, 40968, 40758, + 56948, 57674, 23792, 25550, 8844, 7602, 37410, 44316, 60510, 54112, + 28378, 20964, 4262, 12184, 30671, 18673, 2483, 13965, 35639, 46089, + 62795, 51829, 36173, 45683, 62257, 52239, 29109, 20107, 4041, 12535, + 26784, 22430, 5852, 10722, 37976, 43878, 59940, 54554, 23178, 26036, + 9462, 7112, 42610, 39244, 55310, 59184, 48999, 32857, 49435, 65061, + 17311, 31905, 15843, 733, 16094, 480, 16546, 32668, 49702, 64792, + 48218, 33636, 56115, 58381, 42319, 39537, 10187, 6389, 22967, 26249, + 59673, 54823, 38757, 43099, 5601, 10975, 27549, 21667, 3316, 13258, + 29320, 19894, 61452, 53042, 36464, 45390, + }, + { 0, 7966, 15932, 8482, 31864, 25446, 16964, 23898, 63728, 59374, + 50892, 55762, 33928, 39830, 47796, 42410, 60925, 62179, 54209, 52447, + 37253, 36507, 44985, 45223, 5389, 2579, 11057, 13359, 26997, 30315, + 22345, 18519, 51175, 55545, 63963, 59077, 48031, 42113, 34211, 39613, + 16151, 8201, 299, 7733, 17263, 23665, 32083, 25165, 10778, 13572, + 5158, 2872, 22114, 18812, 26718, 30528, 53994, 52724, 60630, 62408, + 44690, 45452, 37038, 36784, 37843, 36045, 44527, 45809, 61355, 61621, + 53655, 52873, 27427, 29757, 21791, 18945, 5979, 2117, 10599, 13945, + 32302, 24880, 16402, 24332, 598, 7496, 15466, 9076, 34526, 39360, + 47330, 43004, 64166, 58808, 50330, 56196, 21556, 19242, 27144, 29974, + 10316, 14162, 5744, 2414, 44228, 46042, 37624, 36326, 53436, 53154, + 61056, 61854, 47561, 42711, 34805, 39147, 50609, 55983, 64397, 58515, + 16697, 24103, 32517, 24603, 15681, 8799, 893, 7267, 15291, 9381, + 1415, 6809, 18371, 22749, 31231, 26337, 49995, 56405, 64887, 57961, + 48947, 41005, 33039, 40465, 54854, 51544, 59514, 63332, 43582, 46368, + 37890, 35612, 11958, 12712, 4234, 3988, 21198, 19920, 27890, 29676, + 64604, 58178, 49760, 56702, 32804, 40762, 48664, 41222, 1196, 7090, + 14992, 9614, 30932, 26570, 18152, 23030, 4513, 3775, 12189, 12419, + 28121, 29383, 21477, 19707, 59729, 63055, 55149, 51315, 38185, 35383, + 43797, 46091, 43112, 46966, 38484, 35146, 54288, 51982, 59948, 62770, + 20632, 20358, 28324, 29114, 11488, 13310, 4828, 3522, 17813, 23179, + 31657, 25783, 14829, 9971, 2001, 6351, 48485, 41595, 33625, 40007, + 49437, 56835, 65313, 57407, 28559, 28817, 20915, 20141, 5111, 3305, + 11723, 13013, 38783, 34913, 43331, 46685, 60167, 62489, 54587, 51749, + 33394, 40300, 48206, 41808, 65034, 57620, 49206, 57128, 31362, 26012, + 17598, 23456, 1786, 6628, 14534, 10200, + }, + { 0, 3854, 7708, 4370, 15416, 13110, 8740, 11562, 30832, 30590, + 26220, 26978, 17480, 19270, 23124, 21850, 61664, 65518, 61180, 57842, + 52440, 50134, 53956, 56778, 34960, 34718, 38540, 39298, 46248, 48038, + 43700, 42426, 64989, 62163, 58305, 60623, 49637, 52971, 57337, 53495, + 34221, 35491, 39857, 38079, 47509, 46747, 42889, 43143, 3389, 563, + 4897, 7215, 12549, 15883, 12057, 8215, 30029, 31299, 27473, 25695, + 18805, 18043, 22377, 22631, 59303, 59561, 63931, 63157, 56223, 54417, + 50563, 51853, 40919, 37081, 33227, 36549, 41967, 44257, 48627, 45821, + 5959, 6217, 2395, 1621, 11135, 9329, 13667, 14957, 28471, 24633, + 28971, 32293, 21263, 23553, 19731, 16925, 6778, 5492, 1126, 2920, + 9794, 10572, 14430, 14160, 25098, 27908, 31766, 29464, 24114, 20796, + 16430, 20256, 60058, 58772, 62598, 64392, 54946, 55724, 51390, 51120, + 37610, 40420, 36086, 33784, 44754, 41436, 45262, 49088, 54099, 56413, + 52559, 49729, 61291, 57445, 61815, 65145, 43811, 42029, 46399, 47665, + 38683, 38933, 35079, 34313, 9139, 11453, 15791, 12961, 8075, 4229, + 407, 3737, 23491, 21709, 17887, 19153, 26619, 26869, 31207, 30441, + 11918, 8576, 12434, 16284, 4790, 7608, 3242, 932, 22270, 23024, + 18658, 18412, 27334, 26056, 29914, 31700, 56942, 53600, 49266, 53116, + 57942, 60760, 64586, 62276, 42526, 43280, 47106, 46860, 39462, 38184, + 33850, 35636, 13556, 15354, 10984, 9702, 2252, 1986, 5840, 6622, + 19588, 17290, 21144, 23958, 28860, 32690, 28320, 25006, 50196, 51994, + 55816, 54534, 63532, 63266, 58928, 59710, 48228, 45930, 41592, 44406, + 32860, 36690, 40512, 37198, 51497, 50727, 55093, 55355, 62737, 64031, + 60173, 58371, 45401, 48727, 44869, 41035, 36193, 33391, 37757, 40051, + 14793, 14023, 10197, 10459, 1521, 2815, 7149, 5347, 16825, 20151, + 24485, 20651, 32129, 29327, 25501, 27795, + }, + { 0, 1798, 3596, 2314, 7192, 6942, 4628, 5394, 14384, 16182, + 13884, 12602, 9256, 9006, 10788, 11554, 28768, 30566, 32364, 31082, + 27768, 27518, 25204, 25970, 18512, 20310, 18012, 16730, 21576, 21326, + 23108, 23874, 57536, 59334, 61132, 59850, 64728, 64478, 62164, 62930, + 55536, 57334, 55036, 53754, 50408, 50158, 51940, 52706, 37024, 38822, + 40620, 39338, 36024, 35774, 33460, 34226, 43152, 44950, 42652, 41370, + 46216, 45966, 47748, 48514, 56733, 55963, 54161, 54423, 49541, 50819, + 53129, 51343, 58797, 58027, 60321, 60583, 63925, 65203, 63417, 61631, + 44541, 43771, 41969, 42231, 45541, 46819, 49129, 47343, 38349, 37579, + 39873, 40135, 35285, 36563, 34777, 32991, 15709, 14939, 13137, 13399, + 8517, 9795, 12105, 10319, 1389, 619, 2913, 3175, 6517, 7795, + 6009, 4223, 19773, 19003, 17201, 17463, 20773, 22051, 24361, 22575, + 29965, 29195, 31489, 31751, 26901, 28179, 26393, 24607, 42791, 40993, + 43307, 44589, 47935, 48185, 46387, 45621, 40727, 38929, 37147, 38429, + 33551, 33801, 36099, 35333, 55111, 53313, 55627, 56909, 52063, 52313, + 50515, 49749, 61303, 59505, 57723, 59005, 62319, 62569, 64867, 64101, + 18407, 16609, 18923, 20205, 23551, 23801, 22003, 21237, 32727, 30929, + 29147, 30429, 25551, 25801, 28099, 27333, 14215, 12417, 14731, 16013, + 11167, 11417, 9619, 8853, 4023, 2225, 443, 1725, 5039, 5289, + 7587, 6821, 31418, 32188, 29878, 29616, 26274, 24996, 26798, 28584, + 17034, 17804, 19590, 19328, 24210, 22932, 20638, 22424, 2778, 3548, + 1238, 976, 5826, 4548, 6350, 8136, 13034, 13804, 15590, 15328, + 12018, 10740, 8446, 10232, 39546, 40316, 38006, 37744, 34402, 33124, + 34926, 36712, 41546, 42316, 44102, 43840, 48722, 47444, 45150, 46936, + 59930, 60700, 58390, 58128, 62978, 61700, 63502, 65288, 53802, 54572, + 56358, 56096, 52786, 51508, 49214, 51000, + }, + { 0, 770, 1540, 1286, 3080, 3850, 2572, 2318, 6160, 6930, + 7700, 7446, 5144, 5914, 4636, 4382, 12320, 13090, 13860, 13606, + 15400, 16170, 14892, 14638, 10288, 11058, 11828, 11574, 9272, 10042, + 8764, 8510, 24640, 25410, 26180, 25926, 27720, 28490, 27212, 26958, + 30800, 31570, 32340, 32086, 29784, 30554, 29276, 29022, 20576, 21346, + 22116, 21862, 23656, 24426, 23148, 22894, 18544, 19314, 20084, 19830, + 17528, 18298, 17020, 16766, 49280, 50050, 50820, 50566, 52360, 53130, + 51852, 51598, 55440, 56210, 56980, 56726, 54424, 55194, 53916, 53662, + 61600, 62370, 63140, 62886, 64680, 65450, 64172, 63918, 59568, 60338, + 61108, 60854, 58552, 59322, 58044, 57790, 41152, 41922, 42692, 42438, + 44232, 45002, 43724, 43470, 47312, 48082, 48852, 48598, 46296, 47066, + 45788, 45534, 37088, 37858, 38628, 38374, 40168, 40938, 39660, 39406, + 35056, 35826, 36596, 36342, 34040, 34810, 33532, 33278, 40221, 40479, + 39705, 38939, 37141, 37399, 38673, 37907, 34061, 34319, 33545, 32779, + 35077, 35335, 36609, 35843, 44349, 44607, 43833, 43067, 41269, 41527, + 42801, 42035, 46381, 46639, 45865, 45099, 47397, 47655, 48929, 48163, + 64861, 65119, 64345, 63579, 61781, 62039, 63313, 62547, 58701, 58959, + 58185, 57419, 59717, 59975, 61249, 60483, 52605, 52863, 52089, 51323, + 49525, 49783, 51057, 50291, 54637, 54895, 54121, 53355, 55653, 55911, + 57185, 56419, 23965, 24223, 23449, 22683, 20885, 21143, 22417, 21651, + 17805, 18063, 17289, 16523, 18821, 19079, 20353, 19587, 28093, 28351, + 27577, 26811, 25013, 25271, 26545, 25779, 30125, 30383, 29609, 28843, + 31141, 31399, 32673, 31907, 15837, 16095, 15321, 14555, 12757, 13015, + 14289, 13523, 9677, 9935, 9161, 8395, 10693, 10951, 12225, 11459, + 3581, 3839, 3065, 2299, 501, 759, 2033, 1267, 5613, 5871, + 5097, 4331, 6629, 6887, 8161, 7395, + }, +}; + +#endif /* __VCD_CD_SECTOR_PRIVATE_H__ */ + + +/* + * Local variables: + * c-file-style: "gnu" + * tab-width: 8 + * indent-tabs-mode: nil + * End: + */ diff --git a/contrib/libvcd/stream.c b/contrib/libvcd/stream.c new file mode 100644 index 000000000..2840a05d6 --- /dev/null +++ b/contrib/libvcd/stream.c @@ -0,0 +1,276 @@ +/* + $Id: stream.c,v 1.2 2004/04/11 12:20:32 miguelfreitas 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 "vcd_assert.h" +#include "stream.h" +#include "util.h" + +static const char _rcsid[] = "$Id: stream.c,v 1.2 2004/04/11 12:20:32 miguelfreitas 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/contrib/libvcd/stream.h b/contrib/libvcd/stream.h new file mode 100644 index 000000000..43b8c911a --- /dev/null +++ b/contrib/libvcd/stream.h @@ -0,0 +1,123 @@ +/* + $Id: stream.h,v 1.2 2004/04/11 12:20:32 miguelfreitas 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/contrib/libvcd/stream_stdio.c b/contrib/libvcd/stream_stdio.c new file mode 100644 index 000000000..4eac0f6b3 --- /dev/null +++ b/contrib/libvcd/stream_stdio.c @@ -0,0 +1,236 @@ +/* + $Id: stream_stdio.c,v 1.2 2004/04/11 12:20:32 miguelfreitas 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.2 2004/04/11 12:20:32 miguelfreitas 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/contrib/libvcd/stream_stdio.h b/contrib/libvcd/stream_stdio.h new file mode 100644 index 000000000..46bca3140 --- /dev/null +++ b/contrib/libvcd/stream_stdio.h @@ -0,0 +1,43 @@ +/* + $Id: stream_stdio.h,v 1.2 2004/04/11 12:20:32 miguelfreitas 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/contrib/libvcd/util.c b/contrib/libvcd/util.c new file mode 100644 index 000000000..a1e5c57d4 --- /dev/null +++ b/contrib/libvcd/util.c @@ -0,0 +1,179 @@ +/* + $Id: util.c,v 1.2 2004/04/11 12:20:32 miguelfreitas 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 "vcd_assert.h" +#include "bytesex.h" +#include "util.h" + +static const char _rcsid[] = "$Id: util.c,v 1.2 2004/04/11 12:20:32 miguelfreitas 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/contrib/libvcd/util.h b/contrib/libvcd/util.h new file mode 100644 index 000000000..c5a9ead7c --- /dev/null +++ b/contrib/libvcd/util.h @@ -0,0 +1,108 @@ +/* + $Id: util.h,v 1.2 2004/04/11 12:20:32 miguelfreitas 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/contrib/libvcd/vcd.c b/contrib/libvcd/vcd.c new file mode 100644 index 000000000..0772149ec --- /dev/null +++ b/contrib/libvcd/vcd.c @@ -0,0 +1,2412 @@ +/* + $Id: vcd.c,v 1.4 2006/12/08 16:26:10 mshopf Exp $ + + Copyright (C) 2000, 2004 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.4 2006/12/08 16:26:10 mshopf 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[]) +{ + CdioListNode *node; + + vcd_assert (sequence_id != NULL); + vcd_assert (obj != NULL); + + _CDIO_LIST_FOREACH (node, obj->mpeg_sequence_list) + { + mpeg_sequence_t *_sequence = _cdio_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[]) +{ + CdioListNode *node; + + vcd_assert (entry_id != NULL); + vcd_assert (obj != NULL); + + _CDIO_LIST_FOREACH (node, obj->mpeg_sequence_list) + { + mpeg_sequence_t *_sequence = _cdio_list_node_data (node); + CdioListNode *node2; + + /* default entry point */ + if (_sequence->default_entry_id + && !strcmp (entry_id, _sequence->default_entry_id)) + return _sequence; + + /* additional entry points */ + _CDIO_LIST_FOREACH (node2, _sequence->entry_list) + { + entry_t *_entry = _cdio_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[]) +{ + CdioListNode *node; + + vcd_assert (segment_id != NULL); + vcd_assert (obj != NULL); + + _CDIO_LIST_FOREACH (node, obj->mpeg_segment_list) + { + mpeg_segment_t *_segment = _cdio_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 = _cdio_list_new (); + new_obj->custom_dir_list = _cdio_list_new (); + + + new_obj->mpeg_sequence_list = _cdio_list_new (); + + new_obj->mpeg_segment_list = _cdio_list_new (); + + new_obj->pbc_list = _cdio_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; + CdioListNode *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 *) _cdio_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 */ + { + CdioListNode *node2 = node; + while ((node2 = _cdio_list_node_next (node2)) != NULL) + ((mpeg_sequence_t *) _cdio_list_node_data (node))->relative_start_extent -= length; + } + + obj->relative_end_extent -= length; + + /* shift up */ + _cdio_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...", + _cdio_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 = _cdio_list_new (); + + vcd_debug ("SPI length is %d sector(s), allocated %d segment(s)", + segment->info->packets, + segment->segment_count); + + _cdio_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 = _cdio_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 = _cdio_list_new (); + sequence->pause_list = _cdio_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); */ + + _cdio_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 = + _cdio_list_node_data (_cdio_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; + + _cdio_list_append (_sequence->pause_list, _pause); + } + + _vcd_list_sort (_sequence->pause_list, + (_cdio_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 = _cdio_list_node_data (_cdio_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; + + _cdio_list_append (_segment->pause_list, _pause); + } + + _vcd_list_sort (_segment->pause_list, + (_cdio_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 = + _cdio_list_node_data (_cdio_list_end (obj->mpeg_sequence_list)); + + if (!_sequence) + { + vcd_error ("sequence id `%s' not found", sequence_id); + return -1; + } + + if (_cdio_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; + + _cdio_list_append (_sequence->entry_list, _entry); + } + + _vcd_list_sort (_sequence->entry_list, + (_cdio_list_cmp_func) _entry_cmp); + + return 0; +} + +void +vcd_obj_destroy (VcdObj *obj) +{ + CdioListNode *node; + + vcd_assert (obj != NULL); + vcd_assert (!obj->in_output); + + free (obj->iso_volume_label); + free (obj->iso_application_id); + + _CDIO_LIST_FOREACH (node, obj->custom_file_list) + { + custom_file_t *p = _cdio_list_node_data (node); + + free (p->iso_pathname); + } + + _cdio_list_free (obj->custom_file_list, true); + + _cdio_list_free (obj->custom_dir_list, true); + + while (_cdio_list_length (obj->mpeg_sequence_list)) + _vcd_obj_remove_mpeg_track (obj, 0); + _cdio_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; + } + + _cdio_list_append (obj->custom_dir_list, _iso_pathname); + + _vcd_list_sort (obj->custom_dir_list, + (_cdio_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; + + _cdio_list_append (obj->custom_file_list, p); + } + + return 0; +} + +static void +_finalize_vcd_iso_track_allocation (VcdObj *obj) +{ + int n; + CdioListNode *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 */ + + _CDIO_LIST_FOREACH (node, obj->mpeg_segment_list) + { + mpeg_segment_t *_segment = _cdio_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 */ + + _CDIO_LIST_FOREACH (node, obj->custom_file_list) + { + custom_file_t *p = _cdio_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; + CdioListNode *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 (_cdio_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 (_cdio_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; + _CDIO_LIST_FOREACH (node, obj->mpeg_segment_list) + { + mpeg_segment_t *segment = _cdio_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 */ + _CDIO_LIST_FOREACH (node, obj->custom_dir_list) + { + char *p = _cdio_list_node_data (node); + _vcd_directory_mkdir (obj->dir, p); + } + + _CDIO_LIST_FOREACH (node, obj->custom_file_list) + { + custom_file_t *p = _cdio_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; + _CDIO_LIST_FOREACH (node, obj->mpeg_sequence_list) + { + char avseq_pathname[128] = { 0, }; + const char *fmt = NULL; + mpeg_sequence_t *_sequence = _cdio_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 = _cdio_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 = + _cdio_list_node_data (_vcd_list_at (obj->mpeg_sequence_list, track_idx)); + CdioListNode *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(buf2) - strlen(buf2) - 1); + } + + 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 = _cdio_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 = _cdio_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 = _cdio_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) +{ + CdioListNode *pause_node; + unsigned packet_no; + int n = obj->sectors_written; + + vcd_assert (_segment->start_extent == n); + + pause_node = _cdio_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 = _cdio_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 = _cdio_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) +{ + CdioListNode *node; + struct aps_data best_aps; + bool first = true; + + vcd_assert (_mpeg_info != NULL); + vcd_assert (_mpeg_info->shdr[0].aps_list != NULL); + + _CDIO_LIST_FOREACH (node, _mpeg_info->shdr[0].aps_list) + { + struct aps_data *_aps = _cdio_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) +{ + CdioListNode *sequence_node; + + _CDIO_LIST_FOREACH (sequence_node, obj->mpeg_sequence_list) + { + mpeg_sequence_t *_sequence = _cdio_list_node_data (sequence_node); + CdioListNode *entry_node; + unsigned last_packet_no = 0; + + _CDIO_LIST_FOREACH (entry_node, _sequence->entry_list) + { + entry_t *_entry = _cdio_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) +{ + CdioListNode *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); + + _CDIO_LIST_FOREACH (node, obj->mpeg_segment_list) + { + mpeg_segment_t *_segment = _cdio_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); + + _CDIO_LIST_FOREACH (node, obj->custom_file_list) + { + custom_file_t *p = _cdio_list_node_data (node); + + vcd_info ("writing file `%s' (%lu bytes%s)", + p->iso_pathname, (unsigned long) 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 (_cdio_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 (_cdio_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 = _cdio_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); + _cdio_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; + } + + _cdio_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) +{ + CdioListNode *node; + + vcd_assert (obj != NULL); + vcd_assert (obj->in_output); + + if (!image_sink) + return -1; + + /* start with meta info */ + + { + CdioList *cue_list; + vcd_cue_t *_cue; + + cue_list = _cdio_list_new (); + + _cdio_list_append (cue_list, (_cue = _vcd_malloc (sizeof (vcd_cue_t)))); + + _cue->lsn = 0; + _cue->type = VCD_CUE_TRACK_START; + + _CDIO_LIST_FOREACH (node, obj->mpeg_sequence_list) + { + mpeg_sequence_t *track = _cdio_list_node_data (node); + CdioListNode *entry_node; + + _cdio_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; + + _cdio_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; + + _CDIO_LIST_FOREACH (entry_node, track->entry_list) + { + entry_t *_entry = _cdio_list_node_data (entry_node); + + _cdio_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... */ + + _cdio_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); + + _cdio_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 < _cdio_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/contrib/libvcd/vcd.h b/contrib/libvcd/vcd.h new file mode 100644 index 000000000..7c001d282 --- /dev/null +++ b/contrib/libvcd/vcd.h @@ -0,0 +1,176 @@ +/* + $Id: vcd.h,v 1.2 2004/04/11 12:20:32 miguelfreitas 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/contrib/libvcd/vcd_assert.h b/contrib/libvcd/vcd_assert.h new file mode 100644 index 000000000..c096b3677 --- /dev/null +++ b/contrib/libvcd/vcd_assert.h @@ -0,0 +1,55 @@ +/* + $Id: vcd_assert.h,v 1.1 2004/04/11 12:20:32 miguelfreitas 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/contrib/libvcd/vcd_read.c b/contrib/libvcd/vcd_read.c new file mode 100644 index 000000000..dd9d30cfa --- /dev/null +++ b/contrib/libvcd/vcd_read.c @@ -0,0 +1,108 @@ +/* + $Id: vcd_read.c,v 1.4 2005/01/01 02:43:59 rockyb 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 "vcd_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_t *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_t *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/contrib/libvcd/vcd_read.h b/contrib/libvcd/vcd_read.h new file mode 100644 index 000000000..fb9e1509b --- /dev/null +++ b/contrib/libvcd/vcd_read.h @@ -0,0 +1,36 @@ +/* + $Id: vcd_read.h,v 1.3 2005/01/01 02:43:59 rockyb 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_t *entries); +bool read_info(CdIo *cdio, InfoVcd_t *info, vcd_type_t *vcd_type); + + + |