Mercurial > mplayer.hg
annotate libmpcodecs/vf_kerndeint.c @ 12324:fa573cfa912d
vocabulary
author | rathann |
---|---|
date | Tue, 27 Apr 2004 17:01:00 +0000 |
parents | 1c268fb0503d |
children | 6ff3379a0862 |
rev | line source |
---|---|
11869 | 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; | |
11879 | 88 int psrc_pitch; |
11869 | 89 int dst_pitch; |
90 int x, y, z; | |
91 int n = vf->priv->frame++; | |
92 int val, hi, lo, w, h; | |
93 double valf; | |
94 unsigned int hint; | |
95 int plane; | |
96 int threshold = vf->priv->thresh; | |
97 int order = vf->priv->order; | |
98 int map = vf->priv->map; | |
99 int sharp = vf->priv->sharp; | |
100 int twoway = vf->priv->twoway; | |
101 | |
102 mp_image_t *dmpi=vf_get_image(vf->next,mpi->imgfmt, | |
103 MP_IMGTYPE_IP, MP_IMGFLAG_ACCEPT_STRIDE, | |
104 mpi->w,mpi->h); | |
105 mp_image_t *pmpi=vf_get_image(vf->next,mpi->imgfmt, | |
106 MP_IMGTYPE_TEMP, MP_IMGFLAG_ACCEPT_STRIDE, | |
107 mpi->w,mpi->h); | |
108 if(!dmpi) return 0; | |
109 | |
110 for (z=0; z<mpi->num_planes; z++) { | |
111 if (z == 0) plane = PLANAR_Y; | |
112 else if (z == 1) plane = PLANAR_U; | |
113 else plane = PLANAR_V; | |
114 | |
115 h = plane == PLANAR_Y ? H : ch; | |
116 w = plane == PLANAR_Y ? W : cw; | |
117 | |
118 srcp = srcp_saved = mpi->planes[z]; | |
119 src_pitch = mpi->stride[z]; | |
11879 | 120 psrc_pitch = pmpi->stride[z]; |
11869 | 121 dstp = dstp_saved = dmpi->planes[z]; |
122 dst_pitch = dmpi->stride[z]; | |
123 srcp = srcp_saved + (1-order) * src_pitch; | |
124 dstp = dstp_saved + (1-order) * dst_pitch; | |
125 | |
126 for (y=0; y<h; y+=2) { | |
127 memcpy(dstp, srcp, w); | |
128 srcp += 2*src_pitch; | |
129 dstp += 2*dst_pitch; | |
130 } | |
131 | |
132 // Copy through the lines that will be missed below. | |
133 memcpy(dstp_saved + order*dst_pitch, srcp_saved + (1-order)*src_pitch, w); | |
134 memcpy(dstp_saved + (2+order)*dst_pitch, srcp_saved + (3-order)*src_pitch, w); | |
135 memcpy(dstp_saved + (h-2+order)*dst_pitch, srcp_saved + (h-1-order)*src_pitch, w); | |
136 memcpy(dstp_saved + (h-4+order)*dst_pitch, srcp_saved + (h-3-order)*src_pitch, w); | |
137 /* For the other field choose adaptively between using the previous field | |
138 or the interpolant from the current field. */ | |
139 | |
11879 | 140 prvp = pmpi->planes[z] + 5*psrc_pitch - (1-order)*psrc_pitch; |
141 prvpp = prvp - psrc_pitch; | |
142 prvppp = prvp - 2*psrc_pitch; | |
143 prvp4p = prvp - 4*psrc_pitch; | |
144 prvpn = prvp + psrc_pitch; | |
145 prvpnn = prvp + 2*psrc_pitch; | |
146 prvp4n = prvp + 4*psrc_pitch; | |
11869 | 147 srcp = srcp_saved + 5*src_pitch - (1-order)*src_pitch; |
148 srcpp = srcp - src_pitch; | |
149 srcppp = srcp - 2*src_pitch; | |
150 srcp3p = srcp - 3*src_pitch; | |
151 srcp4p = srcp - 4*src_pitch; | |
152 srcpn = srcp + src_pitch; | |
153 srcpnn = srcp + 2*src_pitch; | |
154 srcp3n = srcp + 3*src_pitch; | |
155 srcp4n = srcp + 4*src_pitch; | |
156 dstp = dstp_saved + 5*dst_pitch - (1-order)*dst_pitch; | |
157 for (y = 5 - (1-order); y <= h - 5 - (1-order); y+=2) | |
158 { | |
159 for (x = 0; x < w; x++) | |
160 { | |
161 if ((threshold == 0) || (n == 0) || | |
162 (abs((int)prvp[x] - (int)srcp[x]) > threshold) || | |
163 (abs((int)prvpp[x] - (int)srcpp[x]) > threshold) || | |
164 (abs((int)prvpn[x] - (int)srcpn[x]) > threshold)) | |
165 { | |
166 if (map == 1) | |
167 { | |
168 int g = x & ~3; | |
169 if (IsRGB(mpi) == 1) | |
170 { | |
171 dstp[g++] = 255; | |
172 dstp[g++] = 255; | |
173 dstp[g++] = 255; | |
174 dstp[g] = 255; | |
175 x = g; | |
176 } | |
177 else if (IsYUY2(mpi) == 1) | |
178 { | |
179 dstp[g++] = 235; | |
180 dstp[g++] = 128; | |
181 dstp[g++] = 235; | |
182 dstp[g] = 128; | |
183 x = g; | |
184 } | |
185 else | |
186 { | |
187 if (plane == PLANAR_Y) dstp[x] = 235; | |
188 else dstp[x] = 128; | |
189 } | |
190 } | |
191 else | |
192 { | |
193 if (IsRGB(mpi)) | |
194 { | |
195 hi = 255; | |
196 lo = 0; | |
197 } | |
198 else if (IsYUY2(mpi)) | |
199 { | |
200 hi = (x & 1) ? 240 : 235; | |
201 lo = 16; | |
202 } | |
203 else | |
204 { | |
205 hi = (plane == PLANAR_Y) ? 235 : 240; | |
206 lo = 16; | |
207 } | |
208 | |
209 if (sharp == 1) | |
210 { | |
211 if (twoway == 1) | |
212 valf = + 0.526*((int)srcpp[x] + (int)srcpn[x]) | |
213 + 0.170*((int)srcp[x] + (int)prvp[x]) | |
214 - 0.116*((int)srcppp[x] + (int)srcpnn[x] + (int)prvppp[x] + (int)prvpnn[x]) | |
215 - 0.026*((int)srcp3p[x] + (int)srcp3n[x]) | |
216 + 0.031*((int)srcp4p[x] + (int)srcp4n[x] + (int)prvp4p[x] + (int)prvp4n[x]); | |
217 else | |
218 valf = + 0.526*((int)srcpp[x] + (int)srcpn[x]) | |
219 + 0.170*((int)prvp[x]) | |
220 - 0.116*((int)prvppp[x] + (int)prvpnn[x]) | |
221 - 0.026*((int)srcp3p[x] + (int)srcp3n[x]) | |
222 + 0.031*((int)prvp4p[x] + (int)prvp4p[x]); | |
223 if (valf > hi) valf = hi; | |
224 else if (valf < lo) valf = lo; | |
225 dstp[x] = (int) valf; | |
226 } | |
227 else | |
228 { | |
229 if (twoway == 1) | |
230 val = (8*((int)srcpp[x] + (int)srcpn[x]) + 2*((int)srcp[x] + (int)prvp[x]) - | |
231 (int)(srcppp[x]) - (int)(srcpnn[x]) - | |
232 (int)(prvppp[x]) - (int)(prvpnn[x])) >> 4; | |
233 else | |
234 val = (8*((int)srcpp[x] + (int)srcpn[x]) + 2*((int)prvp[x]) - | |
235 (int)(prvppp[x]) - (int)(prvpnn[x])) >> 4; | |
236 if (val > hi) val = hi; | |
237 else if (val < lo) val = lo; | |
238 dstp[x] = (int) val; | |
239 } | |
240 } | |
241 } | |
242 else | |
243 { | |
244 dstp[x] = srcp[x]; | |
245 } | |
246 } | |
11879 | 247 prvp += 2*psrc_pitch; |
248 prvpp += 2*psrc_pitch; | |
249 prvppp += 2*psrc_pitch; | |
250 prvpn += 2*psrc_pitch; | |
251 prvpnn += 2*psrc_pitch; | |
252 prvp4p += 2*psrc_pitch; | |
253 prvp4n += 2*psrc_pitch; | |
11869 | 254 srcp += 2*src_pitch; |
255 srcpp += 2*src_pitch; | |
256 srcppp += 2*src_pitch; | |
257 srcp3p += 2*src_pitch; | |
258 srcp4p += 2*src_pitch; | |
259 srcpn += 2*src_pitch; | |
260 srcpnn += 2*src_pitch; | |
261 srcp3n += 2*src_pitch; | |
262 srcp4n += 2*src_pitch; | |
263 dstp += 2*dst_pitch; | |
264 } | |
11879 | 265 |
266 srcp = mpi->planes[z]; | |
267 dstp = pmpi->planes[z]; | |
268 for (y=0; y<h; y++) { | |
269 memcpy(dstp, srcp, w); | |
270 srcp += src_pitch; | |
271 dstp += psrc_pitch; | |
272 } | |
11869 | 273 } |
274 | |
275 return vf_next_put_image(vf,dmpi); | |
276 } | |
277 | |
278 //===========================================================================// | |
279 | |
280 static int query_format(struct vf_instance_s* vf, unsigned int fmt){ | |
281 switch(fmt) | |
282 { | |
283 case IMGFMT_YV12: | |
11991
1c268fb0503d
A bit late, but here's the "more sane options ordering" patch.
ranma
parents:
11879
diff
changeset
|
284 case IMGFMT_RGB: |
1c268fb0503d
A bit late, but here's the "more sane options ordering" patch.
ranma
parents:
11879
diff
changeset
|
285 case IMGFMT_YUY2: |
11869 | 286 return vf_next_query_format(vf, fmt); |
287 } | |
288 return 0; | |
289 } | |
290 | |
291 static int open(vf_instance_t *vf, char* args){ | |
292 double LumSpac, LumTmp, ChromSpac, ChromTmp; | |
293 double Param1, Param2, Param3; | |
294 | |
295 vf->config=config; | |
296 vf->put_image=put_image; | |
297 vf->query_format=query_format; | |
298 vf->uninit=uninit; | |
299 vf->priv=malloc(sizeof(struct vf_priv_s)); | |
300 memset(vf->priv, 0, sizeof(struct vf_priv_s)); | |
301 | |
302 vf->priv->frame = 0; | |
303 | |
304 vf->priv->map = 0; | |
305 vf->priv->order = 0; | |
306 vf->priv->thresh = 10; | |
307 vf->priv->sharp = 0; | |
308 vf->priv->twoway = 0; | |
309 | |
310 if (args) | |
311 { | |
312 sscanf(args, "%d:%d:%d:%d:%d", | |
11991
1c268fb0503d
A bit late, but here's the "more sane options ordering" patch.
ranma
parents:
11879
diff
changeset
|
313 &vf->priv->thresh, &vf->priv->map, |
1c268fb0503d
A bit late, but here's the "more sane options ordering" patch.
ranma
parents:
11879
diff
changeset
|
314 &vf->priv->order, &vf->priv->sharp, |
11869 | 315 &vf->priv->twoway); |
316 } | |
317 if (vf->priv->order > 1) vf->priv->order = 1; | |
318 | |
319 return 1; | |
320 } | |
321 | |
322 vf_info_t vf_info_kerndeint = { | |
323 "Kernel Deinterlacer", | |
324 "kerndeint", | |
325 "Donald Graft", | |
326 "", | |
327 open, | |
328 NULL | |
329 }; | |
330 | |
331 //===========================================================================// |