2 * factory.c -- the factory method interfaces
3 * Copyright (c) 2008 Marco Gittler <g.marco@freenet.de>
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.
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.
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
21 #include <framework/mlt.h>
27 #include <sys/types.h>
37 #define FREI0R_PLUGIN_PATH "\\lib\\frei0r-1"
38 #elif defined(__DARWIN__) && defined(RELOCATABLE)
40 #define FREI0R_PLUGIN_PATH "/lib/frei0r-1"
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"
46 #define GET_FREI0R_PATH (getenv("FREI0R_PATH") ? getenv("FREI0R_PATH") : getenv("MLT_FREI0R_PLUGIN_PATH") ? getenv("MLT_FREI0R_PLUGIN_PATH") : FREI0R_PLUGIN_PATH)
48 #define CLAMP( x, min, max ) ((x) < (min) ? (min) : (x) > (max) ? (max) : (x))
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 );
58 static char* get_frei0r_path()
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 );
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 );
71 return strdup( GET_FREI0R_PATH );
75 static void check_thread_safe( mlt_properties properties, const char *name )
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 );
82 for ( i = 0; i < mlt_properties_count( not_thread_safe ); i++ )
84 if ( strcmp( name, mlt_properties_get_name( not_thread_safe, i ) ) == 0 )
86 mlt_properties_set_int( properties, "_not_thread_safe", 1 );
90 mlt_properties_close( not_thread_safe );
93 static mlt_properties fill_param_info ( mlt_service_type type, const char *service_name, char *name )
95 char file[ PATH_MAX ];
96 char servicetype[ 1024 ]="";
97 struct stat stat_buff;
101 strcpy ( servicetype , "producer" );
104 strcpy ( servicetype , "filter" );
106 case transition_type:
107 strcpy ( servicetype , "transition" ) ;
110 strcpy ( servicetype , "" );
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);
117 if (S_ISREG(stat_buff.st_mode)){
118 return mlt_properties_parse_yaml( file );
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) {
134 mlt_properties metadata = mlt_properties_new();
135 f0r_plugin_info_t info;
140 f0r_instance_t instance = f0r_construct(720, 576);
144 mlt_properties_close(metadata);
148 snprintf ( string, sizeof(string) , "%d" , info.minor_version );
149 mlt_properties_set_double ( metadata, "schema_version" , 0.1 );
150 mlt_properties_set ( metadata, "title" , info.name );
151 mlt_properties_set_double ( metadata, "version",
152 info.major_version + info.minor_version / pow( 10, strlen( string ) ) );
153 mlt_properties_set ( metadata, "identifier" , service_name );
154 mlt_properties_set ( metadata, "description" , info.explanation );
155 mlt_properties_set ( metadata, "creator" , info.author );
158 mlt_properties_set ( metadata, "type" , "producer" );
161 mlt_properties_set ( metadata, "type" , "filter" );
163 case transition_type:
164 mlt_properties_set ( metadata, "type" , "transition" );
170 mlt_properties tags = mlt_properties_new ( );
171 mlt_properties_set_data ( metadata , "tags" , tags , 0 , ( mlt_destructor )mlt_properties_close, NULL );
172 mlt_properties_set ( tags , "0" , "Video" );
174 mlt_properties parameter = mlt_properties_new ( );
175 mlt_properties_set_data ( metadata , "parameters" , parameter , 0 , ( mlt_destructor )mlt_properties_close, NULL );
177 for (j=0;j<info.num_params;j++){
178 snprintf ( string , sizeof(string), "%d" , j );
179 mlt_properties pnum = mlt_properties_new ( );
180 mlt_properties_set_data ( parameter , string , pnum , 0 , ( mlt_destructor )mlt_properties_close, NULL );
181 f0r_param_info_t paraminfo;
182 param_info(¶minfo,j);
183 mlt_properties_set ( pnum , "identifier" , string );
184 mlt_properties_set ( pnum , "title" , paraminfo.name );
185 mlt_properties_set ( pnum , "description" , paraminfo.explanation);
186 if ( paraminfo.type == F0R_PARAM_DOUBLE ){
188 mlt_properties_set ( pnum , "type" , "float" );
189 mlt_properties_set ( pnum , "minimum" , "0" );
190 mlt_properties_set ( pnum , "maximum" , "1" );
191 f0r_get_param_value( instance, &deflt, j);
192 mlt_properties_set_double ( pnum, "default", CLAMP(deflt, 0.0, 1.0) );
193 mlt_properties_set ( pnum , "mutable" , "yes" );
194 mlt_properties_set ( pnum , "widget" , "spinner" );
196 if ( paraminfo.type == F0R_PARAM_BOOL ){
198 mlt_properties_set ( pnum , "type" , "boolean" );
199 mlt_properties_set ( pnum , "minimum" , "0" );
200 mlt_properties_set ( pnum , "maximum" , "1" );
201 f0r_get_param_value( instance, &deflt, j);
202 mlt_properties_set_int ( pnum, "default", deflt != 0.0 );
203 mlt_properties_set ( pnum , "mutable" , "yes" );
204 mlt_properties_set ( pnum , "widget" , "checkbox" );
206 if ( paraminfo.type == F0R_PARAM_COLOR ){
208 f0r_param_color_t deflt = {0, 0, 0};
210 mlt_properties_set ( pnum , "type" , "color" );
211 f0r_get_param_value( instance, &deflt, j);
212 sprintf( colorstr, "#%02x%02x%02x", (unsigned) CLAMP(deflt.r * 255, 0 , 255),
213 (unsigned) CLAMP(deflt.g * 255, 0 , 255), (unsigned) CLAMP(deflt.b * 255, 0 , 255));
215 mlt_properties_set ( pnum , "default", colorstr );
216 mlt_properties_set ( pnum , "mutable" , "yes" );
217 mlt_properties_set ( pnum , "widget" , "color" );
219 if ( paraminfo.type == F0R_PARAM_STRING ){
221 mlt_properties_set ( pnum , "type" , "string" );
222 f0r_get_param_value( instance, &deflt, j );
223 mlt_properties_set ( pnum , "default", deflt );
224 mlt_properties_set ( pnum , "mutable" , "yes" );
225 mlt_properties_set ( pnum , "widget" , "text" );
228 f0r_destruct(instance);
236 static void * load_lib( mlt_profile profile, mlt_service_type type , void* handle, const char *name ){
239 void (*f0r_get_plugin_info)(f0r_plugin_info_t*),
240 *f0r_construct , *f0r_update , *f0r_destruct,
241 (*f0r_get_param_info)(f0r_param_info_t* info, int param_index),
242 (*f0r_init)(void) , *f0r_deinit ,
243 (*f0r_set_param_value)(f0r_instance_t instance, f0r_param_t param, int param_index),
244 (*f0r_get_param_value)(f0r_instance_t instance, f0r_param_t param, int param_index),
245 (*f0r_update2) (f0r_instance_t instance, double time, const uint32_t* inframe1,
246 const uint32_t* inframe2,const uint32_t* inframe3, uint32_t* outframe);
248 if ( ( f0r_construct = dlsym(handle, "f0r_construct") ) &&
249 (f0r_destruct = dlsym(handle,"f0r_destruct") ) &&
250 (f0r_get_plugin_info = dlsym(handle,"f0r_get_plugin_info") ) &&
251 (f0r_get_param_info = dlsym(handle,"f0r_get_param_info") ) &&
252 (f0r_set_param_value= dlsym(handle,"f0r_set_param_value" ) ) &&
253 (f0r_get_param_value= dlsym(handle,"f0r_get_param_value" ) ) &&
254 (f0r_init= dlsym(handle,"f0r_init" ) ) &&
255 (f0r_deinit= dlsym(handle,"f0r_deinit" ) )
258 f0r_update=dlsym(handle,"f0r_update");
259 f0r_update2=dlsym(handle,"f0r_update2");
261 f0r_plugin_info_t info;
262 f0r_get_plugin_info(&info);
265 mlt_properties properties=NULL;
268 if (type == producer_type && info.plugin_type == F0R_PLUGIN_TYPE_SOURCE ){
269 mlt_producer this = mlt_producer_new( profile );
272 this->get_frame = producer_get_frame;
273 this->close = ( mlt_destructor )producer_close;
275 properties=MLT_PRODUCER_PROPERTIES ( this );
277 for (i=0;i<info.num_params;i++){
278 f0r_param_info_t pinfo;
279 f0r_get_param_info(&pinfo,i);
285 } else if (type == filter_type && info.plugin_type == F0R_PLUGIN_TYPE_FILTER ){
286 mlt_filter this = mlt_filter_new( );
289 this->process = filter_process;
290 this->close = filter_close;
292 properties=MLT_FILTER_PROPERTIES ( this );
294 for (i=0;i<info.num_params;i++){
295 f0r_param_info_t pinfo;
296 f0r_get_param_info(&pinfo,i);
302 }else if (type == transition_type && info.plugin_type == F0R_PLUGIN_TYPE_MIXER2){
303 mlt_transition transition = mlt_transition_new( );
304 if ( transition != NULL )
306 transition->process = transition_process;
307 transition->close = transition_close;
308 properties=MLT_TRANSITION_PROPERTIES( transition );
309 mlt_properties_set_int(properties, "_transition_type", 1 );
314 check_thread_safe( properties, name );
315 mlt_properties_set_data(properties, "_dlclose_handle", handle , sizeof ( handle ) , NULL , NULL );
316 mlt_properties_set_data(properties, "_dlclose", dlclose , sizeof (void*) , NULL , NULL );
317 mlt_properties_set_data(properties, "f0r_construct", f0r_construct , sizeof( f0r_construct ),NULL,NULL);
318 mlt_properties_set_data(properties, "f0r_update", f0r_update , sizeof( f0r_update ),NULL,NULL);
320 mlt_properties_set_data(properties, "f0r_update2", f0r_update2 , sizeof( f0r_update2 ),NULL,NULL);
321 mlt_properties_set_data(properties, "f0r_destruct", f0r_destruct , sizeof( f0r_destruct ),NULL,NULL);
322 mlt_properties_set_data(properties, "f0r_get_plugin_info", f0r_get_plugin_info , sizeof(void*),NULL,NULL);
323 mlt_properties_set_data(properties, "f0r_get_param_info", f0r_get_param_info , sizeof(void*),NULL,NULL);
324 mlt_properties_set_data(properties, "f0r_set_param_value", f0r_set_param_value , sizeof(void*),NULL,NULL);
325 mlt_properties_set_data(properties, "f0r_get_param_value", f0r_get_param_value , sizeof(void*),NULL,NULL);
327 // Let frei0r plugin version be serialized using same format as metadata
328 snprintf( minor, sizeof( minor ), "%d", info.minor_version );
329 mlt_properties_set_double( properties, "version", info.major_version + info.minor_version / pow( 10, strlen( minor ) ) );
331 // Use the global param name map for backwards compatibility when
332 // param names change and setting frei0r params by name instead of index.
333 mlt_properties param_name_map = mlt_properties_get_data( mlt_global_properties(), "frei0r.param_name_map", NULL );
334 if ( param_name_map ) {
335 // Lookup my plugin in the map
336 param_name_map = mlt_properties_get_data( param_name_map, name, NULL );
337 mlt_properties_set_data( properties, "_param_name_map", param_name_map, 0, NULL, NULL );
342 mlt_log_error( NULL, "frei0r plugin \"%s\" is missing a function\n", name );
348 static void * create_frei0r_item ( mlt_profile profile, mlt_service_type type, const char *id, void *arg){
350 mlt_tokeniser tokeniser = mlt_tokeniser_init ( );
351 char *frei0r_path = get_frei0r_path();
352 int dircount=mlt_tokeniser_parse_new (
359 char soname[PATH_MAX];
360 char *myid = strdup( id );
363 char *firstname = strtok( myid, "." );
365 char *save_firstptr = NULL;
366 char *firstname = strtok_r( myid, ".", &save_firstptr );
368 char* directory = mlt_tokeniser_get_string (tokeniser, dircount);
371 firstname = strtok( NULL, "." );
373 firstname = strtok_r( NULL, ".", &save_firstptr );
375 if (strncmp(directory, "$HOME", 5))
376 snprintf(soname, PATH_MAX, "%s/%s" LIBSUF, directory, firstname );
378 snprintf(soname, PATH_MAX, "%s%s/%s" LIBSUF, getenv("HOME"), strchr(directory, '/'), firstname );
382 void* handle=dlopen(soname,RTLD_LAZY);
385 ret=load_lib ( profile , type , handle, firstname );
392 mlt_tokeniser_close ( tokeniser );
401 mlt_tokeniser tokeniser = mlt_tokeniser_init ( );
402 char *frei0r_path = get_frei0r_path();
403 int dircount=mlt_tokeniser_parse_new (
408 char dirname[PATH_MAX];
409 snprintf( dirname, PATH_MAX, "%s/frei0r/blacklist.txt", mlt_environment( "MLT_DATA" ) );
410 mlt_properties blacklist = mlt_properties_load( dirname );
412 // Load a param name map into global properties for backwards compatibility when
413 // param names change and setting frei0r params by name instead of index.
414 snprintf( dirname, PATH_MAX, "%s/frei0r/param_name_map.yaml", mlt_environment( "MLT_DATA" ) );
415 mlt_properties_set_data( mlt_global_properties(), "frei0r.param_name_map",
416 mlt_properties_parse_yaml( dirname ), 0, (mlt_destructor) mlt_properties_close, NULL );
420 mlt_properties direntries = mlt_properties_new();
421 char* directory = mlt_tokeniser_get_string (tokeniser, dircount);
423 if (strncmp(directory, "$HOME", 5))
424 snprintf(dirname, PATH_MAX, "%s", directory);
426 snprintf(dirname, PATH_MAX, "%s%s", getenv("HOME"), strchr(directory, '/'));
427 mlt_properties_dir_list(direntries, dirname ,"*" LIBSUF, 1);
429 for (i=0; i<mlt_properties_count(direntries);i++){
430 char* name=mlt_properties_get_value(direntries,i);
431 char* shortname=name+strlen(dirname)+1;
433 char* firstname = strtok( shortname, "." );
435 char *save_firstptr = NULL;
436 char* firstname = strtok_r( shortname, ".", &save_firstptr );
438 char pluginname[1024]="frei0r.";
440 strncat( pluginname, firstname, sizeof( pluginname ) - strlen( pluginname ) -1 );
442 if ( firstname && mlt_properties_get( blacklist, firstname ) )
445 void* handle=dlopen(strcat(name, LIBSUF),RTLD_LAZY);
447 void (*plginfo)(f0r_plugin_info_t*)=dlsym(handle,"f0r_get_plugin_info");
450 f0r_plugin_info_t info;
452 if (firstname && info.plugin_type==F0R_PLUGIN_TYPE_SOURCE){
453 if (mlt_properties_get(mlt_repository_producers(repository), pluginname))
458 MLT_REGISTER( producer_type, pluginname, create_frei0r_item );
459 MLT_REGISTER_METADATA( producer_type, pluginname, fill_param_info, strdup(name) );
461 else if (firstname && info.plugin_type==F0R_PLUGIN_TYPE_FILTER){
462 if (mlt_properties_get(mlt_repository_filters(repository), pluginname))
467 MLT_REGISTER( filter_type, pluginname, create_frei0r_item );
468 MLT_REGISTER_METADATA( filter_type, pluginname, fill_param_info, strdup(name) );
470 else if (firstname && info.plugin_type==F0R_PLUGIN_TYPE_MIXER2 ){
471 if (mlt_properties_get(mlt_repository_transitions(repository), pluginname))
476 MLT_REGISTER( transition_type, pluginname, create_frei0r_item );
477 MLT_REGISTER_METADATA( transition_type, pluginname, fill_param_info, strdup(name) );
483 mlt_properties_close(direntries);
485 mlt_tokeniser_close ( tokeniser );
486 mlt_properties_close( blacklist );