2 * filter_deinterlace.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 <framework/mlt_filter.h>
22 #include <framework/mlt_log.h>
23 #include <framework/mlt_producer.h>
24 #include <framework/mlt_events.h>
25 #include "deinterlace.h"
28 #include <framework/mlt_frame.h>
33 int deinterlace_yadif( mlt_frame frame, mlt_filter filter, uint8_t **image, mlt_image_format *format, int *width, int *height, int mode )
35 mlt_properties properties = MLT_FRAME_PROPERTIES( frame );
36 mlt_frame previous_frame = mlt_properties_get_data( properties, "previous frame", NULL );
37 uint8_t* previous_image = NULL;
38 int previous_width = *width;
39 int previous_height = *height;
40 mlt_frame next_frame = mlt_properties_get_data( properties, "next frame", NULL );
41 uint8_t* next_image = NULL;
42 int next_width = *width;
43 int next_height = *height;
44 yadif_filter *yadif = mlt_properties_get_data( MLT_FILTER_PROPERTIES( filter ), "yadif", NULL );
46 mlt_log_debug( MLT_FILTER_SERVICE(filter), "previous %d current %d next %d\n",
47 previous_frame? mlt_frame_get_position(previous_frame) : -1,
48 mlt_frame_get_position(frame),
49 next_frame? mlt_frame_get_position(next_frame) : -1);
51 if ( !previous_frame || !next_frame )
54 // Get the preceding frame's image
55 int error = mlt_frame_get_image( previous_frame, &previous_image, format, &previous_width, &previous_height, 0 );
57 // Check that we aren't already progressive
58 if ( !error && previous_image && *format == mlt_image_yuv422 &&
59 !mlt_properties_get_int( MLT_FRAME_PROPERTIES( previous_frame ), "progressive" ) )
61 // Get the current frame's image
62 error = mlt_frame_get_image( frame, image, format, width, height, 0 );
64 if ( !error && *image && *format == mlt_image_yuv422 )
66 // Get the following frame's image
67 error = mlt_frame_get_image( next_frame, &next_image, format, &next_width, &next_height, 0 );
69 if ( !error && next_image && *format == mlt_image_yuv422 )
73 // Create intermediate planar planes
74 yadif->yheight = *height;
75 yadif->ywidth = *width;
76 yadif->uvwidth = yadif->ywidth / 2;
77 yadif->ypitch = ( yadif->ywidth + 15 ) / 16 * 16;
78 yadif->uvpitch = ( yadif->uvwidth + 15 ) / 16 * 16;
79 yadif->ysrc = (unsigned char *) mlt_pool_alloc( yadif->yheight * yadif->ypitch );
80 yadif->usrc = (unsigned char *) mlt_pool_alloc( yadif->yheight * yadif->uvpitch);
81 yadif->vsrc = (unsigned char *) mlt_pool_alloc( yadif->yheight * yadif->uvpitch );
82 yadif->yprev = (unsigned char *) mlt_pool_alloc( yadif->yheight * yadif->ypitch );
83 yadif->uprev = (unsigned char *) mlt_pool_alloc( yadif->yheight * yadif->uvpitch );
84 yadif->vprev = (unsigned char *) mlt_pool_alloc( yadif->yheight * yadif->uvpitch );
85 yadif->ynext = (unsigned char *) mlt_pool_alloc( yadif->yheight * yadif->ypitch );
86 yadif->unext = (unsigned char *) mlt_pool_alloc( yadif->yheight * yadif->uvpitch );
87 yadif->vnext = (unsigned char *) mlt_pool_alloc( yadif->yheight * yadif->uvpitch );
88 yadif->ydest = (unsigned char *) mlt_pool_alloc( yadif->yheight * yadif->ypitch );
89 yadif->udest = (unsigned char *) mlt_pool_alloc( yadif->yheight * yadif->uvpitch );
90 yadif->vdest = (unsigned char *) mlt_pool_alloc( yadif->yheight * yadif->uvpitch );
94 const int order = mlt_properties_get_int( properties, "top_field_first" );
95 const int pitch = *width << 1;
98 // Convert packed to planar
99 YUY2ToPlanes( *image, pitch, *width, *height, yadif->ysrc,
100 yadif->ypitch, yadif->usrc, yadif->vsrc, yadif->uvpitch, yadif->cpu );
101 YUY2ToPlanes( previous_image, pitch, *width, *height, yadif->yprev,
102 yadif->ypitch, yadif->uprev, yadif->vprev, yadif->uvpitch, yadif->cpu );
103 YUY2ToPlanes( next_image, pitch, *width, *height, yadif->ynext,
104 yadif->ypitch, yadif->unext, yadif->vnext, yadif->uvpitch, yadif->cpu );
106 // Deinterlace each plane
107 filter_plane( mode, yadif->ydest, yadif->ypitch, yadif->yprev, yadif->ysrc,
108 yadif->ynext, yadif->ypitch, *width, *height, parity, order, yadif->cpu);
109 filter_plane( mode, yadif->udest, yadif->uvpitch,yadif->uprev, yadif->usrc,
110 yadif->unext, yadif->uvpitch, *width >> 1, *height, parity, order, yadif->cpu);
111 filter_plane( mode, yadif->vdest, yadif->uvpitch, yadif->vprev, yadif->vsrc,
112 yadif->vnext, yadif->uvpitch, *width >> 1, *height, parity, order, yadif->cpu);
114 // Convert planar to packed
115 YUY2FromPlanes( *image, pitch, *width, *height, yadif->ydest,
116 yadif->ypitch, yadif->udest, yadif->vdest, yadif->uvpitch, yadif->cpu);
122 // Get the current frame's image
123 error = mlt_frame_get_image( frame, image, format, width, height, 0 );
131 static int filter_get_image( mlt_frame this, uint8_t **image, mlt_image_format *format, int *width, int *height, int writable )
134 mlt_properties properties = MLT_FRAME_PROPERTIES( this );
135 int deinterlace = mlt_properties_get_int( properties, "consumer_deinterlace" );
136 int progressive = mlt_properties_get_int( properties, "progressive" );
138 // Pop the service off the stack
139 mlt_filter filter = mlt_frame_pop_service( this );
141 // Get the input image
142 if ( deinterlace && !progressive )
144 // Determine deinterlace method
145 char *method_str = mlt_properties_get( MLT_FILTER_PROPERTIES( filter ), "method" );
146 int method = DEINTERLACE_NONE;
147 char *frame_method_str = mlt_properties_get( properties, "deinterlace_method" );
149 if ( frame_method_str )
150 method_str = frame_method_str;
152 if ( !method_str || strcmp( method_str, "yadif" ) == 0 )
153 method = DEINTERLACE_YADIF;
154 else if ( strcmp( method_str, "yadif-nospatial" ) == 0 )
155 method = DEINTERLACE_YADIF_NOSPATIAL;
156 else if ( strcmp( method_str, "onefield" ) == 0 )
157 method = DEINTERLACE_ONEFIELD;
158 else if ( strcmp( method_str, "linearblend" ) == 0 )
159 method = DEINTERLACE_LINEARBLEND;
160 else if ( strcmp( method_str, "bob" ) == 0 )
161 method = DEINTERLACE_BOB;
162 else if ( strcmp( method_str, "weave" ) == 0 )
163 method = DEINTERLACE_BOB;
164 else if ( strcmp( method_str, "greedy" ) == 0 )
165 method = DEINTERLACE_GREEDY;
167 *format = mlt_image_yuv422;
169 if ( method == DEINTERLACE_YADIF )
172 error = deinterlace_yadif( this, filter, image, format, width, height, mode );
173 progressive = mlt_properties_get_int( properties, "progressive" );
175 else if ( method == DEINTERLACE_YADIF_NOSPATIAL )
178 error = deinterlace_yadif( this, filter, image, format, width, height, mode );
179 progressive = mlt_properties_get_int( properties, "progressive" );
181 if ( error || ( method > DEINTERLACE_NONE && method < DEINTERLACE_YADIF ) )
183 // Signal that we no longer need previous and next frames
184 mlt_service service = mlt_properties_get_data( MLT_FILTER_PROPERTIES(filter), "service", NULL );
185 mlt_properties_set_int( MLT_SERVICE_PROPERTIES(service), "_need_previous_next", 0 );
188 method = DEINTERLACE_ONEFIELD;
190 // Get the current frame's image
191 error = mlt_frame_get_image( this, image, format, width, height, writable );
192 progressive = mlt_properties_get_int( properties, "progressive" );
194 // Check that we aren't already progressive
195 if ( !progressive && !error && *image && *format == mlt_image_yuv422 )
197 // Deinterlace the image using one of the Xine deinterlacers
198 int image_size = *width * *height * 2;
199 uint8_t *new_image = mlt_pool_alloc( image_size );
201 deinterlace_yuv( new_image, image, *width * 2, *height, method );
202 mlt_properties_set_data( properties, "image", new_image, image_size, mlt_pool_release, NULL );
206 else if ( method == DEINTERLACE_NONE )
208 error = mlt_frame_get_image( this, image, format, width, height, writable );
211 mlt_log_debug( MLT_FILTER_SERVICE( filter ), "error %d deint %d prog %d fmt %s method %s\n",
212 error, deinterlace, progressive, mlt_image_format_name( *format ), method_str ? method_str : "yadif" );
216 // Make sure that others know the frame is deinterlaced
217 mlt_properties_set_int( properties, "progressive", 1 );
223 error = mlt_frame_get_image( this, image, format, width, height, writable );
226 if ( !deinterlace || progressive )
228 // Signal that we no longer need previous and next frames
229 mlt_service service = mlt_properties_get_data( MLT_FILTER_PROPERTIES(filter), "service", NULL );
231 mlt_properties_set_int( MLT_SERVICE_PROPERTIES(service), "_need_previous_next", 0 );
237 /** Deinterlace filter processing - this should be lazy evaluation here...
240 static mlt_frame deinterlace_process( mlt_filter this, mlt_frame frame )
242 // Push this on to the service stack
243 mlt_frame_push_service( frame, this );
245 // Push the get_image method on to the stack
246 mlt_frame_push_get_image( frame, filter_get_image );
251 static void filter_close( mlt_filter this )
253 yadif_filter *yadif = mlt_properties_get_data( MLT_FILTER_PROPERTIES( this ), "yadif", NULL );
258 mlt_pool_release( yadif->ysrc );
259 mlt_pool_release( yadif->usrc );
260 mlt_pool_release( yadif->vsrc );
261 mlt_pool_release( yadif->yprev );
262 mlt_pool_release( yadif->uprev );
263 mlt_pool_release( yadif->vprev );
264 mlt_pool_release( yadif->ynext );
265 mlt_pool_release( yadif->unext );
266 mlt_pool_release( yadif->vnext );
267 mlt_pool_release( yadif->ydest );
268 mlt_pool_release( yadif->udest );
269 mlt_pool_release( yadif->vdest );
271 mlt_pool_release( yadif );
275 static void on_service_changed( mlt_service owner, mlt_service filter )
277 mlt_service service = mlt_properties_get_data( MLT_SERVICE_PROPERTIES(filter), "service", NULL );
278 mlt_properties_set_int( MLT_SERVICE_PROPERTIES(service), "_need_previous_next", 1 );
281 /** Constructor for the filter.
284 mlt_filter filter_deinterlace_init( mlt_profile profile, mlt_service_type type, const char *id, char *arg )
286 mlt_filter this = mlt_filter_new( );
289 yadif_filter *yadif = mlt_pool_alloc( sizeof( *yadif ) );
291 yadif->cpu = 0; // Pure C
293 yadif->cpu |= AVS_CPU_INTEGER_SSE;
296 yadif->cpu |= AVS_CPU_SSE2;
299 this->process = deinterlace_process;
300 this->close = filter_close;
301 mlt_properties_set( MLT_FILTER_PROPERTIES( this ), "method", arg );
302 mlt_properties_set_data( MLT_FILTER_PROPERTIES( this ), "yadif", yadif, sizeof(*yadif), NULL, NULL );
303 mlt_events_listen( MLT_FILTER_PROPERTIES( this ), this, "service-changed", (mlt_listener) on_service_changed );
305 #if defined(__GNUC__) && !defined(PIC)
306 // Set SSSE3 bit to cpu
308 "mov $1, %%eax \n\t"\
312 "mov %%ecx, %%edx \n\t"\
313 "shr $9, %%edx \n\t"\
314 "and $1, %%edx \n\t"\
315 "shl $9, %%edx \n\t"\
316 "and $511, %%ebx \n\t"\
317 "or %%edx, %%ebx \n\t"\
318 : "=b"(yadif->cpu) : "p"(yadif->cpu) : "%eax", "%ecx", "%edx");