]> git.sesse.net Git - mlt/blob - src/framework/mlt_profile.c
Added new profiles system: mlt_profile, MLT_PROFILE, and profiles documents.
[mlt] / 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                         profile->name = strdup( "DV PAL" );
45                         profile->frame_rate_num = 25;
46                         profile->frame_rate_den = 1;
47                         profile->width = 720;
48                         profile->height = 576;
49                         profile->progressive = 0;
50                         profile->sample_aspect_num = 59;
51                         profile->sample_aspect_den = 54;
52                         profile->display_aspect_num = 4;
53                         profile->display_aspect_den = 3;
54                 }
55         }
56         return profile;
57 }
58
59
60 /** Load a profile from the system folder
61 */
62
63 mlt_profile mlt_profile_select( const char *name )
64 {
65         const char *prefix = PREFIX;
66         char *filename = calloc( 1, strlen( prefix ) + strlen( PROFILES_DIR ) + strlen( name ) + 1 );
67         strcpy( filename, prefix );
68         if ( filename[ strlen( filename ) - 1 ] != '/' )
69                 filename[ strlen( filename ) ] = '/';
70         strcat( filename, PROFILES_DIR );
71         strcat( filename, name );
72         return mlt_profile_load_file( filename );
73 }
74
75 /** Load a profile from specific file
76 */
77
78 mlt_profile mlt_profile_load_file( const char *file )
79 {
80         // Load the profile as properties
81         mlt_properties properties = mlt_properties_load( file );
82         if ( properties && mlt_properties_get_int( properties, "width" ) )
83         {
84                 mlt_profile_load_properties( properties );
85                 if ( !profile->name )
86                 {
87                         char *filename = strdup( file );
88                         profile->name = strdup( basename( filename ) );
89                         free( filename );
90                 }
91         }
92         else
93         {
94                 mlt_properties_close( properties );
95                 mlt_profile_close();
96         }
97         return profile;
98 }
99
100 /** Load a profile from a properties object
101 */
102
103 mlt_profile mlt_profile_load_properties( mlt_properties properties )
104 {
105         mlt_profile_close();
106         profile = calloc( 1, sizeof( struct mlt_profile_s ) );
107         if ( profile )
108         {
109                 if ( mlt_properties_get( properties, "name" ) )
110                         profile->name = mlt_properties_get( properties, "name" );
111                 profile->frame_rate_num = mlt_properties_get_int( properties, "frame_rate_num" );
112                 profile->frame_rate_den = mlt_properties_get_int( properties, "frame_rate_den" );
113                 profile->width = mlt_properties_get_int( properties, "width" );
114                 profile->height = mlt_properties_get_int( properties, "height" );
115                 profile->progressive = mlt_properties_get_int( properties, "progressive" );
116                 profile->sample_aspect_num = mlt_properties_get_int( properties, "sample_aspect_num" );
117                 profile->sample_aspect_den = mlt_properties_get_int( properties, "sample_aspect_den" );
118                 profile->display_aspect_num = mlt_properties_get_int( properties, "display_aspect_num" );
119                 profile->display_aspect_den = mlt_properties_get_int( properties, "display_aspect_den" );
120         }
121         return profile;
122 }
123
124 /** Load an anonymous profile from string
125 */
126
127 mlt_profile mlt_profile_load_string( const char *string )
128 {
129         mlt_properties properties = mlt_properties_new();
130         if ( properties )
131         {
132                 const char *p = string;
133                 while ( p )
134                 {
135                         if ( strcmp( p, "" ) && p[ 0 ] != '#' )
136                                 mlt_properties_parse( properties, p );
137                         p = strchr( p, '\n' );
138                         if ( p ) p++;
139                 }
140         }
141         mlt_profile_load_properties( properties );
142         if ( profile && !profile->name )
143                 profile->name = strdup( "untitled" );
144         return profile;
145 }
146
147 /** Get the framerate as float
148 */
149
150 double mlt_profile_fps( mlt_profile aprofile )
151 {
152         if ( aprofile )
153                 return ( double ) aprofile->frame_rate_num / aprofile->frame_rate_den;
154         else
155                 return ( double ) mlt_profile_get()->frame_rate_num / mlt_profile_get()->frame_rate_den;
156 }
157
158 /** Get the sample aspect ratio as float
159 */
160
161 double mlt_profile_sar( mlt_profile aprofile )
162 {
163         if ( aprofile )
164                 return ( double ) aprofile->sample_aspect_num / aprofile->sample_aspect_den;
165         else
166                 return ( double ) mlt_profile_get()->sample_aspect_num / mlt_profile_get()->sample_aspect_den;
167 }
168
169 /** Get the display aspect ratio as float
170 */
171
172 double mlt_profile_dar( mlt_profile aprofile )
173 {
174         if ( aprofile )
175                 return ( double ) aprofile->display_aspect_num / aprofile->display_aspect_den;
176         else
177                 return ( double ) mlt_profile_get()->display_aspect_num / mlt_profile_get()->display_aspect_den;
178 }
179
180 /** Free up the global profile resources
181 */
182
183 void mlt_profile_close( )
184 {
185         if ( profile )
186         {
187                 if ( profile->name )
188                         free( profile->name );
189                 profile->name = NULL;
190                 free( profile );
191                 profile = NULL;
192         }
193 }