]> git.sesse.net Git - mlt/blob - src/modules/frei0r/factory.c
Merge branch 'frei0r-metadata'
[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 #define CLAMP( x, min, max ) ((x) < (min) ? (min) : (x) > (max) ? (max) : (x))
49
50 extern mlt_filter filter_frei0r_init( mlt_profile profile, mlt_service_type type, const char *id, char *arg );
51 extern mlt_frame filter_process( mlt_filter this, mlt_frame frame );
52 extern void filter_close( mlt_filter this );
53 extern int producer_get_frame( mlt_producer producer, mlt_frame_ptr frame, int index );
54 extern void producer_close( mlt_producer this );
55 extern void transition_close( mlt_transition this );
56 extern mlt_frame transition_process( mlt_transition transition, mlt_frame a_frame, mlt_frame b_frame );
57
58 static char* get_frei0r_path()
59 {
60 #ifdef WIN32
61         char *dirname = malloc( strlen( mlt_environment( "MLT_APPDIR" ) ) + strlen( FREI0R_PLUGIN_PATH ) + 1 );
62         strcpy( dirname, mlt_environment( "MLT_APPDIR" ) );
63         strcat( dirname, FREI0R_PLUGIN_PATH );
64         return dirname;
65 #elif defined(__DARWIN__) && defined(RELOCATABLE)
66         char *dirname = malloc( strlen( mlt_environment( "MLT_APPDIR" ) ) + strlen( FREI0R_PLUGIN_PATH ) + 1 );
67         strcpy( dirname, mlt_environment( "MLT_APPDIR" ) );
68         strcat( dirname, FREI0R_PLUGIN_PATH );
69         return dirname;
70 #else
71         return strdup( GET_FREI0R_PATH );
72 #endif
73 }
74
75 static void check_thread_safe( mlt_properties properties, const char *name )
76 {
77         char dirname[PATH_MAX];
78         snprintf( dirname, PATH_MAX, "%s/frei0r/not_thread_safe.txt", mlt_environment( "MLT_DATA" ) );
79         mlt_properties not_thread_safe = mlt_properties_load( dirname );
80         int i;
81
82         for ( i = 0; i < mlt_properties_count( not_thread_safe ); i++ )
83         {
84                 if ( strcmp( name, mlt_properties_get_name( not_thread_safe, i ) ) == 0 )
85                 {
86                         mlt_properties_set_int( properties, "_not_thread_safe", 1 );
87                         break;
88                 }
89         }
90         mlt_properties_close( not_thread_safe );
91 }
92
93 static mlt_properties fill_param_info ( mlt_service_type type, const char *service_name, char *name )
94 {
95         char file[ PATH_MAX ];
96         char servicetype[ 1024 ]="";
97         struct stat stat_buff;
98
99         switch ( type ) {
100                 case producer_type:
101                         strcpy ( servicetype , "producer" );
102                         break;
103                 case filter_type:
104                         strcpy ( servicetype , "filter" );
105                         break;
106                 case transition_type:
107                         strcpy ( servicetype , "transition" ) ;
108                         break;
109                 default:
110                         strcpy ( servicetype , "" );
111         };
112
113         snprintf( file, PATH_MAX, "%s/frei0r/%s_%s.yml", mlt_environment( "MLT_DATA" ), servicetype, service_name );
114         memset(&stat_buff, 0, sizeof(stat_buff));
115         stat(file,&stat_buff);
116
117         if (S_ISREG(stat_buff.st_mode)){
118                 return mlt_properties_parse_yaml( file );
119         }
120
121         void* handle=dlopen(name,RTLD_LAZY);
122         if (!handle) return NULL;
123         void (*plginfo)(f0r_plugin_info_t*)=dlsym(handle,"f0r_get_plugin_info");
124         void (*param_info)(f0r_param_info_t*,int param_index)=dlsym(handle,"f0r_get_param_info");
125         void (*f0r_init)(void)=dlsym(handle,"f0r_init");
126         void (*f0r_deinit)(void)=dlsym(handle,"f0r_deinit");
127         f0r_instance_t (*f0r_construct)(unsigned int , unsigned int)=dlsym(handle, "f0r_construct");
128         void (*f0r_destruct)(f0r_instance_t)=dlsym(handle, "f0r_destruct");
129         void (*f0r_get_param_value)(f0r_instance_t instance, f0r_param_t param, int param_index)=dlsym(handle,"f0r_get_param_value" );
130         if (!plginfo || !param_info) {
131                 dlclose(handle);
132                 return NULL;
133         }
134         mlt_properties metadata = mlt_properties_new();
135         f0r_plugin_info_t info;
136         char string[48];
137         int j=0;
138
139         f0r_init();
140         f0r_instance_t instance = f0r_construct(720, 576);
141         if (!instance) {
142                 f0r_deinit();
143                 dlclose(handle);
144                 return NULL;
145         }
146         plginfo(&info);
147         snprintf ( string, sizeof(string) , "%d" , info.minor_version );
148         mlt_properties_set_double ( metadata, "schema_version" , 0.1 );
149         mlt_properties_set ( metadata, "title" , info.name );
150         mlt_properties_set_double ( metadata, "version",
151                 info.major_version +  info.minor_version / pow( 10, strlen( string ) ) );
152         mlt_properties_set ( metadata, "identifier" , service_name );
153         mlt_properties_set ( metadata, "description" , info.explanation );
154         mlt_properties_set ( metadata, "creator" , info.author );
155         switch (type){
156                 case producer_type:
157                         mlt_properties_set ( metadata, "type" , "producer" );
158                         break;
159                 case filter_type:
160                         mlt_properties_set ( metadata, "type" , "filter" );
161                         break;
162                 case transition_type:
163                         mlt_properties_set ( metadata, "type" , "transition" );
164                         break;
165                 default:
166                         break;
167         }
168
169         mlt_properties parameter = mlt_properties_new ( );
170         mlt_properties_set_data ( metadata , "parameters" , parameter , 0 , ( mlt_destructor )mlt_properties_close, NULL );
171         mlt_properties tags = mlt_properties_new ( );
172         mlt_properties_set_data ( metadata , "tags" , tags , 0 , ( mlt_destructor )mlt_properties_close, NULL );
173         mlt_properties_set ( tags , "0" , "Video" );
174
175         for (j=0;j<info.num_params;j++){
176                 snprintf ( string , sizeof(string), "%d" , j );
177                 mlt_properties pnum = mlt_properties_new ( );
178                 mlt_properties_set_data ( parameter , string , pnum , 0 , ( mlt_destructor )mlt_properties_close, NULL );
179                 f0r_param_info_t paraminfo;
180                 param_info(&paraminfo,j);
181                 mlt_properties_set ( pnum , "identifier" , string );
182                 mlt_properties_set ( pnum , "title" , paraminfo.name );
183                 mlt_properties_set ( pnum , "description" , paraminfo.explanation);
184                 if ( paraminfo.type == F0R_PARAM_DOUBLE ){
185                         double deflt = 0;
186                         mlt_properties_set ( pnum , "type" , "float" );
187                         mlt_properties_set ( pnum , "minimum" , "0" );
188                         mlt_properties_set ( pnum , "maximum" , "1" );
189                         f0r_get_param_value( instance, &deflt, j);
190                         mlt_properties_set_double ( pnum, "default", deflt );
191                         mlt_properties_set ( pnum , "mutable" , "yes" );
192                         mlt_properties_set ( pnum , "widget" , "spinner" );
193                 }else
194                 if ( paraminfo.type == F0R_PARAM_BOOL ){
195                         double deflt = 0;
196                         mlt_properties_set ( pnum , "type" , "boolean" );
197                         mlt_properties_set ( pnum , "minimum" , "0" );
198                         mlt_properties_set ( pnum , "maximum" , "1" );
199                         f0r_get_param_value( instance, &deflt, j);
200                         mlt_properties_set_double ( pnum, "default", deflt );
201                         mlt_properties_set ( pnum , "mutable" , "yes" );
202                         mlt_properties_set ( pnum , "widget" , "checkbox" );
203                 }else
204                 if ( paraminfo.type == F0R_PARAM_COLOR ){
205                         char colorstr[8];
206                         f0r_param_color_t deflt = {0, 0, 0};
207
208                         mlt_properties_set ( pnum , "type" , "color" );
209                         f0r_get_param_value( instance, &deflt, j);
210                         sprintf( colorstr, "#%02x%02x%02x", (unsigned) CLAMP(deflt.r * 255, 0 , 255),
211                                 (unsigned) CLAMP(deflt.g * 255, 0 , 255), (unsigned) CLAMP(deflt.b * 255, 0 , 255));
212                         colorstr[7] = 0;
213                         mlt_properties_set ( pnum , "default", colorstr );
214                         mlt_properties_set ( pnum , "mutable" , "yes" );
215                         mlt_properties_set ( pnum , "widget" , "color" );
216                 }else
217                 if ( paraminfo.type == F0R_PARAM_STRING ){
218                         char *deflt = NULL;
219                         mlt_properties_set ( pnum , "type" , "string" );
220                         f0r_get_param_value( instance, &deflt, j );
221                         mlt_properties_set ( pnum , "default", deflt );
222                         mlt_properties_set ( pnum , "mutable" , "yes" );
223                         mlt_properties_set ( pnum , "widget" , "text" );
224                 }
225         }
226         f0r_destruct(instance);
227         f0r_deinit();
228         dlclose(handle);
229         free(name);
230
231         return metadata;
232 }
233
234 static void * load_lib( mlt_profile profile, mlt_service_type type , void* handle, const char *name ){
235
236         int i=0;
237         void (*f0r_get_plugin_info)(f0r_plugin_info_t*),
238                 *f0r_construct , *f0r_update , *f0r_destruct,
239                 (*f0r_get_param_info)(f0r_param_info_t* info, int param_index),
240                 (*f0r_init)(void) , *f0r_deinit ,
241                 (*f0r_set_param_value)(f0r_instance_t instance, f0r_param_t param, int param_index),
242                 (*f0r_get_param_value)(f0r_instance_t instance, f0r_param_t param, int param_index),
243                 (*f0r_update2) (f0r_instance_t instance, double time,   const uint32_t* inframe1,
244                   const uint32_t* inframe2,const uint32_t* inframe3, uint32_t* outframe);
245
246         if ( ( f0r_construct = dlsym(handle, "f0r_construct") ) &&
247                                 (f0r_destruct = dlsym(handle,"f0r_destruct") ) &&
248                                 (f0r_get_plugin_info = dlsym(handle,"f0r_get_plugin_info") ) &&
249                                 (f0r_get_param_info = dlsym(handle,"f0r_get_param_info") ) &&
250                                 (f0r_set_param_value= dlsym(handle,"f0r_set_param_value" ) ) &&
251                                 (f0r_get_param_value= dlsym(handle,"f0r_get_param_value" ) ) &&
252                                 (f0r_init= dlsym(handle,"f0r_init" ) ) &&
253                                 (f0r_deinit= dlsym(handle,"f0r_deinit" ) )
254                 ){
255
256                 f0r_update=dlsym(handle,"f0r_update");
257                 f0r_update2=dlsym(handle,"f0r_update2");
258
259                 f0r_plugin_info_t info;
260                 f0r_get_plugin_info(&info);
261
262                 void* ret=NULL;
263                 mlt_properties properties=NULL;
264                 char minor[12];
265
266                 if (type == producer_type && info.plugin_type == F0R_PLUGIN_TYPE_SOURCE ){
267                         mlt_producer this = mlt_producer_new( profile );
268                         if ( this != NULL )
269                         {
270                                 this->get_frame = producer_get_frame;
271                                 this->close = ( mlt_destructor )producer_close;
272                                 f0r_init();
273                                 properties=MLT_PRODUCER_PROPERTIES ( this );
274
275                                 for (i=0;i<info.num_params;i++){
276                                         f0r_param_info_t pinfo;
277                                         f0r_get_param_info(&pinfo,i);
278
279                                 }
280
281                                 ret=this;
282                         }
283                 } else if (type == filter_type && info.plugin_type == F0R_PLUGIN_TYPE_FILTER ){
284                         mlt_filter this = mlt_filter_new( );
285                         if ( this != NULL )
286                         {
287                                 this->process = filter_process;
288                                 this->close = filter_close;
289                                 f0r_init();
290                                 properties=MLT_FILTER_PROPERTIES ( this );
291
292                                 for (i=0;i<info.num_params;i++){
293                                         f0r_param_info_t pinfo;
294                                         f0r_get_param_info(&pinfo,i);
295
296                                 }
297
298                                 ret=this;
299                         }
300                 }else if (type == transition_type && info.plugin_type == F0R_PLUGIN_TYPE_MIXER2){
301                         mlt_transition transition = mlt_transition_new( );
302                         if ( transition != NULL )
303                         {
304                                 transition->process = transition_process;
305                                 transition->close = transition_close;
306                                 properties=MLT_TRANSITION_PROPERTIES( transition );
307                                 mlt_properties_set_int(properties, "_transition_type", 1 );
308
309                                 ret=transition;
310                         }
311                 }
312                 check_thread_safe( properties, name );
313                 mlt_properties_set_data(properties, "_dlclose_handle", handle , sizeof ( handle ) , NULL , NULL );
314                 mlt_properties_set_data(properties, "_dlclose", dlclose , sizeof (void*) , NULL , NULL );
315                 mlt_properties_set_data(properties, "f0r_construct", f0r_construct , sizeof( f0r_construct ),NULL,NULL);
316                 mlt_properties_set_data(properties, "f0r_update", f0r_update , sizeof( f0r_update ),NULL,NULL);
317                 if (f0r_update2)
318                         mlt_properties_set_data(properties, "f0r_update2", f0r_update2 , sizeof( f0r_update2 ),NULL,NULL);
319                 mlt_properties_set_data(properties, "f0r_destruct", f0r_destruct , sizeof( f0r_destruct ),NULL,NULL);
320                 mlt_properties_set_data(properties, "f0r_get_plugin_info", f0r_get_plugin_info , sizeof(void*),NULL,NULL);
321                 mlt_properties_set_data(properties, "f0r_get_param_info", f0r_get_param_info , sizeof(void*),NULL,NULL);
322                 mlt_properties_set_data(properties, "f0r_set_param_value", f0r_set_param_value , sizeof(void*),NULL,NULL);
323                 mlt_properties_set_data(properties, "f0r_get_param_value", f0r_get_param_value , sizeof(void*),NULL,NULL);
324
325                 // Let frei0r plugin version be serialized using same format as metadata
326                 snprintf( minor, sizeof( minor ), "%d", info.minor_version );
327                 mlt_properties_set_double( properties, "version", info.major_version +  info.minor_version / pow( 10, strlen( minor ) ) );
328
329                 return ret;
330         }else{
331                 mlt_log_error( NULL, "frei0r plugin \"%s\" is missing a function\n", name );
332                 dlerror();
333         }
334         return NULL;
335 }
336
337 static void * create_frei0r_item ( mlt_profile profile, mlt_service_type type, const char *id, void *arg){
338
339         mlt_tokeniser tokeniser = mlt_tokeniser_init ( );
340         char *frei0r_path = get_frei0r_path();
341         int dircount=mlt_tokeniser_parse_new (
342                 tokeniser,
343                 frei0r_path,
344                  ":"
345         );
346         void* ret=NULL;
347         while (dircount--){
348                 char soname[PATH_MAX];
349                 char *myid = strdup( id );
350
351 #ifdef WIN32
352                 char *firstname = strtok( myid, "." );
353 #else
354                 char *save_firstptr = NULL;
355                 char *firstname = strtok_r( myid, ".", &save_firstptr );
356 #endif
357                 char* directory = mlt_tokeniser_get_string (tokeniser, dircount);
358
359 #ifdef WIN32
360                 firstname = strtok( NULL, "." );
361 #else
362                 firstname = strtok_r( NULL, ".", &save_firstptr );
363 #endif
364                 if (strncmp(directory, "$HOME", 5))
365                         snprintf(soname, PATH_MAX, "%s/%s" LIBSUF, directory, firstname );
366                 else
367                         snprintf(soname, PATH_MAX, "%s%s/%s" LIBSUF, getenv("HOME"), strchr(directory, '/'), firstname );
368
369                 if (firstname){
370
371                         void* handle=dlopen(soname,RTLD_LAZY);
372
373                         if (handle ){
374                                 ret=load_lib ( profile , type , handle, firstname );
375                         }else{
376                                 dlerror();
377                         }
378                 }
379                 free( myid );
380         }
381         mlt_tokeniser_close ( tokeniser );
382         free( frei0r_path );
383         return ret;
384 }
385
386
387 MLT_REPOSITORY
388 {
389         int i=0;
390         mlt_tokeniser tokeniser = mlt_tokeniser_init ( );
391         char *frei0r_path = get_frei0r_path();
392         int dircount=mlt_tokeniser_parse_new (
393                 tokeniser ,
394                 frei0r_path,
395                 ":"
396         );
397         char dirname[PATH_MAX];
398         snprintf( dirname, PATH_MAX, "%s/frei0r/blacklist.txt", mlt_environment( "MLT_DATA" ) );
399         mlt_properties blacklist = mlt_properties_load( dirname );
400
401         while (dircount--){
402
403                 mlt_properties direntries = mlt_properties_new();
404                 char* directory = mlt_tokeniser_get_string (tokeniser, dircount);
405                 
406                 if (strncmp(directory, "$HOME", 5))
407                         snprintf(dirname, PATH_MAX, "%s", directory);
408                 else
409                         snprintf(dirname, PATH_MAX, "%s%s", getenv("HOME"), strchr(directory, '/'));
410                 mlt_properties_dir_list(direntries, dirname ,"*" LIBSUF, 1);
411
412                 for (i=0; i<mlt_properties_count(direntries);i++){
413                         char* name=mlt_properties_get_value(direntries,i);
414                         char* shortname=name+strlen(dirname)+1;
415 #ifdef WIN32
416                         char* firstname = strtok( shortname, "." );
417 #else
418                         char *save_firstptr = NULL;
419                         char* firstname = strtok_r( shortname, ".", &save_firstptr );
420 #endif
421                         char pluginname[1024]="frei0r.";
422                         if ( firstname )
423                                 strncat( pluginname, firstname, sizeof( pluginname ) - strlen( pluginname ) -1 );
424
425                         if ( firstname && mlt_properties_get( blacklist, firstname ) )
426                                 continue;
427
428                         void* handle=dlopen(strcat(name, LIBSUF),RTLD_LAZY);
429                         if (handle){
430                                 void (*plginfo)(f0r_plugin_info_t*)=dlsym(handle,"f0r_get_plugin_info");
431
432                                 if (plginfo){
433                                         f0r_plugin_info_t info;
434                                         plginfo(&info);
435                                         if (firstname && info.plugin_type==F0R_PLUGIN_TYPE_SOURCE){
436                                                 if (mlt_properties_get(mlt_repository_producers(repository), pluginname))
437                                                 {
438                                                         dlclose(handle);
439                                                         continue;
440                                                 }
441                                                 MLT_REGISTER( producer_type, pluginname, create_frei0r_item );
442                                                 MLT_REGISTER_METADATA( producer_type, pluginname, fill_param_info, strdup(name) );
443                                         }
444                                         else if (firstname && info.plugin_type==F0R_PLUGIN_TYPE_FILTER){
445                                                 if (mlt_properties_get(mlt_repository_filters(repository), pluginname))
446                                                 {
447                                                         dlclose(handle);
448                                                         continue;
449                                                 }
450                                                 MLT_REGISTER( filter_type, pluginname, create_frei0r_item );
451                                                 MLT_REGISTER_METADATA( filter_type, pluginname, fill_param_info, strdup(name) );
452                                         }
453                                         else if (firstname && info.plugin_type==F0R_PLUGIN_TYPE_MIXER2 ){
454                                                 if (mlt_properties_get(mlt_repository_transitions(repository), pluginname))
455                                                 {
456                                                         dlclose(handle);
457                                                         continue;
458                                                 }
459                                                 MLT_REGISTER( transition_type, pluginname, create_frei0r_item );
460                                                 MLT_REGISTER_METADATA( transition_type, pluginname, fill_param_info, strdup(name) );
461                                         }
462                                 }
463                                 dlclose(handle);
464                         }
465                 }
466                 mlt_properties_close(direntries);
467         }
468         mlt_tokeniser_close ( tokeniser );
469         mlt_properties_close( blacklist );
470         free( frei0r_path );
471 }