Wrote apps/codecs/wmapro.c and modified libwmapro to make the codec work in the sim...
[kugel-rb.git] / apps / codecs / aiff.c
blob4e16788e0624169fd361df3ec1fde7ca37e3abb8
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 NUM_FORMATS 6
52 #define PCM_SAMPLE_SIZE (1024*2)
54 static int32_t samples[PCM_SAMPLE_SIZE] IBSS_ATTR;
56 static const struct pcm_codec *get_codec(uint32_t formattag)
58 int i;
60 for (i = 0; i < NUM_FORMATS; i++)
62 if (pcm_codecs[i].format_tag == formattag)
64 if (pcm_codecs[i].get_codec)
65 return pcm_codecs[i].get_codec();
66 return 0;
69 return 0;
72 enum codec_status codec_main(void)
74 int status = CODEC_OK;
75 struct pcm_format format;
76 uint32_t bytesdone, decodedsamples;
77 uint32_t num_sample_frames = 0;
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;
87 uint32_t size;
89 /* Generic codec initialisation */
90 ci->configure(DSP_SET_SAMPLE_DEPTH, PCM_OUTPUT_DEPTH-1);
92 next_track:
93 if (codec_init()) {
94 status = CODEC_ERROR;
95 goto exit;
98 while (!*ci->taginfo_ready && !ci->stop_codec)
99 ci->sleep(1);
101 codec_set_replaygain(ci->id3);
103 /* Need to save offset for later use (cleared indirectly by advance_buffer) */
104 bytesdone = ci->id3->offset;
106 /* assume the AIFF header is less than 1024 bytes */
107 buf = ci->request_buffer(&n, 1024);
108 if (n < 54) {
109 status = CODEC_ERROR;
110 goto done;
113 if (memcmp(buf, "FORM", 4) != 0)
115 DEBUGF("CODEC_ERROR: does not aiff format %c%c%c%c\n", buf[0], buf[1], buf[2], buf[3]);
116 status = CODEC_ERROR;
117 goto done;
119 if (memcmp(&buf[8], "AIFF", 4) == 0)
120 is_aifc = false;
121 else if (memcmp(&buf[8], "AIFC", 4) == 0)
122 is_aifc = true;
123 else
125 DEBUGF("CODEC_ERROR: does not aiff format %c%c%c%c\n", buf[8], buf[9], buf[10], buf[11]);
126 status = CODEC_ERROR;
127 goto done;
130 buf += 12;
131 n -= 12;
133 ci->memset(&format, 0, sizeof(struct pcm_format));
134 format.is_signed = true;
135 format.is_little_endian = false;
137 decodedsamples = 0;
138 codec = 0;
140 /* read until 'SSND' chunk, which typically is last */
141 while (format.numbytes == 0 && n >= 8)
143 /* chunkSize */
144 size = ((buf[4]<<24)|(buf[5]<<16)|(buf[6]<<8)|buf[7]);
145 if (memcmp(buf, "COMM", 4) == 0) {
146 if ((!is_aifc && size < 18) || (is_aifc && size < 22))
148 DEBUGF("CODEC_ERROR: 'COMM' chunk size=%lu < %d\n",
149 (unsigned long)size, (is_aifc)?22:18);
150 status = CODEC_ERROR;
151 goto done;
153 /* num_channels */
154 format.channels = ((buf[8]<<8)|buf[9]);
155 /* num_sample_frames */
156 num_sample_frames = ((buf[10]<<24)|(buf[11]<<16)|(buf[12]<<8)
157 |buf[13]);
158 /* sample_size */
159 format.bitspersample = ((buf[14]<<8)|buf[15]);
160 /* sample_rate (don't use last 4 bytes, only integer fs) */
161 if (buf[16] != 0x40) {
162 DEBUGF("CODEC_ERROR: weird sampling rate (no @)\n");
163 status = CODEC_ERROR;
164 goto done;
166 format.samplespersec = ((buf[18]<<24)|(buf[19]<<16)|(buf[20]<<8)|buf[21])+1;
167 format.samplespersec >>= (16 + 14 - buf[17]);
168 /* compressionType (AIFC only) */
169 if (is_aifc)
171 format.formattag = (buf[26]<<24)|(buf[27]<<16)|(buf[28]<<8)|buf[29];
174 * aiff's sample_size is uncompressed sound data size.
175 * But format.bitspersample is compressed sound data size.
177 if (format.formattag == AIFC_FORMAT_ALAW ||
178 format.formattag == AIFC_FORMAT_MULAW)
179 format.bitspersample = 8;
180 else if (format.formattag == AIFC_FORMAT_QT_IMA_ADPCM)
181 format.bitspersample = 4;
183 else
184 format.formattag = AIFC_FORMAT_PCM;
185 /* calc average bytes per second */
186 format.avgbytespersec = format.samplespersec*format.channels*format.bitspersample/8;
187 } else if (memcmp(buf, "SSND", 4)==0) {
188 if (format.bitspersample == 0) {
189 DEBUGF("CODEC_ERROR: unsupported chunk order\n");
190 status = CODEC_ERROR;
191 goto done;
193 /* offset2snd */
194 offset2snd = (buf[8]<<24)|(buf[9]<<16)|(buf[10]<<8)|buf[11];
195 /* block_size */
196 format.blockalign = ((buf[12]<<24)|(buf[13]<<16)|(buf[14]<<8)|buf[15]) >> 3;
197 if (format.blockalign == 0)
198 format.blockalign = format.channels * format.bitspersample >> 3;
199 format.numbytes = size - 8 - offset2snd;
200 size = 8 + offset2snd; /* advance to the beginning of data */
201 } else if (is_aifc && (memcmp(buf, "FVER", 4)==0)) {
202 /* Format Version Chunk (AIFC only chunk) */
203 /* skip this chunk */
204 } else {
205 DEBUGF("unsupported AIFF chunk: '%c%c%c%c', size=%lu\n",
206 buf[0], buf[1], buf[2], buf[3], (unsigned long)size);
209 size += 8 + (size & 0x01); /* odd chunk sizes must be padded */
211 buf += size;
212 if (n < size) {
213 DEBUGF("CODEC_ERROR: AIFF header size > 1024\n");
214 status = CODEC_ERROR;
215 goto done;
217 n -= size;
218 } /* while 'SSND' */
220 if (format.channels == 0) {
221 DEBUGF("CODEC_ERROR: 'COMM' chunk not found or 0-channels file\n");
222 status = CODEC_ERROR;
223 goto done;
225 if (format.numbytes == 0) {
226 DEBUGF("CODEC_ERROR: 'SSND' chunk not found or has zero length\n");
227 status = CODEC_ERROR;
228 goto done;
231 codec = get_codec(format.formattag);
232 if (codec == 0)
234 DEBUGF("CODEC_ERROR: AIFC does not support compressionType: 0x%x\n",
235 (unsigned int)format.formattag);
236 status = CODEC_ERROR;
237 goto done;
240 if (!codec->set_format(&format))
242 status = CODEC_ERROR;
243 goto done;
246 ci->configure(DSP_SWITCH_FREQUENCY, ci->id3->frequency);
248 if (format.channels == 2) {
249 ci->configure(DSP_SET_STEREO_MODE, STEREO_INTERLEAVED);
250 } else if (format.channels == 1) {
251 ci->configure(DSP_SET_STEREO_MODE, STEREO_MONO);
252 } else {
253 DEBUGF("CODEC_ERROR: more than 2 channels unsupported\n");
254 status = CODEC_ERROR;
255 goto done;
258 if (format.samplesperblock == 0)
260 DEBUGF("CODEC_ERROR: samplesperblock is 0\n");
261 status = CODEC_ERROR;
262 goto done;
264 if (format.blockalign == 0)
266 DEBUGF("CODEC_ERROR: blockalign is 0\n");
267 status = CODEC_ERROR;
268 goto done;
271 /* check chunksize */
272 if ((format.chunksize / format.blockalign) * format.samplesperblock * format.channels
273 > PCM_SAMPLE_SIZE)
274 format.chunksize = (PCM_SAMPLE_SIZE / format.blockalign) * format.blockalign;
275 if (format.chunksize == 0)
277 DEBUGF("CODEC_ERROR: chunksize is 0\n");
278 status = CODEC_ERROR;
279 goto done;
282 firstblockposn = 1024 - n;
283 ci->advance_buffer(firstblockposn);
285 /* make sure we're at the correct offset */
286 if (bytesdone > (uint32_t) firstblockposn) {
287 /* Round down to previous block */
288 struct pcm_pos *newpos = codec->get_seek_pos(bytesdone - firstblockposn,
289 PCM_SEEK_POS, NULL);
291 if (newpos->pos > format.numbytes)
292 goto done;
293 if (ci->seek_buffer(firstblockposn + newpos->pos))
295 bytesdone = newpos->pos;
296 decodedsamples = newpos->samples;
298 ci->seek_complete();
299 } else {
300 /* already where we need to be */
301 bytesdone = 0;
304 /* The main decoder loop */
305 endofstream = 0;
307 while (!endofstream) {
308 ci->yield();
309 if (ci->stop_codec || ci->new_track)
310 break;
312 if (ci->seek_time) {
313 /* 3rd args(read_buffer) is unnecessary in the format which AIFF supports. */
314 struct pcm_pos *newpos = codec->get_seek_pos(ci->seek_time, PCM_SEEK_TIME, NULL);
316 if (newpos->pos > format.numbytes)
317 break;
318 if (ci->seek_buffer(firstblockposn + newpos->pos))
320 bytesdone = newpos->pos;
321 decodedsamples = newpos->samples;
323 ci->seek_complete();
325 aifbuf = (uint8_t *)ci->request_buffer(&n, format.chunksize);
327 if (n == 0)
328 break; /* End of stream */
330 if (bytesdone + n > format.numbytes) {
331 n = format.numbytes - bytesdone;
332 endofstream = 1;
335 status = codec->decode(aifbuf, n, samples, &bufcount);
336 if (status == CODEC_ERROR)
338 DEBUGF("codec error\n");
339 goto done;
342 ci->pcmbuf_insert(samples, NULL, bufcount);
344 ci->advance_buffer(n);
345 bytesdone += n;
346 decodedsamples += bufcount;
347 if (bytesdone >= format.numbytes)
348 endofstream = 1;
350 ci->set_elapsed(decodedsamples*1000LL/ci->id3->frequency);
352 status = CODEC_OK;
354 done:
355 if (ci->request_next_track())
356 goto next_track;
358 exit:
359 return status;