244ef256445b85b67b83653818db7d4ddea095a5
[kugel-rb.git] / apps / codecs / libmusepack / mpc_demux.c
blob244ef256445b85b67b83653818db7d4ddea095a5
1 /*
2 Copyright (c) 2005-2009, The Musepack Development Team
3 All rights reserved.
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are
7 met:
9 * Redistributions of source code must retain the above copyright
10 notice, this list of conditions and the following disclaimer.
12 * Redistributions in binary form must reproduce the above
13 copyright notice, this list of conditions and the following
14 disclaimer in the documentation and/or other materials provided
15 with the distribution.
17 * Neither the name of the The Musepack Development Team nor the
18 names of its contributors may be used to endorse or promote
19 products derived from this software without specific prior
20 written permission.
22 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
25 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
26 OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
28 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
32 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 #include <math.h>
36 #include <string.h>
37 #include "streaminfo.h"
38 #include "mpcdec.h"
39 #include "internal.h"
40 #include "decoder.h"
41 #include "huffman.h"
42 #include "mpc_bits_reader.h"
44 #include <codeclib.h>
46 /// maximum number of seek points in the table. The distance between points will
47 /// be adapted so this value is never exceeded.
48 #define MAX_SEEK_TABLE_SIZE 8192
50 // globals
51 static mpc_uint8_t g_buffer[DEMUX_BUFFER_SIZE + MAX_FRAME_SIZE];
52 static mpc_seek_t g_seek_table[MAX_SEEK_TABLE_SIZE];
53 static mpc_demux g_mpc_demux IBSS_ATTR;
55 enum {
56 MPC_BUFFER_SWAP = 1,
57 MPC_BUFFER_FULL = 2,
60 static void mpc_demux_clear_buff(mpc_demux * d)
62 d->bytes_total = 0;
63 d->bits_reader.buff = d->buffer;
64 d->bits_reader.count = 8;
65 d->bits_reader.buffered_addr = 0;
66 d->bits_reader.buffered_code = 0;
67 d->block_bits = 0;
68 d->block_frames = 0;
69 memset(d->buffer, 0, sizeof(g_buffer));
72 static mpc_uint32_t
73 mpc_demux_fill(mpc_demux * d, mpc_uint32_t min_bytes, int flags)
75 mpc_uint32_t unread_bytes = d->bytes_total + d->buffer - d->bits_reader.buff
76 - ((8 - d->bits_reader.count) >> 3);
77 int offset = 0;
79 if (min_bytes == 0 || min_bytes > DEMUX_BUFFER_SIZE ||
80 (unread_bytes < min_bytes && flags & MPC_BUFFER_FULL))
81 min_bytes = DEMUX_BUFFER_SIZE;
83 if (unread_bytes < min_bytes) {
84 mpc_uint32_t bytes2read = min_bytes - unread_bytes;
85 mpc_uint32_t bytes_free = DEMUX_BUFFER_SIZE - d->bytes_total;
87 if (flags & MPC_BUFFER_SWAP) {
88 bytes2read &= -1 << 2;
89 offset = (unread_bytes + 3) & ( -1 << 2);
90 offset -= unread_bytes;
93 if (bytes2read > bytes_free) {
94 if (d->bits_reader.count == 0) {
95 d->bits_reader.count = 8;
96 d->bits_reader.buff++;
98 memmove(d->buffer + offset, d->bits_reader.buff, unread_bytes);
99 d->bits_reader.buff = d->buffer + offset;
100 d->bytes_total = unread_bytes + offset;
102 bytes2read = d->r->read(d->r, d->buffer + d->bytes_total, bytes2read);
103 if (flags & MPC_BUFFER_SWAP){
104 unsigned int i, * tmp = (unsigned int *) (d->buffer + d->bytes_total);
105 for(i = 0 ;i < (bytes2read >> 2); i++)
106 tmp[i] = swap32(tmp[i]);
108 d->bytes_total += bytes2read;
109 return bytes2read;
112 return (mpc_uint32_t) -1;
116 * seek to a bit position in the stream
117 * @param d demuxer context
118 * @param fpos position in the stream in bits from the beginning of mpc datas
119 * @param min_bytes number of bytes to load after seeking
121 static void
122 mpc_demux_seek(mpc_demux * d, mpc_seek_t fpos, mpc_uint32_t min_bytes) {
123 mpc_seek_t next_pos;
124 mpc_int_t bit_offset;
126 // FIXME : do not flush the buffer if fpos is in the current buffer
128 next_pos = fpos >> 3;
129 if (d->si.stream_version == 7)
130 next_pos = ((next_pos - d->si.header_position) & (-1 << 2)) + d->si.header_position;
131 bit_offset = (int) (fpos - (next_pos << 3));
133 d->r->seek(d->r, (mpc_int32_t) next_pos);
134 mpc_demux_clear_buff(d);
135 if (d->si.stream_version == 7)
136 mpc_demux_fill(d, (min_bytes + ((bit_offset + 7) >> 3) + 3) & (~3), MPC_BUFFER_SWAP);
137 else
138 mpc_demux_fill(d, min_bytes + ((bit_offset + 7) >> 3), 0);
139 d->bits_reader.buff += bit_offset >> 3;
140 d->bits_reader.count = 8 - (bit_offset & 7);
144 * return the current position in the stream (in bits) from the beginning
145 * of the file
146 * @param d demuxer context
147 * @return current stream position in bits
149 mpc_seek_t mpc_demux_pos(mpc_demux * d)
151 return (((mpc_seek_t)(d->r->tell(d->r)) - d->bytes_total +
152 d->bits_reader.buff - d->buffer) << 3) + 8 - d->bits_reader.count;
156 * Searches for a ID3v2-tag and reads the length (in bytes) of it.
158 * @param d demuxer context
159 * @return size of tag, in bytes
160 * @return MPC_STATUS_FILE on errors of any kind
162 static mpc_int32_t mpc_demux_skip_id3v2(mpc_demux * d)
164 mpc_uint8_t tmp [4];
165 mpc_bool_t footerPresent; // ID3v2.4-flag
166 mpc_int32_t size;
168 // we must be at the beginning of the stream
169 mpc_demux_fill(d, 3, 0);
171 // check id3-tag
172 if ( 0 != memcmp( d->bits_reader.buff, "ID3", 3 ) )
173 return 0;
175 mpc_demux_fill(d, 10, 0);
177 mpc_bits_read(&d->bits_reader, 24); // read ID3
178 mpc_bits_read(&d->bits_reader, 16); // read tag version
180 tmp[0] = mpc_bits_read(&d->bits_reader, 8); // read flags
181 footerPresent = tmp[0] & 0x10;
182 if ( tmp[0] & 0x0F )
183 return MPC_STATUS_FILE; // not (yet???) allowed
185 tmp[0] = mpc_bits_read(&d->bits_reader, 8); // read size
186 tmp[1] = mpc_bits_read(&d->bits_reader, 8); // read size
187 tmp[2] = mpc_bits_read(&d->bits_reader, 8); // read size
188 tmp[3] = mpc_bits_read(&d->bits_reader, 8); // read size
190 if ( (tmp[0] | tmp[1] | tmp[2] | tmp[3]) & 0x80 )
191 return MPC_STATUS_FILE; // not allowed
193 // read headerSize (syncsave: 4 * $0xxxxxxx = 28 significant bits)
194 size = tmp[0] << 21;
195 size |= tmp[1] << 14;
196 size |= tmp[2] << 7;
197 size |= tmp[3];
199 size += 10; //header
201 if ( footerPresent ) size += 10;
203 // This is called before file headers get read, streamversion etc isn't yet known, demuxing isn't properly initialized and we can't call mpc_demux_seek() from here.
204 mpc_demux_clear_buff(d);
205 if (!d->r->seek(d->r, size)) return MPC_STATUS_FILE;
207 return size;
210 static mpc_status mpc_demux_seek_init(mpc_demux * d)
212 size_t seek_table_size;
213 if (d->seek_table != 0)
214 return MPC_STATUS_OK;
216 d->seek_pwr = 6;
217 if (d->si.block_pwr > d->seek_pwr)
218 d->seek_pwr = d->si.block_pwr;
219 seek_table_size = (2 + d->si.samples / (MPC_FRAME_LENGTH << d->seek_pwr));
220 while (seek_table_size > MAX_SEEK_TABLE_SIZE) {
221 d->seek_pwr++;
222 seek_table_size = (2 + d->si.samples / (MPC_FRAME_LENGTH << d->seek_pwr));
224 d->seek_table = g_seek_table;
225 if (d->seek_table == 0)
226 return MPC_STATUS_FILE;
227 d->seek_table[0] = (mpc_seek_t)mpc_demux_pos(d);
228 d->seek_table_size = 1;
230 return MPC_STATUS_OK;
233 static void mpc_demux_ST(mpc_demux * d)
235 mpc_uint64_t tmp;
236 mpc_seek_t * table, last[2];
237 mpc_bits_reader r = d->bits_reader;
238 mpc_uint_t i, diff_pwr = 0, mask;
239 mpc_uint32_t file_table_size;
241 if (d->seek_table != 0)
242 return;
244 mpc_bits_get_size(&r, &tmp);
245 file_table_size = (mpc_seek_t) tmp;
246 d->seek_pwr = d->si.block_pwr + mpc_bits_read(&r, 4);
248 tmp = 2 + d->si.samples / (MPC_FRAME_LENGTH << d->seek_pwr);
249 while (tmp > MAX_SEEK_TABLE_SIZE) {
250 d->seek_pwr++;
251 diff_pwr++;
252 tmp = 2 + d->si.samples / (MPC_FRAME_LENGTH << d->seek_pwr);
254 if ((file_table_size >> diff_pwr) > tmp)
255 file_table_size = tmp << diff_pwr;
256 d->seek_table = g_seek_table;
257 d->seek_table_size = (file_table_size + ((1 << diff_pwr) - 1)) >> diff_pwr;
259 table = d->seek_table;
260 mpc_bits_get_size(&r, &tmp);
261 table[0] = last[0] = (mpc_seek_t) (tmp + d->si.header_position) * 8;
263 if (d->seek_table_size == 1)
264 return;
266 mpc_bits_get_size(&r, &tmp);
267 last[1] = (mpc_seek_t) (tmp + d->si.header_position) * 8;
268 if (diff_pwr == 0) table[1] = last[1];
270 mask = (1 << diff_pwr) - 1;
271 for (i = 2; i < file_table_size; i++) {
272 int code = mpc_bits_golomb_dec(&r, 12);
273 if (code & 1)
274 code = -(code & (-1 << 1));
275 code <<= 2;
276 last[i & 1] = code + 2 * last[(i-1) & 1] - last[i & 1];
277 if ((i & mask) == 0)
278 table[i >> diff_pwr] = last[i & 1];
282 static void mpc_demux_SP(mpc_demux * d, int size, int block_size)
284 mpc_seek_t cur;
285 mpc_uint64_t ptr;
286 mpc_block b;
287 int st_head_size;
289 cur = mpc_demux_pos(d);
290 mpc_bits_get_size(&d->bits_reader, &ptr);
291 mpc_demux_seek(d, (ptr - size) * 8 + cur, 11);
292 st_head_size = mpc_bits_get_block(&d->bits_reader, &b);
293 if (memcmp(b.key, "ST", 2) == 0) {
294 d->chap_pos = (ptr - size + b.size + st_head_size) * 8 + cur;
295 d->chap_nb = -1;
296 mpc_demux_fill(d, (mpc_uint32_t) b.size, 0);
297 mpc_demux_ST(d);
299 mpc_demux_seek(d, cur, 11 + block_size);
301 /* rockbox: not used
302 static void mpc_demux_chap_find(mpc_demux * d)
304 mpc_block b;
305 int tag_size = 0, chap_size = 0, size, i = 0;
307 d->chap_nb = 0;
309 if (d->si.stream_version < 8)
310 return;
312 if (d->chap_pos == 0) {
313 mpc_uint64_t cur_pos = (d->si.header_position + 4) * 8;
314 mpc_demux_seek(d, cur_pos, 11); // seek to the beginning of the stream
315 size = mpc_bits_get_block(&d->bits_reader, &b);
316 while (memcmp(b.key, "SE", 2) != 0) {
317 if (mpc_check_key(b.key) != MPC_STATUS_OK)
318 return;
319 if (memcmp(b.key, "CT", 2) == 0) {
320 if (d->chap_pos == 0) d->chap_pos = cur_pos;
321 } else
322 d->chap_pos = 0;
323 cur_pos += (size + b.size) * 8;
324 mpc_demux_seek(d, cur_pos, 11);
325 size = mpc_bits_get_block(&d->bits_reader, &b);
327 if (d->chap_pos == 0)
328 d->chap_pos = cur_pos;
331 mpc_demux_seek(d, d->chap_pos, 20);
332 size = mpc_bits_get_block(&d->bits_reader, &b);
333 while (memcmp(b.key, "CT", 2) == 0) {
334 mpc_uint64_t chap_sample;
335 d->chap_nb++;
336 chap_size += size;
337 size = mpc_bits_get_size(&d->bits_reader, &chap_sample) + 4;
338 chap_size += size;
339 tag_size += b.size - size;
340 mpc_demux_seek(d, d->chap_pos + (chap_size + tag_size) * 8, 20);
341 size = mpc_bits_get_block(&d->bits_reader, &b);
344 if (d->chap_nb > 0) {
345 char * ptag;
346 d->chap = malloc(sizeof(mpc_chap_info) * d->chap_nb + tag_size);
347 ptag = (char*)(d->chap + d->chap_nb);
349 mpc_demux_seek(d, d->chap_pos, 11);
350 size = mpc_bits_get_block(&d->bits_reader, &b);
351 while (memcmp(b.key, "CT", 2) == 0) {
352 mpc_demux_fill(d, 11 + (mpc_uint32_t) b.size, 0);
353 size = mpc_bits_get_size(&d->bits_reader, &d->chap[i].sample) + 4;
354 d->chap[i].gain = (mpc_uint16_t) mpc_bits_read(&d->bits_reader, 16);
355 d->chap[i].peak = (mpc_uint16_t) mpc_bits_read(&d->bits_reader, 16);
356 memcpy(ptag, d->bits_reader.buff + ((8 - d->bits_reader.count) >> 3), b.size - size);
357 d->bits_reader.buff += b.size - size;
358 d->chap[i].tag_size = b.size - size;
359 d->chap[i].tag = ptag;
360 ptag += b.size - size;
361 i++;
362 size = mpc_bits_get_block(&d->bits_reader, &b);
366 d->bits_reader.buff -= size;
371 * Gets the number of chapters in the stream
372 * @param d pointer to a musepack demuxer
373 * @return the number of chapters found in the stream
375 /* rockbox: not used
376 mpc_int_t mpc_demux_chap_nb(mpc_demux * d)
378 if (d->chap_nb == -1)
379 mpc_demux_chap_find(d);
380 return d->chap_nb;
384 * Gets datas associated to a given chapter
385 * The chapter tag is an APEv2 tag without the preamble
386 * @param d pointer to a musepack demuxer
387 * @param chap_nb chapter number you want datas (from 0 to mpc_demux_chap_nb(d) - 1)
388 * @return the chapter information structure
390 /* rockbox: not used
391 mpc_chap_info const * mpc_demux_chap(mpc_demux * d, int chap_nb)
393 if (d->chap_nb == -1)
394 mpc_demux_chap_find(d);
395 if (chap_nb >= d->chap_nb || chap_nb < 0)
396 return 0;
397 return &d->chap[chap_nb];
401 static mpc_status mpc_demux_header(mpc_demux * d)
403 char magic[4];
405 d->si.pns = 0xFF;
406 /* rockbox: not used
407 d->si.profile_name = "n.a.";
409 // get header position
410 d->si.header_position = mpc_demux_skip_id3v2(d);
411 if(d->si.header_position < 0) return MPC_STATUS_FILE;
413 d->si.tag_offset = d->si.total_file_length = d->r->get_size(d->r);
415 mpc_demux_fill(d, 4, 0);
416 magic[0] = mpc_bits_read(&d->bits_reader, 8);
417 magic[1] = mpc_bits_read(&d->bits_reader, 8);
418 magic[2] = mpc_bits_read(&d->bits_reader, 8);
419 magic[3] = mpc_bits_read(&d->bits_reader, 8);
421 if (memcmp(magic, "MP+", 3) == 0) {
422 d->si.stream_version = magic[3] & 15;
423 d->si.pns = magic[3] >> 4;
424 if (d->si.stream_version == 7) {
425 mpc_status ret;
426 mpc_demux_fill(d, 6 * 4, MPC_BUFFER_SWAP); // header block size + endian convertion
427 ret = streaminfo_read_header_sv7(&d->si, &d->bits_reader);
428 if (ret != MPC_STATUS_OK) return ret;
429 } else {
430 return MPC_STATUS_INVALIDSV;
432 } else if (memcmp(magic, "MPCK", 4) == 0) {
433 mpc_block b;
434 int size;
435 mpc_demux_fill(d, 11, 0); // max header block size
436 size = mpc_bits_get_block(&d->bits_reader, &b);
437 while( memcmp(b.key, "AP", 2) != 0 ){ // scan all blocks until audio
438 if (mpc_check_key(b.key) != MPC_STATUS_OK)
439 return MPC_STATUS_INVALIDSV;
440 if (b.size > (mpc_uint64_t) DEMUX_BUFFER_SIZE - 11)
441 return MPC_STATUS_INVALIDSV;
442 mpc_demux_fill(d, 11 + (mpc_uint32_t) b.size, 0);
443 if (memcmp(b.key, "SH", 2) == 0){
444 int ret = streaminfo_read_header_sv8(&d->si, &d->bits_reader, (mpc_uint32_t) b.size);
445 if (ret != MPC_STATUS_OK) return ret;
446 } else if (memcmp(b.key, "RG", 2) == 0)
447 streaminfo_gain(&d->si, &d->bits_reader);
448 else if (memcmp(b.key, "EI", 2) == 0)
449 streaminfo_encoder_info(&d->si, &d->bits_reader);
450 else if (memcmp(b.key, "SO", 2) == 0)
451 mpc_demux_SP(d, size, (mpc_uint32_t) b.size);
452 else if (memcmp(b.key, "ST", 2) == 0)
453 mpc_demux_ST(d);
454 d->bits_reader.buff += b.size;
455 size = mpc_bits_get_block(&d->bits_reader, &b);
457 d->bits_reader.buff -= size;
458 if (d->si.stream_version == 0) // si not initialized !!!
459 return MPC_STATUS_INVALIDSV;
460 } else
461 return MPC_STATUS_INVALIDSV;
463 return MPC_STATUS_OK;
466 mpc_demux * mpc_demux_init(mpc_reader * p_reader)
468 mpc_demux* p_tmp = &g_mpc_demux;
470 if (p_tmp != 0) {
471 memset(p_tmp, 0, sizeof(mpc_demux));
472 p_tmp->buffer = g_buffer;
473 p_tmp->r = p_reader;
474 p_tmp->chap_nb = -1;
475 mpc_demux_clear_buff(p_tmp);
476 if (mpc_demux_header(p_tmp) == MPC_STATUS_OK &&
477 mpc_demux_seek_init(p_tmp) == MPC_STATUS_OK) {
478 p_tmp->d = mpc_decoder_init(&p_tmp->si);
479 } else {
480 if (p_tmp->seek_table)
481 memset(p_tmp->seek_table, 0, sizeof(g_seek_table));
482 p_tmp = 0;
486 return p_tmp;
489 void mpc_demux_exit(mpc_demux * d)
491 mpc_decoder_exit(d->d);
492 memset(d->seek_table, 0, sizeof(g_seek_table));
495 void mpc_demux_get_info(mpc_demux * d, mpc_streaminfo * i)
497 memcpy(i, &d->si, sizeof d->si);
500 mpc_status mpc_demux_decode(mpc_demux * d, mpc_frame_info * i)
502 mpc_bits_reader r;
503 if (d->si.stream_version >= 8) {
504 i->is_key_frame = MPC_FALSE;
506 if (d->block_frames == 0) {
507 mpc_block b = {{0,0},0};
508 d->bits_reader.count &= -8;
509 if (d->d->decoded_samples == (d->seek_table_size << d->seek_pwr) * MPC_FRAME_LENGTH) {
510 d->seek_table[d->seek_table_size] = (mpc_seek_t) mpc_demux_pos(d);
511 d->seek_table_size ++;
513 mpc_demux_fill(d, 11, 0); // max header block size
514 mpc_bits_get_block(&d->bits_reader, &b);
515 while( memcmp(b.key, "AP", 2) != 0 ) { // scan all blocks until audio
516 if (mpc_check_key(b.key) != MPC_STATUS_OK)
517 goto error;
518 if (memcmp(b.key, "SE", 2) == 0) { // end block
519 i->bits = -1;
520 return MPC_STATUS_OK;
522 if (mpc_demux_fill(d, 11 + (mpc_uint32_t) b.size, 0) == 0)
523 goto error;
524 d->bits_reader.buff += b.size;
525 mpc_bits_get_block(&d->bits_reader, &b);
527 d->block_bits = (mpc_uint32_t) b.size * 8;
528 d->block_frames = 1 << d->si.block_pwr;
529 i->is_key_frame = MPC_TRUE;
531 if (d->buffer + d->bytes_total - d->bits_reader.buff <= MAX_FRAME_SIZE)
532 mpc_demux_fill(d, (d->block_bits >> 3) + 1, 0);
533 r = d->bits_reader;
534 mpc_decoder_decode_frame(d->d, &d->bits_reader, i);
535 d->block_bits -= ((d->bits_reader.buff - r.buff) << 3) + r.count - d->bits_reader.count;
536 d->block_frames--;
537 if (d->block_bits < 0 || (d->block_frames == 0 && d->block_bits > 7))
538 goto error;
539 } else {
540 if (d->d->decoded_samples == (d->seek_table_size << d->seek_pwr) * MPC_FRAME_LENGTH) {
541 d->seek_table[d->seek_table_size] = (mpc_seek_t) mpc_demux_pos(d);
542 d->seek_table_size ++;
544 mpc_demux_fill(d, MAX_FRAME_SIZE, MPC_BUFFER_FULL | MPC_BUFFER_SWAP);
545 d->block_bits = (mpc_int_t) mpc_bits_read(&d->bits_reader, 20); // read frame size
546 if (MPC_FRAME_LENGTH > d->d->samples - d->d->decoded_samples - 1) d->block_bits += 11; // we will read last frame size
547 r = d->bits_reader;
548 mpc_decoder_decode_frame(d->d, &d->bits_reader, i);
549 if (i->bits != -1 && d->block_bits != (mpc_int32_t)(((d->bits_reader.buff - r.buff) << 3) + r.count - d->bits_reader.count))
550 goto error;
552 if (i->bits != -1 && d->buffer + d->bytes_total < d->bits_reader.buff + ((8 - d->bits_reader.count) >> 3))
553 goto error;
555 return MPC_STATUS_OK;
556 error:
557 i->bits = -1; // we pretend it's end of file
558 return MPC_STATUS_INVALIDSV;
561 mpc_status mpc_demux_seek_second(mpc_demux * d, double seconds)
563 return mpc_demux_seek_sample(d, (mpc_int64_t)(seconds * (double)d->si.sample_freq + 0.5));
566 mpc_status mpc_demux_seek_sample(mpc_demux * d, mpc_uint64_t destsample)
568 mpc_uint32_t fwd, samples_to_skip, i;
569 mpc_uint32_t block_samples = MPC_FRAME_LENGTH << d->si.block_pwr;
570 mpc_seek_t fpos;
572 destsample += d->si.beg_silence;
573 if (destsample > d->si.samples) destsample = d->si.samples;
574 fwd = (mpc_uint32_t) (destsample / block_samples);
575 samples_to_skip = MPC_DECODER_SYNTH_DELAY +
576 (mpc_uint32_t) (destsample % block_samples);
577 if (d->si.stream_version == 7) {
578 if (fwd > 32) {
579 fwd -= 32;
580 samples_to_skip += MPC_FRAME_LENGTH * 32;
581 } else {
582 samples_to_skip += MPC_FRAME_LENGTH * fwd;
583 fwd = 0;
587 i = fwd >> (d->seek_pwr - d->si.block_pwr);
588 if (i >= d->seek_table_size)
589 i = d->seek_table_size - 1;
590 fpos = d->seek_table[i];
591 i <<= d->seek_pwr - d->si.block_pwr;
592 d->d->decoded_samples = i * block_samples;
594 if (d->si.stream_version >= 8) {
595 mpc_block b;
596 int size;
597 mpc_demux_seek(d, fpos, 11);
598 size = mpc_bits_get_block(&d->bits_reader, &b);
599 while(i < fwd) {
600 if (memcmp(b.key, "AP", 2) == 0) {
601 if (d->d->decoded_samples == (d->seek_table_size << d->seek_pwr) * MPC_FRAME_LENGTH) {
602 d->seek_table[d->seek_table_size] = (mpc_seek_t) mpc_demux_pos(d) - 8 * size;
603 d->seek_table_size ++;
605 d->d->decoded_samples += block_samples;
606 i++;
608 fpos += ((mpc_uint32_t)b.size + size) * 8;
609 mpc_demux_seek(d, fpos, 11);
610 size = mpc_bits_get_block(&d->bits_reader, &b);
612 d->bits_reader.buff -= size;
613 } else {
614 mpc_decoder_reset_scf(d->d, fwd != 0);
615 mpc_demux_seek(d, fpos, 4);
616 for( ; i < fwd; i++){
617 if (d->d->decoded_samples == (d->seek_table_size << d->seek_pwr) * MPC_FRAME_LENGTH) {
618 d->seek_table[d->seek_table_size] = (mpc_seek_t) mpc_demux_pos(d);
619 d->seek_table_size ++;
621 d->d->decoded_samples += block_samples;
622 fpos += mpc_bits_read(&d->bits_reader, 20) + 20;
623 mpc_demux_seek(d, fpos, 4);
626 d->d->samples_to_skip = samples_to_skip;
627 return MPC_STATUS_OK;
630 /* rockbox: not used
631 void mpc_set_replay_level(mpc_demux * d, float level, mpc_bool_t use_gain,
632 mpc_bool_t use_title, mpc_bool_t clip_prevention)
634 float peak = use_title ? d->si.peak_title : d->si.peak_album;
635 float gain = use_title ? d->si.gain_title : d->si.gain_album;
637 if(!use_gain && !clip_prevention)
638 return;
640 if(!peak)
641 peak = 1.;
642 else
643 peak = (1 << 15) / pow(10, peak / (20 * 256));
645 if(!gain)
646 gain = 1.;
647 else
648 gain = pow(10, (level - gain / 256) / 20);
650 if(clip_prevention && (peak < gain || !use_gain))
651 gain = peak;
653 mpc_decoder_scale_output(d->d, gain);