2 * WavPack lossless audio decoder
3 * Copyright (c) 2006,2011 Konstantin Shishkov
5 * This file is part of FFmpeg.
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 #define BITSTREAM_READER_LE
24 #include "libavutil/channel_layout.h"
30 #include "bytestream.h"
35 * WavPack lossless audio decoder
38 typedef struct SavedContext
{
45 typedef struct WavpackFrameContext
{
46 AVCodecContext
*avctx
;
48 int stereo
, stereo_in
;
53 uint32_t crc_extra_bits
;
54 GetBitContext gb_extra_bits
;
55 int data_size
; // in bits
58 Decorr decorr
[MAX_TERMS
];
59 int zero
, one
, zeroes
;
63 int hybrid
, hybrid_bitrate
;
64 int hybrid_maxclip
, hybrid_minclip
;
70 SavedContext sc
, extra_sc
;
71 } WavpackFrameContext
;
73 #define WV_MAX_FRAME_DECODERS 14
75 typedef struct WavpackContext
{
76 AVCodecContext
*avctx
;
78 WavpackFrameContext
*fdec
[WV_MAX_FRAME_DECODERS
];
86 #define LEVEL_DECAY(a) (((a) + 0x80) >> 8)
88 static av_always_inline
int get_tail(GetBitContext
*gb
, int k
)
95 e
= (1 << (p
+ 1)) - k
- 1;
96 res
= p
? get_bits(gb
, p
) : 0;
98 res
= (res
<< 1) - e
+ get_bits1(gb
);
102 static void update_error_limit(WavpackFrameContext
*ctx
)
106 for (i
= 0; i
<= ctx
->stereo_in
; i
++) {
107 ctx
->ch
[i
].bitrate_acc
+= ctx
->ch
[i
].bitrate_delta
;
108 br
[i
] = ctx
->ch
[i
].bitrate_acc
>> 16;
109 sl
[i
] = LEVEL_DECAY(ctx
->ch
[i
].slow_level
);
111 if (ctx
->stereo_in
&& ctx
->hybrid_bitrate
) {
112 int balance
= (sl
[1] - sl
[0] + br
[1] + 1) >> 1;
113 if (balance
> br
[0]) {
116 } else if (-balance
> br
[0]) {
120 br
[1] = br
[0] + balance
;
121 br
[0] = br
[0] - balance
;
124 for (i
= 0; i
<= ctx
->stereo_in
; i
++) {
125 if (ctx
->hybrid_bitrate
) {
126 if (sl
[i
] - br
[i
] > -0x100)
127 ctx
->ch
[i
].error_limit
= wp_exp2(sl
[i
] - br
[i
] + 0x100);
129 ctx
->ch
[i
].error_limit
= 0;
131 ctx
->ch
[i
].error_limit
= wp_exp2(br
[i
]);
136 static int wv_get_value(WavpackFrameContext
*ctx
, GetBitContext
*gb
,
137 int channel
, int *last
)
140 int sign
, base
, add
, ret
;
141 WvChannel
*c
= &ctx
->ch
[channel
];
145 if ((ctx
->ch
[0].median
[0] < 2U) && (ctx
->ch
[1].median
[0] < 2U) &&
146 !ctx
->zero
&& !ctx
->one
) {
150 c
->slow_level
-= LEVEL_DECAY(c
->slow_level
);
154 t
= get_unary_0_33(gb
);
156 if (get_bits_left(gb
) < t
- 1)
158 t
= get_bits(gb
, t
- 1) | (1 << (t
- 1));
160 if (get_bits_left(gb
) < 0)
165 memset(ctx
->ch
[0].median
, 0, sizeof(ctx
->ch
[0].median
));
166 memset(ctx
->ch
[1].median
, 0, sizeof(ctx
->ch
[1].median
));
167 c
->slow_level
-= LEVEL_DECAY(c
->slow_level
);
177 t
= get_unary_0_33(gb
);
178 if (get_bits_left(gb
) < 0)
181 t2
= get_unary_0_33(gb
);
183 if (get_bits_left(gb
) < 0)
187 if (get_bits_left(gb
) < t2
- 1)
189 t
+= get_bits(gb
, t2
- 1) | (1 << (t2
- 1));
200 ctx
->zero
= !ctx
->one
;
203 if (ctx
->hybrid
&& !channel
)
204 update_error_limit(ctx
);
208 add
= GET_MED(0) - 1;
212 add
= GET_MED(1) - 1;
216 base
= GET_MED(0) + GET_MED(1);
217 add
= GET_MED(2) - 1;
222 base
= GET_MED(0) + GET_MED(1) + GET_MED(2) * (t
- 2);
223 add
= GET_MED(2) - 1;
228 if (!c
->error_limit
) {
229 if (add
>= 0x2000000U
) {
230 av_log(ctx
->avctx
, AV_LOG_ERROR
, "k %d is too large\n", add
);
233 ret
= base
+ get_tail(gb
, add
);
234 if (get_bits_left(gb
) <= 0)
237 int mid
= (base
* 2 + add
+ 1) >> 1;
238 while (add
> c
->error_limit
) {
239 if (get_bits_left(gb
) <= 0)
245 add
= mid
- base
- 1;
246 mid
= (base
* 2 + add
+ 1) >> 1;
250 sign
= get_bits1(gb
);
251 if (ctx
->hybrid_bitrate
)
252 c
->slow_level
+= wp_log2(ret
) - LEVEL_DECAY(c
->slow_level
);
253 return sign
? ~ret
: ret
;
256 ret
= get_bits_left(gb
);
258 av_log(ctx
->avctx
, AV_LOG_ERROR
, "Too few bits (%d) left\n", ret
);
264 static inline int wv_get_value_integer(WavpackFrameContext
*s
, uint32_t *crc
,
272 if (s
->got_extra_bits
&&
273 get_bits_left(&s
->gb_extra_bits
) >= s
->extra_bits
) {
274 S
|= get_bits(&s
->gb_extra_bits
, s
->extra_bits
);
275 *crc
= *crc
* 9 + (S
& 0xffff) * 3 + ((unsigned)S
>> 16);
279 bit
= (S
& s
->and) | s
->or;
280 bit
= ((S
+ bit
) << s
->shift
) - bit
;
283 bit
= av_clip(bit
, s
->hybrid_minclip
, s
->hybrid_maxclip
);
285 return bit
<< s
->post_shift
;
288 static float wv_get_value_float(WavpackFrameContext
*s
, uint32_t *crc
, int S
)
296 int exp
= s
->float_max_exp
;
298 if (s
->got_extra_bits
) {
299 const int max_bits
= 1 + 23 + 8 + 1;
300 const int left_bits
= get_bits_left(&s
->gb_extra_bits
);
302 if (left_bits
+ 8 * FF_INPUT_BUFFER_PADDING_SIZE
< max_bits
)
307 S
<<= s
->float_shift
;
311 if (S
>= 0x1000000) {
312 if (s
->got_extra_bits
&& get_bits1(&s
->gb_extra_bits
))
313 S
= get_bits(&s
->gb_extra_bits
, 23);
318 int shift
= 23 - av_log2(S
);
319 exp
= s
->float_max_exp
;
326 if ((s
->float_flag
& WV_FLT_SHIFT_ONES
) ||
327 (s
->got_extra_bits
&&
328 (s
->float_flag
& WV_FLT_SHIFT_SAME
) &&
329 get_bits1(&s
->gb_extra_bits
))) {
330 S
|= (1 << shift
) - 1;
331 } else if (s
->got_extra_bits
&&
332 (s
->float_flag
& WV_FLT_SHIFT_SENT
)) {
333 S
|= get_bits(&s
->gb_extra_bits
, shift
);
337 exp
= s
->float_max_exp
;
343 if (s
->got_extra_bits
&& (s
->float_flag
& WV_FLT_ZERO_SENT
)) {
344 if (get_bits1(&s
->gb_extra_bits
)) {
345 S
= get_bits(&s
->gb_extra_bits
, 23);
346 if (s
->float_max_exp
>= 25)
347 exp
= get_bits(&s
->gb_extra_bits
, 8);
348 sign
= get_bits1(&s
->gb_extra_bits
);
350 if (s
->float_flag
& WV_FLT_ZERO_SIGN
)
351 sign
= get_bits1(&s
->gb_extra_bits
);
356 *crc
= *crc
* 27 + S
* 9 + exp
* 3 + sign
;
358 value
.u
= (sign
<< 31) | (exp
<< 23) | S
;
362 static void wv_reset_saved_context(WavpackFrameContext
*s
)
365 s
->sc
.crc
= s
->extra_sc
.crc
= 0xFFFFFFFF;
368 static inline int wv_check_crc(WavpackFrameContext
*s
, uint32_t crc
,
369 uint32_t crc_extra_bits
)
372 av_log(s
->avctx
, AV_LOG_ERROR
, "CRC error\n");
373 return AVERROR_INVALIDDATA
;
375 if (s
->got_extra_bits
&& crc_extra_bits
!= s
->crc_extra_bits
) {
376 av_log(s
->avctx
, AV_LOG_ERROR
, "Extra bits CRC error\n");
377 return AVERROR_INVALIDDATA
;
383 static inline int wv_unpack_stereo(WavpackFrameContext
*s
, GetBitContext
*gb
,
384 void *dst_l
, void *dst_r
, const int type
)
388 int A
, B
, L
, L2
, R
, R2
;
390 uint32_t crc
= s
->sc
.crc
;
391 uint32_t crc_extra_bits
= s
->extra_sc
.crc
;
392 int16_t *dst16_l
= dst_l
;
393 int16_t *dst16_r
= dst_r
;
394 int32_t *dst32_l
= dst_l
;
395 int32_t *dst32_r
= dst_r
;
396 float *dstfl_l
= dst_l
;
397 float *dstfl_r
= dst_r
;
399 s
->one
= s
->zero
= s
->zeroes
= 0;
401 L
= wv_get_value(s
, gb
, 0, &last
);
404 R
= wv_get_value(s
, gb
, 1, &last
);
407 for (i
= 0; i
< s
->terms
; i
++) {
408 t
= s
->decorr
[i
].value
;
412 A
= 2 * s
->decorr
[i
].samplesA
[0] - s
->decorr
[i
].samplesA
[1];
413 B
= 2 * s
->decorr
[i
].samplesB
[0] - s
->decorr
[i
].samplesB
[1];
415 A
= (3 * s
->decorr
[i
].samplesA
[0] - s
->decorr
[i
].samplesA
[1]) >> 1;
416 B
= (3 * s
->decorr
[i
].samplesB
[0] - s
->decorr
[i
].samplesB
[1]) >> 1;
418 s
->decorr
[i
].samplesA
[1] = s
->decorr
[i
].samplesA
[0];
419 s
->decorr
[i
].samplesB
[1] = s
->decorr
[i
].samplesB
[0];
422 A
= s
->decorr
[i
].samplesA
[pos
];
423 B
= s
->decorr
[i
].samplesB
[pos
];
426 if (type
!= AV_SAMPLE_FMT_S16P
) {
427 L2
= L
+ ((s
->decorr
[i
].weightA
* (int64_t)A
+ 512) >> 10);
428 R2
= R
+ ((s
->decorr
[i
].weightB
* (int64_t)B
+ 512) >> 10);
430 L2
= L
+ ((s
->decorr
[i
].weightA
* A
+ 512) >> 10);
431 R2
= R
+ ((s
->decorr
[i
].weightB
* B
+ 512) >> 10);
434 s
->decorr
[i
].weightA
-= ((((L
^ A
) >> 30) & 2) - 1) * s
->decorr
[i
].delta
;
436 s
->decorr
[i
].weightB
-= ((((R
^ B
) >> 30) & 2) - 1) * s
->decorr
[i
].delta
;
437 s
->decorr
[i
].samplesA
[j
] = L
= L2
;
438 s
->decorr
[i
].samplesB
[j
] = R
= R2
;
439 } else if (t
== -1) {
440 if (type
!= AV_SAMPLE_FMT_S16P
)
441 L2
= L
+ ((s
->decorr
[i
].weightA
* (int64_t)s
->decorr
[i
].samplesA
[0] + 512) >> 10);
443 L2
= L
+ ((s
->decorr
[i
].weightA
* s
->decorr
[i
].samplesA
[0] + 512) >> 10);
444 UPDATE_WEIGHT_CLIP(s
->decorr
[i
].weightA
, s
->decorr
[i
].delta
, s
->decorr
[i
].samplesA
[0], L
);
446 if (type
!= AV_SAMPLE_FMT_S16P
)
447 R2
= R
+ ((s
->decorr
[i
].weightB
* (int64_t)L2
+ 512) >> 10);
449 R2
= R
+ ((s
->decorr
[i
].weightB
* L2
+ 512) >> 10);
450 UPDATE_WEIGHT_CLIP(s
->decorr
[i
].weightB
, s
->decorr
[i
].delta
, L2
, R
);
452 s
->decorr
[i
].samplesA
[0] = R
;
454 if (type
!= AV_SAMPLE_FMT_S16P
)
455 R2
= R
+ ((s
->decorr
[i
].weightB
* (int64_t)s
->decorr
[i
].samplesB
[0] + 512) >> 10);
457 R2
= R
+ ((s
->decorr
[i
].weightB
* s
->decorr
[i
].samplesB
[0] + 512) >> 10);
458 UPDATE_WEIGHT_CLIP(s
->decorr
[i
].weightB
, s
->decorr
[i
].delta
, s
->decorr
[i
].samplesB
[0], R
);
462 R2
= s
->decorr
[i
].samplesA
[0];
463 s
->decorr
[i
].samplesA
[0] = R
;
466 if (type
!= AV_SAMPLE_FMT_S16P
)
467 L2
= L
+ ((s
->decorr
[i
].weightA
* (int64_t)R2
+ 512) >> 10);
469 L2
= L
+ ((s
->decorr
[i
].weightA
* R2
+ 512) >> 10);
470 UPDATE_WEIGHT_CLIP(s
->decorr
[i
].weightA
, s
->decorr
[i
].delta
, R2
, L
);
472 s
->decorr
[i
].samplesB
[0] = L
;
477 L
+= (R
-= (L
>> 1));
478 crc
= (crc
* 3 + L
) * 3 + R
;
480 if (type
== AV_SAMPLE_FMT_FLTP
) {
481 *dstfl_l
++ = wv_get_value_float(s
, &crc_extra_bits
, L
);
482 *dstfl_r
++ = wv_get_value_float(s
, &crc_extra_bits
, R
);
483 } else if (type
== AV_SAMPLE_FMT_S32P
) {
484 *dst32_l
++ = wv_get_value_integer(s
, &crc_extra_bits
, L
);
485 *dst32_r
++ = wv_get_value_integer(s
, &crc_extra_bits
, R
);
487 *dst16_l
++ = wv_get_value_integer(s
, &crc_extra_bits
, L
);
488 *dst16_r
++ = wv_get_value_integer(s
, &crc_extra_bits
, R
);
491 } while (!last
&& count
< s
->samples
);
493 wv_reset_saved_context(s
);
495 if (last
&& count
< s
->samples
) {
496 int size
= av_get_bytes_per_sample(type
);
497 memset((uint8_t*)dst_l
+ count
*size
, 0, (s
->samples
-count
)*size
);
498 memset((uint8_t*)dst_r
+ count
*size
, 0, (s
->samples
-count
)*size
);
501 if ((s
->avctx
->err_recognition
& AV_EF_CRCCHECK
) &&
502 wv_check_crc(s
, crc
, crc_extra_bits
))
503 return AVERROR_INVALIDDATA
;
508 static inline int wv_unpack_mono(WavpackFrameContext
*s
, GetBitContext
*gb
,
509 void *dst
, const int type
)
515 uint32_t crc
= s
->sc
.crc
;
516 uint32_t crc_extra_bits
= s
->extra_sc
.crc
;
517 int16_t *dst16
= dst
;
518 int32_t *dst32
= dst
;
521 s
->one
= s
->zero
= s
->zeroes
= 0;
523 T
= wv_get_value(s
, gb
, 0, &last
);
527 for (i
= 0; i
< s
->terms
; i
++) {
528 t
= s
->decorr
[i
].value
;
531 A
= 2 * s
->decorr
[i
].samplesA
[0] - s
->decorr
[i
].samplesA
[1];
533 A
= (3 * s
->decorr
[i
].samplesA
[0] - s
->decorr
[i
].samplesA
[1]) >> 1;
534 s
->decorr
[i
].samplesA
[1] = s
->decorr
[i
].samplesA
[0];
537 A
= s
->decorr
[i
].samplesA
[pos
];
540 if (type
!= AV_SAMPLE_FMT_S16P
)
541 S
= T
+ ((s
->decorr
[i
].weightA
* (int64_t)A
+ 512) >> 10);
543 S
= T
+ ((s
->decorr
[i
].weightA
* A
+ 512) >> 10);
545 s
->decorr
[i
].weightA
-= ((((T
^ A
) >> 30) & 2) - 1) * s
->decorr
[i
].delta
;
546 s
->decorr
[i
].samplesA
[j
] = T
= S
;
551 if (type
== AV_SAMPLE_FMT_FLTP
) {
552 *dstfl
++ = wv_get_value_float(s
, &crc_extra_bits
, S
);
553 } else if (type
== AV_SAMPLE_FMT_S32P
) {
554 *dst32
++ = wv_get_value_integer(s
, &crc_extra_bits
, S
);
556 *dst16
++ = wv_get_value_integer(s
, &crc_extra_bits
, S
);
559 } while (!last
&& count
< s
->samples
);
561 wv_reset_saved_context(s
);
563 if (last
&& count
< s
->samples
) {
564 int size
= av_get_bytes_per_sample(type
);
565 memset((uint8_t*)dst
+ count
*size
, 0, (s
->samples
-count
)*size
);
568 if (s
->avctx
->err_recognition
& AV_EF_CRCCHECK
) {
569 int ret
= wv_check_crc(s
, crc
, crc_extra_bits
);
570 if (ret
< 0 && s
->avctx
->err_recognition
& AV_EF_EXPLODE
)
577 static av_cold
int wv_alloc_frame_context(WavpackContext
*c
)
579 if (c
->fdec_num
== WV_MAX_FRAME_DECODERS
)
582 c
->fdec
[c
->fdec_num
] = av_mallocz(sizeof(**c
->fdec
));
583 if (!c
->fdec
[c
->fdec_num
])
586 c
->fdec
[c
->fdec_num
- 1]->avctx
= c
->avctx
;
587 wv_reset_saved_context(c
->fdec
[c
->fdec_num
- 1]);
592 static int init_thread_copy(AVCodecContext
*avctx
)
594 WavpackContext
*s
= avctx
->priv_data
;
599 static av_cold
int wavpack_decode_init(AVCodecContext
*avctx
)
601 WavpackContext
*s
= avctx
->priv_data
;
610 static av_cold
int wavpack_decode_end(AVCodecContext
*avctx
)
612 WavpackContext
*s
= avctx
->priv_data
;
615 for (i
= 0; i
< s
->fdec_num
; i
++)
616 av_freep(&s
->fdec
[i
]);
622 static int wavpack_decode_block(AVCodecContext
*avctx
, int block_no
,
623 AVFrame
*frame
, const uint8_t *buf
, int buf_size
)
625 WavpackContext
*wc
= avctx
->priv_data
;
626 ThreadFrame tframe
= { .f
= frame
};
627 WavpackFrameContext
*s
;
629 void *samples_l
= NULL
, *samples_r
= NULL
;
631 int got_terms
= 0, got_weights
= 0, got_samples
= 0,
632 got_entropy
= 0, got_bs
= 0, got_float
= 0, got_hybrid
= 0;
633 int i
, j
, id
, size
, ssize
, weights
, t
;
634 int bpp
, chan
= 0, chmask
= 0, orig_bpp
, sample_rate
= 0;
637 if (block_no
>= wc
->fdec_num
&& wv_alloc_frame_context(wc
) < 0) {
638 av_log(avctx
, AV_LOG_ERROR
, "Error creating frame decode context\n");
639 return AVERROR_INVALIDDATA
;
642 s
= wc
->fdec
[block_no
];
644 av_log(avctx
, AV_LOG_ERROR
, "Context for block %d is not present\n",
646 return AVERROR_INVALIDDATA
;
649 memset(s
->decorr
, 0, MAX_TERMS
* sizeof(Decorr
));
650 memset(s
->ch
, 0, sizeof(s
->ch
));
652 s
->and = s
->or = s
->shift
= 0;
653 s
->got_extra_bits
= 0;
655 bytestream2_init(&gb
, buf
, buf_size
);
657 s
->samples
= bytestream2_get_le32(&gb
);
658 if (s
->samples
!= wc
->samples
) {
659 av_log(avctx
, AV_LOG_ERROR
, "Mismatching number of samples in "
660 "a sequence: %d and %d\n", wc
->samples
, s
->samples
);
661 return AVERROR_INVALIDDATA
;
663 s
->frame_flags
= bytestream2_get_le32(&gb
);
664 bpp
= av_get_bytes_per_sample(avctx
->sample_fmt
);
665 orig_bpp
= ((s
->frame_flags
& 0x03) + 1) << 3;
666 multiblock
= (s
->frame_flags
& WV_SINGLE_BLOCK
) != WV_SINGLE_BLOCK
;
668 s
->stereo
= !(s
->frame_flags
& WV_MONO
);
669 s
->stereo_in
= (s
->frame_flags
& WV_FALSE_STEREO
) ? 0 : s
->stereo
;
670 s
->joint
= s
->frame_flags
& WV_JOINT_STEREO
;
671 s
->hybrid
= s
->frame_flags
& WV_HYBRID_MODE
;
672 s
->hybrid_bitrate
= s
->frame_flags
& WV_HYBRID_BITRATE
;
673 s
->post_shift
= bpp
* 8 - orig_bpp
+ ((s
->frame_flags
>> 13) & 0x1f);
674 s
->hybrid_maxclip
= ((1LL << (orig_bpp
- 1)) - 1);
675 s
->hybrid_minclip
= ((-1LL << (orig_bpp
- 1)));
676 s
->CRC
= bytestream2_get_le32(&gb
);
678 // parse metadata blocks
679 while (bytestream2_get_bytes_left(&gb
)) {
680 id
= bytestream2_get_byte(&gb
);
681 size
= bytestream2_get_byte(&gb
);
682 if (id
& WP_IDF_LONG
) {
683 size
|= (bytestream2_get_byte(&gb
)) << 8;
684 size
|= (bytestream2_get_byte(&gb
)) << 16;
686 size
<<= 1; // size is specified in words
691 av_log(avctx
, AV_LOG_ERROR
,
692 "Got incorrect block %02X with size %i\n", id
, size
);
695 if (bytestream2_get_bytes_left(&gb
) < ssize
) {
696 av_log(avctx
, AV_LOG_ERROR
,
697 "Block size %i is out of bounds\n", size
);
700 switch (id
& WP_IDF_MASK
) {
702 if (size
> MAX_TERMS
) {
703 av_log(avctx
, AV_LOG_ERROR
, "Too many decorrelation terms\n");
705 bytestream2_skip(&gb
, ssize
);
709 for (i
= 0; i
< s
->terms
; i
++) {
710 uint8_t val
= bytestream2_get_byte(&gb
);
711 s
->decorr
[s
->terms
- i
- 1].value
= (val
& 0x1F) - 5;
712 s
->decorr
[s
->terms
- i
- 1].delta
= val
>> 5;
716 case WP_ID_DECWEIGHTS
:
718 av_log(avctx
, AV_LOG_ERROR
, "No decorrelation terms met\n");
721 weights
= size
>> s
->stereo_in
;
722 if (weights
> MAX_TERMS
|| weights
> s
->terms
) {
723 av_log(avctx
, AV_LOG_ERROR
, "Too many decorrelation weights\n");
724 bytestream2_skip(&gb
, ssize
);
727 for (i
= 0; i
< weights
; i
++) {
728 t
= (int8_t)bytestream2_get_byte(&gb
);
729 s
->decorr
[s
->terms
- i
- 1].weightA
= t
<< 3;
730 if (s
->decorr
[s
->terms
- i
- 1].weightA
> 0)
731 s
->decorr
[s
->terms
- i
- 1].weightA
+=
732 (s
->decorr
[s
->terms
- i
- 1].weightA
+ 64) >> 7;
734 t
= (int8_t)bytestream2_get_byte(&gb
);
735 s
->decorr
[s
->terms
- i
- 1].weightB
= t
<< 3;
736 if (s
->decorr
[s
->terms
- i
- 1].weightB
> 0)
737 s
->decorr
[s
->terms
- i
- 1].weightB
+=
738 (s
->decorr
[s
->terms
- i
- 1].weightB
+ 64) >> 7;
743 case WP_ID_DECSAMPLES
:
745 av_log(avctx
, AV_LOG_ERROR
, "No decorrelation terms met\n");
749 for (i
= s
->terms
- 1; (i
>= 0) && (t
< size
); i
--) {
750 if (s
->decorr
[i
].value
> 8) {
751 s
->decorr
[i
].samplesA
[0] =
752 wp_exp2(bytestream2_get_le16(&gb
));
753 s
->decorr
[i
].samplesA
[1] =
754 wp_exp2(bytestream2_get_le16(&gb
));
757 s
->decorr
[i
].samplesB
[0] =
758 wp_exp2(bytestream2_get_le16(&gb
));
759 s
->decorr
[i
].samplesB
[1] =
760 wp_exp2(bytestream2_get_le16(&gb
));
764 } else if (s
->decorr
[i
].value
< 0) {
765 s
->decorr
[i
].samplesA
[0] =
766 wp_exp2(bytestream2_get_le16(&gb
));
767 s
->decorr
[i
].samplesB
[0] =
768 wp_exp2(bytestream2_get_le16(&gb
));
771 for (j
= 0; j
< s
->decorr
[i
].value
; j
++) {
772 s
->decorr
[i
].samplesA
[j
] =
773 wp_exp2(bytestream2_get_le16(&gb
));
775 s
->decorr
[i
].samplesB
[j
] =
776 wp_exp2(bytestream2_get_le16(&gb
));
779 t
+= s
->decorr
[i
].value
* 2 * (s
->stereo_in
+ 1);
785 if (size
!= 6 * (s
->stereo_in
+ 1)) {
786 av_log(avctx
, AV_LOG_ERROR
,
787 "Entropy vars size should be %i, got %i.\n",
788 6 * (s
->stereo_in
+ 1), size
);
789 bytestream2_skip(&gb
, ssize
);
792 for (j
= 0; j
<= s
->stereo_in
; j
++)
793 for (i
= 0; i
< 3; i
++) {
794 s
->ch
[j
].median
[i
] = wp_exp2(bytestream2_get_le16(&gb
));
799 if (s
->hybrid_bitrate
) {
800 for (i
= 0; i
<= s
->stereo_in
; i
++) {
801 s
->ch
[i
].slow_level
= wp_exp2(bytestream2_get_le16(&gb
));
805 for (i
= 0; i
< (s
->stereo_in
+ 1); i
++) {
806 s
->ch
[i
].bitrate_acc
= bytestream2_get_le16(&gb
) << 16;
810 for (i
= 0; i
< (s
->stereo_in
+ 1); i
++) {
811 s
->ch
[i
].bitrate_delta
=
812 wp_exp2((int16_t)bytestream2_get_le16(&gb
));
815 for (i
= 0; i
< (s
->stereo_in
+ 1); i
++)
816 s
->ch
[i
].bitrate_delta
= 0;
820 case WP_ID_INT32INFO
: {
823 av_log(avctx
, AV_LOG_ERROR
,
824 "Invalid INT32INFO, size = %i\n",
826 bytestream2_skip(&gb
, ssize
- 4);
829 bytestream2_get_buffer(&gb
, val
, 4);
831 s
->extra_bits
= val
[0];
841 /* original WavPack decoder forces 32-bit lossy sound to be treated
842 * as 24-bit one in order to have proper clipping */
843 if (s
->hybrid
&& bpp
== 4 && s
->post_shift
< 8 && s
->shift
> 8) {
846 s
->hybrid_maxclip
>>= 8;
847 s
->hybrid_minclip
>>= 8;
851 case WP_ID_FLOATINFO
:
853 av_log(avctx
, AV_LOG_ERROR
,
854 "Invalid FLOATINFO, size = %i\n", size
);
855 bytestream2_skip(&gb
, ssize
);
858 s
->float_flag
= bytestream2_get_byte(&gb
);
859 s
->float_shift
= bytestream2_get_byte(&gb
);
860 s
->float_max_exp
= bytestream2_get_byte(&gb
);
862 bytestream2_skip(&gb
, 1);
865 s
->sc
.offset
= bytestream2_tell(&gb
);
866 s
->sc
.size
= size
* 8;
867 if ((ret
= init_get_bits8(&s
->gb
, gb
.buffer
, size
)) < 0)
869 s
->data_size
= size
* 8;
870 bytestream2_skip(&gb
, size
);
873 case WP_ID_EXTRABITS
:
875 av_log(avctx
, AV_LOG_ERROR
, "Invalid EXTRABITS, size = %i\n",
877 bytestream2_skip(&gb
, size
);
880 s
->extra_sc
.offset
= bytestream2_tell(&gb
);
881 s
->extra_sc
.size
= size
* 8;
882 if ((ret
= init_get_bits8(&s
->gb_extra_bits
, gb
.buffer
, size
)) < 0)
884 s
->crc_extra_bits
= get_bits_long(&s
->gb_extra_bits
, 32);
885 bytestream2_skip(&gb
, size
);
886 s
->got_extra_bits
= 1;
890 av_log(avctx
, AV_LOG_ERROR
,
891 "Insufficient channel information\n");
892 return AVERROR_INVALIDDATA
;
894 chan
= bytestream2_get_byte(&gb
);
897 chmask
= bytestream2_get_byte(&gb
);
900 chmask
= bytestream2_get_le16(&gb
);
903 chmask
= bytestream2_get_le24(&gb
);
906 chmask
= bytestream2_get_le32(&gb
);
909 size
= bytestream2_get_byte(&gb
);
910 if (avctx
->channels
!= size
)
911 av_log(avctx
, AV_LOG_WARNING
, "%i channels signalled"
912 " instead of %i.\n", size
, avctx
->channels
);
913 chan
|= (bytestream2_get_byte(&gb
) & 0xF) << 8;
914 chmask
= bytestream2_get_le16(&gb
);
917 av_log(avctx
, AV_LOG_ERROR
, "Invalid channel info size %d\n",
919 chan
= avctx
->channels
;
920 chmask
= avctx
->channel_layout
;
923 case WP_ID_SAMPLE_RATE
:
925 av_log(avctx
, AV_LOG_ERROR
, "Invalid custom sample rate.\n");
926 return AVERROR_INVALIDDATA
;
928 sample_rate
= bytestream2_get_le24(&gb
);
931 bytestream2_skip(&gb
, size
);
934 bytestream2_skip(&gb
, 1);
938 av_log(avctx
, AV_LOG_ERROR
, "No block with decorrelation terms\n");
939 return AVERROR_INVALIDDATA
;
942 av_log(avctx
, AV_LOG_ERROR
, "No block with decorrelation weights\n");
943 return AVERROR_INVALIDDATA
;
946 av_log(avctx
, AV_LOG_ERROR
, "No block with decorrelation samples\n");
947 return AVERROR_INVALIDDATA
;
950 av_log(avctx
, AV_LOG_ERROR
, "No block with entropy info\n");
951 return AVERROR_INVALIDDATA
;
953 if (s
->hybrid
&& !got_hybrid
) {
954 av_log(avctx
, AV_LOG_ERROR
, "Hybrid config not found\n");
955 return AVERROR_INVALIDDATA
;
958 av_log(avctx
, AV_LOG_ERROR
, "Packed samples not found\n");
959 return AVERROR_INVALIDDATA
;
961 if (!got_float
&& avctx
->sample_fmt
== AV_SAMPLE_FMT_FLTP
) {
962 av_log(avctx
, AV_LOG_ERROR
, "Float information not found\n");
963 return AVERROR_INVALIDDATA
;
965 if (s
->got_extra_bits
&& avctx
->sample_fmt
!= AV_SAMPLE_FMT_FLTP
) {
966 const int size
= get_bits_left(&s
->gb_extra_bits
);
967 const int wanted
= s
->samples
* s
->extra_bits
<< s
->stereo_in
;
969 av_log(avctx
, AV_LOG_ERROR
, "Too small EXTRABITS\n");
970 s
->got_extra_bits
= 0;
974 if (!wc
->ch_offset
) {
975 int sr
= (s
->frame_flags
>> 23) & 0xf;
978 av_log(avctx
, AV_LOG_ERROR
, "Custom sample rate missing.\n");
979 return AVERROR_INVALIDDATA
;
981 avctx
->sample_rate
= sample_rate
;
983 avctx
->sample_rate
= wv_rates
[sr
];
987 avctx
->channels
= chan
;
989 avctx
->channel_layout
= chmask
;
991 avctx
->channels
= s
->stereo
? 2 : 1;
992 avctx
->channel_layout
= s
->stereo
? AV_CH_LAYOUT_STEREO
:
996 /* get output buffer */
997 frame
->nb_samples
= s
->samples
+ 1;
998 if ((ret
= ff_thread_get_buffer(avctx
, &tframe
, 0)) < 0)
1000 frame
->nb_samples
= s
->samples
;
1003 if (wc
->ch_offset
+ s
->stereo
>= avctx
->channels
) {
1004 av_log(avctx
, AV_LOG_WARNING
, "Too many channels coded in a packet.\n");
1005 return (avctx
->err_recognition
& AV_EF_EXPLODE
) ? AVERROR_INVALIDDATA
: 0;
1008 samples_l
= frame
->extended_data
[wc
->ch_offset
];
1010 samples_r
= frame
->extended_data
[wc
->ch_offset
+ 1];
1012 wc
->ch_offset
+= 1 + s
->stereo
;
1015 ret
= wv_unpack_stereo(s
, &s
->gb
, samples_l
, samples_r
, avctx
->sample_fmt
);
1019 ret
= wv_unpack_mono(s
, &s
->gb
, samples_l
, avctx
->sample_fmt
);
1024 memcpy(samples_r
, samples_l
, bpp
* s
->samples
);
1030 static void wavpack_decode_flush(AVCodecContext
*avctx
)
1032 WavpackContext
*s
= avctx
->priv_data
;
1035 for (i
= 0; i
< s
->fdec_num
; i
++)
1036 wv_reset_saved_context(s
->fdec
[i
]);
1039 static int wavpack_decode_frame(AVCodecContext
*avctx
, void *data
,
1040 int *got_frame_ptr
, AVPacket
*avpkt
)
1042 WavpackContext
*s
= avctx
->priv_data
;
1043 const uint8_t *buf
= avpkt
->data
;
1044 int buf_size
= avpkt
->size
;
1045 AVFrame
*frame
= data
;
1046 int frame_size
, ret
, frame_flags
;
1048 if (avpkt
->size
<= WV_HEADER_SIZE
)
1049 return AVERROR_INVALIDDATA
;
1054 /* determine number of samples */
1055 s
->samples
= AV_RL32(buf
+ 20);
1056 frame_flags
= AV_RL32(buf
+ 24);
1057 if (s
->samples
<= 0 || s
->samples
> WV_MAX_SAMPLES
) {
1058 av_log(avctx
, AV_LOG_ERROR
, "Invalid number of samples: %d\n",
1060 return AVERROR_INVALIDDATA
;
1063 if (frame_flags
& 0x80) {
1064 avctx
->sample_fmt
= AV_SAMPLE_FMT_FLTP
;
1065 } else if ((frame_flags
& 0x03) <= 1) {
1066 avctx
->sample_fmt
= AV_SAMPLE_FMT_S16P
;
1068 avctx
->sample_fmt
= AV_SAMPLE_FMT_S32P
;
1069 avctx
->bits_per_raw_sample
= ((frame_flags
& 0x03) + 1) << 3;
1072 while (buf_size
> 0) {
1073 if (buf_size
<= WV_HEADER_SIZE
)
1075 frame_size
= AV_RL32(buf
+ 4) - 12;
1078 if (frame_size
<= 0 || frame_size
> buf_size
) {
1079 av_log(avctx
, AV_LOG_ERROR
,
1080 "Block %d has invalid size (size %d vs. %d bytes left)\n",
1081 s
->block
, frame_size
, buf_size
);
1082 wavpack_decode_flush(avctx
);
1083 return AVERROR_INVALIDDATA
;
1085 if ((ret
= wavpack_decode_block(avctx
, s
->block
,
1086 frame
, buf
, frame_size
)) < 0) {
1087 wavpack_decode_flush(avctx
);
1092 buf_size
-= frame_size
;
1095 if (s
->ch_offset
!= avctx
->channels
) {
1096 av_log(avctx
, AV_LOG_ERROR
, "Not enough channels coded in a packet.\n");
1097 return AVERROR_INVALIDDATA
;
1105 AVCodec ff_wavpack_decoder
= {
1107 .long_name
= NULL_IF_CONFIG_SMALL("WavPack"),
1108 .type
= AVMEDIA_TYPE_AUDIO
,
1109 .id
= AV_CODEC_ID_WAVPACK
,
1110 .priv_data_size
= sizeof(WavpackContext
),
1111 .init
= wavpack_decode_init
,
1112 .close
= wavpack_decode_end
,
1113 .decode
= wavpack_decode_frame
,
1114 .flush
= wavpack_decode_flush
,
1115 .init_thread_copy
= ONLY_IF_THREADS_ENABLED(init_thread_copy
),
1116 .capabilities
= CODEC_CAP_DR1
| CODEC_CAP_FRAME_THREADS
,