static int smka_decode_frame(AVCodecContext *avctx, void *data, int *data_size, uint8_t *buf, int buf_size)

{

    GetBitContext gb;

    HuffContext h[4];

    VLC vlc[4];

    int16_t *samples = data;

    int val;

    int i, res;

    int unp_size;

    int bits, stereo;

    int pred[2] = {0, 0};



    unp_size = AV_RL32(buf);



    init_get_bits(&gb, buf + 4, (buf_size - 4) * 8);



    if(!get_bits1(&gb)){

        av_log(avctx, AV_LOG_INFO, "Sound: no data\n");

        *data_size = 0;

        return 1;

    }

    stereo = get_bits1(&gb);

    bits = get_bits1(&gb);

    if ((unp_size << !bits) > *data_size) {

        av_log(avctx, AV_LOG_ERROR, "Frame is too large to fit in buffer\n");

        return -1;

    }



    memset(vlc, 0, sizeof(VLC) * 4);

    memset(h, 0, sizeof(HuffContext) * 4);

    // Initialize

    for(i = 0; i < (1 << (bits + stereo)); i++) {

        h[i].length = 256;

        h[i].maxlength = 0;

        h[i].current = 0;

        h[i].bits = av_mallocz(256 * 4);

        h[i].lengths = av_mallocz(256 * sizeof(int));

        h[i].values = av_mallocz(256 * sizeof(int));

        skip_bits1(&gb);

        smacker_decode_tree(&gb, &h[i], 0, 0);

        skip_bits1(&gb);

        if(h[i].current > 1) {

            res = init_vlc(&vlc[i], SMKTREE_BITS, h[i].length,

                    h[i].lengths, sizeof(int), sizeof(int),

                    h[i].bits, sizeof(uint32_t), sizeof(uint32_t), INIT_VLC_LE);

            if(res < 0) {

                av_log(avctx, AV_LOG_ERROR, "Cannot build VLC table\n");

                return -1;

            }

        }

    }

    if(bits) { //decode 16-bit data

        for(i = stereo; i >= 0; i--)

            pred[i] = bswap_16(get_bits(&gb, 16));

        for(i = 0; i < stereo; i++)

            *samples++ = pred[i];

        for(i = 0; i < unp_size / 2; i++) {

            if(i & stereo) {

                if(vlc[2].table)

                    res = get_vlc2(&gb, vlc[2].table, SMKTREE_BITS, 3);

                else

                    res = 0;

                val  = h[2].values[res];

                if(vlc[3].table)

                    res = get_vlc2(&gb, vlc[3].table, SMKTREE_BITS, 3);

                else

                    res = 0;

                val |= h[3].values[res] << 8;

                pred[1] += (int16_t)val;

                *samples++ = pred[1];

            } else {

                if(vlc[0].table)

                    res = get_vlc2(&gb, vlc[0].table, SMKTREE_BITS, 3);

                else

                    res = 0;

                val  = h[0].values[res];

                if(vlc[1].table)

                    res = get_vlc2(&gb, vlc[1].table, SMKTREE_BITS, 3);

                else

                    res = 0;

                val |= h[1].values[res] << 8;

                pred[0] += val;

                *samples++ = pred[0];

            }

        }

    } else { //8-bit data

        for(i = stereo; i >= 0; i--)

            pred[i] = get_bits(&gb, 8);

        for(i = 0; i < stereo; i++)

            *samples++ = (pred[i] - 0x80) << 8;

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

            if(i & stereo){

                if(vlc[1].table)

                    res = get_vlc2(&gb, vlc[1].table, SMKTREE_BITS, 3);

                else

                    res = 0;

                pred[1] += (int8_t)h[1].values[res];

                *samples++ = (pred[1] - 0x80) << 8;

            } else {

                if(vlc[0].table)

                    res = get_vlc2(&gb, vlc[0].table, SMKTREE_BITS, 3);

                else

                    res = 0;

                pred[0] += (int8_t)h[0].values[res];

                *samples++ = (pred[0] - 0x80) << 8;

            }

        }

        unp_size *= 2;

    }



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

        if(vlc[i].table)

            free_vlc(&vlc[i]);

        if(h[i].bits)

            av_free(h[i].bits);

        if(h[i].lengths)

            av_free(h[i].lengths);

        if(h[i].values)

            av_free(h[i].values);

    }



    *data_size = unp_size;

    return buf_size;

}

