summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorYoshihisa Uchida <uchida@rockbox.org>2010-02-28 07:22:20 +0000
committerYoshihisa Uchida <uchida@rockbox.org>2010-02-28 07:22:20 +0000
commit4e3c8074664fa87b023643f375171d544d662141 (patch)
tree62e61e1eda814db6bd766b443b0b40d8c2e86bb7
parent1fefb48e871cee8328d8f9a5d375cde0627e603a (diff)
downloadrockbox-4e3c8074664fa87b023643f375171d544d662141.tar.gz
rockbox-4e3c8074664fa87b023643f375171d544d662141.zip
Add Sun Audio codec. (FS#10433)
git-svn-id: svn://svn.rockbox.org/rockbox/trunk@24955 a1c6a512-1295-4272-9138-f99709370657
-rw-r--r--apps/SOURCES1
-rw-r--r--apps/codecs/SOURCES1
-rw-r--r--apps/codecs/au.c319
-rw-r--r--apps/codecs/codecs.make1
-rw-r--r--apps/filetypes.c2
-rw-r--r--apps/metadata.c11
-rw-r--r--apps/metadata.h1
-rw-r--r--apps/metadata/au.c122
-rw-r--r--apps/metadata/metadata_parsers.h1
9 files changed, 459 insertions, 0 deletions
diff --git a/apps/SOURCES b/apps/SOURCES
index 069e619451..47937fab70 100644
--- a/apps/SOURCES
+++ b/apps/SOURCES
@@ -188,6 +188,7 @@ metadata/rm.c
188metadata/nsf.c 188metadata/nsf.c
189metadata/oma.c 189metadata/oma.c
190metadata/smaf.c 190metadata/smaf.c
191metadata/au.c
191#endif 192#endif
192#ifdef HAVE_TAGCACHE 193#ifdef HAVE_TAGCACHE
193tagcache.c 194tagcache.c
diff --git a/apps/codecs/SOURCES b/apps/codecs/SOURCES
index 9787caa122..b8a86aed8a 100644
--- a/apps/codecs/SOURCES
+++ b/apps/codecs/SOURCES
@@ -28,6 +28,7 @@ aiff.c
28speex.c 28speex.c
29adx.c 29adx.c
30smaf.c 30smaf.c
31au.c
31#if defined(HAVE_RECORDING) && !defined(SIMULATOR) 32#if defined(HAVE_RECORDING) && !defined(SIMULATOR)
32/* encoders */ 33/* encoders */
33aiff_enc.c 34aiff_enc.c
diff --git a/apps/codecs/au.c b/apps/codecs/au.c
new file mode 100644
index 0000000000..5632bf7a9b
--- /dev/null
+++ b/apps/codecs/au.c
@@ -0,0 +1,319 @@
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
25CODEC_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
37static int32_t samples[PCM_SAMPLE_SIZE] IBSS_ATTR;
38
39enum
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
48static int support_formats[28][2] = {
49 { AU_FORMAT_UNSUPPORT, 0 },
50 { AU_FORMAT_MULAW, 8 }, /* G.711 MULAW */
51 { AU_FORMAT_PCM, 8 }, /* Linear PCM 8bit (signed) */
52 { AU_FORMAT_PCM, 16 }, /* Linear PCM 16bit (signed, big endian) */
53 { AU_FORMAT_PCM, 24 }, /* Linear PCM 24bit (signed, big endian) */
54 { AU_FORMAT_PCM, 32 }, /* Linear PCM 32bit (signed, big endian) */
55 { AU_FORMAT_IEEE_FLOAT, 32 }, /* Linear PCM float 32bit (signed, big endian) */
56 { AU_FORMAT_IEEE_FLOAT, 64 }, /* Linear PCM float 64bit (signed, big endian) */
57 { AU_FORMAT_UNSUPPORT, 0 }, /* Fragmented sample data */
58 { AU_FORMAT_UNSUPPORT, 0 }, /* DSP program */
59 { AU_FORMAT_UNSUPPORT, 0 }, /* 8bit fixed point */
60 { AU_FORMAT_UNSUPPORT, 0 }, /* 16bit fixed point */
61 { AU_FORMAT_UNSUPPORT, 0 }, /* 24bit fixed point */
62 { AU_FORMAT_UNSUPPORT, 0 }, /* 32bit fixed point */
63 { AU_FORMAT_UNSUPPORT, 0 },
64 { AU_FORMAT_UNSUPPORT, 0 },
65 { AU_FORMAT_UNSUPPORT, 0 },
66 { AU_FORMAT_UNSUPPORT, 0 },
67 { AU_FORMAT_UNSUPPORT, 0 }, /* 16bit linear with emphasis */
68 { AU_FORMAT_UNSUPPORT, 0 }, /* 16bit linear compressed */
69 { AU_FORMAT_UNSUPPORT, 0 }, /* 16bit linear with emphasis and compression */
70 { AU_FORMAT_UNSUPPORT, 0 }, /* Music kit DSP commands */
71 { AU_FORMAT_UNSUPPORT, 0 },
72 { AU_FORMAT_UNSUPPORT, 0 }, /* G.721 MULAW */
73 { AU_FORMAT_UNSUPPORT, 0 }, /* G.722 */
74 { AU_FORMAT_UNSUPPORT, 0 }, /* G.723 3bit */
75 { AU_FORMAT_UNSUPPORT, 0 }, /* G.723 5bit */
76 { AU_FORMAT_ALAW, 8 }, /* G.711 ALAW */
77};
78
79const struct pcm_entry au_codecs[] = {
80 { AU_FORMAT_MULAW, get_itut_g711_mulaw_codec },
81 { AU_FORMAT_PCM, get_linear_pcm_codec },
82 { AU_FORMAT_IEEE_FLOAT, get_ieee_float_codec },
83 { AU_FORMAT_ALAW, get_itut_g711_alaw_codec },
84};
85
86#define NUM_FORMATS 4
87
88static const struct pcm_codec *get_au_codec(uint32_t formattag)
89{
90 int i;
91
92 for (i = 0; i < NUM_FORMATS; i++)
93 {
94 if (au_codecs[i].format_tag == formattag)
95 {
96 if (au_codecs[i].get_codec)
97 return au_codecs[i].get_codec();
98 return 0;
99 }
100 }
101 return 0;
102}
103
104static unsigned int get_be32(uint8_t *buf)
105{
106 return (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3];
107}
108
109static int convert_au_format(unsigned int encoding, struct pcm_format *fmt)
110{
111 if (encoding > 27)
112 {
113 fmt->formattag = AU_FORMAT_UNSUPPORT;
114 fmt->bitspersample = 0;
115 }
116 else
117 {
118 fmt->formattag = support_formats[encoding][0];
119 fmt->bitspersample = support_formats[encoding][1];
120 }
121
122 return fmt->formattag;
123}
124
125/* this is the codec entry point */
126enum codec_status codec_main(void)
127{
128 int status = CODEC_OK;
129 struct pcm_format format;
130 uint32_t bytesdone, decodedsamples;
131 size_t n;
132 int bufcount;
133 int endofstream;
134 unsigned char *buf;
135 uint8_t *aubuf;
136 off_t firstblockposn; /* position of the first block in file */
137 const struct pcm_codec *codec;
138 int offset = 0;
139
140 /* Generic codec initialisation */
141 ci->configure(DSP_SET_SAMPLE_DEPTH, 28);
142
143next_track:
144 if (codec_init()) {
145 DEBUGF("codec_init() error\n");
146 status = CODEC_ERROR;
147 goto exit;
148 }
149
150 while (!*ci->taginfo_ready && !ci->stop_codec)
151 ci->sleep(1);
152
153 codec_set_replaygain(ci->id3);
154
155 ci->memset(&format, 0, sizeof(struct pcm_format));
156 format.is_signed = true;
157 format.is_little_endian = false;
158
159 /* set format */
160 buf = ci->request_buffer(&n, 24);
161 if (n < 24 || (memcmp(buf, ".snd", 4) != 0))
162 {
163 /*
164 * headerless sun audio file
165 * It is decoded under conditions.
166 * format: G.711 mu-law
167 * channel: mono
168 * frequency: 8000 kHz
169 */
170 offset = 0;
171 format.formattag = AU_FORMAT_MULAW;
172 format.channels = 1;
173 format.bitspersample = 8;
174 format.numbytes = ci->id3->filesize;
175 }
176 else
177 {
178 /* parse header */
179
180 /* data offset */
181 offset = get_be32(buf + 4);
182 if (offset < 24)
183 {
184 DEBUGF("CODEC_ERROR: sun audio offset size is small: %d\n", offset);
185 status = CODEC_ERROR;
186 goto done;
187 }
188 /* data size */
189 format.numbytes = get_be32(buf + 8);
190 if (format.numbytes == (uint32_t)0xffffffff)
191 format.numbytes = ci->id3->filesize - offset;
192 /* encoding */
193 format.formattag = convert_au_format(get_be32(buf + 12), &format);
194 if (format.formattag == AU_FORMAT_UNSUPPORT)
195 {
196 DEBUGF("CODEC_ERROR: sun audio unsupport format: %d\n", get_be32(buf + 12));
197 status = CODEC_ERROR;
198 goto done;
199 }
200 /* skip sample rate */
201 format.channels = get_be32(buf + 20);
202 if (format.channels == 0) {
203 DEBUGF("CODEC_ERROR: sun audio 0-channels file\n");
204 status = CODEC_ERROR;
205 goto done;
206 }
207 }
208
209 /* advance to first WAVE chunk */
210 ci->advance_buffer(offset);
211
212 firstblockposn = offset;
213
214 decodedsamples = 0;
215 codec = 0;
216 bytesdone = 0;
217
218 /* blockalign = 1 sample */
219 format.blockalign = format.bitspersample * format.channels >> 3;
220
221 /* get codec */
222 codec = get_au_codec(format.formattag);
223 if (!codec)
224 {
225 DEBUGF("CODEC_ERROR: unsupport sun audio format: %lx\n", format.formattag);
226 status = CODEC_ERROR;
227 goto done;
228 }
229
230 if (!codec->set_format(&format))
231 {
232 status = CODEC_ERROR;
233 goto done;
234 }
235
236 if (format.numbytes == 0) {
237 DEBUGF("CODEC_ERROR: data size is 0\n");
238 status = CODEC_ERROR;
239 goto done;
240 }
241
242 /* check chunksize */
243 if ((format.chunksize / format.blockalign) * format.samplesperblock * format.channels
244 > PCM_SAMPLE_SIZE)
245 format.chunksize = (PCM_SAMPLE_SIZE / format.blockalign) * format.blockalign;
246 if (format.chunksize == 0)
247 {
248 DEBUGF("CODEC_ERROR: chunksize is 0\n");
249 status = CODEC_ERROR;
250 goto done;
251 }
252
253 ci->configure(DSP_SWITCH_FREQUENCY, ci->id3->frequency);
254 if (format.channels == 2) {
255 ci->configure(DSP_SET_STEREO_MODE, STEREO_INTERLEAVED);
256 } else if (format.channels == 1) {
257 ci->configure(DSP_SET_STEREO_MODE, STEREO_MONO);
258 } else {
259 DEBUGF("CODEC_ERROR: more than 2 channels\n");
260 status = CODEC_ERROR;
261 goto done;
262 }
263
264 /* The main decoder loop */
265 endofstream = 0;
266
267 while (!endofstream) {
268 ci->yield();
269 if (ci->stop_codec || ci->new_track) {
270 break;
271 }
272
273 if (ci->seek_time) {
274 /* 2nd args(read_buffer) is unnecessary in the format which Sun Audio supports. */
275 struct pcm_pos *newpos = codec->get_seek_pos(ci->seek_time, NULL);
276
277 decodedsamples = newpos->samples;
278 if (newpos->pos > format.numbytes)
279 break;
280 if (ci->seek_buffer(firstblockposn + newpos->pos))
281 {
282 bytesdone = newpos->pos;
283 }
284 ci->seek_complete();
285 }
286
287 aubuf = (uint8_t *)ci->request_buffer(&n, format.chunksize);
288 if (n == 0)
289 break; /* End of stream */
290 if (bytesdone + n > format.numbytes) {
291 n = format.numbytes - bytesdone;
292 endofstream = 1;
293 }
294
295 status = codec->decode(aubuf, n, samples, &bufcount);
296 if (status == CODEC_ERROR)
297 {
298 DEBUGF("codec error\n");
299 goto done;
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 status = CODEC_OK;
312
313done:
314 if (ci->request_next_track())
315 goto next_track;
316
317exit:
318 return status;
319}
diff --git a/apps/codecs/codecs.make b/apps/codecs/codecs.make
index 6a86517119..cf1a057d8c 100644
--- a/apps/codecs/codecs.make
+++ b/apps/codecs/codecs.make
@@ -91,6 +91,7 @@ $(CODECDIR)/atrac3_oma.codec : $(CODECDIR)/libatrac.a
91$(CODECDIR)/aiff.codec : $(CODECDIR)/libpcm.a 91$(CODECDIR)/aiff.codec : $(CODECDIR)/libpcm.a
92$(CODECDIR)/wav.codec : $(CODECDIR)/libpcm.a 92$(CODECDIR)/wav.codec : $(CODECDIR)/libpcm.a
93$(CODECDIR)/smaf.codec : $(CODECDIR)/libpcm.a 93$(CODECDIR)/smaf.codec : $(CODECDIR)/libpcm.a
94$(CODECDIR)/au.codec : $(CODECDIR)/libpcm.a
94 95
95$(CODECS): $(CODECLIB) # this must be last in codec dependency list 96$(CODECS): $(CODECLIB) # this must be last in codec dependency list
96 97
diff --git a/apps/filetypes.c b/apps/filetypes.c
index 17a1f0345f..bbb52c8418 100644
--- a/apps/filetypes.c
+++ b/apps/filetypes.c
@@ -102,6 +102,8 @@ static const struct filetype inbuilt_filetypes[] = {
102 { "aa3", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA }, 102 { "aa3", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
103 { "at3", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA }, 103 { "at3", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
104 { "mmf", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA }, 104 { "mmf", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
105 { "au", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
106 { "snd", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
105#endif 107#endif
106 { "m3u", FILE_ATTR_M3U, Icon_Playlist, LANG_PLAYLIST }, 108 { "m3u", FILE_ATTR_M3U, Icon_Playlist, LANG_PLAYLIST },
107 { "m3u8",FILE_ATTR_M3U, Icon_Playlist, LANG_PLAYLIST }, 109 { "m3u8",FILE_ATTR_M3U, Icon_Playlist, LANG_PLAYLIST },
diff --git a/apps/metadata.c b/apps/metadata.c
index 36e97268ea..fe25bbc074 100644
--- a/apps/metadata.c
+++ b/apps/metadata.c
@@ -168,6 +168,9 @@ const struct afmt_entry audio_formats[AFMT_NUM_CODECS] =
168 /* SMAF (Synthetic music Mobile Application Format) */ 168 /* SMAF (Synthetic music Mobile Application Format) */
169 [AFMT_SMAF] = 169 [AFMT_SMAF] =
170 AFMT_ENTRY("SMAF", "smaf", NULL, "mmf\0" ), 170 AFMT_ENTRY("SMAF", "smaf", NULL, "mmf\0" ),
171 /* Sun Audio file */
172 [AFMT_AU] =
173 AFMT_ENTRY("AU", "au", NULL, "au\0snd\0" ),
171#endif 174#endif
172}; 175};
173 176
@@ -458,6 +461,14 @@ bool get_metadata(struct mp3entry* id3, int fd, const char* trackname)
458 return false; 461 return false;
459 } 462 }
460 break; 463 break;
464
465 case AFMT_AU:
466 if (!get_au_metadata(fd, id3))
467 {
468 DEBUGF("get_au_metadata error\n");
469 return false;
470 }
471 break;
461 472
462#endif /* CONFIG_CODEC == SWCODEC */ 473#endif /* CONFIG_CODEC == SWCODEC */
463 474
diff --git a/apps/metadata.h b/apps/metadata.h
index 7f6843f6d9..f5ca99eeff 100644
--- a/apps/metadata.h
+++ b/apps/metadata.h
@@ -79,6 +79,7 @@ enum
79 AFMT_TM2, /* Atari 8bit tm2 format */ 79 AFMT_TM2, /* Atari 8bit tm2 format */
80 AFMT_OMA_ATRAC3, /* Atrac3 in Sony OMA container */ 80 AFMT_OMA_ATRAC3, /* Atrac3 in Sony OMA container */
81 AFMT_SMAF, /* SMAF */ 81 AFMT_SMAF, /* SMAF */
82 AFMT_AU, /* Sun Audio file */
82#endif 83#endif
83 84
84 /* add new formats at any index above this line to have a sensible order - 85 /* add new formats at any index above this line to have a sensible order -
diff --git a/apps/metadata/au.c b/apps/metadata/au.c
new file mode 100644
index 0000000000..1a7eef03c9
--- /dev/null
+++ b/apps/metadata/au.c
@@ -0,0 +1,122 @@
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#include <stdio.h>
22#include <string.h>
23#include <stdlib.h>
24#include <ctype.h>
25#include <inttypes.h>
26
27#include "system.h"
28#include "metadata.h"
29#include "metadata_common.h"
30#include "metadata_parsers.h"
31#include "logf.h"
32
33static int bitspersamples[28] = {
34 0,
35 8, /* G.711 MULAW */
36 8, /* 8bit */
37 16, /* 16bit */
38 24, /* 24bit */
39 32, /* 32bit */
40 32, /* 32bit */
41 64, /* 64bit */
42 0, /* Fragmented sample data */
43 0, /* DSP program */
44 0, /* 8bit fixed point */
45 0, /* 16bit fixed point */
46 0, /* 24bit fixed point */
47 0, /* 32bit fixed point */
48 0,
49 0,
50 0,
51 0,
52 0, /* 16bit linear with emphasis */
53 0, /* 16bit linear compressed */
54 0, /* 16bit linear with emphasis and compression */
55 0, /* Music kit DSP commands */
56 0,
57 0, /* G.721 MULAW */
58 0, /* G.722 */
59 0, /* G.723 3bit */
60 0, /* G.723 5bit */
61 8, /* G.711 ALAW */
62};
63
64static int get_au_bitspersample(unsigned int encoding)
65{
66 if (encoding > 27)
67 return 0;
68 return bitspersamples[encoding];
69}
70
71bool get_au_metadata(int fd, struct mp3entry* id3)
72{
73 /* Use the trackname part of the id3 structure as a temporary buffer */
74 unsigned char* buf = (unsigned char *)id3->path;
75 unsigned long totalsamples = 0;
76 unsigned long channels = 0;
77 unsigned long bitspersample = 0;
78 unsigned long numbytes = 0;
79 int read_bytes;
80 int offset;
81
82 id3->vbr = false; /* All Sun audio files are CBR */
83 id3->filesize = filesize(fd);
84
85 if ((lseek(fd, 0, SEEK_SET) < 0) || ((read_bytes = read(fd, buf, 24)) < 0))
86 return false;
87
88 if (read_bytes < 24 || (memcmp(buf, ".snd", 4) != 0))
89 {
90 /* no header */
91 numbytes = id3->filesize;
92 bitspersample = 8;
93 id3->frequency = 8000;
94 channels = 1;
95 }
96 else
97 {
98 /* data offset */
99 offset = get_long_be(buf + 4);
100 if (offset < 24)
101 {
102 DEBUGF("CODEC_ERROR: sun audio offset size is small: %d\n", offset);
103 return false;
104 }
105 /* data size */
106 numbytes = get_long_be(buf + 8);
107 if (numbytes == (uint32_t)0xffffffff)
108 numbytes = id3->filesize - offset;
109 /* bitspersample */
110 bitspersample = get_au_bitspersample(get_long_be(buf + 12));
111 /* sample rate */
112 id3->frequency = get_long_be(buf + 16);
113 channels = get_long_be(buf + 20);
114 }
115
116 totalsamples = numbytes / ((((bitspersample - 1) / 8) + 1) * channels);
117
118 /* Calculate track length (in ms) and estimate the bitrate (in kbit/s) */
119 id3->length = ((int64_t) totalsamples * 1000) / id3->frequency;
120
121 return true;
122}
diff --git a/apps/metadata/metadata_parsers.h b/apps/metadata/metadata_parsers.h
index 1e439c807b..392f16a8d9 100644
--- a/apps/metadata/metadata_parsers.h
+++ b/apps/metadata/metadata_parsers.h
@@ -43,3 +43,4 @@ bool get_rm_metadata(int fd, struct mp3entry* id3);
43bool get_nsf_metadata(int fd, struct mp3entry* id3); 43bool get_nsf_metadata(int fd, struct mp3entry* id3);
44bool get_oma_metadata(int fd, struct mp3entry* id3); 44bool get_oma_metadata(int fd, struct mp3entry* id3);
45bool get_smaf_metadata(int fd, struct mp3entry* id3); 45bool get_smaf_metadata(int fd, struct mp3entry* id3);
46bool get_au_metadata(int fd, struct mp3entry* id3);