212
212
// macros for manipulating median values
213
213
#define GET_MED(n) ((c->median[n] >> 4) + 1)
214
#define DEC_MED(n) c->median[n] -= ((c->median[n] + (128>>n) - 2) / (128>>n)) * 2
215
#define INC_MED(n) c->median[n] += ((c->median[n] + (128>>n)) / (128>>n)) * 5
214
#define DEC_MED(n) c->median[n] -= ((c->median[n] + (128 >> n) - 2) / (128 >> n)) * 2
215
#define INC_MED(n) c->median[n] += ((c->median[n] + (128 >> n) ) / (128 >> n)) * 5
217
217
// macros for applying weight
218
218
#define UPDATE_WEIGHT_CLIP(weight, delta, samples, in) \
220
if((samples ^ in) < 0){ \
222
if(weight < -1024) weight = -1024; \
225
if(weight > 1024) weight = 1024; \
219
if (samples && in) { \
220
if ((samples ^ in) < 0) { \
222
if (weight < -1024) \
230
232
static av_always_inline int get_tail(GetBitContext *gb, int k)
236
239
e = (1 << (p + 1)) - k - 1;
237
240
res = p ? get_bits(gb, p) : 0;
239
res = (res<<1) - e + get_bits1(gb);
242
res = (res << 1) - e + get_bits1(gb);
246
248
int i, br[2], sl[2];
248
for(i = 0; i <= ctx->stereo_in; i++){
250
for (i = 0; i <= ctx->stereo_in; i++) {
249
251
ctx->ch[i].bitrate_acc += ctx->ch[i].bitrate_delta;
250
252
br[i] = ctx->ch[i].bitrate_acc >> 16;
251
253
sl[i] = LEVEL_DECAY(ctx->ch[i].slow_level);
253
if(ctx->stereo_in && ctx->hybrid_bitrate){
255
if (ctx->stereo_in && ctx->hybrid_bitrate) {
254
256
int balance = (sl[1] - sl[0] + br[1] + 1) >> 1;
257
if (balance > br[0]) {
256
258
br[1] = br[0] << 1;
258
}else if(-balance > br[0]){
260
} else if (-balance > br[0]) {
262
264
br[1] = br[0] + balance;
263
265
br[0] = br[0] - balance;
266
for(i = 0; i <= ctx->stereo_in; i++){
267
if(ctx->hybrid_bitrate){
268
if(sl[i] - br[i] > -0x100)
268
for (i = 0; i <= ctx->stereo_in; i++) {
269
if (ctx->hybrid_bitrate) {
270
if (sl[i] - br[i] > -0x100)
269
271
ctx->ch[i].error_limit = wp_exp2(sl[i] - br[i] + 0x100);
271
273
ctx->ch[i].error_limit = 0;
273
275
ctx->ch[i].error_limit = wp_exp2(br[i]);
278
static int wv_get_value(WavpackFrameContext *ctx, GetBitContext *gb, int channel, int *last)
280
static int wv_get_value(WavpackFrameContext *ctx, GetBitContext *gb,
281
int channel, int *last)
281
284
int sign, base, add, ret;
306
if(get_bits_count(gb) >= ctx->data_size){
315
321
t = get_unary_0_33(gb);
316
if(get_bits_count(gb) >= ctx->data_size){
322
if (get_bits_left(gb) < 0)
321
325
t2 = get_unary_0_33(gb);
323
else t += get_bits(gb, t2 - 1) | (1 << (t2 - 1));
327
if (get_bits_left(gb) < 0)
331
if (get_bits_left(gb) < t2 - 1)
333
t += get_bits(gb, t2 - 1) | (1 << (t2 - 1));
333
344
ctx->zero = !ctx->one;
336
if(ctx->hybrid && !channel)
347
if (ctx->hybrid && !channel)
337
348
update_error_limit(ctx);
341
add = GET_MED(0) - 1;
352
add = GET_MED(0) - 1;
344
355
base = GET_MED(0);
345
add = GET_MED(1) - 1;
356
add = GET_MED(1) - 1;
349
360
base = GET_MED(0) + GET_MED(1);
350
add = GET_MED(2) - 1;
361
add = GET_MED(2) - 1;
355
366
base = GET_MED(0) + GET_MED(1) + GET_MED(2) * (t - 2);
356
add = GET_MED(2) - 1;
367
add = GET_MED(2) - 1;
372
if (!c->error_limit) {
362
373
ret = base + get_tail(gb, add);
364
int mid = (base*2 + add + 1) >> 1;
365
while(add > c->error_limit){
374
if (get_bits_left(gb) <= 0)
377
int mid = (base * 2 + add + 1) >> 1;
378
while (add > c->error_limit) {
379
if (get_bits_left(gb) <= 0)
367
382
add -= (mid - base);
370
385
add = mid - base - 1;
371
mid = (base*2 + add + 1) >> 1;
386
mid = (base * 2 + add + 1) >> 1;
375
390
sign = get_bits1(gb);
376
if(ctx->hybrid_bitrate)
391
if (ctx->hybrid_bitrate)
377
392
c->slow_level += wp_log2(ret) - LEVEL_DECAY(c->slow_level);
378
393
return sign ? ~ret : ret;
381
static inline int wv_get_value_integer(WavpackFrameContext *s, uint32_t *crc, int S)
400
static inline int wv_get_value_integer(WavpackFrameContext *s, uint32_t *crc,
386
406
S <<= s->extra_bits;
388
if(s->got_extra_bits){
408
if (s->got_extra_bits && get_bits_left(&s->gb_extra_bits) >= s->extra_bits) {
389
409
S |= get_bits(&s->gb_extra_bits, s->extra_bits);
390
*crc = *crc * 9 + (S&0xffff) * 3 + ((unsigned)S>>16);
410
*crc = *crc * 9 + (S & 0xffff) * 3 + ((unsigned)S >> 16);
393
414
bit = (S & s->and) | s->or;
394
return (((S + bit) << s->shift) - bit) << s->post_shift;
415
bit = (((S + bit) << s->shift) - bit) << s->post_shift;
418
bit = av_clip(bit, -s->hybrid_maxclip - 1, s->hybrid_maxclip);
397
423
static float wv_get_value_float(WavpackFrameContext *s, uint32_t *crc, int S)
405
431
int exp = s->float_max_exp;
407
if(s->got_extra_bits){
408
const int max_bits = 1 + 23 + 8 + 1;
433
if (s->got_extra_bits) {
434
const int max_bits = 1 + 23 + 8 + 1;
409
435
const int left_bits = get_bits_left(&s->gb_extra_bits);
411
if(left_bits + 8 * FF_INPUT_BUFFER_PADDING_SIZE < max_bits)
437
if (left_bits + 8 * FF_INPUT_BUFFER_PADDING_SIZE < max_bits)
416
442
S <<= s->float_shift;
421
if(s->got_extra_bits && get_bits1(&s->gb_extra_bits)){
446
if (S >= 0x1000000) {
447
if (s->got_extra_bits && get_bits1(&s->gb_extra_bits))
422
448
S = get_bits(&s->gb_extra_bits, 23);
428
453
int shift = 23 - av_log2(S);
429
454
exp = s->float_max_exp;
437
if((s->float_flag & WV_FLT_SHIFT_ONES) ||
438
(s->got_extra_bits && (s->float_flag & WV_FLT_SHIFT_SAME) && get_bits1(&s->gb_extra_bits)) ){
461
if ((s->float_flag & WV_FLT_SHIFT_ONES) ||
462
(s->got_extra_bits && (s->float_flag & WV_FLT_SHIFT_SAME) &&
463
get_bits1(&s->gb_extra_bits))) {
439
464
S |= (1 << shift) - 1;
440
} else if(s->got_extra_bits && (s->float_flag & WV_FLT_SHIFT_SENT)){
465
} else if (s->got_extra_bits &&
466
(s->float_flag & WV_FLT_SHIFT_SENT)) {
441
467
S |= get_bits(&s->gb_extra_bits, shift);
445
471
exp = s->float_max_exp;
451
if(s->got_extra_bits && (s->float_flag & WV_FLT_ZERO_SENT)){
452
if(get_bits1(&s->gb_extra_bits)){
477
if (s->got_extra_bits && (s->float_flag & WV_FLT_ZERO_SENT)) {
478
if (get_bits1(&s->gb_extra_bits)) {
453
479
S = get_bits(&s->gb_extra_bits, 23);
454
if(s->float_max_exp >= 25)
480
if (s->float_max_exp >= 25)
455
481
exp = get_bits(&s->gb_extra_bits, 8);
456
482
sign = get_bits1(&s->gb_extra_bits);
458
if(s->float_flag & WV_FLT_ZERO_SIGN)
484
if (s->float_flag & WV_FLT_ZERO_SIGN)
459
485
sign = get_bits1(&s->gb_extra_bits);
486
513
float *dstfl = dst;
487
514
const int channel_pad = s->avctx->channels - 2;
489
if(s->samples_left == s->samples)
490
s->one = s->zero = s->zeroes = 0;
516
s->one = s->zero = s->zeroes = 0;
492
518
L = wv_get_value(s, gb, 0, &last);
494
521
R = wv_get_value(s, gb, 1, &last);
496
for(i = 0; i < s->terms; i++){
524
for (i = 0; i < s->terms; i++) {
497
525
t = s->decorr[i].value;
501
529
A = 2 * s->decorr[i].samplesA[0] - s->decorr[i].samplesA[1];
502
530
B = 2 * s->decorr[i].samplesB[0] - s->decorr[i].samplesB[1];
504
532
A = (3 * s->decorr[i].samplesA[0] - s->decorr[i].samplesA[1]) >> 1;
505
533
B = (3 * s->decorr[i].samplesB[0] - s->decorr[i].samplesB[1]) >> 1;
507
535
s->decorr[i].samplesA[1] = s->decorr[i].samplesA[0];
508
536
s->decorr[i].samplesB[1] = s->decorr[i].samplesB[0];
511
539
A = s->decorr[i].samplesA[pos];
512
540
B = s->decorr[i].samplesB[pos];
513
541
j = (pos + t) & 7;
515
if(type != AV_SAMPLE_FMT_S16){
543
if (type != AV_SAMPLE_FMT_S16) {
516
544
L2 = L + ((s->decorr[i].weightA * (int64_t)A + 512) >> 10);
517
545
R2 = R + ((s->decorr[i].weightB * (int64_t)B + 512) >> 10);
519
547
L2 = L + ((s->decorr[i].weightA * A + 512) >> 10);
520
548
R2 = R + ((s->decorr[i].weightB * B + 512) >> 10);
522
if(A && L) s->decorr[i].weightA -= ((((L ^ A) >> 30) & 2) - 1) * s->decorr[i].delta;
523
if(B && R) s->decorr[i].weightB -= ((((R ^ B) >> 30) & 2) - 1) * s->decorr[i].delta;
550
if (A && L) s->decorr[i].weightA -= ((((L ^ A) >> 30) & 2) - 1) * s->decorr[i].delta;
551
if (B && R) s->decorr[i].weightB -= ((((R ^ B) >> 30) & 2) - 1) * s->decorr[i].delta;
524
552
s->decorr[i].samplesA[j] = L = L2;
525
553
s->decorr[i].samplesB[j] = R = R2;
527
if(type != AV_SAMPLE_FMT_S16)
554
} else if (t == -1) {
555
if (type != AV_SAMPLE_FMT_S16)
528
556
L2 = L + ((s->decorr[i].weightA * (int64_t)s->decorr[i].samplesA[0] + 512) >> 10);
530
558
L2 = L + ((s->decorr[i].weightA * s->decorr[i].samplesA[0] + 512) >> 10);
531
559
UPDATE_WEIGHT_CLIP(s->decorr[i].weightA, s->decorr[i].delta, s->decorr[i].samplesA[0], L);
533
if(type != AV_SAMPLE_FMT_S16)
561
if (type != AV_SAMPLE_FMT_S16)
534
562
R2 = R + ((s->decorr[i].weightB * (int64_t)L2 + 512) >> 10);
536
564
R2 = R + ((s->decorr[i].weightB * L2 + 512) >> 10);
537
565
UPDATE_WEIGHT_CLIP(s->decorr[i].weightB, s->decorr[i].delta, L2, R);
539
567
s->decorr[i].samplesA[0] = R;
541
if(type != AV_SAMPLE_FMT_S16)
569
if (type != AV_SAMPLE_FMT_S16)
542
570
R2 = R + ((s->decorr[i].weightB * (int64_t)s->decorr[i].samplesB[0] + 512) >> 10);
544
572
R2 = R + ((s->decorr[i].weightB * s->decorr[i].samplesB[0] + 512) >> 10);
545
573
UPDATE_WEIGHT_CLIP(s->decorr[i].weightB, s->decorr[i].delta, s->decorr[i].samplesB[0], R);
549
577
R2 = s->decorr[i].samplesA[0];
550
578
s->decorr[i].samplesA[0] = R;
553
if(type != AV_SAMPLE_FMT_S16)
581
if (type != AV_SAMPLE_FMT_S16)
554
582
L2 = L + ((s->decorr[i].weightA * (int64_t)R2 + 512) >> 10);
556
584
L2 = L + ((s->decorr[i].weightA * R2 + 512) >> 10);
578
606
dst16 += channel_pad;
581
}while(!last && count < s->max_samples);
583
s->samples_left -= count;
584
if(!s->samples_left){
586
av_log(s->avctx, AV_LOG_ERROR, "CRC error\n");
589
if(s->got_extra_bits && crc_extra_bits != s->crc_extra_bits){
590
av_log(s->avctx, AV_LOG_ERROR, "Extra bits CRC error\n");
593
wv_reset_saved_context(s);
597
s->sc.bits_used = get_bits_count(&s->gb);
598
if(s->got_extra_bits){
599
s->extra_sc.crc = crc_extra_bits;
600
s->extra_sc.bits_used = get_bits_count(&s->gb_extra_bits);
609
} while (!last && count < s->samples);
611
wv_reset_saved_context(s);
613
av_log(s->avctx, AV_LOG_ERROR, "CRC error\n");
616
if (s->got_extra_bits && crc_extra_bits != s->crc_extra_bits) {
617
av_log(s->avctx, AV_LOG_ERROR, "Extra bits CRC error\n");
603
621
return count * 2;
606
static inline int wv_unpack_mono(WavpackFrameContext *s, GetBitContext *gb, void *dst, const int type)
624
static inline int wv_unpack_mono(WavpackFrameContext *s, GetBitContext *gb,
625
void *dst, const int type)
608
627
int i, j, count = 0;
616
635
float *dstfl = dst;
617
636
const int channel_stride = s->avctx->channels;
619
if(s->samples_left == s->samples)
620
s->one = s->zero = s->zeroes = 0;
638
s->one = s->zero = s->zeroes = 0;
622
640
T = wv_get_value(s, gb, 0, &last);
625
for(i = 0; i < s->terms; i++){
644
for (i = 0; i < s->terms; i++) {
626
645
t = s->decorr[i].value;
629
A = 2 * s->decorr[i].samplesA[0] - s->decorr[i].samplesA[1];
648
A = 2 * s->decorr[i].samplesA[0] - s->decorr[i].samplesA[1];
631
650
A = (3 * s->decorr[i].samplesA[0] - s->decorr[i].samplesA[1]) >> 1;
632
651
s->decorr[i].samplesA[1] = s->decorr[i].samplesA[0];
635
654
A = s->decorr[i].samplesA[pos];
636
655
j = (pos + t) & 7;
638
if(type != AV_SAMPLE_FMT_S16)
657
if (type != AV_SAMPLE_FMT_S16)
639
658
S = T + ((s->decorr[i].weightA * (int64_t)A + 512) >> 10);
641
660
S = T + ((s->decorr[i].weightA * A + 512) >> 10);
642
if(A && T) s->decorr[i].weightA -= ((((T ^ A) >> 30) & 2) - 1) * s->decorr[i].delta;
662
s->decorr[i].weightA -= ((((T ^ A) >> 30) & 2) - 1) * s->decorr[i].delta;
643
663
s->decorr[i].samplesA[j] = T = S;
645
665
pos = (pos + 1) & 7;
646
666
crc = crc * 3 + S;
648
if(type == AV_SAMPLE_FMT_FLT){
668
if (type == AV_SAMPLE_FMT_FLT) {
649
669
*dstfl = wv_get_value_float(s, &crc_extra_bits, S);
650
670
dstfl += channel_stride;
651
}else if(type == AV_SAMPLE_FMT_S32){
671
} else if (type == AV_SAMPLE_FMT_S32) {
652
672
*dst32 = wv_get_value_integer(s, &crc_extra_bits, S);
653
673
dst32 += channel_stride;
655
675
*dst16 = wv_get_value_integer(s, &crc_extra_bits, S);
656
676
dst16 += channel_stride;
659
}while(!last && count < s->max_samples);
661
s->samples_left -= count;
662
if(!s->samples_left){
664
av_log(s->avctx, AV_LOG_ERROR, "CRC error\n");
667
if(s->got_extra_bits && crc_extra_bits != s->crc_extra_bits){
668
av_log(s->avctx, AV_LOG_ERROR, "Extra bits CRC error\n");
671
wv_reset_saved_context(s);
675
s->sc.bits_used = get_bits_count(&s->gb);
676
if(s->got_extra_bits){
677
s->extra_sc.crc = crc_extra_bits;
678
s->extra_sc.bits_used = get_bits_count(&s->gb_extra_bits);
679
} while (!last && count < s->samples);
681
wv_reset_saved_context(s);
683
av_log(s->avctx, AV_LOG_ERROR, "CRC error\n");
686
if (s->got_extra_bits && crc_extra_bits != s->crc_extra_bits) {
687
av_log(s->avctx, AV_LOG_ERROR, "Extra bits CRC error\n");
684
694
static av_cold int wv_alloc_frame_context(WavpackContext *c)
687
if(c->fdec_num == WV_MAX_FRAME_DECODERS)
697
if (c->fdec_num == WV_MAX_FRAME_DECODERS)
690
700
c->fdec[c->fdec_num] = av_mallocz(sizeof(**c->fdec));
691
if(!c->fdec[c->fdec_num])
701
if (!c->fdec[c->fdec_num])
694
704
c->fdec[c->fdec_num - 1]->avctx = c->avctx;
702
712
WavpackContext *s = avctx->priv_data;
704
714
s->avctx = avctx;
705
if(avctx->bits_per_coded_sample <= 16)
715
if (avctx->bits_per_coded_sample <= 16)
706
716
avctx->sample_fmt = AV_SAMPLE_FMT_S16;
708
718
avctx->sample_fmt = AV_SAMPLE_FMT_S32;
709
if(avctx->channels <= 2 && !avctx->channel_layout)
710
avctx->channel_layout = (avctx->channels==2) ? AV_CH_LAYOUT_STEREO : AV_CH_LAYOUT_MONO;
719
if (avctx->channels <= 2 && !avctx->channel_layout)
720
avctx->channel_layout = (avctx->channels == 2) ? AV_CH_LAYOUT_STEREO :
712
723
s->multichannel = avctx->channels > 2;
713
724
/* lavf demuxer does not provide extradata, Matroska stores 0x403
714
725
there, use this to detect decoding mode for multichannel */
716
if(s->multichannel && avctx->extradata && avctx->extradata_size == 2){
727
if (s->multichannel && avctx->extradata && avctx->extradata_size == 2) {
717
728
int ver = AV_RL16(avctx->extradata);
718
if(ver >= 0x402 && ver <= 0x410)
729
if (ver >= 0x402 && ver <= 0x410)
735
avcodec_get_frame_defaults(&s->frame);
736
avctx->coded_frame = &s->frame;
739
753
static int wavpack_decode_block(AVCodecContext *avctx, int block_no,
740
void *data, int *data_size,
754
void *data, int *got_frame_ptr,
741
755
const uint8_t *buf, int buf_size)
743
757
WavpackContext *wc = avctx->priv_data;
744
758
WavpackFrameContext *s;
745
759
void *samples = data;
747
int got_terms = 0, got_weights = 0, got_samples = 0, got_entropy = 0, got_bs = 0, got_float = 0;
749
const uint8_t* orig_buf = buf;
750
const uint8_t* buf_end = buf + buf_size;
761
int got_terms = 0, got_weights = 0, got_samples = 0,
762
got_entropy = 0, got_bs = 0, got_float = 0, got_hybrid = 0;
763
const uint8_t *orig_buf = buf;
764
const uint8_t *buf_end = buf + buf_size;
751
765
int i, j, id, size, ssize, weights, t;
752
766
int bpp, chan, chmask;
759
if(block_no >= wc->fdec_num && wv_alloc_frame_context(wc) < 0){
773
if (block_no >= wc->fdec_num && wv_alloc_frame_context(wc) < 0) {
760
774
av_log(avctx, AV_LOG_ERROR, "Error creating frame decode context\n");
764
778
s = wc->fdec[block_no];
766
780
av_log(avctx, AV_LOG_ERROR, "Context for block %d is not present\n", block_no);
770
if(!s->samples_left){
771
memset(s->decorr, 0, MAX_TERMS * sizeof(Decorr));
772
memset(s->ch, 0, sizeof(s->ch));
774
s->and = s->or = s->shift = 0;
775
s->got_extra_bits = 0;
784
memset(s->decorr, 0, MAX_TERMS * sizeof(Decorr));
785
memset(s->ch, 0, sizeof(s->ch));
787
s->and = s->or = s->shift = 0;
788
s->got_extra_bits = 0;
779
791
s->samples = AV_RL32(buf); buf += 4;
785
797
s->samples = wc->samples;
787
799
s->frame_flags = AV_RL32(buf); buf += 4;
788
if(s->frame_flags&0x80){
790
avctx->sample_fmt = AV_SAMPLE_FMT_FLT;
791
} else if((s->frame_flags&0x03) <= 1){
793
avctx->sample_fmt = AV_SAMPLE_FMT_S16;
796
avctx->sample_fmt = AV_SAMPLE_FMT_S32;
800
bpp = av_get_bytes_per_sample(avctx->sample_fmt);
798
801
samples = (uint8_t*)samples + bpp * wc->ch_offset;
800
s->stereo = !(s->frame_flags & WV_MONO);
801
s->stereo_in = (s->frame_flags & WV_FALSE_STEREO) ? 0 : s->stereo;
802
s->joint = s->frame_flags & WV_JOINT_STEREO;
803
s->hybrid = s->frame_flags & WV_HYBRID_MODE;
804
s->hybrid_bitrate = s->frame_flags & WV_HYBRID_BITRATE;
805
s->post_shift = 8 * (bpp-1-(s->frame_flags&0x03)) + ((s->frame_flags >> 13) & 0x1f);
806
s->CRC = AV_RL32(buf); buf += 4;
803
s->stereo = !(s->frame_flags & WV_MONO);
804
s->stereo_in = (s->frame_flags & WV_FALSE_STEREO) ? 0 : s->stereo;
805
s->joint = s->frame_flags & WV_JOINT_STEREO;
806
s->hybrid = s->frame_flags & WV_HYBRID_MODE;
807
s->hybrid_bitrate = s->frame_flags & WV_HYBRID_BITRATE;
808
s->hybrid_maxclip = (1LL << ((((s->frame_flags & 0x03) + 1) << 3) - 1)) - 1;
809
s->post_shift = 8 * (bpp - 1 - (s->frame_flags & 0x03)) +
810
((s->frame_flags >> 13) & 0x1f);
811
s->CRC = AV_RL32(buf); buf += 4;
808
813
buf += 4; //skip block size;
810
815
wc->ch_offset += 1 + s->stereo;
812
s->max_samples = *data_size / (bpp * avctx->channels);
813
s->max_samples = FFMIN(s->max_samples, s->samples);
814
if(s->samples_left > 0){
815
s->max_samples = FFMIN(s->max_samples, s->samples_left);
819
817
// parse metadata blocks
820
while(buf < buf_end){
818
while (buf < buf_end) {
823
if(id & WP_IDF_LONG) {
821
if (id & WP_IDF_LONG) {
824
822
size |= (*buf++) << 8;
825
823
size |= (*buf++) << 16;
827
825
size <<= 1; // size is specified in words
829
if(id & WP_IDF_ODD) size--;
831
830
av_log(avctx, AV_LOG_ERROR, "Got incorrect block %02X with size %i\n", id, size);
834
if(buf + ssize > buf_end){
833
if (buf + ssize > buf_end) {
835
834
av_log(avctx, AV_LOG_ERROR, "Block size %i is out of bounds\n", size);
838
if(id & WP_IDF_IGNORE){
837
if (id & WP_IDF_IGNORE) {
842
switch(id & WP_IDF_MASK){
841
switch (id & WP_IDF_MASK) {
843
842
case WP_ID_DECTERMS:
843
if (size > MAX_TERMS) {
844
av_log(avctx, AV_LOG_ERROR, "Too many decorrelation terms\n");
845
if(s->terms > MAX_TERMS){
846
av_log(avctx, AV_LOG_ERROR, "Too many decorrelation terms\n");
850
for(i = 0; i < s->terms; i++) {
850
for (i = 0; i < s->terms; i++) {
851
851
s->decorr[s->terms - i - 1].value = (*buf & 0x1F) - 5;
852
852
s->decorr[s->terms - i - 1].delta = *buf >> 5;
857
857
case WP_ID_DECWEIGHTS:
859
859
av_log(avctx, AV_LOG_ERROR, "No decorrelation terms met\n");
862
862
weights = size >> s->stereo_in;
863
if(weights > MAX_TERMS || weights > s->terms){
863
if (weights > MAX_TERMS || weights > s->terms) {
864
864
av_log(avctx, AV_LOG_ERROR, "Too many decorrelation weights\n");
868
for(i = 0; i < weights; i++) {
868
for (i = 0; i < weights; i++) {
869
869
t = (int8_t)(*buf++);
870
870
s->decorr[s->terms - i - 1].weightA = t << 3;
871
if(s->decorr[s->terms - i - 1].weightA > 0)
872
s->decorr[s->terms - i - 1].weightA += (s->decorr[s->terms - i - 1].weightA + 64) >> 7;
871
if (s->decorr[s->terms - i - 1].weightA > 0)
872
s->decorr[s->terms - i - 1].weightA +=
873
(s->decorr[s->terms - i - 1].weightA + 64) >> 7;
874
875
t = (int8_t)(*buf++);
875
876
s->decorr[s->terms - i - 1].weightB = t << 3;
876
if(s->decorr[s->terms - i - 1].weightB > 0)
877
s->decorr[s->terms - i - 1].weightB += (s->decorr[s->terms - i - 1].weightB + 64) >> 7;
877
if (s->decorr[s->terms - i - 1].weightB > 0)
878
s->decorr[s->terms - i - 1].weightB +=
879
(s->decorr[s->terms - i - 1].weightB + 64) >> 7;
882
884
case WP_ID_DECSAMPLES:
884
886
av_log(avctx, AV_LOG_ERROR, "No decorrelation terms met\n");
888
for(i = s->terms - 1; (i >= 0) && (t < size); i--) {
889
if(s->decorr[i].value > 8){
890
for (i = s->terms - 1; (i >= 0) && (t < size); i--) {
891
if (s->decorr[i].value > 8) {
890
892
s->decorr[i].samplesA[0] = wp_exp2(AV_RL16(buf)); buf += 2;
891
893
s->decorr[i].samplesA[1] = wp_exp2(AV_RL16(buf)); buf += 2;
893
895
s->decorr[i].samplesB[0] = wp_exp2(AV_RL16(buf)); buf += 2;
894
896
s->decorr[i].samplesB[1] = wp_exp2(AV_RL16(buf)); buf += 2;
898
}else if(s->decorr[i].value < 0){
900
} else if (s->decorr[i].value < 0) {
899
901
s->decorr[i].samplesA[0] = wp_exp2(AV_RL16(buf)); buf += 2;
900
902
s->decorr[i].samplesB[0] = wp_exp2(AV_RL16(buf)); buf += 2;
903
for(j = 0; j < s->decorr[i].value; j++){
905
for (j = 0; j < s->decorr[i].value; j++) {
904
906
s->decorr[i].samplesA[j] = wp_exp2(AV_RL16(buf)); buf += 2;
906
908
s->decorr[i].samplesB[j] = wp_exp2(AV_RL16(buf)); buf += 2;
909
910
t += s->decorr[i].value * 2 * (s->stereo_in + 1);
928
930
case WP_ID_HYBRID:
929
if(s->hybrid_bitrate){
930
for(i = 0; i <= s->stereo_in; i++){
931
if (s->hybrid_bitrate) {
932
for (i = 0; i <= s->stereo_in; i++) {
931
933
s->ch[i].slow_level = wp_exp2(AV_RL16(buf));
936
for(i = 0; i < (s->stereo_in + 1); i++){
938
for (i = 0; i < (s->stereo_in + 1); i++) {
937
939
s->ch[i].bitrate_acc = AV_RL16(buf) << 16;
942
for(i = 0; i < (s->stereo_in + 1); i++){
944
for (i = 0; i < (s->stereo_in + 1); i++) {
943
945
s->ch[i].bitrate_delta = wp_exp2((int16_t)AV_RL16(buf));
947
for(i = 0; i < (s->stereo_in + 1); i++)
949
for (i = 0; i < (s->stereo_in + 1); i++)
948
950
s->ch[i].bitrate_delta = 0;
952
954
case WP_ID_INT32INFO:
954
956
av_log(avctx, AV_LOG_ERROR, "Invalid INT32INFO, size = %i, sent_bits = %i\n", size, *buf);
959
961
s->extra_bits = buf[0];
961
963
s->shift = buf[1];
963
965
s->and = s->or = 1;
964
966
s->shift = buf[2];
967
969
s->shift = buf[3];
971
973
case WP_ID_FLOATINFO:
973
975
av_log(avctx, AV_LOG_ERROR, "Invalid FLOATINFO, size = %i\n", size);
977
s->float_flag = buf[0];
978
s->float_shift = buf[1];
979
s->float_flag = buf[0];
980
s->float_shift = buf[1];
979
981
s->float_max_exp = buf[2];
1002
1005
s->got_extra_bits = 1;
1004
1007
case WP_ID_CHANINFO:
1006
1009
av_log(avctx, AV_LOG_ERROR, "Insufficient channel information\n");
1015
chmask = AV_RL16(buf);
1018
chmask = AV_RL24(buf);
1021
chmask = AV_RL32(buf);
1014
case 0: chmask = *buf; break;
1015
case 1: chmask = AV_RL16(buf); break;
1016
case 2: chmask = AV_RL24(buf); break;
1017
case 3: chmask = AV_RL32(buf); break;
1024
1019
chan |= (buf[1] & 0xF) << 8;
1025
1020
chmask = AV_RL24(buf + 2);
1028
av_log(avctx, AV_LOG_ERROR, "Invalid channel info size %d\n", size);
1029
chan = avctx->channels;
1023
av_log(avctx, AV_LOG_ERROR, "Invalid channel info size %d\n",
1025
chan = avctx->channels;
1030
1026
chmask = avctx->channel_layout;
1032
if(chan != avctx->channels){
1033
av_log(avctx, AV_LOG_ERROR, "Block reports total %d channels, decoder believes it's %d channels\n",
1034
chan, avctx->channels);
1028
if (chan != avctx->channels) {
1029
av_log(avctx, AV_LOG_ERROR, "Block reports total %d channels, "
1030
"decoder believes it's %d channels\n", chan,
1037
if(!avctx->channel_layout)
1034
if (!avctx->channel_layout)
1038
1035
avctx->channel_layout = chmask;
1039
1036
buf += size - 1;
1044
if(id & WP_IDF_ODD) buf++;
1046
if(!s->samples_left){
1048
av_log(avctx, AV_LOG_ERROR, "No block with decorrelation terms\n");
1052
av_log(avctx, AV_LOG_ERROR, "No block with decorrelation weights\n");
1056
av_log(avctx, AV_LOG_ERROR, "No block with decorrelation samples\n");
1060
av_log(avctx, AV_LOG_ERROR, "No block with entropy info\n");
1063
if(s->hybrid && !got_hybrid){
1064
av_log(avctx, AV_LOG_ERROR, "Hybrid config not found\n");
1068
av_log(avctx, AV_LOG_ERROR, "Packed samples not found\n");
1071
if(!got_float && avctx->sample_fmt == AV_SAMPLE_FMT_FLT){
1072
av_log(avctx, AV_LOG_ERROR, "Float information not found\n");
1075
if(s->got_extra_bits && avctx->sample_fmt != AV_SAMPLE_FMT_FLT){
1076
const int size = get_bits_left(&s->gb_extra_bits);
1077
const int wanted = s->samples * s->extra_bits << s->stereo_in;
1079
av_log(avctx, AV_LOG_ERROR, "Too small EXTRABITS\n");
1080
s->got_extra_bits = 0;
1083
s->samples_left = s->samples;
1085
init_get_bits(&s->gb, orig_buf + s->sc.offset, s->sc.size);
1086
skip_bits_long(&s->gb, s->sc.bits_used);
1087
if(s->got_extra_bits){
1088
init_get_bits(&s->gb_extra_bits, orig_buf + s->extra_sc.offset,
1090
skip_bits_long(&s->gb_extra_bits, s->extra_sc.bits_used);
1095
if(avctx->sample_fmt == AV_SAMPLE_FMT_S16)
1041
if (id & WP_IDF_ODD)
1046
av_log(avctx, AV_LOG_ERROR, "No block with decorrelation terms\n");
1050
av_log(avctx, AV_LOG_ERROR, "No block with decorrelation weights\n");
1054
av_log(avctx, AV_LOG_ERROR, "No block with decorrelation samples\n");
1058
av_log(avctx, AV_LOG_ERROR, "No block with entropy info\n");
1061
if (s->hybrid && !got_hybrid) {
1062
av_log(avctx, AV_LOG_ERROR, "Hybrid config not found\n");
1066
av_log(avctx, AV_LOG_ERROR, "Packed samples not found\n");
1069
if (!got_float && avctx->sample_fmt == AV_SAMPLE_FMT_FLT) {
1070
av_log(avctx, AV_LOG_ERROR, "Float information not found\n");
1073
if (s->got_extra_bits && avctx->sample_fmt != AV_SAMPLE_FMT_FLT) {
1074
const int size = get_bits_left(&s->gb_extra_bits);
1075
const int wanted = s->samples * s->extra_bits << s->stereo_in;
1076
if (size < wanted) {
1077
av_log(avctx, AV_LOG_ERROR, "Too small EXTRABITS\n");
1078
s->got_extra_bits = 0;
1083
if (avctx->sample_fmt == AV_SAMPLE_FMT_S16)
1096
1084
samplecount = wv_unpack_stereo(s, &s->gb, samples, AV_SAMPLE_FMT_S16);
1097
else if(avctx->sample_fmt == AV_SAMPLE_FMT_S32)
1085
else if (avctx->sample_fmt == AV_SAMPLE_FMT_S32)
1098
1086
samplecount = wv_unpack_stereo(s, &s->gb, samples, AV_SAMPLE_FMT_S32);
1100
1088
samplecount = wv_unpack_stereo(s, &s->gb, samples, AV_SAMPLE_FMT_FLT);
1090
if (samplecount < 0)
1101
1093
samplecount >>= 1;
1103
1095
const int channel_stride = avctx->channels;
1105
if(avctx->sample_fmt == AV_SAMPLE_FMT_S16)
1097
if (avctx->sample_fmt == AV_SAMPLE_FMT_S16)
1106
1098
samplecount = wv_unpack_mono(s, &s->gb, samples, AV_SAMPLE_FMT_S16);
1107
else if(avctx->sample_fmt == AV_SAMPLE_FMT_S32)
1099
else if (avctx->sample_fmt == AV_SAMPLE_FMT_S32)
1108
1100
samplecount = wv_unpack_mono(s, &s->gb, samples, AV_SAMPLE_FMT_S32);
1110
1102
samplecount = wv_unpack_mono(s, &s->gb, samples, AV_SAMPLE_FMT_FLT);
1112
if(s->stereo && avctx->sample_fmt == AV_SAMPLE_FMT_S16){
1104
if (samplecount < 0)
1107
if (s->stereo && avctx->sample_fmt == AV_SAMPLE_FMT_S16) {
1113
1108
int16_t *dst = (int16_t*)samples + 1;
1114
1109
int16_t *src = (int16_t*)samples;
1115
1110
int cnt = samplecount;
1118
1113
src += channel_stride;
1119
1114
dst += channel_stride;
1121
}else if(s->stereo && avctx->sample_fmt == AV_SAMPLE_FMT_S32){
1116
} else if (s->stereo && avctx->sample_fmt == AV_SAMPLE_FMT_S32) {
1122
1117
int32_t *dst = (int32_t*)samples + 1;
1123
1118
int32_t *src = (int32_t*)samples;
1124
1119
int cnt = samplecount;
1127
1122
src += channel_stride;
1128
1123
dst += channel_stride;
1130
}else if(s->stereo){
1125
} else if (s->stereo) {
1131
1126
float *dst = (float*)samples + 1;
1132
1127
float *src = (float*)samples;
1133
1128
int cnt = samplecount;
1136
1131
src += channel_stride;
1137
1132
dst += channel_stride;
1142
wc->samples_left = s->samples_left;
1144
1139
return samplecount * bpp;
1147
static int wavpack_decode_frame(AVCodecContext *avctx,
1148
void *data, int *data_size,
1142
static void wavpack_decode_flush(AVCodecContext *avctx)
1151
1144
WavpackContext *s = avctx->priv_data;
1147
for (i = 0; i < s->fdec_num; i++)
1148
wv_reset_saved_context(s->fdec[i]);
1151
static int wavpack_decode_frame(AVCodecContext *avctx, void *data,
1152
int *got_frame_ptr, AVPacket *avpkt)
1154
WavpackContext *s = avctx->priv_data;
1152
1155
const uint8_t *buf = avpkt->data;
1153
int buf_size = avpkt->size;
1156
int buf_size = avpkt->size;
1157
int frame_size, ret, frame_flags;
1155
1158
int samplecount = 0;
1158
s->samples_left = 0;
1159
1161
s->ch_offset = 0;
1162
s->samples = AV_RL32(buf); buf += 4;
1164
while(buf_size > 0){
1165
if(!s->multichannel){
1163
/* determine number of samples */
1165
s->samples = AV_RL32(buf); buf += 4;
1166
frame_flags = AV_RL32(buf);
1168
if (s->multichannel) {
1169
s->samples = AV_RL32(buf + 4);
1170
frame_flags = AV_RL32(buf + 8);
1172
s->samples = AV_RL32(buf);
1173
frame_flags = AV_RL32(buf + 4);
1176
if (s->samples <= 0) {
1177
av_log(avctx, AV_LOG_ERROR, "Invalid number of samples: %d\n",
1179
return AVERROR(EINVAL);
1182
if (frame_flags & 0x80) {
1183
avctx->sample_fmt = AV_SAMPLE_FMT_FLT;
1184
} else if ((frame_flags & 0x03) <= 1) {
1185
avctx->sample_fmt = AV_SAMPLE_FMT_S16;
1187
avctx->sample_fmt = AV_SAMPLE_FMT_S32;
1190
/* get output buffer */
1191
s->frame.nb_samples = s->samples;
1192
if ((ret = avctx->get_buffer(avctx, &s->frame)) < 0) {
1193
av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
1197
while (buf_size > 0) {
1198
if (!s->multichannel) {
1166
1199
frame_size = buf_size;
1169
1202
frame_size = AV_RL32(buf) - 12; buf += 4; buf_size -= 4;
1171
if(buf_size < 12) //MKV files can have zero flags after last block
1204
if (buf_size < 12) //MKV files can have zero flags after last block
1173
1206
frame_size = AV_RL32(buf + 8) + 12;
1176
if(frame_size < 0 || frame_size > buf_size){
1177
av_log(avctx, AV_LOG_ERROR, "Block %d has invalid size (size %d vs. %d bytes left)\n",
1178
s->block, frame_size, buf_size);
1181
if((samplecount = wavpack_decode_block(avctx, s->block, data,
1182
data_size, buf, frame_size)) < 0)
1209
if (frame_size < 0 || frame_size > buf_size) {
1210
av_log(avctx, AV_LOG_ERROR, "Block %d has invalid size (size %d "
1211
"vs. %d bytes left)\n", s->block, frame_size, buf_size);
1212
wavpack_decode_flush(avctx);
1215
if ((samplecount = wavpack_decode_block(avctx, s->block,
1216
s->frame.data[0], got_frame_ptr,
1217
buf, frame_size)) < 0) {
1218
wavpack_decode_flush(avctx);
1185
1222
buf += frame_size; buf_size -= frame_size;
1187
*data_size = samplecount * avctx->channels;
1189
return s->samples_left > 0 ? 0 : avpkt->size;
1226
*(AVFrame *)data = s->frame;
1192
1231
AVCodec ff_wavpack_decoder = {
1196
sizeof(WavpackContext),
1197
wavpack_decode_init,
1200
wavpack_decode_frame,
1201
.capabilities = CODEC_CAP_SUBFRAMES,
1202
.long_name = NULL_IF_CONFIG_SMALL("WavPack"),
1233
.type = AVMEDIA_TYPE_AUDIO,
1234
.id = CODEC_ID_WAVPACK,
1235
.priv_data_size = sizeof(WavpackContext),
1236
.init = wavpack_decode_init,
1237
.close = wavpack_decode_end,
1238
.decode = wavpack_decode_frame,
1239
.flush = wavpack_decode_flush,
1240
.capabilities = CODEC_CAP_SUBFRAMES | CODEC_CAP_DR1,
1241
.long_name = NULL_IF_CONFIG_SMALL("WavPack"),