[MPlayer-dev-eng] [PATCH] Donald Graft's kerndeint adaptive deinterlacing filter

Tobias Diedrich ranma at gmx.at
Thu Jan 29 00:33:10 CET 2004


This is a port of Donald Graft's kernel deinterlacing filter from
avisynth.  I needed an adaptive deinterlacer (we don't have one, do
we?), so I ported this one.  If no one is opposed I'll commit this after
pre4 is out.  (Hmm, should even be ok to commit before, since it only
adds a new filter and doesn't touch anything else)

-- 
Tobias						PGP: http://9ac7e0bc.2ya.com
This mail is made of 100% recycled bits.
-------------- next part --------------
? vf_kerndeint.c
Index: libmpcodecs/Makefile
===================================================================
RCS file: /cvsroot/mplayer/main/libmpcodecs/Makefile,v
retrieving revision 1.119
diff -u -r1.119 Makefile
--- libmpcodecs/Makefile	23 Dec 2003 21:04:50 -0000	1.119
+++ libmpcodecs/Makefile	28 Jan 2004 23:12:57 -0000
@@ -14,7 +14,7 @@
 VIDEO_SRCS_OPT=vd_realvid.c vd_ffmpeg.c vd_dshow.c vd_dmo.c vd_vfw.c vd_vfwex.c vd_odivx.c vd_divx4.c vd_zrmjpeg.c vd_xanim.c vd_xvid.c vd_xvid4.c vd_libdv.c vd_qtvideo.c vd_theora.c
 VIDEO_SRCS=dec_video.c vd.c $(VIDEO_SRCS_NAT) $(VIDEO_SRCS_LIB) $(VIDEO_SRCS_OPT)
 
-VFILTER_SRCS=vf.c vf_vo.c vf_crop.c vf_expand.c vf_scale.c vf_format.c vf_yuy2.c vf_flip.c vf_rgb2bgr.c vf_rotate.c vf_mirror.c vf_palette.c vf_lavc.c vf_dvbscale.c vf_cropdetect.c vf_test.c vf_noise.c vf_yvu9.c vf_rectangle.c vf_lavcdeint.c vf_eq.c vf_eq2.c vf_halfpack.c vf_dint.c vf_1bpp.c vf_bmovl.c vf_2xsai.c vf_unsharp.c vf_swapuv.c vf_il.c vf_boxblur.c vf_sab.c vf_smartblur.c vf_perspective.c vf_down3dright.c vf_field.c vf_denoise3d.c vf_hqdn3d.c vf_detc.c vf_telecine.c vf_tfields.c vf_ivtc.c vf_ilpack.c vf_dsize.c vf_decimate.c vf_softpulldown.c vf_tinterlace.c vf_pullup.c pullup.c vf_framestep.c vf_tile.c vf_delogo.c vf_fil.c vf_hue.c vf_spp.c vf_yuvcsp.c vf_filmdint.c
+VFILTER_SRCS=vf.c vf_vo.c vf_crop.c vf_expand.c vf_scale.c vf_format.c vf_yuy2.c vf_flip.c vf_rgb2bgr.c vf_rotate.c vf_mirror.c vf_palette.c vf_lavc.c vf_dvbscale.c vf_cropdetect.c vf_test.c vf_noise.c vf_yvu9.c vf_rectangle.c vf_lavcdeint.c vf_eq.c vf_eq2.c vf_halfpack.c vf_dint.c vf_1bpp.c vf_bmovl.c vf_2xsai.c vf_unsharp.c vf_swapuv.c vf_il.c vf_boxblur.c vf_sab.c vf_smartblur.c vf_perspective.c vf_down3dright.c vf_field.c vf_denoise3d.c vf_hqdn3d.c vf_detc.c vf_telecine.c vf_tfields.c vf_ivtc.c vf_ilpack.c vf_dsize.c vf_decimate.c vf_softpulldown.c vf_tinterlace.c vf_pullup.c pullup.c vf_framestep.c vf_tile.c vf_delogo.c vf_fil.c vf_hue.c vf_spp.c vf_yuvcsp.c vf_filmdint.c vf_kerndeint.c
 ifeq ($(HAVE_FFPOSTPROCESS),yes)
 VFILTER_SRCS += vf_pp.c
 endif
Index: libmpcodecs/vf.c
===================================================================
RCS file: /cvsroot/mplayer/main/libmpcodecs/vf.c,v
retrieving revision 1.98
diff -u -r1.98 vf.c
--- libmpcodecs/vf.c	19 Dec 2003 22:15:36 -0000	1.98
+++ libmpcodecs/vf.c	28 Jan 2004 23:12:58 -0000
@@ -83,6 +83,7 @@
 extern vf_info_t vf_info_hue;
 extern vf_info_t vf_info_spp;
 extern vf_info_t vf_info_yuvcsp;
+extern vf_info_t vf_info_kerndeint;
 
 // list of available filters:
 static vf_info_t* filter_list[]={
@@ -157,6 +158,7 @@
     &vf_info_spp,
 #endif
     &vf_info_yuvcsp,
+    &vf_info_kerndeint,
     NULL
 };
 
--- libmpcodecs/vf_kerndeint.c	2004-01-29 00:13:17.000000000 +0100
+++ libmpcodecs/vf_kerndeint.c	2004-01-29 00:11:33.000000000 +0100
@@ -0,0 +1,326 @@
+/*
+    Original AVISynth Filter Copyright (C) 2003 Donald A. Graft
+    Adapted to MPlayer by Tobias Diedrich
+
+    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.
+
+    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., 675 Mass Ave, Cambridge, MA 02139, USA.
+*/
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <inttypes.h>
+#include <math.h>
+
+#include "../config.h"
+#include "../mp_msg.h"
+
+#ifdef HAVE_MALLOC_H
+#include <malloc.h>
+#endif
+
+#include "img_format.h"
+#include "mp_image.h"
+#include "vf.h"
+#include "../libvo/fastmemcpy.h"
+
+//===========================================================================//
+
+struct vf_priv_s {
+	int	frame;
+	int	map;
+	int	order;
+	int	thresh;
+	int	sharp;
+	int	twoway;
+};
+
+
+/***************************************************************************/
+
+
+static int config(struct vf_instance_s* vf,
+        int width, int height, int d_width, int d_height,
+	unsigned int flags, unsigned int outfmt){
+
+	return vf_next_config(vf,width,height,d_width,d_height,flags,outfmt);
+}
+
+
+static void uninit(struct vf_instance_s* vf)
+{
+	free(vf->priv);
+}
+
+static inline int IsRGB(mp_image_t *mpi)
+{
+	return mpi->imgfmt == IMGFMT_RGB;
+}
+
+static inline int IsYUY2(mp_image_t *mpi)
+{
+	return mpi->imgfmt == IMGFMT_YUY2;
+}
+
+#define PLANAR_Y 0
+#define PLANAR_U 1
+#define PLANAR_V 2
+
+static int put_image(struct vf_instance_s* vf, mp_image_t *mpi){
+	int cw= mpi->w >> mpi->chroma_x_shift;
+	int ch= mpi->h >> mpi->chroma_y_shift;
+        int W = mpi->w, H = mpi->h;
+	const unsigned char *prvp, *prvpp, *prvpn, *prvpnn, *prvppp, *prvp4p, *prvp4n;
+	const unsigned char *srcp_saved;
+	const unsigned char *srcp, *srcpp, *srcpn, *srcpnn, *srcppp, *srcp3p, *srcp3n, *srcp4p, *srcp4n;
+	unsigned char *dstp, *dstp_saved;
+	int src_pitch;
+	int dst_pitch;
+	int x, y, z;
+	int n = vf->priv->frame++;
+	int val, hi, lo, w, h;
+	double valf;
+	unsigned int hint;
+	int plane;
+	int threshold = vf->priv->thresh;
+	int order = vf->priv->order;
+	int map = vf->priv->map;
+	int sharp = vf->priv->sharp;
+	int twoway = vf->priv->twoway;
+
+	mp_image_t *dmpi=vf_get_image(vf->next,mpi->imgfmt,
+		MP_IMGTYPE_IP, MP_IMGFLAG_ACCEPT_STRIDE,
+		mpi->w,mpi->h);
+	mp_image_t *pmpi=vf_get_image(vf->next,mpi->imgfmt,
+		MP_IMGTYPE_TEMP, MP_IMGFLAG_ACCEPT_STRIDE,
+		mpi->w,mpi->h);
+	if(!dmpi) return 0;
+
+	for (z=0; z<mpi->num_planes; z++) {
+		if (z == 0) plane = PLANAR_Y;
+		else if (z == 1) plane = PLANAR_U;
+		else plane = PLANAR_V;
+
+		h = plane == PLANAR_Y ? H : ch;
+		w = plane == PLANAR_Y ? W : cw;
+
+		srcp = srcp_saved = mpi->planes[z];
+		src_pitch = mpi->stride[z];
+		dstp = dstp_saved = dmpi->planes[z];
+		dst_pitch = dmpi->stride[z];
+		srcp = srcp_saved + (1-order) * src_pitch;
+		dstp = dstp_saved + (1-order) * dst_pitch;
+
+		for (y=0; y<h; y+=2) {
+			memcpy(dstp, srcp, w);
+			srcp += 2*src_pitch;
+			dstp += 2*dst_pitch;
+		}
+
+		// Copy through the lines that will be missed below.
+		memcpy(dstp_saved + order*dst_pitch, srcp_saved + (1-order)*src_pitch, w);
+		memcpy(dstp_saved + (2+order)*dst_pitch, srcp_saved + (3-order)*src_pitch, w);
+		memcpy(dstp_saved + (h-2+order)*dst_pitch, srcp_saved + (h-1-order)*src_pitch, w);
+		memcpy(dstp_saved + (h-4+order)*dst_pitch, srcp_saved + (h-3-order)*src_pitch, w);
+		/* For the other field choose adaptively between using the previous field
+		   or the interpolant from the current field. */
+
+		prvp = pmpi->planes[z] + 5*src_pitch - (1-order)*src_pitch;
+		prvpp = prvp - src_pitch;
+		prvppp = prvp - 2*src_pitch;
+		prvp4p = prvp - 4*src_pitch;
+		prvpn = prvp + src_pitch;
+		prvpnn = prvp + 2*src_pitch;
+		prvp4n = prvp + 4*src_pitch;
+		srcp = srcp_saved + 5*src_pitch - (1-order)*src_pitch;
+		srcpp = srcp - src_pitch;
+		srcppp = srcp - 2*src_pitch;
+		srcp3p = srcp - 3*src_pitch;
+		srcp4p = srcp - 4*src_pitch;
+		srcpn = srcp + src_pitch;
+		srcpnn = srcp + 2*src_pitch;
+		srcp3n = srcp + 3*src_pitch;
+		srcp4n = srcp + 4*src_pitch;
+		dstp =  dstp_saved  + 5*dst_pitch - (1-order)*dst_pitch;
+		for (y = 5 - (1-order); y <= h - 5 - (1-order); y+=2)
+		{
+			for (x = 0; x < w; x++)
+			{
+				if ((threshold == 0) || (n == 0) ||
+					(abs((int)prvp[x] - (int)srcp[x]) > threshold) ||
+					(abs((int)prvpp[x] - (int)srcpp[x]) > threshold) ||
+					(abs((int)prvpn[x] - (int)srcpn[x]) > threshold))
+				{
+					if (map == 1)
+					{
+						int g = x & ~3;
+						if (IsRGB(mpi) == 1)
+						{
+							dstp[g++] = 255;
+							dstp[g++] = 255;
+							dstp[g++] = 255;
+							dstp[g] = 255;
+							x = g;
+						}
+						else if (IsYUY2(mpi) == 1)
+						{
+							dstp[g++] = 235;
+							dstp[g++] = 128;
+							dstp[g++] = 235;
+							dstp[g] = 128;
+							x = g;
+						}
+						else
+						{
+							if (plane == PLANAR_Y) dstp[x] = 235;
+							else dstp[x] = 128;
+						}
+					}
+					else
+					{
+						if (IsRGB(mpi))
+						{
+							hi = 255;
+							lo = 0;
+						}
+						else if (IsYUY2(mpi))
+						{
+							hi = (x & 1) ? 240 : 235;
+							lo = 16;
+						}
+						else
+						{
+							hi = (plane == PLANAR_Y) ? 235 : 240;
+							lo = 16;
+						}
+
+						if (sharp == 1)
+						{
+							if (twoway == 1)
+								valf = + 0.526*((int)srcpp[x] + (int)srcpn[x])
+								   + 0.170*((int)srcp[x] + (int)prvp[x])
+								   - 0.116*((int)srcppp[x] + (int)srcpnn[x] + (int)prvppp[x] + (int)prvpnn[x])
+					 			   - 0.026*((int)srcp3p[x] + (int)srcp3n[x])
+								   + 0.031*((int)srcp4p[x] + (int)srcp4n[x] + (int)prvp4p[x] + (int)prvp4n[x]);
+							else
+								valf = + 0.526*((int)srcpp[x] + (int)srcpn[x])
+								   + 0.170*((int)prvp[x])
+								   - 0.116*((int)prvppp[x] + (int)prvpnn[x])
+					 			   - 0.026*((int)srcp3p[x] + (int)srcp3n[x])
+								   + 0.031*((int)prvp4p[x] + (int)prvp4p[x]);
+							if (valf > hi) valf = hi;
+							else if (valf < lo) valf = lo;
+							dstp[x] = (int) valf;
+						}
+						else
+						{
+							if (twoway == 1)
+								val = (8*((int)srcpp[x] + (int)srcpn[x]) + 2*((int)srcp[x] + (int)prvp[x]) -
+									(int)(srcppp[x]) - (int)(srcpnn[x]) -
+									(int)(prvppp[x]) - (int)(prvpnn[x])) >> 4;
+							else
+								val = (8*((int)srcpp[x] + (int)srcpn[x]) + 2*((int)prvp[x]) -
+									(int)(prvppp[x]) - (int)(prvpnn[x])) >> 4;
+							if (val > hi) val = hi;
+							else if (val < lo) val = lo;
+							dstp[x] = (int) val;
+						}
+					}
+				}
+				else
+				{
+					dstp[x] = srcp[x];
+				}
+			}
+			prvp  += 2*src_pitch;
+			prvpp  += 2*src_pitch;
+			prvppp  += 2*src_pitch;
+			prvpn  += 2*src_pitch;
+			prvpnn  += 2*src_pitch;
+			prvp4p  += 2*src_pitch;
+			prvp4n  += 2*src_pitch;
+			srcp  += 2*src_pitch;
+			srcpp += 2*src_pitch;
+			srcppp += 2*src_pitch;
+			srcp3p += 2*src_pitch;
+			srcp4p += 2*src_pitch;
+			srcpn += 2*src_pitch;
+			srcpnn += 2*src_pitch;
+			srcp3n += 2*src_pitch;
+			srcp4n += 2*src_pitch;
+			dstp  += 2*dst_pitch;
+		}
+		memcpy(pmpi->planes[z], mpi->planes[z], w*h);
+	}
+
+	return vf_next_put_image(vf,dmpi);
+}
+
+//===========================================================================//
+
+static int query_format(struct vf_instance_s* vf, unsigned int fmt){
+        switch(fmt)
+	{
+	case IMGFMT_YV12:
+	case IMGFMT_I420:
+	case IMGFMT_IYUV:
+	case IMGFMT_YVU9:
+	case IMGFMT_444P:
+	case IMGFMT_422P:
+	case IMGFMT_411P:
+		return vf_next_query_format(vf, fmt);
+	}
+	return 0;
+}
+
+static int open(vf_instance_t *vf, char* args){
+        double LumSpac, LumTmp, ChromSpac, ChromTmp;
+        double Param1, Param2, Param3;
+
+	vf->config=config;
+	vf->put_image=put_image;
+        vf->query_format=query_format;
+        vf->uninit=uninit;
+	vf->priv=malloc(sizeof(struct vf_priv_s));
+        memset(vf->priv, 0, sizeof(struct vf_priv_s));
+
+	vf->priv->frame = 0;
+
+	vf->priv->map = 0;
+	vf->priv->order = 0;
+	vf->priv->thresh = 10;
+	vf->priv->sharp = 0;
+	vf->priv->twoway = 0;
+
+        if (args)
+        {
+            sscanf(args, "%d:%d:%d:%d:%d",
+		&vf->priv->map, &vf->priv->order,
+		&vf->priv->thresh, &vf->priv->sharp,
+		&vf->priv->twoway);
+        }
+	if (vf->priv->order > 1) vf->priv->order = 1;
+
+	return 1;
+}
+
+vf_info_t vf_info_kerndeint = {
+    "Kernel Deinterlacer",
+    "kerndeint",
+    "Donald Graft",
+    "",
+    open,
+    NULL
+};
+
+//===========================================================================//
Index: DOCS/man/en/mplayer.1
===================================================================
RCS file: /cvsroot/mplayer/main/DOCS/man/en/mplayer.1,v
retrieving revision 1.521
diff -u -r1.521 mplayer.1
--- DOCS/man/en/mplayer.1	16 Jan 2004 22:27:25 -0000	1.521
+++ DOCS/man/en/mplayer.1	28 Jan 2004 23:27:36 -0000
@@ -1439,6 +1439,10 @@
 fails
 .IPs "\-ao sdl:esd"
 specify the SDL subdriver
+.IPs "\-ao pcm:file=audio.fifo:nowaveheader:realtime"
+realtime raw pcm dump to file audio.fifo
+.IPs "\-ao pcm:file=foo.wav:waveheader:norealtime"
+fast wave dump to file foo.wav
 .RE
 .PD 1
 .
@@ -3306,6 +3310,24 @@
 The option fd instructs the filter to always perform the requested
 decimation (ugly).
 .RE
+.PD 1
+.TP
+.B kerndeint[=map[:order[:thresh[:sharp[:twoway]]]]]
+Donald Graft's adaptive kernel deinterlacer.
+Deinterlaces parts of video if a configurable threshold is exceeded.
+.PD 0
+.RSs
+.IPs map (0 or 1)
+Paint pixels which exceed the threshold white (default 0).
+.IPs order (0 or 1)
+Swap fields if 1 (default 0).
+.IPs thresh (0 - 255)
+Threshold (default 10).
+.IPs sharp (0 or 1)
+Enable additional sharping (default 0).
+.IPs twoway (0 or 1)
+Enable twoway sharping (default 0).
+.RE
 .
 .
 .SH "VIDEO OUTPUT DRIVERS (MPLAYER ONLY)"


More information about the MPlayer-dev-eng mailing list