summaryrefslogtreecommitdiffstats
path: root/libmpcodecs/vf_kerndeint.c
blob: c6fb389f3a0844ee576f4f3333c13eb6e0363853 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
/*
 * Original AVISynth Filter Copyright (C) 2003 Donald A. Graft
 *  Adapted to MPlayer by Tobias Diedrich
 *
 * This file is part of MPlayer.
 *
 * MPlayer 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.
 *
 * MPlayer 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 MPlayer; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <inttypes.h>
#include <math.h>

#include "mp_msg.h"
#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;
	int	do_deinterlace;
};


/***************************************************************************/


static int config(struct vf_instance *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 *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 *vf, mp_image_t *mpi, double pts){
	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 psrc_pitch;
	int dst_pitch;
	int x, y, z;
	int n = vf->priv->frame++;
	int val, hi, lo, w, h;
	double valf;
	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, *pmpi;

	if(!vf->priv->do_deinterlace)
		return vf_next_put_image(vf, mpi, pts);

	dmpi=vf_get_image(vf->next,mpi->imgfmt,
		MP_IMGTYPE_IP, MP_IMGFLAG_ACCEPT_STRIDE,
		mpi->w,mpi->h);
	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];
		psrc_pitch = pmpi->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) {
			fast_memcpy(dstp, srcp, w);
			srcp += 2*src_pitch;
			dstp += 2*dst_pitch;
		}

		// Copy through the lines that will be missed below.
		fast_memcpy(dstp_saved + order*dst_pitch, srcp_saved + (1-order)*src_pitch, w);
		fast_memcpy(dstp_saved + (2+order)*dst_pitch, srcp_saved + (3-order)*src_pitch, w);
		fast_memcpy(dstp_saved + (h-2+order)*dst_pitch, srcp_saved + (h-1-order)*src_pitch, w);
		fast_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*psrc_pitch - (1-order)*psrc_pitch;
		prvpp = prvp - psrc_pitch;
		prvppp = prvp - 2*psrc_pitch;
		prvp4p = prvp - 4*psrc_pitch;
		prvpn = prvp + psrc_pitch;
		prvpnn = prvp + 2*psrc_pitch;
		prvp4n = prvp + 4*psrc_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*psrc_pitch;
			prvpp  += 2*psrc_pitch;
			prvppp  += 2*psrc_pitch;
			prvpn  += 2*psrc_pitch;
			prvpnn  += 2*psrc_pitch;
			prvp4p  += 2*psrc_pitch;
			prvp4n  += 2*psrc_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;
		}

		srcp = mpi->planes[z];
		dstp = pmpi->planes[z];
		for (y=0; y<h; y++) {
			fast_memcpy(dstp, srcp, w);
			srcp += src_pitch;
			dstp += psrc_pitch;
		}
	}

	return vf_next_put_image(vf,dmpi, pts);
}

//===========================================================================//

static int query_format(struct vf_instance *vf, unsigned int fmt){
        switch(fmt)
	{
	case IMGFMT_YV12:
	case IMGFMT_RGB:
	case IMGFMT_YUY2:
		return vf_next_query_format(vf, fmt);
	}
	return 0;
}

static int control(struct vf_instance *vf, int request, void* data){
	switch (request)
	{
	case VFCTRL_GET_DEINTERLACE:
		*(int*)data = vf->priv->do_deinterlace;
		return CONTROL_OK;
	case VFCTRL_SET_DEINTERLACE:
		vf->priv->do_deinterlace = *(int*)data;
		return CONTROL_OK;
	}
	return vf_next_control (vf, request, data);
}

static int vf_open(vf_instance_t *vf, char *args){

	vf->control=control;
	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;
	vf->priv->do_deinterlace=1;

        if (args)
        {
            sscanf(args, "%d:%d:%d:%d:%d",
		&vf->priv->thresh, &vf->priv->map,
		&vf->priv->order, &vf->priv->sharp,
		&vf->priv->twoway);
        }
	if (vf->priv->order > 1) vf->priv->order = 1;

	return 1;
}

const vf_info_t vf_info_kerndeint = {
    "Kernel Deinterlacer",
    "kerndeint",
    "Donald Graft",
    "",
    vf_open,
    NULL
};

//===========================================================================//