static int output_packet(InputStream *ist, const AVPacket *pkt)

{

    int ret = 0, i;

    int got_output;



    AVPacket avpkt;

    if (!ist->saw_first_ts) {

        ist->dts = ist->st->avg_frame_rate.num ? - ist->st->codec->has_b_frames * AV_TIME_BASE / av_q2d(ist->st->avg_frame_rate) : 0;

        ist->pts = 0;

        if (pkt != NULL && pkt->pts != AV_NOPTS_VALUE && !ist->decoding_needed) {

            ist->dts += av_rescale_q(pkt->pts, ist->st->time_base, AV_TIME_BASE_Q);

            ist->pts = ist->dts; //unused but better to set it to a value thats not totally wrong

        }

        ist->saw_first_ts = 1;

    }



    if (ist->next_dts == AV_NOPTS_VALUE)

        ist->next_dts = ist->dts;

    if (ist->next_pts == AV_NOPTS_VALUE)

        ist->next_pts = ist->pts;



    if (pkt == NULL) {

        /* EOF handling */

        av_init_packet(&avpkt);

        avpkt.data = NULL;

        avpkt.size = 0;

        goto handle_eof;

    } else {

        avpkt = *pkt;

    }



    if (pkt->dts != AV_NOPTS_VALUE) {

        ist->next_dts = ist->dts = av_rescale_q(pkt->dts, ist->st->time_base, AV_TIME_BASE_Q);

        if (ist->st->codec->codec_type != AVMEDIA_TYPE_VIDEO || !ist->decoding_needed)

            ist->next_pts = ist->pts = ist->dts;

    }



    // while we have more to decode or while the decoder did output something on EOF

    while (ist->decoding_needed && (avpkt.size > 0 || (!pkt && got_output))) {

        int duration;

    handle_eof:



        ist->pts = ist->next_pts;

        ist->dts = ist->next_dts;



        if (avpkt.size && avpkt.size != pkt->size) {

            av_log(NULL, ist->showed_multi_packet_warning ? AV_LOG_VERBOSE : AV_LOG_WARNING,

                   "Multiple frames in a packet from stream %d\n", pkt->stream_index);

            ist->showed_multi_packet_warning = 1;

        }



        switch (ist->st->codec->codec_type) {

        case AVMEDIA_TYPE_AUDIO:

            ret = decode_audio    (ist, &avpkt, &got_output);

            break;

        case AVMEDIA_TYPE_VIDEO:

            ret = decode_video    (ist, &avpkt, &got_output);

            if (avpkt.duration) {

                duration = av_rescale_q(avpkt.duration, ist->st->time_base, AV_TIME_BASE_Q);

            } else if(ist->st->codec->time_base.num != 0 && ist->st->codec->time_base.den != 0) {

                int ticks= ist->st->parser ? ist->st->parser->repeat_pict+1 : ist->st->codec->ticks_per_frame;

                duration = ((int64_t)AV_TIME_BASE *

                                ist->st->codec->time_base.num * ticks) /

                                ist->st->codec->time_base.den;

            } else

                duration = 0;



            if(ist->dts != AV_NOPTS_VALUE && duration) {

                ist->next_dts += duration;

            }else

                ist->next_dts = AV_NOPTS_VALUE;



            if (got_output)

                ist->next_pts += duration; //FIXME the duration is not correct in some cases

            break;

        case AVMEDIA_TYPE_SUBTITLE:

            ret = transcode_subtitles(ist, &avpkt, &got_output);

            break;

        default:

            return -1;

        }



        if (ret < 0)

            return ret;



        avpkt.dts=

        avpkt.pts= AV_NOPTS_VALUE;



        // touch data and size only if not EOF

        if (pkt) {

            if(ist->st->codec->codec_type != AVMEDIA_TYPE_AUDIO)

                ret = avpkt.size;

            avpkt.data += ret;

            avpkt.size -= ret;

        }

        if (!got_output) {

            continue;

        }

    }



    /* handle stream copy */

    if (!ist->decoding_needed) {

        rate_emu_sleep(ist);

        ist->dts = ist->next_dts;

        switch (ist->st->codec->codec_type) {

        case AVMEDIA_TYPE_AUDIO:

            ist->next_dts += ((int64_t)AV_TIME_BASE * ist->st->codec->frame_size) /

                             ist->st->codec->sample_rate;

            break;

        case AVMEDIA_TYPE_VIDEO:

            if (pkt->duration) {

                ist->next_dts += av_rescale_q(pkt->duration, ist->st->time_base, AV_TIME_BASE_Q);

            } else if(ist->st->codec->time_base.num != 0) {

                int ticks= ist->st->parser ? ist->st->parser->repeat_pict + 1 : ist->st->codec->ticks_per_frame;

                ist->next_dts += ((int64_t)AV_TIME_BASE *

                                  ist->st->codec->time_base.num * ticks) /

                                  ist->st->codec->time_base.den;

            }

            break;

        }

        ist->pts = ist->dts;

        ist->next_pts = ist->next_dts;

    }

    for (i = 0; pkt && i < nb_output_streams; i++) {

        OutputStream *ost = output_streams[i];



        if (!check_output_constraints(ist, ost) || ost->encoding_needed)

            continue;



        do_streamcopy(ist, ost, pkt);

    }



    return 0;

}

