lcd-m6sp.c: remove \r
[kugel-rb.git] / apps / codecs / libmusepack / mpc_demux.c
blob9b47802ac2b9f58eec587b1a0d479dc8c85763b6
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 static 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 /* rockbox: not used
304 d->chap_pos = (ptr - size + b.size + st_head_size) * 8 + cur;
305 d->chap_nb = -1;
307 mpc_demux_fill(d, (mpc_uint32_t) b.size, 0);
308 mpc_demux_ST(d);
310 mpc_demux_seek(d, cur, 11 + block_size);
312 /* rockbox: not used
313 static void mpc_demux_chap_find(mpc_demux * d)
315 mpc_block b;
316 int tag_size = 0, chap_size = 0, size, i = 0;
318 d->chap_nb = 0;
320 if (d->si.stream_version < 8)
321 return;
323 if (d->chap_pos == 0) {
324 mpc_uint64_t cur_pos = (d->si.header_position + 4) * 8;
325 mpc_demux_seek(d, cur_pos, 11); // seek to the beginning of the stream
326 size = mpc_bits_get_block(&d->bits_reader, &b);
327 while (memcmp(b.key, "SE", 2) != 0) {
328 if (mpc_check_key(b.key) != MPC_STATUS_OK)
329 return;
330 if (memcmp(b.key, "CT", 2) == 0) {
331 if (d->chap_pos == 0) d->chap_pos = cur_pos;
332 } else
333 d->chap_pos = 0;
334 cur_pos += (size + b.size) * 8;
335 mpc_demux_seek(d, cur_pos, 11);
336 size = mpc_bits_get_block(&d->bits_reader, &b);
338 if (d->chap_pos == 0)
339 d->chap_pos = cur_pos;
342 mpc_demux_seek(d, d->chap_pos, 20);
343 size = mpc_bits_get_block(&d->bits_reader, &b);
344 while (memcmp(b.key, "CT", 2) == 0) {
345 mpc_uint64_t chap_sample;
346 d->chap_nb++;
347 chap_size += size;
348 size = mpc_bits_get_size(&d->bits_reader, &chap_sample) + 4;
349 chap_size += size;
350 tag_size += b.size - size;
351 mpc_demux_seek(d, d->chap_pos + (chap_size + tag_size) * 8, 20);
352 size = mpc_bits_get_block(&d->bits_reader, &b);
355 if (d->chap_nb > 0) {
356 char * ptag;
357 d->chap = malloc(sizeof(mpc_chap_info) * d->chap_nb + tag_size);
358 ptag = (char*)(d->chap + d->chap_nb);
360 mpc_demux_seek(d, d->chap_pos, 11);
361 size = mpc_bits_get_block(&d->bits_reader, &b);
362 while (memcmp(b.key, "CT", 2) == 0) {
363 mpc_demux_fill(d, 11 + (mpc_uint32_t) b.size, 0);
364 size = mpc_bits_get_size(&d->bits_reader, &d->chap[i].sample) + 4;
365 d->chap[i].gain = (mpc_uint16_t) mpc_bits_read(&d->bits_reader, 16);
366 d->chap[i].peak = (mpc_uint16_t) mpc_bits_read(&d->bits_reader, 16);
367 memcpy(ptag, d->bits_reader.buff + ((8 - d->bits_reader.count) >> 3), b.size - size);
368 d->bits_reader.buff += b.size - size;
369 d->chap[i].tag_size = b.size - size;
370 d->chap[i].tag = ptag;
371 ptag += b.size - size;
372 i++;
373 size = mpc_bits_get_block(&d->bits_reader, &b);
377 d->bits_reader.buff -= size;
382 * Gets the number of chapters in the stream
383 * @param d pointer to a musepack demuxer
384 * @return the number of chapters found in the stream
386 /* rockbox: not used
387 mpc_int_t mpc_demux_chap_nb(mpc_demux * d)
389 if (d->chap_nb == -1)
390 mpc_demux_chap_find(d);
391 return d->chap_nb;
395 * Gets datas associated to a given chapter
396 * The chapter tag is an APEv2 tag without the preamble
397 * @param d pointer to a musepack demuxer
398 * @param chap_nb chapter number you want datas (from 0 to mpc_demux_chap_nb(d) - 1)
399 * @return the chapter information structure
401 /* rockbox: not used
402 mpc_chap_info const * mpc_demux_chap(mpc_demux * d, int chap_nb)
404 if (d->chap_nb == -1)
405 mpc_demux_chap_find(d);
406 if (chap_nb >= d->chap_nb || chap_nb < 0)
407 return 0;
408 return &d->chap[chap_nb];
412 static mpc_status mpc_demux_header(mpc_demux * d)
414 char magic[4];
416 d->si.pns = 0xFF;
417 /* rockbox: not used
418 d->si.profile_name = "n.a.";
420 // get header position
421 d->si.header_position = mpc_demux_skip_id3v2(d);
422 if(d->si.header_position < 0) return MPC_STATUS_FILE;
424 d->si.tag_offset = d->si.total_file_length = d->r->get_size(d->r);
426 mpc_demux_fill(d, 4, 0);
427 magic[0] = mpc_bits_read(&d->bits_reader, 8);
428 magic[1] = mpc_bits_read(&d->bits_reader, 8);
429 magic[2] = mpc_bits_read(&d->bits_reader, 8);
430 magic[3] = mpc_bits_read(&d->bits_reader, 8);
432 if (memcmp(magic, "MP+", 3) == 0) {
433 d->si.stream_version = magic[3] & 15;
434 d->si.pns = magic[3] >> 4;
435 if (d->si.stream_version == 7) {
436 mpc_status ret;
437 mpc_demux_fill(d, 6 * 4, MPC_BUFFER_SWAP); // header block size + endian convertion
438 ret = streaminfo_read_header_sv7(&d->si, &d->bits_reader);
439 if (ret != MPC_STATUS_OK) return ret;
440 } else {
441 return MPC_STATUS_INVALIDSV;
443 } else if (memcmp(magic, "MPCK", 4) == 0) {
444 mpc_block b;
445 int size;
446 mpc_demux_fill(d, 11, 0); // max header block size
447 size = mpc_bits_get_block(&d->bits_reader, &b);
448 while( memcmp(b.key, "AP", 2) != 0 ){ // scan all blocks until audio
449 if (mpc_check_key(b.key) != MPC_STATUS_OK)
450 return MPC_STATUS_INVALIDSV;
451 if (b.size > (mpc_uint64_t) DEMUX_BUFFER_SIZE - 11)
452 return MPC_STATUS_INVALIDSV;
453 mpc_demux_fill(d, 11 + (mpc_uint32_t) b.size, 0);
454 if (memcmp(b.key, "SH", 2) == 0){
455 int ret = streaminfo_read_header_sv8(&d->si, &d->bits_reader, (mpc_uint32_t) b.size);
456 if (ret != MPC_STATUS_OK) return ret;
457 } else if (memcmp(b.key, "RG", 2) == 0)
458 streaminfo_gain(&d->si, &d->bits_reader);
459 else if (memcmp(b.key, "EI", 2) == 0)
460 streaminfo_encoder_info(&d->si, &d->bits_reader);
461 else if (memcmp(b.key, "SO", 2) == 0)
462 mpc_demux_SP(d, size, (mpc_uint32_t) b.size);
463 else if (memcmp(b.key, "ST", 2) == 0)
464 mpc_demux_ST(d);
465 d->bits_reader.buff += b.size;
466 size = mpc_bits_get_block(&d->bits_reader, &b);
468 d->bits_reader.buff -= size;
469 if (d->si.stream_version == 0) // si not initialized !!!
470 return MPC_STATUS_INVALIDSV;
471 } else
472 return MPC_STATUS_INVALIDSV;
474 return MPC_STATUS_OK;
477 mpc_demux * mpc_demux_init(mpc_reader * p_reader)
479 mpc_demux* p_tmp = &g_mpc_demux;
481 if (p_tmp != 0) {
482 memset(p_tmp, 0, sizeof(mpc_demux));
483 p_tmp->buffer = g_buffer;
484 p_tmp->r = p_reader;
485 /* rockbox: not used
486 p_tmp->chap_nb = -1;
488 mpc_demux_clear_buff(p_tmp);
489 if (mpc_demux_header(p_tmp) == MPC_STATUS_OK &&
490 mpc_demux_seek_init(p_tmp) == MPC_STATUS_OK) {
491 p_tmp->d = mpc_decoder_init(&p_tmp->si);
492 } else {
493 if (p_tmp->seek_table)
494 memset(p_tmp->seek_table, 0, sizeof(g_seek_table));
495 p_tmp = 0;
499 return p_tmp;
502 /* rockbox: not used
503 void mpc_demux_exit(mpc_demux * d)
505 mpc_decoder_exit(d->d);
506 memset(d->seek_table, 0, sizeof(g_seek_table));
510 void mpc_demux_get_info(mpc_demux * d, mpc_streaminfo * i)
512 memcpy(i, &d->si, sizeof d->si);
515 mpc_status mpc_demux_decode(mpc_demux * d, mpc_frame_info * i)
517 mpc_bits_reader r;
518 if (d->si.stream_version >= 8) {
519 i->is_key_frame = MPC_FALSE;
521 if (d->block_frames == 0) {
522 mpc_block b = {{0,0},0};
523 d->bits_reader.count &= -8;
524 if (d->d->decoded_samples == (d->seek_table_size << d->seek_pwr) * MPC_FRAME_LENGTH) {
525 d->seek_table[d->seek_table_size] = (mpc_seek_t) mpc_demux_pos(d);
526 d->seek_table_size ++;
528 mpc_demux_fill(d, 11, 0); // max header block size
529 mpc_bits_get_block(&d->bits_reader, &b);
530 while( memcmp(b.key, "AP", 2) != 0 ) { // scan all blocks until audio
531 if (mpc_check_key(b.key) != MPC_STATUS_OK)
532 goto error;
533 if (memcmp(b.key, "SE", 2) == 0) { // end block
534 i->bits = -1;
535 return MPC_STATUS_OK;
537 if (mpc_demux_fill(d, 11 + (mpc_uint32_t) b.size, 0) == 0)
538 goto error;
539 d->bits_reader.buff += b.size;
540 mpc_bits_get_block(&d->bits_reader, &b);
542 d->block_bits = (mpc_uint32_t) b.size * 8;
543 d->block_frames = 1 << d->si.block_pwr;
544 i->is_key_frame = MPC_TRUE;
546 if (d->buffer + d->bytes_total - d->bits_reader.buff <= MAX_FRAME_SIZE)
547 mpc_demux_fill(d, (d->block_bits >> 3) + 1, 0);
548 r = d->bits_reader;
549 mpc_decoder_decode_frame(d->d, &d->bits_reader, i);
550 d->block_bits -= ((d->bits_reader.buff - r.buff) << 3) + r.count - d->bits_reader.count;
551 d->block_frames--;
552 if (d->block_bits < 0 || (d->block_frames == 0 && d->block_bits > 7))
553 goto error;
554 } else {
555 if (d->d->decoded_samples == (d->seek_table_size << d->seek_pwr) * MPC_FRAME_LENGTH) {
556 d->seek_table[d->seek_table_size] = (mpc_seek_t) mpc_demux_pos(d);
557 d->seek_table_size ++;
559 mpc_demux_fill(d, MAX_FRAME_SIZE, MPC_BUFFER_FULL | MPC_BUFFER_SWAP);
560 d->block_bits = (mpc_int_t) mpc_bits_read(&d->bits_reader, 20); // read frame size
561 if (MPC_FRAME_LENGTH > d->d->samples - d->d->decoded_samples - 1) d->block_bits += 11; // we will read last frame size
562 r = d->bits_reader;
563 mpc_decoder_decode_frame(d->d, &d->bits_reader, i);
564 if (i->bits != -1 && d->block_bits != (mpc_int32_t)(((d->bits_reader.buff - r.buff) << 3) + r.count - d->bits_reader.count))
565 goto error;
567 if (i->bits != -1 && d->buffer + d->bytes_total < d->bits_reader.buff + ((8 - d->bits_reader.count) >> 3))
568 goto error;
570 return MPC_STATUS_OK;
571 error:
572 i->bits = -1; // we pretend it's end of file
573 return MPC_STATUS_INVALIDSV;
576 /* rockbox: not used
577 mpc_status mpc_demux_seek_second(mpc_demux * d, double seconds)
579 return mpc_demux_seek_sample(d, (mpc_int64_t)(seconds * (double)d->si.sample_freq + 0.5));
583 mpc_status mpc_demux_seek_sample(mpc_demux * d, mpc_uint64_t destsample)
585 mpc_uint32_t fwd, samples_to_skip, i;
586 mpc_uint32_t block_samples = MPC_FRAME_LENGTH << d->si.block_pwr;
587 mpc_seek_t fpos;
589 destsample += d->si.beg_silence;
590 if (destsample > d->si.samples) destsample = d->si.samples;
591 fwd = (mpc_uint32_t) (destsample / block_samples);
592 samples_to_skip = MPC_DECODER_SYNTH_DELAY +
593 (mpc_uint32_t) (destsample % block_samples);
594 if (d->si.stream_version == 7) {
595 if (fwd > 32) {
596 fwd -= 32;
597 samples_to_skip += MPC_FRAME_LENGTH * 32;
598 } else {
599 samples_to_skip += MPC_FRAME_LENGTH * fwd;
600 fwd = 0;
604 i = fwd >> (d->seek_pwr - d->si.block_pwr);
605 if (i >= d->seek_table_size)
606 i = d->seek_table_size - 1;
607 fpos = d->seek_table[i];
608 i <<= d->seek_pwr - d->si.block_pwr;
609 d->d->decoded_samples = i * block_samples;
611 if (d->si.stream_version >= 8) {
612 mpc_block b;
613 int size;
614 mpc_demux_seek(d, fpos, 11);
615 size = mpc_bits_get_block(&d->bits_reader, &b);
616 while(i < fwd) {
617 if (memcmp(b.key, "AP", 2) == 0) {
618 if (d->d->decoded_samples == (d->seek_table_size << d->seek_pwr) * MPC_FRAME_LENGTH) {
619 d->seek_table[d->seek_table_size] = (mpc_seek_t) mpc_demux_pos(d) - 8 * size;
620 d->seek_table_size ++;
622 d->d->decoded_samples += block_samples;
623 i++;
625 fpos += ((mpc_uint32_t)b.size + size) * 8;
626 mpc_demux_seek(d, fpos, 11);
627 size = mpc_bits_get_block(&d->bits_reader, &b);
629 d->bits_reader.buff -= size;
630 } else {
631 mpc_decoder_reset_scf(d->d, fwd != 0);
632 mpc_demux_seek(d, fpos, 4);
633 for( ; i < fwd; i++){
634 if (d->d->decoded_samples == (d->seek_table_size << d->seek_pwr) * MPC_FRAME_LENGTH) {
635 d->seek_table[d->seek_table_size] = (mpc_seek_t) mpc_demux_pos(d);
636 d->seek_table_size ++;
638 d->d->decoded_samples += block_samples;
639 fpos += mpc_bits_read(&d->bits_reader, 20) + 20;
640 mpc_demux_seek(d, fpos, 4);
643 d->d->samples_to_skip = samples_to_skip;
644 return MPC_STATUS_OK;
647 /* rockbox: not used
648 void mpc_set_replay_level(mpc_demux * d, float level, mpc_bool_t use_gain,
649 mpc_bool_t use_title, mpc_bool_t clip_prevention)
651 float peak = use_title ? d->si.peak_title : d->si.peak_album;
652 float gain = use_title ? d->si.gain_title : d->si.gain_album;
654 if(!use_gain && !clip_prevention)
655 return;
657 if(!peak)
658 peak = 1.;
659 else
660 peak = (1 << 15) / pow(10, peak / (20 * 256));
662 if(!gain)
663 gain = 1.;
664 else
665 gain = pow(10, (level - gain / 256) / 20);
667 if(clip_prevention && (peak < gain || !use_gain))
668 gain = peak;
670 mpc_decoder_scale_output(d->d, gain);