audio off
[melted] / src / modules / fezzik / producer_fezzik.c
index 3f0445b..429cf80 100644 (file)
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
+#include <ctype.h>
+#include <fnmatch.h>
 
 #include <framework/mlt.h>
 
+static mlt_properties dictionary = NULL;
+static mlt_properties normalisers = NULL;
+
 static void track_service( mlt_tractor tractor, void *service, mlt_destructor destructor )
 {
        mlt_properties properties = mlt_tractor_properties( tractor );
@@ -38,59 +43,81 @@ static void track_service( mlt_tractor tractor, void *service, mlt_destructor de
        free( real );
 }
 
+static mlt_producer create_from( char *file, char *services )
+{
+       mlt_producer producer = NULL;
+       char *temp = strdup( services );
+       char *service = temp;
+       do
+       {
+               char *p = strchr( service, ',' );
+               if ( p != NULL )
+                       *p ++ = '\0';
+               producer = mlt_factory_producer( service, file );
+               service = p;
+       }
+       while ( producer == NULL && service != NULL );
+       free( temp );
+       return producer;
+}
+
 static mlt_producer create_producer( char *file )
 {
        mlt_producer result = NULL;
 
-       // 1st Line preferences
-       if ( strstr( file, ".inigo" ) )
-               result = mlt_factory_producer( "inigo_file", file );
-       else if ( strstr( file, ".mpg" ) )
-               result = mlt_factory_producer( "mcmpeg", file );
-       else if ( strstr( file, ".mpeg" ) )
-               result = mlt_factory_producer( "mcmpeg", file );
-       else if ( strstr( file, ".dv" ) )
-               result = mlt_factory_producer( "mcdv", file );
-       else if ( strstr( file, ".dif" ) )
-               result = mlt_factory_producer( "mcdv", file );
-       else if ( strstr( file, ".jpg" ) )
-               result = mlt_factory_producer( "pixbuf", file );
-       else if ( strstr( file, ".JPG" ) )
-               result = mlt_factory_producer( "pixbuf", file );
-       else if ( strstr( file, ".jpeg" ) )
-               result = mlt_factory_producer( "pixbuf", file );
-       else if ( strstr( file, ".png" ) )
-               result = mlt_factory_producer( "pixbuf", file );
-       else if ( strstr( file, ".svg" ) )
-               result = mlt_factory_producer( "pixbuf", file );
-       else if ( strstr( file, ".txt" ) )
-               result = mlt_factory_producer( "pango", file );
-       else if ( strstr( file, ".westley" ) )
-               result = mlt_factory_producer( "westley", file );
-       else if ( strstr( file, ".ogg" ) )
-               result = mlt_factory_producer( "vorbis", file );
-
-       // 2nd Line fallbacks
-       if ( result == NULL )
+       // 1st Line - check for service:resource handling
+       if ( strchr( file, ':' ) )
        {
-               if ( strstr( file, ".dv" ) )
-                       result = mlt_factory_producer( "libdv", file );
-               else if ( strstr( file, ".dif" ) )
-                       result = mlt_factory_producer( "libdv", file );
+               char *temp = strdup( file );
+               char *service = temp;
+               char *resource = strchr( temp, ':' );
+               *resource ++ = '\0';
+               result = mlt_factory_producer( service, resource );
+               free( temp );
        }
 
-       // 3rd line fallbacks 
+       // 2nd Line preferences
        if ( result == NULL )
-               result = mlt_factory_producer( "avformat", file );
+       {
+               int i = 0;
+               char *lookup = strdup( file );
+               char *p = lookup;
+
+               // We only need to load the dictionary once
+               if ( dictionary == NULL )
+               {
+                       char temp[ 1024 ];
+                       sprintf( temp, "%s/fezzik.dict", mlt_factory_prefix( ) );
+                       dictionary = mlt_properties_load( temp );
+                       mlt_factory_register_for_clean_up( dictionary, ( mlt_destructor )mlt_properties_close );
+               }
+
+               // Convert the lookup string to lower case
+               while ( *p )
+               {
+                       *p = tolower( *p );
+                       p ++;
+               }
+
+               // Iterate through the dictionary
+               for ( i = 0; result == NULL && i < mlt_properties_count( dictionary ); i ++ )
+               {
+                       char *name = mlt_properties_get_name( dictionary, i );
+                       if ( fnmatch( name, lookup, 0 ) == 0 )
+                               result = create_from( file, mlt_properties_get_value( dictionary, i ) );
+               }
+
+               free( lookup );
+       }
 
-       // 4th - allow explicit construction
+       // Finally, try just loading as service
        if ( result == NULL )
                result = mlt_factory_producer( file, NULL );
 
        return result;
 }
 
-static mlt_service create_filter( mlt_tractor tractor, mlt_service last, char *effect )
+static mlt_service create_filter( mlt_tractor tractor, mlt_service last, char *effect, int *created )
 {
        char *id = strdup( effect );
        char *arg = strchr( id, ':' );
@@ -102,15 +129,51 @@ static mlt_service create_filter( mlt_tractor tractor, mlt_service last, char *e
                mlt_filter_connect( filter, last, 0 );
                track_service( tractor, filter, ( mlt_destructor )mlt_filter_close );
                last = mlt_filter_service( filter );
+               *created = 1;
        }
        free( id );
        return last;
 }
 
+static mlt_service attach_normalisers( mlt_tractor tractor, mlt_service last )
+{
+       // Loop variable
+       int i;
+
+       // Tokeniser
+       mlt_tokeniser tokeniser = mlt_tokeniser_init( );
+
+       // We only need to load the normalising properties once
+       if ( normalisers == NULL )
+       {
+               char temp[ 1024 ];
+               sprintf( temp, "%s/fezzik.ini", mlt_factory_prefix( ) );
+               normalisers = mlt_properties_load( temp );
+               mlt_factory_register_for_clean_up( normalisers, ( mlt_destructor )mlt_properties_close );
+       }
+
+       // Apply normalisers
+       for ( i = 0; i < mlt_properties_count( normalisers ); i ++ )
+       {
+               int j = 0;
+               int created = 0;
+               char *value = mlt_properties_get_value( normalisers, i );
+               mlt_tokeniser_parse_new( tokeniser, value, "," );
+               for ( j = 0; !created && j < mlt_tokeniser_count( tokeniser ); j ++ )
+                       last = create_filter( tractor, last, mlt_tokeniser_get_string( tokeniser, j ), &created );
+       }
+
+       // Close the tokeniser
+       mlt_tokeniser_close( tokeniser );
+
+       return last;
+}
+
 mlt_producer producer_fezzik_init( char *arg )
 {
        // Create the producer that the tractor will contain
        mlt_producer producer = NULL;
+
        if ( arg != NULL )
                producer = create_producer( arg );
 
@@ -136,10 +199,7 @@ mlt_producer producer_fezzik_init( char *arg )
                        mlt_properties_set_data( properties, "producer", producer, 0, ( mlt_destructor )mlt_producer_close, NULL );
 
                        // Now attach normalising filters
-                       last = create_filter( tractor, last, "deinterlace" );
-                       last = create_filter( tractor, last, "rescale" );
-                       last = create_filter( tractor, last, "resize" );
-                       last = create_filter( tractor, last, "resample" );
+                       last = attach_normalisers( tractor, last );
 
                        // Connect the tractor to the last
                        mlt_tractor_connect( tractor, last );
@@ -157,6 +217,9 @@ mlt_producer producer_fezzik_init( char *arg )
                        // We need to ensure that all further properties are mirrored in the producer
                        mlt_properties_mirror( properties, mlt_producer_properties( producer ) );
 
+                       // Ensure that the inner producer ignores the in point
+                       mlt_properties_set_int( mlt_producer_properties( producer ), "ignore_points", 1 );
+
                        // Now, we return the producer of the tractor
                        producer = mlt_tractor_producer( tractor );
                }