Fix Pacbox controls for D2 touchscreen
[Rockbox.git] / apps / metadata / flac.c
blobfd8cae013532f946970999330d691e1396d26b4d
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"
28 #include "metadata_parsers.h"
29 #include "logf.h"
31 bool get_flac_metadata(int fd, struct mp3entry* id3)
33 /* A simple parser to read vital metadata from a FLAC file - length,
34 * frequency, bitrate etc. This code should either be moved to a
35 * seperate file, or discarded in favour of the libFLAC code.
36 * The FLAC stream specification can be found at
37 * http://flac.sourceforge.net/format.html#stream
40 /* Use the trackname part of the id3 structure as a temporary buffer */
41 unsigned char* buf = (unsigned char *)id3->path;
42 bool last_metadata = false;
43 bool rc = false;
45 if (!skip_id3v2(fd, id3) || (read(fd, buf, 4) < 4))
47 return rc;
50 if (memcmp(buf, "fLaC", 4) != 0)
52 return rc;
55 while (!last_metadata)
57 unsigned long i;
58 int type;
60 if (read(fd, buf, 4) < 0)
62 return rc;
65 last_metadata = buf[0] & 0x80;
66 type = buf[0] & 0x7f;
67 /* The length of the block */
68 i = (buf[1] << 16) | (buf[2] << 8) | buf[3];
70 if (type == 0) /* 0 is the STREAMINFO block */
72 unsigned long totalsamples;
74 if (i >= sizeof(id3->path) || read(fd, buf, i) < 0)
76 return rc;
79 id3->vbr = true; /* All FLAC files are VBR */
80 id3->filesize = filesize(fd);
81 id3->frequency = (buf[10] << 12) | (buf[11] << 4)
82 | ((buf[12] & 0xf0) >> 4);
83 rc = true; /* Got vital metadata */
85 /* totalsamples is a 36-bit field, but we assume <= 32 bits are used */
86 totalsamples = get_long_be(&buf[14]);
88 /* Calculate track length (in ms) and estimate the bitrate (in kbit/s) */
89 id3->length = ((int64_t) totalsamples * 1000) / id3->frequency;
91 if (id3->length <= 0)
93 logf("flac length invalid!");
94 return false;
97 id3->bitrate = (id3->filesize * 8) / id3->length;
99 else if (type == 4) /* 4 is the VORBIS_COMMENT block */
101 /* The next i bytes of the file contain the VORBIS COMMENTS. */
102 if (!read_vorbis_tags(fd, id3, i))
104 return rc;
107 else if (!last_metadata)
109 /* Skip to next metadata block */
110 if (lseek(fd, i, SEEK_CUR) < 0)
112 return rc;
117 return true;