Calibrate iPod Classic battery gauge a bit better
[kugel-rb.git] / apps / codecs / aiff.c
blobd4cf8660ddc7b95b845f3c9209c10456a5f3e415
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (c) 2005 Jvo Studer
11 * Copyright (c) 2009 Yoshihisa Uchida
13 * This program is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU General Public License
15 * as published by the Free Software Foundation; either version 2
16 * of the License, or (at your option) any later version.
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
21 ****************************************************************************/
23 #include "codeclib.h"
24 #include "codecs/libpcm/support_formats.h"
26 CODEC_HEADER
28 #define FOURCC(c1, c2, c3, c4) \
29 ((((uint32_t)c1)<<24)|(((uint32_t)c2)<<16)|(((uint32_t)c3)<<8)|((uint32_t)c4))
31 /* This codec supports the following AIFC compressionType formats */
32 enum {
33 AIFC_FORMAT_PCM = FOURCC('N', 'O', 'N', 'E'), /* AIFC PCM Format (big endian) */
34 AIFC_FORMAT_ALAW = FOURCC('a', 'l', 'a', 'w'), /* AIFC ALaw compressed */
35 AIFC_FORMAT_MULAW = FOURCC('u', 'l', 'a', 'w'), /* AIFC uLaw compressed */
36 AIFC_FORMAT_IEEE_FLOAT32 = FOURCC('f', 'l', '3', '2'), /* AIFC IEEE float 32 bit */
37 AIFC_FORMAT_IEEE_FLOAT64 = FOURCC('f', 'l', '6', '4'), /* AIFC IEEE float 64 bit */
38 AIFC_FORMAT_QT_IMA_ADPCM = FOURCC('i', 'm', 'a', '4'), /* AIFC QuickTime IMA ADPCM */
41 static const struct pcm_entry pcm_codecs[] = {
42 { AIFC_FORMAT_PCM, get_linear_pcm_codec },
43 { AIFC_FORMAT_ALAW, get_itut_g711_alaw_codec },
44 { AIFC_FORMAT_MULAW, get_itut_g711_mulaw_codec },
45 { AIFC_FORMAT_IEEE_FLOAT32, get_ieee_float_codec },
46 { AIFC_FORMAT_IEEE_FLOAT64, get_ieee_float_codec },
47 { AIFC_FORMAT_QT_IMA_ADPCM, get_qt_ima_adpcm_codec },
50 #define PCM_SAMPLE_SIZE (1024*2)
52 static int32_t samples[PCM_SAMPLE_SIZE] IBSS_ATTR;
54 static const struct pcm_codec *get_codec(uint32_t formattag)
56 unsigned i;
57 for (i = 0; i < sizeof(pcm_codecs)/sizeof(pcm_codecs[0]); i++)
58 if (pcm_codecs[i].format_tag == formattag)
59 return pcm_codecs[i].get_codec();
61 return NULL;
64 enum codec_status codec_main(void)
66 int status;
67 struct pcm_format format;
68 uint32_t bytesdone, decodedsamples;
69 uint32_t num_sample_frames = 0;
70 size_t n;
71 int bufcount;
72 int endofstream;
73 unsigned char *buf;
74 uint8_t *aifbuf;
75 uint32_t offset2snd = 0;
76 off_t firstblockposn; /* position of the first block in file */
77 bool is_aifc = false;
78 const struct pcm_codec *codec;
79 uint32_t size;
81 /* Generic codec initialisation */
82 ci->configure(DSP_SET_SAMPLE_DEPTH, PCM_OUTPUT_DEPTH-1);
84 next_track:
85 status = CODEC_OK;
87 if (codec_init()) {
88 status = CODEC_ERROR;
89 goto exit;
92 if (codec_wait_taginfo() != 0)
93 goto done;
95 codec_set_replaygain(ci->id3);
97 /* Need to save offset for later use (cleared indirectly by advance_buffer) */
98 bytesdone = ci->id3->offset;
100 /* assume the AIFF header is less than 1024 bytes */
101 buf = ci->request_buffer(&n, 1024);
102 if (n < 54) {
103 status = CODEC_ERROR;
104 goto done;
107 if (memcmp(buf, "FORM", 4) != 0)
109 DEBUGF("CODEC_ERROR: does not aiff format %4.4s\n", (char*)&buf[0]);
110 status = CODEC_ERROR;
111 goto done;
113 if (memcmp(&buf[8], "AIFF", 4) == 0)
114 is_aifc = false;
115 else if (memcmp(&buf[8], "AIFC", 4) == 0)
116 is_aifc = true;
117 else
119 DEBUGF("CODEC_ERROR: does not aiff format %4.4s\n", (char*)&buf[8]);
120 status = CODEC_ERROR;
121 goto done;
124 buf += 12;
125 n -= 12;
127 ci->memset(&format, 0, sizeof(struct pcm_format));
128 format.is_signed = true;
129 format.is_little_endian = false;
131 decodedsamples = 0;
132 codec = 0;
134 /* read until 'SSND' chunk, which typically is last */
135 while (format.numbytes == 0 && n >= 8)
137 /* chunkSize */
138 size = ((buf[4]<<24)|(buf[5]<<16)|(buf[6]<<8)|buf[7]);
139 if (memcmp(buf, "COMM", 4) == 0) {
140 if ((!is_aifc && size < 18) || (is_aifc && size < 22))
142 DEBUGF("CODEC_ERROR: 'COMM' chunk size=%lu < %d\n",
143 (unsigned long)size, (is_aifc)?22:18);
144 status = CODEC_ERROR;
145 goto done;
147 /* num_channels */
148 format.channels = ((buf[8]<<8)|buf[9]);
149 /* num_sample_frames */
150 num_sample_frames = ((buf[10]<<24)|(buf[11]<<16)|(buf[12]<<8)
151 |buf[13]);
152 /* sample_size */
153 format.bitspersample = ((buf[14]<<8)|buf[15]);
154 /* sample_rate (don't use last 4 bytes, only integer fs) */
155 if (buf[16] != 0x40) {
156 DEBUGF("CODEC_ERROR: weird sampling rate (no @)\n");
157 status = CODEC_ERROR;
158 goto done;
160 format.samplespersec = ((buf[18]<<24)|(buf[19]<<16)|(buf[20]<<8)|buf[21])+1;
161 format.samplespersec >>= (16 + 14 - buf[17]);
162 /* compressionType (AIFC only) */
163 if (is_aifc)
165 format.formattag = (buf[26]<<24)|(buf[27]<<16)|(buf[28]<<8)|buf[29];
168 * aiff's sample_size is uncompressed sound data size.
169 * But format.bitspersample is compressed sound data size.
171 if (format.formattag == AIFC_FORMAT_ALAW ||
172 format.formattag == AIFC_FORMAT_MULAW)
173 format.bitspersample = 8;
174 else if (format.formattag == AIFC_FORMAT_QT_IMA_ADPCM)
175 format.bitspersample = 4;
177 else
178 format.formattag = AIFC_FORMAT_PCM;
179 /* calc average bytes per second */
180 format.avgbytespersec = format.samplespersec*format.channels*format.bitspersample/8;
181 } else if (memcmp(buf, "SSND", 4)==0) {
182 if (format.bitspersample == 0) {
183 DEBUGF("CODEC_ERROR: unsupported chunk order\n");
184 status = CODEC_ERROR;
185 goto done;
187 /* offset2snd */
188 offset2snd = (buf[8]<<24)|(buf[9]<<16)|(buf[10]<<8)|buf[11];
189 /* block_size */
190 format.blockalign = ((buf[12]<<24)|(buf[13]<<16)|(buf[14]<<8)|buf[15]) >> 3;
191 if (format.blockalign == 0)
192 format.blockalign = format.channels * format.bitspersample >> 3;
193 format.numbytes = size - 8 - offset2snd;
194 size = 8 + offset2snd; /* advance to the beginning of data */
195 } else if (is_aifc && (memcmp(buf, "FVER", 4)==0)) {
196 /* Format Version Chunk (AIFC only chunk) */
197 /* skip this chunk */
198 } else {
199 DEBUGF("unsupported AIFF chunk: '%c%c%c%c', size=%lu\n",
200 buf[0], buf[1], buf[2], buf[3], (unsigned long)size);
203 size += 8 + (size & 0x01); /* odd chunk sizes must be padded */
205 buf += size;
206 if (n < size) {
207 DEBUGF("CODEC_ERROR: AIFF header size > 1024\n");
208 status = CODEC_ERROR;
209 goto done;
211 n -= size;
212 } /* while 'SSND' */
214 if (format.channels == 0) {
215 DEBUGF("CODEC_ERROR: 'COMM' chunk not found or 0-channels file\n");
216 status = CODEC_ERROR;
217 goto done;
219 if (format.numbytes == 0) {
220 DEBUGF("CODEC_ERROR: 'SSND' chunk not found or has zero length\n");
221 status = CODEC_ERROR;
222 goto done;
225 codec = get_codec(format.formattag);
226 if (codec == 0)
228 DEBUGF("CODEC_ERROR: AIFC does not support compressionType: 0x%x\n",
229 (unsigned int)format.formattag);
230 status = CODEC_ERROR;
231 goto done;
234 if (!codec->set_format(&format))
236 status = CODEC_ERROR;
237 goto done;
240 ci->configure(DSP_SWITCH_FREQUENCY, ci->id3->frequency);
242 if (format.channels == 2) {
243 ci->configure(DSP_SET_STEREO_MODE, STEREO_INTERLEAVED);
244 } else if (format.channels == 1) {
245 ci->configure(DSP_SET_STEREO_MODE, STEREO_MONO);
246 } else {
247 DEBUGF("CODEC_ERROR: more than 2 channels unsupported\n");
248 status = CODEC_ERROR;
249 goto done;
252 if (format.samplesperblock == 0)
254 DEBUGF("CODEC_ERROR: samplesperblock is 0\n");
255 status = CODEC_ERROR;
256 goto done;
258 if (format.blockalign == 0)
260 DEBUGF("CODEC_ERROR: blockalign is 0\n");
261 status = CODEC_ERROR;
262 goto done;
265 /* check chunksize */
266 if ((format.chunksize / format.blockalign) * format.samplesperblock * format.channels
267 > PCM_SAMPLE_SIZE)
268 format.chunksize = (PCM_SAMPLE_SIZE / format.blockalign) * format.blockalign;
269 if (format.chunksize == 0)
271 DEBUGF("CODEC_ERROR: chunksize is 0\n");
272 status = CODEC_ERROR;
273 goto done;
276 firstblockposn = 1024 - n;
277 ci->advance_buffer(firstblockposn);
279 /* make sure we're at the correct offset */
280 if (bytesdone > (uint32_t) firstblockposn) {
281 /* Round down to previous block */
282 struct pcm_pos *newpos = codec->get_seek_pos(bytesdone - firstblockposn,
283 PCM_SEEK_POS, NULL);
285 if (newpos->pos > format.numbytes)
286 goto done;
287 if (ci->seek_buffer(firstblockposn + newpos->pos))
289 bytesdone = newpos->pos;
290 decodedsamples = newpos->samples;
292 ci->seek_complete();
293 } else {
294 /* already where we need to be */
295 bytesdone = 0;
298 /* The main decoder loop */
299 endofstream = 0;
301 while (!endofstream) {
302 ci->yield();
303 if (ci->stop_codec || ci->new_track)
304 break;
306 if (ci->seek_time) {
307 /* 3rd args(read_buffer) is unnecessary in the format which AIFF supports. */
308 struct pcm_pos *newpos = codec->get_seek_pos(ci->seek_time, PCM_SEEK_TIME, NULL);
310 if (newpos->pos > format.numbytes)
311 break;
312 if (ci->seek_buffer(firstblockposn + newpos->pos))
314 bytesdone = newpos->pos;
315 decodedsamples = newpos->samples;
317 ci->seek_complete();
319 aifbuf = (uint8_t *)ci->request_buffer(&n, format.chunksize);
321 if (n == 0)
322 break; /* End of stream */
324 if (bytesdone + n > format.numbytes) {
325 n = format.numbytes - bytesdone;
326 endofstream = 1;
329 status = codec->decode(aifbuf, n, samples, &bufcount);
330 if (status == CODEC_ERROR)
332 DEBUGF("codec error\n");
333 goto done;
336 ci->pcmbuf_insert(samples, NULL, bufcount);
338 ci->advance_buffer(n);
339 bytesdone += n;
340 decodedsamples += bufcount;
341 if (bytesdone >= format.numbytes)
342 endofstream = 1;
344 ci->set_elapsed(decodedsamples*1000LL/ci->id3->frequency);
346 status = CODEC_OK;
348 done:
349 if (ci->request_next_track())
350 goto next_track;
352 exit:
353 return status;