]> git.sesse.net Git - mlt/blob - src/modules/xine/filter_deinterlace.c
make mlt_position type double
[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 #define YADIF_MODE_TEMPORAL_SPATIAL (0)
34 #define YADIF_MODE_TEMPORAL (2)
35
36 static yadif_filter *init_yadif( int width, int height )
37 {
38         yadif_filter *yadif = mlt_pool_alloc( sizeof( *yadif ) );
39
40         yadif->cpu = 0; // Pure C
41 #ifdef USE_SSE
42         yadif->cpu |= AVS_CPU_INTEGER_SSE;
43 #endif
44 #ifdef USE_SSE2
45         yadif->cpu |= AVS_CPU_SSE2;
46 #endif
47         // Create intermediate planar planes
48         yadif->yheight = height;
49         yadif->ywidth  = width;
50         yadif->uvwidth = yadif->ywidth / 2;
51         yadif->ypitch  = ( yadif->ywidth +  15 ) / 16 * 16;
52         yadif->uvpitch = ( yadif->uvwidth + 15 ) / 16 * 16;
53         yadif->ysrc  = (unsigned char *) mlt_pool_alloc( yadif->yheight * yadif->ypitch );
54         yadif->usrc  = (unsigned char *) mlt_pool_alloc( yadif->yheight * yadif->uvpitch);
55         yadif->vsrc  = (unsigned char *) mlt_pool_alloc( yadif->yheight * yadif->uvpitch );
56         yadif->yprev = (unsigned char *) mlt_pool_alloc( yadif->yheight * yadif->ypitch );
57         yadif->uprev = (unsigned char *) mlt_pool_alloc( yadif->yheight * yadif->uvpitch );
58         yadif->vprev = (unsigned char *) mlt_pool_alloc( yadif->yheight * yadif->uvpitch );
59         yadif->ynext = (unsigned char *) mlt_pool_alloc( yadif->yheight * yadif->ypitch );
60         yadif->unext = (unsigned char *) mlt_pool_alloc( yadif->yheight * yadif->uvpitch );
61         yadif->vnext = (unsigned char *) mlt_pool_alloc( yadif->yheight * yadif->uvpitch );
62         yadif->ydest = (unsigned char *) mlt_pool_alloc( yadif->yheight * yadif->ypitch );
63         yadif->udest = (unsigned char *) mlt_pool_alloc( yadif->yheight * yadif->uvpitch );
64         yadif->vdest = (unsigned char *) mlt_pool_alloc( yadif->yheight * yadif->uvpitch );
65
66         return yadif;
67 }
68
69 static void close_yadif(yadif_filter *yadif)
70 {
71         mlt_pool_release( yadif->ysrc );
72         mlt_pool_release( yadif->usrc );
73         mlt_pool_release( yadif->vsrc );
74         mlt_pool_release( yadif->yprev );
75         mlt_pool_release( yadif->uprev );
76         mlt_pool_release( yadif->vprev );
77         mlt_pool_release( yadif->ynext );
78         mlt_pool_release( yadif->unext );
79         mlt_pool_release( yadif->vnext );
80         mlt_pool_release( yadif->ydest );
81         mlt_pool_release( yadif->udest );
82         mlt_pool_release( yadif->vdest );
83         mlt_pool_release( yadif );
84
85 #if defined(__GNUC__) && !defined(PIC)
86         // Set SSSE3 bit to cpu
87         asm (\
88         "mov $1, %%eax \n\t"\
89         "push %%ebx \n\t"\
90         "cpuid \n\t"\
91         "pop %%ebx \n\t"\
92         "mov %%ecx, %%edx \n\t"\
93         "shr $9, %%edx \n\t"\
94         "and $1, %%edx \n\t"\
95         "shl $9, %%edx \n\t"\
96         "and $511, %%ebx \n\t"\
97         "or %%edx, %%ebx \n\t"\
98         : "=b"(yadif->cpu) : "p"(yadif->cpu) : "%eax", "%ecx", "%edx");
99 #endif
100 }
101
102 static int deinterlace_yadif( mlt_frame frame, mlt_filter filter, uint8_t **image, mlt_image_format *format, int *width, int *height, int mode )
103 {
104         mlt_properties properties = MLT_FRAME_PROPERTIES( frame );
105         mlt_frame previous_frame = mlt_properties_get_data( properties, "previous frame", NULL );
106         uint8_t* previous_image = NULL;
107         int previous_width = *width;
108         int previous_height = *height;
109         mlt_frame next_frame = mlt_properties_get_data( properties, "next frame", NULL );
110         uint8_t* next_image = NULL;
111         int next_width = *width;
112         int next_height = *height;
113         
114         mlt_log_debug( MLT_FILTER_SERVICE(filter), "previous " MLT_POSITION_FMT " current " MLT_POSITION_FMT " next " MLT_POSITION_FMT "\n",
115                 previous_frame? mlt_frame_original_position(previous_frame) : -1,
116                 mlt_frame_original_position(frame),
117                 next_frame?  mlt_frame_original_position(next_frame) : -1);
118
119         if ( !previous_frame || !next_frame )
120                 return 1;
121
122         // Get the preceding frame's image
123         int error = mlt_frame_get_image( previous_frame, &previous_image, format, &previous_width, &previous_height, 0 );
124         int progressive = mlt_properties_get_int( MLT_FRAME_PROPERTIES( previous_frame ), "progressive" );
125
126         // Check that we aren't already progressive
127         if ( !error && previous_image && !progressive )
128         {
129                 // OK, now we know we have work to do and can request the image in our format
130                 frame->convert_image( previous_frame, &previous_image, format, mlt_image_yuv422 );
131
132                 // Get the current frame's image
133                 *format = mlt_image_yuv422;
134                 error = mlt_frame_get_image( frame, image, format, width, height, 0 );
135
136                 if ( !error && *image && *format == mlt_image_yuv422 )
137                 {
138                         // Get the following frame's image
139                         error = mlt_frame_get_image( next_frame, &next_image, format, &next_width, &next_height, 0 );
140                 
141                         if ( !error && next_image && *format == mlt_image_yuv422 )
142                         {
143                                 yadif_filter *yadif = init_yadif( *width, *height );
144                                 if ( yadif )
145                                 {
146                                         const int order = mlt_properties_get_int( properties, "top_field_first" );
147                                         const int pitch = *width << 1;
148                                         const int parity = 0;
149
150                                         // Convert packed to planar
151                                         YUY2ToPlanes( *image, pitch, *width, *height, yadif->ysrc,
152                                                 yadif->ypitch, yadif->usrc, yadif->vsrc, yadif->uvpitch, yadif->cpu );
153                                         YUY2ToPlanes( previous_image, pitch, *width, *height, yadif->yprev,
154                                                 yadif->ypitch, yadif->uprev, yadif->vprev, yadif->uvpitch, yadif->cpu );
155                                         YUY2ToPlanes( next_image, pitch, *width, *height, yadif->ynext,
156                                                 yadif->ypitch, yadif->unext, yadif->vnext, yadif->uvpitch, yadif->cpu );
157
158                                         // Deinterlace each plane
159                                         filter_plane( mode, yadif->ydest, yadif->ypitch, yadif->yprev, yadif->ysrc,
160                                                 yadif->ynext, yadif->ypitch, *width, *height, parity, order, yadif->cpu);
161                                         filter_plane( mode, yadif->udest, yadif->uvpitch,yadif->uprev, yadif->usrc,
162                                                 yadif->unext, yadif->uvpitch, *width >> 1, *height, parity, order, yadif->cpu);
163                                         filter_plane( mode, yadif->vdest, yadif->uvpitch, yadif->vprev, yadif->vsrc,
164                                                 yadif->vnext, yadif->uvpitch, *width >> 1, *height, parity, order, yadif->cpu);
165
166                                         // Convert planar to packed
167                                         YUY2FromPlanes( *image, pitch, *width, *height, yadif->ydest,
168                                                 yadif->ypitch, yadif->udest, yadif->vdest, yadif->uvpitch, yadif->cpu);
169
170                                         close_yadif( yadif );
171                                 }
172                         }
173                 }
174         }
175         else
176         {
177                 // Get the current frame's image
178                 error = mlt_frame_get_image( frame, image, format, width, height, 0 );
179         }
180         return error;
181 }
182
183 /** Do it :-).
184 */
185
186 static int filter_get_image( mlt_frame frame, uint8_t **image, mlt_image_format *format, int *width, int *height, int writable )
187 {
188         int error = 0;
189         mlt_filter filter = mlt_frame_pop_service( frame );
190         mlt_properties properties = MLT_FRAME_PROPERTIES( frame );
191         int deinterlace = mlt_properties_get_int( properties, "consumer_deinterlace" );
192         // The progressive var should only represent the frame's original state and not the state
193         // as modified by this filter!
194         int progressive = mlt_properties_get_int( properties, "progressive" );
195         // At this point - before image was requested - (progressive == 0) cannot be trusted because
196         // some producers (avformat) do not yet know.
197
198         if ( deinterlace && !mlt_properties_get_int( properties, "test_image" ) )
199         {
200                 // Determine deinterlace method
201                 char *method_str = mlt_properties_get( MLT_FILTER_PROPERTIES( filter ), "method" );
202                 int method = DEINTERLACE_NONE;
203                 char *frame_method_str = mlt_properties_get( properties, "deinterlace_method" );
204                 
205                 if ( frame_method_str )
206                         method_str = frame_method_str;
207                 
208                 if ( !method_str || strcmp( method_str, "yadif" ) == 0 )
209                         method = DEINTERLACE_YADIF;
210                 else if ( strcmp( method_str, "yadif-nospatial" ) == 0 )
211                         method = DEINTERLACE_YADIF_NOSPATIAL;
212                 else if ( strcmp( method_str, "onefield" ) == 0 )
213                         method = DEINTERLACE_ONEFIELD;
214                 else if ( strcmp( method_str, "linearblend" ) == 0 )
215                         method = DEINTERLACE_LINEARBLEND;
216                 else if ( strcmp( method_str, "bob" ) == 0 )
217                         method = DEINTERLACE_BOB;
218                 else if ( strcmp( method_str, "weave" ) == 0 )
219                         method = DEINTERLACE_BOB;
220                 else if ( strcmp( method_str, "greedy" ) == 0 )
221                         method = DEINTERLACE_GREEDY;
222
223                 // Some producers like pixbuf want rescale_width & _height, but will not get them if you request
224                 // the previous image first. So, on the first iteration, we use linearblend.
225                 if ( ( method == DEINTERLACE_YADIF || method == DEINTERLACE_YADIF_NOSPATIAL ) &&
226                         !mlt_properties_get_int( MLT_FILTER_PROPERTIES(filter), "_notfirst" ) )
227                 {
228                         mlt_properties_set_int( MLT_FILTER_PROPERTIES(filter), "_notfirst", 1 );
229                         method = DEINTERLACE_LINEARBLEND;
230                         error = 1;
231                 }
232
233                 if ( method == DEINTERLACE_YADIF )
234                 {
235                         error = deinterlace_yadif( frame, filter, image, format, width, height, YADIF_MODE_TEMPORAL_SPATIAL );
236                 }
237                 else if ( method == DEINTERLACE_YADIF_NOSPATIAL )
238                 {
239                         error = deinterlace_yadif( frame, filter, image, format, width, height, YADIF_MODE_TEMPORAL );
240                 }
241                 if ( error || ( method > DEINTERLACE_NONE && method < DEINTERLACE_YADIF ) )
242                 {
243                         mlt_service service = mlt_properties_get_data( MLT_FILTER_PROPERTIES(filter), "service", NULL );
244
245                         // Get the current frame's image
246                         int error2 = mlt_frame_get_image( frame, image, format, width, height, writable );
247                         progressive = mlt_properties_get_int( properties, "progressive" );
248
249                         if ( error )
250                         {
251                                 method = DEINTERLACE_LINEARBLEND;
252                                 // If YADIF requested, prev/next cancelled because some previous frames were progressive,
253                                 // but new frames are interlaced, then turn prev/next frames back on.
254                                 if ( !progressive )
255                                         mlt_properties_set_int( MLT_SERVICE_PROPERTIES(service), "_need_previous_next", 1 );
256                         }
257                         else
258                         {
259                                 // Signal that we no longer need previous and next frames
260                                 mlt_properties_set_int( MLT_SERVICE_PROPERTIES(service), "_need_previous_next", 0 );
261                         }
262                         error = error2;
263                         
264                         if ( !error && !progressive )
265                         {
266                                 // OK, now we know we have work to do and can request the image in our format
267                                 error = frame->convert_image( frame, image, format, mlt_image_yuv422 );
268
269                                 // Check that we aren't already progressive
270                                 if ( !error && *image && *format == mlt_image_yuv422 )
271                                 {
272                                         // Deinterlace the image using one of the Xine deinterlacers
273                                         int image_size = *width * *height * 2;
274                                         uint8_t *new_image = mlt_pool_alloc( image_size );
275
276                                         deinterlace_yuv( new_image, image, *width * 2, *height, method );
277                                         mlt_frame_set_image( frame, new_image, image_size, mlt_pool_release );
278                                         *image = new_image;
279                                 }
280                         }
281                 }
282                 else if ( method == DEINTERLACE_NONE )
283                 {
284                         error = mlt_frame_get_image( frame, image, format, width, height, writable );
285                 }
286
287                 // update progressive flag after having obtained image
288                 progressive = mlt_properties_get_int( properties, "progressive" );
289
290                 mlt_log_debug( MLT_FILTER_SERVICE( filter ), "error %d deint %d prog %d fmt %s method %s\n",
291                         error, deinterlace, progressive, mlt_image_format_name( *format ), method_str ? method_str : "yadif" );
292
293                 if ( !error )
294                 {
295                         // Make sure that others know the frame is deinterlaced
296                         mlt_properties_set_int( properties, "progressive", 1 );
297                 }
298         }
299         else
300         {
301                 // Pass through
302                 error = mlt_frame_get_image( frame, image, format, width, height, writable );
303         }
304
305         if ( !deinterlace || progressive )
306         {
307                 // Signal that we no longer need previous and next frames
308                 mlt_service service = mlt_properties_get_data( MLT_FILTER_PROPERTIES(filter), "service", NULL );
309                 if ( service )
310                         mlt_properties_set_int( MLT_SERVICE_PROPERTIES(service), "_need_previous_next", 0 );
311         }
312
313         return error;
314 }
315
316 /** Deinterlace filter processing - this should be lazy evaluation here...
317 */
318
319 static mlt_frame deinterlace_process( mlt_filter filter, mlt_frame frame )
320 {
321         // Push filter on to the service stack
322         mlt_frame_push_service( frame, filter );
323
324         // Push the get_image method on to the stack
325         mlt_frame_push_get_image( frame, filter_get_image );
326         
327         return frame;
328 }
329
330 static void on_service_changed( mlt_service owner, mlt_service filter )
331 {
332         mlt_service service = mlt_properties_get_data( MLT_SERVICE_PROPERTIES(filter), "service", NULL );
333         mlt_properties_set_int( MLT_SERVICE_PROPERTIES(service), "_need_previous_next", 1 );
334 }
335
336 /** Constructor for the filter.
337 */
338
339 mlt_filter filter_deinterlace_init( mlt_profile profile, mlt_service_type type, const char *id, char *arg )
340 {
341         mlt_filter filter = mlt_filter_new( );
342         if ( filter != NULL )
343         {
344                 filter->process = deinterlace_process;
345                 mlt_properties_set( MLT_FILTER_PROPERTIES( filter ), "method", arg );
346                 mlt_events_listen( MLT_FILTER_PROPERTIES( filter ), filter, "service-changed", (mlt_listener) on_service_changed );
347         }
348         return filter;
349 }
350