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