]> git.sesse.net Git - mlt/blobdiff - src/framework/mlt_frame.c
make mlt_position type double
[mlt] / src / framework / mlt_frame.c
index 546d5b478e17a0ad2e84d72b341a2cf161857d30..7c8b5f83145a30a26cf429b223d955f9ea3c5905 100644 (file)
 mlt_frame mlt_frame_init( mlt_service service )
 {
        // Allocate a frame
-       mlt_frame this = calloc( sizeof( struct mlt_frame_s ), 1 );
+       mlt_frame self = calloc( 1, sizeof( struct mlt_frame_s ) );
 
-       if ( this != NULL )
+       if ( self != NULL )
        {
                mlt_profile profile = mlt_service_profile( service );
 
                // Initialise the properties
-               mlt_properties properties = &this->parent;
-               mlt_properties_init( properties, this );
+               mlt_properties properties = &self->parent;
+               mlt_properties_init( properties, self );
 
                // Set default properties on the frame
                mlt_properties_set_position( properties, "_position", 0.0 );
                mlt_properties_set_data( properties, "image", NULL, 0, NULL, NULL );
                mlt_properties_set_int( properties, "width", profile? profile->width : 720 );
                mlt_properties_set_int( properties, "height", profile? profile->height : 576 );
-               mlt_properties_set_int( properties, "normalised_width", profile? profile->width : 720 );
-               mlt_properties_set_int( properties, "normalised_height", profile? profile->height : 576 );
                mlt_properties_set_double( properties, "aspect_ratio", mlt_profile_sar( NULL ) );
                mlt_properties_set_data( properties, "audio", NULL, 0, NULL, NULL );
                mlt_properties_set_data( properties, "alpha", NULL, 0, NULL, NULL );
 
                // Construct stacks for frames and methods
-               this->stack_image = mlt_deque_init( );
-               this->stack_audio = mlt_deque_init( );
-               this->stack_service = mlt_deque_init( );
+               self->stack_image = mlt_deque_init( );
+               self->stack_audio = mlt_deque_init( );
+               self->stack_service = mlt_deque_init( );
        }
 
-       return this;
+       return self;
 }
 
 /** Get a frame's properties.
  *
  * \public \memberof mlt_frame_s
- * \param this a frame
+ * \param self a frame
  * \return the frame's properties or NULL if an invalid frame is supplied
  */
 
-mlt_properties mlt_frame_properties( mlt_frame this )
+mlt_properties mlt_frame_properties( mlt_frame self )
 {
-       return this != NULL ? &this->parent : NULL;
+       return self != NULL ? &self->parent : NULL;
 }
 
 /** Determine if the frame will produce a test card image.
  *
  * \public \memberof mlt_frame_s
- * \param this a frame
+ * \param self a frame
  * \return true (non-zero) if this will produce from a test card
  */
 
-int mlt_frame_is_test_card( mlt_frame this )
+int mlt_frame_is_test_card( mlt_frame self )
 {
-       return mlt_deque_count( this->stack_image ) == 0 || mlt_properties_get_int( MLT_FRAME_PROPERTIES( this ), "test_image" );
+       return mlt_deque_count( self->stack_image ) == 0 || mlt_properties_get_int( MLT_FRAME_PROPERTIES( self ), "test_image" );
 }
 
 /** Determine if the frame will produce audio from a test card.
  *
  * \public \memberof mlt_frame_s
- * \param this a frame
+ * \param self a frame
  * \return true (non-zero) if this will produce from a test card
  */
 
-int mlt_frame_is_test_audio( mlt_frame this )
+int mlt_frame_is_test_audio( mlt_frame self )
 {
-       return mlt_deque_count( this->stack_audio ) == 0 || mlt_properties_get_int( MLT_FRAME_PROPERTIES( this ), "test_audio" );
+       return mlt_deque_count( self->stack_audio ) == 0 || mlt_properties_get_int( MLT_FRAME_PROPERTIES( self ), "test_audio" );
 }
 
 /** Get the sample aspect ratio of the frame.
  *
  * \public \memberof  mlt_frame_s
- * \param this a frame
+ * \param self a frame
  * \return the aspect ratio
  */
 
-double mlt_frame_get_aspect_ratio( mlt_frame this )
+double mlt_frame_get_aspect_ratio( mlt_frame self )
 {
-       return mlt_properties_get_double( MLT_FRAME_PROPERTIES( this ), "aspect_ratio" );
+       return mlt_properties_get_double( MLT_FRAME_PROPERTIES( self ), "aspect_ratio" );
 }
 
 /** Set the sample aspect ratio of the frame.
  *
  * \public \memberof mlt_frame_s
- * \param this a frame
+ * \param self a frame
  * \param value the new image sample aspect ratio
  * \return true if error
  */
 
-int mlt_frame_set_aspect_ratio( mlt_frame this, double value )
+int mlt_frame_set_aspect_ratio( mlt_frame self, double value )
 {
-       return mlt_properties_set_double( MLT_FRAME_PROPERTIES( this ), "aspect_ratio", value );
+       return mlt_properties_set_double( MLT_FRAME_PROPERTIES( self ), "aspect_ratio", value );
 }
 
 /** Get the time position of this frame.
+ *
+ * This position is not necessarily the position as the original
+ * producer knows it. It could be the position that the playlist,
+ * multitrack, or tractor producer set.
+ *
+ * \public \memberof mlt_frame_s
+ * \param self a frame
+ * \return the position
+ * \see mlt_frame_original_position
+ */
+
+mlt_position mlt_frame_get_position( mlt_frame self )
+{
+       int pos = mlt_properties_get_position( MLT_FRAME_PROPERTIES( self ), "_position" );
+       return pos < 0 ? 0 : pos;
+}
+
+/** Get the original time position of this frame.
+ *
+ * This is the position that the original producer set on the frame.
  *
  * \public \memberof mlt_frame_s
- * \param this a frame
+ * \param self a frame
  * \return the position
  */
 
