]> git.sesse.net Git - mlt/blob - src/framework/mlt_profile.c
mlt_profile.c, mlt_factory.c: bugfix loading profile by file specification and remove...
[mlt] / src / framework / mlt_profile.c
1 /*
2  * mlt_profile.c -- video output definition
3  * Copyright (C) 2007 Ushodaya Enterprises Limited
4  * Author: Dan Dennedy <dan@dennedy.org>
5  *
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.
10  *
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.
15  *
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
19  */
20
21 #include "mlt_profile.h"
22 #include "mlt_factory.h"
23 #include "mlt_properties.h"
24
25 #include <stdlib.h>
26 #include <string.h>
27 #include <libgen.h>
28
29 #define PROFILES_DIR "/share/mlt/profiles/"
30
31 static mlt_profile profile = NULL;
32
33 /** Get the current profile
34 * Builds one for PAL DV if non-existing
35 */
36
37 mlt_profile mlt_profile_get( )
38 {
39         if ( !profile )
40         {
41                 profile = calloc( 1, sizeof( struct mlt_profile_s ) );
42                 if ( profile )
43                 {
44                         mlt_environment_set( "MLT_PROFILE", "dv_pal" );
45                         profile->description = strdup( "PAL 4:3 DV or DVD" );
46                         profile->frame_rate_num = 25;
47                         profile->frame_rate_den = 1;
48                         profile->width = 720;
49                         profile->height = 576;
50                         profile->progressive = 0;
51                         profile->sample_aspect_num = 59;
52                         profile->sample_aspect_den = 54;
53                         profile->display_aspect_num = 4;
54                         profile->display_aspect_den = 3;
55                 }
56         }
57         return profile;
58 }
59
60
61 /** Load a profile from the system folder
62 */
63
64 mlt_profile mlt_profile_select( const char *name )
65 {
66         char *filename = NULL;
67         const char *prefix = getenv( "MLT_PROFILES_PATH" );
68         mlt_properties properties = mlt_properties_load( name );
69         
70         // Try to load from file specification
71         if ( properties && mlt_properties_get_int( properties, "width" ) )
72         {
73                 filename = calloc( 1, strlen( name ) + 1 );
74         }
75         // Load from $prefix/share/mlt/profiles
76         else if ( prefix == NULL )
77         {
78                 prefix = PREFIX;
79                 filename = calloc( 1, strlen( prefix ) + strlen( PROFILES_DIR ) + strlen( name ) + 2 );
80                 strcpy( filename, prefix );
81                 if ( filename[ strlen( filename ) - 1 ] != '/' )
82                         filename[ strlen( filename ) ] = '/';
83                 strcat( filename, PROFILES_DIR );
84         }
85         // Use environment variable instead
86         else
87         {
88                 filename = calloc( 1, strlen( prefix ) + strlen( name ) + 2 );
89                 strcpy( filename, prefix );
90                 if ( filename[ strlen( filename ) - 1 ] != '/' )
91                         filename[ strlen( filename ) ] = '/';
92         }
93         
94         // Finish loading
95         strcat( filename, name );
96         mlt_profile_load_file( filename );
97
98         // Cleanup
99         mlt_properties_close( properties );
100         free( filename );
101
102         return profile;
103 }
104
105 /** Load a profile from specific file
106 */
107
108 mlt_profile mlt_profile_load_file( const char *file )
109 {
110         // Load the profile as properties
111         mlt_properties properties = mlt_properties_load( file );
112         if ( properties && mlt_properties_get_int( properties, "width" ) )
113         {
114                 mlt_profile_load_properties( properties );
115                 mlt_properties_close( properties );
116
117                 // Set MLT_PROFILE to basename
118                 char *filename = strdup( file );
119                 mlt_environment_set( "MLT_PROFILE", basename( filename ) );
120                 free( filename );
121         }
122         else
123         {
124                 // Cleanup
125                 mlt_properties_close( properties );
126                 mlt_profile_close();
127                 // Failover
128                 mlt_profile_get();
129         }
130
131         // Set MLT_NORMALISATION to appease legacy modules
132         char *profile_name = mlt_environment( "MLT_PROFILE" );
133         if ( strstr( profile_name, "_ntsc" ) ||
134              strstr( profile_name, "_60" ) ||
135              strstr( profile_name, "_30" ) )
136         {
137                 mlt_environment_set( "MLT_NORMALISATION", "NTSC" );
138         }
139         else if ( strstr( profile_name, "_pal" ) ||
140                   strstr( profile_name, "_50" ) ||
141                   strstr( profile_name, "_25" ) )
142         {
143                 mlt_environment_set( "MLT_NORMALISATION", "PAL" );
144         }
145
146         return profile;
147 }
148
149 /** Load a profile from a properties object
150 */
151
152 mlt_profile mlt_profile_load_properties( mlt_properties properties )
153 {
154         mlt_profile_close();
155         profile = calloc( 1, sizeof( struct mlt_profile_s ) );
156         if ( profile )
157         {
158                 if ( mlt_properties_get( properties, "name" ) )
159                         mlt_environment_set( "MLT_PROFILE", mlt_properties_get( properties, "name" ) );
160                 if ( mlt_properties_get( properties, "description" ) )
161                         profile->description = strdup( mlt_properties_get( properties, "description" ) );
162                 profile->frame_rate_num = mlt_properties_get_int( properties, "frame_rate_num" );
163                 profile->frame_rate_den = mlt_properties_get_int( properties, "frame_rate_den" );
164                 profile->width = mlt_properties_get_int( properties, "width" );
165                 profile->height = mlt_properties_get_int( properties, "height" );
166                 profile->progressive = mlt_properties_get_int( properties, "progressive" );
167                 profile->sample_aspect_num = mlt_properties_get_int( properties, "sample_aspect_num" );
168                 profile->sample_aspect_den = mlt_properties_get_int( properties, "sample_aspect_den" );
169                 profile->display_aspect_num = mlt_properties_get_int( properties, "display_aspect_num" );
170                 profile->display_aspect_den = mlt_properties_get_int( properties, "display_aspect_den" );
171         }
172         return profile;
173 }
174
175 /** Load an anonymous profile from string
176 */
177
178 mlt_profile mlt_profile_load_string( const char *string )
179 {
180         mlt_properties properties = mlt_properties_new();
181         if ( properties )
182         {
183                 const char *p = string;
184                 while ( p )
185                 {
186                         if ( strcmp( p, "" ) && p[ 0 ] != '#' )
187                                 mlt_properties_parse( properties, p );
188                         p = strchr( p, '\n' );
189                         if ( p ) p++;
190                 }
191         }
192         return mlt_profile_load_properties( properties );
193 }
194
195 /** Get the framerate as float
196 */
197
198 double mlt_profile_fps( mlt_profile aprofile )
199 {
200         if ( aprofile )
201                 return ( double ) aprofile->frame_rate_num / aprofile->frame_rate_den;
202         else
203                 return ( double ) mlt_profile_get()->frame_rate_num / mlt_profile_get()->frame_rate_den;
204 }
205
206 /** Get the sample aspect ratio as float
207 */
208
209 double mlt_profile_sar( mlt_profile aprofile )
210 {
211         if ( aprofile )
212                 return ( double ) aprofile->sample_aspect_num / aprofile->sample_aspect_den;
213         else
214                 return ( double ) mlt_profile_get()->sample_aspect_num / mlt_profile_get()->sample_aspect_den;
215 }
216
217 /** Get the display aspect ratio as float
218 */
219
220 double mlt_profile_dar( mlt_profile aprofile )
221 {
222         if ( aprofile )
223                 return ( double ) aprofile->display_aspect_num / aprofile->display_aspect_den;
224         else
225                 return ( double ) mlt_profile_get()->display_aspect_num / mlt_profile_get()->display_aspect_den;
226 }
227
228 /** Free up the global profile resources
229 */
230
231 void mlt_profile_close( )
232 {
233         if ( profile )
234         {
235                 if ( profile->description )
236                         free( profile->description );
237                 profile->description = NULL;
238                 free( profile );
239                 profile = NULL;
240         }
241 }