]> git.sesse.net Git - mlt/blob - src/modules/videostab/filter_videostab2.c
Fix compile error on Windows.
[mlt] / src / modules / videostab / filter_videostab2.c
1 /*
2  * filter_imagestab.c -- video stabilization with code from http://vstab.sourceforge.net/
3  * Copyright (c) 2011 Marco Gittler <g.marco@freenet.de>
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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18  */
19
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>
25
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <math.h>
29 #include <sys/stat.h>
30 #include <string.h>
31
32 #include "stabilize.h"
33 #include "transform_image.h"
34
35 typedef struct {
36         StabData* stab;
37         TransformData* trans;
38         int initialized;
39         void* parent;
40 } videostab2_data;
41
42 static void serialize_vectors( videostab2_data* self, mlt_position length )
43 {
44         mlt_geometry g = mlt_geometry_init();
45         if ( g )
46         {
47                 struct mlt_geometry_item_s item;
48                 mlt_position i;
49
50                 // Initialize geometry item
51                 item.key = item.f[0] = item.f[1] = item.f[2] = item.f[3] = 1;
52                 item.f[4] = 0;
53
54                 tlist* transform_data =self->stab->transs;
55                 for ( i = 0; i < length; i++ )
56                 {
57                         // Set the geometry item
58                         item.frame = i;
59                         if (transform_data){
60                                 if ( transform_data->data){
61                                         Transform* t=transform_data->data;
62                                         item.x=t->x;
63                                         item.y=t->y;
64                                         item.w=t->alpha;
65                                         item.h=t->zoom;
66                                         transform_data=transform_data->next;
67                                 }
68                         }
69                         // Add the geometry item
70                         mlt_geometry_insert( g, &item );
71                 }
72
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 );
77         }
78 }
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 )
81 {
82         mlt_geometry g = mlt_geometry_init();
83         Transform* tx=NULL;
84         // Parse the property as a geometry
85         if ( g && !mlt_geometry_parse( g, vectors, length, -1, -1 ) )
86         {
87                 struct mlt_geometry_item_s item;
88                 int i;
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++ )
92                 {
93                         mlt_geometry_fetch( g, &item, i );
94                         Transform t;
95                         t.x=scale_zoom*item.x;
96                         t.y=scale_zoom*item.y;
97                         t.alpha=item.w;
98                         t.zoom=scale_zoom*item.h;
99                         t.extra=0;
100                         tx[i]=t;
101                 }
102
103         }
104         else
105         {
106                 //mlt_log_warning( NULL, "failed to parse vectors\n" );
107         }
108
109         // We are done with this mlt_geometry
110         if ( g ) mlt_geometry_close( g );
111         return tx;
112 }
113
114 static int filter_get_image( mlt_frame frame, uint8_t **image, mlt_image_format *format, int *width, int *height, int writable )
115 {
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;
119         if (vectors)
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 );
123
124         if ( !error && *image )
125         {
126                 videostab2_data* data = filter->child;
127                 if ( data==NULL ) { // big error, abort
128                         return 1;
129                 }
130                 mlt_position length = mlt_filter_get_length2( filter, frame );
131                 int h = *height;
132                 int w = *width;
133
134                 // Service locks are for concurrency control
135                 mlt_service_lock( MLT_FILTER_SERVICE( filter ) );
136
137                 // Handle signal from app to re-init data
138                 if ( mlt_properties_get_int( MLT_FILTER_PROPERTIES(filter) , "refresh" ) )
139                 {
140                         mlt_properties_set( MLT_FILTER_PROPERTIES(filter) , "refresh", NULL );
141                         data->initialized = 0;
142                 }
143
144                 if ( !vectors) {
145                         if ( !data->initialized )
146                         {
147                                 // Initialize our context
148                                 data->initialized = 1;
149                                 data->stab->width=w;
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);
160                         }
161                                 // Analyse
162                                 mlt_position pos = mlt_filter_get_position( filter, frame );
163                                 stabilize_filter_video ( data->stab , *image, *format );
164
165                                 // On last frame
166                                 if ( pos == length - 1 )
167                                 {
168                                         serialize_vectors( data , length );
169                                 }
170                 }
171                 else
172                 {
173                         if ( data->initialized!=1  )
174                         {
175                                 char *interps = mlt_properties_get( MLT_FRAME_PROPERTIES( frame ), "rescale.interp" );
176
177                                 if ( data->initialized != 2 )
178                                 {
179                                         // Load analysis results from property
180                                         data->initialized = 2;
181
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 )
187                                                 interp = 0;
188                                         else if ( strcmp( interps, "tiles" ) == 0 || strcmp( interps, "fast_bilinear" ) == 0 )
189                                                 interp = 1;
190
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" );
201
202                                         transform_configure(data->trans,w,h,*format ,*image, deserialize_vectors(  vectors, length , scale_zoom ),length);
203
204                                 }
205                                 if ( data->initialized == 2 )
206                                 {
207                                         // Stabilize
208                                         float pos = mlt_filter_get_position( filter, frame );
209                                         data->trans->current_trans=pos;
210                                         transform_filter_video(data->trans, *image, *format );
211
212                                 }
213                 }
214                 }
215                 mlt_service_unlock( MLT_FILTER_SERVICE( filter ) );
216         }
217         return error;
218 }
219
220 static mlt_frame filter_process( mlt_filter filter, mlt_frame frame )
221 {
222         mlt_frame_push_service( frame, filter );
223         mlt_frame_push_get_image( frame, filter_get_image );
224         return frame;
225 }
226
227 static void filter_close( mlt_filter parent )
228 {
229         videostab2_data* data = parent->child;
230         if (data){
231                 if (data->stab) stabilize_stop(data->stab);
232                 if (data->trans){
233                         if (data->trans->src) free(data->trans->src);
234                         free (data->trans);
235                 }
236                 free( data );
237         }
238         parent->close = NULL;
239         parent->child = NULL;
240 }
241
242 mlt_filter filter_videostab2_init( mlt_profile profile, mlt_service_type type, const char *id, char *arg )
243 {
244         videostab2_data* data= calloc( 1, sizeof(videostab2_data));
245         if ( data )
246         {
247                 data->stab = calloc( 1, sizeof(StabData) );
248                 if ( !data->stab )
249                 {
250                         free( data );
251                         return NULL;
252                 }
253
254                 data->trans = calloc( 1, sizeof (TransformData) ) ;
255                 if ( !data->trans )
256                 {
257                         free( data->stab );
258                         free( data );
259                         return NULL;
260                 }
261
262                 mlt_filter parent = mlt_filter_new();
263                 if ( !parent )
264                 {
265                         free( data->trans );
266                         free( data->stab );
267                         free( data );
268                         return NULL;
269                 }
270
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" );
282
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" );
293                 return parent;
294         }
295         return NULL;
296 }