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 if ( !error && previous_image && *format == mlt_image_yuv422 )
59 // Get the current frame's image
60 error = mlt_frame_get_image( frame, image, format, width, height, 0 );
62 // Check that we aren't already progressive
63 if ( !error && *image && *format == mlt_image_yuv422 &&
64 !mlt_properties_get_int( MLT_FRAME_PROPERTIES( frame ), "progressive" ) )
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);
126 static int filter_get_image( mlt_frame this, uint8_t **image, mlt_image_format *format, int *width, int *height, int writable )
129 mlt_properties properties = MLT_FRAME_PROPERTIES( this );
130 int deinterlace = mlt_properties_get_int( properties, "consumer_deinterlace" );
131 int progressive = mlt_properties_get_int( properties, "progressive" );
133 // Pop the service off the stack
134 mlt_filter filter = mlt_frame_pop_service( this );
136 // Get the input image
137 if ( deinterlace && !progressive )
139 // Determine deinterlace method
140 char *method_str = mlt_properties_get( MLT_FILTER_PROPERTIES( filter ), "method" );
141 int method = DEINTERLACE_NONE;
142 char *frame_method_str = mlt_properties_get( properties, "deinterlace_method" );
144 if ( frame_method_str )
145 method_str = frame_method_str;
147 if ( !method_str || strcmp( method_str, "yadif" ) == 0 )
148 method = DEINTERLACE_YADIF;
149 else if ( strcmp( method_str, "yadif-nospatial" ) == 0 )
150 method = DEINTERLACE_YADIF_NOSPATIAL;
151 else if ( strcmp( method_str, "onefield" ) == 0 )
152 method = DEINTERLACE_ONEFIELD;
153 else if ( strcmp( method_str, "linearblend" ) == 0 )
154 method = DEINTERLACE_LINEARBLEND;
155 else if ( strcmp( method_str, "bob" ) == 0 )
156 method = DEINTERLACE_BOB;
157 else if ( strcmp( method_str, "weave" ) == 0 )
158 method = DEINTERLACE_BOB;
159 else if ( strcmp( method_str, "greedy" ) == 0 )
160 method = DEINTERLACE_GREEDY;
162 *format = mlt_image_yuv422;
164 if ( method == DEINTERLACE_YADIF )
167 error = deinterlace_yadif( this, filter, image, format, width, height, mode );
168 progressive = mlt_properties_get_int( properties, "progressive" );
170 else if ( method == DEINTERLACE_YADIF_NOSPATIAL )
173 error = deinterlace_yadif( this, filter, image, format, width, height, mode );
174 progressive = mlt_properties_get_int( properties, "progressive" );
176 if ( error || ( method > DEINTERLACE_NONE && method < DEINTERLACE_YADIF ) )
178 // Signal that we no longer need previous and next frames
179 mlt_service service = mlt_properties_get_data( MLT_FILTER_PROPERTIES(filter), "service", NULL );
180 mlt_properties_set_int( MLT_SERVICE_PROPERTIES(service), "_need_previous_next", 0 );
183 method = DEINTERLACE_ONEFIELD;
185 // Get the current frame's image
186 error = mlt_frame_get_image( this, image, format, width, height, writable );
187 progressive = mlt_properties_get_int( properties, "progressive" );
189 // Check that we aren't already progressive
190 if ( !progressive && !error && *image && *format == mlt_image_yuv422 )
192 // Deinterlace the image using one of the Xine deinterlacers
193 int image_size = *width * *height * 2;
194 uint8_t *new_image = mlt_pool_alloc( image_size );
196 deinterlace_yuv( new_image, image, *width * 2, *height, method );
197 mlt_properties_set_data( properties, "image", new_image, image_size, mlt_pool_release, NULL );
201 else if ( method == DEINTERLACE_NONE )
203 error = mlt_frame_get_image( this, image, format, width, height, writable );
206 mlt_log_debug( MLT_FILTER_SERVICE( filter ), "error %d deint %d prog %d fmt %s method %s\n",
207 error, deinterlace, progressive, mlt_image_format_name( *format ), method_str ? method_str : "yadif" );
211 // Make sure that others know the frame is deinterlaced
212 mlt_properties_set_int( properties, "progressive", 1 );
217 // Signal that we no longer need previous and next frames
218 mlt_service service = mlt_properties_get_data( MLT_FILTER_PROPERTIES(filter), "service", NULL );
219 mlt_properties_set_int( MLT_SERVICE_PROPERTIES(service), "_need_previous_next", 0 );
222 error = mlt_frame_get_image( this, image, format, width, height, writable );
228 /** Deinterlace filter processing - this should be lazy evaluation here...
231 static mlt_frame deinterlace_process( mlt_filter this, mlt_frame frame )
233 // Push this on to the service stack
234 mlt_frame_push_service( frame, this );
236 // Push the get_image method on to the stack
237 mlt_frame_push_get_image( frame, filter_get_image );
242 static void filter_close( mlt_filter this )
244 yadif_filter *yadif = mlt_properties_get_data( MLT_FILTER_PROPERTIES( this ), "yadif", NULL );
249 mlt_pool_release( yadif->ysrc );
250 mlt_pool_release( yadif->usrc );
251 mlt_pool_release( yadif->vsrc );
252 mlt_pool_release( yadif->yprev );
253 mlt_pool_release( yadif->uprev );
254 mlt_pool_release( yadif->vprev );
255 mlt_pool_release( yadif->ynext );
256 mlt_pool_release( yadif->unext );
257 mlt_pool_release( yadif->vnext );
258 mlt_pool_release( yadif->ydest );
259 mlt_pool_release( yadif->udest );
260 mlt_pool_release( yadif->vdest );
262 mlt_pool_release( yadif );
266 static void on_service_changed( mlt_service owner, mlt_service filter )
268 mlt_service service = mlt_properties_get_data( MLT_SERVICE_PROPERTIES(filter), "service", NULL );
269 mlt_properties_set_int( MLT_SERVICE_PROPERTIES(service), "_need_previous_next", 1 );
272 /** Constructor for the filter.
275 mlt_filter filter_deinterlace_init( mlt_profile profile, mlt_service_type type, const char *id, char *arg )
277 mlt_filter this = mlt_filter_new( );
280 yadif_filter *yadif = mlt_pool_alloc( sizeof( *yadif ) );
282 yadif->cpu = 0; // Pure C
284 yadif->cpu |= AVS_CPU_INTEGER_SSE;
287 yadif->cpu |= AVS_CPU_SSE2;
290 this->process = deinterlace_process;
291 this->close = filter_close;
292 mlt_properties_set( MLT_FILTER_PROPERTIES( this ), "method", arg );
293 mlt_properties_set_data( MLT_FILTER_PROPERTIES( this ), "yadif", yadif, sizeof(*yadif), NULL, NULL );
294 mlt_events_listen( MLT_FILTER_PROPERTIES( this ), this, "service-changed", (mlt_listener) on_service_changed );
296 #if defined(__GNUC__) && !defined(PIC)
297 // Set SSSE3 bit to cpu
299 "mov $1, %%eax \n\t"\
303 "mov %%ecx, %%edx \n\t"\
304 "shr $9, %%edx \n\t"\
305 "and $1, %%edx \n\t"\
306 "shl $9, %%edx \n\t"\
307 "and $511, %%ebx \n\t"\
308 "or %%edx, %%ebx \n\t"\
309 : "=b"(yadif->cpu) : "p"(yadif->cpu) : "%eax", "%ecx", "%edx");