]> git.sesse.net Git - mlt/blob - src/framework/mlt_profile.c
Add Mlt::Profile.list().
[mlt] / src / framework / mlt_profile.c
1 /**
2  * \file mlt_profile.c
3  * \brief video output definition
4  * \see mlt_profile_s
5  *
6  * Copyright (C) 2007-2009 Ushodaya Enterprises Limited
7  * \author Dan Dennedy <dan@dennedy.org>
8  *
9  * This library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 2.1 of the License, or (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this library; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
22  */
23
24 #include "mlt_profile.h"
25 #include "mlt_factory.h"
26 #include "mlt_properties.h"
27
28 #include <stdlib.h>
29 #include <string.h>
30 #include <libgen.h>
31
32
33 /** the default subdirectory of the prefix for holding profiles */
34 #define PROFILES_DIR "/share/mlt/profiles/"
35
36 /** Load a profile from the system folder.
37  *
38  * The environment variable MLT_PROFILES_PATH overrides the default \p PROFILES_DIR.
39  *
40  * \private \memberof mlt_profile_s
41  * \param name the name of a profile settings file located in the standard location or
42  * the full path name to a profile settings file
43  * \return a profile or NULL on error
44  */
45
46 static mlt_profile mlt_profile_select( const char *name )
47 {
48         char *filename = NULL;
49         const char *prefix = getenv( "MLT_PROFILES_PATH" );
50         mlt_properties properties = mlt_properties_load( name );
51         mlt_profile profile = NULL;
52
53         // Try to load from file specification
54         if ( properties && mlt_properties_get_int( properties, "width" ) )
55         {
56                 filename = calloc( 1, strlen( name ) + 1 );
57         }
58         // Load from $prefix/share/mlt/profiles
59         else if ( prefix == NULL )
60         {
61                 prefix = PREFIX;
62                 filename = calloc( 1, strlen( prefix ) + strlen( PROFILES_DIR ) + strlen( name ) + 2 );
63                 strcpy( filename, prefix );
64                 if ( filename[ strlen( filename ) - 1 ] != '/' )
65                         filename[ strlen( filename ) ] = '/';
66                 strcat( filename, PROFILES_DIR );
67         }
68         // Use environment variable instead
69         else
70         {
71                 filename = calloc( 1, strlen( prefix ) + strlen( name ) + 2 );
72                 strcpy( filename, prefix );
73                 if ( filename[ strlen( filename ) - 1 ] != '/' )
74                         filename[ strlen( filename ) ] = '/';
75         }
76
77         // Finish loading
78         strcat( filename, name );
79         profile = mlt_profile_load_file( filename );
80
81         // Cleanup
82         mlt_properties_close( properties );
83         free( filename );
84
85         return profile;
86 }
87
88 /** Construct a profile.
89  *
90  * This will never return NULL as it uses the dv_pal settings as hard-coded fallback default.
91  *
92  * \public \memberof mlt_profile_s
93  * \param name the name of a profile settings file located in the standard location or
94  * the full path name to a profile settings file
95  * \return a profile
96  */
97
98 mlt_profile mlt_profile_init( const char *name )
99 {
100         mlt_profile profile = NULL;
101
102         // Explicit profile by name gets priority over environment variables
103         if ( name )
104                 profile = mlt_profile_select( name );
105
106         // Try to load by environment variable
107         if ( profile == NULL )
108         {
109                 // MLT_PROFILE is preferred environment variable
110                 if ( getenv( "MLT_PROFILE" ) )
111                         profile = mlt_profile_select( getenv( "MLT_PROFILE" ) );
112                 // MLT_NORMALISATION backwards compatibility
113                 else if ( getenv( "MLT_NORMALISATION" ) && strcmp( getenv( "MLT_NORMALISATION" ), "PAL" ) )
114                         profile = mlt_profile_select( "dv_ntsc" );
115                 else
116                         profile = mlt_profile_select( "dv_pal" );
117
118                 // If still not loaded (no profile files), default to PAL
119                 if ( profile == NULL )
120                 {
121                         profile = calloc( 1, sizeof( struct mlt_profile_s ) );
122                         if ( profile )
123                         {
124                                 mlt_environment_set( "MLT_PROFILE", "dv_pal" );
125                                 profile->description = strdup( "PAL 4:3 DV or DVD" );
126                                 profile->frame_rate_num = 25;
127                                 profile->frame_rate_den = 1;
128                                 profile->width = 720;
129                                 profile->height = 576;
130                                 profile->progressive = 0;
131                                 profile->sample_aspect_num = 16;
132                                 profile->sample_aspect_den = 15;
133                                 profile->display_aspect_num = 4;
134                                 profile->display_aspect_den = 3;
135                                 profile->colorspace = 601;
136                         }
137                 }
138         }
139         return profile;
140 }
141
142 static void set_mlt_normalisation( const char *profile_name )
143 {
144         if ( profile_name )
145         {
146                 if ( strstr( profile_name, "_ntsc" ) ||
147                      strstr( profile_name, "_60" ) ||
148                      strstr( profile_name, "_5994" ) ||
149                      strstr( profile_name, "_2997" ) ||
150                      strstr( profile_name, "_30" ) )
151                 {
152                         mlt_environment_set( "MLT_NORMALISATION", "NTSC" );
153                 }
154                 else if ( strstr( profile_name, "_pal" ) ||
155                           strstr( profile_name, "_50" ) ||
156                           strstr( profile_name, "_25" ) )
157                 {
158                         mlt_environment_set( "MLT_NORMALISATION", "PAL" );
159                 }
160         }
161 }
162
163 /** Load a profile from specific file.
164  *
165  * \public \memberof mlt_profile_s
166  * \param file the full path name to a properties file
167  * \return a profile or NULL on error
168  */
169
170 mlt_profile mlt_profile_load_file( const char *file )
171 {
172         mlt_profile profile = NULL;
173
174         // Load the profile as properties
175         mlt_properties properties = mlt_properties_load( file );
176         if ( properties )
177         {
178                 // Simple check if the profile is valid
179                 if ( mlt_properties_get_int( properties, "width" ) )
180                 {
181                         profile = mlt_profile_load_properties( properties );
182
183                         // Set MLT_PROFILE to basename
184                         char *filename = strdup( file );
185                         mlt_environment_set( "MLT_PROFILE", basename( filename ) );
186                         set_mlt_normalisation( basename( filename ) );
187                         free( filename );
188                 }
189                 mlt_properties_close( properties );
190         }
191
192         // Set MLT_NORMALISATION to appease legacy modules
193         char *profile_name = mlt_environment( "MLT_PROFILE" );
194         set_mlt_normalisation( profile_name );
195         return profile;
196 }
197
198 /** Load a profile from a properties object.
199  *
200  * \public \memberof mlt_profile_s
201  * \param properties a properties list
202  * \return a profile or NULL if out of memory
203  */
204
205 mlt_profile mlt_profile_load_properties( mlt_properties properties )
206 {
207         mlt_profile profile = calloc( 1, sizeof( struct mlt_profile_s ) );
208         if ( profile )
209         {
210                 if ( mlt_properties_get( properties, "name" ) )
211                         mlt_environment_set( "MLT_PROFILE", mlt_properties_get( properties, "name" ) );
212                 if ( mlt_properties_get( properties, "description" ) )
213                         profile->description = strdup( mlt_properties_get( properties, "description" ) );
214                 profile->frame_rate_num = mlt_properties_get_int( properties, "frame_rate_num" );
215                 profile->frame_rate_den = mlt_properties_get_int( properties, "frame_rate_den" );
216                 profile->width = mlt_properties_get_int( properties, "width" );
217                 profile->height = mlt_properties_get_int( properties, "height" );
218                 profile->progressive = mlt_properties_get_int( properties, "progressive" );
219                 profile->sample_aspect_num = mlt_properties_get_int( properties, "sample_aspect_num" );
220                 profile->sample_aspect_den = mlt_properties_get_int( properties, "sample_aspect_den" );
221                 profile->display_aspect_num = mlt_properties_get_int( properties, "display_aspect_num" );
222                 profile->display_aspect_den = mlt_properties_get_int( properties, "display_aspect_den" );
223                 profile->colorspace = mlt_properties_get_int( properties, "colorspace" );
224         }
225         return profile;
226 }
227
228 /** Load an anonymous profile from string.
229  *
230  * \public \memberof mlt_profile_s
231  * \param string a newline-delimited list of properties as name=value pairs
232  * \return a profile or NULL if out of memory
233  */
234
235 mlt_profile mlt_profile_load_string( const char *string )
236 {
237         mlt_properties properties = mlt_properties_new();
238         if ( properties )
239         {
240                 const char *p = string;
241                 while ( p )
242                 {
243                         if ( strcmp( p, "" ) && p[ 0 ] != '#' )
244                                 mlt_properties_parse( properties, p );
245                         p = strchr( p, '\n' );
246                         if ( p ) p++;
247                 }
248         }
249         return mlt_profile_load_properties( properties );
250 }
251
252 /** Get the video frame rate as a floating point value.
253  *
254  * \public \memberof mlt_profile_s
255  * @param profile a profile
256  * @return the frame rate
257  */
258
259 double mlt_profile_fps( mlt_profile profile )
260 {
261         if ( profile )
262                 return ( double ) profile->frame_rate_num / profile->frame_rate_den;
263         else
264                 return 0;
265 }
266
267 /** Get the sample aspect ratio as a floating point value.
268  *
269  * \public \memberof mlt_profile_s
270  * \param profile a profile
271  * \return the pixel aspect ratio
272  */
273
274 double mlt_profile_sar( mlt_profile profile )
275 {
276         if ( profile )
277                 return ( double ) profile->sample_aspect_num / profile->sample_aspect_den;
278         else
279                 return 0;
280 }
281
282 /** Get the display aspect ratio as floating point value.
283  *
284  * \public \memberof mlt_profile_s
285  * \param profile a profile
286  * \return the image aspect ratio
287  */
288
289 double mlt_profile_dar( mlt_profile profile )
290 {
291         if ( profile )
292                 return ( double ) profile->display_aspect_num / profile->display_aspect_den;
293         else
294                 return 0;
295 }
296
297 /** Free up the global profile resources.
298  *
299  * \public \memberof mlt_profile_s
300  * \param profile a profile
301  */
302
303 void mlt_profile_close( mlt_profile profile )
304 {
305         if ( profile )
306         {
307                 if ( profile->description )
308                         free( profile->description );
309                 profile->description = NULL;
310                 free( profile );
311                 profile = NULL;
312         }
313 }
314
315 /** Make a copy of a profile.
316   *
317   * \public \memberof mlt_profile_s
318   * \param profile the profile to clone
319   * \return a copy of the profile
320   */
321
322 mlt_profile mlt_profile_clone( mlt_profile profile )
323 {
324         mlt_profile clone = NULL;
325
326         if ( profile )
327         {
328                 clone = calloc( 1, sizeof( *profile ) );
329                 if ( clone )
330                 {
331                         memcpy( clone, profile, sizeof( *profile ) );
332                         clone->description = strdup( profile->description );
333                 }
334         }
335         return clone;
336 }
337
338
339 /** Get the list of profiles.
340  *
341  * The caller MUST close the returned properties object!
342  * Each entry in the list is keyed on its name, and its value is another
343  * properties object that contains the attributes of the profile.
344  * \public \memberof mlt_profile_s
345  * \return a list of profiles
346  */
347
348 mlt_properties mlt_profile_list( )
349 {
350         char *filename = NULL;
351         const char *prefix = getenv( "MLT_PROFILES_PATH" );
352         mlt_properties properties = mlt_properties_new();
353         mlt_properties dir = mlt_properties_new();
354         int sort = 1;
355         const char *wildcard = NULL;
356         int i;
357
358         // Load from $prefix/share/mlt/profiles if no env var
359         if ( prefix == NULL )
360         {
361                 prefix = PREFIX;
362                 filename = calloc( 1, strlen( prefix ) + strlen( PROFILES_DIR ) + 2 );
363                 strcpy( filename, prefix );
364                 if ( filename[ strlen( filename ) - 1 ] != '/' )
365                         filename[ strlen( filename ) ] = '/';
366                 strcat( filename, PROFILES_DIR );
367                 prefix = filename;
368         }
369
370         mlt_properties_dir_list( dir, prefix, wildcard, sort );
371
372         for ( i = 0; i < mlt_properties_count( dir ); i++ )
373         {
374                 char *filename = mlt_properties_get_value( dir, i );
375                 char *profile_name = basename( filename );
376                 if ( profile_name[0] != '.' && strcmp( profile_name, "Makefile" ) &&
377                      profile_name[ strlen( profile_name ) - 1 ] != '~' )
378                 {
379                         mlt_properties profile = mlt_properties_load( filename );
380                         if ( profile )
381                         {
382                                 mlt_properties_set_data( properties, profile_name, profile, 0,
383                                         (mlt_destructor) mlt_properties_close, NULL );
384                         }
385                 }
386         }
387         mlt_properties_close( dir );
388         if ( filename )
389                 free( filename );
390
391         return properties;
392 }