static int wavpack_decode_block(AVCodecContext *avctx, int block_no,

                                void *data, int *got_frame_ptr,

                                const uint8_t *buf, int buf_size)

{

    WavpackContext *wc = avctx->priv_data;

    WavpackFrameContext *s;

    void *samples = data;

    int samplecount;

    int got_terms = 0, got_weights = 0, got_samples = 0, got_entropy = 0, got_bs = 0, got_float = 0;

    int got_hybrid = 0;

    const uint8_t* orig_buf = buf;

    const uint8_t* buf_end = buf + buf_size;

    int i, j, id, size, ssize, weights, t;

    int bpp, chan, chmask;



    if (buf_size == 0){

        *got_frame_ptr = 0;

        return 0;

    }



    if(block_no >= wc->fdec_num && wv_alloc_frame_context(wc) < 0){

        av_log(avctx, AV_LOG_ERROR, "Error creating frame decode context\n");

        return -1;

    }



    s = wc->fdec[block_no];

    if(!s){

        av_log(avctx, AV_LOG_ERROR, "Context for block %d is not present\n", block_no);

        return -1;

    }



        memset(s->decorr, 0, MAX_TERMS * sizeof(Decorr));

        memset(s->ch, 0, sizeof(s->ch));

        s->extra_bits = 0;

        s->and = s->or = s->shift = 0;

        s->got_extra_bits = 0;



    if(!wc->mkv_mode){

        s->samples = AV_RL32(buf); buf += 4;

        if(!s->samples){

            *got_frame_ptr = 0;

            return 0;

        }

    }else{

        s->samples = wc->samples;

    }

    s->frame_flags = AV_RL32(buf); buf += 4;

    if(s->frame_flags&0x80){

        avctx->sample_fmt = AV_SAMPLE_FMT_FLT;

    } else if((s->frame_flags&0x03) <= 1){

        avctx->sample_fmt = AV_SAMPLE_FMT_S16;

    } else {

        avctx->sample_fmt = AV_SAMPLE_FMT_S32;

    }

    bpp = av_get_bytes_per_sample(avctx->sample_fmt);

    samples = (uint8_t*)samples + bpp * wc->ch_offset;



    s->stereo = !(s->frame_flags & WV_MONO);

    s->stereo_in = (s->frame_flags & WV_FALSE_STEREO) ? 0 : s->stereo;

    s->joint = s->frame_flags & WV_JOINT_STEREO;

    s->hybrid = s->frame_flags & WV_HYBRID_MODE;

    s->hybrid_bitrate = s->frame_flags & WV_HYBRID_BITRATE;

    s->hybrid_maxclip = 1 << ((((s->frame_flags & 0x03) + 1) << 3) - 1);

    s->post_shift = 8 * (bpp-1-(s->frame_flags&0x03)) + ((s->frame_flags >> 13) & 0x1f);

    s->CRC = AV_RL32(buf); buf += 4;

    if(wc->mkv_mode)

        buf += 4; //skip block size;



    wc->ch_offset += 1 + s->stereo;



    // parse metadata blocks

    while(buf < buf_end){

        id = *buf++;

        size = *buf++;

        if(id & WP_IDF_LONG) {

            size |= (*buf++) << 8;

            size |= (*buf++) << 16;

        }

        size <<= 1; // size is specified in words

        ssize = size;

        if(id & WP_IDF_ODD) size--;

        if(size < 0){

            av_log(avctx, AV_LOG_ERROR, "Got incorrect block %02X with size %i\n", id, size);

            break;

        }

        if(buf + ssize > buf_end){

            av_log(avctx, AV_LOG_ERROR, "Block size %i is out of bounds\n", size);

            break;

        }

        if(id & WP_IDF_IGNORE){

            buf += ssize;

            continue;

        }

        switch(id & WP_IDF_MASK){

        case WP_ID_DECTERMS:

            if(size > MAX_TERMS){

                av_log(avctx, AV_LOG_ERROR, "Too many decorrelation terms\n");

                s->terms = 0;

                buf += ssize;

                continue;

            }

            s->terms = size;

            for(i = 0; i < s->terms; i++) {

                s->decorr[s->terms - i - 1].value = (*buf & 0x1F) - 5;

                s->decorr[s->terms - i - 1].delta = *buf >> 5;

                buf++;

            }

            got_terms = 1;

            break;

        case WP_ID_DECWEIGHTS:

            if(!got_terms){

                av_log(avctx, AV_LOG_ERROR, "No decorrelation terms met\n");

                continue;

            }

            weights = size >> s->stereo_in;

            if(weights > MAX_TERMS || weights > s->terms){

                av_log(avctx, AV_LOG_ERROR, "Too many decorrelation weights\n");

                buf += ssize;

                continue;

            }

            for(i = 0; i < weights; i++) {

                t = (int8_t)(*buf++);

                s->decorr[s->terms - i - 1].weightA = t << 3;

                if(s->decorr[s->terms - i - 1].weightA > 0)

                    s->decorr[s->terms - i - 1].weightA += (s->decorr[s->terms - i - 1].weightA + 64) >> 7;

                if(s->stereo_in){

                    t = (int8_t)(*buf++);

                    s->decorr[s->terms - i - 1].weightB = t << 3;

                    if(s->decorr[s->terms - i - 1].weightB > 0)

                        s->decorr[s->terms - i - 1].weightB += (s->decorr[s->terms - i - 1].weightB + 64) >> 7;

                }

            }

            got_weights = 1;

            break;

        case WP_ID_DECSAMPLES:

            if(!got_terms){

                av_log(avctx, AV_LOG_ERROR, "No decorrelation terms met\n");

                continue;

            }

            t = 0;

            for(i = s->terms - 1; (i >= 0) && (t < size); i--) {

                if(s->decorr[i].value > 8){

                    s->decorr[i].samplesA[0] = wp_exp2(AV_RL16(buf)); buf += 2;

                    s->decorr[i].samplesA[1] = wp_exp2(AV_RL16(buf)); buf += 2;

                    if(s->stereo_in){

                        s->decorr[i].samplesB[0] = wp_exp2(AV_RL16(buf)); buf += 2;

                        s->decorr[i].samplesB[1] = wp_exp2(AV_RL16(buf)); buf += 2;

                        t += 4;

                    }

                    t += 4;

                }else if(s->decorr[i].value < 0){

                    s->decorr[i].samplesA[0] = wp_exp2(AV_RL16(buf)); buf += 2;

                    s->decorr[i].samplesB[0] = wp_exp2(AV_RL16(buf)); buf += 2;

                    t += 4;

                }else{

                    for(j = 0; j < s->decorr[i].value; j++){

                        s->decorr[i].samplesA[j] = wp_exp2(AV_RL16(buf)); buf += 2;

                        if(s->stereo_in){

                            s->decorr[i].samplesB[j] = wp_exp2(AV_RL16(buf)); buf += 2;

                        }

                    }

                    t += s->decorr[i].value * 2 * (s->stereo_in + 1);

                }

            }

            got_samples = 1;

            break;

        case WP_ID_ENTROPY:

            if(size != 6 * (s->stereo_in + 1)){

                av_log(avctx, AV_LOG_ERROR, "Entropy vars size should be %i, got %i", 6 * (s->stereo_in + 1), size);

                buf += ssize;

                continue;

            }

            for(j = 0; j <= s->stereo_in; j++){

                for(i = 0; i < 3; i++){

                    s->ch[j].median[i] = wp_exp2(AV_RL16(buf));

                    buf += 2;

                }

            }

            got_entropy = 1;

            break;

        case WP_ID_HYBRID:

            if(s->hybrid_bitrate){

                for(i = 0; i <= s->stereo_in; i++){

                    s->ch[i].slow_level = wp_exp2(AV_RL16(buf));

                    buf += 2;

                    size -= 2;

                }

            }

            for(i = 0; i < (s->stereo_in + 1); i++){

                s->ch[i].bitrate_acc = AV_RL16(buf) << 16;

                buf += 2;

                size -= 2;

            }

            if(size > 0){

                for(i = 0; i < (s->stereo_in + 1); i++){

                    s->ch[i].bitrate_delta = wp_exp2((int16_t)AV_RL16(buf));

                    buf += 2;

                }

            }else{

                for(i = 0; i < (s->stereo_in + 1); i++)

                    s->ch[i].bitrate_delta = 0;

            }

            got_hybrid = 1;

            break;

        case WP_ID_INT32INFO:

            if(size != 4){

                av_log(avctx, AV_LOG_ERROR, "Invalid INT32INFO, size = %i, sent_bits = %i\n", size, *buf);

                buf += ssize;

                continue;

            }

            if(buf[0])

                s->extra_bits = buf[0];

            else if(buf[1])

                s->shift = buf[1];

            else if(buf[2]){

                s->and = s->or = 1;

                s->shift = buf[2];

            }else if(buf[3]){

                s->and = 1;

                s->shift = buf[3];

            }

            buf += 4;

            break;

        case WP_ID_FLOATINFO:

            if(size != 4){

                av_log(avctx, AV_LOG_ERROR, "Invalid FLOATINFO, size = %i\n", size);

                buf += ssize;

                continue;

            }

            s->float_flag = buf[0];

            s->float_shift = buf[1];

            s->float_max_exp = buf[2];

            buf += 4;

            got_float = 1;

            break;

        case WP_ID_DATA:

            s->sc.offset = buf - orig_buf;

            s->sc.size   = size * 8;

            init_get_bits(&s->gb, buf, size * 8);

            s->data_size = size * 8;

            buf += size;

            got_bs = 1;

            break;

        case WP_ID_EXTRABITS:

            if(size <= 4){

                av_log(avctx, AV_LOG_ERROR, "Invalid EXTRABITS, size = %i\n", size);

                buf += size;

                continue;

            }

            s->extra_sc.offset = buf - orig_buf;

            s->extra_sc.size   = size * 8;

            init_get_bits(&s->gb_extra_bits, buf, size * 8);

            s->crc_extra_bits = get_bits_long(&s->gb_extra_bits, 32);

            buf += size;

            s->got_extra_bits = 1;

            break;

        case WP_ID_CHANINFO:

            if(size <= 1){

                av_log(avctx, AV_LOG_ERROR, "Insufficient channel information\n");

                return -1;

            }

            chan = *buf++;

            switch(size - 2){

            case 0:

                chmask = *buf;

                break;

            case 1:

                chmask = AV_RL16(buf);

                break;

            case 2:

                chmask = AV_RL24(buf);

                break;

            case 3:

                chmask = AV_RL32(buf);

                break;

            case 5:

                chan |= (buf[1] & 0xF) << 8;

                chmask = AV_RL24(buf + 2);

                break;

            default:

                av_log(avctx, AV_LOG_ERROR, "Invalid channel info size %d\n", size);

                chan = avctx->channels;

                chmask = avctx->channel_layout;

            }

            if(chan != avctx->channels){

                av_log(avctx, AV_LOG_ERROR, "Block reports total %d channels, decoder believes it's %d channels\n",

                       chan, avctx->channels);

                return -1;

            }

            if(!avctx->channel_layout)

                avctx->channel_layout = chmask;

            buf += size - 1;

            break;

        default:

            buf += size;

        }

        if(id & WP_IDF_ODD) buf++;

    }



        if(!got_terms){

            av_log(avctx, AV_LOG_ERROR, "No block with decorrelation terms\n");

            return -1;

        }

        if(!got_weights){

            av_log(avctx, AV_LOG_ERROR, "No block with decorrelation weights\n");

            return -1;

        }

        if(!got_samples){

            av_log(avctx, AV_LOG_ERROR, "No block with decorrelation samples\n");

            return -1;

        }

        if(!got_entropy){

            av_log(avctx, AV_LOG_ERROR, "No block with entropy info\n");

            return -1;

        }

        if(s->hybrid && !got_hybrid){

            av_log(avctx, AV_LOG_ERROR, "Hybrid config not found\n");

            return -1;

        }

        if(!got_bs){

            av_log(avctx, AV_LOG_ERROR, "Packed samples not found\n");

            return -1;

        }

        if(!got_float && avctx->sample_fmt == AV_SAMPLE_FMT_FLT){

            av_log(avctx, AV_LOG_ERROR, "Float information not found\n");

            return -1;

        }

        if(s->got_extra_bits && avctx->sample_fmt != AV_SAMPLE_FMT_FLT){

            const int size = get_bits_left(&s->gb_extra_bits);

            const int wanted = s->samples * s->extra_bits << s->stereo_in;

            if(size < wanted){

                av_log(avctx, AV_LOG_ERROR, "Too small EXTRABITS\n");

                s->got_extra_bits = 0;

            }

        }



    if(s->stereo_in){

        if(avctx->sample_fmt == AV_SAMPLE_FMT_S16)

            samplecount = wv_unpack_stereo(s, &s->gb, samples, AV_SAMPLE_FMT_S16);

        else if(avctx->sample_fmt == AV_SAMPLE_FMT_S32)

            samplecount = wv_unpack_stereo(s, &s->gb, samples, AV_SAMPLE_FMT_S32);

        else

            samplecount = wv_unpack_stereo(s, &s->gb, samples, AV_SAMPLE_FMT_FLT);



        if (samplecount < 0)

            return -1;



        samplecount >>= 1;

    }else{

        const int channel_stride = avctx->channels;



        if(avctx->sample_fmt == AV_SAMPLE_FMT_S16)

            samplecount = wv_unpack_mono(s, &s->gb, samples, AV_SAMPLE_FMT_S16);

        else if(avctx->sample_fmt == AV_SAMPLE_FMT_S32)

            samplecount = wv_unpack_mono(s, &s->gb, samples, AV_SAMPLE_FMT_S32);

        else

            samplecount = wv_unpack_mono(s, &s->gb, samples, AV_SAMPLE_FMT_FLT);



        if (samplecount < 0)

            return -1;



        if(s->stereo && avctx->sample_fmt == AV_SAMPLE_FMT_S16){

            int16_t *dst = (int16_t*)samples + 1;

            int16_t *src = (int16_t*)samples;

            int cnt = samplecount;

            while(cnt--){

                *dst = *src;

                src += channel_stride;

                dst += channel_stride;

            }

        }else if(s->stereo && avctx->sample_fmt == AV_SAMPLE_FMT_S32){

            int32_t *dst = (int32_t*)samples + 1;

            int32_t *src = (int32_t*)samples;

            int cnt = samplecount;

            while(cnt--){

                *dst = *src;

                src += channel_stride;

                dst += channel_stride;

            }

        }else if(s->stereo){

            float *dst = (float*)samples + 1;

            float *src = (float*)samples;

            int cnt = samplecount;

            while(cnt--){

                *dst = *src;

                src += channel_stride;

                dst += channel_stride;

            }

        }

    }



    *got_frame_ptr = 1;



    return samplecount * bpp;

}

