b9e230f68b834d5b85fdf72a2e1823c8e5de77b5
[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 /** Load a profile from the system folder
32 */
33
34 static mlt_profile mlt_profile_select( const char *name )
35 {
36 char *filename = NULL;
37 const char *prefix = getenv( "MLT_PROFILES_PATH" );
38 mlt_properties properties = mlt_properties_load( name );
39 mlt_profile profile = NULL;
40
41 // Try to load from file specification
42 if ( properties && mlt_properties_get_int( properties, "width" ) )
43 {
44 filename = calloc( 1, strlen( name ) + 1 );
45 }
46 // Load from $prefix/share/mlt/profiles
47 else if ( prefix == NULL )
48 {
49 prefix = PREFIX;
50 filename = calloc( 1, strlen( prefix ) + strlen( PROFILES_DIR ) + strlen( name ) + 2 );
51 strcpy( filename, prefix );
52 if ( filename[ strlen( filename ) - 1 ] != '/' )
53 filename[ strlen( filename ) ] = '/';
54 strcat( filename, PROFILES_DIR );
55 }
56 // Use environment variable instead
57 else
58 {
59 filename = calloc( 1, strlen( prefix ) + strlen( name ) + 2 );
60 strcpy( filename, prefix );
61 if ( filename[ strlen( filename ) - 1 ] != '/' )
62 filename[ strlen( filename ) ] = '/';
63 }
64
65 // Finish loading
66 strcat( filename, name );
67 profile = mlt_profile_load_file( filename );
68
69 // Cleanup
70 mlt_properties_close( properties );
71 free( filename );
72
73 return profile;
74 }
75
76 /** Construct a profile.
77 */
78
79 mlt_profile mlt_profile_init( const char *name )
80 {
81 mlt_profile profile = NULL;
82
83 // Explicit profile by name gets priority over environment variables
84 if ( name )
85 profile = mlt_profile_select( name );
86
87 // Try to load by environment variable
88 if ( profile == NULL )
89 {
90 // MLT_PROFILE is preferred environment variable
91 if ( getenv( "MLT_PROFILE" ) )
92 profile = mlt_profile_select( getenv( "MLT_PROFILE" ) );
93 // MLT_NORMALISATION backwards compatibility
94 else if ( getenv( "MLT_NORMALISATION" ) && strcmp( getenv( "MLT_NORMALISATION" ), "PAL" ) )
95 profile = mlt_profile_select( "dv_ntsc" );
96 else
97 profile = mlt_profile_select( "dv_pal" );
98
99 // If still not loaded (no profile files), default to PAL
100 if ( profile == NULL )
101 {
102 profile = calloc( 1, sizeof( struct mlt_profile_s ) );
103 if ( profile )
104 {
105 mlt_environment_set( "MLT_PROFILE", "dv_pal" );
106 profile->description = strdup( "PAL 4:3 DV or DVD" );
107 profile->frame_rate_num = 25;
108 profile->frame_rate_den = 1;
109 profile->width = 720;
110 profile->height = 576;
111 profile->progressive = 0;
112 profile->sample_aspect_num = 59;
113 profile->sample_aspect_den = 54;
114 profile->display_aspect_num = 4;
115 profile->display_aspect_den = 3;
116 }
117 }
118 }
119 return profile;
120 }
121
122 /** Load a profile from specific file
123 */
124
125 mlt_profile mlt_profile_load_file( const char *file )
126 {
127 mlt_profile profile = NULL;
128
129 // Load the profile as properties
130 mlt_properties properties = mlt_properties_load( file );
131 if ( properties )
132 {
133 // Simple check if the profile is valid
134 if ( mlt_properties_get_int( properties, "width" ) )
135 {
136 profile = mlt_profile_load_properties( properties );
137
138 // Set MLT_PROFILE to basename
139 char *filename = strdup( file );
140 mlt_environment_set( "MLT_PROFILE", basename( filename ) );
141 free( filename );
142 }
143 mlt_properties_close( properties );
144 }
145
146 // Set MLT_NORMALISATION to appease legacy modules
147 char *profile_name = getenv( "MLT_PROFILE" );
148 if ( profile_name )
149 {
150 if ( strstr( profile_name, "_ntsc" ) ||
151 strstr( profile_name, "_60" ) ||
152 strstr( profile_name, "_30" ) )
153 {
154 mlt_environment_set( "MLT_NORMALISATION", "NTSC" );
155 }
156 else if ( strstr( profile_name, "_pal" ) ||
157 strstr( profile_name, "_50" ) ||
158 strstr( profile_name, "_25" ) )
159 {
160 mlt_environment_set( "MLT_NORMALISATION", "PAL" );
161 }
162 }
163 return profile;
164 }
165
166 /** Load a profile from a properties object
167 */
168
169 mlt_profile mlt_profile_load_properties( mlt_properties properties )
170 {
171 mlt_profile profile = calloc( 1, sizeof( struct mlt_profile_s ) );
172 if ( profile )
173 {
174 if ( mlt_properties_get( properties, "name" ) )
175 mlt_environment_set( "MLT_PROFILE", mlt_properties_get( properties, "name" ) );
176 if ( mlt_properties_get( properties, "description" ) )
177 profile->description = strdup( mlt_properties_get( properties, "description" ) );
178 profile->frame_rate_num = mlt_properties_get_int( properties, "frame_rate_num" );
179 profile->frame_rate_den = mlt_properties_get_int( properties, "frame_rate_den" );
180 profile->width = mlt_properties_get_int( properties, "width" );
181 profile->height = mlt_properties_get_int( properties, "height" );
182 profile->progressive = mlt_properties_get_int( properties, "progressive" );
183 profile->sample_aspect_num = mlt_properties_get_int( properties, "sample_aspect_num" );
184 profile->sample_aspect_den = mlt_properties_get_int( properties, "sample_aspect_den" );
185 profile->display_aspect_num = mlt_properties_get_int( properties, "display_aspect_num" );
186 profile->display_aspect_den = mlt_properties_get_int( properties, "display_aspect_den" );
187 }
188 return profile;
189 }
190
191 /** Load an anonymous profile from string
192 */
193
194 mlt_profile mlt_profile_load_string( const char *string )
195 {
196 mlt_properties properties = mlt_properties_new();
197 if ( properties )
198 {
199 const char *p = string;
200 while ( p )
201 {
202 if ( strcmp( p, "" ) && p[ 0 ] != '#' )
203 mlt_properties_parse( properties, p );
204 p = strchr( p, '\n' );
205 if ( p ) p++;
206 }
207 }
208 return mlt_profile_load_properties( properties );
209 }
210
211 /** Get the framerate as float
212 */
213
214 double mlt_profile_fps( mlt_profile aprofile )
215 {
216 if ( aprofile )
217 return ( double ) aprofile->frame_rate_num / aprofile->frame_rate_den;
218 else
219 return 0;
220 }
221
222 /** Get the sample aspect ratio as float
223 */
224
225 double mlt_profile_sar( mlt_profile aprofile )
226 {
227 if ( aprofile )
228 return ( double ) aprofile->sample_aspect_num / aprofile->sample_aspect_den;
229 else
230 return 0;
231 }
232
233 /** Get the display aspect ratio as float
234 */
235
236 double mlt_profile_dar( mlt_profile aprofile )
237 {
238 if ( aprofile )
239 return ( double ) aprofile->display_aspect_num / aprofile->display_aspect_den;
240 else
241 return 0;
242 }
243
244 /** Free up the global profile resources
245 */
246
247 void mlt_profile_close( mlt_profile profile )
248 {
249 if ( profile )
250 {
251 if ( profile->description )
252 free( profile->description );
253 profile->description = NULL;
254 free( profile );
255 profile = NULL;
256 }
257 }