summaryrefslogtreecommitdiffstats
path: root/apps/metadata/smaf.c
blob: f7d825c96a2ea7f443abc4ceaa48258528edb60c (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
/***************************************************************************
 *             __________               __   ___.
 *   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;
}