RaaA: move Android apps-code to separate dir under apps/hosted
[maemo-rb.git] / apps / metadata / aiff.c
blob6c16a3c2264f91a8ec2bb3a6e227844bdeaad93c
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2005 Dave Chapman
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 ****************************************************************************/
21 #include <stdio.h>
22 #include <string.h>
23 #include <stdlib.h>
24 #include <ctype.h>
25 #include <inttypes.h>
27 #include "system.h"
28 #include "metadata.h"
29 #include "metadata_common.h"
30 #include "metadata_parsers.h"
32 #include "debug.h"
34 /* compressionType: AIFC QuickTime IMA ADPCM */
35 #define AIFC_FORMAT_QT_IMA_ADPCM "ima4"
37 bool get_aiff_metadata(int fd, struct mp3entry* id3)
39 unsigned char buf[512];
40 unsigned long numChannels = 0;
41 unsigned long numSampleFrames = 0;
42 unsigned long numbytes = 0;
43 int read_bytes;
44 bool is_aifc = false;
46 if ((lseek(fd, 0, SEEK_SET) < 0) || (read(fd, &buf[0], 12) < 12) ||
47 (memcmp(&buf[0], "FORM", 4) != 0) || (memcmp(&buf[8], "AIF", 3) != 0) ||
48 (!(is_aifc = (buf[11] == 'C')) && buf[11] != 'F'))
50 return false;
53 while((read_bytes = read(fd, &buf[0], 8)) == 8)
55 size_t size = get_long_be(&buf[4]); /* chunkSize */
57 if (memcmp(&buf[0], "SSND", 4) == 0)
59 numbytes = size - 8;
60 break; /* assume COMM was already read */
63 /* odd chunk sizes must be padded */
64 size += size & 1;
66 if (size > sizeof(buf))
68 DEBUGF("AIFF \"%4.4s\" chunk too large (%zd > %zd)",
69 (char*) &buf[0], size, sizeof(buf));
72 if (memcmp(&buf[0], "COMM", 4) == 0)
74 if (size > sizeof(buf) || read(fd, &buf[0], size) != (ssize_t)size)
75 return false;
77 numChannels = ((buf[0]<<8)|buf[1]);
79 numSampleFrames = get_long_be(&buf[2]);
81 /* sampleRate */
82 id3->frequency = get_long_be(&buf[10]);
83 id3->frequency >>= (16+14-buf[9]);
85 /* save format infos */
86 id3->bitrate = ((buf[6]<<8)|buf[7]) * numChannels * id3->frequency;
87 id3->bitrate /= 1000;
89 if (!is_aifc || memcmp(&buf[18], AIFC_FORMAT_QT_IMA_ADPCM, 4) != 0)
90 id3->length = ((int64_t) numSampleFrames * 1000) / id3->frequency;
91 else
93 /* QuickTime IMA ADPCM is 1block = 64 data for each channel */
94 id3->length = ((int64_t) numSampleFrames * 64000LL) / id3->frequency;
97 id3->vbr = false; /* AIFF files are CBR */
98 id3->filesize = filesize(fd);
100 else
102 /* skip chunk */
103 if (lseek(fd, size, SEEK_CUR) < 0)
104 return false;
108 return numbytes && numChannels;