static void do_video_out(AVFormatContext *s, OutputStream *ost,

                         InputStream *ist, AVFrame *in_picture)

{

    int nb_frames, i, ret, format_video_sync;

    AVFrame *final_picture;

    AVCodecContext *enc;

    double sync_ipts;

    double duration = 0;

    int frame_size = 0;

    float quality = same_quant ? in_picture->quality

                               : ost->st->codec->global_quality;



    enc = ost->st->codec;



    if (ist->st->start_time != AV_NOPTS_VALUE && ist->st->first_dts != AV_NOPTS_VALUE) {

        duration = FFMAX(av_q2d(ist->st->time_base), av_q2d(ist->st->codec->time_base));

        if(ist->st->avg_frame_rate.num)

            duration= FFMAX(duration, 1/av_q2d(ist->st->avg_frame_rate));



        duration /= av_q2d(enc->time_base);

    }



    sync_ipts = get_sync_ipts(ost, in_picture->pts) / av_q2d(enc->time_base);



    /* by default, we output a single frame */

    nb_frames = 1;



    format_video_sync = video_sync_method;

    if (format_video_sync == VSYNC_AUTO)

        format_video_sync = (s->oformat->flags & AVFMT_VARIABLE_FPS) ? ((s->oformat->flags & AVFMT_NOTIMESTAMPS) ? VSYNC_PASSTHROUGH : VSYNC_VFR) : 1;



    if (format_video_sync != VSYNC_PASSTHROUGH && format_video_sync != VSYNC_DROP) {

        double vdelta = sync_ipts - ost->sync_opts + duration;

        // FIXME set to 0.5 after we fix some dts/pts bugs like in avidec.c

        if (vdelta < -1.1)

            nb_frames = 0;

        else if (format_video_sync == VSYNC_VFR) {

            if (vdelta <= -0.6) {

                nb_frames = 0;

            } else if (vdelta > 0.6)

                ost->sync_opts = lrintf(sync_ipts);

        } else if (vdelta > 1.1)

            nb_frames = lrintf(vdelta);

        if (nb_frames == 0) {

            ++nb_frames_drop;

            av_log(NULL, AV_LOG_VERBOSE, "*** drop!\n");

        } else if (nb_frames > 1) {

            nb_frames_dup += nb_frames - 1;

            av_log(NULL, AV_LOG_VERBOSE, "*** %d dup!\n", nb_frames - 1);

        }

    } else

        ost->sync_opts = lrintf(sync_ipts);



    nb_frames = FFMIN(nb_frames, ost->max_frames - ost->frame_number);

    if (nb_frames <= 0)

        return;



    do_video_resample(ost, ist, in_picture, &final_picture);



    /* duplicates frame if needed */

    for (i = 0; i < nb_frames; i++) {

        AVPacket pkt;

        av_init_packet(&pkt);

        pkt.data = NULL;

        pkt.size = 0;



        if (s->oformat->flags & AVFMT_RAWPICTURE &&

            enc->codec->id == CODEC_ID_RAWVIDEO) {

            /* raw pictures are written as AVPicture structure to

               avoid any copies. We support temporarily the older

               method. */

            enc->coded_frame->interlaced_frame = in_picture->interlaced_frame;

            enc->coded_frame->top_field_first  = in_picture->top_field_first;

            pkt.data   = (uint8_t *)final_picture;

            pkt.size   =  sizeof(AVPicture);

            pkt.pts    = av_rescale_q(ost->sync_opts, enc->time_base, ost->st->time_base);

            pkt.flags |= AV_PKT_FLAG_KEY;



            write_frame(s, &pkt, ost);

        } else {

            int got_packet;

            AVFrame big_picture;



            big_picture = *final_picture;

            /* better than nothing: use input picture interlaced

               settings */

            big_picture.interlaced_frame = in_picture->interlaced_frame;

            if (ost->st->codec->flags & (CODEC_FLAG_INTERLACED_DCT|CODEC_FLAG_INTERLACED_ME)) {

                if (ost->top_field_first == -1)

                    big_picture.top_field_first = in_picture->top_field_first;

                else

                    big_picture.top_field_first = !!ost->top_field_first;

            }



            /* handles same_quant here. This is not correct because it may

               not be a global option */

            big_picture.quality = quality;

            if (!enc->me_threshold)

                big_picture.pict_type = 0;

            big_picture.pts = ost->sync_opts;

            if (ost->forced_kf_index < ost->forced_kf_count &&

                big_picture.pts >= ost->forced_kf_pts[ost->forced_kf_index]) {

                big_picture.pict_type = AV_PICTURE_TYPE_I;

                ost->forced_kf_index++;

            }

            ret = avcodec_encode_video2(enc, &pkt, &big_picture, &got_packet);

            if (ret < 0) {

                av_log(NULL, AV_LOG_FATAL, "Video encoding failed\n");

                exit_program(1);

            }



            if (got_packet) {

                if (pkt.pts == AV_NOPTS_VALUE && !(enc->codec->capabilities & CODEC_CAP_DELAY))

                    pkt.pts = ost->sync_opts;



                if (pkt.pts != AV_NOPTS_VALUE)

                    pkt.pts = av_rescale_q(pkt.pts, enc->time_base, ost->st->time_base);

                if (pkt.dts != AV_NOPTS_VALUE)

                    pkt.dts = av_rescale_q(pkt.dts, enc->time_base, ost->st->time_base);



                if (debug_ts) {

                    av_log(NULL, AV_LOG_INFO, "encoder -> type:video "

                           "pkt_pts:%s pkt_pts_time:%s pkt_dts:%s pkt_dts_time:%s\n",

                           av_ts2str(pkt.pts), av_ts2timestr(pkt.pts, &ost->st->time_base),

                           av_ts2str(pkt.dts), av_ts2timestr(pkt.dts, &ost->st->time_base));

                }



                if (format_video_sync == VSYNC_DROP)

                    pkt.pts = pkt.dts = AV_NOPTS_VALUE;



                write_frame(s, &pkt, ost);

                frame_size = pkt.size;

                video_size += pkt.size;




                /* if two pass, output log */

                if (ost->logfile && enc->stats_out) {

                    fprintf(ost->logfile, "%s", enc->stats_out);

                }

            }

        }

        ost->sync_opts++;

        /*

         * For video, number of frames in == number of packets out.

         * But there may be reordering, so we can't throw away frames on encoder

         * flush, we need to limit them here, before they go into encoder.

         */

        ost->frame_number++;

    }

    if (vstats_filename && frame_size)

        do_video_stats(output_files[ost->file_index].ctx, ost, frame_size);

}
static void do_video_out(AVFormatContext *s, OutputStream *ost,

                         InputStream *ist, AVFrame *in_picture)

