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 int wavpack_decode_block(AVCodecContext *avctx, int block_no,\n                                AVFrame *frame, const uint8_t *buf, int buf_size)\n{\n    WavpackContext *wc = avctx->priv_data;\n    WavpackFrameContext *s;\n    GetByteContext gb;\n    void *samples_l, *samples_r;\n    int ret;\n    int got_terms   = 0, got_weights = 0, got_samples = 0,\n        got_entropy = 0, got_bs      = 0, got_float   = 0, got_hybrid = 0;\n    int i, j, id, size, ssize, weights, t;\n    int bpp, chan = 0, chmask = 0, orig_bpp, sample_rate = 0;\n    int multiblock;\n    if (block_no >= wc->fdec_num && wv_alloc_frame_context(wc) < 0) {\n        av_log(avctx, AV_LOG_ERROR, "Error creating frame decode context\\n");\n        return AVERROR_INVALIDDATA;\n    }\n    s = wc->fdec[block_no];\n    if (!s) {\n        av_log(avctx, AV_LOG_ERROR, "Context for block %d is not present\\n",\n               block_no);\n        return AVERROR_INVALIDDATA;\n    }\n    memset(s->decorr, 0, MAX_TERMS * sizeof(Decorr));\n    memset(s->ch, 0, sizeof(s->ch));\n    s->extra_bits     = 0;\n    s->and            = s->or = s->shift = 0;\n    s->got_extra_bits = 0;\n    bytestream2_init(&gb, buf, buf_size);\n    s->samples = bytestream2_get_le32(&gb);\n    if (s->samples != wc->samples) {\n        av_log(avctx, AV_LOG_ERROR, "Mismatching number of samples in "\n               "a sequence: %d and %d\\n", wc->samples, s->samples);\n        return AVERROR_INVALIDDATA;\n    }\n    s->frame_flags = bytestream2_get_le32(&gb);\n    bpp            = av_get_bytes_per_sample(avctx->sample_fmt);\n    orig_bpp       = ((s->frame_flags & 0x03) + 1) << 3;\n    multiblock     = (s->frame_flags & WV_SINGLE_BLOCK) != WV_SINGLE_BLOCK;\n    s->stereo         = !(s->frame_flags & WV_MONO);\n    s->stereo_in      =  (s->frame_flags & WV_FALSE_STEREO) ? 0 : s->stereo;\n    s->joint          =   s->frame_flags & WV_JOINT_STEREO;\n    s->hybrid         =   s->frame_flags & WV_HYBRID_MODE;\n    s->hybrid_bitrate =   s->frame_flags & WV_HYBRID_BITRATE;\n    s->post_shift     = bpp * 8 - orig_bpp + ((s->frame_flags >> 13) & 0x1f);\n    s->hybrid_maxclip =  ((1LL << (orig_bpp - 1)) - 1);\n    s->hybrid_minclip = ((-1LL << (orig_bpp - 1)));\n    s->CRC            = bytestream2_get_le32(&gb);\n    while (bytestream2_get_bytes_left(&gb)) {\n        id   = bytestream2_get_byte(&gb);\n        size = bytestream2_get_byte(&gb);\n        if (id & WP_IDF_LONG) {\n            size |= (bytestream2_get_byte(&gb)) << 8;\n            size |= (bytestream2_get_byte(&gb)) << 16;\n        }\n        size <<= 1;\n        ssize  = size;\n        if (id & WP_IDF_ODD)\n            size--;\n        if (size < 0) {\n            av_log(avctx, AV_LOG_ERROR,\n                   "Got incorrect block %02X with size %i\\n", id, size);\n            break;\n        }\n        if (bytestream2_get_bytes_left(&gb) < ssize) {\n            av_log(avctx, AV_LOG_ERROR,\n                   "Block size %i is out of bounds\\n", size);\n            break;\n        }\n        switch (id & WP_IDF_MASK) {\n        case WP_ID_DECTERMS:\n            if (size > MAX_TERMS) {\n                av_log(avctx, AV_LOG_ERROR, "Too many decorrelation terms\\n");\n                s->terms = 0;\n                bytestream2_skip(&gb, ssize);\n                continue;\n            }\n            s->terms = size;\n            for (i = 0; i < s->terms; i++) {\n                uint8_t val = bytestream2_get_byte(&gb);\n                s->decorr[s->terms - i - 1].value = (val & 0x1F) - 5;\n                s->decorr[s->terms - i - 1].delta =  val >> 5;\n            }\n            got_terms = 1;\n            break;\n        case WP_ID_DECWEIGHTS:\n            if (!got_terms) {\n                av_log(avctx, AV_LOG_ERROR, "No decorrelation terms met\\n");\n                continue;\n            }\n            weights = size >> s->stereo_in;\n            if (weights > MAX_TERMS || weights > s->terms) {\n                av_log(avctx, AV_LOG_ERROR, "Too many decorrelation weights\\n");\n                bytestream2_skip(&gb, ssize);\n                continue;\n            }\n            for (i = 0; i < weights; i++) {\n                t = (int8_t)bytestream2_get_byte(&gb);\n                s->decorr[s->terms - i - 1].weightA = t << 3;\n                if (s->decorr[s->terms - i - 1].weightA > 0)\n                    s->decorr[s->terms - i - 1].weightA +=\n                        (s->decorr[s->terms - i - 1].weightA + 64) >> 7;\n                if (s->stereo_in) {\n                    t = (int8_t)bytestream2_get_byte(&gb);\n                    s->decorr[s->terms - i - 1].weightB = t << 3;\n                    if (s->decorr[s->terms - i - 1].weightB > 0)\n                        s->decorr[s->terms - i - 1].weightB +=\n                            (s->decorr[s->terms - i - 1].weightB + 64) >> 7;\n                }\n            }\n            got_weights = 1;\n            break;\n        case WP_ID_DECSAMPLES:\n            if (!got_terms) {\n                av_log(avctx, AV_LOG_ERROR, "No decorrelation terms met\\n");\n                continue;\n            }\n            t = 0;\n            for (i = s->terms - 1; (i >= 0) && (t < size); i--) {\n                if (s->decorr[i].value > 8) {\n                    s->decorr[i].samplesA[0] =\n                        wp_exp2(bytestream2_get_le16(&gb));\n                    s->decorr[i].samplesA[1] =\n                        wp_exp2(bytestream2_get_le16(&gb));\n                    if (s->stereo_in) {\n                        s->decorr[i].samplesB[0] =\n                            wp_exp2(bytestream2_get_le16(&gb));\n                        s->decorr[i].samplesB[1] =\n                            wp_exp2(bytestream2_get_le16(&gb));\n                        t                       += 4;\n                    }\n                    t += 4;\n                } else if (s->decorr[i].value < 0) {\n                    s->decorr[i].samplesA[0] =\n                        wp_exp2(bytestream2_get_le16(&gb));\n                    s->decorr[i].samplesB[0] =\n                        wp_exp2(bytestream2_get_le16(&gb));\n                    t                       += 4;\n                } else {\n                    for (j = 0; j < s->decorr[i].value; j++) {\n                        s->decorr[i].samplesA[j] =\n                            wp_exp2(bytestream2_get_le16(&gb));\n                        if (s->stereo_in) {\n                            s->decorr[i].samplesB[j] =\n                                wp_exp2(bytestream2_get_le16(&gb));\n                        }\n                    }\n                    t += s->decorr[i].value * 2 * (s->stereo_in + 1);\n                }\n            }\n            got_samples = 1;\n            break;\n        case WP_ID_ENTROPY:\n            if (size != 6 * (s->stereo_in + 1)) {\n                av_log(avctx, AV_LOG_ERROR,\n                       "Entropy vars size should be %i, got %i",\n                       6 * (s->stereo_in + 1), size);\n                bytestream2_skip(&gb, ssize);\n                continue;\n            }\n            for (j = 0; j <= s->stereo_in; j++)\n                for (i = 0; i < 3; i++) {\n                    s->ch[j].median[i] = wp_exp2(bytestream2_get_le16(&gb));\n                }\n            got_entropy = 1;\n            break;\n        case WP_ID_HYBRID:\n            if (s->hybrid_bitrate) {\n                for (i = 0; i <= s->stereo_in; i++) {\n                    s->ch[i].slow_level = wp_exp2(bytestream2_get_le16(&gb));\n                    size               -= 2;\n                }\n            }\n            for (i = 0; i < (s->stereo_in + 1); i++) {\n                s->ch[i].bitrate_acc = bytestream2_get_le16(&gb) << 16;\n                size                -= 2;\n            }\n            if (size > 0) {\n                for (i = 0; i < (s->stereo_in + 1); i++) {\n                    s->ch[i].bitrate_delta =\n                        wp_exp2((int16_t)bytestream2_get_le16(&gb));\n                }\n            } else {\n                for (i = 0; i < (s->stereo_in + 1); i++)\n                    s->ch[i].bitrate_delta = 0;\n            }\n            got_hybrid = 1;\n            break;\n        case WP_ID_INT32INFO: {\n            uint8_t val[4];\n            if (size != 4) {\n                av_log(avctx, AV_LOG_ERROR,\n                       "Invalid INT32INFO, size = %i\\n",\n                       size);\n                bytestream2_skip(&gb, ssize - 4);\n                continue;\n            }\n            bytestream2_get_buffer(&gb, val, 4);\n            if (val[0]) {\n                s->extra_bits = val[0];\n            } else if (val[1]) {\n                s->shift = val[1];\n            } else if (val[2]) {\n                s->and   = s->or = 1;\n                s->shift = val[2];\n            } else if (val[3]) {\n                s->and   = 1;\n                s->shift = val[3];\n            }\n            if (s->hybrid && bpp == 4 && s->post_shift < 8 && s->shift > 8) {\n                s->post_shift      += 8;\n                s->shift           -= 8;\n                s->hybrid_maxclip >>= 8;\n                s->hybrid_minclip >>= 8;\n            }\n            break;\n        }\n        case WP_ID_FLOATINFO:\n            if (size != 4) {\n                av_log(avctx, AV_LOG_ERROR,\n                       "Invalid FLOATINFO, size = %i\\n", size);\n                bytestream2_skip(&gb, ssize);\n                continue;\n            }\n            s->float_flag    = bytestream2_get_byte(&gb);\n            s->float_shift   = bytestream2_get_byte(&gb);\n            s->float_max_exp = bytestream2_get_byte(&gb);\n            got_float        = 1;\n            bytestream2_skip(&gb, 1);\n            break;\n        case WP_ID_DATA:\n            s->sc.offset = bytestream2_tell(&gb);\n            s->sc.size   = size * 8;\n            bitstream_init8(&s->bc, gb.buffer, size);\n            s->data_size = size * 8;\n            bytestream2_skip(&gb, size);\n            got_bs       = 1;\n            break;\n        case WP_ID_EXTRABITS:\n            if (size <= 4) {\n                av_log(avctx, AV_LOG_ERROR, "Invalid EXTRABITS, size = %i\\n",\n                       size);\n                bytestream2_skip(&gb, size);\n                continue;\n            }\n            s->extra_sc.offset = bytestream2_tell(&gb);\n            s->extra_sc.size   = size * 8;\n            bitstream_init8(&s->bc_extra_bits, gb.buffer, size);\n            s->crc_extra_bits  = bitstream_read(&s->bc_extra_bits, 32);\n            bytestream2_skip(&gb, size);\n            s->got_extra_bits  = 1;\n            break;\n        case WP_ID_CHANINFO:\n            if (size <= 1) {\n                av_log(avctx, AV_LOG_ERROR,\n                       "Insufficient channel information\\n");\n                return AVERROR_INVALIDDATA;\n            }\n            chan = bytestream2_get_byte(&gb);\n            switch (size - 2) {\n            case 0:\n                chmask = bytestream2_get_byte(&gb);\n                break;\n            case 1:\n                chmask = bytestream2_get_le16(&gb);\n                break;\n            case 2:\n                chmask = bytestream2_get_le24(&gb);\n                break;\n            case 3:\n                chmask = bytestream2_get_le32(&gb);;\n                break;\n            case 5:\n                bytestream2_skip(&gb, 1);\n                chan  |= (bytestream2_get_byte(&gb) & 0xF) << 8;\n                chmask = bytestream2_get_le16(&gb);\n                break;\n            default:\n                av_log(avctx, AV_LOG_ERROR, "Invalid channel info size %d\\n",\n                       size);\n                chan   = avctx->channels;\n                chmask = avctx->channel_layout;\n            }\n            break;\n        case WP_ID_SAMPLE_RATE:\n            if (size != 3) {\n                av_log(avctx, AV_LOG_ERROR, "Invalid custom sample rate.\\n");\n                return AVERROR_INVALIDDATA;\n            }\n            sample_rate = bytestream2_get_le24(&gb);\n            break;\n        default:\n            bytestream2_skip(&gb, size);\n        }\n        if (id & WP_IDF_ODD)\n            bytestream2_skip(&gb, 1);\n    }\n    if (!got_terms) {\n        av_log(avctx, AV_LOG_ERROR, "No block with decorrelation terms\\n");\n        return AVERROR_INVALIDDATA;\n    }\n    if (!got_weights) {\n        av_log(avctx, AV_LOG_ERROR, "No block with decorrelation weights\\n");\n        return AVERROR_INVALIDDATA;\n    }\n    if (!got_samples) {\n        av_log(avctx, AV_LOG_ERROR, "No block with decorrelation samples\\n");\n        return AVERROR_INVALIDDATA;\n    }\n    if (!got_entropy) {\n        av_log(avctx, AV_LOG_ERROR, "No block with entropy info\\n");\n        return AVERROR_INVALIDDATA;\n    }\n    if (s->hybrid && !got_hybrid) {\n        av_log(avctx, AV_LOG_ERROR, "Hybrid config not found\\n");\n        return AVERROR_INVALIDDATA;\n    }\n    if (!got_bs) {\n        av_log(avctx, AV_LOG_ERROR, "Packed samples not found\\n");\n        return AVERROR_INVALIDDATA;\n    }\n    if (!got_float && avctx->sample_fmt == AV_SAMPLE_FMT_FLTP) {\n        av_log(avctx, AV_LOG_ERROR, "Float information not found\\n");\n        return AVERROR_INVALIDDATA;\n    }\n    if (s->got_extra_bits && avctx->sample_fmt != AV_SAMPLE_FMT_FLTP) {\n        const int size   = bitstream_bits_left(&s->bc_extra_bits);\n        const int wanted = s->samples * s->extra_bits << s->stereo_in;\n        if (size < wanted) {\n            av_log(avctx, AV_LOG_ERROR, "Too small EXTRABITS\\n");\n            s->got_extra_bits = 0;\n        }\n    }\n    if (!wc->ch_offset) {\n        int sr = (s->frame_flags >> 23) & 0xf;\n        if (sr == 0xf) {\n            if (!sample_rate) {\n                av_log(avctx, AV_LOG_ERROR, "Custom sample rate missing.\\n");\n                return AVERROR_INVALIDDATA;\n            }\n            avctx->sample_rate = sample_rate;\n        } else\n            avctx->sample_rate = wv_rates[sr];\n        if (multiblock) {\n            if (chan)\n                avctx->channels = chan;\n            if (chmask)\n                avctx->channel_layout = chmask;\n        } else {\n            avctx->channels       = s->stereo ? 2 : 1;\n            avctx->channel_layout = s->stereo ? AV_CH_LAYOUT_STEREO :\n                                                AV_CH_LAYOUT_MONO;\n        }\n        frame->nb_samples = s->samples;\n        if ((ret = ff_get_buffer(avctx, frame, 0)) < 0) {\n            av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\\n");\n            return ret;\n        }\n    }\n    if (wc->ch_offset + s->stereo >= avctx->channels) {\n        av_log(avctx, AV_LOG_WARNING, "Too many channels coded in a packet.\\n");\n        return (avctx->err_recognition & AV_EF_EXPLODE) ? AVERROR_INVALIDDATA : 0;\n    }\n    samples_l = frame->extended_data[wc->ch_offset];\n    if (s->stereo)\n        samples_r = frame->extended_data[wc->ch_offset + 1];\n    wc->ch_offset += 1 + s->stereo;\n    if (s->stereo_in) {\n        ret = wv_unpack_stereo(s, &s->bc, samples_l, samples_r, avctx->sample_fmt);\n        if (ret < 0)\n            return ret;\n    } else {\n        ret = wv_unpack_mono(s, &s->bc, samples_l, avctx->sample_fmt);\n        if (ret < 0)\n            return ret;\n        if (s->stereo)\n            memcpy(samples_r, samples_l, bpp * s->samples);\n    }\n    return 0;\n}', 'static inline uint32_t bitstream_read(BitstreamContext *bc, unsigned n)\n{\n    if (!n)\n        return 0;\n    if (n > bc->bits_left) {\n        refill_32(bc);\n        if (bc->bits_left < 32)\n            bc->bits_left = n;\n    }\n    return get_val(bc, n);\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}']