2 * \file mlt_properties.c
3 * \brief Properties class definition
4 * \see mlt_properties_s
6 * Copyright (C) 2003-2013 Ushodaya Enterprises Limited
7 * \author Charles Yates <charles.yates@pandora.be>
8 * \author Dan Dennedy <dan@dennedy.org>
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) any later version.
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30 #include "mlt_properties.h"
31 #include "mlt_property.h"
32 #include "mlt_deque.h"
34 #include "mlt_factory.h"
42 #include <sys/types.h>
49 #define PRESETS_DIR "/presets"
51 /** \brief private implementation of the property list */
60 mlt_properties mirror;
62 pthread_mutex_t mutex;
67 /* Memory leak checks */
69 //#define _MLT_PROPERTY_CHECKS_ 2
70 #ifdef _MLT_PROPERTY_CHECKS_
71 static int properties_created = 0;
72 static int properties_destroyed = 0;
75 /** Initialize a properties object that was already allocated.
77 * This does allocate its ::property_list, and it adds a reference count.
78 * \public \memberof mlt_properties_s
79 * \param self the properties structure to initialize
80 * \param child an opaque pointer to a subclass object
81 * \return true if failed
84 int mlt_properties_init( mlt_properties self, void *child )
88 #ifdef _MLT_PROPERTY_CHECKS_
89 // Increment number of properties created
90 properties_created ++;
94 memset( self, 0, sizeof( struct mlt_properties_s ) );
96 // Assign the child of the object
99 // Allocate the local structure
100 self->local = calloc( 1, sizeof( property_list ) );
102 // Increment the ref count
103 ( ( property_list * )self->local )->ref_count = 1;
104 pthread_mutex_init( &( ( property_list * )self->local )->mutex, NULL );;
107 // Check that initialisation was successful
108 return self != NULL && self->local == NULL;
111 /** Create a properties object.
113 * This allocates the properties structure and calls mlt_properties_init() on it.
114 * Free the properties object with mlt_properties_close().
115 * \public \memberof mlt_properties_s
116 * \return a new properties object
119 mlt_properties mlt_properties_new( )
121 // Construct a standalone properties object
122 mlt_properties self = calloc( 1, sizeof( struct mlt_properties_s ) );
125 mlt_properties_init( self, NULL );
127 // Return the pointer
131 /** Set the numeric locale used for string/double conversions.
133 * \public \memberof mlt_properties_s
134 * \param self a properties list
135 * \param locale the locale name
136 * \return true if error
139 int mlt_properties_set_lcnumeric( mlt_properties self, const char *locale )
143 if ( self && locale )
145 property_list *list = self->local;
147 #if defined(__linux__) || defined(__DARWIN__)
149 freelocale( list->locale );
150 list->locale = newlocale( LC_NUMERIC_MASK, locale, NULL );
152 error = list->locale == NULL;
160 /** Get the numeric locale for this properties object.
162 * Do not free the result.
163 * \public \memberof mlt_properties_s
164 * \param self a properties list
165 * \return the locale name if this properties has a specific locale it is using, NULL otherwise
168 const char* mlt_properties_get_lcnumeric( mlt_properties self )
170 property_list *list = self->local;
171 const char *result = NULL;
175 #if defined(__DARWIN__)
176 result = querylocale( LC_NUMERIC, list->locale );
177 #elif defined(__linux__)
178 result = list->locale->__names[ LC_NUMERIC ];
180 // TODO: not yet sure what to do on other platforms
186 static int load_properties( mlt_properties self, const char *filename )
189 FILE *file = fopen( filename, "r" );
191 // Load contents of file
196 char last[ 1024 ] = "";
198 // Read each string from the file
199 while( fgets( temp, 1024, file ) )
201 // Chomp the new line character from the string
202 int x = strlen( temp ) - 1;
203 if ( temp[x] == '\n' || temp[x] == '\r' )
206 // Check if the line starts with a .
207 if ( temp[ 0 ] == '.' )
210 sprintf( temp2, "%s%s", last, temp );
211 strcpy( temp, temp2 );
213 else if ( strchr( temp, '=' ) )
215 strcpy( last, temp );
216 *( strchr( last, '=' ) ) = '\0';
219 // Parse and set the property
220 if ( strcmp( temp, "" ) && temp[ 0 ] != '#' )
221 mlt_properties_parse( self, temp );
227 return file? 0 : errno;
230 /** Create a properties object by reading a .properties text file.
232 * Free the properties object with mlt_properties_close().
233 * \deprecated Please start using mlt_properties_parse_yaml().
234 * \public \memberof mlt_properties_s
235 * \param filename the absolute file name
236 * \return a new properties object
239 mlt_properties mlt_properties_load( const char *filename )
241 // Construct a standalone properties object
242 mlt_properties self = mlt_properties_new( );
245 load_properties( self, filename );
247 // Return the pointer
251 /** Set properties from a preset.
253 * Presets are typically installed to $prefix/share/mlt/presets/{type}/{service}/[{profile}/]{name}.
254 * For example, "/usr/share/mlt/presets/consumer/avformat/dv_ntsc_wide/DVD"
255 * could be an encoding preset for a widescreen NTSC DVD Video.
256 * Do not specify the type and service in the preset name parameter; these are
257 * inferred automatically from the service to which you are applying the preset.
258 * Using the example above and assuming you are calling this function on the
259 * avformat consumer, the name passed to the function should simply be DVD.
260 * Note that the profile portion of the path is optional, but a profile-specific
261 * preset with the same name as a more generic one is given a higher priority.
262 * \todo Look in a user-specific location - somewhere in the home directory.
264 * \public \memberof mlt_properties_s
265 * \param self a properties list
266 * \param name the name of a preset in a well-known location or the explicit path
267 * \return true if error
270 int mlt_properties_preset( mlt_properties self, const char *name )
272 struct stat stat_buff;
275 if ( !( self && name && strlen( name ) ) )
278 // See if name is an explicit file
279 if ( ! stat( name, &stat_buff ) )
281 return load_properties( self, name );
285 // Look for profile-specific preset before a generic one.
286 char *data = getenv( "MLT_PRESETS_PATH" );
287 const char *type = mlt_properties_get( self, "mlt_type" );
288 const char *service = mlt_properties_get( self, "mlt_service" );
289 const char *profile = mlt_environment( "MLT_PROFILE" );
294 data = strdup( data );
298 data = malloc( strlen( mlt_environment( "MLT_DATA" ) ) + strlen( PRESETS_DIR ) + 1 );
299 strcpy( data, mlt_environment( "MLT_DATA" ) );
300 strcat( data, PRESETS_DIR );
302 if ( data && type && service )
304 char *path = malloc( 5 + strlen(name) + strlen(data) + strlen(type) + strlen(service) + ( profile? strlen(profile) : 0 ) );
305 sprintf( path, "%s/%s/%s/%s/%s", data, type, service, profile, name );
306 if ( load_properties( self, path ) )
308 sprintf( path, "%s/%s/%s/%s", data, type, service, name );
309 error = load_properties( self, path );
322 /** Generate a hash key.
324 * \private \memberof mlt_properties_s
325 * \param name a string
329 static inline int generate_hash( const char *name )
334 hash = ( hash + ( i ++ * ( *name ++ & 31 ) ) ) % 199;
338 /** Copy a serializable property to a properties list that is mirroring this one.
340 * Special case - when a container (such as loader) is protecting another
341 * producer, we need to ensure that properties are passed through to the
343 * \private \memberof mlt_properties_s
344 * \param self a properties list
345 * \param name the name of the property to copy
348 static inline void mlt_properties_do_mirror( mlt_properties self, const char *name )
351 property_list *list = self->local;
352 if ( list->mirror != NULL )
354 char *value = mlt_properties_get( self, name );
356 mlt_properties_set( list->mirror, name, value );
360 /** Increment the reference count.
362 * \public \memberof mlt_properties_s
363 * \param self a properties list
364 * \return the new reference count
367 int mlt_properties_inc_ref( mlt_properties self )
372 property_list *list = self->local;
373 pthread_mutex_lock( &list->mutex );
374 result = ++ list->ref_count;
375 pthread_mutex_unlock( &list->mutex );
380 /** Decrement the reference count.
382 * \public \memberof mlt_properties_s
383 * \param self a properties list
384 * \return the new reference count
387 int mlt_properties_dec_ref( mlt_properties self )
392 property_list *list = self->local;
393 pthread_mutex_lock( &list->mutex );
394 result = -- list->ref_count;
395 pthread_mutex_unlock( &list->mutex );
400 /** Get the reference count.
402 * \public \memberof mlt_properties_s
403 * \param self a properties list
404 * \return the current reference count
407 int mlt_properties_ref_count( mlt_properties self )
411 property_list *list = self->local;
412 return list->ref_count;
417 /** Set a properties list to be a mirror copy of another.
419 * Note that this does not copy all existing properties. Rather, you must
420 * call this before setting the properties that you wish to copy.
421 * \public \memberof mlt_properties_s
422 * \param that the properties which will receive copies of the properties as they are set.
423 * \param self the properties to mirror
426 void mlt_properties_mirror( mlt_properties self, mlt_properties that )
429 property_list *list = self->local;
433 /** Copy all serializable properties to another properties list.
435 * \public \memberof mlt_properties_s
436 * \param self The properties to copy to
437 * \param that The properties to copy from
438 * \return true if error
441 int mlt_properties_inherit( mlt_properties self, mlt_properties that )
443 if ( !self || !that ) return 1;
444 int count = mlt_properties_count( that );
446 for ( i = 0; i < count; i ++ )
448 char *value = mlt_properties_get_value( that, i );
451 char *name = mlt_properties_get_name( that, i );
452 mlt_properties_set( self, name, value );
458 /** Pass all serializable properties that match a prefix to another properties object
460 * \warning The prefix is stripped from the name when it is set on the \p self properties list!
461 * For example a property named "foo.bar" will match prefix "foo.", but the property
462 * will be named simply "bar" on the receiving properties object.
463 * \public \memberof mlt_properties_s
464 * \param self the properties to copy to
465 * \param that The properties to copy from
466 * \param prefix the property names to match (required)
467 * \return true if error
470 int mlt_properties_pass( mlt_properties self, mlt_properties that, const char *prefix )
472 if ( !self || !that ) return 1;
473 int count = mlt_properties_count( that );
474 int length = strlen( prefix );
476 for ( i = 0; i < count; i ++ )
478 char *name = mlt_properties_get_name( that, i );
479 if ( !strncmp( name, prefix, length ) )
481 char *value = mlt_properties_get_value( that, i );
483 mlt_properties_set( self, name + length, value );
489 /** Locate a property by name.
491 * \private \memberof mlt_properties_s
492 * \param self a properties list
493 * \param name the property to lookup by name
494 * \return the property or NULL for failure
497 static inline mlt_property mlt_properties_find( mlt_properties self, const char *name )
499 if ( !self || !name ) return NULL;
500 property_list *list = self->local;
501 mlt_property value = NULL;
502 int key = generate_hash( name );
504 mlt_properties_lock( self );
506 int i = list->hash[ key ] - 1;
509 // Check if we're hashed
510 if ( list->count > 0 &&
511 name[ 0 ] == list->name[ i ][ 0 ] &&
512 !strcmp( list->name[ i ], name ) )
513 value = list->value[ i ];
516 for ( i = list->count - 1; value == NULL && i >= 0; i -- )
517 if ( name[ 0 ] == list->name[ i ][ 0 ] && !strcmp( list->name[ i ], name ) )
518 value = list->value[ i ];
520 mlt_properties_unlock( self );
525 /** Add a new property.
527 * \private \memberof mlt_properties_s
528 * \param self a properties list
529 * \param name the name of the new property
530 * \return the new property
533 static mlt_property mlt_properties_add( mlt_properties self, const char *name )
535 property_list *list = self->local;
536 int key = generate_hash( name );
539 mlt_properties_lock( self );
541 // Check that we have space and resize if necessary
542 if ( list->count == list->size )
545 list->name = realloc( list->name, list->size * sizeof( const char * ) );
546 list->value = realloc( list->value, list->size * sizeof( mlt_property ) );
549 // Assign name/value pair
550 list->name[ list->count ] = strdup( name );
551 list->value[ list->count ] = mlt_property_init( );
553 // Assign to hash table
554 if ( list->hash[ key ] == 0 )
555 list->hash[ key ] = list->count + 1;
557 // Return and increment count accordingly
558 result = list->value[ list->count ++ ];
560 mlt_properties_unlock( self );
565 /** Fetch a property by name and add one if not found.
567 * \private \memberof mlt_properties_s
568 * \param self a properties list
569 * \param name the property to lookup or add
570 * \return the property
573 static mlt_property mlt_properties_fetch( mlt_properties self, const char *name )
575 // Try to find an existing property first
576 mlt_property property = mlt_properties_find( self, name );
578 // If it wasn't found, create one
579 if ( property == NULL )
580 property = mlt_properties_add( self, name );
582 // Return the property
586 /** Copy a property to another properties list.
588 * \public \memberof mlt_properties_s
589 * \author Zach <zachary.drew@gmail.com>
590 * \param self the properties to copy to
591 * \param that the properties to copy from
592 * \param name the name of the property to copy
595 void mlt_properties_pass_property( mlt_properties self, mlt_properties that, const char *name )
597 // Make sure the source property isn't null.
598 mlt_property that_prop = mlt_properties_find( that, name );
599 if( that_prop == NULL )
602 mlt_property_pass( mlt_properties_fetch( self, name ), that_prop );
605 /** Copy all properties specified in a comma-separated list to another properties list.
607 * White space is also a delimiter.
608 * \public \memberof mlt_properties_s
609 * \author Zach <zachary.drew@gmail.com>
610 * \param self the properties to copy to
611 * \param that the properties to copy from
612 * \param list a delimited list of property names
613 * \return true if error
617 int mlt_properties_pass_list( mlt_properties self, mlt_properties that, const char *list )
619 if ( !self || !that || !list ) return 1;
620 char *props = strdup( list );
622 const char *delim = " ,\t\n"; // Any combination of spaces, commas, tabs, and newlines
627 count = strcspn( ptr, delim );
629 if( ptr[count] == '\0' )
632 ptr[count] = '\0'; // Make it a real string
634 mlt_properties_pass_property( self, that, ptr );
638 ptr += strspn( ptr, delim );
647 /** Set a property to a string.
649 * The property name "properties" is reserved to load the preset in \p value.
650 * When the value begins with '@' then it is interpreted as a very simple math
651 * expression containing only the +, -, *, and / operators.
652 * The event "property-changed" is fired after the property has been set.
654 * This makes a copy of the string value you supply.
655 * \public \memberof mlt_properties_s
656 * \param self a properties list
657 * \param name the property to set
658 * \param value the property's new value
659 * \return true if error
662 int mlt_properties_set( mlt_properties self, const char *name, const char *value )
666 if ( !self || !name ) return error;
668 // Fetch the property to work with
669 mlt_property property = mlt_properties_fetch( self, name );
671 // Set it if not NULL
672 if ( property == NULL )
674 mlt_log( NULL, MLT_LOG_FATAL, "Whoops - %s not found (should never occur)\n", name );
676 else if ( value == NULL )
678 error = mlt_property_set_string( property, value );
679 mlt_properties_do_mirror( self, name );
681 else if ( *value != '@' )
683 error = mlt_property_set_string( property, value );
684 mlt_properties_do_mirror( self, name );
685 if ( !strcmp( name, "properties" ) )
686 mlt_properties_preset( self, value );
688 else if ( value[ 0 ] == '@' )
697 while ( *value != '\0' )
699 int length = strcspn( value, "+-*/" );
701 // Get the identifier
702 strncpy( id, value, length );
706 // Determine the value
707 if ( isdigit( id[ 0 ] ) )
709 #if defined(__GLIBC__) || defined(__DARWIN__)
710 property_list *list = self->local;
712 current = strtod_l( id, NULL, list->locale );
715 current = strtod( id, NULL );
719 current = mlt_properties_get_double( self, id );
722 // Apply the operation
735 total = total / current;
740 op = *value != '\0' ? *value ++ : ' ';
743 error = mlt_property_set_double( property, total );
744 mlt_properties_do_mirror( self, name );
747 mlt_events_fire( self, "property-changed", name, NULL );
752 /** Set or default a property to a string.
754 * This makes a copy of the string value you supply.
755 * \public \memberof mlt_properties_s
756 * \param self a properties list
757 * \param name the property to set
758 * \param value the string value to set or NULL to use the default
759 * \param def the default string if value is NULL
760 * \return true if error
763 int mlt_properties_set_or_default( mlt_properties self, const char *name, const char *value, const char *def )
765 return mlt_properties_set( self, name, value == NULL ? def : value );
768 /** Get a string value by name.
770 * Do not free the returned string. It's lifetime is controlled by the property
771 * and this properties object.
772 * \public \memberof mlt_properties_s
773 * \param self a properties list
774 * \param name the property to get
775 * \return the property's string value or NULL if it does not exist
778 char *mlt_properties_get( mlt_properties self, const char *name )
780 mlt_property value = mlt_properties_find( self, name );
781 property_list *list = self->local;
782 return value == NULL ? NULL : mlt_property_get_string_l( value, list->locale );
785 /** Get a property name by index.
787 * Do not free the returned string.
788 * \public \memberof mlt_properties_s
789 * \param self a properties list
790 * \param index the numeric index of the property
791 * \return the name of the property or NULL if index is out of range
794 char *mlt_properties_get_name( mlt_properties self, int index )
796 if ( !self ) return NULL;
797 property_list *list = self->local;
798 if ( index >= 0 && index < list->count )
799 return list->name[ index ];
803 /** Get a property's string value by index.
805 * Do not free the returned string.
806 * \public \memberof mlt_properties_s
807 * \param self a properties list
808 * \param index the numeric index of the property
809 * \return the property value as a string or NULL if the index is out of range
812 char *mlt_properties_get_value( mlt_properties self, int index )
814 if ( !self ) return NULL;
815 property_list *list = self->local;
816 if ( index >= 0 && index < list->count )
817 return mlt_property_get_string_l( list->value[ index ], list->locale );
821 /** Get a data value by index.
823 * Do not free the returned pointer if you supplied a destructor function when you
825 * \public \memberof mlt_properties_s
826 * \param self a properties list
827 * \param index the numeric index of the property
828 * \param[out] size the size of the binary data in bytes or NULL if the index is out of range
831 void *mlt_properties_get_data_at( mlt_properties self, int index, int *size )
833 if ( !self ) return NULL;
834 property_list *list = self->local;
835 if ( index >= 0 && index < list->count )
836 return mlt_property_get_data( list->value[ index ], size );
840 /** Return the number of items in the list.
842 * \public \memberof mlt_properties_s
843 * \param self a properties list
844 * \return the number of property objects or -1 if error
847 int mlt_properties_count( mlt_properties self )
849 if ( !self ) return -1;
850 property_list *list = self->local;
854 /** Set a value by parsing a name=value string.
856 * \public \memberof mlt_properties_s
857 * \param self a properties list
858 * \param namevalue a string containing name and value delimited by '='
859 * \return true if there was an error
862 int mlt_properties_parse( mlt_properties self, const char *namevalue )
864 if ( !self ) return 1;
865 char *name = strdup( namevalue );
868 char *ptr = strchr( name, '=' );
876 value = strdup( ptr );
881 value = strdup( ptr );
882 if ( value != NULL && value[ strlen( value ) - 1 ] == '\"' )
883 value[ strlen( value ) - 1 ] = '\0';
888 value = strdup( "" );
891 error = mlt_properties_set( self, name, value );
899 /** Get an integer associated to the name.
901 * \public \memberof mlt_properties_s
902 * \param self a properties list
903 * \param name the property to get
904 * \return The integer value, 0 if not found (which may also be a legitimate value)
907 int mlt_properties_get_int( mlt_properties self, const char *name )
909 mlt_profile profile = mlt_properties_get_data( self, "_profile", NULL );
910 double fps = mlt_profile_fps( profile );
911 property_list *list = self->local;
912 mlt_property value = mlt_properties_find( self, name );
913 return value == NULL ? 0 : mlt_property_get_int( value, fps, list->locale );
916 /** Set a property to an integer value.
918 * \public \memberof mlt_properties_s
919 * \param self a properties list
920 * \param name the property to set
921 * \param value the integer
922 * \return true if error
925 int mlt_properties_set_int( mlt_properties self, const char *name, int value )
929 if ( !self || !name ) return error;
931 // Fetch the property to work with
932 mlt_property property = mlt_properties_fetch( self, name );
934 // Set it if not NULL
935 if ( property != NULL )
937 error = mlt_property_set_int( property, value );
938 mlt_properties_do_mirror( self, name );
941 mlt_events_fire( self, "property-changed", name, NULL );
946 /** Get a 64-bit integer associated to the name.
948 * \public \memberof mlt_properties_s
949 * \param self a properties list
950 * \param name the property to get
951 * \return the integer value, 0 if not found (which may also be a legitimate value)
954 int64_t mlt_properties_get_int64( mlt_properties self, const char *name )
956 mlt_property value = mlt_properties_find( self, name );
957 return value == NULL ? 0 : mlt_property_get_int64( value );
960 /** Set a property to a 64-bit integer value.
962 * \public \memberof mlt_properties_s
963 * \param self a properties list
964 * \param name the property to set
965 * \param value the integer
966 * \return true if error
969 int mlt_properties_set_int64( mlt_properties self, const char *name, int64_t value )
973 if ( !self || !name ) return error;
975 // Fetch the property to work with
976 mlt_property property = mlt_properties_fetch( self, name );
978 // Set it if not NULL
979 if ( property != NULL )
981 error = mlt_property_set_int64( property, value );
982 mlt_properties_do_mirror( self, name );
985 mlt_events_fire( self, "property-changed", name, NULL );
990 /** Get a floating point value associated to the name.
992 * \public \memberof mlt_properties_s
993 * \param self a properties list
994 * \param name the property to get
995 * \return the floating point, 0 if not found (which may also be a legitimate value)
998 double mlt_properties_get_double( mlt_properties self, const char *name )
1000 mlt_profile profile = mlt_properties_get_data( self, "_profile", NULL );
1001 double fps = mlt_profile_fps( profile );
1002 mlt_property value = mlt_properties_find( self, name );
1003 property_list *list = self->local;
1004 return value == NULL ? 0 : mlt_property_get_double( value, fps, list->locale );
1007 /** Set a property to a floating point value.
1009 * \public \memberof mlt_properties_s
1010 * \param self a properties list
1011 * \param name the property to set
1012 * \param value the floating point value
1013 * \return true if error
1016 int mlt_properties_set_double( mlt_properties self, const char *name, double value )
1020 if ( !self || !name ) return error;
1022 // Fetch the property to work with
1023 mlt_property property = mlt_properties_fetch( self, name );
1025 // Set it if not NULL
1026 if ( property != NULL )
1028 error = mlt_property_set_double( property, value );
1029 mlt_properties_do_mirror( self, name );
1032 mlt_events_fire( self, "property-changed", name, NULL );
1037 /** Get a position value associated to the name.
1039 * \public \memberof mlt_properties_s
1040 * \param self a properties list
1041 * \param name the property to get
1042 * \return the position, 0 if not found (which may also be a legitimate value)
1045 mlt_position mlt_properties_get_position( mlt_properties self, const char *name )
1047 mlt_profile profile = mlt_properties_get_data( self, "_profile", NULL );
1048 double fps = mlt_profile_fps( profile );
1049 property_list *list = self->local;
1050 mlt_property value = mlt_properties_find( self, name );
1051 return value == NULL ? 0 : mlt_property_get_position( value, fps, list->locale );
1054 /** Set a property to a position value.
1056 * \public \memberof mlt_properties_s
1057 * \param self a properties list
1058 * \param name the property to get
1059 * \param value the position
1060 * \return true if error
1063 int mlt_properties_set_position( mlt_properties self, const char *name, mlt_position value )
1067 if ( !self || !name ) return error;
1069 // Fetch the property to work with
1070 mlt_property property = mlt_properties_fetch( self, name );
1072 // Set it if not NULL
1073 if ( property != NULL )
1075 error = mlt_property_set_position( property, value );
1076 mlt_properties_do_mirror( self, name );
1079 mlt_events_fire( self, "property-changed", name, NULL );
1084 /** Get a binary data value associated to the name.
1086 * Do not free the returned pointer if you supplied a destructor function
1087 * when you set this property.
1088 * \public \memberof mlt_properties_s
1089 * \param self a properties list
1090 * \param name the property to get
1091 * \param[out] length The size of the binary data in bytes, if available (often it is not, you should know)
1094 void *mlt_properties_get_data( mlt_properties self, const char *name, int *length )
1096 mlt_property value = mlt_properties_find( self, name );
1097 return value == NULL ? NULL : mlt_property_get_data( value, length );
1100 /** Store binary data as a property.
1102 * \public \memberof mlt_properties_s
1103 * \param self a properties list
1104 * \param name the property to set
1105 * \param value an opaque pointer to binary data
1106 * \param length the size of the binary data in bytes (optional)
1107 * \param destroy a function to deallocate the binary data when the property is closed (optional)
1108 * \param serialise a function that can serialize the binary data as text (optional)
1109 * \return true if error
1112 int mlt_properties_set_data( mlt_properties self, const char *name, void *value, int length, mlt_destructor destroy, mlt_serialiser serialise )
1116 if ( !self || !name ) return error;
1118 // Fetch the property to work with
1119 mlt_property property = mlt_properties_fetch( self, name );
1121 // Set it if not NULL
1122 if ( property != NULL )
1123 error = mlt_property_set_data( property, value, length, destroy, serialise );
1125 mlt_events_fire( self, "property-changed", name, NULL );
1130 /** Rename a property.
1132 * \public \memberof mlt_properties_s
1133 * \param self a properties list
1134 * \param source the property to rename
1135 * \param dest the new name
1136 * \return true if the name is already in use
1139 int mlt_properties_rename( mlt_properties self, const char *source, const char *dest )
1141 mlt_property value = mlt_properties_find( self, dest );
1143 if ( value == NULL )
1145 property_list *list = self->local;
1149 mlt_properties_lock( self );
1150 for ( i = 0; i < list->count; i ++ )
1152 if ( !strcmp( list->name[ i ], source ) )
1154 free( list->name[ i ] );
1155 list->name[ i ] = strdup( dest );
1156 list->hash[ generate_hash( dest ) ] = i + 1;
1160 mlt_properties_unlock( self );
1163 return value != NULL;
1166 /** Dump the properties to a file handle.
1168 * \public \memberof mlt_properties_s
1169 * \param self a properties list
1170 * \param output a file handle
1173 void mlt_properties_dump( mlt_properties self, FILE *output )
1175 if ( !self || !output ) return;
1176 property_list *list = self->local;
1178 for ( i = 0; i < list->count; i ++ )
1179 if ( mlt_properties_get( self, list->name[ i ] ) != NULL )
1180 fprintf( output, "%s=%s\n", list->name[ i ], mlt_properties_get( self, list->name[ i ] ) );
1183 /** Output the properties to a file handle.
1185 * This version includes reference counts and does not put each property on a new line.
1186 * \public \memberof mlt_properties_s
1187 * \param self a properties pointer
1188 * \param title a string to preface the output
1189 * \param output a file handle
1191 void mlt_properties_debug( mlt_properties self, const char *title, FILE *output )
1193 if ( !self || !output ) return;
1194 if ( output == NULL ) output = stderr;
1195 fprintf( output, "%s: ", title );
1198 property_list *list = self->local;
1200 fprintf( output, "[ ref=%d", list->ref_count );
1201 for ( i = 0; i < list->count; i ++ )
1202 if ( mlt_properties_get( self, list->name[ i ] ) != NULL )
1203 fprintf( output, ", %s=%s", list->name[ i ], mlt_properties_get( self, list->name[ i ] ) );
1205 fprintf( output, ", %s=%p", list->name[ i ], mlt_properties_get_data( self, list->name[ i ], NULL ) );
1206 fprintf( output, " ]" );
1208 fprintf( output, "\n" );
1211 /** Save the properties to a file by name.
1213 * This uses the dump format - one line per property.
1214 * \public \memberof mlt_properties_s
1215 * \param self a properties list
1216 * \param filename the name of a file to create or overwrite
1217 * \return true if there was an error
1220 int mlt_properties_save( mlt_properties self, const char *filename )
1223 if ( !self || !filename ) return error;
1224 FILE *f = fopen( filename, "w" );
1227 mlt_properties_dump( self, f );
1234 /* This is a very basic cross platform fnmatch replacement - it will fail in
1235 * many cases, but for the basic *.XXX and YYY*.XXX, it will work ok.
1238 /** Test whether a filename or pathname matches a shell-style pattern.
1240 * \private \memberof mlt_properties_s
1241 * \param wild a string containing a wildcard pattern
1242 * \param file the name of a file to test against
1243 * \return true if the file name matches the wildcard pattern
1246 static int mlt_fnmatch( const char *wild, const char *file )
1251 while( f < strlen( file ) && w < strlen( wild ) )
1253 if ( wild[ w ] == '*' )
1256 if ( w == strlen( wild ) )
1258 while ( f != strlen( file ) && tolower( file[ f ] ) != tolower( wild[ w ] ) )
1261 else if ( wild[ w ] == '?' || tolower( file[ f ] ) == tolower( wild[ w ] ) )
1266 else if ( wild[ 0 ] == '*' )
1276 return strlen( file ) == f && strlen( wild ) == w;
1279 /** Compare the string or serialized value of two properties.
1281 * \private \memberof mlt_properties_s
1282 * \param self a property
1283 * \param that a property
1284 * \return < 0 if \p self less than \p that, 0 if equal, or > 0 if \p self is greater than \p that
1287 static int mlt_compare( const void *self, const void *that )
1289 return strcmp( mlt_property_get_string( *( const mlt_property * )self ), mlt_property_get_string( *( const mlt_property * )that ) );
1292 /** Get the contents of a directory.
1294 * Obtains an optionally sorted list of the files found in a directory with a specific wild card.
1295 * Entries in the list have a numeric name (running from 0 to count - 1). Only values change
1296 * position if sort is enabled. Designed to be posix compatible (linux, os/x, mingw etc).
1297 * \public \memberof mlt_properties_s
1298 * \param self a properties list
1299 * \param dirname the name of the directory
1300 * \param pattern a wildcard pattern to filter the directory listing
1301 * \param sort Do you want to sort the directory listing?
1302 * \return the number of items in the directory listing
1305 int mlt_properties_dir_list( mlt_properties self, const char *dirname, const char *pattern, int sort )
1307 DIR *dir = opendir( dirname );
1312 struct dirent *de = readdir( dir );
1313 char fullname[ 1024 ];
1316 sprintf( key, "%d", mlt_properties_count( self ) );
1317 snprintf( fullname, 1024, "%s/%s", dirname, de->d_name );
1318 if ( pattern == NULL )
1319 mlt_properties_set( self, key, fullname );
1320 else if ( de->d_name[ 0 ] != '.' && mlt_fnmatch( pattern, de->d_name ) )
1321 mlt_properties_set( self, key, fullname );
1322 de = readdir( dir );
1328 if ( sort && mlt_properties_count( self ) )
1330 property_list *list = self->local;
1331 mlt_properties_lock( self );
1332 qsort( list->value, mlt_properties_count( self ), sizeof( mlt_property ), mlt_compare );
1333 mlt_properties_unlock( self );
1336 return mlt_properties_count( self );
1339 /** Close a properties object.
1341 * Deallocates the properties object and everything it contains.
1342 * \public \memberof mlt_properties_s
1343 * \param self a properties object
1346 void mlt_properties_close( mlt_properties self )
1348 if ( self != NULL && mlt_properties_dec_ref( self ) <= 0 )
1350 if ( self->close != NULL )
1352 self->close( self->close_object );
1356 property_list *list = self->local;
1359 #if _MLT_PROPERTY_CHECKS_ == 1
1361 mlt_properties_debug( self, "Closing", stderr );
1364 #ifdef _MLT_PROPERTY_CHECKS_
1365 // Increment destroyed count
1366 properties_destroyed ++;
1368 // Show current stats - these should match when the app is closed
1369 mlt_log( NULL, MLT_LOG_DEBUG, "Created %d, destroyed %d\n", properties_created, properties_destroyed );
1372 // Clean up names and values
1373 for ( index = list->count - 1; index >= 0; index -- )
1375 mlt_property_close( list->value[ index ] );
1376 free( list->name[ index ] );
1379 #if defined(__linux__) || defined(__DARWIN__)
1382 freelocale( list->locale );
1385 // Clear up the list
1386 pthread_mutex_destroy( &list->mutex );
1388 free( list->value );
1391 // Free self now if self has no child
1392 if ( self->child == NULL )
1398 /** Determine if the properties list is really just a sequence or ordered list.
1400 * \public \memberof mlt_properties_s
1401 * \param properties a properties list
1402 * \return true if all of the property names are numeric (a sequence)
1405 int mlt_properties_is_sequence( mlt_properties properties )
1408 int n = mlt_properties_count( properties );
1409 for ( i = 0; i < n; i++ )
1410 if ( ! isdigit( mlt_properties_get_name( properties, i )[0] ) )
1415 /** \brief YAML Tiny Parser context structure
1417 * YAML is a nifty text format popular in the Ruby world as a cleaner,
1418 * less verbose alternative to XML. See this Wikipedia topic for an overview:
1419 * http://en.wikipedia.org/wiki/YAML
1420 * The YAML specification is at:
1422 * YAML::Tiny is a Perl module that specifies a subset of YAML that we are
1423 * using here (for the same reasons):
1424 * http://search.cpan.org/~adamk/YAML-Tiny-1.25/lib/YAML/Tiny.pm
1428 struct yaml_parser_context
1433 mlt_deque index_stack;
1436 unsigned int block_indent;
1439 typedef struct yaml_parser_context *yaml_parser;
1441 /** Remove spaces from the left side of a string.
1443 * \param s the string to trim
1444 * \return the number of characters removed
1447 static unsigned int ltrim( char **s )
1451 int n = strlen( c );
1452 for ( i = 0; i < n && *c == ' '; i++, c++ );
1457 /** Remove spaces from the right side of a string.
1459 * \param s the string to trim
1460 * \return the number of characters removed
1463 static unsigned int rtrim( char *s )
1465 int n = strlen( s );
1467 for ( i = n; i > 0 && s[i - 1] == ' '; --i )
1472 /** Parse a line of YAML Tiny.
1474 * Adds a property if needed.
1475 * \private \memberof yaml_parser_context
1476 * \param context a YAML Tiny Parser context
1477 * \param namevalue a line of YAML Tiny
1478 * \return true if there was an error
1481 static int parse_yaml( yaml_parser context, const char *namevalue )
1483 char *name_ = strdup( namevalue );
1487 char *ptr = strchr( name, ':' );
1488 unsigned int indent = ltrim( &name );
1489 mlt_properties properties = mlt_deque_peek_back( context->stack );
1491 // Ascending one more levels in the tree
1492 if ( indent < context->level )
1495 unsigned int n = ( context->level - indent ) / 2;
1496 for ( i = 0; i < n; i++ )
1498 mlt_deque_pop_back( context->stack );
1499 context->index = mlt_deque_pop_back_int( context->index_stack );
1501 properties = mlt_deque_peek_back( context->stack );
1502 context->level = indent;
1505 // Descending a level in the tree
1506 else if ( indent > context->level && context->block == 0 )
1508 context->level = indent;
1511 // If there is a colon that is not part of a block
1512 if ( ptr && ( indent == context->level ) )
1514 // Reset block processing
1515 if ( context->block_name )
1517 free( context->block_name );
1518 context->block_name = NULL;
1522 // Terminate the name and setup the value pointer
1526 char *comment = strchr( ptr, '#' );
1532 // Trim leading and trailing spaces from bare value
1536 // No value means a child
1537 if ( strcmp( ptr, "" ) == 0 )
1539 mlt_properties child = mlt_properties_new();
1540 mlt_properties_set_lcnumeric( child, mlt_properties_get_lcnumeric( properties ) );
1541 mlt_properties_set_data( properties, name, child, 0,
1542 ( mlt_destructor )mlt_properties_close, NULL );
1543 mlt_deque_push_back( context->stack, child );
1544 mlt_deque_push_back_int( context->index_stack, context->index );
1550 // A dash indicates a sequence item
1551 if ( name[0] == '-' )
1553 mlt_properties child = mlt_properties_new();
1556 mlt_properties_set_lcnumeric( child, mlt_properties_get_lcnumeric( properties ) );
1557 snprintf( key, sizeof(key), "%d", context->index++ );
1558 mlt_properties_set_data( properties, key, child, 0,
1559 ( mlt_destructor )mlt_properties_close, NULL );
1560 mlt_deque_push_back( context->stack, child );
1561 mlt_deque_push_back_int( context->index_stack, context->index );
1564 context->level += ltrim( &name ) + 1;
1572 value = strdup( ptr );
1573 if ( value && value[ strlen( value ) - 1 ] == '\"' )
1574 value[ strlen( value ) - 1 ] = 0;
1577 // Value is folded or unfolded block
1578 else if ( *ptr == '|' || *ptr == '>' )
1580 context->block = *ptr;
1581 context->block_name = strdup( name );
1582 context->block_indent = 0;
1583 value = strdup( "" );
1589 value = strdup( ptr );
1593 // A list of scalars
1594 else if ( name[0] == '-' )
1596 // Reset block processing
1597 if ( context->block_name )
1599 free( context->block_name );
1600 context->block_name = NULL;
1606 snprintf( key, sizeof(key), "%d", context->index++ );
1610 char *comment = strchr( ptr, '#' );
1614 // Trim leading and trailing spaces from bare value
1622 value = strdup( ptr );
1623 if ( value && value[ strlen( value ) - 1 ] == '\"' )
1624 value[ strlen( value ) - 1 ] = 0;
1627 // Value is folded or unfolded block
1628 else if ( *ptr == '|' || *ptr == '>' )
1630 context->block = *ptr;
1631 context->block_name = strdup( key );
1632 context->block_indent = 0;
1633 value = strdup( "" );
1639 value = strdup( ptr );
1643 name = name_ = strdup( key );
1647 else if ( context->block == '|' )
1649 if ( context->block_indent == 0 )
1650 context->block_indent = indent;
1651 if ( indent > context->block_indent )
1652 name = &name_[ context->block_indent ];
1654 char *old_value = mlt_properties_get( properties, context->block_name );
1655 value = calloc( 1, strlen( old_value ) + strlen( name ) + 2 );
1656 strcpy( value, old_value );
1657 if ( strcmp( old_value, "" ) )
1658 strcat( value, "\n" );
1659 strcat( value, name );
1660 name = context->block_name;
1664 else if ( context->block == '>' )
1668 char *old_value = mlt_properties_get( properties, context->block_name );
1670 // Blank line (prepended with spaces) is new line
1671 if ( strcmp( name, "" ) == 0 )
1673 value = calloc( 1, strlen( old_value ) + 2 );
1674 strcat( value, old_value );
1675 strcat( value, "\n" );
1677 // Concatenate with space
1680 value = calloc( 1, strlen( old_value ) + strlen( name ) + 2 );
1681 strcat( value, old_value );
1682 if ( strcmp( old_value, "" ) && old_value[ strlen( old_value ) - 1 ] != '\n' )
1683 strcat( value, " " );
1684 strcat( value, name );
1686 name = context->block_name;
1691 value = strdup( "" );
1694 error = mlt_properties_set( properties, name, value );
1696 if ( !strcmp( name, "LC_NUMERIC" ) )
1697 mlt_properties_set_lcnumeric( properties, value );
1705 /** Parse a YAML Tiny file by name.
1707 * \public \memberof mlt_properties_s
1708 * \param filename the name of a text file containing YAML Tiny
1709 * \return a new properties list
1712 mlt_properties mlt_properties_parse_yaml( const char *filename )
1714 // Construct a standalone properties object
1715 mlt_properties self = mlt_properties_new( );
1720 FILE *file = fopen( filename, "r" );
1722 // Load contents of file
1727 char *ptemp = &temp[ 0 ];
1729 // Default to LC_NUMERIC = C
1730 mlt_properties_set_lcnumeric( self, "C" );
1733 yaml_parser context = calloc( 1, sizeof( struct yaml_parser_context ) );
1734 context->stack = mlt_deque_init();
1735 context->index_stack = mlt_deque_init();
1736 mlt_deque_push_back( context->stack, self );
1737 mlt_deque_push_back_int( context->index_stack, 0 );
1739 // Read each string from the file
1740 while( fgets( temp, 1024, file ) )
1742 // Check for end-of-stream
1743 if ( strncmp( ptemp, "...", 3 ) == 0 )
1747 temp[ strlen( temp ) - 1 ] = '\0';
1749 // Skip blank lines, comment lines, and document separator
1750 if ( strcmp( ptemp, "" ) && ptemp[ 0 ] != '#' && strncmp( ptemp, "---", 3 )
1751 && strncmp( ptemp, "%YAML", 5 ) && strncmp( ptemp, "% YAML", 6 ) )
1752 parse_yaml( context, temp );
1757 mlt_deque_close( context->stack );
1758 mlt_deque_close( context->index_stack );
1759 if ( context->block_name )
1760 free( context->block_name );
1765 // Return the pointer
1770 * YAML Tiny Serializer
1773 /** How many bytes to grow at a time */
1774 #define STRBUF_GROWTH (1024)
1776 /** \brief Private to mlt_properties_s, a self-growing buffer for building strings
1786 typedef struct strbuf_s *strbuf;
1788 /** Create a new string buffer
1790 * \private \memberof strbuf_s
1791 * \return a new string buffer
1794 static strbuf strbuf_new( )
1796 strbuf buffer = calloc( 1, sizeof( struct strbuf_s ) );
1797 buffer->size = STRBUF_GROWTH;
1798 buffer->string = calloc( 1, buffer->size );
1802 /** Destroy a string buffer
1804 * \private \memberof strbuf_s
1805 * \param buffer the string buffer to close
1808 static void strbuf_close( strbuf buffer )
1810 // We do not free buffer->string; strbuf user must save that pointer
1816 /** Format a string into a string buffer
1818 * A variable number of arguments follows the format string - one for each
1820 * \private \memberof strbuf_s
1821 * \param buffer the string buffer to write into
1822 * \param format a string that contains text and formatting instructions
1823 * \return the formatted string
1826 static char *strbuf_printf( strbuf buffer, const char *format, ... )
1828 while ( buffer->string )
1831 va_start( ap, format );
1832 size_t len = strlen( buffer->string );
1833 size_t remain = buffer->size - len - 1;
1834 int need = vsnprintf( buffer->string + len, remain, format, ap );
1836 if ( need > -1 && need < remain )
1838 buffer->string[ len ] = 0;
1839 buffer->size += need + STRBUF_GROWTH;
1840 buffer->string = realloc( buffer->string, buffer->size );
1842 return buffer->string;
1845 /** Indent a line of YAML Tiny.
1847 * \private \memberof strbuf_s
1848 * \param output a string buffer
1849 * \param indent the number of spaces to indent
1852 static inline void indent_yaml( strbuf output, int indent )
1855 for ( j = 0; j < indent; j++ )
1856 strbuf_printf( output, " " );
1859 static void strbuf_escape( strbuf output, const char *value, char c )
1861 char *v = strdup( value );
1863 char *found = strchr( s, c );
1868 strbuf_printf( output, "%s\\%c", s, c );
1870 found = strchr( s, c );
1872 strbuf_printf( output, "%s", s );
1876 /** Convert a line string into a YAML block literal.
1878 * \private \memberof strbuf_s
1879 * \param output a string buffer
1880 * \param value the string to format as a block literal
1881 * \param indent the number of spaces to indent
1884 static void output_yaml_block_literal( strbuf output, const char *value, int indent )
1886 char *v = strdup( value );
1888 char *eol = strchr( sol, '\n' );
1892 indent_yaml( output, indent );
1894 strbuf_printf( output, "%s\n", sol );
1896 eol = strchr( sol, '\n' );
1898 indent_yaml( output, indent );
1899 strbuf_printf( output, "%s\n", sol );
1903 /** Recursively serialize a properties list into a string buffer as YAML Tiny.
1905 * \private \memberof mlt_properties_s
1906 * \param self a properties list
1907 * \param output a string buffer to hold the serialized YAML Tiny
1908 * \param indent the number of spaces to indent (for recursion, initialize to 0)
1909 * \param is_parent_sequence Is this properties list really just a sequence (for recursion, initialize to 0)?
1912 static void serialise_yaml( mlt_properties self, strbuf output, int indent, int is_parent_sequence )
1914 property_list *list = self->local;
1917 for ( i = 0; i < list->count; i ++ )
1919 // This implementation assumes that all data elements are property lists.
1920 // Unfortunately, we do not have run time type identification.
1921 mlt_properties child = mlt_property_get_data( list->value[ i ], NULL );
1923 if ( mlt_properties_is_sequence( self ) )
1925 // Ignore hidden/non-serialisable items
1926 if ( list->name[ i ][ 0 ] != '_' )
1928 // Indicate a sequence item
1929 indent_yaml( output, indent );
1930 strbuf_printf( output, "- " );
1932 // If the value can be represented as a string
1933 const char *value = mlt_properties_get( self, list->name[ i ] );
1934 if ( value && strcmp( value, "" ) )
1936 // Determine if this is an unfolded block literal
1937 if ( strchr( value, '\n' ) )
1939 strbuf_printf( output, "|\n" );
1940 output_yaml_block_literal( output, value, indent + strlen( list->name[ i ] ) + strlen( "|" ) );
1942 else if ( strchr( value, ':' ) || strchr( value, '[' ) )
1944 strbuf_printf( output, "\"" );
1945 strbuf_escape( output, value, '"' );
1946 strbuf_printf( output, "\"\n", value );
1950 strbuf_printf( output, "%s\n", value );
1956 serialise_yaml( child, output, indent + 2, 1 );
1960 // Assume this is a normal map-oriented properties list
1961 const char *value = mlt_properties_get( self, list->name[ i ] );
1963 // Ignore hidden/non-serialisable items
1964 // If the value can be represented as a string
1965 if ( list->name[ i ][ 0 ] != '_' && value && strcmp( value, "" ) )
1967 if ( is_parent_sequence == 0 )
1968 indent_yaml( output, indent );
1970 is_parent_sequence = 0;
1972 // Determine if this is an unfolded block literal
1973 if ( strchr( value, '\n' ) )
1975 strbuf_printf( output, "%s: |\n", list->name[ i ] );
1976 output_yaml_block_literal( output, value, indent + strlen( list->name[ i ] ) + strlen( ": " ) );
1978 else if ( strchr( value, ':' ) || strchr( value, '[' ) )
1980 strbuf_printf( output, "%s: \"", list->name[ i ] );
1981 strbuf_escape( output, value, '"' );
1982 strbuf_printf( output, "\"\n" );
1986 strbuf_printf( output, "%s: %s\n", list->name[ i ], value );
1990 // Output a child as a map item
1993 indent_yaml( output, indent );
1994 strbuf_printf( output, "%s:\n", list->name[ i ] );
1997 serialise_yaml( child, output, indent + 2, 0 );
2003 /** Serialize a properties list as a string of YAML Tiny.
2005 * The caller MUST free the returned string!
2006 * This operates on properties containing properties as a hierarchical data
2008 * \public \memberof mlt_properties_s
2009 * \param self a properties list
2010 * \return a string containing YAML Tiny that represents the properties list
2013 char *mlt_properties_serialise_yaml( mlt_properties self )
2015 if ( !self ) return NULL;
2016 const char *lc_numeric = mlt_properties_get_lcnumeric( self );
2017 strbuf b = strbuf_new();
2018 strbuf_printf( b, "---\n" );
2019 mlt_properties_set_lcnumeric( self, "C" );
2020 serialise_yaml( self, b, 0, 0 );
2021 mlt_properties_set_lcnumeric( self, lc_numeric );
2022 strbuf_printf( b, "...\n" );
2023 char *ret = b->string;
2028 /** Protect a properties list against concurrent access.
2030 * \public \memberof mlt_properties_s
2031 * \param self a properties list
2034 void mlt_properties_lock( mlt_properties self )
2037 pthread_mutex_lock( &( ( property_list* )( self->local ) )->mutex );
2040 /** End protecting a properties list against concurrent access.
2042 * \public \memberof mlt_properties_s
2043 * \param self a properties list
2046 void mlt_properties_unlock( mlt_properties self )
2049 pthread_mutex_unlock( &( ( property_list* )( self->local ) )->mutex );
2052 /** Get a time string associated to the name.
2054 * Do not free the returned string. It's lifetime is controlled by the property.
2055 * \public \memberof mlt_properties_s
2056 * \param self a properties list
2057 * \param name the property to get
2058 * \param format the time format that you want
2059 * \return the property's time value or NULL if \p name does not exist or there is no profile
2062 char *mlt_properties_get_time( mlt_properties self, const char* name, mlt_time_format format )
2064 mlt_profile profile = mlt_properties_get_data( self, "_profile", NULL );
2067 double fps = mlt_profile_fps( profile );
2068 mlt_property value = mlt_properties_find( self, name );
2069 property_list *list = self->local;
2070 return value == NULL ? NULL : mlt_property_get_time( value, format, fps, list->locale );
2075 /** Convert a numeric property to a tuple of color components.
2077 * If the property's string is red, green, blue, white, or black, then it
2078 * is converted to the corresponding opaque color tuple. Otherwise, the property
2079 * is fetched as an integer and then converted.
2080 * \public \memberof mlt_properties_s
2081 * \param self a properties list
2082 * \param name the property to get
2083 * \return a color structure
2086 mlt_color mlt_properties_get_color( mlt_properties self, const char* name )
2088 mlt_profile profile = mlt_properties_get_data( self, "_profile", NULL );
2089 double fps = mlt_profile_fps( profile );
2090 property_list *list = self->local;
2091 mlt_property value = mlt_properties_find( self, name );
2092 mlt_color result = { 0xff, 0xff, 0xff, 0xff };
2095 const char *color = mlt_property_get_string_l( value, list->locale );
2096 unsigned int color_int = mlt_property_get_int( value, fps, list->locale );
2098 if ( !strcmp( color, "red" ) )
2104 else if ( !strcmp( color, "green" ) )
2110 else if ( !strcmp( color, "blue" ) )
2116 else if ( !strcmp( color, "black" ) )
2122 else if ( strcmp( color, "white" ) )
2124 result.r = ( color_int >> 24 ) & 0xff;
2125 result.g = ( color_int >> 16 ) & 0xff;
2126 result.b = ( color_int >> 8 ) & 0xff;
2127 result.a = ( color_int ) & 0xff;
2133 /** Set a property to an integer value by color.
2135 * \public \memberof mlt_properties_s
2136 * \param self a properties list
2137 * \param name the property to set
2138 * \param color the color
2139 * \return true if error
2142 int mlt_properties_set_color( mlt_properties self, const char *name, mlt_color color )
2146 if ( !self || !name ) return error;
2148 // Fetch the property to work with
2149 mlt_property property = mlt_properties_fetch( self, name );
2151 // Set it if not NULL
2152 if ( property != NULL )
2154 uint32_t value = ( color.r << 24 ) | ( color.g << 16 ) | ( color.b << 8 ) | color.a;
2155 error = mlt_property_set_int( property, value );
2156 mlt_properties_do_mirror( self, name );
2159 mlt_events_fire( self, "property-changed", name, NULL );
2164 /** Get a string value by name at a frame position.
2166 * Do not free the returned string. It's lifetime is controlled by the property
2167 * and this properties object.
2168 * \public \memberof mlt_properties_s
2169 * \param self a properties list
2170 * \param name the property to get
2171 * \param position the frame number
2172 * \param length the maximum number of frames when interpreting negative keyframe times,
2173 * <=0 if you don't care or need that
2174 * \return the property's string value or NULL if it does not exist
2177 char* mlt_properties_anim_get( mlt_properties self, const char *name, int position, int length )
2179 mlt_profile profile = mlt_properties_get_data( self, "_profile", NULL );
2180 double fps = mlt_profile_fps( profile );
2181 mlt_property value = mlt_properties_find( self, name );
2182 property_list *list = self->local;
2183 return value == NULL ? NULL : mlt_property_anim_get_string( value, fps, list->locale, position, length );
2186 /** Set a property to a string at a frame position.
2188 * The event "property-changed" is fired after the property has been set.
2190 * This makes a copy of the string value you supply.
2191 * \public \memberof mlt_properties_s
2192 * \param self a properties list
2193 * \param name the property to set
2194 * \param value the property's new value
2195 * \param position the frame number
2196 * \param length the maximum number of frames when interpreting negative keyframe times,
2197 * <=0 if you don't care or need that
2198 * \return true if error
2201 int mlt_properties_anim_set( mlt_properties self, const char *name, const char *value, int position, int length )
2205 if ( !self || !name ) return error;
2207 // Fetch the property to work with
2208 mlt_property property = mlt_properties_fetch( self, name );
2210 // Set it if not NULL
2213 mlt_profile profile = mlt_properties_get_data( self, "_profile", NULL );
2214 double fps = mlt_profile_fps( profile );
2215 property_list *list = self->local;
2216 error = mlt_property_anim_set_string( property, value,
2217 fps, list->locale, position, length );
2218 mlt_properties_do_mirror( self, name );
2221 mlt_events_fire( self, "property-changed", name, NULL );
2226 /** Get an integer associated to the name at a frame position.
2228 * \public \memberof mlt_properties_s
2229 * \param self a properties list
2230 * \param name the property to get
2231 * \param position the frame number
2232 * \param length the maximum number of frames when interpreting negative keyframe times,
2233 * <=0 if you don't care or need that
2234 * \return the integer value, 0 if not found (which may also be a legitimate value)
2237 int mlt_properties_anim_get_int( mlt_properties self, const char *name, int position, int length )
2239 mlt_profile profile = mlt_properties_get_data( self, "_profile", NULL );
2240 double fps = mlt_profile_fps( profile );
2241 property_list *list = self->local;
2242 mlt_property value = mlt_properties_find( self, name );
2243 return value == NULL ? 0 : mlt_property_anim_get_int( value, fps, list->locale, position, length );
2246 /** Set a property to an integer value at a frame position.
2248 * \public \memberof mlt_properties_s
2249 * \param self a properties list
2250 * \param name the property to set
2251 * \param value the integer
2252 * \param position the frame number
2253 * \param length the maximum number of frames when interpreting negative keyframe times,
2254 * <=0 if you don't care or need that
2255 * \param keyframe_type the interpolation method for this keyframe
2256 * \return true if error
2259 int mlt_properties_anim_set_int( mlt_properties self, const char *name, int value,
2260 int position, int length, mlt_keyframe_type keyframe_type )
2264 if ( !self || !name ) return error;
2266 // Fetch the property to work with
2267 mlt_property property = mlt_properties_fetch( self, name );
2269 // Set it if not NULL
2270 if ( property != NULL )
2272 mlt_profile profile = mlt_properties_get_data( self, "_profile", NULL );
2273 double fps = mlt_profile_fps( profile );
2274 property_list *list = self->local;
2275 error = mlt_property_anim_set_int( property, value, fps, list->locale, position, length, keyframe_type );
2276 mlt_properties_do_mirror( self, name );
2279 mlt_events_fire( self, "property-changed", name, NULL );
2284 /** Get a real number associated to the name at a frame position.
2286 * \public \memberof mlt_properties_s
2287 * \param self a properties list
2288 * \param name the property to get
2289 * \param position the frame number
2290 * \param length the maximum number of frames when interpreting negative keyframe times,
2291 * <=0 if you don't care or need that
2292 * \return the real number, 0 if not found (which may also be a legitimate value)
2295 double mlt_properties_anim_get_double( mlt_properties self, const char *name, int position, int length )
2297 mlt_profile profile = mlt_properties_get_data( self, "_profile", NULL );
2298 double fps = mlt_profile_fps( profile );
2299 property_list *list = self->local;
2300 mlt_property value = mlt_properties_find( self, name );
2301 return value == NULL ? 0.0 : mlt_property_anim_get_double( value, fps, list->locale, position, length );
2304 /** Set a property to a real number at a frame position.
2306 * \public \memberof mlt_properties_s
2307 * \param self a properties list
2308 * \param name the property to set
2309 * \param value the real number
2310 * \param position the frame number
2311 * \param length the maximum number of frames when interpreting negative keyframe times,
2312 * <=0 if you don't care or need that
2313 * \param keyframe_type the interpolation method for this keyframe
2314 * \return true if error
2317 int mlt_properties_anim_set_double( mlt_properties self, const char *name, double value,
2318 int position, int length, mlt_keyframe_type keyframe_type )
2322 if ( !self || !name ) return error;
2324 // Fetch the property to work with
2325 mlt_property property = mlt_properties_fetch( self, name );
2327 // Set it if not NULL
2328 if ( property != NULL )
2330 mlt_profile profile = mlt_properties_get_data( self, "_profile", NULL );
2331 double fps = mlt_profile_fps( profile );
2332 property_list *list = self->local;
2333 error = mlt_property_anim_set_double( property, value, fps, list->locale, position, length, keyframe_type );
2334 mlt_properties_do_mirror( self, name );
2337 mlt_events_fire( self, "property-changed", name, NULL );
2342 /** Get the animation associated to the name.
2344 * \public \memberof mlt_properties_s
2345 * \param self a properties list
2346 * \param name the property to get
2347 * \return The animation object or NULL if the property has no animation
2350 mlt_animation mlt_properties_get_animation( mlt_properties self, const char *name )
2352 mlt_property value = mlt_properties_find( self, name );
2353 return value == NULL ? NULL : mlt_property_get_animation( value );
2356 /** Set a property to a rectangle value.
2358 * \public \memberof mlt_properties_s
2359 * \param self a properties list
2360 * \param name the property to set
2361 * \param value the rectangle
2362 * \return true if error
2365 extern int mlt_properties_set_rect( mlt_properties self, const char *name, mlt_rect value )
2369 if ( !self || !name ) return error;
2371 // Fetch the property to work with
2372 mlt_property property = mlt_properties_fetch( self, name );
2374 // Set it if not NULL
2375 if ( property != NULL )
2377 error = mlt_property_set_rect( property, value );
2378 mlt_properties_do_mirror( self, name );
2381 mlt_events_fire( self, "property-changed", name, NULL );
2386 /** Get a rectangle associated to the name.
2388 * \public \memberof mlt_properties_s
2389 * \param self a properties list
2390 * \param name the property to get
2391 * \return the rectangle value, the rectangle fields will be DBL_MIN if not found
2394 extern mlt_rect mlt_properties_get_rect( mlt_properties self, const char* name )
2396 property_list *list = self->local;
2397 mlt_property value = mlt_properties_find( self, name );
2398 mlt_rect rect = { DBL_MIN, DBL_MIN, DBL_MIN, DBL_MIN, DBL_MIN };
2399 return value == NULL ? rect : mlt_property_get_rect( value, list->locale );
2402 /** Set a property to a rectangle value at a frame position.
2404 * \public \memberof mlt_properties_s
2405 * \param self a properties list
2406 * \param name the property to set
2407 * \param value the rectangle
2408 * \param position the frame number
2409 * \param length the maximum number of frames when interpreting negative keyframe times,
2410 * <=0 if you don't care or need that
2411 * \param keyframe_type the interpolation method for this keyframe
2412 * \return true if error
2415 extern int mlt_properties_anim_set_rect( mlt_properties self, const char *name, mlt_rect value,
2416 int position, int length , mlt_keyframe_type keyframe_type )
2420 if ( !self || !name ) return error;
2422 // Fetch the property to work with
2423 mlt_property property = mlt_properties_fetch( self, name );
2425 // Set it if not NULL
2426 if ( property != NULL )
2428 mlt_profile profile = mlt_properties_get_data( self, "_profile", NULL );
2429 double fps = mlt_profile_fps( profile );
2430 property_list *list = self->local;
2431 error = mlt_property_anim_set_rect( property, value, fps, list->locale, position, length, keyframe_type );
2432 mlt_properties_do_mirror( self, name );
2435 mlt_events_fire( self, "property-changed", name, NULL );
2440 /** Get a rectangle associated to the name at a frame position.
2442 * \public \memberof mlt_properties_s
2443 * \param self a properties list
2444 * \param name the property to get
2445 * \param position the frame number
2446 * \param length the maximum number of frames when interpreting negative keyframe times,
2447 * <=0 if you don't care or need that
2448 * \return the rectangle value, the rectangle fields will be DBL_MIN if not found
2451 extern mlt_rect mlt_properties_anim_get_rect( mlt_properties self, const char *name, int position, int length )
2453 mlt_profile profile = mlt_properties_get_data( self, "_profile", NULL );
2454 double fps = mlt_profile_fps( profile );
2455 property_list *list = self->local;
2456 mlt_property value = mlt_properties_find( self, name );
2457 mlt_rect rect = { DBL_MIN, DBL_MIN, DBL_MIN, DBL_MIN, DBL_MIN };
2458 return value == NULL ? rect : mlt_property_anim_get_rect( value, fps, list->locale, position, length );