Changeset 473 for libmpc/trunk/libmpcdec/mpc_demux.c
- Timestamp:
- 07/28/11 21:51:30 (13 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
libmpc/trunk/libmpcdec/mpc_demux.c
r466 r473 72 72 } 73 73 74 // Returns the amount of unread bytes in the demux buffer. 75 // Unchecked version - may return a negative value when we've been reading 76 // past the end of the valid data as a result of some problem with the file. 77 static mpc_int32_t mpc_unread_bytes_unchecked(mpc_demux * d) { 78 return d->bytes_total + d->buffer - d->bits_reader.buff - ((8 - d->bits_reader.count) >> 3); 79 } 80 81 // Returns the amount of unread bytes in the demux buffer. 82 static mpc_uint32_t mpc_unread_bytes(mpc_demux * d) { 83 mpc_int32_t unread_bytes = mpc_unread_bytes_unchecked(d); 84 85 if (unread_bytes < 0) return 0; 86 87 return (mpc_uint32_t) unread_bytes; 88 } 89 90 91 92 // Returns the number of bytes available in the buffer. 74 93 static mpc_uint32_t 75 94 mpc_demux_fill(mpc_demux * d, mpc_uint32_t min_bytes, int flags) 76 95 { 77 mpc_uint32_t unread_bytes = d->bytes_total + d->buffer - d->bits_reader.buff 78 - ((8 - d->bits_reader.count) >> 3); 96 mpc_uint32_t unread_bytes = (mpc_uint32_t) mpc_unread_bytes_unchecked(d); 79 97 int offset = 0; 80 98 99 if ((mpc_int32_t) 100 unread_bytes < 0) return 0; // Error - we've been reading past the end of the buffer - abort 101 81 102 if (min_bytes == 0 || min_bytes > DEMUX_BUFFER_SIZE || 82 (unread_bytes < min_bytes && flags & MPC_BUFFER_FULL))103 (unread_bytes < min_bytes && (flags & MPC_BUFFER_FULL) != 0 )) 83 104 min_bytes = DEMUX_BUFFER_SIZE; 84 105 … … 86 107 mpc_uint32_t bytes2read = min_bytes - unread_bytes; 87 108 mpc_uint32_t bytes_free = DEMUX_BUFFER_SIZE - d->bytes_total; 109 mpc_uint32_t bytesread; 88 110 89 111 if (flags & MPC_BUFFER_SWAP) { … … 102 124 d->bytes_total = unread_bytes + offset; 103 125 } 104 bytes2read = d->r->read(d->r, d->buffer + d->bytes_total, bytes2read); 105 if (flags & MPC_BUFFER_SWAP){ 126 bytesread = d->r->read(d->r, d->buffer + d->bytes_total, bytes2read); 127 if (bytesread < bytes2read) { 128 memset(d->buffer + d->bytes_total + bytesread, 0, bytes2read - bytesread); // FIXME : why ? 129 } 130 if (flags & MPC_BUFFER_SWAP) { 106 131 unsigned int i, * tmp = (unsigned int *) (d->buffer + d->bytes_total); 107 132 for(i = 0 ;i < (bytes2read >> 2); i++) 108 133 tmp[i] = mpc_swap32(tmp[i]); 109 134 } 110 d->bytes_total += bytes 2read;111 return bytes2read;112 } 113 114 return (mpc_uint32_t) -1;135 d->bytes_total += bytesread; 136 unread_bytes += bytesread; 137 } 138 139 return unread_bytes; 115 140 } 116 141 … … 121 146 * @param min_bytes number of bytes to load after seeking 122 147 */ 123 static void148 static mpc_status 124 149 mpc_demux_seek(mpc_demux * d, mpc_seek_t fpos, mpc_uint32_t min_bytes) { 125 150 mpc_seek_t start_pos, end_pos; … … 141 166 bit_offset = (int) (fpos - (next_pos << 3)); 142 167 143 d->r->seek(d->r, (mpc_int32_t) next_pos);144 168 mpc_demux_clear_buff(d); 169 if (!d->r->seek(d->r, (mpc_int32_t) next_pos)) 170 return MPC_STATUS_FAIL; 145 171 } 146 172 … … 151 177 d->bits_reader.buff += bit_offset >> 3; 152 178 d->bits_reader.count = 8 - (bit_offset & 7); 179 180 return MPC_STATUS_OK; 153 181 } 154 182 … … 170 198 * @param d demuxer context 171 199 * @return size of tag, in bytes 172 * @return MPC_STATUS_F ILEon errors of any kind200 * @return MPC_STATUS_FAIL on errors of any kind 173 201 */ 174 202 static mpc_int32_t mpc_demux_skip_id3v2(mpc_demux * d) … … 193 221 footerPresent = tmp[0] & 0x10; 194 222 if ( tmp[0] & 0x0F ) 195 return MPC_STATUS_F ILE; // not (yet???) allowed223 return MPC_STATUS_FAIL; // not (yet???) allowed 196 224 197 225 tmp[0] = mpc_bits_read(&d->bits_reader, 8); // read size … … 201 229 202 230 if ( (tmp[0] | tmp[1] | tmp[2] | tmp[3]) & 0x80 ) 203 return MPC_STATUS_F ILE; // not allowed231 return MPC_STATUS_FAIL; // not allowed 204 232 205 233 // read headerSize (syncsave: 4 * $0xxxxxxx = 28 significant bits) … … 215 243 // 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. 216 244 mpc_demux_clear_buff(d); 217 if (!d->r->seek(d->r, size)) return MPC_STATUS_FILE; 245 if (!d->r->seek(d->r, size)) 246 return MPC_STATUS_FAIL; 218 247 219 248 return size; … … 236 265 d->seek_table = malloc((size_t)(seek_table_size * sizeof(mpc_seek_t))); 237 266 if (d->seek_table == 0) 238 return MPC_STATUS_F ILE;267 return MPC_STATUS_FAIL; 239 268 d->seek_table[0] = (mpc_seek_t)mpc_demux_pos(d); 240 269 d->seek_table_size = 1; … … 243 272 } 244 273 245 static voidmpc_demux_ST(mpc_demux * d)274 static mpc_status mpc_demux_ST(mpc_demux * d) 246 275 { 247 276 mpc_uint64_t tmp; … … 252 281 253 282 if (d->seek_table != 0) 254 return ;283 return MPC_STATUS_OK; 255 284 256 285 mpc_bits_get_size(&r, &tmp); … … 274 303 275 304 if (d->seek_table_size == 1) 276 return ;305 return MPC_STATUS_OK; 277 306 278 307 mpc_bits_get_size(&r, &tmp); … … 290 319 table[i >> diff_pwr] = last[i & 1]; 291 320 } 292 } 293 294 static void mpc_demux_SP(mpc_demux * d, int size, int block_size) 321 return MPC_STATUS_OK; 322 } 323 324 static mpc_status mpc_demux_SP(mpc_demux * d, int size, int block_size) 295 325 { 296 326 mpc_seek_t cur; … … 301 331 cur = mpc_demux_pos(d); 302 332 mpc_bits_get_size(&d->bits_reader, &ptr); 303 mpc_demux_seek(d, (ptr - size) * 8 + cur, 11);333 MPC_AUTO_FAIL( mpc_demux_seek(d, (ptr - size) * 8 + cur, 11) ); 304 334 st_head_size = mpc_bits_get_block(&d->bits_reader, &b); 305 335 if (memcmp(b.key, "ST", 2) == 0) { 306 336 d->chap_pos = (ptr - size + b.size + st_head_size) * 8 + cur; 307 337 d->chap_nb = -1; 308 mpc_demux_fill(d, (mpc_uint32_t) b.size, 0); 309 mpc_demux_ST(d); 310 } 311 mpc_demux_seek(d, cur, 11 + block_size); 312 } 313 314 static void mpc_demux_chap_find(mpc_demux * d) 338 if (mpc_demux_fill(d, (mpc_uint32_t) b.size, 0) < b.size) 339 return MPC_STATUS_FAIL; 340 MPC_AUTO_FAIL( mpc_demux_ST(d) ); 341 } 342 return mpc_demux_seek(d, cur, 11 + block_size); 343 } 344 345 static void mpc_demux_chap_empty(mpc_demux * d) { 346 free(d->chap); d->chap = 0; 347 d->chap_nb = 0; // -1 for undefined, 0 for no chapters 348 d->chap_pos = 0; 349 } 350 351 static mpc_status mpc_demux_chap_find_inner(mpc_demux * d) 315 352 { 316 353 mpc_block b; … … 320 357 321 358 if (d->si.stream_version < 8) 322 return ;359 return MPC_STATUS_OK; 323 360 324 361 if (d->chap_pos == 0) { 325 362 mpc_uint64_t cur_pos = (d->si.header_position + 4) * 8; 326 mpc_demux_seek(d, cur_pos, 11); // seek to the beginning of the stream363 MPC_AUTO_FAIL( mpc_demux_seek(d, cur_pos, 11) ); // seek to the beginning of the stream 327 364 size = mpc_bits_get_block(&d->bits_reader, &b); 328 365 while (memcmp(b.key, "SE", 2) != 0) { 329 if (mpc_check_key(b.key) != MPC_STATUS_OK) 330 return; 366 mpc_uint64_t new_pos = cur_pos + (size + b.size) * 8; 367 MPC_AUTO_FAIL(mpc_check_key(b.key)); 368 331 369 if (memcmp(b.key, "CT", 2) == 0) { 332 370 if (d->chap_pos == 0) d->chap_pos = cur_pos; 333 } else 371 } else { 334 372 d->chap_pos = 0; 335 cur_pos += (size + b.size) * 8; 336 mpc_demux_seek(d, cur_pos, 11); 373 } 374 if (new_pos <= cur_pos) 375 return MPC_STATUS_FAIL; 376 cur_pos = new_pos; 377 378 MPC_AUTO_FAIL( mpc_demux_seek(d, cur_pos, 11) ); 337 379 size = mpc_bits_get_block(&d->bits_reader, &b); 338 380 } … … 350 392 chap_size += size; 351 393 tag_size += b.size - size; 352 mpc_demux_seek(d, d->chap_pos + (chap_size + tag_size) * 8, 20);394 MPC_AUTO_FAIL( mpc_demux_seek(d, d->chap_pos + (chap_size + tag_size) * 8, 20) ); 353 395 size = mpc_bits_get_block(&d->bits_reader, &b); 354 396 } … … 357 399 char * ptag; 358 400 d->chap = malloc(sizeof(mpc_chap_info) * d->chap_nb + tag_size); 359 if (d->chap != 0) { 360 ptag = (char*)(d->chap + d->chap_nb); 361 362 mpc_demux_seek(d, d->chap_pos, 11); 401 if (d->chap == 0) 402 return MPC_STATUS_FAIL; 403 404 ptag = (char*)(d->chap + d->chap_nb); 405 406 MPC_AUTO_FAIL( mpc_demux_seek(d, d->chap_pos, 11) ); 407 size = mpc_bits_get_block(&d->bits_reader, &b); 408 while (memcmp(b.key, "CT", 2) == 0) { 409 mpc_uint_t tmp_size; 410 char * tmp_ptag = ptag; 411 if (mpc_demux_fill(d, 11 + (mpc_uint32_t) b.size, 0) < b.size) 412 return MPC_STATUS_FAIL; 413 size = mpc_bits_get_size(&d->bits_reader, &d->chap[i].sample) + 4; 414 d->chap[i].gain = (mpc_uint16_t) mpc_bits_read(&d->bits_reader, 16); 415 d->chap[i].peak = (mpc_uint16_t) mpc_bits_read(&d->bits_reader, 16); 416 417 tmp_size = b.size - size; 418 do { 419 mpc_uint_t rd_size = tmp_size; 420 mpc_uint8_t * tmp_buff = d->bits_reader.buff + ((8 - d->bits_reader.count) >> 3); 421 mpc_uint32_t avail_bytes = d->bytes_total + d->buffer - tmp_buff; 422 rd_size = mini(rd_size, avail_bytes); 423 memcpy(tmp_ptag, tmp_buff, rd_size); 424 tmp_size -= rd_size; 425 tmp_ptag += rd_size; 426 d->bits_reader.buff += rd_size; 427 mpc_demux_fill(d, tmp_size, 0); 428 } while (tmp_size > 0); 429 430 d->chap[i].tag_size = b.size - size; 431 d->chap[i].tag = ptag; 432 ptag += b.size - size; 433 i++; 363 434 size = mpc_bits_get_block(&d->bits_reader, &b); 364 while (memcmp(b.key, "CT", 2) == 0) {365 int tmp_size;366 char * tmp_ptag = ptag;367 mpc_demux_fill(d, 11 + (mpc_uint32_t) b.size, 0);368 size = mpc_bits_get_size(&d->bits_reader, &d->chap[i].sample) + 4;369 d->chap[i].gain = (mpc_uint16_t) mpc_bits_read(&d->bits_reader, 16);370 d->chap[i].peak = (mpc_uint16_t) mpc_bits_read(&d->bits_reader, 16);371 372 tmp_size = b.size - size;373 do {374 int rd_size = tmp_size;375 mpc_uint8_t * tmp_buff = d->bits_reader.buff + ((8 - d->bits_reader.count) >> 3);376 mpc_uint32_t avail_bytes = d->bytes_total + d->buffer - tmp_buff;377 rd_size = mini(rd_size, avail_bytes);378 memcpy(tmp_ptag, tmp_buff, rd_size);379 tmp_size -= rd_size;380 tmp_ptag += rd_size;381 d->bits_reader.buff += rd_size;382 mpc_demux_fill(d, tmp_size, 0);383 } while (tmp_size > 0);384 385 d->chap[i].tag_size = b.size - size;386 d->chap[i].tag = ptag;387 ptag += b.size - size;388 i++;389 size = mpc_bits_get_block(&d->bits_reader, &b);390 }391 } else {392 d->chap_nb = 0; // malloc failed, chapters will not be available393 435 } 394 436 } 395 437 396 438 d->bits_reader.buff -= size; 439 return MPC_STATUS_OK; 440 } 441 442 static mpc_status mpc_demux_chap_find(mpc_demux * d) { 443 mpc_status s = mpc_demux_chap_find_inner(d); 444 if (MPC_IS_FAILURE(s)) 445 mpc_demux_chap_empty(d); 446 return s; 397 447 } 398 448 … … 434 484 // get header position 435 485 d->si.header_position = mpc_demux_skip_id3v2(d); 436 if(d->si.header_position < 0) return MPC_STATUS_FILE; 486 if(d->si.header_position < 0) 487 return MPC_STATUS_FAIL; 437 488 438 489 d->si.tag_offset = d->si.total_file_length = d->r->get_size(d->r); … … 447 498 d->si.stream_version = magic[3] & 15; 448 499 d->si.pns = magic[3] >> 4; 449 if (d->si.stream_version == 7) { 450 mpc_status ret; 451 mpc_demux_fill(d, 6 * 4, MPC_BUFFER_SWAP); // header block size + endian convertion 452 ret = streaminfo_read_header_sv7(&d->si, &d->bits_reader); 453 if (ret != MPC_STATUS_OK) return ret; 454 } else { 455 return MPC_STATUS_INVALIDSV; 456 } 500 if (d->si.stream_version != 7) 501 return MPC_STATUS_FAIL; 502 if (mpc_demux_fill(d, 6 * 4, MPC_BUFFER_SWAP) < 6 * 4) // header block size + endian convertion 503 return MPC_STATUS_FAIL; 504 MPC_AUTO_FAIL( streaminfo_read_header_sv7(&d->si, &d->bits_reader) ); 457 505 } else if (memcmp(magic, "MPCK", 4) == 0) { 458 506 mpc_block b; … … 462 510 while( memcmp(b.key, "AP", 2) != 0 ){ // scan all blocks until audio 463 511 if (mpc_check_key(b.key) != MPC_STATUS_OK) 464 return MPC_STATUS_ INVALIDSV;512 return MPC_STATUS_FAIL; 465 513 if (b.size > (mpc_uint64_t) DEMUX_BUFFER_SIZE - 11) 466 return MPC_STATUS_INVALIDSV; 467 mpc_demux_fill(d, 11 + (mpc_uint32_t) b.size, 0); 468 if (memcmp(b.key, "SH", 2) == 0){ 469 int ret = streaminfo_read_header_sv8(&d->si, &d->bits_reader, (mpc_uint32_t) b.size); 470 if (ret != MPC_STATUS_OK) return ret; 471 } else if (memcmp(b.key, "RG", 2) == 0) 514 return MPC_STATUS_FAIL; 515 516 if (mpc_demux_fill(d, 11 + (mpc_uint32_t) b.size, 0) <= b.size) 517 return MPC_STATUS_FAIL; 518 519 if (memcmp(b.key, "SH", 2) == 0) { 520 MPC_AUTO_FAIL( streaminfo_read_header_sv8(&d->si, &d->bits_reader, (mpc_uint32_t) b.size) ); 521 } else if (memcmp(b.key, "RG", 2) == 0) { 472 522 streaminfo_gain(&d->si, &d->bits_reader); 473 else if (memcmp(b.key, "EI", 2) == 0)523 } else if (memcmp(b.key, "EI", 2) == 0) { 474 524 streaminfo_encoder_info(&d->si, &d->bits_reader); 475 else if (memcmp(b.key, "SO", 2) == 0) 476 mpc_demux_SP(d, size, (mpc_uint32_t) b.size); 477 else if (memcmp(b.key, "ST", 2) == 0) 478 mpc_demux_ST(d); 525 } else if (memcmp(b.key, "SO", 2) == 0) { 526 MPC_AUTO_FAIL( mpc_demux_SP(d, size, (mpc_uint32_t) b.size) ); 527 } else if (memcmp(b.key, "ST", 2) == 0) { 528 MPC_AUTO_FAIL( mpc_demux_ST(d) ); 529 } 479 530 d->bits_reader.buff += b.size; 480 531 size = mpc_bits_get_block(&d->bits_reader, &b); … … 482 533 d->bits_reader.buff -= size; 483 534 if (d->si.stream_version == 0) // si not initialized !!! 484 return MPC_STATUS_INVALIDSV; 485 } else 486 return MPC_STATUS_INVALIDSV; 535 return MPC_STATUS_FAIL; 536 } else { 537 return MPC_STATUS_FAIL; 538 } 487 539 488 540 return MPC_STATUS_OK; … … 525 577 } 526 578 527 mpc_status mpc_demux_decode(mpc_demux * d, mpc_frame_info * i)579 static mpc_status mpc_demux_decode_inner(mpc_demux * d, mpc_frame_info * i) 528 580 { 529 581 mpc_bits_reader r; … … 541 593 mpc_bits_get_block(&d->bits_reader, &b); 542 594 while( memcmp(b.key, "AP", 2) != 0 ) { // scan all blocks until audio 543 if (mpc_check_key(b.key) != MPC_STATUS_OK)544 goto error; 595 MPC_AUTO_FAIL( mpc_check_key(b.key) ); 596 545 597 if (memcmp(b.key, "SE", 2) == 0) { // end block 546 598 i->bits = -1; 547 599 return MPC_STATUS_OK; 548 600 } 549 if (mpc_demux_fill(d, 11 + (mpc_uint32_t) b.size, MPC_BUFFER_FULL) == 0) 550 goto error; 601 602 if (mpc_demux_fill(d, 11 + (mpc_uint32_t) b.size, MPC_BUFFER_FULL) < b.size) 603 return MPC_STATUS_FAIL; 604 551 605 d->bits_reader.buff += b.size; 552 606 mpc_bits_get_block(&d->bits_reader, &b); … … 563 617 d->block_frames--; 564 618 if (d->block_bits < 0 || (d->block_frames == 0 && d->block_bits > 7)) 565 goto error;619 return MPC_STATUS_FAIL; 566 620 } else { 567 621 if (d->d->decoded_samples == (d->seek_table_size << d->seek_pwr) * MPC_FRAME_LENGTH) { … … 575 629 mpc_decoder_decode_frame(d->d, &d->bits_reader, i); 576 630 if (i->bits != -1 && d->block_bits != ((d->bits_reader.buff - r.buff) << 3) + r.count - d->bits_reader.count) 577 goto error;631 return MPC_STATUS_FAIL; 578 632 } 579 633 if (i->bits != -1 && d->buffer + d->bytes_total < d->bits_reader.buff + ((8 - d->bits_reader.count) >> 3)) 580 goto error;634 return MPC_STATUS_FAIL; 581 635 582 636 return MPC_STATUS_OK; 583 error: 637 } 638 639 mpc_status mpc_demux_decode(mpc_demux * d, mpc_frame_info * i) { 640 mpc_status s = mpc_demux_decode_inner(d, i); 641 if (MPC_IS_FAILURE(s)) 584 642 i->bits = -1; // we pretend it's end of file 585 return MPC_STATUS_INVALIDSV;643 return s; 586 644 } 587 645 … … 658 716 mpc_bool_t use_title, mpc_bool_t clip_prevention) 659 717 { 660 float peak = use_title ? d->si.peak_title : d->si.peak_album;661 float gain = use_title ? d->si.gain_title : d->si.gain_album;718 float peak = (float) ( use_title ? d->si.peak_title : d->si.peak_album ); 719 float gain = (float) ( use_title ? d->si.gain_title : d->si.gain_album ); 662 720 663 721 if(!use_gain && !clip_prevention) … … 667 725 peak = 1.; 668 726 else 669 peak = ( 1 << 15) / pow(10, peak / (20 * 256));727 peak = (float) ( (1 << 15) / pow(10, peak / (20 * 256)) ); 670 728 671 729 if(!gain) 672 730 gain = 1.; 673 731 else 674 gain = pow(10, (level - gain / 256) / 20);732 gain = (float) pow(10, (level - gain / 256) / 20); 675 733 676 734 if(clip_prevention && (peak < gain || !use_gain))
Note: See TracChangeset
for help on using the changeset viewer.