-mlt_position mlt_frame_get_position( mlt_frame this )
+mlt_position mlt_frame_original_position( mlt_frame self )
 {
-       int pos = mlt_properties_get_position( MLT_FRAME_PROPERTIES( this ), "_position" );
+       int pos = mlt_properties_get_position( MLT_FRAME_PROPERTIES( self ), "original_position" );
        return pos < 0 ? 0 : pos;
 }
 
 /** Set the time position of this frame.
  *
  * \public \memberof mlt_frame_s
- * \param this a frame
+ * \param self a frame
  * \param value the position
  * \return true if error
  */
 
-int mlt_frame_set_position( mlt_frame this, mlt_position value )
+int mlt_frame_set_position( mlt_frame self, mlt_position value )
 {
-       return mlt_properties_set_position( MLT_FRAME_PROPERTIES( this ), "_position", value );
+       // Only set the original_position the first time.
+       if ( ! mlt_properties_get( MLT_FRAME_PROPERTIES( self ), "original_position" ) )
+               mlt_properties_set_position( MLT_FRAME_PROPERTIES( self ), "original_position", value );
+       return mlt_properties_set_position( MLT_FRAME_PROPERTIES( self ), "_position", value );
 }
 
 /** Stack a get_image callback.
  *
  * \public \memberof mlt_frame_s
- * \param this a frame
+ * \param self a frame
  * \param the get_image callback
  * \return true if error
  */
 
-int mlt_frame_push_get_image( mlt_frame this, mlt_get_image get_image )
+int mlt_frame_push_get_image( mlt_frame self, mlt_get_image get_image )
 {
-       return mlt_deque_push_back( this->stack_image, get_image );
+       return mlt_deque_push_back( self->stack_image, get_image );
 }
 
 /** Pop a get_image callback.
  *
  * \public \memberof mlt_frame_s
- * \param this a frame
+ * \param self a frame
  * \return the get_image callback
  */
 
-mlt_get_image mlt_frame_pop_get_image( mlt_frame this )
+mlt_get_image mlt_frame_pop_get_image( mlt_frame self )
 {
-       return mlt_deque_pop_back( this->stack_image );
+       return mlt_deque_pop_back( self->stack_image );
 }
 
 /** Push a frame.
  *
  * \public \memberof mlt_frame_s
- * \param this a frame
- * \param that the frame to push onto \p this
+ * \param self a frame
+ * \param that the frame to push onto \p self
  * \return true if error
  */
 
-int mlt_frame_push_frame( mlt_frame this, mlt_frame that )
+int mlt_frame_push_frame( mlt_frame self, mlt_frame that )
 {
-       return mlt_deque_push_back( this->stack_image, that );
+       return mlt_deque_push_back( self->stack_image, that );
 }
 
 /** Pop a frame.
  *
  * \public \memberof mlt_frame_s
- * \param this a frame
+ * \param self a frame
  * \return a frame that was previously pushed
  */
 
-mlt_frame mlt_frame_pop_frame( mlt_frame this )
+mlt_frame mlt_frame_pop_frame( mlt_frame self )
 {
-       return mlt_deque_pop_back( this->stack_image );
+       return mlt_deque_pop_back( self->stack_image );
 }
 
 /** Push a service.
  *
  * \public \memberof mlt_frame_s
- * \param this a frame
+ * \param self a frame
  * \param that an opaque pointer
  * \return true if error
  */
 
-int mlt_frame_push_service( mlt_frame this, void *that )
+int mlt_frame_push_service( mlt_frame self, void *that )
 {
-       return mlt_deque_push_back( this->stack_image, that );
+       return mlt_deque_push_back( self->stack_image, that );
 }
 
 /** Pop a service.
  *
  * \public \memberof mlt_frame_s
- * \param this a frame
+ * \param self a frame
  * \return an opaque pointer to something previously pushed
  */
 
-void *mlt_frame_pop_service( mlt_frame this )
+void *mlt_frame_pop_service( mlt_frame self )
 {
-       return mlt_deque_pop_back( this->stack_image );
+       return mlt_deque_pop_back( self->stack_image );
 }
 
 /** Push a number.
  *
  * \public \memberof mlt_frame_s
- * \param this a frame
+ * \param self a frame
  * \param that an integer
  * \return true if error
  */
 
-int mlt_frame_push_service_int( mlt_frame this, int that )
+int mlt_frame_push_service_int( mlt_frame self, int that )
 {
-       return mlt_deque_push_back_int( this->stack_image, that );
+       return mlt_deque_push_back_int( self->stack_image, that );
 }
 
 /** Pop a number.
  *
  * \public \memberof mlt_frame_s
- * \param this a frame
+ * \param self a frame
  * \return an integer that was previously pushed
  */
 
-int mlt_frame_pop_service_int( mlt_frame this )
+int mlt_frame_pop_service_int( mlt_frame self )
 {
-       return mlt_deque_pop_back_int( this->stack_image );
+       return mlt_deque_pop_back_int( self->stack_image );
 }
 
 /** Push an audio item on the stack.
  *
  * \public \memberof mlt_frame_s
- * \param this a frame
+ * \param self a frame
  * \param that an opaque pointer
  * \return true if error
  */
 
