]> git.sesse.net Git - mlt/blobdiff - src/modules/frei0r/factory.c
Fix encoding audio distortion for planar codecs (3576437)
[mlt] / src / modules / frei0r / factory.c
index e981de7796a51a89b2c4beeeb216f053a3437e0b..ea745c4087066b647c2da86468c803347aaa0d78 100644 (file)
 
 #include <stddef.h>
 #include <stdio.h>
+#include <sys/stat.h>
 #include <sys/types.h>
 #include <dirent.h>
 #include <dlfcn.h>
 #include <stdlib.h>
+#include <limits.h>
+#include <math.h>
+
+
+#if defined(WIN32)
+#define LIBSUF ".dll"
+#define FREI0R_PLUGIN_PATH "\\lib\\frei0r-1"
+#elif defined(__DARWIN__) && defined(RELOCATABLE)
+#define LIBSUF ".so"
+#define FREI0R_PLUGIN_PATH "/lib/frei0r-1"
+#else
+#define LIBSUF ".so"
+#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"
+#endif
+
+#define GET_FREI0R_PATH (getenv("FREI0R_PATH") ? getenv("FREI0R_PATH") : getenv("MLT_FREI0R_PLUGIN_PATH") ? getenv("MLT_FREI0R_PLUGIN_PATH") : FREI0R_PLUGIN_PATH)
 
 extern mlt_filter filter_frei0r_init( mlt_profile profile, mlt_service_type type, const char *id, char *arg );
 extern mlt_frame filter_process( mlt_filter this, mlt_frame frame );
 extern void filter_close( mlt_filter this );
+extern int producer_get_frame( mlt_producer producer, mlt_frame_ptr frame, int index );
+extern void producer_close( mlt_producer this );
 extern void transition_close( mlt_transition this );
 extern mlt_frame transition_process( mlt_transition transition, mlt_frame a_frame, mlt_frame b_frame );
 