{

    int nb_frames, i, ret, format_video_sync;

    AVFrame *final_picture;

    AVCodecContext *enc;

    double sync_ipts;

    double duration = 0;

    int frame_size = 0;

    float quality = same_quant ? in_picture->quality

                               : ost->st->codec->global_quality;



    enc = ost->st->codec;



    if (ist->st->start_time != AV_NOPTS_VALUE && ist->st->first_dts != AV_NOPTS_VALUE) {

        duration = FFMAX(av_q2d(ist->st->time_base), av_q2d(ist->st->codec->time_base));

        if(ist->st->avg_frame_rate.num)

            duration= FFMAX(duration, 1/av_q2d(ist->st->avg_frame_rate));



        duration /= av_q2d(enc->time_base);

    }



    sync_ipts = get_sync_ipts(ost, in_picture->pts) / av_q2d(enc->time_base);



    

    nb_frames = 1;



    format_video_sync = video_sync_method;

    if (format_video_sync == VSYNC_AUTO)

        format_video_sync = (s->oformat->flags & AVFMT_VARIABLE_FPS) ? ((s->oformat->flags & AVFMT_NOTIMESTAMPS) ? VSYNC_PASSTHROUGH : VSYNC_VFR) : 1;



    if (format_video_sync != VSYNC_PASSTHROUGH && format_video_sync != VSYNC_DROP) {

        double vdelta = sync_ipts - ost->sync_opts + duration;

        

        if (vdelta < -1.1)

            nb_frames = 0;

        else if (format_video_sync == VSYNC_VFR) {

            if (vdelta <= -0.6) {

                nb_frames = 0;

            } else if (vdelta > 0.6)

                ost->sync_opts = lrintf(sync_ipts);

        } else if (vdelta > 1.1)

            nb_frames = lrintf(vdelta);

        if (nb_frames == 0) {

            ++nb_frames_drop;

            av_log(NULL, AV_LOG_VERBOSE, "*** drop!\n");

        } else if (nb_frames > 1) {

            nb_frames_dup += nb_frames - 1;

            av_log(NULL, AV_LOG_VERBOSE, "*** %d dup!\n", nb_frames - 1);

        }

    } else

        ost->sync_opts = lrintf(sync_ipts);



    nb_frames = FFMIN(nb_frames, ost->max_frames - ost->frame_number);

    if (nb_frames <= 0)

        return;



    do_video_resample(ost, ist, in_picture, &final_picture);



    

    for (i = 0; i < nb_frames; i++) {

        AVPacket pkt;

        av_init_packet(&pkt);

        pkt.data = NULL;

        pkt.size = 0;



        if (s->oformat->flags & AVFMT_RAWPICTURE &&

            enc->codec->id == CODEC_ID_RAWVIDEO) {

            





            enc->coded_frame->interlaced_frame = in_picture->interlaced_frame;

            enc->coded_frame->top_field_first  = in_picture->top_field_first;

            pkt.data   = (uint8_t *)final_picture;

            pkt.size   =  sizeof(AVPicture);

            pkt.pts    = av_rescale_q(ost->sync_opts, enc->time_base, ost->st->time_base);

            pkt.flags |= AV_PKT_FLAG_KEY;



            write_frame(s, &pkt, ost);

        } else {

            int got_packet;

            AVFrame big_picture;



            big_picture = *final_picture;

            



            big_picture.interlaced_frame = in_picture->interlaced_frame;

            if (ost->st->codec->flags & (CODEC_FLAG_INTERLACED_DCT|CODEC_FLAG_INTERLACED_ME)) {

                if (ost->top_field_first == -1)

                    big_picture.top_field_first = in_picture->top_field_first;

                else

                    big_picture.top_field_first = !!ost->top_field_first;

            }



            



            big_picture.quality = quality;

            if (!enc->me_threshold)

                big_picture.pict_type = 0;

            big_picture.pts = ost->sync_opts;

            if (ost->forced_kf_index < ost->forced_kf_count &&

                big_picture.pts >= ost->forced_kf_pts[ost->forced_kf_index]) {

                big_picture.pict_type = AV_PICTURE_TYPE_I;

                ost->forced_kf_index++;

            }

            ret = avcodec_encode_video2(enc, &pkt, &big_picture, &got_packet);

            if (ret < 0) {

                av_log(NULL, AV_LOG_FATAL, "Video encoding failed\n");

                exit_program(1);

            }



            if (got_packet) {

                if (pkt.pts == AV_NOPTS_VALUE && !(enc->codec->capabilities & CODEC_CAP_DELAY))

                    pkt.pts = ost->sync_opts;



                if (pkt.pts != AV_NOPTS_VALUE)

                    pkt.pts = av_rescale_q(pkt.pts, enc->time_base, ost->st->time_base);

                if (pkt.dts != AV_NOPTS_VALUE)

                    pkt.dts = av_rescale_q(pkt.dts, enc->time_base, ost->st->time_base);



                if (debug_ts) {

                    av_log(NULL, AV_LOG_INFO, "encoder -> type:video "

                           "pkt_pts:%s pkt_pts_time:%s pkt_dts:%s pkt_dts_time:%s\n",

                           av_ts2str(pkt.pts), av_ts2timestr(pkt.pts, &ost->st->time_base),

                           av_ts2str(pkt.dts), av_ts2timestr(pkt.dts, &ost->st->time_base));

                }



                if (format_video_sync == VSYNC_DROP)

                    pkt.pts = pkt.dts = AV_NOPTS_VALUE;



                write_frame(s, &pkt, ost);

                frame_size = pkt.size;

                video_size += pkt.size;




                

                if (ost->logfile && enc->stats_out) {

                    fprintf(ost->logfile, "%s", enc->stats_out);

                }

            }

        }

        ost->sync_opts++;

        









        ost->frame_number++;

    }

    if (vstats_filename && frame_size)

        do_video_stats(output_files[ost->file_index].ctx, ost, frame_size);

}
static void FUNC_0(AVFormatContext *VAR_0, OutputStream *VAR_1,

                         InputStream *VAR_2, AVFrame *VAR_3)

