blob: f395139674f4e110dd2ab42fcce502f3a9b84c62 [file] [log] [blame]
Dave Chapmanc7282472007-07-03 09:25:36 +00001/***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
Dave Chapman9cb40ea2007-07-03 10:21:23 +00008 * $Id$
Dave Chapmanc7282472007-07-03 09:25:36 +00009 *
Dave Chapmand65930f2008-04-16 19:42:26 +000010 * Copyright (C) 2007 Dave Chapman
11 *
12 * ASF parsing code based on libasf by Juho Vähä-Herttua
13 * http://code.google.com/p/libasf/ libasf itself was based on the ASF
14 * parser in VLC - http://www.videolan.org/
Dave Chapmanc7282472007-07-03 09:25:36 +000015 *
Daniel Stenberg2acc0ac2008-06-28 18:10:04 +000016 * This program is free software; you can redistribute it and/or
17 * modify it under the terms of the GNU General Public License
18 * as published by the Free Software Foundation; either version 2
19 * of the License, or (at your option) any later version.
Dave Chapmanc7282472007-07-03 09:25:36 +000020 *
21 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
22 * KIND, either express or implied.
23 *
24 ****************************************************************************/
25
26#include "codeclib.h"
27#include "libwma/asf.h"
28#include "libwma/wmadec.h"
29
30CODEC_HEADER
31
Michael Giacomellif8613202007-11-22 05:32:26 +000032int packet_count=0;
33
Dave Chapman88e32c22007-07-09 17:24:00 +000034/* The output buffer containing the decoded samples (channels 0 and 1)
35 BLOCK_MAX_SIZE is 2048 (samples) and MAX_CHANNELS is 2.
36 */
Dave Chapmanc7282472007-07-03 09:25:36 +000037
Marcoen Hirschbergc43629f2007-08-06 23:34:28 +000038static uint32_t decoded[BLOCK_MAX_SIZE * MAX_CHANNELS];
Dave Chapmanc7282472007-07-03 09:25:36 +000039
Dave Chapmane6a3f7d2007-07-18 09:43:12 +000040/* NOTE: WMADecodeContext is 120152 bytes (on x86) */
Dave Chapmanc7282472007-07-03 09:25:36 +000041static WMADecodeContext wmadec;
42
Dave Chapmanc7282472007-07-03 09:25:36 +000043enum 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
Dave Chapman47e96922007-07-10 13:37:16 +000078static int asf_read_packet(uint8_t** audiobuf, int* audiobufsize, int* packetlength, asf_waveformatex_t* wfx)
Dave Chapmanc7282472007-07-03 09:25:36 +000079{
80 uint8_t tmp8, packet_flags, packet_property;
Dave Chapman47e96922007-07-10 13:37:16 +000081 int stream_id;
Dave Chapmanc7282472007-07-03 09:25:36 +000082 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;
Dave Chapman47e96922007-07-10 13:37:16 +000099 uint8_t* buf;
100 size_t bufsize;
101 int i;
Michael Giacomelli82dc8172008-04-07 02:32:48 +0000102 /*DEBUGF("Reading new packet at %d bytes ", (int)ci->curpos);*/
Dave Chapmanc7282472007-07-03 09:25:36 +0000103
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 */
Michael Giacomellif8613202007-11-22 05:32:26 +0000110 if (tmp8 != 0x82) {
111 DEBUGF("Read failed: packet did not sync\n");
112 return -1;
113 }
Dave Chapmanc7282472007-07-03 09:25:36 +0000114
Dave Chapmanc7282472007-07-03 09:25:36 +0000115
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;
Michael Giacomelli82dc8172008-04-07 02:32:48 +0000166 /*DEBUGF("and duration %d ms\n", duration);*/
Dave Chapmanc7282472007-07-03 09:25:36 +0000167
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
Dave Chapman47e96922007-07-10 13:37:16 +0000207
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 */
Dave Chapmane6a3f7d2007-07-18 09:43:12 +0000226 DEBUGF("Could not read packet (requested %d bytes, received %d), curpos=%d, aborting\n",
227 (int)length,(int)bufsize,(int)ci->curpos);
Dave Chapman47e96922007-07-10 13:37:16 +0000228 return -1;
Dave Chapmanc7282472007-07-03 09:25:36 +0000229 }
Dave Chapmanc7282472007-07-03 09:25:36 +0000230
Dave Chapman47e96922007-07-10 13:37:16 +0000231 for (i=0; i<payload_count; i++) {
232 stream_id = datap[0]&0x7f;
233 datap++;
234 bytesread++;
Dave Chapmanc7282472007-07-03 09:25:36 +0000235
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
Dave Chapmanc7282472007-07-03 09:25:36 +0000252 bytesread += payload_hdrlen;
Dave Chapmanc7282472007-07-03 09:25:36 +0000253 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? */
Dave Chapman47e96922007-07-10 13:37:16 +0000262 datap += replicated_length;
Dave Chapmanc7282472007-07-03 09:25:36 +0000263 bytesread += replicated_length;
264
Dave Chapmanc7282472007-07-03 09:25:36 +0000265 multiple = packet_flags & 0x01;
266
Michael Giacomellif8613202007-11-22 05:32:26 +0000267
Dave Chapmanc7282472007-07-03 09:25:36 +0000268 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
Dave Chapman47e96922007-07-10 13:37:16 +0000284 payload_datalen = GETVALUE2b(payload_length_type, datap);
285 datap += x;
Dave Chapmanc7282472007-07-03 09:25:36 +0000286 bytesread += x;
Dave Chapmanc7282472007-07-03 09:25:36 +0000287 } else {
Dave Chapman47e96922007-07-10 13:37:16 +0000288 payload_datalen = length - bytesread - padding_length;
Dave Chapmanc7282472007-07-03 09:25:36 +0000289 }
290
Dave Chapman47e96922007-07-10 13:37:16 +0000291 if (stream_id == wfx->audiostream)
292 {
293 if (*audiobuf == NULL) {
294 /* The first payload can stay where it is */
295 *audiobuf = datap;
296 *audiobufsize = payload_datalen;
297 } else {
298 /* The second and subsequent payloads in this packet
299 that belong to the audio stream need to be moved to be
300 contiguous with the first payload.
301 */
302 memmove(*audiobuf + *audiobufsize, datap, payload_datalen);
303 *audiobufsize += payload_datalen;
304 }
305 }
306 datap += payload_datalen;
307 bytesread += payload_datalen;
Dave Chapmanc7282472007-07-03 09:25:36 +0000308 }
Dave Chapman47e96922007-07-10 13:37:16 +0000309
310 if (*audiobuf != NULL)
311 return 1;
312 else
313 return 0;
Dave Chapmanc7282472007-07-03 09:25:36 +0000314}
315
Michael Giacomellif8613202007-11-22 05:32:26 +0000316
Dave Chapman05b158f2007-12-01 02:46:31 +0000317static int get_timestamp(int *duration)
318{
319 uint8_t tmp8, packet_flags, packet_property;
Dave Chapman05b158f2007-12-01 02:46:31 +0000320 int ec_length, opaque_data, ec_length_type;
321 int datalen;
322 uint8_t data[18];
323 uint8_t* datap;
324 uint32_t length;
325 uint32_t padding_length;
326 uint32_t send_time;
Michael Giacomellif8613202007-11-22 05:32:26 +0000327
Dave Chapman05b158f2007-12-01 02:46:31 +0000328 uint32_t bytesread = 0;
329 packet_count++;
330 if (ci->read_filebuf(&tmp8, 1) == 0) {
331 DEBUGF("ASF ERROR (EOF?)\n");
Michael Giacomellif8613202007-11-22 05:32:26 +0000332 return ASF_ERROR_EOF;
Dave Chapman05b158f2007-12-01 02:46:31 +0000333 }
334 bytesread++;
Michael Giacomellif8613202007-11-22 05:32:26 +0000335
Dave Chapman05b158f2007-12-01 02:46:31 +0000336 /* TODO: We need a better way to detect endofstream */
337 if (tmp8 != 0x82) {
338 DEBUGF("Get timestamp: Detected end of stream\n");
339 return ASF_ERROR_EOF;
340 }
Michael Giacomellif8613202007-11-22 05:32:26 +0000341
342
Dave Chapman05b158f2007-12-01 02:46:31 +0000343 if (tmp8 & 0x80) {
Michael Giacomellif8613202007-11-22 05:32:26 +0000344 ec_length = tmp8 & 0x0f;
345 opaque_data = (tmp8 >> 4) & 0x01;
346 ec_length_type = (tmp8 >> 5) & 0x03;
347
348 if (ec_length_type != 0x00 || opaque_data != 0 || ec_length != 0x02) {
349 DEBUGF("incorrect error correction flags\n");
350 return ASF_ERROR_INVALID_VALUE;
351 }
352
353 /* Skip ec_data */
354 ci->advance_buffer(ec_length);
355 bytesread += ec_length;
Dave Chapman05b158f2007-12-01 02:46:31 +0000356 } else {
357 ec_length = 0;
358 }
Michael Giacomellif8613202007-11-22 05:32:26 +0000359
Michael Giacomellidadd80a2008-01-06 02:50:01 +0000360 if (ci->read_filebuf(&packet_flags, 1) == 0) {
Dave Chapman05b158f2007-12-01 02:46:31 +0000361 DEBUGF("Detected end of stream 2\n");
362 return ASF_ERROR_EOF;
363 }
Michael Giacomellif8613202007-11-22 05:32:26 +0000364
Dave Chapman05b158f2007-12-01 02:46:31 +0000365 if (ci->read_filebuf(&packet_property, 1) == 0) {
366 DEBUGF("Detected end of stream3\n");
367 return ASF_ERROR_EOF;
368 }
369 bytesread += 2;
Michael Giacomellif8613202007-11-22 05:32:26 +0000370
Dave Chapman05b158f2007-12-01 02:46:31 +0000371 datalen = GETLEN2b((packet_flags >> 1) & 0x03) +
372 GETLEN2b((packet_flags >> 3) & 0x03) +
373 GETLEN2b((packet_flags >> 5) & 0x03) + 6;
Michael Giacomellif8613202007-11-22 05:32:26 +0000374
Dave Chapman05b158f2007-12-01 02:46:31 +0000375 if (ci->read_filebuf(data, datalen) == 0) {
376 DEBUGF("Detected end of stream4\n");
377 return ASF_ERROR_EOF;
378 }
Michael Giacomellif8613202007-11-22 05:32:26 +0000379
Dave Chapman05b158f2007-12-01 02:46:31 +0000380 bytesread += datalen;
Michael Giacomellif8613202007-11-22 05:32:26 +0000381
Dave Chapman05b158f2007-12-01 02:46:31 +0000382 datap = data;
383 length = GETVALUE2b((packet_flags >> 5) & 0x03, datap);
384 datap += GETLEN2b((packet_flags >> 5) & 0x03);
385
386 /* sequence value is not used */
387 GETVALUE2b((packet_flags >> 1) & 0x03, datap);
388 datap += GETLEN2b((packet_flags >> 1) & 0x03);
389 padding_length = GETVALUE2b((packet_flags >> 3) & 0x03, datap);
390 datap += GETLEN2b((packet_flags >> 3) & 0x03);
391 send_time = get_long_le(datap);
392 datap += 4;
393 *duration = get_short_le(datap);
394
Michael Giacomelli191b7e82008-03-09 22:55:38 +0000395 /*the get_timestamp function advances us 12-13 bytes past the packet start,
396 need to undo this here so that we stay synced with the packet*/
397 ci->seek_buffer(ci->curpos-bytesread);
398
Dave Chapman05b158f2007-12-01 02:46:31 +0000399 return send_time;
400}
Michael Giacomellif8613202007-11-22 05:32:26 +0000401
402/*entry point for seeks*/
Dave Chapman05b158f2007-12-01 02:46:31 +0000403static int seek(int ms, asf_waveformatex_t* wfx)
404{
Michael Giacomelli1a5d5942007-11-30 00:46:32 +0000405 int time, duration, delta, temp, count=0;
Michael Giacomellif8613202007-11-22 05:32:26 +0000406
407 /*estimate packet number from bitrate*/
408 int initial_packet = ci->curpos/wfx->packet_size;
Michael Giacomelliafd5c6a2008-03-09 23:38:11 +0000409 int packet_num = (((int64_t)ms)*(wfx->bitrate>>3))/wfx->packet_size/1000;
Michael Giacomellif8613202007-11-22 05:32:26 +0000410 int last_packet = ci->id3->filesize / wfx->packet_size;
411
Dave Chapman05b158f2007-12-01 02:46:31 +0000412 if (packet_num > last_packet) {
Michael Giacomellif8613202007-11-22 05:32:26 +0000413 packet_num = last_packet;
414 }
Dave Chapman05b158f2007-12-01 02:46:31 +0000415
Michael Giacomellif8613202007-11-22 05:32:26 +0000416 /*calculate byte address of the start of that packet*/
417 int packet_offset = packet_num*wfx->packet_size;
418
419 /*seek to estimated packet*/
420 ci->seek_buffer(ci->id3->first_frame_offset+packet_offset);
421 temp = ms;
Dave Chapman05b158f2007-12-01 02:46:31 +0000422 while (1)
423 {
Michael Giacomelli1a5d5942007-11-30 00:46:32 +0000424 /*for very large files it can be difficult and unimportant to find the exact packet*/
425 count++;
Michael Giacomellif8613202007-11-22 05:32:26 +0000426
427 /*check the time stamp of our packet*/
428 time = get_timestamp(&duration);
429 DEBUGF("seeked to %d ms with duration %d\n", time, duration);
430
Dave Chapman05b158f2007-12-01 02:46:31 +0000431 if (time < 0) {
Michael Giacomellif8613202007-11-22 05:32:26 +0000432 /*unknown error, try to recover*/
433 DEBUGF("UKNOWN SEEK ERROR\n");
434 ci->seek_buffer(ci->id3->first_frame_offset+initial_packet*wfx->packet_size);
Michael Giacomellidadd80a2008-01-06 02:50:01 +0000435 /*seek failed so return time stamp of the initial packet*/
436 return get_timestamp(&duration);
Michael Giacomellif8613202007-11-22 05:32:26 +0000437 }
438
Dave Chapman05b158f2007-12-01 02:46:31 +0000439 if ((time+duration>=ms && time<=ms) || count > 10) {
Michael Giacomellif8613202007-11-22 05:32:26 +0000440 DEBUGF("Found our packet! Now at %d packet\n", packet_num);
441 return time;
Dave Chapman05b158f2007-12-01 02:46:31 +0000442 } else {
Michael Giacomellif8613202007-11-22 05:32:26 +0000443 /*seek again*/
444 delta = ms-time;
Michael Giacomellif8613202007-11-22 05:32:26 +0000445 /*estimate new packet number from bitrate and our current position*/
446 temp += delta;
Michael Giacomelli1a5d5942007-11-30 00:46:32 +0000447 packet_num = ((temp/1000)*(wfx->bitrate>>3) - (wfx->packet_size>>1))/wfx->packet_size; //round down!
Michael Giacomellif8613202007-11-22 05:32:26 +0000448 packet_offset = packet_num*wfx->packet_size;
Michael Giacomellif8613202007-11-22 05:32:26 +0000449 ci->seek_buffer(ci->id3->first_frame_offset+packet_offset);
450 }
Dave Chapman05b158f2007-12-01 02:46:31 +0000451 }
452}
Michael Giacomellif8613202007-11-22 05:32:26 +0000453
454
455
Dave Chapmanc7282472007-07-03 09:25:36 +0000456/* this is the codec entry point */
457enum codec_status codec_main(void)
458{
Dave Chapmanc7282472007-07-03 09:25:36 +0000459 uint32_t elapsedtime;
460 int retval;
461 asf_waveformatex_t wfx;
Dave Chapmanc7282472007-07-03 09:25:36 +0000462 size_t resume_offset;
Dave Chapman88e32c22007-07-09 17:24:00 +0000463 int i;
Dave Chapman47e96922007-07-10 13:37:16 +0000464 int wmares, res;
465 uint8_t* audiobuf;
466 int audiobufsize;
467 int packetlength;
Michael Giacomellif8613202007-11-22 05:32:26 +0000468 int errcount = 0;
Dave Chapmanc7282472007-07-03 09:25:36 +0000469
470 /* Generic codec initialisation */
471 ci->configure(CODEC_SET_FILEBUF_WATERMARK, 1024*512);
Dave Chapmanc7282472007-07-03 09:25:36 +0000472
Marcoen Hirschbergc43629f2007-08-06 23:34:28 +0000473 ci->configure(DSP_SET_SAMPLE_DEPTH, 30);
Dave Chapmanc7282472007-07-03 09:25:36 +0000474
Thom Johansena478e462007-10-17 12:51:37 +0000475next_track:
Dave Chapmanc7282472007-07-03 09:25:36 +0000476
477 /* Wait for the metadata to be read */
478 while (!*ci->taginfo_ready && !ci->stop_codec)
479 ci->sleep(1);
480
481 retval = CODEC_OK;
482
483 /* Remember the resume position - when the codec is opened, the
484 playback engine will reset it. */
485 resume_offset = ci->id3->offset;
Magnus Holmgrenbce419d2008-04-29 19:00:42 +0000486restart_track:
Dave Chapmanc7282472007-07-03 09:25:36 +0000487 if (codec_init()) {
488 LOGF("WMA: Error initialising codec\n");
489 retval = CODEC_ERROR;
490 goto exit;
491 }
492
Michael Giacomelli28b0c582007-07-11 16:58:11 +0000493 /* Copy the format metadata we've stored in the id3 TOC field. This
Dave Chapmanc7282472007-07-03 09:25:36 +0000494 saves us from parsing it again here. */
495 memcpy(&wfx, ci->id3->toc, sizeof(wfx));
496
Dave Chapmana9df7132007-07-11 23:07:41 +0000497 if (wma_decode_init(&wmadec,&wfx) < 0) {
498 LOGF("WMA: Unsupported or corrupt file\n");
499 retval = CODEC_ERROR;
500 goto exit;
Michael Giacomelli28b0c582007-07-11 16:58:11 +0000501 }
Dave Chapmanc7282472007-07-03 09:25:36 +0000502
Michael Giacomelli9a946f62008-04-12 19:52:31 +0000503 DEBUGF("**************** IN WMA.C ******************\n");
504
Michael Giacomelli9a946f62008-04-12 19:52:31 +0000505 if (resume_offset > ci->id3->first_frame_offset)
506 {
507 /* Get start of current packet */
508 int packet_offset = (resume_offset - ci->id3->first_frame_offset)
509 % wfx.packet_size;
510 ci->seek_buffer(resume_offset - packet_offset);
511 elapsedtime = get_timestamp(&i);
512 ci->set_elapsed(elapsedtime);
513 }
514 else
515 {
516 /* Now advance the file position to the first frame */
517 ci->seek_buffer(ci->id3->first_frame_offset);
518 elapsedtime = 0;
519 }
Dave Chapmanc7282472007-07-03 09:25:36 +0000520
Magnus Holmgrenbce419d2008-04-29 19:00:42 +0000521 resume_offset = 0;
Dave Chapmanc7282472007-07-03 09:25:36 +0000522 ci->configure(DSP_SWITCH_FREQUENCY, wfx.rate);
523 ci->configure(DSP_SET_STEREO_MODE, wfx.channels == 1 ?
524 STEREO_MONO : STEREO_INTERLEAVED);
525 codec_set_replaygain(ci->id3);
526
527 /* The main decoding loop */
528
Dave Chapmanc7282472007-07-03 09:25:36 +0000529 res = 1;
530 while (res >= 0)
531 {
532 ci->yield();
533 if (ci->stop_codec || ci->new_track) {
534 goto done;
535 }
536
Thom Johansena478e462007-10-17 12:51:37 +0000537 /* Deal with any pending seek requests */
Michael Giacomellif8613202007-11-22 05:32:26 +0000538 if (ci->seek_time){
539
Thom Johansen7113d112007-10-17 18:19:35 +0000540 if (ci->seek_time == 1) {
541 ci->seek_complete();
Magnus Holmgrenbce419d2008-04-29 19:00:42 +0000542 goto restart_track; /* Pretend you never saw this... */
Thom Johansen7113d112007-10-17 18:19:35 +0000543 }
Dave Chapmanc7282472007-07-03 09:25:36 +0000544
Michael Giacomellif8613202007-11-22 05:32:26 +0000545 elapsedtime = seek(ci->seek_time, &wfx);
Dave Chapman05b158f2007-12-01 02:46:31 +0000546 if (elapsedtime < 1){
Michael Giacomellif8613202007-11-22 05:32:26 +0000547 ci->seek_complete();
548 goto next_track;
549 }
Michael Giacomelli82dc8172008-04-07 02:32:48 +0000550 /*DEBUGF("Seek returned %d\n", (int)elapsedtime);*/
Michael Giacomellif8613202007-11-22 05:32:26 +0000551 ci->set_elapsed(elapsedtime);
552
553 /*flush the wma decoder state*/
Michael Giacomelli1a5d5942007-11-30 00:46:32 +0000554 wmadec.last_superframe_len = 0;
555 wmadec.last_bitoffset = 0;
556 ci->seek_complete();
Michael Giacomellif8613202007-11-22 05:32:26 +0000557 }
558 errcount = 0;
559new_packet:
Dave Chapman47e96922007-07-10 13:37:16 +0000560 res = asf_read_packet(&audiobuf, &audiobufsize, &packetlength, &wfx);
Michael Giacomellif8613202007-11-22 05:32:26 +0000561
Dave Chapman05b158f2007-12-01 02:46:31 +0000562 if (res < 0) {
563 /* We'll try to recover from a parse error a certain number of
564 * times. If we succeed, the error counter will be reset.
565 */
Michael Giacomellif8613202007-11-22 05:32:26 +0000566
Dave Chapman05b158f2007-12-01 02:46:31 +0000567 errcount++;
Michael Giacomellidadd80a2008-01-06 02:50:01 +0000568 DEBUGF("read_packet error %d, errcount %d\n",wmares, errcount);
Dave Chapman05b158f2007-12-01 02:46:31 +0000569 if (errcount > 5) {
570 goto done;
571 } else {
572 ci->advance_buffer(packetlength);
573 goto new_packet;
574 }
575 } else if (res > 0) {
576 wma_decode_superframe_init(&wmadec, audiobuf, audiobufsize);
Dave Chapmanc7282472007-07-03 09:25:36 +0000577
Dave Chapman88e32c22007-07-09 17:24:00 +0000578 for (i=0; i < wmadec.nb_frames; i++)
579 {
580 wmares = wma_decode_superframe_frame(&wmadec,
581 decoded,
Dave Chapman47e96922007-07-10 13:37:16 +0000582 audiobuf, audiobufsize);
Michael Giacomelli1a5d5942007-11-30 00:46:32 +0000583
Dave Chapman88e32c22007-07-09 17:24:00 +0000584 ci->yield ();
585
Dave Chapman05b158f2007-12-01 02:46:31 +0000586 if (wmares < 0) {
587 /* Do the above, but for errors in decode. */
Michael Giacomellif8613202007-11-22 05:32:26 +0000588 errcount++;
589 DEBUGF("WMA decode error %d, errcount %d\n",wmares, errcount);
590 if (errcount > 5) {
591 goto done;
592 } else {
593 ci->advance_buffer(packetlength);
594 goto new_packet;
595 }
Dave Chapman88e32c22007-07-09 17:24:00 +0000596 } else if (wmares > 0) {
597 ci->pcmbuf_insert(decoded, NULL, wmares);
Michael Giacomellif8613202007-11-22 05:32:26 +0000598 elapsedtime += (wmares*10)/(wfx.rate/100);
Dave Chapman88e32c22007-07-09 17:24:00 +0000599 ci->set_elapsed(elapsedtime);
600 }
Michael Giacomellif8613202007-11-22 05:32:26 +0000601 ci->yield();
Dave Chapmanc7282472007-07-03 09:25:36 +0000602 }
Dave Chapmanc7282472007-07-03 09:25:36 +0000603 }
Dave Chapman47e96922007-07-10 13:37:16 +0000604
605 ci->advance_buffer(packetlength);
Dave Chapmanc7282472007-07-03 09:25:36 +0000606 }
607 retval = CODEC_OK;
608
609done:
Michael Giacomelli82dc8172008-04-07 02:32:48 +0000610 /*LOGF("WMA: Decoded %ld samples\n",elapsedtime*wfx.rate/1000);*/
Dave Chapmanc7282472007-07-03 09:25:36 +0000611
612 if (ci->request_next_track())
613 goto next_track;
Dave Chapmanc7282472007-07-03 09:25:36 +0000614exit:
615 return retval;
616}