]> git.sesse.net Git - mlt/blob - src/modules/xine/filter_deinterlace.c
Fix previous optimization commit on deinterlace.
[mlt] / src / modules / xine / filter_deinterlace.c
1 /*
2  * filter_deinterlace.c -- deinterlace filter
3  * Copyright (C) 2003-2004 Ushodaya Enterprises Limited
4  * Author: Charles Yates <charles.yates@pandora.be>
5  *
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.
10  *
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.
15  *
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.
19  */
20
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"
26 #include "yadif.h"
27
28 #include <framework/mlt_frame.h>
29
30 #include <string.h>
31 #include <stdlib.h>
32
33 int deinterlace_yadif( mlt_frame frame, mlt_filter filter, uint8_t **image, mlt_image_format *format, int *width, int *height, int mode )
34 {
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 );
45         
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);
50
51         if ( !previous_frame || !next_frame )
52                 return 1;
53
54         // Get the preceding frame's image
55         int error = mlt_frame_get_image( previous_frame, &previous_image, format, &previous_width, &previous_height, 0 );
56
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" ) )
60         {
61                 // Get the current frame's image
62                 error = mlt_frame_get_image( frame, image, format, width, height, 0 );
63
64                 if ( !error && *image && *format == mlt_image_yuv422 )
65                 {
66                         // Get the following frame's image
67                         error = mlt_frame_get_image( next_frame, &next_image, format, &next_width, &next_height, 0 );
68                 
69                         if ( !error && next_image && *format == mlt_image_yuv422 )
70                         {
71                                 if ( !yadif->ysrc )
72                                 {
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 );
91                                         
92                                 }
93                         
94                                 const int order = mlt_properties_get_int( properties, "top_field_first" );
95                                 const int pitch = *width << 1;
96                                 const int parity = 0;
97                         
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 );
105                                 
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);
113                                 
114                                 // Convert planar to packed
115                                 YUY2FromPlanes( *image, pitch, *width, *height, yadif->ydest,
116                                         yadif->ypitch, yadif->udest, yadif->vdest, yadif->uvpitch, yadif->cpu);
117                         }
118                 }
119         }
120         return error;
121 }
122
123 /** Do it :-).
124 */
125
126 static int filter_get_image( mlt_frame this, uint8_t **image, mlt_image_format *format, int *width, int *height, int writable )
127 {
128         int error = 0;
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" );
132         
133         // Pop the service off the stack
134         mlt_filter filter = mlt_frame_pop_service( this );
135
136         // Get the input image
137         if ( deinterlace && !progressive )
138         {
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" );
143                 
144                 if ( frame_method_str )
145                         method_str = frame_method_str;
146                 
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;
161
162                 *format = mlt_image_yuv422;
163
164                 if ( method == DEINTERLACE_YADIF )
165                 {
166                         int mode = 0;
167                         error = deinterlace_yadif( this, filter, image, format, width, height, mode );
168                         progressive = mlt_properties_get_int( properties, "progressive" );
169                 }
170                 else if ( method == DEINTERLACE_YADIF_NOSPATIAL )
171                 {
172                         int mode = 2;
173                         error = deinterlace_yadif( this, filter, image, format, width, height, mode );
174                         progressive = mlt_properties_get_int( properties, "progressive" );
175                 }
176                 if ( error || ( method > DEINTERLACE_NONE && method < DEINTERLACE_YADIF ) )
177                 {
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 );
181                         
182                         if ( error )
183                                 method = DEINTERLACE_ONEFIELD;
184                         
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" );
188
189                         // Check that we aren't already progressive
190                         if ( !progressive && !error && *image && *format == mlt_image_yuv422 )
191                         {
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 );
195
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 );
198                                 *image = new_image;
199                         }
200                 }
201                 else if ( method == DEINTERLACE_NONE )
202                 {
203                         error = mlt_frame_get_image( this, image, format, width, height, writable );
204                 }
205                 
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" );
208                 
209                 if ( !error )
210                 {
211                         // Make sure that others know the frame is deinterlaced
212                         mlt_properties_set_int( properties, "progressive", 1 );
213                 }
214         }
215         else
216         {
217                 // Pass through
218                 error = mlt_frame_get_image( this, image, format, width, height, writable );
219         }
220
221         if ( !deinterlace || progressive )
222         {
223                 // Signal that we no longer need previous and next frames
224                 mlt_service service = mlt_properties_get_data( MLT_FILTER_PROPERTIES(filter), "service", NULL );
225                 mlt_properties_set_int( MLT_SERVICE_PROPERTIES(service), "_need_previous_next", 0 );
226         }
227
228         return error;
229 }
230
231 /** Deinterlace filter processing - this should be lazy evaluation here...
232 */
233
234 static mlt_frame deinterlace_process( mlt_filter this, mlt_frame frame )
235 {
236         // Push this on to the service stack
237         mlt_frame_push_service( frame, this );
238
239         // Push the get_image method on to the stack
240         mlt_frame_push_get_image( frame, filter_get_image );
241         
242         return frame;
243 }
244
245 static void filter_close( mlt_filter this )
246 {
247         yadif_filter *yadif = mlt_properties_get_data( MLT_FILTER_PROPERTIES( this ), "yadif", NULL );
248         if ( yadif )
249         {
250                 if ( yadif->ysrc )
251                 {
252                         mlt_pool_release( yadif->ysrc );
253                         mlt_pool_release( yadif->usrc );
254                         mlt_pool_release( yadif->vsrc );
255                         mlt_pool_release( yadif->yprev );
256                         mlt_pool_release( yadif->uprev );
257                         mlt_pool_release( yadif->vprev );
258                         mlt_pool_release( yadif->ynext );
259                         mlt_pool_release( yadif->unext );
260                         mlt_pool_release( yadif->vnext );
261                         mlt_pool_release( yadif->ydest );
262                         mlt_pool_release( yadif->udest );
263                         mlt_pool_release( yadif->vdest );
264                 }
265                 mlt_pool_release( yadif );
266         }
267 }
268
269 static void on_service_changed( mlt_service owner, mlt_service filter )
270 {
271         mlt_service service = mlt_properties_get_data( MLT_SERVICE_PROPERTIES(filter), "service", NULL );
272         mlt_properties_set_int( MLT_SERVICE_PROPERTIES(service), "_need_previous_next", 1 );
273 }
274
275 /** Constructor for the filter.
276 */
277
278 mlt_filter filter_deinterlace_init( mlt_profile profile, mlt_service_type type, const char *id, char *arg )
279 {
280         mlt_filter this = mlt_filter_new( );
281         if ( this != NULL )
282         {
283                 yadif_filter *yadif = mlt_pool_alloc( sizeof( *yadif ) );
284
285                 yadif->cpu = 0; // Pure C
286 #ifdef USE_SSE
287                 yadif->cpu |= AVS_CPU_INTEGER_SSE;
288 #endif
289 #ifdef USE_SSE2
290                 yadif->cpu |= AVS_CPU_SSE2;
291 #endif
292                 yadif->ysrc = NULL;
293                 this->process = deinterlace_process;
294                 this->close = filter_close;
295                 mlt_properties_set( MLT_FILTER_PROPERTIES( this ), "method", arg );
296                 mlt_properties_set_data( MLT_FILTER_PROPERTIES( this ), "yadif", yadif, sizeof(*yadif), NULL, NULL );
297                 mlt_events_listen( MLT_FILTER_PROPERTIES( this ), this, "service-changed", (mlt_listener) on_service_changed ); 
298                 
299 #if defined(__GNUC__) && !defined(PIC)
300                 // Set SSSE3 bit to cpu
301                 asm (\
302                 "mov $1, %%eax \n\t"\
303                 "push %%ebx \n\t"\
304                 "cpuid \n\t"\
305                 "pop %%ebx \n\t"\
306                 "mov %%ecx, %%edx \n\t"\
307                 "shr $9, %%edx \n\t"\
308                 "and $1, %%edx \n\t"\
309                 "shl $9, %%edx \n\t"\
310                 "and $511, %%ebx \n\t"\
311                 "or %%edx, %%ebx \n\t"\
312                 : "=b"(yadif->cpu) : "p"(yadif->cpu) : "%eax", "%ecx", "%edx");
313 #endif
314         }
315         return this;
316 }
317