Commit 506d5d26 authored by conrad's avatar conrad

new files for 0.8.0


git-svn-id: http://svn.annodex.net/liboggz/trunk@347 8158c8cd-e7e1-0310-9fa4-c5954c97daef
parent 7e8bc47d
/**
Copyright (C) 2003 Commonwealth Scientific and Industrial Research
Organisation (CSIRO) Australia
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
- Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
- Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
- Neither the name of CSIRO Australia nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE ORGANISATION OR
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <stdlib.h> /* exit */
#include <oggz/oggz.h>
static long serialno;
static ogg_int64_t granulepos = 0;
static ogg_int64_t packetno = 0;
int
main (int argc, char * argv[])
{
char * progname, * filename = NULL;
OGGZ * oggz;
ogg_packet op;
unsigned char buf[1];
long n;
progname = argv[0];
if (argc > 1) filename = argv[1];
if (filename) {
oggz = oggz_open (filename, OGGZ_WRITE);
} else {
oggz = oggz_open_stdio (stdout, OGGZ_WRITE);
}
if (oggz == NULL) {
fprintf (stderr, "%s: Error creating oggz\n", progname);
exit (1);
}
serialno = oggz_serialno_new (oggz);
for (packetno = 0; packetno < 10; packetno++) {
/* Create a packet */
buf[0] = 'A' + (int)packetno;
op.packet = buf;
op.bytes = 1;
op.granulepos = granulepos;
op.packetno = packetno;
if (packetno == 0) op.b_o_s = 1;
else op.b_o_s = 0;
if (packetno == 9) op.e_o_s = 1;
else op.e_o_s = 0;
/* Feed it to the Oggz packet queue */
oggz_write_feed (oggz, &op, serialno, OGGZ_FLUSH_AFTER, NULL);
granulepos += 100;
packetno++;
/* Write bytes from packetized bitstream to the output file */
while ((n = oggz_write (oggz, 32)) > 0);
}
oggz_close (oggz);
exit (0);
}
/**
Copyright (C) 2003 Commonwealth Scientific and Industrial Research
Organisation (CSIRO) Australia
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
- Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
- Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
- Neither the name of CSIRO Australia nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE ORGANISATION OR
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <stdlib.h> /* exit */
#include <oggz/oggz.h>
static long serialno;
static ogg_int64_t granulepos = 0;
static ogg_int64_t packetno = 0;
static int
hungry (OGGZ * oggz, int empty, void * user_data)
{
ogg_packet op;
unsigned char buf[1];
buf[0] = 'A' + (int)packetno;
op.packet = buf;
op.bytes = 1;
op.granulepos = granulepos;
op.packetno = packetno;
if (packetno == 0) op.b_o_s = 1;
else op.b_o_s = 0;
if (packetno == 9) op.e_o_s = 1;
else op.e_o_s = 0;
oggz_write_feed (oggz, &op, serialno, OGGZ_FLUSH_AFTER, NULL);
granulepos += 100;
packetno++;
return 0;
}
int
main (int argc, char * argv[])
{
char * progname, filename = NULL;
OGGZ * oggz;
long n;
progname = argv[0];
if (argc > 1) filename = argv[1];
if (filename) {
oggz = oggz_open (filename, OGGZ_WRITE);
} else {
oggz = oggz_open_stdio (stdout, OGGZ_WRITE);
}
if (oggz == NULL) {
fprintf (stderr, "%s: Error creating oggz\n", progname);
exit (1);
}
serialno = oggz_serialno_new (oggz);
if (oggz_write_set_hungry_callback (oggz, hungry, 1, NULL) == -1) {
fprintf (stderr, "%s: Error setting OggzHungry callback\n", progname);
exit (1);
}
while ((n = oggz_write (oggz, 32)) > 0);
oggz_close (oggz);
exit (0);
}
/*
Copyright (C) 2003 Commonwealth Scientific and Industrial Research
Organisation (CSIRO) Australia
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
- Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
- Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
- Neither the name of CSIRO Australia nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE ORGANISATION OR
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/*
* oggz_auto.c
*
* Conrad Parker <conrad@annodex.net>
*/
#include "config.h"
#if OGGZ_CONFIG_READ
#include <string.h>
#include <oggz/oggz.h>
#include "oggz_auto.h"
#include "oggz_byteorder.h"
#include "oggz_macros.h"
#define INT32_LE_AT(x) _le_32((*(ogg_int32_t *)(x)))
#define INT32_BE_AT(x) _be_32((*(ogg_int32_t *)(x)))
#define INT64_LE_AT(x) _le_64((*(ogg_int64_t *)(x)))
/*#define DEBUG*/
#define OGGZ_AUTO_MULT 1000
static int
auto_speex (OGGZ * oggz, ogg_packet * op, long serialno, void * user_data)
{
unsigned char * header = op->packet;
ogg_int64_t granule_rate = 0;
if (op->bytes < 68) return 0;
if (strncmp ((char *)header, "Speex ", 8)) return 0;
if (!op->b_o_s) return 0;
granule_rate = (ogg_int64_t) INT32_LE_AT(&header[36]);
#ifdef DEBUG
printf ("Got speex rate %d\n", (int)granule_rate);
#endif
oggz_set_metric_linear (oggz, serialno, granule_rate, OGGZ_AUTO_MULT);
return 1;
}
static int
auto_vorbis (OGGZ * oggz, ogg_packet * op, long serialno, void * user_data)
{
unsigned char * header = op->packet;
ogg_int64_t granule_rate = 0;
if (op->bytes < 30) return 0;
if (header[0] != 0x01) return 0;
if (strncmp ((char *)&header[1], "vorbis", 6)) return 0;
if (!op->b_o_s) return 0;
granule_rate = (ogg_int64_t) INT32_LE_AT(&header[12]);
#ifdef DEBUG
printf ("Got vorbis rate %d\n", (int)granule_rate);
#endif
oggz_set_metric_linear (oggz, serialno, granule_rate, OGGZ_AUTO_MULT);
return 1;
}
typedef struct {
ogg_int32_t fps_numerator;
ogg_int32_t fps_denominator;
int keyframe_shift;
} oggz_theora_metric_t;
static int intlog(int num) {
int ret=0;
while(num>0){
num=num/2;
ret=ret+1;
}
return(ret);
}
static ogg_int64_t
auto_theora_metric (OGGZ * oggz, long serialno, ogg_int64_t granulepos,
void * user_data)
{
oggz_theora_metric_t * tdata = (oggz_theora_metric_t *)user_data;
ogg_int64_t iframe, pframe;
ogg_int64_t units;
iframe = granulepos >> tdata->keyframe_shift;
pframe = granulepos - (iframe << tdata->keyframe_shift);
granulepos = (iframe + pframe);
units = OGGZ_AUTO_MULT * granulepos * tdata->fps_denominator /
tdata->fps_numerator;
return units;
}
static int
auto_theora (OGGZ * oggz, ogg_packet * op, long serialno, void * user_data)
{
unsigned char * header = op->packet;
char keyframe_granule_shift = 0;
oggz_theora_metric_t * tdata;
if (op->bytes < 41) return 0;
if (header[0] != 0x80) return 0;
if (strncmp ((char *)&header[1], "theora", 6)) return 0;
if (!op->b_o_s) return 0;
tdata = oggz_malloc (sizeof (oggz_theora_metric_t));
tdata->fps_numerator = INT32_BE_AT(&header[22]);
tdata->fps_denominator = INT32_BE_AT(&header[26]);
keyframe_granule_shift = (header[36] & 0xf8) >> 3;
tdata->keyframe_shift = intlog (keyframe_granule_shift - 1);
#ifdef DEBUG
printf ("Got theora fps %d/%d, keyframe_granule_shift %d\n",
tdata->fps_numerator, tdata->fps_denominator,
keyframe_granule_shift);
#endif
/*oggz_set_metric_internal (oggz, serialno, auto_theora_metric, tdata, 1);*/
oggz_set_metric (oggz, serialno, auto_theora_metric, tdata);
return 1;
}
static int
auto_anxdata (OGGZ * oggz, ogg_packet * op, long serialno, void * user_data)
{
unsigned char * header = op->packet;
ogg_int64_t granule_rate_numerator = 0, granule_rate_denominator = 0;
if (op->bytes < 28) return 0;
if (strncmp ((char *)header, "AnxData", 8)) return 0;
if (!op->b_o_s) return 0;
granule_rate_numerator = INT64_LE_AT(&header[8]);
granule_rate_denominator = INT64_LE_AT(&header[16]);
#ifdef DEBUG
printf ("Got AnxData rate %lld/%lld\n", granule_rate_numerator,
granule_rate_denominator);
#endif
oggz_set_metric_linear (oggz, serialno,
granule_rate_numerator,
OGGZ_AUTO_MULT * granule_rate_denominator);
return 1;
}
static OggzReadPacket auto_readers[] = {
auto_speex,
auto_vorbis,
auto_theora,
auto_anxdata,
NULL
};
int
oggz_auto (OGGZ * oggz, ogg_packet * op, long serialno, void * user_data)
{
OggzReadPacket read_packet;
int i = 0;
for (read_packet = auto_readers[0]; read_packet;
read_packet = auto_readers[++i]) {
if (read_packet (oggz, op, serialno, user_data)) return 0;
}
return 0;
}
#endif /* OGGZ_CONFIG_READ */
This diff is collapsed.
/*
Copyright (C) 2003 Commonwealth Scientific and Industrial Research
Organisation (CSIRO) Australia
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
- Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
- Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
- Neither the name of CSIRO Australia nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE ORGANISATION OR
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef __OGGZ_BYTEORDER_H__
#define __OGGZ_BYTEORDER_H__
static __inline__ unsigned short
_le_16 (unsigned short s)
{
unsigned short ret=s;
#ifdef WORDS_BIGENDIAN
ret = (s>>8) & 0x00ffU;
ret += (s<<8) & 0xff00U;
#endif
return ret;
}
static __inline__ ogg_uint32_t
_le_32 (ogg_uint32_t i)
{
ogg_uint32_t ret=i;
#ifdef WORDS_BIGENDIAN
ret = (i>>24);
ret += (i>>8) & 0x0000ff00;
ret += (i<<8) & 0x00ff0000;
ret += (i<<24);
#endif
return ret;
}
static __inline__ ogg_uint32_t
_be_32 (ogg_uint32_t i)
{
ogg_uint32_t ret=i;
#ifndef WORDS_BIGENDIAN
ret = (i>>24);
ret += (i>>8) & 0x0000ff00;
ret += (i<<8) & 0x00ff0000;
ret += (i<<24);
#endif
return ret;
}
static __inline__ ogg_int64_t
_le_64 (ogg_int64_t l)
{
ogg_int64_t ret=l;
unsigned char *ucptr = (unsigned char *)&ret;
#ifdef WORDS_BIGENDIAN
unsigned char temp;
temp = ucptr [0] ;
ucptr [0] = ucptr [7] ;
ucptr [7] = temp ;
temp = ucptr [1] ;
ucptr [1] = ucptr [6] ;
ucptr [6] = temp ;
temp = ucptr [2] ;
ucptr [2] = ucptr [5] ;
ucptr [5] = temp ;
temp = ucptr [3] ;
ucptr [3] = ucptr [4] ;
ucptr [4] = temp ;
#endif
return (*(ogg_int64_t *)ucptr);
}
#endif /* __OGGZ_BYTEORDER_H__ */
/*
Copyright (C) 2003 Commonwealth Scientific and Industrial Research
Organisation (CSIRO) Australia
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
- Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
- Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
- Neither the name of CSIRO Australia nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE ORGANISATION OR
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef __OGGZ_MACROS_H__
#define __OGGZ_MACROS_H__
#include <ogg/ogg.h>
/* Use the malloc and free used by ogg; defaults are those from stdlib */
#define oggz_malloc _ogg_malloc
#define oggz_free _ogg_free
#undef MIN
#define MIN(a,b) ((a)<(b)?(a):(b))
#undef MAX
#define MAX(a,b) ((a)>(b)?(a):(b))
#endif /* __OGGZ_MACROS_H__ */
/*
Copyright (C) 2003 Commonwealth Scientific and Industrial Research
Organisation (CSIRO) Australia
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
- Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
- Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
- Neither the name of CSIRO Australia nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE ORGANISATION OR
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <stdlib.h>
#include "oggz_macros.h"
#include "oggz_vector.h"
typedef struct _OggzTable OggzTable;
struct _OggzTable {
OggzVector * keys;
OggzVector * data;
};
OggzTable *
oggz_table_new (void)
{
OggzTable * table;
table = oggz_malloc (sizeof (OggzTable));
table->keys = oggz_vector_new ();
table->data = oggz_vector_new ();
return table;
}
void
oggz_table_delete (OggzTable * table)
{
if (table == NULL) return;
oggz_vector_delete (table->keys);
oggz_vector_delete (table->data);
oggz_free (table);
}
void *
oggz_table_lookup (OggzTable * table, long key)
{
int i, size;
size = oggz_vector_size (table->keys);
for (i = 0; i < size; i++) {
if (oggz_vector_nth_l (table->keys, i) == key) {
return oggz_vector_nth_p (table->data, i);
}
}
return NULL;
}
void *
oggz_table_insert (OggzTable * table, long key, void * data)
{
void * old_data;
if ((old_data = oggz_table_lookup (table, key)) != NULL) {
if (oggz_vector_remove_l (table->keys, key) == NULL)
return NULL;
if (oggz_vector_remove_p (table->data, old_data) == NULL) {
/* XXX: This error condition can only happen if the previous
* removal succeeded, and this removal failed, ie. there was
* an error reallocing table->data->data downwards. */
return NULL;
}
}
if (oggz_vector_insert_l (table->keys, key) == -1)
return NULL;
if (oggz_vector_insert_p (table->data, data) == NULL) {
oggz_vector_remove_l (table->keys, key);
return NULL;
}
return data;
}
int
oggz_table_size (OggzTable * table)
{
if (table == NULL) return 0;
return oggz_vector_size (table->data);
}
void *
oggz_table_nth (OggzTable * table, int n, long * key)
{
if (table == NULL) return NULL;
if (key) *key = oggz_vector_nth_l (table->keys, n);
return oggz_vector_nth_p (table->data, n);
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment