summaryrefslogtreecommitdiff
path: root/apps/codecs/wma.c
diff options
context:
space:
mode:
Diffstat (limited to 'apps/codecs/wma.c')
-rw-r--r--apps/codecs/wma.c374
1 files changed, 5 insertions, 369 deletions
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