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;
|
|
50 int stride[3];
|
18625
|
51 uint8_t *ref[4][3];
|
18608
|
52 };
|
|
53
|
|
54 static void store_ref(struct vf_priv_s *p, uint8_t *src[3], int src_stride[3], int width, int height){
|
|
55 int i;
|
|
56
|
18625
|
57 memcpy (p->ref[3], p->ref[0], sizeof(uint8_t *)*3);
|
|
58 memmove(p->ref[0], p->ref[1], sizeof(uint8_t *)*3*3);
|
18608
|
59
|
|
60 for(i=0; i<3; i++){
|
|
61 int is_chroma= !!i;
|
|
62
|
18625
|
63 memcpy_pic(p->ref[2][i], src[i], width>>is_chroma, height>>is_chroma, p->stride[i], src_stride[i]);
|
18608
|
64 }
|
|
65 }
|
|
66
|
18625
|
67 static void filter(struct vf_priv_s *p, uint8_t *dst[3], int dst_stride[3], int width, int height, int parity, int tff){
|
18657
|
68 int x, y, i;
|
18608
|
69
|
|
70 for(i=0; i<3; i++){
|
|
71 int is_chroma= !!i;
|
|
72 int w= width >>is_chroma;
|
|
73 int h= height>>is_chroma;
|
|
74 int refs= p->stride[i];
|
|
75
|
|
76 for(y=0; y<h; y++){
|
|
77 if((y ^ parity) & 1){
|
|
78 for(x=0; x<w; x++){
|
|
79 uint8_t *prev= &p->ref[0][i][x + y*refs];
|
|
80 uint8_t *cur = &p->ref[1][i][x + y*refs];
|
18625
|
81 uint8_t *next= &p->ref[2][i][x + y*refs];
|
18621
|
82 uint8_t *prev2= (tff ^ parity) ? prev : cur ;
|
|
83 uint8_t *next2= (tff ^ parity) ? cur : next;
|
18608
|
84
|
18623
|
85 int c= cur[-refs];
|
|
86 int d= (prev2[0] + next2[0])>>1;
|
|
87 int e= cur[+refs];
|
18608
|
88 int temporal_diff0= ABS(prev2[0] - next2[0]);
|
18623
|
89 int temporal_diff1=( ABS(prev[-refs] - c) + ABS(prev[+refs] - e) )>>1;
|
18625
|
90 int temporal_diff2=( ABS(next[-refs] - c) + ABS(next[+refs] - e) )>>1;
|
18610
|
91 int diff= MAX3(temporal_diff0>>1, temporal_diff1, temporal_diff2);
|
18623
|
92 int spatial_pred= (c+e)>>1;
|
|
93 int spatial_score= ABS(cur[-refs-1] - cur[+refs-1]) + ABS(c-e)
|
18657
|
94 + ABS(cur[-refs+1] - cur[+refs+1]) - 1;
|
18621
|
95
|
18657
|
96 #define CHECK(j)\
|
|
97 { int score= ABS(cur[-refs-1+j] - cur[+refs-1-j])\
|
|
98 + ABS(cur[-refs +j] - cur[+refs -j])\
|
|
99 + ABS(cur[-refs+1+j] - cur[+refs+1-j]);\
|
|
100 if(score < spatial_score){\
|
|
101 spatial_score= score;\
|
|
102 spatial_pred= (cur[-refs +j] + cur[+refs -j])>>1;\
|
18608
|
103
|
18657
|
104 CHECK(-1) CHECK(-2) }} }}
|
|
105 CHECK( 1) CHECK( 2) }} }}
|
|
106
|
18626
|
107 if(p->mode<2){
|
18625
|
108 int b= (prev2[-2*refs] + next2[-2*refs])>>1;
|
|
109 int f= (prev2[+2*refs] + next2[+2*refs])>>1;
|
18608
|
110 #if 0
|
18625
|
111 int a= cur[-3*refs];
|
|
112 int g= cur[+3*refs];
|
18608
|
113 int max= MAX3(d-e, d-c, MIN3(MAX(b-c,f-e),MAX(b-c,b-a),MAX(f-g,f-e)) );
|
|
114 int min= MIN3(d-e, d-c, MAX3(MIN(b-c,f-e),MIN(b-c,b-a),MIN(f-g,f-e)) );
|
|
115 #else
|
|
116 int max= MAX3(d-e, d-c, MIN(b-c, f-e));
|
|
117 int min= MIN3(d-e, d-c, MAX(b-c, f-e));
|
|
118 #endif
|
|
119
|
|
120 diff= MAX3(diff, min, -max);
|
|
121 }
|
|
122
|
18623
|
123 if(d < spatial_pred) d= MIN(d + diff, spatial_pred);
|
|
124 else d= MAX(d - diff, spatial_pred);
|
18608
|
125
|
18623
|
126 dst[i][x + y*dst_stride[i]]= d;
|
18608
|
127 }
|
|
128 }else{
|
18610
|
129 memcpy(&dst[i][y*dst_stride[i]], &p->ref[1][i][y*refs], w);
|
18608
|
130 }
|
|
131 }
|
|
132 }
|
|
133 }
|
|
134
|
|
135 static int config(struct vf_instance_s* vf,
|
|
136 int width, int height, int d_width, int d_height,
|
|
137 unsigned int flags, unsigned int outfmt){
|
18625
|
138 int i, j;
|
18608
|
139
|
|
140 for(i=0; i<3; i++){
|
|
141 int is_chroma= !!i;
|
18625
|
142 int w= ((width + 31) & (~31))>>is_chroma;
|
|
143 int h= ((height+6+ 31) & (~31))>>is_chroma;
|
18608
|
144
|
|
145 vf->priv->stride[i]= w;
|
18625
|
146 for(j=0; j<3; j++)
|
|
147 vf->priv->ref[j][i]= malloc(w*h*sizeof(uint8_t))+3*w;
|
18608
|
148 }
|
|
149
|
|
150 return vf_next_config(vf,width,height,d_width,d_height,flags,outfmt);
|
|
151 }
|
|
152
|
|
153 static int put_image(struct vf_instance_s* vf, mp_image_t *mpi, double pts){
|
|
154 mp_image_t *dmpi;
|
|
155 int ret=0;
|
|
156 int tff, i;
|
|
157
|
|
158 if(vf->priv->parity < 0) {
|
|
159 if (mpi->fields & MP_IMGFIELD_ORDERED)
|
|
160 tff = !!(mpi->fields & MP_IMGFIELD_TOP_FIRST);
|
|
161 else
|
|
162 tff = 1;
|
|
163 }
|
|
164 else tff = (vf->priv->parity&1)^1;
|
|
165
|
18625
|
166 store_ref(vf->priv, mpi->planes, mpi->stride, mpi->w, mpi->h);
|
|
167
|
18626
|
168 for(i=0; i<=(vf->priv->mode&1); i++){
|
18608
|
169 dmpi=vf_get_image(vf->next,mpi->imgfmt,
|
|
170 MP_IMGTYPE_TEMP,
|
|
171 MP_IMGFLAG_ACCEPT_STRIDE|MP_IMGFLAG_PREFER_ALIGNED_STRIDE,
|
|
172 mpi->width,mpi->height);
|
|
173 vf_clone_mpi_attributes(dmpi, mpi);
|
18625
|
174 filter(vf->priv, dmpi->planes, dmpi->stride, mpi->w, mpi->h, i ^ tff ^ 1, tff);
|
18608
|
175 ret |= vf_next_put_image(vf, dmpi, pts /*FIXME*/);
|
18626
|
176 if(i<(vf->priv->mode&1))
|
18608
|
177 vf_next_control(vf, VFCTRL_FLIP_PAGE, NULL);
|
|
178 }
|
|
179
|
|
180 return ret;
|
|
181 }
|
|
182
|
|
183 static void uninit(struct vf_instance_s* vf){
|
|
184 int i;
|
|
185 if(!vf->priv) return;
|
|
186
|
18625
|
187 for(i=0; i<3*3; i++){
|
|
188 uint8_t **p= &vf->priv->ref[i%3][i/3];
|
|
189 if(*p) free(*p - 3*vf->priv->stride[i/3]);
|
|
190 *p= NULL;
|
18608
|
191 }
|
|
192 free(vf->priv);
|
|
193 vf->priv=NULL;
|
|
194 }
|
|
195
|
|
196 //===========================================================================//
|
|
197 static int query_format(struct vf_instance_s* vf, unsigned int fmt){
|
|
198 switch(fmt){
|
|
199 case IMGFMT_YV12:
|
|
200 case IMGFMT_I420:
|
|
201 case IMGFMT_IYUV:
|
|
202 case IMGFMT_Y800:
|
|
203 case IMGFMT_Y8:
|
|
204 return vf_next_query_format(vf,fmt);
|
|
205 }
|
|
206 return 0;
|
|
207 }
|
|
208
|
|
209 static int open(vf_instance_t *vf, char* args){
|
|
210
|
|
211 vf->config=config;
|
|
212 vf->put_image=put_image;
|
|
213 vf->query_format=query_format;
|
|
214 vf->uninit=uninit;
|
|
215 vf->priv=malloc(sizeof(struct vf_priv_s));
|
|
216 memset(vf->priv, 0, sizeof(struct vf_priv_s));
|
|
217
|
|
218 vf->priv->mode=0;
|
|
219 vf->priv->parity= -1;
|
|
220
|
|
221 if (args) sscanf(args, "%d:%d", &vf->priv->mode, &vf->priv->parity);
|
|
222
|
|
223 return 1;
|
|
224 }
|
|
225
|
|
226 vf_info_t vf_info_yadif = {
|
|
227 "Yet Another DeInterlacing Filter",
|
|
228 "yadif",
|
|
229 "Michael Niedermayer",
|
|
230 "",
|
|
231 open,
|
|
232 NULL
|
|
233 };
|