summaryrefslogtreecommitdiff
path: root/apps/codecs/libmusepack/mpc_demux.c
diff options
context:
space:
mode:
Diffstat (limited to 'apps/codecs/libmusepack/mpc_demux.c')
-rwxr-xr-xapps/codecs/libmusepack/mpc_demux.c666
1 files changed, 666 insertions, 0 deletions
diff --git a/apps/codecs/libmusepack/mpc_demux.c b/apps/codecs/libmusepack/mpc_demux.c
new file mode 100755
index 0000000000..75d02bae54
--- /dev/null
+++ b/apps/codecs/libmusepack/mpc_demux.c
@@ -0,0 +1,666 @@
1/*
2 Copyright (c) 2005-2009, The Musepack Development Team
3 All rights reserved.
4
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are
7 met:
8
9 * Redistributions of source code must retain the above copyright
10 notice, this list of conditions and the following disclaimer.
11
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.
16
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.
21
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.
33*/
34
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"
43
44#include <codeclib.h>
45
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
49
50// globals
51static mpc_uint8_t g_buffer[DEMUX_BUFFER_SIZE + MAX_FRAME_SIZE];
52static mpc_seek_t g_seek_table[MAX_SEEK_TABLE_SIZE];
53static mpc_demux g_mpc_demux IBSS_ATTR;
54
55// streaminfo.c
56mpc_status streaminfo_read_header_sv8(mpc_streaminfo* si,
57 const mpc_bits_reader * r_in,
58 mpc_size_t block_size);
59mpc_status streaminfo_read_header_sv7(mpc_streaminfo* si, mpc_bits_reader * r_in);
60void streaminfo_encoder_info(mpc_streaminfo* si, const mpc_bits_reader * r_in);
61void streaminfo_gain(mpc_streaminfo* si, const mpc_bits_reader * r_in);
62
63// mpc_decoder.c
64void mpc_decoder_reset_scf(mpc_decoder * d, int value);
65
66enum {
67 MPC_BUFFER_SWAP = 1,
68 MPC_BUFFER_FULL = 2,
69};
70
71static void mpc_demux_clear_buff(mpc_demux * d)
72{
73 d->bytes_total = 0;
74 d->bits_reader.buff = d->buffer;
75 d->bits_reader.count = 8;
76 d->bits_reader.buffered_addr = 0;
77 d->bits_reader.buffered_code = 0;
78 d->block_bits = 0;
79 d->block_frames = 0;
80 memset(d->buffer, 0, sizeof(g_buffer));
81}
82
83static mpc_uint32_t
84mpc_demux_fill(mpc_demux * d, mpc_uint32_t min_bytes, int flags)
85{
86 mpc_uint32_t unread_bytes = d->bytes_total + d->buffer - d->bits_reader.buff
87 - ((8 - d->bits_reader.count) >> 3);
88 int offset = 0;
89
90 if (min_bytes == 0 || min_bytes > DEMUX_BUFFER_SIZE ||
91 (unread_bytes < min_bytes && flags & MPC_BUFFER_FULL))
92 min_bytes = DEMUX_BUFFER_SIZE;
93
94 if (unread_bytes < min_bytes) {
95 mpc_uint32_t bytes2read = min_bytes - unread_bytes;
96 mpc_uint32_t bytes_free = DEMUX_BUFFER_SIZE - d->bytes_total;
97
98 if (flags & MPC_BUFFER_SWAP) {
99 bytes2read &= -1 << 2;
100 offset = (unread_bytes + 3) & ( -1 << 2);
101 offset -= unread_bytes;
102 }
103
104 if (bytes2read > bytes_free) {
105 if (d->bits_reader.count == 0) {
106 d->bits_reader.count = 8;
107 d->bits_reader.buff++;
108 }
109 memmove(d->buffer + offset, d->bits_reader.buff, unread_bytes);
110 d->bits_reader.buff = d->buffer + offset;
111 d->bytes_total = unread_bytes + offset;
112 }
113 bytes2read = d->r->read(d->r, d->buffer + d->bytes_total, bytes2read);
114 if (flags & MPC_BUFFER_SWAP){
115 unsigned int i, * tmp = (unsigned int *) (d->buffer + d->bytes_total);
116 for(i = 0 ;i < (bytes2read >> 2); i++)
117 tmp[i] = swap32(tmp[i]);
118 }
119 d->bytes_total += bytes2read;
120 return bytes2read;
121 }
122
123 return (mpc_uint32_t) -1;
124}
125
126/**
127 * seek to a bit position in the stream
128 * @param d demuxer context
129 * @param fpos position in the stream in bits from the beginning of mpc datas
130 * @param min_bytes number of bytes to load after seeking
131 */
132static void
133mpc_demux_seek(mpc_demux * d, mpc_seek_t fpos, mpc_uint32_t min_bytes) {
134 mpc_seek_t next_pos;
135 mpc_int_t bit_offset;
136
137 // FIXME : do not flush the buffer if fpos is in the current buffer
138
139 next_pos = fpos >> 3;
140 if (d->si.stream_version == 7)
141 next_pos = ((next_pos - d->si.header_position) & (-1 << 2)) + d->si.header_position;
142 bit_offset = (int) (fpos - (next_pos << 3));
143
144 d->r->seek(d->r, (mpc_int32_t) next_pos);
145 mpc_demux_clear_buff(d);
146 if (d->si.stream_version == 7)
147 mpc_demux_fill(d, (min_bytes + ((bit_offset + 7) >> 3) + 3) & (~3), MPC_BUFFER_SWAP);
148 else
149 mpc_demux_fill(d, min_bytes + ((bit_offset + 7) >> 3), 0);
150 d->bits_reader.buff += bit_offset >> 3;
151 d->bits_reader.count = 8 - (bit_offset & 7);
152}
153
154/**
155 * return the current position in the stream (in bits) from the beginning
156 * of the file
157 * @param d demuxer context
158 * @return current stream position in bits
159 */
160mpc_seek_t mpc_demux_pos(mpc_demux * d)
161{
162 return (((mpc_seek_t)(d->r->tell(d->r)) - d->bytes_total +
163 d->bits_reader.buff - d->buffer) << 3) + 8 - d->bits_reader.count;
164}
165
166/**
167 * Searches for a ID3v2-tag and reads the length (in bytes) of it.
168 *
169 * @param d demuxer context
170 * @return size of tag, in bytes
171 * @return MPC_STATUS_FILE on errors of any kind
172 */
173static mpc_int32_t mpc_demux_skip_id3v2(mpc_demux * d)
174{
175 mpc_uint8_t tmp [4];
176 mpc_bool_t footerPresent; // ID3v2.4-flag
177 mpc_int32_t size;
178
179 // we must be at the beginning of the stream
180 mpc_demux_fill(d, 3, 0);
181
182 // check id3-tag
183 if ( 0 != memcmp( d->bits_reader.buff, "ID3", 3 ) )
184 return 0;
185
186 mpc_demux_fill(d, 10, 0);
187
188 mpc_bits_read(&d->bits_reader, 24); // read ID3
189 mpc_bits_read(&d->bits_reader, 16); // read tag version
190
191 tmp[0] = mpc_bits_read(&d->bits_reader, 8); // read flags
192 footerPresent = tmp[0] & 0x10;
193 if ( tmp[0] & 0x0F )
194 return MPC_STATUS_FILE; // not (yet???) allowed
195
196 tmp[0] = mpc_bits_read(&d->bits_reader, 8); // read size
197 tmp[1] = mpc_bits_read(&d->bits_reader, 8); // read size
198 tmp[2] = mpc_bits_read(&d->bits_reader, 8); // read size
199 tmp[3] = mpc_bits_read(&d->bits_reader, 8); // read size
200
201 if ( (tmp[0] | tmp[1] | tmp[2] | tmp[3]) & 0x80 )
202 return MPC_STATUS_FILE; // not allowed
203
204 // read headerSize (syncsave: 4 * $0xxxxxxx = 28 significant bits)
205 size = tmp[0] << 21;
206 size |= tmp[1] << 14;
207 size |= tmp[2] << 7;
208 size |= tmp[3];
209
210 size += 10; //header
211
212 if ( footerPresent ) size += 10;
213
214 // 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.
215 mpc_demux_clear_buff(d);
216 if (!d->r->seek(d->r, size)) return MPC_STATUS_FILE;
217
218 return size;
219}
220
221static mpc_status mpc_demux_seek_init(mpc_demux * d)
222{
223 size_t seek_table_size;
224 if (d->seek_table != 0)
225 return MPC_STATUS_OK;
226
227 d->seek_pwr = 6;
228 if (d->si.block_pwr > d->seek_pwr)
229 d->seek_pwr = d->si.block_pwr;
230 seek_table_size = (2 + d->si.samples / (MPC_FRAME_LENGTH << d->seek_pwr));
231 while (seek_table_size > MAX_SEEK_TABLE_SIZE) {
232 d->seek_pwr++;
233 seek_table_size = (2 + d->si.samples / (MPC_FRAME_LENGTH << d->seek_pwr));
234 }
235 d->seek_table = g_seek_table;
236 if (d->seek_table == 0)
237 return MPC_STATUS_FILE;
238 d->seek_table[0] = (mpc_seek_t)mpc_demux_pos(d);
239 d->seek_table_size = 1;
240
241 return MPC_STATUS_OK;
242}
243
244static void mpc_demux_ST(mpc_demux * d)
245{
246 mpc_uint64_t tmp;
247 mpc_seek_t * table, last[2];
248 mpc_bits_reader r = d->bits_reader;
249 mpc_uint_t i, diff_pwr = 0, mask;
250 mpc_uint32_t file_table_size;
251
252 if (d->seek_table != 0)
253 return;
254
255 mpc_bits_get_size(&r, &tmp);
256 file_table_size = (mpc_seek_t) tmp;
257 d->seek_pwr = d->si.block_pwr + mpc_bits_read(&r, 4);
258
259 tmp = 2 + d->si.samples / (MPC_FRAME_LENGTH << d->seek_pwr);
260 while (tmp > MAX_SEEK_TABLE_SIZE) {
261 d->seek_pwr++;
262 diff_pwr++;
263 tmp = 2 + d->si.samples / (MPC_FRAME_LENGTH << d->seek_pwr);
264 }
265 if ((file_table_size >> diff_pwr) > tmp)
266 file_table_size = tmp << diff_pwr;
267 d->seek_table = g_seek_table;
268 d->seek_table_size = (file_table_size + ((1 << diff_pwr) - 1)) >> diff_pwr;
269
270 table = d->seek_table;
271 mpc_bits_get_size(&r, &tmp);
272 table[0] = last[0] = (mpc_seek_t) (tmp + d->si.header_position) * 8;
273
274 if (d->seek_table_size == 1)
275 return;
276
277 mpc_bits_get_size(&r, &tmp);
278 last[1] = (mpc_seek_t) (tmp + d->si.header_position) * 8;
279 if (diff_pwr == 0) table[1] = last[1];
280
281 mask = (1 << diff_pwr) - 1;
282 for (i = 2; i < file_table_size; i++) {
283 int code = mpc_bits_golomb_dec(&r, 12);
284 if (code & 1)
285 code = -(code & (-1 << 1));
286 code <<= 2;
287 last[i & 1] = code + 2 * last[(i-1) & 1] - last[i & 1];
288 if ((i & mask) == 0)
289 table[i >> diff_pwr] = last[i & 1];
290 }
291}
292
293static void mpc_demux_SP(mpc_demux * d, int size, int block_size)
294{
295 mpc_seek_t cur;
296 mpc_uint64_t ptr;
297 mpc_block b;
298 int st_head_size;
299
300 cur = mpc_demux_pos(d);
301 mpc_bits_get_size(&d->bits_reader, &ptr);
302 mpc_demux_seek(d, (ptr - size) * 8 + cur, 11);
303 st_head_size = mpc_bits_get_block(&d->bits_reader, &b);
304 if (memcmp(b.key, "ST", 2) == 0) {
305 d->chap_pos = (ptr - size + b.size + st_head_size) * 8 + cur;
306 d->chap_nb = -1;
307 mpc_demux_fill(d, (mpc_uint32_t) b.size, 0);
308 mpc_demux_ST(d);
309 }
310 mpc_demux_seek(d, cur, 11 + block_size);
311}
312/* rockbox: not used
313static void mpc_demux_chap_find(mpc_demux * d)
314{
315 mpc_block b;
316 int tag_size = 0, chap_size = 0, size, i = 0;
317
318 d->chap_nb = 0;
319
320 if (d->si.stream_version < 8)
321 return;
322
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);
337 }
338 if (d->chap_pos == 0)
339 d->chap_pos = cur_pos;
340 }
341
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);
353 }
354
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);
359
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);
374 }
375 }
376
377 d->bits_reader.buff -= size;
378}
379*/
380
381/**
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
385 */
386/* rockbox: not used
387mpc_int_t mpc_demux_chap_nb(mpc_demux * d)
388{
389 if (d->chap_nb == -1)
390 mpc_demux_chap_find(d);
391 return d->chap_nb;
392}
393*/
394/**
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
400 */
401/* rockbox: not used
402mpc_chap_info const * mpc_demux_chap(mpc_demux * d, int chap_nb)
403{
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];
409}
410*/
411
412static mpc_status mpc_demux_header(mpc_demux * d)
413{
414 char magic[4];
415
416 d->si.pns = 0xFF;
417/* rockbox: not used
418 d->si.profile_name = "n.a.";
419*/
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;
423
424 d->si.tag_offset = d->si.total_file_length = d->r->get_size(d->r);
425
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);
431
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;
442 }
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);
467 }
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;
473
474 return MPC_STATUS_OK;
475}
476
477mpc_demux * mpc_demux_init(mpc_reader * p_reader)
478{
479 mpc_demux* p_tmp = &g_mpc_demux;
480
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 p_tmp->chap_nb = -1;
486 mpc_demux_clear_buff(p_tmp);
487 if (mpc_demux_header(p_tmp) == MPC_STATUS_OK &&
488 mpc_demux_seek_init(p_tmp) == MPC_STATUS_OK) {
489 p_tmp->d = mpc_decoder_init(&p_tmp->si);
490 } else {
491 if (p_tmp->seek_table)
492 memset(p_tmp->seek_table, 0, sizeof(g_seek_table));
493 p_tmp = 0;
494 }
495 }
496
497 return p_tmp;
498}
499
500void mpc_demux_exit(mpc_demux * d)
501{
502 mpc_decoder_exit(d->d);
503 memset(d->seek_table, 0, sizeof(g_seek_table));
504}
505
506void mpc_demux_get_info(mpc_demux * d, mpc_streaminfo * i)
507{
508 memcpy(i, &d->si, sizeof d->si);
509}
510
511mpc_status mpc_demux_decode(mpc_demux * d, mpc_frame_info * i)
512{
513 mpc_bits_reader r;
514 if (d->si.stream_version >= 8) {
515 i->is_key_frame = MPC_FALSE;
516
517 if (d->block_frames == 0) {
518 mpc_block b = {{0,0},0};
519 d->bits_reader.count &= -8;
520 if (d->d->decoded_samples == (d->seek_table_size << d->seek_pwr) * MPC_FRAME_LENGTH) {
521 d->seek_table[d->seek_table_size] = (mpc_seek_t) mpc_demux_pos(d);
522 d->seek_table_size ++;
523 }
524 mpc_demux_fill(d, 11, 0); // max header block size
525 mpc_bits_get_block(&d->bits_reader, &b);
526 while( memcmp(b.key, "AP", 2) != 0 ) { // scan all blocks until audio
527 if (mpc_check_key(b.key) != MPC_STATUS_OK)
528 goto error;
529 if (memcmp(b.key, "SE", 2) == 0) { // end block
530 i->bits = -1;
531 return MPC_STATUS_OK;
532 }
533 if (mpc_demux_fill(d, 11 + (mpc_uint32_t) b.size, 0) == 0)
534 goto error;
535 d->bits_reader.buff += b.size;
536 mpc_bits_get_block(&d->bits_reader, &b);
537 }
538 d->block_bits = (mpc_uint32_t) b.size * 8;
539 d->block_frames = 1 << d->si.block_pwr;
540 i->is_key_frame = MPC_TRUE;
541 }
542 if (d->buffer + d->bytes_total - d->bits_reader.buff <= MAX_FRAME_SIZE)
543 mpc_demux_fill(d, (d->block_bits >> 3) + 1, 0);
544 r = d->bits_reader;
545 mpc_decoder_decode_frame(d->d, &d->bits_reader, i);
546 d->block_bits -= ((d->bits_reader.buff - r.buff) << 3) + r.count - d->bits_reader.count;
547 d->block_frames--;
548 if (d->block_bits < 0 || (d->block_frames == 0 && d->block_bits > 7))
549 goto error;
550 } else {
551 if (d->d->decoded_samples == (d->seek_table_size << d->seek_pwr) * MPC_FRAME_LENGTH) {
552 d->seek_table[d->seek_table_size] = (mpc_seek_t) mpc_demux_pos(d);
553 d->seek_table_size ++;
554 }
555 mpc_demux_fill(d, MAX_FRAME_SIZE, MPC_BUFFER_FULL | MPC_BUFFER_SWAP);
556 d->block_bits = (mpc_int_t) mpc_bits_read(&d->bits_reader, 20); // read frame size
557 if (MPC_FRAME_LENGTH > d->d->samples - d->d->decoded_samples - 1) d->block_bits += 11; // we will read last frame size
558 r = d->bits_reader;
559 mpc_decoder_decode_frame(d->d, &d->bits_reader, i);
560 if (i->bits != -1 && d->block_bits != (mpc_int32_t)(((d->bits_reader.buff - r.buff) << 3) + r.count - d->bits_reader.count))
561 goto error;
562 }
563 if (i->bits != -1 && d->buffer + d->bytes_total < d->bits_reader.buff + ((8 - d->bits_reader.count) >> 3))
564 goto error;
565
566 return MPC_STATUS_OK;
567error:
568 i->bits = -1; // we pretend it's end of file
569 return MPC_STATUS_INVALIDSV;
570}
571
572mpc_status mpc_demux_seek_second(mpc_demux * d, double seconds)
573{
574 return mpc_demux_seek_sample(d, (mpc_int64_t)(seconds * (double)d->si.sample_freq + 0.5));
575}
576
577mpc_status mpc_demux_seek_sample(mpc_demux * d, mpc_uint64_t destsample)
578{
579 mpc_uint32_t fwd, samples_to_skip, i;
580 mpc_uint32_t block_samples = MPC_FRAME_LENGTH << d->si.block_pwr;
581 mpc_seek_t fpos;
582
583 destsample += d->si.beg_silence;
584 if (destsample > d->si.samples) destsample = d->si.samples;
585 fwd = (mpc_uint32_t) (destsample / block_samples);
586 samples_to_skip = MPC_DECODER_SYNTH_DELAY +
587 (mpc_uint32_t) (destsample % block_samples);
588 if (d->si.stream_version == 7) {
589 if (fwd > 32) {
590 fwd -= 32;
591 samples_to_skip += MPC_FRAME_LENGTH * 32;
592 } else {
593 samples_to_skip += MPC_FRAME_LENGTH * fwd;
594 fwd = 0;
595 }
596 }
597
598 i = fwd >> (d->seek_pwr - d->si.block_pwr);
599 if (i >= d->seek_table_size)
600 i = d->seek_table_size - 1;
601 fpos = d->seek_table[i];
602 i <<= d->seek_pwr - d->si.block_pwr;
603 d->d->decoded_samples = i * block_samples;
604
605 if (d->si.stream_version >= 8) {
606 mpc_block b;
607 int size;
608 mpc_demux_seek(d, fpos, 11);
609 size = mpc_bits_get_block(&d->bits_reader, &b);
610 while(i < fwd) {
611 if (memcmp(b.key, "AP", 2) == 0) {
612 if (d->d->decoded_samples == (d->seek_table_size << d->seek_pwr) * MPC_FRAME_LENGTH) {
613 d->seek_table[d->seek_table_size] = (mpc_seek_t) mpc_demux_pos(d) - 8 * size;
614 d->seek_table_size ++;
615 }
616 d->d->decoded_samples += block_samples;
617 i++;
618 }
619 fpos += ((mpc_uint32_t)b.size + size) * 8;
620 mpc_demux_seek(d, fpos, 11);
621 size = mpc_bits_get_block(&d->bits_reader, &b);
622 }
623 d->bits_reader.buff -= size;
624 } else {
625 mpc_decoder_reset_scf(d->d, fwd != 0);
626 mpc_demux_seek(d, fpos, 4);
627 for( ; i < fwd; i++){
628 if (d->d->decoded_samples == (d->seek_table_size << d->seek_pwr) * MPC_FRAME_LENGTH) {
629 d->seek_table[d->seek_table_size] = (mpc_seek_t) mpc_demux_pos(d);
630 d->seek_table_size ++;
631 }
632 d->d->decoded_samples += block_samples;
633 fpos += mpc_bits_read(&d->bits_reader, 20) + 20;
634 mpc_demux_seek(d, fpos, 4);
635 }
636 }
637 d->d->samples_to_skip = samples_to_skip;
638 return MPC_STATUS_OK;
639}
640
641/* rockbox: not used
642void mpc_set_replay_level(mpc_demux * d, float level, mpc_bool_t use_gain,
643 mpc_bool_t use_title, mpc_bool_t clip_prevention)
644{
645 float peak = use_title ? d->si.peak_title : d->si.peak_album;
646 float gain = use_title ? d->si.gain_title : d->si.gain_album;
647
648 if(!use_gain && !clip_prevention)
649 return;
650
651 if(!peak)
652 peak = 1.;
653 else
654 peak = (1 << 15) / pow(10, peak / (20 * 256));
655
656 if(!gain)
657 gain = 1.;
658 else
659 gain = pow(10, (level - gain / 256) / 20);
660
661 if(clip_prevention && (peak < gain || !use_gain))
662 gain = peak;
663
664 mpc_decoder_scale_output(d->d, gain);
665}
666*/