int ff_read_packet(AVFormatContext *s, AVPacket *pkt)
{
    int ret, i, err;
    AVStream *st;
    for (;;) {
        AVPacketList *pktl = s->internal->raw_packet_buffer;
        if (pktl) {
            *pkt = pktl->pkt;
            st   = s->streams[pkt->stream_index];
            if (st->codecpar->codec_id != AV_CODEC_ID_PROBE ||
                !st->probe_packets ||
                s->internal->raw_packet_buffer_remaining_size < pkt->size) {
                AVProbeData *pd;
                if (st->probe_packets)
                    if ((err = probe_codec(s, st, NULL)) < 0)
                        return err;
                pd = &st->probe_data;
                av_freep(&pd->buf);
                pd->buf_size = 0;
                s->internal->raw_packet_buffer                 = pktl->next;
                s->internal->raw_packet_buffer_remaining_size += pkt->size;
                av_free(pktl);
                return 0;
            }
        }
        pkt->data = NULL;
        pkt->size = 0;
        av_init_packet(pkt);
        ret = s->iformat->read_packet(s, pkt);
        if (ret < 0) {
            if (!pktl || ret == AVERROR(EAGAIN))
                return ret;
            for (i = 0; i < s->nb_streams; i++) {
                st = s->streams[i];
                if (st->probe_packets)
                    if ((err = probe_codec(s, st, NULL)) < 0)
                        return err;
            }
            continue;
        }
        if (!pkt->buf) {
            AVPacket tmp = { 0 };
            ret = av_packet_ref(&tmp, pkt);
            if (ret < 0)
                return ret;
            *pkt = tmp;
        }
        if ((s->flags & AVFMT_FLAG_DISCARD_CORRUPT) &&
            (pkt->flags & AV_PKT_FLAG_CORRUPT)) {
            av_log(s, AV_LOG_WARNING,
                   "Dropped corrupted packet (stream = %d)\n",
                   pkt->stream_index);
            av_packet_unref(pkt);
            continue;
        }
        st = s->streams[pkt->stream_index];
        switch (st->codecpar->codec_type) {
        case AVMEDIA_TYPE_VIDEO:
            if (s->video_codec_id)
                st->codecpar->codec_id = s->video_codec_id;
            break;
        case AVMEDIA_TYPE_AUDIO:
            if (s->audio_codec_id)
                st->codecpar->codec_id = s->audio_codec_id;
            break;
        case AVMEDIA_TYPE_SUBTITLE:
            if (s->subtitle_codec_id)
                st->codecpar->codec_id = s->subtitle_codec_id;
            break;
        }
        if (!pktl && (st->codecpar->codec_id != AV_CODEC_ID_PROBE ||
                      !st->probe_packets))
            return ret;
        err = add_to_pktbuf(&s->internal->raw_packet_buffer, pkt,
                            &s->internal->raw_packet_buffer_end, 0);
        if (err)
            return err;
        s->internal->raw_packet_buffer_remaining_size -= pkt->size;
        if ((err = probe_codec(s, st, pkt)) < 0)
            return err;
    }
}
['int ff_read_packet(AVFormatContext *s, AVPacket *pkt)\n{\n    int ret, i, err;\n    AVStream *st;\n    for (;;) {\n        AVPacketList *pktl = s->internal->raw_packet_buffer;\n        if (pktl) {\n            *pkt = pktl->pkt;\n            st   = s->streams[pkt->stream_index];\n            if (st->codecpar->codec_id != AV_CODEC_ID_PROBE ||\n                !st->probe_packets ||\n                s->internal->raw_packet_buffer_remaining_size < pkt->size) {\n                AVProbeData *pd;\n                if (st->probe_packets)\n                    if ((err = probe_codec(s, st, NULL)) < 0)\n                        return err;\n                pd = &st->probe_data;\n                av_freep(&pd->buf);\n                pd->buf_size = 0;\n                s->internal->raw_packet_buffer                 = pktl->next;\n                s->internal->raw_packet_buffer_remaining_size += pkt->size;\n                av_free(pktl);\n                return 0;\n            }\n        }\n        pkt->data = NULL;\n        pkt->size = 0;\n        av_init_packet(pkt);\n        ret = s->iformat->read_packet(s, pkt);\n        if (ret < 0) {\n            if (!pktl || ret == AVERROR(EAGAIN))\n                return ret;\n            for (i = 0; i < s->nb_streams; i++) {\n                st = s->streams[i];\n                if (st->probe_packets)\n                    if ((err = probe_codec(s, st, NULL)) < 0)\n                        return err;\n            }\n            continue;\n        }\n        if (!pkt->buf) {\n            AVPacket tmp = { 0 };\n            ret = av_packet_ref(&tmp, pkt);\n            if (ret < 0)\n                return ret;\n            *pkt = tmp;\n        }\n        if ((s->flags & AVFMT_FLAG_DISCARD_CORRUPT) &&\n            (pkt->flags & AV_PKT_FLAG_CORRUPT)) {\n            av_log(s, AV_LOG_WARNING,\n                   "Dropped corrupted packet (stream = %d)\\n",\n                   pkt->stream_index);\n            av_packet_unref(pkt);\n            continue;\n        }\n        st = s->streams[pkt->stream_index];\n        switch (st->codecpar->codec_type) {\n        case AVMEDIA_TYPE_VIDEO:\n            if (s->video_codec_id)\n                st->codecpar->codec_id = s->video_codec_id;\n            break;\n        case AVMEDIA_TYPE_AUDIO:\n            if (s->audio_codec_id)\n                st->codecpar->codec_id = s->audio_codec_id;\n            break;\n        case AVMEDIA_TYPE_SUBTITLE:\n            if (s->subtitle_codec_id)\n                st->codecpar->codec_id = s->subtitle_codec_id;\n            break;\n        }\n        if (!pktl && (st->codecpar->codec_id != AV_CODEC_ID_PROBE ||\n                      !st->probe_packets))\n            return ret;\n        err = add_to_pktbuf(&s->internal->raw_packet_buffer, pkt,\n                            &s->internal->raw_packet_buffer_end, 0);\n        if (err)\n            return err;\n        s->internal->raw_packet_buffer_remaining_size -= pkt->size;\n        if ((err = probe_codec(s, st, pkt)) < 0)\n            return err;\n    }\n}', 'void av_init_packet(AVPacket *pkt)\n{\n    pkt->pts                  = AV_NOPTS_VALUE;\n    pkt->dts                  = AV_NOPTS_VALUE;\n    pkt->pos                  = -1;\n    pkt->duration             = 0;\n#if FF_API_CONVERGENCE_DURATION\nFF_DISABLE_DEPRECATION_WARNINGS\n    pkt->convergence_duration = 0;\nFF_ENABLE_DEPRECATION_WARNINGS\n#endif\n    pkt->flags                = 0;\n    pkt->stream_index         = 0;\n    pkt->buf                  = NULL;\n    pkt->side_data            = NULL;\n    pkt->side_data_elems      = 0;\n}', 'int av_packet_ref(AVPacket *dst, const AVPacket *src)\n{\n    int ret;\n    ret = av_packet_copy_props(dst, src);\n    if (ret < 0)\n        return ret;\n    if (!src->buf) {\n        ret = packet_alloc(&dst->buf, src->size);\n        if (ret < 0)\n            goto fail;\n        memcpy(dst->buf->data, src->data, src->size);\n        dst->data = dst->buf->data;\n    } else {\n        dst->buf = av_buffer_ref(src->buf);\n        if (!dst->buf) {\n            ret = AVERROR(ENOMEM);\n            goto fail;\n        }\n        dst->data = src->data;\n    }\n    dst->size = src->size;\n    return 0;\nfail:\n    av_packet_free_side_data(dst);\n    return ret;\n}', 'int av_packet_copy_props(AVPacket *dst, const AVPacket *src)\n{\n    int i;\n    dst->pts                  = src->pts;\n    dst->dts                  = src->dts;\n    dst->pos                  = src->pos;\n    dst->duration             = src->duration;\n#if FF_API_CONVERGENCE_DURATION\nFF_DISABLE_DEPRECATION_WARNINGS\n    dst->convergence_duration = src->convergence_duration;\nFF_ENABLE_DEPRECATION_WARNINGS\n#endif\n    dst->flags                = src->flags;\n    dst->stream_index         = src->stream_index;\n    for (i = 0; i < src->side_data_elems; i++) {\n         enum AVPacketSideDataType type = src->side_data[i].type;\n         int size          = src->side_data[i].size;\n         uint8_t *src_data = src->side_data[i].data;\n         uint8_t *dst_data = av_packet_new_side_data(dst, type, size);\n        if (!dst_data) {\n            av_packet_free_side_data(dst);\n            return AVERROR(ENOMEM);\n        }\n        memcpy(dst_data, src_data, size);\n    }\n    return 0;\n}', 'static int packet_alloc(AVBufferRef **buf, int size)\n{\n    int ret;\n    if (size < 0 || size >= INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE)\n        return AVERROR(EINVAL);\n    ret = av_buffer_realloc(buf, size + AV_INPUT_BUFFER_PADDING_SIZE);\n    if (ret < 0)\n        return ret;\n    memset((*buf)->data + 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}']