static int wavpack_decode_block(AVCodecContext *avctx, int block_no,

                                void *data, int *got_frame_ptr,

                                const uint8_t *buf, int buf_size)

{

    WavpackContext *wc = avctx->priv_data;

    WavpackFrameContext *s;

    void *samples = data;

    int samplecount;

    int got_terms = 0, got_weights = 0, got_samples = 0, got_entropy = 0, got_bs = 0, got_float = 0;

    int got_hybrid = 0;

    const uint8_t* orig_buf = buf;

    const uint8_t* buf_end = buf + buf_size;

    int i, j, id, size, ssize, weights, t;

    int bpp, chan, chmask;



    if (buf_size == 0){

        *got_frame_ptr = 0;

        return 0;

    }



    if(block_no >= wc->fdec_num && wv_alloc_frame_context(wc) < 0){

        av_log(avctx, AV_LOG_ERROR, "Error creating frame decode context\n");

        return -1;

    }



    s = wc->fdec[block_no];

    if(!s){

        av_log(avctx, AV_LOG_ERROR, "Context for block %d is not present\n", block_no);

        return -1;

    }



        memset(s->decorr, 0, MAX_TERMS * sizeof(Decorr));

        memset(s->ch, 0, sizeof(s->ch));

        s->extra_bits = 0;

        s->and = s->or = s->shift = 0;

        s->got_extra_bits = 0;



    if(!wc->mkv_mode){

        s->samples = AV_RL32(buf); buf += 4;

        if(!s->samples){

            *got_frame_ptr = 0;

            return 0;

        }

    }else{

        s->samples = wc->samples;

    }

    s->frame_flags = AV_RL32(buf); buf += 4;

    if(s->frame_flags&0x80){

        avctx->sample_fmt = AV_SAMPLE_FMT_FLT;

    } else if((s->frame_flags&0x03) <= 1){

        avctx->sample_fmt = AV_SAMPLE_FMT_S16;

    } else {

        avctx->sample_fmt = AV_SAMPLE_FMT_S32;

    }

    bpp = av_get_bytes_per_sample(avctx->sample_fmt);

    samples = (uint8_t*)samples + bpp * wc->ch_offset;



    s->stereo = !(s->frame_flags & WV_MONO);

    s->stereo_in = (s->frame_flags & WV_FALSE_STEREO) ? 0 : s->stereo;

    s->joint = s->frame_flags & WV_JOINT_STEREO;

    s->hybrid = s->frame_flags & WV_HYBRID_MODE;

    s->hybrid_bitrate = s->frame_flags & WV_HYBRID_BITRATE;

    s->hybrid_maxclip = 1 << ((((s->frame_flags & 0x03) + 1) << 3) - 1);

    s->post_shift = 8 * (bpp-1-(s->frame_flags&0x03)) + ((s->frame_flags >> 13) & 0x1f);

    s->CRC = AV_RL32(buf); buf += 4;

    if(wc->mkv_mode)

        buf += 4; 



    wc->ch_offset += 1 + s->stereo;



    

    while(buf < buf_end){

        id = *buf++;

        size = *buf++;

        if(id & WP_IDF_LONG) {

            size |= (*buf++) << 8;

            size |= (*buf++) << 16;

        }

        size <<= 1; 

        ssize = size;

        if(id & WP_IDF_ODD) size--;

        if(size < 0){

            av_log(avctx, AV_LOG_ERROR, "Got incorrect block %02X with size %i\n", id, size);

            break;

        }

        if(buf + ssize > buf_end){

            av_log(avctx, AV_LOG_ERROR, "Block size %i is out of bounds\n", size);

            break;

        }

        if(id & WP_IDF_IGNORE){

            buf += ssize;

            continue;

        }

        switch(id & WP_IDF_MASK){

        case WP_ID_DECTERMS:

            if(size > MAX_TERMS){

                av_log(avctx, AV_LOG_ERROR, "Too many decorrelation terms\n");

                s->terms = 0;

                buf += ssize;

                continue;

            }

            s->terms = size;

            for(i = 0; i < s->terms; i++) {

                s->decorr[s->terms - i - 1].value = (*buf & 0x1F) - 5;

                s->decorr[s->terms - i - 1].delta = *buf >> 5;

                buf++;

            }

            got_terms = 1;

            break;

        case WP_ID_DECWEIGHTS:

            if(!got_terms){

                av_log(avctx, AV_LOG_ERROR, "No decorrelation terms met\n");

                continue;

            }

            weights = size >> s->stereo_in;

            if(weights > MAX_TERMS || weights > s->terms){

                av_log(avctx, AV_LOG_ERROR, "Too many decorrelation weights\n");

                buf += ssize;

                continue;

            }

            for(i = 0; i < weights; i++) {

                t = (int8_t)(*buf++);

                s->decorr[s->terms - i - 1].weightA = t << 3;

                if(s->decorr[s->terms - i - 1].weightA > 0)

                    s->decorr[s->terms - i - 1].weightA += (s->decorr[s->terms - i - 1].weightA + 64) >> 7;

                if(s->stereo_in){

                    t = (int8_t)(*buf++);

                    s->decorr[s->terms - i - 1].weightB = t << 3;

                    if(s->decorr[s->terms - i - 1].weightB > 0)

                        s->decorr[s->terms - i - 1].weightB += (s->decorr[s->terms - i - 1].weightB + 64) >> 7;

                }

            }

            got_weights = 1;

            break;

        case WP_ID_DECSAMPLES:

            if(!got_terms){

                av_log(avctx, AV_LOG_ERROR, "No decorrelation terms met\n");

                continue;

            }

            t = 0;

            for(i = s->terms - 1; (i >= 0) && (t < size); i--) {

                if(s->decorr[i].value > 8){

                    s->decorr[i].samplesA[0] = wp_exp2(AV_RL16(buf)); buf += 2;

                    s->decorr[i].samplesA[1] = wp_exp2(AV_RL16(buf)); buf += 2;

                    if(s->stereo_in){

                        s->decorr[i].samplesB[0] = wp_exp2(AV_RL16(buf)); buf += 2;

                        s->decorr[i].samplesB[1] = wp_exp2(AV_RL16(buf)); buf += 2;

                        t += 4;

                    }

                    t += 4;

                }else if(s->decorr[i].value < 0){

                    s->decorr[i].samplesA[0] = wp_exp2(AV_RL16(buf)); buf += 2;

                    s->decorr[i].samplesB[0] = wp_exp2(AV_RL16(buf)); buf += 2;

                    t += 4;

                }else{

                    for(j = 0; j < s->decorr[i].value; j++){

                        s->decorr[i].samplesA[j] = wp_exp2(AV_RL16(buf)); buf += 2;

                        if(s->stereo_in){

                            s->decorr[i].samplesB[j] = wp_exp2(AV_RL16(buf)); buf += 2;

                        }

                    }

                    t += s->decorr[i].value * 2 * (s->stereo_in + 1);

                }

            }

            got_samples = 1;

            break;

        case WP_ID_ENTROPY:

            if(size != 6 * (s->stereo_in + 1)){

                av_log(avctx, AV_LOG_ERROR, "Entropy vars size should be %i, got %i", 6 * (s->stereo_in + 1), size);

                buf += ssize;

                continue;

            }

            for(j = 0; j <= s->stereo_in; j++){

                for(i = 0; i < 3; i++){

                    s->ch[j].median[i] = wp_exp2(AV_RL16(buf));

                    buf += 2;

                }

            }

            got_entropy = 1;

            break;

        case WP_ID_HYBRID:

            if(s->hybrid_bitrate){

                for(i = 0; i <= s->stereo_in; i++){

                    s->ch[i].slow_level = wp_exp2(AV_RL16(buf));

                    buf += 2;

                    size -= 2;

                }

            }

            for(i = 0; i < (s->stereo_in + 1); i++){

                s->ch[i].bitrate_acc = AV_RL16(buf) << 16;

                buf += 2;

                size -= 2;

            }

            if(size > 0){

                for(i = 0; i < (s->stereo_in + 1); i++){

                    s->ch[i].bitrate_delta = wp_exp2((int16_t)AV_RL16(buf));

                    buf += 2;

                }

            }else{

                for(i = 0; i < (s->stereo_in + 1); i++)

                    s->ch[i].bitrate_delta = 0;

            }

            got_hybrid = 1;

            break;

        case WP_ID_INT32INFO:

            if(size != 4){

                av_log(avctx, AV_LOG_ERROR, "Invalid INT32INFO, size = %i, sent_bits = %i\n", size, *buf);

                buf += ssize;

                continue;

            }

            if(buf[0])

                s->extra_bits = buf[0];

            else if(buf[1])

                s->shift = buf[1];

            else if(buf[2]){

                s->and = s->or = 1;

                s->shift = buf[2];

            }else if(buf[3]){

                s->and = 1;

                s->shift = buf[3];

            }

            buf += 4;

            break;

        case WP_ID_FLOATINFO:

            if(size != 4){

                av_log(avctx, AV_LOG_ERROR, "Invalid FLOATINFO, size = %i\n", size);

                buf += ssize;

                continue;

            }

            s->float_flag = buf[0];

            s->float_shift = buf[1];

            s->float_max_exp = buf[2];

            buf += 4;

            got_float = 1;

            break;

        case WP_ID_DATA:

            s->sc.offset = buf - orig_buf;

            s->sc.size   = size * 8;

            init_get_bits(&s->gb, buf, size * 8);

            s->data_size = size * 8;

            buf += size;

            got_bs = 1;

            break;

        case WP_ID_EXTRABITS:

            if(size <= 4){

                av_log(avctx, AV_LOG_ERROR, "Invalid EXTRABITS, size = %i\n", size);

                buf += size;

                continue;

            }

            s->extra_sc.offset = buf - orig_buf;

            s->extra_sc.size   = size * 8;

            init_get_bits(&s->gb_extra_bits, buf, size * 8);

            s->crc_extra_bits = get_bits_long(&s->gb_extra_bits, 32);

            buf += size;

            s->got_extra_bits = 1;

            break;

        case WP_ID_CHANINFO:

            if(size <= 1){

                av_log(avctx, AV_LOG_ERROR, "Insufficient channel information\n");

                return -1;

            }

            chan = *buf++;

            switch(size - 2){

            case 0:

                chmask = *buf;

                break;

            case 1:

                chmask = AV_RL16(buf);

                break;

            case 2:

                chmask = AV_RL24(buf);

                break;

            case 3:

                chmask = AV_RL32(buf);

                break;

            case 5:

                chan |= (buf[1] & 0xF) << 8;

                chmask = AV_RL24(buf + 2);

                break;

            default:

                av_log(avctx, AV_LOG_ERROR, "Invalid channel info size %d\n", size);

                chan = avctx->channels;

                chmask = avctx->channel_layout;

            }

            if(chan != avctx->channels){

                av_log(avctx, AV_LOG_ERROR, "Block reports total %d channels, decoder believes it's %d channels\n",

                       chan, avctx->channels);

                return -1;

            }

            if(!avctx->channel_layout)

                avctx->channel_layout = chmask;

            buf += size - 1;

            break;

        default:

            buf += size;

        }

        if(id & WP_IDF_ODD) buf++;

    }



        if(!got_terms){

            av_log(avctx, AV_LOG_ERROR, "No block with decorrelation terms\n");

            return -1;

        }

        if(!got_weights){

            av_log(avctx, AV_LOG_ERROR, "No block with decorrelation weights\n");

            return -1;

        }

        if(!got_samples){

            av_log(avctx, AV_LOG_ERROR, "No block with decorrelation samples\n");

            return -1;

        }

        if(!got_entropy){

            av_log(avctx, AV_LOG_ERROR, "No block with entropy info\n");

            return -1;

        }

        if(s->hybrid && !got_hybrid){

            av_log(avctx, AV_LOG_ERROR, "Hybrid config not found\n");

            return -1;

        }

        if(!got_bs){

            av_log(avctx, AV_LOG_ERROR, "Packed samples not found\n");

            return -1;

        }

        if(!got_float && avctx->sample_fmt == AV_SAMPLE_FMT_FLT){

            av_log(avctx, AV_LOG_ERROR, "Float information not found\n");

            return -1;

        }

        if(s->got_extra_bits && avctx->sample_fmt != AV_SAMPLE_FMT_FLT){

            const int size = get_bits_left(&s->gb_extra_bits);

            const int wanted = s->samples * s->extra_bits << s->stereo_in;

            if(size < wanted){

                av_log(avctx, AV_LOG_ERROR, "Too small EXTRABITS\n");

                s->got_extra_bits = 0;

            }

        }



    if(s->stereo_in){

        if(avctx->sample_fmt == AV_SAMPLE_FMT_S16)

            samplecount = wv_unpack_stereo(s, &s->gb, samples, AV_SAMPLE_FMT_S16);

        else if(avctx->sample_fmt == AV_SAMPLE_FMT_S32)

            samplecount = wv_unpack_stereo(s, &s->gb, samples, AV_SAMPLE_FMT_S32);

        else

            samplecount = wv_unpack_stereo(s, &s->gb, samples, AV_SAMPLE_FMT_FLT);



        if (samplecount < 0)

            return -1;



        samplecount >>= 1;

    }else{

        const int channel_stride = avctx->channels;



        if(avctx->sample_fmt == AV_SAMPLE_FMT_S16)

            samplecount = wv_unpack_mono(s, &s->gb, samples, AV_SAMPLE_FMT_S16);

        else if(avctx->sample_fmt == AV_SAMPLE_FMT_S32)

            samplecount = wv_unpack_mono(s, &s->gb, samples, AV_SAMPLE_FMT_S32);

        else

            samplecount = wv_unpack_mono(s, &s->gb, samples, AV_SAMPLE_FMT_FLT);



        if (samplecount < 0)

            return -1;



        if(s->stereo && avctx->sample_fmt == AV_SAMPLE_FMT_S16){

            int16_t *dst = (int16_t*)samples + 1;

            int16_t *src = (int16_t*)samples;

            int cnt = samplecount;

            while(cnt--){

                *dst = *src;

                src += channel_stride;

                dst += channel_stride;

            }

        }else if(s->stereo && avctx->sample_fmt == AV_SAMPLE_FMT_S32){

            int32_t *dst = (int32_t*)samples + 1;

            int32_t *src = (int32_t*)samples;

            int cnt = samplecount;

            while(cnt--){

                *dst = *src;

                src += channel_stride;

                dst += channel_stride;

            }

        }else if(s->stereo){

            float *dst = (float*)samples + 1;

            float *src = (float*)samples;

            int cnt = samplecount;

            while(cnt--){

                *dst = *src;

                src += channel_stride;

                dst += channel_stride;

            }

        }

    }



    *got_frame_ptr = 1;



    return samplecount * bpp;

}

    s->hybrid_maxclip = 1 << ((((s->frame_flags & 0x03) + 1) << 3) - 1);
