summaryrefslogtreecommitdiffstats
path: root/apps/metadata/aiff.c
blob: 063e5d56afc8bdd4ba6075e464a91aac8bc92664 (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
/***************************************************************************
 *             __________               __   ___.
 *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
 *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
 *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
 *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
 *                     \/            \/     \/    \/            \/
 * $Id$
 *
 * Copyright (C) 2005 Dave Chapman
 *
 * 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"

/* compressionType: AIFC QuickTime IMA ADPCM */
#define AIFC_FORMAT_QT_IMA_ADPCM "ima4"

bool get_aiff_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 long numChannels = 0;
    unsigned long numSampleFrames = 0;
    unsigned long numbytes = 0;
    int read_bytes;
    int i;
    bool is_aifc = false;

    if ((lseek(fd, 0, SEEK_SET) < 0) ||
        ((read_bytes = read(fd, buf, sizeof(id3->path))) < 54) ||
        (memcmp(buf, "FORM", 4) != 0) || (memcmp(buf + 8, "AIF", 3) != 0) ||
        (!(is_aifc = (buf[11] == 'C')) && buf[11] != 'F'))
    {
        return false;
    }

    i = 12;
    while ((numbytes == 0) && (read_bytes >= 8)) 
    {
        buf        += i;
        read_bytes -= i;

        /* chunkSize */
        i = get_long_be(&buf[4]);
        
        if (memcmp(buf, "COMM", 4) == 0)
        {
            /* numChannels */
            numChannels = ((buf[8]<<8)|buf[9]);
            /* numSampleFrames */
            numSampleFrames = get_long_be(&buf[10]);
            /* sampleRate */
            id3->frequency = get_long_be(&buf[18]);
            id3->frequency >>= (16+14-buf[17]);
            /* save format infos */
            id3->bitrate = (((buf[14]<<8)|buf[15]) * numChannels * id3->frequency) / 1000;
            if (!is_aifc || memcmp(&buf[26], AIFC_FORMAT_QT_IMA_ADPCM, 4) != 0)
                id3->length = ((int64_t) numSampleFrames * 1000) / id3->frequency;
            else
            {
                /* QuickTime IMA ADPCM is 1block = 64 data for each channel */
                id3->length = ((int64_t) numSampleFrames * 64000LL) / id3->frequency;
            }

            id3->vbr = false;   /* AIFF files are CBR */
            id3->filesize = filesize(fd);
        }
        else if (memcmp(buf, "SSND", 4) == 0) 
        {
            numbytes = i - 8;
        }

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

    return ((numbytes != 0) && (numChannels != 0));
}