778bb538098dadd26aef839b3f38ded2948c57c3
[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( mlt_environment( "MLT_PROFILE" ) );
93 // MLT_NORMALISATION backwards compatibility
94 else if ( strcmp( mlt_environment( "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 = mlt_environment( "MLT_PROFILE" );
148 if ( strstr( profile_name, "_ntsc" ) ||
149 strstr( profile_name, "_60" ) ||
150 strstr( profile_name, "_30" ) )
151 {
152 mlt_environment_set( "MLT_NORMALISATION", "NTSC" );
153 }
154 else if ( strstr( profile_name, "_pal" ) ||
155 strstr( profile_name, "_50" ) ||
156 strstr( profile_name, "_25" ) )
157 {
158 mlt_environment_set( "MLT_NORMALISATION", "PAL" );
159 }
160
161 return profile;
162 }
163
164 /** Load a profile from a properties object
165 */
166
167 mlt_profile mlt_profile_load_properties( mlt_properties properties )
168 {
169 mlt_profile profile = calloc( 1, sizeof( struct mlt_profile_s ) );
170 if ( profile )
171 {
172 if ( mlt_properties_get( properties, "name" ) )
173 mlt_environment_set( "MLT_PROFILE", mlt_properties_get( properties, "name" ) );
174 if ( mlt_properties_get( properties, "description" ) )
175 profile->description = strdup( mlt_properties_get( properties, "description" ) );
176 profile->frame_rate_num = mlt_properties_get_int( properties, "frame_rate_num" );
177 profile->frame_rate_den = mlt_properties_get_int( properties, "frame_rate_den" );
178 profile->width = mlt_properties_get_int( properties, "width" );
179 profile->height = mlt_properties_get_int( properties, "height" );
180 profile->progressive = mlt_properties_get_int( properties, "progressive" );
181 profile->sample_aspect_num = mlt_properties_get_int( properties, "sample_aspect_num" );
182 profile->sample_aspect_den = mlt_properties_get_int( properties, "sample_aspect_den" );
183 profile->display_aspect_num = mlt_properties_get_int( properties, "display_aspect_num" );
184 profile->display_aspect_den = mlt_properties_get_int( properties, "display_aspect_den" );
185 }
186 return profile;
187 }
188
189 /** Load an anonymous profile from string
190 */
191
192 mlt_profile mlt_profile_load_string( const char *string )
193 {
194 mlt_properties properties = mlt_properties_new();
195 if ( properties )
196 {
197 const char *p = string;
198 while ( p )
199 {
200 if ( strcmp( p, "" ) && p[ 0 ] != '#' )
201 mlt_properties_parse( properties, p );
202 p = strchr( p, '\n' );
203 if ( p ) p++;
204 }
205 }
206 return mlt_profile_load_properties( properties );
207 }
208
209 /** Get the framerate as float
210 */
211
212 double mlt_profile_fps( mlt_profile aprofile )
213 {
214 if ( aprofile )
215 return ( double ) aprofile->frame_rate_num / aprofile->frame_rate_den;
216 else
217 return 0;
218 }
219
220 /** Get the sample aspect ratio as float
221 */
222
223 double mlt_profile_sar( mlt_profile aprofile )
224 {
225 if ( aprofile )
226 return ( double ) aprofile->sample_aspect_num / aprofile->sample_aspect_den;
227 else
228 return 0;
229 }
230
231 /** Get the display aspect ratio as float
232 */
233
234 double mlt_profile_dar( mlt_profile aprofile )
235 {
236 if ( aprofile )
237 return ( double ) aprofile->display_aspect_num / aprofile->display_aspect_den;
238 else
239 return 0;
240 }
241
242 /** Free up the global profile resources
243 */
244
245 void mlt_profile_close( mlt_profile profile )
246 {
247 if ( profile )
248 {
249 if ( profile->description )
250 free( profile->description );
251 profile->description = NULL;
252 free( profile );
253 profile = NULL;
254 }
255 }