static void dvbsub_parse_clut_segment(AVCodecContext *avctx,
                                        const uint8_t *buf, int buf_size)
{
    DVBSubContext *ctx = avctx->priv_data;
    const uint8_t *buf_end = buf + buf_size;
    int i, clut_id;
    DVBSubCLUT *clut;
    int entry_id, depth , full_range;
    int y, cr, cb, alpha;
    int r, g, b, r_add, g_add, b_add;
    av_dlog(avctx, "DVB clut packet:\n");
    for (i=0; i < buf_size; i++) {
        av_dlog(avctx, "%02x ", buf[i]);
        if (i % 16 == 15)
            av_dlog(avctx, "\n");
    }
    if (i % 16)
        av_dlog(avctx, "\n");
    clut_id = *buf++;
    buf += 1;
    clut = get_clut(ctx, clut_id);
    if (!clut) {
        clut = av_malloc(sizeof(DVBSubCLUT));
        memcpy(clut, &default_clut, sizeof(DVBSubCLUT));
        clut->id = clut_id;
        clut->next = ctx->clut_list;
        ctx->clut_list = clut;
    }
    while (buf + 4 < buf_end) {
        entry_id = *buf++;
        depth = (*buf) & 0xe0;
        if (depth == 0) {
            av_log(avctx, AV_LOG_ERROR, "Invalid clut depth 0x%x!\n", *buf);
            return;
        }
        full_range = (*buf++) & 1;
        if (full_range) {
            y = *buf++;
            cr = *buf++;
            cb = *buf++;
            alpha = *buf++;
        } else {
            y = buf[0] & 0xfc;
            cr = (((buf[0] & 3) << 2) | ((buf[1] >> 6) & 3)) << 4;
            cb = (buf[1] << 2) & 0xf0;
            alpha = (buf[1] << 6) & 0xc0;
            buf += 2;
        }
        if (y == 0)
            alpha = 0xff;
        YUV_TO_RGB1_CCIR(cb, cr);
        YUV_TO_RGB2_CCIR(r, g, b, y);
        av_dlog(avctx, "clut %d := (%d,%d,%d,%d)\n", entry_id, r, g, b, alpha);
        if (depth & 0x80)
            clut->clut4[entry_id] = RGBA(r,g,b,255 - alpha);
        if (depth & 0x40)
            clut->clut16[entry_id] = RGBA(r,g,b,255 - alpha);
        if (depth & 0x20)
            clut->clut256[entry_id] = RGBA(r,g,b,255 - alpha);
    }
}
['static void dvbsub_parse_clut_segment(AVCodecContext *avctx,\n                                        const uint8_t *buf, int buf_size)\n{\n    DVBSubContext *ctx = avctx->priv_data;\n    const uint8_t *buf_end = buf + buf_size;\n    int i, clut_id;\n    DVBSubCLUT *clut;\n    int entry_id, depth , full_range;\n    int y, cr, cb, alpha;\n    int r, g, b, r_add, g_add, b_add;\n    av_dlog(avctx, "DVB clut packet:\\n");\n    for (i=0; i < buf_size; i++) {\n        av_dlog(avctx, "%02x ", buf[i]);\n        if (i % 16 == 15)\n            av_dlog(avctx, "\\n");\n    }\n    if (i % 16)\n        av_dlog(avctx, "\\n");\n    clut_id = *buf++;\n    buf += 1;\n    clut = get_clut(ctx, clut_id);\n    if (!clut) {\n        clut = av_malloc(sizeof(DVBSubCLUT));\n        memcpy(clut, &default_clut, sizeof(DVBSubCLUT));\n        clut->id = clut_id;\n        clut->next = ctx->clut_list;\n        ctx->clut_list = clut;\n    }\n    while (buf + 4 < buf_end) {\n        entry_id = *buf++;\n        depth = (*buf) & 0xe0;\n        if (depth == 0) {\n            av_log(avctx, AV_LOG_ERROR, "Invalid clut depth 0x%x!\\n", *buf);\n            return;\n        }\n        full_range = (*buf++) & 1;\n        if (full_range) {\n            y = *buf++;\n            cr = *buf++;\n            cb = *buf++;\n            alpha = *buf++;\n        } else {\n            y = buf[0] & 0xfc;\n            cr = (((buf[0] & 3) << 2) | ((buf[1] >> 6) & 3)) << 4;\n            cb = (buf[1] << 2) & 0xf0;\n            alpha = (buf[1] << 6) & 0xc0;\n            buf += 2;\n        }\n        if (y == 0)\n            alpha = 0xff;\n        YUV_TO_RGB1_CCIR(cb, cr);\n        YUV_TO_RGB2_CCIR(r, g, b, y);\n        av_dlog(avctx, "clut %d := (%d,%d,%d,%d)\\n", entry_id, r, g, b, alpha);\n        if (depth & 0x80)\n            clut->clut4[entry_id] = RGBA(r,g,b,255 - alpha);\n        if (depth & 0x40)\n            clut->clut16[entry_id] = RGBA(r,g,b,255 - alpha);\n        if (depth & 0x20)\n            clut->clut256[entry_id] = RGBA(r,g,b,255 - alpha);\n    }\n}', 'static DVBSubCLUT* get_clut(DVBSubContext *ctx, int clut_id)\n{\n    DVBSubCLUT *ptr = ctx->clut_list;\n    while (ptr && ptr->id != clut_id) {\n        ptr = ptr->next;\n    }\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) || !size)\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_ALIGNED_MALLOC\n    ptr = _aligned_malloc(size, 32);\n#elif HAVE_MEMALIGN\n    ptr = memalign(32, size);\n#else\n    ptr = malloc(size);\n#endif\n    return ptr;\n}']