Revision 965828bb

View differences:

libavcodec/wavpack.c
67 67
    WP_ID_CHANINFO
68 68
};
69 69

  
70
typedef struct SavedContext {
71
    int offset;
72
    int size;
73
    int bits_used;
74
    uint32_t crc;
75
} SavedContext;
76

  
70 77
#define MAX_TERMS 16
71 78

  
72 79
typedef struct Decorr {
......
107 114
    int float_shift;
108 115
    int float_max_exp;
109 116
    WvChannel ch[2];
117
    int samples_left;
118
    int max_samples;
119
    int pos;
120
    SavedContext sc, extra_sc;
110 121
} WavpackContext;
111 122

  
112 123
// exponent table copied from WavPack source
......
439 450
    return value.f;
440 451
}
441 452

  
453
static void wv_reset_saved_context(WavpackContext *s)
454
{
455
    s->pos = 0;
456
    s->sc.crc = s->extra_sc.crc = 0xFFFFFFFF;
457
}
458

  
442 459
static inline int wv_unpack_stereo(WavpackContext *s, GetBitContext *gb, void *dst, const int type)
443 460
{
444 461
    int i, j, count = 0;
445 462
    int last, t;
446 463
    int A, B, L, L2, R, R2;
447
    int pos = 0;
448
    uint32_t crc = 0xFFFFFFFF;
449
    uint32_t crc_extra_bits = 0xFFFFFFFF;
464
    int pos = s->pos;
465
    uint32_t crc = s->sc.crc;
466
    uint32_t crc_extra_bits = s->extra_sc.crc;
450 467
    int16_t *dst16 = dst;
451 468
    int32_t *dst32 = dst;
452 469
    float   *dstfl = dst;
453 470

  
471
    if(s->samples_left == s->samples)
454 472
    s->one = s->zero = s->zeroes = 0;
455 473
    do{
456 474
        L = wv_get_value(s, gb, 0, &last);
......
539 557
            *dst16++ = wv_get_value_integer(s, &crc_extra_bits, R);
540 558
        }
541 559
        count++;
542
    }while(!last && count < s->samples);
560
    }while(!last && count < s->max_samples);
543 561

  
562
    s->samples_left -= count;
563
    if(!s->samples_left){
544 564
    if(crc != s->CRC){
545 565
        av_log(s->avctx, AV_LOG_ERROR, "CRC error\n");
546 566
        return -1;
......
549 569
        av_log(s->avctx, AV_LOG_ERROR, "Extra bits CRC error\n");
550 570
        return -1;
551 571
    }
572
    wv_reset_saved_context(s);
573
    }else{
574
        s->pos = pos;
575
        s->sc.crc = crc;
576
        s->sc.bits_used = get_bits_count(&s->gb);
577
        if(s->got_extra_bits){
578
            s->extra_sc.crc = crc_extra_bits;
579
            s->extra_sc.bits_used = get_bits_count(&s->gb_extra_bits);
580
        }
581
    }
552 582
    return count * 2;
553 583
}
554 584

  
......
557 587
    int i, j, count = 0;
558 588
    int last, t;
559 589
    int A, S, T;
560
    int pos = 0;
561
    uint32_t crc = 0xFFFFFFFF;
562
    uint32_t crc_extra_bits = 0xFFFFFFFF;
590
    int pos = s->pos;
591
    uint32_t crc = s->sc.crc;
592
    uint32_t crc_extra_bits = s->extra_sc.crc;
563 593
    int16_t *dst16 = dst;
564 594
    int32_t *dst32 = dst;
565 595
    float   *dstfl = dst;
566 596

  
597
    if(s->samples_left == s->samples)
567 598
    s->one = s->zero = s->zeroes = 0;
568 599
    do{
569 600
        T = wv_get_value(s, gb, 0, &last);
......
601 632
        count++;
602 633
    }while(!last && count < s->samples);
603 634

  
635
    s->samples_left -= count;
