static inline uint64_t get_val(BitstreamContext *bc, unsigned n)
{
#ifdef BITSTREAM_READER_LE
    uint64_t ret = bc->bits & ((UINT64_C(1) << n) - 1);
    bc->bits >>= n;
#else
    uint64_t ret = bc->bits >> (64 - n);
    bc->bits <<= n;
#endif
    bc->bits_left -= n;
    return ret;
}
['static inline int wv_unpack_stereo(WavpackFrameContext *s, BitstreamContext *bc,\n                                   void *dst_l, void *dst_r, const int type)\n{\n    int i, j, count = 0;\n    int last, t;\n    int A, B, L, L2, R, R2;\n    int pos                 = s->pos;\n    uint32_t crc            = s->sc.crc;\n    uint32_t crc_extra_bits = s->extra_sc.crc;\n    int16_t *dst16_l        = dst_l;\n    int16_t *dst16_r        = dst_r;\n    int32_t *dst32_l        = dst_l;\n    int32_t *dst32_r        = dst_r;\n    float *dstfl_l          = dst_l;\n    float *dstfl_r          = dst_r;\n    s->one = s->zero = s->zeroes = 0;\n    do {\n        L = wv_get_value(s, bc, 0, &last);\n        if (last)\n            break;\n        R = wv_get_value(s, bc, 1, &last);\n        if (last)\n            break;\n        for (i = 0; i < s->terms; i++) {\n            t = s->decorr[i].value;\n            if (t > 0) {\n                if (t > 8) {\n                    if (t & 1) {\n                        A = 2 * s->decorr[i].samplesA[0] - s->decorr[i].samplesA[1];\n                        B = 2 * s->decorr[i].samplesB[0] - s->decorr[i].samplesB[1];\n                    } else {\n                        A = (3 * s->decorr[i].samplesA[0] - s->decorr[i].samplesA[1]) >> 1;\n                        B = (3 * s->decorr[i].samplesB[0] - s->decorr[i].samplesB[1]) >> 1;\n                    }\n                    s->decorr[i].samplesA[1] = s->decorr[i].samplesA[0];\n                    s->decorr[i].samplesB[1] = s->decorr[i].samplesB[0];\n                    j                        = 0;\n                } else {\n                    A = s->decorr[i].samplesA[pos];\n                    B = s->decorr[i].samplesB[pos];\n                    j = (pos + t) & 7;\n                }\n                if (type != AV_SAMPLE_FMT_S16P) {\n                    L2 = L + ((s->decorr[i].weightA * (int64_t)A + 512) >> 10);\n                    R2 = R + ((s->decorr[i].weightB * (int64_t)B + 512) >> 10);\n                } else {\n                    L2 = L + ((s->decorr[i].weightA * A + 512) >> 10);\n                    R2 = R + ((s->decorr[i].weightB * B + 512) >> 10);\n                }\n                if (A && L)\n                    s->decorr[i].weightA -= ((((L ^ A) >> 30) & 2) - 1) * s->decorr[i].delta;\n                if (B && R)\n                    s->decorr[i].weightB -= ((((R ^ B) >> 30) & 2) - 1) * s->decorr[i].delta;\n                s->decorr[i].samplesA[j] = L = L2;\n                s->decorr[i].samplesB[j] = R = R2;\n            } else if (t == -1) {\n                if (type != AV_SAMPLE_FMT_S16P)\n                    L2 = L + ((s->decorr[i].weightA * (int64_t)s->decorr[i].samplesA[0] + 512) >> 10);\n                else\n                    L2 = L + ((s->decorr[i].weightA * s->decorr[i].samplesA[0] + 512) >> 10);\n                UPDATE_WEIGHT_CLIP(s->decorr[i].weightA, s->decorr[i].delta, s->decorr[i].samplesA[0], L);\n                L = L2;\n                if (type != AV_SAMPLE_FMT_S16P)\n                    R2 = R + ((s->decorr[i].weightB * (int64_t)L2 + 512) >> 10);\n                else\n                    R2 = R + ((s->decorr[i].weightB * L2 + 512) >> 10);\n                UPDATE_WEIGHT_CLIP(s->decorr[i].weightB, s->decorr[i].delta, L2, R);\n                R                        = R2;\n                s->decorr[i].samplesA[0] = R;\n            } else {\n                if (type != AV_SAMPLE_FMT_S16P)\n                    R2 = R + ((s->decorr[i].weightB * (int64_t)s->decorr[i].samplesB[0] + 512) >> 10);\n                else\n                    R2 = R + ((s->decorr[i].weightB * s->decorr[i].samplesB[0] + 512) >> 10);\n                UPDATE_WEIGHT_CLIP(s->decorr[i].weightB, s->decorr[i].delta, s->decorr[i].samplesB[0], R);\n                R = R2;\n                if (t == -3) {\n                    R2                       = s->decorr[i].samplesA[0];\n                    s->decorr[i].samplesA[0] = R;\n                }\n                if (type != AV_SAMPLE_FMT_S16P)\n                    L2 = L + ((s->decorr[i].weightA * (int64_t)R2 + 512) >> 10);\n                else\n                    L2 = L + ((s->decorr[i].weightA * R2 + 512) >> 10);\n                UPDATE_WEIGHT_CLIP(s->decorr[i].weightA, s->decorr[i].delta, R2, L);\n                L                        = L2;\n                s->decorr[i].samplesB[0] = L;\n            }\n        }\n        pos = (pos + 1) & 7;\n        if (s->joint)\n            L += (R -= (L >> 1));\n        crc = (crc * 3 + L) * 3 + R;\n        if (type == AV_SAMPLE_FMT_FLTP) {\n            *dstfl_l++ = wv_get_value_float(s, &crc_extra_bits, L);\n            *dstfl_r++ = wv_get_value_float(s, &crc_extra_bits, R);\n        } else if (type == AV_SAMPLE_FMT_S32P) {\n            *dst32_l++ = wv_get_value_integer(s, &crc_extra_bits, L);\n            *dst32_r++ = wv_get_value_integer(s, &crc_extra_bits, R);\n        } else {\n            *dst16_l++ = wv_get_value_integer(s, &crc_extra_bits, L);\n            *dst16_r++ = wv_get_value_integer(s, &crc_extra_bits, R);\n        }\n        count++;\n    } while (!last && count < s->samples);\n    wv_reset_saved_context(s);\n    if ((s->avctx->err_recognition & AV_EF_CRCCHECK) &&\n        wv_check_crc(s, crc, crc_extra_bits))\n        return AVERROR_INVALIDDATA;\n    return 0;\n}', 'static float wv_get_value_float(WavpackFrameContext *s, uint32_t *crc, int S)\n{\n    union {\n        float    f;\n        uint32_t u;\n    } value;\n    unsigned int sign;\n    int exp = s->float_max_exp;\n    if (s->got_extra_bits) {\n        const int max_bits  = 1 + 23 + 8 + 1;\n        const int left_bits = bitstream_bits_left(&s->bc_extra_bits);\n        if (left_bits + 8 * AV_INPUT_BUFFER_PADDING_SIZE < max_bits)\n            return 0.0;\n    }\n    if (S) {\n        S  <<= s->float_shift;\n        sign = S < 0;\n        if (sign)\n            S = -S;\n        if (S >= 0x1000000) {\n            if (s->got_extra_bits && bitstream_read_bit(&s->bc_extra_bits))\n                S = bitstream_read(&s->bc_extra_bits, 23);\n            else\n                S = 0;\n            exp = 255;\n        } else if (exp) {\n            int shift = 23 - av_log2(S);\n            exp = s->float_max_exp;\n            if (exp <= shift)\n                shift = --exp;\n            exp -= shift;\n            if (shift) {\n                S <<= shift;\n                if ((s->float_flag & WV_FLT_SHIFT_ONES) ||\n                    (s->got_extra_bits &&\n                     (s->float_flag & WV_FLT_SHIFT_SAME) &&\n                     bitstream_read_bit(&s->bc_extra_bits))) {\n                    S |= (1 << shift) - 1;\n                } else if (s->got_extra_bits &&\n                           (s->float_flag & WV_FLT_SHIFT_SENT)) {\n                    S |= bitstream_read(&s->bc_extra_bits, shift);\n                }\n            }\n        } else {\n            exp = s->float_max_exp;\n        }\n        S &= 0x7fffff;\n    } else {\n        sign = 0;\n        exp  = 0;\n        if (s->got_extra_bits && (s->float_flag & WV_FLT_ZERO_SENT)) {\n            if (bitstream_read_bit(&s->bc_extra_bits)) {\n                S = bitstream_read(&s->bc_extra_bits, 23);\n                if (s->float_max_exp >= 25)\n                    exp = bitstream_read(&s->bc_extra_bits, 8);\n                sign = bitstream_read_bit(&s->bc_extra_bits);\n            } else {\n                if (s->float_flag & WV_FLT_ZERO_SIGN)\n                    sign = bitstream_read_bit(&s->bc_extra_bits);\n            }\n        }\n    }\n    *crc = *crc * 27 + S * 9 + exp * 3 + sign;\n    value.u = (sign << 31) | (exp << 23) | S;\n    return value.f;\n}', 'static inline unsigned bitstream_read_bit(BitstreamContext *bc)\n{\n    if (!bc->bits_left)\n        refill_64(bc);\n    return get_val(bc, 1);\n}', 'static inline uint64_t get_val(BitstreamContext *bc, unsigned n)\n{\n#ifdef BITSTREAM_READER_LE\n    uint64_t ret = bc->bits & ((UINT64_C(1) << n) - 1);\n    bc->bits >>= n;\n#else\n    uint64_t ret = bc->bits >> (64 - n);\n    bc->bits <<= n;\n#endif\n    bc->bits_left -= n;\n    return ret;\n}']