comparison wavpack.c @ 9543:a00e7601b584 libavcodec

Add functions for decoding >16 bits WavPack files. Based on patches by Laurent Aimar (fenrir >whirlpool< videolan >dit< org)
author kostya
date Thu, 23 Apr 2009 17:27:04 +0000
parents c110790496ff
children e5afd314bd14
comparison
equal deleted inserted replaced
9542:c110790496ff 9543:a00e7601b584
461 return -1; 461 return -1;
462 } 462 }
463 return count; 463 return count;
464 } 464 }
465 465
466 static int wv_unpack_stereo_hires(WavpackContext *s, GetBitContext *gb, int32_t *dst)
467 {
468 int i, j, count = 0;
469 int last, t;
470 int A, B, L, L2, R, R2, bit;
471 int pos = 0;
472 uint32_t crc = 0xFFFFFFFF;
473
474 s->one = s->zero = s->zeroes = 0;
475 do{
476 L = wv_get_value(s, gb, 0, &last);
477 if(last) break;
478 R = wv_get_value(s, gb, 1, &last);
479 if(last) break;
480 for(i = 0; i < s->terms; i++){
481 t = s->decorr[i].value;
482 j = 0;
483 if(t > 0){
484 if(t > 8){
485 if(t & 1){
486 A = 2 * s->decorr[i].samplesA[0] - s->decorr[i].samplesA[1];
487 B = 2 * s->decorr[i].samplesB[0] - s->decorr[i].samplesB[1];
488 }else{
489 A = (3 * s->decorr[i].samplesA[0] - s->decorr[i].samplesA[1]) >> 1;
490 B = (3 * s->decorr[i].samplesB[0] - s->decorr[i].samplesB[1]) >> 1;
491 }
492 s->decorr[i].samplesA[1] = s->decorr[i].samplesA[0];
493 s->decorr[i].samplesB[1] = s->decorr[i].samplesB[0];
494 j = 0;
495 }else{
496 A = s->decorr[i].samplesA[pos];
497 B = s->decorr[i].samplesB[pos];
498 j = (pos + t) & 7;
499 }
500 L2 = L + ((s->decorr[i].weightA * (int64_t)A + 512) >> 10);
501 R2 = R + ((s->decorr[i].weightB * (int64_t)B + 512) >> 10);
502 if(A && L) s->decorr[i].weightA -= ((((L ^ A) >> 30) & 2) - 1) * s->decorr[i].delta;
503 if(B && R) s->decorr[i].weightB -= ((((R ^ B) >> 30) & 2) - 1) * s->decorr[i].delta;
504 s->decorr[i].samplesA[j] = L = L2;
505 s->decorr[i].samplesB[j] = R = R2;
506 }else if(t == -1){
507 L2 = L + ((s->decorr[i].weightA * (int64_t)s->decorr[i].samplesA[0] + 512) >> 10);
508 UPDATE_WEIGHT_CLIP(s->decorr[i].weightA, s->decorr[i].delta, s->decorr[i].samplesA[0], L);
509 L = L2;
510 R2 = R + ((s->decorr[i].weightB * (int64_t)L2 + 512) >> 10);
511 UPDATE_WEIGHT_CLIP(s->decorr[i].weightB, s->decorr[i].delta, L2, R);
512 R = R2;
513 s->decorr[i].samplesA[0] = R;
514 }else{
515 R2 = R + ((s->decorr[i].weightB * (int64_t)s->decorr[i].samplesB[0] + 512) >> 10);
516 UPDATE_WEIGHT_CLIP(s->decorr[i].weightB, s->decorr[i].delta, s->decorr[i].samplesB[0], R);
517 R = R2;
518
519 if(t == -3){
520 R2 = s->decorr[i].samplesA[0];
521 s->decorr[i].samplesA[0] = R;
522 }
523
524 L2 = L + ((s->decorr[i].weightA * (int64_t)R2 + 512) >> 10);
525 UPDATE_WEIGHT_CLIP(s->decorr[i].weightA, s->decorr[i].delta, R2, L);
526 L = L2;
527 s->decorr[i].samplesB[0] = L;
528 }
529 }
530 pos = (pos + 1) & 7;
531 if(s->joint)
532 L += (R -= (L >> 1));
533 crc = (crc * 3 + L) * 3 + R;
534 bit = (L & s->and) | s->or;
535 *dst++ = (((L + bit) << s->shift) - bit) << s->post_shift;
536 bit = (R & s->and) | s->or;
537 *dst++ = (((R + bit) << s->shift) - bit) << s->post_shift;
538 count++;
539 }while(!last && count < s->samples);
540
541 if(crc != s->CRC){
542 av_log(s->avctx, AV_LOG_ERROR, "CRC error\n");
543 return -1;
544 }
545 return count * 2;
546 }
547
548 static int wv_unpack_mono_hires(WavpackContext *s, GetBitContext *gb, int32_t *dst)
549 {
550 int i, j, count = 0;
551 int last, t;
552 int A, S, T, bit;
553 int pos = 0;
554 uint32_t crc = 0xFFFFFFFF;
555
556 s->one = s->zero = s->zeroes = 0;
557 do{
558 T = wv_get_value(s, gb, 0, &last);
559 S = 0;
560 if(last) break;
561 for(i = 0; i < s->terms; i++){
562 t = s->decorr[i].value;
563 if(t > 8){
564 if(t & 1)
565 A = 2 * s->decorr[i].samplesA[0] - s->decorr[i].samplesA[1];
566 else
567 A = (3 * s->decorr[i].samplesA[0] - s->decorr[i].samplesA[1]) >> 1;
568 s->decorr[i].samplesA[1] = s->decorr[i].samplesA[0];
569 j = 0;
570 }else{
571 A = s->decorr[i].samplesA[pos];
572 j = (pos + t) & 7;
573 }
574 S = T + ((s->decorr[i].weightA * (int64_t)A + 512) >> 10);
575 if(A && T) s->decorr[i].weightA -= ((((T ^ A) >> 30) & 2) - 1) * s->decorr[i].delta;
576 s->decorr[i].samplesA[j] = T = S;
577 }
578 pos = (pos + 1) & 7;
579 crc = crc * 3 + S;
580 bit = (S & s->and) | s->or;
581 *dst++ = (((S + bit) << s->shift) - bit) << s->post_shift;
582 count++;
583 }while(!last && count < s->samples);
584
585 if(crc != s->CRC){
586 av_log(s->avctx, AV_LOG_ERROR, "CRC error\n");
587 return -1;
588 }
589 return count;
590 }
591
466 static av_cold int wavpack_decode_init(AVCodecContext *avctx) 592 static av_cold int wavpack_decode_init(AVCodecContext *avctx)
467 { 593 {
468 WavpackContext *s = avctx->priv_data; 594 WavpackContext *s = avctx->priv_data;
469 595
470 s->avctx = avctx; 596 s->avctx = avctx;
471 s->stereo = (avctx->channels == 2); 597 s->stereo = (avctx->channels == 2);
472 avctx->sample_fmt = SAMPLE_FMT_S16; 598 if(avctx->bits_per_coded_sample <= 16)
599 avctx->sample_fmt = SAMPLE_FMT_S16;
600 else
601 avctx->sample_fmt = SAMPLE_FMT_S32;
473 avctx->channel_layout = (avctx->channels==2) ? CH_LAYOUT_STEREO : CH_LAYOUT_MONO; 602 avctx->channel_layout = (avctx->channels==2) ? CH_LAYOUT_STEREO : CH_LAYOUT_MONO;
474 603
475 return 0; 604 return 0;
476 } 605 }
477 606
481 { 610 {
482 const uint8_t *buf = avpkt->data; 611 const uint8_t *buf = avpkt->data;
483 int buf_size = avpkt->size; 612 int buf_size = avpkt->size;
484 WavpackContext *s = avctx->priv_data; 613 WavpackContext *s = avctx->priv_data;
485 int16_t *samples = data; 614 int16_t *samples = data;
615 int32_t *samples32 = data;
486 int samplecount; 616 int samplecount;
487 int got_terms = 0, got_weights = 0, got_samples = 0, got_entropy = 0, got_bs = 0; 617 int got_terms = 0, got_weights = 0, got_samples = 0, got_entropy = 0, got_bs = 0;
488 int got_hybrid = 0; 618 int got_hybrid = 0;
489 const uint8_t* buf_end = buf + buf_size; 619 const uint8_t* buf_end = buf + buf_size;
490 int i, j, id, size, ssize, weights, t; 620 int i, j, id, size, ssize, weights, t;
621 int bpp = avctx->bits_per_coded_sample <= 16 ? 2 : 4;
491 622
492 if (buf_size == 0){ 623 if (buf_size == 0){
493 *data_size = 0; 624 *data_size = 0;
494 return 0; 625 return 0;
495 } 626 }
502 if(!s->samples){ 633 if(!s->samples){
503 *data_size = 0; 634 *data_size = 0;
504 return buf_size; 635 return buf_size;
505 } 636 }
506 /* should not happen but who knows */ 637 /* should not happen but who knows */
507 if(s->samples * 2 * avctx->channels > *data_size){ 638 if(s->samples * bpp * avctx->channels > *data_size){
508 av_log(avctx, AV_LOG_ERROR, "Packet size is too big to be handled in lavc!\n"); 639 av_log(avctx, AV_LOG_ERROR, "Packet size is too big to be handled in lavc!\n");
509 return -1; 640 return -1;
510 } 641 }
511 s->frame_flags = AV_RL32(buf); buf += 4; 642 s->frame_flags = AV_RL32(buf); buf += 4;
512 s->stereo_in = (s->frame_flags & WV_FALSE_STEREO) ? 0 : s->stereo; 643 s->stereo_in = (s->frame_flags & WV_FALSE_STEREO) ? 0 : s->stereo;
513 s->joint = s->frame_flags & WV_JOINT_STEREO; 644 s->joint = s->frame_flags & WV_JOINT_STEREO;
514 s->hybrid = s->frame_flags & WV_HYBRID_MODE; 645 s->hybrid = s->frame_flags & WV_HYBRID_MODE;
515 s->hybrid_bitrate = s->frame_flags & WV_HYBRID_BITRATE; 646 s->hybrid_bitrate = s->frame_flags & WV_HYBRID_BITRATE;
516 s->post_shift = (s->frame_flags >> 13) & 0x1f; 647 s->post_shift = 8 * (bpp-1-(s->frame_flags&0x03)) + ((s->frame_flags >> 13) & 0x1f);
517 s->CRC = AV_RL32(buf); buf += 4; 648 s->CRC = AV_RL32(buf); buf += 4;
518 // parse metadata blocks 649 // parse metadata blocks
519 while(buf < buf_end){ 650 while(buf < buf_end){
520 id = *buf++; 651 id = *buf++;
521 size = *buf++; 652 size = *buf++;
699 if(!got_bs){ 830 if(!got_bs){
700 av_log(avctx, AV_LOG_ERROR, "Packed samples not found\n"); 831 av_log(avctx, AV_LOG_ERROR, "Packed samples not found\n");
701 return -1; 832 return -1;
702 } 833 }
703 834
704 if(s->stereo_in) 835 if(s->stereo_in){
705 samplecount = wv_unpack_stereo(s, &s->gb, samples); 836 if(bpp == 2)
706 else{ 837 samplecount = wv_unpack_stereo(s, &s->gb, samples);
707 samplecount = wv_unpack_mono(s, &s->gb, samples); 838 else
708 if(s->stereo){ 839 samplecount = wv_unpack_stereo_hires(s, &s->gb, samples32);
840 }else{
841 if(bpp == 2)
842 samplecount = wv_unpack_mono(s, &s->gb, samples);
843 else
844 samplecount = wv_unpack_mono_hires(s, &s->gb, samples32);
845 if(s->stereo && bpp == 2){
709 int16_t *dst = samples + samplecount * 2; 846 int16_t *dst = samples + samplecount * 2;
710 int16_t *src = samples + samplecount; 847 int16_t *src = samples + samplecount;
711 int cnt = samplecount; 848 int cnt = samplecount;
712 while(cnt--){ 849 while(cnt--){
713 *--dst = *--src; 850 *--dst = *--src;
714 *--dst = *src; 851 *--dst = *src;
715 } 852 }
716 samplecount *= 2; 853 samplecount *= 2;
717 } 854 }else if(s->stereo){ //32-bit output
718 } 855 int32_t *dst = samples32 + samplecount * 2;
719 *data_size = samplecount * 2; 856 int32_t *src = samples32 + samplecount;
857 int cnt = samplecount;
858 while(cnt--){
859 *--dst = *--src;
860 *--dst = *src;
861 }
862 samplecount *= 2;
863 }
864 }
865 *data_size = samplecount * bpp;
720 866
721 return buf_size; 867 return buf_size;
722 } 868 }
723 869
724 AVCodec wavpack_decoder = { 870 AVCodec wavpack_decoder = {