summaryrefslogtreecommitdiffstats
path: root/apps/codecs/aiff.c
blob: 4a127c7e0eb3c218e3e2dbaa6dd59d88597696ce (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
/***************************************************************************
 *             __________               __   ___.
 *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
 *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
 *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
 *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
 *                     \/            \/     \/    \/            \/
 * $Id$
 *
 * Copyright (c) 2005 Jvo Studer
 * Copyright (c) 2009 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

#define FOURCC(c1, c2, c3, c4) \
((((uint32_t)c1)<<24)|(((uint32_t)c2)<<16)|(((uint32_t)c3)<<8)|((uint32_t)c4))

/* This codec supports the following AIFC compressionType formats */
enum {
    AIFC_FORMAT_PCM          = FOURCC('N', 'O', 'N', 'E'), /* AIFC PCM Format (big endian) */
    AIFC_FORMAT_ALAW         = FOURCC('a', 'l', 'a', 'w'), /* AIFC ALaw compressed */
    AIFC_FORMAT_MULAW        = FOURCC('u', 'l', 'a', 'w'), /* AIFC uLaw compressed */
    AIFC_FORMAT_IEEE_FLOAT32 = FOURCC('f', 'l', '3', '2'), /* AIFC IEEE float 32 bit */
    AIFC_FORMAT_IEEE_FLOAT64 = FOURCC('f', 'l', '6', '4'), /* AIFC IEEE float 64 bit */
    AIFC_FORMAT_QT_IMA_ADPCM = FOURCC('i', 'm', 'a', '4'), /* AIFC QuickTime IMA ADPCM */
};

static const struct pcm_entry pcm_codecs[] = {
    { AIFC_FORMAT_PCM,          get_linear_pcm_codec      },
    { AIFC_FORMAT_ALAW,         get_itut_g711_alaw_codec  },
    { AIFC_FORMAT_MULAW,        get_itut_g711_mulaw_codec },
    { AIFC_FORMAT_IEEE_FLOAT32, get_ieee_float_codec      },
    { AIFC_FORMAT_IEEE_FLOAT64, get_ieee_float_codec      },
    { AIFC_FORMAT_QT_IMA_ADPCM, get_qt_ima_adpcm_codec    },
};

#define PCM_SAMPLE_SIZE (1024*2)

static int32_t samples[PCM_SAMPLE_SIZE] IBSS_ATTR;

static const struct pcm_codec *get_codec(uint32_t formattag)
{
    unsigned i;
    for (i = 0; i < sizeof(pcm_codecs)/sizeof(pcm_codecs[0]); i++)
        if (pcm_codecs[i].format_tag == formattag)
            return pcm_codecs[i].get_codec();

    return NULL;
}

enum codec_status codec_main(void)
{
    int status = CODEC_OK;
    struct pcm_format format;
    uint32_t bytesdone, decodedsamples;
    uint32_t num_sample_frames = 0;
    size_t n;
    int bufcount;
    int endofstream;
    unsigned char *buf;
    uint8_t *aifbuf;
    uint32_t offset2snd = 0;
    off_t firstblockposn;     /* position of the first block in file */
    bool is_aifc = false;
    const struct pcm_codec *codec;
    uint32_t size;

    /* 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);
    
    /* Need to save offset for later use (cleared indirectly by advance_buffer) */
    bytesdone = ci->id3->offset;

    /* assume the AIFF header is less than 1024 bytes */
    buf = ci->request_buffer(&n, 1024);
    if (n < 54) {
        status = CODEC_ERROR;
        goto done;
    }

    if (memcmp(buf, "FORM", 4) != 0)
    {
        DEBUGF("CODEC_ERROR: does not aiff format %4.4s\n", (char*)&buf[0]);
        status = CODEC_ERROR;
        goto done;
    }
    if (memcmp(&buf[8], "AIFF", 4) == 0)
        is_aifc = false;
    else if (memcmp(&buf[8], "AIFC", 4) == 0)
        is_aifc = true;
    else
    {
        DEBUGF("CODEC_ERROR: does not aiff format %4.4s\n", (char*)&buf[8]);
        status = CODEC_ERROR;
        goto done;
    }

    buf += 12;
    n -= 12;

    ci->memset(&format, 0, sizeof(struct pcm_format));
    format.is_signed = true;
    format.is_little_endian = false;

    decodedsamples = 0;
    codec = 0;

    /* read until 'SSND' chunk, which typically is last */
    while (format.numbytes == 0 && n >= 8)
    {
        /* chunkSize */
        size = ((buf[4]<<24)|(buf[5]<<16)|(buf[6]<<8)|buf[7]);
        if (memcmp(buf, "COMM", 4) == 0) {
            if ((!is_aifc && size < 18) || (is_aifc && size < 22))
            {
                DEBUGF("CODEC_ERROR: 'COMM' chunk size=%lu < %d\n",
                       (unsigned long)size, (is_aifc)?22:18);
                status = CODEC_ERROR;
                goto done;
            }
            /* num_channels */
            format.channels = ((buf[8]<<8)|buf[9]);
            /* num_sample_frames */
            num_sample_frames = ((buf[10]<<24)|(buf[11]<<16)|(buf[12]<<8)
                                |buf[13]);
            /* sample_size */
            format.bitspersample = ((buf[14]<<8)|buf[15]);
            /* sample_rate (don't use last 4 bytes, only integer fs) */
            if (buf[16] != 0x40) {
                DEBUGF("CODEC_ERROR: weird sampling rate (no @)\n");
                status = CODEC_ERROR;
                goto done;
            }
            format.samplespersec = ((buf[18]<<24)|(buf[19]<<16)|(buf[20]<<8)|buf[21])+1;
            format.samplespersec >>= (16 + 14 - buf[17]);
            /* compressionType (AIFC only) */
            if (is_aifc)
            {
                format.formattag = (buf[26]<<24)|(buf[27]<<16)|(buf[28]<<8)|buf[29];

                /*
                 * aiff's sample_size is uncompressed sound data size.
                 * But format.bitspersample is compressed sound data size.
                 */
                if (format.formattag == AIFC_FORMAT_ALAW ||
                    format.formattag == AIFC_FORMAT_MULAW)
                    format.bitspersample = 8;
                else if (format.formattag == AIFC_FORMAT_QT_IMA_ADPCM)
                    format.bitspersample = 4;
            }
            else
                format.formattag = AIFC_FORMAT_PCM;
            /* calc average bytes per second */
            format.avgbytespersec = format.samplespersec*format.channels*format.bitspersample/8;
        } else if (memcmp(buf, "SSND", 4)==0) {
            if (format.bitspersample == 0) {
                DEBUGF("CODEC_ERROR: unsupported chunk order\n");
                status = CODEC_ERROR;
                goto done;
            }
            /* offset2snd */
            offset2snd = (buf[8]<<24)|(buf[9]<<16)|(buf[10]<<8)|buf[11];
            /* block_size */
            format.blockalign = ((buf[12]<<24)|(buf[13]<<16)|(buf[14]<<8)|buf[15]) >> 3;
            if (format.blockalign == 0)
                format.blockalign = format.channels * format.bitspersample >> 3;
            format.numbytes = size - 8 - offset2snd;
            size = 8 + offset2snd; /* advance to the beginning of data */
        } else if (is_aifc && (memcmp(buf, "FVER", 4)==0)) {
            /* Format Version Chunk (AIFC only chunk) */
            /* skip this chunk */
        } else {
            DEBUGF("unsupported AIFF chunk: '%c%c%c%c', size=%lu\n",
                   buf[0], buf[1], buf[2], buf[3], (unsigned long)size);
        }

        size += 8 + (size & 0x01); /* odd chunk sizes must be padded */

        buf += size;
        if (n < size) {
            DEBUGF("CODEC_ERROR: AIFF header size > 1024\n");
            status = CODEC_ERROR;
            goto done;
        }
        n -= size;
    } /* while 'SSND' */

    if (format.channels == 0) {
        DEBUGF("CODEC_ERROR: 'COMM' chunk not found or 0-channels file\n");
        status = CODEC_ERROR;
        goto done;
    }
    if (format.numbytes == 0) {
        DEBUGF("CODEC_ERROR: 'SSND' chunk not found or has zero length\n");
        status = CODEC_ERROR;
        goto done;
    }

    codec = get_codec(format.formattag);
    if (codec == 0)
    {
        DEBUGF("CODEC_ERROR: AIFC does not support compressionType: 0x%x\n", 
            (unsigned int)format.formattag);
        status = CODEC_ERROR;
        goto done;
    }

    if (!codec->set_format(&format))
    {
        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;
    }

    if (format.samplesperblock == 0)
    {
        DEBUGF("CODEC_ERROR: samplesperblock is 0\n");
        status = CODEC_ERROR;
        goto done;
    }
    if (format.blockalign == 0)
    {
        DEBUGF("CODEC_ERROR: blockalign is 0\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;
    }

    firstblockposn = 1024 - n;
    ci->advance_buffer(firstblockposn);

    /* make sure we're at the correct offset */
    if (bytesdone > (uint32_t) firstblockposn) {
        /* Round down to previous block */
        struct pcm_pos *newpos = codec->get_seek_pos(bytesdone - firstblockposn,
                                                     PCM_SEEK_POS, NULL);

        if (newpos->pos > format.numbytes)
            goto done;
        if (ci->seek_buffer(firstblockposn + newpos->pos))
        {
            bytesdone      = newpos->pos;
            decodedsamples = newpos->samples;
        }
        ci->seek_complete();
    } else {
        /* already where we need to be */
        bytesdone = 0;
    }

    /* The main decoder loop */
    endofstream = 0;

    while (!endofstream) {
        ci->yield();
        if (ci->stop_codec || ci->new_track)
            break;

        if (ci->seek_time) {
            /* 3rd args(read_buffer) is unnecessary in the format which AIFF supports. */
            struct pcm_pos *newpos = codec->get_seek_pos(ci->seek_time, PCM_SEEK_TIME, NULL);

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