]> git.sesse.net Git - mlt/blob - src/modules/xine/filter_deinterlace.c
Add service locks around yadif context.
[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                                 mlt_service_lock( MLT_FILTER_SERVICE(filter) );
72                                 if ( !yadif->ysrc )
73                                 {
74                                         // Create intermediate planar planes
75                                         yadif->yheight = *height;
76                                         yadif->ywidth  = *width;
77                                         yadif->uvwidth = yadif->ywidth / 2;
78                                         yadif->ypitch  = ( yadif->ywidth +  15 ) / 16 * 16;
79                                         yadif->uvpitch = ( yadif->uvwidth + 15 ) / 16 * 16;
80                                         yadif->ysrc  = (unsigned char *) mlt_pool_alloc( yadif->yheight * yadif->ypitch );
81                                         yadif->usrc  = (unsigned char *) mlt_pool_alloc( yadif->yheight * yadif->uvpitch);
82                                         yadif->vsrc  = (unsigned char *) mlt_pool_alloc( yadif->yheight * yadif->uvpitch );
83                                         yadif->yprev = (unsigned char *) mlt_pool_alloc( yadif->yheight * yadif->ypitch );
84                                         yadif->uprev = (unsigned char *) mlt_pool_alloc( yadif->yheight * yadif->uvpitch );
85                                         yadif->vprev = (unsigned char *) mlt_pool_alloc( yadif->yheight * yadif->uvpitch );
86                                         yadif->ynext = (unsigned char *) mlt_pool_alloc( yadif->yheight * yadif->ypitch );
87                                         yadif->unext = (unsigned char *) mlt_pool_alloc( yadif->yheight * yadif->uvpitch );
88                                         yadif->vnext = (unsigned char *) mlt_pool_alloc( yadif->yheight * yadif->uvpitch );
89                                         yadif->ydest = (unsigned char *) mlt_pool_alloc( yadif->yheight * yadif->ypitch );
90                                         yadif->udest = (unsigned char *) mlt_pool_alloc( yadif->yheight * yadif->uvpitch );
91                                         yadif->vdest = (unsigned char *) mlt_pool_alloc( yadif->yheight * yadif->uvpitch );
92                                         
93                                 }
94                         
95                                 const int order = mlt_properties_get_int( properties, "top_field_first" );
96                                 const int pitch = *width << 1;
97                                 const int parity = 0;
98                         
99                                 // Convert packed to planar
100                                 YUY2ToPlanes( *image, pitch, *width, *height, yadif->ysrc,
101                                         yadif->ypitch, yadif->usrc, yadif->vsrc, yadif->uvpitch, yadif->cpu );
102                                 YUY2ToPlanes( previous_image, pitch, *width, *height, yadif->yprev,
103                                         yadif->ypitch, yadif->uprev, yadif->vprev, yadif->uvpitch, yadif->cpu );
104                                 YUY2ToPlanes( next_image, pitch, *width, *height, yadif->ynext,
105                                         yadif->ypitch, yadif->unext, yadif->vnext, yadif->uvpitch, yadif->cpu );
106                                 
107                                 // Deinterlace each plane
108                                 filter_plane( mode, yadif->ydest, yadif->ypitch, yadif->yprev, yadif->ysrc,
109                                         yadif->ynext, yadif->ypitch, *width, *height, parity, order, yadif->cpu);
110                                 filter_plane( mode, yadif->udest, yadif->uvpitch,yadif->uprev, yadif->usrc,
111                                         yadif->unext, yadif->uvpitch, *width >> 1, *height, parity, order, yadif->cpu);
112                                 filter_plane( mode, yadif->vdest, yadif->uvpitch, yadif->vprev, yadif->vsrc,
113                                         yadif->vnext, yadif->uvpitch, *width >> 1, *height, parity, order, yadif->cpu);
114                                 
115                                 // Convert planar to packed
116                                 YUY2FromPlanes( *image, pitch, *width, *height, yadif->ydest,
117                                         yadif->ypitch, yadif->udest, yadif->vdest, yadif->uvpitch, yadif->cpu);
118
119                                 mlt_service_unlock( MLT_FILTER_SERVICE(filter) );
120                         }
121                 }
122         }
123         else
124         {
125                 // Get the current frame's image
126                 error = mlt_frame_get_image( frame, image, format, width, height, 0 );
127         }
128         return error;
129 }
130
131 /** Do it :-).
132 */
133
134 static int filter_get_image( mlt_frame this, uint8_t **image, mlt_image_format *format, int *width, int *height, int writable )
135 {
136         int error = 0;
137         mlt_properties properties = MLT_FRAME_PROPERTIES( this );
138         int deinterlace = mlt_properties_get_int( properties, "consumer_deinterlace" );
139         int progressive = mlt_properties_get_int( properties, "progressive" );
140         
141         // Pop the service off the stack
142         mlt_filter filter = mlt_frame_pop_service( this );
143
144         // Get the input image
145         if ( deinterlace && !progressive )
146         {
147                 // Determine deinterlace method
148                 char *method_str = mlt_properties_get( MLT_FILTER_PROPERTIES( filter ), "method" );
149                 int method = DEINTERLACE_NONE;
150                 char *frame_method_str = mlt_properties_get( properties, "deinterlace_method" );
151                 
152                 if ( frame_method_str )
153                         method_str = frame_method_str;
154                 
155                 if ( !method_str || strcmp( method_str, "yadif" ) == 0 )
156                         method = DEINTERLACE_YADIF;
157                 else if ( strcmp( method_str, "yadif-nospatial" ) == 0 )
158                         method = DEINTERLACE_YADIF_NOSPATIAL;
159                 else if ( strcmp( method_str, "onefield" ) == 0 )
160                         method = DEINTERLACE_ONEFIELD;
161                 else if ( strcmp( method_str, "linearblend" ) == 0 )
162                         method = DEINTERLACE_LINEARBLEND;
163                 else if ( strcmp( method_str, "bob" ) == 0 )
164                         method = DEINTERLACE_BOB;
165                 else if ( strcmp( method_str, "weave" ) == 0 )
166                         method = DEINTERLACE_BOB;
167                 else if ( strcmp( method_str, "greedy" ) == 0 )
168                         method = DEINTERLACE_GREEDY;
169
170                 *format = mlt_image_yuv422;
171
172                 if ( method == DEINTERLACE_YADIF )
173                 {
174                         int mode = 0;
175                         error = deinterlace_yadif( this, filter, image, format, width, height, mode );
176                         progressive = mlt_properties_get_int( properties, "progressive" );
177                 }
178                 else if ( method == DEINTERLACE_YADIF_NOSPATIAL )
179                 {
180                         int mode = 2;
181                         error = deinterlace_yadif( this, filter, image, format, width, height, mode );
182                         progressive = mlt_properties_get_int( properties, "progressive" );
183                 }
184                 if ( error || ( method > DEINTERLACE_NONE && method < DEINTERLACE_YADIF ) )
185                 {
186                         // Signal that we no longer need previous and next frames
187                         mlt_service service = mlt_properties_get_data( MLT_FILTER_PROPERTIES(filter), "service", NULL );
188                         mlt_properties_set_int( MLT_SERVICE_PROPERTIES(service), "_need_previous_next", 0 );
189                         
190                         if ( error )
191                                 method = DEINTERLACE_ONEFIELD;
192                         
193                         // Get the current frame's image
194                         error = mlt_frame_get_image( this, image, format, width, height, writable );
195                         progressive = mlt_properties_get_int( properties, "progressive" );
196
197                         // Check that we aren't already progressive
198                         if ( !progressive && !error && *image && *format == mlt_image_yuv422 )
199                         {
200                                 // Deinterlace the image using one of the Xine deinterlacers
201                                 int image_size = *width * *height * 2;
202                                 uint8_t *new_image = mlt_pool_alloc( image_size );
203
204                                 deinterlace_yuv( new_image, image, *width * 2, *height, method );
205                                 mlt_properties_set_data( properties, "image", new_image, image_size, mlt_pool_release, NULL );
206                                 *image = new_image;
207                         }
208                 }
209                 else if ( method == DEINTERLACE_NONE )
210                 {
211                         error = mlt_frame_get_image( this, image, format, width, height, writable );
212                 }
213                 
214                 mlt_log_debug( MLT_FILTER_SERVICE( filter ), "error %d deint %d prog %d fmt %s method %s\n",
215                         error, deinterlace, progressive, mlt_image_format_name( *format ), method_str ? method_str : "yadif" );
216                 
217                 if ( !error )
218                 {
219                         // Make sure that others know the frame is deinterlaced
220                         mlt_properties_set_int( properties, "progressive", 1 );
221                 }
222         }
223         else
224         {
225                 // Pass through
226                 error = mlt_frame_get_image( this, image, format, width, height, writable );
227         }
228
229         if ( !deinterlace || progressive )
230         {
231                 // Signal that we no longer need previous and next frames
232                 mlt_service service = mlt_properties_get_data( MLT_FILTER_PROPERTIES(filter), "service", NULL );
233                 if ( service )
234                         mlt_properties_set_int( MLT_SERVICE_PROPERTIES(service), "_need_previous_next", 0 );
235         }
236
237         return error;
238 }
239
240 /** Deinterlace filter processing - this should be lazy evaluation here...
241 */
242
243 static mlt_frame deinterlace_process( mlt_filter this, mlt_frame frame )
244 {
245         // Push this on to the service stack
246         mlt_frame_push_service( frame, this );
247
248         // Push the get_image method on to the stack
249         mlt_frame_push_get_image( frame, filter_get_image );
250         
251         return frame;
252 }
253
254 static void filter_close( mlt_filter this )
255 {
256         yadif_filter *yadif = mlt_properties_get_data( MLT_FILTER_PROPERTIES( this ), "yadif", NULL );
257         if ( yadif )
258         {
259                 if ( yadif->ysrc )
260                 {
261                         mlt_pool_release( yadif->ysrc );
262                         mlt_pool_release( yadif->usrc );
263                         mlt_pool_release( yadif->vsrc );
264                         mlt_pool_release( yadif->yprev );
265                         mlt_pool_release( yadif->uprev );
266                         mlt_pool_release( yadif->vprev );
267                         mlt_pool_release( yadif->ynext );
268                         mlt_pool_release( yadif->unext );
269                         mlt_pool_release( yadif->vnext );
270                         mlt_pool_release( yadif->ydest );
271                         mlt_pool_release( yadif->udest );
272                         mlt_pool_release( yadif->vdest );
273                 }
274                 mlt_pool_release( yadif );
275         }
276 }
277
278 static void on_service_changed( mlt_service owner, mlt_service filter )
279 {
280         mlt_service service = mlt_properties_get_data( MLT_SERVICE_PROPERTIES(filter), "service", NULL );
281         mlt_properties_set_int( MLT_SERVICE_PROPERTIES(service), "_need_previous_next", 1 );
282 }
283
284 /** Constructor for the filter.
285 */
286
287 mlt_filter filter_deinterlace_init( mlt_profile profile, mlt_service_type type, const char *id, char *arg )
288 {
289         mlt_filter this = mlt_filter_new( );
290         if ( this != NULL )
291         {
292                 yadif_filter *yadif = mlt_pool_alloc( sizeof( *yadif ) );
293
294                 yadif->cpu = 0; // Pure C
295 #ifdef USE_SSE
296                 yadif->cpu |= AVS_CPU_INTEGER_SSE;
297 #endif
298 #ifdef USE_SSE2
299                 yadif->cpu |= AVS_CPU_SSE2;
300 #endif
301                 yadif->ysrc = NULL;
302                 this->process = deinterlace_process;
303                 this->close = filter_close;
304                 mlt_properties_set( MLT_FILTER_PROPERTIES( this ), "method", arg );
305                 mlt_properties_set_data( MLT_FILTER_PROPERTIES( this ), "yadif", yadif, sizeof(*yadif), NULL, NULL );
306                 mlt_events_listen( MLT_FILTER_PROPERTIES( this ), this, "service-changed", (mlt_listener) on_service_changed ); 
307                 
308 #if defined(__GNUC__) && !defined(PIC)
309                 // Set SSSE3 bit to cpu
310                 asm (\
311                 "mov $1, %%eax \n\t"\
312                 "push %%ebx \n\t"\
313                 "cpuid \n\t"\
314                 "pop %%ebx \n\t"\
315                 "mov %%ecx, %%edx \n\t"\
316                 "shr $9, %%edx \n\t"\
317                 "and $1, %%edx \n\t"\
318                 "shl $9, %%edx \n\t"\
319                 "and $511, %%ebx \n\t"\
320                 "or %%edx, %%ebx \n\t"\
321                 : "=b"(yadif->cpu) : "p"(yadif->cpu) : "%eax", "%ecx", "%edx");
322 #endif
323         }
324         return this;
325 }
326