Linus Nielsen Feltzing | a039091 | 2003-03-10 14:55:31 +0000 | [diff] [blame] | 1 | /*************************************************************************** |
| 2 | * __________ __ ___. |
| 3 | * Open \______ \ ____ ____ | | _\_ |__ _______ ___ |
| 4 | * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / |
| 5 | * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < |
| 6 | * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ |
| 7 | * \/ \/ \/ \/ \/ |
| 8 | * $Id$ |
| 9 | * |
| 10 | * Copyright (C) 2002 by Daniel Stenberg |
| 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. |
Linus Nielsen Feltzing | a039091 | 2003-03-10 14:55:31 +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 | |
| 22 | /* |
| 23 | * Parts of this code has been stolen from the Ample project and was written |
Nicolas Pennequin | 357ffb3 | 2008-05-05 10:32:46 +0000 | [diff] [blame] | 24 | * by David Härdeman. It has since been extended and enhanced pretty much by |
Linus Nielsen Feltzing | a039091 | 2003-03-10 14:55:31 +0000 | [diff] [blame] | 25 | * all sorts of friendly Rockbox people. |
| 26 | * |
| 27 | * A nice reference for MPEG header info: |
| 28 | * http://rockbox.haxx.se/docs/mpeghdr.html |
| 29 | * |
| 30 | */ |
| 31 | |
| 32 | #include <stdio.h> |
| 33 | #include <stdlib.h> |
| 34 | #include <string.h> |
| 35 | #include <stdbool.h> |
Jens Arnold | ec9b202 | 2005-09-10 12:28:16 +0000 | [diff] [blame] | 36 | #include <limits.h> |
Linus Nielsen Feltzing | a039091 | 2003-03-10 14:55:31 +0000 | [diff] [blame] | 37 | #include "debug.h" |
Thom Johansen | 57f0ec1 | 2005-06-12 14:03:31 +0000 | [diff] [blame] | 38 | #include "logf.h" |
Linus Nielsen Feltzing | a039091 | 2003-03-10 14:55:31 +0000 | [diff] [blame] | 39 | #include "mp3data.h" |
| 40 | #include "file.h" |
Linus Nielsen Feltzing | 20d031f | 2003-05-09 16:01:21 +0000 | [diff] [blame] | 41 | #include "buffer.h" |
Linus Nielsen Feltzing | a039091 | 2003-03-10 14:55:31 +0000 | [diff] [blame] | 42 | |
Steve Bavin | 799e948 | 2008-04-07 17:19:53 +0000 | [diff] [blame] | 43 | // #define DEBUG_VERBOSE |
Linus Nielsen Feltzing | a039091 | 2003-03-10 14:55:31 +0000 | [diff] [blame] | 44 | |
Jean-Philippe Bernardy | 98a8132 | 2005-01-30 14:11:50 +0000 | [diff] [blame] | 45 | #define SYNC_MASK (0x7ffL << 21) |
| 46 | #define VERSION_MASK (3L << 19) |
| 47 | #define LAYER_MASK (3L << 17) |
| 48 | #define PROTECTION_MASK (1L << 16) |
| 49 | #define BITRATE_MASK (0xfL << 12) |
| 50 | #define SAMPLERATE_MASK (3L << 10) |
| 51 | #define PADDING_MASK (1L << 9) |
| 52 | #define PRIVATE_MASK (1L << 8) |
| 53 | #define CHANNELMODE_MASK (3L << 6) |
| 54 | #define MODE_EXT_MASK (3L << 4) |
| 55 | #define COPYRIGHT_MASK (1L << 3) |
| 56 | #define ORIGINAL_MASK (1L << 2) |
| 57 | #define EMPHASIS_MASK 3L |
Linus Nielsen Feltzing | a039091 | 2003-03-10 14:55:31 +0000 | [diff] [blame] | 58 | |
Jens Arnold | ec9b202 | 2005-09-10 12:28:16 +0000 | [diff] [blame] | 59 | /* MPEG Version table, sorted by version index */ |
| 60 | static const signed char version_table[4] = { |
| 61 | MPEG_VERSION2_5, -1, MPEG_VERSION2, MPEG_VERSION1 |
Linus Nielsen Feltzing | a039091 | 2003-03-10 14:55:31 +0000 | [diff] [blame] | 62 | }; |
| 63 | |
Jens Arnold | ec9b202 | 2005-09-10 12:28:16 +0000 | [diff] [blame] | 64 | /* Bitrate table for mpeg audio, indexed by row index and birate index */ |
| 65 | static const short bitrates[5][16] = { |
| 66 | {0,32,64,96,128,160,192,224,256,288,320,352,384,416,448,0}, /* V1 L1 */ |
| 67 | {0,32,48,56, 64, 80, 96,112,128,160,192,224,256,320,384,0}, /* V1 L2 */ |
| 68 | {0,32,40,48, 56, 64, 80, 96,112,128,160,192,224,256,320,0}, /* V1 L3 */ |
| 69 | {0,32,48,56, 64, 80, 96,112,128,144,160,176,192,224,256,0}, /* V2 L1 */ |
| 70 | {0, 8,16,24, 32, 40, 48, 56, 64, 80, 96,112,128,144,160,0} /* V2 L2+L3 */ |
| 71 | }; |
Linus Nielsen Feltzing | a039091 | 2003-03-10 14:55:31 +0000 | [diff] [blame] | 72 | |
Jens Arnold | ec9b202 | 2005-09-10 12:28:16 +0000 | [diff] [blame] | 73 | /* Bitrate pointer table, indexed by version and layer */ |
| 74 | static const short *bitrate_table[3][3] = |
Linus Nielsen Feltzing | a039091 | 2003-03-10 14:55:31 +0000 | [diff] [blame] | 75 | { |
Jens Arnold | ec9b202 | 2005-09-10 12:28:16 +0000 | [diff] [blame] | 76 | {bitrates[0], bitrates[1], bitrates[2]}, |
| 77 | {bitrates[3], bitrates[4], bitrates[4]}, |
| 78 | {bitrates[3], bitrates[4], bitrates[4]} |
| 79 | }; |
| 80 | |
| 81 | /* Sampling frequency table, indexed by version and frequency index */ |
Linus Nielsen Feltzing | aa8761f | 2007-02-15 22:55:22 +0000 | [diff] [blame] | 82 | static const unsigned short freq_table[3][3] = |
Jens Arnold | ec9b202 | 2005-09-10 12:28:16 +0000 | [diff] [blame] | 83 | { |
| 84 | {44100, 48000, 32000}, /* MPEG Version 1 */ |
| 85 | {22050, 24000, 16000}, /* MPEG version 2 */ |
| 86 | {11025, 12000, 8000}, /* MPEG version 2.5 */ |
Linus Nielsen Feltzing | a039091 | 2003-03-10 14:55:31 +0000 | [diff] [blame] | 87 | }; |
| 88 | |
Linus Nielsen Feltzing | aa8761f | 2007-02-15 22:55:22 +0000 | [diff] [blame] | 89 | unsigned long bytes2int(unsigned long b0, |
| 90 | unsigned long b1, |
| 91 | unsigned long b2, |
| 92 | unsigned long b3) |
| 93 | { |
| 94 | return (((long)(b0 & 0xFF) << (3*8)) | |
| 95 | ((long)(b1 & 0xFF) << (2*8)) | |
| 96 | ((long)(b2 & 0xFF) << (1*8)) | |
| 97 | ((long)(b3 & 0xFF) << (0*8))); |
| 98 | } |
| 99 | |
Linus Nielsen Feltzing | a039091 | 2003-03-10 14:55:31 +0000 | [diff] [blame] | 100 | /* check if 'head' is a valid mp3 frame header */ |
| 101 | static bool is_mp3frameheader(unsigned long head) |
| 102 | { |
| 103 | if ((head & SYNC_MASK) != (unsigned long)SYNC_MASK) /* bad sync? */ |
| 104 | return false; |
Jean-Philippe Bernardy | 98a8132 | 2005-01-30 14:11:50 +0000 | [diff] [blame] | 105 | if ((head & VERSION_MASK) == (1L << 19)) /* bad version? */ |
Linus Nielsen Feltzing | a039091 | 2003-03-10 14:55:31 +0000 | [diff] [blame] | 106 | return false; |
| 107 | if (!(head & LAYER_MASK)) /* no layer? */ |
| 108 | return false; |
Jens Arnold | d6c0545 | 2005-08-29 21:15:27 +0000 | [diff] [blame] | 109 | #if CONFIG_CODEC != SWCODEC |
Jens Arnold | b028772 | 2005-06-18 11:26:14 +0000 | [diff] [blame] | 110 | /* The MAS can't decode layer 1, so treat layer 1 data as invalid */ |
| 111 | if ((head & LAYER_MASK) == LAYER_MASK) |
| 112 | return false; |
| 113 | #endif |
Linus Nielsen Feltzing | a039091 | 2003-03-10 14:55:31 +0000 | [diff] [blame] | 114 | if ((head & BITRATE_MASK) == BITRATE_MASK) /* bad bitrate? */ |
| 115 | return false; |
| 116 | if (!(head & BITRATE_MASK)) /* no bitrate? */ |
| 117 | return false; |
| 118 | if ((head & SAMPLERATE_MASK) == SAMPLERATE_MASK) /* bad sample rate? */ |
| 119 | return false; |
Linus Nielsen Feltzing | a039091 | 2003-03-10 14:55:31 +0000 | [diff] [blame] | 120 | |
| 121 | return true; |
| 122 | } |
| 123 | |
| 124 | static bool mp3headerinfo(struct mp3info *info, unsigned long header) |
| 125 | { |
Jens Arnold | ec9b202 | 2005-09-10 12:28:16 +0000 | [diff] [blame] | 126 | int bitindex, freqindex; |
| 127 | |
Linus Nielsen Feltzing | a039091 | 2003-03-10 14:55:31 +0000 | [diff] [blame] | 128 | /* MPEG Audio Version */ |
Miika Pekkarinen | 93b6a1d | 2006-04-02 20:19:00 +0000 | [diff] [blame] | 129 | if ((header & VERSION_MASK) >> 19 >= sizeof(version_table)) |
| 130 | return false; |
| 131 | |
Jens Arnold | ec9b202 | 2005-09-10 12:28:16 +0000 | [diff] [blame] | 132 | info->version = version_table[(header & VERSION_MASK) >> 19]; |
| 133 | if (info->version < 0) |
Linus Nielsen Feltzing | a039091 | 2003-03-10 14:55:31 +0000 | [diff] [blame] | 134 | return false; |
Jens Arnold | 509117d | 2005-06-04 09:01:40 +0000 | [diff] [blame] | 135 | |
Jens Arnold | ec9b202 | 2005-09-10 12:28:16 +0000 | [diff] [blame] | 136 | /* Layer */ |
| 137 | info->layer = 3 - ((header & LAYER_MASK) >> 17); |
| 138 | if (info->layer == 3) |
Linus Nielsen Feltzing | a039091 | 2003-03-10 14:55:31 +0000 | [diff] [blame] | 139 | return false; |
Linus Nielsen Feltzing | a039091 | 2003-03-10 14:55:31 +0000 | [diff] [blame] | 140 | |
Jens Arnold | ec9b202 | 2005-09-10 12:28:16 +0000 | [diff] [blame] | 141 | info->protection = (header & PROTECTION_MASK) ? true : false; |
Linus Nielsen Feltzing | a039091 | 2003-03-10 14:55:31 +0000 | [diff] [blame] | 142 | |
| 143 | /* Bitrate */ |
Jens Arnold | ec9b202 | 2005-09-10 12:28:16 +0000 | [diff] [blame] | 144 | bitindex = (header & BITRATE_MASK) >> 12; |
| 145 | info->bitrate = bitrate_table[info->version][info->layer][bitindex]; |
Linus Nielsen Feltzing | df4cf2e | 2003-03-24 11:35:53 +0000 | [diff] [blame] | 146 | if(info->bitrate == 0) |
| 147 | return false; |
Linus Nielsen Feltzing | a039091 | 2003-03-10 14:55:31 +0000 | [diff] [blame] | 148 | |
Jens Arnold | ec9b202 | 2005-09-10 12:28:16 +0000 | [diff] [blame] | 149 | /* Sampling frequency */ |
| 150 | freqindex = (header & SAMPLERATE_MASK) >> 10; |
| 151 | if (freqindex == 3) |
| 152 | return false; |
| 153 | info->frequency = freq_table[info->version][freqindex]; |
| 154 | |
| 155 | info->padding = (header & PADDING_MASK) ? 1 : 0; |
Jens Arnold | 249f86c | 2005-08-22 00:15:57 +0000 | [diff] [blame] | 156 | |
Linus Nielsen Feltzing | a039091 | 2003-03-10 14:55:31 +0000 | [diff] [blame] | 157 | /* Calculate number of bytes, calculation depends on layer */ |
Jens Arnold | ec9b202 | 2005-09-10 12:28:16 +0000 | [diff] [blame] | 158 | if (info->layer == 0) { |
| 159 | info->frame_samples = 384; |
| 160 | info->frame_size = (12000 * info->bitrate / info->frequency |
| 161 | + info->padding) * 4; |
| 162 | } |
| 163 | else { |
| 164 | if ((info->version > MPEG_VERSION1) && (info->layer == 2)) |
| 165 | info->frame_samples = 576; |
| 166 | else |
| 167 | info->frame_samples = 1152; |
| 168 | info->frame_size = (1000/8) * info->frame_samples * info->bitrate |
| 169 | / info->frequency + info->padding; |
Linus Nielsen Feltzing | a039091 | 2003-03-10 14:55:31 +0000 | [diff] [blame] | 170 | } |
| 171 | |
Jens Arnold | 893669c | 2005-09-25 19:07:56 +0000 | [diff] [blame] | 172 | /* Frametime fraction denominator */ |
| 173 | if (freqindex != 0) { /* 48/32/24/16/12/8 kHz */ |
| 174 | info->ft_den = 1; /* integer number of milliseconds */ |
Jens Arnold | ec9b202 | 2005-09-10 12:28:16 +0000 | [diff] [blame] | 175 | } |
Jens Arnold | 893669c | 2005-09-25 19:07:56 +0000 | [diff] [blame] | 176 | else { /* 44.1/22.05/11.025 kHz */ |
| 177 | if (info->layer == 0) /* layer 1 */ |
Jens Arnold | ec9b202 | 2005-09-10 12:28:16 +0000 | [diff] [blame] | 178 | info->ft_den = 147; |
Jens Arnold | 893669c | 2005-09-25 19:07:56 +0000 | [diff] [blame] | 179 | else /* layer 2+3 */ |
Jens Arnold | ec9b202 | 2005-09-10 12:28:16 +0000 | [diff] [blame] | 180 | info->ft_den = 49; |
Jens Arnold | ec9b202 | 2005-09-10 12:28:16 +0000 | [diff] [blame] | 181 | } |
Jens Arnold | 893669c | 2005-09-25 19:07:56 +0000 | [diff] [blame] | 182 | /* Frametime fraction numerator */ |
| 183 | info->ft_num = 1000 * info->ft_den * info->frame_samples / info->frequency; |
Linus Nielsen Feltzing | a039091 | 2003-03-10 14:55:31 +0000 | [diff] [blame] | 184 | |
Jens Arnold | ec9b202 | 2005-09-10 12:28:16 +0000 | [diff] [blame] | 185 | info->channel_mode = (header & CHANNELMODE_MASK) >> 6; |
| 186 | info->mode_extension = (header & MODE_EXT_MASK) >> 4; |
| 187 | info->emphasis = header & EMPHASIS_MASK; |
Linus Nielsen Feltzing | a039091 | 2003-03-10 14:55:31 +0000 | [diff] [blame] | 188 | |
| 189 | #ifdef DEBUG_VERBOSE |
Jens Arnold | f68362a | 2007-03-17 09:54:28 +0000 | [diff] [blame] | 190 | DEBUGF( "Header: %08lx, Ver %d, lay %d, bitr %d, freq %ld, " |
Jens Arnold | ec9b202 | 2005-09-10 12:28:16 +0000 | [diff] [blame] | 191 | "chmode %d, mode_ext %d, emph %d, bytes: %d time: %d/%d\n", |
Linus Nielsen Feltzing | a0d5bea | 2004-12-01 11:04:39 +0000 | [diff] [blame] | 192 | header, info->version, info->layer+1, info->bitrate, |
| 193 | info->frequency, info->channel_mode, info->mode_extension, |
Jens Arnold | ec9b202 | 2005-09-10 12:28:16 +0000 | [diff] [blame] | 194 | info->emphasis, info->frame_size, info->ft_num, info->ft_den); |
Linus Nielsen Feltzing | a039091 | 2003-03-10 14:55:31 +0000 | [diff] [blame] | 195 | #endif |
| 196 | return true; |
| 197 | } |
| 198 | |
Jean-Philippe Bernardy | 98a8132 | 2005-01-30 14:11:50 +0000 | [diff] [blame] | 199 | static unsigned long __find_next_frame(int fd, long *offset, long max_offset, |
| 200 | unsigned long last_header, |
| 201 | int(*getfunc)(int fd, unsigned char *c)) |
Linus Nielsen Feltzing | a039091 | 2003-03-10 14:55:31 +0000 | [diff] [blame] | 202 | { |
| 203 | unsigned long header=0; |
| 204 | unsigned char tmp; |
| 205 | int i; |
| 206 | |
Jean-Philippe Bernardy | 98a8132 | 2005-01-30 14:11:50 +0000 | [diff] [blame] | 207 | long pos = 0; |
Linus Nielsen Feltzing | a039091 | 2003-03-10 14:55:31 +0000 | [diff] [blame] | 208 | |
| 209 | /* We remember the last header we found, to use as a template to see if |
| 210 | the header we find has the same frequency, layer etc */ |
| 211 | last_header &= 0xffff0c00; |
| 212 | |
| 213 | /* Fill up header with first 24 bits */ |
| 214 | for(i = 0; i < 3; i++) { |
| 215 | header <<= 8; |
Linus Nielsen Feltzing | 24a8b6a | 2003-11-02 11:24:38 +0000 | [diff] [blame] | 216 | if(!getfunc(fd, &tmp)) |
Linus Nielsen Feltzing | a039091 | 2003-03-10 14:55:31 +0000 | [diff] [blame] | 217 | return 0; |
| 218 | header |= tmp; |
| 219 | pos++; |
| 220 | } |
| 221 | |
| 222 | do { |
| 223 | header <<= 8; |
Linus Nielsen Feltzing | 24a8b6a | 2003-11-02 11:24:38 +0000 | [diff] [blame] | 224 | if(!getfunc(fd, &tmp)) |
Linus Nielsen Feltzing | a039091 | 2003-03-10 14:55:31 +0000 | [diff] [blame] | 225 | return 0; |
| 226 | header |= tmp; |
| 227 | pos++; |
| 228 | if(max_offset > 0 && pos > max_offset) |
| 229 | return 0; |
| 230 | } while(!is_mp3frameheader(header) || |
| 231 | (last_header?((header & 0xffff0c00) != last_header):false)); |
| 232 | |
| 233 | *offset = pos - 4; |
| 234 | |
Steve Bavin | 799e948 | 2008-04-07 17:19:53 +0000 | [diff] [blame] | 235 | #if defined(DEBUG) |
Linus Nielsen Feltzing | a039091 | 2003-03-10 14:55:31 +0000 | [diff] [blame] | 236 | if(*offset) |
Jens Arnold | f68362a | 2007-03-17 09:54:28 +0000 | [diff] [blame] | 237 | DEBUGF("Warning: skipping %ld bytes of garbage\n", *offset); |
Linus Nielsen Feltzing | a039091 | 2003-03-10 14:55:31 +0000 | [diff] [blame] | 238 | #endif |
Jens Arnold | ec9b202 | 2005-09-10 12:28:16 +0000 | [diff] [blame] | 239 | |
Linus Nielsen Feltzing | a039091 | 2003-03-10 14:55:31 +0000 | [diff] [blame] | 240 | return header; |
| 241 | } |
| 242 | |
Linus Nielsen Feltzing | 24a8b6a | 2003-11-02 11:24:38 +0000 | [diff] [blame] | 243 | static int fileread(int fd, unsigned char *c) |
Miika Pekkarinen | b0e78a4 | 2005-07-13 13:13:10 +0000 | [diff] [blame] | 244 | { |
Linus Nielsen Feltzing | 24a8b6a | 2003-11-02 11:24:38 +0000 | [diff] [blame] | 245 | return read(fd, c, 1); |
| 246 | } |
| 247 | |
Jean-Philippe Bernardy | 98a8132 | 2005-01-30 14:11:50 +0000 | [diff] [blame] | 248 | unsigned long find_next_frame(int fd, long *offset, long max_offset, unsigned long last_header) |
Linus Nielsen Feltzing | 24a8b6a | 2003-11-02 11:24:38 +0000 | [diff] [blame] | 249 | { |
| 250 | return __find_next_frame(fd, offset, max_offset, last_header, fileread); |
| 251 | } |
| 252 | |
Miika Pekkarinen | 0dd7ea2 | 2006-11-10 08:03:33 +0000 | [diff] [blame] | 253 | #ifndef __PCTOOL__ |
Linus Nielsen Feltzing | a039091 | 2003-03-10 14:55:31 +0000 | [diff] [blame] | 254 | static int fnf_read_index; |
| 255 | static int fnf_buf_len; |
| 256 | |
Linus Nielsen Feltzing | 6475aa0 | 2003-03-10 18:05:01 +0000 | [diff] [blame] | 257 | static int buf_getbyte(int fd, unsigned char *c) |
Linus Nielsen Feltzing | a039091 | 2003-03-10 14:55:31 +0000 | [diff] [blame] | 258 | { |
| 259 | if(fnf_read_index < fnf_buf_len) |
| 260 | { |
Linus Nielsen Feltzing | d34865a | 2005-04-05 11:33:58 +0000 | [diff] [blame] | 261 | *c = audiobuf[fnf_read_index++]; |
Linus Nielsen Feltzing | a039091 | 2003-03-10 14:55:31 +0000 | [diff] [blame] | 262 | return 1; |
| 263 | } |
| 264 | else |
| 265 | { |
Linus Nielsen Feltzing | d34865a | 2005-04-05 11:33:58 +0000 | [diff] [blame] | 266 | fnf_buf_len = read(fd, audiobuf, audiobufend - audiobuf); |
Linus Nielsen Feltzing | a039091 | 2003-03-10 14:55:31 +0000 | [diff] [blame] | 267 | if(fnf_buf_len < 0) |
| 268 | return -1; |
| 269 | |
| 270 | fnf_read_index = 0; |
| 271 | |
| 272 | if(fnf_buf_len > 0) |
| 273 | { |
Linus Nielsen Feltzing | d34865a | 2005-04-05 11:33:58 +0000 | [diff] [blame] | 274 | *c = audiobuf[fnf_read_index++]; |
Linus Nielsen Feltzing | a039091 | 2003-03-10 14:55:31 +0000 | [diff] [blame] | 275 | return 1; |
| 276 | } |
| 277 | else |
| 278 | return 0; |
| 279 | } |
| 280 | return 0; |
| 281 | } |
| 282 | |
Linus Nielsen Feltzing | 6475aa0 | 2003-03-10 18:05:01 +0000 | [diff] [blame] | 283 | static int buf_seek(int fd, int len) |
Linus Nielsen Feltzing | a039091 | 2003-03-10 14:55:31 +0000 | [diff] [blame] | 284 | { |
| 285 | fnf_read_index += len; |
| 286 | if(fnf_read_index > fnf_buf_len) |
| 287 | { |
| 288 | len = fnf_read_index - fnf_buf_len; |
| 289 | |
Linus Nielsen Feltzing | d34865a | 2005-04-05 11:33:58 +0000 | [diff] [blame] | 290 | fnf_buf_len = read(fd, audiobuf, audiobufend - audiobuf); |
Linus Nielsen Feltzing | a039091 | 2003-03-10 14:55:31 +0000 | [diff] [blame] | 291 | if(fnf_buf_len < 0) |
| 292 | return -1; |
| 293 | |
| 294 | fnf_read_index = 0; |
| 295 | fnf_read_index += len; |
| 296 | } |
| 297 | |
| 298 | if(fnf_read_index > fnf_buf_len) |
| 299 | { |
| 300 | return -1; |
| 301 | } |
| 302 | else |
| 303 | return 0; |
| 304 | } |
| 305 | |
| 306 | static void buf_init(void) |
| 307 | { |
| 308 | fnf_buf_len = 0; |
| 309 | fnf_read_index = 0; |
| 310 | } |
| 311 | |
Bertrik Sikken | e15f8a2 | 2008-05-03 08:35:14 +0000 | [diff] [blame] | 312 | static unsigned long buf_find_next_frame(int fd, long *offset, long max_offset, |
| 313 | unsigned long last_header) |
Linus Nielsen Feltzing | a039091 | 2003-03-10 14:55:31 +0000 | [diff] [blame] | 314 | { |
Linus Nielsen Feltzing | 24a8b6a | 2003-11-02 11:24:38 +0000 | [diff] [blame] | 315 | return __find_next_frame(fd, offset, max_offset, last_header, buf_getbyte); |
| 316 | } |
Linus Nielsen Feltzing | a039091 | 2003-03-10 14:55:31 +0000 | [diff] [blame] | 317 | |
Linus Nielsen Feltzing | d34865a | 2005-04-05 11:33:58 +0000 | [diff] [blame] | 318 | static int audiobuflen; |
Linus Nielsen Feltzing | 24a8b6a | 2003-11-02 11:24:38 +0000 | [diff] [blame] | 319 | static int mem_pos; |
| 320 | static int mem_cnt; |
| 321 | static int mem_maxlen; |
Linus Nielsen Feltzing | a039091 | 2003-03-10 14:55:31 +0000 | [diff] [blame] | 322 | |
Linus Nielsen Feltzing | 24a8b6a | 2003-11-02 11:24:38 +0000 | [diff] [blame] | 323 | static int mem_getbyte(int dummy, unsigned char *c) |
| 324 | { |
| 325 | dummy = dummy; |
Linus Nielsen Feltzing | a039091 | 2003-03-10 14:55:31 +0000 | [diff] [blame] | 326 | |
Linus Nielsen Feltzing | d34865a | 2005-04-05 11:33:58 +0000 | [diff] [blame] | 327 | *c = audiobuf[mem_pos++]; |
| 328 | if(mem_pos >= audiobuflen) |
Linus Nielsen Feltzing | 24a8b6a | 2003-11-02 11:24:38 +0000 | [diff] [blame] | 329 | mem_pos = 0; |
| 330 | |
| 331 | if(mem_cnt++ >= mem_maxlen) |
| 332 | return 0; |
| 333 | else |
| 334 | return 1; |
| 335 | } |
| 336 | |
Jean-Philippe Bernardy | 98a8132 | 2005-01-30 14:11:50 +0000 | [diff] [blame] | 337 | unsigned long mem_find_next_frame(int startpos, long *offset, long max_offset, |
Linus Nielsen Feltzing | 24a8b6a | 2003-11-02 11:24:38 +0000 | [diff] [blame] | 338 | unsigned long last_header) |
| 339 | { |
Linus Nielsen Feltzing | d34865a | 2005-04-05 11:33:58 +0000 | [diff] [blame] | 340 | audiobuflen = audiobufend - audiobuf; |
Linus Nielsen Feltzing | 24a8b6a | 2003-11-02 11:24:38 +0000 | [diff] [blame] | 341 | mem_pos = startpos; |
| 342 | mem_cnt = 0; |
| 343 | mem_maxlen = max_offset; |
| 344 | |
| 345 | return __find_next_frame(0, offset, max_offset, last_header, mem_getbyte); |
Linus Nielsen Feltzing | a039091 | 2003-03-10 14:55:31 +0000 | [diff] [blame] | 346 | } |
Miika Pekkarinen | 0dd7ea2 | 2006-11-10 08:03:33 +0000 | [diff] [blame] | 347 | #endif |
Linus Nielsen Feltzing | a039091 | 2003-03-10 14:55:31 +0000 | [diff] [blame] | 348 | |
| 349 | int get_mp3file_info(int fd, struct mp3info *info) |
| 350 | { |
Linus Nielsen Feltzing | d28a506 | 2003-06-03 21:50:21 +0000 | [diff] [blame] | 351 | unsigned char frame[1800]; |
Linus Nielsen Feltzing | a039091 | 2003-03-10 14:55:31 +0000 | [diff] [blame] | 352 | unsigned char *vbrheader; |
| 353 | unsigned long header; |
Jean-Philippe Bernardy | 98a8132 | 2005-01-30 14:11:50 +0000 | [diff] [blame] | 354 | long bytecount; |
Linus Nielsen Feltzing | a039091 | 2003-03-10 14:55:31 +0000 | [diff] [blame] | 355 | int num_offsets; |
| 356 | int frames_per_entry; |
| 357 | int i; |
Jean-Philippe Bernardy | 98a8132 | 2005-01-30 14:11:50 +0000 | [diff] [blame] | 358 | long offset; |
Linus Nielsen Feltzing | a039091 | 2003-03-10 14:55:31 +0000 | [diff] [blame] | 359 | int j; |
Jean-Philippe Bernardy | 98a8132 | 2005-01-30 14:11:50 +0000 | [diff] [blame] | 360 | long tmp; |
Linus Nielsen Feltzing | a039091 | 2003-03-10 14:55:31 +0000 | [diff] [blame] | 361 | |
| 362 | header = find_next_frame(fd, &bytecount, 0x20000, 0); |
| 363 | /* Quit if we haven't found a valid header within 128K */ |
| 364 | if(header == 0) |
| 365 | return -1; |
| 366 | |
| 367 | memset(info, 0, sizeof(struct mp3info)); |
Linus Nielsen Feltzing | aa8761f | 2007-02-15 22:55:22 +0000 | [diff] [blame] | 368 | #if CONFIG_CODEC==SWCODEC |
Thom Johansen | 57f0ec1 | 2005-06-12 14:03:31 +0000 | [diff] [blame] | 369 | /* These two are needed for proper LAME gapless MP3 playback */ |
Thom Johansen | 57f0ec1 | 2005-06-12 14:03:31 +0000 | [diff] [blame] | 370 | info->enc_delay = -1; |
| 371 | info->enc_padding = -1; |
Linus Nielsen Feltzing | aa8761f | 2007-02-15 22:55:22 +0000 | [diff] [blame] | 372 | #endif |
Linus Nielsen Feltzing | a039091 | 2003-03-10 14:55:31 +0000 | [diff] [blame] | 373 | if(!mp3headerinfo(info, header)) |
| 374 | return -2; |
| 375 | |
| 376 | /* OK, we have found a frame. Let's see if it has a Xing header */ |
Daniel Stenberg | c16c727 | 2006-04-02 23:06:20 +0000 | [diff] [blame] | 377 | if (info->frame_size-4 >= (int)sizeof(frame)) |
Miika Pekkarinen | 93b6a1d | 2006-04-02 20:19:00 +0000 | [diff] [blame] | 378 | { |
Steve Bavin | 799e948 | 2008-04-07 17:19:53 +0000 | [diff] [blame] | 379 | #if defined(DEBUG) |
Miika Pekkarinen | 93b6a1d | 2006-04-02 20:19:00 +0000 | [diff] [blame] | 380 | DEBUGF("Error: Invalid id3 header, frame_size: %d\n", info->frame_size); |
| 381 | #endif |
| 382 | return -8; |
| 383 | } |
| 384 | |
Linus Nielsen Feltzing | a039091 | 2003-03-10 14:55:31 +0000 | [diff] [blame] | 385 | if(read(fd, frame, info->frame_size-4) < 0) |
| 386 | return -3; |
| 387 | |
| 388 | /* calculate position of VBR header */ |
| 389 | if ( info->version == MPEG_VERSION1 ) { |
| 390 | if (info->channel_mode == 3) /* mono */ |
| 391 | vbrheader = frame + 17; |
| 392 | else |
| 393 | vbrheader = frame + 32; |
| 394 | } |
| 395 | else { |
| 396 | if (info->channel_mode == 3) /* mono */ |
| 397 | vbrheader = frame + 9; |
| 398 | else |
| 399 | vbrheader = frame + 17; |
| 400 | } |
| 401 | |
Magnus Holmgren | 501ab1a | 2005-08-13 08:09:28 +0000 | [diff] [blame] | 402 | if (!memcmp(vbrheader, "Xing", 4) |
| 403 | || !memcmp(vbrheader, "Info", 4)) |
Linus Nielsen Feltzing | a039091 | 2003-03-10 14:55:31 +0000 | [diff] [blame] | 404 | { |
| 405 | int i = 8; /* Where to start parsing info */ |
| 406 | |
Nicolas Pennequin | 06c199e | 2008-04-07 19:33:48 +0000 | [diff] [blame] | 407 | /* DEBUGF("Xing/Info header\n"); */ |
Linus Nielsen Feltzing | a039091 | 2003-03-10 14:55:31 +0000 | [diff] [blame] | 408 | |
| 409 | /* Remember where in the file the Xing header is */ |
Linus Nielsen Feltzing | 6475aa0 | 2003-03-10 18:05:01 +0000 | [diff] [blame] | 410 | info->vbr_header_pos = lseek(fd, 0, SEEK_CUR) - info->frame_size; |
Linus Nielsen Feltzing | a039091 | 2003-03-10 14:55:31 +0000 | [diff] [blame] | 411 | |
| 412 | /* We want to skip the Xing frame when playing the stream */ |
| 413 | bytecount += info->frame_size; |
| 414 | |
| 415 | /* Now get the next frame to find out the real info about |
| 416 | the mp3 stream */ |
| 417 | header = find_next_frame(fd, &tmp, 0x20000, 0); |
| 418 | if(header == 0) |
| 419 | return -4; |
Linus Nielsen Feltzing | 6475aa0 | 2003-03-10 18:05:01 +0000 | [diff] [blame] | 420 | |
Linus Nielsen Feltzing | a039091 | 2003-03-10 14:55:31 +0000 | [diff] [blame] | 421 | if(!mp3headerinfo(info, header)) |
| 422 | return -5; |
| 423 | |
Magnus Holmgren | 501ab1a | 2005-08-13 08:09:28 +0000 | [diff] [blame] | 424 | /* Is it a VBR file? */ |
| 425 | info->is_vbr = info->is_xing_vbr = !memcmp(vbrheader, "Xing", 4); |
| 426 | |
Jens Arnold | ba966c1 | 2005-09-15 05:29:26 +0000 | [diff] [blame] | 427 | if (vbrheader[7] & VBR_FRAMES_FLAG) /* Is the frame count there? */ |
Linus Nielsen Feltzing | a039091 | 2003-03-10 14:55:31 +0000 | [diff] [blame] | 428 | { |
Linus Nielsen Feltzing | aa8761f | 2007-02-15 22:55:22 +0000 | [diff] [blame] | 429 | info->frame_count = bytes2int(vbrheader[i], vbrheader[i+1], |
Linus Nielsen Feltzing | a039091 | 2003-03-10 14:55:31 +0000 | [diff] [blame] | 430 | vbrheader[i+2], vbrheader[i+3]); |
Jens Arnold | ec9b202 | 2005-09-10 12:28:16 +0000 | [diff] [blame] | 431 | if (info->frame_count <= ULONG_MAX / info->ft_num) |
| 432 | info->file_time = info->frame_count * info->ft_num / info->ft_den; |
| 433 | else |
| 434 | info->file_time = info->frame_count / info->ft_den * info->ft_num; |
Linus Nielsen Feltzing | a039091 | 2003-03-10 14:55:31 +0000 | [diff] [blame] | 435 | i += 4; |
| 436 | } |
| 437 | |
Jens Arnold | ba966c1 | 2005-09-15 05:29:26 +0000 | [diff] [blame] | 438 | if (vbrheader[7] & VBR_BYTES_FLAG) /* Is byte count there? */ |
Linus Nielsen Feltzing | a039091 | 2003-03-10 14:55:31 +0000 | [diff] [blame] | 439 | { |
Linus Nielsen Feltzing | aa8761f | 2007-02-15 22:55:22 +0000 | [diff] [blame] | 440 | info->byte_count = bytes2int(vbrheader[i], vbrheader[i+1], |
Linus Nielsen Feltzing | a039091 | 2003-03-10 14:55:31 +0000 | [diff] [blame] | 441 | vbrheader[i+2], vbrheader[i+3]); |
Linus Nielsen Feltzing | a039091 | 2003-03-10 14:55:31 +0000 | [diff] [blame] | 442 | i += 4; |
| 443 | } |
| 444 | |
Jens Arnold | ba966c1 | 2005-09-15 05:29:26 +0000 | [diff] [blame] | 445 | if (info->file_time && info->byte_count) |
| 446 | { |
| 447 | if (info->byte_count <= (ULONG_MAX/8)) |
| 448 | info->bitrate = info->byte_count * 8 / info->file_time; |
| 449 | else |
| 450 | info->bitrate = info->byte_count / (info->file_time >> 3); |
| 451 | } |
Linus Nielsen Feltzing | 6475aa0 | 2003-03-10 18:05:01 +0000 | [diff] [blame] | 452 | else |
| 453 | info->bitrate = 0; |
Jens Arnold | ba966c1 | 2005-09-15 05:29:26 +0000 | [diff] [blame] | 454 | |
| 455 | if (vbrheader[7] & VBR_TOC_FLAG) /* Is table-of-contents there? */ |
Linus Nielsen Feltzing | a039091 | 2003-03-10 14:55:31 +0000 | [diff] [blame] | 456 | { |
Magnus Holmgren | 2cf14db | 2006-10-19 18:03:11 +0000 | [diff] [blame] | 457 | info->has_toc = true; |
Linus Nielsen Feltzing | a039091 | 2003-03-10 14:55:31 +0000 | [diff] [blame] | 458 | memcpy( info->toc, vbrheader+i, 100 ); |
Thom Johansen | 57f0ec1 | 2005-06-12 14:03:31 +0000 | [diff] [blame] | 459 | i += 100; |
| 460 | } |
| 461 | if (vbrheader[7] & VBR_QUALITY_FLAG) |
| 462 | { |
| 463 | /* We don't care about this, but need to skip it */ |
| 464 | i += 4; |
| 465 | } |
Linus Nielsen Feltzing | aa8761f | 2007-02-15 22:55:22 +0000 | [diff] [blame] | 466 | #if CONFIG_CODEC==SWCODEC |
Thom Johansen | 57f0ec1 | 2005-06-12 14:03:31 +0000 | [diff] [blame] | 467 | i += 21; |
| 468 | info->enc_delay = (vbrheader[i] << 4) | (vbrheader[i + 1] >> 4); |
| 469 | info->enc_padding = ((vbrheader[i + 1] & 0x0f) << 8) | vbrheader[i + 2]; |
Thom Johansen | a1df060 | 2005-11-02 18:05:31 +0000 | [diff] [blame] | 470 | /* TODO: This sanity checking is rather silly, seeing as how the LAME |
| 471 | header contains a CRC field that can be used to verify integrity. */ |
| 472 | if (!(info->enc_delay >= 0 && info->enc_delay <= 2880 && |
Thom Johansen | 57f0ec1 | 2005-06-12 14:03:31 +0000 | [diff] [blame] | 473 | info->enc_padding >= 0 && info->enc_padding <= 2*1152)) |
| 474 | { |
| 475 | /* Invalid data */ |
| 476 | info->enc_delay = -1; |
| 477 | info->enc_padding = -1; |
Linus Nielsen Feltzing | a039091 | 2003-03-10 14:55:31 +0000 | [diff] [blame] | 478 | } |
Linus Nielsen Feltzing | aa8761f | 2007-02-15 22:55:22 +0000 | [diff] [blame] | 479 | #endif |
Linus Nielsen Feltzing | a039091 | 2003-03-10 14:55:31 +0000 | [diff] [blame] | 480 | } |
| 481 | |
Jens Arnold | 35b6dc3 | 2004-08-03 21:54:24 +0000 | [diff] [blame] | 482 | if (!memcmp(vbrheader, "VBRI", 4)) |
Linus Nielsen Feltzing | a039091 | 2003-03-10 14:55:31 +0000 | [diff] [blame] | 483 | { |
| 484 | DEBUGF("VBRI header\n"); |
| 485 | |
| 486 | /* We want to skip the VBRI frame when playing the stream */ |
| 487 | bytecount += info->frame_size; |
| 488 | |
| 489 | /* Now get the next frame to find out the real info about |
| 490 | the mp3 stream */ |
Linus Nielsen Feltzing | a233036 | 2004-04-29 01:18:15 +0000 | [diff] [blame] | 491 | header = find_next_frame(fd, &tmp, 0x20000, 0); |
Linus Nielsen Feltzing | a039091 | 2003-03-10 14:55:31 +0000 | [diff] [blame] | 492 | if(header == 0) |
| 493 | return -6; |
Linus Nielsen Feltzing | a233036 | 2004-04-29 01:18:15 +0000 | [diff] [blame] | 494 | |
| 495 | bytecount += tmp; |
Linus Nielsen Feltzing | a039091 | 2003-03-10 14:55:31 +0000 | [diff] [blame] | 496 | |
| 497 | if(!mp3headerinfo(info, header)) |
| 498 | return -7; |
| 499 | |
Jens Arnold | f68362a | 2007-03-17 09:54:28 +0000 | [diff] [blame] | 500 | DEBUGF("%04x: %04x %04x ", 0, (short)(header >> 16), |
| 501 | (short)(header & 0xffff)); |
Linus Nielsen Feltzing | a039091 | 2003-03-10 14:55:31 +0000 | [diff] [blame] | 502 | for(i = 4;i < (int)sizeof(frame)-4;i+=2) { |
| 503 | if(i % 16 == 0) { |
| 504 | DEBUGF("\n%04x: ", i-4); |
| 505 | } |
| 506 | DEBUGF("%04x ", (frame[i-4] << 8) | frame[i-4+1]); |
| 507 | } |
| 508 | |
| 509 | DEBUGF("\n"); |
| 510 | |
| 511 | /* Yes, it is a FhG VBR file */ |
| 512 | info->is_vbr = true; |
| 513 | info->is_vbri_vbr = true; |
| 514 | info->has_toc = false; /* We don't parse the TOC (yet) */ |
| 515 | |
Linus Nielsen Feltzing | aa8761f | 2007-02-15 22:55:22 +0000 | [diff] [blame] | 516 | info->byte_count = bytes2int(vbrheader[10], vbrheader[11], |
Linus Nielsen Feltzing | a039091 | 2003-03-10 14:55:31 +0000 | [diff] [blame] | 517 | vbrheader[12], vbrheader[13]); |
Linus Nielsen Feltzing | aa8761f | 2007-02-15 22:55:22 +0000 | [diff] [blame] | 518 | info->frame_count = bytes2int(vbrheader[14], vbrheader[15], |
Linus Nielsen Feltzing | a039091 | 2003-03-10 14:55:31 +0000 | [diff] [blame] | 519 | vbrheader[16], vbrheader[17]); |
Jens Arnold | ec9b202 | 2005-09-10 12:28:16 +0000 | [diff] [blame] | 520 | if (info->frame_count <= ULONG_MAX / info->ft_num) |
| 521 | info->file_time = info->frame_count * info->ft_num / info->ft_den; |
| 522 | else |
| 523 | info->file_time = info->frame_count / info->ft_den * info->ft_num; |
Jens Arnold | ba966c1 | 2005-09-15 05:29:26 +0000 | [diff] [blame] | 524 | |
| 525 | if (info->byte_count <= (ULONG_MAX/8)) |
| 526 | info->bitrate = info->byte_count * 8 / info->file_time; |
| 527 | else |
| 528 | info->bitrate = info->byte_count / (info->file_time >> 3); |
Linus Nielsen Feltzing | a039091 | 2003-03-10 14:55:31 +0000 | [diff] [blame] | 529 | |
| 530 | /* We don't parse the TOC, since we don't yet know how to (FIXME) */ |
Linus Nielsen Feltzing | aa8761f | 2007-02-15 22:55:22 +0000 | [diff] [blame] | 531 | num_offsets = bytes2int(0, 0, vbrheader[18], vbrheader[19]); |
| 532 | frames_per_entry = bytes2int(0, 0, vbrheader[24], vbrheader[25]); |
Linus Nielsen Feltzing | a039091 | 2003-03-10 14:55:31 +0000 | [diff] [blame] | 533 | DEBUGF("Frame size (%dkpbs): %d bytes (0x%x)\n", |
| 534 | info->bitrate, info->frame_size, info->frame_size); |
Jens Arnold | f68362a | 2007-03-17 09:54:28 +0000 | [diff] [blame] | 535 | DEBUGF("Frame count: %lx\n", info->frame_count); |
| 536 | DEBUGF("Byte count: %lx\n", info->byte_count); |
Linus Nielsen Feltzing | a039091 | 2003-03-10 14:55:31 +0000 | [diff] [blame] | 537 | DEBUGF("Offsets: %d\n", num_offsets); |
| 538 | DEBUGF("Frames/entry: %d\n", frames_per_entry); |
| 539 | |
| 540 | offset = 0; |
| 541 | |
| 542 | for(i = 0;i < num_offsets;i++) |
| 543 | { |
Linus Nielsen Feltzing | aa8761f | 2007-02-15 22:55:22 +0000 | [diff] [blame] | 544 | j = bytes2int(0, 0, vbrheader[26+i*2], vbrheader[27+i*2]); |
Linus Nielsen Feltzing | a039091 | 2003-03-10 14:55:31 +0000 | [diff] [blame] | 545 | offset += j; |
Jens Arnold | f68362a | 2007-03-17 09:54:28 +0000 | [diff] [blame] | 546 | DEBUGF("%03d: %lx (%x)\n", i, offset - bytecount, j); |
Linus Nielsen Feltzing | a039091 | 2003-03-10 14:55:31 +0000 | [diff] [blame] | 547 | } |
| 548 | } |
| 549 | |
Linus Nielsen Feltzing | a039091 | 2003-03-10 14:55:31 +0000 | [diff] [blame] | 550 | return bytecount; |
| 551 | } |
| 552 | |
Jean-Philippe Bernardy | 98a8132 | 2005-01-30 14:11:50 +0000 | [diff] [blame] | 553 | static void long2bytes(unsigned char *buf, long val) |
Linus Nielsen Feltzing | a039091 | 2003-03-10 14:55:31 +0000 | [diff] [blame] | 554 | { |
| 555 | buf[0] = (val >> 24) & 0xff; |
| 556 | buf[1] = (val >> 16) & 0xff; |
| 557 | buf[2] = (val >> 8) & 0xff; |
| 558 | buf[3] = val & 0xff; |
| 559 | } |
| 560 | |
Miika Pekkarinen | 0dd7ea2 | 2006-11-10 08:03:33 +0000 | [diff] [blame] | 561 | #ifndef __PCTOOL__ |
Linus Nielsen Feltzing | a039091 | 2003-03-10 14:55:31 +0000 | [diff] [blame] | 562 | int count_mp3_frames(int fd, int startpos, int filesize, |
| 563 | void (*progressfunc)(int)) |
| 564 | { |
| 565 | unsigned long header = 0; |
| 566 | struct mp3info info; |
| 567 | int num_frames; |
Jean-Philippe Bernardy | 98a8132 | 2005-01-30 14:11:50 +0000 | [diff] [blame] | 568 | long bytes; |
Linus Nielsen Feltzing | a039091 | 2003-03-10 14:55:31 +0000 | [diff] [blame] | 569 | int cnt; |
Jean-Philippe Bernardy | 98a8132 | 2005-01-30 14:11:50 +0000 | [diff] [blame] | 570 | long progress_chunk = filesize / 50; /* Max is 50%, in 1% increments */ |
Linus Nielsen Feltzing | a039091 | 2003-03-10 14:55:31 +0000 | [diff] [blame] | 571 | int progress_cnt = 0; |
Linus Nielsen Feltzing | 2c07e78 | 2003-03-10 18:25:40 +0000 | [diff] [blame] | 572 | bool is_vbr = false; |
| 573 | int last_bitrate = 0; |
Linus Nielsen Feltzing | 1936c41 | 2004-01-13 14:06:18 +0000 | [diff] [blame] | 574 | int header_template = 0; |
Linus Nielsen Feltzing | a039091 | 2003-03-10 14:55:31 +0000 | [diff] [blame] | 575 | |
| 576 | if(lseek(fd, startpos, SEEK_SET) < 0) |
| 577 | return -1; |
| 578 | |
| 579 | buf_init(); |
| 580 | |
| 581 | /* Find out the total number of frames */ |
| 582 | num_frames = 0; |
| 583 | cnt = 0; |
| 584 | |
Linus Nielsen Feltzing | 1936c41 | 2004-01-13 14:06:18 +0000 | [diff] [blame] | 585 | while((header = buf_find_next_frame(fd, &bytes, -1, header_template))) { |
Linus Nielsen Feltzing | a039091 | 2003-03-10 14:55:31 +0000 | [diff] [blame] | 586 | mp3headerinfo(&info, header); |
Linus Nielsen Feltzing | 2c07e78 | 2003-03-10 18:25:40 +0000 | [diff] [blame] | 587 | |
Linus Nielsen Feltzing | 1936c41 | 2004-01-13 14:06:18 +0000 | [diff] [blame] | 588 | if(!header_template) |
| 589 | header_template = header; |
| 590 | |
Linus Nielsen Feltzing | 2c07e78 | 2003-03-10 18:25:40 +0000 | [diff] [blame] | 591 | /* See if this really is a VBR file */ |
| 592 | if(last_bitrate && info.bitrate != last_bitrate) |
| 593 | { |
| 594 | is_vbr = true; |
| 595 | } |
| 596 | last_bitrate = info.bitrate; |
| 597 | |
Linus Nielsen Feltzing | 6475aa0 | 2003-03-10 18:05:01 +0000 | [diff] [blame] | 598 | buf_seek(fd, info.frame_size-4); |
Linus Nielsen Feltzing | a039091 | 2003-03-10 14:55:31 +0000 | [diff] [blame] | 599 | num_frames++; |
| 600 | if(progressfunc) |
| 601 | { |
| 602 | cnt += bytes + info.frame_size; |
| 603 | if(cnt > progress_chunk) |
| 604 | { |
| 605 | progress_cnt++; |
| 606 | progressfunc(progress_cnt); |
| 607 | cnt = 0; |
| 608 | } |
| 609 | } |
| 610 | } |
| 611 | DEBUGF("Total number of frames: %d\n", num_frames); |
Linus Nielsen Feltzing | 2c07e78 | 2003-03-10 18:25:40 +0000 | [diff] [blame] | 612 | |
| 613 | if(is_vbr) |
| 614 | return num_frames; |
| 615 | else |
| 616 | { |
| 617 | DEBUGF("Not a VBR file\n"); |
| 618 | return 0; |
| 619 | } |
Linus Nielsen Feltzing | a039091 | 2003-03-10 14:55:31 +0000 | [diff] [blame] | 620 | } |
| 621 | |
Linus Nielsen Feltzing | 478da62 | 2003-04-20 22:00:30 +0000 | [diff] [blame] | 622 | static const char cooltext[] = "Rockbox - rocks your box"; |
| 623 | |
Jens Arnold | ba966c1 | 2005-09-15 05:29:26 +0000 | [diff] [blame] | 624 | /* buf needs to be the audio buffer with TOC generation enabled, |
| 625 | and at least MAX_XING_HEADER_SIZE bytes otherwise */ |
| 626 | int create_xing_header(int fd, long startpos, long filesize, |
| 627 | unsigned char *buf, unsigned long num_frames, |
| 628 | unsigned long rec_time, unsigned long header_template, |
Linus Nielsen Feltzing | 6475aa0 | 2003-03-10 18:05:01 +0000 | [diff] [blame] | 629 | void (*progressfunc)(int), bool generate_toc) |
Jens Arnold | ba966c1 | 2005-09-15 05:29:26 +0000 | [diff] [blame] | 630 | { |
Linus Nielsen Feltzing | a039091 | 2003-03-10 14:55:31 +0000 | [diff] [blame] | 631 | struct mp3info info; |
Linus Nielsen Feltzing | 478da62 | 2003-04-20 22:00:30 +0000 | [diff] [blame] | 632 | unsigned char toc[100]; |
Jens Arnold | ba966c1 | 2005-09-15 05:29:26 +0000 | [diff] [blame] | 633 | unsigned long header = 0; |
| 634 | unsigned long xing_header_template = header_template; |
| 635 | unsigned long filepos; |
| 636 | long pos, last_pos; |
| 637 | long j; |
| 638 | long bytes; |
| 639 | int i; |
| 640 | int index; |
Linus Nielsen Feltzing | a039091 | 2003-03-10 14:55:31 +0000 | [diff] [blame] | 641 | |
| 642 | DEBUGF("create_xing_header()\n"); |
Linus Nielsen Feltzing | 1936c41 | 2004-01-13 14:06:18 +0000 | [diff] [blame] | 643 | |
Linus Nielsen Feltzing | 6475aa0 | 2003-03-10 18:05:01 +0000 | [diff] [blame] | 644 | if(generate_toc) |
| 645 | { |
Linus Nielsen Feltzing | 478da62 | 2003-04-20 22:00:30 +0000 | [diff] [blame] | 646 | lseek(fd, startpos, SEEK_SET); |
| 647 | buf_init(); |
Jens Arnold | ba966c1 | 2005-09-15 05:29:26 +0000 | [diff] [blame] | 648 | |
Linus Nielsen Feltzing | 6475aa0 | 2003-03-10 18:05:01 +0000 | [diff] [blame] | 649 | /* Generate filepos table */ |
| 650 | last_pos = 0; |
| 651 | filepos = 0; |
| 652 | header = 0; |
Linus Nielsen Feltzing | 6475aa0 | 2003-03-10 18:05:01 +0000 | [diff] [blame] | 653 | for(i = 0;i < 100;i++) { |
| 654 | /* Calculate the absolute frame number for this seek point */ |
| 655 | pos = i * num_frames / 100; |
| 656 | |
| 657 | /* Advance from the last seek point to this one */ |
| 658 | for(j = 0;j < pos - last_pos;j++) |
| 659 | { |
Linus Nielsen Feltzing | 1936c41 | 2004-01-13 14:06:18 +0000 | [diff] [blame] | 660 | header = buf_find_next_frame(fd, &bytes, -1, header_template); |
| 661 | filepos += bytes; |
Linus Nielsen Feltzing | 6475aa0 | 2003-03-10 18:05:01 +0000 | [diff] [blame] | 662 | mp3headerinfo(&info, header); |
| 663 | buf_seek(fd, info.frame_size-4); |
| 664 | filepos += info.frame_size; |
Linus Nielsen Feltzing | 1936c41 | 2004-01-13 14:06:18 +0000 | [diff] [blame] | 665 | |
| 666 | if(!header_template) |
| 667 | header_template = header; |
Linus Nielsen Feltzing | 6475aa0 | 2003-03-10 18:05:01 +0000 | [diff] [blame] | 668 | } |
Linus Nielsen Feltzing | 7fdef57 | 2003-04-19 01:59:23 +0000 | [diff] [blame] | 669 | |
Linus Nielsen Feltzing | fb5f458 | 2003-12-28 03:40:41 +0000 | [diff] [blame] | 670 | /* Save a header for later use if header_template is empty. |
| 671 | We only save one header, and we want to save one in the |
| 672 | middle of the stream, just in case the first and the last |
| 673 | headers are corrupt. */ |
Linus Nielsen Feltzing | 1936c41 | 2004-01-13 14:06:18 +0000 | [diff] [blame] | 674 | if(!xing_header_template && i == 1) |
| 675 | xing_header_template = header; |
Linus Nielsen Feltzing | 6475aa0 | 2003-03-10 18:05:01 +0000 | [diff] [blame] | 676 | |
| 677 | if(progressfunc) |
| 678 | { |
| 679 | progressfunc(50 + i/2); |
| 680 | } |
| 681 | |
Linus Nielsen Feltzing | 478da62 | 2003-04-20 22:00:30 +0000 | [diff] [blame] | 682 | /* Fill in the TOC entry */ |
Daniel Stenberg | 24756e1 | 2003-05-19 14:15:21 +0000 | [diff] [blame] | 683 | /* each toc is a single byte indicating how many 256ths of the |
| 684 | * way through the file, is that percent of the way through the |
| 685 | * song. the easy method, filepos*256/filesize, chokes when |
| 686 | * the upper 8 bits of the file position are nonzero |
| 687 | * (i.e. files over 16mb in size). |
| 688 | */ |
Jens Arnold | ba966c1 | 2005-09-15 05:29:26 +0000 | [diff] [blame] | 689 | if (filepos > (ULONG_MAX/256)) |
Daniel Stenberg | 24756e1 | 2003-05-19 14:15:21 +0000 | [diff] [blame] | 690 | { |
| 691 | /* instead of multiplying filepos by 256, we divide |
| 692 | * filesize by 256. |
| 693 | */ |
| 694 | toc[i] = filepos / (filesize >> 8); |
| 695 | } |
| 696 | else |
| 697 | { |
| 698 | toc[i] = filepos * 256 / filesize; |
| 699 | } |
Linus Nielsen Feltzing | 6475aa0 | 2003-03-10 18:05:01 +0000 | [diff] [blame] | 700 | |
Jens Arnold | f68362a | 2007-03-17 09:54:28 +0000 | [diff] [blame] | 701 | DEBUGF("Pos %d: %ld relpos: %ld filepos: %lx tocentry: %x\n", |
Linus Nielsen Feltzing | 478da62 | 2003-04-20 22:00:30 +0000 | [diff] [blame] | 702 | i, pos, pos-last_pos, filepos, toc[i]); |
Linus Nielsen Feltzing | 6475aa0 | 2003-03-10 18:05:01 +0000 | [diff] [blame] | 703 | |
| 704 | last_pos = pos; |
| 705 | } |
Linus Nielsen Feltzing | 478da62 | 2003-04-20 22:00:30 +0000 | [diff] [blame] | 706 | } |
Jens Arnold | 9352ac8 | 2005-09-04 21:55:15 +0000 | [diff] [blame] | 707 | |
Jens Arnold | ba966c1 | 2005-09-15 05:29:26 +0000 | [diff] [blame] | 708 | /* Use the template header and create a new one. |
| 709 | We ignore the Protection bit even if the rest of the stream is |
| 710 | protected. */ |
| 711 | header = xing_header_template & ~(BITRATE_MASK|PROTECTION_MASK|PADDING_MASK); |
| 712 | header |= 8 << 12; /* This gives us plenty of space, 192..576 bytes */ |
| 713 | |
| 714 | if (!mp3headerinfo(&info, header)) |
Jens Arnold | 9352ac8 | 2005-09-04 21:55:15 +0000 | [diff] [blame] | 715 | return 0; /* invalid header */ |
Jens Arnold | 509117d | 2005-06-04 09:01:40 +0000 | [diff] [blame] | 716 | |
Jens Arnold | ba966c1 | 2005-09-15 05:29:26 +0000 | [diff] [blame] | 717 | if (num_frames == 0 && rec_time) { |
| 718 | /* estimate the number of frames based on the recording time */ |
| 719 | if (rec_time <= ULONG_MAX / info.ft_den) |
| 720 | num_frames = rec_time * info.ft_den / info.ft_num; |
| 721 | else |
| 722 | num_frames = rec_time / info.ft_num * info.ft_den; |
| 723 | } |
| 724 | |
Linus Nielsen Feltzing | 478da62 | 2003-04-20 22:00:30 +0000 | [diff] [blame] | 725 | /* Clear the frame */ |
Jens Arnold | 9352ac8 | 2005-09-04 21:55:15 +0000 | [diff] [blame] | 726 | memset(buf, 0, MAX_XING_HEADER_SIZE); |
Linus Nielsen Feltzing | 478da62 | 2003-04-20 22:00:30 +0000 | [diff] [blame] | 727 | |
Jens Arnold | ba966c1 | 2005-09-15 05:29:26 +0000 | [diff] [blame] | 728 | /* Write the header to the buffer */ |
| 729 | long2bytes(buf, header); |
Jens Arnold | 9352ac8 | 2005-09-04 21:55:15 +0000 | [diff] [blame] | 730 | |
Jens Arnold | ba966c1 | 2005-09-15 05:29:26 +0000 | [diff] [blame] | 731 | /* Calculate position of VBR header */ |
Jens Arnold | 9352ac8 | 2005-09-04 21:55:15 +0000 | [diff] [blame] | 732 | if (info.version == MPEG_VERSION1) { |
Linus Nielsen Feltzing | 478da62 | 2003-04-20 22:00:30 +0000 | [diff] [blame] | 733 | if (info.channel_mode == 3) /* mono */ |
| 734 | index = 21; |
| 735 | else |
| 736 | index = 36; |
Linus Nielsen Feltzing | 7fdef57 | 2003-04-19 01:59:23 +0000 | [diff] [blame] | 737 | } |
Linus Nielsen Feltzing | 478da62 | 2003-04-20 22:00:30 +0000 | [diff] [blame] | 738 | else { |
| 739 | if (info.channel_mode == 3) /* mono */ |
| 740 | index = 13; |
| 741 | else |
| 742 | index = 21; |
| 743 | } |
| 744 | |
Linus Nielsen Feltzing | 478da62 | 2003-04-20 22:00:30 +0000 | [diff] [blame] | 745 | /* Create the Xing data */ |
Jens Arnold | 509117d | 2005-06-04 09:01:40 +0000 | [diff] [blame] | 746 | memcpy(&buf[index], "Xing", 4); |
Jens Arnold | ba966c1 | 2005-09-15 05:29:26 +0000 | [diff] [blame] | 747 | long2bytes(&buf[index+4], (num_frames ? VBR_FRAMES_FLAG : 0) |
| 748 | | (filesize ? VBR_BYTES_FLAG : 0) |
| 749 | | (generate_toc ? VBR_TOC_FLAG : 0)); |
| 750 | index += 8; |
Linus Nielsen Feltzing | 478da62 | 2003-04-20 22:00:30 +0000 | [diff] [blame] | 751 | if(num_frames) |
Linus Nielsen Feltzing | 7fdef57 | 2003-04-19 01:59:23 +0000 | [diff] [blame] | 752 | { |
Jean-Philippe Bernardy | 98a8132 | 2005-01-30 14:11:50 +0000 | [diff] [blame] | 753 | long2bytes(&buf[index], num_frames); |
Linus Nielsen Feltzing | 478da62 | 2003-04-20 22:00:30 +0000 | [diff] [blame] | 754 | index += 4; |
Linus Nielsen Feltzing | 7fdef57 | 2003-04-19 01:59:23 +0000 | [diff] [blame] | 755 | } |
Linus Nielsen Feltzing | 478da62 | 2003-04-20 22:00:30 +0000 | [diff] [blame] | 756 | |
| 757 | if(filesize) |
| 758 | { |
Jean-Philippe Bernardy | 98a8132 | 2005-01-30 14:11:50 +0000 | [diff] [blame] | 759 | long2bytes(&buf[index], filesize - startpos); |
Linus Nielsen Feltzing | 478da62 | 2003-04-20 22:00:30 +0000 | [diff] [blame] | 760 | index += 4; |
| 761 | } |
| 762 | |
| 763 | /* Copy the TOC */ |
| 764 | memcpy(buf + index, toc, 100); |
| 765 | |
| 766 | /* And some extra cool info */ |
Linus Nielsen Feltzing | 1b466e0 | 2003-04-19 02:09:56 +0000 | [diff] [blame] | 767 | memcpy(buf + index + 100, cooltext, sizeof(cooltext)); |
Linus Nielsen Feltzing | 7fdef57 | 2003-04-19 01:59:23 +0000 | [diff] [blame] | 768 | |
Linus Nielsen Feltzing | a039091 | 2003-03-10 14:55:31 +0000 | [diff] [blame] | 769 | #ifdef DEBUG |
Linus Nielsen Feltzing | 126a7b3 | 2003-04-20 22:09:23 +0000 | [diff] [blame] | 770 | for(i = 0;i < info.frame_size;i++) |
Linus Nielsen Feltzing | a039091 | 2003-03-10 14:55:31 +0000 | [diff] [blame] | 771 | { |
| 772 | if(i && !(i % 16)) |
| 773 | DEBUGF("\n"); |
| 774 | |
| 775 | DEBUGF("%02x ", buf[i]); |
| 776 | } |
| 777 | #endif |
| 778 | |
Linus Nielsen Feltzing | 7fdef57 | 2003-04-19 01:59:23 +0000 | [diff] [blame] | 779 | return info.frame_size; |
Linus Nielsen Feltzing | a039091 | 2003-03-10 14:55:31 +0000 | [diff] [blame] | 780 | } |
Miika Pekkarinen | 0dd7ea2 | 2006-11-10 08:03:33 +0000 | [diff] [blame] | 781 | |
| 782 | #endif |