[x264-devel] [PATCH] Simple deinterlace filter using the FFmpeg deinterlacer.

Kazuo Teramoto kaz.rag at gmail.com
Fri Jul 16 09:53:59 CEST 2010


---
 Makefile              |    2 +-
 configure             |    2 +-
 filters/video/deint.c |  116 +++++++++++++++++++++++++++++++++++++++++++++++++
 filters/video/video.c |    1 +
 4 files changed, 119 insertions(+), 2 deletions(-)
 create mode 100644 filters/video/deint.c

diff --git a/Makefile b/Makefile
index 0f4d0cf..ba32bc8 100644
--- a/Makefile
+++ b/Makefile
@@ -18,7 +18,7 @@ SRCCLI = x264.c input/input.c input/timecode.c input/raw.c input/y4m.c \
          output/flv.c output/flv_bytestream.c filters/filters.c \
          filters/video/video.c filters/video/source.c filters/video/internal.c \
          filters/video/resize.c filters/video/cache.c filters/video/fix_vfr_pts.c \
-         filters/video/select_every.c filters/video/crop.c
+         filters/video/select_every.c filters/video/crop.c filters/video/deint.c
 
 SRCSO =
 
diff --git a/configure b/configure
index d8607f8..530127f 100755
--- a/configure
+++ b/configure
@@ -770,7 +770,7 @@ Cflags: -I$includedir
 EOF
 
 filters="crop select_every"
-[ $swscale = yes ] && filters="resize $filters"
+[ $swscale = yes ] && filters="resize deint $filters"
 
 cat > conftest.log <<EOF
 Platform:   $ARCH
diff --git a/filters/video/deint.c b/filters/video/deint.c
new file mode 100644
index 0000000..088b0a8
--- /dev/null
+++ b/filters/video/deint.c
@@ -0,0 +1,116 @@
+/*****************************************************************************
+ * deint.c: x264 video deinterlace filter
+ *****************************************************************************
+ * Copyright (C) 2010 Kazuo Teramoto <kaz.rag at gmail.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02111, USA.
+ *****************************************************************************/
+
+#include "video.h"
+#include <libavcodec/avcodec.h>
+#define NAME "deint"
+#define FAIL_IF_ERROR( cond, ... ) FAIL_IF_ERR( cond, NAME, __VA_ARGS__ )
+
+cli_vid_filter_t deint_filter;
+
+typedef struct
+{
+    hnd_t prev_hnd;
+    cli_vid_filter_t prev_filter;
+
+    int pix_fmt;
+    int width;
+    int height;
+} deint_hnd_t;
+
+static void help( int longhelp )
+{
+    printf( "      "NAME"\n" );
+    if( !longhelp )
+        return;
+    printf( "            deinterlace frames using the FFmpeg deinterlacing filter\n" );
+}
+
+
+static int convert_csp_to_pix_fmt( int csp )
+{
+    if( csp&X264_CSP_OTHER )
+        return csp&X264_CSP_MASK;
+    switch( csp&X264_CSP_MASK )
+    {
+        case X264_CSP_I420: return PIX_FMT_YUV420P;
+        case X264_CSP_I422: return PIX_FMT_YUV422P;
+        case X264_CSP_I444: return PIX_FMT_YUV444P;
+        case X264_CSP_NV12: return PIX_FMT_NV12;
+        case X264_CSP_YV12: return PIX_FMT_YUV420P; /* specially handled via swapping chroma */
+        case X264_CSP_BGR:  return PIX_FMT_BGR24;
+        case X264_CSP_BGRA: return PIX_FMT_BGRA;
+        default:            return PIX_FMT_NONE;
+    }
+}
+
+
+static int init( hnd_t *handle, cli_vid_filter_t *filter, video_info_t *info, x264_param_t *param, char *opt_string )
+{
+    FAIL_IF_ERROR( !info->interlaced,
+                   "deint need an interlaced input\n" )
+
+    deint_hnd_t *h = calloc( 1, sizeof(deint_hnd_t) );
+    if( !h )
+        return -1;
+
+    h->pix_fmt = convert_csp_to_pix_fmt( info->csp );
+    h->width = info->width;
+    h->height = info->height;
+
+    info->interlaced = 0;
+
+    h->prev_filter = *filter;
+    h->prev_hnd = *handle;
+    *handle = h;
+    *filter = deint_filter;
+
+    return 0;
+}
+
+static int get_frame( hnd_t handle, cli_pic_t *output, int frame )
+{
+    deint_hnd_t *h = handle;
+    if( h->prev_filter.get_frame( h->prev_hnd, output, frame ) )
+        return -1;
+
+    AVPicture *img = calloc( 1, sizeof(AVPicture) );
+    for(int i=0; i < 3; i++) {
+       img->data[i] = output->img.plane[i];
+       img->linesize[i] = output->img.stride[i];
+    }
+
+    return avpicture_deinterlace(img, img, h->pix_fmt, h->width, h->height);
+}
+
+static int release_frame( hnd_t handle, cli_pic_t *pic, int frame )
+{
+    deint_hnd_t *h = handle;
+    return h->prev_filter.release_frame( h->prev_hnd, pic, frame );
+}
+
+static void free_filter( hnd_t handle )
+{
+    deint_hnd_t *h = handle;
+    h->prev_filter.free( h->prev_hnd );
+    free( h );
+}
+
+cli_vid_filter_t deint_filter = { NAME, help, init, get_frame, release_frame, free_filter, NULL };
diff --git a/filters/video/video.c b/filters/video/video.c
index 8da89e3..66b6225 100644
--- a/filters/video/video.c
+++ b/filters/video/video.c
@@ -46,6 +46,7 @@ void x264_register_vid_filters()
     REGISTER_VFILTER( fix_vfr_pts );
     REGISTER_VFILTER( resize );
     REGISTER_VFILTER( select_every );
+    REGISTER_VFILTER( deint );
 }
 
 int x264_init_vid_filter( const char *name, hnd_t *handle, cli_vid_filter_t *filter,
-- 
1.7.1.1



More information about the x264-devel mailing list