]> git.sesse.net Git - mlt/blob - src/framework/mlt_profile.c
src/framework/*: improve the doxygen documentation (work in progress). This also...
[mlt] / src / framework / mlt_profile.c
1 /**
2  * \file mlt_profile.c
3  * \brief video output definition
4  *
5  * Copyright (C) 2007-2008 Ushodaya Enterprises Limited
6  * \author Dan Dennedy <dan@dennedy.org>
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21  */
22
23 #include "mlt_profile.h"
24 #include "mlt_factory.h"
25 #include "mlt_properties.h"
26
27 #include <stdlib.h>
28 #include <string.h>
29 #include <libgen.h>
30
31 #define PROFILES_DIR "/share/mlt/profiles/"
32
33 /** Load a profile from the system folder
34 */
35
36 static mlt_profile mlt_profile_select( const char *name )
37 {
38         char *filename = NULL;
39         const char *prefix = getenv( "MLT_PROFILES_PATH" );
40         mlt_properties properties = mlt_properties_load( name );
41         mlt_profile profile = NULL;
42
43         // Try to load from file specification
44         if ( properties && mlt_properties_get_int( properties, "width" ) )
45         {
46                 filename = calloc( 1, strlen( name ) + 1 );
47         }
48         // Load from $prefix/share/mlt/profiles
49         else if ( prefix == NULL )
50         {
51                 prefix = PREFIX;
52                 filename = calloc( 1, strlen( prefix ) + strlen( PROFILES_DIR ) + strlen( name ) + 2 );
53                 strcpy( filename, prefix );
54                 if ( filename[ strlen( filename ) - 1 ] != '/' )
55                         filename[ strlen( filename ) ] = '/';
56                 strcat( filename, PROFILES_DIR );
57         }
58         // Use environment variable instead
59         else
60         {
61                 filename = calloc( 1, strlen( prefix ) + strlen( name ) + 2 );
62                 strcpy( filename, prefix );
63                 if ( filename[ strlen( filename ) - 1 ] != '/' )
64                         filename[ strlen( filename ) ] = '/';
65         }
66
67         // Finish loading
68         strcat( filename, name );
69         profile = mlt_profile_load_file( filename );
70
71         // Cleanup
72         mlt_properties_close( properties );
73         free( filename );
74
75         return profile;
76 }
77
78 /** Construct a profile.
79 */
80
81 mlt_profile mlt_profile_init( const char *name )
82 {
83         mlt_profile profile = NULL;
84
85         // Explicit profile by name gets priority over environment variables
86         if ( name )
87                 profile = mlt_profile_select( name );
88
89         // Try to load by environment variable
90         if ( profile == NULL )
91         {
92                 // MLT_PROFILE is preferred environment variable
93                 if ( getenv( "MLT_PROFILE" ) )
94                         profile = mlt_profile_select( getenv( "MLT_PROFILE" ) );
95                 // MLT_NORMALISATION backwards compatibility
96                 else if ( getenv( "MLT_NORMALISATION" ) && strcmp( getenv( "MLT_NORMALISATION" ), "PAL" ) )
97                         profile = mlt_profile_select( "dv_ntsc" );
98                 else
99                         profile = mlt_profile_select( "dv_pal" );
100
101                 // If still not loaded (no profile files), default to PAL
102                 if ( profile == NULL )
103                 {
104                         profile = calloc( 1, sizeof( struct mlt_profile_s ) );
105                         if ( profile )
106                         {
107                                 mlt_environment_set( "MLT_PROFILE", "dv_pal" );
108                                 profile->description = strdup( "PAL 4:3 DV or DVD" );
109                                 profile->frame_rate_num = 25;
110                                 profile->frame_rate_den = 1;
111                                 profile->width = 720;
112                                 profile->height = 576;
113                                 profile->progressive = 0;
114                                 profile->sample_aspect_num = 16;
115                                 profile->sample_aspect_den = 15;
116                                 profile->display_aspect_num = 4;
117                                 profile->display_aspect_den = 3;
118                         }
119                 }
120         }
121         return profile;
122 }
123
124 /** Load a profile from specific file
125 */
126
127 mlt_profile mlt_profile_load_file( const char *file )
128 {
129         mlt_profile profile = NULL;
130
131         // Load the profile as properties
132         mlt_properties properties = mlt_properties_load( file );
133         if ( properties )
134         {
135                 // Simple check if the profile is valid
136                 if ( mlt_properties_get_int( properties, "width" ) )
137                 {
138                         profile = mlt_profile_load_properties( properties );
139
140                         // Set MLT_PROFILE to basename
141                         char *filename = strdup( file );
142                         mlt_environment_set( "MLT_PROFILE", basename( filename ) );
143                         free( filename );
144                 }
145                 mlt_properties_close( properties );
146         }
147
148         // Set MLT_NORMALISATION to appease legacy modules
149         char *profile_name = mlt_environment( "MLT_PROFILE" );
150         if ( profile_name )
151         {
152                 if ( strstr( profile_name, "_ntsc" ) ||
153                         strstr( profile_name, "_60" ) ||
154                         strstr( profile_name, "_30" ) )
155                 {
156                         mlt_environment_set( "MLT_NORMALISATION", "NTSC" );
157                 }
158                 else if ( strstr( profile_name, "_pal" ) ||
159                                 strstr( profile_name, "_50" ) ||
160                                 strstr( profile_name, "_25" ) )
161                 {
162                         mlt_environment_set( "MLT_NORMALISATION", "PAL" );
163                 }
164         }
165         return profile;
166 }
167
168 /** Load a profile from a properties object
169 */
170
171 mlt_profile mlt_profile_load_properties( mlt_properties properties )
172 {
173         mlt_profile profile = calloc( 1, sizeof( struct mlt_profile_s ) );
174         if ( profile )
175         {
176                 if ( mlt_properties_get( properties, "name" ) )
177                         mlt_environment_set( "MLT_PROFILE", mlt_properties_get( properties, "name" ) );
178                 if ( mlt_properties_get( properties, "description" ) )
179                         profile->description = strdup( mlt_properties_get( properties, "description" ) );
180                 profile->frame_rate_num = mlt_properties_get_int( properties, "frame_rate_num" );
181                 profile->frame_rate_den = mlt_properties_get_int( properties, "frame_rate_den" );
182                 profile->width = mlt_properties_get_int( properties, "width" );
183                 profile->height = mlt_properties_get_int( properties, "height" );
184                 profile->progressive = mlt_properties_get_int( properties, "progressive" );
185                 profile->sample_aspect_num = mlt_properties_get_int( properties, "sample_aspect_num" );
186                 profile->sample_aspect_den = mlt_properties_get_int( properties, "sample_aspect_den" );
187                 profile->display_aspect_num = mlt_properties_get_int( properties, "display_aspect_num" );
188                 profile->display_aspect_den = mlt_properties_get_int( properties, "display_aspect_den" );
189         }
190         return profile;
191 }
192
193 /** Load an anonymous profile from string
194 */
195
196 mlt_profile mlt_profile_load_string( const char *string )
197 {
198         mlt_properties properties = mlt_properties_new();
199         if ( properties )
200         {
201                 const char *p = string;
202                 while ( p )
203                 {
204                         if ( strcmp( p, "" ) && p[ 0 ] != '#' )
205                                 mlt_properties_parse( properties, p );
206                         p = strchr( p, '\n' );
207                         if ( p ) p++;
208                 }
209         }
210         return mlt_profile_load_properties( properties );
211 }
212
213 /** Get the framerate as float
214 */
215
216 double mlt_profile_fps( mlt_profile aprofile )
217 {
218         if ( aprofile )
219                 return ( double ) aprofile->frame_rate_num / aprofile->frame_rate_den;
220         else
221                 return 0;
222 }
223
224 /** Get the sample aspect ratio as float
225 */
226
227 double mlt_profile_sar( mlt_profile aprofile )
228 {
229         if ( aprofile )
230                 return ( double ) aprofile->sample_aspect_num / aprofile->sample_aspect_den;
231         else
232                 return 0;
233 }
234
235 /** Get the display aspect ratio as float
236 */
237
238 double mlt_profile_dar( mlt_profile aprofile )
239 {
240         if ( aprofile )
241                 return ( double ) aprofile->display_aspect_num / aprofile->display_aspect_den;
242         else
243                 return 0;
244 }
245
246 /** Free up the global profile resources
247 */
248
249 void mlt_profile_close( mlt_profile profile )
250 {
251         if ( profile )
252         {
253                 if ( profile->description )
254                         free( profile->description );
255                 profile->description = NULL;
256                 free( profile );
257                 profile = NULL;
258         }
259 }