static int tm2_read_stream(TM2Context *ctx, const uint8_t *buf, int stream_id) {

    int i;

    int cur = 0;

    int skip = 0;

    int len, toks;

    TM2Codes codes;



    /* get stream length in dwords */

    len = AV_RB32(buf); buf += 4; cur += 4;

    skip = len * 4 + 4;



    if(len == 0)

        return 4;



    toks = AV_RB32(buf); buf += 4; cur += 4;

    if(toks & 1) {

        len = AV_RB32(buf); buf += 4; cur += 4;

        if(len == TM2_ESCAPE) {

            len = AV_RB32(buf); buf += 4; cur += 4;

        }

        if(len > 0) {

            init_get_bits(&ctx->gb, buf, (skip - cur) * 8);

            if(tm2_read_deltas(ctx, stream_id) == -1)

                return -1;

            buf += ((get_bits_count(&ctx->gb) + 31) >> 5) << 2;

            cur += ((get_bits_count(&ctx->gb) + 31) >> 5) << 2;

        }

    }

    /* skip unused fields */

    if(AV_RB32(buf) == TM2_ESCAPE) {

        buf += 4; cur += 4; /* some unknown length - could be escaped too */

    }

    buf += 4; cur += 4;

    buf += 4; cur += 4; /* unused by decoder */



    init_get_bits(&ctx->gb, buf, (skip - cur) * 8);

    if(tm2_build_huff_table(ctx, &codes) == -1)

        return -1;

    buf += ((get_bits_count(&ctx->gb) + 31) >> 5) << 2;

    cur += ((get_bits_count(&ctx->gb) + 31) >> 5) << 2;



    toks >>= 1;

    /* check if we have sane number of tokens */

    if((toks < 0) || (toks > 0xFFFFFF)){

        av_log(ctx->avctx, AV_LOG_ERROR, "Incorrect number of tokens: %i\n", toks);

        tm2_free_codes(&codes);

        return -1;

    }

    ctx->tokens[stream_id] = av_realloc(ctx->tokens[stream_id], toks * sizeof(int));

    ctx->tok_lens[stream_id] = toks;

    len = AV_RB32(buf); buf += 4; cur += 4;

    if(len > 0) {

        init_get_bits(&ctx->gb, buf, (skip - cur) * 8);

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

            ctx->tokens[stream_id][i] = tm2_get_token(&ctx->gb, &codes);

    } else {

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

            ctx->tokens[stream_id][i] = codes.recode[0];

    }

    tm2_free_codes(&codes);



    return skip;

}

static int tm2_read_stream(TM2Context *ctx, const uint8_t *buf, int stream_id) {

    int i;

    int cur = 0;

    int skip = 0;

    int len, toks;

    TM2Codes codes;



    

    len = AV_RB32(buf); buf += 4; cur += 4;

    skip = len * 4 + 4;



    if(len == 0)

        return 4;



    toks = AV_RB32(buf); buf += 4; cur += 4;

    if(toks & 1) {

        len = AV_RB32(buf); buf += 4; cur += 4;

        if(len == TM2_ESCAPE) {

            len = AV_RB32(buf); buf += 4; cur += 4;

        }

        if(len > 0) {

            init_get_bits(&ctx->gb, buf, (skip - cur) * 8);

            if(tm2_read_deltas(ctx, stream_id) == -1)

                return -1;

            buf += ((get_bits_count(&ctx->gb) + 31) >> 5) << 2;

            cur += ((get_bits_count(&ctx->gb) + 31) >> 5) << 2;

        }

    }

    

    if(AV_RB32(buf) == TM2_ESCAPE) {

        buf += 4; cur += 4; 

    }

    buf += 4; cur += 4;

    buf += 4; cur += 4; 



    init_get_bits(&ctx->gb, buf, (skip - cur) * 8);

    if(tm2_build_huff_table(ctx, &codes) == -1)

        return -1;

    buf += ((get_bits_count(&ctx->gb) + 31) >> 5) << 2;

    cur += ((get_bits_count(&ctx->gb) + 31) >> 5) << 2;



    toks >>= 1;

    

    if((toks < 0) || (toks > 0xFFFFFF)){

        av_log(ctx->avctx, AV_LOG_ERROR, "Incorrect number of tokens: %i\n", toks);

        tm2_free_codes(&codes);

        return -1;

    }

    ctx->tokens[stream_id] = av_realloc(ctx->tokens[stream_id], toks * sizeof(int));

    ctx->tok_lens[stream_id] = toks;

    len = AV_RB32(buf); buf += 4; cur += 4;

    if(len > 0) {

        init_get_bits(&ctx->gb, buf, (skip - cur) * 8);

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

            ctx->tokens[stream_id][i] = tm2_get_token(&ctx->gb, &codes);

    } else {

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

            ctx->tokens[stream_id][i] = codes.recode[0];

    }

    tm2_free_codes(&codes);



    return skip;

}

