2 * Copyright (C) 2006 Michael Niedermayer <michaelni@gmx.at>
4 * This file is part of MPlayer.
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.
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.
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.
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
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 ...
32 * If non iterative ME is used then snow currently ignores the OBMC window
33 and as a result sometimes creates artifacts
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
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
55 #include "cpudetect.h"
57 #include "libavutil/common.h"
58 #include "libavutil/internal.h"
59 #include "libavutil/intreadwrite.h"
60 #include "libavcodec/avcodec.h"
61 #include "libavcodec/dsputil.h"
67 #include "img_format.h"
70 #include "av_helpers.h"
72 #define MIN(a,b) ((a) > (b) ? (b) : (a))
73 #define MAX(a,b) ((a) < (b) ? (b) : (a))
74 #define ABS(a) ((a) > 0 ? (a) : (-(a)))
76 //===========================================================================//
89 AVCodecContext *avctx_enc;
94 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){
98 p->frame->data[i]= src[i];
99 p->frame->linesize[i]= src_stride[i];
102 p->avctx_enc->me_cmp=
103 p->avctx_enc->me_sub_cmp= FF_CMP_SAD /*| (p->parity ? FF_CMP_ODD : FF_CMP_EVEN)*/;
104 p->frame->quality= p->qp*FF_QP2LAMBDA;
105 avcodec_encode_video(p->avctx_enc, p->outbuf, p->outbuf_size, p->frame);
106 p->frame_dec = p->avctx_enc->coded_frame;
110 int w= width >>is_chroma;
111 int h= height>>is_chroma;
112 int fils= p->frame_dec->linesize[i];
113 int srcs= src_stride[i];
116 if((y ^ p->parity) & 1){
118 if((x-2)+(y-1)*w>=0 && (x+2)+(y+1)*w<w*h){ //FIXME either alloc larger images or optimize this
119 uint8_t *filp= &p->frame_dec->data[i][x + y*fils];
120 uint8_t *srcp= &src[i][x + y*srcs];
121 int diff0= filp[-fils] - srcp[-srcs];
122 int diff1= filp[+fils] - srcp[+srcs];
123 int spatial_score= ABS(srcp[-srcs-1] - srcp[+srcs-1])
124 +ABS(srcp[-srcs ] - srcp[+srcs ])
125 +ABS(srcp[-srcs+1] - srcp[+srcs+1]) - 1;
129 { int score= ABS(srcp[-srcs-1+(j)] - srcp[+srcs-1-(j)])\
130 + ABS(srcp[-srcs +(j)] - srcp[+srcs -(j)])\
131 + ABS(srcp[-srcs+1+(j)] - srcp[+srcs+1-(j)]);\
132 if(score < spatial_score){\
133 spatial_score= score;\
134 diff0= filp[-fils+(j)] - srcp[-srcs+(j)];\
135 diff1= filp[+fils-(j)] - srcp[+srcs-(j)];
137 CHECK(-1) CHECK(-2) }} }}
138 CHECK( 1) CHECK( 2) }} }}
140 if((diff0 ^ diff1) > 0){
141 int mindiff= ABS(diff0) > ABS(diff1) ? diff1 : diff0;
145 if(diff0 + diff1 > 0)
146 temp-= (diff0 + diff1 - ABS( ABS(diff0) - ABS(diff1) )/2)/2;
148 temp-= (diff0 + diff1 + ABS( ABS(diff0) - ABS(diff1) )/2)/2;
150 temp-= (diff0 + diff1)/2;
154 dst[i][x + y*dst_stride[i]]= temp > 255U ? ~(temp>>31) : temp;
156 dst[i][x + y*dst_stride[i]]= filp[0];
157 filp[0]= temp > 255U ? ~(temp>>31) : temp;
160 dst[i][x + y*dst_stride[i]]= p->frame_dec->data[i][x + y*fils];
165 if(!((y ^ p->parity) & 1)){
168 p->frame_dec->data[i][x + y*fils]=
169 dst[i][x + y*dst_stride[i]]= src[i][x + y*srcs];
171 dst[i][x + y*dst_stride[i]]= p->frame_dec->data[i][x + y*fils];
172 p->frame_dec->data[i][x + y*fils]= src[i][x + y*srcs];
182 static int config(struct vf_instance *vf,
183 int width, int height, int d_width, int d_height,
184 unsigned int flags, unsigned int outfmt){
186 AVCodec *enc= avcodec_find_encoder(AV_CODEC_ID_SNOW);
189 AVCodecContext *avctx_enc;
190 AVDictionary *opts = NULL;
193 int w= ((width + 31) & (~31))>>is_chroma;
194 int h= ((height + 31) & (~31))>>is_chroma;
196 vf->priv->temp_stride[i]= w;
197 vf->priv->temp[i]= malloc(vf->priv->temp_stride[i]*h*sizeof(int16_t));
198 vf->priv->src [i]= malloc(vf->priv->temp_stride[i]*h*sizeof(uint8_t));
201 vf->priv->avctx_enc= avcodec_alloc_context3(enc);
202 avctx_enc->width = width;
203 avctx_enc->height = height;
204 avctx_enc->time_base= (AVRational){1,25}; // meaningless
205 avctx_enc->gop_size = 300;
206 avctx_enc->max_b_frames= 0;
207 avctx_enc->pix_fmt = AV_PIX_FMT_YUV420P;
208 avctx_enc->flags = CODEC_FLAG_QSCALE | CODEC_FLAG_LOW_DELAY;
209 avctx_enc->strict_std_compliance = FF_COMPLIANCE_EXPERIMENTAL;
210 avctx_enc->global_quality= 1;
211 av_dict_set(&opts, "memc_only", "1", 0);
213 avctx_enc->me_sub_cmp= FF_CMP_SAD; //SSE;
214 avctx_enc->mb_cmp= FF_CMP_SSE;
216 switch(vf->priv->mode){
220 avctx_enc->me_method= ME_ITER;
222 avctx_enc->flags |= CODEC_FLAG_4MV;
223 avctx_enc->dia_size=2;
224 // avctx_enc->mb_decision = MB_DECISION_RD;
226 avctx_enc->flags |= CODEC_FLAG_QPEL;
229 avcodec_open2(avctx_enc, enc, &opts);
233 vf->priv->frame= avcodec_alloc_frame();
235 vf->priv->outbuf_size= width*height*10;
236 vf->priv->outbuf= malloc(vf->priv->outbuf_size);
238 return ff_vf_next_config(vf,width,height,d_width,d_height,flags,outfmt);
241 static void get_image(struct vf_instance *vf, mp_image_t *mpi){
242 if(mpi->flags&MP_IMGFLAG_PRESERVE) return; // don't change
243 return; //caused problems, dunno why
244 // ok, we can do pp in-place (or pp disabled):
245 vf->dmpi=ff_vf_get_image(vf->next,mpi->imgfmt,
246 mpi->type, mpi->flags | MP_IMGFLAG_READABLE, mpi->width, mpi->height);
247 mpi->planes[0]=vf->dmpi->planes[0];
248 mpi->stride[0]=vf->dmpi->stride[0];
249 mpi->width=vf->dmpi->width;
250 if(mpi->flags&MP_IMGFLAG_PLANAR){
251 mpi->planes[1]=vf->dmpi->planes[1];
252 mpi->planes[2]=vf->dmpi->planes[2];
253 mpi->stride[1]=vf->dmpi->stride[1];
254 mpi->stride[2]=vf->dmpi->stride[2];
256 mpi->flags|=MP_IMGFLAG_DIRECT;
259 static int put_image(struct vf_instance *vf, mp_image_t *mpi, double pts){
262 if(!(mpi->flags&MP_IMGFLAG_DIRECT)){
263 // no DR, so get a new image! hope we'll get DR buffer:
264 dmpi=ff_vf_get_image(vf->next,mpi->imgfmt,
266 MP_IMGFLAG_ACCEPT_STRIDE|MP_IMGFLAG_PREFER_ALIGNED_STRIDE,
267 mpi->width,mpi->height);
268 ff_vf_clone_mpi_attributes(dmpi, mpi);
273 filter(vf->priv, dmpi->planes, mpi->planes, dmpi->stride, mpi->stride, mpi->w, mpi->h);
275 return ff_vf_next_put_image(vf,dmpi, pts);
278 static void uninit(struct vf_instance *vf){
279 if(!vf->priv) return;
283 free(vf->priv->temp[i]);
284 vf->priv->temp[i]= NULL;
285 free(vf->priv->src[i]);
286 vf->priv->src[i]= NULL;
289 if (vf->priv->avctx_enc) {
290 avcodec_close(vf->priv->avctx_enc);
291 av_freep(&vf->priv->avctx_enc);
294 free(vf->priv->outbuf);
299 //===========================================================================//
300 static int query_format(struct vf_instance *vf, unsigned int fmt){
307 return ff_vf_next_query_format(vf,fmt);
312 static int vf_open(vf_instance_t *vf, char *args){
315 vf->put_image=put_image;
316 vf->get_image=get_image;
317 vf->query_format=query_format;
319 vf->priv=malloc(sizeof(struct vf_priv_s));
320 memset(vf->priv, 0, sizeof(struct vf_priv_s));
325 vf->priv->parity= -1;
328 if (args) sscanf(args, "%d:%d:%d", &vf->priv->mode, &vf->priv->parity, &vf->priv->qp);
333 const vf_info_t ff_vf_info_mcdeint = {
334 "motion compensating deinterlacer",
336 "Michael Niedermayer",