static int FUNC_0(AVCodecContext *VAR_0, int VAR_1,

                                void *VAR_2, int *VAR_3,

                                const uint8_t *VAR_4, int VAR_5)

{

    WavpackContext *wc = VAR_0->priv_data;

    WavpackFrameContext *s;

    void *VAR_6 = VAR_2;

    int VAR_7;

    int VAR_8 = 0, VAR_9 = 0, VAR_10 = 0, VAR_11 = 0, VAR_12 = 0, VAR_13 = 0;

    int VAR_14 = 0;

    const uint8_t* VAR_15 = VAR_4;

    const uint8_t* VAR_16 = VAR_4 + VAR_5;

    int VAR_17, VAR_18, VAR_19, VAR_27, VAR_21, VAR_22, VAR_23;

    int VAR_24, VAR_25, VAR_26;



    if (VAR_5 == 0){

        *VAR_3 = 0;

        return 0;

    }



    if(VAR_1 >= wc->fdec_num && wv_alloc_frame_context(wc) < 0){

        av_log(VAR_0, AV_LOG_ERROR, "Error creating frame decode context\n");

        return -1;

    }



    s = wc->fdec[VAR_1];

    if(!s){

        av_log(VAR_0, AV_LOG_ERROR, "Context for block %d is not present\n", VAR_1);

        return -1;

    }



        memset(s->decorr, 0, MAX_TERMS * sizeof(Decorr));

        memset(s->ch, 0, sizeof(s->ch));

        s->extra_bits = 0;

        s->and = s->or = s->shift = 0;

        s->got_extra_bits = 0;



    if(!wc->mkv_mode){

        s->VAR_6 = AV_RL32(VAR_4); VAR_4 += 4;

        if(!s->VAR_6){

            *VAR_3 = 0;

            return 0;

        }

    }else{

        s->VAR_6 = wc->VAR_6;

    }

    s->frame_flags = AV_RL32(VAR_4); VAR_4 += 4;

    if(s->frame_flags&0x80){

        VAR_0->sample_fmt = AV_SAMPLE_FMT_FLT;

    } else if((s->frame_flags&0x03) <= 1){

        VAR_0->sample_fmt = AV_SAMPLE_FMT_S16;

    } else {

        VAR_0->sample_fmt = AV_SAMPLE_FMT_S32;

    }

    VAR_24 = av_get_bytes_per_sample(VAR_0->sample_fmt);

    VAR_6 = (uint8_t*)VAR_6 + VAR_24 * wc->ch_offset;



    s->stereo = !(s->frame_flags & WV_MONO);

    s->stereo_in = (s->frame_flags & WV_FALSE_STEREO) ? 0 : s->stereo;

    s->joint = s->frame_flags & WV_JOINT_STEREO;

    s->hybrid = s->frame_flags & WV_HYBRID_MODE;

    s->hybrid_bitrate = s->frame_flags & WV_HYBRID_BITRATE;

    s->hybrid_maxclip = 1 << ((((s->frame_flags & 0x03) + 1) << 3) - 1);

    s->post_shift = 8 * (VAR_24-1-(s->frame_flags&0x03)) + ((s->frame_flags >> 13) & 0x1f);

    s->CRC = AV_RL32(VAR_4); VAR_4 += 4;

    if(wc->mkv_mode)

        VAR_4 += 4; 



    wc->ch_offset += 1 + s->stereo;



    

    while(VAR_4 < VAR_16){

        VAR_19 = *VAR_4++;

        VAR_27 = *VAR_4++;

        if(VAR_19 & WP_IDF_LONG) {

            VAR_27 |= (*VAR_4++) << 8;

            VAR_27 |= (*VAR_4++) << 16;

        }

        VAR_27 <<= 1; 

        VAR_21 = VAR_27;

        if(VAR_19 & WP_IDF_ODD) VAR_27--;

        if(VAR_27 < 0){

            av_log(VAR_0, AV_LOG_ERROR, "Got incorrect block %02X with VAR_27 %VAR_17\n", VAR_19, VAR_27);

            break;

        }

        if(VAR_4 + VAR_21 > VAR_16){

            av_log(VAR_0, AV_LOG_ERROR, "Block VAR_27 %VAR_17 is out of bounds\n", VAR_27);

            break;

        }

        if(VAR_19 & WP_IDF_IGNORE){

            VAR_4 += VAR_21;

            continue;

        }

        switch(VAR_19 & WP_IDF_MASK){

        case WP_ID_DECTERMS:

            if(VAR_27 > MAX_TERMS){

                av_log(VAR_0, AV_LOG_ERROR, "Too many decorrelation terms\n");

                s->terms = 0;

                VAR_4 += VAR_21;

                continue;

            }

            s->terms = VAR_27;

            for(VAR_17 = 0; VAR_17 < s->terms; VAR_17++) {

                s->decorr[s->terms - VAR_17 - 1].value = (*VAR_4 & 0x1F) - 5;

                s->decorr[s->terms - VAR_17 - 1].delta = *VAR_4 >> 5;

                VAR_4++;

            }

            VAR_8 = 1;

            break;

        case WP_ID_DECWEIGHTS:

            if(!VAR_8){

                av_log(VAR_0, AV_LOG_ERROR, "No decorrelation terms met\n");

                continue;

            }

            VAR_22 = VAR_27 >> s->stereo_in;

            if(VAR_22 > MAX_TERMS || VAR_22 > s->terms){

                av_log(VAR_0, AV_LOG_ERROR, "Too many decorrelation VAR_22\n");

                VAR_4 += VAR_21;

                continue;

            }

            for(VAR_17 = 0; VAR_17 < VAR_22; VAR_17++) {

                VAR_23 = (int8_t)(*VAR_4++);

                s->decorr[s->terms - VAR_17 - 1].weightA = VAR_23 << 3;

                if(s->decorr[s->terms - VAR_17 - 1].weightA > 0)

                    s->decorr[s->terms - VAR_17 - 1].weightA += (s->decorr[s->terms - VAR_17 - 1].weightA + 64) >> 7;

                if(s->stereo_in){

                    VAR_23 = (int8_t)(*VAR_4++);

                    s->decorr[s->terms - VAR_17 - 1].weightB = VAR_23 << 3;

                    if(s->decorr[s->terms - VAR_17 - 1].weightB > 0)

                        s->decorr[s->terms - VAR_17 - 1].weightB += (s->decorr[s->terms - VAR_17 - 1].weightB + 64) >> 7;

                }

            }

            VAR_9 = 1;

            break;

        case WP_ID_DECSAMPLES:

            if(!VAR_8){

                av_log(VAR_0, AV_LOG_ERROR, "No decorrelation terms met\n");

                continue;

            }

            VAR_23 = 0;

            for(VAR_17 = s->terms - 1; (VAR_17 >= 0) && (VAR_23 < VAR_27); VAR_17--) {

                if(s->decorr[VAR_17].value > 8){

                    s->decorr[VAR_17].samplesA[0] = wp_exp2(AV_RL16(VAR_4)); VAR_4 += 2;

                    s->decorr[VAR_17].samplesA[1] = wp_exp2(AV_RL16(VAR_4)); VAR_4 += 2;

                    if(s->stereo_in){

                        s->decorr[VAR_17].samplesB[0] = wp_exp2(AV_RL16(VAR_4)); VAR_4 += 2;

                        s->decorr[VAR_17].samplesB[1] = wp_exp2(AV_RL16(VAR_4)); VAR_4 += 2;

                        VAR_23 += 4;

                    }

                    VAR_23 += 4;

                }else if(s->decorr[VAR_17].value < 0){

                    s->decorr[VAR_17].samplesA[0] = wp_exp2(AV_RL16(VAR_4)); VAR_4 += 2;

                    s->decorr[VAR_17].samplesB[0] = wp_exp2(AV_RL16(VAR_4)); VAR_4 += 2;

                    VAR_23 += 4;

                }else{

                    for(VAR_18 = 0; VAR_18 < s->decorr[VAR_17].value; VAR_18++){

                        s->decorr[VAR_17].samplesA[VAR_18] = wp_exp2(AV_RL16(VAR_4)); VAR_4 += 2;

                        if(s->stereo_in){

                            s->decorr[VAR_17].samplesB[VAR_18] = wp_exp2(AV_RL16(VAR_4)); VAR_4 += 2;

                        }

                    }

                    VAR_23 += s->decorr[VAR_17].value * 2 * (s->stereo_in + 1);

                }

            }

            VAR_10 = 1;

            break;

        case WP_ID_ENTROPY:

            if(VAR_27 != 6 * (s->stereo_in + 1)){

                av_log(VAR_0, AV_LOG_ERROR, "Entropy vars VAR_27 should be %VAR_17, got %VAR_17", 6 * (s->stereo_in + 1), VAR_27);

                VAR_4 += VAR_21;

                continue;

            }

            for(VAR_18 = 0; VAR_18 <= s->stereo_in; VAR_18++){

                for(VAR_17 = 0; VAR_17 < 3; VAR_17++){

                    s->ch[VAR_18].median[VAR_17] = wp_exp2(AV_RL16(VAR_4));

                    VAR_4 += 2;

                }

            }

            VAR_11 = 1;

            break;

        case WP_ID_HYBRID:

            if(s->hybrid_bitrate){

                for(VAR_17 = 0; VAR_17 <= s->stereo_in; VAR_17++){

                    s->ch[VAR_17].slow_level = wp_exp2(AV_RL16(VAR_4));

                    VAR_4 += 2;

                    VAR_27 -= 2;

                }

            }

            for(VAR_17 = 0; VAR_17 < (s->stereo_in + 1); VAR_17++){

                s->ch[VAR_17].bitrate_acc = AV_RL16(VAR_4) << 16;

                VAR_4 += 2;

                VAR_27 -= 2;

            }

            if(VAR_27 > 0){

                for(VAR_17 = 0; VAR_17 < (s->stereo_in + 1); VAR_17++){

                    s->ch[VAR_17].bitrate_delta = wp_exp2((int16_t)AV_RL16(VAR_4));

                    VAR_4 += 2;

                }

            }else{

                for(VAR_17 = 0; VAR_17 < (s->stereo_in + 1); VAR_17++)

                    s->ch[VAR_17].bitrate_delta = 0;

            }

            VAR_14 = 1;

            break;

        case WP_ID_INT32INFO:

            if(VAR_27 != 4){

                av_log(VAR_0, AV_LOG_ERROR, "Invalid INT32INFO, VAR_27 = %VAR_17, sent_bits = %VAR_17\n", VAR_27, *VAR_4);

                VAR_4 += VAR_21;

                continue;

            }

            if(VAR_4[0])

                s->extra_bits = VAR_4[0];

            else if(VAR_4[1])

                s->shift = VAR_4[1];

            else if(VAR_4[2]){

                s->and = s->or = 1;

                s->shift = VAR_4[2];

            }else if(VAR_4[3]){

                s->and = 1;

                s->shift = VAR_4[3];

            }

            VAR_4 += 4;

            break;

        case WP_ID_FLOATINFO:

            if(VAR_27 != 4){

                av_log(VAR_0, AV_LOG_ERROR, "Invalid FLOATINFO, VAR_27 = %VAR_17\n", VAR_27);

                VAR_4 += VAR_21;

                continue;

            }

            s->float_flag = VAR_4[0];

            s->float_shift = VAR_4[1];

            s->float_max_exp = VAR_4[2];

            VAR_4 += 4;

            VAR_13 = 1;

            break;

        case WP_ID_DATA:

            s->sc.offset = VAR_4 - VAR_15;

            s->sc.VAR_27   = VAR_27 * 8;

            init_get_bits(&s->gb, VAR_4, VAR_27 * 8);

            s->data_size = VAR_27 * 8;

            VAR_4 += VAR_27;

            VAR_12 = 1;

            break;

        case WP_ID_EXTRABITS:

            if(VAR_27 <= 4){

                av_log(VAR_0, AV_LOG_ERROR, "Invalid EXTRABITS, VAR_27 = %VAR_17\n", VAR_27);

                VAR_4 += VAR_27;

                continue;

            }

            s->extra_sc.offset = VAR_4 - VAR_15;

            s->extra_sc.VAR_27   = VAR_27 * 8;

            init_get_bits(&s->gb_extra_bits, VAR_4, VAR_27 * 8);

            s->crc_extra_bits = get_bits_long(&s->gb_extra_bits, 32);

            VAR_4 += VAR_27;

            s->got_extra_bits = 1;

            break;

        case WP_ID_CHANINFO:

            if(VAR_27 <= 1){

                av_log(VAR_0, AV_LOG_ERROR, "Insufficient channel information\n");

                return -1;

            }

            VAR_25 = *VAR_4++;

            switch(VAR_27 - 2){

            case 0:

                VAR_26 = *VAR_4;

                break;

            case 1:

                VAR_26 = AV_RL16(VAR_4);

                break;

            case 2:

                VAR_26 = AV_RL24(VAR_4);

                break;

            case 3:

                VAR_26 = AV_RL32(VAR_4);

                break;

            case 5:

                VAR_25 |= (VAR_4[1] & 0xF) << 8;

                VAR_26 = AV_RL24(VAR_4 + 2);

                break;

            default:

                av_log(VAR_0, AV_LOG_ERROR, "Invalid channel info VAR_27 %d\n", VAR_27);

                VAR_25 = VAR_0->channels;

                VAR_26 = VAR_0->channel_layout;

            }

            if(VAR_25 != VAR_0->channels){

                av_log(VAR_0, AV_LOG_ERROR, "Block reports total %d channels, decoder believes it's %d channels\n",

                       VAR_25, VAR_0->channels);

                return -1;

            }

            if(!VAR_0->channel_layout)

                VAR_0->channel_layout = VAR_26;

            VAR_4 += VAR_27 - 1;

            break;

        default:

            VAR_4 += VAR_27;

        }

        if(VAR_19 & WP_IDF_ODD) VAR_4++;

    }



        if(!VAR_8){

            av_log(VAR_0, AV_LOG_ERROR, "No block with decorrelation terms\n");

            return -1;

        }

        if(!VAR_9){

            av_log(VAR_0, AV_LOG_ERROR, "No block with decorrelation VAR_22\n");

            return -1;

        }

        if(!VAR_10){

            av_log(VAR_0, AV_LOG_ERROR, "No block with decorrelation VAR_6\n");

            return -1;

        }

        if(!VAR_11){

            av_log(VAR_0, AV_LOG_ERROR, "No block with entropy info\n");

            return -1;

        }

        if(s->hybrid && !VAR_14){

            av_log(VAR_0, AV_LOG_ERROR, "Hybrid config not found\n");

            return -1;

        }

        if(!VAR_12){

            av_log(VAR_0, AV_LOG_ERROR, "Packed VAR_6 not found\n");

            return -1;

        }

        if(!VAR_13 && VAR_0->sample_fmt == AV_SAMPLE_FMT_FLT){

            av_log(VAR_0, AV_LOG_ERROR, "Float information not found\n");

            return -1;

        }

        if(s->got_extra_bits && VAR_0->sample_fmt != AV_SAMPLE_FMT_FLT){

            const int VAR_27 = get_bits_left(&s->gb_extra_bits);

            const int VAR_27 = s->VAR_6 * s->extra_bits << s->stereo_in;

            if(VAR_27 < VAR_27){

                av_log(VAR_0, AV_LOG_ERROR, "Too small EXTRABITS\n");

                s->got_extra_bits = 0;

            }

        }



    if(s->stereo_in){

        if(VAR_0->sample_fmt == AV_SAMPLE_FMT_S16)

            VAR_7 = wv_unpack_stereo(s, &s->gb, VAR_6, AV_SAMPLE_FMT_S16);

        else if(VAR_0->sample_fmt == AV_SAMPLE_FMT_S32)

            VAR_7 = wv_unpack_stereo(s, &s->gb, VAR_6, AV_SAMPLE_FMT_S32);

        else

            VAR_7 = wv_unpack_stereo(s, &s->gb, VAR_6, AV_SAMPLE_FMT_FLT);



        if (VAR_7 < 0)

            return -1;



        VAR_7 >>= 1;

    }else{

        const int VAR_28 = VAR_0->channels;



        if(VAR_0->sample_fmt == AV_SAMPLE_FMT_S16)

            VAR_7 = wv_unpack_mono(s, &s->gb, VAR_6, AV_SAMPLE_FMT_S16);

        else if(VAR_0->sample_fmt == AV_SAMPLE_FMT_S32)

            VAR_7 = wv_unpack_mono(s, &s->gb, VAR_6, AV_SAMPLE_FMT_S32);

        else

            VAR_7 = wv_unpack_mono(s, &s->gb, VAR_6, AV_SAMPLE_FMT_FLT);



        if (VAR_7 < 0)

            return -1;



        if(s->stereo && VAR_0->sample_fmt == AV_SAMPLE_FMT_S16){

            int16_t *VAR_30 = (int16_t*)VAR_6 + 1;

            int16_t *VAR_31 = (int16_t*)VAR_6;

            int VAR_32 = VAR_7;

            while(VAR_32--){

                *VAR_30 = *VAR_31;

                VAR_31 += VAR_28;

                VAR_30 += VAR_28;

            }

        }else if(s->stereo && VAR_0->sample_fmt == AV_SAMPLE_FMT_S32){

            int32_t *VAR_30 = (int32_t*)VAR_6 + 1;

            int32_t *VAR_31 = (int32_t*)VAR_6;

            int VAR_32 = VAR_7;

            while(VAR_32--){

                *VAR_30 = *VAR_31;

                VAR_31 += VAR_28;

                VAR_30 += VAR_28;

            }

        }else if(s->stereo){

            float *VAR_30 = (float*)VAR_6 + 1;

            float *VAR_31 = (float*)VAR_6;

            int VAR_32 = VAR_7;

            while(VAR_32--){

                *VAR_30 = *VAR_31;

                VAR_31 += VAR_28;

                VAR_30 += VAR_28;

            }

        }

    }



    *VAR_3 = 1;



    return VAR_7 * VAR_24;

}

