static int mpeg_mux_write_packet(AVFormatContext *ctx, AVPacket *pkt)

{

    int stream_index = pkt->stream_index;

    int size         = pkt->size;

    uint8_t *buf     = pkt->data;

    MpegMuxContext *s = ctx->priv_data;

    AVStream *st      = ctx->streams[stream_index];

    StreamInfo *stream = st->priv_data;

    int64_t pts, dts;

    PacketDesc *pkt_desc;

    int preload;

    const int is_iframe = st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO &&

                          (pkt->flags & AV_PKT_FLAG_KEY);



    preload = av_rescale(s->preload, 90000, AV_TIME_BASE);



    pts = pkt->pts;

    dts = pkt->dts;



    if (s->last_scr == AV_NOPTS_VALUE) {

        if (dts == AV_NOPTS_VALUE || (dts < preload && ctx->avoid_negative_ts) || s->is_dvd) {

            if (dts != AV_NOPTS_VALUE)

                s->preload += av_rescale(-dts, AV_TIME_BASE, 90000);

            s->last_scr = 0;

        } else {

            s->last_scr = dts - preload;

            s->preload = 0;

        }

        preload = av_rescale(s->preload, 90000, AV_TIME_BASE);

        av_log(ctx, AV_LOG_DEBUG, "First SCR: %"PRId64" First DTS: %"PRId64"\n", s->last_scr, dts + preload);

    }



    if (dts != AV_NOPTS_VALUE) dts += preload;

    if (pts != AV_NOPTS_VALUE) pts += preload;



    av_log(ctx, AV_LOG_TRACE, "dts:%f pts:%f flags:%d stream:%d nopts:%d\n",

            dts / 90000.0, pts / 90000.0, pkt->flags,

            pkt->stream_index, pts != AV_NOPTS_VALUE);

    if (!stream->premux_packet)

        stream->next_packet = &stream->premux_packet;

    *stream->next_packet     =

    pkt_desc                 = av_mallocz(sizeof(PacketDesc));



    pkt_desc->pts            = pts;

    pkt_desc->dts            = dts;

    pkt_desc->unwritten_size =

    pkt_desc->size           = size;

    if (!stream->predecode_packet)

        stream->predecode_packet = pkt_desc;

    stream->next_packet = &pkt_desc->next;



    if (av_fifo_realloc2(stream->fifo, av_fifo_size(stream->fifo) + size) < 0)

        return -1;



    if (s->is_dvd) {

        // min VOBU length 0.4 seconds (mpucoder)

        if (is_iframe &&

            (s->packet_number == 0 ||

             (pts - stream->vobu_start_pts >= 36000))) {

            stream->bytes_to_iframe = av_fifo_size(stream->fifo);

            stream->align_iframe    = 1;

            stream->vobu_start_pts  = pts;

        }

    }



    av_fifo_generic_write(stream->fifo, buf, size, NULL);



    for (;;) {

        int ret = output_packet(ctx, 0);

        if (ret <= 0)

            return ret;

    }

}
static int mpeg_mux_write_packet(AVFormatContext *ctx, AVPacket *pkt)

