2 * mlt_profile.c -- video output definition
3 * Copyright (C) 2007 Ushodaya Enterprises Limited
4 * Author: Dan Dennedy <dan@dennedy.org>
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #include "mlt_profile.h"
22 #include "mlt_factory.h"
23 #include "mlt_properties.h"
29 #define PROFILES_DIR "/share/mlt/profiles/"
31 /** Load a profile from the system folder
34 static mlt_profile mlt_profile_select( const char *name )
36 char *filename = NULL;
37 const char *prefix = getenv( "MLT_PROFILES_PATH" );
38 mlt_properties properties = mlt_properties_load( name );
39 mlt_profile profile = NULL;
41 // Try to load from file specification
42 if ( properties && mlt_properties_get_int( properties, "width" ) )
44 filename = calloc( 1, strlen( name ) + 1 );
46 // Load from $prefix/share/mlt/profiles
47 else if ( prefix == NULL )
50 filename = calloc( 1, strlen( prefix ) + strlen( PROFILES_DIR ) + strlen( name ) + 2 );
51 strcpy( filename, prefix );
52 if ( filename[ strlen( filename ) - 1 ] != '/' )
53 filename[ strlen( filename ) ] = '/';
54 strcat( filename, PROFILES_DIR );
56 // Use environment variable instead
59 filename = calloc( 1, strlen( prefix ) + strlen( name ) + 2 );
60 strcpy( filename, prefix );
61 if ( filename[ strlen( filename ) - 1 ] != '/' )
62 filename[ strlen( filename ) ] = '/';
66 strcat( filename, name );
67 profile = mlt_profile_load_file( filename );
70 mlt_properties_close( properties );
76 /** Construct a profile.
79 mlt_profile mlt_profile_init( const char *name )
81 mlt_profile profile = NULL;
83 // Explicit profile by name gets priority over environment variables
85 profile = mlt_profile_select( name );
87 // Try to load by environment variable
88 if ( profile == NULL )
90 // MLT_PROFILE is preferred environment variable
91 if ( getenv( "MLT_PROFILE" ) )
92 profile = mlt_profile_select( getenv( "MLT_PROFILE" ) );
93 // MLT_NORMALISATION backwards compatibility
94 else if ( getenv( "MLT_NORMALISATION" ) && strcmp( getenv( "MLT_NORMALISATION" ), "PAL" ) )
95 profile = mlt_profile_select( "dv_ntsc" );
97 profile = mlt_profile_select( "dv_pal" );
99 // If still not loaded (no profile files), default to PAL
100 if ( profile == NULL )
102 profile = calloc( 1, sizeof( struct mlt_profile_s ) );
105 mlt_environment_set( "MLT_PROFILE", "dv_pal" );
106 profile->description = strdup( "PAL 4:3 DV or DVD" );
107 profile->frame_rate_num = 25;
108 profile->frame_rate_den = 1;
109 profile->width = 720;
110 profile->height = 576;
111 profile->progressive = 0;
112 profile->sample_aspect_num = 59;
113 profile->sample_aspect_den = 54;
114 profile->display_aspect_num = 4;
115 profile->display_aspect_den = 3;
122 /** Load a profile from specific file
125 mlt_profile mlt_profile_load_file( const char *file )
127 mlt_profile profile = NULL;
129 // Load the profile as properties
130 mlt_properties properties = mlt_properties_load( file );
133 // Simple check if the profile is valid
134 if ( mlt_properties_get_int( properties, "width" ) )
136 profile = mlt_profile_load_properties( properties );
138 // Set MLT_PROFILE to basename
139 char *filename = strdup( file );
140 mlt_environment_set( "MLT_PROFILE", basename( filename ) );
143 mlt_properties_close( properties );
146 // Set MLT_NORMALISATION to appease legacy modules
147 char *profile_name = mlt_environment( "MLT_PROFILE" );
150 if ( strstr( profile_name, "_ntsc" ) ||
151 strstr( profile_name, "_60" ) ||
152 strstr( profile_name, "_30" ) )
154 mlt_environment_set( "MLT_NORMALISATION", "NTSC" );
156 else if ( strstr( profile_name, "_pal" ) ||
157 strstr( profile_name, "_50" ) ||
158 strstr( profile_name, "_25" ) )
160 mlt_environment_set( "MLT_NORMALISATION", "PAL" );
166 /** Load a profile from a properties object
169 mlt_profile mlt_profile_load_properties( mlt_properties properties )
171 mlt_profile profile = calloc( 1, sizeof( struct mlt_profile_s ) );
174 if ( mlt_properties_get( properties, "name" ) )
175 mlt_environment_set( "MLT_PROFILE", mlt_properties_get( properties, "name" ) );
176 if ( mlt_properties_get( properties, "description" ) )
177 profile->description = strdup( mlt_properties_get( properties, "description" ) );
178 profile->frame_rate_num = mlt_properties_get_int( properties, "frame_rate_num" );
179 profile->frame_rate_den = mlt_properties_get_int( properties, "frame_rate_den" );
180 profile->width = mlt_properties_get_int( properties, "width" );
181 profile->height = mlt_properties_get_int( properties, "height" );
182 profile->progressive = mlt_properties_get_int( properties, "progressive" );
183 profile->sample_aspect_num = mlt_properties_get_int( properties, "sample_aspect_num" );
184 profile->sample_aspect_den = mlt_properties_get_int( properties, "sample_aspect_den" );
185 profile->display_aspect_num = mlt_properties_get_int( properties, "display_aspect_num" );
186 profile->display_aspect_den = mlt_properties_get_int( properties, "display_aspect_den" );
191 /** Load an anonymous profile from string
194 mlt_profile mlt_profile_load_string( const char *string )
196 mlt_properties properties = mlt_properties_new();
199 const char *p = string;
202 if ( strcmp( p, "" ) && p[ 0 ] != '#' )
203 mlt_properties_parse( properties, p );
204 p = strchr( p, '\n' );
208 return mlt_profile_load_properties( properties );
211 /** Get the framerate as float
214 double mlt_profile_fps( mlt_profile aprofile )
217 return ( double ) aprofile->frame_rate_num / aprofile->frame_rate_den;
222 /** Get the sample aspect ratio as float
225 double mlt_profile_sar( mlt_profile aprofile )
228 return ( double ) aprofile->sample_aspect_num / aprofile->sample_aspect_den;
233 /** Get the display aspect ratio as float
236 double mlt_profile_dar( mlt_profile aprofile )
239 return ( double ) aprofile->display_aspect_num / aprofile->display_aspect_den;
244 /** Free up the global profile resources
247 void mlt_profile_close( mlt_profile profile )
251 if ( profile->description )
252 free( profile->description );
253 profile->description = NULL;