Fix warnings on 32 bit sim.
[kugel-rb.git] / apps / codecs / aiff.c
blob497e0c7c3aed7adc406cb46c681c0ef7e6eebaa2
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 */
39 static const struct pcm_entry pcm_codecs[] = {
40 { AIFC_FORMAT_PCM, get_linear_pcm_codec },
41 { AIFC_FORMAT_ALAW, get_itut_g711_alaw_codec },
42 { AIFC_FORMAT_MULAW, get_itut_g711_mulaw_codec },
45 #define NUM_FORMATS 3
47 static int32_t samples[PCM_CHUNK_SIZE] IBSS_ATTR;
49 static const struct pcm_codec *get_codec(uint32_t formattag)
51 int i;
53 for (i = 0; i < NUM_FORMATS; i++)
55 if (pcm_codecs[i].format_tag == formattag)
57 if (pcm_codecs[i].get_codec)
58 return pcm_codecs[i].get_codec();
59 return 0;
62 return 0;
65 enum codec_status codec_main(void)
67 int status = CODEC_OK;
68 struct pcm_format format;
69 uint32_t bytesdone, decodedbytes;
70 uint32_t num_sample_frames = 0;
71 uint32_t i = CODEC_OK;
72 size_t n;
73 int bufcount;
74 int endofstream;
75 unsigned char *buf;
76 uint8_t *aifbuf;
77 uint32_t offset2snd = 0;
78 off_t firstblockposn; /* position of the first block in file */
79 bool is_aifc = false;
80 const struct pcm_codec *codec;
82 /* Generic codec initialisation */
83 ci->configure(DSP_SET_SAMPLE_DEPTH, 28);
85 next_track:
86 if (codec_init()) {
87 i = CODEC_ERROR;
88 goto exit;
91 while (!*ci->taginfo_ready && !ci->stop_codec)
92 ci->sleep(1);
94 codec_set_replaygain(ci->id3);
96 /* assume the AIFF header is less than 1024 bytes */
97 buf = ci->request_buffer(&n, 1024);
98 if (n < 54) {
99 i = CODEC_ERROR;
100 goto done;
103 if (memcmp(buf, "FORM", 4) != 0)
105 DEBUGF("CODEC_ERROR: does not aiff format %c%c%c%c\n", buf[0], buf[1], buf[2], buf[3]);
106 i = CODEC_ERROR;
107 goto done;
109 if (memcmp(&buf[8], "AIFF", 4) == 0)
110 is_aifc = false;
111 else if (memcmp(&buf[8], "AIFC", 4) == 0)
112 is_aifc = true;
113 else
115 DEBUGF("CODEC_ERROR: does not aiff format %c%c%c%c\n", buf[8], buf[9], buf[10], buf[11]);
116 i = CODEC_ERROR;
117 goto done;
120 buf += 12;
121 n -= 12;
123 ci->memset(&format, 0, sizeof(struct pcm_format));
124 format.is_signed = true;
125 format.is_little_endian = false;
127 decodedbytes = 0;
128 codec = 0;
130 /* read until 'SSND' chunk, which typically is last */
131 while (format.numbytes == 0 && n >= 8)
133 /* chunkSize */
134 i = ((buf[4]<<24)|(buf[5]<<16)|(buf[6]<<8)|buf[7]);
135 if (memcmp(buf, "COMM", 4) == 0) {
136 if ((!is_aifc && i < 18) || (is_aifc && i < 22))
138 DEBUGF("CODEC_ERROR: 'COMM' chunk size=%lu < %d\n",
139 (unsigned long)i, (is_aifc)?22:18);
140 i = CODEC_ERROR;
141 goto done;
143 /* num_channels */
144 format.channels = ((buf[8]<<8)|buf[9]);
145 /* num_sample_frames */
146 num_sample_frames = ((buf[10]<<24)|(buf[11]<<16)|(buf[12]<<8)
147 |buf[13]);
148 /* sample_size */
149 format.bitspersample = ((buf[14]<<8)|buf[15]);
150 /* sample_rate (don't use last 4 bytes, only integer fs) */
151 if (buf[16] != 0x40) {
152 DEBUGF("CODEC_ERROR: weird sampling rate (no @)\n");
153 i = CODEC_ERROR;
154 goto done;
156 format.samplespersec = ((buf[18]<<24)|(buf[19]<<16)|(buf[20]<<8)|buf[21])+1;
157 format.samplespersec >>= (16 + 14 - buf[17]);
158 /* compressionType (AIFC only) */
159 if (is_aifc)
161 format.formattag = (buf[26]<<24)|(buf[27]<<16)|(buf[28]<<8)|buf[29];
164 * aiff's sample_size is uncompressed sound data size.
165 * But format.bitspersample is compressed sound data size.
167 if (format.formattag == AIFC_FORMAT_ALAW || format.formattag == AIFC_FORMAT_MULAW)
168 format.bitspersample = 8;
170 else
171 format.formattag = AIFC_FORMAT_PCM;
172 /* calc average bytes per second */
173 format.avgbytespersec = format.samplespersec*format.channels*format.bitspersample/8;
174 } else if (memcmp(buf, "SSND", 4)==0) {
175 if (format.bitspersample == 0) {
176 DEBUGF("CODEC_ERROR: unsupported chunk order\n");
177 i = CODEC_ERROR;
178 goto done;
180 /* offset2snd */
181 offset2snd = (buf[8]<<24)|(buf[9]<<16)|(buf[10]<<8)|buf[11];
182 /* block_size */
183 format.blockalign = (buf[12]<<24)|(buf[13]<<16)|(buf[14]<<8)|buf[15];
184 if (format.blockalign == 0)
185 format.blockalign = format.channels*format.bitspersample;
186 format.numbytes = i - 8 - offset2snd;
187 i = 8 + offset2snd; /* advance to the beginning of data */
188 } else if (is_aifc && (memcmp(buf, "FVER", 4)==0)) {
189 /* Format Version Chunk (AIFC only chunk) */
190 /* skip this chunk */
191 } else {
192 DEBUGF("unsupported AIFF chunk: '%c%c%c%c', size=%lu\n",
193 buf[0], buf[1], buf[2], buf[3], (unsigned long)i);
196 if (i & 0x01) /* odd chunk sizes must be padded */
197 i++;
198 buf += i + 8;
199 if (n < (i + 8)) {
200 DEBUGF("CODEC_ERROR: AIFF header size > 1024\n");
201 i = CODEC_ERROR;
202 goto done;
204 n -= i + 8;
205 } /* while 'SSND' */
207 if (format.channels == 0) {
208 DEBUGF("CODEC_ERROR: 'COMM' chunk not found or 0-channels file\n");
209 i = CODEC_ERROR;
210 goto done;
212 if (format.numbytes == 0) {
213 DEBUGF("CODEC_ERROR: 'SSND' chunk not found or has zero length\n");
214 i = CODEC_ERROR;
215 goto done;
218 codec = get_codec(format.formattag);
219 if (codec == 0)
221 DEBUGF("CODEC_ERROR: AIFC does not support compressionType: 0x%x\n",
222 (unsigned int)format.formattag);
223 i = CODEC_ERROR;
224 goto done;
227 if (!codec->set_format(&format, 0))
229 i = CODEC_ERROR;
230 goto done;
233 ci->configure(DSP_SWITCH_FREQUENCY, ci->id3->frequency);
235 if (format.channels == 2) {
236 ci->configure(DSP_SET_STEREO_MODE, STEREO_INTERLEAVED);
237 } else if (format.channels == 1) {
238 ci->configure(DSP_SET_STEREO_MODE, STEREO_MONO);
239 } else {
240 DEBUGF("CODEC_ERROR: more than 2 channels unsupported\n");
241 i = CODEC_ERROR;
242 goto done;
245 firstblockposn = 1024 - n;
246 ci->advance_buffer(firstblockposn);
248 /* The main decoder loop */
249 bytesdone = 0;
250 ci->set_elapsed(0);
251 endofstream = 0;
253 while (!endofstream) {
254 ci->yield();
255 if (ci->stop_codec || ci->new_track)
256 break;
258 if (ci->seek_time) {
259 uint32_t newpos = codec->get_seek_pos(ci->seek_time);
260 if (newpos > format.numbytes)
261 break;
262 if (ci->seek_buffer(firstblockposn + newpos))
263 bytesdone = newpos;
264 ci->seek_complete();
266 aifbuf = (uint8_t *)ci->request_buffer(&n, format.chunksize);
268 if (n == 0)
269 break; /* End of stream */
271 if (bytesdone + n > format.numbytes) {
272 n = format.numbytes - bytesdone;
273 endofstream = 1;
276 status = codec->decode(aifbuf, n, samples, &bufcount);
277 if (status == CODEC_ERROR)
279 DEBUGF("codec error\n");
280 goto done;
283 ci->pcmbuf_insert(samples, NULL, bufcount);
285 ci->advance_buffer(n);
286 bytesdone += n;
287 decodedbytes += bufcount;
288 if (bytesdone >= format.numbytes)
289 endofstream = 1;
291 ci->set_elapsed(decodedbytes*1000LL/ci->id3->frequency);
293 i = CODEC_OK;
295 done:
296 if (ci->request_next_track())
297 goto next_track;
299 exit:
300 return i;