{

    int stream_index = pkt->stream_index;

    int size         = pkt->size;

    uint8_t *buf     = pkt->data;

    MpegMuxContext *s = ctx->priv_data;

    AVStream *st      = ctx->streams[stream_index];

    StreamInfo *stream = st->priv_data;

    int64_t pts, dts;

    PacketDesc *pkt_desc;

    int preload;

    const int is_iframe = st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO &&

                          (pkt->flags & AV_PKT_FLAG_KEY);



    preload = av_rescale(s->preload, 90000, AV_TIME_BASE);



    pts = pkt->pts;

    dts = pkt->dts;



    if (s->last_scr == AV_NOPTS_VALUE) {

        if (dts == AV_NOPTS_VALUE || (dts < preload && ctx->avoid_negative_ts) || s->is_dvd) {

            if (dts != AV_NOPTS_VALUE)

                s->preload += av_rescale(-dts, AV_TIME_BASE, 90000);

            s->last_scr = 0;

        } else {

            s->last_scr = dts - preload;

            s->preload = 0;

        }

        preload = av_rescale(s->preload, 90000, AV_TIME_BASE);

        av_log(ctx, AV_LOG_DEBUG, "First SCR: %"PRId64" First DTS: %"PRId64"\n", s->last_scr, dts + preload);

    }



    if (dts != AV_NOPTS_VALUE) dts += preload;

    if (pts != AV_NOPTS_VALUE) pts += preload;



    av_log(ctx, AV_LOG_TRACE, "dts:%f pts:%f flags:%d stream:%d nopts:%d\n",

            dts / 90000.0, pts / 90000.0, pkt->flags,

            pkt->stream_index, pts != AV_NOPTS_VALUE);

    if (!stream->premux_packet)

        stream->next_packet = &stream->premux_packet;

    *stream->next_packet     =

    pkt_desc                 = av_mallocz(sizeof(PacketDesc));



    pkt_desc->pts            = pts;

    pkt_desc->dts            = dts;

    pkt_desc->unwritten_size =

    pkt_desc->size           = size;

    if (!stream->predecode_packet)

        stream->predecode_packet = pkt_desc;

    stream->next_packet = &pkt_desc->next;



    if (av_fifo_realloc2(stream->fifo, av_fifo_size(stream->fifo) + size) < 0)

        return -1;



    if (s->is_dvd) {

        

        if (is_iframe &&

            (s->packet_number == 0 ||

             (pts - stream->vobu_start_pts >= 36000))) {

            stream->bytes_to_iframe = av_fifo_size(stream->fifo);

            stream->align_iframe    = 1;

            stream->vobu_start_pts  = pts;

        }

    }



    av_fifo_generic_write(stream->fifo, buf, size, NULL);



    for (;;) {

        int ret = output_packet(ctx, 0);

        if (ret <= 0)

            return ret;

    }

}
static int FUNC_0(AVFormatContext *VAR_0, AVPacket *VAR_1)

