static int h264_mp4toannexb_filter(AVBSFContext *ctx, AVPacket *out)
{
    H264BSFContext *s = ctx->priv_data;
    AVPacket *in;
    uint8_t unit_type;
    int32_t nal_size;
    uint32_t cumul_size    = 0;
    const uint8_t *buf;
    const uint8_t *buf_end;
    int            buf_size;
    int ret = 0;
    ret = ff_bsf_get_packet(ctx, &in);
    if (ret < 0)
        return ret;
    if (!s->extradata_parsed) {
        av_packet_move_ref(out, in);
        av_packet_free(&in);
        return 0;
    }
    buf      = in->data;
    buf_size = in->size;
    buf_end  = in->data + in->size;
    do {
        if (buf + s->length_size > buf_end)
            goto fail;
        if (s->length_size == 1) {
            nal_size = buf[0];
        } else if (s->length_size == 2) {
            nal_size = AV_RB16(buf);
        } else
            nal_size = AV_RB32(buf);
        buf += s->length_size;
        unit_type = *buf & 0x1f;
        if (buf + nal_size > buf_end || nal_size < 0)
            goto fail;
        if (s->first_idr && unit_type == 5) {
            if (alloc_and_copy(out,
                               ctx->par_out->extradata, ctx->par_out->extradata_size,
                               buf, nal_size) < 0)
                goto fail;
            s->first_idr = 0;
        } else {
            if (alloc_and_copy(out,
                               NULL, 0, buf, nal_size) < 0)
                goto fail;
            if (!s->first_idr && unit_type == 1)
                s->first_idr = 1;
        }
        buf        += nal_size;
        cumul_size += nal_size + s->length_size;
    } while (cumul_size < buf_size);
    ret = av_packet_copy_props(out, in);
    if (ret < 0)
        goto fail;
fail:
    if (ret < 0)
        av_packet_unref(out);
    av_packet_free(&in);
    return ret;
}
['static int h264_mp4toannexb_filter(AVBSFContext *ctx, AVPacket *out)\n{\n    H264BSFContext *s = ctx->priv_data;\n    AVPacket *in;\n    uint8_t unit_type;\n    int32_t nal_size;\n    uint32_t cumul_size    = 0;\n    const uint8_t *buf;\n    const uint8_t *buf_end;\n    int            buf_size;\n    int ret = 0;\n    ret = ff_bsf_get_packet(ctx, &in);\n    if (ret < 0)\n        return ret;\n    if (!s->extradata_parsed) {\n        av_packet_move_ref(out, in);\n        av_packet_free(&in);\n        return 0;\n    }\n    buf      = in->data;\n    buf_size = in->size;\n    buf_end  = in->data + in->size;\n    do {\n        if (buf + s->length_size > buf_end)\n            goto fail;\n        if (s->length_size == 1) {\n            nal_size = buf[0];\n        } else if (s->length_size == 2) {\n            nal_size = AV_RB16(buf);\n        } else\n            nal_size = AV_RB32(buf);\n        buf += s->length_size;\n        unit_type = *buf & 0x1f;\n        if (buf + nal_size > buf_end || nal_size < 0)\n            goto fail;\n        if (s->first_idr && unit_type == 5) {\n            if (alloc_and_copy(out,\n                               ctx->par_out->extradata, ctx->par_out->extradata_size,\n                               buf, nal_size) < 0)\n                goto fail;\n            s->first_idr = 0;\n        } else {\n            if (alloc_and_copy(out,\n                               NULL, 0, buf, nal_size) < 0)\n                goto fail;\n            if (!s->first_idr && unit_type == 1)\n                s->first_idr = 1;\n        }\n        buf        += nal_size;\n        cumul_size += nal_size + s->length_size;\n    } while (cumul_size < buf_size);\n    ret = av_packet_copy_props(out, in);\n    if (ret < 0)\n        goto fail;\nfail:\n    if (ret < 0)\n        av_packet_unref(out);\n    av_packet_free(&in);\n    return ret;\n}', 'int ff_bsf_get_packet(AVBSFContext *ctx, AVPacket **pkt)\n{\n    AVBSFInternal *in = ctx->internal;\n    AVPacket *tmp_pkt;\n    if (in->eof)\n        return AVERROR_EOF;\n    if (!ctx->internal->buffer_pkt->data &&\n        !ctx->internal->buffer_pkt->side_data_elems)\n        return AVERROR(EAGAIN);\n    tmp_pkt = av_packet_alloc();\n    if (!tmp_pkt)\n        return AVERROR(ENOMEM);\n    *pkt = ctx->internal->buffer_pkt;\n    ctx->internal->buffer_pkt = tmp_pkt;\n    return 0;\n}', 'static int alloc_and_copy(AVPacket *out,\n                          const uint8_t *sps_pps, uint32_t sps_pps_size,\n                          const uint8_t *in, uint32_t in_size)\n{\n    uint32_t offset         = out->size;\n    uint8_t nal_header_size = offset ? 3 : 4;\n    int err;\n    err = av_grow_packet(out, sps_pps_size + in_size + nal_header_size);\n    if (err < 0)\n        return err;\n    if (sps_pps)\n        memcpy(out->data + offset, sps_pps, sps_pps_size);\n    memcpy(out->data + sps_pps_size + nal_header_size + offset, in, in_size);\n    if (!offset) {\n        AV_WB32(out->data + sps_pps_size, 1);\n    } else {\n        (out->data + offset + sps_pps_size)[0] =\n        (out->data + offset + sps_pps_size)[1] = 0;\n        (out->data + offset + sps_pps_size)[2] = 1;\n    }\n    return 0;\n}', 'int av_grow_packet(AVPacket *pkt, int grow_by)\n{\n    int new_size;\n    av_assert0((unsigned)pkt->size <= INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE);\n    if (!pkt->size)\n        return av_new_packet(pkt, grow_by);\n    if ((unsigned)grow_by >\n        INT_MAX - (pkt->size + AV_INPUT_BUFFER_PADDING_SIZE))\n        return -1;\n    new_size = pkt->size + grow_by + AV_INPUT_BUFFER_PADDING_SIZE;\n    if (pkt->buf) {\n        int ret = av_buffer_realloc(&pkt->buf, new_size);\n        if (ret < 0)\n            return ret;\n    } else {\n        pkt->buf = av_buffer_alloc(new_size);\n        if (!pkt->buf)\n            return AVERROR(ENOMEM);\n        memcpy(pkt->buf->data, pkt->data, FFMIN(pkt->size, pkt->size + grow_by));\n    }\n    pkt->data  = pkt->buf->data;\n    pkt->size += grow_by;\n    memset(pkt->data + pkt->size, 0, AV_INPUT_BUFFER_PADDING_SIZE);\n    return 0;\n}', 'int av_buffer_realloc(AVBufferRef **pbuf, int size)\n{\n    AVBufferRef *buf = *pbuf;\n    uint8_t *tmp;\n    if (!buf) {\n        uint8_t *data = av_realloc(NULL, size);\n        if (!data)\n            return AVERROR(ENOMEM);\n        buf = av_buffer_create(data, size, av_buffer_default_free, NULL, 0);\n        if (!buf) {\n            av_freep(&data);\n            return AVERROR(ENOMEM);\n        }\n        buf->buffer->flags |= BUFFER_FLAG_REALLOCATABLE;\n        *pbuf = buf;\n        return 0;\n    } else if (buf->size == size)\n        return 0;\n    if (!(buf->buffer->flags & BUFFER_FLAG_REALLOCATABLE) ||\n        !av_buffer_is_writable(buf) || buf->data != buf->buffer->data) {\n        AVBufferRef *new = NULL;\n        av_buffer_realloc(&new, size);\n        if (!new)\n            return AVERROR(ENOMEM);\n        memcpy(new->data, buf->data, FFMIN(size, buf->size));\n        av_buffer_unref(pbuf);\n        *pbuf = new;\n        return 0;\n    }\n    tmp = av_realloc(buf->buffer->data, size);\n    if (!tmp)\n        return AVERROR(ENOMEM);\n    buf->buffer->data = buf->data = tmp;\n    buf->buffer->size = buf->size = size;\n    return 0;\n}', 'int av_buffer_is_writable(const AVBufferRef *buf)\n{\n    if (buf->buffer->flags & AV_BUFFER_FLAG_READONLY)\n        return 0;\n    return atomic_load(&buf->buffer->refcount) == 1;\n}', 'void *av_realloc(void *ptr, size_t size)\n{\n    if (size > (INT_MAX - 16))\n        return NULL;\n#if HAVE_ALIGNED_MALLOC\n    return _aligned_realloc(ptr, size, 32);\n#else\n    return realloc(ptr, size);\n#endif\n}']