yadif: Improve pts calculation for is_second.
[ffmpeg.git] / libavfilter / vf_yadif.c
index c9f8705..d8e2ad6 100644 (file)
@@ -218,9 +218,20 @@ static void return_frame(AVFilterContext *ctx, int is_second)
     if (is_second) {
         if (yadif->next->pts != AV_NOPTS_VALUE &&
             yadif->cur->pts != AV_NOPTS_VALUE) {
-            yadif->out->pts =
-                (yadif->next->pts&yadif->cur->pts) +
-                ((yadif->next->pts^yadif->cur->pts)>>1);
+            uint64_t next_pts = yadif->next->pts;
+            uint64_t cur_pts  = yadif->cur->pts;
+            uint64_t prev_pts = yadif->prev->pts;
+
+            uint64_t ft = FFMIN3( cur_pts-prev_pts,
+                                  next_pts-cur_pts,
+                                 (next_pts-prev_pts)/2);
+
+            if(next_pts - cur_pts < 2*ft)
+                yadif->out->pts =
+                    (next_pts&cur_pts) +
+                    ((next_pts^cur_pts)>>1);
+            else
+                yadif->out->pts = cur_pts + ft/2;
         } else {
             yadif->out->pts = AV_NOPTS_VALUE;
         }
@@ -314,11 +325,15 @@ static int poll_frame(AVFilterLink *link)
         return 1;
 
     val = avfilter_poll_frame(link->src->inputs[0]);
+    if (val <= 0)
+        return val;
 
-    if (val==1 && !yadif->next) { //FIXME change API to not requre this red tape
+    if (val >= 1 && !yadif->next) { //FIXME change API to not requre this red tape
         if ((ret = avfilter_request_frame(link->src->inputs[0])) < 0)
             return ret;
         val = avfilter_poll_frame(link->src->inputs[0]);
+        if (val <= 0)
+            return val;
     }
     assert(yadif->next || !val);