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