2007-06-16 18:19:51 +00:00
|
|
|
/***************************************************************************
|
|
|
|
* __________ __ ___.
|
|
|
|
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
|
|
|
|
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
|
|
|
|
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
|
|
|
|
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
|
|
|
|
* \/ \/ \/ \/ \/
|
|
|
|
* $Id$
|
|
|
|
*
|
|
|
|
* Copyright (C) 2005 Dave Chapman
|
|
|
|
*
|
2008-06-28 18:10:04 +00:00
|
|
|
* 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.
|
2007-06-16 18:19:51 +00:00
|
|
|
*
|
|
|
|
* 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>
|
2011-10-30 16:05:04 +00:00
|
|
|
#include "platform.h"
|
2007-06-16 18:19:51 +00:00
|
|
|
|
2008-10-15 06:38:51 +00:00
|
|
|
#include "metadata.h"
|
2007-06-16 18:19:51 +00:00
|
|
|
#include "metadata_common.h"
|
2008-05-03 07:03:59 +00:00
|
|
|
#include "metadata_parsers.h"
|
2007-06-16 18:19:51 +00:00
|
|
|
#include "logf.h"
|
|
|
|
|
|
|
|
bool get_flac_metadata(int fd, struct mp3entry* id3)
|
|
|
|
{
|
|
|
|
/* A simple parser to read vital metadata from a FLAC file - length,
|
2020-08-10 18:32:40 +00:00
|
|
|
* frequency, bitrate etc. This code should either be moved to a
|
2007-06-16 18:19:51 +00:00
|
|
|
* seperate file, or discarded in favour of the libFLAC code.
|
2020-08-10 18:32:40 +00:00
|
|
|
* The FLAC stream specification can be found at
|
2007-06-16 18:19:51 +00:00
|
|
|
* 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;
|
2007-10-13 11:21:10 +00:00
|
|
|
bool last_metadata = false;
|
2007-06-16 18:19:51 +00:00
|
|
|
bool rc = false;
|
|
|
|
|
|
|
|
if (!skip_id3v2(fd, id3) || (read(fd, buf, 4) < 4))
|
|
|
|
{
|
|
|
|
return rc;
|
|
|
|
}
|
2020-08-10 18:32:40 +00:00
|
|
|
|
|
|
|
if (memcmp(buf, "fLaC", 4) != 0)
|
2007-06-16 18:19:51 +00:00
|
|
|
{
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2020-08-10 18:32:40 +00:00
|
|
|
while (!last_metadata)
|
2007-06-16 18:19:51 +00:00
|
|
|
{
|
2007-10-13 11:21:10 +00:00
|
|
|
unsigned long i;
|
|
|
|
int type;
|
2020-08-10 18:32:40 +00:00
|
|
|
|
2019-05-12 14:36:52 +00:00
|
|
|
if (read(fd, buf, 4) != 4)
|
2007-06-16 18:19:51 +00:00
|
|
|
{
|
|
|
|
return rc;
|
|
|
|
}
|
2020-08-10 18:32:40 +00:00
|
|
|
|
2007-10-13 11:21:10 +00:00
|
|
|
last_metadata = buf[0] & 0x80;
|
|
|
|
type = buf[0] & 0x7f;
|
2007-06-16 18:19:51 +00:00
|
|
|
/* The length of the block */
|
|
|
|
i = (buf[1] << 16) | (buf[2] << 8) | buf[3];
|
|
|
|
|
2007-10-13 11:21:10 +00:00
|
|
|
if (type == 0) /* 0 is the STREAMINFO block */
|
2007-06-16 18:19:51 +00:00
|
|
|
{
|
|
|
|
unsigned long totalsamples;
|
2020-08-10 18:32:40 +00:00
|
|
|
|
|
|
|
if (i >= sizeof(id3->path) || read(fd, buf, i) != (int)i)
|
2007-06-16 18:19:51 +00:00
|
|
|
{
|
|
|
|
return rc;
|
|
|
|
}
|
2020-08-10 18:32:40 +00:00
|
|
|
|
2007-06-16 18:19:51 +00:00
|
|
|
id3->vbr = true; /* All FLAC files are VBR */
|
|
|
|
id3->filesize = filesize(fd);
|
2020-08-10 18:32:40 +00:00
|
|
|
id3->frequency = (buf[10] << 12) | (buf[11] << 4)
|
2007-06-16 18:19:51 +00:00
|
|
|
| ((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]);
|
2020-08-10 18:32:40 +00:00
|
|
|
|
2010-07-02 18:39:11 +00:00
|
|
|
if(totalsamples > 0)
|
|
|
|
{
|
2020-08-10 18:32:40 +00:00
|
|
|
/* Calculate track length (in ms) and estimate the bitrate (in kbit/s) */
|
2010-07-02 18:39:11 +00:00
|
|
|
id3->length = ((int64_t) totalsamples * 1000) / id3->frequency;
|
2020-08-10 18:32:40 +00:00
|
|
|
id3->bitrate = (id3->filesize * 8) / id3->length;
|
|
|
|
}
|
2010-07-02 18:39:11 +00:00
|
|
|
else if (totalsamples == 0)
|
|
|
|
{
|
|
|
|
id3->length = 0;
|
|
|
|
id3->bitrate = 0;
|
|
|
|
}
|
2020-08-10 18:32:40 +00:00
|
|
|
else
|
2007-06-16 18:19:51 +00:00
|
|
|
{
|
|
|
|
logf("flac length invalid!");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-08-10 18:32:40 +00:00
|
|
|
}
|
2007-10-13 11:21:10 +00:00
|
|
|
else if (type == 4) /* 4 is the VORBIS_COMMENT block */
|
2007-06-16 18:19:51 +00:00
|
|
|
{
|
|
|
|
/* The next i bytes of the file contain the VORBIS COMMENTS. */
|
2009-05-30 14:24:16 +00:00
|
|
|
if (read_vorbis_tags(fd, id3, i) == 0)
|
2007-06-16 18:19:51 +00:00
|
|
|
{
|
|
|
|
return rc;
|
|
|
|
}
|
2020-08-10 18:32:40 +00:00
|
|
|
}
|
2013-12-13 09:48:48 +00:00
|
|
|
#ifdef HAVE_ALBUMART
|
|
|
|
else if (type == 6) /* 6 is the PICTURE block */
|
|
|
|
{
|
|
|
|
if(!id3->has_embedded_albumart) /* only use the first PICTURE */
|
|
|
|
{
|
|
|
|
unsigned int buf_size = MIN(sizeof(id3->path), i);
|
|
|
|
int picframe_pos = 4; /* skip picture type */
|
|
|
|
int mime_length, description_length;
|
|
|
|
|
|
|
|
id3->albumart.pos = lseek(fd, 0, SEEK_CUR);
|
|
|
|
|
|
|
|
int bytes_read = read(fd, buf, buf_size);
|
|
|
|
i -= bytes_read;
|
|
|
|
|
|
|
|
mime_length = get_long_be(&buf[picframe_pos]);
|
|
|
|
|
|
|
|
char *mime = buf + picframe_pos + 4;
|
|
|
|
picframe_pos += 4 + mime_length;
|
|
|
|
|
|
|
|
id3->albumart.type = AA_TYPE_UNKNOWN;
|
|
|
|
if (memcmp(mime, "image/", 6) == 0)
|
|
|
|
{
|
|
|
|
mime += 6;
|
|
|
|
if (strcmp(mime, "jpeg") == 0 || strcmp(mime, "jpg") == 0){
|
|
|
|
id3->albumart.type = AA_TYPE_JPG;
|
|
|
|
}else if (strcmp(mime, "png") == 0)
|
|
|
|
id3->albumart.type = AA_TYPE_PNG;
|
|
|
|
}
|
|
|
|
|
|
|
|
description_length = get_long_be(&buf[picframe_pos]);
|
|
|
|
|
|
|
|
/* 16 = skip picture width,height,color-depth,color-used */
|
|
|
|
picframe_pos += 4 + description_length + 16;
|
|
|
|
|
|
|
|
/* if we support the format and image length is in the buffer */
|
|
|
|
if(id3->albumart.type != AA_TYPE_UNKNOWN
|
|
|
|
&& (picframe_pos + 4) - buf_size > 0)
|
|
|
|
{
|
|
|
|
id3->has_embedded_albumart = true;
|
|
|
|
id3->albumart.size = get_long_be(&buf[picframe_pos]);
|
|
|
|
id3->albumart.pos += picframe_pos + 4;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (lseek(fd, i, SEEK_CUR) < 0)
|
|
|
|
{
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
2007-10-13 11:21:10 +00:00
|
|
|
else if (!last_metadata)
|
2007-06-16 18:19:51 +00:00
|
|
|
{
|
2007-10-13 11:21:10 +00:00
|
|
|
/* Skip to next metadata block */
|
|
|
|
if (lseek(fd, i, SEEK_CUR) < 0)
|
2007-06-16 18:19:51 +00:00
|
|
|
{
|
2007-10-13 11:21:10 +00:00
|
|
|
return rc;
|
2007-06-16 18:19:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-08-10 18:32:40 +00:00
|
|
|
|
2007-06-16 18:19:51 +00:00
|
|
|
return true;
|
|
|
|
}
|