movtextenc: fix pointer messup and out of array accesses
[ffmpeg.git] / ffmpeg.c
index ea90ada..3eb15a6 100644 (file)
--- a/ffmpeg.c
+++ b/ffmpeg.c
 #include <errno.h>
 #include <limits.h>
 #if HAVE_ISATTY
 #include <errno.h>
 #include <limits.h>
 #if HAVE_ISATTY
+#if HAVE_IO_H
+#include <io.h>
+#endif
+#if HAVE_UNISTD_H
 #include <unistd.h>
 #endif
 #include <unistd.h>
 #endif
+#endif
 #include "libavformat/avformat.h"
 #include "libavdevice/avdevice.h"
 #include "libswscale/swscale.h"
 #include "libavformat/avformat.h"
 #include "libavdevice/avdevice.h"
 #include "libswscale/swscale.h"
@@ -193,16 +198,18 @@ static void sub2video_push_ref(InputStream *ist, int64_t pts)
         av_buffersrc_add_ref(ist->filters[i]->filter,
                              avfilter_ref_buffer(ref, ~0),
                              AV_BUFFERSRC_FLAG_NO_CHECK_FORMAT |
         av_buffersrc_add_ref(ist->filters[i]->filter,
                              avfilter_ref_buffer(ref, ~0),
                              AV_BUFFERSRC_FLAG_NO_CHECK_FORMAT |
-                             AV_BUFFERSRC_FLAG_NO_COPY);
+                             AV_BUFFERSRC_FLAG_NO_COPY |
+                             AV_BUFFERSRC_FLAG_PUSH);
 }
 
 }
 
-static void sub2video_update(InputStream *ist, AVSubtitle *sub, int64_t pts)
+static void sub2video_update(InputStream *ist, AVSubtitle *sub)
 {
     int w = ist->sub2video.w, h = ist->sub2video.h;
     AVFilterBufferRef *ref = ist->sub2video.ref;
     int8_t *dst;
     int     dst_linesize;
     int i;
 {
     int w = ist->sub2video.w, h = ist->sub2video.h;
     AVFilterBufferRef *ref = ist->sub2video.ref;
     int8_t *dst;
     int     dst_linesize;
     int i;
+    int64_t pts = av_rescale_q(sub->pts, AV_TIME_BASE_Q, ist->st->time_base);
 
     if (!ref)
         return;
 
     if (!ref)
         return;
@@ -410,10 +417,11 @@ void av_noreturn exit_program(int ret)
             bsfc = next;
         }
         output_streams[i]->bitstream_filters = NULL;
             bsfc = next;
         }
         output_streams[i]->bitstream_filters = NULL;
+        avcodec_free_frame(&output_streams[i]->filtered_frame);
 
         av_freep(&output_streams[i]->forced_keyframes);
         av_freep(&output_streams[i]->avfilter);
 
         av_freep(&output_streams[i]->forced_keyframes);
         av_freep(&output_streams[i]->avfilter);
-        av_freep(&output_streams[i]->filtered_frame);
+        av_freep(&output_streams[i]->logfile_prefix);
         av_freep(&output_streams[i]);
     }
     for (i = 0; i < nb_input_files; i++) {
         av_freep(&output_streams[i]);
     }
     for (i = 0; i < nb_input_files; i++) {
@@ -421,7 +429,7 @@ void av_noreturn exit_program(int ret)
         av_freep(&input_files[i]);
     }
     for (i = 0; i < nb_input_streams; i++) {
         av_freep(&input_files[i]);
     }
     for (i = 0; i < nb_input_streams; i++) {
-        av_freep(&input_streams[i]->decoded_frame);
+        avcodec_free_frame(&input_streams[i]->decoded_frame);
         av_dict_free(&input_streams[i]->opts);
         free_buffer_pool(&input_streams[i]->buffer_pool);
         avfilter_unref_bufferp(&input_streams[i]->sub2video.ref);
         av_dict_free(&input_streams[i]->opts);
         free_buffer_pool(&input_streams[i]->buffer_pool);
         avfilter_unref_bufferp(&input_streams[i]->sub2video.ref);
@@ -505,11 +513,14 @@ static void write_frame(AVFormatContext *s, AVPacket *pkt, OutputStream *ost)
         (avctx->codec_type == AVMEDIA_TYPE_AUDIO && audio_sync_method < 0))
         pkt->pts = pkt->dts = AV_NOPTS_VALUE;
 
         (avctx->codec_type == AVMEDIA_TYPE_AUDIO && audio_sync_method < 0))
         pkt->pts = pkt->dts = AV_NOPTS_VALUE;
 
-    if (avctx->codec_type == AVMEDIA_TYPE_AUDIO && pkt->dts != AV_NOPTS_VALUE) {
+    if ((avctx->codec_type == AVMEDIA_TYPE_AUDIO || avctx->codec_type == AVMEDIA_TYPE_VIDEO) && pkt->dts != AV_NOPTS_VALUE) {
         int64_t max = ost->st->cur_dts + !(s->oformat->flags & AVFMT_TS_NONSTRICT);
         if (ost->st->cur_dts && ost->st->cur_dts != AV_NOPTS_VALUE &&  max > pkt->dts) {
         int64_t max = ost->st->cur_dts + !(s->oformat->flags & AVFMT_TS_NONSTRICT);
         if (ost->st->cur_dts && ost->st->cur_dts != AV_NOPTS_VALUE &&  max > pkt->dts) {
-            av_log(s, max - pkt->dts > 2 ? AV_LOG_WARNING : AV_LOG_DEBUG, "Audio timestamp %"PRId64" < %"PRId64" invalid, cliping\n", pkt->dts, max);
-            pkt->pts = pkt->dts = max;
+            av_log(s, max - pkt->dts > 2 || avctx->codec_type == AVMEDIA_TYPE_VIDEO ? AV_LOG_WARNING : AV_LOG_DEBUG,
+                   "st:%d PTS: %"PRId64" DTS: %"PRId64" < %"PRId64" invalid, clipping\n", pkt->stream_index, pkt->pts, pkt->dts, max);
+            if(pkt->pts >= pkt->dts)
+                pkt->pts = FFMAX(pkt->pts, max);
+            pkt->dts = max;
         }
     }
 
         }
     }
 
@@ -534,6 +545,16 @@ static void write_frame(AVFormatContext *s, AVPacket *pkt, OutputStream *ost)
                                            &new_pkt.data, &new_pkt.size,
                                            pkt->data, pkt->size,
                                            pkt->flags & AV_PKT_FLAG_KEY);
                                            &new_pkt.data, &new_pkt.size,
                                            pkt->data, pkt->size,
                                            pkt->flags & AV_PKT_FLAG_KEY);
+        if(a == 0 && new_pkt.data != pkt->data && new_pkt.destruct) {
+            uint8_t *t = av_malloc(new_pkt.size + FF_INPUT_BUFFER_PADDING_SIZE); //the new should be a subset of the old so cannot overflow
+            if(t) {
+                memcpy(t, new_pkt.data, new_pkt.size);
+                memset(t + new_pkt.size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
+                new_pkt.data = t;
+                a = 1;
+            } else
+                a = AVERROR(ENOMEM);
+        }
         if (a > 0) {
             av_free_packet(pkt);
             new_pkt.destruct = av_destruct_packet;
         if (a > 0) {
             av_free_packet(pkt);
             new_pkt.destruct = av_destruct_packet;
@@ -551,6 +572,16 @@ static void write_frame(AVFormatContext *s, AVPacket *pkt, OutputStream *ost)
     }
 
     pkt->stream_index = ost->index;
     }
 
     pkt->stream_index = ost->index;
+
+    if (debug_ts) {
+        av_log(NULL, AV_LOG_INFO, "muxer <- type:%s "
+                "pkt_pts:%s pkt_pts_time:%s pkt_dts:%s pkt_dts_time:%s\n",
+                av_get_media_type_string(ost->st->codec->codec_type),
+                av_ts2str(pkt->pts), av_ts2timestr(pkt->pts, &ost->st->time_base),
+                av_ts2str(pkt->dts), av_ts2timestr(pkt->dts, &ost->st->time_base)
+              );
+    }
+
     ret = av_interleaved_write_frame(s, pkt);
     if (ret < 0) {
         print_error("av_interleaved_write_frame()", ret);
     ret = av_interleaved_write_frame(s, pkt);
     if (ret < 0) {
         print_error("av_interleaved_write_frame()", ret);
@@ -558,6 +589,18 @@ static void write_frame(AVFormatContext *s, AVPacket *pkt, OutputStream *ost)
     }
 }
 
     }
 }
 
