Revision 0638c2ae libavcodec/wavpack.c

View differences:

libavcodec/wavpack.c
463 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 592
static av_cold int wavpack_decode_init(AVCodecContext *avctx)
467 593
{
468 594
    WavpackContext *s = avctx->priv_data;
469 595

  
470 596
    s->avctx = avctx;
471 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 602
    avctx->channel_layout = (avctx->channels==2) ? CH_LAYOUT_STEREO : CH_LAYOUT_MONO;
474 603

  
475 604
    return 0;
......
483 612
    int buf_size = avpkt->size;
484 613
    WavpackContext *s = avctx->priv_data;
485 614
    int16_t *samples = data;
615
    int32_t *samples32 = data;
486 616
    int samplecount;
487 617
    int got_terms = 0, got_weights = 0, got_samples = 0, got_entropy = 0, got_bs = 0;
488 618
    int got_hybrid = 0;
489 619
    const uint8_t* buf_end = buf + buf_size;
490 620
    int i, j, id, size, ssize, weights, t;
621
    int bpp = avctx->bits_per_coded_sample <= 16 ? 2 : 4;
491 622

  
492 623
    if (buf_size == 0){
493 624
        *data_size = 0;
......
504 635
        return buf_size;
505 636
    }
506 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 639
        av_log(avctx, AV_LOG_ERROR, "Packet size is too big to be handled in lavc!\n");
509 640
        return -1;
510 641
    }
......
513 644
    s->joint = s->frame_flags & WV_JOINT_STEREO;
514 645
    s->hybrid = s->frame_flags & WV_HYBRID_MODE;
515 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 648
    s->CRC = AV_RL32(buf); buf += 4;
518 649
    // parse metadata blocks
519 650
    while(buf < buf_end){
......
701 832
        return -1;
702 833
    }
703 834

  
704
    if(s->stereo_in)
705
        samplecount = wv_unpack_stereo(s, &s->gb, samples);
706
    else{
707
        samplecount = wv_unpack_mono(s, &s->gb, samples);
708
        if(s->stereo){
835
    if(s->stereo_in){
836
        if(bpp == 2)
837
            samplecount = wv_unpack_stereo(s, &s->gb, samples);
838
        else
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 846
            int16_t *dst = samples + samplecount * 2;
710 847
            int16_t *src = samples + samplecount;
711 848
            int cnt = samplecount;
......
714 851
                *--dst = *src;
715 852
            }
716 853
            samplecount *= 2;
854
        }else if(s->stereo){ //32-bit output
855
            int32_t *dst = samples32 + 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;
717 863
        }
718 864
    }
719
    *data_size = samplecount * 2;
865
    *data_size = samplecount * bpp;
720 866

  
721 867
    return buf_size;
722 868
}

Also available in: Unified diff