]> git.sesse.net Git - mlt/blob - src/framework/mlt_profile.c
Make profiles relative to MLT_DATA instead of $prefix/share/mlt
[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.h"
25
26 #include <stdlib.h>
27 #include <string.h>
28 #include <libgen.h>
29
30
31 /** the default subdirectory of the datadir for holding profiles */
32 #define PROFILES_DIR "/profiles"
33
34 /** Load a profile from the system folder.
35  *
36  * The environment variable MLT_PROFILES_PATH overrides the default \p PROFILES_DIR.
37  *
38  * \private \memberof mlt_profile_s
39  * \param name the name of a profile settings file located in the standard location or
40  * the full path name to a profile settings file
41  * \return a profile or NULL on error
42  */
43
44 static mlt_profile mlt_profile_select( const char *name )
45 {
46         char *filename = NULL;
47         const char *prefix = getenv( "MLT_PROFILES_PATH" );
48         mlt_properties properties = mlt_properties_load( name );
49         mlt_profile profile = NULL;
50
51         // Try to load from file specification
52         if ( properties && mlt_properties_get_int( properties, "width" ) )
53         {
54                 filename = calloc( 1, strlen( name ) + 1 );
55         }
56         // Load from $prefix/share/mlt/profiles
57         else if ( prefix == NULL )
58         {
59                 prefix = PREFIX;
60                 filename = calloc( 1, strlen( prefix ) + strlen( PROFILES_DIR ) + strlen( name ) + 2 );
61                 strcpy( filename, prefix );
62                 if ( filename[ strlen( filename ) - 1 ] != '/' )
63                         filename[ strlen( filename ) ] = '/';
64                 strcat( filename, PROFILES_DIR );
65         }
66         // Use environment variable instead
67         else
68         {
69                 filename = calloc( 1, strlen( prefix ) + strlen( name ) + 2 );
70                 strcpy( filename, prefix );
71                 if ( filename[ strlen( filename ) - 1 ] != '/' )
72                         filename[ strlen( filename ) ] = '/';
73         }
74
75         // Finish loading
76         strcat( filename, name );
77         profile = mlt_profile_load_file( filename );
78
79         // Cleanup
80         mlt_properties_close( properties );
81         free( filename );
82
83         return profile;
84 }
85
86 /** Construct a profile.
87  *
88  * This will never return NULL as it uses the dv_pal settings as hard-coded fallback default.
89  *
90  * \public \memberof mlt_profile_s
91  * \param name the name of a profile settings file located in the standard location or
92  * the full path name to a profile settings file
93  * \return a profile
94  */
95
96 mlt_profile mlt_profile_init( const char *name )
97 {
98         mlt_profile profile = NULL;
99
100         // Explicit profile by name gets priority over environment variables
101         if ( name )
102                 profile = mlt_profile_select( name );
103
104         // Try to load by environment variable
105         if ( profile == NULL )
106         {
107                 // MLT_PROFILE is preferred environment variable
108                 if ( getenv( "MLT_PROFILE" ) )
109                         profile = mlt_profile_select( getenv( "MLT_PROFILE" ) );
110                 // MLT_NORMALISATION backwards compatibility
111                 else if ( getenv( "MLT_NORMALISATION" ) && strcmp( getenv( "MLT_NORMALISATION" ), "PAL" ) )
112                         profile = mlt_profile_select( "dv_ntsc" );
113                 else
114                         profile = mlt_profile_select( "dv_pal" );
115
116                 // If still not loaded (no profile files), default to PAL
117                 if ( profile == NULL )
118                 {
119                         profile = calloc( 1, sizeof( struct mlt_profile_s ) );
120                         if ( profile )
121                         {
122                                 mlt_environment_set( "MLT_PROFILE", "dv_pal" );
123                                 profile->description = strdup( "PAL 4:3 DV or DVD" );
124                                 profile->frame_rate_num = 25;
125                                 profile->frame_rate_den = 1;
126                                 profile->width = 720;
127                                 profile->height = 576;
128                                 profile->progressive = 0;
129                                 profile->sample_aspect_num = 16;
130                                 profile->sample_aspect_den = 15;
131                                 profile->display_aspect_num = 4;
132                                 profile->display_aspect_den = 3;
133                                 profile->colorspace = 601;
134                         }
135                 }
136         }
137         return profile;
138 }
139
140 static void set_mlt_normalisation( const char *profile_name )
141 {
142         if ( profile_name )
143         {
144                 if ( strstr( profile_name, "_ntsc" ) ||
145                      strstr( profile_name, "_60" ) ||
146                      strstr( profile_name, "_5994" ) ||
147                      strstr( profile_name, "_2997" ) ||
148                      strstr( profile_name, "_30" ) )
149                 {
150                         mlt_environment_set( "MLT_NORMALISATION", "NTSC" );
151                 }
152                 else if ( strstr( profile_name, "_pal" ) ||
153                           strstr( profile_name, "_50" ) ||
154                           strstr( profile_name, "_25" ) )
155                 {
156                         mlt_environment_set( "MLT_NORMALISATION", "PAL" );
157                 }
158         }
159 }
160
161 /** Load a profile from specific file.
162  *
163  * \public \memberof mlt_profile_s
164  * \param file the full path name to a properties file
165  * \return a profile or NULL on error
166  */
167
168 mlt_profile mlt_profile_load_file( const char *file )
169 {
170         mlt_profile profile = NULL;
171
172         // Load the profile as properties
173         mlt_properties properties = mlt_properties_load( file );
174         if ( properties )
175         {
176                 // Simple check if the profile is valid
177                 if ( mlt_properties_get_int( properties, "width" ) )
178                 {
179                         profile = mlt_profile_load_properties( properties );
180
181                         // Set MLT_PROFILE to basename
182                         char *filename = strdup( file );
183                         mlt_environment_set( "MLT_PROFILE", basename( filename ) );
184                         set_mlt_normalisation( basename( filename ) );
185                         free( filename );
186                 }
187                 mlt_properties_close( properties );
188         }
189
190         // Set MLT_NORMALISATION to appease legacy modules
191         char *profile_name = mlt_environment( "MLT_PROFILE" );
192         set_mlt_normalisation( profile_name );
193         return profile;
194 }
195
196 /** Load a profile from a properties object.
197  *
198  * \public \memberof mlt_profile_s
199  * \param properties a properties list
200  * \return a profile or NULL if out of memory
201  */
202
203 mlt_profile mlt_profile_load_properties( mlt_properties properties )
204 {
205         mlt_profile profile = calloc( 1, sizeof( struct mlt_profile_s ) );
206         if ( profile )
207         {
208                 if ( mlt_properties_get( properties, "name" ) )
209                         mlt_environment_set( "MLT_PROFILE", mlt_properties_get( properties, "name" ) );
210                 if ( mlt_properties_get( properties, "description" ) )
211                         profile->description = strdup( mlt_properties_get( properties, "description" ) );
212                 profile->frame_rate_num = mlt_properties_get_int( properties, "frame_rate_num" );
213                 profile->frame_rate_den = mlt_properties_get_int( properties, "frame_rate_den" );
214                 profile->width = mlt_properties_get_int( properties, "width" );
215                 profile->height = mlt_properties_get_int( properties, "height" );
216                 profile->progressive = mlt_properties_get_int( properties, "progressive" );
217                 profile->sample_aspect_num = mlt_properties_get_int( properties, "sample_aspect_num" );
218                 profile->sample_aspect_den = mlt_properties_get_int( properties, "sample_aspect_den" );
219                 profile->display_aspect_num = mlt_properties_get_int( properties, "display_aspect_num" );
220                 profile->display_aspect_den = mlt_properties_get_int( properties, "display_aspect_den" );
221                 profile->colorspace = mlt_properties_get_int( properties, "colorspace" );
222         }
223         return profile;
224 }
225
226 /** Load an anonymous profile from string.
227  *
228  * \public \memberof mlt_profile_s
229  * \param string a newline-delimited list of properties as name=value pairs
230  * \return a profile or NULL if out of memory
231  */
232
233 mlt_profile mlt_profile_load_string( const char *string )
234 {
235         mlt_properties properties = mlt_properties_new();
236         if ( properties )
237         {
238                 const char *p = string;
239                 while ( p )
240                 {
241                         if ( strcmp( p, "" ) && p[ 0 ] != '#' )
242                                 mlt_properties_parse( properties, p );
243                         p = strchr( p, '\n' );
244                         if ( p ) p++;
245                 }
246         }
247         return mlt_profile_load_properties( properties );
248 }
249
250 /** Get the video frame rate as a floating point value.
251  *
252  * \public \memberof mlt_profile_s
253  * @param profile a profile
254  * @return the frame rate
255  */
256
257 double mlt_profile_fps( mlt_profile profile )
258 {
259         if ( profile )
260                 return ( double ) profile->frame_rate_num / profile->frame_rate_den;
261         else
262                 return 0;
263 }
264
265 /** Get the sample aspect ratio as a floating point value.
266  *
267  * \public \memberof mlt_profile_s
268  * \param profile a profile
269  * \return the pixel aspect ratio
270  */
271
272 double mlt_profile_sar( mlt_profile profile )
273 {
274         if ( profile )
275                 return ( double ) profile->sample_aspect_num / profile->sample_aspect_den;
276         else
277                 return 0;
278 }
279
280 /** Get the display aspect ratio as floating point value.
281  *
282  * \public \memberof mlt_profile_s
283  * \param profile a profile
284  * \return the image aspect ratio
285  */
286
287 double mlt_profile_dar( mlt_profile profile )
288 {
289         if ( profile )
290                 return ( double ) profile->display_aspect_num / profile->display_aspect_den;
291         else
292                 return 0;
293 }
294
295 /** Free up the global profile resources.
296  *
297  * \public \memberof mlt_profile_s
298  * \param profile a profile
299  */
300
301 void mlt_profile_close( mlt_profile profile )
302 {
303         if ( profile )
304         {
305                 if ( profile->description )
306                         free( profile->description );
307                 profile->description = NULL;
308                 free( profile );
309                 profile = NULL;
310         }
311 }
312
313 /** Make a copy of a profile.
314   *
315   * \public \memberof mlt_profile_s
316   * \param profile the profile to clone
317   * \return a copy of the profile
318   */
319
320 mlt_profile mlt_profile_clone( mlt_profile profile )
321 {
322         mlt_profile clone = NULL;
323
324         if ( profile )
325         {
326                 clone = calloc( 1, sizeof( *profile ) );
327                 if ( clone )
328                 {
329                         memcpy( clone, profile, sizeof( *profile ) );
330                         clone->description = strdup( profile->description );
331                 }
332         }
333         return clone;
334 }
335
336
337 /** Get the list of profiles.
338  *
339  * The caller MUST close the returned properties object!
340  * Each entry in the list is keyed on its name, and its value is another
341  * properties object that contains the attributes of the profile.
342  * \public \memberof mlt_profile_s
343  * \return a list of profiles
344  */
345
346 mlt_properties mlt_profile_list( )
347 {
348         char *filename = NULL;
349         const char *prefix = getenv( "MLT_PROFILES_PATH" );
350         mlt_properties properties = mlt_properties_new();
351         mlt_properties dir = mlt_properties_new();
352         int sort = 1;
353         const char *wildcard = NULL;
354         int i;
355
356         // Load from $prefix/share/mlt/profiles if no env var
357         if ( prefix == NULL )
358         {
359                 prefix = mlt_environment( "MLT_DATA" );
360                 filename = calloc( 1, strlen( prefix ) + strlen( PROFILES_DIR ) + 2 );
361                 strcpy( filename, prefix );
362                 if ( filename[ strlen( filename ) - 1 ] != '/' )
363                         filename[ strlen( filename ) ] = '/';
364                 strcat( filename, PROFILES_DIR );
365                 prefix = filename;
366         }
367
368         mlt_properties_dir_list( dir, prefix, wildcard, sort );
369
370         for ( i = 0; i < mlt_properties_count( dir ); i++ )
371         {
372                 char *filename = mlt_properties_get_value( dir, i );
373                 char *profile_name = basename( filename );
374                 if ( profile_name[0] != '.' && strcmp( profile_name, "Makefile" ) &&
375                      profile_name[ strlen( profile_name ) - 1 ] != '~' )
376                 {
377                         mlt_properties profile = mlt_properties_load( filename );
378                         if ( profile )
379                         {
380                                 mlt_properties_set_data( properties, profile_name, profile, 0,
381                                         (mlt_destructor) mlt_properties_close, NULL );
382                         }
383                 }
384         }
385         mlt_properties_close( dir );
386         if ( filename )
387                 free( filename );
388
389         return properties;
390 }
391
392 /** Update the profile using the attributes of a producer.
393  *
394  * Use this to make an "auto-profile." Typically, you need to re-open the producer
395  * after you use this because some producers (e.g. avformat) adjust their framerate
396  * to that of the profile used when you created it.
397  * \public \memberof mlt_profile_s
398  * \param profile the profile to update
399  * \param producer the producer to inspect
400  */
401
402 void mlt_profile_from_producer( mlt_profile profile, mlt_producer producer )
403 {
404         mlt_frame fr = NULL;
405         uint8_t *buffer;
406         mlt_image_format fmt = mlt_image_yuv422;
407         mlt_properties p;
408         int w = profile->width;
409         int h = profile->height;
410
411         if ( ! mlt_service_get_frame( MLT_PRODUCER_SERVICE(producer), &fr, 0 ) && fr )
412         {
413                 mlt_properties_set_double( MLT_FRAME_PROPERTIES( fr ), "consumer_aspect_ratio", mlt_profile_sar( profile ) );
414                 if ( ! mlt_frame_get_image( fr, &buffer, &fmt, &w, &h, 0 ) )
415                 {
416                         // Some source properties are not exposed until after the first get_image call.
417                         mlt_frame_close( fr );
418                         mlt_service_get_frame( MLT_PRODUCER_SERVICE(producer), &fr, 0 );
419                         p = MLT_FRAME_PROPERTIES( fr );
420 //                      mlt_properties_dump(p, stderr);
421                         if ( mlt_properties_get_int( p, "meta.media.frame_rate_den" ) && mlt_properties_get_int( p, "meta.media.sample_aspect_den" ) )
422                         {
423                                 profile->width = mlt_properties_get_int( p, "meta.media.width" );
424                                 profile->height = mlt_properties_get_int( p, "meta.media.height" );
425                                 profile->progressive = mlt_properties_get_int( p, "meta.media.progressive" );
426                                 profile->frame_rate_num = mlt_properties_get_int( p, "meta.media.frame_rate_num" );
427                                 profile->frame_rate_den = mlt_properties_get_int( p, "meta.media.frame_rate_den" );
428                                 // AVCHD is mis-reported as double frame rate.
429                                 if ( profile->progressive == 0 && (
430                                      profile->frame_rate_num / profile->frame_rate_den == 50 ||
431                                      profile->frame_rate_num / profile->frame_rate_den == 59 ) )
432                                         profile->frame_rate_num /= 2;
433                                 profile->sample_aspect_num = mlt_properties_get_int( p, "meta.media.sample_aspect_num" );
434                                 profile->sample_aspect_den = mlt_properties_get_int( p, "meta.media.sample_aspect_den" );
435                                 profile->colorspace = mlt_properties_get_int( p, "meta.media.colorspace" );
436                                 profile->display_aspect_num = (int) ( (double) profile->sample_aspect_num * profile->width / profile->sample_aspect_den + 0.5 );
437                                 profile->display_aspect_den = profile->height;
438                                 free( profile->description );
439                                 profile->description = strdup( "automatic" );
440                                 profile->is_explicit = 0;
441                         }
442                 }
443         }
444         mlt_frame_close( fr );
445         mlt_producer_seek( producer, 0 );
446 }