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