static int output_packet(InputStream *ist, const AVPacket *pkt)

{

    int ret = 0, i;

    int got_output;



    AVPacket avpkt;

    if (!ist->saw_first_ts) {

        ist->dts = ist->st->avg_frame_rate.num ? - ist->st->codec->has_b_frames * AV_TIME_BASE / av_q2d(ist->st->avg_frame_rate) : 0;

        ist->pts = 0;

        if (pkt != NULL && pkt->pts != AV_NOPTS_VALUE && !ist->decoding_needed) {

            ist->dts += av_rescale_q(pkt->pts, ist->st->time_base, AV_TIME_BASE_Q);

            ist->pts = ist->dts; 

        }

        ist->saw_first_ts = 1;

    }



    if (ist->next_dts == AV_NOPTS_VALUE)

        ist->next_dts = ist->dts;

    if (ist->next_pts == AV_NOPTS_VALUE)

        ist->next_pts = ist->pts;



    if (pkt == NULL) {

        

        av_init_packet(&avpkt);

        avpkt.data = NULL;

        avpkt.size = 0;

        goto handle_eof;

    } else {

        avpkt = *pkt;

    }



    if (pkt->dts != AV_NOPTS_VALUE) {

        ist->next_dts = ist->dts = av_rescale_q(pkt->dts, ist->st->time_base, AV_TIME_BASE_Q);

        if (ist->st->codec->codec_type != AVMEDIA_TYPE_VIDEO || !ist->decoding_needed)

            ist->next_pts = ist->pts = ist->dts;

    }



    

    while (ist->decoding_needed && (avpkt.size > 0 || (!pkt && got_output))) {

        int duration;

    handle_eof:



        ist->pts = ist->next_pts;

        ist->dts = ist->next_dts;



        if (avpkt.size && avpkt.size != pkt->size) {

            av_log(NULL, ist->showed_multi_packet_warning ? AV_LOG_VERBOSE : AV_LOG_WARNING,

                   "Multiple frames in a packet from stream %d\n", pkt->stream_index);

            ist->showed_multi_packet_warning = 1;

        }



        switch (ist->st->codec->codec_type) {

        case AVMEDIA_TYPE_AUDIO:

            ret = decode_audio    (ist, &avpkt, &got_output);

            break;

        case AVMEDIA_TYPE_VIDEO:

            ret = decode_video    (ist, &avpkt, &got_output);

            if (avpkt.duration) {

                duration = av_rescale_q(avpkt.duration, ist->st->time_base, AV_TIME_BASE_Q);

            } else if(ist->st->codec->time_base.num != 0 && ist->st->codec->time_base.den != 0) {

                int ticks= ist->st->parser ? ist->st->parser->repeat_pict+1 : ist->st->codec->ticks_per_frame;

                duration = ((int64_t)AV_TIME_BASE *

                                ist->st->codec->time_base.num * ticks) /

                                ist->st->codec->time_base.den;

            } else

                duration = 0;



            if(ist->dts != AV_NOPTS_VALUE && duration) {

                ist->next_dts += duration;

            }else

                ist->next_dts = AV_NOPTS_VALUE;



            if (got_output)

                ist->next_pts += duration; 

            break;

        case AVMEDIA_TYPE_SUBTITLE:

            ret = transcode_subtitles(ist, &avpkt, &got_output);

            break;

        default:

            return -1;

        }



        if (ret < 0)

            return ret;



        avpkt.dts=

        avpkt.pts= AV_NOPTS_VALUE;



        

        if (pkt) {

            if(ist->st->codec->codec_type != AVMEDIA_TYPE_AUDIO)

                ret = avpkt.size;

            avpkt.data += ret;

            avpkt.size -= ret;

        }

        if (!got_output) {

            continue;

        }

    }



    

    if (!ist->decoding_needed) {

        rate_emu_sleep(ist);

        ist->dts = ist->next_dts;

        switch (ist->st->codec->codec_type) {

        case AVMEDIA_TYPE_AUDIO:

            ist->next_dts += ((int64_t)AV_TIME_BASE * ist->st->codec->frame_size) /

                             ist->st->codec->sample_rate;

            break;

        case AVMEDIA_TYPE_VIDEO:

            if (pkt->duration) {

                ist->next_dts += av_rescale_q(pkt->duration, ist->st->time_base, AV_TIME_BASE_Q);

            } else if(ist->st->codec->time_base.num != 0) {

                int ticks= ist->st->parser ? ist->st->parser->repeat_pict + 1 : ist->st->codec->ticks_per_frame;

                ist->next_dts += ((int64_t)AV_TIME_BASE *

                                  ist->st->codec->time_base.num * ticks) /

                                  ist->st->codec->time_base.den;

            }

            break;

        }

        ist->pts = ist->dts;

        ist->next_pts = ist->next_dts;

    }

    for (i = 0; pkt && i < nb_output_streams; i++) {

        OutputStream *ost = output_streams[i];



        if (!check_output_constraints(ist, ost) || ost->encoding_needed)

            continue;



        do_streamcopy(ist, ost, pkt);

    }



    return 0;

}

