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