{

    int VAR_2 = VAR_1->VAR_2;

    int VAR_3         = VAR_1->VAR_3;

    uint8_t *buf     = VAR_1->data;

    MpegMuxContext *s = VAR_0->priv_data;

    AVStream *st      = VAR_0->streams[VAR_2];

    StreamInfo *stream = st->priv_data;

    int64_t pts, dts;

    PacketDesc *pkt_desc;

    int VAR_4;

    const int VAR_5 = st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO &&

                          (VAR_1->flags & AV_PKT_FLAG_KEY);



    VAR_4 = av_rescale(s->VAR_4, 90000, AV_TIME_BASE);



    pts = VAR_1->pts;

    dts = VAR_1->dts;



    if (s->last_scr == AV_NOPTS_VALUE) {

        if (dts == AV_NOPTS_VALUE || (dts < VAR_4 && VAR_0->avoid_negative_ts) || s->is_dvd) {

            if (dts != AV_NOPTS_VALUE)

                s->VAR_4 += av_rescale(-dts, AV_TIME_BASE, 90000);

            s->last_scr = 0;

        } else {

            s->last_scr = dts - VAR_4;

            s->VAR_4 = 0;

        }

        VAR_4 = av_rescale(s->VAR_4, 90000, AV_TIME_BASE);

        av_log(VAR_0, AV_LOG_DEBUG, "First SCR: %"PRId64" First DTS: %"PRId64"\n", s->last_scr, dts + VAR_4);

    }



    if (dts != AV_NOPTS_VALUE) dts += VAR_4;

    if (pts != AV_NOPTS_VALUE) pts += VAR_4;



    av_log(VAR_0, AV_LOG_TRACE, "dts:%f pts:%f flags:%d stream:%d nopts:%d\n",

            dts / 90000.0, pts / 90000.0, VAR_1->flags,

            VAR_1->VAR_2, pts != AV_NOPTS_VALUE);

    if (!stream->premux_packet)

        stream->next_packet = &stream->premux_packet;

    *stream->next_packet     =

    pkt_desc                 = av_mallocz(sizeof(PacketDesc));



    pkt_desc->pts            = pts;

    pkt_desc->dts            = dts;

    pkt_desc->unwritten_size =

    pkt_desc->VAR_3           = VAR_3;

    if (!stream->predecode_packet)

        stream->predecode_packet = pkt_desc;

    stream->next_packet = &pkt_desc->next;



    if (av_fifo_realloc2(stream->fifo, av_fifo_size(stream->fifo) + VAR_3) < 0)

        return -1;



    if (s->is_dvd) {

        

        if (VAR_5 &&

            (s->packet_number == 0 ||

             (pts - stream->vobu_start_pts >= 36000))) {

            stream->bytes_to_iframe = av_fifo_size(stream->fifo);

            stream->align_iframe    = 1;

            stream->vobu_start_pts  = pts;

        }

    }



    av_fifo_generic_write(stream->fifo, buf, VAR_3, NULL);



    for (;;) {

        int VAR_6 = output_packet(VAR_0, 0);

        if (VAR_6 <= 0)

            return VAR_6;

    }

}
static int FUNC_0(AVFormatContext *VAR_0, AVPacket *VAR_1)
{ int VAR_2 = VAR_1->VAR_2; int VAR_3         = VAR_1->VAR_3; uint8_t *buf     = VAR_1->data; MpegMuxContext *s = VAR_0->priv_data; AVStream *st      = VAR_0->streams[VAR_2]; StreamInfo *stream = st->priv_data; int64_t pts, dts; PacketDesc *pkt_desc; int VAR_4; const int VAR_5 = st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO &&
(VAR_1->flags & AV_PKT_FLAG_KEY); VAR_4 = av_rescale(s->VAR_4, 90000, AV_TIME_BASE); pts = VAR_1->pts; dts = VAR_1->dts; if (s->last_scr == AV_NOPTS_VALUE) { if (dts == AV_NOPTS_VALUE || (dts < VAR_4 && VAR_0->avoid_negative_ts) || s->is_dvd) { if (dts != AV_NOPTS_VALUE)
s->VAR_4 += av_rescale(-dts, AV_TIME_BASE, 90000); s->last_scr = 0; } else { s->last_scr = dts - VAR_4; s->VAR_4 = 0; } VAR_4 = av_rescale(s->VAR_4, 90000, AV_TIME_BASE); av_log(VAR_0, AV_LOG_DEBUG, "First SCR: %"PRId64" First DTS: %"PRId64"\n", s->last_scr, dts + VAR_4); } if (dts != AV_NOPTS_VALUE) dts += VAR_4; if (pts != AV_NOPTS_VALUE) pts += VAR_4; av_log(VAR_0, AV_LOG_TRACE, "dts:%f pts:%f flags:%d stream:%d nopts:%d\n",
dts / 90000.0, pts / 90000.0, VAR_1->flags,
VAR_1->VAR_2, pts != AV_NOPTS_VALUE); if (!stream->premux_packet)
stream->next_packet = &stream->premux_packet; *stream->next_packet     =
pkt_desc                 = av_mallocz(sizeof(PacketDesc)); pkt_desc->pts            = pts; pkt_desc->dts            = dts; pkt_desc->unwritten_size =
pkt_desc->VAR_3           = VAR_3; if (!stream->predecode_packet)
stream->predecode_packet = pkt_desc; stream->next_packet = &pkt_desc->next; if (av_fifo_realloc2(stream->fifo, av_fifo_size(stream->fifo) + VAR_3) < 0)
return -1; if (s->is_dvd) { if (VAR_5 &&
(s->packet_number == 0 ||
(pts - stream->vobu_start_pts >= 36000))) { stream->bytes_to_iframe = av_fifo_size(stream->fifo); stream->align_iframe    = 1; stream->vobu_start_pts  = pts; } } av_fifo_generic_write(stream->fifo, buf, VAR_3, NULL); for (;;) { int VAR_6 = output_packet(VAR_0, 0); if (VAR_6 <= 0)
return VAR_6; } }