]> git.sesse.net Git - mlt/blob - src/modules/xine/filter_deinterlace.c
Fix a segfault if one tries to use deinterlace explicitly.
[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         else
121         {
122                 // Get the current frame's image
123                 error = mlt_frame_get_image( frame, image, format, width, height, 0 );
124         }
125         return error;
126 }
127
128 /** Do it :-).
129 */
130
131 static int filter_get_image( mlt_frame this, uint8_t **image, mlt_image_format *format, int *width, int *height, int writable )
132 {
133         int error = 0;
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" );
137         
138         // Pop the service off the stack
139         mlt_filter filter = mlt_frame_pop_service( this );
140
141         // Get the input image
142         if ( deinterlace && !progressive )
143         {
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" );
148                 
149                 if ( frame_method_str )
150                         method_str = frame_method_str;
151                 
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;
166
167                 *format = mlt_image_yuv422;
168
169                 if ( method == DEINTERLACE_YADIF )
170                 {
171                         int mode = 0;
172                         error = deinterlace_yadif( this, filter, image, format, width, height, mode );
173                         progressive = mlt_properties_get_int( properties, "progressive" );
174                 }
175                 else if ( method == DEINTERLACE_YADIF_NOSPATIAL )
176                 {
177                         int mode = 2;
178                         error = deinterlace_yadif( this, filter, image, format, width, height, mode );
179                         progressive = mlt_properties_get_int( properties, "progressive" );
180                 }
181                 if ( error || ( method > DEINTERLACE_NONE && method < DEINTERLACE_YADIF ) )
182                 {
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 );
186                         
187                         if ( error )
188                                 method = DEINTERLACE_ONEFIELD;
189                         
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" );
193
194                         // Check that we aren't already progressive
195                         if ( !progressive && !error && *image && *format == mlt_image_yuv422 )
196                         {
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 );
200
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 );
203                                 *image = new_image;
204                         }
205                 }
206                 else if ( method == DEINTERLACE_NONE )
207                 {
208                         error = mlt_frame_get_image( this, image, format, width, height, writable );
209                 }
210                 
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" );
213                 
214                 if ( !error )
215                 {
216                         // Make sure that others know the frame is deinterlaced
217                         mlt_properties_set_int( properties, "progressive", 1 );
218                 }
219         }
220         else
221         {
222                 // Pass through
223                 error = mlt_frame_get_image( this, image, format, width, height, writable );
224         }
225
226         if ( !deinterlace || progressive )
227         {
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 );
230                 if ( service )
231                         mlt_properties_set_int( MLT_SERVICE_PROPERTIES(service), "_need_previous_next", 0 );
232         }
233
234         return error;
235 }
236
237 /** Deinterlace filter processing - this should be lazy evaluation here...
238 */
239
240 static mlt_frame deinterlace_process( mlt_filter this, mlt_frame frame )
241 {
242         // Push this on to the service stack
243         mlt_frame_push_service( frame, this );
244
245         // Push the get_image method on to the stack
246         mlt_frame_push_get_image( frame, filter_get_image );
247         
248         return frame;
249 }
250
251 static void filter_close( mlt_filter this )
252 {
253         yadif_filter *yadif = mlt_properties_get_data( MLT_FILTER_PROPERTIES( this ), "yadif", NULL );
254         if ( yadif )
255         {
256                 if ( yadif->ysrc )
257                 {
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 );
270                 }
271                 mlt_pool_release( yadif );
272         }
273 }
274
275 static void on_service_changed( mlt_service owner, mlt_service filter )
276 {
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 );
279 }
280
281 /** Constructor for the filter.
282 */
283
284 mlt_filter filter_deinterlace_init( mlt_profile profile, mlt_service_type type, const char *id, char *arg )
285 {
286         mlt_filter this = mlt_filter_new( );
287         if ( this != NULL )
288         {
289                 yadif_filter *yadif = mlt_pool_alloc( sizeof( *yadif ) );
290
291                 yadif->cpu = 0; // Pure C
292 #ifdef USE_SSE
293                 yadif->cpu |= AVS_CPU_INTEGER_SSE;
294 #endif
295 #ifdef USE_SSE2
296                 yadif->cpu |= AVS_CPU_SSE2;
297 #endif
298                 yadif->ysrc = NULL;
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 ); 
304                 
305 #if defined(__GNUC__) && !defined(PIC)
306                 // Set SSSE3 bit to cpu
307                 asm (\
308                 "mov $1, %%eax \n\t"\
309                 "push %%ebx \n\t"\
310                 "cpuid \n\t"\
311                 "pop %%ebx \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");
319 #endif
320         }
321         return this;
322 }
323