diff options
author | Sean Bartell <wingedtachikoma@gmail.com> | 2011-06-25 21:32:25 -0400 |
---|---|---|
committer | Nils Wallménius <nils@rockbox.org> | 2012-04-25 22:13:20 +0200 |
commit | f40bfc9267b13b54e6379dfe7539447662879d24 (patch) | |
tree | 9b20069d5e62809ff434061ad730096836f916f2 /lib/rbcodec/codecs/aiff.c | |
parent | a0009907de7a0107d49040d8a180f140e2eff299 (diff) | |
download | rockbox-f40bfc9267b13b54e6379dfe7539447662879d24.tar.gz rockbox-f40bfc9267b13b54e6379dfe7539447662879d24.zip |
Add codecs to librbcodec.
Change-Id: Id7f4717d51ed02d67cb9f9cb3c0ada4a81843f97
Reviewed-on: http://gerrit.rockbox.org/137
Reviewed-by: Nils Wallménius <nils@rockbox.org>
Tested-by: Nils Wallménius <nils@rockbox.org>
Diffstat (limited to 'lib/rbcodec/codecs/aiff.c')
-rw-r--r-- | lib/rbcodec/codecs/aiff.c | 350 |
1 files changed, 350 insertions, 0 deletions
diff --git a/lib/rbcodec/codecs/aiff.c b/lib/rbcodec/codecs/aiff.c new file mode 100644 index 0000000000..333bcd0455 --- /dev/null +++ b/lib/rbcodec/codecs/aiff.c | |||
@@ -0,0 +1,350 @@ | |||
1 | /*************************************************************************** | ||
2 | * __________ __ ___. | ||
3 | * Open \______ \ ____ ____ | | _\_ |__ _______ ___ | ||
4 | * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / | ||
5 | * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < | ||
6 | * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ | ||
7 | * \/ \/ \/ \/ \/ | ||
8 | * $Id$ | ||
9 | * | ||
10 | * Copyright (c) 2005 Jvo Studer | ||
11 | * Copyright (c) 2009 Yoshihisa Uchida | ||
12 | * | ||
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. | ||
17 | * | ||
18 | * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY | ||
19 | * KIND, either express or implied. | ||
20 | * | ||
21 | ****************************************************************************/ | ||
22 | |||
23 | #include "codeclib.h" | ||
24 | #include "codecs/libpcm/support_formats.h" | ||
25 | |||
26 | CODEC_HEADER | ||
27 | |||
28 | #define FOURCC(c1, c2, c3, c4) \ | ||
29 | ((((uint32_t)c1)<<24)|(((uint32_t)c2)<<16)|(((uint32_t)c3)<<8)|((uint32_t)c4)) | ||
30 | |||
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 */ | ||
39 | }; | ||
40 | |||
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 }, | ||
48 | }; | ||
49 | |||
50 | #define PCM_SAMPLE_SIZE (1024*2) | ||
51 | |||
52 | static int32_t samples[PCM_SAMPLE_SIZE] IBSS_ATTR; | ||
53 | |||
54 | static const struct pcm_codec *get_codec(uint32_t formattag) | ||
55 | { | ||
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(); | ||
60 | |||
61 | return NULL; | ||
62 | } | ||
63 | |||
64 | /* this is the codec entry point */ | ||
65 | enum codec_status codec_main(enum codec_entry_call_reason reason) | ||
66 | { | ||
67 | if (reason == CODEC_LOAD) { | ||
68 | /* Generic codec initialisation */ | ||
69 | ci->configure(DSP_SET_SAMPLE_DEPTH, PCM_OUTPUT_DEPTH-1); | ||
70 | } | ||
71 | |||
72 | return CODEC_OK; | ||
73 | } | ||
74 | |||
75 | /* this is called for each file to process */ | ||
76 | enum codec_status codec_run(void) | ||
77 | { | ||
78 | struct pcm_format format; | ||
79 | uint32_t bytesdone, decodedsamples; | ||
80 | /* rockbox: comment 'set but unused' variables | ||
81 | uint32_t num_sample_frames = 0; | ||
82 | */ | ||
83 | size_t n; | ||
84 | int bufcount; | ||
85 | int endofstream; | ||
86 | unsigned char *buf; | ||
87 | uint8_t *aifbuf; | ||
88 | uint32_t offset2snd = 0; | ||
89 | off_t firstblockposn; /* position of the first block in file */ | ||
90 | bool is_aifc = false; | ||
91 | const struct pcm_codec *codec; | ||
92 | uint32_t size; | ||
93 | intptr_t param; | ||
94 | |||
95 | if (codec_init()) { | ||
96 | return CODEC_ERROR; | ||
97 | } | ||
98 | |||
99 | codec_set_replaygain(ci->id3); | ||
100 | |||
101 | /* Need to save offset for later use (cleared indirectly by advance_buffer) */ | ||
102 | bytesdone = ci->id3->offset; | ||
103 | |||
104 | /* assume the AIFF header is less than 1024 bytes */ | ||
105 | ci->seek_buffer(0); | ||
106 | buf = ci->request_buffer(&n, 1024); | ||
107 | if (n < 54) { | ||
108 | return CODEC_ERROR; | ||
109 | } | ||
110 | |||
111 | if (memcmp(buf, "FORM", 4) != 0) | ||
112 | { | ||
113 | DEBUGF("CODEC_ERROR: does not aiff format %4.4s\n", (char*)&buf[0]); | ||
114 | return CODEC_ERROR; | ||
115 | } | ||
116 | if (memcmp(&buf[8], "AIFF", 4) == 0) | ||
117 | is_aifc = false; | ||
118 | else if (memcmp(&buf[8], "AIFC", 4) == 0) | ||
119 | is_aifc = true; | ||
120 | else | ||
121 | { | ||
122 | DEBUGF("CODEC_ERROR: does not aiff format %4.4s\n", (char*)&buf[8]); | ||
123 | return CODEC_ERROR; | ||
124 | } | ||
125 | |||
126 | buf += 12; | ||
127 | n -= 12; | ||
128 | |||
129 | ci->memset(&format, 0, sizeof(struct pcm_format)); | ||
130 | format.is_signed = true; | ||
131 | format.is_little_endian = false; | ||
132 | |||
133 | decodedsamples = 0; | ||
134 | codec = 0; | ||
135 | |||
136 | /* read until 'SSND' chunk, which typically is last */ | ||
137 | while (format.numbytes == 0 && n >= 8) | ||
138 | { | ||
139 | /* chunkSize */ | ||
140 | size = ((buf[4]<<24)|(buf[5]<<16)|(buf[6]<<8)|buf[7]); | ||
141 | if (memcmp(buf, "COMM", 4) == 0) { | ||
142 | if ((!is_aifc && size < 18) || (is_aifc && size < 22)) | ||
143 | { | ||
144 | DEBUGF("CODEC_ERROR: 'COMM' chunk size=%lu < %d\n", | ||
145 | (unsigned long)size, (is_aifc)?22:18); | ||
146 | return CODEC_ERROR; | ||
147 | } | ||
148 | /* num_channels */ | ||
149 | format.channels = ((buf[8]<<8)|buf[9]); | ||
150 | /* num_sample_frames */ | ||
151 | /* | ||
152 | num_sample_frames = ((buf[10]<<24)|(buf[11]<<16)|(buf[12]<<8) | ||
153 | |buf[13]); | ||
154 | */ | ||
155 | |||
156 | /* sample_size */ | ||
157 | format.bitspersample = ((buf[14]<<8)|buf[15]); | ||
158 | /* sample_rate (don't use last 4 bytes, only integer fs) */ | ||
159 | if (buf[16] != 0x40) { | ||
160 | DEBUGF("CODEC_ERROR: weird sampling rate (no @)\n"); | ||
161 | return CODEC_ERROR; | ||
162 | } | ||
163 | format.samplespersec = ((buf[18]<<24)|(buf[19]<<16)|(buf[20]<<8)|buf[21])+1; | ||
164 | format.samplespersec >>= (16 + 14 - buf[17]); | ||
165 | /* compressionType (AIFC only) */ | ||
166 | if (is_aifc) | ||
167 | { | ||
168 | format.formattag = (buf[26]<<24)|(buf[27]<<16)|(buf[28]<<8)|buf[29]; | ||
169 | |||
170 | /* | ||
171 | * aiff's sample_size is uncompressed sound data size. | ||
172 | * But format.bitspersample is compressed sound data size. | ||
173 | */ | ||
174 | if (format.formattag == AIFC_FORMAT_ALAW || | ||
175 | format.formattag == AIFC_FORMAT_MULAW) | ||
176 | format.bitspersample = 8; | ||
177 | else if (format.formattag == AIFC_FORMAT_QT_IMA_ADPCM) | ||
178 | format.bitspersample = 4; | ||
179 | } | ||
180 | else | ||
181 | format.formattag = AIFC_FORMAT_PCM; | ||
182 | /* calc average bytes per second */ | ||
183 | format.avgbytespersec = format.samplespersec*format.channels*format.bitspersample/8; | ||
184 | } else if (memcmp(buf, "SSND", 4)==0) { | ||
185 | if (format.bitspersample == 0) { | ||
186 | DEBUGF("CODEC_ERROR: unsupported chunk order\n"); | ||
187 | return CODEC_ERROR; | ||
188 | } | ||
189 | /* offset2snd */ | ||
190 | offset2snd = (buf[8]<<24)|(buf[9]<<16)|(buf[10]<<8)|buf[11]; | ||
191 | /* block_size */ | ||
192 | format.blockalign = ((buf[12]<<24)|(buf[13]<<16)|(buf[14]<<8)|buf[15]) >> 3; | ||
193 | if (format.blockalign == 0) | ||
194 | format.blockalign = format.channels * format.bitspersample >> 3; | ||
195 | format.numbytes = size - 8 - offset2snd; | ||
196 | size = 8 + offset2snd; /* advance to the beginning of data */ | ||
197 | } else if (is_aifc && (memcmp(buf, "FVER", 4)==0)) { | ||
198 | /* Format Version Chunk (AIFC only chunk) */ | ||
199 | /* skip this chunk */ | ||
200 | } else { | ||
201 | DEBUGF("unsupported AIFF chunk: '%c%c%c%c', size=%lu\n", | ||
202 | buf[0], buf[1], buf[2], buf[3], (unsigned long)size); | ||
203 | } | ||
204 | |||
205 | size += 8 + (size & 0x01); /* odd chunk sizes must be padded */ | ||
206 | |||
207 | buf += size; | ||
208 | if (n < size) { | ||
209 | DEBUGF("CODEC_ERROR: AIFF header size > 1024\n"); | ||
210 | return CODEC_ERROR; | ||
211 | } | ||
212 | n -= size; | ||
213 | } /* while 'SSND' */ | ||
214 | |||
215 | if (format.channels == 0) { | ||
216 | DEBUGF("CODEC_ERROR: 'COMM' chunk not found or 0-channels file\n"); | ||
217 | return CODEC_ERROR; | ||
218 | } | ||
219 | if (format.numbytes == 0) { | ||
220 | DEBUGF("CODEC_ERROR: 'SSND' chunk not found or has zero length\n"); | ||
221 | return CODEC_ERROR; | ||
222 | } | ||
223 | |||
224 | codec = get_codec(format.formattag); | ||
225 | if (codec == 0) | ||
226 | { | ||
227 | DEBUGF("CODEC_ERROR: AIFC does not support compressionType: 0x%x\n", | ||
228 | (unsigned int)format.formattag); | ||
229 | return CODEC_ERROR; | ||
230 | } | ||
231 | |||
232 | if (!codec->set_format(&format)) | ||
233 | { | ||
234 | return CODEC_ERROR; | ||
235 | } | ||
236 | |||
237 | ci->configure(DSP_SWITCH_FREQUENCY, ci->id3->frequency); | ||
238 | |||
239 | if (format.channels == 2) { | ||
240 | ci->configure(DSP_SET_STEREO_MODE, STEREO_INTERLEAVED); | ||
241 | } else if (format.channels == 1) { | ||
242 | ci->configure(DSP_SET_STEREO_MODE, STEREO_MONO); | ||
243 | } else { | ||
244 | DEBUGF("CODEC_ERROR: more than 2 channels unsupported\n"); | ||
245 | return CODEC_ERROR; | ||
246 | } | ||
247 | |||
248 | if (format.samplesperblock == 0) | ||
249 | { | ||
250 | DEBUGF("CODEC_ERROR: samplesperblock is 0\n"); | ||
251 | return CODEC_ERROR; | ||
252 | } | ||
253 | if (format.blockalign == 0) | ||
254 | { | ||
255 | DEBUGF("CODEC_ERROR: blockalign is 0\n"); | ||
256 | return CODEC_ERROR; | ||
257 | } | ||
258 | |||
259 | /* check chunksize */ | ||
260 | if ((format.chunksize / format.blockalign) * format.samplesperblock * format.channels | ||
261 | > PCM_SAMPLE_SIZE) | ||
262 | format.chunksize = (PCM_SAMPLE_SIZE / format.blockalign) * format.blockalign; | ||
263 | if (format.chunksize == 0) | ||
264 | { | ||
265 | DEBUGF("CODEC_ERROR: chunksize is 0\n"); | ||
266 | return CODEC_ERROR; | ||
267 | } | ||
268 | |||
269 | firstblockposn = 1024 - n; | ||
270 | ci->advance_buffer(firstblockposn); | ||
271 | |||
272 | /* make sure we're at the correct offset */ | ||
273 | if (bytesdone > (uint32_t) firstblockposn) { | ||
274 | /* Round down to previous block */ | ||
275 | struct pcm_pos *newpos = codec->get_seek_pos(bytesdone - firstblockposn, | ||
276 | PCM_SEEK_POS, NULL); | ||
277 | |||
278 | if (newpos->pos > format.numbytes) | ||
279 | return CODEC_OK; | ||
280 | |||
281 | if (ci->seek_buffer(firstblockposn + newpos->pos)) | ||
282 | { | ||
283 | bytesdone = newpos->pos; | ||
284 | decodedsamples = newpos->samples; | ||
285 | } | ||
286 | } else { | ||
287 | /* already where we need to be */ | ||
288 | bytesdone = 0; | ||
289 | } | ||
290 | |||
291 | ci->set_elapsed(decodedsamples*1000LL/ci->id3->frequency); | ||
292 | |||
293 | /* The main decoder loop */ | ||
294 | endofstream = 0; | ||
295 | |||
296 | while (!endofstream) { | ||
297 | enum codec_command_action action = ci->get_command(¶m); | ||
298 | |||
299 | if (action == CODEC_ACTION_HALT) | ||
300 | break; | ||
301 | |||
302 | if (action == CODEC_ACTION_SEEK_TIME) { | ||
303 | /* 3rd args(read_buffer) is unnecessary in the format which AIFF supports. */ | ||
304 | struct pcm_pos *newpos = codec->get_seek_pos(param, PCM_SEEK_TIME, NULL); | ||
305 | |||
306 | if (newpos->pos > format.numbytes) | ||
307 | { | ||
308 | ci->set_elapsed(ci->id3->length); | ||
309 | ci->seek_complete(); | ||
310 | break; | ||
311 | } | ||
312 | |||
313 | if (ci->seek_buffer(firstblockposn + newpos->pos)) | ||
314 | { | ||
315 | bytesdone = newpos->pos; | ||
316 | decodedsamples = newpos->samples; | ||
317 | } | ||
318 | |||
319 | ci->set_elapsed(decodedsamples*1000LL/ci->id3->frequency); | ||
320 | ci->seek_complete(); | ||
321 | } | ||
322 | aifbuf = (uint8_t *)ci->request_buffer(&n, format.chunksize); | ||
323 | |||
324 | if (n == 0) | ||
325 | break; /* End of stream */ | ||
326 | |||
327 | if (bytesdone + n > format.numbytes) { | ||
328 | n = format.numbytes - bytesdone; | ||
329 | endofstream = 1; | ||
330 | } | ||
331 | |||
332 | if (codec->decode(aifbuf, n, samples, &bufcount) == CODEC_ERROR) | ||
333 | { | ||
334 | DEBUGF("codec error\n"); | ||
335 | return CODEC_ERROR; | ||
336 | } | ||
337 | |||
338 | ci->pcmbuf_insert(samples, NULL, bufcount); | ||
339 | |||
340 | ci->advance_buffer(n); | ||
341 | bytesdone += n; | ||
342 | decodedsamples += bufcount; | ||
343 | if (bytesdone >= format.numbytes) | ||
344 | endofstream = 1; | ||
345 | |||
346 | ci->set_elapsed(decodedsamples*1000LL/ci->id3->frequency); | ||
347 | } | ||
348 | |||
349 | return CODEC_OK; | ||
350 | } | ||