3 * \brief video output definition
6 * Copyright (C) 2007-2009 Ushodaya Enterprises Limited
7 * \author Dan Dennedy <dan@dennedy.org>
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
31 /** the default subdirectory of the datadir for holding profiles */
32 #define PROFILES_DIR "/profiles"
34 /** Load a profile from the system folder.
36 * The environment variable MLT_PROFILES_PATH overrides the default \p PROFILES_DIR.
38 * \private \memberof mlt_profile_s
39 * \param name the name of a profile settings file located in the standard location or
40 * the full path name to a profile settings file
41 * \return a profile or NULL on error
44 static mlt_profile mlt_profile_select( const char *name )
46 char *filename = NULL;
47 const char *prefix = getenv( "MLT_PROFILES_PATH" );
48 mlt_properties properties = mlt_properties_load( name );
49 mlt_profile profile = NULL;
51 // Try to load from file specification
52 if ( properties && mlt_properties_get_int( properties, "width" ) )
54 filename = calloc( 1, strlen( name ) + 1 );
56 // Load from $datadir/mlt/profiles
57 else if ( prefix == NULL )
60 filename = calloc( 1, strlen( prefix ) + strlen( PROFILES_DIR ) + strlen( name ) + 1 );
61 strcpy( filename, prefix );
62 strcat( filename, PROFILES_DIR );
64 // Use environment variable instead
67 filename = calloc( 1, strlen( prefix ) + strlen( name ) + 2 );
68 strcpy( filename, prefix );
69 if ( filename[ strlen( filename ) - 1 ] != '/' )
70 filename[ strlen( filename ) ] = '/';
74 strcat( filename, name );
75 profile = mlt_profile_load_file( filename );
78 mlt_properties_close( properties );
84 /** Construct a profile.
86 * This will never return NULL as it uses the dv_pal settings as hard-coded fallback default.
88 * \public \memberof mlt_profile_s
89 * \param name the name of a profile settings file located in the standard location or
90 * the full path name to a profile settings file
94 mlt_profile mlt_profile_init( const char *name )
96 mlt_profile profile = NULL;
98 // Explicit profile by name gets priority over environment variables
100 profile = mlt_profile_select( name );
102 // Try to load by environment variable
103 if ( profile == NULL )
105 // MLT_PROFILE is preferred environment variable
106 if ( getenv( "MLT_PROFILE" ) )
107 profile = mlt_profile_select( getenv( "MLT_PROFILE" ) );
108 // MLT_NORMALISATION backwards compatibility
109 else if ( getenv( "MLT_NORMALISATION" ) && strcmp( getenv( "MLT_NORMALISATION" ), "PAL" ) )
110 profile = mlt_profile_select( "dv_ntsc" );
112 profile = mlt_profile_select( "dv_pal" );
114 // If still not loaded (no profile files), default to PAL
115 if ( profile == NULL )
117 profile = calloc( 1, sizeof( struct mlt_profile_s ) );
120 mlt_environment_set( "MLT_PROFILE", "dv_pal" );
121 profile->description = strdup( "PAL 4:3 DV or DVD" );
122 profile->frame_rate_num = 25;
123 profile->frame_rate_den = 1;
124 profile->width = 720;
125 profile->height = 576;
126 profile->progressive = 0;
127 profile->sample_aspect_num = 16;
128 profile->sample_aspect_den = 15;
129 profile->display_aspect_num = 4;
130 profile->display_aspect_den = 3;
131 profile->colorspace = 601;
138 static void set_mlt_normalisation( const char *profile_name )
142 if ( strstr( profile_name, "_ntsc" ) ||
143 strstr( profile_name, "_60" ) ||
144 strstr( profile_name, "_5994" ) ||
145 strstr( profile_name, "_2997" ) ||
146 strstr( profile_name, "_30" ) )
148 mlt_environment_set( "MLT_NORMALISATION", "NTSC" );
150 else if ( strstr( profile_name, "_pal" ) ||
151 strstr( profile_name, "_50" ) ||
152 strstr( profile_name, "_25" ) )
154 mlt_environment_set( "MLT_NORMALISATION", "PAL" );
159 /** Load a profile from specific file.
161 * \public \memberof mlt_profile_s
162 * \param file the full path name to a properties file
163 * \return a profile or NULL on error
166 mlt_profile mlt_profile_load_file( const char *file )
168 mlt_profile profile = NULL;
170 // Load the profile as properties
171 mlt_properties properties = mlt_properties_load( file );
174 // Simple check if the profile is valid
175 if ( mlt_properties_get_int( properties, "width" ) )
177 profile = mlt_profile_load_properties( properties );
179 // Set MLT_PROFILE to basename
180 char *filename = strdup( file );
181 mlt_environment_set( "MLT_PROFILE", basename( filename ) );
182 set_mlt_normalisation( basename( filename ) );
185 mlt_properties_close( properties );
188 // Set MLT_NORMALISATION to appease legacy modules
189 char *profile_name = mlt_environment( "MLT_PROFILE" );
190 set_mlt_normalisation( profile_name );
194 /** Load a profile from a properties object.
196 * \public \memberof mlt_profile_s
197 * \param properties a properties list
198 * \return a profile or NULL if out of memory
201 mlt_profile mlt_profile_load_properties( mlt_properties properties )
203 mlt_profile profile = calloc( 1, sizeof( struct mlt_profile_s ) );
206 if ( mlt_properties_get( properties, "name" ) )
207 mlt_environment_set( "MLT_PROFILE", mlt_properties_get( properties, "name" ) );
208 if ( mlt_properties_get( properties, "description" ) )
209 profile->description = strdup( mlt_properties_get( properties, "description" ) );
210 profile->frame_rate_num = mlt_properties_get_int( properties, "frame_rate_num" );
211 profile->frame_rate_den = mlt_properties_get_int( properties, "frame_rate_den" );
212 profile->width = mlt_properties_get_int( properties, "width" );
213 profile->height = mlt_properties_get_int( properties, "height" );
214 profile->progressive = mlt_properties_get_int( properties, "progressive" );
215 profile->sample_aspect_num = mlt_properties_get_int( properties, "sample_aspect_num" );
216 profile->sample_aspect_den = mlt_properties_get_int( properties, "sample_aspect_den" );
217 profile->display_aspect_num = mlt_properties_get_int( properties, "display_aspect_num" );
218 profile->display_aspect_den = mlt_properties_get_int( properties, "display_aspect_den" );
219 profile->colorspace = mlt_properties_get_int( properties, "colorspace" );
224 /** Load an anonymous profile from string.
226 * \public \memberof mlt_profile_s
227 * \param string a newline-delimited list of properties as name=value pairs
228 * \return a profile or NULL if out of memory
231 mlt_profile mlt_profile_load_string( const char *string )
233 mlt_properties properties = mlt_properties_new();
236 const char *p = string;
239 if ( strcmp( p, "" ) && p[ 0 ] != '#' )
240 mlt_properties_parse( properties, p );
241 p = strchr( p, '\n' );
245 return mlt_profile_load_properties( properties );
248 /** Get the video frame rate as a floating point value.
250 * \public \memberof mlt_profile_s
251 * @param profile a profile
252 * @return the frame rate
255 double mlt_profile_fps( mlt_profile profile )
258 return ( double ) profile->frame_rate_num / profile->frame_rate_den;
263 /** Get the sample aspect ratio as a floating point value.
265 * \public \memberof mlt_profile_s
266 * \param profile a profile
267 * \return the pixel aspect ratio
270 double mlt_profile_sar( mlt_profile profile )
273 return ( double ) profile->sample_aspect_num / profile->sample_aspect_den;
278 /** Get the display aspect ratio as floating point value.
280 * \public \memberof mlt_profile_s
281 * \param profile a profile
282 * \return the image aspect ratio
285 double mlt_profile_dar( mlt_profile profile )
288 return ( double ) profile->display_aspect_num / profile->display_aspect_den;
293 /** Free up the global profile resources.
295 * \public \memberof mlt_profile_s
296 * \param profile a profile
299 void mlt_profile_close( mlt_profile profile )
303 if ( profile->description )
304 free( profile->description );
305 profile->description = NULL;
311 /** Make a copy of a profile.
313 * \public \memberof mlt_profile_s
314 * \param profile the profile to clone
315 * \return a copy of the profile
318 mlt_profile mlt_profile_clone( mlt_profile profile )
320 mlt_profile clone = NULL;
324 clone = calloc( 1, sizeof( *profile ) );
327 memcpy( clone, profile, sizeof( *profile ) );
328 clone->description = strdup( profile->description );
335 /** Get the list of profiles.
337 * The caller MUST close the returned properties object!
338 * Each entry in the list is keyed on its name, and its value is another
339 * properties object that contains the attributes of the profile.
340 * \public \memberof mlt_profile_s
341 * \return a list of profiles
344 mlt_properties mlt_profile_list( )
346 char *filename = NULL;
347 const char *prefix = getenv( "MLT_PROFILES_PATH" );
348 mlt_properties properties = mlt_properties_new();
349 mlt_properties dir = mlt_properties_new();
351 const char *wildcard = NULL;
354 // Load from $datadir/mlt/profiles if no env var
355 if ( prefix == NULL )
357 prefix = mlt_environment( "MLT_DATA" );
358 filename = calloc( 1, strlen( prefix ) + strlen( PROFILES_DIR ) + 1 );
359 strcpy( filename, prefix );
360 strcat( filename, PROFILES_DIR );
364 mlt_properties_dir_list( dir, prefix, wildcard, sort );
366 for ( i = 0; i < mlt_properties_count( dir ); i++ )
368 char *filename = mlt_properties_get_value( dir, i );
369 char *profile_name = basename( filename );
370 if ( profile_name[0] != '.' && strcmp( profile_name, "Makefile" ) &&
371 profile_name[ strlen( profile_name ) - 1 ] != '~' )
373 mlt_properties profile = mlt_properties_load( filename );
376 mlt_properties_set_data( properties, profile_name, profile, 0,
377 (mlt_destructor) mlt_properties_close, NULL );
381 mlt_properties_close( dir );
388 /** Update the profile using the attributes of a producer.
390 * Use this to make an "auto-profile." Typically, you need to re-open the producer
391 * after you use this because some producers (e.g. avformat) adjust their framerate
392 * to that of the profile used when you created it.
393 * \public \memberof mlt_profile_s
394 * \param profile the profile to update
395 * \param producer the producer to inspect
398 void mlt_profile_from_producer( mlt_profile profile, mlt_producer producer )
402 mlt_image_format fmt = mlt_image_yuv422;
404 int w = profile->width;
405 int h = profile->height;
407 if ( ! mlt_service_get_frame( MLT_PRODUCER_SERVICE(producer), &fr, 0 ) && fr )
409 mlt_properties_set_double( MLT_FRAME_PROPERTIES( fr ), "consumer_aspect_ratio", mlt_profile_sar( profile ) );
410 if ( ! mlt_frame_get_image( fr, &buffer, &fmt, &w, &h, 0 ) )
412 // Some source properties are not exposed until after the first get_image call.
413 mlt_frame_close( fr );
414 mlt_service_get_frame( MLT_PRODUCER_SERVICE(producer), &fr, 0 );
415 p = MLT_FRAME_PROPERTIES( fr );
416 // mlt_properties_dump(p, stderr);
417 if ( mlt_properties_get_int( p, "meta.media.frame_rate_den" ) && mlt_properties_get_int( p, "meta.media.sample_aspect_den" ) )
419 profile->width = mlt_properties_get_int( p, "meta.media.width" );
420 profile->height = mlt_properties_get_int( p, "meta.media.height" );
421 profile->progressive = mlt_properties_get_int( p, "meta.media.progressive" );
422 profile->frame_rate_num = mlt_properties_get_int( p, "meta.media.frame_rate_num" );
423 profile->frame_rate_den = mlt_properties_get_int( p, "meta.media.frame_rate_den" );
424 // AVCHD is mis-reported as double frame rate.
425 if ( profile->progressive == 0 && (
426 profile->frame_rate_num / profile->frame_rate_den == 50 ||
427 profile->frame_rate_num / profile->frame_rate_den == 59 ) )
428 profile->frame_rate_num /= 2;
429 profile->sample_aspect_num = mlt_properties_get_int( p, "meta.media.sample_aspect_num" );
430 profile->sample_aspect_den = mlt_properties_get_int( p, "meta.media.sample_aspect_den" );
431 profile->colorspace = mlt_properties_get_int( p, "meta.media.colorspace" );
432 profile->display_aspect_num = (int) ( (double) profile->sample_aspect_num * profile->width / profile->sample_aspect_den + 0.5 );
433 profile->display_aspect_den = profile->height;
434 free( profile->description );
435 profile->description = strdup( "automatic" );
436 profile->is_explicit = 0;
440 mlt_frame_close( fr );
441 mlt_producer_seek( producer, 0 );