static int svq3_decode_slice_header(AVCodecContext *avctx)
{
    SVQ3Context *s = avctx->priv_data;
    const int mb_xy   = s->mb_xy;
    int i, header;
    unsigned slice_id;
    header = get_bits(&s->gb, 8);
    if (((header & 0x9F) != 1 && (header & 0x9F) != 2) || (header & 0x60) == 0) {
        av_log(avctx, AV_LOG_ERROR, "unsupported slice header (%02X)\n", header);
        return -1;
    } else {
        int slice_bits, slice_bytes, slice_length;
        int length = header >> 5 & 3;
        slice_length = show_bits(&s->gb, 8 * length);
        slice_bits   = slice_length * 8;
        slice_bytes  = slice_length + length - 1;
        if (slice_bytes > get_bits_left(&s->gb)) {
            av_log(avctx, AV_LOG_ERROR, "slice after bitstream end\n");
            return -1;
        }
        skip_bits(&s->gb, 8);
        av_fast_malloc(&s->slice_buf, &s->slice_size, slice_bytes + AV_INPUT_BUFFER_PADDING_SIZE);
        if (!s->slice_buf)
            return AVERROR(ENOMEM);
        memcpy(s->slice_buf, s->gb.buffer + s->gb.index / 8, slice_bytes);
        init_get_bits(&s->gb_slice, s->slice_buf, slice_bits);
        if (s->watermark_key) {
            uint32_t header = AV_RL32(&s->gb_slice.buffer[1]);
            AV_WL32(&s->gb_slice.buffer[1], header ^ s->watermark_key);
        }
        if (length > 0) {
            memcpy(s->slice_buf, &s->slice_buf[slice_length], length - 1);
        }
        skip_bits_long(&s->gb, slice_bytes * 8);
    }
    if ((slice_id = svq3_get_ue_golomb(&s->gb_slice)) >= 3) {
        av_log(s->avctx, AV_LOG_ERROR, "illegal slice type %u \n", slice_id);
        return -1;
    }
    s->pict_type = ff_h264_golomb_to_pict_type[slice_id];
    if ((header & 0x9F) == 2) {
        i = (s->mb_num < 64) ? 6 : (1 + av_log2(s->mb_num - 1));
        get_bits(&s->gb_slice, i);
    } else {
        skip_bits1(&s->gb_slice);
    }
    s->slice_num      = get_bits(&s->gb_slice, 8);
    s->qscale         = get_bits(&s->gb_slice, 5);
    s->adaptive_quant = get_bits1(&s->gb_slice);
    skip_bits1(&s->gb_slice);
    if (s->unknown_flag)
        skip_bits1(&s->gb_slice);
    skip_bits1(&s->gb_slice);
    skip_bits(&s->gb_slice, 2);
    while (get_bits1(&s->gb_slice))
        skip_bits(&s->gb_slice, 8);
    if (s->mb_x > 0) {
        memset(s->intra4x4_pred_mode + s->mb2br_xy[mb_xy - 1] + 3,
               -1, 4 * sizeof(int8_t));
        memset(s->intra4x4_pred_mode + s->mb2br_xy[mb_xy - s->mb_x],
               -1, 8 * sizeof(int8_t) * s->mb_x);
    }
    if (s->mb_y > 0) {
        memset(s->intra4x4_pred_mode + s->mb2br_xy[mb_xy - s->mb_stride],
               -1, 8 * sizeof(int8_t) * (s->mb_width - s->mb_x));
        if (s->mb_x > 0)
            s->intra4x4_pred_mode[s->mb2br_xy[mb_xy - s->mb_stride - 1] + 3] = -1;
    }
    return 0;
}
['static int svq3_decode_slice_header(AVCodecContext *avctx)\n{\n    SVQ3Context *s = avctx->priv_data;\n    const int mb_xy   = s->mb_xy;\n    int i, header;\n    unsigned slice_id;\n    header = get_bits(&s->gb, 8);\n    if (((header & 0x9F) != 1 && (header & 0x9F) != 2) || (header & 0x60) == 0) {\n        av_log(avctx, AV_LOG_ERROR, "unsupported slice header (%02X)\\n", header);\n        return -1;\n    } else {\n        int slice_bits, slice_bytes, slice_length;\n        int length = header >> 5 & 3;\n        slice_length = show_bits(&s->gb, 8 * length);\n        slice_bits   = slice_length * 8;\n        slice_bytes  = slice_length + length - 1;\n        if (slice_bytes > get_bits_left(&s->gb)) {\n            av_log(avctx, AV_LOG_ERROR, "slice after bitstream end\\n");\n            return -1;\n        }\n        skip_bits(&s->gb, 8);\n        av_fast_malloc(&s->slice_buf, &s->slice_size, slice_bytes + AV_INPUT_BUFFER_PADDING_SIZE);\n        if (!s->slice_buf)\n            return AVERROR(ENOMEM);\n        memcpy(s->slice_buf, s->gb.buffer + s->gb.index / 8, slice_bytes);\n        init_get_bits(&s->gb_slice, s->slice_buf, slice_bits);\n        if (s->watermark_key) {\n            uint32_t header = AV_RL32(&s->gb_slice.buffer[1]);\n            AV_WL32(&s->gb_slice.buffer[1], header ^ s->watermark_key);\n        }\n        if (length > 0) {\n            memcpy(s->slice_buf, &s->slice_buf[slice_length], length - 1);\n        }\n        skip_bits_long(&s->gb, slice_bytes * 8);\n    }\n    if ((slice_id = svq3_get_ue_golomb(&s->gb_slice)) >= 3) {\n        av_log(s->avctx, AV_LOG_ERROR, "illegal slice type %u \\n", slice_id);\n        return -1;\n    }\n    s->pict_type = ff_h264_golomb_to_pict_type[slice_id];\n    if ((header & 0x9F) == 2) {\n        i = (s->mb_num < 64) ? 6 : (1 + av_log2(s->mb_num - 1));\n        get_bits(&s->gb_slice, i);\n    } else {\n        skip_bits1(&s->gb_slice);\n    }\n    s->slice_num      = get_bits(&s->gb_slice, 8);\n    s->qscale         = get_bits(&s->gb_slice, 5);\n    s->adaptive_quant = get_bits1(&s->gb_slice);\n    skip_bits1(&s->gb_slice);\n    if (s->unknown_flag)\n        skip_bits1(&s->gb_slice);\n    skip_bits1(&s->gb_slice);\n    skip_bits(&s->gb_slice, 2);\n    while (get_bits1(&s->gb_slice))\n        skip_bits(&s->gb_slice, 8);\n    if (s->mb_x > 0) {\n        memset(s->intra4x4_pred_mode + s->mb2br_xy[mb_xy - 1] + 3,\n               -1, 4 * sizeof(int8_t));\n        memset(s->intra4x4_pred_mode + s->mb2br_xy[mb_xy - s->mb_x],\n               -1, 8 * sizeof(int8_t) * s->mb_x);\n    }\n    if (s->mb_y > 0) {\n        memset(s->intra4x4_pred_mode + s->mb2br_xy[mb_xy - s->mb_stride],\n               -1, 8 * sizeof(int8_t) * (s->mb_width - s->mb_x));\n        if (s->mb_x > 0)\n            s->intra4x4_pred_mode[s->mb2br_xy[mb_xy - s->mb_stride - 1] + 3] = -1;\n    }\n    return 0;\n}', 'static inline int get_bits_left(GetBitContext *gb)\n{\n    return gb->size_in_bits - get_bits_count(gb);\n}', 'static inline int get_bits_count(const GetBitContext *s)\n{\n    return s->index;\n}', 'void av_fast_malloc(void *ptr, unsigned int *size, size_t min_size)\n{\n    void **p = ptr;\n    if (min_size < *size)\n        return;\n    min_size = FFMAX(17 * min_size / 16 + 32, min_size);\n    av_free(*p);\n    *p = av_malloc(min_size);\n    if (!*p)\n        min_size = 0;\n    *size = min_size;\n}', 'static inline int init_get_bits(GetBitContext *s, const uint8_t *buffer,\n                                int bit_size)\n{\n    int buffer_size;\n    int ret = 0;\n    if (bit_size > INT_MAX - 7 || bit_size < 0 || !buffer) {\n        bit_size    = 0;\n        buffer      = NULL;\n        ret         = AVERROR_INVALIDDATA;\n    }\n    buffer_size = (bit_size + 7) >> 3;\n    s->buffer             = buffer;\n    s->size_in_bits       = bit_size;\n#if !UNCHECKED_BITSTREAM_READER\n    s->size_in_bits_plus8 = bit_size + 8;\n#endif\n    s->buffer_end         = buffer + buffer_size;\n    s->index              = 0;\n    return ret;\n}']