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/au.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/au.c')
-rw-r--r-- | lib/rbcodec/codecs/au.c | 314 |
1 files changed, 314 insertions, 0 deletions
diff --git a/lib/rbcodec/codecs/au.c b/lib/rbcodec/codecs/au.c new file mode 100644 index 0000000000..cb75c7423d --- /dev/null +++ b/lib/rbcodec/codecs/au.c | |||
@@ -0,0 +1,314 @@ | |||
1 | /*************************************************************************** | ||
2 | * __________ __ ___. | ||
3 | * Open \______ \ ____ ____ | | _\_ |__ _______ ___ | ||
4 | * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / | ||
5 | * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < | ||
6 | * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ | ||
7 | * \/ \/ \/ \/ \/ | ||
8 | * $Id$ | ||
9 | * | ||
10 | * Copyright (C) 2010 Yoshihisa Uchida | ||
11 | * | ||
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. | ||
16 | * | ||
17 | * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY | ||
18 | * KIND, either express or implied. | ||
19 | * | ||
20 | ****************************************************************************/ | ||
21 | |||
22 | #include "codeclib.h" | ||
23 | #include "codecs/libpcm/support_formats.h" | ||
24 | |||
25 | CODEC_HEADER | ||
26 | |||
27 | /* Sun Audio file (Au file format) codec | ||
28 | * | ||
29 | * References | ||
30 | * [1] Sun Microsystems, Inc., Header file for Audio, .au, 1992 | ||
31 | * URL http://www.opengroup.org/public/pubs/external/auformat.html | ||
32 | * [2] Wikipedia, Au file format, URL: http://en.wikipedia.org/wiki/Sun_Audio | ||
33 | */ | ||
34 | |||
35 | #define PCM_SAMPLE_SIZE (1024*2) | ||
36 | |||
37 | static int32_t samples[PCM_SAMPLE_SIZE] IBSS_ATTR; | ||
38 | |||
39 | enum | ||
40 | { | ||
41 | AU_FORMAT_UNSUPPORT = 0, /* unsupported format */ | ||
42 | AU_FORMAT_MULAW, /* G.711 MULAW */ | ||
43 | AU_FORMAT_PCM, /* Linear PCM */ | ||
44 | AU_FORMAT_IEEE_FLOAT, /* IEEE float */ | ||
45 | AU_FORMAT_ALAW, /* G.711 ALAW */ | ||
46 | }; | ||
47 | |||
48 | static const char support_formats[9][2] = { | ||
49 | { AU_FORMAT_UNSUPPORT, 0 }, /* encoding */ | ||
50 | { AU_FORMAT_MULAW, 8 }, /* 1: G.711 MULAW */ | ||
51 | { AU_FORMAT_PCM, 8 }, /* 2: Linear PCM 8bit (signed) */ | ||
52 | { AU_FORMAT_PCM, 16 }, /* 3: Linear PCM 16bit (signed, big endian) */ | ||
53 | { AU_FORMAT_PCM, 24 }, /* 4: Linear PCM 24bit (signed, big endian) */ | ||
54 | { AU_FORMAT_PCM, 32 }, /* 5: Linear PCM 32bit (signed, big endian) */ | ||
55 | { AU_FORMAT_IEEE_FLOAT, 32 }, /* 6: Linear PCM float 32bit (signed, big endian) */ | ||
56 | { AU_FORMAT_IEEE_FLOAT, 64 }, /* 7: Linear PCM float 64bit (signed, big endian) */ | ||
57 | /* encoding 8 - 26 unsupported. */ | ||
58 | { AU_FORMAT_ALAW, 8 }, /* 27: G.711 ALAW */ | ||
59 | }; | ||
60 | |||
61 | static const struct pcm_entry au_codecs[] = { | ||
62 | { AU_FORMAT_MULAW, get_itut_g711_mulaw_codec }, | ||
63 | { AU_FORMAT_PCM, get_linear_pcm_codec }, | ||
64 | { AU_FORMAT_IEEE_FLOAT, get_ieee_float_codec }, | ||
65 | { AU_FORMAT_ALAW, get_itut_g711_alaw_codec }, | ||
66 | }; | ||
67 | |||
68 | #define NUM_FORMATS 4 | ||
69 | |||
70 | static const struct pcm_codec *get_au_codec(uint32_t formattag) | ||
71 | { | ||
72 | int i; | ||
73 | |||
74 | for (i = 0; i < NUM_FORMATS; i++) | ||
75 | { | ||
76 | if (au_codecs[i].format_tag == formattag) | ||
77 | { | ||
78 | if (au_codecs[i].get_codec) | ||
79 | return au_codecs[i].get_codec(); | ||
80 | return 0; | ||
81 | } | ||
82 | } | ||
83 | return 0; | ||
84 | } | ||
85 | |||
86 | static unsigned int get_be32(uint8_t *buf) | ||
87 | { | ||
88 | return (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3]; | ||
89 | } | ||
90 | |||
91 | static int convert_au_format(unsigned int encoding, struct pcm_format *fmt) | ||
92 | { | ||
93 | fmt->formattag = AU_FORMAT_UNSUPPORT; | ||
94 | if (encoding < 8) | ||
95 | { | ||
96 | fmt->formattag = support_formats[encoding][0]; | ||
97 | fmt->bitspersample = support_formats[encoding][1]; | ||
98 | } | ||
99 | else if (encoding == 27) | ||
100 | { | ||
101 | fmt->formattag = support_formats[8][0]; | ||
102 | fmt->bitspersample = support_formats[8][1]; | ||
103 | } | ||
104 | |||
105 | return fmt->formattag; | ||
106 | } | ||
107 | |||
108 | /* this is the codec entry point */ | ||
109 | enum codec_status codec_main(enum codec_entry_call_reason reason) | ||
110 | { | ||
111 | if (reason == CODEC_LOAD) { | ||
112 | /* Generic codec initialisation */ | ||
113 | ci->configure(DSP_SET_SAMPLE_DEPTH, PCM_OUTPUT_DEPTH-1); | ||
114 | } | ||
115 | |||
116 | return CODEC_OK; | ||
117 | } | ||
118 | |||
119 | /* this is called for each file to process */ | ||
120 | enum codec_status codec_run(void) | ||
121 | { | ||
122 | struct pcm_format format; | ||
123 | uint32_t bytesdone, decodedsamples; | ||
124 | size_t n; | ||
125 | int bufcount; | ||
126 | int endofstream; | ||
127 | unsigned char *buf; | ||
128 | uint8_t *aubuf; | ||
129 | off_t firstblockposn; /* position of the first block in file */ | ||
130 | const struct pcm_codec *codec; | ||
131 | int offset = 0; | ||
132 | intptr_t param; | ||
133 | |||
134 | if (codec_init()) { | ||
135 | DEBUGF("codec_init() error\n"); | ||
136 | return CODEC_ERROR; | ||
137 | } | ||
138 | |||
139 | codec_set_replaygain(ci->id3); | ||
140 | |||
141 | /* Need to save offset for later use (cleared indirectly by advance_buffer) */ | ||
142 | bytesdone = ci->id3->offset; | ||
143 | |||
144 | ci->memset(&format, 0, sizeof(struct pcm_format)); | ||
145 | format.is_signed = true; | ||
146 | format.is_little_endian = false; | ||
147 | |||
148 | /* set format */ | ||
149 | ci->seek_buffer(0); | ||
150 | buf = ci->request_buffer(&n, 24); | ||
151 | if (n < 24 || (memcmp(buf, ".snd", 4) != 0)) | ||
152 | { | ||
153 | /* | ||
154 | * headerless sun audio file | ||
155 | * It is decoded under conditions. | ||
156 | * format: G.711 mu-law | ||
157 | * channel: mono | ||
158 | * frequency: 8000 kHz | ||
159 | */ | ||
160 | offset = 0; | ||
161 | format.formattag = AU_FORMAT_MULAW; | ||
162 | format.channels = 1; | ||
163 | format.bitspersample = 8; | ||
164 | format.numbytes = ci->id3->filesize; | ||
165 | } | ||
166 | else | ||
167 | { | ||
168 | /* parse header */ | ||
169 | |||
170 | /* data offset */ | ||
171 | offset = get_be32(buf + 4); | ||
172 | if (offset < 24) | ||
173 | { | ||
174 | DEBUGF("CODEC_ERROR: sun audio offset size is small: %d\n", offset); | ||
175 | return CODEC_ERROR; | ||
176 | } | ||
177 | /* data size */ | ||
178 | format.numbytes = get_be32(buf + 8); | ||
179 | if (format.numbytes == (uint32_t)0xffffffff) | ||
180 | format.numbytes = ci->id3->filesize - offset; | ||
181 | /* encoding */ | ||
182 | format.formattag = convert_au_format(get_be32(buf + 12), &format); | ||
183 | if (format.formattag == AU_FORMAT_UNSUPPORT) | ||
184 | { | ||
185 | DEBUGF("CODEC_ERROR: sun audio unsupport format: %d\n", get_be32(buf + 12)); | ||
186 | return CODEC_ERROR; | ||
187 | } | ||
188 | /* skip sample rate */ | ||
189 | format.channels = get_be32(buf + 20); | ||
190 | } | ||
191 | |||
192 | /* advance to first WAVE chunk */ | ||
193 | ci->advance_buffer(offset); | ||
194 | |||
195 | firstblockposn = offset; | ||
196 | |||
197 | decodedsamples = 0; | ||
198 | codec = 0; | ||
199 | |||
200 | /* get codec */ | ||
201 | codec = get_au_codec(format.formattag); | ||
202 | if (!codec) | ||
203 | { | ||
204 | DEBUGF("CODEC_ERROR: unsupport sun audio format: %x\n", (int)format.formattag); | ||
205 | return CODEC_ERROR; | ||
206 | } | ||
207 | |||
208 | if (!codec->set_format(&format)) | ||
209 | { | ||
210 | return CODEC_ERROR; | ||
211 | } | ||
212 | |||
213 | if (format.numbytes == 0) { | ||
214 | DEBUGF("CODEC_ERROR: data size is 0\n"); | ||
215 | return CODEC_ERROR; | ||
216 | } | ||
217 | |||
218 | /* check chunksize */ | ||
219 | if ((format.chunksize / format.blockalign) * format.samplesperblock * format.channels | ||
220 | > PCM_SAMPLE_SIZE) | ||
221 | format.chunksize = (PCM_SAMPLE_SIZE / format.blockalign) * format.blockalign; | ||
222 | if (format.chunksize == 0) | ||
223 | { | ||
224 | DEBUGF("CODEC_ERROR: chunksize is 0\n"); | ||
225 | return CODEC_ERROR; | ||
226 | } | ||
227 | |||
228 | ci->configure(DSP_SWITCH_FREQUENCY, ci->id3->frequency); | ||
229 | if (format.channels == 2) { | ||
230 | ci->configure(DSP_SET_STEREO_MODE, STEREO_INTERLEAVED); | ||
231 | } else if (format.channels == 1) { | ||
232 | ci->configure(DSP_SET_STEREO_MODE, STEREO_MONO); | ||
233 | } else { | ||
234 | DEBUGF("CODEC_ERROR: more than 2 channels\n"); | ||
235 | return CODEC_ERROR; | ||
236 | } | ||
237 | |||
238 | /* make sure we're at the correct offset */ | ||
239 | if (bytesdone > (uint32_t) firstblockposn) { | ||
240 | /* Round down to previous block */ | ||
241 | struct pcm_pos *newpos = codec->get_seek_pos(bytesdone - firstblockposn, | ||
242 | PCM_SEEK_POS, NULL); | ||
243 | |||
244 | if (newpos->pos > format.numbytes) | ||
245 | goto done; | ||
246 | if (ci->seek_buffer(firstblockposn + newpos->pos)) | ||
247 | { | ||
248 | bytesdone = newpos->pos; | ||
249 | decodedsamples = newpos->samples; | ||
250 | } | ||
251 | } else { | ||
252 | /* already where we need to be */ | ||
253 | bytesdone = 0; | ||
254 | } | ||
255 | |||
256 | ci->set_elapsed(decodedsamples*1000LL/ci->id3->frequency); | ||
257 | |||
258 | /* The main decoder loop */ | ||
259 | endofstream = 0; | ||
260 | |||
261 | while (!endofstream) { | ||
262 | enum codec_command_action action = ci->get_command(¶m); | ||
263 | |||
264 | if (action == CODEC_ACTION_HALT) | ||
265 | break; | ||
266 | |||
267 | if (action == CODEC_ACTION_SEEK_TIME) { | ||
268 | /* 3rd args(read_buffer) is unnecessary in the format which Sun Audio supports. */ | ||
269 | struct pcm_pos *newpos = codec->get_seek_pos(param, PCM_SEEK_TIME, NULL); | ||
270 | |||
271 | if (newpos->pos > format.numbytes) | ||
272 | { | ||
273 | ci->set_elapsed(ci->id3->length); | ||
274 | ci->seek_complete(); | ||
275 | break; | ||
276 | } | ||
277 | |||
278 | if (ci->seek_buffer(firstblockposn + newpos->pos)) | ||
279 | { | ||
280 | bytesdone = newpos->pos; | ||
281 | decodedsamples = newpos->samples; | ||
282 | } | ||
283 | |||
284 | ci->set_elapsed(decodedsamples*1000LL/ci->id3->frequency); | ||
285 | ci->seek_complete(); | ||
286 | } | ||
287 | |||
288 | aubuf = (uint8_t *)ci->request_buffer(&n, format.chunksize); | ||
289 | if (n == 0) | ||
290 | break; /* End of stream */ | ||
291 | if (bytesdone + n > format.numbytes) { | ||
292 | n = format.numbytes - bytesdone; | ||
293 | endofstream = 1; | ||
294 | } | ||
295 | |||
296 | if (codec->decode(aubuf, n, samples, &bufcount) == CODEC_ERROR) | ||
297 | { | ||
298 | DEBUGF("codec error\n"); | ||
299 | return CODEC_ERROR; | ||
300 | } | ||
301 | |||
302 | ci->pcmbuf_insert(samples, NULL, bufcount); | ||
303 | ci->advance_buffer(n); | ||
304 | bytesdone += n; | ||
305 | decodedsamples += bufcount; | ||
306 | |||
307 | if (bytesdone >= format.numbytes) | ||
308 | endofstream = 1; | ||
309 | ci->set_elapsed(decodedsamples*1000LL/ci->id3->frequency); | ||
310 | } | ||
311 | |||
312 | done: | ||
313 | return CODEC_OK; | ||
314 | } | ||