{

    int VAR_4, VAR_5, VAR_6, VAR_7;

    AVFrame *final_picture;

    AVCodecContext *enc;

    double VAR_8;

    double VAR_9 = 0;

    int VAR_10 = 0;

    float VAR_11 = same_quant ? VAR_3->VAR_11

                               : VAR_1->st->codec->global_quality;



    enc = VAR_1->st->codec;



    if (VAR_2->st->start_time != AV_NOPTS_VALUE && VAR_2->st->first_dts != AV_NOPTS_VALUE) {

        VAR_9 = FFMAX(av_q2d(VAR_2->st->time_base), av_q2d(VAR_2->st->codec->time_base));

        if(VAR_2->st->avg_frame_rate.num)

            VAR_9= FFMAX(VAR_9, 1/av_q2d(VAR_2->st->avg_frame_rate));



        VAR_9 /= av_q2d(enc->time_base);

    }



    VAR_8 = get_sync_ipts(VAR_1, VAR_3->pts) / av_q2d(enc->time_base);



    

    VAR_4 = 1;



    VAR_7 = video_sync_method;

    if (VAR_7 == VSYNC_AUTO)

        VAR_7 = (VAR_0->oformat->flags & AVFMT_VARIABLE_FPS) ? ((VAR_0->oformat->flags & AVFMT_NOTIMESTAMPS) ? VSYNC_PASSTHROUGH : VSYNC_VFR) : 1;



    if (VAR_7 != VSYNC_PASSTHROUGH && VAR_7 != VSYNC_DROP) {

        double VAR_12 = VAR_8 - VAR_1->sync_opts + VAR_9;

        

        if (VAR_12 < -1.1)

            VAR_4 = 0;

        else if (VAR_7 == VSYNC_VFR) {

            if (VAR_12 <= -0.6) {

                VAR_4 = 0;

            } else if (VAR_12 > 0.6)

                VAR_1->sync_opts = lrintf(VAR_8);

        } else if (VAR_12 > 1.1)

            VAR_4 = lrintf(VAR_12);

        if (VAR_4 == 0) {

            ++nb_frames_drop;

            av_log(NULL, AV_LOG_VERBOSE, "*** drop!\n");

        } else if (VAR_4 > 1) {

            nb_frames_dup += VAR_4 - 1;

            av_log(NULL, AV_LOG_VERBOSE, "*** %d dup!\n", VAR_4 - 1);

        }

    } else

        VAR_1->sync_opts = lrintf(VAR_8);



    VAR_4 = FFMIN(VAR_4, VAR_1->max_frames - VAR_1->frame_number);

    if (VAR_4 <= 0)

        return;



    do_video_resample(VAR_1, VAR_2, VAR_3, &final_picture);



    

    for (VAR_5 = 0; VAR_5 < VAR_4; VAR_5++) {

        AVPacket pkt;

        av_init_packet(&pkt);

        pkt.data = NULL;

        pkt.size = 0;



        if (VAR_0->oformat->flags & AVFMT_RAWPICTURE &&

            enc->codec->id == CODEC_ID_RAWVIDEO) {

            





            enc->coded_frame->interlaced_frame = VAR_3->interlaced_frame;

            enc->coded_frame->top_field_first  = VAR_3->top_field_first;

            pkt.data   = (uint8_t *)final_picture;

            pkt.size   =  sizeof(AVPicture);

            pkt.pts    = av_rescale_q(VAR_1->sync_opts, enc->time_base, VAR_1->st->time_base);

            pkt.flags |= AV_PKT_FLAG_KEY;



            write_frame(VAR_0, &pkt, VAR_1);

        } else {

            int VAR_13;

            AVFrame big_picture;



            big_picture = *final_picture;

            



            big_picture.interlaced_frame = VAR_3->interlaced_frame;

            if (VAR_1->st->codec->flags & (CODEC_FLAG_INTERLACED_DCT|CODEC_FLAG_INTERLACED_ME)) {

                if (VAR_1->top_field_first == -1)

                    big_picture.top_field_first = VAR_3->top_field_first;

                else

                    big_picture.top_field_first = !!VAR_1->top_field_first;

            }



            



            big_picture.VAR_11 = VAR_11;

            if (!enc->me_threshold)

                big_picture.pict_type = 0;

            big_picture.pts = VAR_1->sync_opts;

            if (VAR_1->forced_kf_index < VAR_1->forced_kf_count &&

                big_picture.pts >= VAR_1->forced_kf_pts[VAR_1->forced_kf_index]) {

                big_picture.pict_type = AV_PICTURE_TYPE_I;

                VAR_1->forced_kf_index++;

            }

            VAR_6 = avcodec_encode_video2(enc, &pkt, &big_picture, &VAR_13);

            if (VAR_6 < 0) {

                av_log(NULL, AV_LOG_FATAL, "Video encoding failed\n");

                exit_program(1);

            }



            if (VAR_13) {

                if (pkt.pts == AV_NOPTS_VALUE && !(enc->codec->capabilities & CODEC_CAP_DELAY))

                    pkt.pts = VAR_1->sync_opts;



                if (pkt.pts != AV_NOPTS_VALUE)

                    pkt.pts = av_rescale_q(pkt.pts, enc->time_base, VAR_1->st->time_base);

                if (pkt.dts != AV_NOPTS_VALUE)

                    pkt.dts = av_rescale_q(pkt.dts, enc->time_base, VAR_1->st->time_base);



                if (debug_ts) {

                    av_log(NULL, AV_LOG_INFO, "encoder -> type:video "

                           "pkt_pts:%VAR_0 pkt_pts_time:%VAR_0 pkt_dts:%VAR_0 pkt_dts_time:%VAR_0\n",

                           av_ts2str(pkt.pts), av_ts2timestr(pkt.pts, &VAR_1->st->time_base),

                           av_ts2str(pkt.dts), av_ts2timestr(pkt.dts, &VAR_1->st->time_base));

                }



                if (VAR_7 == VSYNC_DROP)

                    pkt.pts = pkt.dts = AV_NOPTS_VALUE;



                write_frame(VAR_0, &pkt, VAR_1);

                VAR_10 = pkt.size;

                video_size += pkt.size;




                

                if (VAR_1->logfile && enc->stats_out) {

                    fprintf(VAR_1->logfile, "%VAR_0", enc->stats_out);

                }

            }

        }

        VAR_1->sync_opts++;

        









        VAR_1->frame_number++;

    }

    if (vstats_filename && VAR_10)

        do_video_stats(output_files[VAR_1->file_index].ctx, VAR_1, VAR_10);

}
static void FUNC_0(AVFormatContext *VAR_0, OutputStream *VAR_1,
InputStream *VAR_2, AVFrame *VAR_3)
{ int VAR_4, VAR_5, VAR_6, VAR_7; AVFrame *final_picture; AVCodecContext *enc; double VAR_8; double VAR_9 = 0; int VAR_10 = 0; float VAR_11 = same_quant ? VAR_3->VAR_11
: VAR_1->st->codec->global_quality; enc = VAR_1->st->codec; if (VAR_2->st->start_time != AV_NOPTS_VALUE && VAR_2->st->first_dts != AV_NOPTS_VALUE) { VAR_9 = FFMAX(av_q2d(VAR_2->st->time_base), av_q2d(VAR_2->st->codec->time_base)); if(VAR_2->st->avg_frame_rate.num)
VAR_9= FFMAX(VAR_9, 1/av_q2d(VAR_2->st->avg_frame_rate)); VAR_9 /= av_q2d(enc->time_base); } VAR_8 = get_sync_ipts(VAR_1, VAR_3->pts) / av_q2d(enc->time_base); VAR_4 = 1; VAR_7 = video_sync_method; if (VAR_7 == VSYNC_AUTO)
VAR_7 = (VAR_0->oformat->flags & AVFMT_VARIABLE_FPS) ? ((VAR_0->oformat->flags & AVFMT_NOTIMESTAMPS) ? VSYNC_PASSTHROUGH : VSYNC_VFR) : 1; if (VAR_7 != VSYNC_PASSTHROUGH && VAR_7 != VSYNC_DROP) { double VAR_12 = VAR_8 - VAR_1->sync_opts + VAR_9; if (VAR_12 < -1.1)
VAR_4 = 0; else if (VAR_7 == VSYNC_VFR) { if (VAR_12 <= -0.6) { VAR_4 = 0; } else if (VAR_12 > 0.6) VAR_1->sync_opts = lrintf(VAR_8); } else if (VAR_12 > 1.1) VAR_4 = lrintf(VAR_12); if (VAR_4 == 0) { ++nb_frames_drop; av_log(NULL, AV_LOG_VERBOSE, "*** drop!\n"); } else if (VAR_4 > 1) { nb_frames_dup += VAR_4 - 1; av_log(NULL, AV_LOG_VERBOSE, "*** %d dup!\n", VAR_4 - 1); } } else VAR_1->sync_opts = lrintf(VAR_8); VAR_4 = FFMIN(VAR_4, VAR_1->max_frames - VAR_1->frame_number); if (VAR_4 <= 0)
return; do_video_resample(VAR_1, VAR_2, VAR_3, &final_picture); for (VAR_5 = 0; VAR_5 < VAR_4; VAR_5++) { AVPacket pkt; av_init_packet(&pkt); pkt.data = NULL; pkt.size = 0; if (VAR_0->oformat->flags & AVFMT_RAWPICTURE &&
enc->codec->id == CODEC_ID_RAWVIDEO) { enc->coded_frame->interlaced_frame = VAR_3->interlaced_frame; enc->coded_frame->top_field_first  = VAR_3->top_field_first; pkt.data   = (uint8_t *)final_picture; pkt.size   =  sizeof(AVPicture); pkt.pts    = av_rescale_q(VAR_1->sync_opts, enc->time_base, VAR_1->st->time_base); pkt.flags |= AV_PKT_FLAG_KEY; write_frame(VAR_0, &pkt, VAR_1); } else { int VAR_13; AVFrame big_picture; big_picture = *final_picture; big_picture.interlaced_frame = VAR_3->interlaced_frame; if (VAR_1->st->codec->flags & (CODEC_FLAG_INTERLACED_DCT|CODEC_FLAG_INTERLACED_ME)) { if (VAR_1->top_field_first == -1)
big_picture.top_field_first = VAR_3->top_field_first; else
big_picture.top_field_first = !!VAR_1->top_field_first; } big_picture.VAR_11 = VAR_11; if (!enc->me_threshold)
big_picture.pict_type = 0; big_picture.pts = VAR_1->sync_opts; if (VAR_1->forced_kf_index < VAR_1->forced_kf_count &&
big_picture.pts >= VAR_1->forced_kf_pts[VAR_1->forced_kf_index]) { big_picture.pict_type = AV_PICTURE_TYPE_I; VAR_1->forced_kf_index++; } VAR_6 = avcodec_encode_video2(enc, &pkt, &big_picture, &VAR_13); if (VAR_6 < 0) { av_log(NULL, AV_LOG_FATAL, "Video encoding failed\n"); exit_program(1); } if (VAR_13) { if (pkt.pts == AV_NOPTS_VALUE && !(enc->codec->capabilities & CODEC_CAP_DELAY))
pkt.pts = VAR_1->sync_opts; if (pkt.pts != AV_NOPTS_VALUE)
pkt.pts = av_rescale_q(pkt.pts, enc->time_base, VAR_1->st->time_base); if (pkt.dts != AV_NOPTS_VALUE)
pkt.dts = av_rescale_q(pkt.dts, enc->time_base, VAR_1->st->time_base); if (debug_ts) { av_log(NULL, AV_LOG_INFO, "encoder -> type:video "
"pkt_pts:%VAR_0 pkt_pts_time:%VAR_0 pkt_dts:%VAR_0 pkt_dts_time:%VAR_0\n",
av_ts2str(pkt.pts), av_ts2timestr(pkt.pts, &VAR_1->st->time_base),
av_ts2str(pkt.dts), av_ts2timestr(pkt.dts, &VAR_1->st->time_base)); } if (VAR_7 == VSYNC_DROP)
pkt.pts = pkt.dts = AV_NOPTS_VALUE; write_frame(VAR_0, &pkt, VAR_1); VAR_10 = pkt.size; video_size += pkt.size; if (VAR_1->logfile && enc->stats_out) { fprintf(VAR_1->logfile, "%VAR_0", enc->stats_out); } } } VAR_1->sync_opts++; VAR_1->frame_number++; } if (vstats_filename && VAR_10)
do_video_stats(output_files[VAR_1->file_index].ctx, VAR_1, VAR_10); }