+static void close_output_stream(OutputStream *ost)
+{
+    OutputFile *of = output_files[ost->file_index];
+
+    ost->finished = 1;
+    if (of->shortest) {
+        int i;
+        for (i = 0; i < of->ctx->nb_streams; i++)
+            output_streams[of->ost_index + i]->finished = 1;
+    }
+}
+
 static int check_recording_time(OutputStream *ost)
 {
     OutputFile *of = output_files[ost->file_index];
 static int check_recording_time(OutputStream *ost)
 {
     OutputFile *of = output_files[ost->file_index];
@@ -565,7 +608,7 @@ static int check_recording_time(OutputStream *ost)
     if (of->recording_time != INT64_MAX &&
         av_compare_ts(ost->sync_opts - ost->first_pts, ost->st->codec->time_base, of->recording_time,
                       AV_TIME_BASE_Q) >= 0) {
     if (of->recording_time != INT64_MAX &&
         av_compare_ts(ost->sync_opts - ost->first_pts, ost->st->codec->time_base, of->recording_time,
                       AV_TIME_BASE_Q) >= 0) {
-        ost->is_past_recording_time = 1;
+        close_output_stream(ost);
         return 0;
     }
     return 1;
         return 0;
     }
     return 1;
@@ -612,9 +655,9 @@ static void do_audio_out(AVFormatContext *s, OutputStream *ost,
                    av_ts2str(pkt.dts), av_ts2timestr(pkt.dts, &ost->st->time_base));
         }
 
                    av_ts2str(pkt.dts), av_ts2timestr(pkt.dts, &ost->st->time_base));
         }
 
+        audio_size += pkt.size;
         write_frame(s, &pkt, ost);
 
         write_frame(s, &pkt, ost);
 
-        audio_size += pkt.size;
         av_free_packet(&pkt);
     }
 }
         av_free_packet(&pkt);
     }
 }
