26 #define BITSTREAM_READER_LE 41 #define DSD_BYTE_READY(low,high) (!(((low) ^ (high)) & 0xff000000)) 44 #define PTABLE_BINS (1<<PTABLE_BITS) 45 #define PTABLE_MASK (PTABLE_BINS-1) 48 #define DOWN 0x00010000 52 #define VALUE_ONE (1 << PRECISION) 53 #define PRECISION_USE 12 57 #define MAX_HISTORY_BITS 5 58 #define MAX_HISTORY_BINS (1 << MAX_HISTORY_BITS) 59 #define MAX_BIN_BYTES 1280 // for value_lookup, per bin (2k - 512 - 256) 98 #define WV_MAX_FRAME_DECODERS 14 119 #define LEVEL_DECAY(a) (((a) + 0x80) >> 8) 128 e = (1 << (p + 1)) - k - 1;
147 int balance = (sl[1] - sl[0] + br[1] + 1) >> 1;
148 if (balance > br[0]) {
151 }
else if (-balance > br[0]) {
155 br[1] = br[0] + balance;
156 br[0] = br[0] - balance;
161 if (sl[i] - br[i] > -0x100)
177 int sign,
base, add, ret;
240 if (ctx->
hybrid && !channel) {
268 if (add >= 0x2000000U) {
276 int mid = (base * 2
U + add + 1) >> 1;
281 add -= (mid - (unsigned)base);
284 add = mid - (unsigned)base - 1;
285 mid = (base * 2
U + add + 1) >> 1;
292 return sign ? ~ret : ret;
314 *crc = *crc * 9 + (S & 0xffff) * 3 + ((
unsigned)S >> 16);
318 bit = (S & s->
and) | s->
or;
319 bit = ((S + bit) << s->
shift) - bit;
338 const int max_bits = 1 + 23 + 8 + 1;
350 if (S >= 0x1000000U) {
369 S |= (1 <<
shift) - 1;
395 *crc = *crc * 27 + S * 9 + exp * 3 + sign;
397 value.u = (sign << 31) | (exp << 23) |
S;
418 int value = 0x808000, rate = rate_i << 8;
420 for (
int c = (rate + 128) >> 8;
c--;)
427 if (value > 0x010000) {
428 rate += (rate * rate_s + 128) >> 8;
430 for (
int c = (rate + 64) >> 7;
c--;)
444 uint8_t *dst_l = dst_left, *dst_r = dst_right;
448 uint32_t low, high,
value;
453 rate_i = bytestream2_get_byte(&s->
gbyte);
454 rate_s = bytestream2_get_byte(&s->
gbyte);
470 sp->
factor = bytestream2_get_byte(&s->
gbyte) & 0xff;
471 sp->
factor |= (bytestream2_get_byte(&s->
gbyte) << 8) & 0xff00;
475 value = bytestream2_get_be32(&s->
gbyte);
479 while (total_samples--) {
489 uint32_t
split = low + ((high - low) >> 8) * (*pp >> 16);
491 if (value <= split) {
502 value = (value << 8) | bytestream2_get_byte(&s->
gbyte);
503 high = (high << 8) | 0xff;
508 sp[0].
byte = (sp[0].
byte << 1) | (sp[0].fltr0 & 1);
510 ((sp[0].value ^ (sp[0].value - (sp[0].fltr6 * 16))) >> 31);
524 split = low + ((high - low) >> 8) * (*pp >> 16);
526 if (value <= split) {
537 value = (value << 8) | bytestream2_get_byte(&s->
gbyte);
538 high = (high << 8) | 0xff;
543 sp[1].
byte = (sp[1].
byte << 1) | (sp[1].fltr0 & 1);
545 ((sp[1].value ^ (sp[1].value - (sp[1].fltr6 * 16))) >> 31);
556 checksum += (checksum << 1) + (*dst_l = sp[0].byte & 0xff);
561 checksum += (checksum << 1) + (*dst_r = filters[1].byte & 0xff);
571 memset(dst_left, 0x69, s->
samples * 4);
574 memset(dst_right, 0x69, s->
samples * 4);
582 uint8_t *dst_l = dst_left, *dst_r = dst_right;
583 uint8_t history_bits, max_probability;
584 int total_summed_probabilities = 0;
585 int total_samples = s->
samples;
587 int history_bins, p0, p1, chan;
589 uint32_t low, high,
value;
594 history_bits = bytestream2_get_byte(&s->
gbyte);
599 history_bins = 1 << history_bits;
600 max_probability = bytestream2_get_byte(&s->
gbyte);
602 if (max_probability < 0xff) {
607 int code = bytestream2_get_byte(&s->
gbyte);
609 if (code > max_probability) {
610 int zcount = code - max_probability;
612 while (outptr < outend && zcount--)
622 if (outptr < outend ||
632 for (p0 = 0; p0 < history_bins; p0++) {
635 for (
int i = 0;
i < 256;
i++)
639 total_summed_probabilities += sum_values;
641 if (total_summed_probabilities > history_bins *
MAX_BIN_BYTES)
646 for (
int i = 0;
i < 256;
i++) {
659 low = 0; high = 0xffffffff;
660 value = bytestream2_get_be32(&s->
gbyte);
665 while (total_samples--) {
675 value = bytestream2_get_be32(&s->
gbyte);
685 index = (value - low) / mult;
712 checksum += (checksum << 1) + code;
715 p0 = code & (history_bins-1);
718 p1 = code & (history_bins-1);
722 value = (value << 8) | bytestream2_get_byte(&s->
gbyte);
723 high = (high << 8) | 0xff;
732 memset(dst_left, 0x69, s->
samples * 4);
735 memset(dst_right, 0x69, s->
samples * 4);
743 uint8_t *dst_l = dst_left, *dst_r = dst_right;
744 int total_samples = s->
samples;
750 while (total_samples--) {
751 checksum += (checksum << 1) + (*dst_l = bytestream2_get_byte(&s->
gbyte));
755 checksum += (checksum << 1) + (*dst_r = bytestream2_get_byte(&s->
gbyte));
764 memset(dst_left, 0x69, s->
samples * 4);
767 memset(dst_right, 0x69, s->
samples * 4);
774 void *dst_l,
void *dst_r,
const int type)
780 uint32_t crc = 0xFFFFFFFF;
782 int16_t *dst16_l = dst_l;
783 int16_t *dst16_r = dst_r;
786 float *dstfl_l = dst_l;
787 float *dstfl_r = dst_r;
797 for (i = 0; i < s->
terms; i++) {
820 L2 = L + (unsigned)((
int)(s->
decorr[
i].
weightA * (unsigned)A + 512) >> 10);
821 R2 = R + (unsigned)((
int)(s->
decorr[
i].
weightB * (unsigned)B + 512) >> 10);
829 }
else if (t == -1) {
839 R2 = R + (unsigned)((
int)(s->
decorr[
i].
weightB * (unsigned)L2 + 512) >> 10);
859 L2 = L + (unsigned)((
int)(s->
decorr[
i].
weightA * (unsigned)R2 + 512) >> 10);
867 if (
FFABS((int64_t)L) +
FFABS((int64_t)R) > (1<<19)) {
875 L += (unsigned)(R -= (
unsigned)(L >> 1));
876 crc = (crc * 3 +
L) * 3 + R;
889 }
while (!last && count < s->
samples);
891 if (last && count < s->samples) {
905 void *dst,
const int type)
911 uint32_t crc = 0xFFFFFFFF;
913 int16_t *dst16 = dst;
923 for (i = 0; i < s->
terms; i++) {
939 S = T + (unsigned)((
int)(s->
decorr[
i].
weightA * (unsigned)A + 512) >> 10);
955 }
while (!last && count < s->
samples);
957 if (last && count < s->samples) {
996 if (channels > INT_MAX /
sizeof(*s->
dsdctx))
1081 const uint8_t *buf,
int buf_size)
1087 void *samples_l =
NULL, *samples_r =
NULL;
1089 int got_terms = 0, got_weights = 0, got_samples = 0,
1090 got_entropy = 0, got_pcm = 0, got_float = 0, got_hybrid = 0;
1092 int i, j,
id,
size, ssize, weights, t;
1093 int bpp, chan = 0, orig_bpp,
sample_rate = 0, rate_x = 1, dsd_mode = 0;
1095 uint64_t chmask = 0;
1102 s = wc->
fdec[block_no];
1110 memset(s->
ch, 0,
sizeof(s->
ch));
1117 s->
samples = bytestream2_get_le32(&gb);
1150 s->
CRC = bytestream2_get_le32(&gb);
1154 id = bytestream2_get_byte(&gb);
1155 size = bytestream2_get_byte(&gb);
1157 size |= (bytestream2_get_le16u(&gb)) << 8;
1164 "Got incorrect block %02X with size %i\n",
id, size);
1169 "Block size %i is out of bounds\n", size);
1181 for (i = 0; i < s->
terms; i++) {
1199 for (i = 0; i < weights; i++) {
1200 t = (int8_t)bytestream2_get_byte(&gb);
1206 t = (int8_t)bytestream2_get_byte(&gb);
1221 for (i = s->
terms - 1; (i >= 0) && (t < size); i--) {
1224 wp_exp2(bytestream2_get_le16(&gb));
1226 wp_exp2(bytestream2_get_le16(&gb));
1230 wp_exp2(bytestream2_get_le16(&gb));
1232 wp_exp2(bytestream2_get_le16(&gb));
1238 wp_exp2(bytestream2_get_le16(&gb));
1240 wp_exp2(bytestream2_get_le16(&gb));
1245 wp_exp2(bytestream2_get_le16(&gb));
1248 wp_exp2(bytestream2_get_le16(&gb));
1259 "Entropy vars size should be %i, got %i.\n",
1265 for (i = 0; i < 3; i++) {
1277 for (i = 0; i < (s->
stereo_in + 1); i++) {
1282 for (i = 0; i < (s->
stereo_in + 1); i++) {
1284 wp_exp2((int16_t)bytestream2_get_le16(&gb));
1287 for (i = 0; i < (s->
stereo_in + 1); i++)
1296 "Invalid INT32INFO, size = %i\n",
1304 "Invalid INT32INFO, extra_bits = %d (> 30)\n", val[0]);
1306 }
else if (val[0]) {
1308 }
else if (val[1]) {
1310 }
else if (val[2]) {
1313 }
else if (val[3]) {
1317 if (s->
shift > 31) {
1319 "Invalid INT32INFO, shift = %d (> 31)\n", s->
shift);
1336 "Invalid FLOATINFO, size = %i\n", size);
1345 "Invalid FLOATINFO, shift = %d (> 31)\n", s->
float_shift);
1365 rate_x = bytestream2_get_byte(&gb);
1368 rate_x = 1 << rate_x;
1369 dsd_mode = bytestream2_get_byte(&gb);
1370 if (dsd_mode && dsd_mode != 1 && dsd_mode != 3) {
1395 "Insufficient channel information\n");
1398 chan = bytestream2_get_byte(&gb);
1401 chmask = bytestream2_get_byte(&gb);
1404 chmask = bytestream2_get_le16(&gb);
1407 chmask = bytestream2_get_le24(&gb);
1410 chmask = bytestream2_get_le32(&gb);
1413 size = bytestream2_get_byte(&gb);
1414 chan |= (bytestream2_get_byte(&gb) & 0xF) << 8;
1418 " instead of %i.\n", chan, avctx->
channels);
1419 chmask = bytestream2_get_le24(&gb);
1422 size = bytestream2_get_byte(&gb);
1423 chan |= (bytestream2_get_byte(&gb) & 0xF) << 8;
1427 " instead of %i.\n", chan, avctx->
channels);
1428 chmask = bytestream2_get_le32(&gb);
1442 sample_rate = bytestream2_get_le24(&gb);
1447 if (
id & WP_IDF_ODD)
1468 if (s->
hybrid && !got_hybrid) {
1479 if (size < wanted) {
1486 if (!got_pcm && !got_dsd) {
1498 int new_channels = avctx->
channels;
1511 if (new_samplerate * (uint64_t)rate_x > INT_MAX)
1513 new_samplerate *= rate_x;
1517 new_channels = chan;
1519 new_chmask = chmask;
1521 new_channels = s->
stereo ? 2 : 1;
1536 !!got_dsd != !!wc->
dsdctx) {
1575 if (dsd_mode == 3) {
1577 }
else if (dsd_mode == 1) {
1589 if (dsd_mode == 3) {
1591 }
else if (dsd_mode == 1) {
1603 memcpy(samples_r, samples_l, bpp * s->
samples);
1629 int *got_frame_ptr,
AVPacket *avpkt)
1633 int buf_size = avpkt->
size;
1645 frame_flags =
AV_RL32(buf + 24);
1655 frame_size =
AV_RL32(buf + 4) - 12;
1658 if (frame_size <= 0 || frame_size > buf_size) {
1660 "Block %d has invalid size (size %d vs. %d bytes left)\n",
1661 s->
block, frame_size, buf_size);
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
void av_buffer_unref(AVBufferRef **buf)
Free a given reference and automatically free the buffer if there are no more references to it...
This structure describes decoded (raw) audio or video data.
static void flush(AVCodecContext *avctx)
static void wavpack_decode_flush(AVCodecContext *avctx)
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
#define AV_LOG_WARNING
Something somehow does not look correct.
static int wv_dsd_reset(WavpackContext *s, int channels)
static av_cold int init(AVCodecContext *avctx)
static int wavpack_decode_block(AVCodecContext *avctx, int block_no, const uint8_t *buf, int buf_size)
static av_always_inline void bytestream2_init(GetByteContext *g, const uint8_t *buf, int buf_size)
#define WV_FLT_SHIFT_ONES
static int dsd_channel(AVCodecContext *avctx, void *frmptr, int jobnr, int threadnr)
#define WV_HYBRID_BITRATE
int bits_per_raw_sample
Bits per sample/pixel of internal libavcodec pixel/sample format.
#define AV_CH_LAYOUT_STEREO
static void error(const char *err)
void ff_thread_await_progress(ThreadFrame *f, int n, int field)
Wait for earlier decoding threads to finish reference pictures.
av_cold void ff_init_dsd_data(void)
static void decode(AVCodecContext *dec_ctx, AVPacket *pkt, AVFrame *frame, FILE *outfile)
int av_get_channel_layout_nb_channels(uint64_t channel_layout)
Return the number of channels in the channel layout.
static int get_unary_0_33(GetBitContext *gb)
Get unary code terminated by a 0 with a maximum length of 33.
enum AVSampleFormat sample_fmt
audio sample format
static int wv_unpack_dsd_high(WavpackFrameContext *s, uint8_t *dst_left, uint8_t *dst_right)
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
AVCodec ff_wavpack_decoder
void ff_dsd2pcm_translate(DSDContext *s, size_t samples, int lsbf, const uint8_t *src, ptrdiff_t src_stride, float *dst, ptrdiff_t dst_stride)
Multithreading support functions.
int av_frame_ref(AVFrame *dst, const AVFrame *src)
Set up a new reference to the data described by the source frame.
#define u(width, name, range_min, range_max)
int ff_thread_ref_frame(ThreadFrame *dst, ThreadFrame *src)
bitstream reader API header.
void ff_thread_finish_setup(AVCodecContext *avctx)
If the codec defines update_thread_context(), call this when they are ready for the next thread to st...
static av_always_inline int wp_log2(uint32_t val)
static const uint16_t table[]
#define FF_CODEC_CAP_ALLOCATE_PROGRESS
WavpackFrameContext * fdec[WV_MAX_FRAME_DECODERS]
static av_always_inline int bytestream2_get_bytes_left(GetByteContext *g)
uint8_t probabilities[MAX_HISTORY_BINS][256]
static int wv_unpack_dsd_copy(WavpackFrameContext *s, uint8_t *dst_left, uint8_t *dst_right)
static int get_bits_left(GetBitContext *gb)
static int update_error_limit(WavpackFrameContext *ctx)
#define i(width, name, range_min, range_max)
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
void ff_thread_release_buffer(AVCodecContext *avctx, ThreadFrame *f)
Wrapper around release_buffer() frame-for multithreaded codecs.
#define DSD_BYTE_READY(low, high)
static av_always_inline void bytestream2_skip(GetByteContext *g, unsigned int size)
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
static av_always_inline int wp_exp2(int16_t val)
static av_always_inline unsigned int bytestream2_get_buffer(GetByteContext *g, uint8_t *dst, unsigned int size)
uint8_t * value_lookup[MAX_HISTORY_BINS]
void * av_mallocz(size_t size)
Allocate a memory block with alignment suitable for all memory accesses (including vectors if availab...
const char * name
Name of the codec implementation.
#define AV_CODEC_CAP_FRAME_THREADS
Codec supports frame-level multithreading.
uint64_t channel_layout
Audio channel layout.
static char * split(char *message, char delim)
#define ONLY_IF_THREADS_ENABLED(x)
Define a function with only the non-default version specified.
audio channel layout utility functions
static int16_t mult(Float11 *f1, Float11 *f2)
int err_recognition
Error recognition; may misdetect some more or less valid parts as errors.
static int wv_unpack_stereo(WavpackFrameContext *s, GetBitContext *gb, void *dst_l, void *dst_r, const int type)
static const int wv_rates[16]
static int wv_unpack_mono(WavpackFrameContext *s, GetBitContext *gb, void *dst, const int type)
void ff_thread_report_progress(ThreadFrame *f, int n, int field)
Notify later decoding threads when part of their reference picture is ready.
#define WV_FLT_SHIFT_SAME
#define FFABS(a)
Absolute value, Note, INT_MIN / INT64_MIN result in undefined behavior as they are not representable ...
#define AV_EF_EXPLODE
abort decoding on minor error detection
#define WV_FLT_SHIFT_SENT
static volatile int checksum
#define AV_CODEC_CAP_SLICE_THREADS
Codec supports slice-based (or partition-based) multithreading.
int(* execute2)(struct AVCodecContext *c, int(*func)(struct AVCodecContext *c2, void *arg, int jobnr, int threadnr), void *arg2, int *ret, int count)
The codec may call this to execute several independent things.
static av_cold int wavpack_decode_end(AVCodecContext *avctx)
Libavcodec external API header.
AVSampleFormat
Audio sample formats.
int sample_rate
samples per second
static int init_get_bits8(GetBitContext *s, const uint8_t *buffer, int byte_size)
Initialize GetBitContext.
int ff_thread_get_buffer(AVCodecContext *avctx, ThreadFrame *f, int flags)
Wrapper around get_buffer() for frame-multithreaded codecs.
main external API structure.
uint8_t * data
The data buffer.
static float wv_get_value_float(WavpackFrameContext *s, uint32_t *crc, int S)
static int wv_get_value_integer(WavpackFrameContext *s, uint32_t *crc, unsigned S)
AVBufferRef * av_buffer_allocz(int size)
Same as av_buffer_alloc(), except the returned buffer will be initialized to zero.
static unsigned int get_bits1(GetBitContext *s)
static void init_ptable(int *table, int rate_i, int rate_s)
#define AV_EF_CRCCHECK
Verify checksums embedded in the bitstream (could be of either encoded or decoded data...
refcounted data buffer API
static const int factor[16]
static int wavpack_decode_frame(AVCodecContext *avctx, void *data, int *got_frame_ptr, AVPacket *avpkt)
static int wv_check_crc(WavpackFrameContext *s, uint32_t crc, uint32_t crc_extra_bits)
static unsigned int get_bits_long(GetBitContext *s, int n)
Read 0-32 bits.
static int wv_get_value(WavpackFrameContext *ctx, GetBitContext *gb, int channel, int *last)
#define UPDATE_WEIGHT_CLIP(weight, delta, samples, in)
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
int av_get_bytes_per_sample(enum AVSampleFormat sample_fmt)
Return number of bytes per sample.
static av_always_inline unsigned get_tail(GetBitContext *gb, int k)
A reference to a data buffer.
static int wv_unpack_dsd_fast(WavpackFrameContext *s, uint8_t *dst_left, uint8_t *dst_right)
common internal api header.
#define bit(string, value)
channel
Use these values when setting the channel map with ebur128_set_channel().
AVBufferRef * av_buffer_ref(AVBufferRef *buf)
Create a new reference to an AVBuffer.
#define AV_INPUT_BUFFER_PADDING_SIZE
Required number of additionally allocated bytes at the end of the input bitstream for decoding...
GetBitContext gb_extra_bits
#define xf(width, name, var, range_min, range_max, subs,...)
int channels
number of audio channels
static const struct PPFilter filters[]
uint16_t summed_probabilities[MAX_HISTORY_BINS][256]
static av_cold int wv_alloc_frame_context(WavpackContext *c)
uint8_t value_lookup_buffer[MAX_HISTORY_BINS *MAX_BIN_BYTES]
static av_cold int wavpack_decode_init(AVCodecContext *avctx)
#define FFSWAP(type, a, b)
#define WV_MAX_FRAME_DECODERS
uint8_t ** extended_data
pointers to the data planes/channels.
#define AV_CH_LAYOUT_MONO
static double val(void *priv, double ch)
This structure stores compressed data.
#define AV_GET_BUFFER_FLAG_REF
The decoder will keep a reference to the frame and may reuse it later.
int nb_samples
number of audio samples (per channel) described by this frame
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() for allocating buffers and supports custom allocators.
static av_always_inline int get_bitsz(GetBitContext *s, int n)
Read 0-25 bits.