Mercurial > mplayer.hg
comparison libmpcodecs/vf_kerndeint.c @ 11869:c10bd91a4eb2
kerndeint adaptive deinterlacer
author | ranma |
---|---|
date | Thu, 29 Jan 2004 09:50:17 +0000 |
parents | |
children | 205d12acbb5f |
comparison
equal
deleted
inserted
replaced
11868:711e584107af | 11869:c10bd91a4eb2 |
---|---|
1 /* | |
2 Original AVISynth Filter Copyright (C) 2003 Donald A. Graft | |
3 Adapted to MPlayer by Tobias Diedrich | |
4 | |
5 This program is free software; you can redistribute it and/or modify | |
6 it under the terms of the GNU General Public License as published by | |
7 the Free Software Foundation. | |
8 | |
9 This program is distributed in the hope that it will be useful, | |
10 but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 GNU General Public License for more details. | |
13 | |
14 You should have received a copy of the GNU General Public License | |
15 along with this program; if not, write to the Free Software | |
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | |
17 */ | |
18 | |
19 #include <stdio.h> | |
20 #include <stdlib.h> | |
21 #include <string.h> | |
22 #include <inttypes.h> | |
23 #include <math.h> | |
24 | |
25 #include "../config.h" | |
26 #include "../mp_msg.h" | |
27 | |
28 #ifdef HAVE_MALLOC_H | |
29 #include <malloc.h> | |
30 #endif | |
31 | |
32 #include "img_format.h" | |
33 #include "mp_image.h" | |
34 #include "vf.h" | |
35 #include "../libvo/fastmemcpy.h" | |
36 | |
37 //===========================================================================// | |
38 | |
39 struct vf_priv_s { | |
40 int frame; | |
41 int map; | |
42 int order; | |
43 int thresh; | |
44 int sharp; | |
45 int twoway; | |
46 }; | |
47 | |
48 | |
49 /***************************************************************************/ | |
50 | |
51 | |
52 static int config(struct vf_instance_s* vf, | |
53 int width, int height, int d_width, int d_height, | |
54 unsigned int flags, unsigned int outfmt){ | |
55 | |
56 return vf_next_config(vf,width,height,d_width,d_height,flags,outfmt); | |
57 } | |
58 | |
59 | |
60 static void uninit(struct vf_instance_s* vf) | |
61 { | |
62 free(vf->priv); | |
63 } | |
64 | |
65 static inline int IsRGB(mp_image_t *mpi) | |
66 { | |
67 return mpi->imgfmt == IMGFMT_RGB; | |
68 } | |
69 | |
70 static inline int IsYUY2(mp_image_t *mpi) | |
71 { | |
72 return mpi->imgfmt == IMGFMT_YUY2; | |
73 } | |
74 | |
75 #define PLANAR_Y 0 | |
76 #define PLANAR_U 1 | |
77 #define PLANAR_V 2 | |
78 | |
79 static int put_image(struct vf_instance_s* vf, mp_image_t *mpi){ | |
80 int cw= mpi->w >> mpi->chroma_x_shift; | |
81 int ch= mpi->h >> mpi->chroma_y_shift; | |
82 int W = mpi->w, H = mpi->h; | |
83 const unsigned char *prvp, *prvpp, *prvpn, *prvpnn, *prvppp, *prvp4p, *prvp4n; | |
84 const unsigned char *srcp_saved; | |
85 const unsigned char *srcp, *srcpp, *srcpn, *srcpnn, *srcppp, *srcp3p, *srcp3n, *srcp4p, *srcp4n; | |
86 unsigned char *dstp, *dstp_saved; | |
87 int src_pitch; | |
88 int dst_pitch; | |
89 int x, y, z; | |
90 int n = vf->priv->frame++; | |
91 int val, hi, lo, w, h; | |
92 double valf; | |
93 unsigned int hint; | |
94 int plane; | |
95 int threshold = vf->priv->thresh; | |
96 int order = vf->priv->order; | |
97 int map = vf->priv->map; | |
98 int sharp = vf->priv->sharp; | |
99 int twoway = vf->priv->twoway; | |
100 | |
101 mp_image_t *dmpi=vf_get_image(vf->next,mpi->imgfmt, | |
102 MP_IMGTYPE_IP, MP_IMGFLAG_ACCEPT_STRIDE, | |
103 mpi->w,mpi->h); | |
104 mp_image_t *pmpi=vf_get_image(vf->next,mpi->imgfmt, | |
105 MP_IMGTYPE_TEMP, MP_IMGFLAG_ACCEPT_STRIDE, | |
106 mpi->w,mpi->h); | |
107 if(!dmpi) return 0; | |
108 | |
109 for (z=0; z<mpi->num_planes; z++) { | |
110 if (z == 0) plane = PLANAR_Y; | |
111 else if (z == 1) plane = PLANAR_U; | |
112 else plane = PLANAR_V; | |
113 | |
114 h = plane == PLANAR_Y ? H : ch; | |
115 w = plane == PLANAR_Y ? W : cw; | |
116 | |
117 srcp = srcp_saved = mpi->planes[z]; | |
118 src_pitch = mpi->stride[z]; | |
119 dstp = dstp_saved = dmpi->planes[z]; | |
120 dst_pitch = dmpi->stride[z]; | |
121 srcp = srcp_saved + (1-order) * src_pitch; | |
122 dstp = dstp_saved + (1-order) * dst_pitch; | |
123 | |
124 for (y=0; y<h; y+=2) { | |
125 memcpy(dstp, srcp, w); | |
126 srcp += 2*src_pitch; | |
127 dstp += 2*dst_pitch; | |
128 } | |
129 | |
130 // Copy through the lines that will be missed below. | |
131 memcpy(dstp_saved + order*dst_pitch, srcp_saved + (1-order)*src_pitch, w); | |
132 memcpy(dstp_saved + (2+order)*dst_pitch, srcp_saved + (3-order)*src_pitch, w); | |
133 memcpy(dstp_saved + (h-2+order)*dst_pitch, srcp_saved + (h-1-order)*src_pitch, w); | |
134 memcpy(dstp_saved + (h-4+order)*dst_pitch, srcp_saved + (h-3-order)*src_pitch, w); | |
135 /* For the other field choose adaptively between using the previous field | |
136 or the interpolant from the current field. */ | |
137 | |
138 prvp = pmpi->planes[z] + 5*src_pitch - (1-order)*src_pitch; | |
139 prvpp = prvp - src_pitch; | |
140 prvppp = prvp - 2*src_pitch; | |
141 prvp4p = prvp - 4*src_pitch; | |
142 prvpn = prvp + src_pitch; | |
143 prvpnn = prvp + 2*src_pitch; | |
144 prvp4n = prvp + 4*src_pitch; | |
145 srcp = srcp_saved + 5*src_pitch - (1-order)*src_pitch; | |
146 srcpp = srcp - src_pitch; | |
147 srcppp = srcp - 2*src_pitch; | |
148 srcp3p = srcp - 3*src_pitch; | |
149 srcp4p = srcp - 4*src_pitch; | |
150 srcpn = srcp + src_pitch; | |
151 srcpnn = srcp + 2*src_pitch; | |
152 srcp3n = srcp + 3*src_pitch; | |
153 srcp4n = srcp + 4*src_pitch; | |
154 dstp = dstp_saved + 5*dst_pitch - (1-order)*dst_pitch; | |
155 for (y = 5 - (1-order); y <= h - 5 - (1-order); y+=2) | |
156 { | |
157 for (x = 0; x < w; x++) | |
158 { | |
159 if ((threshold == 0) || (n == 0) || | |
160 (abs((int)prvp[x] - (int)srcp[x]) > threshold) || | |
161 (abs((int)prvpp[x] - (int)srcpp[x]) > threshold) || | |
162 (abs((int)prvpn[x] - (int)srcpn[x]) > threshold)) | |
163 { | |
164 if (map == 1) | |
165 { | |
166 int g = x & ~3; | |
167 if (IsRGB(mpi) == 1) | |
168 { | |
169 dstp[g++] = 255; | |
170 dstp[g++] = 255; | |
171 dstp[g++] = 255; | |
172 dstp[g] = 255; | |
173 x = g; | |
174 } | |
175 else if (IsYUY2(mpi) == 1) | |
176 { | |
177 dstp[g++] = 235; | |
178 dstp[g++] = 128; | |
179 dstp[g++] = 235; | |
180 dstp[g] = 128; | |
181 x = g; | |
182 } | |
183 else | |
184 { | |
185 if (plane == PLANAR_Y) dstp[x] = 235; | |
186 else dstp[x] = 128; | |
187 } | |
188 } | |
189 else | |
190 { | |
191 if (IsRGB(mpi)) | |
192 { | |
193 hi = 255; | |
194 lo = 0; | |
195 } | |
196 else if (IsYUY2(mpi)) | |
197 { | |
198 hi = (x & 1) ? 240 : 235; | |
199 lo = 16; | |
200 } | |
201 else | |
202 { | |
203 hi = (plane == PLANAR_Y) ? 235 : 240; | |
204 lo = 16; | |
205 } | |
206 | |
207 if (sharp == 1) | |
208 { | |
209 if (twoway == 1) | |
210 valf = + 0.526*((int)srcpp[x] + (int)srcpn[x]) | |
211 + 0.170*((int)srcp[x] + (int)prvp[x]) | |
212 - 0.116*((int)srcppp[x] + (int)srcpnn[x] + (int)prvppp[x] + (int)prvpnn[x]) | |
213 - 0.026*((int)srcp3p[x] + (int)srcp3n[x]) | |
214 + 0.031*((int)srcp4p[x] + (int)srcp4n[x] + (int)prvp4p[x] + (int)prvp4n[x]); | |
215 else | |
216 valf = + 0.526*((int)srcpp[x] + (int)srcpn[x]) | |
217 + 0.170*((int)prvp[x]) | |
218 - 0.116*((int)prvppp[x] + (int)prvpnn[x]) | |
219 - 0.026*((int)srcp3p[x] + (int)srcp3n[x]) | |
220 + 0.031*((int)prvp4p[x] + (int)prvp4p[x]); | |
221 if (valf > hi) valf = hi; | |
222 else if (valf < lo) valf = lo; | |
223 dstp[x] = (int) valf; | |
224 } | |
225 else | |
226 { | |
227 if (twoway == 1) | |
228 val = (8*((int)srcpp[x] + (int)srcpn[x]) + 2*((int)srcp[x] + (int)prvp[x]) - | |
229 (int)(srcppp[x]) - (int)(srcpnn[x]) - | |
230 (int)(prvppp[x]) - (int)(prvpnn[x])) >> 4; | |
231 else | |
232 val = (8*((int)srcpp[x] + (int)srcpn[x]) + 2*((int)prvp[x]) - | |
233 (int)(prvppp[x]) - (int)(prvpnn[x])) >> 4; | |
234 if (val > hi) val = hi; | |
235 else if (val < lo) val = lo; | |
236 dstp[x] = (int) val; | |
237 } | |
238 } | |
239 } | |
240 else | |
241 { | |
242 dstp[x] = srcp[x]; | |
243 } | |
244 } | |
245 prvp += 2*src_pitch; | |
246 prvpp += 2*src_pitch; | |
247 prvppp += 2*src_pitch; | |
248 prvpn += 2*src_pitch; | |
249 prvpnn += 2*src_pitch; | |
250 prvp4p += 2*src_pitch; | |
251 prvp4n += 2*src_pitch; | |
252 srcp += 2*src_pitch; | |
253 srcpp += 2*src_pitch; | |
254 srcppp += 2*src_pitch; | |
255 srcp3p += 2*src_pitch; | |
256 srcp4p += 2*src_pitch; | |
257 srcpn += 2*src_pitch; | |
258 srcpnn += 2*src_pitch; | |
259 srcp3n += 2*src_pitch; | |
260 srcp4n += 2*src_pitch; | |
261 dstp += 2*dst_pitch; | |
262 } | |
263 memcpy(pmpi->planes[z], mpi->planes[z], w*h); | |
264 } | |
265 | |
266 return vf_next_put_image(vf,dmpi); | |
267 } | |
268 | |
269 //===========================================================================// | |
270 | |
271 static int query_format(struct vf_instance_s* vf, unsigned int fmt){ | |
272 switch(fmt) | |
273 { | |
274 case IMGFMT_YV12: | |
275 case IMGFMT_I420: | |
276 case IMGFMT_IYUV: | |
277 case IMGFMT_YVU9: | |
278 case IMGFMT_444P: | |
279 case IMGFMT_422P: | |
280 case IMGFMT_411P: | |
281 return vf_next_query_format(vf, fmt); | |
282 } | |
283 return 0; | |
284 } | |
285 | |
286 static int open(vf_instance_t *vf, char* args){ | |
287 double LumSpac, LumTmp, ChromSpac, ChromTmp; | |
288 double Param1, Param2, Param3; | |
289 | |
290 vf->config=config; | |
291 vf->put_image=put_image; | |
292 vf->query_format=query_format; | |
293 vf->uninit=uninit; | |
294 vf->priv=malloc(sizeof(struct vf_priv_s)); | |
295 memset(vf->priv, 0, sizeof(struct vf_priv_s)); | |
296 | |
297 vf->priv->frame = 0; | |
298 | |
299 vf->priv->map = 0; | |
300 vf->priv->order = 0; | |
301 vf->priv->thresh = 10; | |
302 vf->priv->sharp = 0; | |
303 vf->priv->twoway = 0; | |
304 | |
305 if (args) | |
306 { | |
307 sscanf(args, "%d:%d:%d:%d:%d", | |
308 &vf->priv->map, &vf->priv->order, | |
309 &vf->priv->thresh, &vf->priv->sharp, | |
310 &vf->priv->twoway); | |
311 } | |
312 if (vf->priv->order > 1) vf->priv->order = 1; | |
313 | |
314 return 1; | |
315 } | |
316 | |
317 vf_info_t vf_info_kerndeint = { | |
318 "Kernel Deinterlacer", | |
319 "kerndeint", | |
320 "Donald Graft", | |
321 "", | |
322 open, | |
323 NULL | |
324 }; | |
325 | |
326 //===========================================================================// |