]> git.sesse.net Git - mlt/blob - src/framework/mlt_profile.c
fix memory leak (coverity-709378)
[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 $datadir/mlt/profiles
57         else if ( prefix == NULL )
58         {
59                 prefix = mlt_environment( "MLT_DATA" );
60                 filename = calloc( 1, strlen( prefix ) + strlen( PROFILES_DIR ) + strlen( name ) + 1 );
61                 strcpy( filename, prefix );
62                 strcat( filename, PROFILES_DIR );
63         }
64         // Use environment variable instead
65         else
66         {
67                 filename = calloc( 1, strlen( prefix ) + strlen( name ) + 2 );
68                 strcpy( filename, prefix );
69                 if ( filename[ strlen( filename ) - 1 ] != '/' )
70                         filename[ strlen( filename ) ] = '/';
71         }
72
73         // Finish loading
74         strcat( filename, name );
75         profile = mlt_profile_load_file( filename );
76
77         // Cleanup
78         mlt_properties_close( properties );
79         free( filename );
80
81         return profile;
82 }
83
84 /** Construct a profile.
85  *
86  * This will never return NULL as it uses the dv_pal settings as hard-coded fallback default.
87  *
88  * \public \memberof mlt_profile_s
89  * \param name the name of a profile settings file located in the standard location or
90  * the full path name to a profile settings file
91  * \return a profile
92  */
93
94 mlt_profile mlt_profile_init( const char *name )
95 {
96         mlt_profile profile = NULL;
97
98         // Explicit profile by name gets priority over environment variables
99         if ( name )
100                 profile = mlt_profile_select( name );
101
102         // Try to load by environment variable
103         if ( profile == NULL )
104         {
105                 // MLT_PROFILE is preferred environment variable
106                 if ( getenv( "MLT_PROFILE" ) )
107                         profile = mlt_profile_select( getenv( "MLT_PROFILE" ) );
108                 // MLT_NORMALISATION backwards compatibility
109                 else if ( getenv( "MLT_NORMALISATION" ) && strcmp( getenv( "MLT_NORMALISATION" ), "PAL" ) )
110                         profile = mlt_profile_select( "dv_ntsc" );
111                 else
112                         profile = mlt_profile_select( "dv_pal" );
113
114                 // If still not loaded (no profile files), default to PAL
115                 if ( profile == NULL )
116                 {
117                         profile = calloc( 1, sizeof( struct mlt_profile_s ) );
118                         if ( profile )
119                         {
120                                 mlt_environment_set( "MLT_PROFILE", "dv_pal" );
121                                 profile->description = strdup( "PAL 4:3 DV or DVD" );
122                                 profile->frame_rate_num = 25;
123                                 profile->frame_rate_den = 1;
124                                 profile->width = 720;
125                                 profile->height = 576;
126                                 profile->progressive = 0;
127                                 profile->sample_aspect_num = 16;
128                                 profile->sample_aspect_den = 15;
129                                 profile->display_aspect_num = 4;
130                                 profile->display_aspect_den = 3;
131                                 profile->colorspace = 601;
132                         }
133                 }
134         }
135         return profile;
136 }
137
138 static void set_mlt_normalisation( const char *profile_name )
139 {
140         if ( profile_name )
141         {
142                 if ( strstr( profile_name, "_ntsc" ) ||
143                      strstr( profile_name, "_60" ) ||
144                      strstr( profile_name, "_5994" ) ||
145                      strstr( profile_name, "_2997" ) ||
146                      strstr( profile_name, "_30" ) )
147                 {
148                         mlt_environment_set( "MLT_NORMALISATION", "NTSC" );
149                 }
150                 else if ( strstr( profile_name, "_pal" ) ||
151                           strstr( profile_name, "_50" ) ||
152                           strstr( profile_name, "_25" ) )
153                 {
154                         mlt_environment_set( "MLT_NORMALISATION", "PAL" );
155                 }
156         }
157 }
158
159 /** Load a profile from specific file.
160  *
161  * \public \memberof mlt_profile_s
162  * \param file the full path name to a properties file
163  * \return a profile or NULL on error
164  */
165
166 mlt_profile mlt_profile_load_file( const char *file )
167 {
168         mlt_profile profile = NULL;
169
170         // Load the profile as properties
171         mlt_properties properties = mlt_properties_load( file );
172         if ( properties )
173         {
174                 // Simple check if the profile is valid
175                 if ( mlt_properties_get_int( properties, "width" ) )
176                 {
177                         profile = mlt_profile_load_properties( properties );
178
179                         // Set MLT_PROFILE to basename
180                         char *filename = strdup( file );
181                         mlt_environment_set( "MLT_PROFILE", basename( filename ) );
182                         set_mlt_normalisation( basename( filename ) );
183                         free( filename );
184                 }
185                 mlt_properties_close( properties );
186         }
187
188         // Set MLT_NORMALISATION to appease legacy modules
189         char *profile_name = mlt_environment( "MLT_PROFILE" );
190         set_mlt_normalisation( profile_name );
191         return profile;
192 }
193
194 /** Load a profile from a properties object.
195  *
196  * \public \memberof mlt_profile_s
197  * \param properties a properties list
198  * \return a profile or NULL if out of memory
199  */
200
201 mlt_profile mlt_profile_load_properties( mlt_properties properties )
202 {
203         mlt_profile profile = calloc( 1, sizeof( struct mlt_profile_s ) );
204         if ( profile )
205         {
206                 if ( mlt_properties_get( properties, "name" ) )
207                         mlt_environment_set( "MLT_PROFILE", mlt_properties_get( properties, "name" ) );
208                 if ( mlt_properties_get( properties, "description" ) )
209                         profile->description = strdup( mlt_properties_get( properties, "description" ) );
210                 profile->frame_rate_num = mlt_properties_get_int( properties, "frame_rate_num" );
211                 profile->frame_rate_den = mlt_properties_get_int( properties, "frame_rate_den" );
212                 profile->width = mlt_properties_get_int( properties, "width" );
213                 profile->height = mlt_properties_get_int( properties, "height" );
214                 profile->progressive = mlt_properties_get_int( properties, "progressive" );
215                 profile->sample_aspect_num = mlt_properties_get_int( properties, "sample_aspect_num" );
216                 profile->sample_aspect_den = mlt_properties_get_int( properties, "sample_aspect_den" );
217                 profile->display_aspect_num = mlt_properties_get_int( properties, "display_aspect_num" );
218                 profile->display_aspect_den = mlt_properties_get_int( properties, "display_aspect_den" );
219                 profile->colorspace = mlt_properties_get_int( properties, "colorspace" );
220         }
221         return profile;
222 }
223
224 /** Load an anonymous profile from string.
225  *
226  * \public \memberof mlt_profile_s
227  * \param string a newline-delimited list of properties as name=value pairs
228  * \return a profile or NULL if out of memory
229  */
230
231 mlt_profile mlt_profile_load_string( const char *string )
232 {
233         mlt_properties properties = mlt_properties_new();
234         mlt_profile profile = NULL;
235
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                 profile = mlt_profile_load_properties( properties );
247                 mlt_properties_close( properties );
248         }
249         return profile;
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 $datadir/mlt/profiles if no env var
359         if ( prefix == NULL )
360         {
361                 prefix = mlt_environment( "MLT_DATA" );
362                 filename = calloc( 1, strlen( prefix ) + strlen( PROFILES_DIR ) + 1 );
363                 strcpy( filename, prefix );
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                 if ( ! mlt_frame_get_image( fr, &buffer, &fmt, &w, &h, 0 ) )
414                 {
415                         // Some source properties are not exposed until after the first get_image call.
416                         mlt_frame_close( fr );
417                         mlt_service_get_frame( MLT_PRODUCER_SERVICE(producer), &fr, 0 );
418                         p = MLT_FRAME_PROPERTIES( fr );
419 //                      mlt_properties_dump(p, stderr);
420                         if ( mlt_properties_get_int( p, "meta.media.frame_rate_den" ) && mlt_properties_get_int( p, "meta.media.sample_aspect_den" ) )
421                         {
422                                 profile->width = mlt_properties_get_int( p, "meta.media.width" );
423                                 profile->height = mlt_properties_get_int( p, "meta.media.height" );
424                                 profile->progressive = mlt_properties_get_int( p, "meta.media.progressive" );
425                                 profile->frame_rate_num = mlt_properties_get_int( p, "meta.media.frame_rate_num" );
426                                 profile->frame_rate_den = mlt_properties_get_int( p, "meta.media.frame_rate_den" );
427                                 // AVCHD is mis-reported as double frame rate.
428                                 if ( profile->progressive == 0 && (
429                                      profile->frame_rate_num / profile->frame_rate_den == 50 ||
430                                      profile->frame_rate_num / profile->frame_rate_den == 59 ) )
431                                         profile->frame_rate_num /= 2;
432                                 profile->sample_aspect_num = mlt_properties_get_int( p, "meta.media.sample_aspect_num" );
433                                 profile->sample_aspect_den = mlt_properties_get_int( p, "meta.media.sample_aspect_den" );
434                                 profile->colorspace = mlt_properties_get_int( p, "meta.media.colorspace" );
435                                 profile->display_aspect_num = (int) ( (double) profile->sample_aspect_num * profile->width / profile->sample_aspect_den + 0.5 );
436                                 profile->display_aspect_den = profile->height;
437                                 free( profile->description );
438                                 profile->description = strdup( "automatic" );
439                                 profile->is_explicit = 0;
440                         }
441                 }
442         }
443         mlt_frame_close( fr );
444         mlt_producer_seek( producer, 0 );
445 }