summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorYoshihisa Uchida <uchida@rockbox.org>2010-02-24 11:46:29 +0000
committerYoshihisa Uchida <uchida@rockbox.org>2010-02-24 11:46:29 +0000
commit45e009a364d3ee105bf6b2ebb2e28445c9c6e3fd (patch)
tree7c0cff93773ebb8d88a4fc0fecd126fcbee46124
parentaa58715a54cb62638b263b7942c46e25ec928534 (diff)
downloadrockbox-45e009a364d3ee105bf6b2ebb2e28445c9c6e3fd.tar.gz
rockbox-45e009a364d3ee105bf6b2ebb2e28445c9c6e3fd.tar.bz2
rockbox-45e009a364d3ee105bf6b2ebb2e28445c9c6e3fd.zip
add SMAF codec (.mmf extension)(FS#10432)
This codec supports only wave data (ADPCM and PCM). It does not support MIDI, picture, and movie. git-svn-id: svn://svn.rockbox.org/rockbox/trunk@24878 a1c6a512-1295-4272-9138-f99709370657
-rw-r--r--apps/SOURCES1
-rw-r--r--apps/codecs/SOURCES1
-rw-r--r--apps/codecs/codecs.make1
-rw-r--r--apps/codecs/smaf.c435
-rw-r--r--apps/filetypes.c1
-rw-r--r--apps/metadata.c11
-rw-r--r--apps/metadata.h1
-rw-r--r--apps/metadata/metadata_parsers.h2
-rw-r--r--apps/metadata/smaf.c369
9 files changed, 821 insertions, 1 deletions
diff --git a/apps/SOURCES b/apps/SOURCES
index d86bb11fab..39159d639b 100644
--- a/apps/SOURCES
+++ b/apps/SOURCES
@@ -186,6 +186,7 @@ metadata/asap.c
metadata/rm.c
metadata/nsf.c
metadata/oma.c
+metadata/smaf.c
#endif
#ifdef HAVE_TAGCACHE
tagcache.c
diff --git a/apps/codecs/SOURCES b/apps/codecs/SOURCES
index 4c847c23e0..9787caa122 100644
--- a/apps/codecs/SOURCES
+++ b/apps/codecs/SOURCES
@@ -27,6 +27,7 @@ shorten.c
aiff.c
speex.c
adx.c
+smaf.c
#if defined(HAVE_RECORDING) && !defined(SIMULATOR)
/* encoders */
aiff_enc.c
diff --git a/apps/codecs/codecs.make b/apps/codecs/codecs.make
index 633f35b273..6a86517119 100644
--- a/apps/codecs/codecs.make
+++ b/apps/codecs/codecs.make
@@ -90,6 +90,7 @@ $(CODECDIR)/atrac3_rm.codec : $(CODECDIR)/libatrac.a $(CODECDIR)/librm.a
$(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
$(CODECS): $(CODECLIB) # this must be last in codec dependency list
diff --git a/apps/codecs/smaf.c b/apps/codecs/smaf.c
new file mode 100644
index 0000000000..227227ada8
--- /dev/null
+++ b/apps/codecs/smaf.c
@@ -0,0 +1,435 @@
+/***************************************************************************
+ * __________ __ ___.
+ * 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
+
+/*
+ * SMAF (Synthetic music Mobile Application Format)
+ *
+ * References
+ * [1] YAMAHA Corporation, Synthetic music Mobile Application Format Ver.3.05, 2002
+ */
+
+enum {
+ SMAF_TRACK_CHUNK_SCORE = 0, /* Score Track */
+ SMAF_TRACK_CHUNK_AUDIO, /* PCM Audio Track */
+};
+
+/* SMAF supported codec formats */
+enum {
+ SMAF_FORMAT_UNSUPPORT = 0, /* unsupported format */
+ SMAF_FORMAT_SIGNED_PCM, /* 2's complement PCM */
+ SMAF_FORMAT_UNSIGNED_PCM, /* Offset Binary PCM */
+ SMAF_FORMAT_ADPCM, /* YAMAHA ADPCM */
+};
+
+static int support_formats[2][3] = {
+ {SMAF_FORMAT_SIGNED_PCM, SMAF_FORMAT_UNSIGNED_PCM, SMAF_FORMAT_ADPCM },
+ {SMAF_FORMAT_SIGNED_PCM, SMAF_FORMAT_ADPCM, SMAF_FORMAT_UNSUPPORT },
+};
+
+static const struct pcm_entry pcm_codecs[] = {
+ { SMAF_FORMAT_SIGNED_PCM, get_linear_pcm_codec },
+ { SMAF_FORMAT_UNSIGNED_PCM, get_linear_pcm_codec },
+ { SMAF_FORMAT_ADPCM, get_yamaha_adpcm_codec },
+};
+
+#define NUM_FORMATS 3
+
+static int basebits[4] = { 4, 8, 12, 16 };
+
+#define PCM_SAMPLE_SIZE (2048*2)
+
+static int32_t samples[PCM_SAMPLE_SIZE] IBSS_ATTR;
+
+static const struct pcm_codec *get_codec(uint32_t formattag)
+{
+ int i;
+
+ for (i = 0; i < NUM_FORMATS; i++)
+ {
+ if (pcm_codecs[i].format_tag == formattag)
+ {
+ if (pcm_codecs[i].get_codec)
+ return pcm_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_smaf_audio_format(int track_chunk, unsigned int audio_format)
+{
+ if (audio_format > 3)
+ return SMAF_FORMAT_UNSUPPORT;
+
+ return support_formats[track_chunk][audio_format];
+}
+
+static int convert_smaf_audio_basebit(unsigned int basebit)
+{
+ if (basebit > 4)
+ return 0;
+ return basebits[basebit];
+}
+
+static bool parse_audio_track(struct pcm_format *fmt,
+ unsigned char **stbuf, unsigned char *endbuf)
+{
+ unsigned char *buf = *stbuf;
+ int chunksize;
+
+ buf += 8;
+ fmt->channels = ((buf[2] & 0x80) >> 7) + 1;
+ fmt->formattag = convert_smaf_audio_format(SMAF_TRACK_CHUNK_AUDIO,
+ (buf[2] >> 4) & 0x07);
+ if (fmt->formattag == SMAF_FORMAT_UNSUPPORT)
+ {
+ DEBUGF("CODEC_ERROR: unsupport pcm data format : %d\n", (buf[2] >> 4) & 0x07);
+ return false;
+ }
+ fmt->bitspersample = convert_smaf_audio_basebit(buf[3] >> 4);
+ if (fmt->bitspersample == 0)
+ {
+ DEBUGF("CODEC_ERROR: unsupport pcm data basebit : %d\n", buf[3] >> 4);
+ return false;
+ }
+ buf += 6;
+ while (buf < endbuf)
+ {
+ chunksize = get_be32(buf + 4) + 8;
+ if (memcmp(buf, "Awa", 3) == 0)
+ {
+ fmt->numbytes = get_be32(buf + 4);
+ buf += 8;
+ return true;
+ }
+ buf += chunksize;
+ }
+ DEBUGF("CODEC_ERROR: smaf does not include stream pcm data\n");
+ return false;
+}
+
+static bool parse_score_track(struct pcm_format *fmt,
+ unsigned char **stbuf, unsigned char *endbuf)
+{
+ unsigned char *buf = *stbuf;
+ int chunksize;
+
+ if (buf[9] != 0x00)
+ {
+ DEBUGF("CODEC_ERROR: score track chunk unsupport sequence type %d\n", buf[9]);
+ return false;
+ }
+
+ /*
+ * skip to the next chunk.
+ * MA-2/MA-3/MA-5: padding 16 bytes
+ * MA-7: padding 32 bytes
+ */
+ if (buf[3] < 7)
+ buf += 28;
+ else
+ buf += 44;
+
+ while (buf < endbuf)
+ {
+ chunksize = get_be32(buf + 4) + 8;
+ if (memcmp(buf, "Mtsp", 4) == 0)
+ {
+ buf += 8;
+ if (memcmp(buf, "Mwa", 3) != 0)
+ {
+ DEBUGF("CODEC_ERROR: smaf does not include stream pcm data\n");
+ return false;
+ }
+ fmt->numbytes = get_be32(buf + 4) - 3;
+ fmt->channels = ((buf[8] & 0x80) >> 7) + 1;
+ fmt->formattag = convert_smaf_audio_format(SMAF_TRACK_CHUNK_SCORE,
+ (buf[8] >> 4) & 0x07);
+ if (fmt->formattag == SMAF_FORMAT_UNSUPPORT)
+ {
+ DEBUGF("CODEC_ERROR: unsupport pcm data format : %d\n",
+ (buf[8] >> 4) & 0x07);
+ return false;
+ }
+ fmt->bitspersample = convert_smaf_audio_basebit(buf[8] & 0x0f);
+ if (fmt->bitspersample == 0)
+ {
+ DEBUGF("CODEC_ERROR: unsupport pcm data basebit : %d\n",
+ buf[8] & 0x0f);
+ return false;
+ }
+ buf += 11;
+ return true;
+ }
+ buf += chunksize;
+ }
+
+ DEBUGF("CODEC_ERROR: smaf does not include stream pcm data\n");
+ return false;
+}
+
+static bool parse_header(struct pcm_format *fmt, size_t *pos)
+{
+ unsigned char *buf, *stbuf, *endbuf;
+ size_t chunksize;
+
+ ci->memset(fmt, 0, sizeof(struct pcm_format));
+
+ /* assume the SMAF pcm data position is less than 1024 bytes */
+ stbuf = ci->request_buffer(&chunksize, 1024);
+ if (chunksize < 1024)
+ return false;
+
+ buf = stbuf;
+ endbuf = stbuf + chunksize;
+
+ if (memcmp(buf, "MMMD", 4) != 0)
+ {
+ DEBUGF("CODEC_ERROR: does not smaf format %c%c%c%c\n",
+ buf[0], buf[1], buf[2], buf[3]);
+ return false;
+ }
+ buf += 8;
+
+ while (buf < endbuf)
+ {
+ chunksize = get_be32(buf + 4) + 8;
+ if (memcmp(buf, "ATR", 3) == 0)
+ {
+ if (!parse_audio_track(fmt, &buf, endbuf))
+ return false;
+ break;
+ }
+ if (memcmp(buf, "MTR", 3) == 0)
+ {
+ if (!parse_score_track(fmt, &buf, endbuf))
+ return false;
+ break;
+ }
+ buf += chunksize;
+ }
+
+ if (buf >= endbuf)
+ {
+ DEBUGF("CODEC_ERROR: unsupported smaf format\n");
+ return false;
+ }
+
+ /* blockalign */
+ if (fmt->formattag == SMAF_FORMAT_SIGNED_PCM ||
+ fmt->formattag == SMAF_FORMAT_UNSIGNED_PCM)
+ fmt->blockalign = fmt->channels * fmt->bitspersample >> 3;
+
+ /* data signess (default signed) */
+ fmt->is_signed = (fmt->formattag != SMAF_FORMAT_UNSIGNED_PCM);
+
+ fmt->is_little_endian = false;
+
+ /* sets pcm data position */
+ *pos = buf - stbuf;
+
+ return true;
+}
+
+static struct pcm_format format;
+static uint32_t bytesdone;
+
+static uint8_t *read_buffer(size_t *realsize)
+{
+ uint8_t *buffer = (uint8_t *)ci->request_buffer(realsize, format.chunksize);
+ if (bytesdone + (*realsize) > format.numbytes)
+ *realsize = format.numbytes - bytesdone;
+ bytesdone += *realsize;
+ ci->advance_buffer(*realsize);
+ return buffer;
+}
+
+enum codec_status codec_main(void)
+{
+ int status = CODEC_OK;
+ uint32_t decodedsamples;
+ uint32_t i = CODEC_OK;
+ size_t n;
+ int bufcount;
+ int endofstream;
+ uint8_t *smafbuf;
+ off_t firstblockposn; /* position of the first block in file */
+ const struct pcm_codec *codec;
+
+ /* Generic codec initialisation */
+ ci->configure(DSP_SET_SAMPLE_DEPTH, 28);
+
+next_track:
+ if (codec_init()) {
+ i = 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;
+
+ decodedsamples = 0;
+ codec = 0;
+
+ if (!parse_header(&format, &n))
+ {
+ i = CODEC_ERROR;
+ goto done;
+ }
+
+ codec = get_codec(format.formattag);
+ if (codec == 0)
+ {
+ DEBUGF("CODEC_ERROR: unsupport audio format: 0x%lx\n", format.formattag);
+ i = CODEC_ERROR;
+ goto done;
+ }
+
+ if (!codec->set_format(&format))
+ {
+ i = CODEC_ERROR;
+ goto done;
+ }
+
+ /* common format check */
+ if (format.channels == 0) {
+ DEBUGF("CODEC_ERROR: 'fmt ' chunk not found or 0-channels file\n");
+ status = CODEC_ERROR;
+ goto done;
+ }
+ if (format.samplesperblock == 0) {
+ DEBUGF("CODEC_ERROR: 'fmt ' chunk not found or 0-wSamplesPerBlock file\n");
+ status = CODEC_ERROR;
+ goto done;
+ }
+ if (format.blockalign == 0)
+ {
+ DEBUGF("CODEC_ERROR: 'fmt ' chunk not found or 0-blockalign file\n");
+ i = CODEC_ERROR;
+ goto done;
+ }
+ if (format.numbytes == 0) {
+ DEBUGF("CODEC_ERROR: 'data' chunk not found or has zero-length\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");
+ i = 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 unsupported\n");
+ i = CODEC_ERROR;
+ goto done;
+ }
+
+ firstblockposn = 1024 - n;
+ ci->advance_buffer(firstblockposn);
+
+ /* The main decoder loop */
+ bytesdone = 0;
+ ci->set_elapsed(0);
+ endofstream = 0;
+
+ while (!endofstream) {
+ ci->yield();
+ if (ci->stop_codec || ci->new_track)
+ break;
+
+ if (ci->seek_time) {
+ struct pcm_pos *newpos = codec->get_seek_pos(ci->seek_time, &read_buffer);
+
+ decodedsamples = newpos->samples;
+ if (newpos->pos > format.numbytes)
+ break;
+ if (ci->seek_buffer(firstblockposn + newpos->pos))
+ {
+ bytesdone = newpos->pos;
+ }
+ ci->seek_complete();
+ }
+ smafbuf = (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(smafbuf, 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);
+ }
+ i = CODEC_OK;
+
+done:
+ if (ci->request_next_track())
+ goto next_track;
+
+exit:
+ return i;
+}
+
diff --git a/apps/filetypes.c b/apps/filetypes.c
index 49ae4afde2..17a1f0345f 100644
--- a/apps/filetypes.c
+++ b/apps/filetypes.c
@@ -101,6 +101,7 @@ static const struct filetype inbuilt_filetypes[] = {
{ "oma", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
{ "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 },
#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 ce3a4ec22a..36e97268ea 100644
--- a/apps/metadata.c
+++ b/apps/metadata.c
@@ -165,6 +165,9 @@ const struct afmt_entry audio_formats[AFMT_NUM_CODECS] =
/* Atrac3 in Sony OMA Container */
[AFMT_OMA_ATRAC3] =
AFMT_ENTRY("ATRAC3", "atrac3_oma", NULL, "oma\0aa3\0" ),
+ /* SMAF (Synthetic music Mobile Application Format) */
+ [AFMT_SMAF] =
+ AFMT_ENTRY("SMAF", "smaf", NULL, "mmf\0" ),
#endif
};
@@ -447,6 +450,14 @@ bool get_metadata(struct mp3entry* id3, int fd, const char* trackname)
return false;
}
break;
+
+ case AFMT_SMAF:
+ if (!get_smaf_metadata(fd, id3))
+ {
+ DEBUGF("get_smaf_metadata error\n");
+ return false;
+ }
+ break;
#endif /* CONFIG_CODEC == SWCODEC */
diff --git a/apps/metadata.h b/apps/metadata.h
index bd9cd27eca..7f6843f6d9 100644
--- a/apps/metadata.h
+++ b/apps/metadata.h
@@ -78,6 +78,7 @@ enum
AFMT_TM8, /* Atari 8bit tm8 format */
AFMT_TM2, /* Atari 8bit tm2 format */
AFMT_OMA_ATRAC3, /* Atrac3 in Sony OMA container */
+ AFMT_SMAF, /* SMAF */
#endif
/* add new formats at any index above this line to have a sensible order -
diff --git a/apps/metadata/metadata_parsers.h b/apps/metadata/metadata_parsers.h
index aa07101269..1e439c807b 100644
--- a/apps/metadata/metadata_parsers.h
+++ b/apps/metadata/metadata_parsers.h
@@ -42,4 +42,4 @@ bool get_asap_metadata(int fd, struct mp3entry* id3);
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);
diff --git a/apps/metadata/smaf.c b/apps/metadata/smaf.c
new file mode 100644
index 0000000000..f7d825c96a
--- /dev/null
+++ b/apps/metadata/smaf.c
@@ -0,0 +1,369 @@
+/***************************************************************************
+ * __________ __ ___.
+ * 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 <stdio.h>
+#include <string.h>
+#include <stdlib.h>
+#include <ctype.h>
+#include <inttypes.h>
+
+#include "system.h"
+#include "metadata.h"
+#include "metadata_common.h"
+#include "metadata_parsers.h"
+#include "rbunicode.h"
+#include "logf.h"
+
+static int basebits[4] = { 4, 8, 12, 16 };
+
+static int frequency[5] = { 4000, 8000, 11025, 22050, 44100 };
+
+static int support_codepages[7] = {
+ SJIS, ISO_8859_1, -1, GB_2312, BIG_5, -1, -1,
+};
+
+/* extra codepage */
+#define UCS_2 (NUM_CODEPAGES + 1)
+#define UTF_16 (NUM_CODEPAGES + 2)
+
+/* support id3 tag */
+#define TAG_TITLE (('S'<<8)|'T')
+#define TAG_ARTIST (('A'<<8)|'N')
+#define TAG_COMPOSER (('S'<<8)|'W')
+
+static unsigned char smafbuf[1024];
+
+static bool read_datachunk(unsigned char *src, int size, unsigned short tag,
+ int codepage, unsigned char *dst)
+{
+ int datasize = 0;
+ unsigned char *utf8;
+
+ while(size > datasize + 4)
+ {
+ datasize = (src[2] << 8) | src[3];
+ if (tag == ((src[0] << 8) | src[1]))
+ {
+ src += 4;
+ if (codepage < NUM_CODEPAGES)
+ utf8 = iso_decode(src, dst, codepage, datasize);
+ else /* codepage == UTF_16, UCS_2 */
+ utf8 = utf16BEdecode(src, dst, datasize);
+ *utf8 = '\0';
+
+ return true;
+ }
+ src += (datasize + 4);
+ }
+ return false;
+}
+
+static bool read_option(unsigned char *src, int size, unsigned short tag,
+ int codepage, unsigned char *dst)
+{
+ int datasize = 0;
+ unsigned char *endsrc = src + size;
+ unsigned char *utf8;
+
+ while(src < endsrc)
+ {
+ utf8 = src;
+ src += 3;
+ datasize = 0;
+ while (*src != ',' || *(src-1) == '\\')
+ {
+ datasize++;
+ src++;
+ }
+ if (tag == ((utf8[0] << 8) | utf8[1]) && utf8[2] == ':')
+ {
+ utf8 += 3;
+ if (codepage < NUM_CODEPAGES)
+ utf8 = iso_decode(utf8, dst, codepage, datasize);
+ else /* codepage == UTF_16, UCS_2 */
+ utf8 = utf16BEdecode(utf8, dst, datasize);
+ *utf8 = '\0';
+
+ return true;
+ }
+ src++;
+ }
+ return false;
+}
+
+static int convert_smaf_audio_frequency(unsigned int freq)
+{
+ if (freq > 4)
+ return 0;
+ return frequency[freq];
+}
+
+static int convert_smaf_audio_basebit(unsigned int basebit)
+{
+ if (basebit > 4)
+ return 0;
+ return basebits[basebit];
+}
+
+static int convert_smaf_codetype(unsigned int codetype)
+{
+ if (codetype < 7)
+ return support_codepages[codetype];
+ else if (codetype < 0x20)
+ return -1;
+ else if (codetype == 0x20)
+ return UCS_2;
+ else if (codetype == 0x23)
+ return UTF_8;
+ else if (codetype == 0x24)
+ return UTF_16;
+ else if (codetype == 0xff)
+ return ISO_8859_1;
+ return -1;
+}
+
+static bool get_smaf_metadata_audio_track(struct mp3entry *id3,
+ unsigned char* buf, unsigned char *endbuf)
+{
+ int bitspersample;
+ int channels;
+ int chunksize;
+ long numbytes;
+ unsigned long totalsamples;
+
+ channels = ((buf[10] & 0x80) >> 7) + 1;
+ bitspersample = convert_smaf_audio_basebit(buf[11] >> 4);
+ if (bitspersample == 0)
+ {
+ DEBUGF("metada error: smaf unsupport basebit %d\n", buf[11] >> 4);
+ return false;
+ }
+ id3->frequency = convert_smaf_audio_frequency(buf[10] & 0x0f);
+
+ buf += 14;
+ while (buf < endbuf)
+ {
+ chunksize = get_long_be(buf + 4) + 8;
+ if (memcmp(buf, "Awa", 3) == 0)
+ {
+ numbytes = get_long_be(buf + 4) - 3;
+ totalsamples = (numbytes << 3) / (bitspersample * channels);
+
+ /* Calculate track length (in ms) and estimate the bitrate (in kbit/s) */
+ id3->length = ((int64_t)totalsamples * 1000LL) / id3->frequency;
+
+ return true;
+ }
+ buf += chunksize;
+ }
+ DEBUGF("metada error: smaf does not include pcm audio data\n");
+ return false;
+}
+
+static bool get_smaf_metadata_score_track(struct mp3entry *id3,
+ unsigned char* buf, unsigned char *endbuf)
+{
+ int bitspersample;
+ int channels;
+ int chunksize;
+ long numbytes;
+ unsigned long totalsamples;
+
+ /*
+ * skip to the next chunk.
+ * MA-2/MA-3/MA-5: padding 16 bytes
+ * MA-7: padding 32 bytes
+ */
+ if (buf[3] < 7)
+ buf += 28;
+ else
+ buf += 44;
+
+ while (buf + 10 < endbuf)
+ {
+ chunksize = get_long_be(buf + 4) + 8;
+ if (memcmp(buf, "Mtsp", 4) == 0)
+ {
+ buf += 8;
+ if (memcmp(buf, "Mwa", 3) != 0)
+ {
+ DEBUGF("metada error: smaf unsupport format: %c%c%c%c\n",
+ buf[0], buf[1], buf[2], buf[3]);
+ return false;
+ }
+
+ channels = ((buf[8] & 0x80) >> 7) + 1;
+ bitspersample = convert_smaf_audio_basebit(buf[8] & 0x0f);
+ if (bitspersample == 0)
+ {
+ DEBUGF("metada error: smaf unsupport basebit %d\n", buf[8] & 0x0f);
+ return false;
+ }
+
+ numbytes = get_long_be(buf + 4) - 3;
+ totalsamples = numbytes * 8 / (bitspersample * channels);
+
+ id3->frequency = (buf[9] << 8) | buf[10];
+
+ /* Calculate track length (in ms) and estimate the bitrate (in kbit/s) */
+ id3->length = ((int64_t) totalsamples * 1000) / id3->frequency;
+
+ return true;
+ }
+ buf += chunksize;
+ }
+ DEBUGF("metada error: smaf does not include pcm audio data\n");
+ return false;
+}
+
+bool get_smaf_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 char *endbuf = smafbuf + 1024;
+ int i;
+ int contents_size;
+ int codepage = ISO_8859_1;
+
+ id3->title = NULL;
+ id3->artist = NULL;
+ id3->composer = NULL;
+
+ id3->vbr = false; /* All SMAF files are CBR */
+ id3->filesize = filesize(fd);
+
+ /* get RIFF chunk header */
+ if ((lseek(fd, 0, SEEK_SET) < 0) || (read(fd, buf, 21) < 21))
+ {
+ return false;
+ }
+
+ if ((memcmp(buf, "MMMD", 4) != 0) || (memcmp(&buf[8], "CNTI", 4) != 0))
+ {
+ DEBUGF("metada error: does not smaf format\n");
+ return false;
+ }
+
+ contents_size = get_long_be(buf + 12);
+ if (contents_size < 5)
+ {
+ DEBUGF("metada error: CNTI chunk size is small %d\n", contents_size);
+ return false;
+ }
+
+ contents_size -= 5;
+ i = contents_size;
+ if (i == 0)
+ {
+ read(fd, buf, 16);
+ if (memcmp(buf, "OPDA", 4) != 0)
+ {
+ DEBUGF("metada error: smaf does not include OPDA chunk\n");
+ return false;
+ }
+ contents_size = get_long_be(buf + 4) - 8;
+
+ if (memcmp(buf + 8, "Dch", 3) != 0)
+ {
+ DEBUGF("metada error: smaf does not include Dch chunk\n");
+ return false;
+ }
+ codepage = convert_smaf_codetype(buf[11]);
+ if (codepage < 0)
+ {
+ DEBUGF("metada error: smaf unsupport codetype: %d\n", buf[11]);
+ return false;
+ }
+
+ i = get_long_be(buf + 12);
+
+ if (i > MAX_PATH)
+ {
+ DEBUGF("metada warning: smaf contents size is big %d\n", i);
+ i = MAX_PATH;
+ }
+ if (read(fd, buf, i) < i)
+ return false;
+
+ /* title */
+ if (read_datachunk(buf, i, TAG_TITLE, codepage, id3->id3v1buf[0]))
+ id3->title = id3->id3v1buf[0];
+
+ /* artist */
+ if (read_datachunk(buf, i, TAG_ARTIST, codepage, id3->id3v1buf[1]))
+ id3->artist = id3->id3v1buf[1];
+
+ /* composer */
+ if (read_datachunk(buf, i, TAG_COMPOSER, codepage, id3->id3v1buf[2]))
+ id3->composer = id3->id3v1buf[2];
+ }
+ else
+ {
+ codepage = convert_smaf_codetype(buf[14]);
+ if (codepage < 0)
+ {
+ DEBUGF("metada error: smaf unsupport codetype: %d\n", buf[11]);
+ return false;
+ }
+
+ if (i > MAX_PATH)
+ {
+ DEBUGF("metada warning: smaf contents size is big %d\n", i);
+ i = MAX_PATH;
+ }
+ if (read(fd, buf, i) < i)
+ return false;
+
+ /* title */
+ if (read_option(buf, i, TAG_TITLE, codepage, id3->id3v1buf[0]))
+ id3->title = id3->id3v1buf[0];
+
+ /* artist */
+ if (read_option(buf, i, TAG_ARTIST, codepage, id3->id3v1buf[1]))
+ id3->artist = id3->id3v1buf[1];
+
+ /* composer */
+ if (read_option(buf, i, TAG_COMPOSER, codepage, id3->id3v1buf[2]))
+ id3->composer = id3->id3v1buf[2];
+ }
+
+ if (contents_size > i)
+ lseek(fd, contents_size - i, SEEK_CUR);
+
+ /* assume the SMAF pcm data position is less than 1024 bytes */
+ if (read(fd, smafbuf, 1024) < 1024)
+ return false;
+
+ buf = smafbuf;
+ while (buf + 8 < endbuf)
+ {
+ i = get_long_be(buf + 4) + 8;
+
+ if (memcmp(buf, "ATR", 3) == 0)
+ return get_smaf_metadata_audio_track(id3, buf, endbuf);
+ else if (memcmp(buf, "MTR", 3) == 0)
+ return get_smaf_metadata_score_track(id3, buf, endbuf);
+
+ buf += i;
+ }
+
+ DEBUGF("metada error: smaf does not include track chunk\n");
+ return false;
+}