@@ -661,15 +704,15 @@ static void pre_process_video_frame(InputStream *ist, AVPicture *picture, void *
 static void do_subtitle_out(AVFormatContext *s,
                             OutputStream *ost,
                             InputStream *ist,
 static void do_subtitle_out(AVFormatContext *s,
                             OutputStream *ost,
                             InputStream *ist,
-                            AVSubtitle *sub,
-                            int64_t pts)
+                            AVSubtitle *sub)
 {
     int subtitle_out_max_size = 1024 * 1024;
     int subtitle_out_size, nb, i;
     AVCodecContext *enc;
     AVPacket pkt;
 {
     int subtitle_out_max_size = 1024 * 1024;
     int subtitle_out_size, nb, i;
     AVCodecContext *enc;
     AVPacket pkt;
+    int64_t pts;
 
 
-    if (pts == AV_NOPTS_VALUE) {
+    if (sub->pts == AV_NOPTS_VALUE) {
         av_log(NULL, AV_LOG_ERROR, "Subtitle packets must have a pts\n");
         if (exit_on_error)
             exit_program(1);
         av_log(NULL, AV_LOG_ERROR, "Subtitle packets must have a pts\n");
         if (exit_on_error)
             exit_program(1);
@@ -691,8 +734,7 @@ static void do_subtitle_out(AVFormatContext *s,
         nb = 1;
 
     /* shift timestamp to honor -ss and make check_recording_time() work with -t */
         nb = 1;
 
     /* shift timestamp to honor -ss and make check_recording_time() work with -t */
-    pts = av_rescale_q(pts, ist->st->time_base, AV_TIME_BASE_Q)
-        - output_files[ost->file_index]->start_time;
+    pts = sub->pts - output_files[ost->file_index]->start_time;
     for (i = 0; i < nb; i++) {
         ost->sync_opts = av_rescale_q(pts, AV_TIME_BASE_Q, enc->time_base);
         if (!check_recording_time(ost))
     for (i = 0; i < nb; i++) {
         ost->sync_opts = av_rescale_q(pts, AV_TIME_BASE_Q, enc->time_base);
         if (!check_recording_time(ost))
@@ -703,6 +745,8 @@ static void do_subtitle_out(AVFormatContext *s,
         sub->pts               += av_rescale_q(sub->start_display_time, (AVRational){ 1, 1000 }, AV_TIME_BASE_Q);
         sub->end_display_time  -= sub->start_display_time;
         sub->start_display_time = 0;
         sub->pts               += av_rescale_q(sub->start_display_time, (AVRational){ 1, 1000 }, AV_TIME_BASE_Q);
         sub->end_display_time  -= sub->start_display_time;
         sub->start_display_time = 0;
+        if (i == 1)
+            sub->num_rects = 0;
         subtitle_out_size = avcodec_encode_subtitle(enc, subtitle_out,
                                                     subtitle_out_max_size, sub);
         if (subtitle_out_size < 0) {
         subtitle_out_size = avcodec_encode_subtitle(enc, subtitle_out,
                                                     subtitle_out_max_size, sub);
         if (subtitle_out_size < 0) {
@@ -723,8 +767,8 @@ static void do_subtitle_out(AVFormatContext *s,
             else
                 pkt.pts += 90 * sub->end_display_time;
         }
             else
                 pkt.pts += 90 * sub->end_display_time;
         }
-        write_frame(s, &pkt, ost);
         subtitle_size += pkt.size;
         subtitle_size += pkt.size;
+        write_frame(s, &pkt, ost);
     }
 }
 
     }
 }
 
@@ -787,7 +831,7 @@ static void do_video_out(AVFormatContext *s,
         return;
     } else if (nb_frames > 1) {
         if (nb_frames > dts_error_threshold * 30) {
         return;
     } else if (nb_frames > 1) {
         if (nb_frames > dts_error_threshold * 30) {
-            av_log(NULL, AV_LOG_ERROR, "%d frame duplication too large, skiping\n", nb_frames - 1);
+            av_log(NULL, AV_LOG_ERROR, "%d frame duplication too large, skipping\n", nb_frames - 1);
             nb_frames_drop++;
             return;
         }
             nb_frames_drop++;
             return;
         }
@@ -818,8 +862,8 @@ static void do_video_out(AVFormatContext *s,
         pkt.pts    = av_rescale_q(in_picture->pts, enc->time_base, ost->st->time_base);
         pkt.flags |= AV_PKT_FLAG_KEY;
 
         pkt.pts    = av_rescale_q(in_picture->pts, enc->time_base, ost->st->time_base);
         pkt.flags |= AV_PKT_FLAG_KEY;
 
-        write_frame(s, &pkt, ost);
         video_size += pkt.size;
         video_size += pkt.size;
+        write_frame(s, &pkt, ost);
     } else {
         int got_packet;
         AVFrame big_picture;
     } else {
         int got_packet;
         AVFrame big_picture;
@@ -869,9 +913,9 @@ static void do_video_out(AVFormatContext *s,
                     av_ts2str(pkt.dts), av_ts2timestr(pkt.dts, &ost->st->time_base));
             }
 
                     av_ts2str(pkt.dts), av_ts2timestr(pkt.dts, &ost->st->time_base));
             }
 
-            write_frame(s, &pkt, ost);
             frame_size = pkt.size;
             video_size += pkt.size;
             frame_size = pkt.size;
             video_size += pkt.size;
+            write_frame(s, &pkt, ost);
             av_free_packet(&pkt);
 
             /* if two pass, output log */
             av_free_packet(&pkt);
 
             /* if two pass, output log */
@@ -935,106 +979,90 @@ static void do_video_stats(AVFormatContext *os, OutputStream *ost,
     }
 }
 
     }
 }
 
-/* check for new output on any of the filtergraphs */
-static int poll_filters(void)
+/**
+ * Get and encode new output from any of the filtergraphs, without causing
+ * activity.
+ *
+ * @return  0 for success, <0 for severe errors
+ */
+static int reap_filters(void)
 {
     AVFilterBufferRef *picref;
     AVFrame *filtered_frame = NULL;
 {
     AVFilterBufferRef *picref;
     AVFrame *filtered_frame = NULL;
-    int i, ret, ret_all;
-    unsigned nb_success = 1, av_uninit(nb_eof);
+    int i;
     int64_t frame_pts;
 
     int64_t frame_pts;
 
-    while (1) {
-        /* Reap all buffers present in the buffer sinks */
-        for (i = 0; i < nb_output_streams; i++) {
-            OutputStream *ost = output_streams[i];
-            OutputFile    *of = output_files[ost->file_index];
-            int ret = 0;
-
-            if (!ost->filter)
-                continue;
+    /* Reap all buffers present in the buffer sinks */
+    for (i = 0; i < nb_output_streams; i++) {
+        OutputStream *ost = output_streams[i];
+        OutputFile    *of = output_files[ost->file_index];
+        int ret = 0;
 
 
-            if (!ost->filtered_frame && !(ost->filtered_frame = avcodec_alloc_frame())) {
-                return AVERROR(ENOMEM);
-            } else
-                avcodec_get_frame_defaults(ost->filtered_frame);
-            filtered_frame = ost->filtered_frame;
+        if (!ost->filter)
+            continue;
 
 
-            while (1) {
-                ret = av_buffersink_get_buffer_ref(ost->filter->filter, &picref,
-                                                   AV_BUFFERSINK_FLAG_NO_REQUEST);
-                if (ret < 0) {
-                    if (ret != AVERROR(EAGAIN) && ret != AVERROR_EOF) {
-                        char buf[256];
-                        av_strerror(ret, buf, sizeof(buf));
-                        av_log(NULL, AV_LOG_WARNING,
-                               "Error in av_buffersink_get_buffer_ref(): %s\n", buf);
-                    }
-                    break;
+        if (!ost->filtered_frame && !(ost->filtered_frame = avcodec_alloc_frame())) {
+            return AVERROR(ENOMEM);
+        } else
+            avcodec_get_frame_defaults(ost->filtered_frame);
+        filtered_frame = ost->filtered_frame;
+
+        while (1) {
+            ret = av_buffersink_get_buffer_ref(ost->filter->filter, &picref,
+                                               AV_BUFFERSINK_FLAG_NO_REQUEST);
+            if (ret < 0) {
+                if (ret != AVERROR(EAGAIN) && ret != AVERROR_EOF) {
+                    char buf[256];
+                    av_strerror(ret, buf, sizeof(buf));
+                    av_log(NULL, AV_LOG_WARNING,
+                           "Error in av_buffersink_get_buffer_ref(): %s\n", buf);
                 }
                 }
-                frame_pts = AV_NOPTS_VALUE;
-                if (picref->pts != AV_NOPTS_VALUE) {
-                    filtered_frame->pts = frame_pts = av_rescale_q(picref->pts,
-                                                    ost->filter->filter->inputs[0]->time_base,
-                                                    ost->st->codec->time_base) -
-                                        av_rescale_q(of->start_time,
-                                                    AV_TIME_BASE_Q,
-                                                    ost->st->codec->time_base);
-
-                    if (of->start_time && filtered_frame->pts < 0) {
-                        avfilter_unref_buffer(picref);
-                        continue;
-                    }
+                break;
+            }
+            frame_pts = AV_NOPTS_VALUE;
+            if (picref->pts != AV_NOPTS_VALUE) {
+                filtered_frame->pts = frame_pts = av_rescale_q(picref->pts,
+                                                ost->filter->filter->inputs[0]->time_base,
+                                                ost->st->codec->time_base) -
+                                    av_rescale_q(of->start_time,
+                                                AV_TIME_BASE_Q,
+                                                ost->st->codec->time_base);
+
+                if (of->start_time && filtered_frame->pts < 0) {
+                    avfilter_unref_buffer(picref);
+                    continue;
                 }
                 }
-                //if (ost->source_index >= 0)
-                //    *filtered_frame= *input_streams[ost->source_index]->decoded_frame; //for me_threshold
-
-
-                switch (ost->filter->filter->inputs[0]->type) {
-                case AVMEDIA_TYPE_VIDEO:
-                    avfilter_copy_buf_props(filtered_frame, picref);
-                    filtered_frame->pts = frame_pts;
-                    if (!ost->frame_aspect_ratio)
-                        ost->st->codec->sample_aspect_ratio = picref->video->sample_aspect_ratio;
+            }
+            //if (ost->source_index >= 0)
+            //    *filtered_frame= *input_streams[ost->source_index]->decoded_frame; //for me_threshold
 
 
-                    do_video_out(of->ctx, ost, filtered_frame,
-                                 same_quant ? ost->last_quality :
-                                              ost->st->codec->global_quality);
-                    break;
-                case AVMEDIA_TYPE_AUDIO:
-                    avfilter_copy_buf_props(filtered_frame, picref);
-                    filtered_frame->pts = frame_pts;
-                    do_audio_out(of->ctx, ost, filtered_frame);
-                    break;
-                default:
-                    // TODO support subtitle filters
-                    av_assert0(0);
-                }
 
 
-                avfilter_unref_buffer(picref);
-            }
-        }
-        if (!nb_success) /* from last round */
-            break;
-        /* Request frames through all the graphs */
-        ret_all = nb_success = nb_eof = 0;
-        for (i = 0; i < nb_filtergraphs; i++) {
-            ret = avfilter_graph_request_oldest(filtergraphs[i]->graph);
-            if (!ret) {
-                nb_success++;
-            } else if (ret == AVERROR_EOF) {
-                nb_eof++;
-            } else if (ret != AVERROR(EAGAIN)) {
-                char buf[256];
-                av_strerror(ret, buf, sizeof(buf));
-                av_log(NULL, AV_LOG_WARNING,
-                       "Error in request_frame(): %s\n", buf);
-                ret_all = ret;
+            switch (ost->filter->filter->inputs[0]->type) {
+            case AVMEDIA_TYPE_VIDEO:
+                avfilter_copy_buf_props(filtered_frame, picref);
+                filtered_frame->pts = frame_pts;
+                if (!ost->frame_aspect_ratio)
+                    ost->st->codec->sample_aspect_ratio = picref->video->sample_aspect_ratio;
+
+                do_video_out(of->ctx, ost, filtered_frame,
+                             same_quant ? ost->last_quality :
+                                          ost->st->codec->global_quality);
+                break;
+            case AVMEDIA_TYPE_AUDIO:
+                avfilter_copy_buf_props(filtered_frame, picref);
+                filtered_frame->pts = frame_pts;
+                do_audio_out(of->ctx, ost, filtered_frame);
+                break;
+            default:
+                // TODO support subtitle filters
+                av_assert0(0);
             }
             }
+
+            avfilter_unref_buffer(picref);
         }
         }
-        /* Try again if anything succeeded */
     }
     }
-    return nb_eof == nb_filtergraphs ? AVERROR_EOF : ret_all;
+
+    return 0;
 }
 
 static void print_report(int is_last_report, int64_t timer_start, int64_t cur_time)
 }
 
 static void print_report(int is_last_report, int64_t timer_start, int64_t cur_time)
@@ -1047,7 +1075,7 @@ static void print_report(int is_last_report, int64_t timer_start, int64_t cur_ti
     AVCodecContext *enc;
     int frame_number, vid, i;
     double bitrate;
     AVCodecContext *enc;
     int frame_number, vid, i;
     double bitrate;
-    int64_t pts = INT64_MAX;
+    int64_t pts = INT64_MIN;
     static int64_t last_time = -1;
     static int qp_histogram[52];
     int hours, mins, secs, us;
     static int64_t last_time = -1;
     static int qp_histogram[52];
     int hours, mins, secs, us;
@@ -1108,7 +1136,7 @@ static void print_report(int is_last_report, int64_t timer_start, int64_t cur_ti
                 if (qp >= 0 && qp < FF_ARRAY_ELEMS(qp_histogram))
                     qp_histogram[qp]++;
                 for (j = 0; j < 32; j++)
                 if (qp >= 0 && qp < FF_ARRAY_ELEMS(qp_histogram))
                     qp_histogram[qp]++;
                 for (j = 0; j < 32; j++)
-                    snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), "%X", (int)lrintf(log(qp_histogram[j] + 1) / log(2)));
+                    snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), "%X", (int)lrintf(log2(qp_histogram[j] + 1)));
             }
             if (enc->flags&CODEC_FLAG_PSNR) {
                 int j;
             }
             if (enc->flags&CODEC_FLAG_PSNR) {
                 int j;
@@ -1142,8 +1170,9 @@ static void print_report(int is_last_report, int64_t timer_start, int64_t cur_ti
             vid = 1;
         }
         /* compute min output value */
             vid = 1;
         }
         /* compute min output value */
