static int tm2_build_huff_table(TM2Context *ctx, TM2Codes *code)
{
    TM2Huff huff;
    int res = 0;
    huff.val_bits = get_bits(&ctx->gb, 5);
    huff.max_bits = get_bits(&ctx->gb, 5);
    huff.min_bits = get_bits(&ctx->gb, 5);
    huff.nodes = get_bits_long(&ctx->gb, 17);
    huff.num = 0;
    if((huff.val_bits < 1) || (huff.val_bits > 32) ||
       (huff.max_bits < 0) || (huff.max_bits > 25)) {
        av_log(ctx->avctx, AV_LOG_ERROR, "Incorrect tree parameters - literal length: %i, max code length: %i\n",
               huff.val_bits, huff.max_bits);
        return -1;
    }
    if((huff.nodes <= 0) || (huff.nodes > 0x10000)) {
        av_log(ctx->avctx, AV_LOG_ERROR, "Incorrect number of Huffman tree nodes: %i\n", huff.nodes);
        return -1;
    }
    if(huff.max_bits == 0)
        huff.max_bits = 1;
    huff.max_num = (huff.nodes + 1) >> 1;
    huff.nums = av_mallocz(huff.max_num * sizeof(int));
    huff.bits = av_mallocz(huff.max_num * sizeof(uint32_t));
    huff.lens = av_mallocz(huff.max_num * sizeof(int));
    if(tm2_read_tree(ctx, 0, 0, &huff) == -1)
        res = -1;
    if(huff.num != huff.max_num) {
        av_log(ctx->avctx, AV_LOG_ERROR, "Got less codes than expected: %i of %i\n",
               huff.num, huff.max_num);
        res = -1;
    }
    if(res != -1) {
        int i;
        res = init_vlc(&code->vlc, huff.max_bits, huff.max_num,
                    huff.lens, sizeof(int), sizeof(int),
                    huff.bits, sizeof(uint32_t), sizeof(uint32_t), 0);
        if(res < 0) {
            av_log(ctx->avctx, AV_LOG_ERROR, "Cannot build VLC table\n");
            res = -1;
        } else
            res = 0;
        if(res != -1) {
            code->bits = huff.max_bits;
            code->length = huff.max_num;
            code->recode = av_malloc(code->length * sizeof(int));
            for(i = 0; i < code->length; i++)
                code->recode[i] = huff.nums[i];
        }
    }
    av_free(huff.nums);
    av_free(huff.bits);
    av_free(huff.lens);
    return res;
}
['static int tm2_build_huff_table(TM2Context *ctx, TM2Codes *code)\n{\n    TM2Huff huff;\n    int res = 0;\n    huff.val_bits = get_bits(&ctx->gb, 5);\n    huff.max_bits = get_bits(&ctx->gb, 5);\n    huff.min_bits = get_bits(&ctx->gb, 5);\n    huff.nodes = get_bits_long(&ctx->gb, 17);\n    huff.num = 0;\n    if((huff.val_bits < 1) || (huff.val_bits > 32) ||\n       (huff.max_bits < 0) || (huff.max_bits > 25)) {\n        av_log(ctx->avctx, AV_LOG_ERROR, "Incorrect tree parameters - literal length: %i, max code length: %i\\n",\n               huff.val_bits, huff.max_bits);\n        return -1;\n    }\n    if((huff.nodes <= 0) || (huff.nodes > 0x10000)) {\n        av_log(ctx->avctx, AV_LOG_ERROR, "Incorrect number of Huffman tree nodes: %i\\n", huff.nodes);\n        return -1;\n    }\n    if(huff.max_bits == 0)\n        huff.max_bits = 1;\n    huff.max_num = (huff.nodes + 1) >> 1;\n    huff.nums = av_mallocz(huff.max_num * sizeof(int));\n    huff.bits = av_mallocz(huff.max_num * sizeof(uint32_t));\n    huff.lens = av_mallocz(huff.max_num * sizeof(int));\n    if(tm2_read_tree(ctx, 0, 0, &huff) == -1)\n        res = -1;\n    if(huff.num != huff.max_num) {\n        av_log(ctx->avctx, AV_LOG_ERROR, "Got less codes than expected: %i of %i\\n",\n               huff.num, huff.max_num);\n        res = -1;\n    }\n    if(res != -1) {\n        int i;\n        res = init_vlc(&code->vlc, huff.max_bits, huff.max_num,\n                    huff.lens, sizeof(int), sizeof(int),\n                    huff.bits, sizeof(uint32_t), sizeof(uint32_t), 0);\n        if(res < 0) {\n            av_log(ctx->avctx, AV_LOG_ERROR, "Cannot build VLC table\\n");\n            res = -1;\n        } else\n            res = 0;\n        if(res != -1) {\n            code->bits = huff.max_bits;\n            code->length = huff.max_num;\n            code->recode = av_malloc(code->length * sizeof(int));\n            for(i = 0; i < code->length; i++)\n                code->recode[i] = huff.nums[i];\n        }\n    }\n    av_free(huff.nums);\n    av_free(huff.bits);\n    av_free(huff.lens);\n    return res;\n}', 'static inline unsigned int get_bits(GetBitContext *s, int n)\n{\n    register int tmp;\n    OPEN_READER(re, s);\n    UPDATE_CACHE(re, s);\n    tmp = SHOW_UBITS(re, s, n);\n    LAST_SKIP_BITS(re, s, n);\n    CLOSE_READER(re, s);\n    return tmp;\n}', 'static av_always_inline av_const uint32_t av_bswap32(uint32_t x)\n{\n    return AV_BSWAP32C(x);\n}', 'static inline unsigned int get_bits_long(GetBitContext *s, int n)\n{\n    if (n <= MIN_CACHE_BITS)\n        return get_bits(s, n);\n    else {\n#ifdef BITSTREAM_READER_LE\n        int ret = get_bits(s, 16);\n        return ret | (get_bits(s, n-16) << 16);\n#else\n        int ret = get_bits(s, 16) << (n-16);\n        return ret | get_bits(s, n-16);\n#endif\n    }\n}', 'void *av_mallocz(size_t size)\n{\n    void *ptr = av_malloc(size);\n    if (ptr)\n        memset(ptr, 0, size);\n    return ptr;\n}', 'void *av_malloc(size_t size)\n{\n    void *ptr = NULL;\n#if CONFIG_MEMALIGN_HACK\n    long diff;\n#endif\n    if(size > (INT_MAX-32) )\n        return NULL;\n#if CONFIG_MEMALIGN_HACK\n    ptr = malloc(size+32);\n    if(!ptr)\n        return ptr;\n    diff= ((-(long)ptr - 1)&31) + 1;\n    ptr = (char*)ptr + diff;\n    ((char*)ptr)[-1]= diff;\n#elif HAVE_POSIX_MEMALIGN\n    if (posix_memalign(&ptr,32,size))\n        ptr = NULL;\n#elif HAVE_MEMALIGN\n    ptr = memalign(32,size);\n#else\n    ptr = malloc(size);\n#endif\n    return ptr;\n}', 'static int tm2_read_tree(TM2Context *ctx, uint32_t prefix, int length, TM2Huff *huff)\n{\n    if(length > huff->max_bits) {\n        av_log(ctx->avctx, AV_LOG_ERROR, "Tree exceeded its given depth (%i)\\n", huff->max_bits);\n        return -1;\n    }\n    if(!get_bits1(&ctx->gb)) {\n        if (length == 0) {\n            length = 1;\n        }\n        if(huff->num >= huff->max_num) {\n            av_log(ctx->avctx, AV_LOG_DEBUG, "Too many literals\\n");\n            return -1;\n        }\n        huff->nums[huff->num] = get_bits_long(&ctx->gb, huff->val_bits);\n        huff->bits[huff->num] = prefix;\n        huff->lens[huff->num] = length;\n        huff->num++;\n        return 0;\n    } else {\n        if(tm2_read_tree(ctx, prefix << 1, length + 1, huff) == -1)\n            return -1;\n        if(tm2_read_tree(ctx, (prefix << 1) | 1, length + 1, huff) == -1)\n            return -1;\n    }\n    return 0;\n}', 'static inline unsigned int get_bits1(GetBitContext *s)\n{\n    unsigned int index = s->index;\n    uint8_t result = s->buffer[index>>3];\n#ifdef BITSTREAM_READER_LE\n    result >>= index & 7;\n    result &= 1;\n#else\n    result <<= index & 7;\n    result >>= 8 - 1;\n#endif\n#if !UNCHECKED_BITSTREAM_READER\n    if (s->index < s->size_in_bits_plus8)\n#endif\n        index++;\n    s->index = index;\n    return result;\n}']