1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
10 * Copyright (c) 2005 Jvo Studer
12 * All files in this archive are subject to the GNU General Public License.
13 * See the file COPYING in the source tree root for full license agreement.
15 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
16 * KIND, either express or implied.
18 ****************************************************************************/
25 /* Macro that sign extends an unsigned byte */
26 #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
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)
47 extern char iramcopy
[];
48 extern char iramstart
[];
49 extern char iramend
[];
54 static int32_t samples
[AIF_CHUNK_SIZE
] IBSS_ATTR
;
56 enum codec_status
codec_start(struct codec_api
*api
)
59 uint32_t numbytes
, bytesdone
;
60 uint16_t num_channels
= 0;
61 uint32_t num_sample_frames
= 0;
62 uint16_t sample_size
= 0;
63 uint32_t sample_rate
= 0;
70 uint32_t offset2snd
= 0;
71 uint16_t block_size
= 0;
72 uint32_t avgbytespersec
= 0;
73 off_t firstblockposn
; /* position of the first block in file */
75 /* Generic codec initialisation */
80 ci
->memcpy(iramstart
, iramcopy
, iramend
- iramstart
);
81 ci
->memset(iedata
, 0, iend
- iedata
);
84 ci
->configure(CODEC_SET_FILEBUF_WATERMARK
, (int *)(1024*512));
85 ci
->configure(CODEC_SET_FILEBUF_CHUNKSIZE
, (int *)(1024*256));
86 ci
->configure(DSP_DITHER
, (bool *)false);
89 if (codec_init(api
)) {
94 while (!*ci
->taginfo_ready
)
97 /* assume the AIFF header is less than 1024 bytes */
98 buf
= ci
->request_buffer(&n
, 1024);
103 if ((memcmp(buf
, "FORM", 4) != 0) || (memcmp(&buf
[8], "AIFF", 4) != 0)) {
112 /* read until 'SSND' chunk, which typically is last */
113 while (numbytes
== 0 && n
>= 8) {
115 i
= ((buf
[4]<<24)|(buf
[5]<<16)|(buf
[6]<<8)|buf
[7]);
116 if (memcmp(buf
, "COMM", 4) == 0) {
118 DEBUGF("CODEC_ERROR: 'COMM' chunk size=%lu != 18\n", i
);
123 num_channels
= ((buf
[8]<<8)|buf
[9]);
124 /* num_sample_frames */
125 num_sample_frames
= ((buf
[10]<<24)|(buf
[11]<<16)|(buf
[12]<<8)
128 sample_size
= ((buf
[14]<<8)|buf
[15]);
129 /* sample_rate (don't use last 4 bytes, only integer fs) */
130 if (buf
[16] != 0x40) {
131 DEBUGF("CODEC_ERROR: weird sampling rate (no @)\n", i
);
135 sample_rate
= ((buf
[18]<<24)|(buf
[19]<<16)|(buf
[20]<<8)|buf
[21])+1;
136 sample_rate
= sample_rate
>> (16 + 14 - buf
[17]);
137 /* calc average bytes per second */
138 avgbytespersec
= sample_rate
*num_channels
*sample_size
/8;
139 } else if (memcmp(buf
, "SSND", 4)==0) {
140 if (sample_size
== 0) {
141 DEBUGF("CODEC_ERROR: unsupported chunk order\n");
146 offset2snd
= ((buf
[8]<<8)|buf
[9]);
148 block_size
= ((buf
[10]<<8)|buf
[11]);
150 block_size
= num_channels
*sample_size
;
151 numbytes
= i
- 8 - offset2snd
;
152 i
= 8 + offset2snd
; /* advance to the beginning of data */
154 DEBUGF("unsupported AIFF chunk: '%c%c%c%c', size=%lu\n",
155 buf
[0], buf
[1], buf
[2], buf
[3], i
);
158 if (i
& 0x01) /* odd chunk sizes must be padded */
162 DEBUGF("CODEC_ERROR: AIFF header size > 1024\n");
169 if (num_channels
== 0) {
170 DEBUGF("CODEC_ERROR: 'COMM' chunk not found or 0-channels file\n");
175 DEBUGF("CODEC_ERROR: 'SSND' chunk not found or has zero length\n");
179 if (sample_size
> 24) {
180 DEBUGF("CODEC_ERROR: PCM with more than 24 bits per sample "
186 ci
->configure(CODEC_DSP_ENABLE
, (bool *)true);
187 ci
->configure(DSP_SET_FREQUENCY
, (long *)(ci
->id3
->frequency
));
188 ci
->configure(DSP_SET_SAMPLE_DEPTH
, (long *)28);
190 if (num_channels
== 2) {
191 ci
->configure(DSP_SET_STEREO_MODE
, (int *)STEREO_INTERLEAVED
);
192 } else if (num_channels
== 1) {
193 ci
->configure(DSP_SET_STEREO_MODE
, (int *)STEREO_MONO
);
195 DEBUGF("CODEC_ERROR: more than 2 channels unsupported\n");
200 firstblockposn
= 1024 - n
;
201 ci
->advance_buffer(firstblockposn
);
203 /* The main decoder loop */
207 /* chunksize is computed so that one chunk is about 1/50s.
208 * this make 4096 for 44.1kHz 16bits stereo.
209 * It also has to be a multiple of blockalign */
210 chunksize
= (1 + avgbytespersec
/(50*block_size
))*block_size
;
211 /* check that the output buffer is big enough (convert to samplespersec,
212 then round to the block_size multiple below) */
213 if (((uint64_t)chunksize
*ci
->id3
->frequency
*num_channels
*2)
214 /(uint64_t)avgbytespersec
>= AIF_CHUNK_SIZE
) {
215 chunksize
= ((uint64_t)AIF_CHUNK_SIZE
*avgbytespersec
216 /((uint64_t)ci
->id3
->frequency
*num_channels
*2
217 *block_size
))*block_size
;
220 while (!endofstream
) {
222 if (ci
->stop_codec
|| ci
->reload_codec
)
228 /* use avgbytespersec to round to the closest blockalign multiple,
229 add firstblockposn. 64-bit casts to avoid overflows. */
230 newpos
= (((uint64_t)avgbytespersec
*(ci
->seek_time
- 1))
231 /(1000LL*block_size
))*block_size
;
232 if (newpos
> numbytes
)
234 if (ci
->seek_buffer(firstblockposn
+ newpos
))
238 aifbuf
= (uint8_t *)ci
->request_buffer(&n
, chunksize
);
241 break; /* End of stream */
243 if (bytesdone
+ n
> numbytes
) {
244 n
= numbytes
- bytesdone
;
248 if (sample_size
> 24) {
249 for (i
= 0; i
< n
; i
+= 4) {
250 samples
[i
/4] = (SE(aifbuf
[i
])<<21)|(aifbuf
[i
+ 1]<<13)
251 |(aifbuf
[i
+ 2]<<5)|(aifbuf
[i
+ 3]>>3);
254 } else if (sample_size
> 16) {
255 for (i
= 0; i
< n
; i
+= 3) {
256 samples
[i
/3] = (SE(aifbuf
[i
])<<21)|(aifbuf
[i
+ 1]<<13)
260 } else if (sample_size
> 8) {
261 for (i
= 0; i
< n
; i
+= 2)
262 samples
[i
/2] = (SE(aifbuf
[i
])<<21)|(aifbuf
[i
+ 1]<<13);
265 for (i
= 0; i
< n
; i
++)
266 samples
[i
] = SE(aifbuf
[i
]) << 21;
270 while (!ci
->pcmbuf_insert((char *)samples
, bufsize
))
273 ci
->advance_buffer(n
);
275 if (bytesdone
>= numbytes
)
278 ci
->set_elapsed(bytesdone
*1000LL/avgbytespersec
);
281 if (ci
->request_next_track())