2 * filter_avdeinterlace.c -- deinterlace filter
3 * Copyright (C) 2003-2004 Ushodaya Enterprises Limited
4 * Author: Charles Yates <charles.yates@pandora.be>
6 * This program 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 * This program 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
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21 #include "filter_avdeinterlace.h"
23 #include <framework/mlt_frame.h>
28 // ffmpeg Header files
36 #define DEINT_INPLACE_LINE_LUM \
37 movd_m2r(lum_m4[0],mm0);\
38 movd_m2r(lum_m3[0],mm1);\
39 movd_m2r(lum_m2[0],mm2);\
40 movd_m2r(lum_m1[0],mm3);\
41 movd_m2r(lum[0],mm4);\
42 punpcklbw_r2r(mm7,mm0);\
43 movd_r2m(mm2,lum_m4[0]);\
44 punpcklbw_r2r(mm7,mm1);\
45 punpcklbw_r2r(mm7,mm2);\
46 punpcklbw_r2r(mm7,mm3);\
47 punpcklbw_r2r(mm7,mm4);\
54 psubusw_r2r(mm0,mm1);\
56 packuswb_r2r(mm7,mm1);\
57 movd_r2m(mm1,lum_m2[0]);
59 #define DEINT_LINE_LUM \
60 movd_m2r(lum_m4[0],mm0);\
61 movd_m2r(lum_m3[0],mm1);\
62 movd_m2r(lum_m2[0],mm2);\
63 movd_m2r(lum_m1[0],mm3);\
64 movd_m2r(lum[0],mm4);\
65 punpcklbw_r2r(mm7,mm0);\
66 punpcklbw_r2r(mm7,mm1);\
67 punpcklbw_r2r(mm7,mm2);\
68 punpcklbw_r2r(mm7,mm3);\
69 punpcklbw_r2r(mm7,mm4);\
76 psubusw_r2r(mm0,mm1);\
78 packuswb_r2r(mm7,mm1);\
82 /* filter parameters: [-1 4 2 4 -1] // 8 */
83 static void deinterlace_line(uint8_t *dst,
84 const uint8_t *lum_m4, const uint8_t *lum_m3,
85 const uint8_t *lum_m2, const uint8_t *lum_m1,
90 uint8_t *cm = cropTbl + MAX_NEG_CROP;
93 for(;size > 0;size--) {
95 sum += lum_m3[0] << 2;
96 sum += lum_m2[0] << 1;
97 sum += lum_m1[0] << 2;
99 dst[0] = cm[(sum + 4) >> 3];
116 movq_m2r(rounder,mm6);
118 for (;size > 3; size-=4) {
129 static void deinterlace_line_inplace(uint8_t *lum_m4, uint8_t *lum_m3, uint8_t *lum_m2, uint8_t *lum_m1, uint8_t *lum,
133 uint8_t *cm = cropTbl + MAX_NEG_CROP;
136 for(;size > 0;size--) {
138 sum += lum_m3[0] << 2;
139 sum += lum_m2[0] << 1;
141 sum += lum_m1[0] << 2;
143 lum_m2[0] = cm[(sum + 4) >> 3];
159 movq_m2r(rounder,mm6);
161 for (;size > 3; size-=4) {
162 DEINT_INPLACE_LINE_LUM
172 /* deinterlacing : 2 temporal taps, 3 spatial taps linear filter. The
173 top field is copied as is, but the bottom field is deinterlaced
174 against the top field. */
175 static void deinterlace_bottom_field(uint8_t *dst, int dst_wrap,
176 const uint8_t *src1, int src_wrap,
177 int width, int height)
179 const uint8_t *src_m2, *src_m1, *src_0, *src_p1, *src_p2;
184 src_0=&src_m1[src_wrap];
185 src_p1=&src_0[src_wrap];
186 src_p2=&src_p1[src_wrap];
187 for(y=0;y<(height-2);y+=2) {
188 memcpy(dst,src_m1,width);
190 deinterlace_line(dst,src_m2,src_m1,src_0,src_p1,src_p2,width);
194 src_p1 += 2*src_wrap;
195 src_p2 += 2*src_wrap;
198 memcpy(dst,src_m1,width);
201 deinterlace_line(dst,src_m2,src_m1,src_0,src_0,src_0,width);
204 static void deinterlace_bottom_field_inplace(uint8_t *src1, int src_wrap,
205 int width, int height)
207 uint8_t *src_m1, *src_0, *src_p1, *src_p2;
210 buf = (uint8_t*)av_malloc(width);
213 memcpy(buf,src_m1,width);
214 src_0=&src_m1[src_wrap];
215 src_p1=&src_0[src_wrap];
216 src_p2=&src_p1[src_wrap];
217 for(y=0;y<(height-2);y+=2) {
218 deinterlace_line_inplace(buf,src_m1,src_0,src_p1,src_p2,width);
221 src_p1 += 2*src_wrap;
222 src_p2 += 2*src_wrap;
225 deinterlace_line_inplace(buf,src_m1,src_0,src_0,src_0,width);
230 /* deinterlace - if not supported return -1 */
231 static int mlt_avpicture_deinterlace(AVPicture *dst, const AVPicture *src,
232 int pix_fmt, int width, int height)
236 if (pix_fmt != PIX_FMT_YUV420P &&
237 pix_fmt != PIX_FMT_YUV422P &&
238 pix_fmt != PIX_FMT_YUV422 &&
239 pix_fmt != PIX_FMT_YUV444P &&
240 pix_fmt != PIX_FMT_YUV411P)
242 if ((width & 3) != 0 || (height & 3) != 0)
245 if ( pix_fmt != PIX_FMT_YUV422 )
250 case PIX_FMT_YUV420P:
254 case PIX_FMT_YUV422P:
257 case PIX_FMT_YUV411P:
265 deinterlace_bottom_field_inplace(dst->data[i], dst->linesize[i],
268 deinterlace_bottom_field(dst->data[i],dst->linesize[i],
269 src->data[i], src->linesize[i],
276 deinterlace_bottom_field_inplace(dst->data[0], dst->linesize[0],
279 deinterlace_bottom_field(dst->data[0],dst->linesize[0],
280 src->data[0], src->linesize[0],
294 static int filter_get_image( mlt_frame this, uint8_t **image, mlt_image_format *format, int *width, int *height, int writable )
298 // Check that we want progressive and we aren't already progressive
299 if ( *format == mlt_image_yuv422 &&
300 !mlt_properties_get_int( MLT_FRAME_PROPERTIES( this ), "progressive" ) &&
301 mlt_properties_get_int( MLT_FRAME_PROPERTIES( this ), "consumer_deinterlace" ) )
304 AVPicture *output = mlt_pool_alloc( sizeof( AVPicture ) );
306 // Get the input image
307 error = mlt_frame_get_image( this, image, format, width, height, 1 );
310 if ( *format == mlt_image_yuv422 )
312 avpicture_fill( output, *image, PIX_FMT_YUV422, *width, *height );
313 mlt_avpicture_deinterlace( output, output, PIX_FMT_YUV422, *width, *height );
317 mlt_pool_release( output );
319 // Make sure that others know the frame is deinterlaced
320 mlt_properties_set_int( MLT_FRAME_PROPERTIES( this ), "progressive", 1 );
324 // Get the input image
325 error = mlt_frame_get_image( this, image, format, width, height, writable );
331 /** Deinterlace filter processing - this should be lazy evaluation here...
334 static mlt_frame deinterlace_process( mlt_filter this, mlt_frame frame )
336 // Push the get_image method on to the stack
337 mlt_frame_push_get_image( frame, filter_get_image );
342 /** Constructor for the filter.
345 mlt_filter filter_avdeinterlace_init( void *arg )
347 mlt_filter this = mlt_filter_new( );
349 this->process = deinterlace_process;