wave codec
[kugel-rb.git] / apps / codecs / smaf.c
blob930993770035e3331141954e22064aaf79d65934
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
28 * SMAF (Synthetic music Mobile Application Format)
30 * References
31 * [1] YAMAHA Corporation, Synthetic music Mobile Application Format Ver.3.05, 2002
34 enum {
35 SMAF_TRACK_CHUNK_SCORE = 0, /* Score Track */
36 SMAF_TRACK_CHUNK_AUDIO, /* PCM Audio Track */
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 */
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 },
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 },
58 #define NUM_FORMATS 3
60 static int basebits[4] = { 4, 8, 12, 16 };
62 #define PCM_SAMPLE_SIZE (2048*2)
64 static int32_t samples[PCM_SAMPLE_SIZE] IBSS_ATTR;
66 static const struct pcm_codec *get_codec(uint32_t formattag)
68 int i;
70 for (i = 0; i < NUM_FORMATS; i++)
72 if (pcm_codecs[i].format_tag == formattag)
74 if (pcm_codecs[i].get_codec)
75 return pcm_codecs[i].get_codec();
76 return 0;
79 return 0;
82 static unsigned int get_be32(uint8_t *buf)
84 return (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3];
87 static int convert_smaf_audio_format(int track_chunk, unsigned int audio_format)
89 if (audio_format > 3)
90 return SMAF_FORMAT_UNSUPPORT;
92 return support_formats[track_chunk][audio_format];
95 static int convert_smaf_audio_basebit(unsigned int basebit)
97 if (basebit > 4)
98 return 0;
99 return basebits[basebit];
102 static bool parse_audio_track(struct pcm_format *fmt,
103 unsigned char **stbuf, unsigned char *endbuf)
105 unsigned char *buf = *stbuf;
106 int chunksize;
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)
114 DEBUGF("CODEC_ERROR: unsupport pcm data format : %d\n", (buf[2] >> 4) & 0x07);
115 return false;
117 fmt->bitspersample = convert_smaf_audio_basebit(buf[3] >> 4);
118 if (fmt->bitspersample == 0)
120 DEBUGF("CODEC_ERROR: unsupport pcm data basebit : %d\n", buf[3] >> 4);
121 return false;
123 buf += 6;
124 while (buf < endbuf)
126 chunksize = get_be32(buf + 4) + 8;
127 if (memcmp(buf, "Awa", 3) == 0)
129 fmt->numbytes = get_be32(buf + 4);
130 buf += 8;
131 return true;
133 buf += chunksize;
135 DEBUGF("CODEC_ERROR: smaf does not include stream pcm data\n");
136 return false;
139 static bool parse_score_track(struct pcm_format *fmt,
140 unsigned char **stbuf, unsigned char *endbuf)
142 unsigned char *buf = *stbuf;
143 int chunksize;
145 if (buf[9] != 0x00)
147 DEBUGF("CODEC_ERROR: score track chunk unsupport sequence type %d\n", buf[9]);
148 return false;
152 * skip to the next chunk.
153 * MA-2/MA-3/MA-5: padding 16 bytes
154 * MA-7: padding 32 bytes
156 if (buf[3] < 7)
157 buf += 28;
158 else
159 buf += 44;
161 while (buf < endbuf)
163 chunksize = get_be32(buf + 4) + 8;
164 if (memcmp(buf, "Mtsp", 4) == 0)
166 buf += 8;
167 if (memcmp(buf, "Mwa", 3) != 0)
169 DEBUGF("CODEC_ERROR: smaf does not include stream pcm data\n");
170 return false;
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)
178 DEBUGF("CODEC_ERROR: unsupport pcm data format : %d\n",
179 (buf[8] >> 4) & 0x07);
180 return false;
182 fmt->bitspersample = convert_smaf_audio_basebit(buf[8] & 0x0f);
183 if (fmt->bitspersample == 0)
185 DEBUGF("CODEC_ERROR: unsupport pcm data basebit : %d\n",
186 buf[8] & 0x0f);
187 return false;
189 buf += 11;
190 return true;
192 buf += chunksize;
195 DEBUGF("CODEC_ERROR: smaf does not include stream pcm data\n");
196 return false;
199 static bool parse_header(struct pcm_format *fmt, size_t *pos)
201 unsigned char *buf, *stbuf, *endbuf;
202 size_t chunksize;
204 ci->memset(fmt, 0, sizeof(struct pcm_format));
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;
211 buf = stbuf;
212 endbuf = stbuf + chunksize;
214 if (memcmp(buf, "MMMD", 4) != 0)
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;
220 buf += 8;
222 while (buf < endbuf)
224 chunksize = get_be32(buf + 4) + 8;
225 if (memcmp(buf, "ATR", 3) == 0)
227 if (!parse_audio_track(fmt, &buf, endbuf))
228 return false;
229 break;
231 if (memcmp(buf, "MTR", 3) == 0)
233 if (!parse_score_track(fmt, &buf, endbuf))
234 return false;
235 break;
237 buf += chunksize;
240 if (buf >= endbuf)
242 DEBUGF("CODEC_ERROR: unsupported smaf format\n");
243 return false;
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;
251 /* data signess (default signed) */
252 fmt->is_signed = (fmt->formattag != SMAF_FORMAT_UNSIGNED_PCM);
254 fmt->is_little_endian = false;
256 /* sets pcm data position */
257 *pos = buf - stbuf;
259 return true;
262 static struct pcm_format format;
263 static uint32_t bytesdone;
265 static uint8_t *read_buffer(size_t *realsize)
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;
275 enum codec_status codec_main(void)
277 int status = CODEC_OK;
278 uint32_t decodedsamples;
279 size_t n;
280 int bufcount;
281 int endofstream;
282 uint8_t *smafbuf;
283 off_t firstblockposn; /* position of the first block in file */
284 const struct pcm_codec *codec;
286 /* Generic codec initialisation */
287 ci->configure(DSP_SET_SAMPLE_DEPTH, PCM_OUTPUT_DEPTH);
289 next_track:
290 if (codec_init()) {
291 status = CODEC_ERROR;
292 goto exit;
295 while (!*ci->taginfo_ready && !ci->stop_codec)
296 ci->sleep(1);
298 codec_set_replaygain(ci->id3);
300 ci->memset(&format, 0, sizeof(struct pcm_format));
301 format.is_signed = true;
302 format.is_little_endian = false;
304 decodedsamples = 0;
305 codec = 0;
307 if (!parse_header(&format, &n))
309 status = CODEC_ERROR;
310 goto done;
313 codec = get_codec(format.formattag);
314 if (codec == 0)
316 DEBUGF("CODEC_ERROR: unsupport audio format: 0x%x\n", (int)format.formattag);
317 status = CODEC_ERROR;
318 goto done;
321 if (!codec->set_format(&format))
323 status = CODEC_ERROR;
324 goto done;
327 /* common format check */
328 if (format.channels == 0) {
329 DEBUGF("CODEC_ERROR: 'fmt ' chunk not found or 0-channels file\n");
330 status = CODEC_ERROR;
331 goto done;
333 if (format.samplesperblock == 0) {
334 DEBUGF("CODEC_ERROR: 'fmt ' chunk not found or 0-wSamplesPerBlock file\n");
335 status = CODEC_ERROR;
336 goto done;
338 if (format.blockalign == 0)
340 DEBUGF("CODEC_ERROR: 'fmt ' chunk not found or 0-blockalign file\n");
341 status = CODEC_ERROR;
342 goto done;
344 if (format.numbytes == 0) {
345 DEBUGF("CODEC_ERROR: 'data' chunk not found or has zero-length\n");
346 status = CODEC_ERROR;
347 goto done;
350 /* check chunksize */
351 if ((format.chunksize / format.blockalign) * format.samplesperblock * format.channels
352 > PCM_SAMPLE_SIZE)
353 format.chunksize = (PCM_SAMPLE_SIZE / format.blockalign) * format.blockalign;
354 if (format.chunksize == 0)
356 DEBUGF("CODEC_ERROR: chunksize is 0\n");
357 status = CODEC_ERROR;
358 goto done;
361 ci->configure(DSP_SWITCH_FREQUENCY, ci->id3->frequency);
363 if (format.channels == 2) {
364 ci->configure(DSP_SET_STEREO_MODE, STEREO_INTERLEAVED);
365 } else if (format.channels == 1) {
366 ci->configure(DSP_SET_STEREO_MODE, STEREO_MONO);
367 } else {
368 DEBUGF("CODEC_ERROR: more than 2 channels unsupported\n");
369 status = CODEC_ERROR;
370 goto done;
373 firstblockposn = 1024 - n;
374 ci->advance_buffer(firstblockposn);
376 /* The main decoder loop */
377 bytesdone = 0;
378 ci->set_elapsed(0);
379 endofstream = 0;
381 while (!endofstream) {
382 ci->yield();
383 if (ci->stop_codec || ci->new_track)
384 break;
386 if (ci->seek_time) {
387 struct pcm_pos *newpos = codec->get_seek_pos(ci->seek_time, &read_buffer);
389 if (newpos->pos > format.numbytes)
390 break;
391 if (ci->seek_buffer(firstblockposn + newpos->pos))
393 bytesdone = newpos->pos;
394 decodedsamples = newpos->samples;
396 ci->seek_complete();
398 smafbuf = (uint8_t *)ci->request_buffer(&n, format.chunksize);
400 if (n == 0)
401 break; /* End of stream */
403 if (bytesdone + n > format.numbytes) {
404 n = format.numbytes - bytesdone;
405 endofstream = 1;
408 status = codec->decode(smafbuf, n, samples, &bufcount);
409 if (status == CODEC_ERROR)
411 DEBUGF("codec error\n");
412 goto done;
415 ci->pcmbuf_insert(samples, NULL, bufcount);
417 ci->advance_buffer(n);
418 bytesdone += n;
419 decodedsamples += bufcount;
420 if (bytesdone >= format.numbytes)
421 endofstream = 1;
423 ci->set_elapsed(decodedsamples*1000LL/ci->id3->frequency);
425 status = CODEC_OK;
427 done:
428 if (ci->request_next_track())
429 goto next_track;
431 exit:
432 return status;