static int FUNC_0(InputStream *VAR_0, const AVPacket *VAR_1)

{

    int VAR_2 = 0, VAR_3;

    int VAR_4;



    AVPacket avpkt;

    if (!VAR_0->saw_first_ts) {

        VAR_0->dts = VAR_0->st->avg_frame_rate.num ? - VAR_0->st->codec->has_b_frames * AV_TIME_BASE / av_q2d(VAR_0->st->avg_frame_rate) : 0;

        VAR_0->pts = 0;

        if (VAR_1 != NULL && VAR_1->pts != AV_NOPTS_VALUE && !VAR_0->decoding_needed) {

            VAR_0->dts += av_rescale_q(VAR_1->pts, VAR_0->st->time_base, AV_TIME_BASE_Q);

            VAR_0->pts = VAR_0->dts; 

        }

        VAR_0->saw_first_ts = 1;

    }



    if (VAR_0->next_dts == AV_NOPTS_VALUE)

        VAR_0->next_dts = VAR_0->dts;

    if (VAR_0->next_pts == AV_NOPTS_VALUE)

        VAR_0->next_pts = VAR_0->pts;



    if (VAR_1 == NULL) {

        

        av_init_packet(&avpkt);

        avpkt.data = NULL;

        avpkt.size = 0;

        goto handle_eof;

    } else {

        avpkt = *VAR_1;

    }



    if (VAR_1->dts != AV_NOPTS_VALUE) {

        VAR_0->next_dts = VAR_0->dts = av_rescale_q(VAR_1->dts, VAR_0->st->time_base, AV_TIME_BASE_Q);

        if (VAR_0->st->codec->codec_type != AVMEDIA_TYPE_VIDEO || !VAR_0->decoding_needed)

            VAR_0->next_pts = VAR_0->pts = VAR_0->dts;

    }



    

    while (VAR_0->decoding_needed && (avpkt.size > 0 || (!VAR_1 && VAR_4))) {

        int VAR_5;

    handle_eof:



        VAR_0->pts = VAR_0->next_pts;

        VAR_0->dts = VAR_0->next_dts;



        if (avpkt.size && avpkt.size != VAR_1->size) {

            av_log(NULL, VAR_0->showed_multi_packet_warning ? AV_LOG_VERBOSE : AV_LOG_WARNING,

                   "Multiple frames in a packet from stream %d\n", VAR_1->stream_index);

            VAR_0->showed_multi_packet_warning = 1;

        }



        switch (VAR_0->st->codec->codec_type) {

        case AVMEDIA_TYPE_AUDIO:

            VAR_2 = decode_audio    (VAR_0, &avpkt, &VAR_4);

            break;

        case AVMEDIA_TYPE_VIDEO:

            VAR_2 = decode_video    (VAR_0, &avpkt, &VAR_4);

            if (avpkt.VAR_5) {

                VAR_5 = av_rescale_q(avpkt.VAR_5, VAR_0->st->time_base, AV_TIME_BASE_Q);

            } else if(VAR_0->st->codec->time_base.num != 0 && VAR_0->st->codec->time_base.den != 0) {

                int VAR_7= VAR_0->st->parser ? VAR_0->st->parser->repeat_pict+1 : VAR_0->st->codec->ticks_per_frame;

                VAR_5 = ((int64_t)AV_TIME_BASE *

                                VAR_0->st->codec->time_base.num * VAR_7) /

                                VAR_0->st->codec->time_base.den;

            } else

                VAR_5 = 0;



            if(VAR_0->dts != AV_NOPTS_VALUE && VAR_5) {

                VAR_0->next_dts += VAR_5;

            }else

                VAR_0->next_dts = AV_NOPTS_VALUE;



            if (VAR_4)

                VAR_0->next_pts += VAR_5; 

            break;

        case AVMEDIA_TYPE_SUBTITLE:

            VAR_2 = transcode_subtitles(VAR_0, &avpkt, &VAR_4);

            break;

        default:

            return -1;

        }



        if (VAR_2 < 0)

            return VAR_2;



        avpkt.dts=

        avpkt.pts= AV_NOPTS_VALUE;



        

        if (VAR_1) {

            if(VAR_0->st->codec->codec_type != AVMEDIA_TYPE_AUDIO)

                VAR_2 = avpkt.size;

            avpkt.data += VAR_2;

            avpkt.size -= VAR_2;

        }

        if (!VAR_4) {

            continue;

        }

    }



    

    if (!VAR_0->decoding_needed) {

        rate_emu_sleep(VAR_0);

        VAR_0->dts = VAR_0->next_dts;

        switch (VAR_0->st->codec->codec_type) {

        case AVMEDIA_TYPE_AUDIO:

            VAR_0->next_dts += ((int64_t)AV_TIME_BASE * VAR_0->st->codec->frame_size) /

                             VAR_0->st->codec->sample_rate;

            break;

        case AVMEDIA_TYPE_VIDEO:

            if (VAR_1->VAR_5) {

                VAR_0->next_dts += av_rescale_q(VAR_1->VAR_5, VAR_0->st->time_base, AV_TIME_BASE_Q);

            } else if(VAR_0->st->codec->time_base.num != 0) {

                int VAR_7= VAR_0->st->parser ? VAR_0->st->parser->repeat_pict + 1 : VAR_0->st->codec->ticks_per_frame;

                VAR_0->next_dts += ((int64_t)AV_TIME_BASE *

                                  VAR_0->st->codec->time_base.num * VAR_7) /

                                  VAR_0->st->codec->time_base.den;

            }

            break;

        }

        VAR_0->pts = VAR_0->dts;

        VAR_0->next_pts = VAR_0->next_dts;

    }

    for (VAR_3 = 0; VAR_1 && VAR_3 < nb_output_streams; VAR_3++) {

        OutputStream *ost = output_streams[VAR_3];



        if (!check_output_constraints(VAR_0, ost) || ost->encoding_needed)

            continue;



        do_streamcopy(VAR_0, ost, VAR_1);

    }



    return 0;

}