static int FUNC_0(AVCodecContext *VAR_0, int VAR_1,
void *VAR_2, int *VAR_3,
const uint8_t *VAR_4, int VAR_5)
{ WavpackContext *wc = VAR_0->priv_data; WavpackFrameContext *s; void *VAR_6 = VAR_2; int VAR_7; int VAR_8 = 0, VAR_9 = 0, VAR_10 = 0, VAR_11 = 0, VAR_12 = 0, VAR_13 = 0; int VAR_14 = 0; const uint8_t* VAR_15 = VAR_4; const uint8_t* VAR_16 = VAR_4 + VAR_5; int VAR_17, VAR_18, VAR_19, VAR_27, VAR_21, VAR_22, VAR_23; int VAR_24, VAR_25, VAR_26; if (VAR_5 == 0){ *VAR_3 = 0; return 0; } if(VAR_1 >= wc->fdec_num && wv_alloc_frame_context(wc) < 0){ av_log(VAR_0, AV_LOG_ERROR, "Error creating frame decode context\n"); return -1; } s = wc->fdec[VAR_1]; if(!s){ av_log(VAR_0, AV_LOG_ERROR, "Context for block %d is not present\n", VAR_1); return -1; } memset(s->decorr, 0, MAX_TERMS * sizeof(Decorr)); memset(s->ch, 0, sizeof(s->ch)); s->extra_bits = 0; s->and = s->or = s->shift = 0; s->got_extra_bits = 0; if(!wc->mkv_mode){ s->VAR_6 = AV_RL32(VAR_4); VAR_4 += 4; if(!s->VAR_6){ *VAR_3 = 0; return 0; } }else{ s->VAR_6 = wc->VAR_6; } s->frame_flags = AV_RL32(VAR_4); VAR_4 += 4; if(s->frame_flags&0x80){ VAR_0->sample_fmt = AV_SAMPLE_FMT_FLT; } else if((s->frame_flags&0x03) <= 1){ VAR_0->sample_fmt = AV_SAMPLE_FMT_S16; } else { VAR_0->sample_fmt = AV_SAMPLE_FMT_S32; } VAR_24 = av_get_bytes_per_sample(VAR_0->sample_fmt); VAR_6 = (uint8_t*)VAR_6 + VAR_24 * wc->ch_offset; s->stereo = !(s->frame_flags & WV_MONO); s->stereo_in = (s->frame_flags & WV_FALSE_STEREO) ? 0 : s->stereo; s->joint = s->frame_flags & WV_JOINT_STEREO; s->hybrid = s->frame_flags & WV_HYBRID_MODE; s->hybrid_bitrate = s->frame_flags & WV_HYBRID_BITRATE; s->hybrid_maxclip = 1 << ((((s->frame_flags & 0x03) + 1) << 3) - 1); s->post_shift = 8 * (VAR_24-1-(s->frame_flags&0x03)) + ((s->frame_flags >> 13) & 0x1f); s->CRC = AV_RL32(VAR_4); VAR_4 += 4; if(wc->mkv_mode)
VAR_4 += 4; wc->ch_offset += 1 + s->stereo; while(VAR_4 < VAR_16){ VAR_19 = *VAR_4++; VAR_27 = *VAR_4++; if(VAR_19 & WP_IDF_LONG) { VAR_27 |= (*VAR_4++) << 8; VAR_27 |= (*VAR_4++) << 16; } VAR_27 <<= 1; VAR_21 = VAR_27; if(VAR_19 & WP_IDF_ODD) VAR_27--; if(VAR_27 < 0){ av_log(VAR_0, AV_LOG_ERROR, "Got incorrect block %02X with VAR_27 %VAR_17\n", VAR_19, VAR_27); break; } if(VAR_4 + VAR_21 > VAR_16){ av_log(VAR_0, AV_LOG_ERROR, "Block VAR_27 %VAR_17 is out of bounds\n", VAR_27); break; } if(VAR_19 & WP_IDF_IGNORE){ VAR_4 += VAR_21; continue; } switch(VAR_19 & WP_IDF_MASK){ case WP_ID_DECTERMS:
if(VAR_27 > MAX_TERMS){ av_log(VAR_0, AV_LOG_ERROR, "Too many decorrelation terms\n"); s->terms = 0; VAR_4 += VAR_21; continue; } s->terms = VAR_27; for(VAR_17 = 0; VAR_17 < s->terms; VAR_17++) { s->decorr[s->terms - VAR_17 - 1].value = (*VAR_4 & 0x1F) - 5; s->decorr[s->terms - VAR_17 - 1].delta = *VAR_4 >> 5; VAR_4++; } VAR_8 = 1; break; case WP_ID_DECWEIGHTS:
if(!VAR_8){ av_log(VAR_0, AV_LOG_ERROR, "No decorrelation terms met\n"); continue; } VAR_22 = VAR_27 >> s->stereo_in; if(VAR_22 > MAX_TERMS || VAR_22 > s->terms){ av_log(VAR_0, AV_LOG_ERROR, "Too many decorrelation VAR_22\n"); VAR_4 += VAR_21; continue; } for(VAR_17 = 0; VAR_17 < VAR_22; VAR_17++) { VAR_23 = (int8_t)(*VAR_4++); s->decorr[s->terms - VAR_17 - 1].weightA = VAR_23 << 3; if(s->decorr[s->terms - VAR_17 - 1].weightA > 0)
s->decorr[s->terms - VAR_17 - 1].weightA += (s->decorr[s->terms - VAR_17 - 1].weightA + 64) >> 7; if(s->stereo_in){ VAR_23 = (int8_t)(*VAR_4++); s->decorr[s->terms - VAR_17 - 1].weightB = VAR_23 << 3; if(s->decorr[s->terms - VAR_17 - 1].weightB > 0)
s->decorr[s->terms - VAR_17 - 1].weightB += (s->decorr[s->terms - VAR_17 - 1].weightB + 64) >> 7; } } VAR_9 = 1; break; case WP_ID_DECSAMPLES:
if(!VAR_8){ av_log(VAR_0, AV_LOG_ERROR, "No decorrelation terms met\n"); continue; } VAR_23 = 0; for(VAR_17 = s->terms - 1; (VAR_17 >= 0) && (VAR_23 < VAR_27); VAR_17--) { if(s->decorr[VAR_17].value > 8){ s->decorr[VAR_17].samplesA[0] = wp_exp2(AV_RL16(VAR_4)); VAR_4 += 2; s->decorr[VAR_17].samplesA[1] = wp_exp2(AV_RL16(VAR_4)); VAR_4 += 2; if(s->stereo_in){ s->decorr[VAR_17].samplesB[0] = wp_exp2(AV_RL16(VAR_4)); VAR_4 += 2; s->decorr[VAR_17].samplesB[1] = wp_exp2(AV_RL16(VAR_4)); VAR_4 += 2; VAR_23 += 4; } VAR_23 += 4; }else if(s->decorr[VAR_17].value < 0){ s->decorr[VAR_17].samplesA[0] = wp_exp2(AV_RL16(VAR_4)); VAR_4 += 2; s->decorr[VAR_17].samplesB[0] = wp_exp2(AV_RL16(VAR_4)); VAR_4 += 2; VAR_23 += 4; }else{ for(VAR_18 = 0; VAR_18 < s->decorr[VAR_17].value; VAR_18++){ s->decorr[VAR_17].samplesA[VAR_18] = wp_exp2(AV_RL16(VAR_4)); VAR_4 += 2; if(s->stereo_in){ s->decorr[VAR_17].samplesB[VAR_18] = wp_exp2(AV_RL16(VAR_4)); VAR_4 += 2; } } VAR_23 += s->decorr[VAR_17].value * 2 * (s->stereo_in + 1); } } VAR_10 = 1; break; case WP_ID_ENTROPY:
if(VAR_27 != 6 * (s->stereo_in + 1)){ av_log(VAR_0, AV_LOG_ERROR, "Entropy vars VAR_27 should be %VAR_17, got %VAR_17", 6 * (s->stereo_in + 1), VAR_27); VAR_4 += VAR_21; continue; } for(VAR_18 = 0; VAR_18 <= s->stereo_in; VAR_18++){ for(VAR_17 = 0; VAR_17 < 3; VAR_17++){ s->ch[VAR_18].median[VAR_17] = wp_exp2(AV_RL16(VAR_4)); VAR_4 += 2; } } VAR_11 = 1; break; case WP_ID_HYBRID:
if(s->hybrid_bitrate){ for(VAR_17 = 0; VAR_17 <= s->stereo_in; VAR_17++){ s->ch[VAR_17].slow_level = wp_exp2(AV_RL16(VAR_4)); VAR_4 += 2; VAR_27 -= 2; } } for(VAR_17 = 0; VAR_17 < (s->stereo_in + 1); VAR_17++){ s->ch[VAR_17].bitrate_acc = AV_RL16(VAR_4) << 16; VAR_4 += 2; VAR_27 -= 2; } if(VAR_27 > 0){ for(VAR_17 = 0; VAR_17 < (s->stereo_in + 1); VAR_17++){ s->ch[VAR_17].bitrate_delta = wp_exp2((int16_t)AV_RL16(VAR_4)); VAR_4 += 2; } }else{ for(VAR_17 = 0; VAR_17 < (s->stereo_in + 1); VAR_17++) s->ch[VAR_17].bitrate_delta = 0; } VAR_14 = 1; break; case WP_ID_INT32INFO:
if(VAR_27 != 4){ av_log(VAR_0, AV_LOG_ERROR, "Invalid INT32INFO, VAR_27 = %VAR_17, sent_bits = %VAR_17\n", VAR_27, *VAR_4); VAR_4 += VAR_21; continue; } if(VAR_4[0])
s->extra_bits = VAR_4[0]; else if(VAR_4[1])
s->shift = VAR_4[1]; else if(VAR_4[2]){ s->and = s->or = 1; s->shift = VAR_4[2]; }else if(VAR_4[3]){ s->and = 1; s->shift = VAR_4[3]; } VAR_4 += 4; break; case WP_ID_FLOATINFO:
if(VAR_27 != 4){ av_log(VAR_0, AV_LOG_ERROR, "Invalid FLOATINFO, VAR_27 = %VAR_17\n", VAR_27); VAR_4 += VAR_21; continue; } s->float_flag = VAR_4[0]; s->float_shift = VAR_4[1]; s->float_max_exp = VAR_4[2]; VAR_4 += 4; VAR_13 = 1; break; case WP_ID_DATA:
s->sc.offset = VAR_4 - VAR_15; s->sc.VAR_27   = VAR_27 * 8; init_get_bits(&s->gb, VAR_4, VAR_27 * 8); s->data_size = VAR_27 * 8; VAR_4 += VAR_27; VAR_12 = 1; break; case WP_ID_EXTRABITS:
if(VAR_27 <= 4){ av_log(VAR_0, AV_LOG_ERROR, "Invalid EXTRABITS, VAR_27 = %VAR_17\n", VAR_27); VAR_4 += VAR_27; continue; } s->extra_sc.offset = VAR_4 - VAR_15; s->extra_sc.VAR_27   = VAR_27 * 8; init_get_bits(&s->gb_extra_bits, VAR_4, VAR_27 * 8); s->crc_extra_bits = get_bits_long(&s->gb_extra_bits, 32); VAR_4 += VAR_27; s->got_extra_bits = 1; break; case WP_ID_CHANINFO:
if(VAR_27 <= 1){ av_log(VAR_0, AV_LOG_ERROR, "Insufficient channel information\n"); return -1; } VAR_25 = *VAR_4++; switch(VAR_27 - 2){ case 0:
VAR_26 = *VAR_4; break; case 1:
VAR_26 = AV_RL16(VAR_4); break; case 2:
VAR_26 = AV_RL24(VAR_4); break; case 3:
VAR_26 = AV_RL32(VAR_4); break; case 5:
VAR_25 |= (VAR_4[1] & 0xF) << 8; VAR_26 = AV_RL24(VAR_4 + 2); break; default:
av_log(VAR_0, AV_LOG_ERROR, "Invalid channel info VAR_27 %d\n", VAR_27); VAR_25 = VAR_0->channels; VAR_26 = VAR_0->channel_layout; } if(VAR_25 != VAR_0->channels){ av_log(VAR_0, AV_LOG_ERROR, "Block reports total %d channels, decoder believes it's %d channels\n",
VAR_25, VAR_0->channels); return -1; } if(!VAR_0->channel_layout)
VAR_0->channel_layout = VAR_26; VAR_4 += VAR_27 - 1; break; default:
VAR_4 += VAR_27; } if(VAR_19 & WP_IDF_ODD) VAR_4++; } if(!VAR_8){ av_log(VAR_0, AV_LOG_ERROR, "No block with decorrelation terms\n"); return -1; } if(!VAR_9){ av_log(VAR_0, AV_LOG_ERROR, "No block with decorrelation VAR_22\n"); return -1; } if(!VAR_10){ av_log(VAR_0, AV_LOG_ERROR, "No block with decorrelation VAR_6\n"); return -1; } if(!VAR_11){ av_log(VAR_0, AV_LOG_ERROR, "No block with entropy info\n"); return -1; } if(s->hybrid && !VAR_14){ av_log(VAR_0, AV_LOG_ERROR, "Hybrid config not found\n"); return -1; } if(!VAR_12){ av_log(VAR_0, AV_LOG_ERROR, "Packed VAR_6 not found\n"); return -1; } if(!VAR_13 && VAR_0->sample_fmt == AV_SAMPLE_FMT_FLT){ av_log(VAR_0, AV_LOG_ERROR, "Float information not found\n"); return -1; } if(s->got_extra_bits && VAR_0->sample_fmt != AV_SAMPLE_FMT_FLT){ const int VAR_27 = get_bits_left(&s->gb_extra_bits); const int VAR_27 = s->VAR_6 * s->extra_bits << s->stereo_in; if(VAR_27 < VAR_27){ av_log(VAR_0, AV_LOG_ERROR, "Too small EXTRABITS\n"); s->got_extra_bits = 0; } } if(s->stereo_in){ if(VAR_0->sample_fmt == AV_SAMPLE_FMT_S16)
VAR_7 = wv_unpack_stereo(s, &s->gb, VAR_6, AV_SAMPLE_FMT_S16); else if(VAR_0->sample_fmt == AV_SAMPLE_FMT_S32)
VAR_7 = wv_unpack_stereo(s, &s->gb, VAR_6, AV_SAMPLE_FMT_S32); else
VAR_7 = wv_unpack_stereo(s, &s->gb, VAR_6, AV_SAMPLE_FMT_FLT); if (VAR_7 < 0)
return -1; VAR_7 >>= 1; }else{ const int VAR_28 = VAR_0->channels; if(VAR_0->sample_fmt == AV_SAMPLE_FMT_S16)
VAR_7 = wv_unpack_mono(s, &s->gb, VAR_6, AV_SAMPLE_FMT_S16); else if(VAR_0->sample_fmt == AV_SAMPLE_FMT_S32)
VAR_7 = wv_unpack_mono(s, &s->gb, VAR_6, AV_SAMPLE_FMT_S32); else
VAR_7 = wv_unpack_mono(s, &s->gb, VAR_6, AV_SAMPLE_FMT_FLT); if (VAR_7 < 0)
return -1; if(s->stereo && VAR_0->sample_fmt == AV_SAMPLE_FMT_S16){ int16_t *VAR_30 = (int16_t*)VAR_6 + 1; int16_t *VAR_31 = (int16_t*)VAR_6; int VAR_32 = VAR_7; while(VAR_32--){ *VAR_30 = *VAR_31; VAR_31 += VAR_28; VAR_30 += VAR_28; } }else if(s->stereo && VAR_0->sample_fmt == AV_SAMPLE_FMT_S32){ int32_t *VAR_30 = (int32_t*)VAR_6 + 1; int32_t *VAR_31 = (int32_t*)VAR_6; int VAR_32 = VAR_7; while(VAR_32--){ *VAR_30 = *VAR_31; VAR_31 += VAR_28; VAR_30 += VAR_28; } }else if(s->stereo){ float *VAR_30 = (float*)VAR_6 + 1; float *VAR_31 = (float*)VAR_6; int VAR_32 = VAR_7; while(VAR_32--){ *VAR_30 = *VAR_31; VAR_31 += VAR_28; VAR_30 += VAR_28; } } } *VAR_3 = 1; return VAR_7 * VAR_24; }