yadif: Improve pts calculation for is_second.
[ffmpeg.git] / libavfilter / vf_yadif.c
index c3fd93c..d8e2ad6 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2006-2010 Michael Niedermayer <michaelni@gmx.at>
+ * Copyright (C) 2006-2011 Michael Niedermayer <michaelni@gmx.at>
  *               2010      James Darnley <james.darnley@gmail.com>
  *
  * FFmpeg is free software; you can redistribute it and/or modify
@@ -143,7 +143,7 @@ static void filter(AVFilterContext *ctx, AVFilterBufferRef *dstpic,
         int w = dstpic->video->w;
         int h = dstpic->video->h;
         int refs = yadif->cur->linesize[i];
-        int df = (yadif->csp->comp[i].depth_minus1+1) / 8;
+        int df = (yadif->csp->comp[i].depth_minus1 + 8) / 8;
 
         if (i == 1 || i == 2) {
         /* Why is this not part of the per-plane description thing? */
@@ -210,17 +210,28 @@ static void return_frame(AVFilterContext *ctx, int is_second)
 
     if (!yadif->csp)
         yadif->csp = &av_pix_fmt_descriptors[link->format];
-    if (yadif->csp->comp[0].depth_minus1 == 15)
-        yadif->filter_line = filter_line_c_16bit;
+    if (yadif->csp->comp[0].depth_minus1 / 8 == 1)
+        yadif->filter_line = (void*)filter_line_c_16bit;
 
     filter(ctx, yadif->out, tff ^ !is_second, tff);
 
     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);
 
@@ -352,6 +367,9 @@ static int query_formats(AVFilterContext *ctx)
         AV_NE( PIX_FMT_GRAY16BE, PIX_FMT_GRAY16LE ),
         PIX_FMT_YUV440P,
         PIX_FMT_YUVJ440P,
+        AV_NE( PIX_FMT_YUV420P10BE, PIX_FMT_YUV420P10LE ),
+        AV_NE( PIX_FMT_YUV422P10BE, PIX_FMT_YUV422P10LE ),
+        AV_NE( PIX_FMT_YUV444P10BE, PIX_FMT_YUV444P10LE ),
         AV_NE( PIX_FMT_YUV420P16BE, PIX_FMT_YUV420P16LE ),
         AV_NE( PIX_FMT_YUV422P16BE, PIX_FMT_YUV422P16LE ),
         AV_NE( PIX_FMT_YUV444P16BE, PIX_FMT_YUV444P16LE ),
@@ -367,7 +385,7 @@ static int query_formats(AVFilterContext *ctx)
 static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque)
 {
     YADIFContext *yadif = ctx->priv;
-    av_unused int cpu_flags = av_get_cpu_flags();
+    int cpu_flags = av_get_cpu_flags();
 
     yadif->mode = 0;
     yadif->parity = -1;
@@ -393,22 +411,23 @@ static void null_draw_slice(AVFilterLink *link, int y, int h, int slice_dir) { }
 
 AVFilter avfilter_vf_yadif = {
     .name          = "yadif",
-    .description   = NULL_IF_CONFIG_SMALL("Deinterlace the input image"),
+    .description   = NULL_IF_CONFIG_SMALL("Deinterlace the input image."),
 
     .priv_size     = sizeof(YADIFContext),
     .init          = init,
     .uninit        = uninit,
     .query_formats = query_formats,
 
-    .inputs    = (AVFilterPad[]) {{ .name             = "default",
+    .inputs    = (const AVFilterPad[]) {{ .name       = "default",
                                     .type             = AVMEDIA_TYPE_VIDEO,
                                     .start_frame      = start_frame,
                                     .get_video_buffer = get_video_buffer,
                                     .draw_slice       = null_draw_slice,
-                                    .end_frame        = end_frame, },
+                                    .end_frame        = end_frame,
+                                    .rej_perms        = AV_PERM_REUSE2, },
                                   { .name = NULL}},
 
-    .outputs   = (AVFilterPad[]) {{ .name             = "default",
+    .outputs   = (const AVFilterPad[]) {{ .name       = "default",
                                     .type             = AVMEDIA_TYPE_VIDEO,
                                     .poll_frame       = poll_frame,
                                     .request_frame    = request_frame, },