static int write_packet(AVFormatContext *s, AVPacket *pkt)
{
    int ret, did_split;
    if (s->output_ts_offset) {
        AVStream *st = s->streams[pkt->stream_index];
        int64_t offset = av_rescale_q(s->output_ts_offset, AV_TIME_BASE_Q, st->time_base);
        if (pkt->dts != AV_NOPTS_VALUE)
            pkt->dts += offset;
        if (pkt->pts != AV_NOPTS_VALUE)
            pkt->pts += offset;
    }
    if (s->avoid_negative_ts > 0) {
        AVStream *st = s->streams[pkt->stream_index];
        int64_t offset = st->mux_ts_offset;
        int64_t ts = s->internal->avoid_negative_ts_use_pts ? pkt->pts : pkt->dts;
        if (s->internal->offset == AV_NOPTS_VALUE && ts != AV_NOPTS_VALUE &&
            (ts < 0 || s->avoid_negative_ts == AVFMT_AVOID_NEG_TS_MAKE_ZERO)) {
            s->internal->offset = -ts;
            s->internal->offset_timebase = st->time_base;
        }
        if (s->internal->offset != AV_NOPTS_VALUE && !offset) {
            offset = st->mux_ts_offset =
                av_rescale_q_rnd(s->internal->offset,
                                 s->internal->offset_timebase,
                                 st->time_base,
                                 AV_ROUND_UP);
        }
        if (pkt->dts != AV_NOPTS_VALUE)
            pkt->dts += offset;
        if (pkt->pts != AV_NOPTS_VALUE)
            pkt->pts += offset;
        if (s->internal->avoid_negative_ts_use_pts) {
            if (pkt->pts != AV_NOPTS_VALUE && pkt->pts < 0) {
                av_log(s, AV_LOG_WARNING, "failed to avoid negative "
                    "pts %s in stream %d.\n"
                    "Try -avoid_negative_ts 1 as a possible workaround.\n",
                    av_ts2str(pkt->dts),
                    pkt->stream_index
                );
            }
        } else {
            av_assert2(pkt->dts == AV_NOPTS_VALUE || pkt->dts >= 0 || s->max_interleave_delta > 0);
            if (pkt->dts != AV_NOPTS_VALUE && pkt->dts < 0) {
                av_log(s, AV_LOG_WARNING,
                    "Packets poorly interleaved, failed to avoid negative "
                    "timestamp %s in stream %d.\n"
                    "Try -max_interleave_delta 0 as a possible workaround.\n",
                    av_ts2str(pkt->dts),
                    pkt->stream_index
                );
            }
        }
    }
    did_split = av_packet_split_side_data(pkt);
    if (!s->internal->header_written) {
        ret = s->internal->write_header_ret ? s->internal->write_header_ret : write_header_internal(s);
        if (ret < 0)
            goto fail;
    }
    if ((pkt->flags & AV_PKT_FLAG_UNCODED_FRAME)) {
        AVFrame *frame = (AVFrame *)pkt->data;
        av_assert0(pkt->size == UNCODED_FRAME_PACKET_SIZE);
        ret = s->oformat->write_uncoded_frame(s, pkt->stream_index, &frame, 0);
        av_frame_free(&frame);
    } else {
        ret = s->oformat->write_packet(s, pkt);
    }
    if (s->pb && ret >= 0) {
        if (s->flush_packets && s->flags & AVFMT_FLAG_FLUSH_PACKETS)
            avio_flush(s->pb);
        if (s->pb->error < 0)
            ret = s->pb->error;
    }
fail:
    if (did_split)
        av_packet_merge_side_data(pkt);
    if (ret < 0) {
        pkt->pts = pts_backup;
        pkt->dts = dts_backup;
    }
    return ret;
}
static int write_packet(AVFormatContext *s, AVPacket *pkt)
{
    int ret, did_split;
    if (s->output_ts_offset) {
        AVStream *st = s->streams[pkt->stream_index];
        int64_t offset = av_rescale_q(s->output_ts_offset, AV_TIME_BASE_Q, st->time_base);
        if (pkt->dts != AV_NOPTS_VALUE)
            pkt->dts += offset;
        if (pkt->pts != AV_NOPTS_VALUE)
            pkt->pts += offset;
    }
    if (s->avoid_negative_ts > 0) {
        AVStream *st = s->streams[pkt->stream_index];
        int64_t offset = st->mux_ts_offset;
        int64_t ts = s->internal->avoid_negative_ts_use_pts ? pkt->pts : pkt->dts;
        if (s->internal->offset == AV_NOPTS_VALUE && ts != AV_NOPTS_VALUE &&
            (ts < 0 || s->avoid_negative_ts == AVFMT_AVOID_NEG_TS_MAKE_ZERO)) {
            s->internal->offset = -ts;
            s->internal->offset_timebase = st->time_base;
        }
        if (s->internal->offset != AV_NOPTS_VALUE && !offset) {
            offset = st->mux_ts_offset =
                av_rescale_q_rnd(s->internal->offset,
                                 s->internal->offset_timebase,
                                 st->time_base,
                                 AV_ROUND_UP);
        }
        if (pkt->dts != AV_NOPTS_VALUE)
            pkt->dts += offset;
        if (pkt->pts != AV_NOPTS_VALUE)
            pkt->pts += offset;
        if (s->internal->avoid_negative_ts_use_pts) {
            if (pkt->pts != AV_NOPTS_VALUE && pkt->pts < 0) {
                av_log(s, AV_LOG_WARNING, "failed to avoid negative "
                    "pts %s in stream %d.\n"
                    "Try -avoid_negative_ts 1 as a possible workaround.\n",
                    av_ts2str(pkt->dts),
                    pkt->stream_index
                );
            }
        } else {
            av_assert2(pkt->dts == AV_NOPTS_VALUE || pkt->dts >= 0 || s->max_interleave_delta > 0);
            if (pkt->dts != AV_NOPTS_VALUE && pkt->dts < 0) {
                av_log(s, AV_LOG_WARNING,
                    "Packets poorly interleaved, failed to avoid negative "
                    "timestamp %s in stream %d.\n"
                    "Try -max_interleave_delta 0 as a possible workaround.\n",
                    av_ts2str(pkt->dts),
                    pkt->stream_index
                );
            }
        }
    }
    did_split = av_packet_split_side_data(pkt);
    if (!s->internal->header_written) {
        ret = s->internal->write_header_ret ? s->internal->write_header_ret : write_header_internal(s);
        if (ret < 0)
            goto fail;
    }
    if ((pkt->flags & AV_PKT_FLAG_UNCODED_FRAME)) {
        AVFrame *frame = (AVFrame *)pkt->data;
        av_assert0(pkt->size == UNCODED_FRAME_PACKET_SIZE);
        ret = s->oformat->write_uncoded_frame(s, pkt->stream_index, &frame, 0);
        av_frame_free(&frame);
    } else {
        ret = s->oformat->write_packet(s, pkt);
    }
    if (s->pb && ret >= 0) {
        if (s->flush_packets && s->flags & AVFMT_FLAG_FLUSH_PACKETS)
            avio_flush(s->pb);
        if (s->pb->error < 0)
            ret = s->pb->error;
    }
fail:
    if (did_split)
        av_packet_merge_side_data(pkt);
    if (ret < 0) {
        pkt->pts = pts_backup;
        pkt->dts = dts_backup;
    }
    return ret;
}
static int FUNC_0(AVFormatContext *VAR_0, AVPacket *VAR_1)
{
    int VAR_2, VAR_3;
    if (VAR_0->output_ts_offset) {
        AVStream *st = VAR_0->streams[VAR_1->stream_index];
        int64_t offset = av_rescale_q(VAR_0->output_ts_offset, AV_TIME_BASE_Q, st->time_base);
        if (VAR_1->dts != AV_NOPTS_VALUE)
            VAR_1->dts += offset;
        if (VAR_1->pts != AV_NOPTS_VALUE)
            VAR_1->pts += offset;
    }
    if (VAR_0->avoid_negative_ts > 0) {
        AVStream *st = VAR_0->streams[VAR_1->stream_index];
        int64_t offset = st->mux_ts_offset;
        int64_t ts = VAR_0->internal->avoid_negative_ts_use_pts ? VAR_1->pts : VAR_1->dts;
        if (VAR_0->internal->offset == AV_NOPTS_VALUE && ts != AV_NOPTS_VALUE &&
            (ts < 0 || VAR_0->avoid_negative_ts == AVFMT_AVOID_NEG_TS_MAKE_ZERO)) {
            VAR_0->internal->offset = -ts;
            VAR_0->internal->offset_timebase = st->time_base;
        }
        if (VAR_0->internal->offset != AV_NOPTS_VALUE && !offset) {
            offset = st->mux_ts_offset =
                av_rescale_q_rnd(VAR_0->internal->offset,
                                 VAR_0->internal->offset_timebase,
                                 st->time_base,
                                 AV_ROUND_UP);
        }
        if (VAR_1->dts != AV_NOPTS_VALUE)
            VAR_1->dts += offset;
        if (VAR_1->pts != AV_NOPTS_VALUE)
            VAR_1->pts += offset;
        if (VAR_0->internal->avoid_negative_ts_use_pts) {
            if (VAR_1->pts != AV_NOPTS_VALUE && VAR_1->pts < 0) {
                av_log(VAR_0, AV_LOG_WARNING, "failed to avoid negative "
                    "pts %VAR_0 in stream %d.\n"
                    "Try -avoid_negative_ts 1 as a possible workaround.\n",
                    av_ts2str(VAR_1->dts),
                    VAR_1->stream_index
                );
            }
        } else {
            av_assert2(VAR_1->dts == AV_NOPTS_VALUE || VAR_1->dts >= 0 || VAR_0->max_interleave_delta > 0);
            if (VAR_1->dts != AV_NOPTS_VALUE && VAR_1->dts < 0) {
                av_log(VAR_0, AV_LOG_WARNING,
                    "Packets poorly interleaved, failed to avoid negative "
                    "timestamp %VAR_0 in stream %d.\n"
                    "Try -max_interleave_delta 0 as a possible workaround.\n",
                    av_ts2str(VAR_1->dts),
                    VAR_1->stream_index
                );
            }
        }
    }
    VAR_3 = av_packet_split_side_data(VAR_1);
    if (!VAR_0->internal->header_written) {
        VAR_2 = VAR_0->internal->write_header_ret ? VAR_0->internal->write_header_ret : write_header_internal(VAR_0);
        if (VAR_2 < 0)
            goto fail;
    }
    if ((VAR_1->flags & AV_PKT_FLAG_UNCODED_FRAME)) {
        AVFrame *frame = (AVFrame *)VAR_1->data;
        av_assert0(VAR_1->size == UNCODED_FRAME_PACKET_SIZE);
        VAR_2 = VAR_0->oformat->write_uncoded_frame(VAR_0, VAR_1->stream_index, &frame, 0);
        av_frame_free(&frame);
    } else {
        VAR_2 = VAR_0->oformat->FUNC_0(VAR_0, VAR_1);
    }
    if (VAR_0->pb && VAR_2 >= 0) {
        if (VAR_0->flush_packets && VAR_0->flags & AVFMT_FLAG_FLUSH_PACKETS)
            avio_flush(VAR_0->pb);
        if (VAR_0->pb->error < 0)
            VAR_2 = VAR_0->pb->error;
    }
fail:
    if (VAR_3)
        av_packet_merge_side_data(VAR_1);
    if (VAR_2 < 0) {
        VAR_1->pts = pts_backup;
        VAR_1->dts = dts_backup;
    }
    return VAR_2;
}
static int FUNC_0(AVFormatContext *VAR_0, AVPacket *VAR_1)
{ int VAR_2, VAR_3; if (VAR_0->output_ts_offset) { AVStream *st = VAR_0->streams[VAR_1->stream_index]; int64_t offset = av_rescale_q(VAR_0->output_ts_offset, AV_TIME_BASE_Q, st->time_base); if (VAR_1->dts != AV_NOPTS_VALUE)
VAR_1->dts += offset; if (VAR_1->pts != AV_NOPTS_VALUE)
VAR_1->pts += offset; } if (VAR_0->avoid_negative_ts > 0) { AVStream *st = VAR_0->streams[VAR_1->stream_index]; int64_t offset = st->mux_ts_offset; int64_t ts = VAR_0->internal->avoid_negative_ts_use_pts ? VAR_1->pts : VAR_1->dts; if (VAR_0->internal->offset == AV_NOPTS_VALUE && ts != AV_NOPTS_VALUE &&
(ts < 0 || VAR_0->avoid_negative_ts == AVFMT_AVOID_NEG_TS_MAKE_ZERO)) { VAR_0->internal->offset = -ts; VAR_0->internal->offset_timebase = st->time_base; } if (VAR_0->internal->offset != AV_NOPTS_VALUE && !offset) { offset = st->mux_ts_offset =
av_rescale_q_rnd(VAR_0->internal->offset,
VAR_0->internal->offset_timebase,
st->time_base,
AV_ROUND_UP); } if (VAR_1->dts != AV_NOPTS_VALUE)
VAR_1->dts += offset; if (VAR_1->pts != AV_NOPTS_VALUE)
VAR_1->pts += offset; if (VAR_0->internal->avoid_negative_ts_use_pts) { if (VAR_1->pts != AV_NOPTS_VALUE && VAR_1->pts < 0) { av_log(VAR_0, AV_LOG_WARNING, "failed to avoid negative "
"pts %VAR_0 in stream %d.\n"
"Try -avoid_negative_ts 1 as a possible workaround.\n",
av_ts2str(VAR_1->dts),
VAR_1->stream_index
); } } else { av_assert2(VAR_1->dts == AV_NOPTS_VALUE || VAR_1->dts >= 0 || VAR_0->max_interleave_delta > 0); if (VAR_1->dts != AV_NOPTS_VALUE && VAR_1->dts < 0) { av_log(VAR_0, AV_LOG_WARNING,
"Packets poorly interleaved, failed to avoid negative "
"timestamp %VAR_0 in stream %d.\n"
"Try -max_interleave_delta 0 as a possible workaround.\n",
av_ts2str(VAR_1->dts),
VAR_1->stream_index
); } } } VAR_3 = av_packet_split_side_data(VAR_1); if (!VAR_0->internal->header_written) { VAR_2 = VAR_0->internal->write_header_ret ? VAR_0->internal->write_header_ret : write_header_internal(VAR_0); if (VAR_2 < 0)
goto fail; } if ((VAR_1->flags & AV_PKT_FLAG_UNCODED_FRAME)) { AVFrame *frame = (AVFrame *)VAR_1->data; av_assert0(VAR_1->size == UNCODED_FRAME_PACKET_SIZE); VAR_2 = VAR_0->oformat->write_uncoded_frame(VAR_0, VAR_1->stream_index, &frame, 0); av_frame_free(&frame); } else { VAR_2 = VAR_0->oformat->FUNC_0(VAR_0, VAR_1); } if (VAR_0->pb && VAR_2 >= 0) { if (VAR_0->flush_packets && VAR_0->flags & AVFMT_FLAG_FLUSH_PACKETS)
avio_flush(VAR_0->pb); if (VAR_0->pb->error < 0)
VAR_2 = VAR_0->pb->error; } fail:
if (VAR_3)
av_packet_merge_side_data(VAR_1); if (VAR_2 < 0) { VAR_1->pts = pts_backup; VAR_1->dts = dts_backup; } return VAR_2; }