static int FUNC_0(InputStream *VAR_0, const AVPacket *VAR_1)
{ int VAR_2 = 0, VAR_3; int VAR_4; AVPacket avpkt; if (!VAR_0->saw_first_ts) { VAR_0->dts = VAR_0->st->avg_frame_rate.num ? - VAR_0->st->codec->has_b_frames * AV_TIME_BASE / av_q2d(VAR_0->st->avg_frame_rate) : 0; VAR_0->pts = 0; if (VAR_1 != NULL && VAR_1->pts != AV_NOPTS_VALUE && !VAR_0->decoding_needed) { VAR_0->dts += av_rescale_q(VAR_1->pts, VAR_0->st->time_base, AV_TIME_BASE_Q); VAR_0->pts = VAR_0->dts; } VAR_0->saw_first_ts = 1; } if (VAR_0->next_dts == AV_NOPTS_VALUE)
VAR_0->next_dts = VAR_0->dts; if (VAR_0->next_pts == AV_NOPTS_VALUE)
VAR_0->next_pts = VAR_0->pts; if (VAR_1 == NULL) { av_init_packet(&avpkt); avpkt.data = NULL; avpkt.size = 0; goto handle_eof; } else { avpkt = *VAR_1; } if (VAR_1->dts != AV_NOPTS_VALUE) { VAR_0->next_dts = VAR_0->dts = av_rescale_q(VAR_1->dts, VAR_0->st->time_base, AV_TIME_BASE_Q); if (VAR_0->st->codec->codec_type != AVMEDIA_TYPE_VIDEO || !VAR_0->decoding_needed)
VAR_0->next_pts = VAR_0->pts = VAR_0->dts; } while (VAR_0->decoding_needed && (avpkt.size > 0 || (!VAR_1 && VAR_4))) { int VAR_5; handle_eof:
VAR_0->pts = VAR_0->next_pts; VAR_0->dts = VAR_0->next_dts; if (avpkt.size && avpkt.size != VAR_1->size) { av_log(NULL, VAR_0->showed_multi_packet_warning ? AV_LOG_VERBOSE : AV_LOG_WARNING,
"Multiple frames in a packet from stream %d\n", VAR_1->stream_index); VAR_0->showed_multi_packet_warning = 1; } switch (VAR_0->st->codec->codec_type) { case AVMEDIA_TYPE_AUDIO:
VAR_2 = decode_audio    (VAR_0, &avpkt, &VAR_4); break; case AVMEDIA_TYPE_VIDEO:
VAR_2 = decode_video    (VAR_0, &avpkt, &VAR_4); if (avpkt.VAR_5) { VAR_5 = av_rescale_q(avpkt.VAR_5, VAR_0->st->time_base, AV_TIME_BASE_Q); } else if(VAR_0->st->codec->time_base.num != 0 && VAR_0->st->codec->time_base.den != 0) { int VAR_7= VAR_0->st->parser ? VAR_0->st->parser->repeat_pict+1 : VAR_0->st->codec->ticks_per_frame; VAR_5 = ((int64_t)AV_TIME_BASE *
VAR_0->st->codec->time_base.num * VAR_7) /
VAR_0->st->codec->time_base.den; } else VAR_5 = 0; if(VAR_0->dts != AV_NOPTS_VALUE && VAR_5) { VAR_0->next_dts += VAR_5; }else VAR_0->next_dts = AV_NOPTS_VALUE; if (VAR_4)
VAR_0->next_pts += VAR_5; break; case AVMEDIA_TYPE_SUBTITLE:
VAR_2 = transcode_subtitles(VAR_0, &avpkt, &VAR_4); break; default:
return -1; } if (VAR_2 < 0)
return VAR_2; avpkt.dts=
avpkt.pts= AV_NOPTS_VALUE; if (VAR_1) { if(VAR_0->st->codec->codec_type != AVMEDIA_TYPE_AUDIO)
VAR_2 = avpkt.size; avpkt.data += VAR_2; avpkt.size -= VAR_2; } if (!VAR_4) { continue; } } if (!VAR_0->decoding_needed) { rate_emu_sleep(VAR_0); VAR_0->dts = VAR_0->next_dts; switch (VAR_0->st->codec->codec_type) { case AVMEDIA_TYPE_AUDIO:
VAR_0->next_dts += ((int64_t)AV_TIME_BASE * VAR_0->st->codec->frame_size) /
VAR_0->st->codec->sample_rate; break; case AVMEDIA_TYPE_VIDEO:
if (VAR_1->VAR_5) { VAR_0->next_dts += av_rescale_q(VAR_1->VAR_5, VAR_0->st->time_base, AV_TIME_BASE_Q); } else if(VAR_0->st->codec->time_base.num != 0) { int VAR_7= VAR_0->st->parser ? VAR_0->st->parser->repeat_pict + 1 : VAR_0->st->codec->ticks_per_frame; VAR_0->next_dts += ((int64_t)AV_TIME_BASE *
VAR_0->st->codec->time_base.num * VAR_7) /
VAR_0->st->codec->time_base.den; } break; } VAR_0->pts = VAR_0->dts; VAR_0->next_pts = VAR_0->next_dts; } for (VAR_3 = 0; VAR_1 && VAR_3 < nb_output_streams; VAR_3++) { OutputStream *ost = output_streams[VAR_3]; if (!check_output_constraints(VAR_0, ost) || ost->encoding_needed)
continue; do_streamcopy(VAR_0, ost, VAR_1); } return 0; }