static int compute_pkt_fields2(AVFormatContext *s, AVStream *st, AVPacket *pkt)
{
    int delay = FFMAX(st->codec->has_b_frames, st->codec->max_b_frames > 0);
    int num, den, frame_size, i;
    av_dlog(s, "compute_pkt_fields2: pts:%s dts:%s cur_dts:%s b:%d size:%d st:%d\n",
            av_ts2str(pkt->pts), av_ts2str(pkt->dts), av_ts2str(st->cur_dts), delay, pkt->size, pkt->stream_index);
    /* duration field */
    if (pkt->duration == 0) {
        ff_compute_frame_duration(&num, &den, st, NULL, pkt);
        if (den && num) {
            pkt->duration = av_rescale(1, num * (int64_t)st->time_base.den * st->codec->ticks_per_frame, den * (int64_t)st->time_base.num);
    if (pkt->pts == AV_NOPTS_VALUE && pkt->dts != AV_NOPTS_VALUE && delay == 0)
        pkt->pts = pkt->dts;
    //XXX/FIXME this is a temporary hack until all encoders output pts
    if ((pkt->pts == 0 || pkt->pts == AV_NOPTS_VALUE) && pkt->dts == AV_NOPTS_VALUE && !delay) {
        static int warned;
        if (!warned) {
            av_log(s, AV_LOG_WARNING, "Encoder did not produce proper pts, making some up.\n");
            warned = 1;
        pkt->dts =
//        pkt->pts= st->cur_dts;
            pkt->pts = st->pts.val;
    //calculate dts from pts
    if (pkt->pts != AV_NOPTS_VALUE && pkt->dts == AV_NOPTS_VALUE && delay <= MAX_REORDER_DELAY) {
        st->pts_buffer[0] = pkt->pts;
        for (i = 1; i < delay + 1 && st->pts_buffer[i] == AV_NOPTS_VALUE; i++)
            st->pts_buffer[i] = pkt->pts + (i - delay - 1) * pkt->duration;
        for (i = 0; i<delay && st->pts_buffer[i] > st->pts_buffer[i + 1]; i++)
            FFSWAP(int64_t, st->pts_buffer[i], st->pts_buffer[i + 1]);
        pkt->dts = st->pts_buffer[0];
    if (st->cur_dts && st->cur_dts != AV_NOPTS_VALUE &&
        ((!(s->oformat->flags & AVFMT_TS_NONSTRICT) &&
          st->cur_dts >= pkt->dts) || st->cur_dts > pkt->dts)) {
        av_log(s, AV_LOG_ERROR,
               "Application provided invalid, non monotonically increasing dts to muxer in stream %d: %s >= %s\n",
               st->index, av_ts2str(st->cur_dts), av_ts2str(pkt->dts));
        return AVERROR(EINVAL);
    if (pkt->dts != AV_NOPTS_VALUE && pkt->pts != AV_NOPTS_VALUE && pkt->pts < pkt->dts) {
        av_log(s, AV_LOG_ERROR, "pts (%s) < dts (%s) in stream %d\n",
               av_ts2str(pkt->pts), av_ts2str(pkt->dts), st->index);
        return AVERROR(EINVAL);
    av_dlog(s, "av_write_frame: pts2:%s dts2:%s\n",
            av_ts2str(pkt->pts), av_ts2str(pkt->dts));
    st->cur_dts = pkt->dts;
    st->pts.val = pkt->dts;
    /* update pts */
    switch (st->codec->codec_type) {
    case AVMEDIA_TYPE_AUDIO:
        frame_size = (pkt->flags & AV_PKT_FLAG_UNCODED_FRAME) ?
                     ((AVFrame *)pkt->data)->nb_samples :
                     ff_get_audio_frame_size(st->codec, pkt->size, 1);
        /* HACK/FIXME, we skip the initial 0 size packets as they are most
         * likely equal to the encoder delay, but it would be better if we
         * had the real timestamps from the encoder */
        if (frame_size >= 0 && (pkt->size || st->pts.num != st->pts.den >> 1 || st->pts.val)) {
            frac_add(&st->pts, (int64_t)st->time_base.den * frame_size);
        break;
    case AVMEDIA_TYPE_VIDEO:
        frac_add(&st->pts, (int64_t)st->time_base.den * st->codec->time_base.num);
        break;
    default:
        break;
    return 0;
static int compute_pkt_fields2(AVFormatContext *s, AVStream *st, AVPacket *pkt)
{
    int delay = FFMAX(st->codec->has_b_frames, st->codec->max_b_frames > 0);
    int num, den, frame_size, i;
    av_dlog(s, "compute_pkt_fields2: pts:%s dts:%s cur_dts:%s b:%d size:%d st:%d\n",
            av_ts2str(pkt->pts), av_ts2str(pkt->dts), av_ts2str(st->cur_dts), delay, pkt->size, pkt->stream_index);
    
    if (pkt->duration == 0) {
        ff_compute_frame_duration(&num, &den, st, NULL, pkt);
        if (den && num) {
            pkt->duration = av_rescale(1, num * (int64_t)st->time_base.den * st->codec->ticks_per_frame, den * (int64_t)st->time_base.num);
    if (pkt->pts == AV_NOPTS_VALUE && pkt->dts != AV_NOPTS_VALUE && delay == 0)
        pkt->pts = pkt->dts;
    
    if ((pkt->pts == 0 || pkt->pts == AV_NOPTS_VALUE) && pkt->dts == AV_NOPTS_VALUE && !delay) {
        static int warned;
        if (!warned) {
            av_log(s, AV_LOG_WARNING, "Encoder did not produce proper pts, making some up.\n");
            warned = 1;
        pkt->dts =

            pkt->pts = st->pts.val;
    
    if (pkt->pts != AV_NOPTS_VALUE && pkt->dts == AV_NOPTS_VALUE && delay <= MAX_REORDER_DELAY) {
        st->pts_buffer[0] = pkt->pts;
        for (i = 1; i < delay + 1 && st->pts_buffer[i] == AV_NOPTS_VALUE; i++)
            st->pts_buffer[i] = pkt->pts + (i - delay - 1) * pkt->duration;
        for (i = 0; i<delay && st->pts_buffer[i] > st->pts_buffer[i + 1]; i++)
            FFSWAP(int64_t, st->pts_buffer[i], st->pts_buffer[i + 1]);
        pkt->dts = st->pts_buffer[0];
    if (st->cur_dts && st->cur_dts != AV_NOPTS_VALUE &&
        ((!(s->oformat->flags & AVFMT_TS_NONSTRICT) &&
          st->cur_dts >= pkt->dts) || st->cur_dts > pkt->dts)) {
        av_log(s, AV_LOG_ERROR,
               "Application provided invalid, non monotonically increasing dts to muxer in stream %d: %s >= %s\n",
               st->index, av_ts2str(st->cur_dts), av_ts2str(pkt->dts));
        return AVERROR(EINVAL);
    if (pkt->dts != AV_NOPTS_VALUE && pkt->pts != AV_NOPTS_VALUE && pkt->pts < pkt->dts) {
        av_log(s, AV_LOG_ERROR, "pts (%s) < dts (%s) in stream %d\n",
               av_ts2str(pkt->pts), av_ts2str(pkt->dts), st->index);
        return AVERROR(EINVAL);
    av_dlog(s, "av_write_frame: pts2:%s dts2:%s\n",
            av_ts2str(pkt->pts), av_ts2str(pkt->dts));
    st->cur_dts = pkt->dts;
    st->pts.val = pkt->dts;
    
    switch (st->codec->codec_type) {
    case AVMEDIA_TYPE_AUDIO:
        frame_size = (pkt->flags & AV_PKT_FLAG_UNCODED_FRAME) ?
                     ((AVFrame *)pkt->data)->nb_samples :
                     ff_get_audio_frame_size(st->codec, pkt->size, 1);
        


        if (frame_size >= 0 && (pkt->size || st->pts.num != st->pts.den >> 1 || st->pts.val)) {
            frac_add(&st->pts, (int64_t)st->time_base.den * frame_size);
        break;
    case AVMEDIA_TYPE_VIDEO:
        frac_add(&st->pts, (int64_t)st->time_base.den * st->codec->time_base.num);
        break;
    default:
        break;
    return 0;
static int FUNC_0(AVFormatContext *VAR_0, AVStream *VAR_1, AVPacket *VAR_2)
{
    int VAR_3 = FFMAX(VAR_1->codec->has_b_frames, VAR_1->codec->max_b_frames > 0);
    int VAR_4, VAR_5, VAR_6, VAR_7;
    av_dlog(VAR_0, "FUNC_0: pts:%VAR_0 dts:%VAR_0 cur_dts:%VAR_0 b:%d size:%d VAR_1:%d\n",
            av_ts2str(VAR_2->pts), av_ts2str(VAR_2->dts), av_ts2str(VAR_1->cur_dts), VAR_3, VAR_2->size, VAR_2->stream_index);
    
    if (VAR_2->duration == 0) {
        ff_compute_frame_duration(&VAR_4, &VAR_5, VAR_1, NULL, VAR_2);
        if (VAR_5 && VAR_4) {
            VAR_2->duration = av_rescale(1, VAR_4 * (int64_t)VAR_1->time_base.VAR_5 * VAR_1->codec->ticks_per_frame, VAR_5 * (int64_t)VAR_1->time_base.VAR_4);
    if (VAR_2->pts == AV_NOPTS_VALUE && VAR_2->dts != AV_NOPTS_VALUE && VAR_3 == 0)
        VAR_2->pts = VAR_2->dts;
    
    if ((VAR_2->pts == 0 || VAR_2->pts == AV_NOPTS_VALUE) && VAR_2->dts == AV_NOPTS_VALUE && !VAR_3) {
        static int VAR_8;
        if (!VAR_8) {
            av_log(VAR_0, AV_LOG_WARNING, "Encoder did not produce proper pts, making some up.\n");
            VAR_8 = 1;
        VAR_2->dts =

            VAR_2->pts = VAR_1->pts.val;
    
    if (VAR_2->pts != AV_NOPTS_VALUE && VAR_2->dts == AV_NOPTS_VALUE && VAR_3 <= MAX_REORDER_DELAY) {
        VAR_1->pts_buffer[0] = VAR_2->pts;
        for (VAR_7 = 1; VAR_7 < VAR_3 + 1 && VAR_1->pts_buffer[VAR_7] == AV_NOPTS_VALUE; VAR_7++)
            VAR_1->pts_buffer[VAR_7] = VAR_2->pts + (VAR_7 - VAR_3 - 1) * VAR_2->duration;
        for (VAR_7 = 0; VAR_7<VAR_3 && VAR_1->pts_buffer[VAR_7] > VAR_1->pts_buffer[VAR_7 + 1]; VAR_7++)
            FFSWAP(int64_t, VAR_1->pts_buffer[VAR_7], VAR_1->pts_buffer[VAR_7 + 1]);
        VAR_2->dts = VAR_1->pts_buffer[0];
    if (VAR_1->cur_dts && VAR_1->cur_dts != AV_NOPTS_VALUE &&
        ((!(VAR_0->oformat->flags & AVFMT_TS_NONSTRICT) &&
          VAR_1->cur_dts >= VAR_2->dts) || VAR_1->cur_dts > VAR_2->dts)) {
        av_log(VAR_0, AV_LOG_ERROR,
               "Application provided invalid, non monotonically increasing dts to muxer in stream %d: %VAR_0 >= %VAR_0\n",
               VAR_1->index, av_ts2str(VAR_1->cur_dts), av_ts2str(VAR_2->dts));
        return AVERROR(EINVAL);
    if (VAR_2->dts != AV_NOPTS_VALUE && VAR_2->pts != AV_NOPTS_VALUE && VAR_2->pts < VAR_2->dts) {
        av_log(VAR_0, AV_LOG_ERROR, "pts (%VAR_0) < dts (%VAR_0) in stream %d\n",
               av_ts2str(VAR_2->pts), av_ts2str(VAR_2->dts), VAR_1->index);
        return AVERROR(EINVAL);
    av_dlog(VAR_0, "av_write_frame: pts2:%VAR_0 dts2:%VAR_0\n",
            av_ts2str(VAR_2->pts), av_ts2str(VAR_2->dts));
    VAR_1->cur_dts = VAR_2->dts;
    VAR_1->pts.val = VAR_2->dts;
    
    switch (VAR_1->codec->codec_type) {
    case AVMEDIA_TYPE_AUDIO:
        VAR_6 = (VAR_2->flags & AV_PKT_FLAG_UNCODED_FRAME) ?
                     ((AVFrame *)VAR_2->data)->nb_samples :
                     ff_get_audio_frame_size(VAR_1->codec, VAR_2->size, 1);
        


        if (VAR_6 >= 0 && (VAR_2->size || VAR_1->pts.VAR_4 != VAR_1->pts.VAR_5 >> 1 || VAR_1->pts.val)) {
            frac_add(&VAR_1->pts, (int64_t)VAR_1->time_base.VAR_5 * VAR_6);
        break;
    case AVMEDIA_TYPE_VIDEO:
        frac_add(&VAR_1->pts, (int64_t)VAR_1->time_base.VAR_5 * VAR_1->codec->time_base.VAR_4);
        break;
    default:
        break;
    return 0;
static int FUNC_0(AVFormatContext *VAR_0, AVStream *VAR_1, AVPacket *VAR_2)
{ int VAR_3 = FFMAX(VAR_1->codec->has_b_frames, VAR_1->codec->max_b_frames > 0); int VAR_4, VAR_5, VAR_6, VAR_7; av_dlog(VAR_0, "FUNC_0: pts:%VAR_0 dts:%VAR_0 cur_dts:%VAR_0 b:%d size:%d VAR_1:%d\n",
av_ts2str(VAR_2->pts), av_ts2str(VAR_2->dts), av_ts2str(VAR_1->cur_dts), VAR_3, VAR_2->size, VAR_2->stream_index); if (VAR_2->duration == 0) { ff_compute_frame_duration(&VAR_4, &VAR_5, VAR_1, NULL, VAR_2); if (VAR_5 && VAR_4) { VAR_2->duration = av_rescale(1, VAR_4 * (int64_t)VAR_1->time_base.VAR_5 * VAR_1->codec->ticks_per_frame, VAR_5 * (int64_t)VAR_1->time_base.VAR_4); if (VAR_2->pts == AV_NOPTS_VALUE && VAR_2->dts != AV_NOPTS_VALUE && VAR_3 == 0)
VAR_2->pts = VAR_2->dts; if ((VAR_2->pts == 0 || VAR_2->pts == AV_NOPTS_VALUE) && VAR_2->dts == AV_NOPTS_VALUE && !VAR_3) { static int VAR_8; if (!VAR_8) { av_log(VAR_0, AV_LOG_WARNING, "Encoder did not produce proper pts, making some up.\n"); VAR_8 = 1; VAR_2->dts =
VAR_2->pts = VAR_1->pts.val; if (VAR_2->pts != AV_NOPTS_VALUE && VAR_2->dts == AV_NOPTS_VALUE && VAR_3 <= MAX_REORDER_DELAY) { VAR_1->pts_buffer[0] = VAR_2->pts; for (VAR_7 = 1; VAR_7 < VAR_3 + 1 && VAR_1->pts_buffer[VAR_7] == AV_NOPTS_VALUE; VAR_7++) VAR_1->pts_buffer[VAR_7] = VAR_2->pts + (VAR_7 - VAR_3 - 1) * VAR_2->duration; for (VAR_7 = 0; VAR_7<VAR_3 && VAR_1->pts_buffer[VAR_7] > VAR_1->pts_buffer[VAR_7 + 1]; VAR_7++) FFSWAP(int64_t, VAR_1->pts_buffer[VAR_7], VAR_1->pts_buffer[VAR_7 + 1]); VAR_2->dts = VAR_1->pts_buffer[0]; if (VAR_1->cur_dts && VAR_1->cur_dts != AV_NOPTS_VALUE &&
((!(VAR_0->oformat->flags & AVFMT_TS_NONSTRICT) &&
VAR_1->cur_dts >= VAR_2->dts) || VAR_1->cur_dts > VAR_2->dts)) { av_log(VAR_0, AV_LOG_ERROR,
"Application provided invalid, non monotonically increasing dts to muxer in stream %d: %VAR_0 >= %VAR_0\n",
VAR_1->index, av_ts2str(VAR_1->cur_dts), av_ts2str(VAR_2->dts)); return AVERROR(EINVAL); if (VAR_2->dts != AV_NOPTS_VALUE && VAR_2->pts != AV_NOPTS_VALUE && VAR_2->pts < VAR_2->dts) { av_log(VAR_0, AV_LOG_ERROR, "pts (%VAR_0) < dts (%VAR_0) in stream %d\n",
av_ts2str(VAR_2->pts), av_ts2str(VAR_2->dts), VAR_1->index); return AVERROR(EINVAL); av_dlog(VAR_0, "av_write_frame: pts2:%VAR_0 dts2:%VAR_0\n",
av_ts2str(VAR_2->pts), av_ts2str(VAR_2->dts)); VAR_1->cur_dts = VAR_2->dts; VAR_1->pts.val = VAR_2->dts; switch (VAR_1->codec->codec_type) { case AVMEDIA_TYPE_AUDIO:
VAR_6 = (VAR_2->flags & AV_PKT_FLAG_UNCODED_FRAME) ?
((AVFrame *)VAR_2->data)->nb_samples :
ff_get_audio_frame_size(VAR_1->codec, VAR_2->size, 1); if (VAR_6 >= 0 && (VAR_2->size || VAR_1->pts.VAR_4 != VAR_1->pts.VAR_5 >> 1 || VAR_1->pts.val)) { frac_add(&VAR_1->pts, (int64_t)VAR_1->time_base.VAR_5 * VAR_6); break; case AVMEDIA_TYPE_VIDEO:
frac_add(&VAR_1->pts, (int64_t)VAR_1->time_base.VAR_5 * VAR_1->codec->time_base.VAR_4); break; default:
break; return 0;