]> git.sesse.net Git - mlt/blobdiff - src/framework/mlt_profile.c
Restrict auto-profile frame rate to a sane value.
[mlt] / src / framework / mlt_profile.c
index e2ccc56c5a02b06d51b96c86904f33c7c91bdb9f..4385d7a789be5bf857c1d0b05f170256dce057f0 100644 (file)
  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  */
 
-#include "mlt_profile.h"
-#include "mlt_factory.h"
-#include "mlt_properties.h"
+#include "mlt.h"
 
 #include <stdlib.h>
 #include <string.h>
 #include <libgen.h>
+#include <math.h>
 
 
-/** the default subdirectory of the prefix for holding profiles */
-#define PROFILES_DIR "/share/mlt/profiles/"
+/** the default subdirectory of the datadir for holding profiles */
+#define PROFILES_DIR "/profiles/"
 
 /** Load a profile from the system folder.
  *
@@ -55,14 +54,12 @@ static mlt_profile mlt_profile_select( const char *name )
        {
                filename = calloc( 1, strlen( name ) + 1 );
        }
-       // Load from $prefix/share/mlt/profiles
+       // Load from $datadir/mlt/profiles
        else if ( prefix == NULL )
        {
-               prefix = PREFIX;
-               filename = calloc( 1, strlen( prefix ) + strlen( PROFILES_DIR ) + strlen( name ) + 2 );
+               prefix = mlt_environment( "MLT_DATA" );
+               filename = calloc( 1, strlen( prefix ) + strlen( PROFILES_DIR ) + strlen( name ) + 1 );
                strcpy( filename, prefix );
-               if ( filename[ strlen( filename ) - 1 ] != '/' )
-                       filename[ strlen( filename ) ] = '/';
                strcat( filename, PROFILES_DIR );
        }
        // Use environment variable instead
@@ -235,6 +232,8 @@ mlt_profile mlt_profile_load_properties( mlt_properties properties )
 mlt_profile mlt_profile_load_string( const char *string )
 {
        mlt_properties properties = mlt_properties_new();
+       mlt_profile profile = NULL;
+
        if ( properties )
        {
                const char *p = string;
@@ -245,8 +244,10 @@ mlt_profile mlt_profile_load_string( const char *string )
                        p = strchr( p, '\n' );
                        if ( p ) p++;
                }
+               profile = mlt_profile_load_properties( properties );
+               mlt_properties_close( properties );
        }
-       return mlt_profile_load_properties( properties );
+       return profile;
 }
 
 /** Get the video frame rate as a floating point value.
@@ -338,7 +339,7 @@ mlt_profile mlt_profile_clone( mlt_profile profile )
 
 /** Get the list of profiles.
  *
- * The caller MUST free the returned properties object!
+ * The caller MUST close the returned properties object!
  * Each entry in the list is keyed on its name, and its value is another
  * properties object that contains the attributes of the profile.
  * \public \memberof mlt_profile_s
@@ -347,6 +348,7 @@ mlt_profile mlt_profile_clone( mlt_profile profile )
 
 mlt_properties mlt_profile_list( )
 {
+       char *filename = NULL;
        const char *prefix = getenv( "MLT_PROFILES_PATH" );
        mlt_properties properties = mlt_properties_new();
        mlt_properties dir = mlt_properties_new();
@@ -354,9 +356,15 @@ mlt_properties mlt_profile_list( )
        const char *wildcard = NULL;
        int i;
 
-       // Load from $prefix/share/mlt/profiles if no env var
+       // Load from $datadir/mlt/profiles if no env var
        if ( prefix == NULL )
-               prefix = PREFIX;
+       {
+               prefix = mlt_environment( "MLT_DATA" );
+               filename = calloc( 1, strlen( prefix ) + strlen( PROFILES_DIR ) + 1 );
+               strcpy( filename, prefix );
+               strcat( filename, PROFILES_DIR );
+               prefix = filename;
+       }
 
        mlt_properties_dir_list( dir, prefix, wildcard, sort );
 
@@ -376,6 +384,72 @@ mlt_properties mlt_profile_list( )
                }
        }
        mlt_properties_close( dir );
+       if ( filename )
+               free( filename );
 
        return properties;
 }
+
+/** Update the profile using the attributes of a producer.
+ *
+ * Use this to make an "auto-profile." Typically, you need to re-open the producer
+ * after you use this because some producers (e.g. avformat) adjust their framerate
+ * to that of the profile used when you created it.
+ * \public \memberof mlt_profile_s
+ * \param profile the profile to update
+ * \param producer the producer to inspect
+ */
+
+void mlt_profile_from_producer( mlt_profile profile, mlt_producer producer )
+{
+       mlt_frame fr = NULL;
+       uint8_t *buffer = NULL;
+       mlt_image_format fmt = mlt_image_none;
+       mlt_properties p;
+       int w = profile->width;
+       int h = profile->height;
+
+       if ( ! mlt_service_get_frame( MLT_PRODUCER_SERVICE(producer), &fr, 0 ) && fr )
+       {
+               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(producer), &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" );
+                               if ( 1000 > mlt_properties_get_double( p, "meta.media.frame_rate_num" )
+                                         / mlt_properties_get_double( p, "meta.media.frame_rate_den" ) )
+                               {
+                                       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" );
+                               } else {
+                                       profile->frame_rate_num = 60;
+                                       profile->frame_rate_den = 1;
+                               }
+                               // 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 = lrint( (double) profile->sample_aspect_num * profile->width
+                                       / profile->sample_aspect_den );
+                               profile->display_aspect_den = profile->height;
+                               free( profile->description );
+                               profile->description = strdup( "automatic" );
+                               profile->is_explicit = 0;
+                       }
+               }
+       }
+       mlt_frame_close( fr );
+       mlt_producer_seek( producer, 0 );
+}