rockbox/lib/rbcodec/metadata/flac.c
Sean Bartell cadb3627fc Add rbcodecplatform.h and rbcodecconfig.h.
librbcodec users must provide these two files when the library is built.
rbcodecconfig.h provides configuration #defines and basic types, and
will be included by public librbcodec headers, so it must not conflict
with the user's code. rbcodecplatform.h provides various OS functions,
and will only be included by source files and private headers. This
system is intended to provide maximum flexibility for use on embedded
systems, where no operating system headers are included. Unix systems
can just copy rbcodecconfig-example.h and rbcodecplatform-unix.h with
minimal changes.

Change-Id: I350a2274d173da391fd1ca00c4202e9760d91def
Reviewed-on: http://gerrit.rockbox.org/143
Reviewed-by: Nils Wallménius <nils@rockbox.org>
Tested-by: Nils Wallménius <nils@rockbox.org>
2012-05-03 14:49:35 +02:00

127 lines
3.9 KiB
C

/***************************************************************************
* __________ __ ___.
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
* \/ \/ \/ \/ \/
* $Id$
*
* Copyright (C) 2005 Dave Chapman
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* 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 "platform.h"
#include "metadata.h"
#include "metadata_common.h"
#include "metadata_parsers.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 last_metadata = false;
bool rc = false;
if (!skip_id3v2(fd, id3) || (read(fd, buf, 4) < 4))
{
return rc;
}
if (memcmp(buf, "fLaC", 4) != 0)
{
return rc;
}
while (!last_metadata)
{
unsigned long i;
int type;
if (read(fd, buf, 4) < 0)
{
return rc;
}
last_metadata = buf[0] & 0x80;
type = buf[0] & 0x7f;
/* The length of the block */
i = (buf[1] << 16) | (buf[2] << 8) | buf[3];
if (type == 0) /* 0 is the STREAMINFO block */
{
unsigned long totalsamples;
if (i >= sizeof(id3->path) || 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]);
if(totalsamples > 0)
{
/* Calculate track length (in ms) and estimate the bitrate (in kbit/s) */
id3->length = ((int64_t) totalsamples * 1000) / id3->frequency;
id3->bitrate = (id3->filesize * 8) / id3->length;
}
else if (totalsamples == 0)
{
id3->length = 0;
id3->bitrate = 0;
}
else
{
logf("flac length invalid!");
return false;
}
}
else if (type == 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) == 0)
{
return rc;
}
}
else if (!last_metadata)
{
/* Skip to next metadata block */
if (lseek(fd, i, SEEK_CUR) < 0)
{
return rc;
}
}
}
return true;
}