Mercurial > mplayer.hg
annotate libmpcodecs/vf_mcdeint.c @ 37191:5b78a5e38218
DMO_AudioDecoder: Disable some debug code.
author | reimar |
---|---|
date | Sat, 27 Sep 2014 18:44:45 +0000 |
parents | 1294b0ff7e06 |
children |
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 | |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
29087
diff
changeset
|
28 Note: completly ignoring the "unavailable" lines during motion estimation |
18592 | 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 | |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
29087
diff
changeset
|
36 like filtering the whole movie in forward and then backward direction seems |
18592 | 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 "mp_msg.h" | |
55 #include "cpudetect.h" | |
56 | |
35712
d206960484fe
Add a number of missing libavutil header #includes.
diego
parents:
34718
diff
changeset
|
57 #include "libavutil/common.h" |
28329
ed42e982e79f
Fix compilation after DECLARE_ASM_CONST/DECLARE_ALIGNED moving within FFmpeg.
diego
parents:
26727
diff
changeset
|
58 #include "libavutil/internal.h" |
24977 | 59 #include "libavutil/intreadwrite.h" |
18590 | 60 #include "libavcodec/avcodec.h" |
61 | |
28329
ed42e982e79f
Fix compilation after DECLARE_ASM_CONST/DECLARE_ALIGNED moving within FFmpeg.
diego
parents:
26727
diff
changeset
|
62 #undef fprintf |
ed42e982e79f
Fix compilation after DECLARE_ASM_CONST/DECLARE_ALIGNED moving within FFmpeg.
diego
parents:
26727
diff
changeset
|
63 #undef free |
ed42e982e79f
Fix compilation after DECLARE_ASM_CONST/DECLARE_ALIGNED moving within FFmpeg.
diego
parents:
26727
diff
changeset
|
64 #undef malloc |
ed42e982e79f
Fix compilation after DECLARE_ASM_CONST/DECLARE_ALIGNED moving within FFmpeg.
diego
parents:
26727
diff
changeset
|
65 |
18590 | 66 #include "img_format.h" |
67 #include "mp_image.h" | |
68 #include "vf.h" | |
33871
30f5e5cd3676
Move code for setting up libav* logging callbacks from vd_ffmpeg to a
reimar
parents:
33343
diff
changeset
|
69 #include "av_helpers.h" |
18590 | 70 |
71 #define MIN(a,b) ((a) > (b) ? (b) : (a)) | |
72 #define MAX(a,b) ((a) < (b) ? (b) : (a)) | |
73 #define ABS(a) ((a) > 0 ? (a) : (-(a))) | |
74 | |
75 //===========================================================================// | |
76 | |
77 struct vf_priv_s { | |
78 int mode; | |
79 int qp; | |
80 int parity; | |
81 #if 0 | |
82 int temp_stride[3]; | |
83 uint8_t *src[3]; | |
84 int16_t *temp[3]; | |
85 #endif | |
86 int outbuf_size; | |
87 uint8_t *outbuf; | |
88 AVCodecContext *avctx_enc; | |
89 AVFrame *frame; | |
90 AVFrame *frame_dec; | |
91 }; | |
92 | |
93 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 | 94 int x, y, i; |
18590 | 95 |
96 for(i=0; i<3; i++){ | |
97 p->frame->data[i]= src[i]; | |
98 p->frame->linesize[i]= src_stride[i]; | |
99 } | |
100 | |
101 p->avctx_enc->me_cmp= | |
102 p->avctx_enc->me_sub_cmp= FF_CMP_SAD /*| (p->parity ? FF_CMP_ODD : FF_CMP_EVEN)*/; | |
103 p->frame->quality= p->qp*FF_QP2LAMBDA; | |
33343 | 104 avcodec_encode_video(p->avctx_enc, p->outbuf, p->outbuf_size, p->frame); |
18590 | 105 p->frame_dec = p->avctx_enc->coded_frame; |
106 | |
107 for(i=0; i<3; i++){ | |
108 int is_chroma= !!i; | |
109 int w= width >>is_chroma; | |
110 int h= height>>is_chroma; | |
111 int fils= p->frame_dec->linesize[i]; | |
112 int srcs= src_stride[i]; | |
113 | |
114 for(y=0; y<h; y++){ | |
115 if((y ^ p->parity) & 1){ | |
116 for(x=0; x<w; x++){ | |
36199 | 117 if(y>0 && y<h-1){ |
118 int is_edge= x<3 || x>w-4; | |
18590 | 119 uint8_t *filp= &p->frame_dec->data[i][x + y*fils]; |
120 uint8_t *srcp= &src[i][x + y*srcs]; | |
18647 | 121 int diff0= filp[-fils] - srcp[-srcs]; |
122 int diff1= filp[+fils] - srcp[+srcs]; | |
18590 | 123 int temp= filp[0]; |
18647 | 124 |
36199 | 125 #define DELTA(j) av_clip(j, -x, w-1-x) |
126 | |
127 #define GET_SCORE_EDGE(j)\ | |
128 ABS(srcp[-srcs+DELTA(-1+(j))] - srcp[+srcs+DELTA(-1-(j))])+\ | |
129 ABS(srcp[-srcs+DELTA(j) ] - srcp[+srcs+DELTA( -(j))])+\ | |
130 ABS(srcp[-srcs+DELTA(1+(j)) ] - srcp[+srcs+DELTA( 1-(j))]) | |
131 | |
132 #define GET_SCORE(j)\ | |
133 ABS(srcp[-srcs-1+(j)] - srcp[+srcs-1-(j)])+\ | |
134 ABS(srcp[-srcs +(j)] - srcp[+srcs -(j)])+\ | |
135 ABS(srcp[-srcs+1+(j)] - srcp[+srcs+1-(j)]) | |
136 | |
137 #define CHECK_EDGE(j)\ | |
138 { int score= GET_SCORE_EDGE(j);\ | |
139 if(score < spatial_score){\ | |
140 spatial_score= score;\ | |
141 diff0= filp[-fils+DELTA(j)] - srcp[-srcs+DELTA(j)];\ | |
142 diff1= filp[+fils+DELTA(-(j))] - srcp[+srcs+DELTA(-(j))];\ | |
143 | |
18657 | 144 #define CHECK(j)\ |
36199 | 145 { int score= GET_SCORE(j);\ |
18657 | 146 if(score < spatial_score){\ |
147 spatial_score= score;\ | |
36199 | 148 diff0= filp[-fils+(j)] - srcp[-srcs+(j)];\ |
149 diff1= filp[+fils-(j)] - srcp[+srcs-(j)];\ | |
18657 | 150 |
36199 | 151 if (is_edge) { |
152 int spatial_score= GET_SCORE_EDGE(0)-1; | |
153 CHECK_EDGE(-1) CHECK_EDGE(-2) }} }} | |
154 CHECK_EDGE( 1) CHECK_EDGE( 2) }} }} | |
155 } else { | |
156 int spatial_score= GET_SCORE(0)-1; | |
157 CHECK(-1) CHECK(-2) }} }} | |
158 CHECK( 1) CHECK( 2) }} }} | |
159 } | |
18590 | 160 #if 0 |
161 if((diff0 ^ diff1) > 0){ | |
162 int mindiff= ABS(diff0) > ABS(diff1) ? diff1 : diff0; | |
18647 | 163 temp-= mindiff; |
18590 | 164 } |
165 #elif 1 | |
166 if(diff0 + diff1 > 0) | |
18647 | 167 temp-= (diff0 + diff1 - ABS( ABS(diff0) - ABS(diff1) )/2)/2; |
18590 | 168 else |
18647 | 169 temp-= (diff0 + diff1 + ABS( ABS(diff0) - ABS(diff1) )/2)/2; |
18590 | 170 #else |
18647 | 171 temp-= (diff0 + diff1)/2; |
18590 | 172 #endif |
173 #if 1 | |
174 filp[0]= | |
175 dst[i][x + y*dst_stride[i]]= temp > 255U ? ~(temp>>31) : temp; | |
176 #else | |
177 dst[i][x + y*dst_stride[i]]= filp[0]; | |
178 filp[0]= temp > 255U ? ~(temp>>31) : temp; | |
179 #endif | |
180 }else | |
181 dst[i][x + y*dst_stride[i]]= p->frame_dec->data[i][x + y*fils]; | |
182 } | |
183 } | |
184 } | |
185 for(y=0; y<h; y++){ | |
186 if(!((y ^ p->parity) & 1)){ | |
187 for(x=0; x<w; x++){ | |
188 #if 1 | |
189 p->frame_dec->data[i][x + y*fils]= | |
190 dst[i][x + y*dst_stride[i]]= src[i][x + y*srcs]; | |
191 #else | |
192 dst[i][x + y*dst_stride[i]]= p->frame_dec->data[i][x + y*fils]; | |
193 p->frame_dec->data[i][x + y*fils]= src[i][x + y*srcs]; | |
194 #endif | |
195 } | |
196 } | |
197 } | |
198 } | |
199 p->parity ^= 1; | |
200 | |
201 } | |
202 | |
30642
a972c1a4a012
cosmetics: Rename struct vf_instance_s --> vf_instance.
diego
parents:
30638
diff
changeset
|
203 static int config(struct vf_instance *vf, |
18590 | 204 int width, int height, int d_width, int d_height, |
32702
7af3e6f901fd
Convert some tabs to whitespace to allow using MPlayer filter sourcecode in FFmpeg.
cehoyos
parents:
32537
diff
changeset
|
205 unsigned int flags, unsigned int outfmt){ |
18590 | 206 int i; |
35715
8517826b0dbd
Replace CODEC_IDs their modern AV_-prefixed counterparts.
diego
parents:
35712
diff
changeset
|
207 AVCodec *enc= avcodec_find_encoder(AV_CODEC_ID_SNOW); |
18590 | 208 |
209 for(i=0; i<3; i++){ | |
210 AVCodecContext *avctx_enc; | |
34718 | 211 AVDictionary *opts = NULL; |
18590 | 212 #if 0 |
213 int is_chroma= !!i; | |
214 int w= ((width + 31) & (~31))>>is_chroma; | |
215 int h= ((height + 31) & (~31))>>is_chroma; | |
216 | |
217 vf->priv->temp_stride[i]= w; | |
218 vf->priv->temp[i]= malloc(vf->priv->temp_stride[i]*h*sizeof(int16_t)); | |
219 vf->priv->src [i]= malloc(vf->priv->temp_stride[i]*h*sizeof(uint8_t)); | |
220 #endif | |
221 avctx_enc= | |
34543 | 222 vf->priv->avctx_enc= avcodec_alloc_context3(enc); |
18590 | 223 avctx_enc->width = width; |
224 avctx_enc->height = height; | |
225 avctx_enc->time_base= (AVRational){1,25}; // meaningless | |
226 avctx_enc->gop_size = 300; | |
227 avctx_enc->max_b_frames= 0; | |
228 avctx_enc->pix_fmt = PIX_FMT_YUV420P; | |
229 avctx_enc->flags = CODEC_FLAG_QSCALE | CODEC_FLAG_LOW_DELAY; | |
230 avctx_enc->strict_std_compliance = FF_COMPLIANCE_EXPERIMENTAL; | |
231 avctx_enc->global_quality= 1; | |
34542 | 232 av_dict_set(&opts, "memc_only", "1", 0); |
18590 | 233 avctx_enc->me_cmp= |
234 avctx_enc->me_sub_cmp= FF_CMP_SAD; //SSE; | |
235 avctx_enc->mb_cmp= FF_CMP_SSE; | |
236 | |
237 switch(vf->priv->mode){ | |
238 case 3: | |
239 avctx_enc->refs= 3; | |
240 case 2: | |
241 avctx_enc->me_method= ME_ITER; | |
242 case 1: | |
243 avctx_enc->flags |= CODEC_FLAG_4MV; | |
244 avctx_enc->dia_size=2; | |
22283
bc9e95184521
cosmetics: Fix some common typos, sepErate --> sepArate, deciSSion --> deciSion.
diego
parents:
18821
diff
changeset
|
245 // avctx_enc->mb_decision = MB_DECISION_RD; |
18590 | 246 case 0: |
247 avctx_enc->flags |= CODEC_FLAG_QPEL; | |
248 } | |
249 | |
34542 | 250 avcodec_open2(avctx_enc, enc, &opts); |
251 av_dict_free(&opts); | |
18590 | 252 |
253 } | |
254 vf->priv->frame= avcodec_alloc_frame(); | |
255 | |
256 vf->priv->outbuf_size= width*height*10; | |
257 vf->priv->outbuf= malloc(vf->priv->outbuf_size); | |
258 | |
32702
7af3e6f901fd
Convert some tabs to whitespace to allow using MPlayer filter sourcecode in FFmpeg.
cehoyos
parents:
32537
diff
changeset
|
259 return vf_next_config(vf,width,height,d_width,d_height,flags,outfmt); |
18590 | 260 } |
261 | |
30642
a972c1a4a012
cosmetics: Rename struct vf_instance_s --> vf_instance.
diego
parents:
30638
diff
changeset
|
262 static void get_image(struct vf_instance *vf, mp_image_t *mpi){ |
18590 | 263 if(mpi->flags&MP_IMGFLAG_PRESERVE) return; // don't change |
264 return; //caused problems, dunno why | |
265 // ok, we can do pp in-place (or pp disabled): | |
266 vf->dmpi=vf_get_image(vf->next,mpi->imgfmt, | |
267 mpi->type, mpi->flags | MP_IMGFLAG_READABLE, mpi->width, mpi->height); | |
268 mpi->planes[0]=vf->dmpi->planes[0]; | |
269 mpi->stride[0]=vf->dmpi->stride[0]; | |
270 mpi->width=vf->dmpi->width; | |
271 if(mpi->flags&MP_IMGFLAG_PLANAR){ | |
272 mpi->planes[1]=vf->dmpi->planes[1]; | |
273 mpi->planes[2]=vf->dmpi->planes[2]; | |
32702
7af3e6f901fd
Convert some tabs to whitespace to allow using MPlayer filter sourcecode in FFmpeg.
cehoyos
parents:
32537
diff
changeset
|
274 mpi->stride[1]=vf->dmpi->stride[1]; |
7af3e6f901fd
Convert some tabs to whitespace to allow using MPlayer filter sourcecode in FFmpeg.
cehoyos
parents:
32537
diff
changeset
|
275 mpi->stride[2]=vf->dmpi->stride[2]; |
18590 | 276 } |
277 mpi->flags|=MP_IMGFLAG_DIRECT; | |
278 } | |
279 | |
30642
a972c1a4a012
cosmetics: Rename struct vf_instance_s --> vf_instance.
diego
parents:
30638
diff
changeset
|
280 static int put_image(struct vf_instance *vf, mp_image_t *mpi, double pts){ |
18590 | 281 mp_image_t *dmpi; |
282 | |
283 if(!(mpi->flags&MP_IMGFLAG_DIRECT)){ | |
284 // no DR, so get a new image! hope we'll get DR buffer: | |
285 dmpi=vf_get_image(vf->next,mpi->imgfmt, | |
286 MP_IMGTYPE_TEMP, | |
287 MP_IMGFLAG_ACCEPT_STRIDE|MP_IMGFLAG_PREFER_ALIGNED_STRIDE, | |
288 mpi->width,mpi->height); | |
289 vf_clone_mpi_attributes(dmpi, mpi); | |
290 }else{ | |
291 dmpi=vf->dmpi; | |
292 } | |
293 | |
294 filter(vf->priv, dmpi->planes, mpi->planes, dmpi->stride, mpi->stride, mpi->w, mpi->h); | |
295 | |
296 return vf_next_put_image(vf,dmpi, pts); | |
297 } | |
298 | |
30642
a972c1a4a012
cosmetics: Rename struct vf_instance_s --> vf_instance.
diego
parents:
30638
diff
changeset
|
299 static void uninit(struct vf_instance *vf){ |
18590 | 300 if(!vf->priv) return; |
301 | |
302 #if 0 | |
303 for(i=0; i<3; i++){ | |
32537
8fa2f43cb760
Remove most of the NULL pointer check before free all over the code
cboesch
parents:
31959
diff
changeset
|
304 free(vf->priv->temp[i]); |
18590 | 305 vf->priv->temp[i]= NULL; |
32537
8fa2f43cb760
Remove most of the NULL pointer check before free all over the code
cboesch
parents:
31959
diff
changeset
|
306 free(vf->priv->src[i]); |
18590 | 307 vf->priv->src[i]= NULL; |
308 } | |
309 #endif | |
18821 | 310 if (vf->priv->avctx_enc) { |
18752 | 311 avcodec_close(vf->priv->avctx_enc); |
18590 | 312 av_freep(&vf->priv->avctx_enc); |
18821 | 313 } |
18590 | 314 |
315 free(vf->priv->outbuf); | |
316 free(vf->priv); | |
317 vf->priv=NULL; | |
318 } | |
319 | |
320 //===========================================================================// | |
30642
a972c1a4a012
cosmetics: Rename struct vf_instance_s --> vf_instance.
diego
parents:
30638
diff
changeset
|
321 static int query_format(struct vf_instance *vf, unsigned int fmt){ |
18590 | 322 switch(fmt){ |
32702
7af3e6f901fd
Convert some tabs to whitespace to allow using MPlayer filter sourcecode in FFmpeg.
cehoyos
parents:
32537
diff
changeset
|
323 case IMGFMT_YV12: |
7af3e6f901fd
Convert some tabs to whitespace to allow using MPlayer filter sourcecode in FFmpeg.
cehoyos
parents:
32537
diff
changeset
|
324 case IMGFMT_I420: |
7af3e6f901fd
Convert some tabs to whitespace to allow using MPlayer filter sourcecode in FFmpeg.
cehoyos
parents:
32537
diff
changeset
|
325 case IMGFMT_IYUV: |
7af3e6f901fd
Convert some tabs to whitespace to allow using MPlayer filter sourcecode in FFmpeg.
cehoyos
parents:
32537
diff
changeset
|
326 case IMGFMT_Y800: |
7af3e6f901fd
Convert some tabs to whitespace to allow using MPlayer filter sourcecode in FFmpeg.
cehoyos
parents:
32537
diff
changeset
|
327 case IMGFMT_Y8: |
7af3e6f901fd
Convert some tabs to whitespace to allow using MPlayer filter sourcecode in FFmpeg.
cehoyos
parents:
32537
diff
changeset
|
328 return vf_next_query_format(vf,fmt); |
18590 | 329 } |
330 return 0; | |
331 } | |
332 | |
30638
a7b908875c14
Rename open() vf initialization function to vf_open().
diego
parents:
29263
diff
changeset
|
333 static int vf_open(vf_instance_t *vf, char *args){ |
18590 | 334 |
335 vf->config=config; | |
336 vf->put_image=put_image; | |
337 vf->get_image=get_image; | |
338 vf->query_format=query_format; | |
339 vf->uninit=uninit; | |
340 vf->priv=malloc(sizeof(struct vf_priv_s)); | |
341 memset(vf->priv, 0, sizeof(struct vf_priv_s)); | |
342 | |
31959
f957f330aa6d
Introduce init_avcodec function to avoid duplicated FFmpeg initializations.
diego
parents:
30642
diff
changeset
|
343 init_avcodec(); |
18590 | 344 |
345 vf->priv->mode=0; | |
346 vf->priv->parity= -1; | |
347 vf->priv->qp=1; | |
348 | |
349 if (args) sscanf(args, "%d:%d:%d", &vf->priv->mode, &vf->priv->parity, &vf->priv->qp); | |
350 | |
351 return 1; | |
352 } | |
353 | |
25221 | 354 const vf_info_t vf_info_mcdeint = { |
18590 | 355 "motion compensating deinterlacer", |
356 "mcdeint", | |
357 "Michael Niedermayer", | |
358 "", | |
30638
a7b908875c14
Rename open() vf initialization function to vf_open().
diego
parents:
29263
diff
changeset
|
359 vf_open, |
18590 | 360 NULL |
361 }; |