-int mlt_frame_push_audio( mlt_frame this, void *that )
+int mlt_frame_push_audio( mlt_frame self, void *that )
 {
-       return mlt_deque_push_back( this->stack_audio, that );
+       return mlt_deque_push_back( self->stack_audio, that );
 }
 
 /** Pop an audio item from the stack
  *
  * \public \memberof mlt_frame_s
- * \param this a frame
+ * \param self a frame
  * \return an opaque pointer to something that was pushed onto the frame's audio stack
  */
 
-void *mlt_frame_pop_audio( mlt_frame this )
+void *mlt_frame_pop_audio( mlt_frame self )
 {
-       return mlt_deque_pop_back( this->stack_audio );
+       return mlt_deque_pop_back( self->stack_audio );
 }
 
 /** Return the service stack
  *
  * \public \memberof mlt_frame_s
- * \param this a frame
+ * \param self a frame
  * \return the service stack
  */
 
-mlt_deque mlt_frame_service_stack( mlt_frame this )
+mlt_deque mlt_frame_service_stack( mlt_frame self )
 {
-       return this->stack_service;
+       return self->stack_service;
+}
+
+/** Set a new image on the frame.
+  *
+  * \public \memberof mlt_frame_s
+  * \param self a frame
+  * \param image a pointer to the raw image data
+  * \param size the size of the image data in bytes (optional)
+  * \param destroy a function to deallocate \p image when the frame is closed (optional)
+  * \return true if error
+  */
+
+int mlt_frame_set_image( mlt_frame self, uint8_t *image, int size, mlt_destructor destroy )
+{
+       return mlt_properties_set_data( MLT_FRAME_PROPERTIES( self ), "image", image, size, destroy, NULL );
+}
+
+/** Set a new alpha channel on the frame.
+  *
+  * \public \memberof mlt_frame_s
+  * \param self a frame
+  * \param alpha a pointer to the alpha channel
+  * \param size the size of the alpha channel in bytes (optional)
+  * \param destroy a function to deallocate \p alpha when the frame is closed (optional)
+  * \return true if error
+  */
+
+int mlt_frame_set_alpha( mlt_frame self, uint8_t *alpha, int size, mlt_destructor destroy )
+{
+       self->get_alpha_mask = NULL;
+       return mlt_properties_set_data( MLT_FRAME_PROPERTIES( self ), "alpha", alpha, size, destroy, NULL );
 }
 
 /** Replace image stack with the information provided.
@@ -314,24 +366,24 @@ mlt_deque mlt_frame_service_stack( mlt_frame this )
  * and the upper tracks should simply not be invited to stack...
  *
  * \public \memberof mlt_frame_s
- * \param this a frame
+ * \param self a frame
  * \param image a new image
  * \param format the image format
  * \param width the width of the new image
  * \param height the height of the new image
  */
 
-void mlt_frame_replace_image( mlt_frame this, uint8_t *image, mlt_image_format format, int width, int height )
+void mlt_frame_replace_image( mlt_frame self, uint8_t *image, mlt_image_format format, int width, int height )
 {
        // Remove all items from the stack
-       while( mlt_deque_pop_back( this->stack_image ) ) ;
+       while( mlt_deque_pop_back( self->stack_image ) ) ;
 
        // Update the information
-       mlt_properties_set_data( MLT_FRAME_PROPERTIES( this ), "image", image, 0, NULL, NULL );
-       mlt_properties_set_int( MLT_FRAME_PROPERTIES( this ), "width", width );
-       mlt_properties_set_int( MLT_FRAME_PROPERTIES( this ), "height", height );
-       mlt_properties_set_int( MLT_FRAME_PROPERTIES( this ), "format", format );
-       this->get_alpha_mask = NULL;
+       mlt_properties_set_data( MLT_FRAME_PROPERTIES( self ), "image", image, 0, NULL, NULL );
+       mlt_properties_set_int( MLT_FRAME_PROPERTIES( self ), "width", width );
+       mlt_properties_set_int( MLT_FRAME_PROPERTIES( self ), "height", height );
+       mlt_properties_set_int( MLT_FRAME_PROPERTIES( self ), "format", format );
+       self->get_alpha_mask = NULL;
 }
 
 /** Get the short name for an image format.
@@ -351,92 +403,79 @@ const char * mlt_image_format_name( mlt_image_format format )
                case mlt_image_yuv422:  return "yuv422";
                case mlt_image_yuv420p: return "yuv420p";
                case mlt_image_opengl:  return "opengl";
+               case mlt_image_glsl:    return "glsl";
+               case mlt_image_glsl_texture: return "glsl_texture";
        }
        return "invalid";
 }
 
-/** Get the image associated to the frame.
- *
- * You should express the desired format, width, and height as inputs. As long
- * as the loader producer was used to generate this or the imageconvert filter
- * was attached, then you will get the image back in the format you desire.
- * However, you do not always get the width and height you request depending
- * on properties and filters. You do not need to supply a pre-allocated
- * buffer, but you should always supply the desired image format.
- *
- * \public \memberof mlt_frame_s
- * \param this a frame
- * \param[out] buffer an image buffer
- * \param[in,out] format the image format
- * \param[in,out] width the horizontal size in pixels
- * \param[in,out] height the vertical size in pixels
- * \param writable whether or not you will need to be able to write to the memory returned in \p buffer
- * \return true if error
- * \todo Better describe the width and height as inputs.
- */
+/** Get the number of bytes needed for an image.
+  *
+  * \public \memberof mlt_frame_s
+  * \param format the image format
+  * \param width width of the image in pixels
+  * \param height height of the image in pixels
+  * \param[out] bpp the number of bytes per pixel (optional)
+  * \return the number of bytes
+  */
+int mlt_image_format_size( mlt_image_format format, int width, int height, int *bpp )
+{
+       height += 1;
+       switch ( format )
+       {
+               case mlt_image_rgb24:
+                       if ( bpp ) *bpp = 3;
+                       return width * height * 3;
+               case mlt_image_opengl:
+               case mlt_image_rgb24a:
+                       if ( bpp ) *bpp = 4;
+                       return width * height * 4;
+               case mlt_image_yuv422:
+                       if ( bpp ) *bpp = 2;
+                       return width * height * 2;
+               case mlt_image_yuv420p:
+                       if ( bpp ) *bpp = 3 / 2;
+                       return width * height * 3 / 2;
+               default:
+                       if ( bpp ) *bpp = 0;
+                       return 0;
+       }
+       return 0;
+}
 
