HAVE_ADJUSTABLE_CPU_FREQ isn't defined for simulators, so we don't have to check...
[Rockbox.git] / apps / codecs / aiff.c
blob144f4627bb8870ef139610812eba4b24ec78c8e4
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (c) 2005 Jvo Studer
12 * All files in this archive are subject to the GNU General Public License.
13 * See the file COPYING in the source tree root for full license agreement.
15 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
16 * KIND, either express or implied.
18 ****************************************************************************/
20 #include "codeclib.h"
21 #include <inttypes.h>
23 CODEC_HEADER
25 /* Macro that sign extends an unsigned byte */
26 #define SE(x) ((int32_t)((int8_t)(x)))
28 struct codec_api *rb;
30 /* This codec supports AIFF files with the following formats:
31 * - PCM, 8, 16 and 24 bits, mono or stereo
34 enum
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 */
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)
46 #ifdef USE_IRAM
47 extern char iramcopy[];
48 extern char iramstart[];
49 extern char iramend[];
50 extern char iedata[];
51 extern char iend[];
52 #endif
54 static int32_t samples[AIF_CHUNK_SIZE] IBSS_ATTR;
56 enum codec_status codec_start(struct codec_api *api)
58 struct codec_api *ci;
59 uint32_t numbytes, bytesdone;
60 uint16_t num_channels = 0;
61 uint32_t num_sample_frames = 0;
62 uint16_t sample_size = 0;
63 uint32_t sample_rate = 0;
64 uint32_t i;
65 size_t n, bufsize;
66 int endofstream;
67 unsigned char *buf;
68 uint8_t *aifbuf;
69 long chunksize;
70 uint32_t offset2snd = 0;
71 uint16_t block_size = 0;
72 uint32_t avgbytespersec = 0;
73 off_t firstblockposn; /* position of the first block in file */
75 /* Generic codec initialisation */
76 rb = api;
77 ci = api;
79 #ifdef USE_IRAM
80 ci->memcpy(iramstart, iramcopy, iramend - iramstart);
81 ci->memset(iedata, 0, iend - iedata);
82 #endif
84 ci->configure(CODEC_SET_FILEBUF_WATERMARK, (int *)(1024*512));
85 ci->configure(CODEC_SET_FILEBUF_CHUNKSIZE, (int *)(1024*256));
86 ci->configure(DSP_DITHER, (bool *)false);
88 next_track:
89 if (codec_init(api)) {
90 i = CODEC_ERROR;
91 goto exit;
94 while (!*ci->taginfo_ready)
95 ci->yield();
97 /* assume the AIFF header is less than 1024 bytes */
98 buf = ci->request_buffer(&n, 1024);
99 if (n < 44) {
100 i = CODEC_ERROR;
101 goto exit;
103 if ((memcmp(buf, "FORM", 4) != 0) || (memcmp(&buf[8], "AIFF", 4) != 0)) {
104 i = CODEC_ERROR;
105 goto exit;
108 buf += 12;
109 n -= 12;
110 numbytes = 0;
112 /* read until 'SSND' chunk, which typically is last */
113 while (numbytes == 0 && n >= 8) {
114 /* chunkSize */
115 i = ((buf[4]<<24)|(buf[5]<<16)|(buf[6]<<8)|buf[7]);
116 if (memcmp(buf, "COMM", 4) == 0) {
117 if (i != 18) {
118 DEBUGF("CODEC_ERROR: 'COMM' chunk size=%lu != 18\n", i);
119 i = CODEC_ERROR;
120 goto exit;
122 /* num_channels */
123 num_channels = ((buf[8]<<8)|buf[9]);
124 /* num_sample_frames */
125 num_sample_frames = ((buf[10]<<24)|(buf[11]<<16)|(buf[12]<<8)
126 |buf[13]);
127 /* sample_size */
128 sample_size = ((buf[14]<<8)|buf[15]);
129 /* sample_rate (don't use last 4 bytes, only integer fs) */
130 if (buf[16] != 0x40) {
131 DEBUGF("CODEC_ERROR: weird sampling rate (no @)\n", i);
132 i = CODEC_ERROR;
133 goto exit;
135 sample_rate = ((buf[18]<<24)|(buf[19]<<16)|(buf[20]<<8)|buf[21])+1;
136 sample_rate = sample_rate >> (16 + 14 - buf[17]);
137 /* calc average bytes per second */
138 avgbytespersec = sample_rate*num_channels*sample_size/8;
139 } else if (memcmp(buf, "SSND", 4)==0) {
140 if (sample_size == 0) {
141 DEBUGF("CODEC_ERROR: unsupported chunk order\n");
142 i = CODEC_ERROR;
143 goto exit;
145 /* offset2snd */
146 offset2snd = ((buf[8]<<8)|buf[9]);
147 /* block_size */
148 block_size = ((buf[10]<<8)|buf[11]);
149 if (block_size == 0)
150 block_size = num_channels*sample_size;
151 numbytes = i - 8 - offset2snd;
152 i = 8 + offset2snd; /* advance to the beginning of data */
153 } else {
154 DEBUGF("unsupported AIFF chunk: '%c%c%c%c', size=%lu\n",
155 buf[0], buf[1], buf[2], buf[3], i);
158 if (i & 0x01) /* odd chunk sizes must be padded */
159 i++;
160 buf += i + 8;
161 if (n < (i + 8)) {
162 DEBUGF("CODEC_ERROR: AIFF header size > 1024\n");
163 i = CODEC_ERROR;
164 goto exit;
166 n -= i + 8;
167 } /* while 'SSND' */
169 if (num_channels == 0) {
170 DEBUGF("CODEC_ERROR: 'COMM' chunk not found or 0-channels file\n");
171 i = CODEC_ERROR;
172 goto exit;
174 if (numbytes == 0) {
175 DEBUGF("CODEC_ERROR: 'SSND' chunk not found or has zero length\n");
176 i = CODEC_ERROR;
177 goto exit;
179 if (sample_size > 24) {
180 DEBUGF("CODEC_ERROR: PCM with more than 24 bits per sample "
181 "is unsupported\n");
182 i = CODEC_ERROR;
183 goto exit;
186 ci->configure(CODEC_DSP_ENABLE, (bool *)true);
187 ci->configure(DSP_SET_FREQUENCY, (long *)(ci->id3->frequency));
188 ci->configure(DSP_SET_SAMPLE_DEPTH, (long *)28);
190 if (num_channels == 2) {
191 ci->configure(DSP_SET_STEREO_MODE, (int *)STEREO_INTERLEAVED);
192 } else if (num_channels == 1) {
193 ci->configure(DSP_SET_STEREO_MODE, (int *)STEREO_MONO);
194 } else {
195 DEBUGF("CODEC_ERROR: more than 2 channels unsupported\n");
196 i = CODEC_ERROR;
197 goto exit;
200 firstblockposn = 1024 - n;
201 ci->advance_buffer(firstblockposn);
203 /* The main decoder loop */
204 bytesdone = 0;
205 ci->set_elapsed(0);
206 endofstream = 0;
207 /* chunksize is computed so that one chunk is about 1/50s.
208 * this make 4096 for 44.1kHz 16bits stereo.
209 * It also has to be a multiple of blockalign */
210 chunksize = (1 + avgbytespersec/(50*block_size))*block_size;
211 /* check that the output buffer is big enough (convert to samplespersec,
212 then round to the block_size multiple below) */
213 if (((uint64_t)chunksize*ci->id3->frequency*num_channels*2)
214 /(uint64_t)avgbytespersec >= AIF_CHUNK_SIZE) {
215 chunksize = ((uint64_t)AIF_CHUNK_SIZE*avgbytespersec
216 /((uint64_t)ci->id3->frequency*num_channels*2
217 *block_size))*block_size;
220 while (!endofstream) {
221 ci->yield();
222 if (ci->stop_codec || ci->reload_codec)
223 break;
225 if (ci->seek_time) {
226 uint32_t newpos;
228 /* use avgbytespersec to round to the closest blockalign multiple,
229 add firstblockposn. 64-bit casts to avoid overflows. */
230 newpos = (((uint64_t)avgbytespersec*(ci->seek_time - 1))
231 /(1000LL*block_size))*block_size;
232 if (newpos > numbytes)
233 break;
234 if (ci->seek_buffer(firstblockposn + newpos))
235 bytesdone = newpos;
236 ci->seek_complete();
238 aifbuf = (uint8_t *)ci->request_buffer(&n, chunksize);
240 if (n == 0)
241 break; /* End of stream */
243 if (bytesdone + n > numbytes) {
244 n = numbytes - bytesdone;
245 endofstream = 1;
248 if (sample_size > 24) {
249 for (i = 0; i < n; i += 4) {
250 samples[i/4] = (SE(aifbuf[i])<<21)|(aifbuf[i + 1]<<13)
251 |(aifbuf[i + 2]<<5)|(aifbuf[i + 3]>>3);
253 bufsize = n;
254 } else if (sample_size > 16) {
255 for (i = 0; i < n; i += 3) {
256 samples[i/3] = (SE(aifbuf[i])<<21)|(aifbuf[i + 1]<<13)
257 |(aifbuf[i + 2]<<5);
259 bufsize = n*4/3;
260 } else if (sample_size > 8) {
261 for (i = 0; i < n; i += 2)
262 samples[i/2] = (SE(aifbuf[i])<<21)|(aifbuf[i + 1]<<13);
263 bufsize = n*2;
264 } else {
265 for (i = 0; i < n; i++)
266 samples[i] = SE(aifbuf[i]) << 21;
267 bufsize = n*4;
270 while (!ci->pcmbuf_insert((char *)samples, bufsize))
271 ci->yield();
273 ci->advance_buffer(n);
274 bytesdone += n;
275 if (bytesdone >= numbytes)
276 endofstream = 1;
278 ci->set_elapsed(bytesdone*1000LL/avgbytespersec);
281 if (ci->request_next_track())
282 goto next_track;
284 i = CODEC_OK;
285 exit:
286 return i;