A modern Music Player Daemon based on Rockbox open source high quality audio player
libadwaita audio rust zig deno mpris rockbox mpd
2
fork

Configure Feed

Select the types of activity you want to include in your feed.

at master 730 lines 25 kB view raw
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// defines 51#define MAX_BUFFER_SIZE (DEMUX_BUFFER_SIZE + MAX_FRAME_SIZE) 52 53// globals 54static mpc_uint8_t g_buffer[MAX_BUFFER_SIZE] IBSS_ATTR_MPC_BITBUFFER; 55static mpc_seek_t g_seek_table[MAX_SEEK_TABLE_SIZE]; 56static mpc_demux g_mpc_demux IBSS_ATTR; 57 58enum { 59 MPC_BUFFER_SWAP = 1, 60 MPC_BUFFER_FULL = 2, 61}; 62 63static void mpc_demux_clear_buff(mpc_demux * d) 64{ 65 d->bytes_total = 0; 66 d->bits_reader.buff = d->buffer; 67 d->bits_reader.count = 8; 68 d->bits_reader.buffered_addr = 0; 69 d->bits_reader.buffered_code = 0; 70 d->block_bits = 0; 71 d->block_frames = 0; 72 memset(d->buffer, 0, sizeof(g_buffer)); 73} 74 75static mpc_uint32_t 76mpc_demux_fill(mpc_demux * d, mpc_uint32_t min_bytes, int flags) 77{ 78 mpc_uint32_t unread_bytes = d->bytes_total + d->buffer - d->bits_reader.buff 79 - ((8 - d->bits_reader.count) >> 3); 80 mpc_int32_t offset = 0; 81 82 if (min_bytes == 0 || min_bytes > MAX_BUFFER_SIZE || 83 (unread_bytes < min_bytes && flags & MPC_BUFFER_FULL)) 84 min_bytes = MAX_BUFFER_SIZE; 85 86 if (unread_bytes < min_bytes) { 87 mpc_uint32_t bytes2read = min_bytes - unread_bytes; 88 mpc_uint32_t bytes_free = MAX_BUFFER_SIZE - d->bytes_total; 89 90 if (flags & MPC_BUFFER_SWAP) { 91 bytes2read &= -1 << 2; 92 offset = (unread_bytes + 3) & ( -1 << 2); 93 offset -= unread_bytes; 94 } 95 96 if (bytes2read > bytes_free) { 97 if (d->bits_reader.count == 0) { 98 d->bits_reader.count = 8; 99 d->bits_reader.buff++; 100 } 101 memmove(d->buffer + offset, d->bits_reader.buff, unread_bytes); 102 d->bits_reader.buff = d->buffer + offset; 103 d->bytes_total = unread_bytes + offset; 104 /* reset Coldfire optimized read when rebuffering */ 105 d->bits_reader.buffered_addr = 0; 106 d->bits_reader.buffered_code = 0; 107 } 108 bytes2read = d->r->read(d->r, d->buffer + d->bytes_total, bytes2read); 109 if (flags & MPC_BUFFER_SWAP){ 110 unsigned int i, * tmp = (unsigned int *) (d->buffer + d->bytes_total); 111 for(i = 0 ;i < (bytes2read >> 2); i++) 112 tmp[i] = swap32(tmp[i]); 113 } 114 d->bytes_total += bytes2read; 115 return bytes2read; 116 } 117 118 return (mpc_uint32_t) -1; 119} 120 121/** 122 * seek to a bit position in the stream 123 * @param d demuxer context 124 * @param fpos position in the stream in bits from the beginning of mpc datas 125 * @param min_bytes number of bytes to load after seeking 126 */ 127static mpc_status 128mpc_demux_seek(mpc_demux * d, mpc_seek_t fpos, mpc_uint32_t min_bytes) { 129 // d->bits_reader.buff - d->buffer = current byte position within buffer 130 // d->bytes_total = buffer is filled with bytes_total bytes 131 // fpos = desired file position in bit (not byte) 132 // buf_fpos = desired byte position within buffer 133 mpc_seek_t next_pos = fpos>>3; 134 mpc_int_t buf_fpos = next_pos - d->r->tell(d->r) + d->bytes_total; 135 136 // is desired byte position within lower and upper boundaries of buffer? 137 if (buf_fpos >= 0 && buf_fpos + min_bytes <= d->bytes_total) { 138 // desired bytes are available in current buffer 139 d->bits_reader.buff += buf_fpos - (d->bits_reader.buff - d->buffer); 140 d->bits_reader.count = 8 - (fpos & 7); 141 } else { 142 // buffer needs to be refilled 143 if (d->si.stream_version == 7) 144 next_pos = ((next_pos - d->si.header_position) & (-1 << 2)) + d->si.header_position; 145 buf_fpos = fpos - (next_pos << 3); 146 if (!d->r->seek(d->r, (mpc_int32_t) next_pos)) 147 return MPC_STATUS_FAIL; 148 mpc_demux_clear_buff(d); 149 if (d->si.stream_version == 7) 150 mpc_demux_fill(d, MAX_BUFFER_SIZE, MPC_BUFFER_FULL | MPC_BUFFER_SWAP); 151 else 152 mpc_demux_fill(d, MAX_BUFFER_SIZE, MPC_BUFFER_FULL); 153 d->bits_reader.buff += buf_fpos >> 3; 154 d->bits_reader.count = 8 - (buf_fpos & 7); 155 } 156 157 return MPC_STATUS_OK; 158} 159 160/** 161 * return the current position in the stream (in bits) from the beginning 162 * of the file 163 * @param d demuxer context 164 * @return current stream position in bits 165 */ 166static mpc_seek_t mpc_demux_pos(mpc_demux * d) 167{ 168 return (((mpc_seek_t)(d->r->tell(d->r)) - d->bytes_total + 169 d->bits_reader.buff - d->buffer) << 3) + 8 - d->bits_reader.count; 170} 171 172/** 173 * Searches for a ID3v2-tag and reads the length (in bytes) of it. 174 * 175 * @param d demuxer context 176 * @return size of tag, in bytes 177 * @return MPC_STATUS_FAIL on errors of any kind 178 */ 179static mpc_int32_t mpc_demux_skip_id3v2(mpc_demux * d) 180{ 181 mpc_uint8_t tmp [4]; 182 mpc_bool_t footerPresent; // ID3v2.4-flag 183 mpc_int32_t size; 184 185 // we must be at the beginning of the stream 186 mpc_demux_fill(d, 3, 0); 187 188 // check id3-tag 189 if ( 0 != memcmp( d->bits_reader.buff, "ID3", 3 ) ) 190 return 0; 191 192 mpc_demux_fill(d, 10, 0); 193 194 mpc_bits_read(&d->bits_reader, 24); // read ID3 195 mpc_bits_read(&d->bits_reader, 16); // read tag version 196 197 tmp[0] = mpc_bits_read(&d->bits_reader, 8); // read flags 198 footerPresent = tmp[0] & 0x10; 199 if ( tmp[0] & 0x0F ) 200 return MPC_STATUS_FAIL; // not (yet???) allowed 201 202 tmp[0] = mpc_bits_read(&d->bits_reader, 8); // read size 203 tmp[1] = mpc_bits_read(&d->bits_reader, 8); // read size 204 tmp[2] = mpc_bits_read(&d->bits_reader, 8); // read size 205 tmp[3] = mpc_bits_read(&d->bits_reader, 8); // read size 206 207 if ( (tmp[0] | tmp[1] | tmp[2] | tmp[3]) & 0x80 ) 208 return MPC_STATUS_FAIL; // not allowed 209 210 // read headerSize (syncsave: 4 * $0xxxxxxx = 28 significant bits) 211 size = tmp[0] << 21; 212 size |= tmp[1] << 14; 213 size |= tmp[2] << 7; 214 size |= tmp[3]; 215 216 size += 10; //header 217 218 if ( footerPresent ) size += 10; 219 220 // 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. 221 mpc_demux_clear_buff(d); 222 if (!d->r->seek(d->r, size)) 223 return MPC_STATUS_FAIL; 224 225 return size; 226} 227 228static mpc_status mpc_demux_seek_init(mpc_demux * d) 229{ 230 size_t seek_table_size; 231 if (d->seek_table != 0) 232 return MPC_STATUS_OK; 233 234 d->seek_pwr = 6; 235 if (d->si.block_pwr > d->seek_pwr) 236 d->seek_pwr = d->si.block_pwr; 237 seek_table_size = (2 + d->si.samples / (MPC_FRAME_LENGTH << d->seek_pwr)); 238 while (seek_table_size > MAX_SEEK_TABLE_SIZE) { 239 d->seek_pwr++; 240 seek_table_size = (2 + d->si.samples / (MPC_FRAME_LENGTH << d->seek_pwr)); 241 } 242 d->seek_table = g_seek_table; 243 if (d->seek_table == 0) 244 return MPC_STATUS_FAIL; 245 d->seek_table[0] = (mpc_seek_t)mpc_demux_pos(d); 246 d->seek_table_size = 1; 247 248 return MPC_STATUS_OK; 249} 250 251/* rockbox: do not use 252static mpc_status mpc_demux_ST(mpc_demux * d) 253{ 254 mpc_uint64_t tmp; 255 mpc_seek_t * table, last[2]; 256 mpc_bits_reader r = d->bits_reader; 257 mpc_uint_t i, diff_pwr = 0, mask; 258 mpc_uint32_t file_table_size; 259 260 if (d->seek_table != 0) 261 return MPC_STATUS_OK; 262 263 mpc_bits_get_size(&r, &tmp); 264 file_table_size = (mpc_seek_t) tmp; 265 d->seek_pwr = d->si.block_pwr + mpc_bits_read(&r, 4); 266 267 tmp = 2 + d->si.samples / (MPC_FRAME_LENGTH << d->seek_pwr); 268 while (tmp > MAX_SEEK_TABLE_SIZE) { 269 d->seek_pwr++; 270 diff_pwr++; 271 tmp = 2 + d->si.samples / (MPC_FRAME_LENGTH << d->seek_pwr); 272 } 273 if ((file_table_size >> diff_pwr) > tmp) 274 file_table_size = tmp << diff_pwr; 275 d->seek_table = g_seek_table; 276 d->seek_table_size = (file_table_size + ((1 << diff_pwr) - 1)) >> diff_pwr; 277 278 table = d->seek_table; 279 mpc_bits_get_size(&r, &tmp); 280 table[0] = last[0] = (mpc_seek_t) (tmp + d->si.header_position) * 8; 281 282 if (d->seek_table_size == 1) 283 return MPC_STATUS_OK; 284 285 mpc_bits_get_size(&r, &tmp); 286 last[1] = (mpc_seek_t) (tmp + d->si.header_position) * 8; 287 if (diff_pwr == 0) table[1] = last[1]; 288 289 mask = (1 << diff_pwr) - 1; 290 for (i = 2; i < file_table_size; i++) { 291 int code = mpc_bits_golomb_dec(&r, 12); 292 if (code & 1) 293 code = -(code & (-1 << 1)); 294 code <<= 2; 295 last[i & 1] = code + 2 * last[(i-1) & 1] - last[i & 1]; 296 if ((i & mask) == 0) 297 table[i >> diff_pwr] = last[i & 1]; 298 } 299 return MPC_STATUS_OK; 300} 301 302static mpc_status mpc_demux_SP(mpc_demux * d, int size, int block_size) 303{ 304 mpc_seek_t cur; 305 mpc_uint64_t ptr; 306 mpc_block b; 307 int st_head_size; 308 309 cur = mpc_demux_pos(d); 310 mpc_bits_get_size(&d->bits_reader, &ptr); 311 MPC_AUTO_FAIL( mpc_demux_seek(d, (ptr - size) * 8 + cur, 11) ); 312 st_head_size = mpc_bits_get_block(&d->bits_reader, &b); 313 if (memcmp(b.key, "ST", 2) == 0) { 314 d->chap_pos = (ptr - size + b.size + st_head_size) * 8 + cur; 315 d->chap_nb = -1; 316 if (mpc_demux_fill(d, (mpc_uint32_t) b.size, 0) < b.size) 317 return MPC_STATUS_FAIL; 318 MPC_AUTO_FAIL( mpc_demux_ST(d) ); 319 } 320 return mpc_demux_seek(d, cur, 11 + block_size); 321} 322*/ 323/* rockbox: not used 324static void mpc_demux_chap_empty(mpc_demux * d) { 325 free(d->chap); d->chap = 0; 326 d->chap_nb = 0; // -1 for undefined, 0 for no chapters 327 d->chap_pos = 0; 328} 329*/ 330/* rockbox: not used 331static mpc_status mpc_demux_chap_find_inner(mpc_demux * d) 332{ 333 mpc_block b; 334 int tag_size = 0, chap_size = 0, size, i = 0; 335 336 d->chap_nb = 0; 337 338 if (d->si.stream_version < 8) 339 return MPC_STATUS_OK; 340 341 if (d->chap_pos == 0) { 342 mpc_uint64_t cur_pos = (d->si.header_position + 4) * 8; 343 MPC_AUTO_FAIL( mpc_demux_seek(d, cur_pos, 11) ); // seek to the beginning of the stream 344 size = mpc_bits_get_block(&d->bits_reader, &b); 345 while (memcmp(b.key, "SE", 2) != 0) { 346 mpc_uint64_t new_pos = cur_pos + (size + b.size) * 8; 347 MPC_AUTO_FAIL(mpc_check_key(b.key)); 348 349 if (memcmp(b.key, "CT", 2) == 0) { 350 if (d->chap_pos == 0) d->chap_pos = cur_pos; 351 } else { 352 d->chap_pos = 0; 353 } 354 if (new_pos <= cur_pos) 355 return MPC_STATUS_FAIL; 356 cur_pos = new_pos; 357 358 MPC_AUTO_FAIL( mpc_demux_seek(d, cur_pos, 11) ); 359 size = mpc_bits_get_block(&d->bits_reader, &b); 360 } 361 if (d->chap_pos == 0) 362 d->chap_pos = cur_pos; 363 } 364 365 mpc_demux_seek(d, d->chap_pos, 20); 366 size = mpc_bits_get_block(&d->bits_reader, &b); 367 while (memcmp(b.key, "CT", 2) == 0) { 368 mpc_uint64_t chap_sample; 369 d->chap_nb++; 370 chap_size += size; 371 size = mpc_bits_get_size(&d->bits_reader, &chap_sample) + 4; 372 chap_size += size; 373 tag_size += b.size - size; 374 MPC_AUTO_FAIL( mpc_demux_seek(d, d->chap_pos + (chap_size + tag_size) * 8, 20) ); 375 size = mpc_bits_get_block(&d->bits_reader, &b); 376 } 377 378 if (d->chap_nb > 0) { 379 char * ptag; 380 d->chap = malloc(sizeof(mpc_chap_info) * d->chap_nb + tag_size); 381 if (d->chap == 0) 382 return MPC_STATUS_FAIL; 383 384 ptag = (char*)(d->chap + d->chap_nb); 385 386 MPC_AUTO_FAIL( mpc_demux_seek(d, d->chap_pos, 11) ); 387 size = mpc_bits_get_block(&d->bits_reader, &b); 388 while (memcmp(b.key, "CT", 2) == 0) { 389 mpc_uint_t tmp_size; 390 char * tmp_ptag = ptag; 391 if (mpc_demux_fill(d, 11 + (mpc_uint32_t) b.size, 0) < b.size) 392 return MPC_STATUS_FAIL; 393 size = mpc_bits_get_size(&d->bits_reader, &d->chap[i].sample) + 4; 394 d->chap[i].gain = (mpc_uint16_t) mpc_bits_read(&d->bits_reader, 16); 395 d->chap[i].peak = (mpc_uint16_t) mpc_bits_read(&d->bits_reader, 16); 396 397 tmp_size = b.size - size; 398 do { 399 mpc_uint_t rd_size = tmp_size; 400 mpc_uint8_t * tmp_buff = d->bits_reader.buff + ((8 - d->bits_reader.count) >> 3); 401 mpc_uint32_t avail_bytes = d->bytes_total + d->buffer - tmp_buff; 402 rd_size = mini(rd_size, avail_bytes); 403 memcpy(tmp_ptag, tmp_buff, rd_size); 404 tmp_size -= rd_size; 405 tmp_ptag += rd_size; 406 d->bits_reader.buff += rd_size; 407 mpc_demux_fill(d, tmp_size, 0); 408 } while (tmp_size > 0); 409 410 d->chap[i].tag_size = b.size - size; 411 d->chap[i].tag = ptag; 412 ptag += b.size - size; 413 i++; 414 size = mpc_bits_get_block(&d->bits_reader, &b); 415 } 416 } 417 418 d->bits_reader.buff -= size; 419 return MPC_STATUS_OK; 420} 421*/ 422/* rockbox: not used 423static mpc_status mpc_demux_chap_find(mpc_demux * d) { 424 mpc_status s = mpc_demux_chap_find_inner(d); 425 if (MPC_IS_FAILURE(s)) 426 mpc_demux_chap_empty(d); 427 return s; 428} 429*/ 430/** 431 * Gets the number of chapters in the stream 432 * @param d pointer to a musepack demuxer 433 * @return the number of chapters found in the stream 434 */ 435/* rockbox: not used 436mpc_int_t mpc_demux_chap_nb(mpc_demux * d) 437{ 438 if (d->chap_nb == -1) 439 mpc_demux_chap_find(d); 440 return d->chap_nb; 441} 442*/ 443/** 444 * Gets datas associated to a given chapter 445 * The chapter tag is an APEv2 tag without the preamble 446 * @param d pointer to a musepack demuxer 447 * @param chap_nb chapter number you want datas (from 0 to mpc_demux_chap_nb(d) - 1) 448 * @return the chapter information structure 449 */ 450/* rockbox: not used 451mpc_chap_info const * mpc_demux_chap(mpc_demux * d, int chap_nb) 452{ 453 if (d->chap_nb == -1) 454 mpc_demux_chap_find(d); 455 if (chap_nb >= d->chap_nb || chap_nb < 0) 456 return 0; 457 return &d->chap[chap_nb]; 458} 459*/ 460 461static mpc_status mpc_demux_header(mpc_demux * d) 462{ 463 char magic[4]; 464 465 d->si.pns = 0xFF; 466/* rockbox: not used 467 d->si.profile_name = "n.a."; 468*/ 469 // get header position 470 d->si.header_position = mpc_demux_skip_id3v2(d); 471 if(d->si.header_position < 0) 472 return MPC_STATUS_FAIL; 473 474 d->si.tag_offset = d->si.total_file_length = d->r->get_size(d->r); 475 476 mpc_demux_fill(d, 4, 0); 477 magic[0] = mpc_bits_read(&d->bits_reader, 8); 478 magic[1] = mpc_bits_read(&d->bits_reader, 8); 479 magic[2] = mpc_bits_read(&d->bits_reader, 8); 480 magic[3] = mpc_bits_read(&d->bits_reader, 8); 481 482 if (memcmp(magic, "MP+", 3) == 0) { 483 d->si.stream_version = magic[3] & 15; 484 d->si.pns = magic[3] >> 4; 485 if (d->si.stream_version != 7) 486 return MPC_STATUS_FAIL; 487 if (mpc_demux_fill(d, 6 * 4, MPC_BUFFER_SWAP) < 6 * 4) // header block size + endian convertion 488 return MPC_STATUS_FAIL; 489 MPC_AUTO_FAIL( streaminfo_read_header_sv7(&d->si, &d->bits_reader) ); 490 } else if (memcmp(magic, "MPCK", 4) == 0) { 491 mpc_block b; 492 int size; 493 mpc_demux_fill(d, 11, 0); // max header block size 494 size = mpc_bits_get_block(&d->bits_reader, &b); 495 while( memcmp(b.key, "AP", 2) != 0 ){ // scan all blocks until audio 496 if (mpc_check_key(b.key) != MPC_STATUS_OK) 497 return MPC_STATUS_FAIL; 498 if (b.size > (mpc_uint64_t) MAX_BUFFER_SIZE - 11) 499 return MPC_STATUS_FAIL; 500 501 if (mpc_demux_fill(d, 11 + (mpc_uint32_t) b.size, 0) <= b.size) 502 return MPC_STATUS_FAIL; 503 504 if (memcmp(b.key, "SH", 2) == 0) { 505 MPC_AUTO_FAIL( streaminfo_read_header_sv8(&d->si, &d->bits_reader, (mpc_uint32_t) b.size) ); 506 } else if (memcmp(b.key, "RG", 2) == 0) { 507 streaminfo_gain(&d->si, &d->bits_reader); 508 } else if (memcmp(b.key, "EI", 2) == 0) { 509 streaminfo_encoder_info(&d->si, &d->bits_reader); 510/* rockbox: do not use 511 } else if (memcmp(b.key, "SO", 2) == 0) { 512 MPC_AUTO_FAIL( mpc_demux_SP(d, size, (mpc_uint32_t) b.size) ); 513 } else if (memcmp(b.key, "ST", 2) == 0) { 514 MPC_AUTO_FAIL( mpc_demux_ST(d) ); 515*/ 516 } 517 d->bits_reader.buff += b.size; 518 size = mpc_bits_get_block(&d->bits_reader, &b); 519 } 520 d->bits_reader.buff -= size; 521 if (d->si.stream_version == 0) // si not initialized !!! 522 return MPC_STATUS_FAIL; 523 } else { 524 return MPC_STATUS_FAIL; 525 } 526 527 return MPC_STATUS_OK; 528} 529 530mpc_demux * mpc_demux_init(mpc_reader * p_reader) 531{ 532 mpc_demux* p_tmp = &g_mpc_demux; 533 534 if (p_tmp != 0) { 535 memset(p_tmp, 0, sizeof(mpc_demux)); 536 p_tmp->buffer = g_buffer; 537 p_tmp->r = p_reader; 538/* rockbox: not used 539 p_tmp->chap_nb = -1; 540*/ 541 mpc_demux_clear_buff(p_tmp); 542 if (mpc_demux_header(p_tmp) == MPC_STATUS_OK && 543 mpc_demux_seek_init(p_tmp) == MPC_STATUS_OK) { 544 p_tmp->d = mpc_decoder_init(&p_tmp->si); 545 } else { 546 if (p_tmp->seek_table) 547 memset(p_tmp->seek_table, 0, sizeof(g_seek_table)); 548 p_tmp = 0; 549 } 550 } 551 552 return p_tmp; 553} 554 555/* rockbox: not used 556void mpc_demux_exit(mpc_demux * d) 557{ 558 mpc_decoder_exit(d->d); 559 memset(d->seek_table, 0, sizeof(g_seek_table)); 560} 561*/ 562 563void mpc_demux_get_info(mpc_demux * d, mpc_streaminfo * i) 564{ 565 memcpy(i, &d->si, sizeof d->si); 566} 567 568static mpc_status mpc_demux_decode_inner(mpc_demux * d, mpc_frame_info * i) 569{ 570 mpc_bits_reader r; 571 if (d->si.stream_version >= 8) { 572 i->is_key_frame = MPC_FALSE; 573 574 if (d->block_frames == 0) { 575 mpc_block b = {{0,0},0}; 576 d->bits_reader.count &= -8; 577 if (d->d->decoded_samples == (d->seek_table_size << d->seek_pwr) * MPC_FRAME_LENGTH) { 578 d->seek_table[d->seek_table_size] = (mpc_seek_t) mpc_demux_pos(d); 579 d->seek_table_size ++; 580 } 581 mpc_demux_fill(d, 11, MPC_BUFFER_FULL); // max header block size 582 mpc_bits_get_block(&d->bits_reader, &b); 583 while( memcmp(b.key, "AP", 2) != 0 ) { // scan all blocks until audio 584 MPC_AUTO_FAIL( mpc_check_key(b.key) ); 585 586 if (memcmp(b.key, "SE", 2) == 0) { // end block 587 i->bits = -1; 588 return MPC_STATUS_OK; 589 } 590 591 if (mpc_demux_fill(d, 11 + (mpc_uint32_t) b.size, MPC_BUFFER_FULL) < b.size) 592 return MPC_STATUS_FAIL; 593 594 d->bits_reader.buff += b.size; 595 mpc_bits_get_block(&d->bits_reader, &b); 596 } 597 d->block_bits = (mpc_uint32_t) b.size * 8; 598 d->block_frames = 1 << d->si.block_pwr; 599 i->is_key_frame = MPC_TRUE; 600 } 601 mpc_demux_fill(d, MAX_FRAME_SIZE, MPC_BUFFER_FULL); 602 r = d->bits_reader; 603 mpc_decoder_decode_frame(d->d, &d->bits_reader, i); 604 d->block_bits -= ((d->bits_reader.buff - r.buff) << 3) + r.count - d->bits_reader.count; 605 d->block_frames--; 606 if (d->block_bits < 0 || (d->block_frames == 0 && d->block_bits > 7)) 607 return MPC_STATUS_FAIL; 608 } else { 609 if (d->d->decoded_samples == (d->seek_table_size << d->seek_pwr) * MPC_FRAME_LENGTH) { 610 d->seek_table[d->seek_table_size] = (mpc_seek_t) mpc_demux_pos(d); 611 d->seek_table_size ++; 612 } 613 mpc_demux_fill(d, MAX_FRAME_SIZE, MPC_BUFFER_FULL | MPC_BUFFER_SWAP); 614 d->block_bits = (mpc_int_t) mpc_bits_read(&d->bits_reader, 20); // read frame size 615 if (MPC_FRAME_LENGTH > d->d->samples - d->d->decoded_samples - 1) d->block_bits += 11; // we will read last frame size 616 r = d->bits_reader; 617 mpc_decoder_decode_frame(d->d, &d->bits_reader, i); 618 if (i->bits != -1 && d->block_bits != (mpc_int32_t)(((d->bits_reader.buff - r.buff) << 3) + r.count - d->bits_reader.count)) 619 return MPC_STATUS_FAIL; 620 } 621 if (i->bits != -1 && d->buffer + d->bytes_total < d->bits_reader.buff + ((8 - d->bits_reader.count) >> 3)) 622 return MPC_STATUS_FAIL; 623 624 return MPC_STATUS_OK; 625} 626 627mpc_status mpc_demux_decode(mpc_demux * d, mpc_frame_info * i) { 628 mpc_status s = mpc_demux_decode_inner(d, i); 629 if (MPC_IS_FAILURE(s)) 630 i->bits = -1; // we pretend it's end of file 631 return s; 632} 633 634/* rockbox: not used 635mpc_status mpc_demux_seek_second(mpc_demux * d, double seconds) 636{ 637 return mpc_demux_seek_sample(d, (mpc_int64_t)(seconds * (double)d->si.sample_freq + 0.5)); 638} 639*/ 640 641mpc_status mpc_demux_seek_sample(mpc_demux * d, mpc_uint64_t destsample) 642{ 643 mpc_uint32_t fwd, samples_to_skip, i; 644 mpc_uint32_t block_samples = MPC_FRAME_LENGTH << d->si.block_pwr; 645 mpc_seek_t fpos; 646 647 destsample += d->si.beg_silence; 648 if (destsample > d->si.samples) destsample = d->si.samples; 649 fwd = (mpc_uint32_t) (destsample / block_samples); 650 samples_to_skip = MPC_DECODER_SYNTH_DELAY + 651 (mpc_uint32_t) (destsample % block_samples); 652 if (d->si.stream_version == 7) { 653 if (fwd > 32) { 654 fwd -= 32; 655 samples_to_skip += MPC_FRAME_LENGTH * 32; 656 } else { 657 samples_to_skip += MPC_FRAME_LENGTH * fwd; 658 fwd = 0; 659 } 660 } 661 662 i = fwd >> (d->seek_pwr - d->si.block_pwr); 663 if (i >= d->seek_table_size) 664 i = d->seek_table_size - 1; 665 fpos = d->seek_table[i]; 666 i <<= d->seek_pwr - d->si.block_pwr; 667 d->d->decoded_samples = i * block_samples; 668 669 if (d->si.stream_version >= 8) { 670 mpc_block b; 671 int size; 672 mpc_demux_seek(d, fpos, 11); 673 size = mpc_bits_get_block(&d->bits_reader, &b); 674 while(i < fwd) { 675 if (memcmp(b.key, "AP", 2) == 0) { 676 if (d->d->decoded_samples == (d->seek_table_size << d->seek_pwr) * MPC_FRAME_LENGTH) { 677 d->seek_table[d->seek_table_size] = (mpc_seek_t) mpc_demux_pos(d) - 8 * size; 678 d->seek_table_size ++; 679 } 680 d->d->decoded_samples += block_samples; 681 i++; 682 } 683 fpos += ((mpc_uint32_t)b.size + size) * 8; 684 mpc_demux_seek(d, fpos, 11); 685 size = mpc_bits_get_block(&d->bits_reader, &b); 686 } 687 d->bits_reader.buff -= size; 688 } else { 689 mpc_decoder_reset_scf(d->d, fwd != 0); 690 mpc_demux_seek(d, fpos, 4); 691 for( ; i < fwd; i++){ 692 if (d->d->decoded_samples == (d->seek_table_size << d->seek_pwr) * MPC_FRAME_LENGTH) { 693 d->seek_table[d->seek_table_size] = (mpc_seek_t) mpc_demux_pos(d); 694 d->seek_table_size ++; 695 } 696 d->d->decoded_samples += block_samples; 697 fpos += mpc_bits_read(&d->bits_reader, 20) + 20; 698 mpc_demux_seek(d, fpos, 4); 699 } 700 } 701 d->d->samples_to_skip = samples_to_skip; 702 return MPC_STATUS_OK; 703} 704 705/* rockbox: not used 706void mpc_set_replay_level(mpc_demux * d, float level, mpc_bool_t use_gain, 707 mpc_bool_t use_title, mpc_bool_t clip_prevention) 708{ 709 float peak = (float) ( use_title ? d->si.peak_title : d->si.peak_album ); 710 float gain = (float) ( use_title ? d->si.gain_title : d->si.gain_album ); 711 712 if(!use_gain && !clip_prevention) 713 return; 714 715 if(!peak) 716 peak = 1.; 717 else 718 peak = (float) ( (1 << 15) / pow(10, peak / (20 * 256)) ); 719 720 if(!gain) 721 gain = 1.; 722 else 723 gain = (float) pow(10, (level - gain / 256) / 20); 724 725 if(clip_prevention && (peak < gain || !use_gain)) 726 gain = peak; 727 728 mpc_decoder_scale_output(d->d, gain); 729} 730*/