-        pts = FFMIN(pts, av_rescale_q(ost->st->pts.val,
-                                      ost->st->time_base, AV_TIME_BASE_Q));
+        if ((is_last_report || !ost->finished) && ost->st->pts.val != AV_NOPTS_VALUE)
+            pts = FFMAX(pts, av_rescale_q(ost->st->pts.val,
+                                          ost->st->time_base, AV_TIME_BASE_Q));
     }
 
     secs = pts / AV_TIME_BASE;
     }
 
     secs = pts / AV_TIME_BASE;
@@ -1311,9 +1340,13 @@ static void do_streamcopy(InputStream *ist, OutputStream *ost, const AVPacket *p
         !ost->copy_initial_nonkeyframes)
         return;
 
         !ost->copy_initial_nonkeyframes)
         return;
 
+    if (!ost->frame_number && ist->pts < of->start_time &&
+        !ost->copy_prior_start)
+        return;
+
     if (of->recording_time != INT64_MAX &&
         ist->pts >= of->recording_time + of->start_time) {
     if (of->recording_time != INT64_MAX &&
         ist->pts >= of->recording_time + of->start_time) {
-        ost->is_past_recording_time = 1;
+        close_output_stream(ost);
         return;
     }
 
         return;
     }
 
@@ -1411,20 +1444,17 @@ static int decode_audio(InputStream *ist, AVPacket *pkt, int *got_output)
     update_benchmark(NULL);
     ret = avcodec_decode_audio4(avctx, decoded_frame, got_output, pkt);
     update_benchmark("decode_audio %d.%d", ist->file_index, ist->st->index);
     update_benchmark(NULL);
     ret = avcodec_decode_audio4(avctx, decoded_frame, got_output, pkt);
     update_benchmark("decode_audio %d.%d", ist->file_index, ist->st->index);
-    if (ret < 0) {
-        return ret;
-    }
-    if (avctx->sample_rate <= 0) {
+
+    if (ret >= 0 && avctx->sample_rate <= 0) {
         av_log(avctx, AV_LOG_ERROR, "Sample rate %d invalid\n", avctx->sample_rate);
         av_log(avctx, AV_LOG_ERROR, "Sample rate %d invalid\n", avctx->sample_rate);
-        return AVERROR_INVALIDDATA;
+        ret = AVERROR_INVALIDDATA;
     }
 
     }
 
-    if (!*got_output) {
-        /* no audio frame */
-        if (!pkt->size)
+    if (!*got_output || ret < 0) {
+        if (!pkt->size) {
             for (i = 0; i < ist->nb_filters; i++)
             for (i = 0; i < ist->nb_filters; i++)
-                av_buffersrc_add_ref(ist->filters[i]->filter, NULL,
-                                     AV_BUFFERSRC_FLAG_NO_COPY);
+                av_buffersrc_add_ref(ist->filters[i]->filter, NULL, 0);
+        }
         return ret;
     }
 
         return ret;
     }
 
@@ -1512,7 +1542,8 @@ static int decode_audio(InputStream *ist, AVPacket *pkt, int *got_output)
                                           decoded_frame_tb,
                                           (AVRational){1, ist->st->codec->sample_rate});
     for (i = 0; i < ist->nb_filters; i++)
                                           decoded_frame_tb,
                                           (AVRational){1, ist->st->codec->sample_rate});
     for (i = 0; i < ist->nb_filters; i++)
-        av_buffersrc_add_frame(ist->filters[i]->filter, decoded_frame, 0);
+        av_buffersrc_add_frame(ist->filters[i]->filter, decoded_frame,
+                               AV_BUFFERSRC_FLAG_PUSH);
 
     decoded_frame->pts = AV_NOPTS_VALUE;
 
 
     decoded_frame->pts = AV_NOPTS_VALUE;
 
@@ -1539,18 +1570,16 @@ static int decode_video(InputStream *ist, AVPacket *pkt, int *got_output)
     ret = avcodec_decode_video2(ist->st->codec,
                                 decoded_frame, got_output, pkt);
     update_benchmark("decode_video %d.%d", ist->file_index, ist->st->index);
     ret = avcodec_decode_video2(ist->st->codec,
                                 decoded_frame, got_output, pkt);
     update_benchmark("decode_video %d.%d", ist->file_index, ist->st->index);
-    if (ret < 0)
-        return ret;
-
-    quality = same_quant ? decoded_frame->quality : 0;
-    if (!*got_output) {
-        /* no picture yet */
-        if (!pkt->size)
+    if (!*got_output || ret < 0) {
+        if (!pkt->size) {
             for (i = 0; i < ist->nb_filters; i++)
             for (i = 0; i < ist->nb_filters; i++)
-                av_buffersrc_add_ref(ist->filters[i]->filter, NULL, AV_BUFFERSRC_FLAG_NO_COPY);
+                av_buffersrc_add_ref(ist->filters[i]->filter, NULL, 0);
+        }
         return ret;
     }
 
         return ret;
     }
 
+    quality = same_quant ? decoded_frame->quality : 0;
+
     if(ist->top_field_first>=0)
         decoded_frame->top_field_first = ist->top_field_first;
 
     if(ist->top_field_first>=0)
         decoded_frame->top_field_first = ist->top_field_first;
 
@@ -1625,9 +1654,10 @@ static int decode_video(InputStream *ist, AVPacket *pkt, int *got_output)
             buf->refcount++;
             av_buffersrc_add_ref(ist->filters[i]->filter, fb,
                                  AV_BUFFERSRC_FLAG_NO_CHECK_FORMAT |
             buf->refcount++;
             av_buffersrc_add_ref(ist->filters[i]->filter, fb,
                                  AV_BUFFERSRC_FLAG_NO_CHECK_FORMAT |
-                                 AV_BUFFERSRC_FLAG_NO_COPY);
+                                 AV_BUFFERSRC_FLAG_NO_COPY |
+                                 AV_BUFFERSRC_FLAG_PUSH);
         } else
         } else
-        if(av_buffersrc_add_frame(ist->filters[i]->filter, decoded_frame, 0)<0) {
+        if(av_buffersrc_add_frame(ist->filters[i]->filter, decoded_frame, AV_BUFFERSRC_FLAG_PUSH)<0) {
             av_log(NULL, AV_LOG_FATAL, "Failed to inject frame into filter network\n");
             exit_program(1);
         }
             av_log(NULL, AV_LOG_FATAL, "Failed to inject frame into filter network\n");
             exit_program(1);
         }
@@ -1649,9 +1679,28 @@ static int transcode_subtitles(InputStream *ist, AVPacket *pkt, int *got_output)
         return ret;
     }
 
         return ret;
     }
 
-    rate_emu_sleep(ist);
+    if (ist->fix_sub_duration) {
+        if (ist->prev_sub.got_output) {
+            int end = av_rescale(subtitle.pts - ist->prev_sub.subtitle.pts,
+                                 1000, AV_TIME_BASE);
+            if (end < ist->prev_sub.subtitle.end_display_time) {
+                av_log(ist->st->codec, AV_LOG_DEBUG,
+                       "Subtitle duration reduced from %d to %d\n",
+                       ist->prev_sub.subtitle.end_display_time, end);
+                ist->prev_sub.subtitle.end_display_time = end;
+            }
+        }
+        FFSWAP(int,        *got_output, ist->prev_sub.got_output);
+        FFSWAP(int,        ret,         ist->prev_sub.ret);
+        FFSWAP(AVSubtitle, subtitle,    ist->prev_sub.subtitle);
+    }
+
+    sub2video_update(ist, &subtitle);
 
 
-    sub2video_update(ist, &subtitle, pkt->pts);
+    if (!*got_output || !subtitle.num_rects)
+        return ret;
+
+    rate_emu_sleep(ist);
 
     for (i = 0; i < nb_output_streams; i++) {
         OutputStream *ost = output_streams[i];
 
     for (i = 0; i < nb_output_streams; i++) {
         OutputStream *ost = output_streams[i];
@@ -1659,7 +1708,7 @@ static int transcode_subtitles(InputStream *ist, AVPacket *pkt, int *got_output)
         if (!check_output_constraints(ist, ost) || !ost->encoding_needed)
             continue;
 
         if (!check_output_constraints(ist, ost) || !ost->encoding_needed)
             continue;
 
-        do_subtitle_out(output_files[ost->file_index]->ctx, ost, ist, &subtitle, pkt->pts);
+        do_subtitle_out(output_files[ost->file_index]->ctx, ost, ist, &subtitle);
     }
 
     avsubtitle_free(&subtitle);
     }
 
     avsubtitle_free(&subtitle);
