summaryrefslogtreecommitdiff
path: root/apps
diff options
context:
space:
mode:
authorMohamed Tarek <mt@rockbox.org>2010-05-02 17:19:42 +0000
committerMohamed Tarek <mt@rockbox.org>2010-05-02 17:19:42 +0000
commit792ae6b1bd7ec85f2ab8eec34dad1d22b05a213e (patch)
tree83a8976bba26b5252061334292bbf6e4e57fa628 /apps
parent13075dea876898afdb195056d22a921b18cd4450 (diff)
downloadrockbox-792ae6b1bd7ec85f2ab8eec34dad1d22b05a213e.tar.gz
rockbox-792ae6b1bd7ec85f2ab8eec34dad1d22b05a213e.zip
- Factor out container specific code from apps/codecs/wma.c.
- Create an independent asf packet-parsing library in apps/codecs/libasf. - Modify wma.c to use the newly created libasf. git-svn-id: svn://svn.rockbox.org/rockbox/trunk@25780 a1c6a512-1295-4272-9138-f99709370657
Diffstat (limited to 'apps')
-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
26include $(APPSDIR)/codecs/liba52/liba52.make 26include $(APPSDIR)/codecs/liba52/liba52.make
27include $(APPSDIR)/codecs/libalac/libalac.make 27include $(APPSDIR)/codecs/libalac/libalac.make
28include $(APPSDIR)/codecs/libasap/libasap.make 28include $(APPSDIR)/codecs/libasap/libasap.make
29include $(APPSDIR)/codecs/libasf/libasf.make
29include $(APPSDIR)/codecs/libfaad/libfaad.make 30include $(APPSDIR)/codecs/libfaad/libfaad.make
30include $(APPSDIR)/codecs/libffmpegFLAC/libffmpegFLAC.make 31include $(APPSDIR)/codecs/libffmpegFLAC/libffmpegFLAC.make
31include $(APPSDIR)/codecs/libm4a/libm4a.make 32include $(APPSDIR)/codecs/libm4a/libm4a.make
@@ -80,7 +81,7 @@ $(CODECDIR)/aac.codec : $(CODECDIR)/libfaad.a $(CODECDIR)/libm4a.a
80$(CODECDIR)/shorten.codec : $(CODECDIR)/libffmpegFLAC.a 81$(CODECDIR)/shorten.codec : $(CODECDIR)/libffmpegFLAC.a
81$(CODECDIR)/ape-pre.map : $(CODECDIR)/libdemac-pre.a 82$(CODECDIR)/ape-pre.map : $(CODECDIR)/libdemac-pre.a
82$(CODECDIR)/ape.codec : $(CODECDIR)/libdemac.a 83$(CODECDIR)/ape.codec : $(CODECDIR)/libdemac.a
83$(CODECDIR)/wma.codec : $(CODECDIR)/libwma.a 84$(CODECDIR)/wma.codec : $(CODECDIR)/libwma.a $(CODECDIR)/libasf.a
84$(CODECDIR)/wavpack_enc.codec: $(CODECDIR)/libwavpack.a 85$(CODECDIR)/wavpack_enc.codec: $(CODECDIR)/libwavpack.a
85$(CODECDIR)/asap.codec : $(CODECDIR)/libasap.a 86$(CODECDIR)/asap.codec : $(CODECDIR)/libasap.a
86$(CODECDIR)/cook.codec : $(CODECDIR)/libcook.a $(CODECDIR)/librm.a 87$(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 @@
1#include <inttypes.h>
2#include "codeclib.h"
3#include "asf.h"
4
5/* Read an unaligned 32-bit little endian long from buffer. */
6static unsigned long get_long_le(void* buf)
7{
8 unsigned char* p = (unsigned char*) buf;
9
10 return p[0] | (p[1] << 8) | (p[2] << 16) | (p[3] << 24);
11}
12
13/* Read an unaligned 16-bit little endian short from buffer. */
14static unsigned short get_short_le(void* buf)
15{
16 unsigned char* p = (unsigned char*) buf;
17
18 return p[0] | (p[1] << 8);
19}
20
21#define GETLEN2b(bits) (((bits) == 0x03) ? 4 : bits)
22
23#define GETVALUE2b(bits, data) \
24 (((bits) != 0x03) ? ((bits) != 0x02) ? ((bits) != 0x01) ? \
25 0 : *(data) : get_short_le(data) : get_long_le(data))
26
27int asf_read_packet(uint8_t** audiobuf, int* audiobufsize, int* packetlength,
28 asf_waveformatex_t* wfx, struct codec_api* ci)
29{
30 uint8_t tmp8, packet_flags, packet_property;
31 int stream_id;
32 int ec_length, opaque_data, ec_length_type;
33 int datalen;
34 uint8_t data[18];
35 uint8_t* datap;
36 uint32_t length;
37 uint32_t padding_length;
38 uint32_t send_time;
39 uint16_t duration;
40 uint16_t payload_count;
41 int payload_length_type;
42 uint32_t payload_hdrlen;
43 int payload_datalen;
44 int multiple;
45 uint32_t replicated_length;
46 uint32_t media_object_number;
47 uint32_t media_object_offset;
48 uint32_t bytesread = 0;
49 uint8_t* buf;
50 size_t bufsize;
51 int i;
52 /*DEBUGF("Reading new packet at %d bytes ", (int)ci->curpos);*/
53
54 if (ci->read_filebuf(&tmp8, 1) == 0) {
55 return ASF_ERROR_EOF;
56 }
57 bytesread++;
58
59 /* TODO: We need a better way to detect endofstream */
60 if (tmp8 != 0x82) {
61 DEBUGF("Read failed: packet did not sync\n");
62 return -1;
63 }
64
65
66 if (tmp8 & 0x80) {
67 ec_length = tmp8 & 0x0f;
68 opaque_data = (tmp8 >> 4) & 0x01;
69 ec_length_type = (tmp8 >> 5) & 0x03;
70
71 if (ec_length_type != 0x00 || opaque_data != 0 || ec_length != 0x02) {
72 DEBUGF("incorrect error correction flags\n");
73 return ASF_ERROR_INVALID_VALUE;
74 }
75
76 /* Skip ec_data */
77 ci->advance_buffer(ec_length);
78 bytesread += ec_length;
79 } else {
80 ec_length = 0;
81 }
82
83 if (ci->read_filebuf(&packet_flags, 1) == 0) { return ASF_ERROR_EOF; }
84 if (ci->read_filebuf(&packet_property, 1) == 0) { return ASF_ERROR_EOF; }
85 bytesread += 2;
86
87 datalen = GETLEN2b((packet_flags >> 1) & 0x03) +
88 GETLEN2b((packet_flags >> 3) & 0x03) +
89 GETLEN2b((packet_flags >> 5) & 0x03) + 6;
90
91#if 0
92 if (datalen > sizeof(data)) {
93 DEBUGF("Unexpectedly long datalen in data - %d\n",datalen);
94 return ASF_ERROR_OUTOFMEM;
95 }
96#endif
97
98 if (ci->read_filebuf(data, datalen) == 0) {
99 return ASF_ERROR_EOF;
100 }
101
102 bytesread += datalen;
103
104 datap = data;
105 length = GETVALUE2b((packet_flags >> 5) & 0x03, datap);
106 datap += GETLEN2b((packet_flags >> 5) & 0x03);
107 /* sequence value is not used */
108 GETVALUE2b((packet_flags >> 1) & 0x03, datap);
109 datap += GETLEN2b((packet_flags >> 1) & 0x03);
110 padding_length = GETVALUE2b((packet_flags >> 3) & 0x03, datap);
111 datap += GETLEN2b((packet_flags >> 3) & 0x03);
112 send_time = get_long_le(datap);
113 datap += 4;
114 duration = get_short_le(datap);
115 datap += 2;
116 /*DEBUGF("and duration %d ms\n", duration);*/
117
118 /* this is really idiotic, packet length can (and often will) be
119 * undefined and we just have to use the header packet size as the size
120 * value */
121 if (!((packet_flags >> 5) & 0x03)) {
122 length = wfx->packet_size;
123 }
124
125 /* this is also really idiotic, if packet length is smaller than packet
126 * size, we need to manually add the additional bytes into padding length
127 */
128 if (length < wfx->packet_size) {
129 padding_length += wfx->packet_size - length;
130 length = wfx->packet_size;
131 }
132
133 if (length > wfx->packet_size) {
134 DEBUGF("packet with too big length value\n");
135 return ASF_ERROR_INVALID_LENGTH;
136 }
137
138 /* check if we have multiple payloads */
139 if (packet_flags & 0x01) {
140 if (ci->read_filebuf(&tmp8, 1) == 0) {
141 return ASF_ERROR_EOF;
142 }
143 payload_count = tmp8 & 0x3f;
144 payload_length_type = (tmp8 >> 6) & 0x03;
145 bytesread++;
146 } else {
147 payload_count = 1;
148 payload_length_type = 0x02; /* not used */
149 }
150
151 if (length < bytesread) {
152 DEBUGF("header exceeded packet size, invalid file - length=%d, bytesread=%d\n",(int)length,(int)bytesread);
153 /* FIXME: should this be checked earlier? */
154 return ASF_ERROR_INVALID_LENGTH;
155 }
156
157
158 /* We now parse the individual payloads, and move all payloads
159 belonging to our audio stream to a contiguous block, starting at
160 the location of the first payload.
161 */
162
163 *audiobuf = NULL;
164 *audiobufsize = 0;
165 *packetlength = length - bytesread;
166
167 buf = ci->request_buffer(&bufsize, length);
168 datap = buf;
169
170 if (bufsize != length) {
171 /* This should only happen with packets larger than 32KB (the
172 guard buffer size). All the streams I've seen have
173 relatively small packets less than about 8KB), but I don't
174 know what is expected.
175 */
176 DEBUGF("Could not read packet (requested %d bytes, received %d), curpos=%d, aborting\n",
177 (int)length,(int)bufsize,(int)ci->curpos);
178 return -1;
179 }
180
181 for (i=0; i<payload_count; i++) {
182 stream_id = datap[0]&0x7f;
183 datap++;
184 bytesread++;
185
186 payload_hdrlen = GETLEN2b(packet_property & 0x03) +
187 GETLEN2b((packet_property >> 2) & 0x03) +
188 GETLEN2b((packet_property >> 4) & 0x03);
189
190 //DEBUGF("payload_hdrlen = %d\n",payload_hdrlen);
191#if 0
192 /* TODO */
193 if (payload_hdrlen > size) {
194 return ASF_ERROR_INVALID_LENGTH;
195 }
196#endif
197 if (payload_hdrlen > sizeof(data)) {
198 DEBUGF("Unexpectedly long datalen in data - %d\n",datalen);
199 return ASF_ERROR_OUTOFMEM;
200 }
201
202 bytesread += payload_hdrlen;
203 media_object_number = GETVALUE2b((packet_property >> 4) & 0x03, datap);
204 datap += GETLEN2b((packet_property >> 4) & 0x03);
205 media_object_offset = GETVALUE2b((packet_property >> 2) & 0x03, datap);
206 datap += GETLEN2b((packet_property >> 2) & 0x03);
207 replicated_length = GETVALUE2b(packet_property & 0x03, datap);
208 datap += GETLEN2b(packet_property & 0x03);
209
210 /* TODO: Validate replicated_length */
211 /* TODO: Is the content of this important for us? */
212 datap += replicated_length;
213 bytesread += replicated_length;
214
215 multiple = packet_flags & 0x01;
216
217
218 if (multiple) {
219 int x;
220
221 x = GETLEN2b(payload_length_type);
222
223 if (x != 2) {
224 /* in multiple payloads datalen should be a word */
225 return ASF_ERROR_INVALID_VALUE;
226 }
227
228#if 0
229 if (skip + tmp > datalen) {
230 /* not enough data */
231 return ASF_ERROR_INVALID_LENGTH;
232 }
233#endif
234 payload_datalen = GETVALUE2b(payload_length_type, datap);
235 datap += x;
236 bytesread += x;
237 } else {
238 payload_datalen = length - bytesread - padding_length;
239 }
240
241 if (replicated_length==1)
242 datap++;
243
244 if (stream_id == wfx->audiostream)
245 {
246 if (*audiobuf == NULL) {
247 /* The first payload can stay where it is */
248 *audiobuf = datap;
249 *audiobufsize = payload_datalen;
250 } else {
251 /* The second and subsequent payloads in this packet
252 that belong to the audio stream need to be moved to be
253 contiguous with the first payload.
254 */
255 memmove(*audiobuf + *audiobufsize, datap, payload_datalen);
256 *audiobufsize += payload_datalen;
257 }
258 }
259 datap += payload_datalen;
260 bytesread += payload_datalen;
261 }
262
263 if (*audiobuf != NULL)
264 return 1;
265 else
266 return 0;
267}
268
269
270int asf_get_timestamp(int *duration, struct codec_api* ci)
271{
272 uint8_t tmp8, packet_flags, packet_property;
273 int ec_length, opaque_data, ec_length_type;
274 int datalen;
275 uint8_t data[18];
276 uint8_t* datap;
277 uint32_t length;
278 uint32_t padding_length;
279 uint32_t send_time;
280 static int packet_count = 0;
281
282 uint32_t bytesread = 0;
283 packet_count++;
284 if (ci->read_filebuf(&tmp8, 1) == 0) {
285 DEBUGF("ASF ERROR (EOF?)\n");
286 return ASF_ERROR_EOF;
287 }
288 bytesread++;
289
290 /* TODO: We need a better way to detect endofstream */
291 if (tmp8 != 0x82) {
292 DEBUGF("Get timestamp: Detected end of stream\n");
293 return ASF_ERROR_EOF;
294 }
295
296
297 if (tmp8 & 0x80) {
298 ec_length = tmp8 & 0x0f;
299 opaque_data = (tmp8 >> 4) & 0x01;
300 ec_length_type = (tmp8 >> 5) & 0x03;
301
302 if (ec_length_type != 0x00 || opaque_data != 0 || ec_length != 0x02) {
303 DEBUGF("incorrect error correction flags\n");
304 return ASF_ERROR_INVALID_VALUE;
305 }
306
307 /* Skip ec_data */
308 ci->advance_buffer(ec_length);
309 bytesread += ec_length;
310 } else {
311 ec_length = 0;
312 }
313
314 if (ci->read_filebuf(&packet_flags, 1) == 0) {
315 DEBUGF("Detected end of stream 2\n");
316 return ASF_ERROR_EOF;
317 }
318
319 if (ci->read_filebuf(&packet_property, 1) == 0) {
320 DEBUGF("Detected end of stream3\n");
321 return ASF_ERROR_EOF;
322 }
323 bytesread += 2;
324
325 datalen = GETLEN2b((packet_flags >> 1) & 0x03) +
326 GETLEN2b((packet_flags >> 3) & 0x03) +
327 GETLEN2b((packet_flags >> 5) & 0x03) + 6;
328
329 if (ci->read_filebuf(data, datalen) == 0) {
330 DEBUGF("Detected end of stream4\n");
331 return ASF_ERROR_EOF;
332 }
333
334 bytesread += datalen;
335
336 datap = data;
337 length = GETVALUE2b((packet_flags >> 5) & 0x03, datap);
338 datap += GETLEN2b((packet_flags >> 5) & 0x03);
339
340 /* sequence value is not used */
341 GETVALUE2b((packet_flags >> 1) & 0x03, datap);
342 datap += GETLEN2b((packet_flags >> 1) & 0x03);
343 padding_length = GETVALUE2b((packet_flags >> 3) & 0x03, datap);
344 datap += GETLEN2b((packet_flags >> 3) & 0x03);
345 send_time = get_long_le(datap);
346 datap += 4;
347 *duration = get_short_le(datap);
348
349 /*the asf_get_timestamp function advances us 12-13 bytes past the packet start,
350 need to undo this here so that we stay synced with the packet*/
351 ci->seek_buffer(ci->curpos-bytesread);
352
353 return send_time;
354}
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 @@
1#ifndef _ASF_H
2#define _ASF_H
3
4#include <inttypes.h>
5
6/* ASF codec IDs */
7#define ASF_CODEC_ID_WMAV1 0x160
8#define ASF_CODEC_ID_WMAV2 0x161
9
10enum asf_error_e {
11 ASF_ERROR_INTERNAL = -1, /* incorrect input to API calls */
12 ASF_ERROR_OUTOFMEM = -2, /* some malloc inside program failed */
13 ASF_ERROR_EOF = -3, /* unexpected end of file */
14 ASF_ERROR_IO = -4, /* error reading or writing to file */
15 ASF_ERROR_INVALID_LENGTH = -5, /* length value conflict in input data */
16 ASF_ERROR_INVALID_VALUE = -6, /* other value conflict in input data */
17 ASF_ERROR_INVALID_OBJECT = -7, /* ASF object missing or in wrong place */
18 ASF_ERROR_OBJECT_SIZE = -8, /* invalid ASF object size (too small) */
19 ASF_ERROR_SEEKABLE = -9, /* file not seekable */
20 ASF_ERROR_SEEK = -10 /* file is seekable but seeking failed */
21};
22
23struct asf_waveformatex_s {
24 uint32_t packet_size;
25 int audiostream;
26 uint16_t codec_id;
27 uint16_t channels;
28 uint32_t rate;
29 uint32_t bitrate;
30 uint16_t blockalign;
31 uint16_t bitspersample;
32 uint16_t datalen;
33 uint8_t data[6];
34};
35typedef struct asf_waveformatex_s asf_waveformatex_t;
36
37int asf_read_packet(uint8_t** audiobuf, int* audiobufsize, int* packetlength,
38 asf_waveformatex_t* wfx, struct codec_api* ci);
39
40int asf_get_timestamp(int *duration, struct codec_api* ci);
41
42#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 @@
1# __________ __ ___.
2# Open \______ \ ____ ____ | | _\_ |__ _______ ___
3# Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
4# Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
5# Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
6# \/ \/ \/ \/ \/
7# $Id$
8#
9
10# libasf
11ASFLIB := $(CODECDIR)/libasf.a
12ASFLIB_SRC := $(call preprocess, $(APPSDIR)/codecs/libasf/SOURCES)
13ASFLIB_OBJ := $(call c2obj, $(ASFLIB_SRC))
14OTHER_SRC += $(ASFLIB_SRC)
15
16$(ASFLIB): $(ASFLIB_OBJ)
17 $(SILENT)$(shell rm -f $@)
18 $(call PRINTS,AR $(@F))$(AR) rcs $@ $^ >/dev/null
19
20ASFFLAGS = $(filter-out -O%,$(CODECFLAGS))
21ASFFLAGS += -O3
22
23$(CODECDIR)/libasf/%.o: $(ROOTDIR)/apps/codecs/libasf/%.c
24 $(SILENT)mkdir -p $(dir $@)
25 $(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 @@
24 ****************************************************************************/ 24 ****************************************************************************/
25 25
26#include "codeclib.h" 26#include "codeclib.h"
27#include "libwma/asf.h" 27#include "libasf/asf.h"
28#include "libwma/wmadec.h" 28#include "libwma/wmadec.h"
29 29
30CODEC_HEADER 30CODEC_HEADER
31 31
32int packet_count=0;
33
34/* The output buffer containing the decoded samples (channels 0 and 1) 32/* The output buffer containing the decoded samples (channels 0 and 1)
35 BLOCK_MAX_SIZE is 2048 (samples) and MAX_CHANNELS is 2. 33 BLOCK_MAX_SIZE is 2048 (samples) and MAX_CHANNELS is 2.
36 */ 34 */
@@ -40,368 +38,6 @@ static uint32_t decoded[BLOCK_MAX_SIZE * MAX_CHANNELS] IBSS_ATTR;
40/* NOTE: WMADecodeContext is 120152 bytes (on x86) */ 38/* NOTE: WMADecodeContext is 120152 bytes (on x86) */
41static WMADecodeContext wmadec; 39static WMADecodeContext wmadec;
42 40
43enum asf_error_e {
44 ASF_ERROR_INTERNAL = -1, /* incorrect input to API calls */
45 ASF_ERROR_OUTOFMEM = -2, /* some malloc inside program failed */
46 ASF_ERROR_EOF = -3, /* unexpected end of file */
47 ASF_ERROR_IO = -4, /* error reading or writing to file */
48 ASF_ERROR_INVALID_LENGTH = -5, /* length value conflict in input data */
49 ASF_ERROR_INVALID_VALUE = -6, /* other value conflict in input data */
50 ASF_ERROR_INVALID_OBJECT = -7, /* ASF object missing or in wrong place */
51 ASF_ERROR_OBJECT_SIZE = -8, /* invalid ASF object size (too small) */
52 ASF_ERROR_SEEKABLE = -9, /* file not seekable */
53 ASF_ERROR_SEEK = -10 /* file is seekable but seeking failed */
54};
55
56/* Read an unaligned 32-bit little endian long from buffer. */
57static unsigned long get_long_le(void* buf)
58{
59 unsigned char* p = (unsigned char*) buf;
60
61 return p[0] | (p[1] << 8) | (p[2] << 16) | (p[3] << 24);
62}
63
64/* Read an unaligned 16-bit little endian short from buffer. */
65static unsigned short get_short_le(void* buf)
66{
67 unsigned char* p = (unsigned char*) buf;
68
69 return p[0] | (p[1] << 8);
70}
71
72#define GETLEN2b(bits) (((bits) == 0x03) ? 4 : bits)
73
74#define GETVALUE2b(bits, data) \
75 (((bits) != 0x03) ? ((bits) != 0x02) ? ((bits) != 0x01) ? \
76 0 : *(data) : get_short_le(data) : get_long_le(data))
77
78static int asf_read_packet(uint8_t** audiobuf, int* audiobufsize, int* packetlength, asf_waveformatex_t* wfx)
79{
80 uint8_t tmp8, packet_flags, packet_property;
81 int stream_id;
82 int ec_length, opaque_data, ec_length_type;
83 int datalen;
84 uint8_t data[18];
85 uint8_t* datap;
86 uint32_t length;
87 uint32_t padding_length;
88 uint32_t send_time;
89 uint16_t duration;
90 uint16_t payload_count;
91 int payload_length_type;
92 uint32_t payload_hdrlen;
93 int payload_datalen;
94 int multiple;
95 uint32_t replicated_length;
96 uint32_t media_object_number;
97 uint32_t media_object_offset;
98 uint32_t bytesread = 0;
99 uint8_t* buf;
100 size_t bufsize;
101 int i;
102 /*DEBUGF("Reading new packet at %d bytes ", (int)ci->curpos);*/
103
104 if (ci->read_filebuf(&tmp8, 1) == 0) {
105 return ASF_ERROR_EOF;
106 }
107 bytesread++;
108
109 /* TODO: We need a better way to detect endofstream */
110 if (tmp8 != 0x82) {
111 DEBUGF("Read failed: packet did not sync\n");
112 return -1;
113 }
114
115
116 if (tmp8 & 0x80) {
117 ec_length = tmp8 & 0x0f;
118 opaque_data = (tmp8 >> 4) & 0x01;
119 ec_length_type = (tmp8 >> 5) & 0x03;
120
121 if (ec_length_type != 0x00 || opaque_data != 0 || ec_length != 0x02) {
122 DEBUGF("incorrect error correction flags\n");
123 return ASF_ERROR_INVALID_VALUE;
124 }
125
126 /* Skip ec_data */
127 ci->advance_buffer(ec_length);
128 bytesread += ec_length;
129 } else {
130 ec_length = 0;
131 }
132
133 if (ci->read_filebuf(&packet_flags, 1) == 0) { return ASF_ERROR_EOF; }
134 if (ci->read_filebuf(&packet_property, 1) == 0) { return ASF_ERROR_EOF; }
135 bytesread += 2;
136
137 datalen = GETLEN2b((packet_flags >> 1) & 0x03) +
138 GETLEN2b((packet_flags >> 3) & 0x03) +
139 GETLEN2b((packet_flags >> 5) & 0x03) + 6;
140
141#if 0
142 if (datalen > sizeof(data)) {
143 DEBUGF("Unexpectedly long datalen in data - %d\n",datalen);
144 return ASF_ERROR_OUTOFMEM;
145 }
146#endif
147
148 if (ci->read_filebuf(data, datalen) == 0) {
149 return ASF_ERROR_EOF;
150 }
151
152 bytesread += datalen;
153
154 datap = data;
155 length = GETVALUE2b((packet_flags >> 5) & 0x03, datap);
156 datap += GETLEN2b((packet_flags >> 5) & 0x03);
157 /* sequence value is not used */
158 GETVALUE2b((packet_flags >> 1) & 0x03, datap);
159 datap += GETLEN2b((packet_flags >> 1) & 0x03);
160 padding_length = GETVALUE2b((packet_flags >> 3) & 0x03, datap);
161 datap += GETLEN2b((packet_flags >> 3) & 0x03);
162 send_time = get_long_le(datap);
163 datap += 4;
164 duration = get_short_le(datap);
165 datap += 2;
166 /*DEBUGF("and duration %d ms\n", duration);*/
167
168 /* this is really idiotic, packet length can (and often will) be
169 * undefined and we just have to use the header packet size as the size
170 * value */
171 if (!((packet_flags >> 5) & 0x03)) {
172 length = wfx->packet_size;
173 }
174
175 /* this is also really idiotic, if packet length is smaller than packet
176 * size, we need to manually add the additional bytes into padding length
177 */
178 if (length < wfx->packet_size) {
179 padding_length += wfx->packet_size - length;
180 length = wfx->packet_size;
181 }
182
183 if (length > wfx->packet_size) {
184 DEBUGF("packet with too big length value\n");
185 return ASF_ERROR_INVALID_LENGTH;
186 }
187
188 /* check if we have multiple payloads */
189 if (packet_flags & 0x01) {
190 if (ci->read_filebuf(&tmp8, 1) == 0) {
191 return ASF_ERROR_EOF;
192 }
193 payload_count = tmp8 & 0x3f;
194 payload_length_type = (tmp8 >> 6) & 0x03;
195 bytesread++;
196 } else {
197 payload_count = 1;
198 payload_length_type = 0x02; /* not used */
199 }
200
201 if (length < bytesread) {
202 DEBUGF("header exceeded packet size, invalid file - length=%d, bytesread=%d\n",(int)length,(int)bytesread);
203 /* FIXME: should this be checked earlier? */
204 return ASF_ERROR_INVALID_LENGTH;
205 }
206
207
208 /* We now parse the individual payloads, and move all payloads
209 belonging to our audio stream to a contiguous block, starting at
210 the location of the first payload.
211 */
212
213 *audiobuf = NULL;
214 *audiobufsize = 0;
215 *packetlength = length - bytesread;
216
217 buf = ci->request_buffer(&bufsize, length);
218 datap = buf;
219
220 if (bufsize != length) {
221 /* This should only happen with packets larger than 32KB (the
222 guard buffer size). All the streams I've seen have
223 relatively small packets less than about 8KB), but I don't
224 know what is expected.
225 */
226 DEBUGF("Could not read packet (requested %d bytes, received %d), curpos=%d, aborting\n",
227 (int)length,(int)bufsize,(int)ci->curpos);
228 return -1;
229 }
230
231 for (i=0; i<payload_count; i++) {
232 stream_id = datap[0]&0x7f;
233 datap++;
234 bytesread++;
235
236 payload_hdrlen = GETLEN2b(packet_property & 0x03) +
237 GETLEN2b((packet_property >> 2) & 0x03) +
238 GETLEN2b((packet_property >> 4) & 0x03);
239
240 //DEBUGF("payload_hdrlen = %d\n",payload_hdrlen);
241#if 0
242 /* TODO */
243 if (payload_hdrlen > size) {
244 return ASF_ERROR_INVALID_LENGTH;
245 }
246#endif
247 if (payload_hdrlen > sizeof(data)) {
248 DEBUGF("Unexpectedly long datalen in data - %d\n",datalen);
249 return ASF_ERROR_OUTOFMEM;
250 }
251
252 bytesread += payload_hdrlen;
253 media_object_number = GETVALUE2b((packet_property >> 4) & 0x03, datap);
254 datap += GETLEN2b((packet_property >> 4) & 0x03);
255 media_object_offset = GETVALUE2b((packet_property >> 2) & 0x03, datap);
256 datap += GETLEN2b((packet_property >> 2) & 0x03);
257 replicated_length = GETVALUE2b(packet_property & 0x03, datap);
258 datap += GETLEN2b(packet_property & 0x03);
259
260 /* TODO: Validate replicated_length */
261 /* TODO: Is the content of this important for us? */
262 datap += replicated_length;
263 bytesread += replicated_length;
264
265 multiple = packet_flags & 0x01;
266
267
268 if (multiple) {
269 int x;
270
271 x = GETLEN2b(payload_length_type);
272
273 if (x != 2) {
274 /* in multiple payloads datalen should be a word */
275 return ASF_ERROR_INVALID_VALUE;
276 }
277
278#if 0
279 if (skip + tmp > datalen) {
280 /* not enough data */
281 return ASF_ERROR_INVALID_LENGTH;
282 }
283#endif
284 payload_datalen = GETVALUE2b(payload_length_type, datap);
285 datap += x;
286 bytesread += x;
287 } else {
288 payload_datalen = length - bytesread - padding_length;
289 }
290
291 if (replicated_length==1)
292 datap++;
293
294 if (stream_id == wfx->audiostream)
295 {
296 if (*audiobuf == NULL) {
297 /* The first payload can stay where it is */
298 *audiobuf = datap;
299 *audiobufsize = payload_datalen;
300 } else {
301 /* The second and subsequent payloads in this packet
302 that belong to the audio stream need to be moved to be
303 contiguous with the first payload.
304 */
305 memmove(*audiobuf + *audiobufsize, datap, payload_datalen);
306 *audiobufsize += payload_datalen;
307 }
308 }
309 datap += payload_datalen;
310 bytesread += payload_datalen;
311 }
312
313 if (*audiobuf != NULL)
314 return 1;
315 else
316 return 0;
317}
318
319
320static int get_timestamp(int *duration)
321{
322 uint8_t tmp8, packet_flags, packet_property;
323 int ec_length, opaque_data, ec_length_type;
324 int datalen;
325 uint8_t data[18];
326 uint8_t* datap;
327 uint32_t length;
328 uint32_t padding_length;
329 uint32_t send_time;
330
331 uint32_t bytesread = 0;
332 packet_count++;
333 if (ci->read_filebuf(&tmp8, 1) == 0) {
334 DEBUGF("ASF ERROR (EOF?)\n");
335 return ASF_ERROR_EOF;
336 }
337 bytesread++;
338
339 /* TODO: We need a better way to detect endofstream */
340 if (tmp8 != 0x82) {
341 DEBUGF("Get timestamp: Detected end of stream\n");
342 return ASF_ERROR_EOF;
343 }
344
345
346 if (tmp8 & 0x80) {
347 ec_length = tmp8 & 0x0f;
348 opaque_data = (tmp8 >> 4) & 0x01;
349 ec_length_type = (tmp8 >> 5) & 0x03;
350
351 if (ec_length_type != 0x00 || opaque_data != 0 || ec_length != 0x02) {
352 DEBUGF("incorrect error correction flags\n");
353 return ASF_ERROR_INVALID_VALUE;
354 }
355
356 /* Skip ec_data */
357 ci->advance_buffer(ec_length);
358 bytesread += ec_length;
359 } else {
360 ec_length = 0;
361 }
362
363 if (ci->read_filebuf(&packet_flags, 1) == 0) {
364 DEBUGF("Detected end of stream 2\n");
365 return ASF_ERROR_EOF;
366 }
367
368 if (ci->read_filebuf(&packet_property, 1) == 0) {
369 DEBUGF("Detected end of stream3\n");
370 return ASF_ERROR_EOF;
371 }
372 bytesread += 2;
373
374 datalen = GETLEN2b((packet_flags >> 1) & 0x03) +
375 GETLEN2b((packet_flags >> 3) & 0x03) +
376 GETLEN2b((packet_flags >> 5) & 0x03) + 6;
377
378 if (ci->read_filebuf(data, datalen) == 0) {
379 DEBUGF("Detected end of stream4\n");
380 return ASF_ERROR_EOF;
381 }
382
383 bytesread += datalen;
384
385 datap = data;
386 length = GETVALUE2b((packet_flags >> 5) & 0x03, datap);
387 datap += GETLEN2b((packet_flags >> 5) & 0x03);
388
389 /* sequence value is not used */
390 GETVALUE2b((packet_flags >> 1) & 0x03, datap);
391 datap += GETLEN2b((packet_flags >> 1) & 0x03);
392 padding_length = GETVALUE2b((packet_flags >> 3) & 0x03, datap);
393 datap += GETLEN2b((packet_flags >> 3) & 0x03);
394 send_time = get_long_le(datap);
395 datap += 4;
396 *duration = get_short_le(datap);
397
398 /*the get_timestamp function advances us 12-13 bytes past the packet start,
399 need to undo this here so that we stay synced with the packet*/
400 ci->seek_buffer(ci->curpos-bytesread);
401
402 return send_time;
403}
404
405/*entry point for seeks*/ 41/*entry point for seeks*/
406static int seek(int ms, asf_waveformatex_t* wfx) 42static int seek(int ms, asf_waveformatex_t* wfx)
407{ 43{
@@ -428,7 +64,7 @@ static int seek(int ms, asf_waveformatex_t* wfx)
428 count++; 64 count++;
429 65
430 /*check the time stamp of our packet*/ 66 /*check the time stamp of our packet*/
431 time = get_timestamp(&duration); 67 time = asf_get_timestamp(&duration, ci);
432 DEBUGF("seeked to %d ms with duration %d\n", time, duration); 68 DEBUGF("seeked to %d ms with duration %d\n", time, duration);
433 69
434 if (time < 0) { 70 if (time < 0) {
@@ -436,7 +72,7 @@ static int seek(int ms, asf_waveformatex_t* wfx)
436 DEBUGF("UKNOWN SEEK ERROR\n"); 72 DEBUGF("UKNOWN SEEK ERROR\n");
437 ci->seek_buffer(ci->id3->first_frame_offset+initial_packet*wfx->packet_size); 73 ci->seek_buffer(ci->id3->first_frame_offset+initial_packet*wfx->packet_size);
438 /*seek failed so return time stamp of the initial packet*/ 74 /*seek failed so return time stamp of the initial packet*/
439 return get_timestamp(&duration); 75 return asf_get_timestamp(&duration, ci);
440 } 76 }
441 77
442 if ((time+duration>=ms && time<=ms) || count > 10) { 78 if ((time+duration>=ms && time<=ms) || count > 10) {
@@ -509,7 +145,7 @@ restart_track:
509 int packet_offset = (resume_offset - ci->id3->first_frame_offset) 145 int packet_offset = (resume_offset - ci->id3->first_frame_offset)
510 % wfx.packet_size; 146 % wfx.packet_size;
511 ci->seek_buffer(resume_offset - packet_offset); 147 ci->seek_buffer(resume_offset - packet_offset);
512 elapsedtime = get_timestamp(&i); 148 elapsedtime = asf_get_timestamp(&i, ci);
513 ci->set_elapsed(elapsedtime); 149 ci->set_elapsed(elapsedtime);
514 } 150 }
515 else 151 else
@@ -558,7 +194,7 @@ restart_track:
558 } 194 }
559 errcount = 0; 195 errcount = 0;
560new_packet: 196new_packet:
561 res = asf_read_packet(&audiobuf, &audiobufsize, &packetlength, &wfx); 197 res = asf_read_packet(&audiobuf, &audiobufsize, &packetlength, &wfx, ci);
562 198
563 if (res < 0) { 199 if (res < 0) {
564 /* We'll try to recover from a parse error a certain number of 200 /* We'll try to recover from a parse error a certain number of