]> git.sesse.net Git - mlt/blobdiff - src/melt/melt.c
When doing glReadPixels(), make sure we read from the right FBO.
[mlt] / src / melt / melt.c
index 177ab5af36c45f399aa391a4763470badcbb51f2..4526090d9ab148d49276944c523da1814a66fb87 100644 (file)
@@ -1,6 +1,6 @@
 /*
  * melt.c -- MLT command line utility
- * Copyright (C) 2002-2011 Ushodaya Enterprises Limited
+ * Copyright (C) 2002-2013 Ushodaya Enterprises Limited
  * Authors: Charles Yates <charles.yates@pandora.be>
  *          Dan Dennedy <dan@dennedy.org>
  *
 #include <sched.h>
 #include <libgen.h>
 #include <limits.h>
+#include <unistd.h>
+#include <signal.h>
 
 #include <framework/mlt.h>
 
-#if defined(__DARWIN__) || defined(WIN32)
+#if (defined(__DARWIN__) || defined(WIN32)) && !defined(MELT_NOSDL)
 #include <SDL.h>
 #endif
 
 #include "io.h"
 
+static mlt_producer melt = NULL;
+
+static void stop_handler(int signum)
+{
+       if ( melt )
+       {
+               mlt_properties properties = MLT_PRODUCER_PROPERTIES( melt );
+               mlt_properties_set_int( properties, "done", 1 );
+       }
+}
+
+static void abnormal_exit_handler(int signum)
+{
+       // The process is going down hard. Restore the terminal first.
+       term_exit();
+       // Reset the default handler so the core gets dumped.
+       signal( signum, SIG_DFL );
+       raise( signum );
+}
+
 static void transport_action( mlt_producer producer, char *value )
 {
        mlt_properties properties = MLT_PRODUCER_PROPERTIES( producer );
        mlt_multitrack multitrack = mlt_properties_get_data( properties, "multitrack", NULL );
        mlt_consumer consumer = mlt_properties_get_data( properties, "transport_consumer", NULL );
        mlt_properties jack = mlt_properties_get_data( MLT_CONSUMER_PROPERTIES( consumer ), "jack_filter", NULL );
-       mlt_position position = mlt_producer_position( producer );
+       mlt_position position = producer? mlt_producer_position( producer ) : 0;
 
        mlt_properties_set_int( properties, "stats_off", 1 );
 
@@ -78,6 +100,7 @@ static void transport_action( mlt_producer producer, char *value )
                        case '5':
                                mlt_producer_set_speed( producer, 0 );
                                mlt_consumer_purge( consumer );
+                               mlt_producer_seek( producer, mlt_consumer_position( consumer ) + 1 );
                                mlt_events_fire( jack, "jack-stop", NULL );
                                break;
                        case '6':
@@ -261,7 +284,59 @@ static mlt_consumer create_consumer( mlt_profile profile, char *id )
 static void load_consumer( mlt_consumer *consumer, mlt_profile profile, int argc, char **argv )
 {
        int i;
-       for ( i = 1; i < argc; i ++ )
+       int multi = 0;
+       int qglsl = 0;
+
+       for ( i = 1; i < argc; i ++ ) {
+               // See if we need multi consumer.
+               multi += !strcmp( argv[i], "-consumer" );
+               // Seee if we need the qglsl variant of multi consumer.
+               if ( !strncmp( argv[i], "glsl.", 5 ) || !strncmp( argv[i], "movit.", 6 ) )
+                       qglsl = 1;
+       }
+       // Disable qglsl if xgl is being used!
+       for ( i = 1; qglsl && i < argc; i ++ )
+               if ( !strcmp( argv[i], "xgl" ) )
+                       qglsl = 0;
+
+       if ( multi > 1 || qglsl )
+       {
+               // If there is more than one -consumer use the 'multi' consumer.
+               int k = 0;
+               char key[20];
+
+               if ( *consumer )
+                       mlt_consumer_close( *consumer );
+               *consumer = create_consumer( profile, ( qglsl? "qglsl" : "multi" ) );
+               mlt_properties properties = MLT_CONSUMER_PROPERTIES( *consumer );
+               for ( i = 1; i < argc; i ++ )
+               {
+                       if ( !strcmp( argv[ i ], "-consumer" ) && argv[ i + 1 ])
+                       {
+                               // Create a properties object for each sub-consumer
+                               mlt_properties new_props = mlt_properties_new();
+                               snprintf( key, sizeof(key), "%d", k++ );
+                               mlt_properties_set_data( properties, key, new_props, 0,
+                                       (mlt_destructor) mlt_properties_close, NULL );
+                               if ( strchr( argv[i + 1], ':' ) )
+                               {
+                                       char *temp = strdup( argv[++i] );
+                                       char *service = temp;
+                                       char *target = strchr( temp, ':' );
+                                       *target++ = 0;
+                                       mlt_properties_set( new_props, "mlt_service", service );
+                                       mlt_properties_set( new_props, "target", target );
+                               }
+                               else
+                               {
+                                       mlt_properties_set( new_props, "mlt_service", argv[ ++i ] );
+                               }
+                               while ( argv[ i + 1 ] && strchr( argv[ i + 1 ], '=' ) )
+                                       mlt_properties_parse( new_props, argv[ ++ i ] );
+                       }
+               }
+       }
+       else for ( i = 1; i < argc; i ++ )
        {
                if ( !strcmp( argv[ i ], "-consumer" ) )
                {
@@ -271,14 +346,14 @@ static void load_consumer( mlt_consumer *consumer, mlt_profile profile, int argc
                        if ( *consumer )
                        {
                                mlt_properties properties = MLT_CONSUMER_PROPERTIES( *consumer );
-                               while ( argv[ i + 1 ] != NULL && strstr( argv[ i + 1 ], "=" ) )
+                               while ( argv[ i + 1 ] != NULL && strchr( argv[ i + 1 ], '=' ) )
                                        mlt_properties_parse( properties, argv[ ++ i ] );
                        }
                }
        }
 }
 
-#if defined(__DARWIN__) || defined(WIN32)
+#if (defined(__DARWIN__) || defined(WIN32)) && !defined(MELT_NOSDL)
 
 static void event_handling( mlt_producer producer, mlt_consumer consumer )
 {
@@ -310,7 +385,7 @@ static void transport( mlt_producer producer, mlt_consumer consumer )
        mlt_properties properties = MLT_PRODUCER_PROPERTIES( producer );
        int silent = mlt_properties_get_int( MLT_CONSUMER_PROPERTIES( consumer ), "silent" );
        int progress = mlt_properties_get_int( MLT_CONSUMER_PROPERTIES( consumer ), "progress" );
-       struct timespec tm = { 0, 40000 };
+       struct timespec tm = { 0, 40000000 };
        int total_length = mlt_producer_get_length( producer );
        int last_position = 0;
 
@@ -342,7 +417,7 @@ static void transport( mlt_producer producer, mlt_consumer consumer )
                                transport_action( producer, string );
                        }
 
-#if defined(__DARWIN__) || defined(WIN32)
+#if (defined(__DARWIN__) || defined(WIN32)) && !defined(MELT_NOSDL)
                        event_handling( producer, consumer );
 #endif
 
@@ -353,8 +428,9 @@ static void transport( mlt_producer producer, mlt_consumer consumer )
                                        int current_position = mlt_producer_position( producer );
                                        if ( current_position > last_position )
                                        {
-                                               fprintf( stderr, "Current Frame: %10d, percentage: %10d\r",
-                                                       current_position, 100 * current_position / total_length );
+                                               fprintf( stderr, "Current Frame: %10d, percentage: %10d%c",
+                                                       current_position, 100 * current_position / total_length,
+                                                       progress == 2 ? '\n' : '\r' );
                                                last_position = current_position;
                                        }
                                }
@@ -362,6 +438,7 @@ static void transport( mlt_producer producer, mlt_consumer consumer )
                                {
                                        fprintf( stderr, "Current Position: %10d\r", (int)mlt_consumer_position( consumer ) );
                                }
+                               fflush( stderr );
                        }
 
                        if ( silent || progress )
@@ -375,7 +452,7 @@ static void transport( mlt_producer producer, mlt_consumer consumer )
 
 static void show_usage( char *program_name )
 {
-       fprintf( stderr,
+       fprintf( stdout,
 "Usage: %s [options] [producer [name=value]* ]+\n"
 "Options:\n"
 "  -attach filter[:arg] [name=value]*       Attach a filter to the output\n"
@@ -389,6 +466,7 @@ static void show_usage( char *program_name )
 "  -filter filter[:arg] [name=value]*       Add a filter to the current track\n"
 "  -group [name=value]*                     Apply properties repeatedly\n"
 "  -help                                    Show this message\n"
+"  -jack                                    Enable JACK transport synchronization\n"
 "  -join clips                              Join multiple clips into one cut\n"
 "  -mix length                              Add a mix between the last two cuts\n"
 "  -mixer transition                        Add a transition to the mix\n"
@@ -420,67 +498,46 @@ static void show_usage( char *program_name )
        basename( program_name ) );
 }
 
-static void guess_profile( mlt_producer melt, mlt_profile profile )
-{
-       mlt_frame fr = NULL;
-       uint8_t *buffer;
-       mlt_image_format fmt = mlt_image_yuv422;
-       mlt_properties p;
-       int w = profile->width;
-       int h = profile->height;
-
-       if ( ! mlt_service_get_frame( MLT_PRODUCER_SERVICE(melt), &fr, 0 ) && fr )
-       {
-               mlt_properties_set_double( MLT_FRAME_PROPERTIES( fr ), "consumer_aspect_ratio", mlt_profile_sar( profile ) );
-               if ( ! mlt_frame_get_image( fr, &buffer, &fmt, &w, &h, 0 ) )
-               {
-                       // Some source properties are not exposed until after the first get_image call.
-                       mlt_frame_close( fr );
-                       mlt_service_get_frame( MLT_PRODUCER_SERVICE(melt), &fr, 0 );
-                       p = MLT_FRAME_PROPERTIES( fr );
-//                     mlt_properties_dump(p, stderr);
-                       if ( mlt_properties_get_int( p, "meta.media.frame_rate_den" ) && mlt_properties_get_int( p, "meta.media.sample_aspect_den" ) )
-                       {
-                               profile->width = mlt_properties_get_int( p, "meta.media.width" );
-                               profile->height = mlt_properties_get_int( p, "meta.media.height" );
-                               profile->progressive = mlt_properties_get_int( p, "meta.media.progressive" );
-                               profile->frame_rate_num = mlt_properties_get_int( p, "meta.media.frame_rate_num" );
-                               profile->frame_rate_den = mlt_properties_get_int( p, "meta.media.frame_rate_den" );
-                               // AVCHD is mis-reported as double frame rate.
-                               if ( profile->progressive == 0 && (
-                                    profile->frame_rate_num / profile->frame_rate_den == 50 ||
-                                    profile->frame_rate_num / profile->frame_rate_den == 59 ) )
-                                       profile->frame_rate_num /= 2;
-                               profile->sample_aspect_num = mlt_properties_get_int( p, "meta.media.sample_aspect_num" );
-                               profile->sample_aspect_den = mlt_properties_get_int( p, "meta.media.sample_aspect_den" );
-                               profile->colorspace = mlt_properties_get_int( p, "meta.media.colorspace" );
-                               profile->display_aspect_num = (int) ( (double) profile->sample_aspect_num * profile->width / profile->sample_aspect_den + 0.5 );
-                               profile->display_aspect_den = profile->height;
-                               free( profile->description );
-                               profile->description = strdup( "automatic" );
-                               profile->is_explicit = 0;
-                       }
-               }
-       }
-       mlt_frame_close( fr );
-       mlt_producer_seek( melt, 0 );
-}
-
 static void query_metadata( mlt_repository repo, mlt_service_type type, const char *typestr, char *id )
 {
        mlt_properties metadata = mlt_repository_metadata( repo, type, id );
        if ( metadata )
        {
                char *s = mlt_properties_serialise_yaml( metadata );
-               fprintf( stderr, "%s", s );
+               fprintf( stdout, "%s", s );
                free( s );
        }
        else
        {
-               fprintf( stderr, "# No metadata for %s \"%s\"\n", typestr, id );
+               fprintf( stdout, "# No metadata for %s \"%s\"\n", typestr, id );
        }
 }
 
+static int is_service_hidden(mlt_repository repo, mlt_service_type type, const char *service_name )
+{
+       mlt_properties metadata = NULL;
+       mlt_properties tags = NULL;
+       metadata = mlt_repository_metadata(repo, type, service_name);
+
+       if( metadata )
+       {
+               tags = mlt_properties_get_data( metadata, "tags", NULL );
+               if( tags )
+               {
+                       int k;
+                       for ( k = 0; k < mlt_properties_count( tags ); k++ )
+                       {
+                               const char* value = mlt_properties_get_value(tags, k);
+                               if( !strcmp("Hidden", value) )
+                               {
+                                       return 1;
+                               }
+                       }
+               }
+       }
+       return 0;
+}
+
 static void query_services( mlt_repository repo, mlt_service_type type )
 {
        mlt_properties services = NULL;
@@ -506,27 +563,31 @@ static void query_services( mlt_repository repo, mlt_service_type type )
                default:
                        return;
        }
-       fprintf( stderr, "---\n%s:\n", typestr );
+       fprintf( stdout, "---\n%s:\n", typestr );
        if ( services )
        {
                int j;
                for ( j = 0; j < mlt_properties_count( services ); j++ )
-                       fprintf( stderr, "  - %s\n", mlt_properties_get_name( services, j ) );
+               {
+                       const char* service_name = mlt_properties_get_name( services, j );
+                       if( !is_service_hidden(repo, type, service_name ) )
+                               fprintf( stdout, "  - %s\n", service_name );
+               }
        }
-       fprintf( stderr, "...\n" );
+       fprintf( stdout, "...\n" );
 }
 
 static void query_profiles()
 {
        mlt_properties profiles = mlt_profile_list();
-       fprintf( stderr, "---\nprofiles:\n" );
+       fprintf( stdout, "---\nprofiles:\n" );
        if ( profiles )
        {
                int j;
                for ( j = 0; j < mlt_properties_count( profiles ); j++ )
-                       fprintf( stderr, "  - %s\n", mlt_properties_get_name( profiles, j ) );
+                       fprintf( stdout, "  - %s\n", mlt_properties_get_name( profiles, j ) );
        }
-       fprintf( stderr, "...\n" );
+       fprintf( stdout, "...\n" );
        mlt_properties_close( profiles );
 }
 
@@ -537,12 +598,12 @@ static void query_profile( const char *id )
        if ( profile )
        {
                char *s = mlt_properties_serialise_yaml( profile );
-               fprintf( stderr, "%s", s );
+               fprintf( stdout, "%s", s );
                free( s );
        }
        else
        {
-               fprintf( stderr, "# No metadata for profile \"%s\"\n", id );
+               fprintf( stdout, "# No metadata for profile \"%s\"\n", id );
        }
        mlt_properties_close( profiles );
 }
@@ -550,14 +611,14 @@ static void query_profile( const char *id )
 static void query_presets()
 {
        mlt_properties presets = mlt_repository_presets();
-       fprintf( stderr, "---\npresets:\n" );
+       fprintf( stdout, "---\npresets:\n" );
        if ( presets )
        {
                int j;
                for ( j = 0; j < mlt_properties_count( presets ); j++ )
-                       fprintf( stderr, "  - %s\n", mlt_properties_get_name( presets, j ) );
+                       fprintf( stdout, "  - %s\n", mlt_properties_get_name( presets, j ) );
        }
-       fprintf( stderr, "...\n" );
+       fprintf( stdout, "...\n" );
        mlt_properties_close( presets );
 }
 
@@ -568,12 +629,12 @@ static void query_preset( const char *id )
        if ( preset )
        {
                char *s = mlt_properties_serialise_yaml( preset );
-               fprintf( stderr, "%s", s );
+               fprintf( stdout, "%s", s );
                free( s );
        }
        else
        {
-               fprintf( stderr, "# No metadata for preset \"%s\"\n", id );
+               fprintf( stdout, "# No metadata for preset \"%s\"\n", id );
        }
        mlt_properties_close( presets );
 }
@@ -589,7 +650,7 @@ static void query_formats( )
        }
        else
        {
-               fprintf( stderr, "# No formats - failed to load avformat consumer\n" );
+               fprintf( stdout, "# No formats - failed to load avformat consumer\n" );
        }
 }
 
@@ -604,7 +665,7 @@ static void query_acodecs( )
        }
        else
        {
-               fprintf( stderr, "# No audio codecs - failed to load avformat consumer\n" );
+               fprintf( stdout, "# No audio codecs - failed to load avformat consumer\n" );
        }
 }
 
@@ -619,21 +680,19 @@ static void query_vcodecs( )
        }
        else
        {
-               fprintf( stderr, "# No video codecs - failed to load avformat consumer\n" );
+               fprintf( stdout, "# No video codecs - failed to load avformat consumer\n" );
        }
 }
 
 static void on_fatal_error( mlt_properties owner, mlt_consumer consumer )
 {
-       mlt_consumer_stop( consumer );
-       exit( EXIT_FAILURE );
+       mlt_properties_set_int( MLT_CONSUMER_PROPERTIES(consumer), "done", 1 );
 }
 
 int main( int argc, char **argv )
 {
        int i;
        mlt_consumer consumer = NULL;
-       mlt_producer melt = NULL;
        FILE *store = NULL;
        char *name = NULL;
        mlt_profile profile = NULL;
@@ -641,10 +700,15 @@ int main( int argc, char **argv )
        int is_silent = 0;
        mlt_profile backup_profile;
 
+       // Handle abnormal exit situations.
+       signal( SIGSEGV, abnormal_exit_handler );
+       signal( SIGILL, abnormal_exit_handler );
+       signal( SIGABRT, abnormal_exit_handler );
+
        // Construct the factory
        mlt_repository repo = mlt_factory_init( NULL );
 
-#ifdef WIN32
+#if defined(WIN32) && !defined(MELT_NOSDL)
        is_silent = 1;
 #endif
        
@@ -674,6 +738,10 @@ int main( int argc, char **argv )
                {
                        is_progress = 1;
                }
+               else if ( !strcmp( argv[ i ], "-progress2" ) )
+               {
+                       is_progress = 2;
+               }
                // Look for the query option
                else if ( !strcmp( argv[ i ], "-query" ) )
                {
@@ -721,7 +789,7 @@ query_all:
                                query_services( repo, filter_type );
                                query_services( repo, producer_type );
                                query_services( repo, transition_type );
-                               fprintf( stderr, "# You can query the metadata for a specific service using:\n"
+                               fprintf( stdout, "# You can query the metadata for a specific service using:\n"
                                        "# -query <type>=<identifer>\n"
                                        "# where <type> is one of: consumer, filter, producer, or transition.\n" );
                        }
@@ -737,8 +805,8 @@ query_all:
                }
                else if ( !strcmp( argv[ i ], "-version" ) || !strcmp( argv[ i ], "--version" ) )
                {
-                       fprintf( stderr, "MLT %s " VERSION "\n"
-                               "Copyright (C) 2002-2011 Ushodaya Enterprises Limited\n"
+                       fprintf( stdout, "%s " VERSION "\n"
+                               "Copyright (C) 2002-2013 Ushodaya Enterprises Limited\n"
                                "<http://www.mltframework.org/>\n"
                                "This is free software; see the source for copying conditions.  There is NO\n"
                                "warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n",
@@ -750,6 +818,8 @@ query_all:
                        mlt_log_set_level( MLT_LOG_DEBUG );
                }
        }
+       if ( !is_silent && !isatty( STDIN_FILENO ) && !is_progress )
+               is_progress = 1;
 
        // Create profile if not set explicitly
        if ( getenv( "MLT_PROFILE" ) )
@@ -784,15 +854,27 @@ query_all:
                // Generate an automatic profile if needed.
                if ( ! profile->is_explicit )
                {
-                       guess_profile( melt, profile );
+                       mlt_profile_from_producer( profile, melt );
                        mlt_producer_close( melt );
                        melt = mlt_factory_producer( profile, "melt", &argv[ 1 ] );
                }
                
                // Reload the consumer with the fully qualified profile.
-               // The producer or guess_profile could have changed the profile.
+               // The producer or auto-profile could have changed the profile.
                load_consumer( &consumer, profile, argc, argv );
 
+               // See if producer has consumer already attached
+               if ( !store && !consumer )
+               {
+                       consumer = MLT_CONSUMER( mlt_service_consumer( MLT_PRODUCER_SERVICE( melt ) ) );
+                       if ( consumer )
+                       {
+                               mlt_properties_inc_ref( MLT_CONSUMER_PROPERTIES(consumer) ); // because we explicitly close it
+                               mlt_properties_set_data( MLT_CONSUMER_PROPERTIES(consumer),
+                                       "transport_callback", transport_action, 0, NULL, NULL );
+                       }
+               }
+
                // If we have no consumer, default to sdl
                if ( store == NULL && consumer == NULL )
                        consumer = create_consumer( profile, NULL );
@@ -860,6 +942,14 @@ query_all:
                        mlt_events_listen( properties, consumer, "consumer-fatal-error", ( mlt_listener )on_fatal_error );
                        if ( mlt_consumer_start( consumer ) == 0 )
                        {
+                               // Try to exit gracefully upon these signals
+                               signal( SIGINT, stop_handler );
+                               signal( SIGTERM, stop_handler );
+#ifndef WIN32
+                               signal( SIGHUP, stop_handler );
+                               signal( SIGPIPE, stop_handler );
+#endif
+
                                // Transport functionality
                                transport( melt, consumer );
                                
@@ -878,6 +968,13 @@ query_all:
                show_usage( argv[0] );
        }
 
+       // Disconnect producer from consumer to prevent ref cycles from closing services
+       if ( consumer )
+       {
+               mlt_consumer_connect( consumer, NULL );
+               mlt_events_fire( MLT_CONSUMER_PROPERTIES(consumer), "consumer-cleanup", NULL);
+       }
+
        // Close the producer
        if ( melt != NULL )
                mlt_producer_close( melt );
@@ -890,8 +987,11 @@ query_all:
        mlt_profile_close( profile );
 
 exit_factory:
-               
+
+// Workaround qmelt on OS X from crashing at exit.
+#if !defined(__MACH__) || !defined(QT_GUI_LIB)
        mlt_factory_close( );
+#endif
 
        return 0;
 }