-static mlt_properties subtag ( mlt_properties prop , char *name ,  mlt_serialiser serialise_yaml ){
-       mlt_properties ret = mlt_properties_get_data( prop , name , NULL );
-       
-       if (!ret){
-               ret = mlt_properties_new ( );
-               mlt_properties_set_data ( prop , name , ret , 0 , ( mlt_destructor )mlt_properties_close , serialise_yaml );
+static char* get_frei0r_path()
+{
+#ifdef WIN32
+       char *dirname = malloc( strlen( mlt_environment( "MLT_APPDIR" ) ) + strlen( FREI0R_PLUGIN_PATH ) + 1 );
+       strcpy( dirname, mlt_environment( "MLT_APPDIR" ) );
+       strcat( dirname, FREI0R_PLUGIN_PATH );
+       return dirname;
+#elif defined(__DARWIN__) && defined(RELOCATABLE)
+       char *dirname = malloc( strlen( mlt_environment( "MLT_APPDIR" ) ) + strlen( FREI0R_PLUGIN_PATH ) + 1 );
+       strcpy( dirname, mlt_environment( "MLT_APPDIR" ) );
+       strcat( dirname, FREI0R_PLUGIN_PATH );
+       return dirname;
+#else
+       return strdup( GET_FREI0R_PATH );
+#endif
+}
+
+static void check_thread_safe( mlt_properties properties, const char *name )
+{
+       char dirname[PATH_MAX];
+       snprintf( dirname, PATH_MAX, "%s/frei0r/not_thread_safe.txt", mlt_environment( "MLT_DATA" ) );
+       mlt_properties not_thread_safe = mlt_properties_load( dirname );
+       int i;
+
+       for ( i = 0; i < mlt_properties_count( not_thread_safe ); i++ )
+       {
+               if ( strcmp( name, mlt_properties_get_name( not_thread_safe, i ) ) == 0 )
+               {
+                       mlt_properties_set_int( properties, "_not_thread_safe", 1 );
+                       break;
+               }
        }
-       return ret;
+       mlt_properties_close( not_thread_safe );
 }
 
-void fill_param_info ( mlt_repository repository , void* handle, f0r_plugin_info_t* info , mlt_service_type type , char* name ) {
-       
-       int j=0;
+static mlt_properties fill_param_info ( mlt_service_type type, const char *service_name, char *name )
+{
+       char file[ PATH_MAX ];
+       char servicetype[ 1024 ]="";
+       struct stat stat_buff;
+
+       switch ( type ) {
+               case producer_type:
+                       strcpy ( servicetype , "producer" );
+                       break;
+               case filter_type:
+                       strcpy ( servicetype , "filter" );
+                       break;
+               case transition_type:
+                       strcpy ( servicetype , "transition" ) ;
+                       break;
+               default:
+                       strcpy ( servicetype , "" );
+       };
+
+       snprintf( file, PATH_MAX, "%s/frei0r/%s_%s.yml", mlt_environment( "MLT_DATA" ), servicetype, service_name );
+       memset(&stat_buff, 0, sizeof(stat_buff));
+       stat(file,&stat_buff);
+
+       if (S_ISREG(stat_buff.st_mode)){
+               return mlt_properties_parse_yaml( file );
+       }
+
+       void* handle=dlopen(name,RTLD_LAZY);
+       if (!handle) return NULL;
+       void (*plginfo)(f0r_plugin_info_t*)=dlsym(handle,"f0r_get_plugin_info");
        void (*param_info)(f0r_param_info_t*,int param_index)=dlsym(handle,"f0r_get_param_info");
-       mlt_properties metadata_properties=NULL;
+       void (*f0r_init)(void)=dlsym(handle,"f0r_init");
+       void (*f0r_deinit)(void)=dlsym(handle,"f0r_deinit");
+       if (!plginfo || !param_info) {
+               dlclose(handle);
+               return NULL;
+       }
+       mlt_properties metadata = mlt_properties_new();
+       f0r_plugin_info_t info;
+       char string[48];
+       int j=0;
+
+       f0r_init();
+       plginfo(&info);
+       snprintf ( string, sizeof(string) , "%d" , info.minor_version );
+       mlt_properties_set_double ( metadata, "schema_version" , 0.1 );
+       mlt_properties_set ( metadata, "title" , info.name );
+       mlt_properties_set_double ( metadata, "version",
+               info.major_version +  info.minor_version / pow( 10, strlen( string ) ) );
+       mlt_properties_set ( metadata, "identifier" , service_name );
+       mlt_properties_set ( metadata, "description" , info.explanation );
+       mlt_properties_set ( metadata, "creator" , info.author );
        switch (type){
+               case producer_type:
+                       mlt_properties_set ( metadata, "type" , "producer" );
+                       break;
                case filter_type:
-                       metadata_properties=mlt_repository_filters(repository);
+                       mlt_properties_set ( metadata, "type" , "filter" );
                        break;
                case transition_type:
-                       metadata_properties=mlt_repository_transitions(repository);
+                       mlt_properties_set ( metadata, "type" , "transition" );
                        break;
                default:
-                       metadata_properties=NULL;
-       }
-       
-       if (!metadata_properties){
-               return;
+                       break;
        }
-       
-       mlt_properties this_item_properties = mlt_properties_get_data( metadata_properties , name , NULL );
-       mlt_properties metadata = subtag( this_item_properties , "metadata" , ( mlt_serialiser )mlt_properties_serialise_yaml );
-       
-       char descstr[2048];
-       snprintf ( descstr, 2048 , "%s (Version: %d.%d)" , info->explanation , info->major_version , info->minor_version );
-       mlt_properties_set ( metadata, "title" , info->name );
-       mlt_properties_set ( metadata, "identifier" , name );
-       mlt_properties_set ( metadata, "description" , descstr );
-       mlt_properties_set ( metadata, "creator" , info->author );
-       
-       mlt_properties parameter = subtag ( metadata , "parameters" , NULL );
-       mlt_properties tags = subtag ( metadata , "tags" , NULL );
+
+       mlt_properties parameter = mlt_properties_new ( );
+       mlt_properties_set_data ( metadata , "parameters" , parameter , 0 , ( mlt_destructor )mlt_properties_close, NULL );
+       mlt_properties tags = mlt_properties_new ( );
+       mlt_properties_set_data ( metadata , "tags" , tags , 0 , ( mlt_destructor )mlt_properties_close, NULL );
        mlt_properties_set ( tags , "0" , "Video" );
-       
-       char numstr[512];
-       
-       for (j=0;j<info->num_params;j++){
-               snprintf ( numstr , 512 , "%d" , j );
-               mlt_properties pnum = subtag ( parameter , numstr , NULL );
-               
+
+       for (j=0;j<info.num_params;j++){
+               snprintf ( string , sizeof(string), "%d" , j );
+               mlt_properties pnum = mlt_properties_new ( );
+               mlt_properties_set_data ( parameter , string , pnum , 0 , ( mlt_destructor )mlt_properties_close, NULL );
                f0r_param_info_t paraminfo;
                param_info(&paraminfo,j);
                mlt_properties_set ( pnum , "identifier" , paraminfo.name );
                mlt_properties_set ( pnum , "title" , paraminfo.name );
                mlt_properties_set ( pnum , "description" , paraminfo.explanation);
-               
                if ( paraminfo.type == F0R_PARAM_DOUBLE ){
-                       mlt_properties_set ( pnum , "type" , "integer" );
+                       mlt_properties_set ( pnum , "type" , "float" );
                        mlt_properties_set ( pnum , "minimum" , "0" );
-                       mlt_properties_set ( pnum , "maximum" , "1000" );
+                       mlt_properties_set ( pnum , "maximum" , "1" );
                        mlt_properties_set ( pnum , "readonly" , "no" );
                        mlt_properties_set ( pnum , "widget" , "spinner" );
                }else
@@ -103,15 +183,24 @@ void fill_param_info ( mlt_repository repository , void* handle, f0r_plugin_info
                        mlt_properties_set ( pnum , "maximum" , "1" );
                        mlt_properties_set ( pnum , "readonly" , "no" );
                }else
+               if ( paraminfo.type == F0R_PARAM_COLOR ){
+                       mlt_properties_set ( pnum , "type" , "color" );
+                       mlt_properties_set ( pnum , "readonly" , "no" );
+               }else
                if ( paraminfo.type == F0R_PARAM_STRING ){
                        mlt_properties_set ( pnum , "type" , "string" );
                        mlt_properties_set ( pnum , "readonly" , "no" );
                }
        }
+       f0r_deinit();
+       dlclose(handle);
+       free(name);
+
+       return metadata;
 }
 
-void * load_lib(  mlt_profile profile, mlt_service_type type , void* handle){
-       
+static void * load_lib( mlt_profile profile, mlt_service_type type , void* handle, const char *name ){
+
        int i=0;
        void (*f0r_get_plugin_info)(f0r_plugin_info_t*),
                *f0r_construct , *f0r_update , *f0r_destruct,
@@ -121,41 +210,59 @@ void * load_lib(  mlt_profile profile, mlt_service_type type , void* handle){
                (*f0r_get_param_value)(f0r_instance_t instance, f0r_param_t param, int param_index),
                (*f0r_update2) (f0r_instance_t instance, double time,   const uint32_t* inframe1,
                  const uint32_t* inframe2,const uint32_t* inframe3, uint32_t* outframe);
-                               
-       if ( ( f0r_construct = dlsym(handle, "f0r_construct") ) && 
-                               (f0r_update = dlsym(handle,"f0r_update") ) && 
-                               (f0r_destruct = dlsym(handle,"f0r_destruct") ) && 
-                               (f0r_get_plugin_info = dlsym(handle,"f0r_get_plugin_info") ) && 
-                               (f0r_get_param_info = dlsym(handle,"f0r_get_param_info") ) && 
+
+       if ( ( f0r_construct = dlsym(handle, "f0r_construct") ) &&
+                               (f0r_destruct = dlsym(handle,"f0r_destruct") ) &&
+                               (f0r_get_plugin_info = dlsym(handle,"f0r_get_plugin_info") ) &&
+                               (f0r_get_param_info = dlsym(handle,"f0r_get_param_info") ) &&
                                (f0r_set_param_value= dlsym(handle,"f0r_set_param_value" ) ) &&
                                (f0r_get_param_value= dlsym(handle,"f0r_get_param_value" ) ) &&
                                (f0r_init= dlsym(handle,"f0r_init" ) ) &&
-                               (f0r_deinit= dlsym(handle,"f0r_deinit" ) ) 
+                               (f0r_deinit= dlsym(handle,"f0r_deinit" ) )
                ){
-       
+
+               f0r_update=dlsym(handle,"f0r_update");
                f0r_update2=dlsym(handle,"f0r_update2");
-               
+
                f0r_plugin_info_t info;
                f0r_get_plugin_info(&info);
-               
-               void* ret=NULL; 
+
+               void* ret=NULL;
                mlt_properties properties=NULL;
-               
-               if (type == filter_type && info.plugin_type == F0R_PLUGIN_TYPE_FILTER ){
+               char minor[12];
+
+               if (type == producer_type && info.plugin_type == F0R_PLUGIN_TYPE_SOURCE ){
+                       mlt_producer this = mlt_producer_new( profile );
+                       if ( this != NULL )
+                       {
+                               this->get_frame = producer_get_frame;
+                               this->close = ( mlt_destructor )producer_close;
+                               f0r_init();
+                               properties=MLT_PRODUCER_PROPERTIES ( this );
+
+                               for (i=0;i<info.num_params;i++){
+                                       f0r_param_info_t pinfo;
+                                       f0r_get_param_info(&pinfo,i);
+
+                               }
+
+                               ret=this;
+                       }
+               } else if (type == filter_type && info.plugin_type == F0R_PLUGIN_TYPE_FILTER ){
                        mlt_filter this = mlt_filter_new( );
                        if ( this != NULL )
                        {
                                this->process = filter_process;
-                               this->close = filter_close;                     
+                               this->close = filter_close;
                                f0r_init();
                                properties=MLT_FILTER_PROPERTIES ( this );
-                                                       
+
                                for (i=0;i<info.num_params;i++){
                                        f0r_param_info_t pinfo;
                                        f0r_get_param_info(&pinfo,i);
-                                       
+
                                }
-                               
+
                                ret=this;
                        }
                }else if (type == transition_type && info.plugin_type == F0R_PLUGIN_TYPE_MIXER2){
@@ -170,57 +277,77 @@ void * load_lib(  mlt_profile profile, mlt_service_type type , void* handle){
                                ret=transition;
                        }
                }
-               mlt_properties_set_data(properties, "_dlclose_handle", handle , sizeof (void*) , NULL , NULL );
+               check_thread_safe( properties, name );
+               mlt_properties_set_data(properties, "_dlclose_handle", handle , sizeof ( handle ) , NULL , NULL );
                mlt_properties_set_data(properties, "_dlclose", dlclose , sizeof (void*) , NULL , NULL );
-               mlt_properties_set_data(properties, "f0r_construct", f0r_construct , sizeof(void*),NULL,NULL);
-               mlt_properties_set_data(properties, "f0r_update", f0r_update , sizeof(void*),NULL,NULL);
+               mlt_properties_set_data(properties, "f0r_construct", f0r_construct , sizeof( f0r_construct ),NULL,NULL);
+               mlt_properties_set_data(properties, "f0r_update", f0r_update , sizeof( f0r_update ),NULL,NULL);
                if (f0r_update2)
-                       mlt_properties_set_data(properties, "f0r_update2", f0r_update2 , sizeof(void*),NULL,NULL);
-               mlt_properties_set_data(properties, "f0r_destruct", f0r_destruct , sizeof(void*),NULL,NULL);
+                       mlt_properties_set_data(properties, "f0r_update2", f0r_update2 , sizeof( f0r_update2 ),NULL,NULL);
+               mlt_properties_set_data(properties, "f0r_destruct", f0r_destruct , sizeof( f0r_destruct ),NULL,NULL);
                mlt_properties_set_data(properties, "f0r_get_plugin_info", f0r_get_plugin_info , sizeof(void*),NULL,NULL);
                mlt_properties_set_data(properties, "f0r_get_param_info", f0r_get_param_info , sizeof(void*),NULL,NULL);
                mlt_properties_set_data(properties, "f0r_set_param_value", f0r_set_param_value , sizeof(void*),NULL,NULL);
                mlt_properties_set_data(properties, "f0r_get_param_value", f0r_get_param_value , sizeof(void*),NULL,NULL);
-               
-               
+
+               // Let frei0r plugin version be serialized using same format as metadata
+               snprintf( minor, sizeof( minor ), "%d", info.minor_version );
+               mlt_properties_set_double( properties, "version", info.major_version +  info.minor_version / pow( 10, strlen( minor ) ) );
+
                return ret;
        }else{
-               printf("some was wrong\n");
+               mlt_log_error( NULL, "frei0r plugin \"%s\" is missing a function\n", name );
                dlerror();
        }
        return NULL;
 }
 
-void * create_frei0r_item ( mlt_profile profile, mlt_service_type type, const char *id, void *arg){
+static void * create_frei0r_item ( mlt_profile profile, mlt_service_type type, const char *id, void *arg){
 
        mlt_tokeniser tokeniser = mlt_tokeniser_init ( );
+       char *frei0r_path = get_frei0r_path();
        int dircount=mlt_tokeniser_parse_new (
-               tokeniser ,
-               getenv("MLT_FREI0R_PLUGIN_PATH") ? getenv("MLT_FREI0R_PLUGIN_PATH") : "/usr/lib/frei0r-1" ,
+               tokeniser,
+               frei0r_path,
                 ":"
        );
        void* ret=NULL;
        while (dircount--){
-               char soname[1024]="";
-               
+               char soname[PATH_MAX];
+               char *myid = strdup( id );
+
+#ifdef WIN32
+               char *firstname = strtok( myid, "." );
+#else
                char *save_firstptr = NULL;
-               char *firstname=strtok_r(strdup(id),".",&save_firstptr);
-               
-               firstname=strtok_r(NULL,".",&save_firstptr);
-               sprintf(soname,"%s/%s.so", mlt_tokeniser_get_string( tokeniser , dircount ) , firstname );
+               char *firstname = strtok_r( myid, ".", &save_firstptr );
+#endif
+               char* directory = mlt_tokeniser_get_string (tokeniser, dircount);
+
+#ifdef WIN32
+               firstname = strtok( NULL, "." );
+#else
+               firstname = strtok_r( NULL, ".", &save_firstptr );
+#endif
+               if (strncmp(directory, "$HOME", 5))
+                       snprintf(soname, PATH_MAX, "%s/%s" LIBSUF, directory, firstname );
+               else
+                       snprintf(soname, PATH_MAX, "%s%s/%s" LIBSUF, getenv("HOME"), strchr(directory, '/'), firstname );
 
                if (firstname){
-                       
+
                        void* handle=dlopen(soname,RTLD_LAZY);
-                       
+
                        if (handle ){
-                               ret=load_lib ( profile , type , handle );
+                               ret=load_lib ( profile , type , handle, firstname );
                        }else{
                                dlerror();
                        }
                }
+               free( myid );
        }
        mlt_tokeniser_close ( tokeniser );
+       free( frei0r_path );
        return ret;
 }
 
@@ -229,46 +356,76 @@ MLT_REPOSITORY
 {
        int i=0;
        mlt_tokeniser tokeniser = mlt_tokeniser_init ( );
+       char *frei0r_path = get_frei0r_path();
        int dircount=mlt_tokeniser_parse_new (
                tokeniser ,
-       getenv("MLT_FREI0R_PLUGIN_PATH") ? getenv("MLT_FREI0R_PLUGIN_PATH") : "/usr/lib/frei0r-1" ,
+               frei0r_path,
                ":"
        );
-       
+       char dirname[PATH_MAX];
+       snprintf( dirname, PATH_MAX, "%s/frei0r/blacklist.txt", mlt_environment( "MLT_DATA" ) );
+       mlt_properties blacklist = mlt_properties_load( dirname );
+
        while (dircount--){
-               
+
                mlt_properties direntries = mlt_properties_new();
-               char* dirname = mlt_tokeniser_get_string ( tokeniser , dircount ) ;
-               mlt_properties_dir_list(direntries, dirname ,"*.so",1);
-       
-               for (i=0;i<mlt_properties_count(direntries);i++){
-                       char* name=mlt_properties_get_value(direntries,i);      
+               char* directory = mlt_tokeniser_get_string (tokeniser, dircount);
+               
+               if (strncmp(directory, "$HOME", 5))
+                       snprintf(dirname, PATH_MAX, "%s", directory);
+               else
+                       snprintf(dirname, PATH_MAX, "%s%s", getenv("HOME"), strchr(directory, '/'));
+               mlt_properties_dir_list(direntries, dirname ,"*" LIBSUF, 1);
+
+               for (i=0; i<mlt_properties_count(direntries);i++){
+                       char* name=mlt_properties_get_value(direntries,i);
                        char* shortname=name+strlen(dirname)+1;
-                       char fname[1024]="";
-                       
-                       strcat(fname,dirname);
-                       strcat(fname,shortname);
-                       
+#ifdef WIN32
+                       char* firstname = strtok( shortname, "." );
+#else
                        char *save_firstptr = NULL;
+                       char* firstname = strtok_r( shortname, ".", &save_firstptr );
+#endif
                        char pluginname[1024]="frei0r.";
-                       char* firstname = strtok_r ( shortname , "." , &save_firstptr );
-                       strcat(pluginname,firstname);
-       
-                       void* handle=dlopen(strcat(name,".so"),RTLD_LAZY);
+                       if ( firstname )
+                               strncat( pluginname, firstname, sizeof( pluginname ) - strlen( pluginname ) -1 );
+
+                       if ( firstname && mlt_properties_get( blacklist, firstname ) )
+                               continue;
+
+                       void* handle=dlopen(strcat(name, LIBSUF),RTLD_LAZY);
                        if (handle){
                                void (*plginfo)(f0r_plugin_info_t*)=dlsym(handle,"f0r_get_plugin_info");
-                               
+
                                if (plginfo){
                                        f0r_plugin_info_t info;
                                        plginfo(&info);
-                                       
-                                       if (firstname && info.plugin_type==F0R_PLUGIN_TYPE_FILTER){
+                                       if (firstname && info.plugin_type==F0R_PLUGIN_TYPE_SOURCE){
+                                               if (mlt_properties_get(mlt_repository_producers(repository), pluginname))
+                                               {
+                                                       dlclose(handle);
+                                                       continue;
+                                               }
+                                               MLT_REGISTER( producer_type, pluginname, create_frei0r_item );
+                                               MLT_REGISTER_METADATA( producer_type, pluginname, fill_param_info, strdup(name) );
+                                       }
+                                       else if (firstname && info.plugin_type==F0R_PLUGIN_TYPE_FILTER){
+                                               if (mlt_properties_get(mlt_repository_filters(repository), pluginname))
+                                               {
+                                                       dlclose(handle);
+                                                       continue;
+                                               }
                                                MLT_REGISTER( filter_type, pluginname, create_frei0r_item );
-                                               fill_param_info ( repository , handle, &info , filter_type , pluginname );
+                                               MLT_REGISTER_METADATA( filter_type, pluginname, fill_param_info, strdup(name) );
                                        }
                                        else if (firstname && info.plugin_type==F0R_PLUGIN_TYPE_MIXER2 ){
+                                               if (mlt_properties_get(mlt_repository_transitions(repository), pluginname))
+                                               {
+                                                       dlclose(handle);
+                                                       continue;
+                                               }
                                                MLT_REGISTER( transition_type, pluginname, create_frei0r_item );
-                                               fill_param_info ( repository , handle, &info , transition_type , pluginname );
+                                               MLT_REGISTER_METADATA( transition_type, pluginname, fill_param_info, strdup(name) );
                                        }
                                }
                                dlclose(handle);
@@ -277,4 +434,6 @@ MLT_REPOSITORY
                mlt_properties_close(direntries);
        }
        mlt_tokeniser_close ( tokeniser );
+       mlt_properties_close( blacklist );
+       free( frei0r_path );
 }