636
    if(!s->samples_left){
604 637
    if(crc != s->CRC){
605 638
        av_log(s->avctx, AV_LOG_ERROR, "CRC error\n");
606 639
        return -1;
......
609 642
        av_log(s->avctx, AV_LOG_ERROR, "Extra bits CRC error\n");
610 643
        return -1;
611 644
    }
645
    wv_reset_saved_context(s);
646
    }else{
647
        s->pos = pos;
648
        s->sc.crc = crc;
649
        s->sc.bits_used = get_bits_count(&s->gb);
650
        if(s->got_extra_bits){
651
            s->extra_sc.crc = crc_extra_bits;
652
            s->extra_sc.bits_used = get_bits_count(&s->gb_extra_bits);
653
        }
654
    }
612 655
    return count;
613 656
}
614 657

  
......
624 667
        avctx->sample_fmt = SAMPLE_FMT_S32;
625 668
    avctx->channel_layout = (avctx->channels==2) ? CH_LAYOUT_STEREO : CH_LAYOUT_MONO;
626 669

  
670
    wv_reset_saved_context(s);
671

  
627 672
    return 0;
628 673
}
629 674

  
......
647 692
        return 0;
648 693
    }
649 694

  
695
    if(!s->samples_left){
650 696
    memset(s->decorr, 0, MAX_TERMS * sizeof(Decorr));
651 697
    memset(s->ch, 0, sizeof(s->ch));
652 698
    s->extra_bits = 0;
653 699
    s->and = s->or = s->shift = 0;
654 700
    s->got_extra_bits = 0;
701
    }
655 702

  
656 703
    s->samples = AV_RL32(buf); buf += 4;
657 704
    if(!s->samples){
......
676 723
    s->post_shift = 8 * (bpp-1-(s->frame_flags&0x03)) + ((s->frame_flags >> 13) & 0x1f);
677 724
    s->CRC = AV_RL32(buf); buf += 4;
678 725

  
679
    /* should not happen but who knows */
680
    if(s->samples * bpp * avctx->channels > *data_size){
681
        av_log(avctx, AV_LOG_ERROR, "Packet size is too big to be handled in lavc!\n");
682
        return -1;
726
    s->max_samples = *data_size / (bpp * avctx->channels);
727
    s->max_samples = FFMIN(s->max_samples, s->samples);
728
    if(s->samples_left > 0){
729
        s->max_samples = FFMIN(s->max_samples, s->samples_left);
730
        buf = buf_end;
683 731
    }
684 732

  
685 733
    // parse metadata blocks
......
847 895
            got_float = 1;
848 896
            break;
849 897
        case WP_ID_DATA:
898
            s->sc.offset = buf - avpkt->data;
899
            s->sc.size   = size * 8;
850 900
            init_get_bits(&s->gb, buf, size * 8);
851 901
            s->data_size = size * 8;
852 902
            buf += size;
......
858 908
                buf += size;
859 909
                continue;
860 910
            }
911
            s->extra_sc.offset = buf - avpkt->data;
912
            s->extra_sc.size   = size * 8;
861 913
            init_get_bits(&s->gb_extra_bits, buf, size * 8);
862 914
            s->crc_extra_bits = get_bits_long(&s->gb_extra_bits, 32);
863 915
            buf += size;
......
868 920
        }
869 921
        if(id & WP_IDF_ODD) buf++;
870 922
    }
923
    if(!s->samples_left){
871 924
    if(!got_terms){
872 925
        av_log(avctx, AV_LOG_ERROR, "No block with decorrelation terms\n");
873 926
        return -1;
......
904 957
            s->got_extra_bits = 0;
905 958
        }
906 959
    }
960
    s->samples_left = s->samples;
961
    }else{
962
        init_get_bits(&s->gb, avpkt->data + s->sc.offset, s->sc.size);
963
        skip_bits_long(&s->gb, s->sc.bits_used);
964
        if(s->got_extra_bits){
965
            init_get_bits(&s->gb_extra_bits, avpkt->data + s->extra_sc.offset,
966
                          s->extra_sc.size);
967
            skip_bits_long(&s->gb_extra_bits, s->extra_sc.bits_used);
968
        }
969
    }
907 970

  
908 971
    if(s->stereo_in){
909 972
        if(avctx->sample_fmt == SAMPLE_FMT_S16)
......
952 1015
    }
953 1016
    *data_size = samplecount * bpp;
954 1017

  
955
    return buf_size;
1018
    return s->samples_left > 0 ? 0 : buf_size;
956 1019
}
957 1020

  
958 1021
AVCodec wavpack_decoder = {

Also available in: Unified diff