From 4e3c8074664fa87b023643f375171d544d662141 Mon Sep 17 00:00:00 2001 From: Yoshihisa Uchida Date: Sun, 28 Feb 2010 07:22:20 +0000 Subject: [PATCH] Add Sun Audio codec. (FS#10433) git-svn-id: svn://svn.rockbox.org/rockbox/trunk@24955 a1c6a512-1295-4272-9138-f99709370657 --- apps/SOURCES | 1 + apps/codecs/SOURCES | 1 + apps/codecs/au.c | 319 +++++++++++++++++++++++++++++++ apps/codecs/codecs.make | 1 + apps/filetypes.c | 2 + apps/metadata.c | 11 ++ apps/metadata.h | 1 + apps/metadata/au.c | 122 ++++++++++++ apps/metadata/metadata_parsers.h | 1 + 9 files changed, 459 insertions(+) create mode 100644 apps/codecs/au.c create mode 100644 apps/metadata/au.c 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 metadata/nsf.c metadata/oma.c metadata/smaf.c +metadata/au.c #endif #ifdef HAVE_TAGCACHE tagcache.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 speex.c adx.c smaf.c +au.c #if defined(HAVE_RECORDING) && !defined(SIMULATOR) /* encoders */ aiff_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 @@ +/*************************************************************************** + * __________ __ ___. + * Open \______ \ ____ ____ | | _\_ |__ _______ ___ + * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / + * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < + * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ + * \/ \/ \/ \/ \/ + * $Id$ + * + * Copyright (C) 2010 Yoshihisa Uchida + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY + * KIND, either express or implied. + * + ****************************************************************************/ + +#include "codeclib.h" +#include "codecs/libpcm/support_formats.h" + +CODEC_HEADER + +/* Sun Audio file (Au file format) codec + * + * References + * [1] Sun Microsystems, Inc., Header file for Audio, .au, 1992 + * URL http://www.opengroup.org/public/pubs/external/auformat.html + * [2] Wikipedia, Au file format, URL: http://en.wikipedia.org/wiki/Sun_Audio + */ + +#define PCM_SAMPLE_SIZE (1024*2) + +static int32_t samples[PCM_SAMPLE_SIZE] IBSS_ATTR; + +enum +{ + AU_FORMAT_UNSUPPORT = 0, /* unsupported format */ + AU_FORMAT_MULAW, /* G.711 MULAW */ + AU_FORMAT_PCM, /* Linear PCM */ + AU_FORMAT_IEEE_FLOAT, /* IEEE float */ + AU_FORMAT_ALAW, /* G.711 ALAW */ +}; + +static int support_formats[28][2] = { + { AU_FORMAT_UNSUPPORT, 0 }, + { AU_FORMAT_MULAW, 8 }, /* G.711 MULAW */ + { AU_FORMAT_PCM, 8 }, /* Linear PCM 8bit (signed) */ + { AU_FORMAT_PCM, 16 }, /* Linear PCM 16bit (signed, big endian) */ + { AU_FORMAT_PCM, 24 }, /* Linear PCM 24bit (signed, big endian) */ + { AU_FORMAT_PCM, 32 }, /* Linear PCM 32bit (signed, big endian) */ + { AU_FORMAT_IEEE_FLOAT, 32 }, /* Linear PCM float 32bit (signed, big endian) */ + { AU_FORMAT_IEEE_FLOAT, 64 }, /* Linear PCM float 64bit (signed, big endian) */ + { AU_FORMAT_UNSUPPORT, 0 }, /* Fragmented sample data */ + { AU_FORMAT_UNSUPPORT, 0 }, /* DSP program */ + { AU_FORMAT_UNSUPPORT, 0 }, /* 8bit fixed point */ + { AU_FORMAT_UNSUPPORT, 0 }, /* 16bit fixed point */ + { AU_FORMAT_UNSUPPORT, 0 }, /* 24bit fixed point */ + { AU_FORMAT_UNSUPPORT, 0 }, /* 32bit fixed point */ + { AU_FORMAT_UNSUPPORT, 0 }, + { AU_FORMAT_UNSUPPORT, 0 }, + { AU_FORMAT_UNSUPPORT, 0 }, + { AU_FORMAT_UNSUPPORT, 0 }, + { AU_FORMAT_UNSUPPORT, 0 }, /* 16bit linear with emphasis */ + { AU_FORMAT_UNSUPPORT, 0 }, /* 16bit linear compressed */ + { AU_FORMAT_UNSUPPORT, 0 }, /* 16bit linear with emphasis and compression */ + { AU_FORMAT_UNSUPPORT, 0 }, /* Music kit DSP commands */ + { AU_FORMAT_UNSUPPORT, 0 }, + { AU_FORMAT_UNSUPPORT, 0 }, /* G.721 MULAW */ + { AU_FORMAT_UNSUPPORT, 0 }, /* G.722 */ + { AU_FORMAT_UNSUPPORT, 0 }, /* G.723 3bit */ + { AU_FORMAT_UNSUPPORT, 0 }, /* G.723 5bit */ + { AU_FORMAT_ALAW, 8 }, /* G.711 ALAW */ +}; + +const struct pcm_entry au_codecs[] = { + { AU_FORMAT_MULAW, get_itut_g711_mulaw_codec }, + { AU_FORMAT_PCM, get_linear_pcm_codec }, + { AU_FORMAT_IEEE_FLOAT, get_ieee_float_codec }, + { AU_FORMAT_ALAW, get_itut_g711_alaw_codec }, +}; + +#define NUM_FORMATS 4 + +static const struct pcm_codec *get_au_codec(uint32_t formattag) +{ + int i; + + for (i = 0; i < NUM_FORMATS; i++) + { + if (au_codecs[i].format_tag == formattag) + { + if (au_codecs[i].get_codec) + return au_codecs[i].get_codec(); + return 0; + } + } + return 0; +} + +static unsigned int get_be32(uint8_t *buf) +{ + return (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3]; +} + +static int convert_au_format(unsigned int encoding, struct pcm_format *fmt) +{ + if (encoding > 27) + { + fmt->formattag = AU_FORMAT_UNSUPPORT; + fmt->bitspersample = 0; + } + else + { + fmt->formattag = support_formats[encoding][0]; + fmt->bitspersample = support_formats[encoding][1]; + } + + return fmt->formattag; +} + +/* this is the codec entry point */ +enum codec_status codec_main(void) +{ + int status = CODEC_OK; + struct pcm_format format; + uint32_t bytesdone, decodedsamples; + size_t n; + int bufcount; + int endofstream; + unsigned char *buf; + uint8_t *aubuf; + off_t firstblockposn; /* position of the first block in file */ + const struct pcm_codec *codec; + int offset = 0; + + /* Generic codec initialisation */ + ci->configure(DSP_SET_SAMPLE_DEPTH, 28); + +next_track: + if (codec_init()) { + DEBUGF("codec_init() error\n"); + status = CODEC_ERROR; + goto exit; + } + + while (!*ci->taginfo_ready && !ci->stop_codec) + ci->sleep(1); + + codec_set_replaygain(ci->id3); + + ci->memset(&format, 0, sizeof(struct pcm_format)); + format.is_signed = true; + format.is_little_endian = false; + + /* set format */ + buf = ci->request_buffer(&n, 24); + if (n < 24 || (memcmp(buf, ".snd", 4) != 0)) + { + /* + * headerless sun audio file + * It is decoded under conditions. + * format: G.711 mu-law + * channel: mono + * frequency: 8000 kHz + */ + offset = 0; + format.formattag = AU_FORMAT_MULAW; + format.channels = 1; + format.bitspersample = 8; + format.numbytes = ci->id3->filesize; + } + else + { + /* parse header */ + + /* data offset */ + offset = get_be32(buf + 4); + if (offset < 24) + { + DEBUGF("CODEC_ERROR: sun audio offset size is small: %d\n", offset); + status = CODEC_ERROR; + goto done; + } + /* data size */ + format.numbytes = get_be32(buf + 8); + if (format.numbytes == (uint32_t)0xffffffff) + format.numbytes = ci->id3->filesize - offset; + /* encoding */ + format.formattag = convert_au_format(get_be32(buf + 12), &format); + if (format.formattag == AU_FORMAT_UNSUPPORT) + { + DEBUGF("CODEC_ERROR: sun audio unsupport format: %d\n", get_be32(buf + 12)); + status = CODEC_ERROR; + goto done; + } + /* skip sample rate */ + format.channels = get_be32(buf + 20); + if (format.channels == 0) { + DEBUGF("CODEC_ERROR: sun audio 0-channels file\n"); + status = CODEC_ERROR; + goto done; + } + } + + /* advance to first WAVE chunk */ + ci->advance_buffer(offset); + + firstblockposn = offset; + + decodedsamples = 0; + codec = 0; + bytesdone = 0; + + /* blockalign = 1 sample */ + format.blockalign = format.bitspersample * format.channels >> 3; + + /* get codec */ + codec = get_au_codec(format.formattag); + if (!codec) + { + DEBUGF("CODEC_ERROR: unsupport sun audio format: %lx\n", format.formattag); + status = CODEC_ERROR; + goto done; + } + + if (!codec->set_format(&format)) + { + status = CODEC_ERROR; + goto done; + } + + if (format.numbytes == 0) { + DEBUGF("CODEC_ERROR: data size is 0\n"); + status = CODEC_ERROR; + goto done; + } + + /* check chunksize */ + if ((format.chunksize / format.blockalign) * format.samplesperblock * format.channels + > PCM_SAMPLE_SIZE) + format.chunksize = (PCM_SAMPLE_SIZE / format.blockalign) * format.blockalign; + if (format.chunksize == 0) + { + DEBUGF("CODEC_ERROR: chunksize is 0\n"); + status = CODEC_ERROR; + goto done; + } + + ci->configure(DSP_SWITCH_FREQUENCY, ci->id3->frequency); + if (format.channels == 2) { + ci->configure(DSP_SET_STEREO_MODE, STEREO_INTERLEAVED); + } else if (format.channels == 1) { + ci->configure(DSP_SET_STEREO_MODE, STEREO_MONO); + } else { + DEBUGF("CODEC_ERROR: more than 2 channels\n"); + status = CODEC_ERROR; + goto done; + } + + /* The main decoder loop */ + endofstream = 0; + + while (!endofstream) { + ci->yield(); + if (ci->stop_codec || ci->new_track) { + break; + } + + if (ci->seek_time) { + /* 2nd args(read_buffer) is unnecessary in the format which Sun Audio supports. */ + struct pcm_pos *newpos = codec->get_seek_pos(ci->seek_time, NULL); + + decodedsamples = newpos->samples; + if (newpos->pos > format.numbytes) + break; + if (ci->seek_buffer(firstblockposn + newpos->pos)) + { + bytesdone = newpos->pos; + } + ci->seek_complete(); + } + + aubuf = (uint8_t *)ci->request_buffer(&n, format.chunksize); + if (n == 0) + break; /* End of stream */ + if (bytesdone + n > format.numbytes) { + n = format.numbytes - bytesdone; + endofstream = 1; + } + + status = codec->decode(aubuf, n, samples, &bufcount); + if (status == CODEC_ERROR) + { + DEBUGF("codec error\n"); + goto done; + } + + ci->pcmbuf_insert(samples, NULL, bufcount); + ci->advance_buffer(n); + bytesdone += n; + decodedsamples += bufcount; + + if (bytesdone >= format.numbytes) + endofstream = 1; + ci->set_elapsed(decodedsamples*1000LL/ci->id3->frequency); + } + status = CODEC_OK; + +done: + if (ci->request_next_track()) + goto next_track; + +exit: + return status; +} 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 $(CODECDIR)/aiff.codec : $(CODECDIR)/libpcm.a $(CODECDIR)/wav.codec : $(CODECDIR)/libpcm.a $(CODECDIR)/smaf.codec : $(CODECDIR)/libpcm.a +$(CODECDIR)/au.codec : $(CODECDIR)/libpcm.a $(CODECS): $(CODECLIB) # this must be last in codec dependency list 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[] = { { "aa3", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA }, { "at3", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA }, { "mmf", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA }, + { "au", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA }, + { "snd", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA }, #endif { "m3u", FILE_ATTR_M3U, Icon_Playlist, LANG_PLAYLIST }, { "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] = /* SMAF (Synthetic music Mobile Application Format) */ [AFMT_SMAF] = AFMT_ENTRY("SMAF", "smaf", NULL, "mmf\0" ), + /* Sun Audio file */ + [AFMT_AU] = + AFMT_ENTRY("AU", "au", NULL, "au\0snd\0" ), #endif }; @@ -458,6 +461,14 @@ bool get_metadata(struct mp3entry* id3, int fd, const char* trackname) return false; } break; + + case AFMT_AU: + if (!get_au_metadata(fd, id3)) + { + DEBUGF("get_au_metadata error\n"); + return false; + } + break; #endif /* CONFIG_CODEC == SWCODEC */ 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 AFMT_TM2, /* Atari 8bit tm2 format */ AFMT_OMA_ATRAC3, /* Atrac3 in Sony OMA container */ AFMT_SMAF, /* SMAF */ + AFMT_AU, /* Sun Audio file */ #endif /* 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 @@ +/*************************************************************************** + * __________ __ ___. + * Open \______ \ ____ ____ | | _\_ |__ _______ ___ + * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / + * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < + * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ + * \/ \/ \/ \/ \/ + * $Id$ + * + * Copyright (C) 2010 Yoshihisa Uchida + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY + * KIND, either express or implied. + * + ****************************************************************************/ +#include +#include +#include +#include +#include + +#include "system.h" +#include "metadata.h" +#include "metadata_common.h" +#include "metadata_parsers.h" +#include "logf.h" + +static int bitspersamples[28] = { + 0, + 8, /* G.711 MULAW */ + 8, /* 8bit */ + 16, /* 16bit */ + 24, /* 24bit */ + 32, /* 32bit */ + 32, /* 32bit */ + 64, /* 64bit */ + 0, /* Fragmented sample data */ + 0, /* DSP program */ + 0, /* 8bit fixed point */ + 0, /* 16bit fixed point */ + 0, /* 24bit fixed point */ + 0, /* 32bit fixed point */ + 0, + 0, + 0, + 0, + 0, /* 16bit linear with emphasis */ + 0, /* 16bit linear compressed */ + 0, /* 16bit linear with emphasis and compression */ + 0, /* Music kit DSP commands */ + 0, + 0, /* G.721 MULAW */ + 0, /* G.722 */ + 0, /* G.723 3bit */ + 0, /* G.723 5bit */ + 8, /* G.711 ALAW */ +}; + +static int get_au_bitspersample(unsigned int encoding) +{ + if (encoding > 27) + return 0; + return bitspersamples[encoding]; +} + +bool get_au_metadata(int fd, struct mp3entry* id3) +{ + /* Use the trackname part of the id3 structure as a temporary buffer */ + unsigned char* buf = (unsigned char *)id3->path; + unsigned long totalsamples = 0; + unsigned long channels = 0; + unsigned long bitspersample = 0; + unsigned long numbytes = 0; + int read_bytes; + int offset; + + id3->vbr = false; /* All Sun audio files are CBR */ + id3->filesize = filesize(fd); + + if ((lseek(fd, 0, SEEK_SET) < 0) || ((read_bytes = read(fd, buf, 24)) < 0)) + return false; + + if (read_bytes < 24 || (memcmp(buf, ".snd", 4) != 0)) + { + /* no header */ + numbytes = id3->filesize; + bitspersample = 8; + id3->frequency = 8000; + channels = 1; + } + else + { + /* data offset */ + offset = get_long_be(buf + 4); + if (offset < 24) + { + DEBUGF("CODEC_ERROR: sun audio offset size is small: %d\n", offset); + return false; + } + /* data size */ + numbytes = get_long_be(buf + 8); + if (numbytes == (uint32_t)0xffffffff) + numbytes = id3->filesize - offset; + /* bitspersample */ + bitspersample = get_au_bitspersample(get_long_be(buf + 12)); + /* sample rate */ + id3->frequency = get_long_be(buf + 16); + channels = get_long_be(buf + 20); + } + + totalsamples = numbytes / ((((bitspersample - 1) / 8) + 1) * channels); + + /* Calculate track length (in ms) and estimate the bitrate (in kbit/s) */ + id3->length = ((int64_t) totalsamples * 1000) / id3->frequency; + + return true; +} 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); bool get_nsf_metadata(int fd, struct mp3entry* id3); bool get_oma_metadata(int fd, struct mp3entry* id3); bool get_smaf_metadata(int fd, struct mp3entry* id3); +bool get_au_metadata(int fd, struct mp3entry* id3);