frei0r/factory.c: set min/max values in metadata to defined min/max from frei0r.h
[melted] / 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
32 extern mlt_filter filter_frei0r_init( mlt_profile profile, mlt_service_type type, const char *id, char *arg );
33 extern mlt_frame filter_process( mlt_filter this, mlt_frame frame );
34 extern void filter_close( mlt_filter this );
35 extern void transition_close( mlt_transition this );
36 extern mlt_frame transition_process( mlt_transition transition, mlt_frame a_frame, mlt_frame b_frame );
37
38 static mlt_properties fill_param_info ( mlt_service_type type, const char *service_name, char *name )
39 {
40 char file[ PATH_MAX ];
41 char servicetype[ 1024 ]="";
42 struct stat stat_buff;
43
44 switch ( type ) {
45 case filter_type:
46 strcpy ( servicetype , "filter" );
47 break;
48 case transition_type:
49 strcpy ( servicetype , "transition" ) ;
50 break;
51 default:
52 strcpy ( servicetype , "" );
53 };
54
55 snprintf( file, PATH_MAX, "%s/frei0r/%s_%s.yml", mlt_environment( "MLT_DATA" ), servicetype, service_name );
56 stat(file,&stat_buff);
57
58 if (S_ISREG(stat_buff.st_mode)){
59 return mlt_properties_parse_yaml( file );
60 }
61
62 void* handle=dlopen(name,RTLD_LAZY);
63 if (!handle) return NULL;
64 void (*plginfo)(f0r_plugin_info_t*)=dlsym(handle,"f0r_get_plugin_info");
65 void (*param_info)(f0r_param_info_t*,int param_index)=dlsym(handle,"f0r_get_param_info");
66 if (!plginfo || !param_info) {
67 dlclose(handle);
68 return NULL;
69 }
70 mlt_properties metadata = mlt_properties_new();
71 f0r_plugin_info_t info;
72 char string[48];
73 int j=0;
74
75 plginfo(&info);
76 snprintf ( string, sizeof(string) , "%d.%d" , info.major_version , info.minor_version );
77 mlt_properties_set ( metadata, "schema_version" , "0.1" );
78 mlt_properties_set ( metadata, "title" , info.name );
79 mlt_properties_set ( metadata, "version", string );
80 mlt_properties_set ( metadata, "identifier" , service_name );
81 mlt_properties_set ( metadata, "description" , info.explanation );
82 mlt_properties_set ( metadata, "creator" , info.author );
83 switch (type){
84 case filter_type:
85 mlt_properties_set ( metadata, "type" , "filter" );
86 break;
87 case transition_type:
88 mlt_properties_set ( metadata, "type" , "transition" );
89 break;
90 default:
91 break;
92 }
93
94 mlt_properties parameter = mlt_properties_new ( );
95 mlt_properties_set_data ( metadata , "parameters" , parameter , 0 , ( mlt_destructor )mlt_properties_close, NULL );
96 mlt_properties tags = mlt_properties_new ( );
97 mlt_properties_set_data ( metadata , "tags" , tags , 0 , ( mlt_destructor )mlt_properties_close, NULL );
98 mlt_properties_set ( tags , "0" , "Video" );
99
100 for (j=0;j<info.num_params;j++){
101 snprintf ( string , sizeof(string), "%d" , j );
102 mlt_properties pnum = mlt_properties_new ( );
103 mlt_properties_set_data ( parameter , string , pnum , 0 , ( mlt_destructor )mlt_properties_close, NULL );
104 f0r_param_info_t paraminfo;
105 param_info(&paraminfo,j);
106 mlt_properties_set ( pnum , "identifier" , paraminfo.name );
107 mlt_properties_set ( pnum , "title" , paraminfo.name );
108 mlt_properties_set ( pnum , "description" , paraminfo.explanation);
109 if ( paraminfo.type == F0R_PARAM_DOUBLE ){
110 mlt_properties_set ( pnum , "type" , "float" );
111 mlt_properties_set ( pnum , "minimum" , "0" );
112 mlt_properties_set ( pnum , "maximum" , "1" );
113 mlt_properties_set ( pnum , "readonly" , "no" );
114 mlt_properties_set ( pnum , "widget" , "spinner" );
115 }else
116 if ( paraminfo.type == F0R_PARAM_BOOL ){
117 mlt_properties_set ( pnum , "type" , "boolean" );
118 mlt_properties_set ( pnum , "minimum" , "0" );
119 mlt_properties_set ( pnum , "maximum" , "1" );
120 mlt_properties_set ( pnum , "readonly" , "no" );
121 }else
122 if ( paraminfo.type == F0R_PARAM_STRING ){
123 mlt_properties_set ( pnum , "type" , "string" );
124 mlt_properties_set ( pnum , "readonly" , "no" );
125 }
126 }
127 dlclose(handle);
128 free(name);
129
130 return metadata;
131 }
132
133 static void * load_lib( mlt_profile profile, mlt_service_type type , void* handle){
134
135 int i=0;
136 void (*f0r_get_plugin_info)(f0r_plugin_info_t*),
137 *f0r_construct , *f0r_update , *f0r_destruct,
138 (*f0r_get_param_info)(f0r_param_info_t* info, int param_index),
139 (*f0r_init)(void) , *f0r_deinit ,
140 (*f0r_set_param_value)(f0r_instance_t instance, f0r_param_t param, int param_index),
141 (*f0r_get_param_value)(f0r_instance_t instance, f0r_param_t param, int param_index),
142 (*f0r_update2) (f0r_instance_t instance, double time, const uint32_t* inframe1,
143 const uint32_t* inframe2,const uint32_t* inframe3, uint32_t* outframe);
144
145 if ( ( f0r_construct = dlsym(handle, "f0r_construct") ) &&
146 (f0r_update = dlsym(handle,"f0r_update") ) &&
147 (f0r_destruct = dlsym(handle,"f0r_destruct") ) &&
148 (f0r_get_plugin_info = dlsym(handle,"f0r_get_plugin_info") ) &&
149 (f0r_get_param_info = dlsym(handle,"f0r_get_param_info") ) &&
150 (f0r_set_param_value= dlsym(handle,"f0r_set_param_value" ) ) &&
151 (f0r_get_param_value= dlsym(handle,"f0r_get_param_value" ) ) &&
152 (f0r_init= dlsym(handle,"f0r_init" ) ) &&
153 (f0r_deinit= dlsym(handle,"f0r_deinit" ) )
154 ){
155
156 f0r_update2=dlsym(handle,"f0r_update2");
157
158 f0r_plugin_info_t info;
159 f0r_get_plugin_info(&info);
160
161 void* ret=NULL;
162 mlt_properties properties=NULL;
163
164 if (type == filter_type && info.plugin_type == F0R_PLUGIN_TYPE_FILTER ){
165 mlt_filter this = mlt_filter_new( );
166 if ( this != NULL )
167 {
168 this->process = filter_process;
169 this->close = filter_close;
170 f0r_init();
171 properties=MLT_FILTER_PROPERTIES ( this );
172
173 for (i=0;i<info.num_params;i++){
174 f0r_param_info_t pinfo;
175 f0r_get_param_info(&pinfo,i);
176
177 }
178
179 ret=this;
180 }
181 }else if (type == transition_type && info.plugin_type == F0R_PLUGIN_TYPE_MIXER2){
182 mlt_transition transition = mlt_transition_new( );
183 if ( transition != NULL )
184 {
185 transition->process = transition_process;
186 transition->close = transition_close;
187 properties=MLT_TRANSITION_PROPERTIES( transition );
188 mlt_properties_set_int(properties, "_transition_type", 1 );
189
190 ret=transition;
191 }
192 }
193 mlt_properties_set_data(properties, "_dlclose_handle", handle , sizeof (void*) , NULL , NULL );
194 mlt_properties_set_data(properties, "_dlclose", dlclose , sizeof (void*) , NULL , NULL );
195 mlt_properties_set_data(properties, "f0r_construct", f0r_construct , sizeof(void*),NULL,NULL);
196 mlt_properties_set_data(properties, "f0r_update", f0r_update , sizeof(void*),NULL,NULL);
197 if (f0r_update2)
198 mlt_properties_set_data(properties, "f0r_update2", f0r_update2 , sizeof(void*),NULL,NULL);
199 mlt_properties_set_data(properties, "f0r_destruct", f0r_destruct , sizeof(void*),NULL,NULL);
200 mlt_properties_set_data(properties, "f0r_get_plugin_info", f0r_get_plugin_info , sizeof(void*),NULL,NULL);
201 mlt_properties_set_data(properties, "f0r_get_param_info", f0r_get_param_info , sizeof(void*),NULL,NULL);
202 mlt_properties_set_data(properties, "f0r_set_param_value", f0r_set_param_value , sizeof(void*),NULL,NULL);
203 mlt_properties_set_data(properties, "f0r_get_param_value", f0r_get_param_value , sizeof(void*),NULL,NULL);
204
205
206 return ret;
207 }else{
208 printf("some was wrong\n");
209 dlerror();
210 }
211 return NULL;
212 }
213
214 static void * create_frei0r_item ( mlt_profile profile, mlt_service_type type, const char *id, void *arg){
215
216 mlt_tokeniser tokeniser = mlt_tokeniser_init ( );
217 int dircount=mlt_tokeniser_parse_new (
218 tokeniser ,
219 getenv("MLT_FREI0R_PLUGIN_PATH") ? getenv("MLT_FREI0R_PLUGIN_PATH") : "/usr/lib/frei0r-1" ,
220 ":"
221 );
222 void* ret=NULL;
223 while (dircount--){
224 char soname[1024]="";
225
226 char *save_firstptr = NULL;
227 char *firstname=strtok_r(strdup(id),".",&save_firstptr);
228
229 firstname=strtok_r(NULL,".",&save_firstptr);
230 sprintf(soname,"%s/%s.so", mlt_tokeniser_get_string( tokeniser , dircount ) , firstname );
231
232 if (firstname){
233
234 void* handle=dlopen(soname,RTLD_LAZY);
235
236 if (handle ){
237 ret=load_lib ( profile , type , handle );
238 }else{
239 dlerror();
240 }
241 }
242 }
243 mlt_tokeniser_close ( tokeniser );
244 return ret;
245 }
246
247
248 MLT_REPOSITORY
249 {
250 int i=0;
251 mlt_tokeniser tokeniser = mlt_tokeniser_init ( );
252 int dircount=mlt_tokeniser_parse_new (
253 tokeniser ,
254 getenv("MLT_FREI0R_PLUGIN_PATH") ? getenv("MLT_FREI0R_PLUGIN_PATH") : "/usr/lib/frei0r-1" ,
255 ":"
256 );
257
258 while (dircount--){
259
260 mlt_properties direntries = mlt_properties_new();
261 char* dirname = mlt_tokeniser_get_string ( tokeniser , dircount ) ;
262 mlt_properties_dir_list(direntries, dirname ,"*.so",1);
263
264 for (i=0;i<mlt_properties_count(direntries);i++){
265 char* name=mlt_properties_get_value(direntries,i);
266 char* shortname=name+strlen(dirname)+1;
267 char fname[1024]="";
268
269 strcat(fname,dirname);
270 strcat(fname,shortname);
271
272 char *save_firstptr = NULL;
273 char pluginname[1024]="frei0r.";
274 char* firstname = strtok_r ( shortname , "." , &save_firstptr );
275 strcat(pluginname,firstname);
276
277 void* handle=dlopen(strcat(name,".so"),RTLD_LAZY);
278 if (handle){
279 void (*plginfo)(f0r_plugin_info_t*)=dlsym(handle,"f0r_get_plugin_info");
280
281 if (plginfo){
282 f0r_plugin_info_t info;
283 plginfo(&info);
284
285 if (firstname && info.plugin_type==F0R_PLUGIN_TYPE_FILTER){
286 MLT_REGISTER( filter_type, pluginname, create_frei0r_item );
287 MLT_REGISTER_METADATA( filter_type, pluginname, fill_param_info, strdup(name) );
288 }
289 else if (firstname && info.plugin_type==F0R_PLUGIN_TYPE_MIXER2 ){
290 MLT_REGISTER( transition_type, pluginname, create_frei0r_item );
291 MLT_REGISTER_METADATA( transition_type, pluginname, fill_param_info, strdup(name) );
292 }
293 }
294 dlclose(handle);
295 }
296 }
297 mlt_properties_close(direntries);
298 }
299 mlt_tokeniser_close ( tokeniser );
300 }