2 * filter_avdeinterlace.c -- deinterlace filter
3 * Copyright (C) 2003-2004 Ushodaya Enterprises Limited
4 * Author: Charles Yates <charles.yates@pandora.be>
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library 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 GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #include <framework/mlt_filter.h>
22 #include <framework/mlt_frame.h>
27 // ffmpeg Header files
35 #define DEINT_INPLACE_LINE_LUM \
36 movd_m2r(lum_m4[0],mm0);\
37 movd_m2r(lum_m3[0],mm1);\
38 movd_m2r(lum_m2[0],mm2);\
39 movd_m2r(lum_m1[0],mm3);\
40 movd_m2r(lum[0],mm4);\
41 punpcklbw_r2r(mm7,mm0);\
42 movd_r2m(mm2,lum_m4[0]);\
43 punpcklbw_r2r(mm7,mm1);\
44 punpcklbw_r2r(mm7,mm2);\
45 punpcklbw_r2r(mm7,mm3);\
46 punpcklbw_r2r(mm7,mm4);\
53 psubusw_r2r(mm0,mm1);\
55 packuswb_r2r(mm7,mm1);\
56 movd_r2m(mm1,lum_m2[0]);
58 #define DEINT_LINE_LUM \
59 movd_m2r(lum_m4[0],mm0);\
60 movd_m2r(lum_m3[0],mm1);\
61 movd_m2r(lum_m2[0],mm2);\
62 movd_m2r(lum_m1[0],mm3);\
63 movd_m2r(lum[0],mm4);\
64 punpcklbw_r2r(mm7,mm0);\
65 punpcklbw_r2r(mm7,mm1);\
66 punpcklbw_r2r(mm7,mm2);\
67 punpcklbw_r2r(mm7,mm3);\
68 punpcklbw_r2r(mm7,mm4);\
75 psubusw_r2r(mm0,mm1);\
77 packuswb_r2r(mm7,mm1);\
81 /* filter parameters: [-1 4 2 4 -1] // 8 */
82 static inline void deinterlace_line(uint8_t *dst,
83 const uint8_t *lum_m4, const uint8_t *lum_m3,
84 const uint8_t *lum_m2, const uint8_t *lum_m1,
89 uint8_t *cm = cropTbl + MAX_NEG_CROP;
92 for(;size > 0;size--) {
94 sum += lum_m3[0] << 2;
95 sum += lum_m2[0] << 1;
96 sum += lum_m1[0] << 2;
98 dst[0] = cm[(sum + 4) >> 3];
115 movq_m2r(rounder,mm6);
117 for (;size > 3; size-=4) {
128 static inline void deinterlace_line_inplace(uint8_t *lum_m4, uint8_t *lum_m3, uint8_t *lum_m2, uint8_t *lum_m1, uint8_t *lum,
132 uint8_t *cm = cropTbl + MAX_NEG_CROP;
135 for(;size > 0;size--) {
137 sum += lum_m3[0] << 2;
138 sum += lum_m2[0] << 1;
140 sum += lum_m1[0] << 2;
142 lum_m2[0] = cm[(sum + 4) >> 3];
158 movq_m2r(rounder,mm6);
160 for (;size > 3; size-=4) {
161 DEINT_INPLACE_LINE_LUM
171 /* deinterlacing : 2 temporal taps, 3 spatial taps linear filter. The
172 top field is copied as is, but the bottom field is deinterlaced
173 against the top field. */
174 static inline void deinterlace_bottom_field(uint8_t *dst, int dst_wrap,
175 const uint8_t *src1, int src_wrap,
176 int width, int height)
178 const uint8_t *src_m2, *src_m1, *src_0, *src_p1, *src_p2;
183 src_0=&src_m1[src_wrap];
184 src_p1=&src_0[src_wrap];
185 src_p2=&src_p1[src_wrap];
186 for(y=0;y<(height-2);y+=2) {
187 memcpy(dst,src_m1,width);
189 deinterlace_line(dst,src_m2,src_m1,src_0,src_p1,src_p2,width);
193 src_p1 += 2*src_wrap;
194 src_p2 += 2*src_wrap;
197 memcpy(dst,src_m1,width);
200 deinterlace_line(dst,src_m2,src_m1,src_0,src_0,src_0,width);
203 static inline void deinterlace_bottom_field_inplace(uint8_t *src1, int src_wrap,
204 int width, int height)
206 uint8_t *src_m1, *src_0, *src_p1, *src_p2;
209 buf = (uint8_t*)av_malloc(width);
212 memcpy(buf,src_m1,width);
213 src_0=&src_m1[src_wrap];
214 src_p1=&src_0[src_wrap];
215 src_p2=&src_p1[src_wrap];
216 for(y=0;y<(height-2);y+=2) {
217 deinterlace_line_inplace(buf,src_m1,src_0,src_p1,src_p2,width);
220 src_p1 += 2*src_wrap;
221 src_p2 += 2*src_wrap;
224 deinterlace_line_inplace(buf,src_m1,src_0,src_0,src_0,width);
229 /* deinterlace - if not supported return -1 */
230 static int mlt_avpicture_deinterlace(AVPicture *dst, const AVPicture *src,
231 int pix_fmt, int width, int height)
235 if (pix_fmt != PIX_FMT_YUV420P &&
236 pix_fmt != PIX_FMT_YUV422P &&
237 pix_fmt != PIX_FMT_YUV422 &&
238 pix_fmt != PIX_FMT_YUV444P &&
239 pix_fmt != PIX_FMT_YUV411P)
241 if ((width & 3) != 0 || (height & 3) != 0)
244 if ( pix_fmt != PIX_FMT_YUV422 )
249 case PIX_FMT_YUV420P:
253 case PIX_FMT_YUV422P:
256 case PIX_FMT_YUV411P:
264 deinterlace_bottom_field_inplace(dst->data[i], dst->linesize[i],
267 deinterlace_bottom_field(dst->data[i],dst->linesize[i],
268 src->data[i], src->linesize[i],
275 deinterlace_bottom_field_inplace(dst->data[0], dst->linesize[0],
278 deinterlace_bottom_field(dst->data[0],dst->linesize[0],
279 src->data[0], src->linesize[0],
293 static int filter_get_image( mlt_frame this, uint8_t **image, mlt_image_format *format, int *width, int *height, int writable )
296 int deinterlace = mlt_properties_get_int( MLT_FRAME_PROPERTIES( this ), "consumer_deinterlace" );
298 // Determine if we need a writable version or not
299 if ( deinterlace && !writable )
300 writable = !mlt_properties_get_int( MLT_FRAME_PROPERTIES( this ), "progressive" );
302 // Get the input image
303 error = mlt_frame_get_image( this, image, format, width, height, writable );
305 // Check that we want progressive and we aren't already progressive
306 if ( deinterlace && *format == mlt_image_yuv422 && *image != NULL && !mlt_properties_get_int( MLT_FRAME_PROPERTIES( this ), "progressive" ) )
309 AVPicture *output = mlt_pool_alloc( sizeof( AVPicture ) );
312 if ( *format == mlt_image_yuv422 )
314 avpicture_fill( output, *image, PIX_FMT_YUV422, *width, *height );
315 mlt_avpicture_deinterlace( output, output, PIX_FMT_YUV422, *width, *height );
319 mlt_pool_release( output );
321 // Make sure that others know the frame is deinterlaced
322 mlt_properties_set_int( MLT_FRAME_PROPERTIES( this ), "progressive", 1 );
328 /** Deinterlace filter processing - this should be lazy evaluation here...
331 static mlt_frame deinterlace_process( mlt_filter this, mlt_frame frame )
333 // Push the get_image method on to the stack
334 mlt_frame_push_get_image( frame, filter_get_image );
339 /** Constructor for the filter.
342 mlt_filter filter_avdeinterlace_init( void *arg )
344 mlt_filter this = mlt_filter_new( );
346 this->process = deinterlace_process;