]> git.sesse.net Git - mlt/blobdiff - src/modules/core/transition_region.c
A little debugging.
[mlt] / src / modules / core / transition_region.c
index 120382e1bea5dc6beb1e01465f79e6baba547e68..0c2e59727b47e752f109c07d8d404ec5894a2dbb 100644 (file)
@@ -3,19 +3,19 @@
  * Copyright (C) 2003-2004 Ushodaya Enterprises Limited
  * Author: Charles Yates <charles.yates@pandora.be>
  *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
  *
- * This program is distributed in the hope that it will be useful,
+ * This library is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
  *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software Foundation,
- * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  */
 
 #include "transition_region.h"
@@ -27,7 +27,7 @@
 #include <stdlib.h>
 #include <string.h>
 
-static int create_instance( mlt_transition this, char *name, char *value, int count )
+static int create_instance( mlt_transition transition, char *name, char *value, int count )
 {
        // Return from this function
        int error = 0;
@@ -46,13 +46,15 @@ static int create_instance( mlt_transition this, char *name, char *value, int co
                *arg ++ = '\0';
 
        // Create the filter
-       filter = mlt_factory_filter( type, arg );
+       mlt_profile profile = mlt_service_profile( MLT_TRANSITION_SERVICE( transition ) );
+       if ( type )
+               filter = mlt_factory_filter( profile, type, arg );
 
        // If we have a filter, then initialise and store it
        if ( filter != NULL )
        {
-               // Properties of this
-               mlt_properties properties = MLT_TRANSITION_PROPERTIES( this );
+               // Properties of transition
+               mlt_properties properties = MLT_TRANSITION_PROPERTIES( transition );
 
                // String to hold the property name
                char id[ 256 ];
@@ -67,11 +69,12 @@ static int create_instance( mlt_transition this, char *name, char *value, int co
                sprintf( key, "%s.", name );
 
                // Just in case, let's assume that the filter here has a composite
-               mlt_properties_set( MLT_FILTER_PROPERTIES( filter ), "composite.start", "0%,0%:100%x100%" );
-               mlt_properties_set( MLT_FILTER_PROPERTIES( filter ), "composite.fill", "true" );
+               //mlt_properties_set( MLT_FILTER_PROPERTIES( filter ), "composite.geometry", "0%/0%:100%x100%" );
+               //mlt_properties_set_int( MLT_FILTER_PROPERTIES( filter ), "composite.fill", 1 );
 
                // Pass all the key properties on the filter down
                mlt_properties_pass( MLT_FILTER_PROPERTIES( filter ), properties, key );
+               mlt_properties_pass_list( MLT_FILTER_PROPERTIES( filter ), properties, "in, out, length" );
 
                // Ensure that filter is assigned
                mlt_properties_set_data( properties, id, filter, 0, ( mlt_destructor )mlt_filter_close, NULL );
@@ -89,44 +92,48 @@ static int create_instance( mlt_transition this, char *name, char *value, int co
        return error;
 }
 
-static uint8_t *filter_get_alpha_mask( mlt_frame this )
+static uint8_t *filter_get_alpha_mask( mlt_frame frame )
 {
        uint8_t *alpha = NULL;
 
        // Obtain properties of frame
-       mlt_properties properties = MLT_FRAME_PROPERTIES( this );
+       mlt_properties properties = MLT_FRAME_PROPERTIES( frame );
 
        // Get the shape frame
        mlt_frame shape_frame = mlt_properties_get_data( properties, "shape_frame", NULL );
 
        // Get the width and height of the image
-       int region_width = mlt_properties_get_int( MLT_FRAME_PROPERTIES( this ), "width" );
-       int region_height = mlt_properties_get_int( MLT_FRAME_PROPERTIES( this ), "height" );
+       int region_width = mlt_properties_get_int( properties, "width" );
+       int region_height = mlt_properties_get_int( properties, "height" );
        uint8_t *image = NULL;
        mlt_image_format format = mlt_image_yuv422;
                                        
        // Get the shape image to trigger alpha creation
-       mlt_properties_set( MLT_FRAME_PROPERTIES( shape_frame ), "distort", "true" );
+       mlt_properties_set_int( MLT_FRAME_PROPERTIES( shape_frame ), "distort", 1 );
        mlt_frame_get_image( shape_frame, &image, &format, &region_width, &region_height, 0 );
 
        alpha = mlt_frame_get_alpha_mask( shape_frame );
 
+       int size = region_width * region_height;
+       uint8_t *alpha_duplicate = mlt_pool_alloc( size );
+
        // Generate from the Y component of the image if no alpha available
        if ( alpha == NULL )
        {
-               int size = region_width * region_height;
-               uint8_t *p = mlt_pool_alloc( size );
-               alpha = p;
+               alpha = alpha_duplicate;
                while ( size -- )
                {
-                       *p ++ = *image ++;
+                       *alpha ++ = ( int )( ( ( *image ++ - 16 ) * 299 ) / 255 );
                        image ++;
                }
-               mlt_properties_set_data( MLT_FRAME_PROPERTIES( shape_frame ), "alpha", alpha, 
-                                                                region_width * region_height, mlt_pool_release, NULL );
        }
+       else
+       {
+               memcpy( alpha_duplicate, alpha, size );
+       }
+       mlt_frame_set_alpha( frame, alpha_duplicate, region_width * region_height, mlt_pool_release );
 
-       return alpha;
+       return alpha_duplicate;
 }
 
 /** Do it :-).
@@ -141,10 +148,15 @@ static int transition_get_image( mlt_frame frame, uint8_t **image, mlt_image_for
        mlt_frame b_frame = mlt_frame_pop_frame( frame );
 
        // Get the watermark transition object
-       mlt_transition this = mlt_frame_pop_service( frame );
+       mlt_transition transition = mlt_frame_pop_service( frame );
 
        // Get the properties of the transition
-       mlt_properties properties = MLT_TRANSITION_PROPERTIES( this );
+       mlt_properties properties = MLT_TRANSITION_PROPERTIES( transition );
+
+       // Get the properties of the a frame
+       mlt_properties a_props = MLT_FRAME_PROPERTIES( frame );
+
+       mlt_service_lock( MLT_TRANSITION_SERVICE( transition ) );
 
        // Get the composite from the transition
        mlt_transition composite = mlt_properties_get_data( properties, "composite", NULL );
@@ -152,17 +164,15 @@ static int transition_get_image( mlt_frame frame, uint8_t **image, mlt_image_for
        // Look for the first filter
        mlt_filter filter = mlt_properties_get_data( properties, "_filter_0", NULL );
 
-       // Get the unique id of the filter (used to reacquire the producer position)
-       char *name = mlt_properties_get( properties, "_unique_id" );
-
-       // Get the original producer position
-       mlt_position position = mlt_properties_get_position( MLT_FRAME_PROPERTIES( frame ), name );
+       // Get the position
+       mlt_position position = mlt_transition_get_position( transition, frame );
 
        // Create a composite if we don't have one
        if ( composite == NULL )
        {
                // Create composite via the factory
-               composite = mlt_factory_transition( "composite", NULL );
+               mlt_profile profile = mlt_service_profile( MLT_TRANSITION_SERVICE( transition ) );
+               composite = mlt_factory_transition( profile, "composite", NULL );
 
                // If we have one
                if ( composite != NULL )
@@ -171,8 +181,8 @@ static int transition_get_image( mlt_frame frame, uint8_t **image, mlt_image_for
                        mlt_properties composite_properties = MLT_TRANSITION_PROPERTIES( composite );
 
                        // We want to ensure that we don't get a wobble...
-                       mlt_properties_set( composite_properties, "distort", "true" );
-                       mlt_properties_set( composite_properties, "progressive", "1" );
+                       //mlt_properties_set_int( composite_properties, "distort", 1 );
+                       mlt_properties_set_int( composite_properties, "progressive", 1 );
 
                        // Pass all the composite. properties on the transition down
                        mlt_properties_pass( composite_properties, properties, "composite." );
@@ -210,10 +220,13 @@ static int transition_get_image( mlt_frame frame, uint8_t **image, mlt_image_for
                                char *value = mlt_properties_get_value( properties, i );
 
                                // Create an instance
-                               if ( create_instance( this, name, value, count ) == 0 )
+                               if ( create_instance( transition, name, value, count ) == 0 )
                                        count ++;
                        }
                }
+       
+               // Look for the first filter again
+               filter = mlt_properties_get_data( properties, "_filter_0", NULL );
        }
        else
        {
@@ -255,14 +268,14 @@ static int transition_get_image( mlt_frame frame, uint8_t **image, mlt_image_for
                }
        }
 
-       // Get the image
-       error = mlt_frame_get_image( frame, image, format, width, height, 1 );
+       mlt_properties_set_int( a_props, "width", *width );
+       mlt_properties_set_int( a_props, "height", *height );
 
        // Only continue if we have both filter and composite
        if ( composite != NULL )
        {
                // Get the resource of this filter (could be a shape [rectangle/circle] or an alpha provider of choice
-               char *resource =  mlt_properties_get( properties, "resource" );
+               const char *resource =  mlt_properties_get( properties, "resource" );
 
                // Get the old resource in case it's changed
                char *old_resource =  mlt_properties_get( properties, "_old_resource" );
@@ -273,18 +286,28 @@ static int transition_get_image( mlt_frame frame, uint8_t **image, mlt_image_for
                // Index to hold the count
                int i = 0;
 
-               // We will get the 'b frame' from the composite only if it's NULL
+               // We will get the 'b frame' from the composite only if it's NULL (region filter)
                if ( b_frame == NULL )
                {
                        // Copy the region
                        b_frame = composite_copy_region( composite, frame, position );
 
                        // Ensure a destructor
-                       mlt_properties_set_data( MLT_FRAME_PROPERTIES( frame ), name, b_frame, 0, ( mlt_destructor )mlt_frame_close, NULL );
+                       char *name = mlt_properties_get( properties, "_unique_id" );
+                       mlt_properties_set_data( a_props, name, b_frame, 0, ( mlt_destructor )mlt_frame_close, NULL );
                }
 
-               // Set the position of the b_frame
-               mlt_frame_set_position( b_frame, position );
+               // Properties of the B frame
+               mlt_properties b_props = MLT_FRAME_PROPERTIES( b_frame );
+
+               // filter_only prevents copying the alpha channel of the shape to the output frame
+               // by compositing filtered frame over itself
+               if ( mlt_properties_get_int( properties, "filter_only" ) )
+               {
+                       char *name = mlt_properties_get( properties, "_unique_id" );
+                       frame = composite_copy_region( composite, b_frame, position );
+                       mlt_properties_set_data( b_props, name, frame, 0, ( mlt_destructor )mlt_frame_close, NULL );
+               }
 
                // Make sure the filter is in the correct position
                while ( filter != NULL )
@@ -328,16 +351,11 @@ static int transition_get_image( mlt_frame frame, uint8_t **image, mlt_image_for
 
                                // Special case circle resource
                                if ( strcmp( resource, "circle" ) == 0 )
-                               {
-                                       // Special case to ensure that fezzik produces a pixbuf with a NULL constructor
-                                       resource = "pixbuf";
+                                       resource = "pixbuf:<svg width='100' height='100'><circle cx='50' cy='50' r='50' fill='black'/></svg>";
 
-                                       // Specify the svg circle
-                                       mlt_properties_set( properties, "producer.resource", "<svg width='100' height='100'><circle cx='50' cy='50' r='50' fill='black'/></svg>" );
-                               }
-
-                               // Create the producer 
-                               producer = mlt_factory_producer( factory, resource );
+                               // Create the producer
+                               mlt_profile profile = mlt_service_profile( MLT_TRANSITION_SERVICE( transition ) );
+                               producer = mlt_factory_producer( profile, factory, resource );
 
                                // If we have one
                                if ( producer != NULL )
@@ -369,7 +387,7 @@ static int transition_get_image( mlt_frame frame, uint8_t **image, mlt_image_for
                                if ( mlt_service_get_frame( MLT_PRODUCER_SERVICE( producer ), &shape_frame, 0 ) == 0 )
                                {
                                        // Ensure that the shape frame will be closed
-                                       mlt_properties_set_data( MLT_FRAME_PROPERTIES( b_frame ), "shape_frame", shape_frame, 0, ( mlt_destructor )mlt_frame_close, NULL );
+                                       mlt_properties_set_data( b_props, "shape_frame", shape_frame, 0, ( mlt_destructor )mlt_frame_close, NULL );
 
                                        // Specify the callback for evaluation
                                        b_frame->get_alpha_mask = filter_get_alpha_mask;
@@ -381,25 +399,18 @@ static int transition_get_image( mlt_frame frame, uint8_t **image, mlt_image_for
                error = mlt_frame_get_image( frame, image, format, width, height, 0 );
        }
 
+       mlt_service_unlock( MLT_TRANSITION_SERVICE( transition ) );
+
        return error;
 }
 
 /** Filter processing.
 */
 
-static mlt_frame transition_process( mlt_transition this, mlt_frame a_frame, mlt_frame b_frame )
+static mlt_frame transition_process( mlt_transition transition, mlt_frame a_frame, mlt_frame b_frame )
 {
-       // Get the properties of the frame
-       mlt_properties properties = MLT_FRAME_PROPERTIES( a_frame );
-
-       // Get a unique name to store the frame position
-       char *name = mlt_properties_get( MLT_TRANSITION_PROPERTIES( this ), "_unique_id" );
-
-       // Assign the current position to the name
-       mlt_properties_set_position( properties, name, mlt_frame_get_position( a_frame ) );
-
        // Push the transition on to the frame
-       mlt_frame_push_service( a_frame, this );
+       mlt_frame_push_service( a_frame, transition );
 
        // Push the b_frame on to the stack
        mlt_frame_push_frame( a_frame, b_frame );
@@ -414,28 +425,31 @@ static mlt_frame transition_process( mlt_transition this, mlt_frame a_frame, mlt
 /** Constructor for the transition.
 */
 
-mlt_transition transition_region_init( void *arg )
+mlt_transition transition_region_init( mlt_profile profile, mlt_service_type type, const char *id, char *arg )
 {
        // Create a new transition
-       mlt_transition this = mlt_transition_new( );
+       mlt_transition transition = mlt_transition_new( );
 
        // Further initialisation
-       if ( this != NULL )
+       if ( transition != NULL )
        {
                // Get the properties from the transition
-               mlt_properties properties = MLT_TRANSITION_PROPERTIES( this );
+               mlt_properties properties = MLT_TRANSITION_PROPERTIES( transition );
 
                // Assign the transition process method
-               this->process = transition_process;
+               transition->process = transition_process;
 
                // Default factory
-               mlt_properties_set( properties, "factory", "fezzik" );
+               mlt_properties_set( properties, "factory", mlt_environment( "MLT_PRODUCER" ) );
                
                // Resource defines the shape of the region
                mlt_properties_set( properties, "resource", arg == NULL ? "rectangle" : arg );
+
+               // Inform apps and framework that this is a video only transition
+               mlt_properties_set_int( properties, "_transition_type", 1 );
        }
 
        // Return the transition
-       return this;
+       return transition;
 }