static int smka_decode_frame(AVCodecContext *avctx, void *data, int *data_size, uint8_t *buf, int buf_size)

{

    GetBitContext gb;

    HuffContext h[4];

    VLC vlc[4];

    int16_t *samples = data;

    int val;

    int i, res;

    int unp_size;

    int bits, stereo;

    int pred[2] = {0, 0};



    unp_size = AV_RL32(buf);



    init_get_bits(&gb, buf + 4, (buf_size - 4) * 8);



    if(!get_bits1(&gb)){

        av_log(avctx, AV_LOG_INFO, "Sound: no data\n");

        *data_size = 0;

        return 1;

    }

    stereo = get_bits1(&gb);

    bits = get_bits1(&gb);

    if ((unp_size << !bits) > *data_size) {

        av_log(avctx, AV_LOG_ERROR, "Frame is too large to fit in buffer\n");

        return -1;

    }



    memset(vlc, 0, sizeof(VLC) * 4);

    memset(h, 0, sizeof(HuffContext) * 4);

    

    for(i = 0; i < (1 << (bits + stereo)); i++) {

        h[i].length = 256;

        h[i].maxlength = 0;

        h[i].current = 0;

        h[i].bits = av_mallocz(256 * 4);

        h[i].lengths = av_mallocz(256 * sizeof(int));

        h[i].values = av_mallocz(256 * sizeof(int));

        skip_bits1(&gb);

        smacker_decode_tree(&gb, &h[i], 0, 0);

        skip_bits1(&gb);

        if(h[i].current > 1) {

            res = init_vlc(&vlc[i], SMKTREE_BITS, h[i].length,

                    h[i].lengths, sizeof(int), sizeof(int),

                    h[i].bits, sizeof(uint32_t), sizeof(uint32_t), INIT_VLC_LE);

            if(res < 0) {

                av_log(avctx, AV_LOG_ERROR, "Cannot build VLC table\n");

                return -1;

            }

        }

    }

    if(bits) { 

        for(i = stereo; i >= 0; i--)

            pred[i] = bswap_16(get_bits(&gb, 16));

        for(i = 0; i < stereo; i++)

            *samples++ = pred[i];

        for(i = 0; i < unp_size / 2; i++) {

            if(i & stereo) {

                if(vlc[2].table)

                    res = get_vlc2(&gb, vlc[2].table, SMKTREE_BITS, 3);

                else

                    res = 0;

                val  = h[2].values[res];

                if(vlc[3].table)

                    res = get_vlc2(&gb, vlc[3].table, SMKTREE_BITS, 3);

                else

                    res = 0;

                val |= h[3].values[res] << 8;

                pred[1] += (int16_t)val;

                *samples++ = pred[1];

            } else {

                if(vlc[0].table)

                    res = get_vlc2(&gb, vlc[0].table, SMKTREE_BITS, 3);

                else

                    res = 0;

                val  = h[0].values[res];

                if(vlc[1].table)

                    res = get_vlc2(&gb, vlc[1].table, SMKTREE_BITS, 3);

                else

                    res = 0;

                val |= h[1].values[res] << 8;

                pred[0] += val;

                *samples++ = pred[0];

            }

        }

    } else { 

        for(i = stereo; i >= 0; i--)

            pred[i] = get_bits(&gb, 8);

        for(i = 0; i < stereo; i++)

            *samples++ = (pred[i] - 0x80) << 8;

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

            if(i & stereo){

                if(vlc[1].table)

                    res = get_vlc2(&gb, vlc[1].table, SMKTREE_BITS, 3);

                else

                    res = 0;

                pred[1] += (int8_t)h[1].values[res];

                *samples++ = (pred[1] - 0x80) << 8;

            } else {

                if(vlc[0].table)

                    res = get_vlc2(&gb, vlc[0].table, SMKTREE_BITS, 3);

                else

                    res = 0;

                pred[0] += (int8_t)h[0].values[res];

                *samples++ = (pred[0] - 0x80) << 8;

            }

        }

        unp_size *= 2;

    }



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

        if(vlc[i].table)

            free_vlc(&vlc[i]);

        if(h[i].bits)

            av_free(h[i].bits);

        if(h[i].lengths)

            av_free(h[i].lengths);

        if(h[i].values)

            av_free(h[i].values);

    }



    *data_size = unp_size;

    return buf_size;

}

    if ((unp_size << !bits) > *data_size) {
static int FUNC_0(AVCodecContext *VAR_0, void *VAR_1, int *VAR_2, uint8_t *VAR_3, int VAR_4)

{

    GetBitContext gb;

    HuffContext h[4];

    VLC vlc[4];

    int16_t *samples = VAR_1;

    int VAR_5;

    int VAR_6, VAR_7;

    int VAR_8;

    int VAR_9, VAR_10;

    int VAR_11[2] = {0, 0};



    VAR_8 = AV_RL32(VAR_3);



    init_get_bits(&gb, VAR_3 + 4, (VAR_4 - 4) * 8);



    if(!get_bits1(&gb)){

        av_log(VAR_0, AV_LOG_INFO, "Sound: no VAR_1\n");

        *VAR_2 = 0;

        return 1;

    }

    VAR_10 = get_bits1(&gb);

    VAR_9 = get_bits1(&gb);

    if ((VAR_8 << !VAR_9) > *VAR_2) {

        av_log(VAR_0, AV_LOG_ERROR, "Frame is too large to fit in buffer\n");

        return -1;

    }



    memset(vlc, 0, sizeof(VLC) * 4);

    memset(h, 0, sizeof(HuffContext) * 4);

    

    for(VAR_6 = 0; VAR_6 < (1 << (VAR_9 + VAR_10)); VAR_6++) {

        h[VAR_6].length = 256;

        h[VAR_6].maxlength = 0;

        h[VAR_6].current = 0;

        h[VAR_6].VAR_9 = av_mallocz(256 * 4);

        h[VAR_6].lengths = av_mallocz(256 * sizeof(int));

        h[VAR_6].values = av_mallocz(256 * sizeof(int));

        skip_bits1(&gb);

        smacker_decode_tree(&gb, &h[VAR_6], 0, 0);

        skip_bits1(&gb);

        if(h[VAR_6].current > 1) {

            VAR_7 = init_vlc(&vlc[VAR_6], SMKTREE_BITS, h[VAR_6].length,

                    h[VAR_6].lengths, sizeof(int), sizeof(int),

                    h[VAR_6].VAR_9, sizeof(uint32_t), sizeof(uint32_t), INIT_VLC_LE);

            if(VAR_7 < 0) {

                av_log(VAR_0, AV_LOG_ERROR, "Cannot build VLC table\n");

                return -1;

            }

        }

    }

    if(VAR_9) { 

        for(VAR_6 = VAR_10; VAR_6 >= 0; VAR_6--)

            VAR_11[VAR_6] = bswap_16(get_bits(&gb, 16));

        for(VAR_6 = 0; VAR_6 < VAR_10; VAR_6++)

            *samples++ = VAR_11[VAR_6];

        for(VAR_6 = 0; VAR_6 < VAR_8 / 2; VAR_6++) {

            if(VAR_6 & VAR_10) {

                if(vlc[2].table)

                    VAR_7 = get_vlc2(&gb, vlc[2].table, SMKTREE_BITS, 3);

                else

                    VAR_7 = 0;

                VAR_5  = h[2].values[VAR_7];

                if(vlc[3].table)

                    VAR_7 = get_vlc2(&gb, vlc[3].table, SMKTREE_BITS, 3);

                else

                    VAR_7 = 0;

                VAR_5 |= h[3].values[VAR_7] << 8;

                VAR_11[1] += (int16_t)VAR_5;

                *samples++ = VAR_11[1];

            } else {

                if(vlc[0].table)

                    VAR_7 = get_vlc2(&gb, vlc[0].table, SMKTREE_BITS, 3);

                else

                    VAR_7 = 0;

                VAR_5  = h[0].values[VAR_7];

                if(vlc[1].table)

                    VAR_7 = get_vlc2(&gb, vlc[1].table, SMKTREE_BITS, 3);

                else

                    VAR_7 = 0;

                VAR_5 |= h[1].values[VAR_7] << 8;

                VAR_11[0] += VAR_5;

                *samples++ = VAR_11[0];

            }

        }

    } else { 

        for(VAR_6 = VAR_10; VAR_6 >= 0; VAR_6--)

            VAR_11[VAR_6] = get_bits(&gb, 8);

        for(VAR_6 = 0; VAR_6 < VAR_10; VAR_6++)

            *samples++ = (VAR_11[VAR_6] - 0x80) << 8;

        for(VAR_6 = 0; VAR_6 < VAR_8; VAR_6++) {

            if(VAR_6 & VAR_10){

                if(vlc[1].table)

                    VAR_7 = get_vlc2(&gb, vlc[1].table, SMKTREE_BITS, 3);

                else

                    VAR_7 = 0;

                VAR_11[1] += (int8_t)h[1].values[VAR_7];

                *samples++ = (VAR_11[1] - 0x80) << 8;

            } else {

                if(vlc[0].table)

                    VAR_7 = get_vlc2(&gb, vlc[0].table, SMKTREE_BITS, 3);

                else

                    VAR_7 = 0;

                VAR_11[0] += (int8_t)h[0].values[VAR_7];

                *samples++ = (VAR_11[0] - 0x80) << 8;

            }

        }

        VAR_8 *= 2;

    }



    for(VAR_6 = 0; VAR_6 < 4; VAR_6++) {

        if(vlc[VAR_6].table)

            free_vlc(&vlc[VAR_6]);

        if(h[VAR_6].VAR_9)

            av_free(h[VAR_6].VAR_9);

        if(h[VAR_6].lengths)

            av_free(h[VAR_6].lengths);

        if(h[VAR_6].values)

            av_free(h[VAR_6].values);

    }



    *VAR_2 = VAR_8;

    return VAR_4;

}

static int FUNC_0(AVCodecContext *VAR_0, void *VAR_1, int *VAR_2, uint8_t *VAR_3, int VAR_4)
{ GetBitContext gb; HuffContext h[4]; VLC vlc[4]; int16_t *samples = VAR_1; int VAR_5; int VAR_6, VAR_7; int VAR_8; int VAR_9, VAR_10; int VAR_11[2] = {0, 0}; VAR_8 = AV_RL32(VAR_3); init_get_bits(&gb, VAR_3 + 4, (VAR_4 - 4) * 8); if(!get_bits1(&gb)){ av_log(VAR_0, AV_LOG_INFO, "Sound: no VAR_1\n"); *VAR_2 = 0; return 1; } VAR_10 = get_bits1(&gb); VAR_9 = get_bits1(&gb); if ((VAR_8 << !VAR_9) > *VAR_2) { av_log(VAR_0, AV_LOG_ERROR, "Frame is too large to fit in buffer\n"); return -1; } memset(vlc, 0, sizeof(VLC) * 4); memset(h, 0, sizeof(HuffContext) * 4); for(VAR_6 = 0; VAR_6 < (1 << (VAR_9 + VAR_10)); VAR_6++) { h[VAR_6].length = 256; h[VAR_6].maxlength = 0; h[VAR_6].current = 0; h[VAR_6].VAR_9 = av_mallocz(256 * 4); h[VAR_6].lengths = av_mallocz(256 * sizeof(int)); h[VAR_6].values = av_mallocz(256 * sizeof(int)); skip_bits1(&gb); smacker_decode_tree(&gb, &h[VAR_6], 0, 0); skip_bits1(&gb); if(h[VAR_6].current > 1) { VAR_7 = init_vlc(&vlc[VAR_6], SMKTREE_BITS, h[VAR_6].length,
h[VAR_6].lengths, sizeof(int), sizeof(int),
h[VAR_6].VAR_9, sizeof(uint32_t), sizeof(uint32_t), INIT_VLC_LE); if(VAR_7 < 0) { av_log(VAR_0, AV_LOG_ERROR, "Cannot build VLC table\n"); return -1; } } } if(VAR_9) { for(VAR_6 = VAR_10; VAR_6 >= 0; VAR_6--) VAR_11[VAR_6] = bswap_16(get_bits(&gb, 16)); for(VAR_6 = 0; VAR_6 < VAR_10; VAR_6++) *samples++ = VAR_11[VAR_6]; for(VAR_6 = 0; VAR_6 < VAR_8 / 2; VAR_6++) { if(VAR_6 & VAR_10) { if(vlc[2].table)
VAR_7 = get_vlc2(&gb, vlc[2].table, SMKTREE_BITS, 3); else
VAR_7 = 0; VAR_5  = h[2].values[VAR_7]; if(vlc[3].table)
VAR_7 = get_vlc2(&gb, vlc[3].table, SMKTREE_BITS, 3); else
VAR_7 = 0; VAR_5 |= h[3].values[VAR_7] << 8; VAR_11[1] += (int16_t)VAR_5; *samples++ = VAR_11[1]; } else { if(vlc[0].table)
VAR_7 = get_vlc2(&gb, vlc[0].table, SMKTREE_BITS, 3); else
VAR_7 = 0; VAR_5  = h[0].values[VAR_7]; if(vlc[1].table)
VAR_7 = get_vlc2(&gb, vlc[1].table, SMKTREE_BITS, 3); else
VAR_7 = 0; VAR_5 |= h[1].values[VAR_7] << 8; VAR_11[0] += VAR_5; *samples++ = VAR_11[0]; } } } else { for(VAR_6 = VAR_10; VAR_6 >= 0; VAR_6--) VAR_11[VAR_6] = get_bits(&gb, 8); for(VAR_6 = 0; VAR_6 < VAR_10; VAR_6++) *samples++ = (VAR_11[VAR_6] - 0x80) << 8; for(VAR_6 = 0; VAR_6 < VAR_8; VAR_6++) { if(VAR_6 & VAR_10){ if(vlc[1].table)
VAR_7 = get_vlc2(&gb, vlc[1].table, SMKTREE_BITS, 3); else
VAR_7 = 0; VAR_11[1] += (int8_t)h[1].values[VAR_7]; *samples++ = (VAR_11[1] - 0x80) << 8; } else { if(vlc[0].table)
VAR_7 = get_vlc2(&gb, vlc[0].table, SMKTREE_BITS, 3); else
VAR_7 = 0; VAR_11[0] += (int8_t)h[0].values[VAR_7]; *samples++ = (VAR_11[0] - 0x80) << 8; } } VAR_8 *= 2; } for(VAR_6 = 0; VAR_6 < 4; VAR_6++) { if(vlc[VAR_6].table)
free_vlc(&vlc[VAR_6]); if(h[VAR_6].VAR_9)
av_free(h[VAR_6].VAR_9); if(h[VAR_6].lengths)
av_free(h[VAR_6].lengths); if(h[VAR_6].values)
av_free(h[VAR_6].values); } *VAR_2 = VAR_8; return VAR_4; }