Change to (or add) 'standard' codec startup delay, fixing potential startup problems...
[kugel-rb.git] / apps / codecs / aiff.c
blob8b90f7fe9c80f6b32d1c949de15dae3e21ed5a25
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 /* This codec supports AIFF files with the following formats:
29 * - PCM, 8, 16 and 24 bits, mono or stereo
32 enum
34 AIFF_FORMAT_PCM = 0x0001, /* AIFF PCM Format (big endian) */
35 IEEE_FORMAT_FLOAT = 0x0003, /* IEEE Float */
36 AIFF_FORMAT_ALAW = 0x0004, /* AIFC ALaw compressed */
37 AIFF_FORMAT_ULAW = 0x0005 /* AIFC uLaw compressed */
40 /* Maximum number of bytes to process in one iteration */
41 /* for 44.1kHz stereo 16bits, this represents 0.023s ~= 1/50s */
42 #define AIF_CHUNK_SIZE (1024*2)
44 static int32_t samples[AIF_CHUNK_SIZE] IBSS_ATTR;
46 enum codec_status codec_main(void)
48 uint32_t numbytes, bytesdone;
49 uint16_t num_channels = 0;
50 uint32_t num_sample_frames = 0;
51 uint16_t sample_size = 0;
52 uint32_t sample_rate = 0;
53 uint32_t i;
54 size_t n;
55 int bufcount;
56 int endofstream;
57 unsigned char *buf;
58 uint8_t *aifbuf;
59 long chunksize;
60 uint32_t offset2snd = 0;
61 uint16_t block_size = 0;
62 uint32_t avgbytespersec = 0;
63 off_t firstblockposn; /* position of the first block in file */
65 /* Generic codec initialisation */
66 ci->configure(DSP_SET_SAMPLE_DEPTH, 28);
67 ci->configure(CODEC_SET_FILEBUF_WATERMARK, 1024*512);
69 next_track:
70 if (codec_init()) {
71 i = CODEC_ERROR;
72 goto exit;
75 while (!*ci->taginfo_ready && !ci->stop_codec)
76 ci->sleep(1);
78 codec_set_replaygain(ci->id3);
80 /* assume the AIFF header is less than 1024 bytes */
81 buf = ci->request_buffer(&n, 1024);
82 if (n < 54) {
83 i = CODEC_ERROR;
84 goto done;
86 if ((memcmp(buf, "FORM", 4) != 0) || (memcmp(&buf[8], "AIFF", 4) != 0)) {
87 i = CODEC_ERROR;
88 goto done;
91 buf += 12;
92 n -= 12;
93 numbytes = 0;
95 /* read until 'SSND' chunk, which typically is last */
96 while (numbytes == 0 && n >= 8) {
97 /* chunkSize */
98 i = ((buf[4]<<24)|(buf[5]<<16)|(buf[6]<<8)|buf[7]);
99 if (memcmp(buf, "COMM", 4) == 0) {
100 if (i < 18) {
101 DEBUGF("CODEC_ERROR: 'COMM' chunk size=%lu < 18\n",
102 (unsigned long)i);
103 i = CODEC_ERROR;
104 goto done;
106 /* num_channels */
107 num_channels = ((buf[8]<<8)|buf[9]);
108 /* num_sample_frames */
109 num_sample_frames = ((buf[10]<<24)|(buf[11]<<16)|(buf[12]<<8)
110 |buf[13]);
111 /* sample_size */
112 sample_size = ((buf[14]<<8)|buf[15]);
113 /* sample_rate (don't use last 4 bytes, only integer fs) */
114 if (buf[16] != 0x40) {
115 DEBUGF("CODEC_ERROR: weird sampling rate (no @)\n");
116 i = CODEC_ERROR;
117 goto done;
119 sample_rate = ((buf[18]<<24)|(buf[19]<<16)|(buf[20]<<8)|buf[21])+1;
120 sample_rate = sample_rate >> (16 + 14 - buf[17]);
121 /* calc average bytes per second */
122 avgbytespersec = sample_rate*num_channels*sample_size/8;
123 } else if (memcmp(buf, "SSND", 4)==0) {
124 if (sample_size == 0) {
125 DEBUGF("CODEC_ERROR: unsupported chunk order\n");
126 i = CODEC_ERROR;
127 goto done;
129 /* offset2snd */
130 offset2snd = (buf[8]<<24)|(buf[9]<<16)|(buf[10]<<8)|buf[11];
131 /* block_size */
132 block_size = (buf[12]<<24)|(buf[13]<<16)|(buf[14]<<8)|buf[15];
133 if (block_size == 0)
134 block_size = num_channels*sample_size;
135 numbytes = i - 8 - offset2snd;
136 i = 8 + offset2snd; /* advance to the beginning of data */
137 } else {
138 DEBUGF("unsupported AIFF chunk: '%c%c%c%c', size=%lu\n",
139 buf[0], buf[1], buf[2], buf[3], (unsigned long)i);
142 if (i & 0x01) /* odd chunk sizes must be padded */
143 i++;
144 buf += i + 8;
145 if (n < (i + 8)) {
146 DEBUGF("CODEC_ERROR: AIFF header size > 1024\n");
147 i = CODEC_ERROR;
148 goto done;
150 n -= i + 8;
151 } /* while 'SSND' */
153 if (num_channels == 0) {
154 DEBUGF("CODEC_ERROR: 'COMM' chunk not found or 0-channels file\n");
155 i = CODEC_ERROR;
156 goto done;
158 if (numbytes == 0) {
159 DEBUGF("CODEC_ERROR: 'SSND' chunk not found or has zero length\n");
160 i = CODEC_ERROR;
161 goto done;
163 if (sample_size > 24) {
164 DEBUGF("CODEC_ERROR: PCM with more than 24 bits per sample "
165 "is unsupported\n");
166 i = CODEC_ERROR;
167 goto done;
170 ci->configure(DSP_SWITCH_FREQUENCY, ci->id3->frequency);
172 if (num_channels == 2) {
173 ci->configure(DSP_SET_STEREO_MODE, STEREO_INTERLEAVED);
174 } else if (num_channels == 1) {
175 ci->configure(DSP_SET_STEREO_MODE, STEREO_MONO);
176 } else {
177 DEBUGF("CODEC_ERROR: more than 2 channels unsupported\n");
178 i = CODEC_ERROR;
179 goto done;
182 firstblockposn = 1024 - n;
183 ci->advance_buffer(firstblockposn);
185 /* The main decoder loop */
186 bytesdone = 0;
187 ci->set_elapsed(0);
188 endofstream = 0;
189 /* chunksize is computed so that one chunk is about 1/50s.
190 * this make 4096 for 44.1kHz 16bits stereo.
191 * It also has to be a multiple of blockalign */
192 chunksize = (1 + avgbytespersec/(50*block_size))*block_size;
193 /* check that the output buffer is big enough (convert to samplespersec,
194 then round to the block_size multiple below) */
195 if (((uint64_t)chunksize*ci->id3->frequency*num_channels*2)
196 /(uint64_t)avgbytespersec >= AIF_CHUNK_SIZE) {
197 chunksize = ((uint64_t)AIF_CHUNK_SIZE*avgbytespersec
198 /((uint64_t)ci->id3->frequency*num_channels*2
199 *block_size))*block_size;
202 while (!endofstream) {
203 ci->yield();
204 if (ci->stop_codec || ci->new_track)
205 break;
207 if (ci->seek_time) {
208 uint32_t newpos;
210 /* use avgbytespersec to round to the closest blockalign multiple,
211 add firstblockposn. 64-bit casts to avoid overflows. */
212 newpos = (((uint64_t)avgbytespersec*(ci->seek_time - 1))
213 /(1000LL*block_size))*block_size;
214 if (newpos > numbytes)
215 break;
216 if (ci->seek_buffer(firstblockposn + newpos))
217 bytesdone = newpos;
218 ci->seek_complete();
220 aifbuf = (uint8_t *)ci->request_buffer(&n, chunksize);
222 if (n == 0)
223 break; /* End of stream */
225 if (bytesdone + n > numbytes) {
226 n = numbytes - bytesdone;
227 endofstream = 1;
230 if (sample_size > 24) {
231 for (i = 0; i < n; i += 4) {
232 samples[i/4] = (SE(aifbuf[i])<<21)|(aifbuf[i + 1]<<13)
233 |(aifbuf[i + 2]<<5)|(aifbuf[i + 3]>>3);
235 bufcount = n >> 2;
236 } else if (sample_size > 16) {
237 for (i = 0; i < n; i += 3) {
238 samples[i/3] = (SE(aifbuf[i])<<21)|(aifbuf[i + 1]<<13)
239 |(aifbuf[i + 2]<<5);
241 bufcount = n/3;
242 } else if (sample_size > 8) {
243 for (i = 0; i < n; i += 2)
244 samples[i/2] = (SE(aifbuf[i])<<21)|(aifbuf[i + 1]<<13);
245 bufcount = n >> 1;
246 } else {
247 for (i = 0; i < n; i++)
248 samples[i] = SE(aifbuf[i]) << 21;
249 bufcount = n;
252 if (num_channels == 2)
253 bufcount >>= 1;
255 ci->pcmbuf_insert(samples, NULL, bufcount);
257 ci->advance_buffer(n);
258 bytesdone += n;
259 if (bytesdone >= numbytes)
260 endofstream = 1;
262 ci->set_elapsed(bytesdone*1000LL/avgbytespersec);
264 i = CODEC_OK;
266 done:
267 if (ci->request_next_track())
268 goto next_track;
270 exit:
271 return i;