FS#11968 by Peter Lecky - Slovak language update
[maemo-rb.git] / apps / codecs / au.c
blob3f9436c9e733a58aa6c812268397eacfe1dd3f15
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2010 Yoshihisa Uchida
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 "codecs/libpcm/support_formats.h"
25 CODEC_HEADER
27 /* Sun Audio file (Au file format) codec
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
35 #define PCM_SAMPLE_SIZE (1024*2)
37 static int32_t samples[PCM_SAMPLE_SIZE] IBSS_ATTR;
39 enum
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 */
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 */
61 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 },
68 #define NUM_FORMATS 4
70 static const struct pcm_codec *get_au_codec(uint32_t formattag)
72 int i;
74 for (i = 0; i < NUM_FORMATS; i++)
76 if (au_codecs[i].format_tag == formattag)
78 if (au_codecs[i].get_codec)
79 return au_codecs[i].get_codec();
80 return 0;
83 return 0;
86 static unsigned int get_be32(uint8_t *buf)
88 return (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3];
91 static int convert_au_format(unsigned int encoding, struct pcm_format *fmt)
93 fmt->formattag = AU_FORMAT_UNSUPPORT;
94 if (encoding < 8)
96 fmt->formattag = support_formats[encoding][0];
97 fmt->bitspersample = support_formats[encoding][1];
99 else if (encoding == 27)
101 fmt->formattag = support_formats[8][0];
102 fmt->bitspersample = support_formats[8][1];
105 return fmt->formattag;
108 /* this is the codec entry point */
109 enum codec_status codec_main(void)
111 int status;
112 struct pcm_format format;
113 uint32_t bytesdone, decodedsamples;
114 size_t n;
115 int bufcount;
116 int endofstream;
117 unsigned char *buf;
118 uint8_t *aubuf;
119 off_t firstblockposn; /* position of the first block in file */
120 const struct pcm_codec *codec;
121 int offset = 0;
123 /* Generic codec initialisation */
124 ci->configure(DSP_SET_SAMPLE_DEPTH, PCM_OUTPUT_DEPTH-1);
126 next_track:
127 status = CODEC_OK;
129 if (codec_init()) {
130 DEBUGF("codec_init() error\n");
131 status = CODEC_ERROR;
132 goto exit;
135 if (codec_wait_taginfo() != 0)
136 goto done;
138 codec_set_replaygain(ci->id3);
140 /* Need to save offset for later use (cleared indirectly by advance_buffer) */
141 bytesdone = ci->id3->offset;
143 ci->memset(&format, 0, sizeof(struct pcm_format));
144 format.is_signed = true;
145 format.is_little_endian = false;
147 /* set format */
148 buf = ci->request_buffer(&n, 24);
149 if (n < 24 || (memcmp(buf, ".snd", 4) != 0))
152 * headerless sun audio file
153 * It is decoded under conditions.
154 * format: G.711 mu-law
155 * channel: mono
156 * frequency: 8000 kHz
158 offset = 0;
159 format.formattag = AU_FORMAT_MULAW;
160 format.channels = 1;
161 format.bitspersample = 8;
162 format.numbytes = ci->id3->filesize;
164 else
166 /* parse header */
168 /* data offset */
169 offset = get_be32(buf + 4);
170 if (offset < 24)
172 DEBUGF("CODEC_ERROR: sun audio offset size is small: %d\n", offset);
173 status = CODEC_ERROR;
174 goto done;
176 /* data size */
177 format.numbytes = get_be32(buf + 8);
178 if (format.numbytes == (uint32_t)0xffffffff)
179 format.numbytes = ci->id3->filesize - offset;
180 /* encoding */
181 format.formattag = convert_au_format(get_be32(buf + 12), &format);
182 if (format.formattag == AU_FORMAT_UNSUPPORT)
184 DEBUGF("CODEC_ERROR: sun audio unsupport format: %d\n", get_be32(buf + 12));
185 status = CODEC_ERROR;
186 goto done;
188 /* skip sample rate */
189 format.channels = get_be32(buf + 20);
192 /* advance to first WAVE chunk */
193 ci->advance_buffer(offset);
195 firstblockposn = offset;
197 decodedsamples = 0;
198 codec = 0;
200 /* get codec */
201 codec = get_au_codec(format.formattag);
202 if (!codec)
204 DEBUGF("CODEC_ERROR: unsupport sun audio format: %x\n", (int)format.formattag);
205 status = CODEC_ERROR;
206 goto done;
209 if (!codec->set_format(&format))
211 status = CODEC_ERROR;
212 goto done;
215 if (format.numbytes == 0) {
216 DEBUGF("CODEC_ERROR: data size is 0\n");
217 status = CODEC_ERROR;
218 goto done;
221 /* check chunksize */
222 if ((format.chunksize / format.blockalign) * format.samplesperblock * format.channels
223 > PCM_SAMPLE_SIZE)
224 format.chunksize = (PCM_SAMPLE_SIZE / format.blockalign) * format.blockalign;
225 if (format.chunksize == 0)
227 DEBUGF("CODEC_ERROR: chunksize is 0\n");
228 status = CODEC_ERROR;
229 goto done;
232 ci->configure(DSP_SWITCH_FREQUENCY, ci->id3->frequency);
233 if (format.channels == 2) {
234 ci->configure(DSP_SET_STEREO_MODE, STEREO_INTERLEAVED);
235 } else if (format.channels == 1) {
236 ci->configure(DSP_SET_STEREO_MODE, STEREO_MONO);
237 } else {
238 DEBUGF("CODEC_ERROR: more than 2 channels\n");
239 status = CODEC_ERROR;
240 goto done;
243 /* make sure we're at the correct offset */
244 if (bytesdone > (uint32_t) firstblockposn) {
245 /* Round down to previous block */
246 struct pcm_pos *newpos = codec->get_seek_pos(bytesdone - firstblockposn,
247 PCM_SEEK_POS, NULL);
249 if (newpos->pos > format.numbytes)
250 goto done;
251 if (ci->seek_buffer(firstblockposn + newpos->pos))
253 bytesdone = newpos->pos;
254 decodedsamples = newpos->samples;
256 ci->seek_complete();
257 } else {
258 /* already where we need to be */
259 bytesdone = 0;
262 /* The main decoder loop */
263 endofstream = 0;
265 while (!endofstream) {
266 ci->yield();
267 if (ci->stop_codec || ci->new_track) {
268 break;
271 if (ci->seek_time) {
272 /* 3rd args(read_buffer) is unnecessary in the format which Sun Audio supports. */
273 struct pcm_pos *newpos = codec->get_seek_pos(ci->seek_time, PCM_SEEK_TIME, NULL);
275 if (newpos->pos > format.numbytes)
276 break;
277 if (ci->seek_buffer(firstblockposn + newpos->pos))
279 bytesdone = newpos->pos;
280 decodedsamples = newpos->samples;
282 ci->seek_complete();
285 aubuf = (uint8_t *)ci->request_buffer(&n, format.chunksize);
286 if (n == 0)
287 break; /* End of stream */
288 if (bytesdone + n > format.numbytes) {
289 n = format.numbytes - bytesdone;
290 endofstream = 1;
293 status = codec->decode(aubuf, n, samples, &bufcount);
294 if (status == CODEC_ERROR)
296 DEBUGF("codec error\n");
297 goto done;
300 ci->pcmbuf_insert(samples, NULL, bufcount);
301 ci->advance_buffer(n);
302 bytesdone += n;
303 decodedsamples += bufcount;
305 if (bytesdone >= format.numbytes)
306 endofstream = 1;
307 ci->set_elapsed(decodedsamples*1000LL/ci->id3->frequency);
310 done:
311 if (ci->request_next_track())
312 goto next_track;
314 exit:
315 return status;