static int extract_extradata_h2645(AVBSFContext *ctx, AVPacket *pkt,
                                   uint8_t **data, int *size)
{
    static const int extradata_nal_types_hevc[] = {
        HEVC_NAL_VPS, HEVC_NAL_SPS, HEVC_NAL_PPS,
    };
    static const int extradata_nal_types_h264[] = {
        H264_NAL_SPS, H264_NAL_PPS,
    };
    ExtractExtradataContext *s = ctx->priv_data;
    H2645Packet h2645_pkt = { 0 };
    int extradata_size = 0;
    const int *extradata_nal_types;
    int nb_extradata_nal_types;
    int i, has_sps = 0, has_vps = 0, ret = 0;
    if (ctx->par_in->codec_id == AV_CODEC_ID_HEVC) {
        extradata_nal_types    = extradata_nal_types_hevc;
        nb_extradata_nal_types = FF_ARRAY_ELEMS(extradata_nal_types_hevc);
    } else {
        extradata_nal_types    = extradata_nal_types_h264;
        nb_extradata_nal_types = FF_ARRAY_ELEMS(extradata_nal_types_h264);
    }
    ret = ff_h2645_packet_split(&h2645_pkt, pkt->data, pkt->size,
                                ctx, 0, 0, ctx->par_in->codec_id);
    if (ret < 0)
        return ret;
    for (i = 0; i < h2645_pkt.nb_nals; i++) {
        H2645NAL *nal = &h2645_pkt.nals[i];
        if (val_in_array(extradata_nal_types, nb_extradata_nal_types, nal->type)) {
            extradata_size += nal->raw_size + 3;
            if (ctx->par_in->codec_id == AV_CODEC_ID_HEVC) {
                if (nal->type == HEVC_NAL_SPS) has_sps = 1;
                if (nal->type == HEVC_NAL_VPS) has_vps = 1;
            } else {
                if (nal->type == H264_NAL_SPS) has_sps = 1;
            }
        }
    }
    if (extradata_size &&
        ((ctx->par_in->codec_id == AV_CODEC_ID_HEVC && has_sps && has_vps) ||
         (ctx->par_in->codec_id == AV_CODEC_ID_H264 && has_sps))) {
        AVBufferRef *filtered_buf;
        uint8_t *extradata, *filtered_data;
        if (s->remove) {
            filtered_buf = av_buffer_alloc(pkt->size + AV_INPUT_BUFFER_PADDING_SIZE);
            if (!filtered_buf)
                goto fail;
            filtered_data = filtered_buf->data;
        }
        extradata = av_malloc(extradata_size + AV_INPUT_BUFFER_PADDING_SIZE);
        if (!extradata) {
            av_buffer_unref(&filtered_buf);
            goto fail;
        }
        *data = extradata;
        *size = extradata_size;
        for (i = 0; i < h2645_pkt.nb_nals; i++) {
            H2645NAL *nal = &h2645_pkt.nals[i];
            if (val_in_array(extradata_nal_types, nb_extradata_nal_types,
                             nal->type)) {
                AV_WB24(extradata, 1);
                memcpy(extradata + 3, nal->raw_data, nal->raw_size);
                extradata += 3 + nal->raw_size;
            } else if (s->remove) {
                AV_WB24(filtered_data, 1);
                memcpy(filtered_data + 3, nal->raw_data, nal->raw_size);
                filtered_data += 3 + nal->raw_size;
            }
        }
        if (s->remove) {
            av_buffer_unref(&pkt->buf);
            pkt->buf  = filtered_buf;
            pkt->data = filtered_buf->data;
            pkt->size = filtered_data - filtered_buf->data;
        }
    }
fail:
    ff_h2645_packet_uninit(&h2645_pkt);
    return ret;
}
['static int extract_extradata_h2645(AVBSFContext *ctx, AVPacket *pkt,\n                                   uint8_t **data, int *size)\n{\n    static const int extradata_nal_types_hevc[] = {\n        HEVC_NAL_VPS, HEVC_NAL_SPS, HEVC_NAL_PPS,\n    };\n    static const int extradata_nal_types_h264[] = {\n        H264_NAL_SPS, H264_NAL_PPS,\n    };\n    ExtractExtradataContext *s = ctx->priv_data;\n    H2645Packet h2645_pkt = { 0 };\n    int extradata_size = 0;\n    const int *extradata_nal_types;\n    int nb_extradata_nal_types;\n    int i, has_sps = 0, has_vps = 0, ret = 0;\n    if (ctx->par_in->codec_id == AV_CODEC_ID_HEVC) {\n        extradata_nal_types    = extradata_nal_types_hevc;\n        nb_extradata_nal_types = FF_ARRAY_ELEMS(extradata_nal_types_hevc);\n    } else {\n        extradata_nal_types    = extradata_nal_types_h264;\n        nb_extradata_nal_types = FF_ARRAY_ELEMS(extradata_nal_types_h264);\n    }\n    ret = ff_h2645_packet_split(&h2645_pkt, pkt->data, pkt->size,\n                                ctx, 0, 0, ctx->par_in->codec_id);\n    if (ret < 0)\n        return ret;\n    for (i = 0; i < h2645_pkt.nb_nals; i++) {\n        H2645NAL *nal = &h2645_pkt.nals[i];\n        if (val_in_array(extradata_nal_types, nb_extradata_nal_types, nal->type)) {\n            extradata_size += nal->raw_size + 3;\n            if (ctx->par_in->codec_id == AV_CODEC_ID_HEVC) {\n                if (nal->type == HEVC_NAL_SPS) has_sps = 1;\n                if (nal->type == HEVC_NAL_VPS) has_vps = 1;\n            } else {\n                if (nal->type == H264_NAL_SPS) has_sps = 1;\n            }\n        }\n    }\n    if (extradata_size &&\n        ((ctx->par_in->codec_id == AV_CODEC_ID_HEVC && has_sps && has_vps) ||\n         (ctx->par_in->codec_id == AV_CODEC_ID_H264 && has_sps))) {\n        AVBufferRef *filtered_buf;\n        uint8_t *extradata, *filtered_data;\n        if (s->remove) {\n            filtered_buf = av_buffer_alloc(pkt->size + AV_INPUT_BUFFER_PADDING_SIZE);\n            if (!filtered_buf)\n                goto fail;\n            filtered_data = filtered_buf->data;\n        }\n        extradata = av_malloc(extradata_size + AV_INPUT_BUFFER_PADDING_SIZE);\n        if (!extradata) {\n            av_buffer_unref(&filtered_buf);\n            goto fail;\n        }\n        *data = extradata;\n        *size = extradata_size;\n        for (i = 0; i < h2645_pkt.nb_nals; i++) {\n            H2645NAL *nal = &h2645_pkt.nals[i];\n            if (val_in_array(extradata_nal_types, nb_extradata_nal_types,\n                             nal->type)) {\n                AV_WB24(extradata, 1);\n                memcpy(extradata + 3, nal->raw_data, nal->raw_size);\n                extradata += 3 + nal->raw_size;\n            } else if (s->remove) {\n                AV_WB24(filtered_data, 1);\n                memcpy(filtered_data + 3, nal->raw_data, nal->raw_size);\n                filtered_data += 3 + nal->raw_size;\n            }\n        }\n        if (s->remove) {\n            av_buffer_unref(&pkt->buf);\n            pkt->buf  = filtered_buf;\n            pkt->data = filtered_buf->data;\n            pkt->size = filtered_data - filtered_buf->data;\n        }\n    }\nfail:\n    ff_h2645_packet_uninit(&h2645_pkt);\n    return ret;\n}', 'int ff_h2645_packet_split(H2645Packet *pkt, const uint8_t *buf, int length,\n                          void *logctx, int is_nalff, int nal_length_size,\n                          enum AVCodecID codec_id)\n{\n    GetByteContext bc;\n    int consumed, ret = 0;\n    size_t next_avc = is_nalff ? 0 : length;\n    bytestream2_init(&bc, buf, length);\n    pkt->nb_nals = 0;\n    while (bytestream2_get_bytes_left(&bc) >= 4) {\n        H2645NAL *nal;\n        int extract_length = 0;\n        int skip_trailing_zeros = 1;\n        if (bytestream2_tell(&bc) == next_avc) {\n            int i;\n            for (i = 0; i < nal_length_size; i++)\n                extract_length = (extract_length << 8) | bytestream2_get_byte(&bc);\n            if (extract_length > bytestream2_get_bytes_left(&bc)) {\n                av_log(logctx, AV_LOG_ERROR,\n                       "Invalid NAL unit size (%d > %d).\\n",\n                       extract_length, bytestream2_get_bytes_left(&bc));\n                return AVERROR_INVALIDDATA;\n            }\n            next_avc = bytestream2_tell(&bc) + extract_length;\n        } else {\n            int buf_index = find_next_start_code(bc.buffer, buf + next_avc);\n            bytestream2_skip(&bc, buf_index);\n            if (bytestream2_tell(&bc) == next_avc)\n                continue;\n            if (bytestream2_get_bytes_left(&bc) > 0) {\n                extract_length = bytestream2_get_bytes_left(&bc);\n            } else if (pkt->nb_nals == 0) {\n                av_log(logctx, AV_LOG_ERROR, "No NAL unit found\\n");\n                return AVERROR_INVALIDDATA;\n            } else {\n                break;\n            }\n        }\n        if (pkt->nals_allocated < pkt->nb_nals + 1) {\n            int new_size = pkt->nals_allocated + 1;\n            H2645NAL *tmp = av_realloc_array(pkt->nals, new_size, sizeof(*tmp));\n            if (!tmp)\n                return AVERROR(ENOMEM);\n            pkt->nals = tmp;\n            memset(pkt->nals + pkt->nals_allocated, 0,\n                   (new_size - pkt->nals_allocated) * sizeof(*tmp));\n            pkt->nals_allocated = new_size;\n        }\n        nal = &pkt->nals[pkt->nb_nals++];\n        consumed = ff_h2645_extract_rbsp(bc.buffer, extract_length, nal);\n        if (consumed < 0)\n            return consumed;\n        bytestream2_skip(&bc, consumed);\n        if (bytestream2_get_bytes_left(&bc) >= 4 &&\n            bytestream2_peek_be32(&bc) == 0x000001E0)\n            skip_trailing_zeros = 0;\n        nal->size_bits = get_bit_length(nal, skip_trailing_zeros);\n        ret = init_get_bits(&nal->gb, nal->data, nal->size_bits);\n        if (ret < 0)\n            return ret;\n        if (codec_id == AV_CODEC_ID_HEVC)\n            ret = hevc_parse_nal_header(nal, logctx);\n        else\n            ret = h264_parse_nal_header(nal, logctx);\n        if (ret <= 0) {\n            if (ret < 0) {\n                av_log(logctx, AV_LOG_ERROR, "Invalid NAL unit %d, skipping.\\n",\n                       nal->type);\n            }\n            pkt->nb_nals--;\n        }\n    }\n    return 0;\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}']