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