Dave Chapman | 6c082a8 | 2005-06-07 18:53:01 +0000 | [diff] [blame] | 1 | /*************************************************************************** |
| 2 | * __________ __ ___. |
| 3 | * Open \______ \ ____ ____ | | _\_ |__ _______ ___ |
| 4 | * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / |
| 5 | * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < |
| 6 | * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ |
| 7 | * \/ \/ \/ \/ \/ |
| 8 | * $Id$ |
| 9 | * |
Dave Chapman | 273d2e8 | 2005-10-26 12:35:58 +0000 | [diff] [blame] | 10 | * Copyright (C) 2005 Dave Chapman |
Dave Chapman | 6c082a8 | 2005-06-07 18:53:01 +0000 | [diff] [blame] | 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 Chapman | 6c082a8 | 2005-06-07 18:53:01 +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 | |
Thom Johansen | c91e0bb | 2005-10-13 11:32:52 +0000 | [diff] [blame] | 22 | #include "codeclib.h" |
Dave Chapman | 273d2e8 | 2005-10-26 12:35:58 +0000 | [diff] [blame] | 23 | #include <codecs/libffmpegFLAC/decoder.h> |
Dave Chapman | 6c082a8 | 2005-06-07 18:53:01 +0000 | [diff] [blame] | 24 | |
Jens Arnold | b8749fd | 2006-01-18 00:05:14 +0000 | [diff] [blame] | 25 | CODEC_HEADER |
| 26 | |
Dave Chapman | 273d2e8 | 2005-10-26 12:35:58 +0000 | [diff] [blame] | 27 | /* The output buffers containing the decoded samples (channels 0 and 1) */ |
Tomasz Malesinski | 5c54ba4 | 2006-11-09 21:59:27 +0000 | [diff] [blame] | 28 | int32_t decoded0[MAX_BLOCKSIZE] IBSS_ATTR_FLAC_DECODED0; |
Dave Chapman | 273d2e8 | 2005-10-26 12:35:58 +0000 | [diff] [blame] | 29 | int32_t decoded1[MAX_BLOCKSIZE] IBSS_ATTR; |
| 30 | |
Dave Chapman | dff9352 | 2005-10-29 01:39:07 +0000 | [diff] [blame] | 31 | #define MAX_SUPPORTED_SEEKTABLE_SIZE 5000 |
| 32 | |
| 33 | /* Notes about seeking: |
| 34 | |
| 35 | The full seek table consists of: |
| 36 | uint64_t sample (only 36 bits are used) |
| 37 | uint64_t offset |
| 38 | uint32_t blocksize |
| 39 | |
Adam Boot | c1916af | 2006-11-08 22:00:45 +0000 | [diff] [blame] | 40 | We also limit the sample and offset values to 32-bits - Rockbox doesn't |
| 41 | support files bigger than 2GB on FAT32 filesystems. |
Dave Chapman | dff9352 | 2005-10-29 01:39:07 +0000 | [diff] [blame] | 42 | |
| 43 | The reference FLAC encoder produces a seek table with points every |
| 44 | 10 seconds, but this can be overridden by the user when encoding a file. |
| 45 | |
| 46 | With the default settings, a typical 4 minute track will contain |
| 47 | 24 seek points. |
| 48 | |
| 49 | Taking the extreme case of a Rockbox supported file to be a 2GB (compressed) |
| 50 | 16-bit/44.1KHz mono stream with a likely uncompressed size of 4GB: |
| 51 | Total duration is: 48694 seconds (about 810 minutes - 13.5 hours) |
| 52 | Total number of seek points: 4869 |
| 53 | |
| 54 | Therefore we limit the number of seek points to 5000. This is a |
| 55 | very extreme case, and requires 5000*8=40000 bytes of storage. |
| 56 | |
| 57 | If we come across a FLAC file with more than this number of seekpoints, we |
| 58 | just use the first 5000. |
| 59 | |
| 60 | */ |
| 61 | |
| 62 | struct FLACseekpoints { |
| 63 | uint32_t sample; |
| 64 | uint32_t offset; |
Adam Boot | c1916af | 2006-11-08 22:00:45 +0000 | [diff] [blame] | 65 | uint16_t blocksize; |
Dave Chapman | dff9352 | 2005-10-29 01:39:07 +0000 | [diff] [blame] | 66 | }; |
| 67 | |
| 68 | struct FLACseekpoints seekpoints[MAX_SUPPORTED_SEEKTABLE_SIZE]; |
| 69 | int nseekpoints; |
| 70 | |
Adam Boot | c1916af | 2006-11-08 22:00:45 +0000 | [diff] [blame] | 71 | static int8_t *bit_buffer; |
| 72 | static size_t buff_size; |
| 73 | |
Dave Chapman | 15a830b | 2005-10-30 15:07:40 +0000 | [diff] [blame] | 74 | static bool flac_init(FLACContext* fc, int first_frame_offset) |
Ryan Jackson | b301b43 | 2005-07-28 18:43:33 +0000 | [diff] [blame] | 75 | { |
Dave Chapman | 273d2e8 | 2005-10-26 12:35:58 +0000 | [diff] [blame] | 76 | unsigned char buf[255]; |
| 77 | bool found_streaminfo=false; |
Dave Chapman | dff9352 | 2005-10-29 01:39:07 +0000 | [diff] [blame] | 78 | uint32_t seekpoint_hi,seekpoint_lo; |
| 79 | uint32_t offset_hi,offset_lo; |
Adam Boot | c1916af | 2006-11-08 22:00:45 +0000 | [diff] [blame] | 80 | uint16_t blocksize; |
Dave Chapman | 273d2e8 | 2005-10-26 12:35:58 +0000 | [diff] [blame] | 81 | int endofmetadata=0; |
Brandon Low | 4bde898 | 2007-10-25 18:58:44 +0000 | [diff] [blame] | 82 | uint32_t blocklength; |
Ryan Jackson | b301b43 | 2005-07-28 18:43:33 +0000 | [diff] [blame] | 83 | |
Dave Chapman | d1dc0bd | 2005-10-27 13:18:05 +0000 | [diff] [blame] | 84 | ci->memset(fc,0,sizeof(FLACContext)); |
Dave Chapman | dff9352 | 2005-10-29 01:39:07 +0000 | [diff] [blame] | 85 | nseekpoints=0; |
Dave Chapman | d1dc0bd | 2005-10-27 13:18:05 +0000 | [diff] [blame] | 86 | |
Adam Boot | c1916af | 2006-11-08 22:00:45 +0000 | [diff] [blame] | 87 | fc->sample_skip = 0; |
| 88 | |
Dave Chapman | 15a830b | 2005-10-30 15:07:40 +0000 | [diff] [blame] | 89 | /* Skip any foreign tags at start of file */ |
| 90 | ci->seek_buffer(first_frame_offset); |
| 91 | |
| 92 | fc->metadatalength = first_frame_offset; |
| 93 | |
Dave Chapman | 273d2e8 | 2005-10-26 12:35:58 +0000 | [diff] [blame] | 94 | if (ci->read_filebuf(buf, 4) < 4) |
| 95 | { |
| 96 | return false; |
Ryan Jackson | b301b43 | 2005-07-28 18:43:33 +0000 | [diff] [blame] | 97 | } |
| 98 | |
Dave Chapman | 273d2e8 | 2005-10-26 12:35:58 +0000 | [diff] [blame] | 99 | if (ci->memcmp(buf,"fLaC",4) != 0) |
| 100 | { |
| 101 | return false; |
| 102 | } |
Dave Chapman | 15a830b | 2005-10-30 15:07:40 +0000 | [diff] [blame] | 103 | fc->metadatalength += 4; |
Ryan Jackson | b301b43 | 2005-07-28 18:43:33 +0000 | [diff] [blame] | 104 | |
Dave Chapman | 273d2e8 | 2005-10-26 12:35:58 +0000 | [diff] [blame] | 105 | while (!endofmetadata) { |
| 106 | if (ci->read_filebuf(buf, 4) < 4) |
| 107 | { |
| 108 | return false; |
| 109 | } |
Ryan Jackson | b301b43 | 2005-07-28 18:43:33 +0000 | [diff] [blame] | 110 | |
Dave Chapman | 273d2e8 | 2005-10-26 12:35:58 +0000 | [diff] [blame] | 111 | endofmetadata=(buf[0]&0x80); |
| 112 | blocklength = (buf[1] << 16) | (buf[2] << 8) | buf[3]; |
| 113 | fc->metadatalength+=blocklength+4; |
Ryan Jackson | b301b43 | 2005-07-28 18:43:33 +0000 | [diff] [blame] | 114 | |
Dave Chapman | 273d2e8 | 2005-10-26 12:35:58 +0000 | [diff] [blame] | 115 | if ((buf[0] & 0x7f) == 0) /* 0 is the STREAMINFO block */ |
| 116 | { |
Brandon Low | 4bde898 | 2007-10-25 18:58:44 +0000 | [diff] [blame] | 117 | if (ci->read_filebuf(buf, blocklength) < blocklength) return false; |
Dave Chapman | 273d2e8 | 2005-10-26 12:35:58 +0000 | [diff] [blame] | 118 | |
| 119 | fc->filesize = ci->filesize; |
| 120 | fc->min_blocksize = (buf[0] << 8) | buf[1]; |
| 121 | fc->max_blocksize = (buf[2] << 8) | buf[3]; |
| 122 | fc->min_framesize = (buf[4] << 16) | (buf[5] << 8) | buf[6]; |
| 123 | fc->max_framesize = (buf[7] << 16) | (buf[8] << 8) | buf[9]; |
| 124 | fc->samplerate = (buf[10] << 12) | (buf[11] << 4) |
| 125 | | ((buf[12] & 0xf0) >> 4); |
| 126 | fc->channels = ((buf[12]&0x0e)>>1) + 1; |
| 127 | fc->bps = (((buf[12]&0x01) << 4) | ((buf[13]&0xf0)>>4) ) + 1; |
Ryan Jackson | b301b43 | 2005-07-28 18:43:33 +0000 | [diff] [blame] | 128 | |
Dave Chapman | 273d2e8 | 2005-10-26 12:35:58 +0000 | [diff] [blame] | 129 | /* totalsamples is a 36-bit field, but we assume <= 32 bits are |
| 130 | used */ |
| 131 | fc->totalsamples = (buf[14] << 24) | (buf[15] << 16) |
| 132 | | (buf[16] << 8) | buf[17]; |
| 133 | |
| 134 | /* Calculate track length (in ms) and estimate the bitrate |
| 135 | (in kbit/s) */ |
| 136 | fc->length = (fc->totalsamples / fc->samplerate) * 1000; |
| 137 | |
| 138 | found_streaminfo=true; |
| 139 | } else if ((buf[0] & 0x7f) == 3) { /* 3 is the SEEKTABLE block */ |
Dave Chapman | dff9352 | 2005-10-29 01:39:07 +0000 | [diff] [blame] | 140 | while ((nseekpoints < MAX_SUPPORTED_SEEKTABLE_SIZE) && |
| 141 | (blocklength >= 18)) { |
Brandon Low | 4bde898 | 2007-10-25 18:58:44 +0000 | [diff] [blame] | 142 | if (ci->read_filebuf(buf,18) < 18) return false; |
| 143 | blocklength-=18; |
Dave Chapman | dff9352 | 2005-10-29 01:39:07 +0000 | [diff] [blame] | 144 | |
Dave Chapman | b0302f0 | 2006-01-29 01:31:28 +0000 | [diff] [blame] | 145 | seekpoint_hi=(buf[0] << 24) | (buf[1] << 16) | |
| 146 | (buf[2] << 8) | buf[3]; |
| 147 | seekpoint_lo=(buf[4] << 24) | (buf[5] << 16) | |
| 148 | (buf[6] << 8) | buf[7]; |
| 149 | offset_hi=(buf[8] << 24) | (buf[9] << 16) | |
| 150 | (buf[10] << 8) | buf[11]; |
| 151 | offset_lo=(buf[12] << 24) | (buf[13] << 16) | |
| 152 | (buf[14] << 8) | buf[15]; |
| 153 | |
Adam Boot | c1916af | 2006-11-08 22:00:45 +0000 | [diff] [blame] | 154 | blocksize=(buf[16] << 8) | buf[17]; |
Dave Chapman | b0302f0 | 2006-01-29 01:31:28 +0000 | [diff] [blame] | 155 | |
| 156 | /* Only store seekpoints where the high 32 bits are zero */ |
Dave Chapman | dff9352 | 2005-10-29 01:39:07 +0000 | [diff] [blame] | 157 | if ((seekpoint_hi == 0) && (seekpoint_lo != 0xffffffff) && |
| 158 | (offset_hi == 0)) { |
| 159 | seekpoints[nseekpoints].sample=seekpoint_lo; |
| 160 | seekpoints[nseekpoints].offset=offset_lo; |
Adam Boot | c1916af | 2006-11-08 22:00:45 +0000 | [diff] [blame] | 161 | seekpoints[nseekpoints].blocksize=blocksize; |
Dave Chapman | dff9352 | 2005-10-29 01:39:07 +0000 | [diff] [blame] | 162 | nseekpoints++; |
| 163 | } |
| 164 | } |
| 165 | /* Skip any unread seekpoints */ |
| 166 | if (blocklength > 0) |
| 167 | ci->advance_buffer(blocklength); |
Ryan Jackson | b301b43 | 2005-07-28 18:43:33 +0000 | [diff] [blame] | 168 | } else { |
Dave Chapman | 273d2e8 | 2005-10-26 12:35:58 +0000 | [diff] [blame] | 169 | /* Skip to next metadata block */ |
| 170 | ci->advance_buffer(blocklength); |
Ryan Jackson | b301b43 | 2005-07-28 18:43:33 +0000 | [diff] [blame] | 171 | } |
| 172 | } |
| 173 | |
Dave Chapman | 273d2e8 | 2005-10-26 12:35:58 +0000 | [diff] [blame] | 174 | if (found_streaminfo) { |
| 175 | fc->bitrate = ((fc->filesize-fc->metadatalength) * 8) / fc->length; |
| 176 | return true; |
| 177 | } else { |
| 178 | return false; |
| 179 | } |
Ryan Jackson | b301b43 | 2005-07-28 18:43:33 +0000 | [diff] [blame] | 180 | } |
| 181 | |
Adam Boot | c1916af | 2006-11-08 22:00:45 +0000 | [diff] [blame] | 182 | /* Synchronize to next frame in stream - adapted from libFLAC 1.1.3b2 */ |
| 183 | bool frame_sync(FLACContext* fc) { |
| 184 | unsigned int x = 0; |
| 185 | bool cached = false; |
Dave Chapman | dff9352 | 2005-10-29 01:39:07 +0000 | [diff] [blame] | 186 | |
Adam Boot | c1916af | 2006-11-08 22:00:45 +0000 | [diff] [blame] | 187 | /* Make sure we're byte aligned. */ |
| 188 | align_get_bits(&fc->gb); |
Dave Chapman | dff9352 | 2005-10-29 01:39:07 +0000 | [diff] [blame] | 189 | |
Adam Boot | c1916af | 2006-11-08 22:00:45 +0000 | [diff] [blame] | 190 | while(1) { |
| 191 | if(fc->gb.size_in_bits - get_bits_count(&fc->gb) < 8) { |
| 192 | /* Error, end of bitstream, a valid stream should never reach here |
| 193 | * since the buffer should contain at least one frame header. |
| 194 | */ |
| 195 | return false; |
| 196 | } |
Dave Chapman | dff9352 | 2005-10-29 01:39:07 +0000 | [diff] [blame] | 197 | |
Adam Boot | c1916af | 2006-11-08 22:00:45 +0000 | [diff] [blame] | 198 | if(cached) |
| 199 | cached = false; |
| 200 | else |
| 201 | x = get_bits(&fc->gb, 8); |
Dave Chapman | dff9352 | 2005-10-29 01:39:07 +0000 | [diff] [blame] | 202 | |
Adam Boot | c1916af | 2006-11-08 22:00:45 +0000 | [diff] [blame] | 203 | if(x == 0xff) { /* MAGIC NUMBER for first 8 frame sync bits. */ |
| 204 | x = get_bits(&fc->gb, 8); |
| 205 | /* We have to check if we just read two 0xff's in a row; the second |
| 206 | * may actually be the beginning of the sync code. |
| 207 | */ |
| 208 | if(x == 0xff) { /* MAGIC NUMBER for first 8 frame sync bits. */ |
| 209 | cached = true; |
| 210 | } |
| 211 | else if(x >> 2 == 0x3e) { /* MAGIC NUMBER for last 6 sync bits. */ |
| 212 | /* Succesfully synced. */ |
| 213 | break; |
| 214 | } |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | /* Advance and init bit buffer to the new frame. */ |
| 219 | ci->advance_buffer((get_bits_count(&fc->gb)-16)>>3); /* consumed bytes */ |
Adam Boot | 5931ab2 | 2006-12-07 20:13:58 +0000 | [diff] [blame] | 220 | bit_buffer = ci->request_buffer(&buff_size, MAX_FRAMESIZE+16); |
Adam Boot | c1916af | 2006-11-08 22:00:45 +0000 | [diff] [blame] | 221 | init_get_bits(&fc->gb, bit_buffer, buff_size*8); |
| 222 | |
| 223 | /* Decode the frame to verify the frame crc and |
| 224 | * fill fc with its metadata. |
| 225 | */ |
| 226 | if(flac_decode_frame(fc, decoded0, decoded1, |
| 227 | bit_buffer, buff_size, ci->yield) < 0) { |
| 228 | return false; |
| 229 | } |
| 230 | |
| 231 | return true; |
Magnus Holmgren | f95dd56 | 2006-06-04 15:04:03 +0000 | [diff] [blame] | 232 | } |
Dave Chapman | dff9352 | 2005-10-29 01:39:07 +0000 | [diff] [blame] | 233 | |
Adam Boot | c1916af | 2006-11-08 22:00:45 +0000 | [diff] [blame] | 234 | /* Seek to sample - adapted from libFLAC 1.1.3b2+ */ |
| 235 | bool flac_seek(FLACContext* fc, uint32_t target_sample) { |
| 236 | off_t orig_pos = ci->curpos; |
| 237 | off_t pos = -1; |
| 238 | unsigned long lower_bound, upper_bound; |
| 239 | unsigned long lower_bound_sample, upper_bound_sample; |
| 240 | int i; |
| 241 | unsigned approx_bytes_per_frame; |
| 242 | uint32_t this_frame_sample = fc->samplenumber; |
| 243 | unsigned this_block_size = fc->blocksize; |
| 244 | bool needs_seek = true, first_seek = true; |
Magnus Holmgren | f95dd56 | 2006-06-04 15:04:03 +0000 | [diff] [blame] | 245 | |
Adam Boot | c1916af | 2006-11-08 22:00:45 +0000 | [diff] [blame] | 246 | /* We are just guessing here. */ |
| 247 | if(fc->max_framesize > 0) |
| 248 | approx_bytes_per_frame = (fc->max_framesize + fc->min_framesize)/2 + 1; |
| 249 | /* Check if it's a known fixed-blocksize stream. */ |
| 250 | else if(fc->min_blocksize == fc->max_blocksize && fc->min_blocksize > 0) |
| 251 | approx_bytes_per_frame = fc->min_blocksize*fc->channels*fc->bps/8 + 64; |
| 252 | else |
| 253 | approx_bytes_per_frame = 4608 * fc->channels * fc->bps/8 + 64; |
| 254 | |
| 255 | /* Set an upper and lower bound on where in the stream we will search. */ |
| 256 | lower_bound = fc->metadatalength; |
| 257 | lower_bound_sample = 0; |
| 258 | upper_bound = fc->filesize; |
| 259 | upper_bound_sample = fc->totalsamples>0 ? fc->totalsamples : target_sample; |
| 260 | |
| 261 | /* Refine the bounds if we have a seektable with suitable points. */ |
| 262 | if(nseekpoints > 0) { |
| 263 | /* Find the closest seek point <= target_sample, if it exists. */ |
| 264 | for(i = nseekpoints-1; i >= 0; i--) { |
| 265 | if(seekpoints[i].sample <= target_sample) |
| 266 | break; |
| 267 | } |
| 268 | if(i >= 0) { /* i.e. we found a suitable seek point... */ |
| 269 | lower_bound = fc->metadatalength + seekpoints[i].offset; |
| 270 | lower_bound_sample = seekpoints[i].sample; |
| 271 | } |
| 272 | |
| 273 | /* Find the closest seek point > target_sample, if it exists. */ |
| 274 | for(i = 0; i < nseekpoints; i++) { |
| 275 | if(seekpoints[i].sample > target_sample) |
| 276 | break; |
| 277 | } |
| 278 | if(i < nseekpoints) { /* i.e. we found a suitable seek point... */ |
| 279 | upper_bound = fc->metadatalength + seekpoints[i].offset; |
| 280 | upper_bound_sample = seekpoints[i].sample; |
| 281 | } |
| 282 | } |
| 283 | |
| 284 | while(1) { |
| 285 | /* Check if bounds are still ok. */ |
| 286 | if(lower_bound_sample >= upper_bound_sample || |
| 287 | lower_bound > upper_bound) { |
| 288 | return false; |
| 289 | } |
| 290 | |
| 291 | /* Calculate new seek position */ |
| 292 | if(needs_seek) { |
| 293 | pos = (off_t)(lower_bound + |
| 294 | (((target_sample - lower_bound_sample) * |
| 295 | (int64_t)(upper_bound - lower_bound)) / |
| 296 | (upper_bound_sample - lower_bound_sample)) - |
| 297 | approx_bytes_per_frame); |
| 298 | |
| 299 | if(pos >= (off_t)upper_bound) |
| 300 | pos = (off_t)upper_bound-1; |
| 301 | if(pos < (off_t)lower_bound) |
| 302 | pos = (off_t)lower_bound; |
| 303 | } |
| 304 | |
| 305 | if(!ci->seek_buffer(pos)) |
| 306 | return false; |
| 307 | |
Adam Boot | 5931ab2 | 2006-12-07 20:13:58 +0000 | [diff] [blame] | 308 | bit_buffer = ci->request_buffer(&buff_size, MAX_FRAMESIZE+16); |
Adam Boot | c1916af | 2006-11-08 22:00:45 +0000 | [diff] [blame] | 309 | init_get_bits(&fc->gb, bit_buffer, buff_size*8); |
| 310 | |
| 311 | /* Now we need to get a frame. It is possible for our seek |
| 312 | * to land in the middle of audio data that looks exactly like |
| 313 | * a frame header from a future version of an encoder. When |
| 314 | * that happens, frame_sync() will return false. |
| 315 | * But there is a remote possibility that it is properly |
| 316 | * synced at such a "future-codec frame", so to make sure, |
| 317 | * we wait to see several "unparseable" errors in a row before |
| 318 | * bailing out. |
| 319 | */ |
| 320 | { |
| 321 | unsigned unparseable_count; |
| 322 | bool got_a_frame = false; |
| 323 | for(unparseable_count = 0; !got_a_frame |
| 324 | && unparseable_count < 10; unparseable_count++) { |
| 325 | if(frame_sync(fc)) |
| 326 | got_a_frame = true; |
| 327 | } |
| 328 | if(!got_a_frame) { |
| 329 | ci->seek_buffer(orig_pos); |
| 330 | return false; |
| 331 | } |
| 332 | } |
| 333 | |
| 334 | this_frame_sample = fc->samplenumber; |
| 335 | this_block_size = fc->blocksize; |
| 336 | |
| 337 | if(target_sample >= this_frame_sample |
| 338 | && target_sample < this_frame_sample+this_block_size) { |
| 339 | /* Found the frame containing the target sample. */ |
| 340 | fc->sample_skip = target_sample - this_frame_sample; |
| 341 | break; |
| 342 | } |
| 343 | |
| 344 | if(this_frame_sample + this_block_size >= upper_bound_sample && |
| 345 | !first_seek) { |
| 346 | if(pos == (off_t)lower_bound || !needs_seek) { |
| 347 | ci->seek_buffer(orig_pos); |
| 348 | return false; |
| 349 | } |
| 350 | /* Our last move backwards wasn't big enough, try again. */ |
| 351 | approx_bytes_per_frame *= 2; |
| 352 | continue; |
| 353 | } |
| 354 | /* Allow one seek over upper bound, |
| 355 | * required for streams with unknown total samples. |
| 356 | */ |
| 357 | first_seek = false; |
| 358 | |
| 359 | /* Make sure we are not seeking in a corrupted stream */ |
| 360 | if(this_frame_sample < lower_bound_sample) { |
| 361 | ci->seek_buffer(orig_pos); |
| 362 | return false; |
| 363 | } |
| 364 | |
| 365 | approx_bytes_per_frame = this_block_size*fc->channels*fc->bps/8 + 64; |
| 366 | |
| 367 | /* We need to narrow the search. */ |
| 368 | if(target_sample < this_frame_sample) { |
Adam Boot | 5931ab2 | 2006-12-07 20:13:58 +0000 | [diff] [blame] | 369 | upper_bound_sample = this_frame_sample; |
Adam Boot | c1916af | 2006-11-08 22:00:45 +0000 | [diff] [blame] | 370 | upper_bound = ci->curpos; |
| 371 | } |
| 372 | else { /* Target is beyond this frame. */ |
| 373 | /* We are close, continue in decoding next frames. */ |
| 374 | if(target_sample < this_frame_sample + 4*this_block_size) { |
| 375 | pos = ci->curpos + fc->framesize; |
| 376 | needs_seek = false; |
| 377 | } |
| 378 | |
| 379 | lower_bound_sample = this_frame_sample + this_block_size; |
| 380 | lower_bound = ci->curpos + fc->framesize; |
| 381 | } |
| 382 | } |
| 383 | |
| 384 | return true; |
| 385 | } |
| 386 | |
| 387 | /* Seek to file offset */ |
Magnus Holmgren | f95dd56 | 2006-06-04 15:04:03 +0000 | [diff] [blame] | 388 | bool flac_seek_offset(FLACContext* fc, uint32_t offset) { |
Adam Boot | c1916af | 2006-11-08 22:00:45 +0000 | [diff] [blame] | 389 | unsigned unparseable_count; |
| 390 | bool got_a_frame = false; |
Magnus Holmgren | f95dd56 | 2006-06-04 15:04:03 +0000 | [diff] [blame] | 391 | |
Adam Boot | c1916af | 2006-11-08 22:00:45 +0000 | [diff] [blame] | 392 | if(!ci->seek_buffer(offset)) |
| 393 | return false; |
Magnus Holmgren | f95dd56 | 2006-06-04 15:04:03 +0000 | [diff] [blame] | 394 | |
Adam Boot | c1916af | 2006-11-08 22:00:45 +0000 | [diff] [blame] | 395 | bit_buffer = ci->request_buffer(&buff_size, MAX_FRAMESIZE); |
| 396 | init_get_bits(&fc->gb, bit_buffer, buff_size*8); |
| 397 | |
| 398 | for(unparseable_count = 0; !got_a_frame |
| 399 | && unparseable_count < 10; unparseable_count++) { |
| 400 | if(frame_sync(fc)) |
| 401 | got_a_frame = true; |
| 402 | } |
| 403 | |
| 404 | if(!got_a_frame) { |
| 405 | ci->seek_buffer(fc->metadatalength); |
| 406 | return false; |
| 407 | } |
| 408 | |
| 409 | return true; |
Dave Chapman | dff9352 | 2005-10-29 01:39:07 +0000 | [diff] [blame] | 410 | } |
| 411 | |
Daniel Stenberg | 1dd672f | 2005-06-22 19:41:30 +0000 | [diff] [blame] | 412 | /* this is the codec entry point */ |
Tomasz Malesinski | 80da8b1 | 2006-11-26 18:31:41 +0000 | [diff] [blame] | 413 | enum codec_status codec_main(void) |
Dave Chapman | 6c082a8 | 2005-06-07 18:53:01 +0000 | [diff] [blame] | 414 | { |
Miika Pekkarinen | 169c935 | 2005-10-30 07:38:52 +0000 | [diff] [blame] | 415 | int8_t *buf; |
Dave Chapman | 273d2e8 | 2005-10-26 12:35:58 +0000 | [diff] [blame] | 416 | FLACContext fc; |
Brandon Low | f3bc1ef | 2006-04-22 14:40:13 +0000 | [diff] [blame] | 417 | uint32_t samplesdone = 0; |
Dave Chapman | 273d2e8 | 2005-10-26 12:35:58 +0000 | [diff] [blame] | 418 | uint32_t elapsedtime; |
Brandon Low | c76904b | 2006-03-24 14:02:27 +0000 | [diff] [blame] | 419 | size_t bytesleft; |
Dave Chapman | 273d2e8 | 2005-10-26 12:35:58 +0000 | [diff] [blame] | 420 | int consumed; |
Dave Chapman | e12ab49 | 2005-10-28 20:09:47 +0000 | [diff] [blame] | 421 | int res; |
| 422 | int frame; |
Brandon Low | 1060e44 | 2006-01-18 20:22:03 +0000 | [diff] [blame] | 423 | int retval; |
Dave Chapman | 6c082a8 | 2005-06-07 18:53:01 +0000 | [diff] [blame] | 424 | |
Dave Chapman | 273d2e8 | 2005-10-26 12:35:58 +0000 | [diff] [blame] | 425 | /* Generic codec initialisation */ |
Michael Sevakis | 97f369a | 2007-02-10 16:34:16 +0000 | [diff] [blame] | 426 | ci->configure(CODEC_SET_FILEBUF_WATERMARK, 1024*512); |
Dave Chapman | 6c082a8 | 2005-06-07 18:53:01 +0000 | [diff] [blame] | 427 | |
Michael Sevakis | 97f369a | 2007-02-10 16:34:16 +0000 | [diff] [blame] | 428 | ci->configure(DSP_SET_SAMPLE_DEPTH, FLAC_OUTPUT_DEPTH-1); |
Brandon Low | 1060e44 | 2006-01-18 20:22:03 +0000 | [diff] [blame] | 429 | |
Dave Chapman | 273d2e8 | 2005-10-26 12:35:58 +0000 | [diff] [blame] | 430 | next_track: |
Magnus Holmgren | f95dd56 | 2006-06-04 15:04:03 +0000 | [diff] [blame] | 431 | |
| 432 | /* Need to save offset for later use (cleared indirectly by flac_init) */ |
| 433 | samplesdone=ci->id3->offset; |
Dave Chapman | 6c082a8 | 2005-06-07 18:53:01 +0000 | [diff] [blame] | 434 | |
Tomasz Malesinski | 80da8b1 | 2006-11-26 18:31:41 +0000 | [diff] [blame] | 435 | if (codec_init()) { |
Dave Chapman | 15a830b | 2005-10-30 15:07:40 +0000 | [diff] [blame] | 436 | LOGF("FLAC: Error initialising codec\n"); |
Brandon Low | 1060e44 | 2006-01-18 20:22:03 +0000 | [diff] [blame] | 437 | retval = CODEC_ERROR; |
| 438 | goto exit; |
Dave Chapman | 15a830b | 2005-10-30 15:07:40 +0000 | [diff] [blame] | 439 | } |
| 440 | |
Magnus Holmgren | 313a6d6 | 2008-02-24 13:22:40 +0000 | [diff] [blame] | 441 | while (!*ci->taginfo_ready && !ci->stop_codec) |
| 442 | ci->sleep(1); |
| 443 | |
Dave Chapman | 15a830b | 2005-10-30 15:07:40 +0000 | [diff] [blame] | 444 | if (!flac_init(&fc,ci->id3->first_frame_offset)) { |
Dave Chapman | 273d2e8 | 2005-10-26 12:35:58 +0000 | [diff] [blame] | 445 | LOGF("FLAC: Error initialising codec\n"); |
Brandon Low | 1060e44 | 2006-01-18 20:22:03 +0000 | [diff] [blame] | 446 | retval = CODEC_ERROR; |
Brandon Low | f3bc1ef | 2006-04-22 14:40:13 +0000 | [diff] [blame] | 447 | goto done; |
Ryan Jackson | b301b43 | 2005-07-28 18:43:33 +0000 | [diff] [blame] | 448 | } |
| 449 | |
Michael Sevakis | 97f369a | 2007-02-10 16:34:16 +0000 | [diff] [blame] | 450 | ci->configure(DSP_SWITCH_FREQUENCY, ci->id3->frequency); |
Jens Arnold | 7ebb291 | 2007-02-04 12:35:44 +0000 | [diff] [blame] | 451 | ci->configure(DSP_SET_STEREO_MODE, fc.channels == 1 ? |
Michael Sevakis | 97f369a | 2007-02-10 16:34:16 +0000 | [diff] [blame] | 452 | STEREO_MONO : STEREO_NONINTERLEAVED); |
Dave Chapman | 057cc06 | 2005-11-02 17:26:47 +0000 | [diff] [blame] | 453 | codec_set_replaygain(ci->id3); |
Dave Chapman | 6c082a8 | 2005-06-07 18:53:01 +0000 | [diff] [blame] | 454 | |
Magnus Holmgren | f95dd56 | 2006-06-04 15:04:03 +0000 | [diff] [blame] | 455 | if (samplesdone) { |
| 456 | flac_seek_offset(&fc, samplesdone); |
| 457 | samplesdone=0; |
| 458 | } |
| 459 | |
Dave Chapman | 273d2e8 | 2005-10-26 12:35:58 +0000 | [diff] [blame] | 460 | /* The main decoding loop */ |
Dave Chapman | e12ab49 | 2005-10-28 20:09:47 +0000 | [diff] [blame] | 461 | frame=0; |
Miika Pekkarinen | 169c935 | 2005-10-30 07:38:52 +0000 | [diff] [blame] | 462 | buf = ci->request_buffer(&bytesleft, MAX_FRAMESIZE); |
Dave Chapman | 273d2e8 | 2005-10-26 12:35:58 +0000 | [diff] [blame] | 463 | while (bytesleft) { |
Thom Johansen | 5e3e957 | 2005-09-07 02:22:26 +0000 | [diff] [blame] | 464 | ci->yield(); |
Brandon Low | ebadcc6 | 2006-04-15 02:03:11 +0000 | [diff] [blame] | 465 | if (ci->stop_codec || ci->new_track) { |
Thom Johansen | 5e3e957 | 2005-09-07 02:22:26 +0000 | [diff] [blame] | 466 | break; |
Dave Chapman | defdf4b | 2005-06-18 20:57:01 +0000 | [diff] [blame] | 467 | } |
Ryan Jackson | b301b43 | 2005-07-28 18:43:33 +0000 | [diff] [blame] | 468 | |
Dave Chapman | 273d2e8 | 2005-10-26 12:35:58 +0000 | [diff] [blame] | 469 | /* Deal with any pending seek requests */ |
Ryan Jackson | b301b43 | 2005-07-28 18:43:33 +0000 | [diff] [blame] | 470 | if (ci->seek_time) { |
Adam Boot | c1916af | 2006-11-08 22:00:45 +0000 | [diff] [blame] | 471 | if (flac_seek(&fc,(uint32_t)(((uint64_t)(ci->seek_time-1) |
| 472 | *ci->id3->frequency)/1000))) { |
Dave Chapman | dff9352 | 2005-10-29 01:39:07 +0000 | [diff] [blame] | 473 | /* Refill the input buffer */ |
Miika Pekkarinen | 169c935 | 2005-10-30 07:38:52 +0000 | [diff] [blame] | 474 | buf = ci->request_buffer(&bytesleft, MAX_FRAMESIZE); |
Dave Chapman | dff9352 | 2005-10-29 01:39:07 +0000 | [diff] [blame] | 475 | } |
Dave Chapman | 5006d15 | 2005-11-02 00:09:42 +0000 | [diff] [blame] | 476 | ci->seek_complete(); |
Ryan Jackson | b301b43 | 2005-07-28 18:43:33 +0000 | [diff] [blame] | 477 | } |
| 478 | |
Dave Chapman | e12ab49 | 2005-10-28 20:09:47 +0000 | [diff] [blame] | 479 | if((res=flac_decode_frame(&fc,decoded0,decoded1,buf, |
| 480 | bytesleft,ci->yield)) < 0) { |
| 481 | LOGF("FLAC: Frame %d, error %d\n",frame,res); |
Brandon Low | 1060e44 | 2006-01-18 20:22:03 +0000 | [diff] [blame] | 482 | retval = CODEC_ERROR; |
Brandon Low | f3bc1ef | 2006-04-22 14:40:13 +0000 | [diff] [blame] | 483 | goto done; |
Dave Chapman | 273d2e8 | 2005-10-26 12:35:58 +0000 | [diff] [blame] | 484 | } |
| 485 | consumed=fc.gb.index/8; |
Dave Chapman | e12ab49 | 2005-10-28 20:09:47 +0000 | [diff] [blame] | 486 | frame++; |
Dave Chapman | 273d2e8 | 2005-10-26 12:35:58 +0000 | [diff] [blame] | 487 | |
| 488 | ci->yield(); |
Michael Sevakis | aba6ca0 | 2007-02-07 00:51:50 +0000 | [diff] [blame] | 489 | ci->pcmbuf_insert(&decoded0[fc.sample_skip], &decoded1[fc.sample_skip], |
| 490 | fc.blocksize - fc.sample_skip); |
Adam Boot | c1916af | 2006-11-08 22:00:45 +0000 | [diff] [blame] | 491 | |
| 492 | fc.sample_skip = 0; |
Dave Chapman | 273d2e8 | 2005-10-26 12:35:58 +0000 | [diff] [blame] | 493 | |
| 494 | /* Update the elapsed-time indicator */ |
| 495 | samplesdone=fc.samplenumber+fc.blocksize; |
| 496 | elapsedtime=(samplesdone*10)/(ci->id3->frequency/100); |
| 497 | ci->set_elapsed(elapsedtime); |
| 498 | |
Miika Pekkarinen | 169c935 | 2005-10-30 07:38:52 +0000 | [diff] [blame] | 499 | ci->advance_buffer(consumed); |
Dave Chapman | 273d2e8 | 2005-10-26 12:35:58 +0000 | [diff] [blame] | 500 | |
Miika Pekkarinen | 169c935 | 2005-10-30 07:38:52 +0000 | [diff] [blame] | 501 | buf = ci->request_buffer(&bytesleft, MAX_FRAMESIZE); |
Dave Chapman | defdf4b | 2005-06-18 20:57:01 +0000 | [diff] [blame] | 502 | } |
Brandon Low | f3bc1ef | 2006-04-22 14:40:13 +0000 | [diff] [blame] | 503 | retval = CODEC_OK; |
| 504 | |
| 505 | done: |
Magnus Holmgren | 01a010f | 2007-03-18 09:50:53 +0000 | [diff] [blame] | 506 | LOGF("FLAC: Decoded %ld samples\n",samplesdone); |
Dave Chapman | defdf4b | 2005-06-18 20:57:01 +0000 | [diff] [blame] | 507 | |
Dave Chapman | 273d2e8 | 2005-10-26 12:35:58 +0000 | [diff] [blame] | 508 | if (ci->request_next_track()) |
Ryan Jackson | b301b43 | 2005-07-28 18:43:33 +0000 | [diff] [blame] | 509 | goto next_track; |
Dave Chapman | b15e546 | 2005-06-07 20:09:53 +0000 | [diff] [blame] | 510 | |
Brandon Low | 1060e44 | 2006-01-18 20:22:03 +0000 | [diff] [blame] | 511 | exit: |
| 512 | return retval; |
Dave Chapman | 6c082a8 | 2005-06-07 18:53:01 +0000 | [diff] [blame] | 513 | } |