comparison wavpack.c @ 9589:3f7496cd7cab libavcodec

Decode extended bitstream for high-precision WavPack files. Patch by Laurent Aimar [(wolf, son of Loki) <at> (videolan) <dot> (org)]
author kostya
date Sat, 02 May 2009 05:17:20 +0000
parents 9a3fddd31092
children 2a52dd5a684a
comparison
equal deleted inserted replaced
9588:b0e7d5ced43b 9589:3f7496cd7cab
55 WP_ID_SHAPING, 55 WP_ID_SHAPING,
56 WP_ID_FLOATINFO, 56 WP_ID_FLOATINFO,
57 WP_ID_INT32INFO, 57 WP_ID_INT32INFO,
58 WP_ID_DATA, 58 WP_ID_DATA,
59 WP_ID_CORR, 59 WP_ID_CORR,
60 WP_ID_FLT, 60 WP_ID_EXTRABITS,
61 WP_ID_CHANINFO 61 WP_ID_CHANINFO
62 }; 62 };
63 63
64 #define MAX_TERMS 16 64 #define MAX_TERMS 16
65 65
83 int frame_flags; 83 int frame_flags;
84 int stereo, stereo_in; 84 int stereo, stereo_in;
85 int joint; 85 int joint;
86 uint32_t CRC; 86 uint32_t CRC;
87 GetBitContext gb; 87 GetBitContext gb;
88 int got_extra_bits;
89 uint32_t crc_extra_bits;
90 GetBitContext gb_extra_bits;
88 int data_size; // in bits 91 int data_size; // in bits
89 int samples; 92 int samples;
90 int terms; 93 int terms;
91 Decorr decorr[MAX_TERMS]; 94 Decorr decorr[MAX_TERMS];
92 int zero, one, zeroes; 95 int zero, one, zeroes;
96 int extra_bits;
93 int and, or, shift; 97 int and, or, shift;
94 int post_shift; 98 int post_shift;
95 int hybrid, hybrid_bitrate; 99 int hybrid, hybrid_bitrate;
96 WvChannel ch[2]; 100 WvChannel ch[2];
97 } WavpackContext; 101 } WavpackContext;
342 int i, j, count = 0; 346 int i, j, count = 0;
343 int last, t; 347 int last, t;
344 int A, B, L, L2, R, R2, bit; 348 int A, B, L, L2, R, R2, bit;
345 int pos = 0; 349 int pos = 0;
346 uint32_t crc = 0xFFFFFFFF; 350 uint32_t crc = 0xFFFFFFFF;
351 uint32_t crc_extra_bits = 0xFFFFFFFF;
347 int16_t *dst16 = dst; 352 int16_t *dst16 = dst;
348 int32_t *dst32 = dst; 353 int32_t *dst32 = dst;
349 354
350 s->one = s->zero = s->zeroes = 0; 355 s->one = s->zero = s->zeroes = 0;
351 do{ 356 do{
422 } 427 }
423 pos = (pos + 1) & 7; 428 pos = (pos + 1) & 7;
424 if(s->joint) 429 if(s->joint)
425 L += (R -= (L >> 1)); 430 L += (R -= (L >> 1));
426 crc = (crc * 3 + L) * 3 + R; 431 crc = (crc * 3 + L) * 3 + R;
432 if(s->extra_bits){
433 L <<= s->extra_bits;
434 R <<= s->extra_bits;
435
436 if(s->got_extra_bits){
437 L |= get_bits(&s->gb_extra_bits, s->extra_bits);
438 crc_extra_bits = crc_extra_bits * 9 + (L&0xffff) * 3 + ((unsigned)L>>16);
439
440 R |= get_bits(&s->gb_extra_bits, s->extra_bits);
441 crc_extra_bits = crc_extra_bits * 9 + (R&0xffff) * 3 + ((unsigned)R>>16);
442 }
443 }
427 bit = (L & s->and) | s->or; 444 bit = (L & s->and) | s->or;
428 if(hires) 445 if(hires)
429 *dst32++ = (((L + bit) << s->shift) - bit) << s->post_shift; 446 *dst32++ = (((L + bit) << s->shift) - bit) << s->post_shift;
430 else 447 else
431 *dst16++ = (((L + bit) << s->shift) - bit) << s->post_shift; 448 *dst16++ = (((L + bit) << s->shift) - bit) << s->post_shift;
439 456
440 if(crc != s->CRC){ 457 if(crc != s->CRC){
441 av_log(s->avctx, AV_LOG_ERROR, "CRC error\n"); 458 av_log(s->avctx, AV_LOG_ERROR, "CRC error\n");
442 return -1; 459 return -1;
443 } 460 }
461 if(s->got_extra_bits && crc_extra_bits != s->crc_extra_bits){
462 av_log(s->avctx, AV_LOG_ERROR, "Extra bits CRC error\n");
463 return -1;
464 }
444 return count * 2; 465 return count * 2;
445 } 466 }
446 467
447 static inline int wv_unpack_mono(WavpackContext *s, GetBitContext *gb, void *dst, const int hires) 468 static inline int wv_unpack_mono(WavpackContext *s, GetBitContext *gb, void *dst, const int hires)
448 { 469 {
449 int i, j, count = 0; 470 int i, j, count = 0;
450 int last, t; 471 int last, t;
451 int A, S, T, bit; 472 int A, S, T, bit;
452 int pos = 0; 473 int pos = 0;
453 uint32_t crc = 0xFFFFFFFF; 474 uint32_t crc = 0xFFFFFFFF;
475 uint32_t crc_extra_bits = 0xFFFFFFFF;
454 int16_t *dst16 = dst; 476 int16_t *dst16 = dst;
455 int32_t *dst32 = dst; 477 int32_t *dst32 = dst;
456 478
457 s->one = s->zero = s->zeroes = 0; 479 s->one = s->zero = s->zeroes = 0;
458 do{ 480 do{
479 if(A && T) s->decorr[i].weightA -= ((((T ^ A) >> 30) & 2) - 1) * s->decorr[i].delta; 501 if(A && T) s->decorr[i].weightA -= ((((T ^ A) >> 30) & 2) - 1) * s->decorr[i].delta;
480 s->decorr[i].samplesA[j] = T = S; 502 s->decorr[i].samplesA[j] = T = S;
481 } 503 }
482 pos = (pos + 1) & 7; 504 pos = (pos + 1) & 7;
483 crc = crc * 3 + S; 505 crc = crc * 3 + S;
506 if(s->extra_bits){
507 S <<= s->extra_bits;
508
509 if(s->got_extra_bits){
510 S |= get_bits(&s->gb_extra_bits, s->extra_bits);
511 crc_extra_bits = crc_extra_bits * 9 + (S&0xffff) * 3 + ((unsigned)S>>16);
512 }
513 }
514
484 bit = (S & s->and) | s->or; 515 bit = (S & s->and) | s->or;
485 if(hires) 516 if(hires)
486 *dst32++ = (((S + bit) << s->shift) - bit) << s->post_shift; 517 *dst32++ = (((S + bit) << s->shift) - bit) << s->post_shift;
487 else 518 else
488 *dst16++ = (((S + bit) << s->shift) - bit) << s->post_shift; 519 *dst16++ = (((S + bit) << s->shift) - bit) << s->post_shift;
489 count++; 520 count++;
490 }while(!last && count < s->samples); 521 }while(!last && count < s->samples);
491 522
492 if(crc != s->CRC){ 523 if(crc != s->CRC){
493 av_log(s->avctx, AV_LOG_ERROR, "CRC error\n"); 524 av_log(s->avctx, AV_LOG_ERROR, "CRC error\n");
525 return -1;
526 }
527 if(s->got_extra_bits && crc_extra_bits != s->crc_extra_bits){
528 av_log(s->avctx, AV_LOG_ERROR, "Extra bits CRC error\n");
494 return -1; 529 return -1;
495 } 530 }
496 return count; 531 return count;
497 } 532 }
498 533
531 return 0; 566 return 0;
532 } 567 }
533 568
534 memset(s->decorr, 0, MAX_TERMS * sizeof(Decorr)); 569 memset(s->decorr, 0, MAX_TERMS * sizeof(Decorr));
535 memset(s->ch, 0, sizeof(s->ch)); 570 memset(s->ch, 0, sizeof(s->ch));
571 s->extra_bits = 0;
536 s->and = s->or = s->shift = 0; 572 s->and = s->or = s->shift = 0;
573 s->got_extra_bits = 0;
537 574
538 s->samples = AV_RL32(buf); buf += 4; 575 s->samples = AV_RL32(buf); buf += 4;
539 if(!s->samples){ 576 if(!s->samples){
540 *data_size = 0; 577 *data_size = 0;
541 return buf_size; 578 return buf_size;
699 av_log(avctx, AV_LOG_ERROR, "Invalid INT32INFO, size = %i, sent_bits = %i\n", size, *buf); 736 av_log(avctx, AV_LOG_ERROR, "Invalid INT32INFO, size = %i, sent_bits = %i\n", size, *buf);
700 buf += ssize; 737 buf += ssize;
701 continue; 738 continue;
702 } 739 }
703 if(buf[0]) 740 if(buf[0])
704 s->post_shift = buf[0]; 741 s->extra_bits = buf[0];
705 else if(buf[1]) 742 else if(buf[1])
706 s->shift = buf[1]; 743 s->shift = buf[1];
707 else if(buf[2]){ 744 else if(buf[2]){
708 s->and = s->or = 1; 745 s->and = s->or = 1;
709 s->shift = buf[2]; 746 s->shift = buf[2];
716 case WP_ID_DATA: 753 case WP_ID_DATA:
717 init_get_bits(&s->gb, buf, size * 8); 754 init_get_bits(&s->gb, buf, size * 8);
718 s->data_size = size * 8; 755 s->data_size = size * 8;
719 buf += size; 756 buf += size;
720 got_bs = 1; 757 got_bs = 1;
758 break;
759 case WP_ID_EXTRABITS:
760 if(size <= 4){
761 av_log(avctx, AV_LOG_ERROR, "Invalid EXTRABITS, size = %i\n", size);
762 buf += size;
763 continue;
764 }
765 init_get_bits(&s->gb_extra_bits, buf, size * 8);
766 s->crc_extra_bits = get_bits_long(&s->gb_extra_bits, 32);
767 buf += size;
768 s->got_extra_bits = 1;
721 break; 769 break;
722 default: 770 default:
723 buf += size; 771 buf += size;
724 } 772 }
725 if(id & WP_IDF_ODD) buf++; 773 if(id & WP_IDF_ODD) buf++;