]> git.sesse.net Git - mlt/blob - src/modules/frei0r/factory.c
Merge pull request #10 from mcfrisk/coverity_review_fixes_v2
[mlt] / src / modules / frei0r / factory.c
1 /*
2  * factory.c -- the factory method interfaces
3  * Copyright (c) 2008 Marco Gittler <g.marco@freenet.de>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library 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 GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18  */
19
20 #include <string.h>
21 #include <framework/mlt.h>
22 #include <frei0r.h>
23
24 #include <stddef.h>
25 #include <stdio.h>
26 #include <sys/stat.h>
27 #include <sys/types.h>
28 #include <dirent.h>
29 #include <dlfcn.h>
30 #include <stdlib.h>
31 #include <limits.h>
32 #include <math.h>
33
34
35 #if defined(WIN32)
36 #define LIBSUF ".dll"
37 #define FREI0R_PLUGIN_PATH "\\lib\\frei0r-1"
38 #elif defined(__DARWIN__) && defined(RELOCATABLE)
39 #define LIBSUF ".so"
40 #define FREI0R_PLUGIN_PATH "/lib/frei0r-1"
41 #else
42 #define LIBSUF ".so"
43 #define FREI0R_PLUGIN_PATH "/usr/lib/frei0r-1:/usr/lib64/frei0r-1:/opt/local/lib/frei0r-1:/usr/local/lib/frei0r-1:$HOME/.frei0r-1/lib"
44 #endif
45
46 #define GET_FREI0R_PATH (getenv("FREI0R_PATH") ? getenv("FREI0R_PATH") : getenv("MLT_FREI0R_PLUGIN_PATH") ? getenv("MLT_FREI0R_PLUGIN_PATH") : FREI0R_PLUGIN_PATH)
47
48 extern mlt_filter filter_frei0r_init( mlt_profile profile, mlt_service_type type, const char *id, char *arg );
49 extern mlt_frame filter_process( mlt_filter this, mlt_frame frame );
50 extern void filter_close( mlt_filter this );
51 extern int producer_get_frame( mlt_producer producer, mlt_frame_ptr frame, int index );
52 extern void producer_close( mlt_producer this );
53 extern void transition_close( mlt_transition this );
54 extern mlt_frame transition_process( mlt_transition transition, mlt_frame a_frame, mlt_frame b_frame );
55
56 static char* get_frei0r_path()
57 {
58 #ifdef WIN32
59         char *dirname = malloc( strlen( mlt_environment( "MLT_APPDIR" ) ) + strlen( FREI0R_PLUGIN_PATH ) + 1 );
60         strcpy( dirname, mlt_environment( "MLT_APPDIR" ) );
61         strcat( dirname, FREI0R_PLUGIN_PATH );
62         return dirname;
63 #elif defined(__DARWIN__) && defined(RELOCATABLE)
64         char *dirname = malloc( strlen( mlt_environment( "MLT_APPDIR" ) ) + strlen( FREI0R_PLUGIN_PATH ) + 1 );
65         strcpy( dirname, mlt_environment( "MLT_APPDIR" ) );
66         strcat( dirname, FREI0R_PLUGIN_PATH );
67         return dirname;
68 #else
69         return strdup( GET_FREI0R_PATH );
70 #endif
71 }
72
73 static void check_thread_safe( mlt_properties properties, const char *name )
74 {
75         char dirname[PATH_MAX];
76         snprintf( dirname, PATH_MAX, "%s/frei0r/not_thread_safe.txt", mlt_environment( "MLT_DATA" ) );
77         mlt_properties not_thread_safe = mlt_properties_load( dirname );
78         int i;
79
80         for ( i = 0; i < mlt_properties_count( not_thread_safe ); i++ )
81         {
82                 if ( strcmp( name, mlt_properties_get_name( not_thread_safe, i ) ) == 0 )
83                 {
84                         mlt_properties_set_int( properties, "_not_thread_safe", 1 );
85                         break;
86                 }
87         }
88         mlt_properties_close( not_thread_safe );
89 }
90
91 static mlt_properties fill_param_info ( mlt_service_type type, const char *service_name, char *name )
92 {
93         char file[ PATH_MAX ];
94         char servicetype[ 1024 ]="";
95         struct stat stat_buff;
96
97         switch ( type ) {
98                 case producer_type:
99                         strcpy ( servicetype , "producer" );
100                         break;
101                 case filter_type:
102                         strcpy ( servicetype , "filter" );
103                         break;
104                 case transition_type:
105                         strcpy ( servicetype , "transition" ) ;
106                         break;
107                 default:
108                         strcpy ( servicetype , "" );
109         };
110
111         snprintf( file, PATH_MAX, "%s/frei0r/%s_%s.yml", mlt_environment( "MLT_DATA" ), servicetype, service_name );
112         memset(&stat_buff, 0, sizeof(stat_buff));
113         stat(file,&stat_buff);
114
115         if (S_ISREG(stat_buff.st_mode)){
116                 return mlt_properties_parse_yaml( file );
117         }
118
119         void* handle=dlopen(name,RTLD_LAZY);
120         if (!handle) return NULL;
121         void (*plginfo)(f0r_plugin_info_t*)=dlsym(handle,"f0r_get_plugin_info");
122         void (*param_info)(f0r_param_info_t*,int param_index)=dlsym(handle,"f0r_get_param_info");
123         void (*f0r_init)(void)=dlsym(handle,"f0r_init");
124         void (*f0r_deinit)(void)=dlsym(handle,"f0r_deinit");
125         if (!plginfo || !param_info) {
126                 dlclose(handle);
127                 return NULL;
128         }
129         mlt_properties metadata = mlt_properties_new();
130         f0r_plugin_info_t info;
131         char string[48];
132         int j=0;
133
134         f0r_init();
135         plginfo(&info);
136         snprintf ( string, sizeof(string) , "%d" , info.minor_version );
137         mlt_properties_set_double ( metadata, "schema_version" , 0.1 );
138         mlt_properties_set ( metadata, "title" , info.name );
139         mlt_properties_set_double ( metadata, "version",
140                 info.major_version +  info.minor_version / pow( 10, strlen( string ) ) );
141         mlt_properties_set ( metadata, "identifier" , service_name );
142         mlt_properties_set ( metadata, "description" , info.explanation );
143         mlt_properties_set ( metadata, "creator" , info.author );
144         switch (type){
145                 case producer_type:
146                         mlt_properties_set ( metadata, "type" , "producer" );
147                         break;
148                 case filter_type:
149                         mlt_properties_set ( metadata, "type" , "filter" );
150                         break;
151                 case transition_type:
152                         mlt_properties_set ( metadata, "type" , "transition" );
153                         break;
154                 default:
155                         break;
156         }
157
158         mlt_properties parameter = mlt_properties_new ( );
159         mlt_properties_set_data ( metadata , "parameters" , parameter , 0 , ( mlt_destructor )mlt_properties_close, NULL );
160         mlt_properties tags = mlt_properties_new ( );
161         mlt_properties_set_data ( metadata , "tags" , tags , 0 , ( mlt_destructor )mlt_properties_close, NULL );
162         mlt_properties_set ( tags , "0" , "Video" );
163
164         for (j=0;j<info.num_params;j++){
165                 snprintf ( string , sizeof(string), "%d" , j );
166                 mlt_properties pnum = mlt_properties_new ( );
167                 mlt_properties_set_data ( parameter , string , pnum , 0 , ( mlt_destructor )mlt_properties_close, NULL );
168                 f0r_param_info_t paraminfo;
169                 param_info(&paraminfo,j);
170                 mlt_properties_set ( pnum , "identifier" , paraminfo.name );
171                 mlt_properties_set ( pnum , "title" , paraminfo.name );
172                 mlt_properties_set ( pnum , "description" , paraminfo.explanation);
173                 if ( paraminfo.type == F0R_PARAM_DOUBLE ){
174                         mlt_properties_set ( pnum , "type" , "float" );
175                         mlt_properties_set ( pnum , "minimum" , "0" );
176                         mlt_properties_set ( pnum , "maximum" , "1" );
177                         mlt_properties_set ( pnum , "readonly" , "no" );
178                         mlt_properties_set ( pnum , "widget" , "spinner" );
179                 }else
180                 if ( paraminfo.type == F0R_PARAM_BOOL ){
181                         mlt_properties_set ( pnum , "type" , "boolean" );
182                         mlt_properties_set ( pnum , "minimum" , "0" );
183                         mlt_properties_set ( pnum , "maximum" , "1" );
184                         mlt_properties_set ( pnum , "readonly" , "no" );
185                 }else
186                 if ( paraminfo.type == F0R_PARAM_COLOR ){
187                         mlt_properties_set ( pnum , "type" , "color" );
188                         mlt_properties_set ( pnum , "readonly" , "no" );
189                 }else
190                 if ( paraminfo.type == F0R_PARAM_STRING ){
191                         mlt_properties_set ( pnum , "type" , "string" );
192                         mlt_properties_set ( pnum , "readonly" , "no" );
193                 }
194         }
195         f0r_deinit();
196         dlclose(handle);
197         free(name);
198
199         return metadata;
200 }
201
202 static void * load_lib( mlt_profile profile, mlt_service_type type , void* handle, const char *name ){
203
204         int i=0;
205         void (*f0r_get_plugin_info)(f0r_plugin_info_t*),
206                 *f0r_construct , *f0r_update , *f0r_destruct,
207                 (*f0r_get_param_info)(f0r_param_info_t* info, int param_index),
208                 (*f0r_init)(void) , *f0r_deinit ,
209                 (*f0r_set_param_value)(f0r_instance_t instance, f0r_param_t param, int param_index),
210                 (*f0r_get_param_value)(f0r_instance_t instance, f0r_param_t param, int param_index),
211                 (*f0r_update2) (f0r_instance_t instance, double time,   const uint32_t* inframe1,
212                   const uint32_t* inframe2,const uint32_t* inframe3, uint32_t* outframe);
213
214         if ( ( f0r_construct = dlsym(handle, "f0r_construct") ) &&
215                                 (f0r_destruct = dlsym(handle,"f0r_destruct") ) &&
216                                 (f0r_get_plugin_info = dlsym(handle,"f0r_get_plugin_info") ) &&
217                                 (f0r_get_param_info = dlsym(handle,"f0r_get_param_info") ) &&
218                                 (f0r_set_param_value= dlsym(handle,"f0r_set_param_value" ) ) &&
219                                 (f0r_get_param_value= dlsym(handle,"f0r_get_param_value" ) ) &&
220                                 (f0r_init= dlsym(handle,"f0r_init" ) ) &&
221                                 (f0r_deinit= dlsym(handle,"f0r_deinit" ) )
222                 ){
223
224                 f0r_update=dlsym(handle,"f0r_update");
225                 f0r_update2=dlsym(handle,"f0r_update2");
226
227                 f0r_plugin_info_t info;
228                 f0r_get_plugin_info(&info);
229
230                 void* ret=NULL;
231                 mlt_properties properties=NULL;
232                 char minor[12];
233
234                 if (type == producer_type && info.plugin_type == F0R_PLUGIN_TYPE_SOURCE ){
235                         mlt_producer this = mlt_producer_new( profile );
236                         if ( this != NULL )
237                         {
238                                 this->get_frame = producer_get_frame;
239                                 this->close = ( mlt_destructor )producer_close;
240                                 f0r_init();
241                                 properties=MLT_PRODUCER_PROPERTIES ( this );
242
243                                 for (i=0;i<info.num_params;i++){
244                                         f0r_param_info_t pinfo;
245                                         f0r_get_param_info(&pinfo,i);
246
247                                 }
248
249                                 ret=this;
250                         }
251                 } else if (type == filter_type && info.plugin_type == F0R_PLUGIN_TYPE_FILTER ){
252                         mlt_filter this = mlt_filter_new( );
253                         if ( this != NULL )
254                         {
255                                 this->process = filter_process;
256                                 this->close = filter_close;
257                                 f0r_init();
258                                 properties=MLT_FILTER_PROPERTIES ( this );
259
260                                 for (i=0;i<info.num_params;i++){
261                                         f0r_param_info_t pinfo;
262                                         f0r_get_param_info(&pinfo,i);
263
264                                 }
265
266                                 ret=this;
267                         }
268                 }else if (type == transition_type && info.plugin_type == F0R_PLUGIN_TYPE_MIXER2){
269                         mlt_transition transition = mlt_transition_new( );
270                         if ( transition != NULL )
271                         {
272                                 transition->process = transition_process;
273                                 transition->close = transition_close;
274                                 properties=MLT_TRANSITION_PROPERTIES( transition );
275                                 mlt_properties_set_int(properties, "_transition_type", 1 );
276
277                                 ret=transition;
278                         }
279                 }
280                 check_thread_safe( properties, name );
281                 mlt_properties_set_data(properties, "_dlclose_handle", handle , sizeof ( handle ) , NULL , NULL );
282                 mlt_properties_set_data(properties, "_dlclose", dlclose , sizeof (void*) , NULL , NULL );
283                 mlt_properties_set_data(properties, "f0r_construct", f0r_construct , sizeof( f0r_construct ),NULL,NULL);
284                 mlt_properties_set_data(properties, "f0r_update", f0r_update , sizeof( f0r_update ),NULL,NULL);
285                 if (f0r_update2)
286                         mlt_properties_set_data(properties, "f0r_update2", f0r_update2 , sizeof( f0r_update2 ),NULL,NULL);
287                 mlt_properties_set_data(properties, "f0r_destruct", f0r_destruct , sizeof( f0r_destruct ),NULL,NULL);
288                 mlt_properties_set_data(properties, "f0r_get_plugin_info", f0r_get_plugin_info , sizeof(void*),NULL,NULL);
289                 mlt_properties_set_data(properties, "f0r_get_param_info", f0r_get_param_info , sizeof(void*),NULL,NULL);
290                 mlt_properties_set_data(properties, "f0r_set_param_value", f0r_set_param_value , sizeof(void*),NULL,NULL);
291                 mlt_properties_set_data(properties, "f0r_get_param_value", f0r_get_param_value , sizeof(void*),NULL,NULL);
292
293                 // Let frei0r plugin version be serialized using same format as metadata
294                 snprintf( minor, sizeof( minor ), "%d", info.minor_version );
295                 mlt_properties_set_double( properties, "version", info.major_version +  info.minor_version / pow( 10, strlen( minor ) ) );
296
297                 return ret;
298         }else{
299                 mlt_log_error( NULL, "frei0r plugin \"%s\" is missing a function\n", name );
300                 dlerror();
301         }
302         return NULL;
303 }
304
305 static void * create_frei0r_item ( mlt_profile profile, mlt_service_type type, const char *id, void *arg){
306
307         mlt_tokeniser tokeniser = mlt_tokeniser_init ( );
308         char *frei0r_path = get_frei0r_path();
309         int dircount=mlt_tokeniser_parse_new (
310                 tokeniser,
311                 frei0r_path,
312                  ":"
313         );
314         void* ret=NULL;
315         while (dircount--){
316                 char soname[PATH_MAX];
317                 char *myid = strdup( id );
318
319 #ifdef WIN32
320                 char *firstname = strtok( myid, "." );
321 #else
322                 char *save_firstptr = NULL;
323                 char *firstname = strtok_r( myid, ".", &save_firstptr );
324 #endif
325                 char* directory = mlt_tokeniser_get_string (tokeniser, dircount);
326
327 #ifdef WIN32
328                 firstname = strtok( NULL, "." );
329 #else
330                 firstname = strtok_r( NULL, ".", &save_firstptr );
331 #endif
332                 if (strncmp(directory, "$HOME", 5))
333                         snprintf(soname, PATH_MAX, "%s/%s" LIBSUF, directory, firstname );
334                 else
335                         snprintf(soname, PATH_MAX, "%s%s/%s" LIBSUF, getenv("HOME"), strchr(directory, '/'), firstname );
336
337                 if (firstname){
338
339                         void* handle=dlopen(soname,RTLD_LAZY);
340
341                         if (handle ){
342                                 ret=load_lib ( profile , type , handle, firstname );
343                         }else{
344                                 dlerror();
345                         }
346                 }
347                 free( myid );
348         }
349         mlt_tokeniser_close ( tokeniser );
350         free( frei0r_path );
351         return ret;
352 }
353
354
355 MLT_REPOSITORY
356 {
357         int i=0;
358         mlt_tokeniser tokeniser = mlt_tokeniser_init ( );
359         char *frei0r_path = get_frei0r_path();
360         int dircount=mlt_tokeniser_parse_new (
361                 tokeniser ,
362                 frei0r_path,
363                 ":"
364         );
365         char dirname[PATH_MAX];
366         snprintf( dirname, PATH_MAX, "%s/frei0r/blacklist.txt", mlt_environment( "MLT_DATA" ) );
367         mlt_properties blacklist = mlt_properties_load( dirname );
368
369         while (dircount--){
370
371                 mlt_properties direntries = mlt_properties_new();
372                 char* directory = mlt_tokeniser_get_string (tokeniser, dircount);
373                 
374                 if (strncmp(directory, "$HOME", 5))
375                         snprintf(dirname, PATH_MAX, "%s", directory);
376                 else
377                         snprintf(dirname, PATH_MAX, "%s%s", getenv("HOME"), strchr(directory, '/'));
378                 mlt_properties_dir_list(direntries, dirname ,"*" LIBSUF, 1);
379
380                 for (i=0; i<mlt_properties_count(direntries);i++){
381                         char* name=mlt_properties_get_value(direntries,i);
382                         char* shortname=name+strlen(dirname)+1;
383 #ifdef WIN32
384                         char* firstname = strtok( shortname, "." );
385 #else
386                         char *save_firstptr = NULL;
387                         char* firstname = strtok_r( shortname, ".", &save_firstptr );
388 #endif
389                         char pluginname[1024]="frei0r.";
390                         if ( firstname )
391                                 strncat( pluginname, firstname, sizeof( pluginname ) - strlen( pluginname ) -1 );
392
393                         if ( firstname && mlt_properties_get( blacklist, firstname ) )
394                                 continue;
395
396                         void* handle=dlopen(strcat(name, LIBSUF),RTLD_LAZY);
397                         if (handle){
398                                 void (*plginfo)(f0r_plugin_info_t*)=dlsym(handle,"f0r_get_plugin_info");
399
400                                 if (plginfo){
401                                         f0r_plugin_info_t info;
402                                         plginfo(&info);
403                                         if (firstname && info.plugin_type==F0R_PLUGIN_TYPE_SOURCE){
404                                                 if (mlt_properties_get(mlt_repository_producers(repository), pluginname))
405                                                 {
406                                                         dlclose(handle);
407                                                         continue;
408                                                 }
409                                                 MLT_REGISTER( producer_type, pluginname, create_frei0r_item );
410                                                 MLT_REGISTER_METADATA( producer_type, pluginname, fill_param_info, strdup(name) );
411                                         }
412                                         else if (firstname && info.plugin_type==F0R_PLUGIN_TYPE_FILTER){
413                                                 if (mlt_properties_get(mlt_repository_filters(repository), pluginname))
414                                                 {
415                                                         dlclose(handle);
416                                                         continue;
417                                                 }
418                                                 MLT_REGISTER( filter_type, pluginname, create_frei0r_item );
419                                                 MLT_REGISTER_METADATA( filter_type, pluginname, fill_param_info, strdup(name) );
420                                         }
421                                         else if (firstname && info.plugin_type==F0R_PLUGIN_TYPE_MIXER2 ){
422                                                 if (mlt_properties_get(mlt_repository_transitions(repository), pluginname))
423                                                 {
424                                                         dlclose(handle);
425                                                         continue;
426                                                 }
427                                                 MLT_REGISTER( transition_type, pluginname, create_frei0r_item );
428                                                 MLT_REGISTER_METADATA( transition_type, pluginname, fill_param_info, strdup(name) );
429                                         }
430                                 }
431                                 dlclose(handle);
432                         }
433                 }
434                 mlt_properties_close(direntries);
435         }
436         mlt_tokeniser_close ( tokeniser );
437         mlt_properties_close( blacklist );
438         free( frei0r_path );
439 }