split up the metadata parser
[Rockbox.git] / apps / metadata / aiff.c
blob3b155caa2b33e0215727b134837b5cccd82e1c9e
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2005 Dave Chapman
12 * All files in this archive are subject to the GNU General Public License.
13 * See the file COPYING in the source tree root for full license agreement.
15 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
16 * KIND, either express or implied.
18 ****************************************************************************/
19 #include <stdio.h>
20 #include <string.h>
21 #include <stdlib.h>
22 #include <ctype.h>
23 #include <inttypes.h>
25 #include "system.h"
26 #include "id3.h"
27 #include "metadata_common.h"
29 bool get_aiff_metadata(int fd, struct mp3entry* id3)
31 /* Use the trackname part of the id3 structure as a temporary buffer */
32 unsigned char* buf = (unsigned char *)id3->path;
33 unsigned long numChannels = 0;
34 unsigned long numSampleFrames = 0;
35 unsigned long sampleSize = 0;
36 unsigned long sampleRate = 0;
37 unsigned long numbytes = 0;
38 int read_bytes;
39 int i;
41 if ((lseek(fd, 0, SEEK_SET) < 0)
42 || ((read_bytes = read(fd, buf, sizeof(id3->path))) < 54))
44 return false;
47 if ((memcmp(buf, "FORM",4) != 0)
48 || (memcmp(&buf[8], "AIFF", 4) !=0 ))
50 return false;
53 buf += 12;
54 read_bytes -= 12;
56 while ((numbytes == 0) && (read_bytes >= 8))
58 /* chunkSize */
59 i = get_long_be(&buf[4]);
61 if (memcmp(buf, "COMM", 4) == 0)
63 /* numChannels */
64 numChannels = ((buf[8]<<8)|buf[9]);
65 /* numSampleFrames */
66 numSampleFrames = get_long_be(&buf[10]);
67 /* sampleSize */
68 sampleSize = ((buf[14]<<8)|buf[15]);
69 /* sampleRate */
70 sampleRate = get_long_be(&buf[18]);
71 sampleRate = sampleRate >> (16+14-buf[17]);
72 /* save format infos */
73 id3->bitrate = (sampleSize * numChannels * sampleRate) / 1000;
74 id3->frequency = sampleRate;
75 id3->length = ((int64_t) numSampleFrames * 1000) / id3->frequency;
77 id3->vbr = false; /* AIFF files are CBR */
78 id3->filesize = filesize(fd);
80 else if (memcmp(buf, "SSND", 4) == 0)
82 numbytes = i - 8;
85 if (i & 0x01)
87 i++; /* odd chunk sizes must be padded */
89 buf += i + 8;
90 read_bytes -= i + 8;
93 if ((numbytes == 0) || (numChannels == 0))
95 return false;
97 return true;