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