Wave/Wave64/vox metadata parser: optimize just a little.
[kugel-rb.git] / apps / metadata / aiff.c
blob063e5d56afc8bdd4ba6075e464a91aac8bc92664
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 /* compressionType: AIFC QuickTime IMA ADPCM */
33 #define AIFC_FORMAT_QT_IMA_ADPCM "ima4"
35 bool get_aiff_metadata(int fd, struct mp3entry* id3)
37 /* Use the trackname part of the id3 structure as a temporary buffer */
38 unsigned char* buf = (unsigned char *)id3->path;
39 unsigned long numChannels = 0;
40 unsigned long numSampleFrames = 0;
41 unsigned long numbytes = 0;
42 int read_bytes;
43 int i;
44 bool is_aifc = false;
46 if ((lseek(fd, 0, SEEK_SET) < 0) ||
47 ((read_bytes = read(fd, buf, sizeof(id3->path))) < 54) ||
48 (memcmp(buf, "FORM", 4) != 0) || (memcmp(buf + 8, "AIF", 3) != 0) ||
49 (!(is_aifc = (buf[11] == 'C')) && buf[11] != 'F'))
51 return false;
54 i = 12;
55 while ((numbytes == 0) && (read_bytes >= 8))
57 buf += i;
58 read_bytes -= i;
60 /* chunkSize */
61 i = get_long_be(&buf[4]);
63 if (memcmp(buf, "COMM", 4) == 0)
65 /* numChannels */
66 numChannels = ((buf[8]<<8)|buf[9]);
67 /* numSampleFrames */
68 numSampleFrames = get_long_be(&buf[10]);
69 /* sampleRate */
70 id3->frequency = get_long_be(&buf[18]);
71 id3->frequency >>= (16+14-buf[17]);
72 /* save format infos */
73 id3->bitrate = (((buf[14]<<8)|buf[15]) * numChannels * id3->frequency) / 1000;
74 if (!is_aifc || memcmp(&buf[26], AIFC_FORMAT_QT_IMA_ADPCM, 4) != 0)
75 id3->length = ((int64_t) numSampleFrames * 1000) / id3->frequency;
76 else
78 /* QuickTime IMA ADPCM is 1block = 64 data for each channel */
79 id3->length = ((int64_t) numSampleFrames * 64000LL) / id3->frequency;
82 id3->vbr = false; /* AIFF files are CBR */
83 id3->filesize = filesize(fd);
85 else if (memcmp(buf, "SSND", 4) == 0)
87 numbytes = i - 8;
90 /* odd chunk sizes must be padded */
91 i += 8 + (i & 0x01);
94 return ((numbytes != 0) && (numChannels != 0));