Dave Chapman | c728247 | 2007-07-03 09:25:36 +0000 | [diff] [blame] | 1 | /*************************************************************************** |
| 2 | * __________ __ ___. |
| 3 | * Open \______ \ ____ ____ | | _\_ |__ _______ ___ |
| 4 | * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / |
| 5 | * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < |
| 6 | * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ |
| 7 | * \/ \/ \/ \/ \/ |
Dave Chapman | 9cb40ea | 2007-07-03 10:21:23 +0000 | [diff] [blame] | 8 | * $Id$ |
Dave Chapman | c728247 | 2007-07-03 09:25:36 +0000 | [diff] [blame] | 9 | * |
Dave Chapman | d65930f | 2008-04-16 19:42:26 +0000 | [diff] [blame] | 10 | * 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 Chapman | c728247 | 2007-07-03 09:25:36 +0000 | [diff] [blame] | 15 | * |
Daniel Stenberg | 2acc0ac | 2008-06-28 18:10:04 +0000 | [diff] [blame^] | 16 | * 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 Chapman | c728247 | 2007-07-03 09:25:36 +0000 | [diff] [blame] | 20 | * |
| 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 | |
| 30 | CODEC_HEADER |
| 31 | |
Michael Giacomelli | f861320 | 2007-11-22 05:32:26 +0000 | [diff] [blame] | 32 | int packet_count=0; |
| 33 | |
Dave Chapman | 88e32c2 | 2007-07-09 17:24:00 +0000 | [diff] [blame] | 34 | /* 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 Chapman | c728247 | 2007-07-03 09:25:36 +0000 | [diff] [blame] | 37 | |
Marcoen Hirschberg | c43629f | 2007-08-06 23:34:28 +0000 | [diff] [blame] | 38 | static uint32_t decoded[BLOCK_MAX_SIZE * MAX_CHANNELS]; |
Dave Chapman | c728247 | 2007-07-03 09:25:36 +0000 | [diff] [blame] | 39 | |
Dave Chapman | e6a3f7d | 2007-07-18 09:43:12 +0000 | [diff] [blame] | 40 | /* NOTE: WMADecodeContext is 120152 bytes (on x86) */ |
Dave Chapman | c728247 | 2007-07-03 09:25:36 +0000 | [diff] [blame] | 41 | static WMADecodeContext wmadec; |
| 42 | |
Dave Chapman | c728247 | 2007-07-03 09:25:36 +0000 | [diff] [blame] | 43 | enum 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. */ |
| 57 | static 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. */ |
| 65 | static 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 Chapman | 47e9692 | 2007-07-10 13:37:16 +0000 | [diff] [blame] | 78 | static int asf_read_packet(uint8_t** audiobuf, int* audiobufsize, int* packetlength, asf_waveformatex_t* wfx) |
Dave Chapman | c728247 | 2007-07-03 09:25:36 +0000 | [diff] [blame] | 79 | { |
| 80 | uint8_t tmp8, packet_flags, packet_property; |
Dave Chapman | 47e9692 | 2007-07-10 13:37:16 +0000 | [diff] [blame] | 81 | int stream_id; |
Dave Chapman | c728247 | 2007-07-03 09:25:36 +0000 | [diff] [blame] | 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; |
Dave Chapman | 47e9692 | 2007-07-10 13:37:16 +0000 | [diff] [blame] | 99 | uint8_t* buf; |
| 100 | size_t bufsize; |
| 101 | int i; |
Michael Giacomelli | 82dc817 | 2008-04-07 02:32:48 +0000 | [diff] [blame] | 102 | /*DEBUGF("Reading new packet at %d bytes ", (int)ci->curpos);*/ |
Dave Chapman | c728247 | 2007-07-03 09:25:36 +0000 | [diff] [blame] | 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 */ |
Michael Giacomelli | f861320 | 2007-11-22 05:32:26 +0000 | [diff] [blame] | 110 | if (tmp8 != 0x82) { |
| 111 | DEBUGF("Read failed: packet did not sync\n"); |
| 112 | return -1; |
| 113 | } |
Dave Chapman | c728247 | 2007-07-03 09:25:36 +0000 | [diff] [blame] | 114 | |
Dave Chapman | c728247 | 2007-07-03 09:25:36 +0000 | [diff] [blame] | 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; |
Michael Giacomelli | 82dc817 | 2008-04-07 02:32:48 +0000 | [diff] [blame] | 166 | /*DEBUGF("and duration %d ms\n", duration);*/ |
Dave Chapman | c728247 | 2007-07-03 09:25:36 +0000 | [diff] [blame] | 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 | |
Dave Chapman | 47e9692 | 2007-07-10 13:37:16 +0000 | [diff] [blame] | 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 | */ |
Dave Chapman | e6a3f7d | 2007-07-18 09:43:12 +0000 | [diff] [blame] | 226 | DEBUGF("Could not read packet (requested %d bytes, received %d), curpos=%d, aborting\n", |
| 227 | (int)length,(int)bufsize,(int)ci->curpos); |
Dave Chapman | 47e9692 | 2007-07-10 13:37:16 +0000 | [diff] [blame] | 228 | return -1; |
Dave Chapman | c728247 | 2007-07-03 09:25:36 +0000 | [diff] [blame] | 229 | } |
Dave Chapman | c728247 | 2007-07-03 09:25:36 +0000 | [diff] [blame] | 230 | |
Dave Chapman | 47e9692 | 2007-07-10 13:37:16 +0000 | [diff] [blame] | 231 | for (i=0; i<payload_count; i++) { |
| 232 | stream_id = datap[0]&0x7f; |
| 233 | datap++; |
| 234 | bytesread++; |
Dave Chapman | c728247 | 2007-07-03 09:25:36 +0000 | [diff] [blame] | 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 | |
Dave Chapman | c728247 | 2007-07-03 09:25:36 +0000 | [diff] [blame] | 252 | bytesread += payload_hdrlen; |
Dave Chapman | c728247 | 2007-07-03 09:25:36 +0000 | [diff] [blame] | 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? */ |
Dave Chapman | 47e9692 | 2007-07-10 13:37:16 +0000 | [diff] [blame] | 262 | datap += replicated_length; |
Dave Chapman | c728247 | 2007-07-03 09:25:36 +0000 | [diff] [blame] | 263 | bytesread += replicated_length; |
| 264 | |
Dave Chapman | c728247 | 2007-07-03 09:25:36 +0000 | [diff] [blame] | 265 | multiple = packet_flags & 0x01; |
| 266 | |
Michael Giacomelli | f861320 | 2007-11-22 05:32:26 +0000 | [diff] [blame] | 267 | |
Dave Chapman | c728247 | 2007-07-03 09:25:36 +0000 | [diff] [blame] | 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 |
Dave Chapman | 47e9692 | 2007-07-10 13:37:16 +0000 | [diff] [blame] | 284 | payload_datalen = GETVALUE2b(payload_length_type, datap); |
| 285 | datap += x; |
Dave Chapman | c728247 | 2007-07-03 09:25:36 +0000 | [diff] [blame] | 286 | bytesread += x; |
Dave Chapman | c728247 | 2007-07-03 09:25:36 +0000 | [diff] [blame] | 287 | } else { |
Dave Chapman | 47e9692 | 2007-07-10 13:37:16 +0000 | [diff] [blame] | 288 | payload_datalen = length - bytesread - padding_length; |
Dave Chapman | c728247 | 2007-07-03 09:25:36 +0000 | [diff] [blame] | 289 | } |
| 290 | |
Dave Chapman | 47e9692 | 2007-07-10 13:37:16 +0000 | [diff] [blame] | 291 | 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 Chapman | c728247 | 2007-07-03 09:25:36 +0000 | [diff] [blame] | 308 | } |
Dave Chapman | 47e9692 | 2007-07-10 13:37:16 +0000 | [diff] [blame] | 309 | |
| 310 | if (*audiobuf != NULL) |
| 311 | return 1; |
| 312 | else |
| 313 | return 0; |
Dave Chapman | c728247 | 2007-07-03 09:25:36 +0000 | [diff] [blame] | 314 | } |
| 315 | |
Michael Giacomelli | f861320 | 2007-11-22 05:32:26 +0000 | [diff] [blame] | 316 | |
Dave Chapman | 05b158f | 2007-12-01 02:46:31 +0000 | [diff] [blame] | 317 | static int get_timestamp(int *duration) |
| 318 | { |
| 319 | uint8_t tmp8, packet_flags, packet_property; |
Dave Chapman | 05b158f | 2007-12-01 02:46:31 +0000 | [diff] [blame] | 320 | 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 Giacomelli | f861320 | 2007-11-22 05:32:26 +0000 | [diff] [blame] | 327 | |
Dave Chapman | 05b158f | 2007-12-01 02:46:31 +0000 | [diff] [blame] | 328 | uint32_t bytesread = 0; |
| 329 | packet_count++; |
| 330 | if (ci->read_filebuf(&tmp8, 1) == 0) { |
| 331 | DEBUGF("ASF ERROR (EOF?)\n"); |
Michael Giacomelli | f861320 | 2007-11-22 05:32:26 +0000 | [diff] [blame] | 332 | return ASF_ERROR_EOF; |
Dave Chapman | 05b158f | 2007-12-01 02:46:31 +0000 | [diff] [blame] | 333 | } |
| 334 | bytesread++; |
Michael Giacomelli | f861320 | 2007-11-22 05:32:26 +0000 | [diff] [blame] | 335 | |
Dave Chapman | 05b158f | 2007-12-01 02:46:31 +0000 | [diff] [blame] | 336 | /* 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 Giacomelli | f861320 | 2007-11-22 05:32:26 +0000 | [diff] [blame] | 341 | |
| 342 | |
Dave Chapman | 05b158f | 2007-12-01 02:46:31 +0000 | [diff] [blame] | 343 | if (tmp8 & 0x80) { |
Michael Giacomelli | f861320 | 2007-11-22 05:32:26 +0000 | [diff] [blame] | 344 | 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 Chapman | 05b158f | 2007-12-01 02:46:31 +0000 | [diff] [blame] | 356 | } else { |
| 357 | ec_length = 0; |
| 358 | } |
Michael Giacomelli | f861320 | 2007-11-22 05:32:26 +0000 | [diff] [blame] | 359 | |
Michael Giacomelli | dadd80a | 2008-01-06 02:50:01 +0000 | [diff] [blame] | 360 | if (ci->read_filebuf(&packet_flags, 1) == 0) { |
Dave Chapman | 05b158f | 2007-12-01 02:46:31 +0000 | [diff] [blame] | 361 | DEBUGF("Detected end of stream 2\n"); |
| 362 | return ASF_ERROR_EOF; |
| 363 | } |
Michael Giacomelli | f861320 | 2007-11-22 05:32:26 +0000 | [diff] [blame] | 364 | |
Dave Chapman | 05b158f | 2007-12-01 02:46:31 +0000 | [diff] [blame] | 365 | 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 Giacomelli | f861320 | 2007-11-22 05:32:26 +0000 | [diff] [blame] | 370 | |
Dave Chapman | 05b158f | 2007-12-01 02:46:31 +0000 | [diff] [blame] | 371 | datalen = GETLEN2b((packet_flags >> 1) & 0x03) + |
| 372 | GETLEN2b((packet_flags >> 3) & 0x03) + |
| 373 | GETLEN2b((packet_flags >> 5) & 0x03) + 6; |
Michael Giacomelli | f861320 | 2007-11-22 05:32:26 +0000 | [diff] [blame] | 374 | |
Dave Chapman | 05b158f | 2007-12-01 02:46:31 +0000 | [diff] [blame] | 375 | if (ci->read_filebuf(data, datalen) == 0) { |
| 376 | DEBUGF("Detected end of stream4\n"); |
| 377 | return ASF_ERROR_EOF; |
| 378 | } |
Michael Giacomelli | f861320 | 2007-11-22 05:32:26 +0000 | [diff] [blame] | 379 | |
Dave Chapman | 05b158f | 2007-12-01 02:46:31 +0000 | [diff] [blame] | 380 | bytesread += datalen; |
Michael Giacomelli | f861320 | 2007-11-22 05:32:26 +0000 | [diff] [blame] | 381 | |
Dave Chapman | 05b158f | 2007-12-01 02:46:31 +0000 | [diff] [blame] | 382 | 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 Giacomelli | 191b7e8 | 2008-03-09 22:55:38 +0000 | [diff] [blame] | 395 | /*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 Chapman | 05b158f | 2007-12-01 02:46:31 +0000 | [diff] [blame] | 399 | return send_time; |
| 400 | } |
Michael Giacomelli | f861320 | 2007-11-22 05:32:26 +0000 | [diff] [blame] | 401 | |
| 402 | /*entry point for seeks*/ |
Dave Chapman | 05b158f | 2007-12-01 02:46:31 +0000 | [diff] [blame] | 403 | static int seek(int ms, asf_waveformatex_t* wfx) |
| 404 | { |
Michael Giacomelli | 1a5d594 | 2007-11-30 00:46:32 +0000 | [diff] [blame] | 405 | int time, duration, delta, temp, count=0; |
Michael Giacomelli | f861320 | 2007-11-22 05:32:26 +0000 | [diff] [blame] | 406 | |
| 407 | /*estimate packet number from bitrate*/ |
| 408 | int initial_packet = ci->curpos/wfx->packet_size; |
Michael Giacomelli | afd5c6a | 2008-03-09 23:38:11 +0000 | [diff] [blame] | 409 | int packet_num = (((int64_t)ms)*(wfx->bitrate>>3))/wfx->packet_size/1000; |
Michael Giacomelli | f861320 | 2007-11-22 05:32:26 +0000 | [diff] [blame] | 410 | int last_packet = ci->id3->filesize / wfx->packet_size; |
| 411 | |
Dave Chapman | 05b158f | 2007-12-01 02:46:31 +0000 | [diff] [blame] | 412 | if (packet_num > last_packet) { |
Michael Giacomelli | f861320 | 2007-11-22 05:32:26 +0000 | [diff] [blame] | 413 | packet_num = last_packet; |
| 414 | } |
Dave Chapman | 05b158f | 2007-12-01 02:46:31 +0000 | [diff] [blame] | 415 | |
Michael Giacomelli | f861320 | 2007-11-22 05:32:26 +0000 | [diff] [blame] | 416 | /*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 Chapman | 05b158f | 2007-12-01 02:46:31 +0000 | [diff] [blame] | 422 | while (1) |
| 423 | { |
Michael Giacomelli | 1a5d594 | 2007-11-30 00:46:32 +0000 | [diff] [blame] | 424 | /*for very large files it can be difficult and unimportant to find the exact packet*/ |
| 425 | count++; |
Michael Giacomelli | f861320 | 2007-11-22 05:32:26 +0000 | [diff] [blame] | 426 | |
| 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 Chapman | 05b158f | 2007-12-01 02:46:31 +0000 | [diff] [blame] | 431 | if (time < 0) { |
Michael Giacomelli | f861320 | 2007-11-22 05:32:26 +0000 | [diff] [blame] | 432 | /*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 Giacomelli | dadd80a | 2008-01-06 02:50:01 +0000 | [diff] [blame] | 435 | /*seek failed so return time stamp of the initial packet*/ |
| 436 | return get_timestamp(&duration); |
Michael Giacomelli | f861320 | 2007-11-22 05:32:26 +0000 | [diff] [blame] | 437 | } |
| 438 | |
Dave Chapman | 05b158f | 2007-12-01 02:46:31 +0000 | [diff] [blame] | 439 | if ((time+duration>=ms && time<=ms) || count > 10) { |
Michael Giacomelli | f861320 | 2007-11-22 05:32:26 +0000 | [diff] [blame] | 440 | DEBUGF("Found our packet! Now at %d packet\n", packet_num); |
| 441 | return time; |
Dave Chapman | 05b158f | 2007-12-01 02:46:31 +0000 | [diff] [blame] | 442 | } else { |
Michael Giacomelli | f861320 | 2007-11-22 05:32:26 +0000 | [diff] [blame] | 443 | /*seek again*/ |
| 444 | delta = ms-time; |
Michael Giacomelli | f861320 | 2007-11-22 05:32:26 +0000 | [diff] [blame] | 445 | /*estimate new packet number from bitrate and our current position*/ |
| 446 | temp += delta; |
Michael Giacomelli | 1a5d594 | 2007-11-30 00:46:32 +0000 | [diff] [blame] | 447 | packet_num = ((temp/1000)*(wfx->bitrate>>3) - (wfx->packet_size>>1))/wfx->packet_size; //round down! |
Michael Giacomelli | f861320 | 2007-11-22 05:32:26 +0000 | [diff] [blame] | 448 | packet_offset = packet_num*wfx->packet_size; |
Michael Giacomelli | f861320 | 2007-11-22 05:32:26 +0000 | [diff] [blame] | 449 | ci->seek_buffer(ci->id3->first_frame_offset+packet_offset); |
| 450 | } |
Dave Chapman | 05b158f | 2007-12-01 02:46:31 +0000 | [diff] [blame] | 451 | } |
| 452 | } |
Michael Giacomelli | f861320 | 2007-11-22 05:32:26 +0000 | [diff] [blame] | 453 | |
| 454 | |
| 455 | |
Dave Chapman | c728247 | 2007-07-03 09:25:36 +0000 | [diff] [blame] | 456 | /* this is the codec entry point */ |
| 457 | enum codec_status codec_main(void) |
| 458 | { |
Dave Chapman | c728247 | 2007-07-03 09:25:36 +0000 | [diff] [blame] | 459 | uint32_t elapsedtime; |
| 460 | int retval; |
| 461 | asf_waveformatex_t wfx; |
Dave Chapman | c728247 | 2007-07-03 09:25:36 +0000 | [diff] [blame] | 462 | size_t resume_offset; |
Dave Chapman | 88e32c2 | 2007-07-09 17:24:00 +0000 | [diff] [blame] | 463 | int i; |
Dave Chapman | 47e9692 | 2007-07-10 13:37:16 +0000 | [diff] [blame] | 464 | int wmares, res; |
| 465 | uint8_t* audiobuf; |
| 466 | int audiobufsize; |
| 467 | int packetlength; |
Michael Giacomelli | f861320 | 2007-11-22 05:32:26 +0000 | [diff] [blame] | 468 | int errcount = 0; |
Dave Chapman | c728247 | 2007-07-03 09:25:36 +0000 | [diff] [blame] | 469 | |
| 470 | /* Generic codec initialisation */ |
| 471 | ci->configure(CODEC_SET_FILEBUF_WATERMARK, 1024*512); |
Dave Chapman | c728247 | 2007-07-03 09:25:36 +0000 | [diff] [blame] | 472 | |
Marcoen Hirschberg | c43629f | 2007-08-06 23:34:28 +0000 | [diff] [blame] | 473 | ci->configure(DSP_SET_SAMPLE_DEPTH, 30); |
Dave Chapman | c728247 | 2007-07-03 09:25:36 +0000 | [diff] [blame] | 474 | |
Thom Johansen | a478e46 | 2007-10-17 12:51:37 +0000 | [diff] [blame] | 475 | next_track: |
Dave Chapman | c728247 | 2007-07-03 09:25:36 +0000 | [diff] [blame] | 476 | |
| 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 Holmgren | bce419d | 2008-04-29 19:00:42 +0000 | [diff] [blame] | 486 | restart_track: |
Dave Chapman | c728247 | 2007-07-03 09:25:36 +0000 | [diff] [blame] | 487 | if (codec_init()) { |
| 488 | LOGF("WMA: Error initialising codec\n"); |
| 489 | retval = CODEC_ERROR; |
| 490 | goto exit; |
| 491 | } |
| 492 | |
Michael Giacomelli | 28b0c58 | 2007-07-11 16:58:11 +0000 | [diff] [blame] | 493 | /* Copy the format metadata we've stored in the id3 TOC field. This |
Dave Chapman | c728247 | 2007-07-03 09:25:36 +0000 | [diff] [blame] | 494 | saves us from parsing it again here. */ |
| 495 | memcpy(&wfx, ci->id3->toc, sizeof(wfx)); |
| 496 | |
Dave Chapman | a9df713 | 2007-07-11 23:07:41 +0000 | [diff] [blame] | 497 | if (wma_decode_init(&wmadec,&wfx) < 0) { |
| 498 | LOGF("WMA: Unsupported or corrupt file\n"); |
| 499 | retval = CODEC_ERROR; |
| 500 | goto exit; |
Michael Giacomelli | 28b0c58 | 2007-07-11 16:58:11 +0000 | [diff] [blame] | 501 | } |
Dave Chapman | c728247 | 2007-07-03 09:25:36 +0000 | [diff] [blame] | 502 | |
Michael Giacomelli | 9a946f6 | 2008-04-12 19:52:31 +0000 | [diff] [blame] | 503 | DEBUGF("**************** IN WMA.C ******************\n"); |
| 504 | |
Michael Giacomelli | 9a946f6 | 2008-04-12 19:52:31 +0000 | [diff] [blame] | 505 | 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 Chapman | c728247 | 2007-07-03 09:25:36 +0000 | [diff] [blame] | 520 | |
Magnus Holmgren | bce419d | 2008-04-29 19:00:42 +0000 | [diff] [blame] | 521 | resume_offset = 0; |
Dave Chapman | c728247 | 2007-07-03 09:25:36 +0000 | [diff] [blame] | 522 | 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 Chapman | c728247 | 2007-07-03 09:25:36 +0000 | [diff] [blame] | 529 | res = 1; |
| 530 | while (res >= 0) |
| 531 | { |
| 532 | ci->yield(); |
| 533 | if (ci->stop_codec || ci->new_track) { |
| 534 | goto done; |
| 535 | } |
| 536 | |
Thom Johansen | a478e46 | 2007-10-17 12:51:37 +0000 | [diff] [blame] | 537 | /* Deal with any pending seek requests */ |
Michael Giacomelli | f861320 | 2007-11-22 05:32:26 +0000 | [diff] [blame] | 538 | if (ci->seek_time){ |
| 539 | |
Thom Johansen | 7113d11 | 2007-10-17 18:19:35 +0000 | [diff] [blame] | 540 | if (ci->seek_time == 1) { |
| 541 | ci->seek_complete(); |
Magnus Holmgren | bce419d | 2008-04-29 19:00:42 +0000 | [diff] [blame] | 542 | goto restart_track; /* Pretend you never saw this... */ |
Thom Johansen | 7113d11 | 2007-10-17 18:19:35 +0000 | [diff] [blame] | 543 | } |
Dave Chapman | c728247 | 2007-07-03 09:25:36 +0000 | [diff] [blame] | 544 | |
Michael Giacomelli | f861320 | 2007-11-22 05:32:26 +0000 | [diff] [blame] | 545 | elapsedtime = seek(ci->seek_time, &wfx); |
Dave Chapman | 05b158f | 2007-12-01 02:46:31 +0000 | [diff] [blame] | 546 | if (elapsedtime < 1){ |
Michael Giacomelli | f861320 | 2007-11-22 05:32:26 +0000 | [diff] [blame] | 547 | ci->seek_complete(); |
| 548 | goto next_track; |
| 549 | } |
Michael Giacomelli | 82dc817 | 2008-04-07 02:32:48 +0000 | [diff] [blame] | 550 | /*DEBUGF("Seek returned %d\n", (int)elapsedtime);*/ |
Michael Giacomelli | f861320 | 2007-11-22 05:32:26 +0000 | [diff] [blame] | 551 | ci->set_elapsed(elapsedtime); |
| 552 | |
| 553 | /*flush the wma decoder state*/ |
Michael Giacomelli | 1a5d594 | 2007-11-30 00:46:32 +0000 | [diff] [blame] | 554 | wmadec.last_superframe_len = 0; |
| 555 | wmadec.last_bitoffset = 0; |
| 556 | ci->seek_complete(); |
Michael Giacomelli | f861320 | 2007-11-22 05:32:26 +0000 | [diff] [blame] | 557 | } |
| 558 | errcount = 0; |
| 559 | new_packet: |
Dave Chapman | 47e9692 | 2007-07-10 13:37:16 +0000 | [diff] [blame] | 560 | res = asf_read_packet(&audiobuf, &audiobufsize, &packetlength, &wfx); |
Michael Giacomelli | f861320 | 2007-11-22 05:32:26 +0000 | [diff] [blame] | 561 | |
Dave Chapman | 05b158f | 2007-12-01 02:46:31 +0000 | [diff] [blame] | 562 | 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 Giacomelli | f861320 | 2007-11-22 05:32:26 +0000 | [diff] [blame] | 566 | |
Dave Chapman | 05b158f | 2007-12-01 02:46:31 +0000 | [diff] [blame] | 567 | errcount++; |
Michael Giacomelli | dadd80a | 2008-01-06 02:50:01 +0000 | [diff] [blame] | 568 | DEBUGF("read_packet error %d, errcount %d\n",wmares, errcount); |
Dave Chapman | 05b158f | 2007-12-01 02:46:31 +0000 | [diff] [blame] | 569 | 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 Chapman | c728247 | 2007-07-03 09:25:36 +0000 | [diff] [blame] | 577 | |
Dave Chapman | 88e32c2 | 2007-07-09 17:24:00 +0000 | [diff] [blame] | 578 | for (i=0; i < wmadec.nb_frames; i++) |
| 579 | { |
| 580 | wmares = wma_decode_superframe_frame(&wmadec, |
| 581 | decoded, |
Dave Chapman | 47e9692 | 2007-07-10 13:37:16 +0000 | [diff] [blame] | 582 | audiobuf, audiobufsize); |
Michael Giacomelli | 1a5d594 | 2007-11-30 00:46:32 +0000 | [diff] [blame] | 583 | |
Dave Chapman | 88e32c2 | 2007-07-09 17:24:00 +0000 | [diff] [blame] | 584 | ci->yield (); |
| 585 | |
Dave Chapman | 05b158f | 2007-12-01 02:46:31 +0000 | [diff] [blame] | 586 | if (wmares < 0) { |
| 587 | /* Do the above, but for errors in decode. */ |
Michael Giacomelli | f861320 | 2007-11-22 05:32:26 +0000 | [diff] [blame] | 588 | 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 Chapman | 88e32c2 | 2007-07-09 17:24:00 +0000 | [diff] [blame] | 596 | } else if (wmares > 0) { |
| 597 | ci->pcmbuf_insert(decoded, NULL, wmares); |
Michael Giacomelli | f861320 | 2007-11-22 05:32:26 +0000 | [diff] [blame] | 598 | elapsedtime += (wmares*10)/(wfx.rate/100); |
Dave Chapman | 88e32c2 | 2007-07-09 17:24:00 +0000 | [diff] [blame] | 599 | ci->set_elapsed(elapsedtime); |
| 600 | } |
Michael Giacomelli | f861320 | 2007-11-22 05:32:26 +0000 | [diff] [blame] | 601 | ci->yield(); |
Dave Chapman | c728247 | 2007-07-03 09:25:36 +0000 | [diff] [blame] | 602 | } |
Dave Chapman | c728247 | 2007-07-03 09:25:36 +0000 | [diff] [blame] | 603 | } |
Dave Chapman | 47e9692 | 2007-07-10 13:37:16 +0000 | [diff] [blame] | 604 | |
| 605 | ci->advance_buffer(packetlength); |
Dave Chapman | c728247 | 2007-07-03 09:25:36 +0000 | [diff] [blame] | 606 | } |
| 607 | retval = CODEC_OK; |
| 608 | |
| 609 | done: |
Michael Giacomelli | 82dc817 | 2008-04-07 02:32:48 +0000 | [diff] [blame] | 610 | /*LOGF("WMA: Decoded %ld samples\n",elapsedtime*wfx.rate/1000);*/ |
Dave Chapman | c728247 | 2007-07-03 09:25:36 +0000 | [diff] [blame] | 611 | |
| 612 | if (ci->request_next_track()) |
| 613 | goto next_track; |
Dave Chapman | c728247 | 2007-07-03 09:25:36 +0000 | [diff] [blame] | 614 | exit: |
| 615 | return retval; |
| 616 | } |