diff options
-rw-r--r-- | apps/SOURCES | 1 | ||||
-rw-r--r-- | apps/codecs/SOURCES | 1 | ||||
-rw-r--r-- | apps/codecs/codecs.make | 1 | ||||
-rw-r--r-- | apps/codecs/smaf.c | 435 | ||||
-rw-r--r-- | apps/filetypes.c | 1 | ||||
-rw-r--r-- | apps/metadata.c | 11 | ||||
-rw-r--r-- | apps/metadata.h | 1 | ||||
-rw-r--r-- | apps/metadata/metadata_parsers.h | 2 | ||||
-rw-r--r-- | apps/metadata/smaf.c | 369 |
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 | |||
186 | metadata/rm.c | 186 | metadata/rm.c |
187 | metadata/nsf.c | 187 | metadata/nsf.c |
188 | metadata/oma.c | 188 | metadata/oma.c |
189 | metadata/smaf.c | ||
189 | #endif | 190 | #endif |
190 | #ifdef HAVE_TAGCACHE | 191 | #ifdef HAVE_TAGCACHE |
191 | tagcache.c | 192 | 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 | |||
27 | aiff.c | 27 | aiff.c |
28 | speex.c | 28 | speex.c |
29 | adx.c | 29 | adx.c |
30 | smaf.c | ||
30 | #if defined(HAVE_RECORDING) && !defined(SIMULATOR) | 31 | #if defined(HAVE_RECORDING) && !defined(SIMULATOR) |
31 | /* encoders */ | 32 | /* encoders */ |
32 | aiff_enc.c | 33 | 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 | |||
90 | $(CODECDIR)/atrac3_oma.codec : $(CODECDIR)/libatrac.a | 90 | $(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 | 94 | ||
94 | $(CODECS): $(CODECLIB) # this must be last in codec dependency list | 95 | $(CODECS): $(CODECLIB) # this must be last in codec dependency list |
95 | 96 | ||
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 @@ | |||
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 | /* | ||
28 | * SMAF (Synthetic music Mobile Application Format) | ||
29 | * | ||
30 | * References | ||
31 | * [1] YAMAHA Corporation, Synthetic music Mobile Application Format Ver.3.05, 2002 | ||
32 | */ | ||
33 | |||
34 | enum { | ||
35 | SMAF_TRACK_CHUNK_SCORE = 0, /* Score Track */ | ||
36 | SMAF_TRACK_CHUNK_AUDIO, /* PCM Audio Track */ | ||
37 | }; | ||
38 | |||
39 | /* SMAF supported codec formats */ | ||
40 | enum { | ||
41 | SMAF_FORMAT_UNSUPPORT = 0, /* unsupported format */ | ||
42 | SMAF_FORMAT_SIGNED_PCM, /* 2's complement PCM */ | ||
43 | SMAF_FORMAT_UNSIGNED_PCM, /* Offset Binary PCM */ | ||
44 | SMAF_FORMAT_ADPCM, /* YAMAHA ADPCM */ | ||
45 | }; | ||
46 | |||
47 | static int support_formats[2][3] = { | ||
48 | {SMAF_FORMAT_SIGNED_PCM, SMAF_FORMAT_UNSIGNED_PCM, SMAF_FORMAT_ADPCM }, | ||
49 | {SMAF_FORMAT_SIGNED_PCM, SMAF_FORMAT_ADPCM, SMAF_FORMAT_UNSUPPORT }, | ||
50 | }; | ||
51 | |||
52 | static const struct pcm_entry pcm_codecs[] = { | ||
53 | { SMAF_FORMAT_SIGNED_PCM, get_linear_pcm_codec }, | ||
54 | { SMAF_FORMAT_UNSIGNED_PCM, get_linear_pcm_codec }, | ||
55 | { SMAF_FORMAT_ADPCM, get_yamaha_adpcm_codec }, | ||
56 | }; | ||
57 | |||
58 | #define NUM_FORMATS 3 | ||
59 | |||
60 | static int basebits[4] = { 4, 8, 12, 16 }; | ||
61 | |||
62 | #define PCM_SAMPLE_SIZE (2048*2) | ||
63 | |||
64 | static int32_t samples[PCM_SAMPLE_SIZE] IBSS_ATTR; | ||
65 | |||
66 | static const struct pcm_codec *get_codec(uint32_t formattag) | ||
67 | { | ||
68 | int i; | ||
69 | |||
70 | for (i = 0; i < NUM_FORMATS; i++) | ||
71 | { | ||
72 | if (pcm_codecs[i].format_tag == formattag) | ||
73 | { | ||
74 | if (pcm_codecs[i].get_codec) | ||
75 | return pcm_codecs[i].get_codec(); | ||
76 | return 0; | ||
77 | } | ||
78 | } | ||
79 | return 0; | ||
80 | } | ||
81 | |||
82 | static unsigned int get_be32(uint8_t *buf) | ||
83 | { | ||
84 | return (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3]; | ||
85 | } | ||
86 | |||
87 | static int convert_smaf_audio_format(int track_chunk, unsigned int audio_format) | ||
88 | { | ||
89 | if (audio_format > 3) | ||
90 | return SMAF_FORMAT_UNSUPPORT; | ||
91 | |||
92 | return support_formats[track_chunk][audio_format]; | ||
93 | } | ||
94 | |||
95 | static int convert_smaf_audio_basebit(unsigned int basebit) | ||
96 | { | ||
97 | if (basebit > 4) | ||
98 | return 0; | ||
99 | return basebits[basebit]; | ||
100 | } | ||
101 | |||
102 | static bool parse_audio_track(struct pcm_format *fmt, | ||
103 | unsigned char **stbuf, unsigned char *endbuf) | ||
104 | { | ||
105 | unsigned char *buf = *stbuf; | ||
106 | int chunksize; | ||
107 | |||
108 | buf += 8; | ||
109 | fmt->channels = ((buf[2] & 0x80) >> 7) + 1; | ||
110 | fmt->formattag = convert_smaf_audio_format(SMAF_TRACK_CHUNK_AUDIO, | ||
111 | (buf[2] >> 4) & 0x07); | ||
112 | if (fmt->formattag == SMAF_FORMAT_UNSUPPORT) | ||
113 | { | ||
114 | DEBUGF("CODEC_ERROR: unsupport pcm data format : %d\n", (buf[2] >> 4) & 0x07); | ||
115 | return false; | ||
116 | } | ||
117 | fmt->bitspersample = convert_smaf_audio_basebit(buf[3] >> 4); | ||
118 | if (fmt->bitspersample == 0) | ||
119 | { | ||
120 | DEBUGF("CODEC_ERROR: unsupport pcm data basebit : %d\n", buf[3] >> 4); | ||
121 | return false; | ||
122 | } | ||
123 | buf += 6; | ||
124 | while (buf < endbuf) | ||
125 | { | ||
126 | chunksize = get_be32(buf + 4) + 8; | ||
127 | if (memcmp(buf, "Awa", 3) == 0) | ||
128 | { | ||
129 | fmt->numbytes = get_be32(buf + 4); | ||
130 | buf += 8; | ||
131 | return true; | ||
132 | } | ||
133 | buf += chunksize; | ||
134 | } | ||
135 | DEBUGF("CODEC_ERROR: smaf does not include stream pcm data\n"); | ||
136 | return false; | ||
137 | } | ||
138 | |||
139 | static bool parse_score_track(struct pcm_format *fmt, | ||
140 | unsigned char **stbuf, unsigned char *endbuf) | ||
141 | { | ||
142 | unsigned char *buf = *stbuf; | ||
143 | int chunksize; | ||
144 | |||
145 | if (buf[9] != 0x00) | ||
146 | { | ||
147 | DEBUGF("CODEC_ERROR: score track chunk unsupport sequence type %d\n", buf[9]); | ||
148 | return false; | ||
149 | } | ||
150 | |||
151 | /* | ||
152 | * skip to the next chunk. | ||
153 | * MA-2/MA-3/MA-5: padding 16 bytes | ||
154 | * MA-7: padding 32 bytes | ||
155 | */ | ||
156 | if (buf[3] < 7) | ||
157 | buf += 28; | ||
158 | else | ||
159 | buf += 44; | ||
160 | |||
161 | while (buf < endbuf) | ||
162 | { | ||
163 | chunksize = get_be32(buf + 4) + 8; | ||
164 | if (memcmp(buf, "Mtsp", 4) == 0) | ||
165 | { | ||
166 | buf += 8; | ||
167 | if (memcmp(buf, "Mwa", 3) != 0) | ||
168 | { | ||
169 | DEBUGF("CODEC_ERROR: smaf does not include stream pcm data\n"); | ||
170 | return false; | ||
171 | } | ||
172 | fmt->numbytes = get_be32(buf + 4) - 3; | ||
173 | fmt->channels = ((buf[8] & 0x80) >> 7) + 1; | ||
174 | fmt->formattag = convert_smaf_audio_format(SMAF_TRACK_CHUNK_SCORE, | ||
175 | (buf[8] >> 4) & 0x07); | ||
176 | if (fmt->formattag == SMAF_FORMAT_UNSUPPORT) | ||
177 | { | ||
178 | DEBUGF("CODEC_ERROR: unsupport pcm data format : %d\n", | ||
179 | (buf[8] >> 4) & 0x07); | ||
180 | return false; | ||
181 | } | ||
182 | fmt->bitspersample = convert_smaf_audio_basebit(buf[8] & 0x0f); | ||
183 | if (fmt->bitspersample == 0) | ||
184 | { | ||
185 | DEBUGF("CODEC_ERROR: unsupport pcm data basebit : %d\n", | ||
186 | buf[8] & 0x0f); | ||
187 | return false; | ||
188 | } | ||
189 | buf += 11; | ||
190 | return true; | ||
191 | } | ||
192 | buf += chunksize; | ||
193 | } | ||
194 | |||
195 | DEBUGF("CODEC_ERROR: smaf does not include stream pcm data\n"); | ||
196 | return false; | ||
197 | } | ||
198 | |||
199 | static bool parse_header(struct pcm_format *fmt, size_t *pos) | ||
200 | { | ||
201 | unsigned char *buf, *stbuf, *endbuf; | ||
202 | size_t chunksize; | ||
203 | |||
204 | ci->memset(fmt, 0, sizeof(struct pcm_format)); | ||
205 | |||
206 | /* assume the SMAF pcm data position is less than 1024 bytes */ | ||
207 | stbuf = ci->request_buffer(&chunksize, 1024); | ||
208 | if (chunksize < 1024) | ||
209 | return false; | ||
210 | |||
211 | buf = stbuf; | ||
212 | endbuf = stbuf + chunksize; | ||
213 | |||
214 | if (memcmp(buf, "MMMD", 4) != 0) | ||
215 | { | ||
216 | DEBUGF("CODEC_ERROR: does not smaf format %c%c%c%c\n", | ||
217 | buf[0], buf[1], buf[2], buf[3]); | ||
218 | return false; | ||
219 | } | ||
220 | buf += 8; | ||
221 | |||
222 | while (buf < endbuf) | ||
223 | { | ||
224 | chunksize = get_be32(buf + 4) + 8; | ||
225 | if (memcmp(buf, "ATR", 3) == 0) | ||
226 | { | ||
227 | if (!parse_audio_track(fmt, &buf, endbuf)) | ||
228 | return false; | ||
229 | break; | ||
230 | } | ||
231 | if (memcmp(buf, "MTR", 3) == 0) | ||
232 | { | ||
233 | if (!parse_score_track(fmt, &buf, endbuf)) | ||
234 | return false; | ||
235 | break; | ||
236 | } | ||
237 | buf += chunksize; | ||
238 | } | ||
239 | |||
240 | if (buf >= endbuf) | ||
241 | { | ||
242 | DEBUGF("CODEC_ERROR: unsupported smaf format\n"); | ||
243 | return false; | ||
244 | } | ||
245 | |||
246 | /* blockalign */ | ||
247 | if (fmt->formattag == SMAF_FORMAT_SIGNED_PCM || | ||
248 | fmt->formattag == SMAF_FORMAT_UNSIGNED_PCM) | ||
249 | fmt->blockalign = fmt->channels * fmt->bitspersample >> 3; | ||
250 | |||
251 | /* data signess (default signed) */ | ||
252 | fmt->is_signed = (fmt->formattag != SMAF_FORMAT_UNSIGNED_PCM); | ||
253 | |||
254 | fmt->is_little_endian = false; | ||
255 | |||
256 | /* sets pcm data position */ | ||
257 | *pos = buf - stbuf; | ||
258 | |||
259 | return true; | ||
260 | } | ||
261 | |||
262 | static struct pcm_format format; | ||
263 | static uint32_t bytesdone; | ||
264 | |||
265 | static uint8_t *read_buffer(size_t *realsize) | ||
266 | { | ||
267 | uint8_t *buffer = (uint8_t *)ci->request_buffer(realsize, format.chunksize); | ||
268 | if (bytesdone + (*realsize) > format.numbytes) | ||
269 | *realsize = format.numbytes - bytesdone; | ||
270 | bytesdone += *realsize; | ||
271 | ci->advance_buffer(*realsize); | ||
272 | return buffer; | ||
273 | } | ||
274 | |||
275 | enum codec_status codec_main(void) | ||
276 | { | ||
277 | int status = CODEC_OK; | ||
278 | uint32_t decodedsamples; | ||
279 | uint32_t i = CODEC_OK; | ||
280 | size_t n; | ||
281 | int bufcount; | ||
282 | int endofstream; | ||
283 | uint8_t *smafbuf; | ||
284 | off_t firstblockposn; /* position of the first block in file */ | ||
285 | const struct pcm_codec *codec; | ||
286 | |||
287 | /* Generic codec initialisation */ | ||
288 | ci->configure(DSP_SET_SAMPLE_DEPTH, 28); | ||
289 | |||
290 | next_track: | ||
291 | if (codec_init()) { | ||
292 | i = CODEC_ERROR; | ||
293 | goto exit; | ||
294 | } | ||
295 | |||
296 | while (!*ci->taginfo_ready && !ci->stop_codec) | ||
297 | ci->sleep(1); | ||
298 | |||
299 | codec_set_replaygain(ci->id3); | ||
300 | |||
301 | ci->memset(&format, 0, sizeof(struct pcm_format)); | ||
302 | format.is_signed = true; | ||
303 | format.is_little_endian = false; | ||
304 | |||
305 | decodedsamples = 0; | ||
306 | codec = 0; | ||
307 | |||
308 | if (!parse_header(&format, &n)) | ||
309 | { | ||
310 | i = CODEC_ERROR; | ||
311 | goto done; | ||
312 | } | ||
313 | |||
314 | codec = get_codec(format.formattag); | ||
315 | if (codec == 0) | ||
316 | { | ||
317 | DEBUGF("CODEC_ERROR: unsupport audio format: 0x%lx\n", format.formattag); | ||
318 | i = CODEC_ERROR; | ||
319 | goto done; | ||
320 | } | ||
321 | |||
322 | if (!codec->set_format(&format)) | ||
323 | { | ||
324 | i = CODEC_ERROR; | ||
325 | goto done; | ||
326 | } | ||
327 | |||
328 | /* common format check */ | ||
329 | if (format.channels == 0) { | ||
330 | DEBUGF("CODEC_ERROR: 'fmt ' chunk not found or 0-channels file\n"); | ||
331 | status = CODEC_ERROR; | ||
332 | goto done; | ||
333 | } | ||
334 | if (format.samplesperblock == 0) { | ||
335 | DEBUGF("CODEC_ERROR: 'fmt ' chunk not found or 0-wSamplesPerBlock file\n"); | ||
336 | status = CODEC_ERROR; | ||
337 | goto done; | ||
338 | } | ||
339 | if (format.blockalign == 0) | ||
340 | { | ||
341 | DEBUGF("CODEC_ERROR: 'fmt ' chunk not found or 0-blockalign file\n"); | ||
342 | i = CODEC_ERROR; | ||
343 | goto done; | ||
344 | } | ||
345 | if (format.numbytes == 0) { | ||
346 | DEBUGF("CODEC_ERROR: 'data' chunk not found or has zero-length\n"); | ||
347 | status = CODEC_ERROR; | ||
348 | goto done; | ||
349 | } | ||
350 | |||
351 | /* check chunksize */ | ||
352 | if ((format.chunksize / format.blockalign) * format.samplesperblock * format.channels | ||
353 | > PCM_SAMPLE_SIZE) | ||
354 | format.chunksize = (PCM_SAMPLE_SIZE / format.blockalign) * format.blockalign; | ||
355 | if (format.chunksize == 0) | ||
356 | { | ||
357 | DEBUGF("CODEC_ERROR: chunksize is 0\n"); | ||
358 | i = CODEC_ERROR; | ||
359 | goto done; | ||
360 | } | ||
361 | |||
362 | ci->configure(DSP_SWITCH_FREQUENCY, ci->id3->frequency); | ||
363 | |||
364 | if (format.channels == 2) { | ||
365 | ci->configure(DSP_SET_STEREO_MODE, STEREO_INTERLEAVED); | ||
366 | } else if (format.channels == 1) { | ||
367 | ci->configure(DSP_SET_STEREO_MODE, STEREO_MONO); | ||
368 | } else { | ||
369 | DEBUGF("CODEC_ERROR: more than 2 channels unsupported\n"); | ||
370 | i = CODEC_ERROR; | ||
371 | goto done; | ||
372 | } | ||
373 | |||
374 | firstblockposn = 1024 - n; | ||
375 | ci->advance_buffer(firstblockposn); | ||
376 | |||
377 | /* The main decoder loop */ | ||
378 | bytesdone = 0; | ||
379 | ci->set_elapsed(0); | ||
380 | endofstream = 0; | ||
381 | |||
382 | while (!endofstream) { | ||
383 | ci->yield(); | ||
384 | if (ci->stop_codec || ci->new_track) | ||
385 | break; | ||
386 | |||
387 | if (ci->seek_time) { | ||
388 | struct pcm_pos *newpos = codec->get_seek_pos(ci->seek_time, &read_buffer); | ||
389 | |||
390 | decodedsamples = newpos->samples; | ||
391 | if (newpos->pos > format.numbytes) | ||
392 | break; | ||
393 | if (ci->seek_buffer(firstblockposn + newpos->pos)) | ||
394 | { | ||
395 | bytesdone = newpos->pos; | ||
396 | } | ||
397 | ci->seek_complete(); | ||
398 | } | ||
399 | smafbuf = (uint8_t *)ci->request_buffer(&n, format.chunksize); | ||
400 | |||
401 | if (n == 0) | ||
402 | break; /* End of stream */ | ||
403 | |||
404 | if (bytesdone + n > format.numbytes) { | ||
405 | n = format.numbytes - bytesdone; | ||
406 | endofstream = 1; | ||
407 | } | ||
408 | |||
409 | status = codec->decode(smafbuf, n, samples, &bufcount); | ||
410 | if (status == CODEC_ERROR) | ||
411 | { | ||
412 | DEBUGF("codec error\n"); | ||
413 | goto done; | ||
414 | } | ||
415 | |||
416 | ci->pcmbuf_insert(samples, NULL, bufcount); | ||
417 | |||
418 | ci->advance_buffer(n); | ||
419 | bytesdone += n; | ||
420 | decodedsamples += bufcount; | ||
421 | if (bytesdone >= format.numbytes) | ||
422 | endofstream = 1; | ||
423 | |||
424 | ci->set_elapsed(decodedsamples*1000LL/ci->id3->frequency); | ||
425 | } | ||
426 | i = CODEC_OK; | ||
427 | |||
428 | done: | ||
429 | if (ci->request_next_track()) | ||
430 | goto next_track; | ||
431 | |||
432 | exit: | ||
433 | return i; | ||
434 | } | ||
435 | |||
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[] = { | |||
101 | { "oma", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA }, | 101 | { "oma", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA }, |
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 | #endif | 105 | #endif |
105 | { "m3u", FILE_ATTR_M3U, Icon_Playlist, LANG_PLAYLIST }, | 106 | { "m3u", FILE_ATTR_M3U, Icon_Playlist, LANG_PLAYLIST }, |
106 | { "m3u8",FILE_ATTR_M3U, Icon_Playlist, LANG_PLAYLIST }, | 107 | { "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] = | |||
165 | /* Atrac3 in Sony OMA Container */ | 165 | /* Atrac3 in Sony OMA Container */ |
166 | [AFMT_OMA_ATRAC3] = | 166 | [AFMT_OMA_ATRAC3] = |
167 | AFMT_ENTRY("ATRAC3", "atrac3_oma", NULL, "oma\0aa3\0" ), | 167 | AFMT_ENTRY("ATRAC3", "atrac3_oma", NULL, "oma\0aa3\0" ), |
168 | /* SMAF (Synthetic music Mobile Application Format) */ | ||
169 | [AFMT_SMAF] = | ||
170 | AFMT_ENTRY("SMAF", "smaf", NULL, "mmf\0" ), | ||
168 | #endif | 171 | #endif |
169 | }; | 172 | }; |
170 | 173 | ||
@@ -447,6 +450,14 @@ bool get_metadata(struct mp3entry* id3, int fd, const char* trackname) | |||
447 | return false; | 450 | return false; |
448 | } | 451 | } |
449 | break; | 452 | break; |
453 | |||
454 | case AFMT_SMAF: | ||
455 | if (!get_smaf_metadata(fd, id3)) | ||
456 | { | ||
457 | DEBUGF("get_smaf_metadata error\n"); | ||
458 | return false; | ||
459 | } | ||
460 | break; | ||
450 | 461 | ||
451 | #endif /* CONFIG_CODEC == SWCODEC */ | 462 | #endif /* CONFIG_CODEC == SWCODEC */ |
452 | 463 | ||
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 | |||
78 | AFMT_TM8, /* Atari 8bit tm8 format */ | 78 | AFMT_TM8, /* Atari 8bit tm8 format */ |
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 | #endif | 82 | #endif |
82 | 83 | ||
83 | /* add new formats at any index above this line to have a sensible order - | 84 | /* 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); | |||
42 | bool get_rm_metadata(int fd, struct mp3entry* id3); | 42 | bool get_rm_metadata(int fd, struct mp3entry* id3); |
43 | bool get_nsf_metadata(int fd, struct mp3entry* id3); | 43 | bool get_nsf_metadata(int fd, struct mp3entry* id3); |
44 | bool get_oma_metadata(int fd, struct mp3entry* id3); | 44 | bool get_oma_metadata(int fd, struct mp3entry* id3); |
45 | 45 | 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 @@ | |||
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 "rbunicode.h" | ||
32 | #include "logf.h" | ||
33 | |||
34 | static int basebits[4] = { 4, 8, 12, 16 }; | ||
35 | |||
36 | static int frequency[5] = { 4000, 8000, 11025, 22050, 44100 }; | ||
37 | |||
38 | static int support_codepages[7] = { | ||
39 | SJIS, ISO_8859_1, -1, GB_2312, BIG_5, -1, -1, | ||
40 | }; | ||
41 | |||
42 | /* extra codepage */ | ||
43 | #define UCS_2 (NUM_CODEPAGES + 1) | ||
44 | #define UTF_16 (NUM_CODEPAGES + 2) | ||
45 | |||
46 | /* support id3 tag */ | ||
47 | #define TAG_TITLE (('S'<<8)|'T') | ||
48 | #define TAG_ARTIST (('A'<<8)|'N') | ||
49 | #define TAG_COMPOSER (('S'<<8)|'W') | ||
50 | |||
51 | static unsigned char smafbuf[1024]; | ||
52 | |||
53 | static bool read_datachunk(unsigned char *src, int size, unsigned short tag, | ||
54 | int codepage, unsigned char *dst) | ||
55 | { | ||
56 | int datasize = 0; | ||
57 | unsigned char *utf8; | ||
58 | |||
59 | while(size > datasize + 4) | ||
60 | { | ||
61 | datasize = (src[2] << 8) | src[3]; | ||
62 | if (tag == ((src[0] << 8) | src[1])) | ||
63 | { | ||
64 | src += 4; | ||
65 | if (codepage < NUM_CODEPAGES) | ||
66 | utf8 = iso_decode(src, dst, codepage, datasize); | ||
67 | else /* codepage == UTF_16, UCS_2 */ | ||
68 | utf8 = utf16BEdecode(src, dst, datasize); | ||
69 | *utf8 = '\0'; | ||
70 | |||
71 | return true; | ||
72 | } | ||
73 | src += (datasize + 4); | ||
74 | } | ||
75 | return false; | ||
76 | } | ||
77 | |||
78 | static bool read_option(unsigned char *src, int size, unsigned short tag, | ||
79 | int codepage, unsigned char *dst) | ||
80 | { | ||
81 | int datasize = 0; | ||
82 | unsigned char *endsrc = src + size; | ||
83 | unsigned char *utf8; | ||
84 | |||
85 | while(src < endsrc) | ||
86 | { | ||
87 | utf8 = src; | ||
88 | src += 3; | ||
89 | datasize = 0; | ||
90 | while (*src != ',' || *(src-1) == '\\') | ||
91 | { | ||
92 | datasize++; | ||
93 | src++; | ||
94 | } | ||
95 | if (tag == ((utf8[0] << 8) | utf8[1]) && utf8[2] == ':') | ||
96 | { | ||
97 | utf8 += 3; | ||
98 | if (codepage < NUM_CODEPAGES) | ||
99 | utf8 = iso_decode(utf8, dst, codepage, datasize); | ||
100 | else /* codepage == UTF_16, UCS_2 */ | ||
101 | utf8 = utf16BEdecode(utf8, dst, datasize); | ||
102 | *utf8 = '\0'; | ||
103 | |||
104 | return true; | ||
105 | } | ||
106 | src++; | ||
107 | } | ||
108 | return false; | ||
109 | } | ||
110 | |||
111 | static int convert_smaf_audio_frequency(unsigned int freq) | ||
112 | { | ||
113 | if (freq > 4) | ||
114 | return 0; | ||
115 | return frequency[freq]; | ||
116 | } | ||
117 | |||
118 | static int convert_smaf_audio_basebit(unsigned int basebit) | ||
119 | { | ||
120 | if (basebit > 4) | ||
121 | return 0; | ||
122 | return basebits[basebit]; | ||
123 | } | ||
124 | |||
125 | static int convert_smaf_codetype(unsigned int codetype) | ||
126 | { | ||
127 | if (codetype < 7) | ||
128 | return support_codepages[codetype]; | ||
129 | else if (codetype < 0x20) | ||
130 | return -1; | ||
131 | else if (codetype == 0x20) | ||
132 | return UCS_2; | ||
133 | else if (codetype == 0x23) | ||
134 | return UTF_8; | ||
135 | else if (codetype == 0x24) | ||
136 | return UTF_16; | ||
137 | else if (codetype == 0xff) | ||
138 | return ISO_8859_1; | ||
139 | return -1; | ||
140 | } | ||
141 | |||
142 | static bool get_smaf_metadata_audio_track(struct mp3entry *id3, | ||
143 | unsigned char* buf, unsigned char *endbuf) | ||
144 | { | ||
145 | int bitspersample; | ||
146 | int channels; | ||
147 | int chunksize; | ||
148 | long numbytes; | ||
149 | unsigned long totalsamples; | ||
150 | |||
151 | channels = ((buf[10] & 0x80) >> 7) + 1; | ||
152 | bitspersample = convert_smaf_audio_basebit(buf[11] >> 4); | ||
153 | if (bitspersample == 0) | ||
154 | { | ||
155 | DEBUGF("metada error: smaf unsupport basebit %d\n", buf[11] >> 4); | ||
156 | return false; | ||
157 | } | ||
158 | id3->frequency = convert_smaf_audio_frequency(buf[10] & 0x0f); | ||
159 | |||
160 | buf += 14; | ||
161 | while (buf < endbuf) | ||
162 | { | ||
163 | chunksize = get_long_be(buf + 4) + 8; | ||
164 | if (memcmp(buf, "Awa", 3) == 0) | ||
165 | { | ||
166 | numbytes = get_long_be(buf + 4) - 3; | ||
167 | totalsamples = (numbytes << 3) / (bitspersample * channels); | ||
168 | |||
169 | /* Calculate track length (in ms) and estimate the bitrate (in kbit/s) */ | ||
170 | id3->length = ((int64_t)totalsamples * 1000LL) / id3->frequency; | ||
171 | |||
172 | return true; | ||
173 | } | ||
174 | buf += chunksize; | ||
175 | } | ||
176 | DEBUGF("metada error: smaf does not include pcm audio data\n"); | ||
177 | return false; | ||
178 | } | ||
179 | |||
180 | static bool get_smaf_metadata_score_track(struct mp3entry *id3, | ||
181 | unsigned char* buf, unsigned char *endbuf) | ||
182 | { | ||
183 | int bitspersample; | ||
184 | int channels; | ||
185 | int chunksize; | ||
186 | long numbytes; | ||
187 | unsigned long totalsamples; | ||
188 | |||
189 | /* | ||
190 | * skip to the next chunk. | ||
191 | * MA-2/MA-3/MA-5: padding 16 bytes | ||
192 | * MA-7: padding 32 bytes | ||
193 | */ | ||
194 | if (buf[3] < 7) | ||
195 | buf += 28; | ||
196 | else | ||
197 | buf += 44; | ||
198 | |||
199 | while (buf + 10 < endbuf) | ||
200 | { | ||
201 | chunksize = get_long_be(buf + 4) + 8; | ||
202 | if (memcmp(buf, "Mtsp", 4) == 0) | ||
203 | { | ||
204 | buf += 8; | ||
205 | if (memcmp(buf, "Mwa", 3) != 0) | ||
206 | { | ||
207 | DEBUGF("metada error: smaf unsupport format: %c%c%c%c\n", | ||
208 | buf[0], buf[1], buf[2], buf[3]); | ||
209 | return false; | ||
210 | } | ||
211 | |||
212 | channels = ((buf[8] & 0x80) >> 7) + 1; | ||
213 | bitspersample = convert_smaf_audio_basebit(buf[8] & 0x0f); | ||
214 | if (bitspersample == 0) | ||
215 | { | ||
216 | DEBUGF("metada error: smaf unsupport basebit %d\n", buf[8] & 0x0f); | ||
217 | return false; | ||
218 | } | ||
219 | |||
220 | numbytes = get_long_be(buf + 4) - 3; | ||
221 | totalsamples = numbytes * 8 / (bitspersample * channels); | ||
222 | |||
223 | id3->frequency = (buf[9] << 8) | buf[10]; | ||
224 | |||
225 | /* Calculate track length (in ms) and estimate the bitrate (in kbit/s) */ | ||
226 | id3->length = ((int64_t) totalsamples * 1000) / id3->frequency; | ||
227 | |||
228 | return true; | ||
229 | } | ||
230 | buf += chunksize; | ||
231 | } | ||
232 | DEBUGF("metada error: smaf does not include pcm audio data\n"); | ||
233 | return false; | ||
234 | } | ||
235 | |||
236 | bool get_smaf_metadata(int fd, struct mp3entry* id3) | ||
237 | { | ||
238 | /* Use the trackname part of the id3 structure as a temporary buffer */ | ||
239 | unsigned char* buf = (unsigned char *)id3->path; | ||
240 | unsigned char *endbuf = smafbuf + 1024; | ||
241 | int i; | ||
242 | int contents_size; | ||
243 | int codepage = ISO_8859_1; | ||
244 | |||
245 | id3->title = NULL; | ||
246 | id3->artist = NULL; | ||
247 | id3->composer = NULL; | ||
248 | |||
249 | id3->vbr = false; /* All SMAF files are CBR */ | ||
250 | id3->filesize = filesize(fd); | ||
251 | |||
252 | /* get RIFF chunk header */ | ||
253 | if ((lseek(fd, 0, SEEK_SET) < 0) || (read(fd, buf, 21) < 21)) | ||
254 | { | ||
255 | return false; | ||
256 | } | ||
257 | |||
258 | if ((memcmp(buf, "MMMD", 4) != 0) || (memcmp(&buf[8], "CNTI", 4) != 0)) | ||
259 | { | ||
260 | DEBUGF("metada error: does not smaf format\n"); | ||
261 | return false; | ||
262 | } | ||
263 | |||
264 | contents_size = get_long_be(buf + 12); | ||
265 | if (contents_size < 5) | ||
266 | { | ||
267 | DEBUGF("metada error: CNTI chunk size is small %d\n", contents_size); | ||
268 | return false; | ||
269 | } | ||
270 | |||
271 | contents_size -= 5; | ||
272 | i = contents_size; | ||
273 | if (i == 0) | ||
274 | { | ||
275 | read(fd, buf, 16); | ||
276 | if (memcmp(buf, "OPDA", 4) != 0) | ||
277 | { | ||
278 | DEBUGF("metada error: smaf does not include OPDA chunk\n"); | ||
279 | return false; | ||
280 | } | ||
281 | contents_size = get_long_be(buf + 4) - 8; | ||
282 | |||
283 | if (memcmp(buf + 8, "Dch", 3) != 0) | ||
284 | { | ||
285 | DEBUGF("metada error: smaf does not include Dch chunk\n"); | ||
286 | return false; | ||
287 | } | ||
288 | codepage = convert_smaf_codetype(buf[11]); | ||
289 | if (codepage < 0) | ||
290 | { | ||
291 | DEBUGF("metada error: smaf unsupport codetype: %d\n", buf[11]); | ||
292 | return false; | ||
293 | } | ||
294 | |||
295 | i = get_long_be(buf + 12); | ||
296 | |||
297 | if (i > MAX_PATH) | ||
298 | { | ||
299 | DEBUGF("metada warning: smaf contents size is big %d\n", i); | ||
300 | i = MAX_PATH; | ||
301 | } | ||
302 | if (read(fd, buf, i) < i) | ||
303 | return false; | ||
304 | |||
305 | /* title */ | ||
306 | if (read_datachunk(buf, i, TAG_TITLE, codepage, id3->id3v1buf[0])) | ||
307 | id3->title = id3->id3v1buf[0]; | ||
308 | |||
309 | /* artist */ | ||
310 | if (read_datachunk(buf, i, TAG_ARTIST, codepage, id3->id3v1buf[1])) | ||
311 | id3->artist = id3->id3v1buf[1]; | ||
312 | |||
313 | /* composer */ | ||
314 | if (read_datachunk(buf, i, TAG_COMPOSER, codepage, id3->id3v1buf[2])) | ||
315 | id3->composer = id3->id3v1buf[2]; | ||
316 | } | ||
317 | else | ||
318 | { | ||
319 | codepage = convert_smaf_codetype(buf[14]); | ||
320 | if (codepage < 0) | ||
321 | { | ||
322 | DEBUGF("metada error: smaf unsupport codetype: %d\n", buf[11]); | ||
323 | return false; | ||
324 | } | ||
325 | |||
326 | if (i > MAX_PATH) | ||
327 | { | ||
328 | DEBUGF("metada warning: smaf contents size is big %d\n", i); | ||
329 | i = MAX_PATH; | ||
330 | } | ||
331 | if (read(fd, buf, i) < i) | ||
332 | return false; | ||
333 | |||
334 | /* title */ | ||
335 | if (read_option(buf, i, TAG_TITLE, codepage, id3->id3v1buf[0])) | ||
336 | id3->title = id3->id3v1buf[0]; | ||
337 | |||
338 | /* artist */ | ||
339 | if (read_option(buf, i, TAG_ARTIST, codepage, id3->id3v1buf[1])) | ||
340 | id3->artist = id3->id3v1buf[1]; | ||
341 | |||
342 | /* composer */ | ||
343 | if (read_option(buf, i, TAG_COMPOSER, codepage, id3->id3v1buf[2])) | ||
344 | id3->composer = id3->id3v1buf[2]; | ||
345 | } | ||
346 | |||
347 | if (contents_size > i) | ||
348 | lseek(fd, contents_size - i, SEEK_CUR); | ||
349 | |||
350 | /* assume the SMAF pcm data position is less than 1024 bytes */ | ||
351 | if (read(fd, smafbuf, 1024) < 1024) | ||
352 | return false; | ||
353 | |||
354 | buf = smafbuf; | ||
355 | while (buf + 8 < endbuf) | ||
356 | { | ||
357 | i = get_long_be(buf + 4) + 8; | ||
358 | |||
359 | if (memcmp(buf, "ATR", 3) == 0) | ||
360 | return get_smaf_metadata_audio_track(id3, buf, endbuf); | ||
361 | else if (memcmp(buf, "MTR", 3) == 0) | ||
362 | return get_smaf_metadata_score_track(id3, buf, endbuf); | ||
363 | |||
364 | buf += i; | ||
365 | } | ||
366 | |||
367 | DEBUGF("metada error: smaf does not include track chunk\n"); | ||
368 | return false; | ||
369 | } | ||