Dave Bryant | bcf97a4 | 2007-08-12 06:36:06 +0000 | [diff] [blame] | 1 | /*************************************************************************** |
| 2 | * __________ __ ___. |
| 3 | * Open \______ \ ____ ____ | | _\_ |__ _______ ___ |
| 4 | * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / |
| 5 | * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < |
| 6 | * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ |
| 7 | * \/ \/ \/ \/ \/ |
| 8 | * $Id:$ |
| 9 | * |
| 10 | * Copyright (C) 2007 David Bryant |
| 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. |
Dave Bryant | bcf97a4 | 2007-08-12 06:36:06 +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" |
Dave Bryant | bcf97a4 | 2007-08-12 06:36:06 +0000 | [diff] [blame] | 31 | #include "logf.h" |
| 32 | |
| 33 | #define ID_UNIQUE 0x3f |
| 34 | #define ID_LARGE 0x80 |
| 35 | #define ID_SAMPLE_RATE 0x27 |
| 36 | |
Dave Bryant | 241fd0f | 2008-03-30 19:28:31 +0000 | [diff] [blame] | 37 | #define MONO_FLAG 4 |
| 38 | #define HYBRID_FLAG 8 |
| 39 | |
Dave Bryant | bcf97a4 | 2007-08-12 06:36:06 +0000 | [diff] [blame] | 40 | static const long wavpack_sample_rates [] = |
| 41 | { |
| 42 | 6000, 8000, 9600, 11025, 12000, 16000, 22050, 24000, |
| 43 | 32000, 44100, 48000, 64000, 88200, 96000, 192000 |
| 44 | }; |
| 45 | |
| 46 | /* A simple parser to read basic information from a WavPack file. This |
| 47 | * now works with self-extrating WavPack files and also will scan the |
| 48 | * metadata for non-standard sampling rates. This no longer fails on |
| 49 | * WavPack files containing floating-point audio data because these are |
| 50 | * now converted to standard Rockbox format in the decoder. |
| 51 | */ |
| 52 | |
| 53 | bool get_wavpack_metadata(int fd, struct mp3entry* id3) |
| 54 | { |
| 55 | /* Use the trackname part of the id3 structure as a temporary buffer */ |
| 56 | unsigned char* buf = (unsigned char *)id3->path; |
Dave Bryant | 241fd0f | 2008-03-30 19:28:31 +0000 | [diff] [blame] | 57 | uint32_t totalsamples, blocksamples, flags; |
Dave Bryant | bcf97a4 | 2007-08-12 06:36:06 +0000 | [diff] [blame] | 58 | int i; |
| 59 | |
| 60 | for (i = 0; i < 256; ++i) { |
| 61 | |
| 62 | /* at every 256 bytes into file, try to read a WavPack header */ |
| 63 | |
| 64 | if ((lseek(fd, i * 256, SEEK_SET) < 0) || (read(fd, buf, 32) < 32)) |
| 65 | return false; |
| 66 | |
| 67 | /* if valid WavPack 4 header version, break */ |
| 68 | |
| 69 | if (memcmp (buf, "wvpk", 4) == 0 && buf [9] == 4 && |
| 70 | (buf [8] >= 2 && buf [8] <= 0x10)) |
| 71 | break; |
| 72 | } |
| 73 | |
| 74 | if (i == 256) { |
| 75 | logf ("Not a WavPack file"); |
| 76 | return false; |
| 77 | } |
| 78 | |
| 79 | id3->vbr = true; /* All WavPack files are VBR */ |
| 80 | id3->filesize = filesize (fd); |
| 81 | totalsamples = get_long_le(&buf[12]); |
| 82 | blocksamples = get_long_le(&buf[20]); |
Dave Bryant | 241fd0f | 2008-03-30 19:28:31 +0000 | [diff] [blame] | 83 | flags = get_long_le(&buf[24]); |
Dave Bryant | bcf97a4 | 2007-08-12 06:36:06 +0000 | [diff] [blame] | 84 | |
Dave Bryant | 241fd0f | 2008-03-30 19:28:31 +0000 | [diff] [blame] | 85 | if (blocksamples) { |
Dave Bryant | bcf97a4 | 2007-08-12 06:36:06 +0000 | [diff] [blame] | 86 | int srindx = ((buf [26] >> 7) & 1) + ((buf [27] << 1) & 14); |
| 87 | |
| 88 | if (srindx == 15) { |
| 89 | uint32_t meta_bytes = buf [4] + (buf [5] << 8) + (buf [6] << 16) - 24; |
| 90 | uint32_t meta_size; |
| 91 | |
| 92 | id3->frequency = 44100; |
| 93 | |
| 94 | while (meta_bytes >= 6) { |
| 95 | if (read(fd, buf, 2) < 2) |
| 96 | break; |
| 97 | |
| 98 | if (buf [0] & ID_LARGE) { |
| 99 | if (read(fd, buf + 2, 2) < 2) |
| 100 | break; |
| 101 | |
| 102 | meta_size = (buf [1] << 1) + (buf [2] << 9) + (buf [3] << 17); |
| 103 | meta_bytes -= meta_size + 4; |
| 104 | } |
| 105 | else { |
| 106 | meta_size = buf [1] << 1; |
| 107 | meta_bytes -= meta_size + 2; |
| 108 | |
| 109 | if ((buf [0] & ID_UNIQUE) == ID_SAMPLE_RATE) { |
| 110 | if (meta_size == 4 && read(fd, buf + 2, 4) == 4) |
| 111 | id3->frequency = buf [2] + (buf [3] << 8) + (buf [4] << 16); |
| 112 | |
| 113 | break; |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | if (meta_size > 0 && lseek(fd, meta_size, SEEK_CUR) < 0) |
| 118 | break; |
| 119 | } |
| 120 | } |
| 121 | else |
| 122 | id3->frequency = wavpack_sample_rates[srindx]; |
| 123 | |
Dave Bryant | 241fd0f | 2008-03-30 19:28:31 +0000 | [diff] [blame] | 124 | /* if the total number of samples is unknown, make a guess on the high side (for now) */ |
| 125 | |
| 126 | if (totalsamples == (uint32_t) -1) { |
| 127 | totalsamples = filesize (fd) * 3; |
| 128 | |
| 129 | if (!(flags & HYBRID_FLAG)) |
| 130 | totalsamples /= 2; |
| 131 | |
| 132 | if (!(flags & MONO_FLAG)) |
| 133 | totalsamples /= 2; |
| 134 | } |
| 135 | |
Dave Bryant | bcf97a4 | 2007-08-12 06:36:06 +0000 | [diff] [blame] | 136 | id3->length = ((int64_t) totalsamples * 1000) / id3->frequency; |
| 137 | id3->bitrate = filesize (fd) / (id3->length / 8); |
Dave Bryant | 241fd0f | 2008-03-30 19:28:31 +0000 | [diff] [blame] | 138 | |
| 139 | return true; |
Dave Bryant | bcf97a4 | 2007-08-12 06:36:06 +0000 | [diff] [blame] | 140 | } |
| 141 | |
Dave Bryant | 241fd0f | 2008-03-30 19:28:31 +0000 | [diff] [blame] | 142 | return false; |
Dave Bryant | bcf97a4 | 2007-08-12 06:36:06 +0000 | [diff] [blame] | 143 | } |