+
+ // Free self now if self has no child
+ if ( self->child == NULL )
+ free( self );
+ }
+ }
+}
+
+/** Determine if the properties list is really just a sequence or ordered list.
+ *
+ * \public \memberof mlt_properties_s
+ * \param properties a properties list
+ * \return true if all of the property names are numeric (a sequence)
+ */
+
+int mlt_properties_is_sequence( mlt_properties properties )
+{
+ int i;
+ int n = mlt_properties_count( properties );
+ for ( i = 0; i < n; i++ )
+ if ( ! isdigit( mlt_properties_get_name( properties, i )[0] ) )
+ return 0;
+ return 1;
+}
+
+/** \brief YAML Tiny Parser context structure
+ *
+ * YAML is a nifty text format popular in the Ruby world as a cleaner,
+ * less verbose alternative to XML. See this Wikipedia topic for an overview:
+ * http://en.wikipedia.org/wiki/YAML
+ * The YAML specification is at:
+ * http://yaml.org/
+ * YAML::Tiny is a Perl module that specifies a subset of YAML that we are
+ * using here (for the same reasons):
+ * http://search.cpan.org/~adamk/YAML-Tiny-1.25/lib/YAML/Tiny.pm
+ * \private
+ */
+
+struct yaml_parser_context
+{
+ mlt_deque stack;
+ unsigned int level;
+ int index;
+ mlt_deque index_stack;
+ char block;
+ char *block_name;
+ unsigned int block_indent;
+
+};
+typedef struct yaml_parser_context *yaml_parser;
+
+/** Remove spaces from the left side of a string.
+ *
+ * \param s the string to trim
+ * \return the number of characters removed
+ */
+
+static unsigned int ltrim( char **s )
+{
+ unsigned int i = 0;
+ char *c = *s;
+ int n = strlen( c );
+ for ( i = 0; i < n && *c == ' '; i++, c++ );
+ *s = c;
+ return i;
+}
+
+/** Remove spaces from the right side of a string.
+ *
+ * \param s the string to trim
+ * \return the number of characters removed
+ */
+
+static unsigned int rtrim( char *s )
+{
+ int n = strlen( s );
+ int i;
+ for ( i = n; i > 0 && s[i - 1] == ' '; --i )
+ s[i - 1] = 0;
+ return n - i;
+}
+
+/** Parse a line of YAML Tiny.
+ *
+ * Adds a property if needed.
+ * \private \memberof yaml_parser_context
+ * \param context a YAML Tiny Parser context
+ * \param namevalue a line of YAML Tiny
+ * \return true if there was an error
+ */
+
+static int parse_yaml( yaml_parser context, const char *namevalue )
+{
+ char *name_ = strdup( namevalue );
+ char *name = name_;
+ char *value = NULL;
+ int error = 0;
+ char *ptr = strchr( name, ':' );
+ unsigned int indent = ltrim( &name );
+ mlt_properties properties = mlt_deque_peek_back( context->stack );
+
+ // Ascending one more levels in the tree
+ if ( indent < context->level )
+ {
+ unsigned int i;
+ unsigned int n = ( context->level - indent ) / 2;
+ for ( i = 0; i < n; i++ )
+ {
+ mlt_deque_pop_back( context->stack );
+ context->index = mlt_deque_pop_back_int( context->index_stack );
+ }
+ properties = mlt_deque_peek_back( context->stack );
+ context->level = indent;
+ }
+
+ // Descending a level in the tree
+ else if ( indent > context->level && context->block == 0 )
+ {
+ context->level = indent;
+ }
+
+ // If there is a colon that is not part of a block
+ if ( ptr && ( indent == context->level ) )
+ {
+ // Reset block processing
+ if ( context->block_name )
+ {
+ free( context->block_name );
+ context->block_name = NULL;
+ context->block = 0;
+ }
+
+ // Terminate the name and setup the value pointer
+ *( ptr ++ ) = 0;
+
+ // Trim comment
+ char *comment = strchr( ptr, '#' );
+ if ( comment )
+ {
+ *comment = 0;
+ }
+
+ // Trim leading and trailing spaces from bare value
+ ltrim( &ptr );
+ rtrim( ptr );
+
+ // No value means a child
+ if ( strcmp( ptr, "" ) == 0 )
+ {
+ mlt_properties child = mlt_properties_new();
+ mlt_properties_set_lcnumeric( child, mlt_properties_get_lcnumeric( properties ) );
+ mlt_properties_set_data( properties, name, child, 0,
+ ( mlt_destructor )mlt_properties_close, NULL );
+ mlt_deque_push_back( context->stack, child );
+ mlt_deque_push_back_int( context->index_stack, context->index );
+ context->index = 0;
+ free( name_ );
+ return error;
+ }
+
+ // A dash indicates a sequence item
+ if ( name[0] == '-' )
+ {
+ mlt_properties child = mlt_properties_new();
+ char key[20];
+
+ mlt_properties_set_lcnumeric( child, mlt_properties_get_lcnumeric( properties ) );
+ snprintf( key, sizeof(key), "%d", context->index++ );
+ mlt_properties_set_data( properties, key, child, 0,
+ ( mlt_destructor )mlt_properties_close, NULL );
+ mlt_deque_push_back( context->stack, child );
+ mlt_deque_push_back_int( context->index_stack, context->index );
+
+ name ++;
+ context->level += ltrim( &name ) + 1;
+ properties = child;
+ }
+
+ // Value is quoted
+ if ( *ptr == '\"' )
+ {
+ ptr ++;
+ value = strdup( ptr );
+ if ( value && value[ strlen( value ) - 1 ] == '\"' )
+ value[ strlen( value ) - 1 ] = 0;
+ }
+
+ // Value is folded or unfolded block
+ else if ( *ptr == '|' || *ptr == '>' )
+ {
+ context->block = *ptr;
+ context->block_name = strdup( name );
+ context->block_indent = 0;
+ value = strdup( "" );
+ }
+
+ // Bare value
+ else
+ {
+ value = strdup( ptr );
+ }
+ }
+
+ // A list of scalars
+ else if ( name[0] == '-' )
+ {
+ // Reset block processing
+ if ( context->block_name )
+ {
+ free( context->block_name );
+ context->block_name = NULL;
+ context->block = 0;
+ }
+
+ char key[20];
+
+ snprintf( key, sizeof(key), "%d", context->index++ );
+ ptr = name + 1;
+
+ // Trim comment
+ char *comment = strchr( ptr, '#' );
+ if ( comment )
+ *comment = 0;
+
+ // Trim leading and trailing spaces from bare value
+ ltrim( &ptr );
+ rtrim( ptr );
+
+ // Value is quoted
+ if ( *ptr == '\"' )
+ {
+ ptr ++;
+ value = strdup( ptr );
+ if ( value && value[ strlen( value ) - 1 ] == '\"' )
+ value[ strlen( value ) - 1 ] = 0;
+ }
+
+ // Value is folded or unfolded block
+ else if ( *ptr == '|' || *ptr == '>' )
+ {
+ context->block = *ptr;
+ context->block_name = strdup( key );
+ context->block_indent = 0;
+ value = strdup( "" );
+ }
+
+ // Bare value
+ else
+ {
+ value = strdup( ptr );
+ }
+
+ free( name_ );
+ name = name_ = strdup( key );
+ }
+
+ // Non-folded block
+ else if ( context->block == '|' )
+ {
+ if ( context->block_indent == 0 )
+ context->block_indent = indent;
+ if ( indent > context->block_indent )
+ name = &name_[ context->block_indent ];
+ rtrim( name );
+ char *old_value = mlt_properties_get( properties, context->block_name );
+ value = calloc( 1, strlen( old_value ) + strlen( name ) + 2 );
+ strcpy( value, old_value );
+ if ( strcmp( old_value, "" ) )
+ strcat( value, "\n" );
+ strcat( value, name );
+ name = context->block_name;
+ }
+
+ // Folded block
+ else if ( context->block == '>' )
+ {
+ ltrim( &name );
+ rtrim( name );
+ char *old_value = mlt_properties_get( properties, context->block_name );
+
+ // Blank line (prepended with spaces) is new line
+ if ( strcmp( name, "" ) == 0 )
+ {
+ value = calloc( 1, strlen( old_value ) + 2 );
+ strcat( value, old_value );
+ strcat( value, "\n" );
+ }
+ // Concatenate with space
+ else
+ {
+ value = calloc( 1, strlen( old_value ) + strlen( name ) + 2 );
+ strcat( value, old_value );
+ if ( strcmp( old_value, "" ) && old_value[ strlen( old_value ) - 1 ] != '\n' )
+ strcat( value, " " );
+ strcat( value, name );
+ }
+ name = context->block_name;
+ }
+
+ else
+ {
+ value = strdup( "" );
+ }
+
+ error = mlt_properties_set( properties, name, value );
+
+ if ( !strcmp( name, "LC_NUMERIC" ) )
+ mlt_properties_set_lcnumeric( properties, value );
+
+ free( name_ );
+ free( value );
+
+ return error;
+}
+
+/** Parse a YAML Tiny file by name.
+ *
+ * \public \memberof mlt_properties_s
+ * \param filename the name of a text file containing YAML Tiny
+ * \return a new properties list
+ */
+
+mlt_properties mlt_properties_parse_yaml( const char *filename )
+{
+ // Construct a standalone properties object
+ mlt_properties self = mlt_properties_new( );
+
+ if ( self )
+ {
+ // Open the file
+ FILE *file = fopen( filename, "r" );
+
+ // Load contents of file
+ if ( file )
+ {
+ // Temp string
+ char temp[ 1024 ];
+ char *ptemp = &temp[ 0 ];
+
+ // Default to LC_NUMERIC = C
+ mlt_properties_set_lcnumeric( self, "C" );
+
+ // Parser context
+ yaml_parser context = calloc( 1, sizeof( struct yaml_parser_context ) );
+ context->stack = mlt_deque_init();
+ context->index_stack = mlt_deque_init();
+ mlt_deque_push_back( context->stack, self );
+ mlt_deque_push_back_int( context->index_stack, 0 );
+
+ // Read each string from the file
+ while( fgets( temp, 1024, file ) )
+ {
+ // Check for end-of-stream
+ if ( strncmp( ptemp, "...", 3 ) == 0 )
+ break;
+
+ // Chomp the string
+ temp[ strlen( temp ) - 1 ] = '\0';
+
+ // Skip blank lines, comment lines, and document separator
+ if ( strcmp( ptemp, "" ) && ptemp[ 0 ] != '#' && strncmp( ptemp, "---", 3 )
+ && strncmp( ptemp, "%YAML", 5 ) && strncmp( ptemp, "% YAML", 6 ) )
+ parse_yaml( context, temp );
+ }
+
+ // Close the file
+ fclose( file );
+ mlt_deque_close( context->stack );
+ mlt_deque_close( context->index_stack );
+ if ( context->block_name )
+ free( context->block_name );
+ free( context );
+ }
+ }
+
+ // Return the pointer
+ return self;
+}
+
+/*
+ * YAML Tiny Serializer
+ */
+
+/** How many bytes to grow at a time */
+#define STRBUF_GROWTH (1024)
+
+/** \brief Private to mlt_properties_s, a self-growing buffer for building strings
+ * \private
+ */
+
+struct strbuf_s
+{
+ size_t size;
+ char *string;
+};
+
+typedef struct strbuf_s *strbuf;
+
+/** Create a new string buffer
+ *
+ * \private \memberof strbuf_s
+ * \return a new string buffer
+ */
+
+static strbuf strbuf_new( )
+{
+ strbuf buffer = calloc( 1, sizeof( struct strbuf_s ) );
+ buffer->size = STRBUF_GROWTH;
+ buffer->string = calloc( 1, buffer->size );
+ return buffer;
+}
+
+/** Destroy a string buffer
+ *
+ * \private \memberof strbuf_s
+ * \param buffer the string buffer to close
+ */
+
+static void strbuf_close( strbuf buffer )
+{
+ // We do not free buffer->string; strbuf user must save that pointer
+ // and free it.
+ if ( buffer )
+ free( buffer );
+}
+
+/** Format a string into a string buffer
+ *
+ * A variable number of arguments follows the format string - one for each
+ * format specifier.
+ * \private \memberof strbuf_s
+ * \param buffer the string buffer to write into
+ * \param format a string that contains text and formatting instructions
+ * \return the formatted string
+ */
+
+static char *strbuf_printf( strbuf buffer, const char *format, ... )
+{
+ while ( buffer->string )
+ {
+ va_list ap;
+ va_start( ap, format );
+ size_t len = strlen( buffer->string );
+ size_t remain = buffer->size - len - 1;
+ int need = vsnprintf( buffer->string + len, remain, format, ap );
+ va_end( ap );
+ if ( need > -1 && need < remain )
+ break;
+ buffer->string[ len ] = 0;
+ buffer->size += need + STRBUF_GROWTH;
+ buffer->string = realloc( buffer->string, buffer->size );
+ }
+ return buffer->string;
+}
+
+/** Indent a line of YAML Tiny.
+ *
+ * \private \memberof strbuf_s
+ * \param output a string buffer
+ * \param indent the number of spaces to indent
+ */
+
+static inline void indent_yaml( strbuf output, int indent )
+{
+ int j;
+ for ( j = 0; j < indent; j++ )
+ strbuf_printf( output, " " );
+}
+
+static void strbuf_escape( strbuf output, const char *value, char c )
+{
+ char *v = strdup( value );
+ char *s = v;
+ char *found = strchr( s, c );
+
+ while ( found )
+ {
+ *found = '\0';
+ strbuf_printf( output, "%s\\%c", s, c );
+ s = found + 1;
+ found = strchr( s, c );
+ }
+ strbuf_printf( output, "%s", s );
+ free( v );
+}
+
+/** Convert a line string into a YAML block literal.
+ *
+ * \private \memberof strbuf_s
+ * \param output a string buffer
+ * \param value the string to format as a block literal
+ * \param indent the number of spaces to indent
+ */
+
+static void output_yaml_block_literal( strbuf output, const char *value, int indent )
+{
+ char *v = strdup( value );
+ char *sol = v;
+ char *eol = strchr( sol, '\n' );
+
+ while ( eol )
+ {
+ indent_yaml( output, indent );
+ *eol = '\0';
+ strbuf_printf( output, "%s\n", sol );
+ sol = eol + 1;
+ eol = strchr( sol, '\n' );
+ }
+ indent_yaml( output, indent );
+ strbuf_printf( output, "%s\n", sol );
+ free( v );
+}
+
+/** Recursively serialize a properties list into a string buffer as YAML Tiny.
+ *
+ * \private \memberof mlt_properties_s
+ * \param self a properties list
+ * \param output a string buffer to hold the serialized YAML Tiny
+ * \param indent the number of spaces to indent (for recursion, initialize to 0)
+ * \param is_parent_sequence Is this properties list really just a sequence (for recursion, initialize to 0)?
+ */
+
+static void serialise_yaml( mlt_properties self, strbuf output, int indent, int is_parent_sequence )
+{
+ property_list *list = self->local;
+ int i = 0;
+
+ for ( i = 0; i < list->count; i ++ )
+ {
+ // This implementation assumes that all data elements are property lists.
+ // Unfortunately, we do not have run time type identification.
+ mlt_properties child = mlt_property_get_data( list->value[ i ], NULL );
+
+ if ( mlt_properties_is_sequence( self ) )
+ {
+ // Ignore hidden/non-serialisable items
+ if ( list->name[ i ][ 0 ] != '_' )
+ {
+ // Indicate a sequence item
+ indent_yaml( output, indent );
+ strbuf_printf( output, "- " );
+
+ // If the value can be represented as a string
+ const char *value = mlt_properties_get( self, list->name[ i ] );
+ if ( value && strcmp( value, "" ) )
+ {
+ // Determine if this is an unfolded block literal
+ if ( strchr( value, '\n' ) )
+ {
+ strbuf_printf( output, "|\n" );
+ output_yaml_block_literal( output, value, indent + strlen( list->name[ i ] ) + strlen( "|" ) );
+ }
+ else if ( strchr( value, ':' ) || strchr( value, '[' ) )
+ {
+ strbuf_printf( output, "\"" );
+ strbuf_escape( output, value, '"' );
+ strbuf_printf( output, "\"\n", value );
+ }
+ else
+ {
+ strbuf_printf( output, "%s\n", value );
+ }
+ }
+ }
+ // Recurse on child
+ if ( child )
+ serialise_yaml( child, output, indent + 2, 1 );
+ }
+ else
+ {
+ // Assume this is a normal map-oriented properties list
+ const char *value = mlt_properties_get( self, list->name[ i ] );
+
+ // Ignore hidden/non-serialisable items
+ // If the value can be represented as a string
+ if ( list->name[ i ][ 0 ] != '_' && value && strcmp( value, "" ) )
+ {
+ if ( is_parent_sequence == 0 )
+ indent_yaml( output, indent );
+ else
+ is_parent_sequence = 0;
+
+ // Determine if this is an unfolded block literal
+ if ( strchr( value, '\n' ) )
+ {
+ strbuf_printf( output, "%s: |\n", list->name[ i ] );
+ output_yaml_block_literal( output, value, indent + strlen( list->name[ i ] ) + strlen( ": " ) );
+ }
+ else if ( strchr( value, ':' ) || strchr( value, '[' ) )
+ {
+ strbuf_printf( output, "%s: \"", list->name[ i ] );
+ strbuf_escape( output, value, '"' );
+ strbuf_printf( output, "\"\n" );
+ }
+ else
+ {
+ strbuf_printf( output, "%s: %s\n", list->name[ i ], value );
+ }
+ }
+
+ // Output a child as a map item
+ if ( child )
+ {
+ indent_yaml( output, indent );
+ strbuf_printf( output, "%s:\n", list->name[ i ] );
+
+ // Recurse on child
+ serialise_yaml( child, output, indent + 2, 0 );
+ }
+ }
+ }
+}
+
+/** Serialize a properties list as a string of YAML Tiny.
+ *
+ * The caller MUST free the returned string!
+ * This operates on properties containing properties as a hierarchical data
+ * structure.
+ * \public \memberof mlt_properties_s
+ * \param self a properties list
+ * \return a string containing YAML Tiny that represents the properties list
+ */
+
+char *mlt_properties_serialise_yaml( mlt_properties self )
+{
+ if ( !self ) return NULL;
+ const char *lc_numeric = mlt_properties_get_lcnumeric( self );
+ strbuf b = strbuf_new();
+ strbuf_printf( b, "---\n" );
+ mlt_properties_set_lcnumeric( self, "C" );
+ serialise_yaml( self, b, 0, 0 );
+ mlt_properties_set_lcnumeric( self, lc_numeric );
+ strbuf_printf( b, "...\n" );
+ char *ret = b->string;
+ strbuf_close( b );
+ return ret;
+}
+
+/** Protect a properties list against concurrent access.
+ *
+ * \public \memberof mlt_properties_s
+ * \param self a properties list
+ */
+
+void mlt_properties_lock( mlt_properties self )
+{
+ if ( self )
+ pthread_mutex_lock( &( ( property_list* )( self->local ) )->mutex );
+}
+
+/** End protecting a properties list against concurrent access.
+ *
+ * \public \memberof mlt_properties_s
+ * \param self a properties list
+ */
+
+void mlt_properties_unlock( mlt_properties self )
+{
+ if ( self )
+ pthread_mutex_unlock( &( ( property_list* )( self->local ) )->mutex );
+}
+
+/** Get a time string associated to the name.
+ *
+ * Do not free the returned string. It's lifetime is controlled by the property.
+ * \public \memberof mlt_properties_s
+ * \param self a properties list
+ * \param name the property to get
+ * \param format the time format that you want
+ * \return the property's time value or NULL if \p name does not exist or there is no profile
+ */
+
+char *mlt_properties_get_time( mlt_properties self, const char* name, mlt_time_format format )
+{
+ mlt_profile profile = mlt_properties_get_data( self, "_profile", NULL );
+ if ( profile )
+ {
+ double fps = mlt_profile_fps( profile );
+ mlt_property value = mlt_properties_find( self, name );
+ property_list *list = self->local;
+ return value == NULL ? NULL : mlt_property_get_time( value, format, fps, list->locale );
+ }
+ return NULL;
+}
+
+/** Convert a frame count to a time string.
+ *
+ * Do not free the returned string. It's lifetime is controlled by the property.
+ * \public \memberof mlt_properties_s
+ * \param self a properties list
+ * \param frames the frame count to convert
+ * \param format the time format that you want
+ * \return the time string or NULL if error, e.g. there is no profile
+ */
+
+char *mlt_properties_frames_to_time( mlt_properties self, mlt_position frames, mlt_time_format format )
+{
+ const char *name = "_mlt_properties_time";
+ mlt_properties_set_position( self, name, frames );
+ return mlt_properties_get_time( self, name, format );
+}
+
+/** Convert a time string to a frame count.
+ *
+ * \public \memberof mlt_properties_s
+ * \param self a properties list
+ * \param time the time string to convert
+ * \return a frame count or a negative value if error, e.g. there is no profile
+ */
+
+mlt_position mlt_properties_time_to_frames( mlt_properties self, const char *time )
+{
+ const char *name = "_mlt_properties_time";
+ mlt_properties_set( self, name, time );
+ return mlt_properties_get_position( self, name );
+}
+
+/** Convert a numeric property to a tuple of color components.
+ *
+ * If the property's string is red, green, blue, white, or black, then it
+ * is converted to the corresponding opaque color tuple. Otherwise, the property
+ * is fetched as an integer and then converted.
+ * \public \memberof mlt_properties_s
+ * \param self a properties list
+ * \param name the property to get
+ * \return a color structure
+ */
+
+mlt_color mlt_properties_get_color( mlt_properties self, const char* name )
+{
+ mlt_profile profile = mlt_properties_get_data( self, "_profile", NULL );
+ double fps = mlt_profile_fps( profile );
+ property_list *list = self->local;
+ mlt_property value = mlt_properties_find( self, name );
+ mlt_color result = { 0xff, 0xff, 0xff, 0xff };
+ if ( value )
+ {
+ const char *color = mlt_property_get_string_l( value, list->locale );
+ unsigned int color_int = mlt_property_get_int( value, fps, list->locale );
+
+ if ( !strcmp( color, "red" ) )
+ {
+ result.r = 0xff;
+ result.g = 0x00;
+ result.b = 0x00;
+ }
+ else if ( !strcmp( color, "green" ) )
+ {
+ result.r = 0x00;
+ result.g = 0xff;
+ result.b = 0x00;
+ }
+ else if ( !strcmp( color, "blue" ) )
+ {
+ result.r = 0x00;
+ result.g = 0x00;
+ result.b = 0xff;
+ }
+ else if ( !strcmp( color, "black" ) )
+ {
+ result.r = 0x00;
+ result.g = 0x00;
+ result.b = 0x00;
+ }
+ else if ( strcmp( color, "white" ) )
+ {
+ result.r = ( color_int >> 24 ) & 0xff;
+ result.g = ( color_int >> 16 ) & 0xff;
+ result.b = ( color_int >> 8 ) & 0xff;
+ result.a = ( color_int ) & 0xff;