summaryrefslogtreecommitdiff
path: root/apps/metadata/flac.c
diff options
context:
space:
mode:
authorMarcoen Hirschberg <marcoen@gmail.com>2007-06-16 18:19:51 +0000
committerMarcoen Hirschberg <marcoen@gmail.com>2007-06-16 18:19:51 +0000
commit2175d1edf65367fd3fe3cff266b8d6ea12930f2f (patch)
tree9d6f50ebfc7919b4e2f0853fc8c0cb844cabe995 /apps/metadata/flac.c
parentc3206a455a455fadb282d09f9af482c66b6bdf8e (diff)
downloadrockbox-2175d1edf65367fd3fe3cff266b8d6ea12930f2f.zip
rockbox-2175d1edf65367fd3fe3cff266b8d6ea12930f2f.tar.gz
rockbox-2175d1edf65367fd3fe3cff266b8d6ea12930f2f.tar.bz2
rockbox-2175d1edf65367fd3fe3cff266b8d6ea12930f2f.tar.xz
split up the metadata parser
git-svn-id: svn://svn.rockbox.org/rockbox/trunk@13637 a1c6a512-1295-4272-9138-f99709370657
Diffstat (limited to 'apps/metadata/flac.c')
-rw-r--r--apps/metadata/flac.c122
1 files changed, 122 insertions, 0 deletions
diff --git a/apps/metadata/flac.c b/apps/metadata/flac.c
new file mode 100644
index 0000000..5b3644e
--- /dev/null
+++ b/apps/metadata/flac.c
@@ -0,0 +1,122 @@
+/***************************************************************************
+ * __________ __ ___.
+ * Open \______ \ ____ ____ | | _\_ |__ _______ ___
+ * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
+ * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
+ * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
+ * \/ \/ \/ \/ \/
+ * $Id$
+ *
+ * Copyright (C) 2005 Dave Chapman
+ *
+ * All files in this archive are subject to the GNU General Public License.
+ * See the file COPYING in the source tree root for full license agreement.
+ *
+ * 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 "id3.h"
+#include "metadata_common.h"
+#include "logf.h"
+
+bool get_flac_metadata(int fd, struct mp3entry* id3)
+{
+ /* A simple parser to read vital metadata from a FLAC file - length,
+ * frequency, bitrate etc. This code should either be moved to a
+ * seperate file, or discarded in favour of the libFLAC code.
+ * The FLAC stream specification can be found at
+ * http://flac.sourceforge.net/format.html#stream
+ */
+
+ /* Use the trackname part of the id3 structure as a temporary buffer */
+ unsigned char* buf = (unsigned char *)id3->path;
+ bool rc = false;
+
+ if (!skip_id3v2(fd, id3) || (read(fd, buf, 4) < 4))
+ {
+ return rc;
+ }
+
+ if (memcmp(buf, "fLaC", 4) != 0)
+ {
+ return rc;
+ }
+
+ while (true)
+ {
+ long i;
+
+ if (read(fd, buf, 4) < 0)
+ {
+ return rc;
+ }
+
+ /* The length of the block */
+ i = (buf[1] << 16) | (buf[2] << 8) | buf[3];
+
+ if ((buf[0] & 0x7f) == 0) /* 0 is the STREAMINFO block */
+ {
+ unsigned long totalsamples;
+
+ /* FIXME: Don't trust the value of i */
+ if (read(fd, buf, i) < 0)
+ {
+ return rc;
+ }
+
+ id3->vbr = true; /* All FLAC files are VBR */
+ id3->filesize = filesize(fd);
+ id3->frequency = (buf[10] << 12) | (buf[11] << 4)
+ | ((buf[12] & 0xf0) >> 4);
+ rc = true; /* Got vital metadata */
+
+ /* totalsamples is a 36-bit field, but we assume <= 32 bits are used */
+ totalsamples = get_long_be(&buf[14]);
+
+ /* Calculate track length (in ms) and estimate the bitrate (in kbit/s) */
+ id3->length = ((int64_t) totalsamples * 1000) / id3->frequency;
+
+ if (id3->length <= 0)
+ {
+ logf("flac length invalid!");
+ return false;
+ }
+
+ id3->bitrate = (id3->filesize * 8) / id3->length;
+ }
+ else if ((buf[0] & 0x7f) == 4) /* 4 is the VORBIS_COMMENT block */
+ {
+ /* The next i bytes of the file contain the VORBIS COMMENTS. */
+ if (!read_vorbis_tags(fd, id3, i))
+ {
+ return rc;
+ }
+ }
+ else
+ {
+ if (buf[0] & 0x80)
+ {
+ /* If we have reached the last metadata block, abort. */
+ break;
+ }
+ else
+ {
+ /* Skip to next metadata block */
+ if (lseek(fd, i, SEEK_CUR) < 0)
+ {
+ return rc;
+ }
+ }
+ }
+ }
+
+ return true;
+}