Mercurial > mplayer.hg
annotate libmpcodecs/vf_yadif.c @ 19829:4f4a58a3fba8
cosmetics (indentation)
author | lorenm |
---|---|
date | Thu, 14 Sep 2006 03:59:48 +0000 |
parents | 1d5832b4b204 |
children | a1a997376658 |
rev | line source |
---|---|
18608 | 1 /* |
2 Copyright (C) 2006 Michael Niedermayer <michaelni@gmx.at> | |
3 | |
4 This program is free software; you can redistribute it and/or modify | |
5 it under the terms of the GNU General Public License as published by | |
6 the Free Software Foundation; either version 2 of the License, or | |
7 (at your option) any later version. | |
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 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 | |
27 #include "mp_msg.h" | |
28 | |
29 #ifdef HAVE_MALLOC_H | |
30 #include <malloc.h> | |
31 #endif | |
32 | |
33 #include "img_format.h" | |
34 #include "mp_image.h" | |
35 #include "vf.h" | |
36 #include "libvo/fastmemcpy.h" | |
37 | |
38 #define MIN(a,b) ((a) > (b) ? (b) : (a)) | |
39 #define MAX(a,b) ((a) < (b) ? (b) : (a)) | |
40 #define ABS(a) ((a) > 0 ? (a) : (-(a))) | |
41 | |
42 #define MIN3(a,b,c) MIN(MIN(a,b),c) | |
43 #define MAX3(a,b,c) MAX(MAX(a,b),c) | |
44 | |
45 //===========================================================================// | |
46 | |
47 struct vf_priv_s { | |
48 int mode; | |
49 int parity; | |
18917
d9a75b26da6c
Add a new video pts tracking mode, enabled by option -correct-pts.
uau
parents:
18657
diff
changeset
|
50 int buffered_i; |
d9a75b26da6c
Add a new video pts tracking mode, enabled by option -correct-pts.
uau
parents:
18657
diff
changeset
|
51 int buffered_tff; |
d9a75b26da6c
Add a new video pts tracking mode, enabled by option -correct-pts.
uau
parents:
18657
diff
changeset
|
52 double buffered_pts; |
d9a75b26da6c
Add a new video pts tracking mode, enabled by option -correct-pts.
uau
parents:
18657
diff
changeset
|
53 mp_image_t *buffered_mpi; |
18608 | 54 int stride[3]; |
18625 | 55 uint8_t *ref[4][3]; |
18608 | 56 }; |
57 | |
58 static void store_ref(struct vf_priv_s *p, uint8_t *src[3], int src_stride[3], int width, int height){ | |
59 int i; | |
60 | |
18625 | 61 memcpy (p->ref[3], p->ref[0], sizeof(uint8_t *)*3); |
62 memmove(p->ref[0], p->ref[1], sizeof(uint8_t *)*3*3); | |
18608 | 63 |
64 for(i=0; i<3; i++){ | |
65 int is_chroma= !!i; | |
66 | |
18625 | 67 memcpy_pic(p->ref[2][i], src[i], width>>is_chroma, height>>is_chroma, p->stride[i], src_stride[i]); |
18608 | 68 } |
69 } | |
70 | |
19828 | 71 static void filter_line(struct vf_priv_s *p, uint8_t *dst, uint8_t *prev, uint8_t *cur, uint8_t *next, int w, int refs, int parity){ |
72 int x; | |
73 uint8_t *prev2= parity ? prev : cur ; | |
74 uint8_t *next2= parity ? cur : next; | |
19829 | 75 for(x=0; x<w; x++){ |
76 int c= cur[-refs]; | |
77 int d= (prev2[0] + next2[0])>>1; | |
78 int e= cur[+refs]; | |
79 int temporal_diff0= ABS(prev2[0] - next2[0]); | |
80 int temporal_diff1=( ABS(prev[-refs] - c) + ABS(prev[+refs] - e) )>>1; | |
81 int temporal_diff2=( ABS(next[-refs] - c) + ABS(next[+refs] - e) )>>1; | |
82 int diff= MAX3(temporal_diff0>>1, temporal_diff1, temporal_diff2); | |
83 int spatial_pred= (c+e)>>1; | |
84 int spatial_score= ABS(cur[-refs-1] - cur[+refs-1]) + ABS(c-e) | |
85 + ABS(cur[-refs+1] - cur[+refs+1]) - 1; | |
18621 | 86 |
18657 | 87 #define CHECK(j)\ |
88 { int score= ABS(cur[-refs-1+j] - cur[+refs-1-j])\ | |
89 + ABS(cur[-refs +j] - cur[+refs -j])\ | |
90 + ABS(cur[-refs+1+j] - cur[+refs+1-j]);\ | |
91 if(score < spatial_score){\ | |
92 spatial_score= score;\ | |
93 spatial_pred= (cur[-refs +j] + cur[+refs -j])>>1;\ | |
18608 | 94 |
19829 | 95 CHECK(-1) CHECK(-2) }} }} |
96 CHECK( 1) CHECK( 2) }} }} | |
18657 | 97 |
19829 | 98 if(p->mode<2){ |
99 int b= (prev2[-2*refs] + next2[-2*refs])>>1; | |
100 int f= (prev2[+2*refs] + next2[+2*refs])>>1; | |
18608 | 101 #if 0 |
19829 | 102 int a= cur[-3*refs]; |
103 int g= cur[+3*refs]; | |
104 int max= MAX3(d-e, d-c, MIN3(MAX(b-c,f-e),MAX(b-c,b-a),MAX(f-g,f-e)) ); | |
105 int min= MIN3(d-e, d-c, MAX3(MIN(b-c,f-e),MIN(b-c,b-a),MIN(f-g,f-e)) ); | |
18608 | 106 #else |
19829 | 107 int max= MAX3(d-e, d-c, MIN(b-c, f-e)); |
108 int min= MIN3(d-e, d-c, MAX(b-c, f-e)); | |
18608 | 109 #endif |
110 | |
19829 | 111 diff= MAX3(diff, min, -max); |
112 } | |
18608 | 113 |
19829 | 114 if(spatial_pred > d + diff) |
115 spatial_pred = d + diff; | |
116 else if(spatial_pred < d - diff) | |
117 spatial_pred = d - diff; | |
19828 | 118 |
19829 | 119 dst[0] = spatial_pred; |
19828 | 120 |
19829 | 121 dst++; |
122 cur++; | |
123 prev++; | |
124 next++; | |
125 prev2++; | |
126 next2++; | |
127 } | |
19828 | 128 } |
18608 | 129 |
19828 | 130 static void filter(struct vf_priv_s *p, uint8_t *dst[3], int dst_stride[3], int width, int height, int parity, int tff){ |
131 int x, y, i; | |
132 | |
133 for(i=0; i<3; i++){ | |
134 int is_chroma= !!i; | |
135 int w= width >>is_chroma; | |
136 int h= height>>is_chroma; | |
137 int refs= p->stride[i]; | |
138 | |
139 for(y=0; y<h; y++){ | |
140 if((y ^ parity) & 1){ | |
141 uint8_t *prev= &p->ref[0][i][y*refs]; | |
142 uint8_t *cur = &p->ref[1][i][y*refs]; | |
143 uint8_t *next= &p->ref[2][i][y*refs]; | |
144 uint8_t *dst2= &dst[i][y*dst_stride[i]]; | |
145 filter_line(p, dst2, prev, cur, next, w, refs, parity ^ tff); | |
18608 | 146 }else{ |
18610 | 147 memcpy(&dst[i][y*dst_stride[i]], &p->ref[1][i][y*refs], w); |
18608 | 148 } |
149 } | |
150 } | |
151 } | |
152 | |
153 static int config(struct vf_instance_s* vf, | |
154 int width, int height, int d_width, int d_height, | |
155 unsigned int flags, unsigned int outfmt){ | |
18625 | 156 int i, j; |
18608 | 157 |
158 for(i=0; i<3; i++){ | |
159 int is_chroma= !!i; | |
18625 | 160 int w= ((width + 31) & (~31))>>is_chroma; |
161 int h= ((height+6+ 31) & (~31))>>is_chroma; | |
18608 | 162 |
163 vf->priv->stride[i]= w; | |
18625 | 164 for(j=0; j<3; j++) |
165 vf->priv->ref[j][i]= malloc(w*h*sizeof(uint8_t))+3*w; | |
18608 | 166 } |
167 | |
168 return vf_next_config(vf,width,height,d_width,d_height,flags,outfmt); | |
169 } | |
170 | |
18917
d9a75b26da6c
Add a new video pts tracking mode, enabled by option -correct-pts.
uau
parents:
18657
diff
changeset
|
171 static int continue_buffered_image(struct vf_instance_s *vf); |
d9a75b26da6c
Add a new video pts tracking mode, enabled by option -correct-pts.
uau
parents:
18657
diff
changeset
|
172 extern int correct_pts; |
d9a75b26da6c
Add a new video pts tracking mode, enabled by option -correct-pts.
uau
parents:
18657
diff
changeset
|
173 |
18608 | 174 static int put_image(struct vf_instance_s* vf, mp_image_t *mpi, double pts){ |
18917
d9a75b26da6c
Add a new video pts tracking mode, enabled by option -correct-pts.
uau
parents:
18657
diff
changeset
|
175 int tff; |
18608 | 176 |
177 if(vf->priv->parity < 0) { | |
178 if (mpi->fields & MP_IMGFIELD_ORDERED) | |
179 tff = !!(mpi->fields & MP_IMGFIELD_TOP_FIRST); | |
180 else | |
181 tff = 1; | |
182 } | |
183 else tff = (vf->priv->parity&1)^1; | |
184 | |
18625 | 185 store_ref(vf->priv, mpi->planes, mpi->stride, mpi->w, mpi->h); |
186 | |
18917
d9a75b26da6c
Add a new video pts tracking mode, enabled by option -correct-pts.
uau
parents:
18657
diff
changeset
|
187 vf->priv->buffered_mpi = mpi; |
d9a75b26da6c
Add a new video pts tracking mode, enabled by option -correct-pts.
uau
parents:
18657
diff
changeset
|
188 vf->priv->buffered_tff = tff; |
d9a75b26da6c
Add a new video pts tracking mode, enabled by option -correct-pts.
uau
parents:
18657
diff
changeset
|
189 vf->priv->buffered_i = 0; |
d9a75b26da6c
Add a new video pts tracking mode, enabled by option -correct-pts.
uau
parents:
18657
diff
changeset
|
190 vf->priv->buffered_pts = pts; |
d9a75b26da6c
Add a new video pts tracking mode, enabled by option -correct-pts.
uau
parents:
18657
diff
changeset
|
191 |
d9a75b26da6c
Add a new video pts tracking mode, enabled by option -correct-pts.
uau
parents:
18657
diff
changeset
|
192 return continue_buffered_image(vf); |
d9a75b26da6c
Add a new video pts tracking mode, enabled by option -correct-pts.
uau
parents:
18657
diff
changeset
|
193 } |
d9a75b26da6c
Add a new video pts tracking mode, enabled by option -correct-pts.
uau
parents:
18657
diff
changeset
|
194 |
d9a75b26da6c
Add a new video pts tracking mode, enabled by option -correct-pts.
uau
parents:
18657
diff
changeset
|
195 static int continue_buffered_image(struct vf_instance_s *vf) |
d9a75b26da6c
Add a new video pts tracking mode, enabled by option -correct-pts.
uau
parents:
18657
diff
changeset
|
196 { |
d9a75b26da6c
Add a new video pts tracking mode, enabled by option -correct-pts.
uau
parents:
18657
diff
changeset
|
197 mp_image_t *mpi = vf->priv->buffered_mpi; |
d9a75b26da6c
Add a new video pts tracking mode, enabled by option -correct-pts.
uau
parents:
18657
diff
changeset
|
198 int tff = vf->priv->buffered_tff; |
d9a75b26da6c
Add a new video pts tracking mode, enabled by option -correct-pts.
uau
parents:
18657
diff
changeset
|
199 double pts = vf->priv->buffered_pts; |
d9a75b26da6c
Add a new video pts tracking mode, enabled by option -correct-pts.
uau
parents:
18657
diff
changeset
|
200 int i; |
d9a75b26da6c
Add a new video pts tracking mode, enabled by option -correct-pts.
uau
parents:
18657
diff
changeset
|
201 int ret=0; |
d9a75b26da6c
Add a new video pts tracking mode, enabled by option -correct-pts.
uau
parents:
18657
diff
changeset
|
202 mp_image_t *dmpi; |
d9a75b26da6c
Add a new video pts tracking mode, enabled by option -correct-pts.
uau
parents:
18657
diff
changeset
|
203 |
d9a75b26da6c
Add a new video pts tracking mode, enabled by option -correct-pts.
uau
parents:
18657
diff
changeset
|
204 pts += vf->priv->buffered_i * .02; // XXX not right |
d9a75b26da6c
Add a new video pts tracking mode, enabled by option -correct-pts.
uau
parents:
18657
diff
changeset
|
205 |
d9a75b26da6c
Add a new video pts tracking mode, enabled by option -correct-pts.
uau
parents:
18657
diff
changeset
|
206 for(i = vf->priv->buffered_i; i<=(vf->priv->mode&1); i++){ |
18608 | 207 dmpi=vf_get_image(vf->next,mpi->imgfmt, |
208 MP_IMGTYPE_TEMP, | |
209 MP_IMGFLAG_ACCEPT_STRIDE|MP_IMGFLAG_PREFER_ALIGNED_STRIDE, | |
210 mpi->width,mpi->height); | |
211 vf_clone_mpi_attributes(dmpi, mpi); | |
18625 | 212 filter(vf->priv, dmpi->planes, dmpi->stride, mpi->w, mpi->h, i ^ tff ^ 1, tff); |
18917
d9a75b26da6c
Add a new video pts tracking mode, enabled by option -correct-pts.
uau
parents:
18657
diff
changeset
|
213 if (correct_pts && i < (vf->priv->mode & 1)) |
d9a75b26da6c
Add a new video pts tracking mode, enabled by option -correct-pts.
uau
parents:
18657
diff
changeset
|
214 vf_queue_frame(vf, continue_buffered_image); |
18608 | 215 ret |= vf_next_put_image(vf, dmpi, pts /*FIXME*/); |
18917
d9a75b26da6c
Add a new video pts tracking mode, enabled by option -correct-pts.
uau
parents:
18657
diff
changeset
|
216 if (correct_pts) |
d9a75b26da6c
Add a new video pts tracking mode, enabled by option -correct-pts.
uau
parents:
18657
diff
changeset
|
217 break; |
18626 | 218 if(i<(vf->priv->mode&1)) |
18608 | 219 vf_next_control(vf, VFCTRL_FLIP_PAGE, NULL); |
220 } | |
18917
d9a75b26da6c
Add a new video pts tracking mode, enabled by option -correct-pts.
uau
parents:
18657
diff
changeset
|
221 vf->priv->buffered_i = 1; |
18608 | 222 return ret; |
223 } | |
224 | |
225 static void uninit(struct vf_instance_s* vf){ | |
226 int i; | |
227 if(!vf->priv) return; | |
228 | |
18625 | 229 for(i=0; i<3*3; i++){ |
230 uint8_t **p= &vf->priv->ref[i%3][i/3]; | |
231 if(*p) free(*p - 3*vf->priv->stride[i/3]); | |
232 *p= NULL; | |
18608 | 233 } |
234 free(vf->priv); | |
235 vf->priv=NULL; | |
236 } | |
237 | |
238 //===========================================================================// | |
239 static int query_format(struct vf_instance_s* vf, unsigned int fmt){ | |
240 switch(fmt){ | |
241 case IMGFMT_YV12: | |
242 case IMGFMT_I420: | |
243 case IMGFMT_IYUV: | |
244 case IMGFMT_Y800: | |
245 case IMGFMT_Y8: | |
246 return vf_next_query_format(vf,fmt); | |
247 } | |
248 return 0; | |
249 } | |
250 | |
251 static int open(vf_instance_t *vf, char* args){ | |
252 | |
253 vf->config=config; | |
254 vf->put_image=put_image; | |
255 vf->query_format=query_format; | |
256 vf->uninit=uninit; | |
257 vf->priv=malloc(sizeof(struct vf_priv_s)); | |
258 memset(vf->priv, 0, sizeof(struct vf_priv_s)); | |
259 | |
260 vf->priv->mode=0; | |
261 vf->priv->parity= -1; | |
262 | |
263 if (args) sscanf(args, "%d:%d", &vf->priv->mode, &vf->priv->parity); | |
264 | |
265 return 1; | |
266 } | |
267 | |
268 vf_info_t vf_info_yadif = { | |
269 "Yet Another DeInterlacing Filter", | |
270 "yadif", | |
271 "Michael Niedermayer", | |
272 "", | |
273 open, | |
274 NULL | |
275 }; |