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 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 );
56 static char* get_frei0r_path()
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 );
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 );
69 return strdup( GET_FREI0R_PATH );
73 static void check_thread_safe( mlt_properties properties, const char *name )
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 );
80 for ( i = 0; i < mlt_properties_count( not_thread_safe ); i++ )
82 if ( strcmp( name, mlt_properties_get_name( not_thread_safe, i ) ) == 0 )
84 mlt_properties_set_int( properties, "_not_thread_safe", 1 );
88 mlt_properties_close( not_thread_safe );
91 static mlt_properties fill_param_info ( mlt_service_type type, const char *service_name, char *name )
93 char file[ PATH_MAX ];
94 char servicetype[ 1024 ]="";
95 struct stat stat_buff;
99 strcpy ( servicetype , "producer" );
102 strcpy ( servicetype , "filter" );
104 case transition_type:
105 strcpy ( servicetype , "transition" ) ;
108 strcpy ( servicetype , "" );
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);
115 if (S_ISREG(stat_buff.st_mode)){
116 return mlt_properties_parse_yaml( file );
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) {
129 mlt_properties metadata = mlt_properties_new();
130 f0r_plugin_info_t 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 );
146 mlt_properties_set ( metadata, "type" , "producer" );
149 mlt_properties_set ( metadata, "type" , "filter" );
151 case transition_type:
152 mlt_properties_set ( metadata, "type" , "transition" );
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" );
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(¶minfo,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" );
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" );
186 if ( paraminfo.type == F0R_PARAM_COLOR ){
187 mlt_properties_set ( pnum , "type" , "color" );
188 mlt_properties_set ( pnum , "readonly" , "no" );
190 if ( paraminfo.type == F0R_PARAM_STRING ){
191 mlt_properties_set ( pnum , "type" , "string" );
192 mlt_properties_set ( pnum , "readonly" , "no" );
202 static void * load_lib( mlt_profile profile, mlt_service_type type , void* handle, const char *name ){
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);
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" ) )
224 f0r_update=dlsym(handle,"f0r_update");
225 f0r_update2=dlsym(handle,"f0r_update2");
227 f0r_plugin_info_t info;
228 f0r_get_plugin_info(&info);
231 mlt_properties properties=NULL;
234 if (type == producer_type && info.plugin_type == F0R_PLUGIN_TYPE_SOURCE ){
235 mlt_producer this = mlt_producer_new( profile );
238 this->get_frame = producer_get_frame;
239 this->close = ( mlt_destructor )producer_close;
241 properties=MLT_PRODUCER_PROPERTIES ( this );
243 for (i=0;i<info.num_params;i++){
244 f0r_param_info_t pinfo;
245 f0r_get_param_info(&pinfo,i);
251 } else if (type == filter_type && info.plugin_type == F0R_PLUGIN_TYPE_FILTER ){
252 mlt_filter this = mlt_filter_new( );
255 this->process = filter_process;
256 this->close = filter_close;
258 properties=MLT_FILTER_PROPERTIES ( this );
260 for (i=0;i<info.num_params;i++){
261 f0r_param_info_t pinfo;
262 f0r_get_param_info(&pinfo,i);
268 }else if (type == transition_type && info.plugin_type == F0R_PLUGIN_TYPE_MIXER2){
269 mlt_transition transition = mlt_transition_new( );
270 if ( transition != NULL )
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 );
280 check_thread_safe( properties, name );
281 mlt_properties_set_data(properties, "_dlclose_handle", handle , sizeof (void*) , 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(void*),NULL,NULL);
284 mlt_properties_set_data(properties, "f0r_update", f0r_update , sizeof(void*),NULL,NULL);
286 mlt_properties_set_data(properties, "f0r_update2", f0r_update2 , sizeof(void*),NULL,NULL);
287 mlt_properties_set_data(properties, "f0r_destruct", f0r_destruct , sizeof(void*),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);
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 ) ) );
299 mlt_log_error( NULL, "frei0r plugin \"%s\" is missing a function\n", name );
305 static void * create_frei0r_item ( mlt_profile profile, mlt_service_type type, const char *id, void *arg){
307 mlt_tokeniser tokeniser = mlt_tokeniser_init ( );
308 char *frei0r_path = get_frei0r_path();
309 int dircount=mlt_tokeniser_parse_new (
316 char soname[PATH_MAX];
317 char *myid = strdup( id );
320 char *firstname = strtok( myid, "." );
322 char *save_firstptr = NULL;
323 char *firstname = strtok_r( myid, ".", &save_firstptr );
325 char* directory = mlt_tokeniser_get_string (tokeniser, dircount);
328 firstname = strtok( NULL, "." );
330 firstname = strtok_r( NULL, ".", &save_firstptr );
332 if (strncmp(directory, "$HOME", 5))
333 snprintf(soname, PATH_MAX, "%s/%s" LIBSUF, directory, firstname );
335 snprintf(soname, PATH_MAX, "%s%s/%s" LIBSUF, getenv("HOME"), strchr(directory, '/'), firstname );
339 void* handle=dlopen(soname,RTLD_LAZY);
342 ret=load_lib ( profile , type , handle, firstname );
349 mlt_tokeniser_close ( tokeniser );
358 mlt_tokeniser tokeniser = mlt_tokeniser_init ( );
359 char *frei0r_path = get_frei0r_path();
360 int dircount=mlt_tokeniser_parse_new (
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 );
371 mlt_properties direntries = mlt_properties_new();
372 char* directory = mlt_tokeniser_get_string (tokeniser, dircount);
374 if (strncmp(directory, "$HOME", 5))
375 snprintf(dirname, PATH_MAX, "%s", directory);
377 snprintf(dirname, PATH_MAX, "%s%s", getenv("HOME"), strchr(directory, '/'));
378 mlt_properties_dir_list(direntries, dirname ,"*" LIBSUF, 1);
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;
384 char* firstname = strtok( shortname, "." );
386 char *save_firstptr = NULL;
387 char* firstname = strtok_r( shortname, ".", &save_firstptr );
389 char pluginname[1024]="frei0r.";
390 strcat(pluginname,firstname);
392 if ( mlt_properties_get( blacklist, firstname ) )
395 void* handle=dlopen(strcat(name, LIBSUF),RTLD_LAZY);
397 void (*plginfo)(f0r_plugin_info_t*)=dlsym(handle,"f0r_get_plugin_info");
400 f0r_plugin_info_t info;
402 if (firstname && info.plugin_type==F0R_PLUGIN_TYPE_SOURCE){
403 if (mlt_properties_get(mlt_repository_producers(repository), pluginname))
408 MLT_REGISTER( producer_type, pluginname, create_frei0r_item );
409 MLT_REGISTER_METADATA( producer_type, pluginname, fill_param_info, strdup(name) );
411 else if (firstname && info.plugin_type==F0R_PLUGIN_TYPE_FILTER){
412 if (mlt_properties_get(mlt_repository_filters(repository), pluginname))
417 MLT_REGISTER( filter_type, pluginname, create_frei0r_item );
418 MLT_REGISTER_METADATA( filter_type, pluginname, fill_param_info, strdup(name) );
420 else if (firstname && info.plugin_type==F0R_PLUGIN_TYPE_MIXER2 ){
421 if (mlt_properties_get(mlt_repository_transitions(repository), pluginname))
426 MLT_REGISTER( transition_type, pluginname, create_frei0r_item );
427 MLT_REGISTER_METADATA( transition_type, pluginname, fill_param_info, strdup(name) );
433 mlt_properties_close(direntries);
435 mlt_tokeniser_close ( tokeniser );
436 mlt_properties_close( blacklist );