commit FS#10424 and FS#10425
[kugel-rb.git] / apps / codecs / aiff.c
blob4b870386c17820ae54d52c0224d9c45bfe64e8b4
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 <inttypes.h>
25 #include "codecs/libpcm/support_formats.h"
27 CODEC_HEADER
29 #define FOURCC(c1, c2, c3, c4) \
30 ((((uint32_t)c1)<<24)|(((uint32_t)c2)<<16)|(((uint32_t)c3)<<8)|((uint32_t)c4))
32 /* This codec supports the following AIFC compressionType formats */
33 enum {
34 AIFC_FORMAT_PCM = FOURCC('N', 'O', 'N', 'E'), /* AIFC PCM Format (big endian) */
35 AIFC_FORMAT_ALAW = FOURCC('a', 'l', 'a', 'w'), /* AIFC ALaw compressed */
36 AIFC_FORMAT_MULAW = FOURCC('u', 'l', 'a', 'w'), /* AIFC uLaw compressed */
37 AIFC_FORMAT_IEEE_FLOAT32 = FOURCC('f', 'l', '3', '2'), /* AIFC IEEE float 32 bit */
38 AIFC_FORMAT_IEEE_FLOAT64 = FOURCC('f', 'l', '6', '4'), /* AIFC IEEE float 64 bit */
39 AIFC_FORMAT_QT_IMA_ADPCM = FOURCC('i', 'm', 'a', '4'), /* AIFC QuickTime IMA ADPCM */
42 static const struct pcm_entry pcm_codecs[] = {
43 { AIFC_FORMAT_PCM, get_linear_pcm_codec },
44 { AIFC_FORMAT_ALAW, get_itut_g711_alaw_codec },
45 { AIFC_FORMAT_MULAW, get_itut_g711_mulaw_codec },
46 { AIFC_FORMAT_IEEE_FLOAT32, get_ieee_float_codec },
47 { AIFC_FORMAT_IEEE_FLOAT64, get_ieee_float_codec },
48 { AIFC_FORMAT_QT_IMA_ADPCM, get_qt_ima_adpcm_codec },
51 #define NUM_FORMATS 6
53 static int32_t samples[PCM_CHUNK_SIZE] IBSS_ATTR;
55 static const struct pcm_codec *get_codec(uint32_t formattag)
57 int i;
59 for (i = 0; i < NUM_FORMATS; i++)
61 if (pcm_codecs[i].format_tag == formattag)
63 if (pcm_codecs[i].get_codec)
64 return pcm_codecs[i].get_codec();
65 return 0;
68 return 0;
71 enum codec_status codec_main(void)
73 int status = CODEC_OK;
74 struct pcm_format format;
75 uint32_t bytesdone, decodedsamples;
76 uint32_t num_sample_frames = 0;
77 uint32_t i = CODEC_OK;
78 size_t n;
79 int bufcount;
80 int endofstream;
81 unsigned char *buf;
82 uint8_t *aifbuf;
83 uint32_t offset2snd = 0;
84 off_t firstblockposn; /* position of the first block in file */
85 bool is_aifc = false;
86 const struct pcm_codec *codec;
88 /* Generic codec initialisation */
89 ci->configure(DSP_SET_SAMPLE_DEPTH, 28);
91 next_track:
92 if (codec_init()) {
93 i = CODEC_ERROR;
94 goto exit;
97 while (!*ci->taginfo_ready && !ci->stop_codec)
98 ci->sleep(1);
100 codec_set_replaygain(ci->id3);
102 /* assume the AIFF header is less than 1024 bytes */
103 buf = ci->request_buffer(&n, 1024);
104 if (n < 54) {
105 i = CODEC_ERROR;
106 goto done;
109 if (memcmp(buf, "FORM", 4) != 0)
111 DEBUGF("CODEC_ERROR: does not aiff format %c%c%c%c\n", buf[0], buf[1], buf[2], buf[3]);
112 i = CODEC_ERROR;
113 goto done;
115 if (memcmp(&buf[8], "AIFF", 4) == 0)
116 is_aifc = false;
117 else if (memcmp(&buf[8], "AIFC", 4) == 0)
118 is_aifc = true;
119 else
121 DEBUGF("CODEC_ERROR: does not aiff format %c%c%c%c\n", buf[8], buf[9], buf[10], buf[11]);
122 i = CODEC_ERROR;
123 goto done;
126 buf += 12;
127 n -= 12;
129 ci->memset(&format, 0, sizeof(struct pcm_format));
130 format.is_signed = true;
131 format.is_little_endian = false;
133 decodedsamples = 0;
134 codec = 0;
136 /* read until 'SSND' chunk, which typically is last */
137 while (format.numbytes == 0 && n >= 8)
139 /* chunkSize */
140 i = ((buf[4]<<24)|(buf[5]<<16)|(buf[6]<<8)|buf[7]);
141 if (memcmp(buf, "COMM", 4) == 0) {
142 if ((!is_aifc && i < 18) || (is_aifc && i < 22))
144 DEBUGF("CODEC_ERROR: 'COMM' chunk size=%lu < %d\n",
145 (unsigned long)i, (is_aifc)?22:18);
146 i = CODEC_ERROR;
147 goto done;
149 /* num_channels */
150 format.channels = ((buf[8]<<8)|buf[9]);
151 /* num_sample_frames */
152 num_sample_frames = ((buf[10]<<24)|(buf[11]<<16)|(buf[12]<<8)
153 |buf[13]);
154 /* sample_size */
155 format.bitspersample = ((buf[14]<<8)|buf[15]);
156 /* sample_rate (don't use last 4 bytes, only integer fs) */
157 if (buf[16] != 0x40) {
158 DEBUGF("CODEC_ERROR: weird sampling rate (no @)\n");
159 i = CODEC_ERROR;
160 goto done;
162 format.samplespersec = ((buf[18]<<24)|(buf[19]<<16)|(buf[20]<<8)|buf[21])+1;
163 format.samplespersec >>= (16 + 14 - buf[17]);
164 /* compressionType (AIFC only) */
165 if (is_aifc)
167 format.formattag = (buf[26]<<24)|(buf[27]<<16)|(buf[28]<<8)|buf[29];
170 * aiff's sample_size is uncompressed sound data size.
171 * But format.bitspersample is compressed sound data size.
173 if (format.formattag == AIFC_FORMAT_ALAW ||
174 format.formattag == AIFC_FORMAT_MULAW)
175 format.bitspersample = 8;
176 else if (format.formattag == AIFC_FORMAT_QT_IMA_ADPCM)
177 format.bitspersample = 4;
179 else
180 format.formattag = AIFC_FORMAT_PCM;
181 /* calc average bytes per second */
182 format.avgbytespersec = format.samplespersec*format.channels*format.bitspersample/8;
183 } else if (memcmp(buf, "SSND", 4)==0) {
184 if (format.bitspersample == 0) {
185 DEBUGF("CODEC_ERROR: unsupported chunk order\n");
186 i = CODEC_ERROR;
187 goto done;
189 /* offset2snd */
190 offset2snd = (buf[8]<<24)|(buf[9]<<16)|(buf[10]<<8)|buf[11];
191 /* block_size */
192 format.blockalign = ((buf[12]<<24)|(buf[13]<<16)|(buf[14]<<8)|buf[15]) >> 3;
193 if (format.blockalign == 0)
194 format.blockalign = format.channels * format.bitspersample >> 3;
195 format.numbytes = i - 8 - offset2snd;
196 i = 8 + offset2snd; /* advance to the beginning of data */
197 } else if (is_aifc && (memcmp(buf, "FVER", 4)==0)) {
198 /* Format Version Chunk (AIFC only chunk) */
199 /* skip this chunk */
200 } else {
201 DEBUGF("unsupported AIFF chunk: '%c%c%c%c', size=%lu\n",
202 buf[0], buf[1], buf[2], buf[3], (unsigned long)i);
205 if (i & 0x01) /* odd chunk sizes must be padded */
206 i++;
207 buf += i + 8;
208 if (n < (i + 8)) {
209 DEBUGF("CODEC_ERROR: AIFF header size > 1024\n");
210 i = CODEC_ERROR;
211 goto done;
213 n -= i + 8;
214 } /* while 'SSND' */
216 if (format.channels == 0) {
217 DEBUGF("CODEC_ERROR: 'COMM' chunk not found or 0-channels file\n");
218 i = CODEC_ERROR;
219 goto done;
221 if (format.numbytes == 0) {
222 DEBUGF("CODEC_ERROR: 'SSND' chunk not found or has zero length\n");
223 i = CODEC_ERROR;
224 goto done;
227 codec = get_codec(format.formattag);
228 if (codec == 0)
230 DEBUGF("CODEC_ERROR: AIFC does not support compressionType: 0x%x\n",
231 (unsigned int)format.formattag);
232 i = CODEC_ERROR;
233 goto done;
236 if (!codec->set_format(&format))
238 i = CODEC_ERROR;
239 goto done;
242 ci->configure(DSP_SWITCH_FREQUENCY, ci->id3->frequency);
244 if (format.channels == 2) {
245 ci->configure(DSP_SET_STEREO_MODE, STEREO_INTERLEAVED);
246 } else if (format.channels == 1) {
247 ci->configure(DSP_SET_STEREO_MODE, STEREO_MONO);
248 } else {
249 DEBUGF("CODEC_ERROR: more than 2 channels unsupported\n");
250 i = CODEC_ERROR;
251 goto done;
254 if (format.samplesperblock == 0)
256 DEBUGF("CODEC_ERROR: samplesperblock is 0\n");
257 i = CODEC_ERROR;
258 goto done;
260 if (format.blockalign == 0)
262 DEBUGF("CODEC_ERROR: blockalign is 0\n");
263 i = CODEC_ERROR;
264 goto done;
267 /* check chunksize */
268 if ((format.chunksize / format.blockalign) * format.samplesperblock * format.channels
269 > PCM_CHUNK_SIZE)
270 format.chunksize = (PCM_CHUNK_SIZE / format.blockalign) * format.blockalign;
271 if (format.chunksize == 0)
273 DEBUGF("CODEC_ERROR: chunksize is 0\n");
274 i = CODEC_ERROR;
275 goto done;
278 firstblockposn = 1024 - n;
279 ci->advance_buffer(firstblockposn);
281 /* The main decoder loop */
282 bytesdone = 0;
283 ci->set_elapsed(0);
284 endofstream = 0;
286 while (!endofstream) {
287 ci->yield();
288 if (ci->stop_codec || ci->new_track)
289 break;
291 if (ci->seek_time) {
292 /* 2nd args(read_buffer) is unnecessary in the format which AIFF supports. */
293 struct pcm_pos *newpos = codec->get_seek_pos(ci->seek_time, NULL);
295 decodedsamples = newpos->samples;
296 if (newpos->pos > format.numbytes)
297 break;
298 if (ci->seek_buffer(firstblockposn + newpos->pos))
299 bytesdone = newpos->pos;
300 ci->seek_complete();
302 aifbuf = (uint8_t *)ci->request_buffer(&n, format.chunksize);
304 if (n == 0)
305 break; /* End of stream */
307 if (bytesdone + n > format.numbytes) {
308 n = format.numbytes - bytesdone;
309 endofstream = 1;
312 status = codec->decode(aifbuf, n, samples, &bufcount);
313 if (status == CODEC_ERROR)
315 DEBUGF("codec error\n");
316 goto done;
319 ci->pcmbuf_insert(samples, NULL, bufcount);
321 ci->advance_buffer(n);
322 bytesdone += n;
323 decodedsamples += bufcount;
324 if (bytesdone >= format.numbytes)
325 endofstream = 1;
327 ci->set_elapsed(decodedsamples*1000LL/ci->id3->frequency);
329 i = CODEC_OK;
331 done:
332 if (ci->request_next_track())
333 goto next_track;
335 exit:
336 return i;