static int tm2_read_stream(TM2Context *ctx, const uint8_t *buf, int stream_id) {         for(i = 0; i < toks; i++)
static int FUNC_0(TM2Context *VAR_0, const uint8_t *VAR_1, int VAR_2) {

    int VAR_3;

    int VAR_4 = 0;

    int VAR_5 = 0;

    int VAR_6, VAR_7;

    TM2Codes codes;



    

    VAR_6 = AV_RB32(VAR_1); VAR_1 += 4; VAR_4 += 4;

    VAR_5 = VAR_6 * 4 + 4;



    if(VAR_6 == 0)

        return 4;



    VAR_7 = AV_RB32(VAR_1); VAR_1 += 4; VAR_4 += 4;

    if(VAR_7 & 1) {

        VAR_6 = AV_RB32(VAR_1); VAR_1 += 4; VAR_4 += 4;

        if(VAR_6 == TM2_ESCAPE) {

            VAR_6 = AV_RB32(VAR_1); VAR_1 += 4; VAR_4 += 4;

        }

        if(VAR_6 > 0) {

            init_get_bits(&VAR_0->gb, VAR_1, (VAR_5 - VAR_4) * 8);

            if(tm2_read_deltas(VAR_0, VAR_2) == -1)

                return -1;

            VAR_1 += ((get_bits_count(&VAR_0->gb) + 31) >> 5) << 2;

            VAR_4 += ((get_bits_count(&VAR_0->gb) + 31) >> 5) << 2;

        }

    }

    

    if(AV_RB32(VAR_1) == TM2_ESCAPE) {

        VAR_1 += 4; VAR_4 += 4; 

    }

    VAR_1 += 4; VAR_4 += 4;

    VAR_1 += 4; VAR_4 += 4; 



    init_get_bits(&VAR_0->gb, VAR_1, (VAR_5 - VAR_4) * 8);

    if(tm2_build_huff_table(VAR_0, &codes) == -1)

        return -1;

    VAR_1 += ((get_bits_count(&VAR_0->gb) + 31) >> 5) << 2;

    VAR_4 += ((get_bits_count(&VAR_0->gb) + 31) >> 5) << 2;



    VAR_7 >>= 1;

    

    if((VAR_7 < 0) || (VAR_7 > 0xFFFFFF)){

        av_log(VAR_0->avctx, AV_LOG_ERROR, "Incorrect number of tokens: %VAR_3\n", VAR_7);

        tm2_free_codes(&codes);

        return -1;

    }

    VAR_0->tokens[VAR_2] = av_realloc(VAR_0->tokens[VAR_2], VAR_7 * sizeof(int));

    VAR_0->tok_lens[VAR_2] = VAR_7;

    VAR_6 = AV_RB32(VAR_1); VAR_1 += 4; VAR_4 += 4;

    if(VAR_6 > 0) {

        init_get_bits(&VAR_0->gb, VAR_1, (VAR_5 - VAR_4) * 8);

        for(VAR_3 = 0; VAR_3 < VAR_7; VAR_3++)

            VAR_0->tokens[VAR_2][VAR_3] = tm2_get_token(&VAR_0->gb, &codes);

    } else {

        for(VAR_3 = 0; VAR_3 < VAR_7; VAR_3++)

            VAR_0->tokens[VAR_2][VAR_3] = codes.recode[0];

    }

    tm2_free_codes(&codes);



    return VAR_5;

}

static int FUNC_0(TM2Context *VAR_0, const uint8_t *VAR_1, int VAR_2) { int VAR_3; int VAR_4 = 0; int VAR_5 = 0; int VAR_6, VAR_7; TM2Codes codes; VAR_6 = AV_RB32(VAR_1); VAR_1 += 4; VAR_4 += 4; VAR_5 = VAR_6 * 4 + 4; if(VAR_6 == 0)
return 4; VAR_7 = AV_RB32(VAR_1); VAR_1 += 4; VAR_4 += 4; if(VAR_7 & 1) { VAR_6 = AV_RB32(VAR_1); VAR_1 += 4; VAR_4 += 4; if(VAR_6 == TM2_ESCAPE) { VAR_6 = AV_RB32(VAR_1); VAR_1 += 4; VAR_4 += 4; } if(VAR_6 > 0) { init_get_bits(&VAR_0->gb, VAR_1, (VAR_5 - VAR_4) * 8); if(tm2_read_deltas(VAR_0, VAR_2) == -1)
return -1; VAR_1 += ((get_bits_count(&VAR_0->gb) + 31) >> 5) << 2; VAR_4 += ((get_bits_count(&VAR_0->gb) + 31) >> 5) << 2; } } if(AV_RB32(VAR_1) == TM2_ESCAPE) { VAR_1 += 4; VAR_4 += 4; } VAR_1 += 4; VAR_4 += 4; VAR_1 += 4; VAR_4 += 4; init_get_bits(&VAR_0->gb, VAR_1, (VAR_5 - VAR_4) * 8); if(tm2_build_huff_table(VAR_0, &codes) == -1)
return -1; VAR_1 += ((get_bits_count(&VAR_0->gb) + 31) >> 5) << 2; VAR_4 += ((get_bits_count(&VAR_0->gb) + 31) >> 5) << 2; VAR_7 >>= 1; if((VAR_7 < 0) || (VAR_7 > 0xFFFFFF)){ av_log(VAR_0->avctx, AV_LOG_ERROR, "Incorrect number of tokens: %VAR_3\n", VAR_7); tm2_free_codes(&codes); return -1; } VAR_0->tokens[VAR_2] = av_realloc(VAR_0->tokens[VAR_2], VAR_7 * sizeof(int)); VAR_0->tok_lens[VAR_2] = VAR_7; VAR_6 = AV_RB32(VAR_1); VAR_1 += 4; VAR_4 += 4; if(VAR_6 > 0) { init_get_bits(&VAR_0->gb, VAR_1, (VAR_5 - VAR_4) * 8); for(VAR_3 = 0; VAR_3 < VAR_7; VAR_3++) VAR_0->tokens[VAR_2][VAR_3] = tm2_get_token(&VAR_0->gb, &codes); } else { for(VAR_3 = 0; VAR_3 < VAR_7; VAR_3++) VAR_0->tokens[VAR_2][VAR_3] = codes.recode[0]; } tm2_free_codes(&codes); return VAR_5; }