Changes to test_mem. Improve readability for smaller displays, increase loop count...
[maemo-rb.git] / apps / codecs / aiff.c
blob4a127c7e0eb3c218e3e2dbaa6dd59d88597696ce
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 = CODEC_OK;
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 if (codec_init()) {
86 status = CODEC_ERROR;
87 goto exit;
90 while (!*ci->taginfo_ready && !ci->stop_codec)
91 ci->sleep(1);
93 codec_set_replaygain(ci->id3);
95 /* Need to save offset for later use (cleared indirectly by advance_buffer) */
96 bytesdone = ci->id3->offset;
98 /* assume the AIFF header is less than 1024 bytes */
99 buf = ci->request_buffer(&n, 1024);
100 if (n < 54) {
101 status = CODEC_ERROR;
102 goto done;
105 if (memcmp(buf, "FORM", 4) != 0)
107 DEBUGF("CODEC_ERROR: does not aiff format %4.4s\n", (char*)&buf[0]);
108 status = CODEC_ERROR;
109 goto done;
111 if (memcmp(&buf[8], "AIFF", 4) == 0)
112 is_aifc = false;
113 else if (memcmp(&buf[8], "AIFC", 4) == 0)
114 is_aifc = true;
115 else
117 DEBUGF("CODEC_ERROR: does not aiff format %4.4s\n", (char*)&buf[8]);
118 status = CODEC_ERROR;
119 goto done;
122 buf += 12;
123 n -= 12;
125 ci->memset(&format, 0, sizeof(struct pcm_format));
126 format.is_signed = true;
127 format.is_little_endian = false;
129 decodedsamples = 0;
130 codec = 0;
132 /* read until 'SSND' chunk, which typically is last */
133 while (format.numbytes == 0 && n >= 8)
135 /* chunkSize */
136 size = ((buf[4]<<24)|(buf[5]<<16)|(buf[6]<<8)|buf[7]);
137 if (memcmp(buf, "COMM", 4) == 0) {
138 if ((!is_aifc && size < 18) || (is_aifc && size < 22))
140 DEBUGF("CODEC_ERROR: 'COMM' chunk size=%lu < %d\n",
141 (unsigned long)size, (is_aifc)?22:18);
142 status = CODEC_ERROR;
143 goto done;
145 /* num_channels */
146 format.channels = ((buf[8]<<8)|buf[9]);
147 /* num_sample_frames */
148 num_sample_frames = ((buf[10]<<24)|(buf[11]<<16)|(buf[12]<<8)
149 |buf[13]);
150 /* sample_size */
151 format.bitspersample = ((buf[14]<<8)|buf[15]);
152 /* sample_rate (don't use last 4 bytes, only integer fs) */
153 if (buf[16] != 0x40) {
154 DEBUGF("CODEC_ERROR: weird sampling rate (no @)\n");
155 status = CODEC_ERROR;
156 goto done;
158 format.samplespersec = ((buf[18]<<24)|(buf[19]<<16)|(buf[20]<<8)|buf[21])+1;
159 format.samplespersec >>= (16 + 14 - buf[17]);
160 /* compressionType (AIFC only) */
161 if (is_aifc)
163 format.formattag = (buf[26]<<24)|(buf[27]<<16)|(buf[28]<<8)|buf[29];
166 * aiff's sample_size is uncompressed sound data size.
167 * But format.bitspersample is compressed sound data size.
169 if (format.formattag == AIFC_FORMAT_ALAW ||
170 format.formattag == AIFC_FORMAT_MULAW)
171 format.bitspersample = 8;
172 else if (format.formattag == AIFC_FORMAT_QT_IMA_ADPCM)
173 format.bitspersample = 4;
175 else
176 format.formattag = AIFC_FORMAT_PCM;
177 /* calc average bytes per second */
178 format.avgbytespersec = format.samplespersec*format.channels*format.bitspersample/8;
179 } else if (memcmp(buf, "SSND", 4)==0) {
180 if (format.bitspersample == 0) {
181 DEBUGF("CODEC_ERROR: unsupported chunk order\n");
182 status = CODEC_ERROR;
183 goto done;
185 /* offset2snd */
186 offset2snd = (buf[8]<<24)|(buf[9]<<16)|(buf[10]<<8)|buf[11];
187 /* block_size */
188 format.blockalign = ((buf[12]<<24)|(buf[13]<<16)|(buf[14]<<8)|buf[15]) >> 3;
189 if (format.blockalign == 0)
190 format.blockalign = format.channels * format.bitspersample >> 3;
191 format.numbytes = size - 8 - offset2snd;
192 size = 8 + offset2snd; /* advance to the beginning of data */
193 } else if (is_aifc && (memcmp(buf, "FVER", 4)==0)) {
194 /* Format Version Chunk (AIFC only chunk) */
195 /* skip this chunk */
196 } else {
197 DEBUGF("unsupported AIFF chunk: '%c%c%c%c', size=%lu\n",
198 buf[0], buf[1], buf[2], buf[3], (unsigned long)size);
201 size += 8 + (size & 0x01); /* odd chunk sizes must be padded */
203 buf += size;
204 if (n < size) {
205 DEBUGF("CODEC_ERROR: AIFF header size > 1024\n");
206 status = CODEC_ERROR;
207 goto done;
209 n -= size;
210 } /* while 'SSND' */
212 if (format.channels == 0) {
213 DEBUGF("CODEC_ERROR: 'COMM' chunk not found or 0-channels file\n");
214 status = CODEC_ERROR;
215 goto done;
217 if (format.numbytes == 0) {
218 DEBUGF("CODEC_ERROR: 'SSND' chunk not found or has zero length\n");
219 status = CODEC_ERROR;
220 goto done;
223 codec = get_codec(format.formattag);
224 if (codec == 0)
226 DEBUGF("CODEC_ERROR: AIFC does not support compressionType: 0x%x\n",
227 (unsigned int)format.formattag);
228 status = CODEC_ERROR;
229 goto done;
232 if (!codec->set_format(&format))
234 status = CODEC_ERROR;
235 goto done;
238 ci->configure(DSP_SWITCH_FREQUENCY, ci->id3->frequency);
240 if (format.channels == 2) {
241 ci->configure(DSP_SET_STEREO_MODE, STEREO_INTERLEAVED);
242 } else if (format.channels == 1) {
243 ci->configure(DSP_SET_STEREO_MODE, STEREO_MONO);
244 } else {
245 DEBUGF("CODEC_ERROR: more than 2 channels unsupported\n");
246 status = CODEC_ERROR;
247 goto done;
250 if (format.samplesperblock == 0)
252 DEBUGF("CODEC_ERROR: samplesperblock is 0\n");
253 status = CODEC_ERROR;
254 goto done;
256 if (format.blockalign == 0)
258 DEBUGF("CODEC_ERROR: blockalign is 0\n");
259 status = CODEC_ERROR;
260 goto done;
263 /* check chunksize */
264 if ((format.chunksize / format.blockalign) * format.samplesperblock * format.channels
265 > PCM_SAMPLE_SIZE)
266 format.chunksize = (PCM_SAMPLE_SIZE / format.blockalign) * format.blockalign;
267 if (format.chunksize == 0)
269 DEBUGF("CODEC_ERROR: chunksize is 0\n");
270 status = CODEC_ERROR;
271 goto done;
274 firstblockposn = 1024 - n;
275 ci->advance_buffer(firstblockposn);
277 /* make sure we're at the correct offset */
278 if (bytesdone > (uint32_t) firstblockposn) {
279 /* Round down to previous block */
280 struct pcm_pos *newpos = codec->get_seek_pos(bytesdone - firstblockposn,
281 PCM_SEEK_POS, NULL);
283 if (newpos->pos > format.numbytes)
284 goto done;
285 if (ci->seek_buffer(firstblockposn + newpos->pos))
287 bytesdone = newpos->pos;
288 decodedsamples = newpos->samples;
290 ci->seek_complete();
291 } else {
292 /* already where we need to be */
293 bytesdone = 0;
296 /* The main decoder loop */
297 endofstream = 0;
299 while (!endofstream) {
300 ci->yield();
301 if (ci->stop_codec || ci->new_track)
302 break;
304 if (ci->seek_time) {
305 /* 3rd args(read_buffer) is unnecessary in the format which AIFF supports. */
306 struct pcm_pos *newpos = codec->get_seek_pos(ci->seek_time, PCM_SEEK_TIME, NULL);
308 if (newpos->pos > format.numbytes)
309 break;
310 if (ci->seek_buffer(firstblockposn + newpos->pos))
312 bytesdone = newpos->pos;
313 decodedsamples = newpos->samples;
315 ci->seek_complete();
317 aifbuf = (uint8_t *)ci->request_buffer(&n, format.chunksize);
319 if (n == 0)
320 break; /* End of stream */
322 if (bytesdone + n > format.numbytes) {
323 n = format.numbytes - bytesdone;
324 endofstream = 1;
327 status = codec->decode(aifbuf, n, samples, &bufcount);
328 if (status == CODEC_ERROR)
330 DEBUGF("codec error\n");
331 goto done;
334 ci->pcmbuf_insert(samples, NULL, bufcount);
336 ci->advance_buffer(n);
337 bytesdone += n;
338 decodedsamples += bufcount;
339 if (bytesdone >= format.numbytes)
340 endofstream = 1;
342 ci->set_elapsed(decodedsamples*1000LL/ci->id3->frequency);
344 status = CODEC_OK;
346 done:
347 if (ci->request_next_track())
348 goto next_track;
350 exit:
351 return status;