Mercurial > mplayer.hg
annotate libmpcodecs/vf_mcdeint.c @ 28870:ff88625be902
synced with r28895
author | Gabrov |
---|---|
date | Mon, 09 Mar 2009 12:29:42 +0000 |
parents | df67d03dde3b |
children | 290420c32921 |
rev | line source |
---|---|
18590 | 1 /* |
26727 | 2 * Copyright (C) 2006 Michael Niedermayer <michaelni@gmx.at> |
3 * | |
4 * This file is part of MPlayer. | |
5 * | |
6 * MPlayer is free software; you can redistribute it and/or modify | |
7 * it under the terms of the GNU General Public License as published by | |
8 * the Free Software Foundation; either version 2 of the License, or | |
9 * (at your option) any later version. | |
10 * | |
11 * MPlayer is distributed in the hope that it will be useful, | |
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 * GNU General Public License for more details. | |
15 * | |
16 * You should have received a copy of the GNU General Public License along | |
17 * with MPlayer; if not, write to the Free Software Foundation, Inc., | |
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | |
19 */ | |
18590 | 20 |
18592 | 21 |
22 /* | |
23 Known Issues: | |
24 * The motion estimation is somewhat at the mercy of the input, if the input | |
25 frames are created purely based on spatial interpolation then for example | |
26 a thin black line or another random and not interpolateable pattern | |
27 will cause problems | |
28 Note: completly ignoring the "unavailable" lines during motion estimation | |
29 didnt look any better, so the most obvious solution would be to improve | |
30 tfields or penalize problematic motion vectors ... | |
31 | |
32 * If non iterative ME is used then snow currently ignores the OBMC window | |
33 and as a result sometimes creates artifacts | |
34 | |
35 * only past frames are used, we should ideally use future frames too, something | |
36 like filtering the whole movie in forward and then backward direction seems | |
37 like a interresting idea but the current filter framework is FAR from | |
38 supporting such things | |
39 | |
40 * combining the motion compensated image with the input image also isnt | |
41 as trivial as it seems, simple blindly taking even lines from one and | |
42 odd ones from the other doesnt work at all as ME/MC sometimes simple | |
43 has nothing in the previous frames which matches the current, the current | |
44 algo has been found by trial and error and almost certainly can be | |
45 improved ... | |
46 */ | |
47 | |
18590 | 48 #include <stdio.h> |
49 #include <stdlib.h> | |
50 #include <string.h> | |
51 #include <inttypes.h> | |
52 #include <math.h> | |
53 | |
54 #include "config.h" | |
55 | |
56 #include "mp_msg.h" | |
57 #include "cpudetect.h" | |
58 | |
28329
ed42e982e79f
Fix compilation after DECLARE_ASM_CONST/DECLARE_ALIGNED moving within FFmpeg.
diego
parents:
26727
diff
changeset
|
59 #include "libavutil/internal.h" |
24977 | 60 #include "libavutil/intreadwrite.h" |
18590 | 61 #include "libavcodec/avcodec.h" |
62 #include "libavcodec/dsputil.h" | |
63 | |
28329
ed42e982e79f
Fix compilation after DECLARE_ASM_CONST/DECLARE_ALIGNED moving within FFmpeg.
diego
parents:
26727
diff
changeset
|
64 #undef fprintf |
ed42e982e79f
Fix compilation after DECLARE_ASM_CONST/DECLARE_ALIGNED moving within FFmpeg.
diego
parents:
26727
diff
changeset
|
65 #undef free |
ed42e982e79f
Fix compilation after DECLARE_ASM_CONST/DECLARE_ALIGNED moving within FFmpeg.
diego
parents:
26727
diff
changeset
|
66 #undef malloc |
ed42e982e79f
Fix compilation after DECLARE_ASM_CONST/DECLARE_ALIGNED moving within FFmpeg.
diego
parents:
26727
diff
changeset
|
67 |
28594
df67d03dde3b
Convert HAVE_MALLOC_H into a 0/1 definition, fixes the warning:
diego
parents:
28329
diff
changeset
|
68 #if HAVE_MALLOC_H |
18590 | 69 #include <malloc.h> |
70 #endif | |
71 | |
72 #include "img_format.h" | |
73 #include "mp_image.h" | |
74 #include "vf.h" | |
75 | |
76 #define MIN(a,b) ((a) > (b) ? (b) : (a)) | |
77 #define MAX(a,b) ((a) < (b) ? (b) : (a)) | |
78 #define ABS(a) ((a) > 0 ? (a) : (-(a))) | |
79 | |
80 //===========================================================================// | |
81 | |
82 struct vf_priv_s { | |
83 int mode; | |
84 int qp; | |
85 int parity; | |
86 #if 0 | |
87 int temp_stride[3]; | |
88 uint8_t *src[3]; | |
89 int16_t *temp[3]; | |
90 #endif | |
91 int outbuf_size; | |
92 uint8_t *outbuf; | |
93 AVCodecContext *avctx_enc; | |
94 AVFrame *frame; | |
95 AVFrame *frame_dec; | |
96 }; | |
97 | |
98 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){ | |
18657 | 99 int x, y, i; |
18590 | 100 int out_size; |
101 | |
102 for(i=0; i<3; i++){ | |
103 p->frame->data[i]= src[i]; | |
104 p->frame->linesize[i]= src_stride[i]; | |
105 } | |
106 | |
107 p->avctx_enc->me_cmp= | |
108 p->avctx_enc->me_sub_cmp= FF_CMP_SAD /*| (p->parity ? FF_CMP_ODD : FF_CMP_EVEN)*/; | |
109 p->frame->quality= p->qp*FF_QP2LAMBDA; | |
110 out_size = avcodec_encode_video(p->avctx_enc, p->outbuf, p->outbuf_size, p->frame); | |
111 p->frame_dec = p->avctx_enc->coded_frame; | |
112 | |
113 for(i=0; i<3; i++){ | |
114 int is_chroma= !!i; | |
115 int w= width >>is_chroma; | |
116 int h= height>>is_chroma; | |
117 int fils= p->frame_dec->linesize[i]; | |
118 int srcs= src_stride[i]; | |
119 | |
120 for(y=0; y<h; y++){ | |
121 if((y ^ p->parity) & 1){ | |
122 for(x=0; x<w; x++){ | |
18648
7cc939d4a9e2
filter left & right edges too (yeah the code is lame i know...)
michael
parents:
18647
diff
changeset
|
123 if((x-2)+(y-1)*w>=0 && (x+2)+(y+1)*w<w*h){ //FIXME either alloc larger images or optimize this |
18590 | 124 uint8_t *filp= &p->frame_dec->data[i][x + y*fils]; |
125 uint8_t *srcp= &src[i][x + y*srcs]; | |
18647 | 126 int diff0= filp[-fils] - srcp[-srcs]; |
127 int diff1= filp[+fils] - srcp[+srcs]; | |
18657 | 128 int spatial_score= ABS(srcp[-srcs-1] - srcp[+srcs-1]) |
129 +ABS(srcp[-srcs ] - srcp[+srcs ]) | |
130 +ABS(srcp[-srcs+1] - srcp[+srcs+1]) - 1; | |
18590 | 131 int temp= filp[0]; |
18647 | 132 |
18657 | 133 #define CHECK(j)\ |
134 { int score= ABS(srcp[-srcs-1+j] - srcp[+srcs-1-j])\ | |
135 + ABS(srcp[-srcs +j] - srcp[+srcs -j])\ | |
136 + ABS(srcp[-srcs+1+j] - srcp[+srcs+1-j]);\ | |
137 if(score < spatial_score){\ | |
138 spatial_score= score;\ | |
139 diff0= filp[-fils+j] - srcp[-srcs+j];\ | |
140 diff1= filp[+fils-j] - srcp[+srcs-j]; | |
141 | |
142 CHECK(-1) CHECK(-2) }} }} | |
143 CHECK( 1) CHECK( 2) }} }} | |
18590 | 144 #if 0 |
145 if((diff0 ^ diff1) > 0){ | |
146 int mindiff= ABS(diff0) > ABS(diff1) ? diff1 : diff0; | |
18647 | 147 temp-= mindiff; |
18590 | 148 } |
149 #elif 1 | |
150 if(diff0 + diff1 > 0) | |
18647 | 151 temp-= (diff0 + diff1 - ABS( ABS(diff0) - ABS(diff1) )/2)/2; |
18590 | 152 else |
18647 | 153 temp-= (diff0 + diff1 + ABS( ABS(diff0) - ABS(diff1) )/2)/2; |
18590 | 154 #else |
18647 | 155 temp-= (diff0 + diff1)/2; |
18590 | 156 #endif |
157 #if 1 | |
158 filp[0]= | |
159 dst[i][x + y*dst_stride[i]]= temp > 255U ? ~(temp>>31) : temp; | |
160 #else | |
161 dst[i][x + y*dst_stride[i]]= filp[0]; | |
162 filp[0]= temp > 255U ? ~(temp>>31) : temp; | |
163 #endif | |
164 }else | |
165 dst[i][x + y*dst_stride[i]]= p->frame_dec->data[i][x + y*fils]; | |
166 } | |
167 } | |
168 } | |
169 for(y=0; y<h; y++){ | |
170 if(!((y ^ p->parity) & 1)){ | |
171 for(x=0; x<w; x++){ | |
172 #if 1 | |
173 p->frame_dec->data[i][x + y*fils]= | |
174 dst[i][x + y*dst_stride[i]]= src[i][x + y*srcs]; | |
175 #else | |
176 dst[i][x + y*dst_stride[i]]= p->frame_dec->data[i][x + y*fils]; | |
177 p->frame_dec->data[i][x + y*fils]= src[i][x + y*srcs]; | |
178 #endif | |
179 } | |
180 } | |
181 } | |
182 } | |
183 p->parity ^= 1; | |
184 | |
185 } | |
186 | |
187 static int config(struct vf_instance_s* vf, | |
188 int width, int height, int d_width, int d_height, | |
189 unsigned int flags, unsigned int outfmt){ | |
190 int i; | |
191 AVCodec *enc= avcodec_find_encoder(CODEC_ID_SNOW); | |
192 | |
193 for(i=0; i<3; i++){ | |
194 AVCodecContext *avctx_enc; | |
195 #if 0 | |
196 int is_chroma= !!i; | |
197 int w= ((width + 31) & (~31))>>is_chroma; | |
198 int h= ((height + 31) & (~31))>>is_chroma; | |
199 | |
200 vf->priv->temp_stride[i]= w; | |
201 vf->priv->temp[i]= malloc(vf->priv->temp_stride[i]*h*sizeof(int16_t)); | |
202 vf->priv->src [i]= malloc(vf->priv->temp_stride[i]*h*sizeof(uint8_t)); | |
203 #endif | |
204 avctx_enc= | |
205 vf->priv->avctx_enc= avcodec_alloc_context(); | |
206 avctx_enc->width = width; | |
207 avctx_enc->height = height; | |
208 avctx_enc->time_base= (AVRational){1,25}; // meaningless | |
209 avctx_enc->gop_size = 300; | |
210 avctx_enc->max_b_frames= 0; | |
211 avctx_enc->pix_fmt = PIX_FMT_YUV420P; | |
212 avctx_enc->flags = CODEC_FLAG_QSCALE | CODEC_FLAG_LOW_DELAY; | |
213 avctx_enc->strict_std_compliance = FF_COMPLIANCE_EXPERIMENTAL; | |
214 avctx_enc->global_quality= 1; | |
215 avctx_enc->flags2= CODEC_FLAG2_MEMC_ONLY; | |
216 avctx_enc->me_cmp= | |
217 avctx_enc->me_sub_cmp= FF_CMP_SAD; //SSE; | |
218 avctx_enc->mb_cmp= FF_CMP_SSE; | |
219 | |
220 switch(vf->priv->mode){ | |
221 case 3: | |
222 avctx_enc->refs= 3; | |
223 case 2: | |
224 avctx_enc->me_method= ME_ITER; | |
225 case 1: | |
226 avctx_enc->flags |= CODEC_FLAG_4MV; | |
227 avctx_enc->dia_size=2; | |
22283
bc9e95184521
cosmetics: Fix some common typos, sepErate --> sepArate, deciSSion --> deciSion.
diego
parents:
18821
diff
changeset
|
228 // avctx_enc->mb_decision = MB_DECISION_RD; |
18590 | 229 case 0: |
230 avctx_enc->flags |= CODEC_FLAG_QPEL; | |
231 } | |
232 | |
233 avcodec_open(avctx_enc, enc); | |
234 | |
235 } | |
236 vf->priv->frame= avcodec_alloc_frame(); | |
237 | |
238 vf->priv->outbuf_size= width*height*10; | |
239 vf->priv->outbuf= malloc(vf->priv->outbuf_size); | |
240 | |
241 return vf_next_config(vf,width,height,d_width,d_height,flags,outfmt); | |
242 } | |
243 | |
244 static void get_image(struct vf_instance_s* vf, mp_image_t *mpi){ | |
245 if(mpi->flags&MP_IMGFLAG_PRESERVE) return; // don't change | |
246 return; //caused problems, dunno why | |
247 // ok, we can do pp in-place (or pp disabled): | |
248 vf->dmpi=vf_get_image(vf->next,mpi->imgfmt, | |
249 mpi->type, mpi->flags | MP_IMGFLAG_READABLE, mpi->width, mpi->height); | |
250 mpi->planes[0]=vf->dmpi->planes[0]; | |
251 mpi->stride[0]=vf->dmpi->stride[0]; | |
252 mpi->width=vf->dmpi->width; | |
253 if(mpi->flags&MP_IMGFLAG_PLANAR){ | |
254 mpi->planes[1]=vf->dmpi->planes[1]; | |
255 mpi->planes[2]=vf->dmpi->planes[2]; | |
256 mpi->stride[1]=vf->dmpi->stride[1]; | |
257 mpi->stride[2]=vf->dmpi->stride[2]; | |
258 } | |
259 mpi->flags|=MP_IMGFLAG_DIRECT; | |
260 } | |
261 | |
262 static int put_image(struct vf_instance_s* vf, mp_image_t *mpi, double pts){ | |
263 mp_image_t *dmpi; | |
264 | |
265 if(!(mpi->flags&MP_IMGFLAG_DIRECT)){ | |
266 // no DR, so get a new image! hope we'll get DR buffer: | |
267 dmpi=vf_get_image(vf->next,mpi->imgfmt, | |
268 MP_IMGTYPE_TEMP, | |
269 MP_IMGFLAG_ACCEPT_STRIDE|MP_IMGFLAG_PREFER_ALIGNED_STRIDE, | |
270 mpi->width,mpi->height); | |
271 vf_clone_mpi_attributes(dmpi, mpi); | |
272 }else{ | |
273 dmpi=vf->dmpi; | |
274 } | |
275 | |
276 filter(vf->priv, dmpi->planes, mpi->planes, dmpi->stride, mpi->stride, mpi->w, mpi->h); | |
277 | |
278 return vf_next_put_image(vf,dmpi, pts); | |
279 } | |
280 | |
281 static void uninit(struct vf_instance_s* vf){ | |
282 if(!vf->priv) return; | |
283 | |
284 #if 0 | |
285 for(i=0; i<3; i++){ | |
286 if(vf->priv->temp[i]) free(vf->priv->temp[i]); | |
287 vf->priv->temp[i]= NULL; | |
288 if(vf->priv->src[i]) free(vf->priv->src[i]); | |
289 vf->priv->src[i]= NULL; | |
290 } | |
291 #endif | |
18821 | 292 if (vf->priv->avctx_enc) { |
18752 | 293 avcodec_close(vf->priv->avctx_enc); |
18590 | 294 av_freep(&vf->priv->avctx_enc); |
18821 | 295 } |
18590 | 296 |
297 free(vf->priv->outbuf); | |
298 free(vf->priv); | |
299 vf->priv=NULL; | |
300 } | |
301 | |
302 //===========================================================================// | |
303 static int query_format(struct vf_instance_s* vf, unsigned int fmt){ | |
304 switch(fmt){ | |
305 case IMGFMT_YV12: | |
306 case IMGFMT_I420: | |
307 case IMGFMT_IYUV: | |
308 case IMGFMT_Y800: | |
309 case IMGFMT_Y8: | |
310 return vf_next_query_format(vf,fmt); | |
311 } | |
312 return 0; | |
313 } | |
314 | |
315 static int open(vf_instance_t *vf, char* args){ | |
316 | |
317 vf->config=config; | |
318 vf->put_image=put_image; | |
319 vf->get_image=get_image; | |
320 vf->query_format=query_format; | |
321 vf->uninit=uninit; | |
322 vf->priv=malloc(sizeof(struct vf_priv_s)); | |
323 memset(vf->priv, 0, sizeof(struct vf_priv_s)); | |
324 | |
325 avcodec_init(); | |
326 avcodec_register_all(); | |
327 | |
328 vf->priv->mode=0; | |
329 vf->priv->parity= -1; | |
330 vf->priv->qp=1; | |
331 | |
332 if (args) sscanf(args, "%d:%d:%d", &vf->priv->mode, &vf->priv->parity, &vf->priv->qp); | |
333 | |
334 return 1; | |
335 } | |
336 | |
25221 | 337 const vf_info_t vf_info_mcdeint = { |
18590 | 338 "motion compensating deinterlacer", |
339 "mcdeint", | |
340 "Michael Niedermayer", | |
341 "", | |
342 open, | |
343 NULL | |
344 }; |