static int decode_segment(TAKDecContext *s, int8_t mode, int32_t *decoded, int len)

{

    struct CParam code;

    GetBitContext *gb = &s->gb;

    int i;



    if (!mode) {

        memset(decoded, 0, len * sizeof(*decoded));

        return 0;

    }



    if (mode > FF_ARRAY_ELEMS(xcodes))

        return AVERROR_INVALIDDATA;

    code = xcodes[mode - 1];



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

        int x = get_bits_long(gb, code.init);

        if (x >= code.escape && get_bits1(gb)) {

            x |= 1 << code.init;

            if (x >= code.aescape) {

                int scale = get_unary(gb, 1, 9);

                if (scale == 9) {

                    int scale_bits = get_bits(gb, 3);

                    if (scale_bits > 0) {

                        if (scale_bits == 7) {

                            scale_bits += get_bits(gb, 5);

                            if (scale_bits > 29)

                                return AVERROR_INVALIDDATA;

                        }

                        scale = get_bits_long(gb, scale_bits) + 1;

                        x    += code.scale * scale;

                    }

                    x += code.bias;

                } else

                    x += code.scale * scale - code.escape;

            } else

                x -= code.escape;

        }

        decoded[i] = (x >> 1) ^ -(x & 1);

    }



    return 0;

}
