Build doom on clipv2 and clip+
[kugel-rb.git] / apps / codecs / libmusepack / mpc_demux.c
blob70ac3bd416685f378c2022af22ca2695c9f95aa4
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 // d->bits_reader.buff - d->buffer = current byte position within buffer
124 // d->bytes_total = buffer is filled with bytes_total bytes
125 // fpos = desired file position in bit (not byte)
126 // buf_fpos = desired byte position within buffer
127 mpc_seek_t next_pos = fpos>>3;
128 mpc_int_t buf_fpos = next_pos - d->r->tell(d->r) + d->bytes_total;
130 // is desired byte position within lower and upper boundaries of buffer?
131 if (buf_fpos >= 0 && buf_fpos + min_bytes <= d->bytes_total) {
132 // desired bytes are available in current buffer
133 d->bits_reader.buff += buf_fpos - (d->bits_reader.buff - d->buffer);
134 d->bits_reader.count = 8 - (fpos & 7);
135 } else {
136 // buffer needs to be refilled
137 if (d->si.stream_version == 7)
138 next_pos = ((next_pos - d->si.header_position) & (-1 << 2)) + d->si.header_position;
139 buf_fpos = fpos - (next_pos << 3);
141 d->r->seek(d->r, (mpc_int32_t) next_pos);
142 mpc_demux_clear_buff(d);
143 if (d->si.stream_version == 7)
144 mpc_demux_fill(d, DEMUX_BUFFER_SIZE, MPC_BUFFER_SWAP);
145 else
146 mpc_demux_fill(d, DEMUX_BUFFER_SIZE, 0);
147 d->bits_reader.buff += buf_fpos >> 3;
148 d->bits_reader.count = 8 - (buf_fpos & 7);
153 * return the current position in the stream (in bits) from the beginning
154 * of the file
155 * @param d demuxer context
156 * @return current stream position in bits
158 mpc_seek_t mpc_demux_pos(mpc_demux * d)
160 return (((mpc_seek_t)(d->r->tell(d->r)) - d->bytes_total +
161 d->bits_reader.buff - d->buffer) << 3) + 8 - d->bits_reader.count;
165 * Searches for a ID3v2-tag and reads the length (in bytes) of it.
167 * @param d demuxer context
168 * @return size of tag, in bytes
169 * @return MPC_STATUS_FILE on errors of any kind
171 static mpc_int32_t mpc_demux_skip_id3v2(mpc_demux * d)
173 mpc_uint8_t tmp [4];
174 mpc_bool_t footerPresent; // ID3v2.4-flag
175 mpc_int32_t size;
177 // we must be at the beginning of the stream
178 mpc_demux_fill(d, 3, 0);
180 // check id3-tag
181 if ( 0 != memcmp( d->bits_reader.buff, "ID3", 3 ) )
182 return 0;
184 mpc_demux_fill(d, 10, 0);
186 mpc_bits_read(&d->bits_reader, 24); // read ID3
187 mpc_bits_read(&d->bits_reader, 16); // read tag version
189 tmp[0] = mpc_bits_read(&d->bits_reader, 8); // read flags
190 footerPresent = tmp[0] & 0x10;
191 if ( tmp[0] & 0x0F )
192 return MPC_STATUS_FILE; // not (yet???) allowed
194 tmp[0] = mpc_bits_read(&d->bits_reader, 8); // read size
195 tmp[1] = mpc_bits_read(&d->bits_reader, 8); // read size
196 tmp[2] = mpc_bits_read(&d->bits_reader, 8); // read size
197 tmp[3] = mpc_bits_read(&d->bits_reader, 8); // read size
199 if ( (tmp[0] | tmp[1] | tmp[2] | tmp[3]) & 0x80 )
200 return MPC_STATUS_FILE; // not allowed
202 // read headerSize (syncsave: 4 * $0xxxxxxx = 28 significant bits)
203 size = tmp[0] << 21;
204 size |= tmp[1] << 14;
205 size |= tmp[2] << 7;
206 size |= tmp[3];
208 size += 10; //header
210 if ( footerPresent ) size += 10;
212 // 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.
213 mpc_demux_clear_buff(d);
214 if (!d->r->seek(d->r, size)) return MPC_STATUS_FILE;
216 return size;
219 static mpc_status mpc_demux_seek_init(mpc_demux * d)
221 size_t seek_table_size;
222 if (d->seek_table != 0)
223 return MPC_STATUS_OK;
225 d->seek_pwr = 6;
226 if (d->si.block_pwr > d->seek_pwr)
227 d->seek_pwr = d->si.block_pwr;
228 seek_table_size = (2 + d->si.samples / (MPC_FRAME_LENGTH << d->seek_pwr));
229 while (seek_table_size > MAX_SEEK_TABLE_SIZE) {
230 d->seek_pwr++;
231 seek_table_size = (2 + d->si.samples / (MPC_FRAME_LENGTH << d->seek_pwr));
233 d->seek_table = g_seek_table;
234 if (d->seek_table == 0)
235 return MPC_STATUS_FILE;
236 d->seek_table[0] = (mpc_seek_t)mpc_demux_pos(d);
237 d->seek_table_size = 1;
239 return MPC_STATUS_OK;
242 static void mpc_demux_ST(mpc_demux * d)
244 mpc_uint64_t tmp;
245 mpc_seek_t * table, last[2];
246 mpc_bits_reader r = d->bits_reader;
247 mpc_uint_t i, diff_pwr = 0, mask;
248 mpc_uint32_t file_table_size;
250 if (d->seek_table != 0)
251 return;
253 mpc_bits_get_size(&r, &tmp);
254 file_table_size = (mpc_seek_t) tmp;
255 d->seek_pwr = d->si.block_pwr + mpc_bits_read(&r, 4);
257 tmp = 2 + d->si.samples / (MPC_FRAME_LENGTH << d->seek_pwr);
258 while (tmp > MAX_SEEK_TABLE_SIZE) {
259 d->seek_pwr++;
260 diff_pwr++;
261 tmp = 2 + d->si.samples / (MPC_FRAME_LENGTH << d->seek_pwr);
263 if ((file_table_size >> diff_pwr) > tmp)
264 file_table_size = tmp << diff_pwr;
265 d->seek_table = g_seek_table;
266 d->seek_table_size = (file_table_size + ((1 << diff_pwr) - 1)) >> diff_pwr;
268 table = d->seek_table;
269 mpc_bits_get_size(&r, &tmp);
270 table[0] = last[0] = (mpc_seek_t) (tmp + d->si.header_position) * 8;
272 if (d->seek_table_size == 1)
273 return;
275 mpc_bits_get_size(&r, &tmp);
276 last[1] = (mpc_seek_t) (tmp + d->si.header_position) * 8;
277 if (diff_pwr == 0) table[1] = last[1];
279 mask = (1 << diff_pwr) - 1;
280 for (i = 2; i < file_table_size; i++) {
281 int code = mpc_bits_golomb_dec(&r, 12);
282 if (code & 1)
283 code = -(code & (-1 << 1));
284 code <<= 2;
285 last[i & 1] = code + 2 * last[(i-1) & 1] - last[i & 1];
286 if ((i & mask) == 0)
287 table[i >> diff_pwr] = last[i & 1];
291 static void mpc_demux_SP(mpc_demux * d, int size, int block_size)
293 mpc_seek_t cur;
294 mpc_uint64_t ptr;
295 mpc_block b;
296 int st_head_size;
298 cur = mpc_demux_pos(d);
299 mpc_bits_get_size(&d->bits_reader, &ptr);
300 mpc_demux_seek(d, (ptr - size) * 8 + cur, 11);
301 st_head_size = mpc_bits_get_block(&d->bits_reader, &b);
302 if (memcmp(b.key, "ST", 2) == 0) {
303 d->chap_pos = (ptr - size + b.size + st_head_size) * 8 + cur;
304 d->chap_nb = -1;
305 mpc_demux_fill(d, (mpc_uint32_t) b.size, 0);
306 mpc_demux_ST(d);
308 mpc_demux_seek(d, cur, 11 + block_size);
310 /* rockbox: not used
311 static void mpc_demux_chap_find(mpc_demux * d)
313 mpc_block b;
314 int tag_size = 0, chap_size = 0, size, i = 0;
316 d->chap_nb = 0;
318 if (d->si.stream_version < 8)
319 return;
321 if (d->chap_pos == 0) {
322 mpc_uint64_t cur_pos = (d->si.header_position + 4) * 8;
323 mpc_demux_seek(d, cur_pos, 11); // seek to the beginning of the stream
324 size = mpc_bits_get_block(&d->bits_reader, &b);
325 while (memcmp(b.key, "SE", 2) != 0) {
326 if (mpc_check_key(b.key) != MPC_STATUS_OK)
327 return;
328 if (memcmp(b.key, "CT", 2) == 0) {
329 if (d->chap_pos == 0) d->chap_pos = cur_pos;
330 } else
331 d->chap_pos = 0;
332 cur_pos += (size + b.size) * 8;
333 mpc_demux_seek(d, cur_pos, 11);
334 size = mpc_bits_get_block(&d->bits_reader, &b);
336 if (d->chap_pos == 0)
337 d->chap_pos = cur_pos;
340 mpc_demux_seek(d, d->chap_pos, 20);
341 size = mpc_bits_get_block(&d->bits_reader, &b);
342 while (memcmp(b.key, "CT", 2) == 0) {
343 mpc_uint64_t chap_sample;
344 d->chap_nb++;
345 chap_size += size;
346 size = mpc_bits_get_size(&d->bits_reader, &chap_sample) + 4;
347 chap_size += size;
348 tag_size += b.size - size;
349 mpc_demux_seek(d, d->chap_pos + (chap_size + tag_size) * 8, 20);
350 size = mpc_bits_get_block(&d->bits_reader, &b);
353 if (d->chap_nb > 0) {
354 char * ptag;
355 d->chap = malloc(sizeof(mpc_chap_info) * d->chap_nb + tag_size);
356 ptag = (char*)(d->chap + d->chap_nb);
358 mpc_demux_seek(d, d->chap_pos, 11);
359 size = mpc_bits_get_block(&d->bits_reader, &b);
360 while (memcmp(b.key, "CT", 2) == 0) {
361 mpc_demux_fill(d, 11 + (mpc_uint32_t) b.size, 0);
362 size = mpc_bits_get_size(&d->bits_reader, &d->chap[i].sample) + 4;
363 d->chap[i].gain = (mpc_uint16_t) mpc_bits_read(&d->bits_reader, 16);
364 d->chap[i].peak = (mpc_uint16_t) mpc_bits_read(&d->bits_reader, 16);
365 memcpy(ptag, d->bits_reader.buff + ((8 - d->bits_reader.count) >> 3), b.size - size);
366 d->bits_reader.buff += b.size - size;
367 d->chap[i].tag_size = b.size - size;
368 d->chap[i].tag = ptag;
369 ptag += b.size - size;
370 i++;
371 size = mpc_bits_get_block(&d->bits_reader, &b);
375 d->bits_reader.buff -= size;
380 * Gets the number of chapters in the stream
381 * @param d pointer to a musepack demuxer
382 * @return the number of chapters found in the stream
384 /* rockbox: not used
385 mpc_int_t mpc_demux_chap_nb(mpc_demux * d)
387 if (d->chap_nb == -1)
388 mpc_demux_chap_find(d);
389 return d->chap_nb;
393 * Gets datas associated to a given chapter
394 * The chapter tag is an APEv2 tag without the preamble
395 * @param d pointer to a musepack demuxer
396 * @param chap_nb chapter number you want datas (from 0 to mpc_demux_chap_nb(d) - 1)
397 * @return the chapter information structure
399 /* rockbox: not used
400 mpc_chap_info const * mpc_demux_chap(mpc_demux * d, int chap_nb)
402 if (d->chap_nb == -1)
403 mpc_demux_chap_find(d);
404 if (chap_nb >= d->chap_nb || chap_nb < 0)
405 return 0;
406 return &d->chap[chap_nb];
410 static mpc_status mpc_demux_header(mpc_demux * d)
412 char magic[4];
414 d->si.pns = 0xFF;
415 /* rockbox: not used
416 d->si.profile_name = "n.a.";
418 // get header position
419 d->si.header_position = mpc_demux_skip_id3v2(d);
420 if(d->si.header_position < 0) return MPC_STATUS_FILE;
422 d->si.tag_offset = d->si.total_file_length = d->r->get_size(d->r);
424 mpc_demux_fill(d, 4, 0);
425 magic[0] = mpc_bits_read(&d->bits_reader, 8);
426 magic[1] = mpc_bits_read(&d->bits_reader, 8);
427 magic[2] = mpc_bits_read(&d->bits_reader, 8);
428 magic[3] = mpc_bits_read(&d->bits_reader, 8);
430 if (memcmp(magic, "MP+", 3) == 0) {
431 d->si.stream_version = magic[3] & 15;
432 d->si.pns = magic[3] >> 4;
433 if (d->si.stream_version == 7) {
434 mpc_status ret;
435 mpc_demux_fill(d, 6 * 4, MPC_BUFFER_SWAP); // header block size + endian convertion
436 ret = streaminfo_read_header_sv7(&d->si, &d->bits_reader);
437 if (ret != MPC_STATUS_OK) return ret;
438 } else {
439 return MPC_STATUS_INVALIDSV;
441 } else if (memcmp(magic, "MPCK", 4) == 0) {
442 mpc_block b;
443 int size;
444 mpc_demux_fill(d, 11, 0); // max header block size
445 size = mpc_bits_get_block(&d->bits_reader, &b);
446 while( memcmp(b.key, "AP", 2) != 0 ){ // scan all blocks until audio
447 if (mpc_check_key(b.key) != MPC_STATUS_OK)
448 return MPC_STATUS_INVALIDSV;
449 if (b.size > (mpc_uint64_t) DEMUX_BUFFER_SIZE - 11)
450 return MPC_STATUS_INVALIDSV;
451 mpc_demux_fill(d, 11 + (mpc_uint32_t) b.size, 0);
452 if (memcmp(b.key, "SH", 2) == 0){
453 int ret = streaminfo_read_header_sv8(&d->si, &d->bits_reader, (mpc_uint32_t) b.size);
454 if (ret != MPC_STATUS_OK) return ret;
455 } else if (memcmp(b.key, "RG", 2) == 0)
456 streaminfo_gain(&d->si, &d->bits_reader);
457 else if (memcmp(b.key, "EI", 2) == 0)
458 streaminfo_encoder_info(&d->si, &d->bits_reader);
459 else if (memcmp(b.key, "SO", 2) == 0)
460 mpc_demux_SP(d, size, (mpc_uint32_t) b.size);
461 else if (memcmp(b.key, "ST", 2) == 0)
462 mpc_demux_ST(d);
463 d->bits_reader.buff += b.size;
464 size = mpc_bits_get_block(&d->bits_reader, &b);
466 d->bits_reader.buff -= size;
467 if (d->si.stream_version == 0) // si not initialized !!!
468 return MPC_STATUS_INVALIDSV;
469 } else
470 return MPC_STATUS_INVALIDSV;
472 return MPC_STATUS_OK;
475 mpc_demux * mpc_demux_init(mpc_reader * p_reader)
477 mpc_demux* p_tmp = &g_mpc_demux;
479 if (p_tmp != 0) {
480 memset(p_tmp, 0, sizeof(mpc_demux));
481 p_tmp->buffer = g_buffer;
482 p_tmp->r = p_reader;
483 p_tmp->chap_nb = -1;
484 mpc_demux_clear_buff(p_tmp);
485 if (mpc_demux_header(p_tmp) == MPC_STATUS_OK &&
486 mpc_demux_seek_init(p_tmp) == MPC_STATUS_OK) {
487 p_tmp->d = mpc_decoder_init(&p_tmp->si);
488 } else {
489 if (p_tmp->seek_table)
490 memset(p_tmp->seek_table, 0, sizeof(g_seek_table));
491 p_tmp = 0;
495 return p_tmp;
498 void mpc_demux_exit(mpc_demux * d)
500 mpc_decoder_exit(d->d);
501 memset(d->seek_table, 0, sizeof(g_seek_table));
504 void mpc_demux_get_info(mpc_demux * d, mpc_streaminfo * i)
506 memcpy(i, &d->si, sizeof d->si);
509 mpc_status mpc_demux_decode(mpc_demux * d, mpc_frame_info * i)
511 mpc_bits_reader r;
512 if (d->si.stream_version >= 8) {
513 i->is_key_frame = MPC_FALSE;
515 if (d->block_frames == 0) {
516 mpc_block b = {{0,0},0};
517 d->bits_reader.count &= -8;
518 if (d->d->decoded_samples == (d->seek_table_size << d->seek_pwr) * MPC_FRAME_LENGTH) {
519 d->seek_table[d->seek_table_size] = (mpc_seek_t) mpc_demux_pos(d);
520 d->seek_table_size ++;
522 mpc_demux_fill(d, 11, 0); // max header block size
523 mpc_bits_get_block(&d->bits_reader, &b);
524 while( memcmp(b.key, "AP", 2) != 0 ) { // scan all blocks until audio
525 if (mpc_check_key(b.key) != MPC_STATUS_OK)
526 goto error;
527 if (memcmp(b.key, "SE", 2) == 0) { // end block
528 i->bits = -1;
529 return MPC_STATUS_OK;
531 if (mpc_demux_fill(d, 11 + (mpc_uint32_t) b.size, 0) == 0)
532 goto error;
533 d->bits_reader.buff += b.size;
534 mpc_bits_get_block(&d->bits_reader, &b);
536 d->block_bits = (mpc_uint32_t) b.size * 8;
537 d->block_frames = 1 << d->si.block_pwr;
538 i->is_key_frame = MPC_TRUE;
540 if (d->buffer + d->bytes_total - d->bits_reader.buff <= MAX_FRAME_SIZE)
541 mpc_demux_fill(d, (d->block_bits >> 3) + 1, 0);
542 r = d->bits_reader;
543 mpc_decoder_decode_frame(d->d, &d->bits_reader, i);
544 d->block_bits -= ((d->bits_reader.buff - r.buff) << 3) + r.count - d->bits_reader.count;
545 d->block_frames--;
546 if (d->block_bits < 0 || (d->block_frames == 0 && d->block_bits > 7))
547 goto error;
548 } else {
549 if (d->d->decoded_samples == (d->seek_table_size << d->seek_pwr) * MPC_FRAME_LENGTH) {
550 d->seek_table[d->seek_table_size] = (mpc_seek_t) mpc_demux_pos(d);
551 d->seek_table_size ++;
553 mpc_demux_fill(d, MAX_FRAME_SIZE, MPC_BUFFER_FULL | MPC_BUFFER_SWAP);
554 d->block_bits = (mpc_int_t) mpc_bits_read(&d->bits_reader, 20); // read frame size
555 if (MPC_FRAME_LENGTH > d->d->samples - d->d->decoded_samples - 1) d->block_bits += 11; // we will read last frame size
556 r = d->bits_reader;
557 mpc_decoder_decode_frame(d->d, &d->bits_reader, i);
558 if (i->bits != -1 && d->block_bits != (mpc_int32_t)(((d->bits_reader.buff - r.buff) << 3) + r.count - d->bits_reader.count))
559 goto error;
561 if (i->bits != -1 && d->buffer + d->bytes_total < d->bits_reader.buff + ((8 - d->bits_reader.count) >> 3))
562 goto error;
564 return MPC_STATUS_OK;
565 error:
566 i->bits = -1; // we pretend it's end of file
567 return MPC_STATUS_INVALIDSV;
570 mpc_status mpc_demux_seek_second(mpc_demux * d, double seconds)
572 return mpc_demux_seek_sample(d, (mpc_int64_t)(seconds * (double)d->si.sample_freq + 0.5));
575 mpc_status mpc_demux_seek_sample(mpc_demux * d, mpc_uint64_t destsample)
577 mpc_uint32_t fwd, samples_to_skip, i;
578 mpc_uint32_t block_samples = MPC_FRAME_LENGTH << d->si.block_pwr;
579 mpc_seek_t fpos;
581 destsample += d->si.beg_silence;
582 if (destsample > d->si.samples) destsample = d->si.samples;
583 fwd = (mpc_uint32_t) (destsample / block_samples);
584 samples_to_skip = MPC_DECODER_SYNTH_DELAY +
585 (mpc_uint32_t) (destsample % block_samples);
586 if (d->si.stream_version == 7) {
587 if (fwd > 32) {
588 fwd -= 32;
589 samples_to_skip += MPC_FRAME_LENGTH * 32;
590 } else {
591 samples_to_skip += MPC_FRAME_LENGTH * fwd;
592 fwd = 0;
596 i = fwd >> (d->seek_pwr - d->si.block_pwr);
597 if (i >= d->seek_table_size)
598 i = d->seek_table_size - 1;
599 fpos = d->seek_table[i];
600 i <<= d->seek_pwr - d->si.block_pwr;
601 d->d->decoded_samples = i * block_samples;
603 if (d->si.stream_version >= 8) {
604 mpc_block b;
605 int size;
606 mpc_demux_seek(d, fpos, 11);
607 size = mpc_bits_get_block(&d->bits_reader, &b);
608 while(i < fwd) {
609 if (memcmp(b.key, "AP", 2) == 0) {
610 if (d->d->decoded_samples == (d->seek_table_size << d->seek_pwr) * MPC_FRAME_LENGTH) {
611 d->seek_table[d->seek_table_size] = (mpc_seek_t) mpc_demux_pos(d) - 8 * size;
612 d->seek_table_size ++;
614 d->d->decoded_samples += block_samples;
615 i++;
617 fpos += ((mpc_uint32_t)b.size + size) * 8;
618 mpc_demux_seek(d, fpos, 11);
619 size = mpc_bits_get_block(&d->bits_reader, &b);
621 d->bits_reader.buff -= size;
622 } else {
623 mpc_decoder_reset_scf(d->d, fwd != 0);
624 mpc_demux_seek(d, fpos, 4);
625 for( ; i < fwd; i++){
626 if (d->d->decoded_samples == (d->seek_table_size << d->seek_pwr) * MPC_FRAME_LENGTH) {
627 d->seek_table[d->seek_table_size] = (mpc_seek_t) mpc_demux_pos(d);
628 d->seek_table_size ++;
630 d->d->decoded_samples += block_samples;
631 fpos += mpc_bits_read(&d->bits_reader, 20) + 20;
632 mpc_demux_seek(d, fpos, 4);
635 d->d->samples_to_skip = samples_to_skip;
636 return MPC_STATUS_OK;
639 /* rockbox: not used
640 void mpc_set_replay_level(mpc_demux * d, float level, mpc_bool_t use_gain,
641 mpc_bool_t use_title, mpc_bool_t clip_prevention)
643 float peak = use_title ? d->si.peak_title : d->si.peak_album;
644 float gain = use_title ? d->si.gain_title : d->si.gain_album;
646 if(!use_gain && !clip_prevention)
647 return;
649 if(!peak)
650 peak = 1.;
651 else
652 peak = (1 << 15) / pow(10, peak / (20 * 256));
654 if(!gain)
655 gain = 1.;
656 else
657 gain = pow(10, (level - gain / 256) / 20);
659 if(clip_prevention && (peak < gain || !use_gain))
660 gain = peak;
662 mpc_decoder_scale_output(d->d, gain);