-int mlt_frame_get_image( mlt_frame this, uint8_t **buffer, mlt_image_format *format, int *width, int *height, int writable )
+static int generate_test_image( mlt_properties properties, uint8_t **buffer,  mlt_image_format *format, int *width, int *height, int writable )
 {
-       mlt_properties properties = MLT_FRAME_PROPERTIES( this );
-       mlt_get_image get_image = mlt_frame_pop_get_image( this );
        mlt_producer producer = mlt_properties_get_data( properties, "test_card_producer", NULL );
        mlt_image_format requested_format = *format;
-       int error = 0;
+       int error = 1;
 
-       if ( get_image )
-       {
-               mlt_properties_set_int( properties, "image_count", mlt_properties_get_int( properties, "image_count" ) - 1 );
-               error = get_image( this, buffer, format, width, height, writable );
-               if ( !error && *buffer )
-               {
-                       mlt_properties_set_int( properties, "width", *width );
-                       mlt_properties_set_int( properties, "height", *height );
-                       mlt_properties_set_int( properties, "format", *format );
-                       if ( this->convert_image )
-                               this->convert_image( this, buffer, format, requested_format );
-               }
-               else
-               {
-                       // Cause the image to be loaded from test card or fallback (white) below.
-                       mlt_frame_get_image( this, buffer, format, width, height, writable );
-               }
-       }
-       else if ( mlt_properties_get_data( properties, "image", NULL ) )
-       {
-               *format = mlt_properties_get_int( properties, "format" );
-               *buffer = mlt_properties_get_data( properties, "image", NULL );
-               *width = mlt_properties_get_int( properties, "width" );
-               *height = mlt_properties_get_int( properties, "height" );
-               if ( this->convert_image && *buffer )
-                       this->convert_image( this, buffer, format, requested_format );
-       }
-       else if ( producer )
+       if ( producer )
        {
                mlt_frame test_frame = NULL;
                mlt_service_get_frame( MLT_PRODUCER_SERVICE( producer ), &test_frame, 0 );
                if ( test_frame )
                {
                        mlt_properties test_properties = MLT_FRAME_PROPERTIES( test_frame );
-                       mlt_properties_set_double( test_properties, "consumer_aspect_ratio", mlt_properties_get_double( properties, "consumer_aspect_ratio" ) );
-                       mlt_properties_set( test_properties, "rescale.interp", mlt_properties_get( properties, "rescale.interp" ) );
-                       mlt_frame_get_image( test_frame, buffer, format, width, height, writable );
                        mlt_properties_set_data( properties, "test_card_frame", test_frame, 0, ( mlt_destructor )mlt_frame_close, NULL );
-                       mlt_properties_set_double( properties, "aspect_ratio", mlt_frame_get_aspect_ratio( test_frame ) );
-//                     mlt_properties_set_data( properties, "image", *buffer, *width * *height * 2, NULL, NULL );
-//                     mlt_properties_set_int( properties, "width", *width );
-//                     mlt_properties_set_int( properties, "height", *height );
-//                     mlt_properties_set_int( properties, "format", *format );
+                       mlt_properties_set( test_properties, "rescale.interp", mlt_properties_get( properties, "rescale.interp" ) );
+                       error = mlt_frame_get_image( test_frame, buffer, format, width, height, writable );
+                       if ( !error && buffer && *buffer )
+                       {
+                               mlt_properties_set_double( properties, "aspect_ratio", mlt_frame_get_aspect_ratio( test_frame ) );
+                               mlt_properties_set_int( properties, "width", *width );
+                               mlt_properties_set_int( properties, "height", *height );
+                               if ( test_frame->convert_image && requested_format != mlt_image_none )
+                                       test_frame->convert_image( test_frame, buffer, format, requested_format );
+                               mlt_properties_set_int( properties, "format", *format );
+                       }
                }
                else
                {
                        mlt_properties_set_data( properties, "test_card_producer", NULL, 0, NULL, NULL );
-                       mlt_frame_get_image( this, buffer, format, width, height, writable );
                }
        }
-       else
+       if ( error && buffer && *format != mlt_image_none )
        {
-               register uint8_t *p;
-               register uint8_t *q;
                int size = 0;
 
                *width = *width == 0 ? 720 : *width;
@@ -446,14 +485,10 @@ int mlt_frame_get_image( mlt_frame this, uint8_t **buffer, mlt_image_format *for
                mlt_properties_set_int( properties, "format", *format );
                mlt_properties_set_int( properties, "width", *width );
                mlt_properties_set_int( properties, "height", *height );
-               mlt_properties_set_int( properties, "aspect_ratio", 0 );
+               mlt_properties_set_double( properties, "aspect_ratio", 1.0 );
 
                switch( *format )
                {
-                       case mlt_image_none:
-                               size = 0;
-                               *buffer = NULL;
-                               break;
                        case mlt_image_rgb24:
                                size *= 3;
                                size += *width * 3;
@@ -473,28 +508,97 @@ int mlt_frame_get_image( mlt_frame this, uint8_t **buffer, mlt_image_format *for
                                size *= 2;
                                size += *width * 2;
                                *buffer = mlt_pool_alloc( size );
-                               p = *buffer;
-                               q = p + size;
-                               while ( p != NULL && p != q )
+                               if ( *buffer )
                                {
-                                       *p ++ = 235;
-                                       *p ++ = 128;
+                                       register uint8_t *p = *buffer;
+                                       register uint8_t *q = p + size;
+                                       while ( p != NULL && p != q )
+                                       {
+                                               *p ++ = 235;
+                                               *p ++ = 128;
+                                       }
                                }
                                break;
                        case mlt_image_yuv420p:
-                               size = size * 3 / 2;
-                               *buffer = mlt_pool_alloc( size );
+                               *buffer = mlt_pool_alloc( size * 3 / 2 );
                                if ( *buffer )
-                                       memset( *buffer, 255, size );
+                               {
+                                       memset( *buffer, 235, size );
+                                       memset( *buffer + size, 128, size / 2 );
+                               }
+                               break;
+                       default:
+                               size = 0;
                                break;
                }
-
                mlt_properties_set_data( properties, "image", *buffer, size, ( mlt_destructor )mlt_pool_release, NULL );
                mlt_properties_set_int( properties, "test_image", 1 );
+               error = 0;
        }
+       return error;
+}
 
-       mlt_properties_set_int( properties, "scaled_width", *width );
-       mlt_properties_set_int( properties, "scaled_height", *height );
+
+/** Get the image associated to the frame.
+ *
+ * You should express the desired format, width, and height as inputs. As long
+ * as the loader producer was used to generate this or the imageconvert filter
+ * was attached, then you will get the image back in the format you desire.
+ * However, you do not always get the width and height you request depending
+ * on properties and filters. You do not need to supply a pre-allocated
+ * buffer, but you should always supply the desired image format.
+ *
+ * \public \memberof mlt_frame_s
+ * \param self a frame
+ * \param[out] buffer an image buffer
+ * \param[in,out] format the image format
+ * \param[in,out] width the horizontal size in pixels
+ * \param[in,out] height the vertical size in pixels
+ * \param writable whether or not you will need to be able to write to the memory returned in \p buffer
+ * \return true if error
+ * \todo Better describe the width and height as inputs.
+ */
+
+int mlt_frame_get_image( mlt_frame self, uint8_t **buffer, mlt_image_format *format, int *width, int *height, int writable )
+{
+       mlt_properties properties = MLT_FRAME_PROPERTIES( self );
+       mlt_get_image get_image = mlt_frame_pop_get_image( self );
+       mlt_image_format requested_format = *format;
+       int error = 0;
+
+       if ( get_image )
+       {
+               mlt_properties_set_int( properties, "image_count", mlt_properties_get_int( properties, "image_count" ) - 1 );
+               error = get_image( self, buffer, format, width, height, writable );
+               if ( !error && buffer && *buffer )
+               {
+                       mlt_properties_set_int( properties, "width", *width );
+                       mlt_properties_set_int( properties, "height", *height );
+                       if ( self->convert_image && requested_format != mlt_image_none )
+                               self->convert_image( self, buffer, format, requested_format );
+                       mlt_properties_set_int( properties, "format", *format );
+               }
+               else
+               {
+                       error = generate_test_image( properties, buffer, format, width, height, writable );
+               }
+       }
+       else if ( mlt_properties_get_data( properties, "image", NULL ) && buffer )
+       {
+               *format = mlt_properties_get_int( properties, "format" );
+               *buffer = mlt_properties_get_data( properties, "image", NULL );
+               *width = mlt_properties_get_int( properties, "width" );
+               *height = mlt_properties_get_int( properties, "height" );
+               if ( self->convert_image && *buffer && requested_format != mlt_image_none )
+               {
+                       self->convert_image( self, buffer, format, requested_format );
+                       mlt_properties_set_int( properties, "format", *format );
+               }
+       }
+       else
+       {
+               error = generate_test_image( properties, buffer, format, width, height, writable );
+       }
 
        return error;
 }
@@ -502,25 +606,25 @@ int mlt_frame_get_image( mlt_frame this, uint8_t **buffer, mlt_image_format *for
 /** Get the alpha channel associated to the frame.
  *
  * \public \memberof mlt_frame_s
- * \param this a frame
+ * \param self a frame
  * \return the alpha channel
  */
 
-uint8_t *mlt_frame_get_alpha_mask( mlt_frame this )
+uint8_t *mlt_frame_get_alpha_mask( mlt_frame self )
 {
        uint8_t *alpha = NULL;
-       if ( this != NULL )
+       if ( self != NULL )
        {
-               if ( this->get_alpha_mask != NULL )
-                       alpha = this->get_alpha_mask( this );
+               if ( self->get_alpha_mask != NULL )
+                       alpha = self->get_alpha_mask( self );
                if ( alpha == NULL )
-                       alpha = mlt_properties_get_data( &this->parent, "alpha", NULL );
+                       alpha = mlt_properties_get_data( &self->parent, "alpha", NULL );
                if ( alpha == NULL )
                {
-                       int size = mlt_properties_get_int( &this->parent, "scaled_width" ) * mlt_properties_get_int( &this->parent, "scaled_height" );
+                       int size = mlt_properties_get_int( &self->parent, "width" ) * mlt_properties_get_int( &self->parent, "height" );
                        alpha = mlt_pool_alloc( size );
                        memset( alpha, 255, size );
-                       mlt_properties_set_data( &this->parent, "alpha", alpha, size, mlt_pool_release, NULL );
+                       mlt_properties_set_data( &self->parent, "alpha", alpha, size, mlt_pool_release, NULL );
                }
        }
        return alpha;
@@ -530,8 +634,7 @@ uint8_t *mlt_frame_get_alpha_mask( mlt_frame this )
  *
  * You do not need to deallocate the returned string.
  * \public \memberof mlt_frame_s
- * \param this a frame
- * \param format an image format enum
+ * \param format an audio format enum
  * \return a string for the name of the image format
  */
 
@@ -542,11 +645,38 @@ const char * mlt_audio_format_name( mlt_audio_format format )
                case mlt_audio_none:   return "none";
                case mlt_audio_s16:    return "s16";
                case mlt_audio_s32:    return "s32";
+               case mlt_audio_s32le:  return "s32le";
                case mlt_audio_float:  return "float";
+               case mlt_audio_f32le:  return "f32le";
+               case mlt_audio_u8:     return "u8";
        }
        return "invalid";
 }
 
+/** Get the amount of bytes needed for a block of audio.
+  *
+  * \public \memberof mlt_frame_s
+  * \param format an audio format enum
+  * \param samples the number of samples per channel
+  * \param channels the number of channels
+  * \return the number of bytes
+  */
+
+int mlt_audio_format_size( mlt_audio_format format, int samples, int channels )
+{
+       switch ( format )
+       {
+               case mlt_audio_none:   return 0;
+               case mlt_audio_s16:    return samples * channels * sizeof( int16_t );
+               case mlt_audio_s32le:
+               case mlt_audio_s32:    return samples * channels * sizeof( int32_t );
+               case mlt_audio_f32le:
+               case mlt_audio_float:  return samples * channels * sizeof( float );
+               case mlt_audio_u8:     return samples * channels;
+       }
+       return 0;
+}
+
 /** Get the audio associated to the frame.
  *
  * You should express the desired format, frequency, channels, and samples as inputs. As long
@@ -559,7 +689,7 @@ const char * mlt_audio_format_name( mlt_audio_format format )
  * You should use the \p mlt_sample_calculator to determine the number of samples you want.
  *
  * \public \memberof mlt_frame_s
- * \param this a frame
+ * \param self a frame
  * \param[out] buffer an audio buffer
  * \param[in,out] format the audio format
  * \param[in,out] frequency the sample rate
@@ -568,22 +698,22 @@ const char * mlt_audio_format_name( mlt_audio_format format )
  * \return true if error
  */
 
-int mlt_frame_get_audio( mlt_frame this, void **buffer, mlt_audio_format *format, int *frequency, int *channels, int *samples )
+int mlt_frame_get_audio( mlt_frame self, void **buffer, mlt_audio_format *format, int *frequency, int *channels, int *samples )
 {
-       mlt_get_audio get_audio = mlt_frame_pop_audio( this );
-       mlt_properties properties = MLT_FRAME_PROPERTIES( this );
+       mlt_get_audio get_audio = mlt_frame_pop_audio( self );
+       mlt_properties properties = MLT_FRAME_PROPERTIES( self );
        int hide = mlt_properties_get_int( properties, "test_audio" );
        mlt_audio_format requested_format = *format;
 
        if ( hide == 0 && get_audio != NULL )
        {
-               get_audio( this, buffer, format, frequency, channels, samples );
+               get_audio( self, buffer, format, frequency, channels, samples );
                mlt_properties_set_int( properties, "audio_frequency", *frequency );
                mlt_properties_set_int( properties, "audio_channels", *channels );
                mlt_properties_set_int( properties, "audio_samples", *samples );
                mlt_properties_set_int( properties, "audio_format", *format );
-               if ( this->convert_audio )
-                       this->convert_audio( this, buffer, format, requested_format );
+               if ( self->convert_audio && *buffer && requested_format != mlt_audio_none )
+                       self->convert_audio( self, buffer, format, requested_format );
        }
        else if ( mlt_properties_get_data( properties, "audio", NULL ) )
        {
@@ -592,8 +722,8 @@ int mlt_frame_get_audio( mlt_frame this, void **buffer, mlt_audio_format *format
                *frequency = mlt_properties_get_int( properties, "audio_frequency" );
                *channels = mlt_properties_get_int( properties, "audio_channels" );
                *samples = mlt_properties_get_int( properties, "audio_samples" );
-               if ( this->convert_audio )
-                       this->convert_audio( this, buffer, format, requested_format );
+               if ( self->convert_audio && *buffer && requested_format != mlt_audio_none )
+                       self->convert_audio( self, buffer, format, requested_format );
        }
        else
        {
@@ -621,6 +751,8 @@ int mlt_frame_get_audio( mlt_frame this, void **buffer, mlt_audio_format *format
                        case mlt_audio_float:
                                size = *samples * *channels * sizeof( float );
                                break;
+                       default:
+                               break;
                }
                if ( size )
                        *buffer = mlt_pool_alloc( size );
@@ -659,7 +791,7 @@ int mlt_frame_get_audio( mlt_frame this, void **buffer, mlt_audio_format *format
 /** Set the audio on a frame.
  *
  * \public \memberof mlt_frame_s
- * \param this a frame
+ * \param self a frame
  * \param buffer an buffer containing audio samples
  * \param format the format of the audio in the \p buffer
  * \param size the total size of the buffer (optional)
@@ -667,10 +799,10 @@ int mlt_frame_get_audio( mlt_frame this, void **buffer, mlt_audio_format *format
  * \return true if error
  */
 
-int mlt_frame_set_audio( mlt_frame this, void *buffer, mlt_audio_format format, int size, mlt_destructor destructor )
+int mlt_frame_set_audio( mlt_frame self, void *buffer, mlt_audio_format format, int size, mlt_destructor destructor )
 {
-       mlt_properties_set_int( MLT_FRAME_PROPERTIES( this ), "audio_format", format );
-       return mlt_properties_set_data( MLT_FRAME_PROPERTIES( this ), "audio", buffer, size, destructor, NULL );
+       mlt_properties_set_int( MLT_FRAME_PROPERTIES( self ), "audio_format", format );
+       return mlt_properties_set_data( MLT_FRAME_PROPERTIES( self ), "audio", buffer, size, destructor, NULL );
 }
 
 /** Get audio on a frame as a waveform image.
@@ -681,38 +813,42 @@ int mlt_frame_set_audio( mlt_frame this, void *buffer, mlt_audio_format format,
  * value with \p mlt_pool_release.
  *
  * \public \memberof mlt_frame_s
- * \param this a frame
+ * \param self a frame
  * \param w the width of the image
  * \param h the height of the image to create
  * \return a pointer to a new bitmap
  */
 
-unsigned char *mlt_frame_get_waveform( mlt_frame this, int w, int h )
+unsigned char *mlt_frame_get_waveform( mlt_frame self, int w, int h )
 {
        int16_t *pcm = NULL;
-       mlt_properties properties = MLT_FRAME_PROPERTIES( this );
+       mlt_properties properties = MLT_FRAME_PROPERTIES( self );
        mlt_audio_format format = mlt_audio_s16;
        int frequency = 16000;
        int channels = 2;
-       mlt_producer producer = mlt_frame_get_original_producer( this );
+       mlt_producer producer = mlt_frame_get_original_producer( self );
        double fps = mlt_producer_get_fps( mlt_producer_cut_parent( producer ) );
-       int samples = mlt_sample_calculator( fps, frequency, mlt_frame_get_position( this ) );
+       int samples = mlt_sample_calculator( fps, frequency, mlt_frame_get_position( self ) );
 
        // Increase audio resolution proportional to requested image size
        while ( samples < w )
        {
                frequency += 16000;
-               samples = mlt_sample_calculator( fps, frequency, mlt_frame_get_position( this ) );
+               samples = mlt_sample_calculator( fps, frequency, mlt_frame_get_position( self ) );
        }
 
        // Get the pcm data
-       mlt_frame_get_audio( this, (void**)&pcm, &format, &frequency, &channels, &samples );
+       mlt_frame_get_audio( self, (void**)&pcm, &format, &frequency, &channels, &samples );
 
        // Make an 8-bit buffer large enough to hold rendering
        int size = w * h;
+       if ( size <= 0 )
+               return NULL;
        unsigned char *bitmap = ( unsigned char* )mlt_pool_alloc( size );
        if ( bitmap != NULL )
                memset( bitmap, 0, size );
+       else
+               return NULL;
        mlt_properties_set_data( properties, "waveform", bitmap, size, ( mlt_destructor )mlt_pool_release, NULL );
 
        // Render vertical lines
@@ -750,40 +886,40 @@ unsigned char *mlt_frame_get_waveform( mlt_frame this, int w, int h )
        return bitmap;
 }
 
-/** Get the end service that produced this frame.
+/** Get the end service that produced self frame.
  *
  * This fetches the first producer of the frame and not any producers that
  * encapsulate it.
  *
  * \public \memberof mlt_frame_s
- * \param this a frame
+ * \param self a frame
  * \return a producer
  */
 
-mlt_producer mlt_frame_get_original_producer( mlt_frame this )
+mlt_producer mlt_frame_get_original_producer( mlt_frame self )
 {
-       if ( this != NULL )
-               return mlt_properties_get_data( MLT_FRAME_PROPERTIES( this ), "_producer", NULL );
+       if ( self != NULL )
+               return mlt_properties_get_data( MLT_FRAME_PROPERTIES( self ), "_producer", NULL );
        return NULL;
 }
 
 /** Destroy the frame.
  *
  * \public \memberof mlt_frame_s
- * \param this a frame
+ * \param self a frame
  */
 
-void mlt_frame_close( mlt_frame this )
+void mlt_frame_close( mlt_frame self )
 {
-       if ( this != NULL && mlt_properties_dec_ref( MLT_FRAME_PROPERTIES( this ) ) <= 0 )
+       if ( self != NULL && mlt_properties_dec_ref( MLT_FRAME_PROPERTIES( self ) ) <= 0 )
        {
-               mlt_deque_close( this->stack_image );
-               mlt_deque_close( this->stack_audio );
-               while( mlt_deque_peek_back( this->stack_service ) )
-                       mlt_service_close( mlt_deque_pop_back( this->stack_service ) );
-               mlt_deque_close( this->stack_service );
-               mlt_properties_close( &this->parent );
-               free( this );
+               mlt_deque_close( self->stack_image );
+               mlt_deque_close( self->stack_audio );
+               while( mlt_deque_peek_back( self->stack_service ) )
+                       mlt_service_close( mlt_deque_pop_back( self->stack_service ) );
+               mlt_deque_close( self->stack_service );
+               mlt_properties_close( &self->parent );
+               free( self );
        }
 }
 
@@ -833,3 +969,141 @@ int64_t mlt_sample_calculator_to_now( float fps, int frequency, int64_t position
 
        return samples;
 }
+
+void mlt_frame_write_ppm( mlt_frame frame )
+{
+       int width = 0;
+       int height = 0;
+       mlt_image_format format = mlt_image_rgb24;
+       uint8_t *image;
+       
+       if ( mlt_frame_get_image( frame, &image, &format, &width, &height, 0 ) == 0 )
+       {
+               FILE *file;
+               char filename[16];
+               
+               sprintf( filename, "frame-%05d.ppm", (int)mlt_frame_get_position( frame ) );
+               file = fopen( filename, "wb" );
+               if ( !file )
+                       return;
+               fprintf( file, "P6\n%d %d\n255\n", width, height);
+               fwrite( image, width * height * 3, 1, file );
+               fclose( file );
+       }
+}
+
+/** Get or create a properties object unique to this service instance.
+ *
+ * Use this function to hold a service's processing parameters for this
+ * particular frame. Set the parameters in the service's process function.
+ * Then, get the parameters in the function it pushes to the frame's audio
+ * or image stack. This makes the service more parallel by reducing race
+ * conditions and less sensitive to multiple instances (by not setting a
+ * non-unique property on the frame). Creation and destruction of the
+ * properties object is handled automatically.
+ *
+ * \public \memberof mlt_frame_s
+ * \param self a frame
+ * \param service a service
+ * \return a properties object
+ */
+
+mlt_properties mlt_frame_unique_properties( mlt_frame self, mlt_service service )
+{
+       mlt_properties frame_props = MLT_FRAME_PROPERTIES( self );
+       mlt_properties service_props = MLT_SERVICE_PROPERTIES( service );
+       char *unique = mlt_properties_get( service_props, "_unique_id" );
+       mlt_properties instance_props = mlt_properties_get_data( frame_props, unique, NULL );
+       
+       if ( !instance_props )
+       {
+               instance_props = mlt_properties_new();
+               mlt_properties_set_data( frame_props, unique, instance_props, 0, (mlt_destructor) mlt_properties_close, NULL );
+       }
+
+       return instance_props;
+}
+
+/** Make a copy of a frame.
+ *
+ * This does not copy the get_image/get_audio processing stacks or any
+ * data properties other than the audio and image.
+ *
+ * \public \memberof mlt_frame_s
+ * \param self the frame to clone
+ * \param is_deep a boolean to indicate whether to make a deep copy of the audio
+ * and video data chunks or to make a shallow copy by pointing to the supplied frame
+ * \return a almost-complete copy of the frame
+ * \todo copy the processing deques
+ */
+
+mlt_frame mlt_frame_clone( mlt_frame self, int is_deep )
+{
+       mlt_frame new_frame = mlt_frame_init( NULL );
+       mlt_properties properties = MLT_FRAME_PROPERTIES( self );
+       mlt_properties new_props = MLT_FRAME_PROPERTIES( new_frame );
+       void *data, *copy;
+       int size;
+
+       mlt_properties_inherit( new_props, properties );
+
+       // Carry over some special data properties for the multi consumer.
+       mlt_properties_set_data( new_props, "_producer",
+               mlt_frame_get_original_producer( self ), 0, NULL, NULL );
+       mlt_properties_set_data( new_props, "movit.convert",
+               mlt_properties_get_data( properties, "movit.convert", NULL), 0, NULL, NULL );
+
+       if ( is_deep )
+       {
+               data = mlt_properties_get_data( properties, "audio", &size );
+               if ( data )
+               {
+                       if ( !size )
+                               size = mlt_audio_format_size( mlt_properties_get_int( properties, "audio_format" ),
+                                       mlt_properties_get_int( properties, "audio_samples" ),
+                                       mlt_properties_get_int( properties, "audio_channels" ) );
+                       copy = mlt_pool_alloc( size );
+                       memcpy( copy, data, size );
+                       mlt_properties_set_data( new_props, "audio", copy, size, mlt_pool_release, NULL );
+               }
+               data = mlt_properties_get_data( properties, "image", &size );
+               if ( data )
+               {
+                       if ( ! size )
+                               size = mlt_image_format_size( mlt_properties_get_int( properties, "format" ),
+                                       mlt_properties_get_int( properties, "width" ),
+                                       mlt_properties_get_int( properties, "height" ), NULL );
+                       copy = mlt_pool_alloc( size );
+                       memcpy( copy, data, size );
+                       mlt_properties_set_data( new_props, "image", copy, size, mlt_pool_release, NULL );
+
+                       data = mlt_properties_get_data( properties, "alpha", &size );
+                       if ( data )
+                       {
+                               if ( ! size )
+                                       size = mlt_properties_get_int( properties, "width" ) *
+                                               mlt_properties_get_int( properties, "height" );
+                               copy = mlt_pool_alloc( size );
+                               memcpy( copy, data, size );
+                               mlt_properties_set_data( new_props, "alpha", copy, size, mlt_pool_release, NULL );
+                       };
+               }
+       }
+       else
+       {
+               // This frame takes a reference on the original frame since the data is a shallow copy.
+               mlt_properties_inc_ref( properties );
+               mlt_properties_set_data( new_props, "_cloned_frame", self, 0,
+                       (mlt_destructor) mlt_frame_close, NULL );
+
+               // Copy properties
+               data = mlt_properties_get_data( properties, "audio", &size );
+               mlt_properties_set_data( new_props, "audio", data, size, NULL, NULL );
+               data = mlt_properties_get_data( properties, "image", &size );
+               mlt_properties_set_data( new_props, "image", data, size, NULL, NULL );
+               data = mlt_properties_get_data( properties, "alpha", &size );
+               mlt_properties_set_data( new_props, "alpha", data, size, NULL, NULL );
+       }
+
+       return new_frame;
+}