summaryrefslogtreecommitdiff
path: root/apps/codecs/smaf.c
blob: 8349d394ac7348b754236a2806852a247ccd2b33 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
/***************************************************************************
 *             __________               __   ___.
 *   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;
    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, PCM_OUTPUT_DEPTH-1);
  
next_track:
    if (codec_init()) {
        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;

    decodedsamples = 0;
    codec = 0;

    if (!parse_header(&format, &n))
    {
        status = CODEC_ERROR;
        goto done;
    }

    codec = get_codec(format.formattag);
    if (codec == 0)
    {
        DEBUGF("CODEC_ERROR: unsupport audio format: 0x%x\n", (int)format.formattag);
        status = CODEC_ERROR;
        goto done;
    }

    if (!codec->set_format(&format))
    {
        status = 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");
        status = 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");
        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 unsupported\n");
        status = 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);

            if (newpos->pos > format.numbytes)
                break;
            if (ci->seek_buffer(firstblockposn + newpos->pos))
            {
                bytesdone      = newpos->pos;
                decodedsamples = newpos->samples;
            }
            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);
    }
    status = CODEC_OK;

done:
    if (ci->request_next_track())
        goto next_track;

exit:
    return status;
}