static void write_packet(OutputFile *of, AVPacket *pkt, OutputStream *ost)
{
    AVFormatContext *s = of->ctx;
    AVStream *st = ost->st;
    int ret;
    if (!of->header_written) {
        AVPacket tmp_pkt = {0};
        if (!av_fifo_space(ost->muxing_queue)) {
            int new_size = FFMIN(2 * av_fifo_size(ost->muxing_queue),
                                 ost->max_muxing_queue_size);
            if (new_size <= av_fifo_size(ost->muxing_queue)) {
                av_log(NULL, AV_LOG_ERROR,
                       "Too many packets buffered for output stream %d:%d.\n",
                       ost->file_index, ost->st->index);
                exit_program(1);
            }
            ret = av_fifo_realloc2(ost->muxing_queue, new_size);
            if (ret < 0)
                exit_program(1);
        }
        ret = av_packet_ref(&tmp_pkt, pkt);
        if (ret < 0)
            exit_program(1);
        av_fifo_generic_write(ost->muxing_queue, &tmp_pkt, sizeof(tmp_pkt), NULL);
        av_packet_unref(pkt);
        return;
    }
    if (!(st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO && ost->encoding_needed)) {
        if (ost->frame_number >= ost->max_frames) {
            av_packet_unref(pkt);
            return;
        }
        ost->frame_number++;
    }
    if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
        uint8_t *sd = av_packet_get_side_data(pkt, AV_PKT_DATA_QUALITY_FACTOR,
                                              NULL);
        ost->quality = sd ? *(int *)sd : -1;
        if (ost->frame_rate.num) {
            pkt->duration = av_rescale_q(1, av_inv_q(ost->frame_rate),
                                         ost->mux_timebase);
        }
    }
    av_packet_rescale_ts(pkt, ost->mux_timebase, ost->st->time_base);
    if (!(s->oformat->flags & AVFMT_NOTIMESTAMPS) &&
        ost->last_mux_dts != AV_NOPTS_VALUE &&
        pkt->dts < ost->last_mux_dts + !(s->oformat->flags & AVFMT_TS_NONSTRICT)) {
        av_log(NULL, AV_LOG_WARNING, "Non-monotonous DTS in output stream "
               "%d:%d; previous: %"PRId64", current: %"PRId64"; ",
               ost->file_index, ost->st->index, ost->last_mux_dts, pkt->dts);
        if (exit_on_error) {
            av_log(NULL, AV_LOG_FATAL, "aborting.\n");
            exit_program(1);
        }
        av_log(NULL, AV_LOG_WARNING, "changing to %"PRId64". This may result "
               "in incorrect timestamps in the output file.\n",
               ost->last_mux_dts + 1);
        pkt->dts = ost->last_mux_dts + 1;
        if (pkt->pts != AV_NOPTS_VALUE)
            pkt->pts = FFMAX(pkt->pts, pkt->dts);
    }
    ost->last_mux_dts = pkt->dts;
    ost->data_size += pkt->size;
    ost->packets_written++;
    pkt->stream_index = ost->index;
    ret = av_interleaved_write_frame(s, pkt);
    if (ret < 0) {
        print_error("av_interleaved_write_frame()", ret);
        exit_program(1);
    }
}
['static void write_packet(OutputFile *of, AVPacket *pkt, OutputStream *ost)\n{\n    AVFormatContext *s = of->ctx;\n    AVStream *st = ost->st;\n    int ret;\n    if (!of->header_written) {\n        AVPacket tmp_pkt = {0};\n        if (!av_fifo_space(ost->muxing_queue)) {\n            int new_size = FFMIN(2 * av_fifo_size(ost->muxing_queue),\n                                 ost->max_muxing_queue_size);\n            if (new_size <= av_fifo_size(ost->muxing_queue)) {\n                av_log(NULL, AV_LOG_ERROR,\n                       "Too many packets buffered for output stream %d:%d.\\n",\n                       ost->file_index, ost->st->index);\n                exit_program(1);\n            }\n            ret = av_fifo_realloc2(ost->muxing_queue, new_size);\n            if (ret < 0)\n                exit_program(1);\n        }\n        ret = av_packet_ref(&tmp_pkt, pkt);\n        if (ret < 0)\n            exit_program(1);\n        av_fifo_generic_write(ost->muxing_queue, &tmp_pkt, sizeof(tmp_pkt), NULL);\n        av_packet_unref(pkt);\n        return;\n    }\n    if (!(st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO && ost->encoding_needed)) {\n        if (ost->frame_number >= ost->max_frames) {\n            av_packet_unref(pkt);\n            return;\n        }\n        ost->frame_number++;\n    }\n    if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {\n        uint8_t *sd = av_packet_get_side_data(pkt, AV_PKT_DATA_QUALITY_FACTOR,\n                                              NULL);\n        ost->quality = sd ? *(int *)sd : -1;\n        if (ost->frame_rate.num) {\n            pkt->duration = av_rescale_q(1, av_inv_q(ost->frame_rate),\n                                         ost->mux_timebase);\n        }\n    }\n    av_packet_rescale_ts(pkt, ost->mux_timebase, ost->st->time_base);\n    if (!(s->oformat->flags & AVFMT_NOTIMESTAMPS) &&\n        ost->last_mux_dts != AV_NOPTS_VALUE &&\n        pkt->dts < ost->last_mux_dts + !(s->oformat->flags & AVFMT_TS_NONSTRICT)) {\n        av_log(NULL, AV_LOG_WARNING, "Non-monotonous DTS in output stream "\n               "%d:%d; previous: %"PRId64", current: %"PRId64"; ",\n               ost->file_index, ost->st->index, ost->last_mux_dts, pkt->dts);\n        if (exit_on_error) {\n            av_log(NULL, AV_LOG_FATAL, "aborting.\\n");\n            exit_program(1);\n        }\n        av_log(NULL, AV_LOG_WARNING, "changing to %"PRId64". This may result "\n               "in incorrect timestamps in the output file.\\n",\n               ost->last_mux_dts + 1);\n        pkt->dts = ost->last_mux_dts + 1;\n        if (pkt->pts != AV_NOPTS_VALUE)\n            pkt->pts = FFMAX(pkt->pts, pkt->dts);\n    }\n    ost->last_mux_dts = pkt->dts;\n    ost->data_size += pkt->size;\n    ost->packets_written++;\n    pkt->stream_index = ost->index;\n    ret = av_interleaved_write_frame(s, pkt);\n    if (ret < 0) {\n        print_error("av_interleaved_write_frame()", ret);\n        exit_program(1);\n    }\n}', 'int av_fifo_space(AVFifoBuffer *f)\n{\n    return f->end - f->buffer - av_fifo_size(f);\n}', 'int av_fifo_size(AVFifoBuffer *f)\n{\n    return (uint32_t)(f->wndx - f->rndx);\n}', 'int av_fifo_realloc2(AVFifoBuffer *f, unsigned int new_size)\n{\n    unsigned int old_size = f->end - f->buffer;\n    if (old_size < new_size) {\n        int len          = av_fifo_size(f);\n        AVFifoBuffer *f2 = av_fifo_alloc(new_size);\n        if (!f2)\n            return AVERROR(ENOMEM);\n        av_fifo_generic_read(f, f2->buffer, len, NULL);\n        f2->wptr += len;\n        f2->wndx += len;\n        av_free(f->buffer);\n        *f = *f2;\n        av_free(f2);\n    }\n    return 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}']