Marcoen Hirschberg | 2175d1e | 2007-06-16 18:19:51 +0000 | [diff] [blame] | 1 | /*************************************************************************** |
| 2 | * __________ __ ___. |
| 3 | * Open \______ \ ____ ____ | | _\_ |__ _______ ___ |
| 4 | * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / |
| 5 | * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < |
| 6 | * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ |
| 7 | * \/ \/ \/ \/ \/ |
| 8 | * $Id$ |
| 9 | * |
| 10 | * Copyright (C) 2005 Dave Chapman |
| 11 | * |
Daniel Stenberg | 2acc0ac | 2008-06-28 18:10:04 +0000 | [diff] [blame^] | 12 | * This program is free software; you can redistribute it and/or |
| 13 | * modify it under the terms of the GNU General Public License |
| 14 | * as published by the Free Software Foundation; either version 2 |
| 15 | * of the License, or (at your option) any later version. |
Marcoen Hirschberg | 2175d1e | 2007-06-16 18:19:51 +0000 | [diff] [blame] | 16 | * |
| 17 | * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY |
| 18 | * KIND, either express or implied. |
| 19 | * |
| 20 | ****************************************************************************/ |
| 21 | #include <stdio.h> |
| 22 | #include <string.h> |
| 23 | #include <stdlib.h> |
| 24 | #include <ctype.h> |
| 25 | #include <inttypes.h> |
| 26 | |
| 27 | #include "system.h" |
| 28 | #include "id3.h" |
| 29 | #include "metadata_common.h" |
Bertrik Sikken | 1273f95 | 2008-05-03 07:03:59 +0000 | [diff] [blame] | 30 | #include "metadata_parsers.h" |
Marcoen Hirschberg | 2175d1e | 2007-06-16 18:19:51 +0000 | [diff] [blame] | 31 | #include "logf.h" |
| 32 | |
| 33 | bool get_flac_metadata(int fd, struct mp3entry* id3) |
| 34 | { |
| 35 | /* A simple parser to read vital metadata from a FLAC file - length, |
| 36 | * frequency, bitrate etc. This code should either be moved to a |
| 37 | * seperate file, or discarded in favour of the libFLAC code. |
| 38 | * The FLAC stream specification can be found at |
| 39 | * http://flac.sourceforge.net/format.html#stream |
| 40 | */ |
| 41 | |
| 42 | /* Use the trackname part of the id3 structure as a temporary buffer */ |
| 43 | unsigned char* buf = (unsigned char *)id3->path; |
Magnus Holmgren | 5fc117e | 2007-10-13 11:21:10 +0000 | [diff] [blame] | 44 | bool last_metadata = false; |
Marcoen Hirschberg | 2175d1e | 2007-06-16 18:19:51 +0000 | [diff] [blame] | 45 | bool rc = false; |
| 46 | |
| 47 | if (!skip_id3v2(fd, id3) || (read(fd, buf, 4) < 4)) |
| 48 | { |
| 49 | return rc; |
| 50 | } |
| 51 | |
| 52 | if (memcmp(buf, "fLaC", 4) != 0) |
| 53 | { |
| 54 | return rc; |
| 55 | } |
| 56 | |
Magnus Holmgren | 5fc117e | 2007-10-13 11:21:10 +0000 | [diff] [blame] | 57 | while (!last_metadata) |
Marcoen Hirschberg | 2175d1e | 2007-06-16 18:19:51 +0000 | [diff] [blame] | 58 | { |
Magnus Holmgren | 5fc117e | 2007-10-13 11:21:10 +0000 | [diff] [blame] | 59 | unsigned long i; |
| 60 | int type; |
Marcoen Hirschberg | 2175d1e | 2007-06-16 18:19:51 +0000 | [diff] [blame] | 61 | |
| 62 | if (read(fd, buf, 4) < 0) |
| 63 | { |
| 64 | return rc; |
| 65 | } |
| 66 | |
Magnus Holmgren | 5fc117e | 2007-10-13 11:21:10 +0000 | [diff] [blame] | 67 | last_metadata = buf[0] & 0x80; |
| 68 | type = buf[0] & 0x7f; |
Marcoen Hirschberg | 2175d1e | 2007-06-16 18:19:51 +0000 | [diff] [blame] | 69 | /* The length of the block */ |
| 70 | i = (buf[1] << 16) | (buf[2] << 8) | buf[3]; |
| 71 | |
Magnus Holmgren | 5fc117e | 2007-10-13 11:21:10 +0000 | [diff] [blame] | 72 | if (type == 0) /* 0 is the STREAMINFO block */ |
Marcoen Hirschberg | 2175d1e | 2007-06-16 18:19:51 +0000 | [diff] [blame] | 73 | { |
| 74 | unsigned long totalsamples; |
Magnus Holmgren | 5fc117e | 2007-10-13 11:21:10 +0000 | [diff] [blame] | 75 | |
| 76 | if (i >= sizeof(id3->path) || read(fd, buf, i) < 0) |
Marcoen Hirschberg | 2175d1e | 2007-06-16 18:19:51 +0000 | [diff] [blame] | 77 | { |
| 78 | return rc; |
| 79 | } |
| 80 | |
| 81 | id3->vbr = true; /* All FLAC files are VBR */ |
| 82 | id3->filesize = filesize(fd); |
| 83 | id3->frequency = (buf[10] << 12) | (buf[11] << 4) |
| 84 | | ((buf[12] & 0xf0) >> 4); |
| 85 | rc = true; /* Got vital metadata */ |
| 86 | |
| 87 | /* totalsamples is a 36-bit field, but we assume <= 32 bits are used */ |
| 88 | totalsamples = get_long_be(&buf[14]); |
| 89 | |
| 90 | /* Calculate track length (in ms) and estimate the bitrate (in kbit/s) */ |
| 91 | id3->length = ((int64_t) totalsamples * 1000) / id3->frequency; |
| 92 | |
| 93 | if (id3->length <= 0) |
| 94 | { |
| 95 | logf("flac length invalid!"); |
| 96 | return false; |
| 97 | } |
| 98 | |
| 99 | id3->bitrate = (id3->filesize * 8) / id3->length; |
| 100 | } |
Magnus Holmgren | 5fc117e | 2007-10-13 11:21:10 +0000 | [diff] [blame] | 101 | else if (type == 4) /* 4 is the VORBIS_COMMENT block */ |
Marcoen Hirschberg | 2175d1e | 2007-06-16 18:19:51 +0000 | [diff] [blame] | 102 | { |
| 103 | /* The next i bytes of the file contain the VORBIS COMMENTS. */ |
| 104 | if (!read_vorbis_tags(fd, id3, i)) |
| 105 | { |
| 106 | return rc; |
| 107 | } |
| 108 | } |
Magnus Holmgren | 5fc117e | 2007-10-13 11:21:10 +0000 | [diff] [blame] | 109 | else if (!last_metadata) |
Marcoen Hirschberg | 2175d1e | 2007-06-16 18:19:51 +0000 | [diff] [blame] | 110 | { |
Magnus Holmgren | 5fc117e | 2007-10-13 11:21:10 +0000 | [diff] [blame] | 111 | /* Skip to next metadata block */ |
| 112 | if (lseek(fd, i, SEEK_CUR) < 0) |
Marcoen Hirschberg | 2175d1e | 2007-06-16 18:19:51 +0000 | [diff] [blame] | 113 | { |
Magnus Holmgren | 5fc117e | 2007-10-13 11:21:10 +0000 | [diff] [blame] | 114 | return rc; |
Marcoen Hirschberg | 2175d1e | 2007-06-16 18:19:51 +0000 | [diff] [blame] | 115 | } |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | return true; |
| 120 | } |