2 * producer_slowmotion.c -- create subspeed frames
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.
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.
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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 #include "filter_motion_est.h"
21 #include <framework/mlt.h>
30 #define ABS(a) ((a) >= 0 ? (a) : (-(a)))
32 // This is used to constrains pixel operations between two blocks to be within the image boundry
33 inline static int constrain( int *x, int *y, int *w, int *h,
34 const int dx, const int dy,
35 const int left, const int right,
36 const int top, const int bottom)
38 uint32_t penalty = 1 << SHIFT; // Retain a few extra bits of precision
44 // Origin of macroblock moves left of image boundy
45 if( *x < left || x2 < left ) {
46 w_remains = *w - left + ((*x < x2) ? *x : x2);
49 // Portion of macroblock moves right of image boundry
50 else if( *x + *w > right || x2 + *w > right )
51 w_remains = right - ((*x > x2) ? *x : x2);
53 // Origin of macroblock moves above image boundy
54 if( *y < top || y2 < top ) {
55 h_remains = *h - top + ((*y < y2) ? *y : y2);
58 // Portion of macroblock moves bellow image boundry
59 else if( *y + *h > bottom || y2 + *h > bottom )
60 h_remains = bottom - ((*y > y2) ? *y : y2);
62 if( w_remains == *w && h_remains == *h ) return penalty;
63 if( w_remains <= 0 || h_remains <= 0) return 0; // Block is clipped out of existance
64 penalty = (*w * *h * penalty)
65 / ( w_remains * h_remains); // Recipricol of the fraction of the block that remains
67 *w = w_remains; // Update the width and height
73 static void motion_interpolate( uint8_t *first_image, uint8_t *second_image, uint8_t *output,
74 int top_mb, int bottom_mb, int left_mb, int right_mb,
76 int width, int height,
77 int xstride, int ystride,
79 motion_vector *vectors )
81 assert ( scale >= 0.0 && scale <= 1.0 );
86 int scaled_dx, scaled_dy;
90 int mv_width = width / mb_w;
92 for( j = top_mb; j <= bottom_mb; j++ ){
93 for( i = left_mb; i <= right_mb; i++ ){
95 here = vectors + j*mv_width + i;
96 scaled_dx = (1.0 - scale) * (double)here->dx;
97 scaled_dy = (1.0 - scale) * (double)here->dy;
101 x = i * w; y = j * h;
103 // Denoise function caused some blocks to be completely clipped, ignore them
104 if (constrain( &x, &y, &w, &h, dx, dy, 0, width, 0, height) == 0 )
107 for( ty = y; ty < y + h ; ty++ ){
108 for( tx = x; tx < x + w ; tx++ ){
110 f = first_image + (tx + dx )*xstride + (ty + dy )*ystride;
111 s = second_image + (tx )*xstride + (ty )*ystride;
112 r = output + (tx+scaled_dx)*xstride + (ty+scaled_dy)*ystride;
114 if( ABS(f[0] - s[0]) > 3 * here->msad / (mb_w * mb_h * 2) )
124 r[0] = ( 1.0 - scale ) * (double)f[0] + scale * (double)s[0];
128 if( scaled_dx % 2 == 0 )
129 r[1] = ( 1.0 - scale ) * (double)f[1] + scale * (double) s[1];
131 *(r-1) = ( 1.0 - scale ) * (double)f[1] + scale * (double) s[1];
135 if( scaled_dx %2 == 0 )
136 // FIXME: may exceed boundies
137 r[1] = ( 1.0 - scale ) * ( (double)(*(f-1) + (double)f[3]) / 2.0 ) + scale * (double) s[1];
139 // FIXME: may exceed boundies
140 *(r-1) = ( 1.0 - scale ) * ( (double)(*(f-1) + (double)f[3]) / 2.0 ) + scale * (double) s[1];
149 // Image stack(able) method
150 static int slowmotion_get_image( mlt_frame this, uint8_t **image, mlt_image_format *format, int *width, int *height, int writable )
153 // Get the filter object and properties
154 mlt_producer producer = mlt_frame_pop_service( this );
155 mlt_frame second_frame = mlt_frame_pop_service( this );
156 mlt_frame first_frame = mlt_frame_pop_service( this );
158 mlt_properties producer_properties = MLT_PRODUCER_PROPERTIES( producer );
160 // Frame properties objects
161 mlt_properties frame_properties = MLT_FRAME_PROPERTIES( this );
162 mlt_properties first_frame_properties = MLT_FRAME_PROPERTIES( first_frame );
163 mlt_properties second_frame_properties = MLT_FRAME_PROPERTIES( second_frame );
166 *format = mlt_image_yuv422;
167 int size = *width * *height * 2;
169 int ystride = 2 * *width;
171 uint8_t *output = mlt_properties_get_data( producer_properties, "output_buffer", 0 );
174 output = mlt_pool_alloc( size );
176 // Let someone else clean up
177 mlt_properties_set_data( producer_properties, "output_buffer", output, size, mlt_pool_release, NULL );
180 uint8_t *first_image = mlt_properties_get_data( first_frame_properties, "image", NULL );
181 uint8_t *second_image = mlt_properties_get_data( second_frame_properties, "image", NULL );
183 // which frames are buffered?
187 if( first_image == NULL )
189 error = mlt_frame_get_image( first_frame, &first_image, format, width, height, writable );
192 fprintf(stderr, "first_image == NULL get image died\n");
197 if( second_image == NULL )
199 error = mlt_frame_get_image( second_frame, &second_image, format, width, height, writable );
202 fprintf(stderr, "second_image == NULL get image died\n");
207 // These need to passed onto the frame for other
208 mlt_properties_pass_list( frame_properties, second_frame_properties,
209 "motion_est.left_mb, motion_est.right_mb, \
210 motion_est.top_mb, motion_est.bottom_mb, \
211 motion_est.macroblock_width, motion_est.macroblock_height" );
213 // Pass the pointer to the vectors without serializing
214 mlt_properties_set_data( frame_properties, "motion_est.vectors",
215 mlt_properties_get_data( second_frame_properties, "motion_est.vectors", NULL ),
219 // Start with a base image
220 memcpy( output, first_image, size );
222 if( mlt_properties_get_int( producer_properties, "method" ) == 1 ) {
224 mlt_position first_position = mlt_frame_get_position( first_frame );
225 double actual_position = mlt_producer_get_speed( producer ) * (double)mlt_frame_get_position( this );
226 double scale = actual_position - first_position;
230 first_image, second_image, output,
231 mlt_properties_get_int( second_frame_properties, "motion_est.top_mb" ),
232 mlt_properties_get_int( second_frame_properties, "motion_est.bottom_mb" ),
233 mlt_properties_get_int( second_frame_properties, "motion_est.left_mb" ),
234 mlt_properties_get_int( second_frame_properties, "motion_est.right_mb" ),
235 mlt_properties_get_int( second_frame_properties, "motion_est.macroblock_width" ),
236 mlt_properties_get_int( second_frame_properties, "motion_est.macroblock_height" ),
240 mlt_properties_get_data( second_frame_properties, "motion_est.vectors", NULL )
243 if( mlt_properties_get_int( producer_properties, "debug" ) == 1 ) {
244 mlt_filter watermark = mlt_properties_get_data( producer_properties, "watermark", NULL );
246 if( watermark == NULL ) {
247 mlt_profile profile = mlt_service_profile( MLT_PRODUCER_SERVICE( producer ) );
248 watermark = mlt_factory_filter( profile, "watermark", NULL );
249 mlt_properties_set_data( producer_properties, "watermark", watermark, 0, (mlt_destructor)mlt_filter_close, NULL );
250 mlt_producer_attach( producer, watermark );
253 mlt_properties wm_properties = MLT_FILTER_PROPERTIES( watermark );
256 sprintf(disp, "+%10.2f.txt", actual_position);
257 mlt_properties_set( wm_properties, "resource", disp );
264 mlt_properties_set_data( frame_properties, "image", output, size, NULL, NULL );
266 // Make sure that no further scaling is done
267 mlt_properties_set( frame_properties, "rescale.interps", "none" );
268 mlt_properties_set( frame_properties, "scale", "off" );
270 mlt_frame_close( first_frame );
271 mlt_frame_close( second_frame );
276 static int slowmotion_get_frame( mlt_producer this, mlt_frame_ptr frame, int index )
278 // Construct a new frame
279 *frame = mlt_frame_init( MLT_PRODUCER_SERVICE( this ) );
281 mlt_properties properties = MLT_PRODUCER_PROPERTIES(this);
287 mlt_frame first_frame = mlt_properties_get_data( properties, "first_frame", NULL );
288 mlt_frame second_frame = mlt_properties_get_data( properties, "second_frame", NULL );
290 mlt_position first_position = (first_frame != NULL) ? mlt_frame_get_position( first_frame ) : -1;
291 mlt_position second_position = (second_frame != NULL) ? mlt_frame_get_position( second_frame ) : -1;
293 // Get the real producer
294 mlt_producer real_producer = mlt_properties_get_data( properties, "producer", NULL );
296 // Our "in" needs to be the same, keep it so
297 mlt_properties_pass_list( MLT_PRODUCER_PROPERTIES( real_producer ), properties, "in" );
299 // Calculate our positions
300 double actual_position = mlt_producer_get_speed( this ) * (double)mlt_producer_position( this );
301 mlt_position need_first = floor( actual_position );
302 mlt_position need_second = need_first + 1;
304 if( need_first != first_position )
306 mlt_frame_close( first_frame );
311 if( need_second != second_position)
313 mlt_frame_close( second_frame );
314 second_position = -1;
318 if( first_frame == NULL )
320 // Seek the producer to the correct place
321 mlt_producer_seek( real_producer, need_first );
324 mlt_service_get_frame( MLT_PRODUCER_SERVICE( real_producer ), &first_frame, index );
327 if( second_frame == NULL )
329 // Seek the producer to the correct place
330 mlt_producer_seek( real_producer, need_second );
333 mlt_service_get_frame( MLT_PRODUCER_SERVICE( real_producer ), &second_frame, index );
336 // Make sure things are in their place
337 mlt_properties_set_data( properties, "first_frame", first_frame, 0, NULL, NULL );
338 mlt_properties_set_data( properties, "second_frame", second_frame, 0, NULL, NULL );
340 mlt_properties_set_int( MLT_FRAME_PROPERTIES( *frame ), "test_image", 0 );
342 // Stack the producer and producer's get image
343 mlt_frame_push_service( *frame, first_frame );
344 mlt_properties_inc_ref( MLT_FRAME_PROPERTIES( first_frame ) );
346 mlt_frame_push_service( *frame, second_frame );
347 mlt_properties_inc_ref( MLT_FRAME_PROPERTIES( second_frame ) );
349 mlt_frame_push_service( *frame, this );
350 mlt_frame_push_service( *frame, slowmotion_get_image );
352 // Give the returned frame temporal identity
353 mlt_frame_set_position( *frame, mlt_producer_position( this ) );
359 mlt_producer producer_slowmotion_init( mlt_profile profile, mlt_service_type type, const char *id, char *arg )
361 mlt_producer this = mlt_producer_new( );
364 mlt_producer real_producer = mlt_factory_producer( profile, NULL, arg );
366 // We need to apply the motion estimation filter manually
367 mlt_filter filter = mlt_factory_filter( profile, "motion_est", NULL );
369 if ( this != NULL && real_producer != NULL && filter != NULL)
371 // attach the motion_est filter to the real producer
372 mlt_producer_attach( real_producer, filter );
374 // Get the properties of this producer
375 mlt_properties properties = MLT_PRODUCER_PROPERTIES( this );
377 // The loader normalised it for us already
378 mlt_properties_set_int( properties, "loader_normalised", 1);
380 // Store the producer and fitler
381 mlt_properties_set_data( properties, "producer", real_producer, 0, ( mlt_destructor )mlt_producer_close, NULL );
382 mlt_properties_set_data( properties, "motion_est", filter, 0, ( mlt_destructor )mlt_filter_close, NULL );
383 mlt_properties_set_int( MLT_FILTER_PROPERTIES( filter ), "macroblock_width", 16 );
384 mlt_properties_set_int( MLT_FILTER_PROPERTIES( filter ), "macroblock_height", 16 );
385 mlt_properties_set_int( MLT_FILTER_PROPERTIES( filter ), "denoise", 0 );
387 // Grap some stuff from the real_producer
388 mlt_properties_pass_list( properties, MLT_PRODUCER_PROPERTIES( real_producer ),
389 "in, out, length, resource" );
391 // Since we control the seeking, prevent it from seeking on its own
392 mlt_producer_set_speed( real_producer, 0 );
394 //mlt_properties_set( properties, "method", "onefield" );
396 // Override the get_frame method
397 this->get_frame = slowmotion_get_frame;
403 mlt_producer_close( this );
405 mlt_producer_close( real_producer );
407 mlt_filter_close( filter );