mlt_profle.c: add support for MLT_PROFILES_DIR environment variable
[melted] / 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
69 // Allow environment to override default behavior
70 if ( prefix == NULL )
71 {
72 // default behavior is to use $prefix/share/mlt/profiles
73 prefix = PREFIX;
74 filename = calloc( 1, strlen( prefix ) + strlen( PROFILES_DIR ) + strlen( name ) + 2 );
75 strcpy( filename, prefix );
76 if ( filename[ strlen( filename ) - 1 ] != '/' )
77 filename[ strlen( filename ) ] = '/';
78 strcat( filename, PROFILES_DIR );
79 }
80 else
81 {
82 // just use environment variable
83 filename = calloc( 1, strlen( prefix ) + strlen( name ) + 2 );
84 strcpy( filename, prefix );
85 if ( filename[ strlen( filename ) - 1 ] != '/' )
86 filename[ strlen( filename ) ] = '/';
87 }
88 strcat( filename, name );
89 return mlt_profile_load_file( filename );
90 }
91
92 /** Load a profile from specific file
93 */
94
95 mlt_profile mlt_profile_load_file( const char *file )
96 {
97 // Load the profile as properties
98 mlt_properties properties = mlt_properties_load( file );
99 if ( properties && mlt_properties_get_int( properties, "width" ) )
100 {
101 mlt_profile_load_properties( properties );
102 mlt_properties_close( properties );
103
104 // Set MLT_PROFILE to basename
105 char *filename = strdup( file );
106 mlt_environment_set( "MLT_PROFILE", basename( filename ) );
107 free( filename );
108 }
109 else
110 {
111 // Cleanup
112 mlt_properties_close( properties );
113 mlt_profile_close();
114 // Failover
115 mlt_profile_get();
116 }
117
118 // Set MLT_NORMALISATION to appease legacy modules
119 char *profile_name = mlt_environment( "MLT_PROFILE" );
120 if ( strstr( profile_name, "_ntsc" ) ||
121 strstr( profile_name, "_60" ) ||
122 strstr( profile_name, "_30" ) )
123 {
124 mlt_environment_set( "MLT_NORMALISATION", "NTSC" );
125 }
126 else if ( strstr( profile_name, "_pal" ) ||
127 strstr( profile_name, "_50" ) ||
128 strstr( profile_name, "_25" ) )
129 {
130 mlt_environment_set( "MLT_NORMALISATION", "PAL" );
131 }
132
133 return profile;
134 }
135
136 /** Load a profile from a properties object
137 */
138
139 mlt_profile mlt_profile_load_properties( mlt_properties properties )
140 {
141 mlt_profile_close();
142 profile = calloc( 1, sizeof( struct mlt_profile_s ) );
143 if ( profile )
144 {
145 if ( mlt_properties_get( properties, "name" ) )
146 mlt_environment_set( "MLT_PROFILE", mlt_properties_get( properties, "name" ) );
147 if ( mlt_properties_get( properties, "description" ) )
148 profile->description = strdup( mlt_properties_get( properties, "description" ) );
149 profile->frame_rate_num = mlt_properties_get_int( properties, "frame_rate_num" );
150 profile->frame_rate_den = mlt_properties_get_int( properties, "frame_rate_den" );
151 profile->width = mlt_properties_get_int( properties, "width" );
152 profile->height = mlt_properties_get_int( properties, "height" );
153 profile->progressive = mlt_properties_get_int( properties, "progressive" );
154 profile->sample_aspect_num = mlt_properties_get_int( properties, "sample_aspect_num" );
155 profile->sample_aspect_den = mlt_properties_get_int( properties, "sample_aspect_den" );
156 profile->display_aspect_num = mlt_properties_get_int( properties, "display_aspect_num" );
157 profile->display_aspect_den = mlt_properties_get_int( properties, "display_aspect_den" );
158 }
159 return profile;
160 }
161
162 /** Load an anonymous profile from string
163 */
164
165 mlt_profile mlt_profile_load_string( const char *string )
166 {
167 mlt_properties properties = mlt_properties_new();
168 if ( properties )
169 {
170 const char *p = string;
171 while ( p )
172 {
173 if ( strcmp( p, "" ) && p[ 0 ] != '#' )
174 mlt_properties_parse( properties, p );
175 p = strchr( p, '\n' );
176 if ( p ) p++;
177 }
178 }
179 return mlt_profile_load_properties( properties );
180 }
181
182 /** Get the framerate as float
183 */
184
185 double mlt_profile_fps( mlt_profile aprofile )
186 {
187 if ( aprofile )
188 return ( double ) aprofile->frame_rate_num / aprofile->frame_rate_den;
189 else
190 return ( double ) mlt_profile_get()->frame_rate_num / mlt_profile_get()->frame_rate_den;
191 }
192
193 /** Get the sample aspect ratio as float
194 */
195
196 double mlt_profile_sar( mlt_profile aprofile )
197 {
198 if ( aprofile )
199 return ( double ) aprofile->sample_aspect_num / aprofile->sample_aspect_den;
200 else
201 return ( double ) mlt_profile_get()->sample_aspect_num / mlt_profile_get()->sample_aspect_den;
202 }
203
204 /** Get the display aspect ratio as float
205 */
206
207 double mlt_profile_dar( mlt_profile aprofile )
208 {
209 if ( aprofile )
210 return ( double ) aprofile->display_aspect_num / aprofile->display_aspect_den;
211 else
212 return ( double ) mlt_profile_get()->display_aspect_num / mlt_profile_get()->display_aspect_den;
213 }
214
215 /** Free up the global profile resources
216 */
217
218 void mlt_profile_close( )
219 {
220 if ( profile )
221 {
222 if ( profile->description )
223 free( profile->description );
224 profile->description = NULL;
225 free( profile );
226 profile = NULL;
227 }
228 }