@@ -1913,7 +1962,7 @@ static int transcode_init(void)
 {
     int ret = 0, i, j, k;
     AVFormatContext *oc;
 {
     int ret = 0, i, j, k;
     AVFormatContext *oc;
-    AVCodecContext *codec, *icodec = NULL;
+    AVCodecContext *codec;
     OutputStream *ost;
     InputStream *ist;
     char error[1024];
     OutputStream *ost;
     InputStream *ist;
     char error[1024];
@@ -1944,6 +1993,7 @@ static int transcode_init(void)
 
     /* for each output stream, we compute the right encoding parameters */
     for (i = 0; i < nb_output_streams; i++) {
 
     /* for each output stream, we compute the right encoding parameters */
     for (i = 0; i < nb_output_streams; i++) {
+        AVCodecContext *icodec = NULL;
         ost = output_streams[i];
         oc  = output_files[ost->file_index]->ctx;
         ist = get_input_stream(ost);
         ost = output_streams[i];
         oc  = output_files[ost->file_index]->ctx;
         ist = get_input_stream(ost);
@@ -2021,8 +2071,10 @@ static int transcode_init(void)
             } else if(!(oc->oformat->flags & AVFMT_VARIABLE_FPS)
                       && strcmp(oc->oformat->name, "mov") && strcmp(oc->oformat->name, "mp4") && strcmp(oc->oformat->name, "3gp")
                       && strcmp(oc->oformat->name, "3g2") && strcmp(oc->oformat->name, "psp") && strcmp(oc->oformat->name, "ipod")
             } else if(!(oc->oformat->flags & AVFMT_VARIABLE_FPS)
                       && strcmp(oc->oformat->name, "mov") && strcmp(oc->oformat->name, "mp4") && strcmp(oc->oformat->name, "3gp")
                       && strcmp(oc->oformat->name, "3g2") && strcmp(oc->oformat->name, "psp") && strcmp(oc->oformat->name, "ipod")
+                      && strcmp(oc->oformat->name, "f4v")
             ) {
             ) {
-                if(   copy_tb<0 && av_q2d(icodec->time_base)*icodec->ticks_per_frame > av_q2d(ist->st->time_base)
+                if(   copy_tb<0 && icodec->time_base.den
+                                && av_q2d(icodec->time_base)*icodec->ticks_per_frame > av_q2d(ist->st->time_base)
                                 && av_q2d(ist->st->time_base) < 1.0/500
                    || copy_tb==0){
                     codec->time_base = icodec->time_base;
                                 && av_q2d(ist->st->time_base) < 1.0/500
                    || copy_tb==0){
                     codec->time_base = icodec->time_base;
@@ -2089,7 +2141,7 @@ static int transcode_init(void)
             }
 
             if (ist)
             }
 
             if (ist)
-                ist->decoding_needed = 1;
+                ist->decoding_needed++;
             ost->encoding_needed = 1;
 
             if (!ost->filter &&
             ost->encoding_needed = 1;
 
             if (!ost->filter &&
@@ -2107,6 +2159,8 @@ static int transcode_init(void)
                 if (ost->filter && !ost->frame_rate.num)
                     ost->frame_rate = av_buffersink_get_frame_rate(ost->filter->filter);
                 if (ist && !ost->frame_rate.num)
                 if (ost->filter && !ost->frame_rate.num)
                     ost->frame_rate = av_buffersink_get_frame_rate(ost->filter->filter);
                 if (ist && !ost->frame_rate.num)
+                    ost->frame_rate = ist->framerate;
+                if (ist && !ost->frame_rate.num)
                     ost->frame_rate = ist->st->r_frame_rate.num ? ist->st->r_frame_rate : (AVRational){25, 1};
 //                    ost->frame_rate = ist->st->avg_frame_rate.num ? ist->st->avg_frame_rate : (AVRational){25, 1};
                 if (ost->enc && ost->enc->supported_framerates && !ost->force_fps) {
                     ost->frame_rate = ist->st->r_frame_rate.num ? ist->st->r_frame_rate : (AVRational){25, 1};
 //                    ost->frame_rate = ist->st->avg_frame_rate.num ? ist->st->avg_frame_rate : (AVRational){25, 1};
                 if (ost->enc && ost->enc->supported_framerates && !ost->force_fps) {
@@ -2158,6 +2212,10 @@ static int transcode_init(void)
                 break;
             case AVMEDIA_TYPE_SUBTITLE:
                 codec->time_base = (AVRational){1, 1000};
                 break;
             case AVMEDIA_TYPE_SUBTITLE:
                 codec->time_base = (AVRational){1, 1000};
+                if (!codec->width) {
+                    codec->width     = input_streams[ost->source_index]->st->codec->width;
+                    codec->height    = input_streams[ost->source_index]->st->codec->height;
+                }
                 break;
             default:
                 abort();
                 break;
             default:
                 abort();
@@ -2169,7 +2227,8 @@ static int transcode_init(void)
                 FILE *f;
 
                 snprintf(logfilename, sizeof(logfilename), "%s-%d.log",
                 FILE *f;
 
                 snprintf(logfilename, sizeof(logfilename), "%s-%d.log",
-                         pass_logfilename_prefix ? pass_logfilename_prefix : DEFAULT_PASS_LOGFILENAME_PREFIX,
+                         ost->logfile_prefix ? ost->logfile_prefix :
+                                               DEFAULT_PASS_LOGFILENAME_PREFIX,
                          i);
                 if (!strcmp(ost->enc->name, "libx264")) {
                     av_dict_set(&ost->opts, "stats", logfilename, AV_DICT_DONT_OVERWRITE);
                          i);
                 if (!strcmp(ost->enc->name, "libx264")) {
                     av_dict_set(&ost->opts, "stats", logfilename, AV_DICT_DONT_OVERWRITE);
@@ -2369,13 +2428,13 @@ static int need_output(void)
         OutputFile *of       = output_files[ost->file_index];
         AVFormatContext *os  = output_files[ost->file_index]->ctx;
 
         OutputFile *of       = output_files[ost->file_index];
         AVFormatContext *os  = output_files[ost->file_index]->ctx;
 
-        if (ost->is_past_recording_time ||
+        if (ost->finished ||
             (os->pb && avio_tell(os->pb) >= of->limit_filesize))
             continue;
         if (ost->frame_number >= ost->max_frames) {
             int j;
             for (j = 0; j < of->ctx->nb_streams; j++)
             (os->pb && avio_tell(os->pb) >= of->limit_filesize))
             continue;
         if (ost->frame_number >= ost->max_frames) {
             int j;
             for (j = 0; j < of->ctx->nb_streams; j++)
-                output_streams[of->ost_index + j]->is_past_recording_time = 1;
+                close_output_stream(output_streams[of->ost_index + j]);
             continue;
         }
 
             continue;
         }
 
@@ -2385,84 +2444,27 @@ static int need_output(void)
     return 0;
 }
 
     return 0;
 }
 
-static int input_acceptable(InputStream *ist)
-{
-    av_assert1(!ist->discard);
-    return !input_files[ist->file_index]->unavailable &&
-           !input_files[ist->file_index]->eof_reached;
-}
-
-static int find_graph_input(FilterGraph *graph)
-{
-    int i, nb_req_max = 0, file_index = -1;
-
-    for (i = 0; i < graph->nb_inputs; i++) {
-        int nb_req = av_buffersrc_get_nb_failed_requests(graph->inputs[i]->filter);
-        if (nb_req > nb_req_max) {
-            InputStream *ist = graph->inputs[i]->ist;
-            if (input_acceptable(ist)) {
-                nb_req_max = nb_req;
-                file_index = ist->file_index;
-            }
-        }
-    }
-
-    return file_index;
-}
-
 /**
 /**
- * Select the input file to read from.
+ * Select the output stream to process.
  *
  *
- * @return  >=0 index of the input file to use;
- *          -1  if no file is acceptable;
- *          -2  to read from filters without reading from a file
+ * @return  selected output stream, or NULL if none available
  */
  */
-static int select_input_file(void)
+static OutputStream *choose_output(void)
 {
 {
-    int i, ret, nb_active_out = nb_output_streams, ost_index = -1;
-    int64_t opts_min;
-    OutputStream *ost;
-    AVFilterBufferRef *dummy;
+    int i;
+    int64_t opts_min = INT64_MAX;
+    OutputStream *ost_min = NULL;
 
 
-    for (i = 0; i < nb_output_streams; i++)
-        nb_active_out -= output_streams[i]->unavailable =
-            output_streams[i]->is_past_recording_time;
-    while (nb_active_out) {
-        opts_min = INT64_MAX;
-        ost_index = -1;
-        for (i = 0; i < nb_output_streams; i++) {
-            OutputStream *ost = output_streams[i];
-            int64_t opts = av_rescale_q(ost->st->cur_dts, ost->st->time_base,
-                                        AV_TIME_BASE_Q);
-            if (!ost->unavailable && opts < opts_min) {
-                opts_min  = opts;
-                ost_index = i;
-            }
+    for (i = 0; i < nb_output_streams; i++) {
+        OutputStream *ost = output_streams[i];
+        int64_t opts = av_rescale_q(ost->st->cur_dts, ost->st->time_base,
+                                    AV_TIME_BASE_Q);
+        if (!ost->unavailable && !ost->finished && opts < opts_min) {
+            opts_min = opts;
+            ost_min  = ost;
         }
         }
-        if (ost_index < 0)
-            return -1;
-
-        ost = output_streams[ost_index];
-        if (ost->source_index >= 0) {
-            /* ost is directly connected to an input */
-            InputStream *ist = input_streams[ost->source_index];
-            if (input_acceptable(ist))
-                return ist->file_index;
-        } else {
-            /* ost is connected to a complex filtergraph */
-            av_assert1(ost->filter);
-            ret = av_buffersink_get_buffer_ref(ost->filter->filter, &dummy,
-                                               AV_BUFFERSINK_FLAG_PEEK);
-            if (ret >= 0)
-                return -2;
-            ret = find_graph_input(ost->filter->graph);
-            if (ret >= 0)
-                return ret;
-        }
-        ost->unavailable = 1;
-        nb_active_out--;
     }
     }
-    return -1;
+    return ost_min;
 }
 
 static int check_keyboard_interaction(int64_t cur_time)
 }
 
 static int check_keyboard_interaction(int64_t cur_time)
@@ -2677,16 +2679,281 @@ static int get_input_packet(InputFile *f, AVPacket *pkt)
     return av_read_frame(f->ctx, pkt);
 }
 
     return av_read_frame(f->ctx, pkt);
 }
 
+static int got_eagain(void)
+{
+    int i;
+    for (i = 0; i < nb_output_streams; i++)
+        if (output_streams[i]->unavailable)
+            return 1;
+    return 0;
+}
+
+static void reset_eagain(void)
+{
+    int i;
+    for (i = 0; i < nb_input_files; i++)
+        input_files[i]->eagain = 0;
+    for (i = 0; i < nb_output_streams; i++)
+        output_streams[i]->unavailable = 0;
+}
+
+/**
+ * @return
+ * - 0 -- one packet was read and processed
+ * - AVERROR(EAGAIN) -- no packets were available for selected file,
+ *   this function should be called again
+ * - AVERROR_EOF -- this function should not be called again
+ */
+static int process_input(int file_index)
+{
+    InputFile *ifile = input_files[file_index];
+    AVFormatContext *is;
+    InputStream *ist;
+    AVPacket pkt;
+    int ret, i, j;
+
+    is  = ifile->ctx;
+    ret = get_input_packet(ifile, &pkt);
+
+    if (ret == AVERROR(EAGAIN)) {
+        ifile->eagain = 1;
+        return ret;
+    }
+    if (ret < 0) {
+        if (ret != AVERROR_EOF) {
+            print_error(is->filename, ret);
+            if (exit_on_error)
+                exit_program(1);
+        }
+        ifile->eof_reached = 1;
+
+        for (i = 0; i < ifile->nb_streams; i++) {
+            ist = input_streams[ifile->ist_index + i];
+            if (ist->decoding_needed)
+                output_packet(ist, NULL);
+
+            /* mark all outputs that don't go through lavfi as finished */
+            for (j = 0; j < nb_output_streams; j++) {
+                OutputStream *ost = output_streams[j];
+
+                if (ost->source_index == ifile->ist_index + i &&
+                    (ost->stream_copy || ost->enc->type == AVMEDIA_TYPE_SUBTITLE))
+                    close_output_stream(ost);
+            }
+        }
+
+        return AVERROR(EAGAIN);
+    }
+
+    reset_eagain();
+
+    if (do_pkt_dump) {
+        av_pkt_dump_log2(NULL, AV_LOG_DEBUG, &pkt, do_hex_dump,
+                         is->streams[pkt.stream_index]);
+    }
+    /* the following test is needed in case new streams appear
+       dynamically in stream : we ignore them */
+    if (pkt.stream_index >= ifile->nb_streams) {
+        report_new_stream(file_index, &pkt);
+        goto discard_packet;
+    }
+
+    ist = input_streams[ifile->ist_index + pkt.stream_index];
+    if (ist->discard)
+        goto discard_packet;
+
+    if(!ist->wrap_correction_done && input_files[file_index]->ctx->start_time != AV_NOPTS_VALUE && ist->st->pts_wrap_bits < 64){
+        int64_t stime = av_rescale_q(input_files[file_index]->ctx->start_time, AV_TIME_BASE_Q, ist->st->time_base);
+        int64_t stime2= stime + (1ULL<<ist->st->pts_wrap_bits);
+        ist->wrap_correction_done = 1;
+
+        if(stime2 > stime && pkt.dts != AV_NOPTS_VALUE && pkt.dts > stime + (1LL<<(ist->st->pts_wrap_bits-1))) {
+            pkt.dts -= 1ULL<<ist->st->pts_wrap_bits;
+            ist->wrap_correction_done = 0;
+        }
+        if(stime2 > stime && pkt.pts != AV_NOPTS_VALUE && pkt.pts > stime + (1LL<<(ist->st->pts_wrap_bits-1))) {
+            pkt.pts -= 1ULL<<ist->st->pts_wrap_bits;
+            ist->wrap_correction_done = 0;
+        }
+    }
+
+    if (pkt.dts != AV_NOPTS_VALUE)
+        pkt.dts += av_rescale_q(ifile->ts_offset, AV_TIME_BASE_Q, ist->st->time_base);
+    if (pkt.pts != AV_NOPTS_VALUE)
+        pkt.pts += av_rescale_q(ifile->ts_offset, AV_TIME_BASE_Q, ist->st->time_base);
+
+    if (pkt.pts != AV_NOPTS_VALUE)
+        pkt.pts *= ist->ts_scale;
+    if (pkt.dts != AV_NOPTS_VALUE)
+        pkt.dts *= ist->ts_scale;
+
+    if (debug_ts) {
+        av_log(NULL, AV_LOG_INFO, "demuxer -> ist_index:%d type:%s "
+                "next_dts:%s next_dts_time:%s next_pts:%s next_pts_time:%s  pkt_pts:%s pkt_pts_time:%s pkt_dts:%s pkt_dts_time:%s off:%"PRId64"\n",
+                ifile->ist_index + pkt.stream_index, av_get_media_type_string(ist->st->codec->codec_type),
+                av_ts2str(ist->next_dts), av_ts2timestr(ist->next_dts, &AV_TIME_BASE_Q),
+                av_ts2str(ist->next_pts), av_ts2timestr(ist->next_pts, &AV_TIME_BASE_Q),
+                av_ts2str(pkt.pts), av_ts2timestr(pkt.pts, &ist->st->time_base),
+                av_ts2str(pkt.dts), av_ts2timestr(pkt.dts, &ist->st->time_base),
+                input_files[ist->file_index]->ts_offset);
+    }
+
+    if (pkt.dts != AV_NOPTS_VALUE && ist->next_dts != AV_NOPTS_VALUE &&
+        !copy_ts) {
+        int64_t pkt_dts = av_rescale_q(pkt.dts, ist->st->time_base, AV_TIME_BASE_Q);
+        int64_t delta   = pkt_dts - ist->next_dts;
+        if (is->iformat->flags & AVFMT_TS_DISCONT) {
+        if(delta < -1LL*dts_delta_threshold*AV_TIME_BASE ||
+            (delta > 1LL*dts_delta_threshold*AV_TIME_BASE &&
+                ist->st->codec->codec_type != AVMEDIA_TYPE_SUBTITLE) ||
+            pkt_dts+1<ist->pts){
+            ifile->ts_offset -= delta;
+            av_log(NULL, AV_LOG_DEBUG,
+                   "timestamp discontinuity %"PRId64", new offset= %"PRId64"\n",
+                   delta, ifile->ts_offset);
+            pkt.dts -= av_rescale_q(delta, AV_TIME_BASE_Q, ist->st->time_base);
+            if (pkt.pts != AV_NOPTS_VALUE)
+                pkt.pts -= av_rescale_q(delta, AV_TIME_BASE_Q, ist->st->time_base);
+        }
+        } else {
+            if ( delta < -1LL*dts_error_threshold*AV_TIME_BASE ||
+                (delta > 1LL*dts_error_threshold*AV_TIME_BASE && ist->st->codec->codec_type != AVMEDIA_TYPE_SUBTITLE)
+               ) {
+                av_log(NULL, AV_LOG_WARNING, "DTS %"PRId64", next:%"PRId64" st:%d invalid dropping\n", pkt.dts, ist->next_dts, pkt.stream_index);
+                pkt.dts = AV_NOPTS_VALUE;
+            }
+            if (pkt.pts != AV_NOPTS_VALUE){
+                int64_t pkt_pts = av_rescale_q(pkt.pts, ist->st->time_base, AV_TIME_BASE_Q);
+                delta   = pkt_pts - ist->next_dts;
+                if ( delta < -1LL*dts_error_threshold*AV_TIME_BASE ||
+                    (delta > 1LL*dts_error_threshold*AV_TIME_BASE && ist->st->codec->codec_type != AVMEDIA_TYPE_SUBTITLE)
+                   ) {
+                    av_log(NULL, AV_LOG_WARNING, "PTS %"PRId64", next:%"PRId64" invalid dropping st:%d\n", pkt.pts, ist->next_dts, pkt.stream_index);
+                    pkt.pts = AV_NOPTS_VALUE;
+                }
+            }
+        }
+    }
+
+    sub2video_heartbeat(ist, pkt.pts);
+
+    ret = output_packet(ist, &pkt);
+    if (ret < 0) {
+        char buf[128];
+        av_strerror(ret, buf, sizeof(buf));
+        av_log(NULL, AV_LOG_ERROR, "Error while decoding stream #%d:%d: %s\n",
+                ist->file_index, ist->st->index, buf);
+        if (exit_on_error)
+            exit_program(1);
+    }
+
+discard_packet:
+    av_free_packet(&pkt);
+
+    return 0;
+}
+
+/**
+ * Perform a step of transcoding for the specified filter graph.
+ *
+ * @param[in]  graph     filter graph to consider
+ * @param[out] best_ist  input stream where a frame would allow to continue
+ * @return  0 for success, <0 for error
+ */
+static int transcode_from_filter(FilterGraph *graph, InputStream **best_ist)
+{
+    int i, ret;
+    int nb_requests, nb_requests_max = 0;
+    InputFilter *ifilter;
+    InputStream *ist;
+
+    *best_ist = NULL;
+    ret = avfilter_graph_request_oldest(graph->graph);
+    if (ret >= 0)
+        return reap_filters();
+
+    if (ret == AVERROR_EOF) {
+        ret = reap_filters();
+        for (i = 0; i < graph->nb_outputs; i++)
+            close_output_stream(graph->outputs[i]->ost);
+        return ret;
+    }
+    if (ret != AVERROR(EAGAIN))
+        return ret;
+
+    for (i = 0; i < graph->nb_inputs; i++) {
+        ifilter = graph->inputs[i];
+        ist = ifilter->ist;
+        if (input_files[ist->file_index]->eagain ||
+            input_files[ist->file_index]->eof_reached)
+            continue;
+        nb_requests = av_buffersrc_get_nb_failed_requests(ifilter->filter);
+        if (nb_requests > nb_requests_max) {
+            nb_requests_max = nb_requests;
+            *best_ist = ist;
+        }
+    }
+
+    if (!*best_ist)
+        for (i = 0; i < graph->nb_outputs; i++)
+            graph->outputs[i]->ost->unavailable = 1;
+
+    return 0;
+}
+
+/**
+ * Run a single step of transcoding.
+ *
+ * @return  0 for success, <0 for error
+ */
+static int transcode_step(void)
+{
+    OutputStream *ost;
+    InputStream  *ist;
+    int ret;
+
+    ost = choose_output();
+    if (!ost) {
+        if (got_eagain()) {
+            reset_eagain();
+            av_usleep(10000);
+            return 0;
+        }
+        av_log(NULL, AV_LOG_VERBOSE, "No more inputs to read from, finishing.\n");
+        return AVERROR_EOF;
+    }
+
+    if (ost->filter) {
+        if ((ret = transcode_from_filter(ost->filter->graph, &ist)) < 0)
+            return ret;
+        if (!ist)
+            return 0;
+    } else {
+        av_assert0(ost->source_index >= 0);
+        ist = input_streams[ost->source_index];
+    }
+
+    ret = process_input(ist->file_index);
+    if (ret == AVERROR(EAGAIN)) {
+        if (input_files[ist->file_index]->eagain)
+            ost->unavailable = 1;
+        return 0;
+    }
+    if (ret < 0)
+        return ret == AVERROR_EOF ? 0 : ret;
+
+    return reap_filters();
+}
+
 /*
  * The following code is the main loop of the file converter
  */
 static int transcode(void)
 {
     int ret, i;
 /*
  * The following code is the main loop of the file converter
  */
 static int transcode(void)
 {
     int ret, i;
-    AVFormatContext *is, *os;
+    AVFormatContext *os;
     OutputStream *ost;
     InputStream *ist;
     OutputStream *ost;
     InputStream *ist;
-    int no_packet_count = 0;
     int64_t timer_start;
 
     ret = transcode_init();
     int64_t timer_start;
 
     ret = transcode_init();
@@ -2704,9 +2971,7 @@ static int transcode(void)
         goto fail;
 #endif
 
         goto fail;
 #endif
 
-    for (; received_sigterm == 0;) {
-        int file_index, ist_index;
-        AVPacket pkt;
+    while (!received_sigterm) {
         int64_t cur_time= av_gettime();
 
         /* if 'q' pressed, exits */
         int64_t cur_time= av_gettime();
 
         /* if 'q' pressed, exits */
@@ -2720,162 +2985,15 @@ static int transcode(void)
             break;
         }
 
             break;
         }
 
-        /* select the stream that we must read now */
-        file_index = select_input_file();
-        /* if none, if is finished */
-        if (file_index == -2) {
-            poll_filters() ;
-            continue;
-        }
-        if (file_index < 0) {
-            if (no_packet_count) {
-                no_packet_count = 0;
-                for (i = 0; i < nb_input_files; i++)
-                    input_files[i]->unavailable = 0;
-                av_usleep(10000);
-                continue;
-            }
-            av_log(NULL, AV_LOG_VERBOSE, "No more inputs to read from, finishing.\n");
-            break;
-        }
-
-        is  = input_files[file_index]->ctx;
-        ret = get_input_packet(input_files[file_index], &pkt);
-
-        if (ret == AVERROR(EAGAIN)) {
-            input_files[file_index]->unavailable = 1;
-            no_packet_count++;
-            continue;
-        }
+        ret = transcode_step();
         if (ret < 0) {
         if (ret < 0) {
-            if (ret != AVERROR_EOF) {
-                print_error(is->filename, ret);
-                if (exit_on_error)
-                    exit_program(1);
-            }
-            input_files[file_index]->eof_reached = 1;
-
-            for (i = 0; i < input_files[file_index]->nb_streams; i++) {
-                ist = input_streams[input_files[file_index]->ist_index + i];
-                if (ist->decoding_needed)
-                    output_packet(ist, NULL);
-                poll_filters();
-            }
-
-            if (opt_shortest)
-                break;
-            else
+            if (ret == AVERROR_EOF || ret == AVERROR(EAGAIN))
                 continue;
                 continue;
-        }
 
 
-        no_packet_count = 0;
-        for (i = 0; i < nb_input_files; i++)
-            input_files[i]->unavailable = 0;
-
-        if (do_pkt_dump) {
-            av_pkt_dump_log2(NULL, AV_LOG_DEBUG, &pkt, do_hex_dump,
-                             is->streams[pkt.stream_index]);
-        }
-        /* the following test is needed in case new streams appear
-           dynamically in stream : we ignore them */
-        if (pkt.stream_index >= input_files[file_index]->nb_streams) {
-            report_new_stream(file_index, &pkt);
-            goto discard_packet;
-        }
-        ist_index = input_files[file_index]->ist_index + pkt.stream_index;
-        ist = input_streams[ist_index];
-        if (ist->discard)
-            goto discard_packet;
-
-        if(!ist->wrap_correction_done && input_files[file_index]->ctx->start_time != AV_NOPTS_VALUE && ist->st->pts_wrap_bits < 64){
-            uint64_t stime = av_rescale_q(input_files[file_index]->ctx->start_time, AV_TIME_BASE_Q, ist->st->time_base);
-            uint64_t stime2= stime + (1LL<<ist->st->pts_wrap_bits);
-            ist->wrap_correction_done = 1;
-            if(pkt.dts != AV_NOPTS_VALUE && pkt.dts > stime && pkt.dts - stime > stime2 - pkt.dts) {
-                pkt.dts -= 1LL<<ist->st->pts_wrap_bits;
-                ist->wrap_correction_done = 0;
-            }
-            if(pkt.pts != AV_NOPTS_VALUE && pkt.pts > stime && pkt.pts - stime > stime2 - pkt.pts) {
-                pkt.pts -= 1LL<<ist->st->pts_wrap_bits;
-                ist->wrap_correction_done = 0;
-            }
-        }
-
-        if (pkt.dts != AV_NOPTS_VALUE)
-            pkt.dts += av_rescale_q(input_files[ist->file_index]->ts_offset, AV_TIME_BASE_Q, ist->st->time_base);
-        if (pkt.pts != AV_NOPTS_VALUE)
-            pkt.pts += av_rescale_q(input_files[ist->file_index]->ts_offset, AV_TIME_BASE_Q, ist->st->time_base);
-
-        if (pkt.pts != AV_NOPTS_VALUE)
-            pkt.pts *= ist->ts_scale;
-        if (pkt.dts != AV_NOPTS_VALUE)
-            pkt.dts *= ist->ts_scale;
-
-        if (debug_ts) {
-            av_log(NULL, AV_LOG_INFO, "demuxer -> ist_index:%d type:%s "
-                    "next_dts:%s next_dts_time:%s next_pts:%s next_pts_time:%s  pkt_pts:%s pkt_pts_time:%s pkt_dts:%s pkt_dts_time:%s off:%"PRId64"\n",
-                    ist_index, av_get_media_type_string(ist->st->codec->codec_type),
-                    av_ts2str(ist->next_dts), av_ts2timestr(ist->next_dts, &AV_TIME_BASE_Q),
-                    av_ts2str(ist->next_pts), av_ts2timestr(ist->next_pts, &AV_TIME_BASE_Q),
-                    av_ts2str(pkt.pts), av_ts2timestr(pkt.pts, &ist->st->time_base),
-                    av_ts2str(pkt.dts), av_ts2timestr(pkt.dts, &ist->st->time_base),
-                    input_files[ist->file_index]->ts_offset);
-        }
-
-        if (pkt.dts != AV_NOPTS_VALUE && ist->next_dts != AV_NOPTS_VALUE && !copy_ts) {
-            int64_t pkt_dts = av_rescale_q(pkt.dts, ist->st->time_base, AV_TIME_BASE_Q);
-            int64_t delta   = pkt_dts - ist->next_dts;
-            if (is->iformat->flags & AVFMT_TS_DISCONT) {
-            if(delta < -1LL*dts_delta_threshold*AV_TIME_BASE ||
-                (delta > 1LL*dts_delta_threshold*AV_TIME_BASE &&
-                 ist->st->codec->codec_type != AVMEDIA_TYPE_SUBTITLE) ||
-                pkt_dts+1<ist->pts){
-                input_files[ist->file_index]->ts_offset -= delta;
-                av_log(NULL, AV_LOG_DEBUG,
-                       "timestamp discontinuity %"PRId64", new offset= %"PRId64"\n",
-                       delta, input_files[ist->file_index]->ts_offset);
-                pkt.dts-= av_rescale_q(delta, AV_TIME_BASE_Q, ist->st->time_base);
-                if (pkt.pts != AV_NOPTS_VALUE)
-                    pkt.pts-= av_rescale_q(delta, AV_TIME_BASE_Q, ist->st->time_base);
-            }
-            } else {
-                if ( delta < -1LL*dts_error_threshold*AV_TIME_BASE ||
-                    (delta > 1LL*dts_error_threshold*AV_TIME_BASE && ist->st->codec->codec_type != AVMEDIA_TYPE_SUBTITLE) ||
-                     pkt_dts+1<ist->pts){
-                    av_log(NULL, AV_LOG_WARNING, "DTS %"PRId64", next:%"PRId64" st:%d invalid dropping\n", pkt.dts, ist->next_dts, pkt.stream_index);
-                    pkt.dts = AV_NOPTS_VALUE;
-                }
-                if (pkt.pts != AV_NOPTS_VALUE){
-                    int64_t pkt_pts = av_rescale_q(pkt.pts, ist->st->time_base, AV_TIME_BASE_Q);
-                    delta   = pkt_pts - ist->next_dts;
-                    if ( delta < -1LL*dts_error_threshold*AV_TIME_BASE ||
-                        (delta > 1LL*dts_error_threshold*AV_TIME_BASE && ist->st->codec->codec_type != AVMEDIA_TYPE_SUBTITLE) ||
-                        pkt_pts+1<ist->pts) {
-                        av_log(NULL, AV_LOG_WARNING, "PTS %"PRId64", next:%"PRId64" invalid dropping st:%d\n", pkt.pts, ist->next_dts, pkt.stream_index);
-                        pkt.pts = AV_NOPTS_VALUE;
-                    }
-                }
-            }
-        }
-
-        sub2video_heartbeat(ist, pkt.pts);
-
-        // fprintf(stderr,"read #%d.%d size=%d\n", ist->file_index, ist->st->index, pkt.size);
-        if ((ret = output_packet(ist, &pkt)) < 0 ||
-            ((ret = poll_filters()) < 0 && ret != AVERROR_EOF)) {
-            char buf[128];
-            av_strerror(ret, buf, sizeof(buf));
-            av_log(NULL, AV_LOG_ERROR, "Error while decoding stream #%d:%d: %s\n",
-                   ist->file_index, ist->st->index, buf);
-            if (exit_on_error)
-                exit_program(1);
-            av_free_packet(&pkt);
-            continue;
+            av_log(NULL, AV_LOG_ERROR, "Error while filtering.\n");
+            break;
         }
 
         }
 
-    discard_packet:
-        av_free_packet(&pkt);
-
         /* dump report by using the output first video and audio streams */
         print_report(0, timer_start, cur_time);
     }
         /* dump report by using the output first video and audio streams */
         print_report(0, timer_start, cur_time);
     }
@@ -2890,7 +3008,6 @@ static int transcode(void)
             output_packet(ist, NULL);
         }
     }
             output_packet(ist, NULL);
         }
     }
-    poll_filters();
     flush_encoders();
 
     term_exit();
     flush_encoders();
 
     term_exit();
@@ -2993,7 +3110,7 @@ static void parse_cpuflags(int argc, char **argv, const OptionDef *options)
 {
     int idx = locate_option(argc, argv, options, "cpuflags");
     if (idx && argv[idx + 1])
 {
     int idx = locate_option(argc, argv, options, "cpuflags");
     if (idx && argv[idx + 1])
-        opt_cpuflags("cpuflags", argv[idx + 1]);
+        opt_cpuflags(NULL, "cpuflags", argv[idx + 1]);
 }
 
 int main(int argc, char **argv)
 }
 
 int main(int argc, char **argv)
@@ -3042,10 +3159,10 @@ int main(int argc, char **argv)
         exit_program(1);
     }
 
         exit_program(1);
     }
 
-    if (nb_input_files == 0) {
-        av_log(NULL, AV_LOG_FATAL, "At least one input file must be specified\n");
-        exit_program(1);
-    }
+//     if (nb_input_files == 0) {
+//         av_log(NULL, AV_LOG_FATAL, "At least one input file must be specified\n");
+//         exit_program(1);
+//     }
 
     current_time = ti = getutime();
     if (transcode() < 0)
 
     current_time = ti = getutime();
     if (transcode() < 0)