Dave Chapman | fbd8e5d | 2006-02-01 16:42:02 +0000 | [diff] [blame] | 1 | /*************************************************************************** |
| 2 | * __________ __ ___. |
| 3 | * Open \______ \ ____ ____ | | _\_ |__ _______ ___ |
| 4 | * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / |
| 5 | * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < |
| 6 | * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ |
| 7 | * \/ \/ \/ \/ \/ |
| 8 | * $Id$ |
| 9 | * |
| 10 | * Copyright (c) 2005 Jvo Studer |
| 11 | * |
Daniel Stenberg | 2acc0ac | 2008-06-28 18:10:04 +0000 | [diff] [blame^] | 12 | * This program is free software; you can redistribute it and/or |
| 13 | * modify it under the terms of the GNU General Public License |
| 14 | * as published by the Free Software Foundation; either version 2 |
| 15 | * of the License, or (at your option) any later version. |
Dave Chapman | fbd8e5d | 2006-02-01 16:42:02 +0000 | [diff] [blame] | 16 | * |
| 17 | * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY |
| 18 | * KIND, either express or implied. |
| 19 | * |
| 20 | ****************************************************************************/ |
| 21 | |
| 22 | #include "codeclib.h" |
Thom Johansen | d8eaefe | 2006-03-20 20:19:40 +0000 | [diff] [blame] | 23 | #include <inttypes.h> |
Dave Chapman | fbd8e5d | 2006-02-01 16:42:02 +0000 | [diff] [blame] | 24 | |
| 25 | CODEC_HEADER |
| 26 | |
Thom Johansen | d8eaefe | 2006-03-20 20:19:40 +0000 | [diff] [blame] | 27 | /* Macro that sign extends an unsigned byte */ |
| 28 | #define SE(x) ((int32_t)((int8_t)(x))) |
| 29 | |
Dave Chapman | fbd8e5d | 2006-02-01 16:42:02 +0000 | [diff] [blame] | 30 | /* This codec supports AIFF files with the following formats: |
Thom Johansen | d8eaefe | 2006-03-20 20:19:40 +0000 | [diff] [blame] | 31 | * - PCM, 8, 16 and 24 bits, mono or stereo |
Dave Chapman | fbd8e5d | 2006-02-01 16:42:02 +0000 | [diff] [blame] | 32 | */ |
| 33 | |
| 34 | enum |
| 35 | { |
| 36 | AIFF_FORMAT_PCM = 0x0001, /* AIFF PCM Format (big endian) */ |
| 37 | IEEE_FORMAT_FLOAT = 0x0003, /* IEEE Float */ |
| 38 | AIFF_FORMAT_ALAW = 0x0004, /* AIFC ALaw compressed */ |
| 39 | AIFF_FORMAT_ULAW = 0x0005 /* AIFC uLaw compressed */ |
| 40 | }; |
| 41 | |
| 42 | /* Maximum number of bytes to process in one iteration */ |
| 43 | /* for 44.1kHz stereo 16bits, this represents 0.023s ~= 1/50s */ |
| 44 | #define AIF_CHUNK_SIZE (1024*2) |
| 45 | |
Thom Johansen | d8eaefe | 2006-03-20 20:19:40 +0000 | [diff] [blame] | 46 | static int32_t samples[AIF_CHUNK_SIZE] IBSS_ATTR; |
Dave Chapman | fbd8e5d | 2006-02-01 16:42:02 +0000 | [diff] [blame] | 47 | |
Tomasz Malesinski | 80da8b1 | 2006-11-26 18:31:41 +0000 | [diff] [blame] | 48 | enum codec_status codec_main(void) |
Dave Chapman | fbd8e5d | 2006-02-01 16:42:02 +0000 | [diff] [blame] | 49 | { |
Thom Johansen | d8eaefe | 2006-03-20 20:19:40 +0000 | [diff] [blame] | 50 | uint32_t numbytes, bytesdone; |
| 51 | uint16_t num_channels = 0; |
| 52 | uint32_t num_sample_frames = 0; |
| 53 | uint16_t sample_size = 0; |
| 54 | uint32_t sample_rate = 0; |
| 55 | uint32_t i; |
Michael Sevakis | aba6ca0 | 2007-02-07 00:51:50 +0000 | [diff] [blame] | 56 | size_t n; |
| 57 | int bufcount; |
Thom Johansen | d8eaefe | 2006-03-20 20:19:40 +0000 | [diff] [blame] | 58 | int endofstream; |
| 59 | unsigned char *buf; |
| 60 | uint8_t *aifbuf; |
| 61 | long chunksize; |
| 62 | uint32_t offset2snd = 0; |
| 63 | uint16_t block_size = 0; |
| 64 | uint32_t avgbytespersec = 0; |
| 65 | off_t firstblockposn; /* position of the first block in file */ |
Dave Chapman | fbd8e5d | 2006-02-01 16:42:02 +0000 | [diff] [blame] | 66 | |
Thom Johansen | d8eaefe | 2006-03-20 20:19:40 +0000 | [diff] [blame] | 67 | /* Generic codec initialisation */ |
Michael Sevakis | 97f369a | 2007-02-10 16:34:16 +0000 | [diff] [blame] | 68 | ci->configure(DSP_SET_SAMPLE_DEPTH, 28); |
| 69 | ci->configure(CODEC_SET_FILEBUF_WATERMARK, 1024*512); |
Dave Chapman | fbd8e5d | 2006-02-01 16:42:02 +0000 | [diff] [blame] | 70 | |
Thom Johansen | d8eaefe | 2006-03-20 20:19:40 +0000 | [diff] [blame] | 71 | next_track: |
Tomasz Malesinski | 80da8b1 | 2006-11-26 18:31:41 +0000 | [diff] [blame] | 72 | if (codec_init()) { |
Thom Johansen | d8eaefe | 2006-03-20 20:19:40 +0000 | [diff] [blame] | 73 | i = CODEC_ERROR; |
| 74 | goto exit; |
Dave Chapman | fbd8e5d | 2006-02-01 16:42:02 +0000 | [diff] [blame] | 75 | } |
| 76 | |
Magnus Holmgren | 87842ca | 2008-02-24 19:12:15 +0000 | [diff] [blame] | 77 | while (!*ci->taginfo_ready && !ci->stop_codec) |
| 78 | ci->sleep(1); |
Michael Sevakis | 9b9e227 | 2007-02-26 17:15:04 +0000 | [diff] [blame] | 79 | |
| 80 | codec_set_replaygain(ci->id3); |
Thom Johansen | d8eaefe | 2006-03-20 20:19:40 +0000 | [diff] [blame] | 81 | |
| 82 | /* assume the AIFF header is less than 1024 bytes */ |
Brandon Low | c76904b | 2006-03-24 14:02:27 +0000 | [diff] [blame] | 83 | buf = ci->request_buffer(&n, 1024); |
Michael Sevakis | cee9d3b | 2006-11-25 21:27:46 +0000 | [diff] [blame] | 84 | if (n < 54) { |
Thom Johansen | d8eaefe | 2006-03-20 20:19:40 +0000 | [diff] [blame] | 85 | i = CODEC_ERROR; |
Brandon Low | f3bc1ef | 2006-04-22 14:40:13 +0000 | [diff] [blame] | 86 | goto done; |
Thom Johansen | d8eaefe | 2006-03-20 20:19:40 +0000 | [diff] [blame] | 87 | } |
| 88 | if ((memcmp(buf, "FORM", 4) != 0) || (memcmp(&buf[8], "AIFF", 4) != 0)) { |
| 89 | i = CODEC_ERROR; |
Brandon Low | f3bc1ef | 2006-04-22 14:40:13 +0000 | [diff] [blame] | 90 | goto done; |
Dave Chapman | fbd8e5d | 2006-02-01 16:42:02 +0000 | [diff] [blame] | 91 | } |
| 92 | |
Thom Johansen | d8eaefe | 2006-03-20 20:19:40 +0000 | [diff] [blame] | 93 | buf += 12; |
| 94 | n -= 12; |
| 95 | numbytes = 0; |
| 96 | |
| 97 | /* read until 'SSND' chunk, which typically is last */ |
| 98 | while (numbytes == 0 && n >= 8) { |
| 99 | /* chunkSize */ |
| 100 | i = ((buf[4]<<24)|(buf[5]<<16)|(buf[6]<<8)|buf[7]); |
| 101 | if (memcmp(buf, "COMM", 4) == 0) { |
Jens Arnold | 27b4a64 | 2007-04-23 23:11:33 +0000 | [diff] [blame] | 102 | if (i < 18) { |
| 103 | DEBUGF("CODEC_ERROR: 'COMM' chunk size=%lu < 18\n", |
Jens Arnold | bd5c0ad | 2007-03-17 10:50:58 +0000 | [diff] [blame] | 104 | (unsigned long)i); |
Thom Johansen | d8eaefe | 2006-03-20 20:19:40 +0000 | [diff] [blame] | 105 | i = CODEC_ERROR; |
Brandon Low | f3bc1ef | 2006-04-22 14:40:13 +0000 | [diff] [blame] | 106 | goto done; |
Thom Johansen | d8eaefe | 2006-03-20 20:19:40 +0000 | [diff] [blame] | 107 | } |
| 108 | /* num_channels */ |
| 109 | num_channels = ((buf[8]<<8)|buf[9]); |
| 110 | /* num_sample_frames */ |
| 111 | num_sample_frames = ((buf[10]<<24)|(buf[11]<<16)|(buf[12]<<8) |
| 112 | |buf[13]); |
| 113 | /* sample_size */ |
| 114 | sample_size = ((buf[14]<<8)|buf[15]); |
| 115 | /* sample_rate (don't use last 4 bytes, only integer fs) */ |
| 116 | if (buf[16] != 0x40) { |
Jens Arnold | f68362a | 2007-03-17 09:54:28 +0000 | [diff] [blame] | 117 | DEBUGF("CODEC_ERROR: weird sampling rate (no @)\n"); |
Thom Johansen | d8eaefe | 2006-03-20 20:19:40 +0000 | [diff] [blame] | 118 | i = CODEC_ERROR; |
Brandon Low | f3bc1ef | 2006-04-22 14:40:13 +0000 | [diff] [blame] | 119 | goto done; |
Thom Johansen | d8eaefe | 2006-03-20 20:19:40 +0000 | [diff] [blame] | 120 | } |
| 121 | sample_rate = ((buf[18]<<24)|(buf[19]<<16)|(buf[20]<<8)|buf[21])+1; |
| 122 | sample_rate = sample_rate >> (16 + 14 - buf[17]); |
| 123 | /* calc average bytes per second */ |
| 124 | avgbytespersec = sample_rate*num_channels*sample_size/8; |
| 125 | } else if (memcmp(buf, "SSND", 4)==0) { |
| 126 | if (sample_size == 0) { |
| 127 | DEBUGF("CODEC_ERROR: unsupported chunk order\n"); |
| 128 | i = CODEC_ERROR; |
Brandon Low | f3bc1ef | 2006-04-22 14:40:13 +0000 | [diff] [blame] | 129 | goto done; |
Thom Johansen | d8eaefe | 2006-03-20 20:19:40 +0000 | [diff] [blame] | 130 | } |
| 131 | /* offset2snd */ |
Michael Sevakis | cee9d3b | 2006-11-25 21:27:46 +0000 | [diff] [blame] | 132 | offset2snd = (buf[8]<<24)|(buf[9]<<16)|(buf[10]<<8)|buf[11]; |
Thom Johansen | d8eaefe | 2006-03-20 20:19:40 +0000 | [diff] [blame] | 133 | /* block_size */ |
Michael Sevakis | cee9d3b | 2006-11-25 21:27:46 +0000 | [diff] [blame] | 134 | block_size = (buf[12]<<24)|(buf[13]<<16)|(buf[14]<<8)|buf[15]; |
Thom Johansen | d8eaefe | 2006-03-20 20:19:40 +0000 | [diff] [blame] | 135 | if (block_size == 0) |
| 136 | block_size = num_channels*sample_size; |
| 137 | numbytes = i - 8 - offset2snd; |
| 138 | i = 8 + offset2snd; /* advance to the beginning of data */ |
| 139 | } else { |
| 140 | DEBUGF("unsupported AIFF chunk: '%c%c%c%c', size=%lu\n", |
Jens Arnold | bd5c0ad | 2007-03-17 10:50:58 +0000 | [diff] [blame] | 141 | buf[0], buf[1], buf[2], buf[3], (unsigned long)i); |
Thom Johansen | d8eaefe | 2006-03-20 20:19:40 +0000 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | if (i & 0x01) /* odd chunk sizes must be padded */ |
| 145 | i++; |
| 146 | buf += i + 8; |
| 147 | if (n < (i + 8)) { |
| 148 | DEBUGF("CODEC_ERROR: AIFF header size > 1024\n"); |
| 149 | i = CODEC_ERROR; |
Brandon Low | f3bc1ef | 2006-04-22 14:40:13 +0000 | [diff] [blame] | 150 | goto done; |
Thom Johansen | d8eaefe | 2006-03-20 20:19:40 +0000 | [diff] [blame] | 151 | } |
| 152 | n -= i + 8; |
| 153 | } /* while 'SSND' */ |
| 154 | |
| 155 | if (num_channels == 0) { |
| 156 | DEBUGF("CODEC_ERROR: 'COMM' chunk not found or 0-channels file\n"); |
| 157 | i = CODEC_ERROR; |
Brandon Low | f3bc1ef | 2006-04-22 14:40:13 +0000 | [diff] [blame] | 158 | goto done; |
Thom Johansen | d8eaefe | 2006-03-20 20:19:40 +0000 | [diff] [blame] | 159 | } |
| 160 | if (numbytes == 0) { |
| 161 | DEBUGF("CODEC_ERROR: 'SSND' chunk not found or has zero length\n"); |
| 162 | i = CODEC_ERROR; |
Brandon Low | f3bc1ef | 2006-04-22 14:40:13 +0000 | [diff] [blame] | 163 | goto done; |
Thom Johansen | d8eaefe | 2006-03-20 20:19:40 +0000 | [diff] [blame] | 164 | } |
| 165 | if (sample_size > 24) { |
| 166 | DEBUGF("CODEC_ERROR: PCM with more than 24 bits per sample " |
| 167 | "is unsupported\n"); |
| 168 | i = CODEC_ERROR; |
Brandon Low | f3bc1ef | 2006-04-22 14:40:13 +0000 | [diff] [blame] | 169 | goto done; |
Dave Chapman | fbd8e5d | 2006-02-01 16:42:02 +0000 | [diff] [blame] | 170 | } |
| 171 | |
Michael Sevakis | 97f369a | 2007-02-10 16:34:16 +0000 | [diff] [blame] | 172 | ci->configure(DSP_SWITCH_FREQUENCY, ci->id3->frequency); |
Dave Chapman | fbd8e5d | 2006-02-01 16:42:02 +0000 | [diff] [blame] | 173 | |
Thom Johansen | d8eaefe | 2006-03-20 20:19:40 +0000 | [diff] [blame] | 174 | if (num_channels == 2) { |
Michael Sevakis | 97f369a | 2007-02-10 16:34:16 +0000 | [diff] [blame] | 175 | ci->configure(DSP_SET_STEREO_MODE, STEREO_INTERLEAVED); |
Thom Johansen | d8eaefe | 2006-03-20 20:19:40 +0000 | [diff] [blame] | 176 | } else if (num_channels == 1) { |
Michael Sevakis | 97f369a | 2007-02-10 16:34:16 +0000 | [diff] [blame] | 177 | ci->configure(DSP_SET_STEREO_MODE, STEREO_MONO); |
Thom Johansen | d8eaefe | 2006-03-20 20:19:40 +0000 | [diff] [blame] | 178 | } else { |
| 179 | DEBUGF("CODEC_ERROR: more than 2 channels unsupported\n"); |
| 180 | i = CODEC_ERROR; |
Brandon Low | f3bc1ef | 2006-04-22 14:40:13 +0000 | [diff] [blame] | 181 | goto done; |
Thom Johansen | d8eaefe | 2006-03-20 20:19:40 +0000 | [diff] [blame] | 182 | } |
Dave Chapman | fbd8e5d | 2006-02-01 16:42:02 +0000 | [diff] [blame] | 183 | |
Thom Johansen | d8eaefe | 2006-03-20 20:19:40 +0000 | [diff] [blame] | 184 | firstblockposn = 1024 - n; |
| 185 | ci->advance_buffer(firstblockposn); |
| 186 | |
| 187 | /* The main decoder loop */ |
| 188 | bytesdone = 0; |
| 189 | ci->set_elapsed(0); |
| 190 | endofstream = 0; |
| 191 | /* chunksize is computed so that one chunk is about 1/50s. |
| 192 | * this make 4096 for 44.1kHz 16bits stereo. |
| 193 | * It also has to be a multiple of blockalign */ |
| 194 | chunksize = (1 + avgbytespersec/(50*block_size))*block_size; |
| 195 | /* check that the output buffer is big enough (convert to samplespersec, |
| 196 | then round to the block_size multiple below) */ |
| 197 | if (((uint64_t)chunksize*ci->id3->frequency*num_channels*2) |
| 198 | /(uint64_t)avgbytespersec >= AIF_CHUNK_SIZE) { |
| 199 | chunksize = ((uint64_t)AIF_CHUNK_SIZE*avgbytespersec |
| 200 | /((uint64_t)ci->id3->frequency*num_channels*2 |
| 201 | *block_size))*block_size; |
| 202 | } |
| 203 | |
| 204 | while (!endofstream) { |
| 205 | ci->yield(); |
Brandon Low | ebadcc6 | 2006-04-15 02:03:11 +0000 | [diff] [blame] | 206 | if (ci->stop_codec || ci->new_track) |
Thom Johansen | d8eaefe | 2006-03-20 20:19:40 +0000 | [diff] [blame] | 207 | break; |
| 208 | |
| 209 | if (ci->seek_time) { |
| 210 | uint32_t newpos; |
| 211 | |
| 212 | /* use avgbytespersec to round to the closest blockalign multiple, |
| 213 | add firstblockposn. 64-bit casts to avoid overflows. */ |
| 214 | newpos = (((uint64_t)avgbytespersec*(ci->seek_time - 1)) |
| 215 | /(1000LL*block_size))*block_size; |
| 216 | if (newpos > numbytes) |
| 217 | break; |
| 218 | if (ci->seek_buffer(firstblockposn + newpos)) |
| 219 | bytesdone = newpos; |
| 220 | ci->seek_complete(); |
| 221 | } |
Brandon Low | c76904b | 2006-03-24 14:02:27 +0000 | [diff] [blame] | 222 | aifbuf = (uint8_t *)ci->request_buffer(&n, chunksize); |
Thom Johansen | d8eaefe | 2006-03-20 20:19:40 +0000 | [diff] [blame] | 223 | |
| 224 | if (n == 0) |
| 225 | break; /* End of stream */ |
| 226 | |
| 227 | if (bytesdone + n > numbytes) { |
| 228 | n = numbytes - bytesdone; |
| 229 | endofstream = 1; |
| 230 | } |
| 231 | |
| 232 | if (sample_size > 24) { |
| 233 | for (i = 0; i < n; i += 4) { |
| 234 | samples[i/4] = (SE(aifbuf[i])<<21)|(aifbuf[i + 1]<<13) |
| 235 | |(aifbuf[i + 2]<<5)|(aifbuf[i + 3]>>3); |
| 236 | } |
Michael Sevakis | aba6ca0 | 2007-02-07 00:51:50 +0000 | [diff] [blame] | 237 | bufcount = n >> 2; |
Thom Johansen | d8eaefe | 2006-03-20 20:19:40 +0000 | [diff] [blame] | 238 | } else if (sample_size > 16) { |
| 239 | for (i = 0; i < n; i += 3) { |
| 240 | samples[i/3] = (SE(aifbuf[i])<<21)|(aifbuf[i + 1]<<13) |
| 241 | |(aifbuf[i + 2]<<5); |
| 242 | } |
Michael Sevakis | aba6ca0 | 2007-02-07 00:51:50 +0000 | [diff] [blame] | 243 | bufcount = n/3; |
Thom Johansen | d8eaefe | 2006-03-20 20:19:40 +0000 | [diff] [blame] | 244 | } else if (sample_size > 8) { |
| 245 | for (i = 0; i < n; i += 2) |
| 246 | samples[i/2] = (SE(aifbuf[i])<<21)|(aifbuf[i + 1]<<13); |
Michael Sevakis | aba6ca0 | 2007-02-07 00:51:50 +0000 | [diff] [blame] | 247 | bufcount = n >> 1; |
Thom Johansen | d8eaefe | 2006-03-20 20:19:40 +0000 | [diff] [blame] | 248 | } else { |
| 249 | for (i = 0; i < n; i++) |
| 250 | samples[i] = SE(aifbuf[i]) << 21; |
Michael Sevakis | aba6ca0 | 2007-02-07 00:51:50 +0000 | [diff] [blame] | 251 | bufcount = n; |
Thom Johansen | d8eaefe | 2006-03-20 20:19:40 +0000 | [diff] [blame] | 252 | } |
| 253 | |
Michael Sevakis | aba6ca0 | 2007-02-07 00:51:50 +0000 | [diff] [blame] | 254 | if (num_channels == 2) |
| 255 | bufcount >>= 1; |
| 256 | |
| 257 | ci->pcmbuf_insert(samples, NULL, bufcount); |
Thom Johansen | d8eaefe | 2006-03-20 20:19:40 +0000 | [diff] [blame] | 258 | |
| 259 | ci->advance_buffer(n); |
| 260 | bytesdone += n; |
| 261 | if (bytesdone >= numbytes) |
| 262 | endofstream = 1; |
| 263 | |
| 264 | ci->set_elapsed(bytesdone*1000LL/avgbytespersec); |
| 265 | } |
Brandon Low | f3bc1ef | 2006-04-22 14:40:13 +0000 | [diff] [blame] | 266 | i = CODEC_OK; |
Thom Johansen | d8eaefe | 2006-03-20 20:19:40 +0000 | [diff] [blame] | 267 | |
Brandon Low | f3bc1ef | 2006-04-22 14:40:13 +0000 | [diff] [blame] | 268 | done: |
Thom Johansen | d8eaefe | 2006-03-20 20:19:40 +0000 | [diff] [blame] | 269 | if (ci->request_next_track()) |
| 270 | goto next_track; |
| 271 | |
Dave Chapman | fbd8e5d | 2006-02-01 16:42:02 +0000 | [diff] [blame] | 272 | exit: |
Thom Johansen | d8eaefe | 2006-03-20 20:19:40 +0000 | [diff] [blame] | 273 | return i; |
Dave Chapman | fbd8e5d | 2006-02-01 16:42:02 +0000 | [diff] [blame] | 274 | } |
Thom Johansen | d8eaefe | 2006-03-20 20:19:40 +0000 | [diff] [blame] | 275 | |