comparison libmpcodecs/vf_yadif.c @ 18608:a80c7de8a4ba

yet another deinterlacing filter
author michael
date Tue, 06 Jun 2006 23:30:23 +0000
parents
children e0df832235c6
comparison
equal deleted inserted replaced
18607:d526762d3979 18608:a80c7de8a4ba
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){
68 int x, y, i, j, k;
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];
86 uint8_t *prev2= (tff ^ (y&1)) ? cur : prev;
87 uint8_t *next2= (tff ^ (y&1)) ? next : cur ;
88 int next2s= (tff ^ (y&1)) ? srcs : refs;
89
90 int temporal_diff0= ABS(prev2[0] - next2[0]);
91 int temporal_diff1=( ABS(prev[-refs] - cur[-refs]) + ABS(prev[+refs] - cur[+refs]) )>>1;
92 int temporal_diff2=( ABS(next[-srcs] - cur[-refs]) + ABS(next[+srcs] - cur[+refs]) )>>1;
93 int diff= MAX(temporal_diff0>>1, MAX(temporal_diff1, temporal_diff2));
94 int temporal_pred= (prev2[0] + next2[0])>>1;
95 int spatial_pred= 0;
96 int spatial_score= 1<<30;
97 int v= temporal_pred;
98 #if 0
99 #define RANGE 8
100 for(j=-RANGE; j<=RANGE; j++){
101 int score= 0;
102 for(k=-ABS(j)-1; k<=ABS(j)+1; k++)
103 score+= ABS(cur[-refs+k+j] - cur[+refs+k-j]);
104 #else
105 #define RANGE 1
106 for(j=-RANGE; j<=RANGE; j++){
107 int score= ABS(cur[-refs-1+j] - cur[+refs-1-j])
108 + ABS(cur[-refs +j] - cur[+refs -j])
109 + ABS(cur[-refs+1+j] - cur[+refs+1-j])
110 + ABS(j);
111 #endif
112
113 if(score < spatial_score){
114 spatial_score= score;
115 spatial_pred= (cur[-refs +j] + cur[+refs -j])>>1;
116 }
117 }
118 if(y>1 && y+2<h){
119 int b= (prev2[-2*refs] + next2[-2*next2s])>>1;
120 int c= cur[-refs];
121 int d= temporal_pred;
122 int e= cur[+refs];
123 int f= (prev2[+2*refs] + next2[+2*next2s])>>1;
124 #if 0
125 int a= y>2 ? cur[-3*refs] : 0;
126 int g= y+3<h ? cur[+3*refs] : 0;
127 int max= MAX3(d-e, d-c, MIN3(MAX(b-c,f-e),MAX(b-c,b-a),MAX(f-g,f-e)) );
128 int min= MIN3(d-e, d-c, MAX3(MIN(b-c,f-e),MIN(b-c,b-a),MIN(f-g,f-e)) );
129 #else
130 int max= MAX3(d-e, d-c, MIN(b-c, f-e));
131 int min= MIN3(d-e, d-c, MAX(b-c, f-e));
132 #endif
133
134 diff= MAX3(diff, min, -max);
135 }
136
137 if(v < spatial_pred) v= MIN(v + diff, spatial_pred);
138 else v= MAX(v - diff, spatial_pred);
139
140 dst[i][x + y*dst_stride[i]]= v;
141 }else
142 dst[i][x + y*dst_stride[i]]= p->ref[1][i][x + y*refs];
143 }
144 }else{
145 for(x=0; x<w; x++){
146 dst[i][x + y*dst_stride[i]]= p->ref[1][i][x + y*refs];
147 }
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){
156 int i;
157
158 for(i=0; i<3; i++){
159 int is_chroma= !!i;
160 int w= ((width + 31) & (~31))>>is_chroma;
161 int h= ((height + 31) & (~31))>>is_chroma;
162
163 vf->priv->stride[i]= w;
164 vf->priv->ref[0][i]= malloc(w*h*sizeof(uint8_t));
165 vf->priv->ref[1][i]= malloc(w*h*sizeof(uint8_t));
166 }
167
168 return vf_next_config(vf,width,height,d_width,d_height,flags,outfmt);
169 }
170
171 static int put_image(struct vf_instance_s* vf, mp_image_t *mpi, double pts){
172 mp_image_t *dmpi;
173 int ret=0;
174 int tff, i;
175
176 if(vf->priv->parity < 0) {
177 if (mpi->fields & MP_IMGFIELD_ORDERED)
178 tff = !!(mpi->fields & MP_IMGFIELD_TOP_FIRST);
179 else
180 tff = 1;
181 }
182 else tff = (vf->priv->parity&1)^1;
183
184 for(i=0; i<=vf->priv->mode; i++){
185 dmpi=vf_get_image(vf->next,mpi->imgfmt,
186 MP_IMGTYPE_TEMP,
187 MP_IMGFLAG_ACCEPT_STRIDE|MP_IMGFLAG_PREFER_ALIGNED_STRIDE,
188 mpi->width,mpi->height);
189 vf_clone_mpi_attributes(dmpi, mpi);
190 filter(vf->priv, dmpi->planes, mpi->planes, dmpi->stride, mpi->stride, mpi->w, mpi->h, i ^ tff ^ 1, tff);
191 ret |= vf_next_put_image(vf, dmpi, pts /*FIXME*/);
192 if(i<vf->priv->mode)
193 vf_next_control(vf, VFCTRL_FLIP_PAGE, NULL);
194 }
195 store_ref(vf->priv, mpi->planes, mpi->stride, mpi->w, mpi->h);
196
197 return ret;
198 }
199
200 static void uninit(struct vf_instance_s* vf){
201 int i;
202 if(!vf->priv) return;
203
204 for(i=0; i<3*2; i++){
205 if(vf->priv->ref[i&1][i/2]) free(vf->priv->ref[i&1][i/2]);
206 vf->priv->ref[i&1][i/2]= NULL;
207 }
208 free(vf->priv);
209 vf->priv=NULL;
210 }
211
212 //===========================================================================//
213 static int query_format(struct vf_instance_s* vf, unsigned int fmt){
214 switch(fmt){
215 case IMGFMT_YV12:
216 case IMGFMT_I420:
217 case IMGFMT_IYUV:
218 case IMGFMT_Y800:
219 case IMGFMT_Y8:
220 return vf_next_query_format(vf,fmt);
221 }
222 return 0;
223 }
224
225 static int open(vf_instance_t *vf, char* args){
226
227 vf->config=config;
228 vf->put_image=put_image;
229 vf->query_format=query_format;
230 vf->uninit=uninit;
231 vf->priv=malloc(sizeof(struct vf_priv_s));
232 memset(vf->priv, 0, sizeof(struct vf_priv_s));
233
234 vf->priv->mode=0;
235 vf->priv->parity= -1;
236
237 if (args) sscanf(args, "%d:%d", &vf->priv->mode, &vf->priv->parity);
238
239 if(vf->priv->mode < 0 || vf->priv->mode > 1)
240 vf->priv->mode=0;
241
242 return 1;
243 }
244
245 vf_info_t vf_info_yadif = {
246 "Yet Another DeInterlacing Filter",
247 "yadif",
248 "Michael Niedermayer",
249 "",
250 open,
251 NULL
252 };