FS#8961 - Anti-Aliased Fonts.
[kugel-rb/myfork.git] / apps / codecs / aiff.c
blob9a675415e28fb5610cf02187a6bcfa15a81f3df9
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (c) 2005 Jvo Studer
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
20 ****************************************************************************/
22 #include "codeclib.h"
23 #include <inttypes.h>
25 CODEC_HEADER
27 /* Macro that sign extends an unsigned byte */
28 #define SE(x) ((int32_t)((int8_t)(x)))
30 /* This codec supports AIFF files with the following formats:
31 * - PCM, 8, 16 and 24 bits, mono or stereo
34 enum
36 AIFF_FORMAT_PCM = 0x0001, /* AIFF PCM Format (big endian) */
37 IEEE_FORMAT_FLOAT = 0x0003, /* IEEE Float */
38 AIFF_FORMAT_ALAW = 0x0004, /* AIFC ALaw compressed */
39 AIFF_FORMAT_ULAW = 0x0005 /* AIFC uLaw compressed */
42 /* Maximum number of bytes to process in one iteration */
43 /* for 44.1kHz stereo 16bits, this represents 0.023s ~= 1/50s */
44 #define AIF_CHUNK_SIZE (1024*2)
46 static int32_t samples[AIF_CHUNK_SIZE] IBSS_ATTR;
48 enum codec_status codec_main(void)
50 uint32_t numbytes, bytesdone;
51 uint16_t num_channels = 0;
52 uint32_t num_sample_frames = 0;
53 uint16_t sample_size = 0;
54 uint32_t sample_rate = 0;
55 uint32_t i;
56 size_t n;
57 int bufcount;
58 int endofstream;
59 unsigned char *buf;
60 uint8_t *aifbuf;
61 long chunksize;
62 uint32_t offset2snd = 0;
63 uint16_t block_size = 0;
64 uint32_t avgbytespersec = 0;
65 off_t firstblockposn; /* position of the first block in file */
67 /* Generic codec initialisation */
68 ci->configure(DSP_SET_SAMPLE_DEPTH, 28);
70 next_track:
71 if (codec_init()) {
72 i = CODEC_ERROR;
73 goto exit;
76 while (!*ci->taginfo_ready && !ci->stop_codec)
77 ci->sleep(1);
79 codec_set_replaygain(ci->id3);
81 /* assume the AIFF header is less than 1024 bytes */
82 buf = ci->request_buffer(&n, 1024);
83 if (n < 54) {
84 i = CODEC_ERROR;
85 goto done;
87 if ((memcmp(buf, "FORM", 4) != 0) || (memcmp(&buf[8], "AIFF", 4) != 0)) {
88 i = CODEC_ERROR;
89 goto done;
92 buf += 12;
93 n -= 12;
94 numbytes = 0;
96 /* read until 'SSND' chunk, which typically is last */
97 while (numbytes == 0 && n >= 8) {
98 /* chunkSize */
99 i = ((buf[4]<<24)|(buf[5]<<16)|(buf[6]<<8)|buf[7]);
100 if (memcmp(buf, "COMM", 4) == 0) {
101 if (i < 18) {
102 DEBUGF("CODEC_ERROR: 'COMM' chunk size=%lu < 18\n",
103 (unsigned long)i);
104 i = CODEC_ERROR;
105 goto done;
107 /* num_channels */
108 num_channels = ((buf[8]<<8)|buf[9]);
109 /* num_sample_frames */
110 num_sample_frames = ((buf[10]<<24)|(buf[11]<<16)|(buf[12]<<8)
111 |buf[13]);
112 /* sample_size */
113 sample_size = ((buf[14]<<8)|buf[15]);
114 /* sample_rate (don't use last 4 bytes, only integer fs) */
115 if (buf[16] != 0x40) {
116 DEBUGF("CODEC_ERROR: weird sampling rate (no @)\n");
117 i = CODEC_ERROR;
118 goto done;
120 sample_rate = ((buf[18]<<24)|(buf[19]<<16)|(buf[20]<<8)|buf[21])+1;
121 sample_rate = sample_rate >> (16 + 14 - buf[17]);
122 /* calc average bytes per second */
123 avgbytespersec = sample_rate*num_channels*sample_size/8;
124 } else if (memcmp(buf, "SSND", 4)==0) {
125 if (sample_size == 0) {
126 DEBUGF("CODEC_ERROR: unsupported chunk order\n");
127 i = CODEC_ERROR;
128 goto done;
130 /* offset2snd */
131 offset2snd = (buf[8]<<24)|(buf[9]<<16)|(buf[10]<<8)|buf[11];
132 /* block_size */
133 block_size = (buf[12]<<24)|(buf[13]<<16)|(buf[14]<<8)|buf[15];
134 if (block_size == 0)
135 block_size = num_channels*sample_size;
136 numbytes = i - 8 - offset2snd;
137 i = 8 + offset2snd; /* advance to the beginning of data */
138 } else {
139 DEBUGF("unsupported AIFF chunk: '%c%c%c%c', size=%lu\n",
140 buf[0], buf[1], buf[2], buf[3], (unsigned long)i);
143 if (i & 0x01) /* odd chunk sizes must be padded */
144 i++;
145 buf += i + 8;
146 if (n < (i + 8)) {
147 DEBUGF("CODEC_ERROR: AIFF header size > 1024\n");
148 i = CODEC_ERROR;
149 goto done;
151 n -= i + 8;
152 } /* while 'SSND' */
154 if (num_channels == 0) {
155 DEBUGF("CODEC_ERROR: 'COMM' chunk not found or 0-channels file\n");
156 i = CODEC_ERROR;
157 goto done;
159 if (numbytes == 0) {
160 DEBUGF("CODEC_ERROR: 'SSND' chunk not found or has zero length\n");
161 i = CODEC_ERROR;
162 goto done;
164 if (sample_size > 24) {
165 DEBUGF("CODEC_ERROR: PCM with more than 24 bits per sample "
166 "is unsupported\n");
167 i = CODEC_ERROR;
168 goto done;
171 ci->configure(DSP_SWITCH_FREQUENCY, ci->id3->frequency);
173 if (num_channels == 2) {
174 ci->configure(DSP_SET_STEREO_MODE, STEREO_INTERLEAVED);
175 } else if (num_channels == 1) {
176 ci->configure(DSP_SET_STEREO_MODE, STEREO_MONO);
177 } else {
178 DEBUGF("CODEC_ERROR: more than 2 channels unsupported\n");
179 i = CODEC_ERROR;
180 goto done;
183 firstblockposn = 1024 - n;
184 ci->advance_buffer(firstblockposn);
186 /* The main decoder loop */
187 bytesdone = 0;
188 ci->set_elapsed(0);
189 endofstream = 0;
190 /* chunksize is computed so that one chunk is about 1/50s.
191 * this make 4096 for 44.1kHz 16bits stereo.
192 * It also has to be a multiple of blockalign */
193 chunksize = (1 + avgbytespersec/(50*block_size))*block_size;
194 /* check that the output buffer is big enough (convert to samplespersec,
195 then round to the block_size multiple below) */
196 if (((uint64_t)chunksize*ci->id3->frequency*num_channels*2)
197 /(uint64_t)avgbytespersec >= AIF_CHUNK_SIZE) {
198 chunksize = ((uint64_t)AIF_CHUNK_SIZE*avgbytespersec
199 /((uint64_t)ci->id3->frequency*num_channels*2
200 *block_size))*block_size;
203 while (!endofstream) {
204 ci->yield();
205 if (ci->stop_codec || ci->new_track)
206 break;
208 if (ci->seek_time) {
209 uint32_t newpos;
211 /* use avgbytespersec to round to the closest blockalign multiple,
212 add firstblockposn. 64-bit casts to avoid overflows. */
213 newpos = (((uint64_t)avgbytespersec*(ci->seek_time - 1))
214 /(1000LL*block_size))*block_size;
215 if (newpos > numbytes)
216 break;
217 if (ci->seek_buffer(firstblockposn + newpos))
218 bytesdone = newpos;
219 ci->seek_complete();
221 aifbuf = (uint8_t *)ci->request_buffer(&n, chunksize);
223 if (n == 0)
224 break; /* End of stream */
226 if (bytesdone + n > numbytes) {
227 n = numbytes - bytesdone;
228 endofstream = 1;
231 if (sample_size > 24) {
232 for (i = 0; i < n; i += 4) {
233 samples[i/4] = (SE(aifbuf[i])<<21)|(aifbuf[i + 1]<<13)
234 |(aifbuf[i + 2]<<5)|(aifbuf[i + 3]>>3);
236 bufcount = n >> 2;
237 } else if (sample_size > 16) {
238 for (i = 0; i < n; i += 3) {
239 samples[i/3] = (SE(aifbuf[i])<<21)|(aifbuf[i + 1]<<13)
240 |(aifbuf[i + 2]<<5);
242 bufcount = n/3;
243 } else if (sample_size > 8) {
244 for (i = 0; i < n; i += 2)
245 samples[i/2] = (SE(aifbuf[i])<<21)|(aifbuf[i + 1]<<13);
246 bufcount = n >> 1;
247 } else {
248 for (i = 0; i < n; i++)
249 samples[i] = SE(aifbuf[i]) << 21;
250 bufcount = n;
253 if (num_channels == 2)
254 bufcount >>= 1;
256 ci->pcmbuf_insert(samples, NULL, bufcount);
258 ci->advance_buffer(n);
259 bytesdone += n;
260 if (bytesdone >= numbytes)
261 endofstream = 1;
263 ci->set_elapsed(bytesdone*1000LL/avgbytespersec);
265 i = CODEC_OK;
267 done:
268 if (ci->request_next_track())
269 goto next_track;
271 exit:
272 return i;