2 * filter_imagestab.c -- video stabilization with code from http://vstab.sourceforge.net/
3 * Copyright (c) 2011 Marco Gittler <g.marco@freenet.de>
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 <framework/mlt_filter.h>
21 #include <framework/mlt_frame.h>
22 #include <framework/mlt_log.h>
23 #include <framework/mlt_producer.h>
24 #include <framework/mlt_geometry.h>
32 #include "stabilize.h"
33 #include "transform_image.h"
42 static void serialize_vectors( videostab2_data* self, mlt_position length )
44 mlt_geometry g = mlt_geometry_init();
47 struct mlt_geometry_item_s item;
50 // Initialize geometry item
51 item.key = item.f[0] = item.f[1] = item.f[2] = item.f[3] = 1;
54 tlist* transform_data =self->stab->transs;
55 for ( i = 0; i < length; i++ )
57 // Set the geometry item
60 if ( transform_data->data){
61 Transform* t=transform_data->data;
66 transform_data=transform_data->next;
69 // Add the geometry item
70 mlt_geometry_insert( g, &item );
73 // Put the analysis results in a property
74 mlt_geometry_set_length( g, length );
75 mlt_properties_set_data( MLT_FILTER_PROPERTIES( (mlt_filter) self->parent ), "vectors", g, 0,
76 (mlt_destructor) mlt_geometry_close, (mlt_serialiser) mlt_geometry_serialise );
79 // scale zoom implements the factor that the vetcors must be scaled since the vector is calulated for real with, now we need it for (scaled)width
80 Transform* deserialize_vectors( char *vectors, mlt_position length ,float scale_zoom )
82 mlt_geometry g = mlt_geometry_init();
84 // Parse the property as a geometry
85 if ( g && !mlt_geometry_parse( g, vectors, length, -1, -1 ) )
87 struct mlt_geometry_item_s item;
89 tx=calloc(1,sizeof(Transform)*length);
90 // Copy the geometry items to a vc array for interp()
91 for ( i = 0; i < length; i++ )
93 mlt_geometry_fetch( g, &item, i );
95 t.x=scale_zoom*item.x;
96 t.y=scale_zoom*item.y;
98 t.zoom=scale_zoom*item.h;
106 //mlt_log_warning( NULL, "failed to parse vectors\n" );
109 // We are done with this mlt_geometry
110 if ( g ) mlt_geometry_close( g );
114 static int filter_get_image( mlt_frame frame, uint8_t **image, mlt_image_format *format, int *width, int *height, int writable )
116 mlt_filter filter = mlt_frame_pop_service( frame );
117 char *vectors = mlt_properties_get( MLT_FILTER_PROPERTIES(filter), "vectors" );
118 *format = mlt_image_yuv422;
120 *format= mlt_image_rgb24;
121 mlt_properties_set_int( MLT_FRAME_PROPERTIES(frame), "consumer_deinterlace", 1 );
122 int error = mlt_frame_get_image( frame, image, format, width, height, 1 );
124 if ( !error && *image )
126 videostab2_data* data = filter->child;
127 if ( data==NULL ) { // big error, abort
130 mlt_position length = mlt_filter_get_length2( filter, frame );
134 // Service locks are for concurrency control
135 mlt_service_lock( MLT_FILTER_SERVICE( filter ) );
137 // Handle signal from app to re-init data
138 if ( mlt_properties_get_int( MLT_FILTER_PROPERTIES(filter) , "refresh" ) )
140 mlt_properties_set( MLT_FILTER_PROPERTIES(filter) , "refresh", NULL );
141 data->initialized = 0;
145 if ( !data->initialized )
147 // Initialize our context
148 data->initialized = 1;
150 data->stab->height=h;
151 if (*format==mlt_image_yuv420p) data->stab->framesize=w*h* 3/2;//( mlt_image_format_size ( *format, w,h , 0) ; // 3/2 =1 too small
152 if (*format==mlt_image_yuv422) data->stab->framesize=w*h;
153 data->stab->shakiness = mlt_properties_get_int( MLT_FILTER_PROPERTIES(filter) , "shakiness" );
154 data->stab->accuracy = mlt_properties_get_int( MLT_FILTER_PROPERTIES(filter) , "accuracy" );
155 data->stab->stepsize = mlt_properties_get_int( MLT_FILTER_PROPERTIES(filter) , "stepsize" );
156 data->stab->algo = mlt_properties_get_int( MLT_FILTER_PROPERTIES(filter) , "algo" );
157 data->stab->show = mlt_properties_get_int( MLT_FILTER_PROPERTIES(filter) , "show" );
158 data->stab->contrast_threshold = mlt_properties_get_double( MLT_FILTER_PROPERTIES(filter) , "mincontrast" );
159 stabilize_configure(data->stab);
162 mlt_position pos = mlt_filter_get_position( filter, frame );
163 stabilize_filter_video ( data->stab , *image, *format );
166 if ( pos == length - 1 )
168 serialize_vectors( data , length );
173 if ( data->initialized!=1 )
175 char *interps = mlt_properties_get( MLT_FRAME_PROPERTIES( frame ), "rescale.interp" );
177 if ( data->initialized != 2 )
179 // Load analysis results from property
180 data->initialized = 2;
182 int interp = 2; // default to bilinear
183 float scale_zoom=1.0;
184 if ( *width != mlt_properties_get_int( MLT_FRAME_PROPERTIES( frame ), "meta.media.width" ) )
185 scale_zoom = (float) *width / (float) mlt_properties_get_int( MLT_FRAME_PROPERTIES( frame ), "meta.media.width" );
186 if ( strcmp( interps, "nearest" ) == 0 || strcmp( interps, "neighbor" ) == 0 )
188 else if ( strcmp( interps, "tiles" ) == 0 || strcmp( interps, "fast_bilinear" ) == 0 )
191 data->trans->interpoltype = interp;
192 data->trans->smoothing = mlt_properties_get_int( MLT_FILTER_PROPERTIES(filter), "smoothing" );
193 data->trans->maxshift = mlt_properties_get_int( MLT_FILTER_PROPERTIES(filter), "maxshift" );
194 data->trans->maxangle = mlt_properties_get_double( MLT_FILTER_PROPERTIES(filter), "maxangle" );
195 data->trans->crop = mlt_properties_get_int( MLT_FILTER_PROPERTIES(filter), "crop" );
196 data->trans->invert = mlt_properties_get_int( MLT_FILTER_PROPERTIES(filter), "invert" );
197 data->trans->relative = mlt_properties_get_int( MLT_FILTER_PROPERTIES(filter), "relative" );
198 data->trans->zoom = mlt_properties_get_int( MLT_FILTER_PROPERTIES(filter), "zoom" );
199 data->trans->optzoom = mlt_properties_get_int( MLT_FILTER_PROPERTIES(filter), "optzoom" );
200 data->trans->sharpen = mlt_properties_get_double( MLT_FILTER_PROPERTIES(filter), "sharpen" );
202 transform_configure(data->trans,w,h,*format ,*image, deserialize_vectors( vectors, length , scale_zoom ),length);
205 if ( data->initialized == 2 )
208 float pos = mlt_filter_get_position( filter, frame );
209 data->trans->current_trans=pos;
210 transform_filter_video(data->trans, *image, *format );
215 mlt_service_unlock( MLT_FILTER_SERVICE( filter ) );
220 static mlt_frame filter_process( mlt_filter filter, mlt_frame frame )
222 mlt_frame_push_service( frame, filter );
223 mlt_frame_push_get_image( frame, filter_get_image );
227 static void filter_close( mlt_filter parent )
229 videostab2_data* data = parent->child;
231 if (data->stab) stabilize_stop(data->stab);
233 if (data->trans->src) free(data->trans->src);
238 parent->close = NULL;
239 parent->child = NULL;
242 mlt_filter filter_videostab2_init( mlt_profile profile, mlt_service_type type, const char *id, char *arg )
244 videostab2_data* data= calloc( 1, sizeof(videostab2_data));
247 data->stab = calloc( 1, sizeof(StabData) );
254 data->trans = calloc( 1, sizeof (TransformData) ) ;
262 mlt_filter parent = mlt_filter_new();
271 parent->child = data;
272 parent->close = filter_close;
273 parent->process = filter_process;
274 data->parent = parent;
275 //properties for stabilize
276 mlt_properties_set( MLT_FILTER_PROPERTIES(parent), "shakiness", "4" );
277 mlt_properties_set( MLT_FILTER_PROPERTIES(parent), "accuracy", "4" );
278 mlt_properties_set( MLT_FILTER_PROPERTIES(parent), "stepsize", "6" );
279 mlt_properties_set( MLT_FILTER_PROPERTIES(parent), "algo", "1" );
280 mlt_properties_set( MLT_FILTER_PROPERTIES(parent), "mincontrast", "0.3" );
281 mlt_properties_set( MLT_FILTER_PROPERTIES(parent), "show", "0" );
283 //properties for transform
284 mlt_properties_set( MLT_FILTER_PROPERTIES(parent), "smoothing", "10" );
285 mlt_properties_set( MLT_FILTER_PROPERTIES(parent), "maxshift", "-1" );
286 mlt_properties_set( MLT_FILTER_PROPERTIES(parent), "maxangle", "-1" );
287 mlt_properties_set( MLT_FILTER_PROPERTIES(parent), "crop", "0" );
288 mlt_properties_set( MLT_FILTER_PROPERTIES(parent), "invert", "0" );
289 mlt_properties_set( MLT_FILTER_PROPERTIES(parent), "relative", "1" );
290 mlt_properties_set( MLT_FILTER_PROPERTIES(parent), "zoom", "0" );
291 mlt_properties_set( MLT_FILTER_PROPERTIES(parent), "optzoom", "1" );
292 mlt_properties_set( MLT_FILTER_PROPERTIES(parent), "sharpen", "0.8" );