summaryrefslogtreecommitdiffstats
path: root/apps/codecs
diff options
context:
space:
mode:
Diffstat (limited to 'apps/codecs')
-rw-r--r--apps/codecs/codecs.make3
-rw-r--r--apps/codecs/libasf/SOURCES1
-rw-r--r--apps/codecs/libasf/asf.c354
-rw-r--r--apps/codecs/libasf/asf.h42
-rw-r--r--apps/codecs/libasf/libasf.make25
-rw-r--r--apps/codecs/wma.c374
6 files changed, 429 insertions, 370 deletions
diff --git a/apps/codecs/codecs.make b/apps/codecs/codecs.make
index 1f377abf3d..fa5eae67c5 100644
--- a/apps/codecs/codecs.make
+++ b/apps/codecs/codecs.make
@@ -26,6 +26,7 @@ include $(APPSDIR)/codecs/demac/libdemac.make
include $(APPSDIR)/codecs/liba52/liba52.make
include $(APPSDIR)/codecs/libalac/libalac.make
include $(APPSDIR)/codecs/libasap/libasap.make
+include $(APPSDIR)/codecs/libasf/libasf.make
include $(APPSDIR)/codecs/libfaad/libfaad.make
include $(APPSDIR)/codecs/libffmpegFLAC/libffmpegFLAC.make
include $(APPSDIR)/codecs/libm4a/libm4a.make
@@ -80,7 +81,7 @@ $(CODECDIR)/aac.codec : $(CODECDIR)/libfaad.a $(CODECDIR)/libm4a.a
$(CODECDIR)/shorten.codec : $(CODECDIR)/libffmpegFLAC.a
$(CODECDIR)/ape-pre.map : $(CODECDIR)/libdemac-pre.a
$(CODECDIR)/ape.codec : $(CODECDIR)/libdemac.a
-$(CODECDIR)/wma.codec : $(CODECDIR)/libwma.a
+$(CODECDIR)/wma.codec : $(CODECDIR)/libwma.a $(CODECDIR)/libasf.a
$(CODECDIR)/wavpack_enc.codec: $(CODECDIR)/libwavpack.a
$(CODECDIR)/asap.codec : $(CODECDIR)/libasap.a
$(CODECDIR)/cook.codec : $(CODECDIR)/libcook.a $(CODECDIR)/librm.a
diff --git a/apps/codecs/libasf/SOURCES b/apps/codecs/libasf/SOURCES
new file mode 100644
index 0000000000..1fee336990
--- /dev/null
+++ b/apps/codecs/libasf/SOURCES
@@ -0,0 +1 @@
+asf.c
diff --git a/apps/codecs/libasf/asf.c b/apps/codecs/libasf/asf.c
new file mode 100644
index 0000000000..f65627b057
--- /dev/null
+++ b/apps/codecs/libasf/asf.c
@@ -0,0 +1,354 @@
+#include <inttypes.h>
+#include "codeclib.h"
+#include "asf.h"
+
+/* Read an unaligned 32-bit little endian long from buffer. */
+static unsigned long get_long_le(void* buf)
+{
+ unsigned char* p = (unsigned char*) buf;
+
+ return p[0] | (p[1] << 8) | (p[2] << 16) | (p[3] << 24);
+}
+
+/* Read an unaligned 16-bit little endian short from buffer. */
+static unsigned short get_short_le(void* buf)
+{
+ unsigned char* p = (unsigned char*) buf;
+
+ return p[0] | (p[1] << 8);
+}
+
+#define GETLEN2b(bits) (((bits) == 0x03) ? 4 : bits)
+
+#define GETVALUE2b(bits, data) \
+ (((bits) != 0x03) ? ((bits) != 0x02) ? ((bits) != 0x01) ? \
+ 0 : *(data) : get_short_le(data) : get_long_le(data))
+
+int asf_read_packet(uint8_t** audiobuf, int* audiobufsize, int* packetlength,
+ asf_waveformatex_t* wfx, struct codec_api* ci)
+{
+ uint8_t tmp8, packet_flags, packet_property;
+ int stream_id;
+ int ec_length, opaque_data, ec_length_type;
+ int datalen;
+ uint8_t data[18];
+ uint8_t* datap;
+ uint32_t length;
+ uint32_t padding_length;
+ uint32_t send_time;
+ uint16_t duration;
+ uint16_t payload_count;
+ int payload_length_type;
+ uint32_t payload_hdrlen;
+ int payload_datalen;
+ int multiple;
+ uint32_t replicated_length;
+ uint32_t media_object_number;
+ uint32_t media_object_offset;
+ uint32_t bytesread = 0;
+ uint8_t* buf;
+ size_t bufsize;
+ int i;
+ /*DEBUGF("Reading new packet at %d bytes ", (int)ci->curpos);*/
+
+ if (ci->read_filebuf(&tmp8, 1) == 0) {
+ return ASF_ERROR_EOF;
+ }
+ bytesread++;
+
+ /* TODO: We need a better way to detect endofstream */
+ if (tmp8 != 0x82) {
+ DEBUGF("Read failed: packet did not sync\n");
+ return -1;
+ }
+
+
+ if (tmp8 & 0x80) {
+ ec_length = tmp8 & 0x0f;
+ opaque_data = (tmp8 >> 4) & 0x01;
+ ec_length_type = (tmp8 >> 5) & 0x03;
+
+ if (ec_length_type != 0x00 || opaque_data != 0 || ec_length != 0x02) {
+ DEBUGF("incorrect error correction flags\n");
+ return ASF_ERROR_INVALID_VALUE;
+ }
+
+ /* Skip ec_data */
+ ci->advance_buffer(ec_length);
+ bytesread += ec_length;
+ } else {
+ ec_length = 0;
+ }
+
+ if (ci->read_filebuf(&packet_flags, 1) == 0) { return ASF_ERROR_EOF; }
+ if (ci->read_filebuf(&packet_property, 1) == 0) { return ASF_ERROR_EOF; }
+ bytesread += 2;
+
+ datalen = GETLEN2b((packet_flags >> 1) & 0x03) +
+ GETLEN2b((packet_flags >> 3) & 0x03) +
+ GETLEN2b((packet_flags >> 5) & 0x03) + 6;
+
+#if 0
+ if (datalen > sizeof(data)) {
+ DEBUGF("Unexpectedly long datalen in data - %d\n",datalen);
+ return ASF_ERROR_OUTOFMEM;
+ }
+#endif
+
+ if (ci->read_filebuf(data, datalen) == 0) {
+ return ASF_ERROR_EOF;
+ }
+
+ bytesread += datalen;
+
+ datap = data;
+ length = GETVALUE2b((packet_flags >> 5) & 0x03, datap);
+ datap += GETLEN2b((packet_flags >> 5) & 0x03);
+ /* sequence value is not used */
+ GETVALUE2b((packet_flags >> 1) & 0x03, datap);
+ datap += GETLEN2b((packet_flags >> 1) & 0x03);
+ padding_length = GETVALUE2b((packet_flags >> 3) & 0x03, datap);
+ datap += GETLEN2b((packet_flags >> 3) & 0x03);
+ send_time = get_long_le(datap);
+ datap += 4;
+ duration = get_short_le(datap);
+ datap += 2;
+ /*DEBUGF("and duration %d ms\n", duration);*/
+
+ /* this is really idiotic, packet length can (and often will) be
+ * undefined and we just have to use the header packet size as the size
+ * value */
+ if (!((packet_flags >> 5) & 0x03)) {
+ length = wfx->packet_size;
+ }
+
+ /* this is also really idiotic, if packet length is smaller than packet
+ * size, we need to manually add the additional bytes into padding length
+ */
+ if (length < wfx->packet_size) {
+ padding_length += wfx->packet_size - length;
+ length = wfx->packet_size;
+ }
+
+ if (length > wfx->packet_size) {
+ DEBUGF("packet with too big length value\n");
+ return ASF_ERROR_INVALID_LENGTH;
+ }
+
+ /* check if we have multiple payloads */
+ if (packet_flags & 0x01) {
+ if (ci->read_filebuf(&tmp8, 1) == 0) {
+ return ASF_ERROR_EOF;
+ }
+ payload_count = tmp8 & 0x3f;
+ payload_length_type = (tmp8 >> 6) & 0x03;
+ bytesread++;
+ } else {
+ payload_count = 1;
+ payload_length_type = 0x02; /* not used */
+ }
+
+ if (length < bytesread) {
+ DEBUGF("header exceeded packet size, invalid file - length=%d, bytesread=%d\n",(int)length,(int)bytesread);
+ /* FIXME: should this be checked earlier? */
+ return ASF_ERROR_INVALID_LENGTH;
+ }
+
+
+ /* We now parse the individual payloads, and move all payloads
+ belonging to our audio stream to a contiguous block, starting at
+ the location of the first payload.
+ */
+
+ *audiobuf = NULL;
+ *audiobufsize = 0;
+ *packetlength = length - bytesread;
+
+ buf = ci->request_buffer(&bufsize, length);
+ datap = buf;
+
+ if (bufsize != length) {
+ /* This should only happen with packets larger than 32KB (the
+ guard buffer size). All the streams I've seen have
+ relatively small packets less than about 8KB), but I don't
+ know what is expected.
+ */
+ DEBUGF("Could not read packet (requested %d bytes, received %d), curpos=%d, aborting\n",
+ (int)length,(int)bufsize,(int)ci->curpos);
+ return -1;
+ }
+
+ for (i=0; i<payload_count; i++) {
+ stream_id = datap[0]&0x7f;
+ datap++;
+ bytesread++;
+
+ payload_hdrlen = GETLEN2b(packet_property & 0x03) +
+ GETLEN2b((packet_property >> 2) & 0x03) +
+ GETLEN2b((packet_property >> 4) & 0x03);
+
+ //DEBUGF("payload_hdrlen = %d\n",payload_hdrlen);
+#if 0
+ /* TODO */
+ if (payload_hdrlen > size) {
+ return ASF_ERROR_INVALID_LENGTH;
+ }
+#endif
+ if (payload_hdrlen > sizeof(data)) {
+ DEBUGF("Unexpectedly long datalen in data - %d\n",datalen);
+ return ASF_ERROR_OUTOFMEM;
+ }
+
+ bytesread += payload_hdrlen;
+ media_object_number = GETVALUE2b((packet_property >> 4) & 0x03, datap);
+ datap += GETLEN2b((packet_property >> 4) & 0x03);
+ media_object_offset = GETVALUE2b((packet_property >> 2) & 0x03, datap);
+ datap += GETLEN2b((packet_property >> 2) & 0x03);
+ replicated_length = GETVALUE2b(packet_property & 0x03, datap);
+ datap += GETLEN2b(packet_property & 0x03);
+
+ /* TODO: Validate replicated_length */
+ /* TODO: Is the content of this important for us? */
+ datap += replicated_length;
+ bytesread += replicated_length;
+
+ multiple = packet_flags & 0x01;
+
+
+ if (multiple) {
+ int x;
+
+ x = GETLEN2b(payload_length_type);
+
+ if (x != 2) {
+ /* in multiple payloads datalen should be a word */
+ return ASF_ERROR_INVALID_VALUE;
+ }
+
+#if 0
+ if (skip + tmp > datalen) {
+ /* not enough data */
+ return ASF_ERROR_INVALID_LENGTH;
+ }
+#endif
+ payload_datalen = GETVALUE2b(payload_length_type, datap);
+ datap += x;
+ bytesread += x;
+ } else {
+ payload_datalen = length - bytesread - padding_length;
+ }
+
+ if (replicated_length==1)
+ datap++;
+
+ if (stream_id == wfx->audiostream)
+ {
+ if (*audiobuf == NULL) {
+ /* The first payload can stay where it is */
+ *audiobuf = datap;
+ *audiobufsize = payload_datalen;
+ } else {
+ /* The second and subsequent payloads in this packet
+ that belong to the audio stream need to be moved to be
+ contiguous with the first payload.
+ */
+ memmove(*audiobuf + *audiobufsize, datap, payload_datalen);
+ *audiobufsize += payload_datalen;
+ }
+ }
+ datap += payload_datalen;
+ bytesread += payload_datalen;
+ }
+
+ if (*audiobuf != NULL)
+ return 1;
+ else
+ return 0;
+}
+
+
+int asf_get_timestamp(int *duration, struct codec_api* ci)
+{
+ uint8_t tmp8, packet_flags, packet_property;
+ int ec_length, opaque_data, ec_length_type;
+ int datalen;
+ uint8_t data[18];
+ uint8_t* datap;
+ uint32_t length;
+ uint32_t padding_length;
+ uint32_t send_time;
+ static int packet_count = 0;
+
+ uint32_t bytesread = 0;
+ packet_count++;
+ if (ci->read_filebuf(&tmp8, 1) == 0) {
+ DEBUGF("ASF ERROR (EOF?)\n");
+ return ASF_ERROR_EOF;
+ }
+ bytesread++;
+
+ /* TODO: We need a better way to detect endofstream */
+ if (tmp8 != 0x82) {
+ DEBUGF("Get timestamp: Detected end of stream\n");
+ return ASF_ERROR_EOF;
+ }
+
+
+ if (tmp8 & 0x80) {
+ ec_length = tmp8 & 0x0f;
+ opaque_data = (tmp8 >> 4) & 0x01;
+ ec_length_type = (tmp8 >> 5) & 0x03;
+
+ if (ec_length_type != 0x00 || opaque_data != 0 || ec_length != 0x02) {
+ DEBUGF("incorrect error correction flags\n");
+ return ASF_ERROR_INVALID_VALUE;
+ }
+
+ /* Skip ec_data */
+ ci->advance_buffer(ec_length);
+ bytesread += ec_length;
+ } else {
+ ec_length = 0;
+ }
+
+ if (ci->read_filebuf(&packet_flags, 1) == 0) {
+ DEBUGF("Detected end of stream 2\n");
+ return ASF_ERROR_EOF;
+ }
+
+ if (ci->read_filebuf(&packet_property, 1) == 0) {
+ DEBUGF("Detected end of stream3\n");
+ return ASF_ERROR_EOF;
+ }
+ bytesread += 2;
+
+ datalen = GETLEN2b((packet_flags >> 1) & 0x03) +
+ GETLEN2b((packet_flags >> 3) & 0x03) +
+ GETLEN2b((packet_flags >> 5) & 0x03) + 6;
+
+ if (ci->read_filebuf(data, datalen) == 0) {
+ DEBUGF("Detected end of stream4\n");
+ return ASF_ERROR_EOF;
+ }
+
+ bytesread += datalen;
+
+ datap = data;
+ length = GETVALUE2b((packet_flags >> 5) & 0x03, datap);
+ datap += GETLEN2b((packet_flags >> 5) & 0x03);
+
+ /* sequence value is not used */
+ GETVALUE2b((packet_flags >> 1) & 0x03, datap);
+ datap += GETLEN2b((packet_flags >> 1) & 0x03);
+ padding_length = GETVALUE2b((packet_flags >> 3) & 0x03, datap);
+ datap += GETLEN2b((packet_flags >> 3) & 0x03);
+ send_time = get_long_le(datap);
+ datap += 4;
+ *duration = get_short_le(datap);
+
+ /*the asf_get_timestamp function advances us 12-13 bytes past the packet start,
+ need to undo this here so that we stay synced with the packet*/
+ ci->seek_buffer(ci->curpos-bytesread);
+
+ return send_time;
+}
diff --git a/apps/codecs/libasf/asf.h b/apps/codecs/libasf/asf.h
new file mode 100644
index 0000000000..03af2b0de1
--- /dev/null
+++ b/apps/codecs/libasf/asf.h
@@ -0,0 +1,42 @@
+#ifndef _ASF_H
+#define _ASF_H
+
+#include <inttypes.h>
+
+/* ASF codec IDs */
+#define ASF_CODEC_ID_WMAV1 0x160
+#define ASF_CODEC_ID_WMAV2 0x161
+
+enum asf_error_e {
+ ASF_ERROR_INTERNAL = -1, /* incorrect input to API calls */
+ ASF_ERROR_OUTOFMEM = -2, /* some malloc inside program failed */
+ ASF_ERROR_EOF = -3, /* unexpected end of file */
+ ASF_ERROR_IO = -4, /* error reading or writing to file */
+ ASF_ERROR_INVALID_LENGTH = -5, /* length value conflict in input data */
+ ASF_ERROR_INVALID_VALUE = -6, /* other value conflict in input data */
+ ASF_ERROR_INVALID_OBJECT = -7, /* ASF object missing or in wrong place */
+ ASF_ERROR_OBJECT_SIZE = -8, /* invalid ASF object size (too small) */
+ ASF_ERROR_SEEKABLE = -9, /* file not seekable */
+ ASF_ERROR_SEEK = -10 /* file is seekable but seeking failed */
+};
+
+struct asf_waveformatex_s {
+ uint32_t packet_size;
+ int audiostream;
+ uint16_t codec_id;
+ uint16_t channels;
+ uint32_t rate;
+ uint32_t bitrate;
+ uint16_t blockalign;
+ uint16_t bitspersample;
+ uint16_t datalen;
+ uint8_t data[6];
+};
+typedef struct asf_waveformatex_s asf_waveformatex_t;
+
+int asf_read_packet(uint8_t** audiobuf, int* audiobufsize, int* packetlength,
+ asf_waveformatex_t* wfx, struct codec_api* ci);
+
+int asf_get_timestamp(int *duration, struct codec_api* ci);
+
+#endif
diff --git a/apps/codecs/libasf/libasf.make b/apps/codecs/libasf/libasf.make
new file mode 100644
index 0000000000..31d80a33f8
--- /dev/null
+++ b/apps/codecs/libasf/libasf.make
@@ -0,0 +1,25 @@
+# __________ __ ___.
+# Open \______ \ ____ ____ | | _\_ |__ _______ ___
+# Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
+# Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
+# Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
+# \/ \/ \/ \/ \/
+# $Id$
+#
+
+# libasf
+ASFLIB := $(CODECDIR)/libasf.a
+ASFLIB_SRC := $(call preprocess, $(APPSDIR)/codecs/libasf/SOURCES)
+ASFLIB_OBJ := $(call c2obj, $(ASFLIB_SRC))
+OTHER_SRC += $(ASFLIB_SRC)
+
+$(ASFLIB): $(ASFLIB_OBJ)
+ $(SILENT)$(shell rm -f $@)
+ $(call PRINTS,AR $(@F))$(AR) rcs $@ $^ >/dev/null
+
+ASFFLAGS = $(filter-out -O%,$(CODECFLAGS))
+ASFFLAGS += -O3
+
+$(CODECDIR)/libasf/%.o: $(ROOTDIR)/apps/codecs/libasf/%.c
+ $(SILENT)mkdir -p $(dir $@)
+ $(call PRINTS,CC $(subst $(ROOTDIR)/,,$<))$(CC) $(ASFFLAGS) -c $< -o $@
diff --git a/apps/codecs/wma.c b/apps/codecs/wma.c
index 8f8b91bd4a..4764093383 100644
--- a/apps/codecs/wma.c
+++ b/apps/codecs/wma.c
@@ -24,13 +24,11 @@
****************************************************************************/
#include "codeclib.h"
-#include "libwma/asf.h"
+#include "libasf/asf.h"
#include "libwma/wmadec.h"
CODEC_HEADER
-int packet_count=0;
-
/* The output buffer containing the decoded samples (channels 0 and 1)
BLOCK_MAX_SIZE is 2048 (samples) and MAX_CHANNELS is 2.
*/
@@ -40,368 +38,6 @@ static uint32_t decoded[BLOCK_MAX_SIZE * MAX_CHANNELS] IBSS_ATTR;
/* NOTE: WMADecodeContext is 120152 bytes (on x86) */
static WMADecodeContext wmadec;
-enum asf_error_e {
- ASF_ERROR_INTERNAL = -1, /* incorrect input to API calls */
- ASF_ERROR_OUTOFMEM = -2, /* some malloc inside program failed */
- ASF_ERROR_EOF = -3, /* unexpected end of file */
- ASF_ERROR_IO = -4, /* error reading or writing to file */
- ASF_ERROR_INVALID_LENGTH = -5, /* length value conflict in input data */
- ASF_ERROR_INVALID_VALUE = -6, /* other value conflict in input data */
- ASF_ERROR_INVALID_OBJECT = -7, /* ASF object missing or in wrong place */
- ASF_ERROR_OBJECT_SIZE = -8, /* invalid ASF object size (too small) */
- ASF_ERROR_SEEKABLE = -9, /* file not seekable */
- ASF_ERROR_SEEK = -10 /* file is seekable but seeking failed */
-};
-
-/* Read an unaligned 32-bit little endian long from buffer. */
-static unsigned long get_long_le(void* buf)
-{
- unsigned char* p = (unsigned char*) buf;
-
- return p[0] | (p[1] << 8) | (p[2] << 16) | (p[3] << 24);
-}
-
-/* Read an unaligned 16-bit little endian short from buffer. */
-static unsigned short get_short_le(void* buf)
-{
- unsigned char* p = (unsigned char*) buf;
-
- return p[0] | (p[1] << 8);
-}
-
-#define GETLEN2b(bits) (((bits) == 0x03) ? 4 : bits)
-
-#define GETVALUE2b(bits, data) \
- (((bits) != 0x03) ? ((bits) != 0x02) ? ((bits) != 0x01) ? \
- 0 : *(data) : get_short_le(data) : get_long_le(data))
-
-static int asf_read_packet(uint8_t** audiobuf, int* audiobufsize, int* packetlength, asf_waveformatex_t* wfx)
-{
- uint8_t tmp8, packet_flags, packet_property;
- int stream_id;
- int ec_length, opaque_data, ec_length_type;
- int datalen;
- uint8_t data[18];
- uint8_t* datap;
- uint32_t length;
- uint32_t padding_length;
- uint32_t send_time;
- uint16_t duration;
- uint16_t payload_count;
- int payload_length_type;
- uint32_t payload_hdrlen;
- int payload_datalen;
- int multiple;
- uint32_t replicated_length;
- uint32_t media_object_number;
- uint32_t media_object_offset;
- uint32_t bytesread = 0;
- uint8_t* buf;
- size_t bufsize;
- int i;
- /*DEBUGF("Reading new packet at %d bytes ", (int)ci->curpos);*/
-
- if (ci->read_filebuf(&tmp8, 1) == 0) {
- return ASF_ERROR_EOF;
- }
- bytesread++;
-
- /* TODO: We need a better way to detect endofstream */
- if (tmp8 != 0x82) {
- DEBUGF("Read failed: packet did not sync\n");
- return -1;
- }
-
-
- if (tmp8 & 0x80) {
- ec_length = tmp8 & 0x0f;
- opaque_data = (tmp8 >> 4) & 0x01;
- ec_length_type = (tmp8 >> 5) & 0x03;
-
- if (ec_length_type != 0x00 || opaque_data != 0 || ec_length != 0x02) {
- DEBUGF("incorrect error correction flags\n");
- return ASF_ERROR_INVALID_VALUE;
- }
-
- /* Skip ec_data */
- ci->advance_buffer(ec_length);
- bytesread += ec_length;
- } else {
- ec_length = 0;
- }
-
- if (ci->read_filebuf(&packet_flags, 1) == 0) { return ASF_ERROR_EOF; }
- if (ci->read_filebuf(&packet_property, 1) == 0) { return ASF_ERROR_EOF; }
- bytesread += 2;
-
- datalen = GETLEN2b((packet_flags >> 1) & 0x03) +
- GETLEN2b((packet_flags >> 3) & 0x03) +
- GETLEN2b((packet_flags >> 5) & 0x03) + 6;
-
-#if 0
- if (datalen > sizeof(data)) {
- DEBUGF("Unexpectedly long datalen in data - %d\n",datalen);
- return ASF_ERROR_OUTOFMEM;
- }
-#endif
-
- if (ci->read_filebuf(data, datalen) == 0) {
- return ASF_ERROR_EOF;
- }
-
- bytesread += datalen;
-
- datap = data;
- length = GETVALUE2b((packet_flags >> 5) & 0x03, datap);
- datap += GETLEN2b((packet_flags >> 5) & 0x03);
- /* sequence value is not used */
- GETVALUE2b((packet_flags >> 1) & 0x03, datap);
- datap += GETLEN2b((packet_flags >> 1) & 0x03);
- padding_length = GETVALUE2b((packet_flags >> 3) & 0x03, datap);
- datap += GETLEN2b((packet_flags >> 3) & 0x03);
- send_time = get_long_le(datap);
- datap += 4;
- duration = get_short_le(datap);
- datap += 2;
- /*DEBUGF("and duration %d ms\n", duration);*/
-
- /* this is really idiotic, packet length can (and often will) be
- * undefined and we just have to use the header packet size as the size
- * value */
- if (!((packet_flags >> 5) & 0x03)) {
- length = wfx->packet_size;
- }
-
- /* this is also really idiotic, if packet length is smaller than packet
- * size, we need to manually add the additional bytes into padding length
- */
- if (length < wfx->packet_size) {
- padding_length += wfx->packet_size - length;
- length = wfx->packet_size;
- }
-
- if (length > wfx->packet_size) {
- DEBUGF("packet with too big length value\n");
- return ASF_ERROR_INVALID_LENGTH;
- }
-
- /* check if we have multiple payloads */
- if (packet_flags & 0x01) {
- if (ci->read_filebuf(&tmp8, 1) == 0) {
- return ASF_ERROR_EOF;
- }
- payload_count = tmp8 & 0x3f;
- payload_length_type = (tmp8 >> 6) & 0x03;
- bytesread++;
- } else {
- payload_count = 1;
- payload_length_type = 0x02; /* not used */
- }
-
- if (length < bytesread) {
- DEBUGF("header exceeded packet size, invalid file - length=%d, bytesread=%d\n",(int)length,(int)bytesread);
- /* FIXME: should this be checked earlier? */
- return ASF_ERROR_INVALID_LENGTH;
- }
-
-
- /* We now parse the individual payloads, and move all payloads
- belonging to our audio stream to a contiguous block, starting at
- the location of the first payload.
- */
-
- *audiobuf = NULL;
- *audiobufsize = 0;
- *packetlength = length - bytesread;
-
- buf = ci->request_buffer(&bufsize, length);
- datap = buf;
-
- if (bufsize != length) {
- /* This should only happen with packets larger than 32KB (the
- guard buffer size). All the streams I've seen have
- relatively small packets less than about 8KB), but I don't
- know what is expected.
- */
- DEBUGF("Could not read packet (requested %d bytes, received %d), curpos=%d, aborting\n",
- (int)length,(int)bufsize,(int)ci->curpos);
- return -1;
- }
-
- for (i=0; i<payload_count; i++) {
- stream_id = datap[0]&0x7f;
- datap++;
- bytesread++;
-
- payload_hdrlen = GETLEN2b(packet_property & 0x03) +
- GETLEN2b((packet_property >> 2) & 0x03) +
- GETLEN2b((packet_property >> 4) & 0x03);
-
- //DEBUGF("payload_hdrlen = %d\n",payload_hdrlen);
-#if 0
- /* TODO */
- if (payload_hdrlen > size) {
- return ASF_ERROR_INVALID_LENGTH;
- }
-#endif
- if (payload_hdrlen > sizeof(data)) {
- DEBUGF("Unexpectedly long datalen in data - %d\n",datalen);
- return ASF_ERROR_OUTOFMEM;
- }
-
- bytesread += payload_hdrlen;
- media_object_number = GETVALUE2b((packet_property >> 4) & 0x03, datap);
- datap += GETLEN2b((packet_property >> 4) & 0x03);
- media_object_offset = GETVALUE2b((packet_property >> 2) & 0x03, datap);
- datap += GETLEN2b((packet_property >> 2) & 0x03);
- replicated_length = GETVALUE2b(packet_property & 0x03, datap);
- datap += GETLEN2b(packet_property & 0x03);
-
- /* TODO: Validate replicated_length */
- /* TODO: Is the content of this important for us? */
- datap += replicated_length;
- bytesread += replicated_length;
-
- multiple = packet_flags & 0x01;
-
-
- if (multiple) {
- int x;
-
- x = GETLEN2b(payload_length_type);
-
- if (x != 2) {
- /* in multiple payloads datalen should be a word */
- return ASF_ERROR_INVALID_VALUE;
- }
-
-#if 0
- if (skip + tmp > datalen) {
- /* not enough data */
- return ASF_ERROR_INVALID_LENGTH;
- }
-#endif
- payload_datalen = GETVALUE2b(payload_length_type, datap);
- datap += x;
- bytesread += x;
- } else {
- payload_datalen = length - bytesread - padding_length;
- }
-
- if (replicated_length==1)
- datap++;
-
- if (stream_id == wfx->audiostream)
- {
- if (*audiobuf == NULL) {
- /* The first payload can stay where it is */
- *audiobuf = datap;
- *audiobufsize = payload_datalen;
- } else {
- /* The second and subsequent payloads in this packet
- that belong to the audio stream need to be moved to be
- contiguous with the first payload.
- */
- memmove(*audiobuf + *audiobufsize, datap, payload_datalen);
- *audiobufsize += payload_datalen;
- }
- }
- datap += payload_datalen;
- bytesread += payload_datalen;
- }
-
- if (*audiobuf != NULL)
- return 1;
- else
- return 0;
-}
-
-
-static int get_timestamp(int *duration)
-{
- uint8_t tmp8, packet_flags, packet_property;
- int ec_length, opaque_data, ec_length_type;
- int datalen;
- uint8_t data[18];
- uint8_t* datap;
- uint32_t length;
- uint32_t padding_length;
- uint32_t send_time;
-
- uint32_t bytesread = 0;
- packet_count++;
- if (ci->read_filebuf(&tmp8, 1) == 0) {
- DEBUGF("ASF ERROR (EOF?)\n");
- return ASF_ERROR_EOF;
- }
- bytesread++;
-
- /* TODO: We need a better way to detect endofstream */
- if (tmp8 != 0x82) {
- DEBUGF("Get timestamp: Detected end of stream\n");
- return ASF_ERROR_EOF;
- }
-
-
- if (tmp8 & 0x80) {
- ec_length = tmp8 & 0x0f;
- opaque_data = (tmp8 >> 4) & 0x01;
- ec_length_type = (tmp8 >> 5) & 0x03;
-
- if (ec_length_type != 0x00 || opaque_data != 0 || ec_length != 0x02) {
- DEBUGF("incorrect error correction flags\n");
- return ASF_ERROR_INVALID_VALUE;
- }
-
- /* Skip ec_data */
- ci->advance_buffer(ec_length);
- bytesread += ec_length;
- } else {
- ec_length = 0;
- }
-
- if (ci->read_filebuf(&packet_flags, 1) == 0) {
- DEBUGF("Detected end of stream 2\n");
- return ASF_ERROR_EOF;
- }
-
- if (ci->read_filebuf(&packet_property, 1) == 0) {
- DEBUGF("Detected end of stream3\n");
- return ASF_ERROR_EOF;
- }
- bytesread += 2;
-
- datalen = GETLEN2b((packet_flags >> 1) & 0x03) +
- GETLEN2b((packet_flags >> 3) & 0x03) +
- GETLEN2b((packet_flags >> 5) & 0x03) + 6;
-
- if (ci->read_filebuf(data, datalen) == 0) {
- DEBUGF("Detected end of stream4\n");
- return ASF_ERROR_EOF;
- }
-
- bytesread += datalen;
-
- datap = data;
- length = GETVALUE2b((packet_flags >> 5) & 0x03, datap);
- datap += GETLEN2b((packet_flags >> 5) & 0x03);
-
- /* sequence value is not used */
- GETVALUE2b((packet_flags >> 1) & 0x03, datap);
- datap += GETLEN2b((packet_flags >> 1) & 0x03);
- padding_length = GETVALUE2b((packet_flags >> 3) & 0x03, datap);
- datap += GETLEN2b((packet_flags >> 3) & 0x03);
- send_time = get_long_le(datap);
- datap += 4;
- *duration = get_short_le(datap);
-
- /*the get_timestamp function advances us 12-13 bytes past the packet start,
- need to undo this here so that we stay synced with the packet*/
- ci->seek_buffer(ci->curpos-bytesread);
-
- return send_time;
-}
-
/*entry point for seeks*/
static int seek(int ms, asf_waveformatex_t* wfx)
{
@@ -428,7 +64,7 @@ static int seek(int ms, asf_waveformatex_t* wfx)
count++;
/*check the time stamp of our packet*/
- time = get_timestamp(&duration);
+ time = asf_get_timestamp(&duration, ci);
DEBUGF("seeked to %d ms with duration %d\n", time, duration);
if (time < 0) {
@@ -436,7 +72,7 @@ static int seek(int ms, asf_waveformatex_t* wfx)
DEBUGF("UKNOWN SEEK ERROR\n");
ci->seek_buffer(ci->id3->first_frame_offset+initial_packet*wfx->packet_size);
/*seek failed so return time stamp of the initial packet*/
- return get_timestamp(&duration);
+ return asf_get_timestamp(&duration, ci);
}
if ((time+duration>=ms && time<=ms) || count > 10) {
@@ -509,7 +145,7 @@ restart_track:
int packet_offset = (resume_offset - ci->id3->first_frame_offset)
% wfx.packet_size;
ci->seek_buffer(resume_offset - packet_offset);
- elapsedtime = get_timestamp(&i);
+ elapsedtime = asf_get_timestamp(&i, ci);
ci->set_elapsed(elapsedtime);
}
else
@@ -558,7 +194,7 @@ restart_track:
}
errcount = 0;
new_packet:
- res = asf_read_packet(&audiobuf, &audiobufsize, &packetlength, &wfx);
+ res = asf_read_packet(&audiobuf, &audiobufsize, &packetlength, &wfx, ci);
if (res < 0) {
/* We'll try to recover from a parse error a certain number of